glibc_check.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. """
  3. Check if all given binaries work with the given glibc version.
  4. check_glibc.py 2.11 bin [bin ...]
  5. """
  6. import re
  7. import subprocess
  8. import sys
  9. verbose = True
  10. objdump = "objdump -T %s"
  11. glibc_re = re.compile(r'GLIBC_([0-9]\.[0-9]+)')
  12. def parse_version(v):
  13. major, minor = v.split('.')
  14. return int(major), int(minor)
  15. def format_version(version):
  16. return "%d.%d" % version
  17. def main():
  18. given = parse_version(sys.argv[1])
  19. filenames = sys.argv[2:]
  20. overall_versions = set()
  21. for filename in filenames:
  22. try:
  23. output = subprocess.check_output(objdump % filename, shell=True,
  24. stderr=subprocess.STDOUT)
  25. output = output.decode('utf-8')
  26. versions = set(parse_version(match.group(1))
  27. for match in glibc_re.finditer(output))
  28. requires_glibc = max(versions)
  29. overall_versions.add(requires_glibc)
  30. if verbose:
  31. print("%s %s" % (filename, format_version(requires_glibc)))
  32. except subprocess.CalledProcessError as e:
  33. if verbose:
  34. print("%s errored." % filename)
  35. wanted = max(overall_versions)
  36. ok = given >= wanted
  37. if verbose:
  38. if ok:
  39. print("The binaries work with the given glibc %s." %
  40. format_version(given))
  41. else:
  42. print("The binaries do not work with the given glibc %s. "
  43. "Minimum is: %s" % (format_version(given), format_version(wanted)))
  44. return ok
  45. if __name__ == '__main__':
  46. ok = main()
  47. sys.exit(0 if ok else 1)