Q&A-2Design and validate interrupt handling for vector table.
Answer
Assumptions first. Cortex-M4 (STM32F4-class), GCC arm-none-eabi, a bootloader at 0x08000000 handing off to an application linked at 0x08020000, and a 128-entry table (16 core exceptions + 112 device IRQs). On Cortex-M0 there is no VTOR, so the relocation story below changes: I'll flag that.
The design. The vector table is 128 words = 512 bytes, and it lives in its own linker section at the start of the image so the boot ROM and bootloader can find it. Two rules decide whether it works:
- Alignment. VTOR (at
0xE000ED08) holds the base in bits [31:7]; bits [6:0] read as zero. So the table must be aligned to a power of two at least as large as itself: 512 bytes here. If it isn't, the VTOR write silently rounds the base down and the core fetches handlers from the wrong addresses. - Relocation. The application's table is at
0x08020000, not0x00000000, so the app must point VTOR at its own table before enabling interrupts.
extern uint32_t __isr_vector[]; // .isr_vector, 512-byte aligned
SCB->VTOR = (uint32_t)__isr_vector; // bits [6:0] ignored
__DSB();
__enable_irq();.isr_vector : ALIGN(512) { KEEP(*(.isr_vector)) } > FLASHEntry 0 is the initial MSP, entry 1 the reset vector, entries 2–15 the core exceptions (NMI, HardFault, SVCall, PendSV, SysTick…), and device IRQ n sits at index 16+n. That order is fixed by the silicon; a table generated from the vendor SVD or CMSIS startup_*.s keeps it honest.
Validation. Static checks come from the build, runtime checks from the boot path:
| check | how | fail signal |
|---|---|---|
| table base | read VTOR, compare to &__isr_vector | VTOR still 0x08000000 → interrupts vector through the bootloader |
| alignment | addr % 512 == 0 | base truncated, wrong handler fetched |
| entries | each entry bit0 == 1 and inside app flash | 0xFFFFFFFF (erased) or 0x00000000 |
| weak defaults | nm/map: symbol != Default_Handler | handler silently aliased to the infinite loop |
| signature | handler is void f(void) | HardFault on exception return |
Failure modes and how you catch them. A retained weak default is the nastiest: the vendor startup file aliases every vector to Default_Handler, an infinite loop, so a forgotten handler spins until the watchdog resets. You find it in the map file, not at runtime. An erased entry reads 0xFFFFFFFF; bit 0 is set so it looks like a valid Thumb address, but the fetch lands in unmapped flash and faults. A wrong offset after an OTA update means the new image's table isn't where VTOR points; CRC the table region and re-check VTOR at boot. An incompatible signature (extra parameter, non-void return, or a C++ member function) is fine on Cortex-M (the core stacks context, so a plain void f(void) is correct) but corrupts registers on cores that need __irq or __attribute__((interrupt)).
Trade-offs. A flash table is the default: simple, read-only, no copy step. A RAM table lets you re-vector at runtime (RTOS dynamic handlers, debugger breakpoints) but costs RAM and needs the copy done before interrupts enable. VTOR relocation is clean on M3/M4; on Cortex-M0 you instead remap the app region to 0x00000000, which works only if the bootloader no longer needs interrupts after the jump. Finally, keeping the weak attribute lets a missing handler link silently; dropping it turns that into a link error: good for production, but it breaks vendor startup files, so the middle ground is to keep weak and add the map-file check to CI.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.