Format:MOD0
| On this wiki | Facts · Code · Notes · Topics |
|---|
MOD0 is the small module header that libnx-built Switch executables (and, Unverified, others) (NRO, NSO, KIP) carries near the start of its code: the magic MOD0 followed by signed offsets, relative to the header itself, to .dynamic, the bss range, the eh_frame_hdr range and a module-object slot.
Finding it
The first bytes of the text segment are an AArch64 branch, then a u32 (offset 4) holding the offset from the start of the image to the MOD0 header; libnx's crt0 writes this as .word __nx_mod0 - _start, followed by the ASCII string HOMEBREW at offset 8 and code from offset 0x80 (Fact:NRO/struct/MOD0 header). In an Format:NRO the 0x70-byte NRO header overwrites bytes 0x10-0x7F of that first segment (elf2nro, Fact:NRO/behavior/elf2nro output layout); in an Format:NSO the same text-segment prefix is used by libnx builds. Unverified: that official SDK modules put the MOD0 offset at text+4 in the same way (not read in these sources).
Layout
All offsets are signed 32-bit values relative to the MOD0 header itself, little endian: .dynamic, bss start, bss end, eh_frame_hdr start, eh_frame_hdr end, and a module-object slot that libnx leaves zero. libnx appends the extension blocks LNY0 (GOT start/end), LNY1 (relro start/end) and LNY2 (a version word); a runtime that does not see both LNY0 and LNY1 skips the relro protection. See Fact:NRO/struct/MOD0 header.
What the loader does with it (libnx)
libnx's __nx_dynamic(base, mod0) returns early if the magic is not MOD0, zeroes bss, reads DT_RELA/DT_RELASZ and DT_RELR/DT_RELRSZ from the dynamic section and relocates by adding the load base; only R_AARCH64_NONE and R_AARCH64_RELATIVE are accepted. For reverse engineering this means a homebrew image is position independent and its relocations are in the .dynamic tables found through MOD0, so a disassembler that does not process them shows unrelocated pointers.
Related
Format:NRO, Format:NSO, Library:Libnx, Platform:Nintendo Switch.