Home / Learn Chip Programming / Memory Sections and the Linker Script

Memory Sections and the Linker Script

.text, .data, .bss, stack and heap: where your variables really live.

In short.text, .data, .bss, stack and heap: where your variables really live.

Theory in plain words

Typical MCU memoryFLASH: vectors, .text, .rodata, initial values of .dataRAM: .data (initialised globals)RAM: .bss (zeroed globals)RAM: heap grows up, stack grows down
Typical MCU memory

What it is

.text holds code (flash). .rodata holds constants (flash). .data holds initialised globals (copied from flash to RAM at boot). .bss holds zero-initialised globals. Stack and heap share the remaining RAM.

Why we use it

Knowing the layout lets you fix "region RAM overflowed", find stack overflows and place code or data in special memory.

How it works

The linker script defines memory regions (FLASH, RAM) and places sections. Startup code copies .data and clears .bss before main().

Where it is used

  • Reading the map file
  • Placing buffers in fast RAM
  • Bootloader and application layout

Where does it go?

Classify C declarationsTry it Yourself

Quiz yourself: guess the section, then reveal.

Linker script excerpt (GNU ld)
MEMORY {
  FLASH (rx)  : ORIGIN = 0x08000000, LENGTH = 256K
  RAM   (rwx) : ORIGIN = 0x20000000, LENGTH = 48K
}
SECTIONS {
  .isr_vector : { KEEP(*(.isr_vector)) } > FLASH
  .text   : { *(.text*) *(.rodata*) } > FLASH
  .data   : { _sdata = .; *(.data*) _edata = .; } > RAM AT > FLASH
  .bss    : { _sbss = .; *(.bss*) *(COMMON) _ebss = .; } > RAM
}
Tip:Run arm-none-eabi-size firmware.elf: text+data is flash use, data+bss is static RAM use. Remaining RAM is for stack and heap.

Quick quiz

1. Where does an initialised global int live?

2. What does startup code do before main()?

Common questions

Where does an initialised global int live?

.data. Value stored in flash, copied to RAM at boot.

What does startup code do before main()?

Copy .data and zero .bss. It prepares C runtime memory.