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.

The Memory Image, Section by SectionStep through. Each step is one thing the start-up code or the linker script decides before main runs.
  • Memory
  • Processor
  • Data
The Memory Image, Section by SectionStep through. Each step is one thing the start-up code or the linker script decides before main runs. memory Boot ROM, 4 KiB at 0x0000_0000:code, constants,first values of .data processor Core after reset pc = reset vector; spand mtvec hold 0 memory .data, in SRAM at 80000000: limit,step (8 bytes) memory .bss, in SRAM 80 bytes that muststart at 0 memory Stack from 0x8001_0000,growing down processor main the first line of C copy first valuesstore zerosset spcall main
  1. memory Boot ROM, 4 KiBat 0x0000_0000: code, constants, first values of .data
    • copy first values → .data, in SRAM
  2. processor Core after resetpc = reset vector; sp and mtvec hold 0
    • store zeros → .bss, in SRAM
    • set sp → Stack
    • call main → main
  3. memory Stackfrom 0x8001_0000, growing down
  4. memory .data, in SRAMat 80000000: limit, step (8 bytes)
  5. memory .bss, in SRAM80 bytes that must start at 0
  6. processor mainthe first line of C
  1. 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.

  2. 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.

  3. Set the stack

    The first useful write puts the top of SRAM in sp. No function can be called before it.

  4. Copy .data

    Variables with a first value run in SRAM, but their values were stored in ROM. A loop copies them across.

  5. Clear .bss

    Variables without a first value must start at 0. A second loop stores zeros over the whole section.

  6. 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.

Five questions this page answers
Question
1Why does a chip need code before main?
2What does a linker script decide?
3What do the load address and the run address of .data mean?
4How does the retire trace show each start-up step?
5Which 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

New words on this page
WordMeaning
Start-up codeThe code between reset and main: the kit's crt0.S. It sets sp, gp and mtvec, copies .data, clears .bss and calls main.
Linker scriptThe file that tells the linker where memory is and where each section goes: the kit's link.ld.
SectionA named block of the program the linker places as one piece: .text for code, .data, .bss.
.dataVariables with a first value. They run in SRAM, so their first values must be copied there.
.bssVariables without a first value. C promises they start at 0, so start-up code stores zeros over them.
Load address and run addressWhere the image keeps a section, and where the program uses it. For .data in ROM layout they differ.
ROM layout and SRAM layoutThe kit's two images: boot from ROM, or start in SRAM with the image placed there by the bench.
Back-door loadThe 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.

The lab program. Built with the firmware kit for the ROM layout.
/* 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.

Where the linker put the lab program
SymbolAddressWhat it is
_start00000000The first instruction: the reset vector of the ROM layout
main0000021cIn ROM, after the start-up code and the kit
__data_load00000288Load address of .data: the first values, kept in ROM
__data_start, __data_end80000000, 80000008Run address of .data: step, then limit
__bss_start, __bss_end80000008, 80000058.bss: the kit's interrupt table, then total and count
__stack_top80010000One 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.

Abridged from tools/soc/fwkit/link.ld. apcc fills in the bases and sizes from map.json.
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

  1. Rule 1: The stack comes first.

    No call is possible until sp points into SRAM.

  2. 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.

  3. Rule 3: Clear every word of .bss.

    Both ends matter: from __bss_start up to, but not including, __bss_end.

  4. Rule 4: Addresses come from the linker script.

    Start-up code uses symbols, so a new map changes no instruction.

  5. 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.

Selected rows of the retire trace, measured on our bench (soc_tb on Verilator 5.052 2026-09-05; program built with riscv64-unknown-elf-gcc 14.2.0, -Os). Another compiler gives other cycles and addresses.
   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 = 80000000

Walk 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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. The lab image has three sections and 119 instructions before main.

  1. 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.

  2. 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.

  3. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 records

Check Yourself

Predict the outputThe program gains four more words of .bss (volatile unsigned spare[4];). The slice reads that run's trace. Each pass of the clear loop retires four instructions. What is the last line?
// The stores made between reset and main, copied from the chip's retire trace (ROM layout).
// Each one is sorted by the symbols the linker gave this program.
module tb;
  localparam logic [31:0] DATA_START = 32'h80000000, DATA_END = 32'h80000008;
  localparam logic [31:0] BSS_START  = 32'h80000008, BSS_END  = 32'h80000068;
  localparam logic [31:0] TB_STATUS  = 32'h10004020;
  localparam int N = 28, RECORDS = 135;
  int          rec  [N];
  logic [31:0] addr [N], data [N];
  int copied = 0, cleared = 0;
  initial begin
    rec[0] = 10; addr[0] = 32'h10004020; data[0] = 32'h00000001;
    rec[1] = 19; addr[1] = 32'h80000000; data[1] = 32'h00000003;
    rec[2] = 25; addr[2] = 32'h80000004; data[2] = 32'h00000005;
    rec[3] = 34; addr[3] = 32'h80000008; data[3] = 32'h00000000;
    rec[4] = 38; addr[4] = 32'h8000000c; data[4] = 32'h00000000;
    rec[5] = 42; addr[5] = 32'h80000010; data[5] = 32'h00000000;
    rec[6] = 46; addr[6] = 32'h80000014; data[6] = 32'h00000000;
    rec[7] = 50; addr[7] = 32'h80000018; data[7] = 32'h00000000;
    rec[8] = 54; addr[8] = 32'h8000001c; data[8] = 32'h00000000;
    rec[9] = 58; addr[9] = 32'h80000020; data[9] = 32'h00000000;
    rec[10] = 62; addr[10] = 32'h80000024; data[10] = 32'h00000000;
    rec[11] = 66; addr[11] = 32'h80000028; data[11] = 32'h00000000;
    rec[12] = 70; addr[12] = 32'h8000002c; data[12] = 32'h00000000;
    rec[13] = 74; addr[13] = 32'h80000030; data[13] = 32'h00000000;
    rec[14] = 78; addr[14] = 32'h80000034; data[14] = 32'h00000000;
    rec[15] = 82; addr[15] = 32'h80000038; data[15] = 32'h00000000;
    rec[16] = 86; addr[16] = 32'h8000003c; data[16] = 32'h00000000;
    rec[17] = 90; addr[17] = 32'h80000040; data[17] = 32'h00000000;
    rec[18] = 94; addr[18] = 32'h80000044; data[18] = 32'h00000000;
    rec[19] = 98; addr[19] = 32'h80000048; data[19] = 32'h00000000;
    rec[20] = 102; addr[20] = 32'h8000004c; data[20] = 32'h00000000;
    rec[21] = 106; addr[21] = 32'h80000050; data[21] = 32'h00000000;
    rec[22] = 110; addr[22] = 32'h80000054; data[22] = 32'h00000000;
    rec[23] = 114; addr[23] = 32'h80000058; data[23] = 32'h00000000;
    rec[24] = 118; addr[24] = 32'h8000005c; data[24] = 32'h00000000;
    rec[25] = 122; addr[25] = 32'h80000060; data[25] = 32'h00000000;
    rec[26] = 126; addr[26] = 32'h80000064; data[26] = 32'h00000000;
    rec[27] = 133; addr[27] = 32'h10004020; data[27] = 32'h00000002;
    for (int k = 0; k < N; k++) begin
      if (addr[k] == TB_STATUS)
        $display("record %0d: TB_STATUS = %0h", rec[k], data[k]);
      else if (addr[k] >= DATA_START && addr[k] < DATA_END) begin
        copied++;
        $display("record %0d: .data copy, %h = %h", rec[k], addr[k], data[k]);
      end else if (addr[k] >= BSS_START && addr[k] < BSS_END) cleared++;
    end
    $display(".bss clear: %0d stores of 0, from %h to %h", cleared, BSS_START, BSS_END - 4);
    $display("before main: %0d copy stores, %0d clear stores, %0d records", copied, cleared, RECORDS);
  end
endmodule
ConceptWhat does the core know right after reset?
ConceptIn the ROM layout, why must start-up code copy .data?
ConceptA test writes the pass word, but TB_STATUS was never 2. What does the bench report?
ConceptWhich evidence proves that start-up code cleared all of .bss?

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.

Practice

Report a problem with this page