Loading NROs at run time (ldr:ro)

src/nx/hle/services/ldr.c implements IRoInterface for ldr:ro and ro:1:

Reference and ABI details

Cross-referenced with Eden at commit a594c8d60ef79280adc046760561912207e16e17:

This is a C implementation for ScarletNX's single-process memory model, not a copy of Eden's page-table code. The runtime now injects its process ID into incoming PID descriptors rather than trusting a client placeholder; its current-process pseudo handle also resolves to a process object for Initialize's copied handle.

An NRR stores SHA-256 hashes of complete, unrelocated NRO images, not build IDs. Registration snapshots the hash list after checking its bounds, header size, program ID and kind. LoadNro checks the mapped image against those hashes, then rejects an already-loaded module ID. RSA certification/signature verification is not implemented, matching Eden's HLE registration path. NRR unregistration does not invalidate already-loaded NROs.

Execution and memory

The service does not relocate the NRO or resolve its ELF symbols. The guest nn::ro linker does that after LoadNro returns. It also manages constructors/destructors. Pre-relocating in the service would change bytes the client expects to receive untouched. The caller's BSS backing is preserved, not silently zeroed.

Where the runtime links the game in its rtld's place (architecture), nn::ro is the runtime's (src/nx/nn/ro.c), not the SDK's: RegisterModuleInfo, LoadModule and UnloadModule reach the same registration, mapping and unmapping without a session (ldr_own_*, a state of the runtime's own beside the sessions'), and the runtime's linker (src/nx/runtime/linker.c) does what the guest linker would: it zeroes the BSS, relocates the NRO, binds its symbols and runs its constructors and destructors.

An NRO the recompiler found in the game's RomFS has its code made ahead of time (below). The code of any other NRO is compiled by the runtime JIT. When LoadNro maps such an NRO, its text joins the JIT's images (jit_code_added in src/nx/runtime/guest_jit.c, snx_jit_add_image in src/common/cpu/recompiler/jit/jit_service.c). Its pcs lie outside CPU.disp, so a call into it misses in snx_lookup and reaches RtAPI.miss. Resolution first checks any compatible AOT variants through variant_resolve, then snx_jit_resolve reads the JIT's own table for that image without a lock. A resolved call therefore includes the miss path and applicable variant lookup as well as the JIT-table load. The first call to a function waits for its compile, as for any code the AOT module lacks. JIT code for the NRO calls the module's functions directly and other code through the dispatch. UnloadNro, and the end of the session, remove the image before unmapping its memory: snx_jit_remove_image waits for the compiles reading it and forgets its code, so an NRO mapped at the same place later is compiled afresh. NRO pcs are not written to .hints, since the next run maps the NRO elsewhere. Without the JIT (a system it can't start on), NRO code can't run.

A host dlopen() alone cannot execute ARM64 NRO bytes on another host architecture. Nor can an existing fixed-base ScarletNX AOT module be relocated just by moving its dispatch entries: generated PC-relative addresses and direct calls use its original guest addresses. The JIT compiles the NRO at the address it is mapped at, and an NRO made ahead of time is mapped at the one address its code was made for.

NROs made ahead of time

The recompile of a game finds every .nro file of its RomFS (an extracted game's romfs/ folder, or a dump's RomFS read in place), and makes each one's code a module of its own (src/nx/runtime/driver/nros.c):

A file that is no NRO ldr:ro maps (not an NRO, compressed, an image or a BSS that is not whole pages) is said and left out. An NCE recompile and an iOS pack make none.

At run time CPU.disp covers the NROs' addresses too, and the memory there is kept for them: an NRO without a place is mapped outside it. When LoadNro is given an image whose hash the table has (ro_load_nro in src/nx/hle/services/ldr.c):

The guest patching an NRO's code (IC IVAU) makes the functions compiled from those lines stale, as it does the game module's; each mapping starts from the module as it was made.

An NRO calls the game's functions, and many of its own, through its PLT: a stub loads the function's address from a slot the game's linker (nn::ro) fills while the game runs, and branches to it. The recompile reads the NRO's PLT slots (snx_module_plt in src/nx/loader/loader.c) and binds each stub whose symbol names a function of the NRO itself, or one that a single module of the game exports. A BL or B to a stub of the game's function calls it through the module's import of it and does not read the slot: the game's modules come first for a name, in the SDK's nn::ro and in the runtime's linker, so the slot is bound to that function. A stub of the NRO's own function, and each BL or B to it, which runs the stub's instructions where the call is, compares the address it loaded with that function's: equal, it calls it as any call inside the module; anything else in the slot (another module loaded before the NRO defines the symbol too) goes through CPU.disp as before. So does the stub of the game's function when it is entered as a function, through a pointer. A module's imports are its snx_imports() table (nro_slots.h): each pointer starts as the module's own lookup in CPU.disp, and the runtime puts there the function that answers the address (nro_imports_bind in guest_jit.c): what its dispatch slot holds, when that is the game module's function, or the function the runtime gave the slot in its place, a hook's handler or NVN's implementation, which wins over the game's own. A slot given such a function later, as NVN's implementation of a function the game resolves while it runs, has the import bound to it then (jit_slot_taken). A slot that holds anything else stays a call through the table, as its content can change again without a word: code the JIT made, a profile pack's variant, or nothing (a function gone stale, or never compiled). A recompile with --hook-direct-calls binds none.

An update's NROs are found in its RomFS over its game's: the recompile of an update's dump reads it together with the game's dump, the one the game's settings record (source=). Without that dump, a game made from an extracted folder or one whose dump is gone, the update has no RomFS to look in, which the recompile says, and the JIT runs its NROs.

What is not made ahead of time: an NRO a mod adds or replaces (its hash is not in the table), and an NRO the game takes from outside its RomFS.

mem_map_code / mem_unmap_code in src/nx/memory/mem.c:

These transactions hold the memory lock, including source validation, so heap shrinking and concurrent mappings cannot consume the same pages. Like the existing MapMemory implementation, this uses copies rather than true host aliases; source permissions are guest metadata rather than host page protections. Clients must not access locked source ranges. Validation failures roll back the mapping. A service lock serializes requests, including cloned sessions, and session destruction releases owned mappings and hash snapshots.

Limits: 64 NROs and 64 NRRs per process (the stock limits; Eden extends these to 256). Compressed NROs are rejected. Only the current application process is supported. ro:dmnt is not part of this implementation.


Made from docs/platforms/nx/nro.md at 69237645142c. GPL-3.0-or-later.