diff --git a/tests/exefs_plugins_test.py b/tests/exefs_plugins_test.py index 6a891929..d9587806 100644 --- a/tests/exefs_plugins_test.py +++ b/tests/exefs_plugins_test.py @@ -603,6 +603,22 @@ class Lines(unittest.TestCase): self.assertIn(f'orig mod_x plugin0 {TRAMP_OFF:x} main {SITE_OFF:x}', out) self.assertIn(f'seed plugin0 {THUNK_OFF:x}', out) + def test_stubs_written_back_to_back_are_each_a_start(self): + # Zonai Fieldworks writes an inline-hook stub per member hook at init, back to back; a member switched on at + # play points its gate at one (1.4.3 plugin+0x135048, the 4th). The .quad callback after each stub is data. + rec = record() + img, row = bytearray(rec['images']['plugin0']), 0x8000 + for k in range(3): + at = row + 0x18 * k + struct.pack_into('<4IQ', img, at, xp.STUR_X30, b(PLUGIN + at + 4, PLUGIN + IMPL_OFF, True), xp.LDUR_X30, + b(PLUGIN + at + 12, PLUGIN + TRAMP_OFF), PLUGIN + CB_OFF) + rec['images']['plugin0'] = bytes(img) + pre = bytearray(img) + pre[row:row + 0x48] = bytes(0x48) + rec['pre_images'] = {'plugin0': bytes(pre)} + starts = xp.init_code(rec, [], 'plugin0') + self.assertEqual(starts, {PLUGIN + row, PLUGIN + row + 0x18, PLUGIN + row + 0x30, PLUGIN + CB_OFF}) + def test_hook_on_main_header_keeps_only_its_expect(self): # Challenge Mode's Enemy Health Regeneration on an update its table lacks: a replace hook at main+0x0, whose # word 1 is main's module header. The write is checked at start; the lift gets no site, orig or seed for it. diff --git a/tools/exefs_plugins.py b/tools/exefs_plugins.py index 1fceea2b..0de35e49 100644 --- a/tools/exefs_plugins.py +++ b/tools/exefs_plugins.py @@ -602,7 +602,12 @@ def init_code(rec, hooks, module): trampolines of its far hooks (the moved original words and a branch back). The plugin may point a veneer at any of them at play (Zonai Fieldworks turns its features' hooks on and off), so each is a root of the reach check and a seed of the lift. The trampolines of inline hooks (never run) and the Orig trampolines of replace hooks (the lift - serves them with the function's unpatched copy) are left out: their way back would split main's functions.""" + serves them with the function's unpatched copy) are left out: their way back would split main's functions. + + An exlaunch inline-hook stub the init writes (stur x30; bl impl; ldur x30; b trampoline; .quad callback) may be the + target of a hook the plugin turns on at play: Zonai Fieldworks writes one per member hook at init, back to back, + and a member switched on points its gate's pointer at it (the 4th of a row is plugin+0x135048 on 1.4.3). Each stub + and its callback are starts; the .quad is data, not code running on into the next stub.""" p = {m['name']: m for m in rec['modules']}[module] img, pre = rec['images'].get(module), rec.get('pre_images', {}).get(module) if img is None or pre is None: @@ -611,13 +616,24 @@ def init_code(rec, hooks, module): now, old = struct.unpack_from(f'<{n}I', img), struct.unpack_from(f'<{n}I', pre) known = {p['base'] + int(h['trampoline'].split('+')[1], 16) for h in hooks if (h.get('trampoline') or '').startswith(module + '+')} - out, inside = set(), False # inside: the word before is a written word that goes on to this one - for i in range(n): + text = range(p['base'], p['base'] + p['text_end']) + out, inside, i = set(), False, 0 # inside: the word before is a written word that goes on to this one + while i < n: new = old[i] == 0 and now[i] != 0 + if (new and i + 5 < n and now[i] == STUR_X30 and now[i + 1] & 0xFC000000 == 0x94000000 + and now[i + 2] == LDUR_X30 and now[i + 3] & 0xFC000000 == 0x14000000): + stub = p['base'] + 4 * i + callback = now[i + 4] | now[i + 5] << 32 + out.add(stub) + if callback in text: + out.add(callback) + inside, i = False, i + 6 + continue if new and not inside: out.add(p['base'] + 4 * i) # b, br, ret end a trampoline; one may follow right after inside = new and now[i] & 0xFC000000 != 0x14000000 and now[i] & 0xFFFFFC1F not in (0xD61F0000, 0xD65F0000) + i += 1 return out - known