Try Embedded

Nucleo-F401RE: where every variable lives

Print the address of a constant, an initialised global, a zeroed one, a static, a local, a heap block and a function, check them against the linker's…

Firmware · Intermediate

In a real product: Memory budget check on a sensor node (Industrial): Firmware engineers check RAM, flash and stack use before every release.

Every variable in a C program ends up in one of a few places: flash, the .data or .bss part of RAM, the heap or the stack. Which one depends on how it is declared. The linker script decides where each place is, and the startup code fills them before main() runs.

This program prints where each kind of variable landed on the STM32F401 (512 KB of flash at 0x08000000, 96 KB of RAM at 0x20000000). It checks each address against the linker's own symbols, then measures how deep a recursive function went into the stack.

What you'll learn

  • .rodata and .text (flash), .data and .bss (RAM), heap and stack, and which declaration goes where
  • The linker symbols _sidata, _sdata, _edata, _sbss, _ebss, _estack and how the startup code uses them
  • Why an initialised global costs flash and RAM, and a zeroed one only RAM
  • Stack painting: fill unused stack with a pattern, run the code, and see how much of the pattern is gone

How it works

  • const data and string literals stay in flash (.rodata), next to the code (.text). A function's address is odd because of the Thumb bit.
  • A global with a value, like int boot_count = 42, lives in RAM between _sdata and _edata. Its first value is stored in flash at _sidata, and Reset_Handler copies it over before main(). The program reads both copies to show they match.
  • A global without a value, or set to 0, is in .bss, between _sbss and _ebss. The startup code only has to zero that range, so it takes no flash.
  • A static variable inside a function is in .data or .bss too: it lives as long as the program, only its name is local.
  • Locals are on the stack, which starts at _estack (the top of RAM) and grows down. malloc takes heap memory, which starts after .bss and grows up towards it.

To measure stack use, the program writes 0xA5A5A5A5 into 4 KB below the current stack pointer, calls a recursive function 20 levels deep, then counts from the bottom how many words still hold the pattern. Everything above the first changed word was used. In the build's map file you would see the same section sizes; here the program works them out from the symbols.

Try this

  • Change int boot_count = 42 to = 0: it moves from .data to .bss, and the .data size shrinks by 4.
  • Make lookup non-const and watch it move to RAM.
  • Double the recursion depth and compare the stack use: each level costs the same number of bytes.

main.c

// Bare-metal C on the Nucleo-F401RE: no Arduino core and no HAL. CMSIS names every register.
// Where each kind of variable lives, checked against the linker script's symbols, and the stack measured by
// painting it.
#include <stdio.h>
#include <stdlib.h>
#include "stm32f4xx.h"

// The linker script's symbols: their addresses are the values (they have no storage of their own).
extern uint32_t _sidata, _sdata, _edata, _sbss, _ebss, _estack;

static const uint16_t lookup[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };   // .rodata (flash)
int boot_count = 42;                                                  // .data (RAM, first value in flash)
int errors;                                                           // .bss (RAM, zeroed at start)

int __io_putchar(int ch) {
  while (!(USART2->SR & USART_SR_TXE)) {}
  USART2->DR = ch;
  return ch;
}

static const char *where(const void *p) {
  uint32_t a = (uint32_t)p;
  if (a >= 0x08000000 && a < 0x08080000) return "flash";
  if (a >= (uint32_t)&_sdata && a < (uint32_t)&_edata) return "RAM .data";
  if (a >= (uint32_t)&_sbss && a < (uint32_t)&_ebss) return "RAM .bss";
  if (a >= (uint32_t)&_ebss && a < (uint32_t)&_estack) return "RAM, heap or stack";
  return "?";
}

static void show(const char *what, const void *p) {
  printf("%-26s 0x%08lx  %s\r\n", what, (unsigned long)(uint32_t)p, where(p));
}

static int *counter(void) {
  static int calls;                                                   // .bss, though its name is local
  calls++;
  return &calls;
}

static uint32_t depth(int n) {                                        // recursion: a stack frame per level
  volatile uint32_t frame[4] = { (uint32_t)n, 0, 0, 0 };
  return n ? depth(n - 1) + frame[0] : 0;
}

static uint32_t stack_pointer(void) {
  uint32_t sp;
  __asm volatile ("mov %0, sp" : "=r"(sp));
  return sp;
}

int main(void) {
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
  GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODER2) | GPIO_MODER_MODER2_1;
  GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu << 8)) | (7u << 8);
  USART2->BRR = SystemCoreClock / 115200;
  USART2->CR1 = USART_CR1_TE | USART_CR1_UE;

  int local = 7;
  int *heap = malloc(64);
  show("const lookup[]", lookup);
  show("string literal", "hello");
  show("function main()", (const void *)main);
  show("int boot_count = 42", &boot_count);
  show("int errors", &errors);
  show("static int calls", counter());
  show("int local", &local);
  show("malloc(64)", heap);

  // boot_count's first value, where the startup code copied it from.
  const int *original = (const int *)((uint32_t)&_sidata + ((uint32_t)&boot_count - (uint32_t)&_sdata));
  printf("\r\nboot_count = %d in RAM, %d in flash at 0x%08lx\r\n", boot_count, *original, (unsigned long)(uint32_t)original);
  printf(".data %lu bytes, .bss %lu bytes, stack top 0x%08lx\r\n",
         (unsigned long)((uint32_t)&_edata - (uint32_t)&_sdata), (unsigned long)((uint32_t)&_ebss - (uint32_t)&_sbss),
         (unsigned long)(uint32_t)&_estack);

  // Paint 4 KB below the stack pointer, use the stack, and see how far down the paint is gone.
  uint32_t sp = stack_pointer();
  volatile uint32_t *bottom = (volatile uint32_t *)(sp - 4096);
  for (volatile uint32_t *p = bottom; p < (volatile uint32_t *)(sp - 64); p++) *p = 0xA5A5A5A5;
  uint32_t sum = depth(20);
  volatile uint32_t *p = bottom;
  while (*p == 0xA5A5A5A5 && p < (volatile uint32_t *)sp) p++;
  printf("depth(20) = %lu used %lu bytes of stack below main's frame\r\n", (unsigned long)sum,
         (unsigned long)(sp - (uint32_t)p));

  for (;;) {}
}

Lessons behind it