Skip to content
systemcoding.dev
Embedded

How the Cortex-M Vector Table Works and Where It Lives in Flash

Learn the Cortex-M vector table layout: system entries, device IRQs, the Thumb bit, how VTOR finds the table, and why it must sit at the start of flash.

6 min read
Summarize in AI
Flat illustration of a microcontroller wired to an LED and a sensor, with the Arm, GNU and C logos.

Every interrupt and fault on a Cortex-M core ends at a table lookup. The core takes the exception number, multiplies by four, adds the table's base address, reads one word, and jumps there. If that word is wrong, the failure is a hard fault with no useful context, which is why the table deserves its own explanation. This post covers the Armv7-M layout used by Cortex-M3, M4 and M7, then the mechanism that locates the table, and finally how a linker script and a startup file agree on where it sits in flash. The Armv8-M differences (M23, M33) are noted at the end.

What the table holds

The vector table is an array of 32-bit words. Entry 0 is not a handler at all: it is the initial value of the main stack pointer. Entries 1 and up are exception handler addresses. The Arm Cortex-M4 Devices Generic User Guide lists the system exceptions, and the entries for them are fixed by the architecture:

EntryOffsetExceptionNotes
00x00Initial MSPLoaded into SP at reset
10x04ResetEntry point, first code run
20x08NMINon-maskable interrupt
30x0CHardFaultEscalated faults
40x10MemManageMPU violations
50x14BusFaultBus errors
60x18UsageFaultUndefined instruction, unaligned access
7 to 100x1C to 0x28Reserved
110x2CSVCallsvc instruction
120x30Debug Monitor
130x34Reserved
140x38PendSVContext switching in an RTOS
150x3CSysTickSystem timer
16 and up0x40 onwardIRQ 0, 1, 2 ...Device-specific interrupts

The 16 entries before the IRQs are common to every Armv7-M part. From entry 16 on, the chip vendor defines the meaning: on an STM32F407, IRQ 0 is the window watchdog and IRQ 6 is EXTI line 0. The vendor's startup file lists them in order and the order is part of the datasheet's interrupt table. Getting one slot wrong makes every handler after it run for the wrong interrupt.

Why bit 0 matters

Cortex-M executes only Thumb code. The architecture records the instruction set state in bit 0 of an address used for a branch, and an exception return or vector fetch requires it set. The core loads the PC from the vector with bit 0 cleared and sets the Thumb state bit (the T bit in the EPSR) from bit 0. A vector with bit 0 clear would try to enter Arm state, which Cortex-M does not have, and the result is a UsageFault (INVSTATE) that escalates to HardFault when the handler is not enabled.

You normally do not have to think about this: a C function pointer to a Thumb function already has bit 0 set. The case where it matters is a hand-built table in assembly, where .word Reset_Handler does the right thing only if the symbol is declared as a Thumb function (.thumb_func in GNU assembler). Without that directive the assembler emits the address with bit 0 clear.

A table in C

A table is a const array in a dedicated section. This one places the stack pointer, the 15 system slots and two device interrupts, with weak default handlers so that any handler you do not write still resolves:

vectors.cc
#include <stdint.h>

extern uint32_t _estack;

void Reset_Handler(void);
void Default_Handler(void);

void NMI_Handler(void)        __attribute__((weak, alias("Default_Handler")));
void HardFault_Handler(void)  __attribute__((weak, alias("Default_Handler")));
void MemManage_Handler(void)  __attribute__((weak, alias("Default_Handler")));
void BusFault_Handler(void)   __attribute__((weak, alias("Default_Handler")));
void UsageFault_Handler(void) __attribute__((weak, alias("Default_Handler")));
void SVC_Handler(void)        __attribute__((weak, alias("Default_Handler")));
void PendSV_Handler(void)     __attribute__((weak, alias("Default_Handler")));
void SysTick_Handler(void)    __attribute__((weak, alias("Default_Handler")));
void WWDG_IRQHandler(void)    __attribute__((weak, alias("Default_Handler")));
void PVD_IRQHandler(void)     __attribute__((weak, alias("Default_Handler")));

typedef void (*isr_t)(void);

__attribute__((section(".isr_vector"), used))
const void *const vector_table[] = {
	&_estack,
	Reset_Handler,
	NMI_Handler,
	HardFault_Handler,
	MemManage_Handler,
	BusFault_Handler,
	UsageFault_Handler,
	0, 0, 0, 0,
	SVC_Handler,
	0,
	0,
	PendSV_Handler,
	SysTick_Handler,
	WWDG_IRQHandler,
	PVD_IRQHandler,
};

The array is of const void *const, so the stack pointer (an address of a linker symbol) and the function addresses fit in one initialiser. GCC emits function addresses with bit 0 set for Thumb targets, so the Reset_Handler word is correct as written. The .isr_vector name is a convention that ST's and CMSIS startup files follow, and the linker script decides what it means.

How the core finds the table

After reset, the table base is 0x00000000. The register that controls it later is SCB->VTOR (Vector Table Offset Register, at 0xE000ED08), whose reset value is zero on a Cortex-M3 or M4 as documented in the Generic User Guide. When the core takes an exception, it reads from VTOR + 4 * exception_number.

The table is at address zero but the program is at 0x08000000 on an STM32. That works because the chip aliases: with the boot pins in their normal state, the flash at 0x08000000 also appears at 0x00000000. Reading vector 0 at address 0 returns the word at 0x08000000. This is why the linker places .isr_vector first in the flash region, and why a linker script that puts .text first produces a board that does not start: the core would read the first bytes of the code as the stack pointer.

Other vendors differ. On an LPC1768, the table is in the first 4 KiB of flash. On an nRF52, flash starts at address zero and no aliasing is involved. Check the reference manual's memory map and boot chapter before assuming.

Alignment

VTOR imposes alignment on any table you relocate. The table must be aligned to a power of two that is at least the size of the table, with a minimum of 128 bytes. The Generic User Guide gives the rule: for a table with N interrupts, round (N + 16) * 4 up to the next power of two. The STM32F407 has 82 device interrupts, so its table is (82 + 16) * 4 = 392 bytes and the alignment is 512 bytes. A table in flash at the start of the image meets that automatically. A copy in RAM needs __attribute__((aligned(512))), which the post on relocating the vector table covers.

Placing it with the linker

Tell the linker where the section goes and make sure it survives:

Text
SECTIONS
{
  .isr_vector :
  {
    . = ALIGN(4);
    KEEP(*(.isr_vector))
    . = ALIGN(4);
  } > FLASH
}

Two details matter. KEEP stops --gc-sections from removing the table since no code references it. And because the script lists .isr_vector first among the output sections that go to FLASH, its address is ORIGIN(FLASH). You can confirm that on the built image. The output below is for a complete STM32F407 startup file with all 82 device slots, not the shortened table above:

Text
arm-none-eabi-objdump -h firmware.elf | grep isr_vector
Text
  0 .isr_vector   00000188  08000000  08000000  00010000  2**2

The size 0x188 is 392 bytes, exactly 98 words, matching 16 system entries and 82 device interrupts. The size and the VMA of 08000000 are the two things to check.

Reading what the core read

In GDB, the first two words are what the core loaded at reset, so you can compare them with what the linker produced:

Text
(gdb) x/4wx 0x08000000
Text
0x8000000 <vector_table>:	0x20020000	0x08000755	0x080005c1	0x080005c1

The first word is the top of 128 KiB of RAM. The second is an odd address, bit 0 set, which proves the reset vector is marked Thumb. The next two are the same address because NMI and HardFault both resolve to Default_Handler here. The exact addresses differ in your build.

Armv8-M differences

Cortex-M23 and M33 keep the same layout for the first 16 entries and add banked vector tables for Secure and Non-secure states, each with its own VTOR (VTOR_S and VTOR_NS). Reset values and the alignment rule change slightly: the Arm Cortex-M33 Devices Generic User Guide documents the details. The principle is the same: one word per exception, stack pointer first.

Also inToolchains
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.