Files
JC2_Recomp/tools/find_code_pointers.py
Thomas MartinandClaude Opus 5.5 2d53791ed1 Scaffold the Jade Cocoon 2 (NTSC-U, SLUS-20309) port on the PS2Recomp fork
PS2Recomp submodule (boubou666/PS2Recomp main c0b08a0), the TLOTR-style scripts (extract, tools,
Ghidra headless map of the stripped ELF, analyzer, recomp, runtime build, run, bench lock), the
analyzer TOML (244 SDK stubs, 40 jump tables), the Ghidra function map and an override skeleton.

SLUS_203.09 is one segment of code and data: Ghidra also labels the data, and the first recompile
had 69,751 unhandled instructions, all in it. tools/filter_ghidra_map.py drops the map entries in
the data spans (code is 0x100000-0x4E3E80 and 0x6B2B00-0x6B9310, the static constructors); the
recompile is then clean: 72,296 functions, 0 errors, 9 VU1 microcode fragments.

Boot 1 stopped on the first static constructor (0x6B2B00), which an earlier filter range had
dropped as data; fixed here. The bench lock now announces its takes and releases through a local
hook.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 02:04:20 +01:00

67 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""Lists the data words of the ELF that point into .text at an address the recompiler does not dispatch: vtables,
callback tables and function pointers Ghidra did not turn into functions. A target qualifies when it opens a
function: a stack adjustment (addiu sp, sp, -n) or, for a leaf, a target that follows a return (jr ra + delay slot,
then any alignment nops). With Ghidra's function list, any target outside every function Ghidra found also qualifies:
that code is only reached through pointers, and the recompiler runs such an entry up to the next known function.
Usage: tools/find_code_pointers.py <elf> <generated/register_functions.cpp> [recomp/ghidra/tta_functions.csv]
-> TOML entry_points strings."""
import csv
import re
import struct
import sys
elf_path, register_path = sys.argv[1], sys.argv[2]
functions_path = sys.argv[3] if len(sys.argv) > 3 else None
data = open(elf_path, "rb").read()
shoff, = struct.unpack_from("<I", data, 32)
shentsize, shnum, shstrndx = struct.unpack_from("<HHH", data, 46)
sections = []
for i in range(shnum):
name, stype, flags, addr, off, size = struct.unpack_from("<6I", data, shoff + i * shentsize)
sections.append([name, stype, flags, addr, off, size])
strtab_off = sections[shstrndx][4]
def sname(s):
end = data.index(b"\0", strtab_off + s[0])
return data[strtab_off + s[0]:end].decode()
text = next(s for s in sections if sname(s) == ".text")
text_va, text_off, text_size = text[3], text[4], text[5]
def word(va):
return struct.unpack_from("<I", data, text_off + va - text_va)[0]
registered = set(int(a, 16) for a in re.findall(r"// 0x([0-9a-fA-F]+)\s*$", open(register_path).read(), re.M))
candidates = {}
for s in sections:
if sname(s) not in (".data", ".rodata", ".sdata") or s[1] != 1:
continue
for off in range(0, s[5] - 3, 4):
target, = struct.unpack_from("<I", data, s[4] + off)
if text_va <= target < text_va + text_size and target % 4 == 0 and target not in registered:
candidates.setdefault(target, []).append(s[3] + off)
ghidra = []
if functions_path:
ghidra = [(int(r["Start"], 16), int(r["End"], 16)) for r in csv.DictReader(open(functions_path))]
def uncovered(va):
return bool(ghidra) and word(va) != 0 and not any(start <= va < end for start, end in ghidra)
entries = []
for target, refs in sorted(candidates.items()):
w = word(target)
opens_frame = (w & 0xFFFF0000) == 0x27BD0000 and (w & 0x8000) # addiu sp, sp, -n
# jr ra; delay slot; then the target, or alignment nops and the target (functions are often 16-byte aligned)
back = target - 4
while back - 4 >= text_va and target - back < 16 and word(back) == 0 and word(back - 4) != 0x03E00008:
back -= 4
after_return = back - 4 >= text_va and word(back - 4) == 0x03E00008
if opens_frame or after_return or uncovered(target):
entries.append((target, refs))
for target, refs in entries:
print(f' "0x{target:08X}", # {len(refs)} ref(s), first at 0x{refs[0]:08X}')
print(f"# {len(entries)} entry points from {len(candidates)} undispatched code pointers", file=sys.stderr)