Debugging & Toolchain · Interview question

Why do you wrap the vector table in KEEP() in the linker script?

A strong answer

Because of garbage collection of unused sections. When you build with -ffunction-sections -fdata-sections and link with --gc-sections, the standard way to minimize image size, the linker discards any section that nothing references, since unreferenced code/data is presumed dead. The interrupt vector table is a special problem here: in normal C, nothing calls or references it, it's read by the hardware at reset and on each interrupt, not by any function, so from the linker's pure reference-analysis perspective it looks like dead, removable data. If it gets garbage-collected, the chip has no valid vector table at the flash base, so at reset it reads a garbage initial stack pointer and reset vector and immediately faults or hangs, the board appears completely dead. Wrapping it as KEEP(*(.isr_vector)) tells the linker to retain that section unconditionally, regardless of whether anything references it, so it survives --gc-sections and stays at the start of flash where the hardware expects it. The same KEEP() is needed for any other section that's used only by hardware or by mechanisms the linker can't see as references (certain init/constructor arrays, bootloader headers, configuration words at fixed addresses). So KEEP() is the escape hatch for "this is essential even though nothing in the code visibly references it."

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Linker Scripts & Memory Sections

Telling the linker the chip's memory map: MEMORY and SECTIONS place .text in flash and .data/.bss in RAM, and the VMA-vs-LMA split is why startup code must copy initialized data.

More Linker Scripts & Memory Sections questions

Browse all 472 interview questions
Why do you wrap the vector table in KEEP() in the linker script? | EmbeddedPrep.io