2
0

undefsym.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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) >= 1:
  15. if len(tokens) > 1:
  16. if from_staticlib and tokens[1] == b'U':
  17. continue
  18. if not from_staticlib and tokens[1] != b'U':
  19. continue
  20. new_line = b'-Wl,-u,' + tokens[0]
  21. if not new_line in linesSet:
  22. linesSet.add(new_line)
  23. return linesSet
  24. def main(args):
  25. if len(args) <= 3:
  26. sys.exit(0)
  27. nm = args[1]
  28. staticlib = args[2]
  29. pc = subprocess.run([nm, "-P", "-g", staticlib], stdout=subprocess.PIPE)
  30. if pc.returncode != 0:
  31. sys.exit(1)
  32. staticlib_syms = filter_lines_set(pc.stdout, True)
  33. shared_modules = args[3:]
  34. pc = subprocess.run([nm, "-P", "-g"] + shared_modules, stdout=subprocess.PIPE)
  35. if pc.returncode != 0:
  36. sys.exit(1)
  37. modules_undef_syms = filter_lines_set(pc.stdout, False)
  38. lines = sorted(staticlib_syms.intersection(modules_undef_syms))
  39. sys.stdout.buffer.write(b'\n'.join(lines))
  40. if __name__ == "__main__":
  41. main(sys.argv)