Native Client Linking and Memory Map

This is a work in progress: please improve it!

When Native Client loads an ELF binary, it expects certain things.  This is an attempt to document these things.

Section Layout

Alignment

NaCl requires the text section [ed: and others?] to begin at 64KiB-aligned locations.  If it doesn't end at a 64KiB boundary, the text region size is rounded up and is considered to end there.  This is a concession to the granularity of mmap on certain platforms.

The text section

NaCl requires that the text section contain at least 32 bytes of empty space at the end.  We overwrite this space with halting instructions (HLT on x86, special sauce on ARM) to ensure that code can't simply run off the end of text.

NaCl will overwrite the last 32 bytes, even if this padding isn't there, destroying whatever is there.  In particular, traditional linkers start rodata immediately at the end of text, and NaCl will happily replace your constants with bogus data.

Note that this padding space must be within the text section: otherwise it may conflict with the dynamic text region, described below.  It is acceptable to simply have the phdr's p_memsz field be 32-bytes larger than the p_filesz field.

The dynamic text section

If the binary leaves space between the end of the text section and the next section (often rodata, and if rodata is missing, data, and if that's missing, bss), NaCl will use it as a "dynamic text section" for runtime-generated code or dynamically-loaded libraries.

The rodata, data, and BSS sections

The rodata, data, and BSS sections must all begin at a 64K boundary.
Comments