LoadDolphinMap.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 load_dolphin_map(filepath):
  10. with open(filepath, "r") as f:
  11. section = ""
  12. symbol_map = []
  13. for line in f.readlines():
  14. t = line.strip().split(" ", 4)
  15. if len(t) == 3 and t[1] == "section" and t[2] == "layout":
  16. section = t[0]
  17. continue
  18. if not section or len(t) != 5:
  19. continue
  20. symbol_map.append(DolphinSymbol(section, *t))
  21. return symbol_map
  22. def ghidra_main():
  23. from ghidra.program.model.symbol import SourceType
  24. from ghidra.program.model.symbol import SymbolUtilities
  25. f = askFile("Load a Dolphin emulator symbol map", "Load")
  26. symbol_map = load_dolphin_map(f.getPath())
  27. for symbol in symbol_map:
  28. addr = toAddr(int(symbol.vaddr, 16))
  29. size = int(symbol.size, 16)
  30. name = SymbolUtilities.replaceInvalidChars(symbol.name, True)
  31. if symbol.section in [".init", ".text"]:
  32. createFunction(addr, symbol.name);
  33. success = getFunctionAt(addr) is not None
  34. if not success:
  35. print("Can't apply properties for symbol:"
  36. " {0.vaddr} - {0.name}\n".format(symbol))
  37. createLabel(addr, name, True)
  38. else:
  39. getFunctionAt(addr).setName(name, SourceType.USER_DEFINED)
  40. else:
  41. createLabel(addr, name, True)
  42. if __name__ == "__main__":
  43. ghidra_main()