Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The Nintendo Switch SDK provides basic binary file parsing functions under the nn::util namespace. This is used by various Switch libraries, especially by NintendoWare.

File Header

The alignment value specifies the number of bits. For example, if the alignment has the value 12, then the file must be aligned to 1 << 12 = 4096 bytes.

The address size field specifies the number of bits in a pointer. This is usually 64.

The relocation flag indicates whether the relocation table has been applied at runtime. The relocation flag is set to 1 at runtime after relocations are applied.

If there is no relocation table, the relocation table offset is 0.

OffsetSizeDescription
0x08Magic number
0x81Micro version
0x91Minor version
0xA2Major version
0xC2Endianness (0xFEFF=Big, 0xFFFE=Little)
0xE1Alignment
0xF1Address size
0x104Filename offset
0x142Flags:
1 - Is relocated
0x162Offset to first block or section
0x184Relocation table offset
0x1C4Filesize

Block Header

The block size is usually the same as the offset to the next block, but in the last block the offset is set to 0.

OffsetSizeDescription
0x04Signature
0x44Offset to next block
0x84Block size, including header
0xC4Padding
0x10Block body

String Pool

OffsetSizeDescription
0x016Block header (_STR)
0x104String count, excluding empty string
0x144Empty string
0x18Strings

String

Every string is null-terminated. If the string contains an odd number of bytes, an additional null byte is added such that the size field of the next string is aligned.

OffsetSizeDescription
0x02Size without null terminator
0x2Null-terminated string

Dictionary

The dictionary contains a binary search tree that allows programs to efficiently find the index of a given string.

Note: not all strings are included in the dictionary. Which strings are included the dictionary depends on the file format.

OffsetSizeDescription
0x04Always _DIC
0x44Number of nodes, excluding root node (N)
0x816Root node
0x1816 * NOther nodes

Every node is stored as follows:

OffsetSizeDescription
0x04Bit index
0x42Left child index (when bit is 0)
0x62Right child index (when bit is 1)
0x88Offset to string in string pool, relative to start of file

In the root node, the bit index is -1, the left index points to the actual root node, the right index is 0, and the offset points to the empty string of the string pool.

In other nodes, the offset refers to a string in the string pool. If this string does not equal the search string, the search continues at one of its children. Which child is chosen depends on whether a specific bit in the search string is 0 or 1.

Bit index 0 refers to the least significant (right-most) bit of the string.

Node index 0 refers to the root node. Node index 1 refers to the first node after the root node. If there are no further children, the node index refers to the node itself or one of its parents.

Relocation Table

When the file is loaded into memory, it is relocated by converting offsets to memory addresses. Which offsets need to be converted is defined by the relocation table.

The relocation table offset is repeated here, so that the beginning of the file can be found from a pointer to the relocation table.

OffsetSizeDescription
0x04Signature (_RLT)
0x44Offset of relocation table (see file header)
0x84Number of sections
0xC4Padding
0x10Sections
Relocations

Relocation Section

The address field is a placeholder that is filled in at runtime. It is always 0 in the file.

OffsetSizeDescription
0x08Memory address
0x84File offset
0xC4File size
0x104Index of first relocation
0x144Number of relocations

Relocation Entry

The word size is specified in the header in the address size field. In most Switch games, a word consists of 8 bytes.

Every chunk consists of a number of relocated words, followed by a number of non-relocated words. Relocated words contains an offset in the file and are replaced by a pointer at runtime. Non-relocated words are left unchanged.

OffsetSizeDescription
0x04Offset to chunks, relative to start of file
0x42Number of chunks
0x61Number of relocated words per chunk
0x71Number of non-relocated words per chunk