Define Flash and RAM Regions With the MEMORY Command
Describe the flash and RAM of a microcontroller to GNU ld with the MEMORY command, then place sections in them and read the overflow errors it produces.

A microcontroller has no operating system to hand out addresses. Flash starts at one fixed address, RAM at another, and the linker has to know both before it can place a single byte of your program. The GNU linker learns them from the MEMORY command in a linker script, and without it the linker assumes one flat region that starts at address zero and never runs out. That default is fine for a hosted Linux program and wrong for firmware. This post builds a MEMORY block for a Cortex-M4 part with 512 KB of flash and 128 KB of RAM, puts sections into the two regions and shows the exact error the linker prints when the program no longer fits. It assumes GNU ld from binutils 2.42 and the arm-none-eabi toolchain built from it. The background on scripts in general is in the complete guide to GNU linker scripts.
What MEMORY does and does not do
MEMORY declares named regions: a name, a set of attributes, a start address (ORIGIN) and a size (LENGTH). It does not place anything. It is a map of the territory, and the SECTIONS command later says which output section goes where. The GNU ld manual describes the syntax exactly, and it is short.
A region has three jobs. It gives the linker a bound to check against, so a section that grows past the end of flash becomes a link error instead of a silent overwrite of the next peripheral. It gives the script named constants through ORIGIN() and LENGTH(), which is how the initial stack pointer is usually computed. And it gives each section a destination through the > REGION syntax, so the script never contains a raw address in the middle of the layout.
Region names live in their own namespace. A region called FLASH and a symbol called FLASH do not collide, though reusing the name for both is a habit that confuses readers.
The smallest useful block
Here is a MEMORY block for an STM32F411-class part. The addresses come from the reference manual's memory map: flash is mapped at 0x08000000 and SRAM at 0x20000000.
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
}Each line has the same shape. The name comes first. The attributes in parentheses say what the region may hold: r read-only, w read-write, x executable, a allocated, i or l initialized. They matter only when the linker has to place a section without an explicit destination, so the script picks the first region whose attributes fit. The colon, then ORIGIN and LENGTH. The suffixes K and M multiply by 1024 and 1048576, so 512K is 0x80000 bytes. ORIGIN can be abbreviated to org or o and LENGTH to len or l, though spelling them out is easier to read a year later.
A MEMORY block is not usable alone. The linker still needs a SECTIONS command that sends output sections to the regions, and a firmware needs an entry point.
Placing sections in the regions
This complete script uses the two regions and the > REGION form. It is the smallest script that links a bare-metal program.
ENTRY(Reset_Handler)
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
}
_estack = ORIGIN(RAM) + LENGTH(RAM);
SECTIONS
{
.isr_vector : { KEEP(*(.isr_vector)) } > FLASH
.text : { *(.text*) *(.rodata*) } > FLASH
.data : { *(.data*) } > RAM AT > FLASH
.bss : { *(.bss*) *(COMMON) } > RAM
}_estack = ORIGIN(RAM) + LENGTH(RAM); sets a symbol to the first address past the end of RAM. A Cortex-M core loads its stack pointer from the first word of the vector table, and a full-descending stack starts at the top, so this one line is the whole stack-pointer setup. The vector table section is wrapped in KEEP so that --gc-sections cannot discard it, which matters because nothing in the code references it.
The .data line uses > RAM AT > FLASH. It says the section runs in RAM but is stored in flash, which is how initialized globals survive a power cycle. The mechanics of that copy are in copying .data from flash to RAM with AT and LOADADDR. Here it is enough to know that two regions are involved.
Compile and link a one-file program against the script:
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -Os -ffreestanding -c main.c -o main.o
arm-none-eabi-ld -T stm32f411.ld -Map=firmware.map main.o -o firmware.elf
arm-none-eabi-readelf -S firmware.elfThere are 8 section headers, starting at offset 0x10a4:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .isr_vector PROGBITS 08000000 010000 000008 00 A 0 0 4
[ 2] .text PROGBITS 08000008 010008 00001c 00 AX 0 0 4
[ 3] .data PROGBITS 20000000 020000 000004 00 WA 0 0 4
[ 4] .bss NOBITS 20000004 020004 000010 00 WA 0 0 4The sizes depend on your main.c, and the section list is trimmed here, but the addresses show the point: the vector table and code sit at 0x08000000 in flash, and .data and .bss sit at 0x20000000 in RAM.
The error you get when a region overflows
Add a 600 KB constant table to the program and relink. The linker refuses:
arm-none-eabi-ld: firmware.elf section `.text' will not fit in region `FLASH'
arm-none-eabi-ld: region `FLASH' overflowed by 98304 bytesBoth lines come from the region check. The first names the section that crossed the end, the second says by how much, which here is 600 KB of table plus the other code minus the 512 KB limit. This is the reason to write MEMORY at all. Without the block, the same program links successfully into an image that does not fit on the chip, and the failure appears later as a flash programming error or, worse, as a program that runs until it reaches an address that does not exist.
The overflow message names the region and the amount. Fix it by shrinking the program, not by raising LENGTH. A LENGTH larger than the silicon is a lie the linker cannot detect.
Attributes and the unnamed placement rule
Attributes decide where a section goes only when the script does not say. If you leave off the > FLASH, the linker looks for a region whose attributes match the section flags: executable and read-only code fits an rx region, while writable data fits an rw one. Explicit placement is clearer, and for firmware it is what you should always write. The case where attributes still help is the safety net. An attribute prefixed with ! inverts the test, so RAM (!rx) says "anything that is not read-only or executable", and the linker will not drop code there by accident.
Two regions can overlap on purpose. Parts with a tightly coupled memory alias often list both views, and an address range that the script reuses for different roles is legal. The linker does not warn. Overlap by accident is the mistake worth guarding against, and the checks in catching layout mistakes at link time with ASSERT cover it.
When MEMORY is not the right tool
On Linux userland you rarely write a MEMORY block. The default linker script for a hosted target places sections at addresses the dynamic loader expects, and overriding it with a flat region would break position-independent executables. MEMORY belongs in bare-metal firmware, bootloaders and kernels, where you control the whole address map.
It is also the wrong place to describe things the linker cannot know, such as which RAM bank is faster or which flash sector is erasable together. Those constraints go in comments, in ASSERT checks or in separate regions per sector.


