Tools: Dolphin symbol map ghidra scripts added

This commit is contained in:
Sepalani 2021-03-02 13:06:24 +04:00
parent 1fe0953bd5
commit 2c4585a506
2 changed files with 116 additions and 0 deletions

View file

@ -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()

View file

@ -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()