Linkers
Concise study notes on linkers: what they do, key concepts (relocation, symbol resolution, static vs dynamic linking, GOT/PLT, PIC), formulas/syntax to remember, worked examples, common mistakes and exam guidance.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Linkers are used by engineers whenever they build programs from multiple object files or libraries and when they deploy software with shared libraries. A correct understanding of linking is essential for debugging undefined-symbol errors, controlling binary size, writing or using shared libraries, and understanding runtime performance and security (e.g., position-independent code, symbol preemption).
Key ideas
Start with definitions (each defined term is bolded when first used):
- Linker: the program (also called link editor) that takes one or more object files and libraries and produces an executable or shared object by performing symbol resolution, relocation, and section combination.
- Object file: a file produced by an assembler or compiler which contains machine code or data in sections, a symbol table, and relocation records. Common formats: ELF (Unix/Linux), PE/COFF (Windows), and Mach-O (macOS); this note uses ELF names when examples are needed.
- Symbol: a name (identifier) representing an address or value (e.g., function or variable). Symbols are recorded in a symbol table in an object file.
- Symbol resolution (binding): the process of finding, for each undefined symbol in an object file, a matching definition in another object file or library and assigning it an address.
- Relocation: the process of adjusting addresses inside code and data so that references point to final addresses after all sections have been placed in memory. Each relocation record contains (at least) the place to change (P), a type, and an addend (A) or uses an implicit addend.
- Loader: the program that loads an executable into memory at process start and performs any run-time relocations or dynamic linking (in dynamic linking). The loader is different from the linker: the linker runs at build time; the loader runs at run time.
- Static linking: linking at build-time so that all needed code is copied into the resulting executable; no run-time symbol resolution for those symbols.
- Dynamic linking: linking in which some symbols are left for resolution at load-time or run-time, typically resolved by a runtime linker/loader (e.g., ld.so on Linux). Shared libraries (.so) can be shared among processes.
- Position-Independent Code (PIC): code that can execute correctly regardless of its absolute load address. Compiled with options like
-fPIC. Executables built as Position Independent Executables (PIE) use similar techniques. - Global vs Local (static) symbols: a global symbol is visible to the linker across object files; a local (or file-local, sometimes called static in C) symbol is visible only within the defining object file.
- GOT (Global Offset Table) and PLT (Procedure Linkage Table): runtime structures used by many ELF ABIs to implement dynamic linking and lazy binding. The GOT holds addresses of global data or function entries; the PLT holds small stubs used to jump to library functions via the GOT.
Linker tasks, step by step (what to draw and label in a diagram):
- Draw multiple boxes on the left labeled
file1.o,file2.o,libfoo.a. Inside each, draw smaller labeled boxes for.text,.data,.bss, a smallsymtabandreloclist. Draw arrows from their undefined-symbol entries to definitions found in other files or libraries (symbol resolution). Then draw a central box labeledlink editorthat merges.textsections, assigns addresses (layout), applies relocations, and emits the finala.outorlibbar.so. From that output box, draw an arrow to aloader (ld.so)box to show dynamic linking at load time.
Assumptions usually stated in exam answers:
- Object files use the same ABI and instruction set.
- Symbols have unique names per scope rules; linker applies language and ABI symbol-mangling rules.
- Addresses are word-aligned as required by the architecture.
Important exact statements often requested in exams:
- For PC-relative relocations that produce a value written at place P: relocated_value = S + A - P (explain S, A, P below). Other relocation types (e.g., absolute) use different forms.
- Static linking resolves all undefined strong symbols at link time; missing definitions cause link-time errors. In ELF, strong vs weak resolution: a strong definition overrides weak ones; if only weak definitions exist, the weak one is used; an undefined weak symbol is permitted to resolve to zero/null (subject to relocation type), allowing optional references.
- Shared libraries can be loaded at different addresses in different processes; PIC or relocations are needed to make code correct when not linked at a fixed address.
Definitions of variables used in relocation formulas (explain before using):
- S: the final runtime address of the symbol being referenced.
- A: the addend — a constant from the relocation record or encoded in the instruction or data.
- P: the place (address) of the storage unit being relocated; for PC-relative relocations, P is the reference address used by the CPU (per ABI), often the address of the relocation field or the next instruction.
Relocation types (conceptual):
- Absolute relocation: value written = S + A.
- PC-relative (or RIP-relative): value written = S + A - P (commonly used for relative branches and position-independent addressing).
- On ELF systems two record formats exist: REL (addend implicit in place) and RELA (explicit addend in relocation entry).
Dynamic linking behaviours to know:
- Eager (bind now): all dynamic relocations are resolved at program start by the loader.
- Lazy: function addresses are resolved on first call via PLT stubs and a resolver which updates GOT entries.
- Symbol preemption: when multiple shared objects define the same global symbol, the dynamic loader chooses which definition satisfies references according to search order and visibility rules (this can cause surprising overrides).
Formulas
relocated_value = S + A - P — S = symbol address, A = addend, P = place (address of relocation); use for PC-relative relocations.
relocated_value = S + A — for absolute relocations where no P applies.
gcc -c file.c -o file.o — compile to object file (syntax to remember).
gcc file.o other.o -o prog — default linking command (driver invokes ld).
gcc -shared -fPIC -o libfoo.so foo.o — create a shared library with PIC.
gcc -pie -fPIE -o prog prog.o — create a position-independent executable (PIE).
ld -o prog file1.o file2.o -L/path -lfoo — direct use of linker ld (remember order affects resolution for static archives).
ldd prog — list dynamic shared object dependencies (runtime; on Linux).
objdump -h file.o and readelf -s file.o — inspect sections and symbol tables.
Worked examples
Example 1 — static linking, address calculation (step-by-step): Given: main.o has a .text size 0x40 bytes and contains a direct absolute reference (relocation) at offset 0x08 of its .text with addend A = 0x0. util.o defines function func at offset 0x30 inside its .text which is size 0x40. The linker places the combined .text at base address 0x1000 with main.o's .text first.
Steps:
- Layout: main.o.text at 0x1000..0x103F (size 0x40). util.o.text will start at 0x1040.
- Address of func (S) = base of util.o.text + offset in util.o = 0x1040 + 0x30 = 0x1070.
- For an absolute relocation, relocated_value = S + A = 0x1070 + 0x0 = 0x1070.
- The linker writes 0x00001070 into the 4- or 8-byte slot at address (P = 0x1000 + 0x08) in the final binary.
Final: func address = 0x1070, relocation slot contains 0x1070.
Example 2 — PC-relative relocation (numeric): Given: a call instruction starts at 0x200 and its PC-relative displacement is computed from place P = 0x204 for this architecture, relocation addend A = 0x0, target symbol address S = 0x350. Compute 32-bit signed displacement stored (use formula relocated_value = S + A - P).
Steps:
- Apply formula: displacement = 0x350 + 0x0 - 0x204 = 0x350 - 0x204 = 0x14C.
- So the 32-bit displacement field is set to 0x0000014C.
- At runtime, when the CPU executes the call, it computes target = next_instruction_address + displacement which results in 0x350 as expected.
Final: stored displacement = 0x14C.
Example 3 — dynamic linking and lazy PLT resolution (algorithm trace): Scenario: main calls printf (in libc). At program start the GOT entry for printf is initialized to point to the PLT entry resolver stub. First call sequence:
- main's call instruction jumps to PLT[printf] stub.
- The PLT stub jumps (indirectly) to the dynamic resolver (in ld.so) using parameters indicating which symbol is requested.
- The resolver finds libc's printf address (S), writes it into the corresponding GOT entry, and returns the real address.
- The PLT stub now jumps to S. On subsequent calls, the PLT stub will read GOT[printf] and jump directly to S, so no further resolver calls occur.
This explains lazy binding and the role of GOT/PLT.
Common mistakes
- Thinking
gccalways links statically — by default it dynamically links;-staticis required for static linking. - Assuming shared-library symbol definitions are private by default — globals in shared libraries can be preempted unless hidden.
- Ignoring object file section order: when using
ldwith archives (.a), order of libraries matters (linker picks objects only when needed when scanning left-to-right). - Not using
-fPICfor shared library code (can cause relocation errors or poor performance).
For your exam
Question types you will see:
- Definitions (linker, relocation, symbol resolution, PIC, GOT/PLT).
- Short notes (steps of linking, static vs dynamic linking, position-independent code).
- Derivations (derive and apply relocation formula to compute values; show why PC-relative relocations use S + A - P).
- Numericals (given offsets and sizes, compute final addresses and relocated values; resolve a small link of two object files).
- Diagrams (draw object files, symbol tables, relocation records, link editor flow).
- Small program tracing (show how PLT/GOT lazy binding resolves a symbol).
What a full-marks answer includes:
- Precise definitions of terms used, explicit statement of assumptions, clear diagrams where requested, step-by-step numeric calculations showing formula used and units (addresses in hex), and a final boxed result.
Quick check
- What is the main difference between static and dynamic linking?
- In the relocation formula
relocated_value = S + A - P, what does P represent? - Why is
-fPICused when creating shared libraries?
Answers: 1) Static linking copies code into the executable at link time; dynamic linking leaves references to be resolved at load time or run-time. 2) P is the place (address) of the storage being relocated (often the address of the relocation site or next instruction for PC-relative). 3) -fPIC produces position-independent code so the same library can be loaded at different addresses without per-process text relocations and with correct addressing via GOT/PC-relative code.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?