glibc_check.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. """
  3. Check if all given binaries work with the given glibc version.
  4. glibc_check.py 2.11 bin [bin ...]
  5. rc = 0 means "yes", rc = 1 means "no".
  6. """
  7. import re
  8. import subprocess
  9. import sys
  10. verbose = True
  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", "-T", filename], stderr=subprocess.STDOUT)
  24. output = output.decode()
  25. versions = {parse_version(match.group(1)) for match in glibc_re.finditer(output)}
  26. requires_glibc = max(versions)
  27. overall_versions.add(requires_glibc)
  28. if verbose:
  29. print(f"{filename} {format_version(requires_glibc)}")
  30. except subprocess.CalledProcessError:
  31. if verbose:
  32. print("%s errored." % filename)
  33. wanted = max(overall_versions)
  34. ok = given >= wanted
  35. if verbose:
  36. if ok:
  37. print("The binaries work with the given glibc %s." % format_version(given))
  38. else:
  39. print(
  40. "The binaries do not work with the given glibc %s. "
  41. "Minimum is: %s" % (format_version(given), format_version(wanted))
  42. )
  43. return ok
  44. if __name__ == "__main__":
  45. ok = main()
  46. sys.exit(0 if ok else 1)