Linker Script Basics: Flash vs RAM Placement

Firmware must fit into hardware memory.
The linker decides where everything goes.
Flash stores code. RAM runs the system.

1. What a Linker Script Does

After source files are compiled, the linker combines object files into the final firmware image.

In embedded systems, the linker also needs instructions about how memory should be organized.

A linker script defines:
  • memory regions available on the microcontroller
  • where program sections should be placed
  • how code and data are arranged in the final binary

Without this mapping, the linker would not know how to place the firmware into device memory.

2. Flash Memory

Flash is non-volatile memory, meaning its contents remain even when power is removed.

In most embedded systems, Flash stores:
  • program instructions
  • constant data
  • initial values for global variables

When the system boots, the processor begins execution from an address located in Flash memory.

This is why firmware images are typically programmed into Flash.

3. RAM Memory

RAM is volatile memory, used for data that changes during program execution.

Typical RAM usage includes:
  • stack memory for function calls
  • heap memory for dynamic allocation
  • global variables that change at runtime
  • buffers and temporary data

Because RAM is faster to access than Flash, variables that require frequent modification are placed here.

4. Why Data Moves from Flash to RAM

Some variables require initial values but must still reside in RAM during execution.

To support this, their initial values are stored in Flash as part of the firmware image. During startup, the system copies these values into RAM.

This initialization step occurs before the program begins normal execution.

5. Practical Insight

Understanding Flash and RAM placement is critical when working with:
  • startup code
  • memory-constrained microcontrollers
  • bootloaders
  • performance-critical firmware

Many embedded bugs arise when memory sections are incorrectly placed or when RAM usage exceeds available limits.

The linker script acts as the map that tells firmware where everything belongs in memory.
The compiler creates instructions.
The linker decides where they live.
Hardware memory defines the limits.