Linking and loading
From ELF object files to a process: symbols, relocation, static and shared libraries, PLT/GOT, the loader and ASLR.
Why a linker
The write system call lives in the C library the compiler never saw. Without a linker you would keep the whole world in one source file: every edit rebuilds everything, libraries are compiled again and again, and 200 processes each with a private libc.so.6 (~1.7 MiB) would hold ~343 MB of identical bytes.
Key idea. Separate compilation yields relocatable objects; the static linker joins them into an executable; the dynamic linker finishes shared libraries at run time.
Worked example: watching the binding happen. The dynamic linker of glibc reports its work when the environment variable LD_DEBUG is set. A program that calls puts twice, linked for lazy binding and run in a Debian 13 container (glibc 2.41, arm64), shows the binding of puts after control has passed to the program, once, right before the first call prints; with LD_BIND_NOW=1 the same binding appears before the program starts. Linux only, not run on macOS; the excerpt keeps the relevant lines and drops the process-ID column:
A call is a pair auipc ra, hi and jalr ra, lo(ra): the target is P+(hi≪12)+loP + (\mathit{hi} \ll 12) + \mathit{lo}P+(hi≪12)+lo, so the value to encode is the distance S−PS - PS−P. A global variable in the non-position-independent code of this example is reached by lui a1, hi and lw a1, lo(a1), whose address is (hi≪12)+lo(\mathit{hi} \ll 12) + \mathit{lo}(hi≪12)+lo, so the value is SSS itself. In both cases lo is a signed 12-bit field, sign-extended by the processor, so the split must round:
ELF and symbols
clang -c writes ELF: magic 7f 45 4c 46, sections .text / .data / .bss / .rodata, a symbol table. Global counter and bump are visible outside; static calls is not; extern in main.c stays undefined until the linker finds a definition.
Worked example. The first call to bump sits at P=0x111acP = \texttt{0x111ac}P=0x111ac (main at 0x111a0 plus offset 0xc), and S=0x111d8S = \texttt{0x111d8}S=0x111d8:
If the code of a program and its libraries is always at the same addresses, an attacker who can overwrite a return address knows exactly where to jump: into system in the C library, for example. Address space layout randomization (ASLR) moves the regions to random addresses on every run, so such an attack must first find out where they are. Linux controls it with /proc/sys/kernel/randomize_va_space: 0 turns it off; 1 randomizes the stack, the area where mmap places libraries, and the vDSO; 2, the usual default, also randomizes the start of the heap (Linux kernel documentation). The executable itself moves only if it is position-independent, which is why distributions now build programs as PIE by default; the Linux addresses quoted in the C note are those of a run with randomization off.
Relocation
In a relocation is the symbol address, the place being patched, the addend (often 0). On RISC-V a far address is split into hi/lo for lui+addi; the linker fills the numbers. Until then many addresses in the .o are zeros.
Worked example. The program aslr.c in the Code section prints the address of main, of a global, of a heap block and of a local, and runs twice on an Apple M1 (macOS always randomizes):
Static, .so, PLT and GOT
An .a archive copies objects into the binary. A shared .so shares code pages across processes. Calls go through the PLT; addresses live in the GOT; lazy binding fills the GOT on first call. That enables ASLR, LD_PRELOAD interposition, and updating a library without rebuilding clients.
reloc.py: python3 reloc.py -- the arithmetic of the three RISC-V relocations in the worked example def hi_lo(value): """Split a 32-bit value into hi (for lui/auipc, upper 20 bits) and lo (for addi/lw/jalr, signed 12 bits).""" hi = (value + 0x800) >> 12 # round, because lo is sign-extended lo = value - (hi << 12) # always in [-2048, 2047] return hi, lo
The loader and the process image
exec maps segments; ld.so resolves dependencies; control reaches the entry (_start → main). The image is fixed at link time — except the stack and whatever the program still asks of the heap. Why link. printf/write live in a library the compiler never saw while compiling your .c. Separate compilation yields relocatable .o files; the static linker joins them and libraries into an executable; the dynamic linker finishes shared libraries at run time. Without sharing, 200 processes each with a private libc.so.6 (~1.7 MiB) would hold ~343 MB of identical bytes. ELF. Magic 7f 45 4c 46; sections .text / .data / .bss / .rodata; a symbol table distinguishes global bump (defined), static calls (local) and extern counter (undefined until link). Relocations record style patches; RISC-V splits far addresses into hi/lo for lui+addi. Static vs dynamic. .a archives copy needed objects into the binary. .so shares text pages: calls go through the PLT, addresses live in the GOT, lazy binding fills the GOT on first call. That enables ASLR, LD_PRELOAD interposition, and library updates without rebuilding clients. exec maps segments; ld.so resolves deps; control reaches _start → main. Mach-O on macOS tells the same story with different headers. Tooling: readelf -h/-S/-s, objdump -d, nm and ldd are the daily drivers for seeing what the linker did. A duplicate-symbol error at link time means two strong definitions; weak symbols and COMDAT sections are how C++ templates and inline functions avoid that. Undefined-reference errors name the missing S in a relocation — either you forgot to link a library (-lm, -lpthread) or the symbol’s language mangling differs (C vs C++). ASLR randomizes load bases; PIE executables relocate like shared libraries. Absolute addresses baked into .text without a relocation break under ASLR — another reason position-independent sequences (auipc+addi on RISC-V, RIP-relative on x86-64) dominate modern code. Next: Dynamic memory allocation and strings — malloc/free, where the heap comes
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.