SaveDolphinMap.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright 2021 Dolphin Emulator Project
  2. # Licensed under GPLv2+
  3. # Refer to the license.txt file included.
  4. #@category Dolphin
  5. from collections import namedtuple
  6. DolphinSymbol = namedtuple("DolphinSymbol", [
  7. "section", "addr", "size", "vaddr", "align", "name"
  8. ])
  9. def save_dolphin_map(filepath, text_map, data_map):
  10. line = "{0.addr:08x} {0.size:08x} {0.vaddr:08x} {0.align} {0.name}\n"
  11. with open(filepath, "w") as f:
  12. f.write(".text section layout\n")
  13. for symbol in text_map:
  14. f.write(line.format(symbol))
  15. f.write("\n.data section layout\n")
  16. for symbol in data_map:
  17. f.write(line.format(symbol))
  18. def ghidra_main():
  19. f = askFile("Save a Dolphin emulator symbol map", "Save")
  20. text_map = []
  21. for function in currentProgram.getListing().getFunctions(True):
  22. ea = int(function.getEntryPoint().toString(), 16)
  23. size = function.getBody().getNumAddresses()
  24. name = function.getName() + "({})".format(
  25. ", ".join(
  26. "{} {}".format(p.getDataType(), p.getName())
  27. for p in function.getParameters()
  28. )
  29. )
  30. text_map.append(
  31. DolphinSymbol(".text", ea, size, ea, 0, name)
  32. )
  33. data_map = []
  34. for data in currentProgram.getListing().getDefinedData(True):
  35. try:
  36. ea = int(data.getAddress().toString(), 16)
  37. size = data.getLength()
  38. name = data.getPathName()
  39. if name.startswith("DAT_") and \
  40. data.getDataType().getName() not in ["string", "unicode"]:
  41. continue
  42. data_map.append(
  43. DolphinSymbol(".data", ea, size, ea, 0, name)
  44. )
  45. except:
  46. pass
  47. save_dolphin_map(f.getPath(), text_map, data_map)
  48. if __name__ == "__main__":
  49. ghidra_main()