From 2c4585a50648d73eeaba51f778d605ed73cb037e Mon Sep 17 00:00:00 2001 From: Sepalani Date: Tue, 2 Mar 2021 13:06:24 +0400 Subject: [PATCH] Tools: Dolphin symbol map ghidra scripts added --- Tools/ghidra/LoadDolphinMap.py | 55 ++++++++++++++++++++++++++++++ Tools/ghidra/SaveDolphinMap.py | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 Tools/ghidra/LoadDolphinMap.py create mode 100644 Tools/ghidra/SaveDolphinMap.py diff --git a/Tools/ghidra/LoadDolphinMap.py b/Tools/ghidra/LoadDolphinMap.py new file mode 100644 index 0000000000..a17850612b --- /dev/null +++ b/Tools/ghidra/LoadDolphinMap.py @@ -0,0 +1,55 @@ +# Copyright 2021 Dolphin Emulator Project +# Licensed under GPLv2+ +# Refer to the license.txt file included. + +#@category Dolphin + +from collections import namedtuple + + +DolphinSymbol = namedtuple("DolphinSymbol", [ + "section", "addr", "size", "vaddr", "align", "name" +]) + + +def load_dolphin_map(filepath): + with open(filepath, "r") as f: + section = "" + symbol_map = [] + for line in f.readlines(): + t = line.strip().split(" ", 4) + if len(t) == 3 and t[1] == "section" and t[2] == "layout": + section = t[0] + continue + if not section or len(t) != 5: + continue + symbol_map.append(DolphinSymbol(section, *t)) + return symbol_map + + +def ghidra_main(): + from ghidra.program.model.symbol import SourceType + from ghidra.program.model.symbol import SymbolUtilities + + f = askFile("Load a Dolphin emulator symbol map", "Load") + symbol_map = load_dolphin_map(f.getPath()) + + for symbol in symbol_map: + addr = toAddr(int(symbol.vaddr, 16)) + size = int(symbol.size, 16) + name = SymbolUtilities.replaceInvalidChars(symbol.name, True) + if symbol.section in [".init", ".text"]: + createFunction(addr, symbol.name); + success = getFunctionAt(addr) is not None + if not success: + print("Can't apply properties for symbol:" + " {0.vaddr} - {0.name}\n".format(symbol)) + createLabel(addr, name, True) + else: + getFunctionAt(addr).setName(name, SourceType.USER_DEFINED) + else: + createLabel(addr, name, True) + + +if __name__ == "__main__": + ghidra_main() diff --git a/Tools/ghidra/SaveDolphinMap.py b/Tools/ghidra/SaveDolphinMap.py new file mode 100644 index 0000000000..307cc0d36d --- /dev/null +++ b/Tools/ghidra/SaveDolphinMap.py @@ -0,0 +1,61 @@ +# Copyright 2021 Dolphin Emulator Project +# Licensed under GPLv2+ +# Refer to the license.txt file included. + +#@category Dolphin + +from collections import namedtuple + + +DolphinSymbol = namedtuple("DolphinSymbol", [ + "section", "addr", "size", "vaddr", "align", "name" +]) + + +def save_dolphin_map(filepath, text_map, data_map): + line = "{0.addr:08x} {0.size:08x} {0.vaddr:08x} {0.align} {0.name}\n" + with open(filepath, "w") as f: + f.write(".text section layout\n") + for symbol in text_map: + f.write(line.format(symbol)) + f.write("\n.data section layout\n") + for symbol in data_map: + f.write(line.format(symbol)) + + +def ghidra_main(): + f = askFile("Save a Dolphin emulator symbol map", "Save") + + text_map = [] + for function in currentProgram.getListing().getFunctions(True): + ea = int(function.getEntryPoint().toString(), 16) + size = function.getBody().getNumAddresses() + name = function.getName() + "({})".format( + ", ".join( + "{} {}".format(p.getDataType(), p.getName()) + for p in function.getParameters() + ) + ) + text_map.append( + DolphinSymbol(".text", ea, size, ea, 0, name) + ) + + data_map = [] + for data in currentProgram.getListing().getDefinedData(True): + try: + ea = int(data.getAddress().toString(), 16) + size = data.getLength() + name = data.getPathName() + if name.startswith("DAT_") and \ + data.getDataType().getName() not in ["string", "unicode"]: + continue + data_map.append( + DolphinSymbol(".data", ea, size, ea, 0, name) + ) + except: + pass + save_dolphin_map(f.getPath(), text_map, data_map) + + +if __name__ == "__main__": + ghidra_main()