C8 · Lesson 30 · Linking, loading and startup code
How separate source files become one program in memory
Real firmware is dozens of source files and a few libraries, compiled one at a time. When main.c calls uart_send from another file, the compiler has no idea where that function will end up, and when it writes int count = 5; it can't put the 5 in RAM, because RAM forgets everything when the power goes off. The linker and a few dozen instructions of startup code solve both problems. Knowing what they do explains where every byte of a program lives, why a microcontroller's flash and RAM fill up the way they do, and what runs before main.
- This is the startup code a microcontroller runs at reset, before
main. The initial values of.data(7, 42, 1000) sit in flash at 0x1000; the RAM at 0x1100 holds leftover junk at power-up. - Step it. The first loop copies the three initial values into RAM; the second clears the four words of
.bssthat follow. Watch the memory window change. - Then it sets sp and calls
main, which finds its variables ready: a0 = 42 and a1 = 0.
Predict
A program has 20 kB of code, 2 kB of constant tables, 1 kB of initialized variables and 6 kB of variables that start at zero. How much flash does it need?
23 kB: the code, the constants, and the initial values of the 1 kB of variables, which startup code copies into RAM. The 6 kB of zeroed variables take no flash at all, only a size and an address; startup code clears them.
Name it
From source to running program
- The compiler turns each C file into assembly.
- The assembler turns each into an object file: machine code and data in sections, a symbol table (the names it defines and the names it uses but doesn't define), and relocations, notes of every place that needs an address it doesn't know yet.
- The linker combines the object files and libraries, places each section at an address given by a linker script (the chip's memory map), resolves every symbol, and patches every relocation. The result is an executable (an ELF file) or a raw image.
- A loader puts it in memory: an operating system reads it from disk, while a microcontroller has it programmed into flash once.
Sections
| Section | Holds | Lives in |
|---|---|---|
.text | code | flash |
.rodata | constants, string literals, tables | flash |
.data | initialized variables | RAM, with the initial values stored in flash |
.bss | variables that start at zero | RAM only |
| stack, heap | locals, saved registers, dynamic memory | RAM |
so flash = .text + .rodata + .data, and RAM = .data + .bss + stack + heap.
Startup code
Before main, the reset handler copies .data's initial values from flash to RAM, fills .bss with zeros, sets sp, and calls main. C guarantees that globals start at their values or at zero; this is the code that keeps that promise.
Addresses relative to the PC
auipc rd, imm puts PC + imm × 4096 in rd. The assembler writes la rd, symbol as auipc plus addi, splitting the distance from the PC into an upper part and a signed lower 12 bits (rounding the upper part as lui and addi do). The code then works wherever it's loaded, as long as the distance stays the same.
Worked example
A program's map file lists .text 18,400 bytes, .rodata 2,100, .data 600 and .bss 3,200, with a 2,048-byte stack. How much flash and RAM does it use?
Show the solution
- Flash.
.text,.rodataand the initial values of.data: - RAM.
.data,.bssand the stack: - Fit. On a chip with 32 kB of flash and 8 kB of RAM there's room for both, but the RAM is 71% full.
Answer 21,100 bytes of flash and 5,848 bytes of RAM.
Worked example
la a0, table sits at 0x0040, and table is at 0x1830. What do the auipc and addi hold?
Show the solution
- The distance.
- Split it. Its low 12 bits, 0x7F0 = 2032, fit as a positive signed number, so the upper part is 0x17F0 >> 12 = 1.
- The
auipc.auipc a0, 1gives 0x0040 + 0x1000 = 0x1040. - The
addi.addi a0, a0, 2032reaches 0x1830.
Answer auipc a0, 1 and addi a0, a0, 2032.
Worked example
Startup code copies 600 bytes of .data from flash to RAM (5 instructions a word) and zeroes 3,200 bytes of .bss (3 instructions a word). How long does that take at 16 MHz, one instruction a cycle?
Show the solution
- Copying
.data. instructions. - Zeroing
.bss. instructions. - Time. Together 3,150 cycles:
- What it means. That's why a C program's globals already hold their initial values when
mainstarts: the reset handler put them there, and.bsscosts no flash because only its size is stored.
Answer 3,150 cycles, about 197 µs.
Check
A firmware image has 30 kB of .text, 1 kB of .rodata, 2 kB of .data and 10 kB of .bss, and reserves 1 kB of stack.
(a)
How much flash does it need?
(b)
How much RAM does it need?
Check
What does auipc t0, 1 at address 1000 put in t0?
Check
la a0, buf sits at 0040, and buf is at 1800.
(a)
What immediate does the auipc hold?
(b)
What immediate does the addi hold?
Check
Startup code copies 2048 bytes of .data a word at a time, taking 5 cycles per word on a 48 MHz clock. How long does the copy take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
Put the steps that turn two C files into a running microcontroller program in order.
- Program the image into flash
- Assemble each into an object file with relocations
- At reset, copy .data to RAM and clear .bss
- Compile each C file to assembly
- Call main
- Link the object files, placing sections by the linker script
Check
A global int count = 5; is declared in a C file for a microcontroller. Where is count while the program runs, and where does its 5 come from?
Check
Startup copies 400 bytes of .data (5 instructions a word) and zeroes 2000 bytes of .bss (3 a word) at 48 MHz.
(a)
How many instructions does that take?
(b)
How long, at one instruction a cycle?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
A map file lists .text 9000, .rodata 800, .data 900 and .bss 6000 bytes, with a 1024-byte stack.
(a)
How much flash does it use?
(b)
And how much RAM?
Explain why
Why can't the assembler fill in the address of a function defined in another source file, and what fills it in?
Each file is assembled on its own, so the assembler sees only that file: it knows where things are relative to the start of its own sections, but not where the linker will put those sections in memory, and it has never seen the other file at all. So it leaves a placeholder in the instruction and records a relocation saying which symbol belongs there and how to encode it. The linker, which sees every object file and the memory map, decides where every section goes, looks up each symbol's final address, and patches each relocation with the right bits.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.