# Linkers and Loaders

> Computer Science · CIE A-Level 9618
> Source: https://www.owlsprep.com/study/cie-9618-u5-linkers-and-loaders/

This module explains the roles of linkers and loaders, the final stages of the program compilation process before execution. You will learn static and dynamic linking, plus common program loading methods for CIE A-Level Computer Science.

**Prerequisites:** [Compilation and assembly process](https://www.owlsprep.com/study/cie-9618-u5-compilation-process/); [Main memory organization](https://www.owlsprep.com/study/cie-9618-u4-memory-organization/)

## Learning objectives

- Distinguish between the core roles of linkers and loaders in program execution
- Compare and contrast static and dynamic linking
- Describe the three main types of program loading
- Identify common errors and misconceptions related to linkers and loaders

## 1. Role of the Linker

**Linker** — A system program that combines multiple independently compiled object files produced by an assembler into a single executable program (or library) by resolving references between them.

*Example:* A C program that uses the standard math library will have references to `sqrt()` that the linker resolves to precompiled library code.

When building large programs, source code is split across multiple files for easier development. Each file is compiled separately into an object file, which contains its own code and data, plus references to symbols (functions, variables) defined in other files or external libraries.

The linker performs three core tasks:

1. Combine all code and data sections from input object files into a single executable image
2. Resolve symbolic references by assigning unique memory addresses to each defined symbol
3. Relocate code and data to match their final positions in the executable

**Worked example:** A program has two object files: `main.o` (contains the main function that calls `calculate()`) and `calc.o` (contains the definition of `calculate()`). Describe the steps the linker takes.

1. 1. Read the symbol tables from both `main.o` and `calc.o`
2. 2. Identify the undefined reference to `calculate()` in `main.o`, and match it to the definition in `calc.o`
3. 3. Combine the code and data sections from both files into a single contiguous executable image
4. 4. Assign a final memory address to `calculate()`, and update the reference in `main.o` to point to this address
5. 5. Output the complete executable file ready for loading into memory

> **Exam tip:** Exam questions often ask to list the three core tasks of a linker, so memorize these clearly

## 2. Static vs Dynamic Linking

Linking can be done either at compile time (static linking) or at load/run time (dynamic linking). The table below compares the two approaches:

| Feature | Static Linking | Dynamic Linking |
| --- | --- | --- |
| Performed when | Compile-time, before execution | Load-time or run-time |
| Executable size | Larger, all library code included | Smaller, only references included |
| Library updates | Requires full recompile to update | Update library without recompiling program |
| Compatibility | No dependency issues, self-contained | Can fail if wrong library version is present |
| Memory usage | Each program has its own copy of library code | Multiple programs share one copy of library code |

**Worked example:** A developer is distributing a desktop application to thousands of users. Should they use static or dynamic linking for shared system libraries? Explain why.

1. The developer will use dynamic linking
2. Dynamic linking produces a much smaller executable file, reducing download size for users
3. It allows the operating system to share a single copy of common system libraries between multiple running applications, saving main memory
4. Security updates to shared libraries can be applied by the operating system without requiring users to download a new version of the application

## 3. Role of the Loader

**Loader** — A system program that loads an executable program from secondary storage into main memory, and prepares it for execution by the CPU.

*Example:* When you double-click an application on your computer, the operating system's loader loads the application into RAM to run it.

After the linker produces the executable file, it is stored on secondary storage. When the program is launched, the loader carries out these core tasks:

1. Read the executable header to get the size of code, data and stack segments
2. Allocate enough free space in main memory for the entire program
3. Copy the code and data from the executable into the allocated memory
4. Resolve any remaining dynamic linking references to shared libraries
5. Set up the program's stack and heap
6. Jump to the program's entry point to start execution

**Worked example:** Explain the loader's role when you launch a dynamically linked web browser on your computer.

1. 1. The user requests to launch the browser, so the operating system calls the loader
2. 2. The loader reads the browser's executable header, and allocates enough main memory for the browser's code and data
3. 3. The loader copies the browser's core code from the executable file into the allocated memory
4. 4. The loader locates all required shared libraries (e.g. graphics, network libraries) that the browser uses
5. 5. The loader loads any missing libraries into memory, and resolves all references to library functions
6. 6. The loader sets up the browser's stack and heap, then jumps to the browser's entry point to start execution

## 4. Common Types of Loading

There are three main types of program loading you need to know for CIE A-Level:

- **Absolute loading**: The linker assigns fixed memory addresses to the program, so the loader loads it into those exact preassigned addresses. Simple, but does not support multi-programming.
- **Relocatable loading**: The program can be loaded into any free block of main memory, and the loader adjusts all address references to match the starting address of the block. Supports multi-programming.
- **Dynamic loading**: A program module is only loaded into memory when it is called by the running program. Unused modules are never loaded, which saves memory.

**Worked example:** An embedded system with only 64KB of memory runs a large program with many optional features (e.g. a printer driver only used when a printer is connected). Which loading method should it use, and why?

1. The embedded system should use dynamic loading
2. Dynamic loading only loads modules when they are needed, so the optional printer driver is only loaded if a printer is actually connected
3. This saves valuable limited main memory space for active program components, and avoids wasting memory on unused code

## Common pitfalls

- **Wrong:** Confusing linker and loader roles, stating the loader resolves static references
  - Why it fails: Static references are resolved before the executable is saved, so this is done by the linker
  - Correct: Linker resolves static references and produces the executable; loader loads the executable into memory. Only dynamic references are resolved by the loader
- **Wrong:** Claiming static linking produces smaller executables than dynamic linking
  - Why it fails: Static linking copies all used library code into the executable, which makes it larger
  - Correct: Dynamic linking produces smaller executable files, as shared library code is not included in the executable
- **Wrong:** Confusing dynamic loading and dynamic linking as the same concept
  - Why it fails: Dynamic linking refers to when reference resolution happens, while dynamic loading refers to when modules are loaded into memory
  - Correct: Dynamic linking is about resolving symbolic references; dynamic loading is about when modules are loaded into memory, they are separate concepts
- **Wrong:** Stating absolute loading supports multi-programming
  - Why it fails: Absolute loading uses fixed preassigned addresses, so multiple programs cannot run at the same time if they share address space
  - Correct: Relocatable loading enables multi-programming by allowing programs to be loaded into any free memory block and adjusting addresses accordingly

## Cheatsheet

| Component/Type | Core Role | Key Feature |
| --- | --- | --- |
| Linker | Combine object files, resolve references | Outputs finished executable file |
| Loader | Load executable to main memory | Prepares program for CPU execution |
| Static Linking | Link at compile time | Self-contained, larger executable |
| Dynamic Linking | Link at load/run time | Shared libraries, smaller executable |
| Absolute Loading | Load to fixed addresses | No multi-programming support |
| Relocatable Loading | Load to any free block | Enables multi-programming |
| Dynamic Loading | Load module when called | Saves main memory for large programs |

## What's next

Linkers and loaders are the final stages of the compilation process, turning your written source code into a running program on your computer. They are core components of system software, working alongside operating systems to manage program execution and main memory usage. This topic is frequently tested in CIE A-Level 9618 Paper 1, often with questions asking to compare linking approaches or describe the key functions of a loader. Understanding how linkers and loaders work also helps you troubleshoot common real-world errors like missing shared libraries on Linux or Windows systems. Next, you can build on this knowledge by exploring other core system software topics, memory management techniques, and operating system process management.

- [Types of System Software](https://www.owlsprep.com/study/cie-9618-u5-types-of-system-software/)
- [Virtual Memory](https://www.owlsprep.com/study/cie-9618-u5-virtual-memory/)
- [File management](https://www.owlsprep.com/study/cie-9618-u5-file-management/)

---

From [OwlsPrep](https://www.owlsprep.com) — free study guides for A-Level, IB, AP and IGCSE, written against the official syllabus. Canonical page: https://www.owlsprep.com/study/cie-9618-u5-linkers-and-loaders/
