Assembler, Linker and Loader: What an Object File Holds

A program is written as many files, by many people, and nobody knows at first where anything will sit in memory. Three tools turn those files into words at fixed addresses. This page follows one address through all three.

You Will Be Able To

  • Name what an object file holds, and say why its code has holes.
  • Carry out the three steps of a linker by hand: place the sections, find the symbols, fill the holes.
  • Say what a loader does, and what goes wrong when code runs with its holes unfilled.
Lesson
S2.20
Time
28 min
Runs on
Your browser
Check
6 questions

A function in one file adds 1 to a variable named counter. The variable is defined in another file. When the first file is turned into machine code, nobody knows the address of counter yet. Still its lui and its addi must hold that address in the end.

Before You Start

You need instruction words from S2.2. You need the lui and addi pair, with its correction for bit 11, from S2.18.

You need the memory map of ap_soc from S0.2: the Boot ROM at 0, and the SRAM at 8000_0000.

Five questions this page answers
Question
1What does an object file hold besides machine code?
2Why does the assembler leave holes in the code?
3How does the linker work out the address of a symbol?
4What does the linker write into a hole?
5What does the loader do with the finished image?

The Idea: Decide Addresses Last

A real program is far too large for one file. It is split into many source files, and each one is built alone. That keeps the work small: when one file changes, only that file is built again.

But a file built alone has a problem. Its code uses names from other files, such as a variable or a function. It does not know their addresses. It does not even know its own address, since that depends on what is placed before it.

So the work stops one step early. The assembler writes the machine code of one file into an object file. Where an address is missing, it writes 0 and leaves a note. The note says: this word needs the address of that name. Such a note is a relocation entry.

A name with an address is called a symbol. A second tool, the linker, reads all the object files together. It decides where everything goes. Then every symbol has an address, and the linker fills the holes.

The result is an executable image: words with final addresses, and no holes. A third tool, the loader, puts those words into memory.

New Words

New words on this page
WordMeaning
CompilerThe tool that turns C into assembly.
AssemblerThe tool that turns assembly into machine code, one source file at a time.
File sectionOne block of code or of data that is placed in memory as a whole.
SymbolA name with an address: a function, a variable or a label.
Symbol tableA list of symbols: those that a file defines, with their offsets, and those it needs.
Relocation entryA note beside the code: this word has a hole, and it needs the address of that symbol.
Object fileThe output of the assembler for one source file: sections, a symbol table and relocation entries.
LinkerThe tool that joins object files. It places the sections, finds the address of each symbol and fills the holes.
Code libraryA bundle of object files that are ready to be linked. Library for short.
Executable imageThe output of the linker: every word of the program, final, with its address.
LoaderWhatever puts the executable image into memory, so that the processor can run it.

One Picture: The Path of a Program

From Source Files to a Running ProgramStep through the picture. Each box is a file or a tool. Follow one program from text to memory.
  • Memory
  • Control / FSM
  • Processor
From Source Files to a Running ProgramStep through the picture. Each box is a file or a tool. Follow one program from text to memory. component C source one file for each partof the program component Assembly made by the compiler,or written by hand component Object file machine code withholes, and two lists component Other objectfiles more parts of theprogram, and libraries component Linker places every part andfills the holes component Executable image every word final, eachwith its address component Loader puts the image intomemory component Memory of thechip the processor startsat the firstinstruction compilerassemblerone object filethe othersone imagewords and addresseswrites them
  1. component C sourceone file for each part of the program
    • compiler → Assembly
  2. component Assemblymade by the compiler, or written by hand
    • assembler → Object file
  3. component Object filemachine code with holes, and two lists
    • one object file → Linker
  4. component Other object filesmore parts of the program, and libraries
    • the others → Linker
  5. component Linkerplaces every part and fills the holes
    • one image → Executable image
  6. component Executable imageevery word final, each with its address
    • words and addresses → Loader
  7. component Loaderputs the image into memory
    • writes them → Memory of the chip
  8. component Memory of the chipthe processor starts at the first instruction
  1. The compiler

    The compiler turns one C file into assembly. It follows the calling convention of S2.3, so its functions can be called by anyone.

  2. The assembler

    The assembler turns each line into a word, as in S2.2. Where it needs an address from another file, it leaves a hole and a note.

  3. Many object files

    Each source file becomes an object file of its own. Ready-made functions come as object files too, bundled in libraries.

  4. The linker

    The linker gives every piece of code and data an address. Then every name has an address, and every hole can be filled.

  5. The loader

    The loader puts each word of the image at its address. On a small chip, that means writing the ROM or the flash once.

Read the diagram as text
  • C source: one file for each part of the program
  • Assembly: made by the compiler, or written by hand
  • Object file: machine code with holes, and two lists
  • Other object files: more parts of the program, and libraries
  • Linker: places every part and fills the holes
  • Executable image: every word final, each with its address
  • Loader: puts the image into memory
  • Memory of the chip: the processor starts at the first instruction
  • C source → Assembly: compiler
  • Assembly → Object file: assembler
  • Object file → Linker: one object file
  • Other object files → Linker: the others
  • Linker → Executable image: one image
  • Executable image → Loader: words and addresses
  • Loader → Memory of the chip: writes them

Four tools in a row. Only the linker sees the whole program.

What an object file holds
PartWhat is in itIn the example below
Code sectioninstruction words, some with holesmain.o: six words, two with holes
Data sectionthe first values of variablesdata.o: two words, 100 and 41
Symbol tableeach name, with its section and its offsetmain at code + 0; limit at data + 0; counter at data + 4
Relocation entrieseach hole: where it is, which symbol it needs, and which part of the addressword 0 needs the upper part of counter; word 1 the lower part

An object file gives offsets inside its own sections. It holds no final address.

The Smallest Example, Worked by Hand

Two source files. The first holds a function that adds 1 to counter. The second defines two variables, limit and counter.

main.o, as the assembler wrote it
OffsetWordAssemblyNote
0000000537lui a0, 0hole: the upper part of the address of counter
0400050513addi a0, a0, 0hole: the lower part of the address of counter
0800052583lw a1, 0(a0)reads counter
0c00158593addi a1, a1, 1adds 1
1000b52023sw a1, 0(a0)writes counter
1400100073ebreakthe lab stops here

The two holes hold 0 for now. The assembly is written by hand.

Now do the work of the linker by hand, in three steps.

Step 1: place the sections. Code must be in memory that keeps its content, so it goes to the Boot ROM at 0000_0000. Data must be writable, so it goes into the SRAM. This example puts it at 8000_0800.

Step 2: find the symbols. A symbol is at the base of its section plus its offset. So counter is at 8000_0800 plus 4, which is 8000_0804.

Step 3: fill the holes. Split 8000_0804 as in S2.18. The lower 12 bits are 804. Bit 11 is 1, so the upper part is one more than 80000: it is 80001.

The two holes, filled
WordBeforeWhat goes inAfterNow it reads
00000053780001 into bits 31 to 1280001537lui a0, 0x80001
100050513804 into bits 31 to 2080450513addi a0, a0, -2044

Check: 8000_1000 less 2044 is 8000_0804.

The Rules

  1. Rule 1: The assembler turns one source file into one object file.

    It knows no address outside that file, and not the final place of the file itself.

  2. Rule 2: An object file holds sections, a symbol table and relocation entries.

    Its code has a hole wherever an address is not yet known.

  3. Rule 3: The linker gives each section an address.

    Code goes to memory that keeps its content. Data that changes goes to writable memory.

  4. Rule 4: The address of a symbol is the base of its section plus its offset.

    Only the linker can work this out.

  5. Rule 5: The linker fills each hole with the part of the address that the instruction needs.

    For a lui and addi pair, that is the upper and the lower part, with the correction for bit 11.

  6. Rule 6: The loader puts the executable image into memory.

    Then the processor can start at the first instruction.

Sources for These Rules

Everything about object files, linkers and loaders on this page is stated as commonly described. That covers the parts of an object file, the three steps of the linker and the job of the loader. No document on an object-file format is in the reference library. So none of this is checked against one, and the page names no format. The instruction words were compared with the assembler of this track.

The plan of this track points to two textbooks for this material. They are Patterson and Hennessy, sections 2.12 and 2.13, and Harris and Harris, section 6.5. Those places are not checked either.

The placement of the data at 8000_0800 is a choice of this page. The assembler of this track works differently from the tools on this page. It takes one source and makes the final image at once, with no object files.

Now Run It

The lab at the end of this page holds the two object files as arrays. A linker and a loader follow, as test code. The lab prints each step.

Printed by the lab on Icarus. The words of main.o and the linked words were compared with the assembler of this track.
section | code of main.o | 24 bytes | placed at 00000000
section | data of data.o | 8 bytes | placed at 80000800
symbol | main | code + 0 | 00000000
symbol | limit | data + 0 | 80000800
symbol | counter | data + 4 | 80000804
hole in word 0 | upper part 80001 | 00000537 becomes 80001537
hole in word 1 | lower part 804 | 00050513 becomes 80450513
load | 00000000 | 80001537
load | 00000004 | 80450513
load | 00000008 | 00052583
load | 0000000c | 00158593
load | 00000010 | 00b52023
load | 00000014 | 00100073
load | 80000800 | 00000064
load | 80000804 | 00000029

Rule 3 at work: rows 1 and 2. Each section gets a base.

Rule 4 at work: rows 3 to 5. The symbol counter is 4 bytes above the base of the data.

Rule 5 at work: rows 6 and 7. They are the two rows of the hand example. Only two words of the code change.

Rule 6 at work: the eight rows that start with load. Six words go to the Boot ROM and two to the SRAM.

The image still has to be proved right. The instruction-level model of this track loaded these eight words and ran them.

Printed by the recording script from the instruction-level model, with the image of the lab in its memory.
00000000 | lui a0, 0x80001 | a0 = 80001000
00000004 | addi a0, a0, -2044 | a0 = 80000804
00000008 | lw a1, 0(a0) | a1 = 00000029 from mem[80000804]
0000000c | addi a1, a1, 1 | a1 = 0000002a
00000010 | sw a1, 0(a0) | mem[80000804] = 0000002a
00000014 | ebreak | stop
the word at 80000804 holds 42

Rows 1 and 2 build 8000_0804 in a0. Row 3 reads 29 in hex, which is 41. Row 5 writes 42 back. The function found its variable.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Each file is built aloneThe whole program is built at onceAfter a change, one file is built again, not thousands. A linker is needed.
Holes and relocation entriesFixed addresses written into the sourceCode can be placed anywhere, and placed late. The object file is not runnable by itself.
Code and data in separate sectionsOne block for everythingCode can go to a ROM and data to RAM. The linker needs a memory map.
Symbols by nameNumbers agreed by handFiles from different people fit together. A name defined twice, or never, stops the link.
Libraries of object filesCopying source codeTested code is reused without being built again. Only what is used should be linked in.

The Trap: The Code That Ran with Its Holes

An engineer assembles main.o, and the assembler reports no error. The six words look like a finished program. To save time, the engineer loads those six words at address 0 and starts the processor.

Printed by the recording script from the instruction-level model, with the unlinked words of main.o at address 0.
00000000 | lui a0, 0x0 | a0 = 00000000
00000004 | addi a0, a0, 0 | a0 = 00000000
00000008 | lw a1, 0(a0) | a1 = 00000537 from mem[00000000]
0000000c | addi a1, a1, 1 | a1 = 00000538
00000010 | word 00b52023 | trap, cause 7: store access fault, address 00000000
the word at 80000804 holds 41

Read rows 1 and 2. The holes hold 0, so a0 becomes 0. The program believes that counter is at address 0.

Read row 3. The load reads address 0 and gets 537 in hex. That is no variable: it is the first instruction of the program itself.

Read row 5. The store tries to write address 0, which is in the Boot ROM. The processor takes a trap for a store access fault. The last row shows that counter still holds 41.

On this chip the mistake is caught, because the ROM cannot be written. With the code in RAM, the store would succeed and change the first instruction. The check is rule 2: an object file is not a program until the linker has filled its holes.

At Scale

This page2 object files, 3 symbols, 2 holes
The software of a phonethousands of object files, millions of holes

This section looks ahead. The same three steps build the software of every chip, from a watch to a server.

  1. The linker script

    The linker must be told the memory map: where ROM and RAM are, and how large. For ap_soc that is the map of S0.2. Module S6 writes such a file.

  2. Data that starts in ROM

    On a chip, RAM is empty at power-on. The first values of the data are kept in ROM, and start-up code copies them. Module S6 covers that copy.

  3. Calls between files

    A call of a function in another file is a hole too. The linker fills it with the distance to the function, as an offset.

  4. Loading on a large system

    An operating system loads programs while it runs, and may fill some holes at that moment. Common practice.

Try It

Run the lab. Match the two rows that start with "hole" with the hand example.

Move the data to 8000_0400. Predict both patched words before you run it. Does the upper part need the correction?

Make the function use limit in place of counter. Which line of the linker must change, and which words of the image?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: section | code of main.o | 24 bytes | placed at 00000000 section | data of data.o | 8 bytes | placed at 80000800 symbol | main | code + 0 | 00000000 symbol | limit | data + 0 | 80000800 symbol | counter | data + 4 | 80000804 hole in word 0 | upper part 80001 | 00000537 becomes 80001537 hole in word 1 | lower part 804 | 00050513 becomes 80450513 load | 00000000 | 80001537 load | 00000004 | 80450513 load | 00000008 | 00052583 load | 0000000c | 00158593 load | 00000010 | 00b52023 load | 00000014 | 00100073 load | 80000800 | 00000064 load | 80000804 | 00000029

Check Yourself

Predict the outputThis is the lab with the data placed at 8000_1000, so counter is at 8000_1004. It prints only the two patched words. What does it print?
// Two object files, and a linker and a loader as test code.
module tb;
  logic [31:0] code [0:5];      // the code of main.o: six words, two of them with a hole
  logic [31:0] vars [0:1];      // the data of data.o: two words
  logic [31:0] code_base, data_base, counter;

  // Fill a hole in a lui: the upper 20 bits of the address. One more when bit 11 is 1 (see S2.18).
  task automatic fill_upper(input int n, input logic [31:0] address);
    logic [31:0] old;
    logic [19:0] upper;
    old = code[n];
    upper = address[31:12] + address[11];
    code[n] = {upper, old[11:0]};
      endtask
  // Fill a hole in an addi: the lower 12 bits of the address.
  task automatic fill_lower(input int n, input logic [31:0] address);
    logic [31:0] old;
    old = code[n];
    code[n] = {address[11:0], old[19:0]};
      endtask

  initial begin
    // ---- What the assembler wrote into main.o. It did not know where counter would be. ----
    code[0] = 32'h0000_0537;   // main: lui  a0, 0         hole: the upper part of the address of counter
    code[1] = 32'h0005_0513;   //       addi a0, a0, 0     hole: the lower part of the address of counter
    code[2] = 32'h0005_2583;   //       lw   a1, 0(a0)
    code[3] = 32'h0015_8593;   //       addi a1, a1, 1
    code[4] = 32'h00b5_2023;   //       sw   a1, 0(a0)
    code[5] = 32'h0010_0073;   //       ebreak
    // ---- What the assembler wrote into data.o ----
    vars[0] = 32'd100;         // limit:   at offset 0 of the data
    vars[1] = 32'd41;          // counter: at offset 4 of the data
    // ---- The linker, step 1: give each section an address ----
    code_base = 32'h0000_0000;                  // code goes to the Boot ROM
    data_base = 32'h8000_1000;                  // data goes to the SRAM
    // ---- step 2: a symbol is at the base of its section plus its offset ----
    counter = data_base + 4;
    // ---- step 3: fill the holes that main.o listed ----
    fill_upper(0, counter);
    fill_lower(1, counter);
    $display("word 0 = %08h, word 1 = %08h", code[0], code[1]);
  end
endmodule
ConceptWhat does an object file hold besides machine code and data?
ConceptWhy does the assembler leave holes in the code?
ConceptThe data section is placed at 8000_2000. A symbol is at offset 12 in it. What is its address?
ConceptWhat does the linker write into the hole of a lui?
ConceptWhat does the loader do?

Common Mistakes

Wrong Run an object file as it is.

Its holes hold 0. Loads and stores go to wrong addresses.

Fix Link first. Only the executable image has final words.

Wrong Expect the assembler to know the address of a symbol in another file.

It sees one file. Addresses are decided later.

Fix Let it leave a relocation entry. The linker fills the hole.

Wrong Place writable data in the ROM, beside the code.

Every store to it fails, or is lost.

Fix Give the linker the memory map: code to ROM, changing data to RAM.

Remember

  • The assembler works on one file alone. It leaves a hole wherever an address is not yet known.
  • An object file holds sections, a symbol table and relocation entries.
  • The linker places the sections, works out every symbol as base plus offset, and fills the holes.
  • The loader puts the finished image into memory. Code with unfilled holes must never run.

Practice

Report a problem with this page