driver.c: CLI dispatch only; extract.c and recompile.c own their workflows.container.c: a dump opened with hactool, its keys loaded and its NCAs picked, shared by the extraction, the
recompiler (a dump given to -r, its ExeFS read through SnxExefsReader) and the runtime's fs.c. Without
Extract dump, fs.c maps the dump source= names (else the one <id>.source links to) and serves exefs/* and the RomFS image from
it through hactool's storage chains (snx_hac.h's SnxHacFs), the sections decrypted, an update's patch applied
and compressed blocks expanded as they are read. Nothing is cached.src/nx/loader/loader.c: NSO/NRO, LZ4, SHA-256, NPDM, MOD0, symbols/relocs/FDEs.src/common/cpu/aarch64/a64_decoder.c: the A64 decoder tools/decodetree generates from QEMU's a64.decode and
ScarletNX's a64.profile (src/common/cpu/aarch64/decode/): each op's flow and the feature it needs, so a target's
feature set (the Switch's Cortex-A57 by default) decides what is UNDEF.src/common/cpu/recompiler/codegen.c: function/CFG discovery over platform-neutral image views.
src/nx/loader/codegen_view.c adapts NX images; module.c holds what the backends share (the dispatch entry
table, chunk labels, progress lines); chunks batch whole functions up to CHUNK_INSTRUCTIONS (library.c), 90000 (20000 on Android).toolchain.c: the runtime build for deployment.src/common/cpu/recompiler/mach/ and a folder a machine: x64, arm64, rv64, ppc64,
mips64, la64, s390x) make all of the guest's code, from the emitters tools/sailgen generates from the Arm
architecture's Sail (src/common/cpu/aarch64/sail/, src/common/cpu/arm/sail/). An instruction a back end has no
code for raises an undefined instruction where it runs, and its function is made all the same; a function it still
cannot make (one longer than its branches reach) is said. The JIT, the AArch32 executor, the recompiler's library
(library.c) and the Switch's AOT modules (src/nx/loader/module_a64.c, module_arm.c) all take their code from
it. The objects are linked by the recompiler itself (link_elf.c, link_coff.c, link_macho.c in
src/common/cpu/recompiler/object/) with the runtime library, the helpers compiled when the recompiler is built
(rtlib_object.h), into a module with no imports, for the host it runs on: x86-64 (ELF or COFF), AArch64 (ELF, COFF
on Windows, or on macOS and iOS a Mach-O dylib with an ad hoc signature, each with a veneer for a branch past
128 MiB), PowerPC64 (ELF, ELFv2, with a stub for a branch past 32 MiB: PowerPC64), RISC-V and
LoongArch (ELF: RISC-V and LoongArch), MIPS64 (ELF with the ABI's GOT:
MIPS64) or s390x (ELF: s390x). An x86-64 or AArch64 ELF module larger
than its code's PC-relative references reach (2 and 4 GiB) has its data in the middle of its code, half the code away
at most, and an x86-64 call past 2 GiB goes through a jump at the cut (link_elf.c; ctest -R elf_relocate links
one with a reach of 1.5 MiB). tests/native_qemu_differential.c runs the
host's native back end on words of every row of the generated decoder (tests/native_steps.c), against
qemu-aarch64 -cpu max (tests/qemu_ref/) on random states, with the groups known to differ in
tests/native_qemu_expected.txt (a new group, or a listed one that no longer differs, fails it; --sweep runs more
of each); the hosts with no QEMU beside the test take a reference that another host recorded
(the implementer guide). tests/a64_ported.c runs rEden's
cases through the steps against their expected values and QEMU, tests/native_module.c a whole program against the
end state QEMU gives it, tests/native_bench.c times it on assembly kernels.CPU.poll on backward branches inside a function and at the D_ dispatch, as codegen.c's C
does, so a loop without SVCs gives its core to a waiting thread of higher priority. A backward branch's poll then
leaves the function: it calls the function itself at the branch's target, which its entry and D_ send to the
target's label.adrp x16, ldr x17 of
its slot, add x16, br x17) branches to the address the game's linker puts in the slot at the stub's first call.
The recompile binds each stub whose symbol one module alone of the program defines as a function
(snx_program_exports and snx_module_bound in src/nx/loader/loader.c, from each module's dynamic symbols as the
game's linker reads them: a symbol with a section, not local; the recompile's PLT: line counts them). A BL or
B to a bound stub calls the function where the call is, as a BL to it would (ir_codegen.c's call_stub), and
does not read the slot: whatever links the game binds it to that function, the runtime when it loads the modules
and a game's own rtld at the stub's first call, since one module alone defines the name. x16 is left as the
stub's adrp and add leave it and x17 holds the function's address, as the bound slot gives it. A slot that
holds anything else (a game's own rtld has not bound it yet, something wrote another function there) changes
nothing for such a call. The stub's own function, which a pointer to the stub reaches, loads the slot, compares
the address with that function's and calls it when they are equal, and goes through CPU.disp when they are not;
a call whose stub's code the guest changed goes to that function, stale as any other (RtAPI.code_changed).
A symbol two modules define is the linker's to pick, and its stub stays as it was; so does every stub with
--hook-direct-calls, and the stub of a function a hook names calls through the function's slot
(Hooks). An NRO's module binds its stubs the same way
(NRO). ctest -R plt_calls (tests/plt_calls.c) recompiles a game of two modules and
runs its calls with each slot unbound, bound and holding another function, and with a stub's code changed.rtld module it carries relocates every module and binds their symbols, then enters the
SDK (nn::init::Start), which calls the rtld back for the modules' constructors and, through nn::ro, for its
lists of modules. The runtime has a linker of its own, src/nx/loader/relocate.c: DT_RELR's and DT_RELA's relative
relocations, and ABS64, GLOB_DAT and JUMP_SLOT by the rule a game's rtld follows (the first definition of the
name that is not local, among the modules in load order; the module's own for a protected or hidden symbol; 0 for
a weak symbol nothing defines). Where it can, the runtime links the game in the rtld's place (runtime/linker.c):
it relocates the modules when they are loaded, every PLT slot bound; the main thread enters nn::init::Start
with three functions of the runtime's (their addresses the rtld's first three instructions, whose dispatch slots
hold them: the second runs each module's DT_INIT, the last module first, the third each DT_FINI); and what
of nn::ro needs an rtld's lists is answered by the runtime (src/nx/nn/ro.hook, ro.c: a symbol by its name,
among every module or the ones after the caller's), so nothing of the SDK reads what an rtld would have left. What
describes a module by its address (FindDynamicSegment, GetModuleName, GetModuleId) stays the SDK's code,
which reads the module's own header where no list was left. No code of the rtld runs. An NRO the game loads
(nn::ro::LoadModule) is linked by the runtime too: mapped by ldr:ro's code on a state of the runtime's own (the
SDK opens no session), at the address its AOT module is made for when it has one; its BSS zeroed; relocated behind
the game's modules and the NROs loaded before it, in that order; each word or PLT slot of those that nothing
defined until then bound to its definition; and its DT_INIT run on the thread that loads it. UnloadModule runs
its DT_FINI, puts each word of the others that points into it back to 0, binds a PLT slot of theirs again to the
definition the modules left give its name (what lazy binding gives it at the slot's next call), or to its lazy
value when none of them defines it, and unmaps
it. What the SDK stops the game on stops it with the reason: a result of ldr:ro's that is not nn::ro's own, an
NRO without a module header, one loaded for no other module to bind to that another binds to, an unload while the
SDK counts thread-local destructors of the module not run yet. Where the runtime cannot link, the game's rtld
links it as before and the log's linker: line says why: under NCE, a 32-bit game, no nn::init::Start the
runtime knows, a module that does not route nn::ro (one recompiled before this), or the nn_ro hooks off
(hooks_off=nn_ro). The runtime then links a copy of the modules' data and, at the game's first call of
nn::init::Start, compares every relocated word of the game's memory with the copy; the log gives the count, or
how many differ and the first of them. ctest -R 'loader_relocate|runtime_linker|ldr_service'
(tests/relocate_test.c, tests/runtime_linker.c, tests/ldr_service.c).snx_native_features), and the runtime library's helpers are portable C compiled when the recompiler is
built; nothing is detected at run time.
The x86-64 back end (x64/x64_vec.c) makes AdvSIMD with SSE4.2's and AVX's forms: GF2P8AFFINEQB, PAVGB/PAVGW, AVX2's
shifts by a lane's own amount (bytes and halfwords by products with the amount's power of two), and FPSR.QC tested
once for all the saturating instructions of a block; CRC32 by PCLMULQDQ, SHA-1 by SHA1RNDS4, and VSM4RNDS4 and VSM4KEY4
where CPUID says SM4; the tricks are wunkolo's (wunkolo.github.io).src/common/utilities/common.c and process.c: host files/processes; argv-based subprocesses.src/common/platform/ / scarletnx-platform: the host layer every driver shares, without SDL: threads and locks,
clocks and sleep, files, address space and shared libraries. The NX runtime's runtime/platform/ builds on it.
NX extraction retains its isolated key environment.Host tooling is strict ISO C23 with SDK fixed-width types, formatting and OS APIs.
The runtime and generated guest code retain their existing Clang/ABI requirements.
Normal builds link the driver into the runtime; SNX_TOOLS_ONLY builds the same CLI independently.
Made from docs/dev/architecture.md at 69237645142c. GPL-3.0-or-later.