Files
wii-ipl/tools/decomp-assist/disasm_fn.py
coah f8d83e5ff2 Fork for AI-assisted matching work
Personal fork so AI-assisted decomp work has a home without disturbing
upstream. README explains the fork; AGENTS.md documents the method and
forbids ever opening a PR against koopthekoopa/wii-ipl.

Matching progress: +9 exact functions, 0 regressions, main.dol still
byte-identical (26116613f624061ba99c8d1a299aaa6efa85670d).

iplESMisc gains CheckTmdParentalControl, CheckSafeDeleteTitle,
NumPrivateContents, checkContentsNum and GetValidTicketIndex, plus the
other functions matched in the previous pass.

The load-bearing finding, written up in AGENTS.md: MWCC emits a
translation unit's string literals in source-token order. A missing
function, or an inverted if/else, shifts every later string offset and
breaks functions that were never edited. Fixing that pool ordering lifted
TMDFile::Open from 99.92 to 99.96 without touching Open.

Adds tools/decomp-assist/ with pool_diff.py, ctxdiff.py, disasm_fn.py,
export_pyghidra.py and the Ghidra decompilation dump.
2026-09-19 13:19:34 +00:00

38 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Disassemble a function from an object file with relocations resolved.
Usage: disasm_fn.py <objectfile> <symbol>
"""
import sys
from capstone import CS_ARCH_PPC, CS_MODE_32, CS_MODE_BIG_ENDIAN, Cs
from elftools.elf.elffile import ELFFile
def main():
path, name = sys.argv[1], sys.argv[2]
with open(path, "rb") as f:
e = ELFFile(f)
st = e.get_section_by_name(".symtab")
syms = list(st.iter_symbols())
names = [s.name for s in syms]
target = next(s for s in syms if s.name == name)
off, size = target["st_value"], target["st_size"]
relocs = {}
rt = e.get_section_by_name(".rela.text")
for r in rt.iter_relocations():
relocs[r["r_offset"]] = names[r["r_info_sym"]]
data = e.get_section_by_name(".text").data()[off:off + size]
md = Cs(CS_ARCH_PPC, CS_MODE_32 + CS_MODE_BIG_ENDIAN)
md.detail = True
print(f"{name} size {hex(size)} ({size // 4} insns)")
for i in md.disasm(data, off):
note = ""
if i.address in relocs:
note = " ; " + relocs[i.address]
print(f"{hex(i.address)} {i.mnemonic:<8} {i.op_str}{note}")
if __name__ == "__main__":
main()