Q&A-1Walk me through where .text, .rodata, .data, .bss, the stack and the heap live in an embedded image, which consume flash, which consume RAM, and what the startup code does to each before main() runs?
Answer
Assume a bare-metal Cortex-M with execute-in-place flash and SRAM, built with arm-none-eabi-gcc or Arm Compiler 6 plus a scatter file. The linker emits one load image in flash and a run image in RAM; they differ only where a section has a different load address (LMA) and virtual address (VMA).
| section | flash (LMA) | RAM (VMA) | startup action |
|---|---|---|---|
| .isr_vector | yes, at 0x08000000 | no | none |
| .text | yes | no (XIP) | none |
| .rodata | yes | no | none |
| .data | yes, initialiser image | yes | copied flash to RAM |
| .bss | no, size only | yes | zeroed |
| .noinit | no | yes | none |
| stack | no | yes | none; painted for watermark |
| heap | no | yes | none; grown by _sbrk |
So .text and .rodata cost flash only, .bss/.noinit/stack/heap cost RAM only, and .data is the only one that costs both: it is counted twice in the budget.
The startup code is what makes that true. GCC's Reset_Handler:
c extern uint32_t _sidata, _sdata, _edata, _sbss, _ebss; void Reset_Handler(void) { uint32_t *src = &_sidata, *dst = &_sdata; while (dst < &_edata) *dst++ = src++; / .data: LMA -> VMA */ for (dst = &_sbss; dst < &_ebss; ) dst++ = 0; / .bss / SystemInit(); __libc_init_array(); / C++ ctors */ main(); }
_sidata is the flash load address the linker script assigns to .data; _sdata/_edata bracket the RAM copy. Arm Compiler does the same work inside __main, driven by the scatter file's RW_IRAM1 region. .bss has no flash image at all: only its size is recorded, which is why a 40 KB .bss costs zero flash.
Arithmetic on an STM32F103C8 (64 KB flash, 20 KB SRAM), hypothetical but representative: .text 18.0 KB, .rodata 3.0 KB, .data 1.2 KB, .bss 6.0 KB, .noinit 0.5 KB, stack 2.0 KB, heap 1.0 KB. Flash = 18.0 + 3.0 + 1.2 = 22.2 KB. RAM = 1.2 + 6.0 + 0.5 + 2.0 + 1.0 = 10.7 KB.
arm-none-eabi-size prints text data bss dec; the data column is the flash cost of .data, not its RAM cost, and dec is not RAM usage. Reading it as RAM is the classic budget error.
Failure modes worth naming:
- A bootloader that jumps to the application's
main, or a warm path that re-enters after its own startup, skips the copy: .data holds whatever was in RAM. Detect with a canary word in .data checked at the top of main, or a CRC over [_sdata, _edata). - The linker script gives .data an LMA that overlaps .text or the vector table.
sizewill not show it; the .map file's load addresses will. - Stack grows down into .bss or the heap. Guard with an MPU region below the stack, or have
_sbrkfail when the new break would cross__StackLimit. - A variable written
= 0lands in .bss under GCC's default-fzero-initialized-in-bss, so anything that must be non-zero at first read is silently zero. -ffunction-sections -fdata-sectionsplus--gc-sectionsis what keeps unused code out of .text; without it, dead drivers inflate flash and push the .data image around.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.