Q&A-1Walk me through the stages of a C/C++ cross-compilation toolchain from source files to final flashable images (.elf, .hex, .bin), explaining what each tool transforms and resolves.
Answer
The 90-second version
gcc is a driver, not a compiler. It runs four tools in sequence, passing the target flags (-mcpu=cortex-m4 -mthumb) down to each: cpp → cc1/cc1plus → as → ld. Each stage lowers the representation and resolves one class of unknown: text, then AST, then target assembly, then a relocatable object with symbolic references, then a fully addressed ELF. .hex and .bin are not stages: they are objcopy re-encodings of the ELF's loadable segments.
Assume bare-metal Cortex-M, arm-none-eabi-gcc, newlib-nano, flash at 0x08000000, RAM at 0x20000000.
| Stage | Tool | In → Out | What it resolves |
|---|---|---|---|
| Preprocess | cpp (-E) | .c/.h → .i | #include, #define, #if; emits #line markers |
| Compile | cc1/cc1plus (-S) | .i → .s | Parse, type-check, optimize, select target instructions |
| Assemble | as (-c) | .s → .o | Mnemonics → opcodes; builds .text/.data/.rodata/.bss plus symbol and relocation tables |
| Link | ld (-Wl,-T) | .o + .a + .ld → .elf | Symbol resolution, VMA/LMA placement, relocation patching, CRT startup |
| Extract | objcopy | .elf → .hex/.bin | Nothing new: re-encodes loadable segments |
Linker: the only stage that knows addresses. ld merges same-named sections from every .o, places them at addresses from the script's MEMORY/SECTIONS, and patches each relocation with the final address. It also pulls in CRT startup (crt0.o, or your startup_stm32.s), which copies .data from its flash LMA to its RAM VMA and zeroes .bss before main. -ffunction-sections -fdata-sections plus -Wl,--gc-sections drops unreferenced functions; without the first two flags, --gc-sections can only drop whole object files.
objcopy: re-encoding, not compiling. The .elf keeps section headers, the symbol table and DWARF. objcopy -O ihex writes ASCII Intel HEX records, each carrying its own address and checksum, so non-contiguous regions cost nothing. objcopy -O binary writes one flat stream from the lowest to the highest loadable LMA: addresses are gone, so the programmer needs a base address supplied separately.
Worked trace
// blink.c
volatile uint32_t ticks;
const char banner[] = "blink";
int main(void) { for (;;) ticks++; }arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O2 \
-ffunction-sections -fdata-sections -c blink.c -o blink.o
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -T stm32.ld \
-Wl,-Map=blink.map -Wl,--gc-sections -Wl,--print-memory-usage \
blink.o -o blink.elf
arm-none-eabi-objcopy -O ihex blink.elf blink.hex
arm-none-eabi-objcopy -O binary blink.elf blink.binticks lands in .bss: no initialiser, so it occupies no image bytes. banner lands in .rodata and does occupy image bytes. arm-none-eabi-size blink.elf prints text/data/bss, but blink.bin's length is not text + data: it is the span from the lowest to the highest loadable LMA, because objcopy -O binary uses LMA. That is also why .data's initialisers appear in the image even though .data lives in RAM.
Now the gap arithmetic. If the script puts the bootloader at 0x08000000 and the app at 0x08040000, the binary spans 0x08040000 - 0x08000000 = 0x40000, i.e. 256 KiB of zeros in the middle. Flashing that writes over whatever lives in those sectors. The .hex does not have this problem: each record carries its address, so the programmer skips the hole. If you must ship a .bin, extract per region (objcopy -O binary -j .app blink.elf app.bin) or flash the hex.
Failure modes worth naming
undefined reference to 'foo'at link: declared but never defined.arm-none-eabi-nmthe.afiles to see what is actually there.region FLASH overflowed by N bytes: the script's region is too small.-Wl,--print-memory-usageor the.mapshows which section grew.- Globals read garbage at runtime:
.datawas never copied from flash, or.bssnever zeroed. Check the startup code and the.mapLMA/VMA columns. - Illegal instruction on target: object built for the wrong
-mcpu/-mthumb.objdump -dshows the encoding.
Verification: readelf -S for sections, nm for symbols, objdump -d for disassembly, -Wl,-Map for placement.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.