nm-symbolicate.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. import sys, subprocess, os
  6. def NMSymbolicate(library, addresses):
  7. target_tools_prefix = os.environ.get("TARGET_TOOLS_PREFIX", "")
  8. args = [
  9. target_tools_prefix + "nm", "-D", "-S", library
  10. ]
  11. nm_lines = subprocess.check_output(args).split("\n")
  12. symbol_table = []
  13. for line in nm_lines:
  14. pieces = line.split(" ", 4)
  15. if len(pieces) != 4 or pieces[2] != "T":
  16. continue
  17. start = int(pieces[0], 16)
  18. end = int(pieces[1], 16)
  19. symbol = pieces[3]
  20. symbol_table.append({
  21. "start": int(pieces[0], 16),
  22. "end": int(pieces[0], 16) + int(pieces[1], 16),
  23. "funcName": pieces[3]
  24. });
  25. for addressStr in addresses:
  26. address = int(addressStr, 16)
  27. symbolForAddress = None
  28. for symbol in symbol_table:
  29. if address >= symbol["start"] and address <= symbol["end"]:
  30. symbolForAddress = symbol
  31. break
  32. if symbolForAddress:
  33. print symbolForAddress["funcName"]
  34. else:
  35. print "??" # match addr2line
  36. print ":0" # no line information from nm
  37. if len(sys.argv) > 1:
  38. NMSymbolicate(sys.argv[1], sys.argv[2:])
  39. sys.exit(0)
  40. print "Usage: nm-symbolicate.py <library> <addresses> > merged.sym"