Section 12.49

Program loading: from an ELF file to a running native process

Full name: Program loading: from an Executable and Linking Format (ELF) file to a running native process.

Definition

Program loading is the work that turns an executable file into a running process: a program in execution, with its own memory, permissions and starting register values. For an ELF §12.6 (Executable and Linking Format) file on Linux, the kernel maps the file’s loadable segments, the byte ranges that must be in memory, to their addresses and builds the first stack. If the program uses shared libraries, the kernel first starts the dynamic linker, a program that loads those libraries and fills in their addresses. Then the program’s own instructions run.

The idea

An executable file is not a running program. It is a set of byte ranges, each with instructions for where it belongs: these bytes are code and go at this address, readable and executable; these are the initial values of variables, writable; this much more memory follows and must be zero; these shared libraries must be present. A process needs all of that in place, plus a stack holding its command-line arguments, before its first instruction runs.

A loader prepares; it neither decodes nor executes the program’s instructions. It places the code as opaque bytes at the right addresses with the right permissions, and the processor decodes each instruction as it executes it. Some systems do decode every instruction before running any: the loader for eBPF §2.1 (extended Berkeley Packet Filter) programs, which run in a virtual machine §2.3 inside the Linux kernel, passes them to the eBPF verifier §3.3, which examines every instruction, and a just-in-time compiler §2.14 translates instructions into native code. Those are stages that particular systems choose, not stages that every loader needs.

How it works

The steps below follow Linux as it starts a dynamically linked ELF executable; the field names are those of the ELF specification.

  1. Request. A process calls execve, the system call that replaces the program it runs, with the file’s path, the arguments and the environment variables.

  2. Header check. The file must begin with the bytes 0x7F, E, L, F, be built for this processor (field e_machine) and be either an executable with fixed addresses (e_type ET_EXEC) or a position-independent executable (ET_DYN), which can run at any address.

  3. Mapping the segments. The kernel reads the program header table, the list of segments. For each segment of type PT_LOAD, it maps p_filesz bytes from offset p_offset in the file to the virtual address p_vaddr plus the load bias. The load bias is 0 for fixed addresses; for a position-independent executable the kernel chooses it, at random on each run when ASLR (address space layout randomization) is on. If p_memsz is larger than p_filesz, the remaining bytes are zero; the zero-initialized variables, the section .bss, live there. p_flags gives the permissions: read, write, execute.

  4. Finding the dynamic linker. A segment of type PT_INTERP holds the path of a program that the ELF specification calls the program interpreter, on x86-64 Linux usually /lib64/ld-linux-x86-64.so.2. Despite the name, it interprets no instructions: it is the dynamic linker. The kernel maps it too.

  5. First stack and jump. The kernel builds a stack holding the argument count, the arguments, the environment variables and the auxiliary vector, a list of facts such as the program’s entry point, the address in e_entry. It jumps to the dynamic linker, or straight to the entry point if there is no PT_INTERP.

  6. Dynamic linking. The dynamic linker reads the program’s dynamic section, which lists, among other things, the shared libraries needed (entries DT_NEEDED). It maps each library, resolves symbols, meaning it finds the address of each named function and variable, and applies relocations §12.50, meaning it writes those addresses where they are needed; calls to library functions may be bound lazily, at their first call. It runs each library’s initialization functions and jumps to the entry point.

  7. Language start-up. The entry point is not main but start-up code from the C library, usually named _start, which prepares the C runtime and then calls main.

Mapping does not copy. The kernel records which page of the file belongs at which page of memory and reads a page only when the program first touches it, a technique called demand paging. A page, the unit of mapping, is 4096 bytes on most x86-64 systems. A statically linked executable has no PT_INTERP, and no dynamic linker runs.

Worked example

A position-independent executable has one PT_LOAD segment with these invented values: p_offset is 0x1000, p_vaddr is 0x2000, p_filesz is 4 bytes, p_memsz is 8 bytes, and p_align is 0x1000, the page size of 4096 bytes. The file bytes at offsets 0x1000 to 0x1003 are AA BB CC DD, and the kernel chooses the load bias 0x400000.

  1. Alignment §4.22 check. The ELF specification requires p_offset and p_vaddr to leave the same remainder when divided by p_align, so that a file page can be mapped onto a memory page. Both leave remainder 0, so the check passes.

  2. Addresses. The segment starts at 𝟶𝚡𝟺𝟶𝟶𝟶𝟶𝟶+𝟶𝚡𝟸𝟶𝟶𝟶=𝟶𝚡𝟺𝟶𝟸𝟶𝟶𝟶\mathtt{0x400000} + \mathtt{0x2000} = \mathtt{0x402000} and covers 8 bytes, up to 0x402007.

  3. Content. 0x402000 to 0x402003 hold AA BB CC DD; the remaining 8−4=48 - 4 = 4 bytes, 0x402004 to 0x402007, hold zeros.

  4. Why the zeros must be written. The kernel maps whole pages, so the file page that starts at offset 0x1000 appears at 0x402000 to 0x402FFF, and its bytes after offset 0x1003 are whatever the file holds there, such as the next section. The loader must overwrite 0x402004 to 0x402007 with zeros. A loader that read 8 bytes from the file would put 4 unrelated bytes into variables that must start at zero.

Stated precisely

For one PT_LOAD segment, let oo be p_offset, vv be p_vaddr, ff be p_filesz, mm be p_memsz and aa be p_align, and let bb be the load bias. The ELF specification requires f≤mf \le m and, when a>1a > 1, vmoda=omodav \bmod a = o \bmod a. A correct loader produces memory in which

The specification describes valid files. A loader that reads files from an untrusted source must also check what the specification takes for granted: o+fo + f does not exceed the file size, no sum such as b+v+mb + v + m overflows, loadable segments do not overlap, and every address lies where the process may map memory. Control reaches the program’s entry point, bb plus e_entry, only after every library listed in DT_NEEDED has been mapped and every relocation that is not deferred by lazy binding has been applied.

Limits and common mistakes

Successful loading proves only what the loader checked: the file is well formed and fits in memory. A well-formed executable can contain harmful instructions. Native loading has no safety verifier like the one eBPF uses, and signatures, sandboxing §2.30 and access control §1.7 are separate mechanisms.

The loader parses untrusted data, often with the privileges of the kernel, so an unchecked size or offset, or a sum that overflows, can break the loader itself before any program instruction runs.

The file that runs must be the file that was approved. If a signature check reads a file by its path and the kernel later opens the same path, a file swapped in between runs unchecked, a TOCTOU §9.26 (time-of-check to time-of-use) error; checking the open file closes the gap. Environment variables such as LD_LIBRARY_PATH change which shared library is found, and the dynamic linker ignores them for programs that run with raised privileges.

Lazy binding moves errors to run time: a library function whose name cannot be resolved is reported only at its first call.

Most microcontrollers §6.1 have no program loader at run time. The linker §12.4 fixes every address, a programming tool writes the image into flash memory §6.6, and the processor executes code in place. At reset, start-up code copies the initial values of variables from flash to RAM and fills .bss with zeros. A bootloader §6.30, a small program that runs first, may choose and check an image, but it maps no segments.

Questions answered

Does instruction decoding have to run as a separate stage before a loader?

No. A native program loader, such as the one in the Linux kernel, does not decode the program’s instructions: it reads the file’s headers, places the bytes of each segment at the right addresses with the right permissions, and transfers control, and the processor decodes each instruction as it executes it. A separate decoding stage exists only where a system chooses one, such as the eBPF (extended Berkeley Packet Filter) verifier, which examines every instruction before the kernel accepts a program, or a just-in-time compiler; such stages run as part of loading or after it. Every design does need agreement on the file format, checks on every offset and size, and one meaning of the instruction encoding §2.7 shared by every component that decodes it.

Why does a four-byte file extent create an eight-byte memory extent in the example?

The ELF (Executable and Linking Format) segment states two sizes with different roles: p_filesz, 4 bytes, is how many bytes come from the file, and p_memsz, 8 bytes, is how much memory the segment occupies. The loader maps the 4 file bytes and sets the other 8−4=48 - 4 = 4 bytes to zero, which is how zero-initialized variables take memory without taking space in the file. Reading 8 bytes from the file would be wrong, because the bytes that follow the segment in the file belong to something else.

What does the ELF program interpreter §2.13 do here?

The ELF (Executable and Linking Format) program interpreter is the dynamic linker, the program named by the PT_INTERP segment, such as /lib64/ld-linux-x86-64.so.2 on x86-64 Linux. The kernel maps it next to the program and starts it first. It loads the shared libraries the program needs, resolves symbols, applies relocations, runs the libraries’ initialization functions and jumps to the program’s entry point. Despite the name, it interprets no instructions: after the jump, the processor runs the program’s native code directly.

Does successful loading prove that the application is safe or authorized?

No. Successful loading proves only that the loader’s own checks passed: the file was well formed, built for this processor and fitted into memory, and its libraries were found. It does not prove that the file is the one that was approved, that the libraries found are the intended ones, that the code is harmless, or that the program may hold the authority it receives. Those need separate mechanisms: signature checks bound to the open file, controlled library search paths, sandboxing and access control.

Related entries: Bytecode (§2.4), Interpreter (§2.13), Linker (§12.4), ELF (§12.6), Symbol table (§12.8), Native application binary interface (ABI) (§12.47), Time-of-check to time-of-use (TOCTOU) (§9.26), Bootloader (§6.30), Relocation: turning a symbolic reference into exact encoded bits (§12.50). Further reading: [110, 196].