What Happens Between Reset and main on a Cortex-M Microcontroller
Follow a Cortex-M core from power-on to main: the two vector table words it fetches, the reset handler, and the C runtime setup that runs before your code.

You press reset on a Cortex-M board, and a few microseconds later main runs with its global variables initialised and a working stack. Nothing in your C source did that. A short sequence of hardware steps and about twenty lines of startup code did it, and when it goes wrong the symptom is usually a board that does nothing at all, with no error to read. This post follows the sequence in order on a Cortex-M3 or M4 (the Armv7-M architecture), so that the startup file in any SDK stops looking like magic. The full treatment of startup code and linker scripts is in the cornerstone guide for this cluster; here the subject is only the path from reset to main.
Step 1: the core reads two words
On reset the core does not start at address zero and run instructions. It reads two 32-bit words from the start of the vector table, which the Armv7-M architecture places at address 0x00000000 after reset:
| Address | Content | What the core does with it |
|---|---|---|
0x00000000 | Initial value of the main stack pointer | Loads it into SP (MSP) |
0x00000004 | Address of the reset handler, with bit 0 set | Loads it into PC and starts executing |
The vector table is data, not code. On most microcontrollers the flash starting at 0x08000000 (STM32) is aliased to address zero at reset, so the table the linker put at the start of flash is the one the core reads. The Arm Cortex-M4 Devices Generic User Guide documents this under "Reset" in the exception model chapter.
Bit 0 of the reset vector is set because Cortex-M only executes Thumb instructions. The core drops the bit when it loads the PC. A function address in a vector table that has bit 0 clear is a classic cause of a hard fault right after reset, and it is the reason a hand-written table needs the | 1 or a compiler that does it for you.
After those two loads, the core is in Thread mode using the main stack, with interrupts enabled at default priority and nothing initialised in RAM. It has run no code yet.
Step 2: the reset handler is the first code
The reset handler is an ordinary function whose address the linker put into the second table slot. Its first job is the C runtime setup, which the language assumes has already happened before main. In C, a global with an initialiser, such as int counter = 5;, must hold 5 when main starts, and a global without one must hold zero. A microcontroller's RAM holds neither at power-on: it contains whatever noise the cells settled to.
So the reset handler does this:
Copies the initial values of
.datafrom flash to RAM.Writes zeros over
.bss.Calls
SystemInit()to set up clocks (vendor-dependent, and sometimes done earlier).Runs static constructors and library initialisation (
__libc_init_array).Calls
main.
Here is a complete reset handler in C for a Cortex-M, the shape of every startup file you will meet:
#include <stdint.h>
extern uint32_t _sidata; /* start of .data initial values in flash */
extern uint32_t _sdata; /* start of .data in RAM */
extern uint32_t _edata; /* end of .data in RAM */
extern uint32_t _sbss; /* start of .bss */
extern uint32_t _ebss; /* end of .bss */
extern uint32_t _estack; /* top of RAM, from the linker script */
int main(void);
void SystemInit(void);
void __libc_init_array(void);
void Reset_Handler(void)
{
uint32_t *src = &_sidata;
uint32_t *dst = &_sdata;
while (dst < &_edata)
*dst++ = *src++;
for (dst = &_sbss; dst < &_ebss; )
*dst++ = 0;
SystemInit();
__libc_init_array();
main();
for (;;)
;
}
__attribute__((section(".isr_vector"), used))
const uint32_t vector_table[] = {
(uint32_t)&_estack,
(uint32_t)Reset_Handler,
};The symbols _sidata, _sdata, _edata, _sbss, _ebss and _estack are not variables. The linker script defines them as addresses, and C sees them as extern objects whose address is the useful part. That is why the code takes &_sdata and never reads _sdata.
The table here has only two entries to keep the example short. A real one continues with NMI, HardFault and the peripheral interrupts. The second word, Reset_Handler converted to an integer, carries bit 0 set because the compiler emits Thumb function addresses that way.
Step 3: the linker script decides what the symbols mean
Those six symbols come from a linker script. The relevant part looks like this for a part with 128 KiB of flash and 32 KiB of RAM:
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 32K
}
_estack = ORIGIN(RAM) + LENGTH(RAM);.data has two addresses. Its load address (where the bytes sit in flash) is _sidata, and its run address (where the code expects to find it) is _sdata in RAM. Writing >RAM AT>FLASH in the script tells the linker to relocate the symbols to RAM while storing the bytes in flash. The copy loop in the reset handler bridges those two addresses. The matching post on writing a linker script covers it in full; the point here is that the startup code and the script are two halves of one contract, and the contract is the list of symbols.
What the stack is doing
_estack is the top of RAM, and the first table word loads it into SP before any code runs. On Cortex-M the stack grows downward, so the first push stores at _estack - 4. This is why a function call inside the reset handler already works: the stack exists before the first instruction. If the table's first word were zero, the first push would write to address 0xFFFFFFFC and fault at once.
Before the .data copy runs, any function call is risky if the compiler decides to use a global it expects to be initialised. The reset handler above is written to touch only locals and the linker symbols, which is also why startup code in C compiled with optimisation is usually fine but one that calls a helper depending on .bss is not.
What can go wrong
Most failures between reset and main come from the contract above:
The vector table is not at the start of flash. If the linker places
.textfirst, the core reads code bytes as the stack pointer and reset vector. Keep the table in its own section and list it first.**No
KEEPon the vector section.** With--gc-sections, a table that nothing references is discarded as unused. The script needsKEEP(*(.isr_vector)).**
_estackoutside RAM.** A typo in the length puts the first push into non-existent memory.**The
.datacopy is wrong.** If_sidatadoes not point at the flash image, initialised globals hold garbage, and the program often runs for a while before misbehaving.Clocks. A board that runs on the default internal oscillator works at the reset-time frequency, and a hang inside
SystemInitwaiting for an oscillator that is not there looks identical to a dead board.
A debugger makes the sequence visible. With OpenOCD and GDB, monitor reset halt stops the core at the reset vector, and info registers pc sp shows the two words the core loaded. The PC will be the reset handler's address and SP will be the top of RAM, in this example:
pc 0x8000134 0x8000134 <Reset_Handler>
sp 0x20008000 0x20008000The stack pointer is the end of a 32 KiB RAM at 0x20000000, and the PC is the handler address with bit 0 cleared (the register shows the real fetch address). The exact PC value depends on your build.
Where main's first line really is
By the time main runs, the C language's guarantees hold: initialised globals are initialised, .bss is zero, constructors have run, and the stack is valid. Nothing in main is special to the core, either. It is a function the reset handler calls and, if it returns, the loop afterwards catches it. Many bare-metal programs never return from main, and the for (;;) after the call is the safety net when one does.


