Debian Codegraph @

Tools/ghidra/SaveDolphinMap.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 save_dolphin_map(filepath, text_map, data_map):
16 line = "{0.addr:08x} {0.size:08x} {0.vaddr:08x} {0.align} {0.name}\n"
17 with open(filepath, "w") as f:
18 f.write(".text section layout\n")
19 for symbol in text_map:
20 f.write(line.format(symbol))
21 f.write("\n.data section layout\n")
22 for symbol in data_map:
23 f.write(line.format(symbol))
24
25
26 def ghidra_main():
27 f = askFile("Save a Dolphin emulator symbol map", "Save")
28
29 text_map = []
30 for function in currentProgram.getListing().getFunctions(True):
31 ea = int(function.getEntryPoint().toString(), 16)
32 size = function.getBody().getNumAddresses()
33 name = function.getName() + "({})".format(
34 ", ".join(
35 "{} {}".format(p.getDataType(), p.getName())
36 for p in function.getParameters()
37 )
38 )
39 text_map.append(
40 DolphinSymbol(".text", ea, size, ea, 0, name)
41 )
42
43 data_map = []
44 for data in currentProgram.getListing().getDefinedData(True):
45 try:
46 ea = int(data.getAddress().toString(), 16)
47 size = data.getLength()
48 name = data.getPathName()
49 if name.startswith("DAT_") and \
50 data.getDataType().getName() not in ["string", "unicode"]:
51 continue
52 data_map.append(
53 DolphinSymbol(".data", ea, size, ea, 0, name)
54 )
55 except:
56 pass
57 save_dolphin_map(f.getPath(), text_map, data_map)
58
59
60 if __name__ == "__main__":
61 ghidra_main()