Skip to content
BIASED

Linking and loading

Block 1 of 5~2 min

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:

Answers are saved on the server and tied to this browser until you sign in.