Start-Up Code: What Runs Before Main
Your program says "int limit = 5". At power-on, SRAM holds nothing. Somebody has to put the 5 there before the first line of main runs.
You Will Be Able To
- List what start-up code must do before main, and in which order.
- Explain why a variable with a first value needs a copy, and a variable without one needs a clear.
- Recognize from a symptom which start-up step is missing.
- Lesson
- S6.7
- Time
- 27 min
- Runs on
- Your browser
- Check
- 5 questions
A board counts events. The source code starts the counter at 0. After every cold start the counter begins at some huge number, yet under the debugger it always begins at 0. The loop that counts is correct.
Before You Start
You need The Boot Flow: the reset address, Boot ROM, and SRAM that holds nothing at a cold start. You need the stack and sp from The Stack Frame.
You need mtvec and a handler from Traps. Sections and symbols come from Assembler, Linker and Loader.
| Question | |
|---|---|
| 1 | What does a C program expect to be true when main starts? |
| 2 | Why must the stack pointer be set by the very first instructions? |
| 3 | Where are the first values of variables kept while the power is off? |
| 4 | Why are variables that start at 0 not stored at all? |
| 5 | What does the program do when one start-up step is missing? |
The Idea: Code That Runs Before Main
A C program is written as if four things were already true. There is a stack. A fault has somewhere to go. Every variable with a first value holds that value. Every other variable holds 0.
After reset none of them is true. Every register holds 0. SRAM holds whatever it woke up with. So a short piece of code runs first and makes the four things true. It is called the start-up code. Its last instruction calls main.
Variables that live for the whole program are global variables. The linker sorts them into two sections. Those with a first value, like "limit = 5", go to the section named .data. Those without one go to the section named .bss.
The two sections need different treatment. The values of .data must survive power-off, so they are stored in ROM and copied to SRAM. Nothing is stored for .bss. Start-up code writes 0 over all of it.
New Words
| Word | Meaning |
|---|---|
| Global variable | A variable that exists for the whole run of the program, at a fixed address. |
| .data | The section of global variables that have a first value. |
| .bss | The section of global variables that have no first value. C promises that they start at 0. |
| Load address | Where the stored copy of a section is kept. For .data: in ROM. The place where the program uses the section is its run address. For .data: in SRAM. |
| Stack top | The first value of sp. On this page it is 0x8001_0000, one past the end of SRAM. |
| Trap vector | The address in mtvec. The processor jumps there when it takes a trap. |
The names .data and .bss begin with a dot. That is how the tools spell section names.
One Picture: Five Steps
- Processor
- Memory
- Control / FSM
- The stack pointer
Reset leaves 0 in every register. A function call needs a stack, so the first instruction gives sp an address in SRAM.
- The trap vector
If anything faults from now on, the processor jumps to the address in mtvec. Start-up code puts a handler there early.
- Copy .data
The first values of limit and step are stored in ROM. The program will read and change them in SRAM. So they are copied, word by word.
- Clear .bss
count and total must start at 0. Nothing is stored for them. A loop writes 0 to each word.
- Call main
Only now is the machine in the state that C code expects. Start-up code calls main.
Read the diagram as text
- Processor: after reset: sp holds 0, mtvec holds 0
- Boot ROM: the code, and the first values of .data
- .data, in SRAM: variables with a first value: limit, step
- .bss, in SRAM: variables that start at 0: count, total
- Stack, in SRAM: from the top of SRAM, growing down
- main: the first function of the program
- Processor → Stack, in SRAM: 1: sp
- Processor → Boot ROM: 2: mtvec
- Boot ROM → .data, in SRAM: 3: copy
- .data, in SRAM → .bss, in SRAM: 4: clear
- Processor → main: 5: call
The start-up code of the lab. The clear loop is code in ROM; the arrow from .data only shows that .bss follows .data in memory.
The order matters in one place only. The stack pointer comes first, because a call or an interrupt may need the stack at any moment after that.
The Smallest Example, Worked by Hand
A program has four global variables. Two have first values: limit = 5 and step = 3. Two have none: count and total. Its main does two things: count = count + step, then total = limit + count.
| Section | Holds | Run address | Stored copy |
|---|---|---|---|
| .data | limit, step | 0x8000_0000 and 0x8000_0004 | in ROM, at 0xD0 and 0xD4: the words 5 and 3 |
| .bss | count, total | 0x8000_0008 and 0x8000_000C | none |
Follow SRAM through the start-up code. A question mark is a word that nobody has written since power-on.
| After | limit | step | count | total |
|---|---|---|---|---|
| power-on | ? | ? | ? | ? |
| the copy of .data | 5 | 3 | ? | ? |
| the clearing of .bss | 5 | 3 | 0 | 0 |
| main | 5 | 3 | 0 + 3 = 3 | 5 + 3 = 8 |
Now leave the clearing out. Then count starts as a question mark, and so "count + step" is unknown as well. The program is the same. Its answer is not.
The Rules
Rule 1: Set the stack pointer first.
A call saves its return address on the stack. With sp at 0 that write goes to an address with no memory.
Rule 2: Set the trap vector early.
A fault before mtvec is set jumps to address 0 and starts the program again, for ever.
Rule 3: Copy .data from its load address to its run address.
Copy every word from the start of the section to its end. An empty section copies nothing.
Rule 4: Write 0 to every word of .bss.
C promises that such a variable starts at 0. SRAM does not.
Rule 5: Call main last.
Use no global variable before the copy and the clear are done.
Rule 6: If main returns, stay in a loop.
Nothing comes after start-up code. A processor that runs on executes whatever follows.
Sources for These Rules
Patterson and Hennessy, Computer Organization and Design, ARM edition, section 2.12: the linker, the loader and the segments of a program. Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 6.5. It covers the memory map of a program, with its global data and its stack, and linking and loading.
Both books describe a machine with an operating system, where a loader does this work. On a chip without one, start-up code does it. Rules 1 to 6 are common practice for such code. No source in our library lists them.
The stack top of 0x8001_0000 is a choice of this course. On ap_rv32, mtvec is 0 after reset, and every register is 0 after reset in the lab model.
Now Run It
The lab at the end of this page is the hand example as a program. The start-up code and main are rows of instructions in Boot ROM. A small model of the processor runs them and prints every instruction. Here are the rows that change the machine.
00000000 | lui x2, 0x80010 | x2 = 80010000
00000008 | csrw mtvec, x5 | mtvec = 000000c0
00000020 | sw x28, 0(x6) | mem[80000000] = 00000005
00000020 | sw x28, 0(x6) | mem[80000004] = 00000003
00000040 | sw x0, 0(x6) | mem[80000008] = 00000000
00000040 | sw x0, 0(x6) | mem[8000000c] = 00000000
0000004c | jal x1, 0x80 | x1 = 00000050, next is 00000080
00000084 | sw x1, 0(x2) | mem[8000fffc] = 00000050
0000009c | sw x12, 8(x8) | mem[80000008] = 00000003
000000a4 | sw x13, 12(x8) | mem[8000000c] = 00000008
00000050 | jal x0, 0x50 | the program stops hereRule 1 at work: the first row gives x2, which is sp, the value 80010000. Rule 3 at work: the words 5 and 3 arrive at 80000000 and 80000004. Rule 4 at work: two zeros go to 80000008 and 8000000c.
Then "jal x1, 0x80" calls main. Its first store saves the return address at 8000fffc, just below the stack top. The last row is rule 6: main came back, and the code stays in a loop at address 50.
limit=5 step=3 count=3 total=8 | sp=80010000 | exceptions=0Now the same program with one step removed each time.
| Left out | Last line of the lab |
|---|---|
| nothing | limit=5 step=3 count=3 total=8 | sp=80010000 | exceptions=0 |
| the copy of .data | limit=xxxxxxxx step=xxxxxxxx count=xxxxxxxx total=xxxxxxxx | sp=80010000 | exceptions=0 |
| the clearing of .bss | limit=5 step=3 count=xxxxxxxx total=xxxxxxxx | sp=80010000 | exceptions=0 |
| the stack pointer | limit=5 step=3 count=0 total=0 | sp=fffffffc | exceptions=1 |
Recorded from the lab. An x is a bit that was never written. On silicon it would be a 0 or a 1 that nobody chose.
Without the copy, limit and step are unknown, and everything worked out from them is unknown too. Without the clear, count is unknown. Without the stack pointer the program never gets through main:
exception, no memory at fffffffc for a store at 00000084: mcause=00000007 mepc=00000084, next is 000000c0Main subtracts 4 from an sp of 0 and tries to store at fffffffc. No memory is there. The processor takes a trap and jumps to the handler, because rule 2 was obeyed. So count and total keep their zeros, and the fault is easy to see.
The lab is a model beside the real processor. A probe ran the same five programs on the real ap_rv32 of this course. The final values of the four variables, the stop address and the number of exceptions were equal in every run.
These labs leave things out. The sections are two words long. The code has no library to prepare. A real start-up file also sets up the clocks and, on larger chips, the caches.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Keep the first values in ROM and copy them | Let each variable be set by code in main | The compiler handles every variable the same way. The values take room twice: in ROM and in SRAM. |
| Store nothing for .bss | Store its zeros in ROM too | A large buffer costs no ROM at all. Start-up needs a second loop. |
| Write start-up code in assembly | Write it in C | It works before a stack exists. It must be written again for each processor. |
Could the chip do this in hardware? It could clear SRAM at reset. Then a large SRAM would need many cycles, or a special memory. A loop of three instructions is cheaper.
The Trap: Correct Under the Debugger
Back to the board from the top of the page. The counter is a global variable with no first value. It lives in .bss. The start-up file of that board was copied from an older project, and its clear loop ends one word early.
On a cold start, the last word of .bss keeps what SRAM woke up with. Our counter is that word. Hence the huge number.
Under the debugger the fault hides, as it did for the boot loader. Many debuggers write the whole image, and some clear memory before they start the program. The run you watch is not the run the customer gets.
When a variable is wrong only after a cold start, do not read main. Find the section the variable is in. Then check the loop of that section, and both of its ends.
At Scale
This section looks ahead. The lab has four variables and thirty instructions of start-up code. A product has thousands of variables.
Who knows the addresses
The lab wrote the addresses into the code by hand. In a real project the linker supplies them as symbols. The next lesson shows where they come from.
Tables of sections
A large program has several regions to copy and several to clear. Its start-up code walks a table of them. The practice problem of this page asks for such a loop.
More to prepare
Before main, real start-up code may also start the clocks, switch on caches and call the set-up functions of libraries.
One stack for each processor
On a chip with several processors, each one runs start-up code and needs a stack top of its own.
Try It
Run the lab. Find the two stores that copy .data and the two that clear .bss.
Delete the first row, which sets x2. Predict the address of the store that fails, then run.
Put that row back. In the row at 034, change 8 to 12: the clear loop now starts one word late. Say which variables end unknown.
Change the two stored words at d0 and d4. Predict the last line.
00000000 | lui x2, 0x80010 | x2 = 80010000 00000004 | addi x5, x0, 192 | x5 = 000000c0 00000008 | csrw mtvec, x5 | mtvec = 000000c0 0000000c | addi x5, x0, 208 | x5 = 000000d0 00000010 | lui x6, 0x80000 | x6 = 80000000 00000014 | addi x7, x6, 8 | x7 = 80000008 00000018 | beq x6, x7, 0x30 | not taken 0000001c | lw x28, 0(x5) | x28 = 00000005 from mem[000000d0] 00000020 | sw x28, 0(x6) | mem[80000000] = 00000005 00000024 | addi x5, x5, 4 | x5 = 000000d4 00000028 | addi x6, x6, 4 | x6 = 80000004 0000002c | bne x6, x7, 0x1c | taken: next is 0000001c 0000001c | lw x28, 0(x5) | x28 = 00000003 from mem[000000d4] 00000020 | sw x28, 0(x6) | mem[80000004] = 00000003 00000024 | addi x5, x5, 4 | x5 = 000000d8 00000028 | addi x6, x6, 4 | x6 = 80000008 0000002c | bne x6, x7, 0x1c | not taken 00000030 | lui x6, 0x80000 | x6 = 80000000 00000034 | addi x6, x6, 8 | x6 = 80000008 00000038 | addi x7, x6, 8 | x7 = 80000010 0000003c | beq x6, x7, 0x4c | not taken 00000040 | sw x0, 0(x6) | mem[80000008] = 00000000 00000044 | addi x6, x6, 4 | x6 = 8000000c 00000048 | bne x6, x7, 0x40 | taken: next is 00000040 00000040 | sw x0, 0(x6) | mem[8000000c] = 00000000 00000044 | addi x6, x6, 4 | x6 = 80000010 00000048 | bne x6, x7, 0x40 | not taken 0000004c | jal x1, 0x80 | x1 = 00000050, next is 00000080 00000080 | addi x2, x2, -4 | x2 = 8000fffc 00000084 | sw x1, 0(x2) | mem[8000fffc] = 00000050 00000088 | lui x8, 0x80000 | x8 = 80000000 0000008c | lw x10, 0(x8) | x10 = 00000005 from mem[80000000] 00000090 | lw x11, 4(x8) | x11 = 00000003 from mem[80000004] 00000094 | lw x12, 8(x8) | x12 = 00000000 from mem[80000008] 00000098 | add x12, x12, x11 | x12 = 00000003 0000009c | sw x12, 8(x8) | mem[80000008] = 00000003 000000a0 | add x13, x10, x12 | x13 = 00000008 000000a4 | sw x13, 12(x8) | mem[8000000c] = 00000008 000000a8 | lw x1, 0(x2) | x1 = 00000050 from mem[8000fffc] 000000ac | addi x2, x2, 4 | x2 = 80010000 000000b0 | jalr x0, 0(x1) | x0 = 00000000, next is 00000050 00000050 | jal x0, 0x50 | the program stops here limit=5 step=3 count=3 total=8 | sp=80010000 | exceptions=0Check Yourself
Common Mistakes
Wrong Call a function before the stack pointer is set.
The call saves its return address through sp, which still holds 0.
Fix Make the write to sp the first instruction.
Wrong Expect a variable without a first value to hold 0 by itself.
SRAM keeps nothing through power-off.
Fix Clear every word of .bss in start-up code.
Wrong Test start-up code only with the debugger attached.
The debugger may write or clear memory itself.
Fix Test from a cold start, with the cable off.
Remember
- Start-up code runs between the reset address and main.
- It sets the stack pointer first, and the trap vector early.
- It copies .data from its load address in ROM to its run address in SRAM.
- It writes 0 to every word of .bss, then calls main.
- A variable that is wrong only after a cold start points at a missing or short start-up step.