glibc_check.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. objdump = "objdump -T %s"
  12. glibc_re = re.compile(r"GLIBC_([0-9]\.[0-9]+)")
  13. def parse_version(v):
  14. major, minor = v.split(".")
  15. return int(major), int(minor)
  16. def format_version(version):
  17. return "%d.%d" % version
  18. def main():
  19. given = parse_version(sys.argv[1])
  20. filenames = sys.argv[2:]
  21. overall_versions = set()
  22. for filename in filenames:
  23. try:
  24. output = subprocess.check_output(objdump % filename, shell=True, stderr=subprocess.STDOUT)
  25. output = output.decode()
  26. versions = {parse_version(match.group(1)) for match in glibc_re.finditer(output)}
  27. requires_glibc = max(versions)
  28. overall_versions.add(requires_glibc)
  29. if verbose:
  30. print(f"{filename} {format_version(requires_glibc)}")
  31. except subprocess.CalledProcessError:
  32. if verbose:
  33. print("%s errored." % filename)
  34. wanted = max(overall_versions)
  35. ok = given >= wanted
  36. if verbose:
  37. if ok:
  38. print("The binaries work with the given glibc %s." % format_version(given))
  39. else:
  40. print(
  41. "The binaries do not work with the given glibc %s. "
  42. "Minimum is: %s" % (format_version(given), format_version(wanted))
  43. )
  44. return ok
  45. if __name__ == "__main__":
  46. ok = main()
  47. sys.exit(0 if ok else 1)