Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Loaders

Loaders place executable code and data into memory, resolve addresses and symbols, and prepare a program image so the CPU can start execution.

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

A loader is a piece of system software used whenever a program runs. Engineers working on operating systems, embedded firmware, runtime systems, boot code, or toolchains implement or use loaders to place programs and libraries into memory, resolve symbol references, and ensure correct address values before execution.

Key ideas

Start simple: a program built by an assembler or compiler produces an object module: machine code plus metadata that describes addresses inside the module and references to symbols defined elsewhere. The loader's job is to convert the object module(s) into a memory image ready to run.

Definitions (bolded where first used):

  • Loader: a program that places an executable module into memory and prepares it for execution by resolving addresses and applying relocations.
  • Absolute loader: a loader that simply places the program at specified absolute addresses without performing relocation or external symbol resolution (assumes addresses are already fixed).
  • Relocation: the process of modifying addresses in code or data because the module is loaded at a different base address than the assembly-time origin.
  • Relocating loader: a loader that supports relocation; it applies a relocation constant equal to (load base R - assembly-time origin O) to adjustable addresses in each control section (when O = 0, this reduces to adding the base address R).
  • Linking loader: a loader that also performs linking: it resolves external references between multiple object modules before or during loading (often in two passes).
  • Bootstrap (boot) loader: a minimal loader that runs at system start (firmware or ROM) to load the operating system kernel.
  • Dynamic loader / dynamic linker: resolves and loads shared libraries at program start or runtime, possibly performing relocations lazily.
  • Symbol table / external symbol table (ESTAB): a data structure the loader uses to map symbol names to absolute addresses.
  • Relocation record / modification record: an entry in the object module that tells the loader which bytes must be modified when relocation is applied.

Exact statements and simple derivations commonly asked in exams:

  1. Relocation arithmetic. If a module was assembled assuming origin 0 and contains an address field A (a.k.a. relative address or displacement), and the loader places the module at base address R (bytes), the relocated address A' written into memory must be: A' = A + R Assumption: A is unsigned or two's complement value large enough to hold the relocated address; R and A are measured in bytes. If A encodes a relative reference (e.g., PC-relative), the relocation action depends on the addressing mode; for absolute fields the above formula applies directly.

  2. Linking-loader two-pass algorithm (exact university-style statement):

    • Pass 1: Read all object modules' headers to determine each control section's length and assign each control section a load address; process Define (D) records to build the ESTAB mapping each external symbol to its absolute address = (control section load address + symbol's offset).
    • Pass 2: Re-read object modules; for each Text (T) record, copy the code/data into memory at the computed absolute addresses; for each Modification (M) record (relocation entry), apply the relocation action (add or subtract the ESTAB value as specified); perform external reference resolution by replacing reference fields with the ESTAB values—if the format uses Refer (R) records to number external symbols, use those numbers to look up names in ESTAB. Assumptions: object-file format provides header, text, modification and end records; control sections have known lengths.
  3. Dynamic loading concept: a program image may contain references to shared-library symbols; the dynamic loader locates the required shared object, maps it into the process address space (possibly at preferred base or at a different base), and applies relocations. Lazy binding delays resolving a symbol until first call (requires a runtime trampoline).

What to draw in an exam diagram (describe exactly):

  • Draw a vertical memory address column at left with increasing addresses downwards. Mark three contiguous ranges and label them "Module A: base = 0x2000, length = 0x0050", "Module B: base = 0x2050, length = 0x0030", and "Unallocated". Inside Module A draw an arrow from offset 0x0010 labeled "symbol X". In Module B at offset 0x0020 draw a reference arrow labeled "ref to X" pointing to the module A symbol (draw the patch action). Also draw a small table at the side labelled ESTAB with entries: X -> 0x2010, A (module) -> 0x2000, B -> 0x2050.

Formulas

A' = A + R — A' is the relocated absolute address in memory (bytes), A is the original address/displacement in the object module (bytes), R is the relocation constant / load base address chosen by the loader (bytes). Applies to absolute address fields in relocation records. If the control section was assembled with a nonzero origin O (bytes), use the general form A' = A + (R - O). Symbol_addr = Section_base + Symbol_offset — Symbol_addr is the absolute address assigned to a symbol when building the ESTAB (bytes); Section_base is the control section's load address (bytes); Symbol_offset is the symbol's offset inside the section (bytes). Applies during Pass 1 of a two-pass linking loader. addr_aligned = ceil(addr / align) * align — rounds an address up to the required alignment (bytes). Useful when loader must align modules or sections (align is e.g., 4 for word, 4096 for page). PC_relative_disp = Target - (PC + instr_len) — if an instruction uses PC-relative addressing, the loader or linker may compute the displacement from the instruction's next PC (bytes). Applies when assemblers or linkers generate PC-relative relocations.

For syntax to remember (object file records often used in teaching): header, text, define, refer, modification, end — typically written as H, T, D, R, M, E records in SIC/XE-style formats. The End (E) record often carries the program's starting address or entry symbol. Verify the exact record format used by your course toolchain.

Worked examples

Example 1 — Relocating addresses (numeric): Given: a single object module assembled at origin 0. Loader places module at base R = 0x4000 (hex). Inside module are absolute addresses A1 = 0x0030, A2 = 0x1000. Compute relocated addresses. Step 1 (formula): A' = A + R. Step 2: A1' = 0x0030 + 0x4000 = 0x4030 (bytes). Step 3: A2' = 0x1000 + 0x4000 = 0x5000 (bytes). Answer: relocated addresses are 0x4030 and 0x5000.

Example 2 — Two-module linking-loader trace (symbol resolution): Given: Module A length = 0x50, defines symbol X at offset 0x10. Module B length = 0x30, has a reference to X at offset 0x20 inside B. Loader will place A at 0x2000 and B immediately after at 0x2050. Pass 1 (build ESTAB):

  • Assign A base = 0x2000. ESTAB: A -> 0x2000, X -> 0x2000 + 0x10 = 0x2010.
  • Assign B base = 0x2050. ESTAB add: B -> 0x2050. Pass 2 (load and modify):
  • Copy A and B bytes to memory at their bases.
  • For B's reference at offset 0x20: the loader reads the reference (originally symbolic), looks up X in ESTAB = 0x2010, and patches the bytes at memory address 0x2050 + 0x20 = 0x2070 with the value 0x2010. Final: memory contains both modules; any instruction in B that referenced X now uses absolute address 0x2010.

Example 3 — Dynamic loader scenario (short model-answer question): Question: Explain how lazy binding works and what the loader must set up. Model answer: The dynamic loader maps the shared object and creates a Procedure Linkage Table (PLT) entry and a Global Offset Table (GOT) slot for each external function. The PLT entry initially jumps to a resolver stub which calls the dynamic linker's resolution routine; the resolver looks up the symbol, fills the GOT slot with the symbol's actual address, and then transfers control to the target. Subsequent calls reach the PLT which reads the GOT and jumps directly to the resolved address, so only the first call pays the resolution cost.

Common mistakes

  • Using the wrong sign in relocation (adding vs subtracting) when resolving external references — always apply the action specified by the relocation record or the two-pass algorithm description.
  • Forgetting to account for module base addresses when computing a symbol's absolute address (use Section_base + Symbol_offset).
  • Not carrying units (bytes / hex) and mixing decimal and hex without conversion.
  • Drawing memory growing in the wrong direction in diagrams (clearly label increasing addresses).
  • Ignoring alignment requirements when placing sections or stacks.

For your exam

Typical question types:

  • Definitions and short notes: define loader types (absolute, relocating, linking, dynamic), state differences. Full marks: concise definition plus one-line example.
  • Diagram + explanation: show how two modules get loaded and how a symbol reference is patched. Full marks: clear labeled diagram (see drawing instructions above), step-by-step ESTAB construction and modification steps.
  • Derivation: show relocation formula and apply it to a small list of addresses. Full marks: write the formula, explain assumptions, compute each relocated address showing arithmetic with units.
  • Algorithm trace: simulate a two-pass loader on given object records (H/T/M/E). Full marks: show Pass 1 ESTAB entries and addresses, Pass 2 copy+modify actions and final memory bytes.
  • Short theory: explain dynamic vs static linking and lazy binding. Full marks: contrast when linking happens, advantages/disadvantages (memory sharing, update ease, startup cost), and mention PLT/GOT if asked.

Quick check

  1. What is the primary purpose of a relocation record in an object module?
  2. In a two-pass linking loader, what does Pass 1 compute and store in the ESTAB?
  3. Give the relocation formula used when a module assembled for origin 0 is loaded at base R. Answers: 1) To tell the loader which bytes must be adjusted when the module is placed at a base address. 2) The absolute load addresses of control sections and external symbols (mapping symbol names to absolute addresses). 3) A' = A + R.

Note where I was not 100% certain: some courses teach specific object-file record letters (H, T, M, E) using the SIC/XE or other tutorial format — the general loader algorithms are independent of those letters, but please verify the exact record syntax your course expects.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?