#!/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 [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("= 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)