Before Main: Start-Up Code and the Linker Script
Between reset and the first line of main, about a hundred instructions build the machine that C expects. Read them on the chip, find the sections the linker placed, and learn what the bench can and cannot see.
You Will Be Able To
- Explain what the core holds after reset and why C cannot run yet.
- Read a linker script: regions, sections, load and run addresses, and the symbols it gives start-up code.
- Find the stack set-up, the .data copy and the .bss clear in a chip retire trace.
- Say which start-up faults a back-door-loaded test can hide.
- Lesson
- V0.4
- Time
- 26 min
- Runs on
- Your browser
- Check
- 5 questions
A chip test fails, and not one line of its C ever ran. The console is empty and main was never entered. Whatever went wrong happened in the 119 instructions between reset and main, in code nobody on the team wrote this year.
- Memory
- Processor
- Data
- The image in ROM
The linker puts the code, the constants and a copy of every first value of .data in ROM. ROM keeps them with the power off.
- A bare core
After reset the core knows only where to fetch. On ap_rv32 the stack pointer and the trap vector hold 0; the RISC-V standard does not fix them. SRAM holds nothing it can trust.
- Set the stack
The first useful write puts the top of SRAM in sp. No function can be called before it.
- Copy .data
Variables with a first value run in SRAM, but their values were stored in ROM. A loop copies them across.
- Clear .bss
Variables without a first value must start at 0. A second loop stores zeros over the whole section.
- Call main
Only now is the machine what C expects. The kit tells the bench "main entered" and calls main.
Read the diagram as text
- Boot ROM, 4 KiB: at 0x0000_0000: code, constants, first values of .data
- Core after reset: pc = reset vector; sp and mtvec hold 0
- .data, in SRAM: at 80000000: limit, step (8 bytes)
- .bss, in SRAM: 80 bytes that must start at 0
- Stack: from 0x8001_0000, growing down
- main: the first line of C
- Boot ROM, 4 KiB → .data, in SRAM: copy first values
- Core after reset → .bss, in SRAM: store zeros
- Core after reset → Stack: set sp
- Core after reset → main: call main
Addresses from the link of the lab program (riscv64-unknown-elf-gcc 14.2.0, -Os). ROM layout: the chip boots from ROM.
Before You Start
Read The Generated Header first. The SoC Architecture track teaches start-up code on a small model in Start-Up Code and the linker script in The Linker Script. This page runs the firmware kit's own start-up code on the whole chip.
| Question | |
|---|---|
| 1 | Why does a chip need code before main? |
| 2 | What does a linker script decide? |
| 3 | What do the load address and the run address of .data mean? |
| 4 | How does the retire trace show each start-up step? |
| 5 | Which start-up faults can a back-door-loaded test hide? |
The Idea: Reset Leaves C Without a Stack or Its Variables
Reset gives the core one fact: the address of its first instruction. Everything else C takes for granted is missing. There is no stack, so no function can be called. Variables with a first value, such as limit = 5, have no 5 in SRAM yet. Variables without one hold whatever SRAM woke up with, not 0.
Start-up code closes that gap. It is a short assembly routine, run once per reset. It sets the stack pointer and the trap vector, copies first values into SRAM and clears the rest. The linker script tells it where everything is. On a chip bench it has one more job. It tells the bench that main has started, so a pass word means the test really ran.
Why does verification care about code this small? Because every test runs it. A fault in start-up code fails every test at once. Or worse, it fails no test on the bench and every board in the field.
New Words
| Word | Meaning |
|---|---|
| Start-up code | The code between reset and main: the kit's crt0.S. It sets sp, gp and mtvec, copies .data, clears .bss and calls main. |
| Linker script | The file that tells the linker where memory is and where each section goes: the kit's link.ld. |
| Section | A named block of the program the linker places as one piece: .text for code, .data, .bss. |
| .data | Variables with a first value. They run in SRAM, so their first values must be copied there. |
| .bss | Variables without a first value. C promises they start at 0, so start-up code stores zeros over them. |
| Load address and run address | Where the image keeps a section, and where the program uses it. For .data in ROM layout they differ. |
| ROM layout and SRAM layout | The kit's two images: boot from ROM, or start in SRAM with the image placed there by the bench. |
| Back-door load | The bench writes an image straight into ROM and SRAM before reset, with no bus transfer. |
The Smallest Example: One Program, Four Variables
The lab program has two variables with first values and two without. Its main reads them and returns 0 only when the sums come out right.
/* Two variables with first values (.data) and two that start at 0 (.bss). */
volatile unsigned limit = 5, step = 3;
volatile unsigned count, total;
int main(void)
{
volatile unsigned increment = step; /* a local: it lives on the stack */
count += increment;
total = limit + count;
return count != 3 || total != 8; /* 0 means pass */
}
The linker placed them as the table shows. Check it by hand: .data runs from 0x8000_0000 to 0x8000_0008, two words. The copy loop must make two stores. The .bss section runs from 0x8000_0008 to 0x8000_0058: 0x50 bytes, which is 20 words.
| Symbol | Address | What it is |
|---|---|---|
| _start | 00000000 | The first instruction: the reset vector of the ROM layout |
| main | 0000021c | In ROM, after the start-up code and the kit |
| __data_load | 00000288 | Load address of .data: the first values, kept in ROM |
| __data_start, __data_end | 80000000, 80000008 | Run address of .data: step, then limit |
| __bss_start, __bss_end | 80000008, 80000058 | .bss: the kit's interrupt table, then total and count |
| __stack_top | 80010000 | One byte past the end of the 64 KiB SRAM |
From the symbol table apcc wrote for this program (riscv64-unknown-elf-gcc 14.2.0, -Os). Another compiler may place things differently.
Two words of .bss are the program's own. The other 18 belong to the kit. Its table of interrupt handlers lives in .bss, because no handler is set until the program sets one. A start-up fault there would break interrupts, not main.
The Linker Script Decides the Addresses
Start-up code never contains an address. It uses symbols such as __bss_start, and the linker script gives them their values. Here is the part of the kit's script that matters for start-up.
MEMORY {
ROM (rx) : ORIGIN = ROM_BASE, LENGTH = ROM_SIZE
RAM (rwx) : ORIGIN = RAM_BASE, LENGTH = RAM_SIZE
}
REGION_ALIAS("PROGRAM", ROM) /* SRAM layout: PROGRAM is RAM */
SECTIONS {
.text : { ... } > PROGRAM
.data : { __data_start = .; ... __data_end = .; } > RAM AT> PROGRAM
__data_load = LOADADDR(.data);
.bss (NOLOAD) : { __bss_start = .; ... __bss_end = .; } > RAM
__stack_top = ORIGIN(RAM) + LENGTH(RAM);
}Read > RAM AT> PROGRAM as two addresses. The section runs in RAM, but the image keeps it in PROGRAM. In the ROM layout those differ, so start-up code must copy. In the SRAM layout PROGRAM is RAM, the two addresses are equal, and the kit's crt0 skips the copy.
NOLOAD on .bss says the image holds nothing for it. Zeros need no storage; a loop can make them. And __stack_top puts the stack at the very top of SRAM, so it grows down toward .bss with all the free space between.
The Rules
Rule 1: The stack comes first.
No call is possible until sp points into SRAM.
Rule 2: Copy .data from its load address to its run address.
In the SRAM layout the two are equal and the copy does nothing.
Rule 3: Clear every word of .bss.
Both ends matter: from __bss_start up to, but not including, __bss_end.
Rule 4: Addresses come from the linker script.
Start-up code uses symbols, so a new map changes no instruction.
Rule 5: Tell the bench where you are.
TB_STATUS = 1 in the ROM image, 2 when main starts; a pass word without the 2 is not a pass.
Sources for These Rules
The kit's start-up code and linker script: tools/soc/fwkit/crt0.S and link.ld (docs/soc-verification-plan.md section 3.3).
The bench rule "pass without main": facts 27.12 and the bench manual (tools/soc/ap_soc/bench/soc_tb/README.md).
The GNU linker documents MEMORY, SECTIONS, AT> and NOLOAD. Not in the reference library; common practice.
After reset, ap_rv32 clears its registers and mtvec (tools/soc/ap_rv32/rtl/ap_rv32_regfile.sv; facts ledger DIG-111). The RISC-V privileged specification does not fix the reset value of mtvec (RV-PRIV 3.4).
Now Run It: Reset to Main on the Chip
The lab program ran on the chip bench in the ROM layout, with the retire trace on. Each row is one instruction the core retired: the cycle, the pc, the instruction, and what it changed. Here are the rows that matter, out of the first 120.
12 | 0000000c | addi sp, sp, -8 | x2 = 80010000
15 | 00000018 | csrrw zero, mtvec, t0 |
23 | 00000028 | sw t1, 0(t0) | mem[10004020] = 00000001
33 | 0000004c | sw t3, 0(t1) | mem[80000000] = 00000003
43 | 0000004c | sw t3, 0(t1) | mem[80000004] = 00000005
57 | 0000006c | sw zero, 0(t0) | mem[80000008] = 00000000
171 | 0000006c | sw zero, 0(t0) | mem[80000054] = 00000000
183 | 00000084 | sw t1, 0(t0) | mem[10004020] = 00000002
184 | 00000088 | jal ra, 0x0000021c | x1 = 0000008c
187 | 0000021c | lui a5, 0x80000 | x15 = 80000000Walk them with the rules. The first two rows set sp to 8001_0000 and mtvec to the kit's trap entry: rule 1. At cycle 23 the ROM image writes 1 to TB_STATUS: rule 5. Two stores then copy 3 and 5 from ROM to 8000_0000 and 8000_0004: rule 2.
Twenty stores of 0 follow, from 8000_0008 to 8000_0054: rule 3. At cycle 183 the kit writes 2, and main's first instruction retires at cycle 187. The run passed, with all 150 records equal to the reference model's.
The same program in the SRAM layout made 0 copy stores: the bench had placed .data at its run address already. It still made 20 clear stores, because .bss is never in the image.
Why It Is Built This Way
The bench could do all of this itself. It could load .data at its run address and zero .bss by the back door, and skip start-up code. Tests would start sooner. Some teams do exactly that for speed.
But silicon has no bench. On a board, the only thing between reset and main is this code, so the chip test must run it, every time. The kit keeps one start-up file for both layouts. The SRAM layout saves only the copy, and the trace shows exactly which steps ran.
The Trap: Zeros for Free
Here is the catch. The bench loads SRAM with an image, and every word the image does not name is 0. So on the first boot of a back-door-loaded test, .bss is already zero, whether start-up code cleared it or not.
A start-up file that forgets the clear, or stops one word short, passes every such test. The pass word proves nothing about rule 3. The trace does: count the clear stores and compare them with the size of .bss. The practice problem shows a chip where the zeros stop being free.
At Scale
This section looks ahead. The lab image has three sections and 119 instructions before main.
Tables of sections
A product has several regions to copy and several to clear, often in more than one memory. Start-up code walks a table that the linker script builds.
Boot from flash
A real chip often copies its whole program from an outside flash first. Module V9 tests that path through the SPI flash model.
One stack per processor
On a chip with several processors, each runs start-up code and needs a stack of its own.
Try It
Run the slice. It reads the stores of the recorded trace and sorts them by the linker's symbols.
Find the record numbers of the two .data stores, then find them in the trace rows above.
Change BSS_END to one word less. Predict the clear count before you run it.
record 10: TB_STATUS = 1 record 19: .data copy, 80000000 = 00000003 record 25: .data copy, 80000004 = 00000005 record 117: TB_STATUS = 2 .bss clear: 20 stores of 0, from 80000008 to 80000054 before main: 2 copy stores, 20 clear stores, 119 recordsCheck Yourself
Common Mistakes
Wrong Call a C function before sp is set.
The call saves its return address through sp, which still holds 0.
Fix Make setting sp the first thing start-up code does.
Wrong Trust a passing back-door-loaded test to prove the .bss clear.
The bench already filled SRAM with zeros.
Fix Count the clear stores in the trace against the size of .bss.
Wrong Write addresses into start-up code by hand.
The next link moves the sections and the code clears the wrong words.
Fix Use the linker script's symbols: __data_start, __bss_end and the rest.
Remember
- Reset gives only a fetch address; start-up code builds the stack, .data and .bss that C assumes.
- The linker script places sections and gives start-up code their bounds as symbols.
- .data has a load address and a run address; in the SRAM layout they are equal and the copy does nothing.
- The bench zero-fills SRAM, so only the trace proves the .bss clear.
- TB_STATUS = 2 marks "main entered"; a pass without it is not a pass.