2
0

undefsym.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python3
  2. # Before a shared module's DSO is produced, a static library is built for it
  3. # and passed to this script. The script generates -Wl,-u options to force
  4. # the inclusion of symbol from libqemuutil.a if the shared modules need them,
  5. # This is necessary because the modules may use functions not needed by the
  6. # executable itself, which would cause the function to not be linked in.
  7. # Then the DSO loading would fail because of the missing symbol.
  8. import sys
  9. import subprocess
  10. def filter_lines_set(stdout, from_staticlib):
  11. linesSet = set()
  12. for line in stdout.splitlines():
  13. tokens = line.split(b' ')
  14. if len(tokens) >= 2:
  15. if from_staticlib and tokens[1] == b'U':
  16. continue
  17. if not from_staticlib and tokens[1] != b'U':
  18. continue
  19. new_line = b'-Wl,-u,' + tokens[0]
  20. if not new_line in linesSet:
  21. linesSet.add(new_line)
  22. return linesSet
  23. def main(args):
  24. if len(args) <= 3:
  25. sys.exit(0)
  26. nm = args[1]
  27. staticlib = args[2]
  28. pc = subprocess.run([nm, "-P", "-g", staticlib], stdout=subprocess.PIPE)
  29. if pc.returncode != 0:
  30. sys.exit(1)
  31. staticlib_syms = filter_lines_set(pc.stdout, True)
  32. shared_modules = args[3:]
  33. pc = subprocess.run([nm, "-P", "-g"] + shared_modules, stdout=subprocess.PIPE)
  34. if pc.returncode != 0:
  35. sys.exit(1)
  36. modules_undef_syms = filter_lines_set(pc.stdout, False)
  37. lines = sorted(staticlib_syms.intersection(modules_undef_syms))
  38. sys.stdout.buffer.write(b'\n'.join(lines))
  39. if __name__ == "__main__":
  40. main(sys.argv)