From 05ec8bc220e355a77e3312aeecd619e8bb54ed0e Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Thu, 1 Oct 2026 20:28:48 -0500 Subject: [PATCH] Discover compiler-named and custom jump tables --- tools/decomp-assist/jump_table_diff.py | 27 ++++++++---- tools/decomp-assist/test_jump_table_diff.py | 49 +++++++++++++++++++++ 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/tools/decomp-assist/jump_table_diff.py b/tools/decomp-assist/jump_table_diff.py index 9274eeae..ba4ef0aa 100644 --- a/tools/decomp-assist/jump_table_diff.py +++ b/tools/decomp-assist/jump_table_diff.py @@ -5,9 +5,11 @@ Paths follow ctxdiff.py: build/43U/{src,obj}/.o. Requires pyelftools. Exit 0: all selected tables match (or no tables); 1: mismatch; 2: invalid or ambiguous input. This checks relative destinations, not behavioral equivalence. -Automatic selection covers local jumptable_* symbols whose R_PPC_ADDR32 entries -all target the selected function. External callback tables/vtables are not -covered. Source locations are inferred from corresponding function relocations, +Automatic selection covers jumptable_* symbols and multi-entry data objects +whose R_PPC_ADDR32 entries all target the selected function, with at least one +interior destination for objects lacking the jumptable_ prefix. This includes +compiler-generated @ names and custom names, but excludes ordinary callback +tables/vtables. Source locations are inferred from corresponding function relocations, never from an assumption that both data sections have the same layout. """ @@ -113,7 +115,11 @@ def jump_target(obj, relocation, function): def reference_tables(obj, function, name=None): candidates = [obj.symbol(name)] if name else [ - s for s in obj.symbols if s.name.startswith("jumptable_") and s.size] + s for s in obj.symbols if s.size and ( + s.name.startswith("jumptable_") or ( + s.kind == "STT_OBJECT" and s.size >= 8 + and isinstance(s.section, int) + and obj.sections[s.section][0] in (".data", ".rodata", ".sdata", ".sdata2")))] result = [] for table in candidates: if not isinstance(table.section, int) or table.size % 4: @@ -123,10 +129,15 @@ def reference_tables(obj, function, name=None): section_size = obj.sections[table.section][1] if table.value < 0 or table.value + table.size > section_size: raise ValueError("reference table is outside its section") - targets = [jump_target(obj, obj.relocs(table.section).get(offset), function) - for offset in range(table.value, table.value + table.size, 4)] - if targets and all(target is not None for target in targets): - result.append((table, targets)) + targets = [] + for offset in range(table.value, table.value + table.size, 4): + target = jump_target(obj, obj.relocs(table.section).get(offset), function) + if target is None: + break + targets.append(target) + if targets and len(targets) == table.size // 4: + if name or table.name.startswith("jumptable_") or any(targets): + result.append((table, targets)) elif name: raise ValueError("reference table does not target the selected function") return result diff --git a/tools/decomp-assist/test_jump_table_diff.py b/tools/decomp-assist/test_jump_table_diff.py index 04dd8239..78a0c6f8 100644 --- a/tools/decomp-assist/test_jump_table_diff.py +++ b/tools/decomp-assist/test_jump_table_diff.py @@ -144,6 +144,55 @@ class JumpTableTests(unittest.TestCase): with self.assertRaisesRegex(ValueError, "does not target"): tool.reference_tables(reference, reference.symbol("dispatch"), "jumptable_test") + def test_discovers_compiler_and_custom_names(self): + for name in ("@123", "scCharacterDispatch"): + reference = fixture() + reference.symbols[1] = replace(reference.symbols[1], name=name) + tables = tool.reference_tables(reference, reference.symbol("dispatch")) + self.assertEqual([(t.name, entries) for t, entries in tables], + [(name, [0x10, 0x20, 0x30])]) + + def test_custom_table_can_include_function_entry(self): + reference = fixture((0, 0x10, 0x20), source=True) + tables = tool.reference_tables(reference, reference.symbol("dispatch")) + self.assertEqual(tables[0][1], [0, 0x10, 0x20]) + + def test_function_entry_callback_array_is_not_auto_selected(self): + reference = fixture((0, 0, 0), source=True) + fn = reference.symbol("dispatch") + self.assertEqual(tool.reference_tables(reference, fn), []) + self.assertEqual(tool.reference_tables(reference, fn, "@123")[0][1], + [0, 0, 0]) + + def test_single_pointer_requires_explicit_name(self): + reference = fixture((0x10,), source=True) + fn = reference.symbol("dispatch") + self.assertEqual(tool.reference_tables(reference, fn), []) + self.assertEqual(tool.reference_tables(reference, fn, "@123")[0][1], [0x10]) + + def test_custom_table_requires_data_object(self): + reference = fixture(source=True) + reference.symbols[1] = replace(reference.symbols[1], kind="STT_NOTYPE") + self.assertEqual(tool.reference_tables(reference, reference.symbol("dispatch")), []) + reference.symbols[1] = replace(reference.symbols[1], kind="STT_OBJECT") + reference.sections[2] = (".text", 0x1000) + self.assertEqual(tool.reference_tables(reference, reference.symbol("dispatch")), []) + + def test_custom_table_with_missing_or_external_entry_is_not_selected(self): + for entries in ((0x10, 0x20, 0x80), (0x10, 0x20, 0x30)): + reference = fixture(entries, source=True) + if entries[-1] == 0x30: + del reference.relocations[2][0x48] + self.assertEqual(tool.reference_tables(reference, reference.symbol("dispatch")), []) + + def test_auto_custom_table_still_detects_swapped_entries(self): + reference = fixture(source=True) + source = fixture((0x20, 0x10, 0x30), source=True, table_offset=0x90) + with patch.object(tool, "read_object", side_effect=[source, reference]): + with contextlib.redirect_stdout(io.StringIO()) as output: + self.assertEqual(tool.main(["unused", "dispatch"]), 1) + self.assertIn("@123: 1/3 entries match", output.getvalue()) + def test_cli_exit_codes(self): for source, expected_status in [(fixture(source=True), 0), (fixture((0x20, 0x10, 0x30), source=True), 1)]: -- 2.49.1