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.
| Question | |
|---|---|
| 1 | What does an object file hold besides machine code? |
| 2 | Why does the assembler leave holes in the code? |
| 3 | How does the linker work out the address of a symbol? |
| 4 | What does the linker write into a hole? |
| 5 | What 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
| Word | Meaning |
|---|---|
| Compiler | The tool that turns C into assembly. |
| Assembler | The tool that turns assembly into machine code, one source file at a time. |
| File section | One block of code or of data that is placed in memory as a whole. |
| Symbol | A name with an address: a function, a variable or a label. |
| Symbol table | A list of symbols: those that a file defines, with their offsets, and those it needs. |
| Relocation entry | A note beside the code: this word has a hole, and it needs the address of that symbol. |
| Object file | The output of the assembler for one source file: sections, a symbol table and relocation entries. |
| Linker | The tool that joins object files. It places the sections, finds the address of each symbol and fills the holes. |
| Code library | A bundle of object files that are ready to be linked. Library for short. |
| Executable image | The output of the linker: every word of the program, final, with its address. |
| Loader | Whatever puts the executable image into memory, so that the processor can run it. |
One Picture: The Path of a Program
- Memory
- Control / FSM
- Processor
- 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.
- 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.
- Many object files
Each source file becomes an object file of its own. Ready-made functions come as object files too, bundled in libraries.
- 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.
- 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.
| Part | What is in it | In the example below |
|---|---|---|
| Code section | instruction words, some with holes | main.o: six words, two with holes |
| Data section | the first values of variables | data.o: two words, 100 and 41 |
| Symbol table | each name, with its section and its offset | main at code + 0; limit at data + 0; counter at data + 4 |
| Relocation entries | each hole: where it is, which symbol it needs, and which part of the address | word 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.
| Offset | Word | Assembly | Note |
|---|---|---|---|
| 00 | 00000537 | lui a0, 0 | hole: the upper part of the address of counter |
| 04 | 00050513 | addi a0, a0, 0 | hole: the lower part of the address of counter |
| 08 | 00052583 | lw a1, 0(a0) | reads counter |
| 0c | 00158593 | addi a1, a1, 1 | adds 1 |
| 10 | 00b52023 | sw a1, 0(a0) | writes counter |
| 14 | 00100073 | ebreak | the 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.
| Word | Before | What goes in | After | Now it reads |
|---|---|---|---|---|
| 0 | 00000537 | 80001 into bits 31 to 12 | 80001537 | lui a0, 0x80001 |
| 1 | 00050513 | 804 into bits 31 to 20 | 80450513 | addi a0, a0, -2044 |
Check: 8000_1000 less 2044 is 8000_0804.
The Rules
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.
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.
Rule 3: The linker gives each section an address.
Code goes to memory that keeps its content. Data that changes goes to writable memory.
Rule 4: The address of a symbol is the base of its section plus its offset.
Only the linker can work this out.
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.
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.
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 | 00000029Rule 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.
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 42Rows 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Each file is built alone | The whole program is built at once | After a change, one file is built again, not thousands. A linker is needed. |
| Holes and relocation entries | Fixed addresses written into the source | Code can be placed anywhere, and placed late. The object file is not runnable by itself. |
| Code and data in separate sections | One block for everything | Code can go to a ROM and data to RAM. The linker needs a memory map. |
| Symbols by name | Numbers agreed by hand | Files from different people fit together. A name defined twice, or never, stops the link. |
| Libraries of object files | Copying source code | Tested 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.
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 41Read 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 section looks ahead. The same three steps build the software of every chip, from a watch to a server.
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.
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.
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.
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?
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 | 00000029Check Yourself
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.