Program Loading
How It All Connects · Assembly & OS track
6 min read
In this module
From Source Code to Executable
Source code can't run. It's text. Getting from "human types for loop" to "CPU executes instructions" means feeding the code through a pipeline of progressively closer-to-the-metal stages, each one building on what came before.
The Compilation Pipeline
Preprocessor: Before the compiler proper sees C code, the preprocessor expands #include files and #define macros and strips out anything excluded by #if.
Compiler: Takes your source code (in C, say) and translates it into assembly language, a human-legible representation of the CPU instructions for your specific architecture (x86-64, ARM, etc.).
Assembler: Converts that assembly into machine code, the raw binary instructions the CPU actually consumes. Output is an object file containing machine code but not yet a complete program.
Linker: Takes one or more object files and stitches them into a final executable. The linker patches up references between files — if one file calls a function defined in another, the linker joins the two ends. It also pulls in any libraries the program depends on, the C standard library being the obvious example.
On Linux, executables use the ELF (Executable and Linkable Format). An ELF file is a structured container, with headers describing where the code lives, where the data lives, where execution should kick off (the entry point), and what memory layout the program is expecting. The OS reads those headers to figure out how to load the program properly.
Click through the ELF fields below. Two of them drive the loading steps that follow: e_phoff tells the kernel where the program headers are (step 1), and e_entry is the address the program counter is pointed at in step 6. The byte values are a typical example, not from one particular file.
Click a section to explore its structure and purpose
How the OS Loads a Program
Run an executable and the OS works through a small ritual to drag it into memory:
- Parse the headers — the kernel reads the ELF headers to understand the program's shape
- Allocate memory — the kernel carves out a fresh virtual address space for the process, with regions for code (text), initialised data, uninitialised data (BSS), the heap, and the stack
- Map the segments — the code and data segments from the executable file are mapped into the right places. Nothing is copied yet: each page is read from the file the first time the program touches it (demand paging)
- Set up the stack — the kernel seeds the new stack with command-line arguments and environment variables
- Hand over to the dynamic linker — if the program uses shared libraries, its
PT_INTERPheader names an interpreter (/lib64/ld-linux-x86-64.so.2on x86-64). The kernel maps that too and starts it instead of the program. The dynamic linker, running in user space, maps the shared libraries and resolves their symbols - Jump to the entry point — the program counter lands on the executable's entry point, and away the process goes. For a static binary the kernel jumps there directly; for a dynamic one the dynamic linker does it once the libraries are ready
The result of steps 2 to 4 is the layout below. Hover over each region: the text, data and BSS at the bottom come straight from the ELF file's segments, while the heap, the mmap area where shared libraries land, and the stack are set up by the kernel and the dynamic linker.
64-bit Virtual Address Space Layout
Hover over a region to see details
Worked Example: Watching ./hello Load
Take a C program that prints hi, built with plain gcc hello.c -o hello on an x86-64 Linux box. Two tools let you watch the six steps. readelf shows what the kernel reads in step 1 (output trimmed, addresses typical for this kind of build):
$ readelf -h -l hello Type: DYN (Position-Independent Executable file) Entry point address: 0x1060 ... INTERP 0x0000000000000318 ... [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2] LOAD 0x0000000000000000 ...The INTERP entry is the PT_INTERP header from step 5, and the LOAD entries are the segments mapped in step 3. The entry point is an offset because this is a position-independent executable: the kernel picks a random base address and adds 0x1060 to it.
strace then shows the system calls as they happen (trimmed again):
$ strace ./hello # values will differ on your machineexecve("./hello", ["./hello"], 0x7ffc... /* 24 vars */) = 0openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0"..., 832) = 832mmap(NULL, 2170256, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f3a1c000000...write(1, "hi\n", 3) = 3exit_group(0) = ?Everything in steps 1 to 4 happens inside that single execve: by the time it returns 0, the kernel has mapped hello and ld-linux, built the stack and started the dynamic linker. The next lines are the dynamic linker at work in user space. It checks its cache to find where libc.so.6 lives, opens it, reads the first 832 bytes (the ELF header and program headers, the same ones you clicked through above) and maps it with mmap. Only after that does the program's own code run, and its first syscall is the write.
Predict: rebuild it with gcc -static hello.c -o hello. Which parts of the readelf and strace output disappear?
The INTERP header goes, because a static binary has no interpreter to request. In the strace output, the openat of /etc/ld.so.cache, the openat and read of libc.so.6 and the mmap calls that map it all vanish: the C library is already copied inside the executable, so the kernel jumps straight to the entry point and nobody has to go looking for libraries. What's left is execve, a few calls libc makes to set itself up, the write and exit_group. The file itself grows from around 16 KB to several hundred KB, which is the trade-off the next section describes.
Static vs. Dynamic Linking
With static linking, every library function the program needs gets copied bodily into the executable at link time. The binary stands alone but takes up more disk. With dynamic linking, the executable just holds references to shared libraries (.so files on Linux, .dll on Windows), which get loaded at runtime. Less disk, less RAM when multiple programs share the same library.