No game changes its own SDK while it runs, but a mod's loader (skyline, exlaunch) hooks SDK functions by writing a branch over their first instruction, and keeps a trampoline: that instruction, then a jump back to the one after it. When such a function has a native hook, its slot would keep running the handler, and the mod's code would never run.
So when the JIT drops a range's code (jit_code_invalidate), hook_restore compares the first 4 bytes of each hooked
function in it with the ones hook_install saw: one A64 or A32 instruction, or two Thumb ones. Only those 4 bytes,
on purpose: they are what every mod's hook writes over, and a change further into the function is not looked at. A
function whose first 4 bytes changed has its native hook invalidated:
hook_invalidated (jump.c, hook.c). It is a jump, not
a call, on every architecture: the sad path gets the arguments the adapter's caller left and returns straight to
that caller. hook_invalidated runs the game's code now at the function (hook_original, the JIT's code of it);The game's code then runs as on the console, the SDK's own code included wherever it calls it: a mod's call of the original function through its trampoline runs the SDK's code of that function, which may reach a service only the native layer answers, unless an ext_skyline hook takes it (below).
When the system refuses to make the adapter's code writable (mprotect, VirtualProtect), the game stops with the
reason and the values, since the mod's code would never run.
The jump, by host architecture, written past the adapter's landing pad (x86_64's endbr64, AArch64's bti; an
AArch64 paciasp becomes bti c, as the jump must not sign the return address):
| Host | Jump | Bytes |
|---|---|---|
| x86_64 | jmp rel32, or movabs r11 and jmp r11 | 5 or 13 |
| AArch64 | b, or ldr x16, #8, br x16 and the address | 4 or 16 |
| ppc64 ELFv2 (LE, BE) | r12 loaded with the global entry (lis, ori, sldi, oris, ori), mtctr r12, bctr | 28 |
| ppc64 ELFv1 | r12 loaded with the descriptor, ld r0, ld r2, mtctr r0, bctr, at the descriptor's entry | 36 |
| s390x | brcl 15, or lgrl %r1 of an aligned doubleword and br %r1 | 6, or up to 22 |
| mips64 (EL, EB) | $t9 loaded with the address, jalr $zero, $t9 and the delay slot's nop | 32 |
| riscv64 | auipc t1 and jalr, or auipc, ld of an aligned doubleword and jalr | 8, or up to 24 |
| loongarch64 | pcaddu18i $t0 and jirl (128 GiB either way) | 8 |
| ia64 | one MLX bundle, nop.m 0 and brl.sptk.few, at the descriptor's entry | 16 |
It assumes the adapter's code is long enough to hold the jump in the default build. Every adapter calls its handler
and stores its result, so none is that short, though an x86_64 function can be a lone ret.
tests/hook_invalidated.c runs the jump on x86_64 alone (the test is skipped on another host); tests/hook_jump.c
checks every architecture's bytes on any host, against llvm-mc's disassembly and, for IA-64, GNU objdump's (clang has
no IA-64 target yet; llvm-ia64 will take its place). Under NCE the JIT drops no code, so nothing is invalidated
there. A 32-bit game's hooks are not invalidated either: their slots are blocks hook_restore does not handle.
Made from docs/platforms/nx/hooks.md at 69237645142c. GPL-3.0-or-later.