A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Linking and Loading

College Depth 104 in the knowledge graph ☐ I know this ☆ Set as goal
541prerequisites beneath it
See this on the map →
Assembly Language BasicsMemory Management Fundamentals+2 more→
linker loader object-files relocation symbol-resolution shared-libraries dynamic-linking

Core Idea

A compiler translates each source file separately into a relocatable object file containing machine code, data and a symbol table, but with some addresses left unfilled. The linker combines object files and libraries into one executable by doing two jobs: symbol resolution, which matches every reference to a function or global variable with exactly one definition, and relocation, which assigns final addresses and patches every reference to use them. The loader then copies the executable into memory and starts it. Shared libraries move part of the linking to load time or run time, so many programs can share one copy of common code.

How It's Best Learned

Compile two small C files with `gcc -c`, inspect the object files with tools such as `nm` and `objdump` to see undefined symbols and relocation entries, and then link them. Trigger the two classic link errors on purpose: an undefined reference and a duplicate definition. Finally, build the same program statically and dynamically and compare file sizes and the output of `ldd`, following the linking chapter of Bryant and O'Hallaron's *Computer Systems: A Programmer's Perspective*.

Common Misconceptions

Explainer

Real programs are split across many source files, and they call code nobody in the project wrote, such as `printf`. The compiler handles one file at a time. When it compiles `main.c` and sees a call to `compute()` defined in `util.c`, it cannot know where `compute` will sit in memory. So it emits a relocatable object file (on Linux, an ELF `.o` file). That file holds machine code in a `.text` section, initialized globals in `.data`, a symbol table listing what the file defines and what it uses, and relocation entries marking every spot that needs an address filled in later.

The linker, such as `ld`, which `gcc` runs for you, turns a set of object files into an executable in two steps. Symbol resolution matches each reference to exactly one definition. If no definition exists anywhere, you get "undefined reference". If two files define the same global function, you get a "multiple definition" error. Relocation then merges the sections of all files, for example every `.text` into one code section. It assigns each section and symbol its final address and walks the relocation entries, patching each call and each global access with the right address.

Libraries come in two forms. A static library (a `.a` archive on Linux) is a bundle of object files. The linker copies in only the members that resolve some needed symbol, and the result is a self-contained executable. A shared library (`.so` on Linux, `.dll` on Windows) is linked differently. At build time the linker records only that the program needs, say, `libc.so.6`. When the program starts, a dynamic linker maps the library into the process and connects the program's references to it. Shared libraries are compiled as position-independent code, so the same physical pages can be mapped at different addresses in different processes.

The loader is the last step. When a shell runs a program, the `execve` system call reads the executable's header and maps its code and data segments into a fresh address space. It sets up the stack with the arguments and environment, and on systems that use shared libraries it hands control to the dynamic linker. Execution finally reaches the program's entry point, typically a startup routine named `_start`, which in turn calls `main`.

Knowing these stages turns confusing build failures into ordinary diagnostics. A compile error points at a line of source. A link error points at a missing or duplicated symbol. A load-time error, such as a missing shared library, points at the environment the program runs in.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Assembly Language Basics → Memory Organization and Addressing → Memory Hierarchy → Memory Management Fundamentals → Linking and Loading

Longest path: 105 steps · 541 total prerequisite topics

Prerequisites (4)

Leads To (0)

No topics depend on this one yet.