mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-25 22:15:57 +00:00
A plugin built with default visibility calls its own exported functions through its PLT. Challenge Mode's Enemy Health Regeneration plugin (exefs subsdk8) does so for the std::vector<const char*> constructor it carries, and the service run counted that as a library call outside the reviewed list, so the plugin was refused and its whole mod stayed off. runtime/loader.c resolve binds such a JUMP_SLOT straight to the plugin's own definition (a host override still wins and stays counted), and tools/exefs_plugins.py plt_imports maps the PLT stub to that code, so the reach check follows into it instead of naming an import. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ed8fa04034
commit
6f97dca93a
4 files changed
+43
-4
No files matched your search
+3
-1
@@ -128,7 +128,9 @@ The rebuild refuses a plugin when its start-up or the code its redirects reach n
|
||||
system calls other than the few its start-up needs (memory queries and maps, the heap size, the clock, debug output),
|
||||
or a library call the game itself never makes and a reviewed list does not hold (memory and string functions, the heap,
|
||||
text formatting, mutexes, the clock, the system language, the SD card's files), such as network sockets or the module
|
||||
loader. A library call the game makes (threads, input, time, its files) runs on the path the game runs on. A start-up
|
||||
loader. A library call the game makes (threads, input, time, its files) runs on the path the game runs on. A call to a
|
||||
function the plugin itself holds counts as its own code, even when it goes through the plugin's import table (C++
|
||||
library templates such as `std::vector` built into it). A start-up
|
||||
still gets only the system calls above, so it cannot start a thread or open a service; a redirect that is not one of exlaunch's hook shapes
|
||||
(inline hook, replace hook, call-site rewrite, a branch into its code from inside a game function, a pointer in the
|
||||
game's data swapped for one into its code); a write into the game's read-only data or into another module; two runs of
|
||||
|
||||
+7
-1
@@ -397,7 +397,13 @@ static Res resolve(Mod *m, uint32_t si, int jump) {
|
||||
uint64_t guest = sym_defined(m, si) ? m->pub.base + m->syms[si].value : find_symbol(name, NULL);
|
||||
GuestFn f;
|
||||
Res r;
|
||||
if (jump && m->pub.plugin && ldr_plugin_import) { // the plugin service run counts the plugin's calls by name
|
||||
// A plugin's calls out of its own code are counted by name (plugin_init.c plugin_import), so the plugin check can
|
||||
// judge what its init reached. A JUMP_SLOT whose symbol the plugin defines itself is no call out: a plugin built
|
||||
// with default visibility calls its own exported functions through its PLT, such as the std::vector<const char*>
|
||||
// constructor of Challenge Mode's Enemy Health Regeneration (exefs subsdk8), and it binds to that definition as
|
||||
// any other own symbol. A host override of the name still wins and stays counted.
|
||||
int own_code = sym_defined(m, si) && !hostfn_override(name);
|
||||
if (jump && m->pub.plugin && ldr_plugin_import && !own_code) {
|
||||
uint64_t to = (f = hostfn_override(name)) ? hostfn_register(name, f) : guest;
|
||||
if (!to) to = hostfn_register(name, unresolved_stub);
|
||||
r = (Res){hostfn_register_observer(name, ldr_plugin_import, to), RES_OBSERVER};
|
||||
|
||||
@@ -452,6 +452,26 @@ class Reach(unittest.TestCase):
|
||||
svcs, imports, _, _, _ = xp.reach(img, self.BASE, 0x200, [self.BASE], {self.BASE + 0x80: 'malloc'})
|
||||
self.assertEqual((svcs, imports), (set(), {'malloc': 1}))
|
||||
|
||||
def test_own_function_through_plt_is_followed(self):
|
||||
# bl plt; ret. The PLT stub at 0x80 binds to the plugin's own function at 0x100: svc 0x40; ret
|
||||
img = bytearray(code([b(self.BASE, self.BASE + 0x80, True), RET_W]))
|
||||
struct.pack_into('<II', img, 0x100, SVC_40, RET_W)
|
||||
svcs, imports, _, _, _ = xp.reach(bytes(img), self.BASE, 0x200, [self.BASE],
|
||||
{self.BASE + 0x80: self.BASE + 0x100})
|
||||
self.assertEqual((svcs, imports), ({0x40}, {}))
|
||||
|
||||
def test_plt_slot_of_own_symbol_maps_to_its_code(self):
|
||||
# Two PLT stubs (adrp x16, page 0; ldr x17, [x16, #slot]; add x16, x16, #slot; br x17) over GOT slots 0x180
|
||||
# and 0x188: the first binds an import, the second a symbol the plugin defines at +0x40.
|
||||
def stub(slot):
|
||||
return [0x90000010, 0xF9400211 | (slot // 8) << 10, 0x91000210 | slot << 10, 0xD61F0220]
|
||||
img = code(stub(0x180) + stub(0x188))
|
||||
nso = mock.Mock(relocs=[(0x180, xp.R_AARCH64_JUMP_SLOT, 1, 0), (0x188, xp.R_AARCH64_JUMP_SLOT, 2, 0)],
|
||||
syms=[('', 0, 0, 0, 0), ('malloc', 0x12, 0, 0, 0),
|
||||
('_ZNSt6vectorIPKcSaIS1_EEC1ESt16initializer_listIS1_ERKS2_', 0x22, 7, 0x40, 8)])
|
||||
self.assertEqual(xp.plt_imports(nso, img, self.BASE, 0x200),
|
||||
{self.BASE: 'malloc', self.BASE + 0x10: self.BASE + 0x40})
|
||||
|
||||
def test_stub_returns_at_its_trampoline_branch(self):
|
||||
# stur; bl impl; ldur; b trampoline (where an svc sits): the b is the stub's return
|
||||
stub = [xp.STUR_X30, b(self.BASE + 4, self.BASE + 0x40, True), xp.LDUR_X30, b(self.BASE + 12, self.BASE + 0x100)]
|
||||
|
||||
+13
-2
@@ -457,8 +457,15 @@ def check_record(rec, impls, game=frozenset()):
|
||||
# ---- the code the hooks reach
|
||||
def plt_imports(nso, img, base, text_end):
|
||||
"""{absolute address of a PLT stub: the import its GOT slot binds} (adrp x16; ldr x17, [x16, #slot]; add x16, x16,
|
||||
#slot; br x17), from the JUMP_SLOT relocations."""
|
||||
slots = {off: nso.syms[sym][0] for off, typ, sym, _ in nso.relocs if typ == R_AARCH64_JUMP_SLOT}
|
||||
#slot; br x17), from the JUMP_SLOT relocations. A slot whose symbol the plugin defines itself binds to that
|
||||
definition (runtime/loader.c resolve), so its stub maps to the absolute address of the plugin's own code instead of
|
||||
a name: a plugin built with default visibility calls its own exported functions through its PLT (the
|
||||
std::vector<const char*> constructor of Challenge Mode's Enemy Health Regeneration)."""
|
||||
slots = {}
|
||||
for off, typ, sym, _ in nso.relocs:
|
||||
if typ == R_AARCH64_JUMP_SLOT:
|
||||
name, _, shndx, value, _ = nso.syms[sym]
|
||||
slots[off] = base + value if shndx else name
|
||||
out = {}
|
||||
for o in range(0, text_end - 16, 4):
|
||||
w0, w1, _, w3 = struct.unpack_from('<4I', img, o)
|
||||
@@ -499,6 +506,10 @@ def reach(img, base, text_end, roots, plt, stubs=()):
|
||||
if a in seen:
|
||||
break
|
||||
if a in plt:
|
||||
if isinstance(plt[a], int): # the plugin's own function behind its PLT: the path goes on there
|
||||
seen.add(a)
|
||||
a = plt[a]
|
||||
continue
|
||||
imports[plt[a]] = imports.get(plt[a], 0) + 1
|
||||
break
|
||||
if not base <= a < base + text_end:
|
||||
|
||||
Reference in new issue
Block a user