UNIT 04 · BUILDING AND RUNNING FIRMWARE
The Build Toolchain
From text files to the bytes in flash.
A firmware image is not written; it is built. Dozens of source files pass through a preprocessor, a compiler and an assembler into object files full of placeholders, and a linker, steered by a script that describes the chip’s memory, stitches them into one image with every address decided. When a build fails with an undefined reference, when RAM overflows by 312 bytes, when a function is mysteriously missing from the binary or a release cannot be rebuilt bit for bit, the explanation is in this pipeline, and the tools to read it (nm, objdump, size, the map file) come with every toolchain.
The unit in six ideas
- 1A cross toolchain runs on your PC and targets the chip; the triple
arm-none-eabinames architecture, OS (none) and ABI. - 2The linker resolves every undefined symbol to exactly one definition, then relocates: patches every placeholder with its formula.
- 3An ELF object holds sections, a symbol table (16-byte entries with binding and type) and relocations;
objdump -h,nmandobjdump -rshow them. - 4
MEMORYnames the chip’s regions;SECTIONSmaps input sections to output sections and regions. Both come from the datasheet, not the compiler. - 5size’s Berkeley columns: text (code and constants), data (initialised), bss (zeroed). Flash = text + data; RAM = data + bss, plus stack and heap.
- 6Configurations differ in optimisation, debug information and assertions;
-gdoes not change code,NDEBUGremoves asserts and their side effects.
Lessons
From Source Code to Machine Code
What did each one do, and why can the number 466 only be computed at the very end?
Compiling, Assembling, and Linking
What rules is the linker applying, and what exactly does it do to the bytes the assembler produced?
Object Files, Symbols, and Libraries
What is inside a .o, and how does the linker choose what to take from a .a?
Linker Scripts and Memory Sections
What does that file say, and how does it turn a pile of sections into an image with every address decided?
Reading a Map File and Disassembly
How do you read each?
Build Configurations and Reproducible Builds
What makes a build fast, correct and repeatable?