5.5.17 · D3Embedded Systems & Real-Time Software

Worked examples — Linker scripts — memory regions, sections (.text, .data, .bss)

2,748 words12 min readBack to topic

The scenario matrix

Every situation this topic can throw at you falls into one of these cells. The examples below are each tagged with the cell(s) they cover, and together they hit all of them.

# Cell class The tricky thing it tests
C1 Pure .text, no variables LMA = VMA (code runs in place)
C2 .data present two addresses; Flash image ≠ RAM run
C3 .bss present size recorded, zero Flash bytes
C4 .data and .bss together RAM stacks them; Flash holds only .data copy
C5 Zero / degenerate: empty .data or empty .bss _sdata == _edata; copy loop runs 0 times
C6 Limiting: region exactly full / one byte over overflow detection, <= L boundary
C7 Alignment forces a gap . jumps forward; sizes stop being "just the sum"
C8 Word-problem: "how much Flash does adding X cost?" reasoning about .bss vs .data growth
C9 Exam twist: forgot the copy loop / dropped KEEP predict the runtime symptom

The two constant facts we lean on everywhere (from the parent's formula callout):

For all examples our chip is: (1K = 1024 bytes, so 256K = 262144 bytes, 64K = 65536 bytes.)


Example 1 — Code only (cell C1)


Example 2 — One initialized global (cell C2)


Example 3 — Zero-init global (cell C3)


Example 4 — .data and .bss stacked (cell C4)


Example 5 — Degenerate: empty .data (cell C5)


Example 6 — Limiting: RAM exactly full, then one byte over (cell C6)


Example 7 — Alignment gap (cell C7)


Example 8 — Word problem: "what does one more variable cost?" (cell C8)


Example 9 — Exam twist: predict the runtime symptom (cell C9)


Recall Quick self-test

In Example 4, what is _ebss? ::: 0x2000008C = 0x20000000 + 140. In Example 5, how many times does the .data copy loop run? ::: Zero — _sdata == _edata. In Example 6, by how many bytes does RAM overflow when .bss = 40500? ::: 84 bytes. In Example 7, how many padding bytes does ALIGN(4) insert after a 4002-byte .text? ::: 2 bytes. In Example 8, which option keeps Flash headroom? ::: (b), the zeroed .bss buffer — 0 Flash cost.


  • Parent: Linker scripts — memory regions, sections (.text, .data, .bss)
  • The boot copy/zero loops: Startup code & Reset_Handler
  • Why .isr_vector must survive GC: Vector table & interrupt handling
  • Where .data/.bss come from: Compilation pipeline — object files & relocation
  • The physical tradeoff behind two memories: Flash vs RAM tradeoffs
  • RAM budget's other tenants: Stack vs Heap in embedded systems
  • What the .bin actually contains: ELF file format & sections