From d99664a4cb8768aa99d6fa3e3f51bf2ebe73eec3 Mon Sep 17 00:00:00 2001 From: dreamsyntax Date: Fri, 8 Oct 2021 19:18:27 -0700 Subject: [PATCH] add IDA save/load dolphin map scripts for 7.x --- Tools/IDA/{ => IDA 6.x}/LoadDolphinMap.py | 4 +- Tools/IDA/{ => IDA 6.x}/SaveDolphinMap.py | 4 +- Tools/IDA/IDA 7.x/LoadDolphinMap.py | 62 +++++++++++++++++++++++ Tools/IDA/IDA 7.x/SaveDolphinMap.py | 48 ++++++++++++++++++ 4 files changed, 116 insertions(+), 2 deletions(-) rename Tools/IDA/{ => IDA 6.x}/LoadDolphinMap.py (94%) rename Tools/IDA/{ => IDA 6.x}/SaveDolphinMap.py (92%) create mode 100644 Tools/IDA/IDA 7.x/LoadDolphinMap.py create mode 100644 Tools/IDA/IDA 7.x/SaveDolphinMap.py diff --git a/Tools/IDA/LoadDolphinMap.py b/Tools/IDA/IDA 6.x/LoadDolphinMap.py similarity index 94% rename from Tools/IDA/LoadDolphinMap.py rename to Tools/IDA/IDA 6.x/LoadDolphinMap.py index 6e51c19d07..eb4e571adf 100644 --- a/Tools/IDA/LoadDolphinMap.py +++ b/Tools/IDA/IDA 6.x/LoadDolphinMap.py @@ -1,6 +1,6 @@ # Copyright 2018 Dolphin Emulator Project # Licensed under GPLv2+ -# Refer to the license.txt file included. +# Refer to the LICENSES/GPL-2.0-or-later.txt file included. from collections import namedtuple @@ -29,6 +29,8 @@ def ida_main(): import idc filepath = idc.AskFile(0, "*.map", "Load a Dolphin emulator symbol map") + if filepath is None: + return symbol_map = load_dolphin_map(filepath) for symbol in symbol_map: diff --git a/Tools/IDA/SaveDolphinMap.py b/Tools/IDA/IDA 6.x/SaveDolphinMap.py similarity index 92% rename from Tools/IDA/SaveDolphinMap.py rename to Tools/IDA/IDA 6.x/SaveDolphinMap.py index 35abd7db85..3556dc84ab 100644 --- a/Tools/IDA/SaveDolphinMap.py +++ b/Tools/IDA/IDA 6.x/SaveDolphinMap.py @@ -1,6 +1,6 @@ # Copyright 2018 Dolphin Emulator Project # Licensed under GPLv2+ -# Refer to the license.txt file included. +# Refer to the LICENSES/GPL-2.0-or-later.txt file included. from collections import namedtuple @@ -27,6 +27,8 @@ def ida_main(): import idc filepath = idc.AskFile(1, "*.map", "Save a Dolphin emulator symbol map") + if filepath is None: + return text_map = [] data_map = [] for ea, name in idautils.Names(): diff --git a/Tools/IDA/IDA 7.x/LoadDolphinMap.py b/Tools/IDA/IDA 7.x/LoadDolphinMap.py new file mode 100644 index 0000000000..c044ee14f8 --- /dev/null +++ b/Tools/IDA/IDA 7.x/LoadDolphinMap.py @@ -0,0 +1,62 @@ +# Copyright 2021 Dolphin Emulator Project +# Licensed under GPLv2+ +# Refer to the LICENSES/GPL-2.0-or-later.txt file included. + +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 ida_main(): + import idc + + filepath = ida_kernwin.ask_file(0, "*.map", "Load a Dolphin emulator symbol map") + if filepath is None: + return + symbol_map = load_dolphin_map(filepath) + + for symbol in symbol_map: + addr = int(symbol.vaddr, 16) + size = int(symbol.size, 16) + ida_bytes.del_items(addr, size, 0) + if symbol.section in [".init", ".text"]: + idc.create_insn(addr) + success = ida_funcs.add_func( + addr, + idc.BADADDR if not size else (addr+size) + ) + else: + success = ida_bytes.create_data(addr, idc.FF_BYTE, size, 0) + + if not success: + ida_kernwin.msg("Can't apply properties for symbol:" + " {0.vaddr} - {0.name}\n".format(symbol)) + + flags = idc.SN_NOCHECK | idc.SN_PUBLIC + if symbol.name.startswith("zz_"): + flags |= idc.SN_AUTO | idc.SN_WEAK + else: + flags |= idc.SN_NON_AUTO + idc.set_name(addr, symbol.name, flags) + + +if __name__ == "__main__": + ida_main() diff --git a/Tools/IDA/IDA 7.x/SaveDolphinMap.py b/Tools/IDA/IDA 7.x/SaveDolphinMap.py new file mode 100644 index 0000000000..aa398ccd31 --- /dev/null +++ b/Tools/IDA/IDA 7.x/SaveDolphinMap.py @@ -0,0 +1,48 @@ +# Copyright 2021 Dolphin Emulator Project +# Licensed under GPLv2+ +# Refer to the LICENSES/GPL-2.0-or-later.txt file included. + +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 ida_main(): + import idaapi + import idautils + import idc + + filepath = ida_kernwin.ask_file(1, "*.map", "Save a Dolphin emulator symbol map") + if filepath is None: + return + text_map = [] + data_map = [] + for ea, name in idautils.Names(): + f = idaapi.get_func(ea) + if f is not None: + text_map.append( + DolphinSymbol(".text", ea, f.size(), ea, 0, name) + ) + else: + data_map.append( + DolphinSymbol(".data", ea, idc.get_item_size(ea), ea, 0, name) + ) + save_dolphin_map(filepath, text_map, data_map) + + +if __name__ == "__main__": + ida_main()