diff --git a/tests/exefs_plugins_test.py b/tests/exefs_plugins_test.py index c2c3caa5..e0a72d51 100644 --- a/tests/exefs_plugins_test.py +++ b/tests/exefs_plugins_test.py @@ -292,6 +292,15 @@ class Check(unittest.TestCase): self.assertEqual(reasons, []) self.assertEqual(hooks[0]['relocated'], '0x37000040 +5 words') + def test_trampoline_backward_unsigned(self): # Challenge: b.eq back 0x2ec; exlaunch's .quad lacks the sign + site, t = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF + form = [0x54000040, 0x14000005, xp.LDR_X17_8, xp.BR_X17] + for target in (site + 0x1FFD14, site - 0x2EC): + words = [*form, *quad(target), b(t + 24, site + 4)] + self.assertEqual(self.reasons(self.long_form(0x54FFE8A0, TRAMP_OFF, words)), [], hex(target)) + self.refused(self.long_form(0x54FFE8A0, TRAMP_OFF, [*form, *quad(site + 0x1FFD18), b(t + 24, site + 4)]), + 'not the original') + def test_trampoline_long_nop(self): # the .quad 8-byte aligned by one nop before the form site, t = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF + 4 form = [0x37000040, 0x14000005, xp.LDR_X17_8, xp.BR_X17, *quad(site + 0x14)] diff --git a/tools/exefs_plugins.py b/tools/exefs_plugins.py index f850ecb2..fefdc05e 100644 --- a/tools/exefs_plugins.py +++ b/tools/exefs_plugins.py @@ -214,7 +214,12 @@ def relocation_end(word, orig, site, at): return literal_end(word, orig, at) p = at + 4 if w0 == NOP else at w = [word(p + 4 * i) for i in range(4)] + targets = {target} if kind in ('b.cond', 'cbz', 'tbz'): + # exlaunch's __fix_cond_comp_test_branch takes the offset field without its sign: a backward target lands + # 2^21 (b.cond, cbz) or 2^16 (tbz) bytes too far. Challenge Mode's trampolines hold such .quads; the generated + # code runs main's own branch instead, to its real target. + targets.add(site + ((orig >> 5 & (0x3FFF if kind == 'tbz' else 0x7FFFF)) << 2)) form, data = pc_relative(w[0], p) == (rel[0], p + 8) and w[1:] == [0x14000005, LDR_X17_8, BR_X17], p + 16 elif kind == 'b' and orig >> 31: form, data = w[:3] == [LDR_X17_12, ADR_X30_16, BR_X17], p + 12 @@ -222,7 +227,7 @@ def relocation_end(word, orig, site, at): form, data = w[:2] == [LDR_X17_8, BR_X17], p + 8 else: # adr, adrp form, data = w[:2] == [0x58000040 | orig & 0x1F, 0x14000003], p + 8 - return data + 8 if form and data % 8 == 0 and quad(word, data) == target else None + return data + 8 if form and data % 8 == 0 and quad(word, data) in targets else None def returns_to(word, at, back):