Debian Codegraph @

Tools/ghidra/LoadDolphinMap.py

raw symbols SCIP
1 # Copyright 2021 Dolphin Emulator Project
2 # Licensed under GPLv2+
3 # Refer to the license.txt file included.
4
5 #@category Dolphin
6
7 from collections import namedtuple
8
9
10 DolphinSymbol = namedtuple("DolphinSymbol", [
11 "section", "addr", "size", "vaddr", "align", "name"
12 ])
13
14
15 def load_dolphin_map(filepath):
16 with open(filepath, "r") as f:
17 section = ""
18 symbol_map = []
19 for line in f.readlines():
20 t = line.strip().split(" ", 4)
21 if len(t) == 3 and t[1] == "section" and t[2] == "layout":
22 section = t[0]
23 continue
24 if not section or len(t) != 5:
25 continue
26 symbol_map.append(DolphinSymbol(section, *t))
27 return symbol_map
28
29
30 def ghidra_main():
31 from ghidra.program.model.symbol import SourceType
32 from ghidra.program.model.symbol import SymbolUtilities
33
34 f = askFile("Load a Dolphin emulator symbol map", "Load")
35 symbol_map = load_dolphin_map(f.getPath())
36
37 for symbol in symbol_map:
38 addr = toAddr(int(symbol.vaddr, 16))
39 size = int(symbol.size, 16)
40 name = SymbolUtilities.replaceInvalidChars(symbol.name, True)
41 if symbol.section in [".init", ".text"]:
42 createFunction(addr, symbol.name);
43 success = getFunctionAt(addr) is not None
44 if not success:
45 print("Can't apply properties for symbol:"
46 " {0.vaddr} - {0.name}\n".format(symbol))
47 createLabel(addr, name, True)
48 else:
49 getFunctionAt(addr).setName(name, SourceType.USER_DEFINED)
50 else:
51 createLabel(addr, name, True)
52
53
54 if __name__ == "__main__":
55 ghidra_main()