A linker script directs the linker when arranging program sections and assigning addresses in an output image. In bare-metal firmware, it describes the device’s memory and places code, data, the vector table, and the stack where the processor can use them. section 4.1 section 3.2.1
Start by defining FLASH and RAM with their ORIGIN addresses and LENGTH sizes from the microcontroller’s datasheet. Some scripts also specify access permissions. Incorrect values can corrupt program data, cause runtime faults, or prevent firmware updates. section 3.2.1 section 4.1.2
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
}
This memory.x example defines FLASH and RAM for a particular STM32 device. Its addresses and sizes must be checked against the actual target. section 3.2.1
With runtime crates such as cortex-m-rt and riscv-rt, a project typically supplies memory.x at the crate root. The runtime’s link.x includes it. For other layouts, a custom script can define both the memory regions and section placement; the build must reference the script intended for the target. section 3.2.1 section 1.3.3 section 4.4.5
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 512K
RAM : ORIGIN = 0x20000000, LENGTH = 128K
}
SECTIONS
{
.vector_table :
{
LONG(ORIGIN(RAM) + LENGTH(RAM));
KEEP(*(.vector_table.reset_vector))
KEEP(*(.vector_table.exceptions))
} > FLASH
.text :
{
*(.text .text.*)
} > FLASH
.rodata :
{
*(.rodata .rodata.*)
} > FLASH
.data :
{
*(.data .data.*)
} > RAM AT > FLASH
.bss (NOLOAD) :
{
. = ALIGN(8);
*(.bss .bss.*)
} > RAM
}
This minimal STM32 layout places the vector table, code, and read-only data in FLASH. It assigns .data to RAM with a FLASH load location, and reserves .bss in RAM. section 4.1.4
A section’s load address and execution address can differ. Initialized .data is stored in FLASH but copied to RAM before the program runs; .bss occupies RAM and must be zeroed. Startup code performs these steps unless a runtime crate handles them. section 4.1.1 section 1.3.3
For manual initialization, the script can define symbols marking the boundaries of .data and .bss and the FLASH load address of .data. The reset handler uses those addresses to copy and zero the right ranges; its symbol names must match the script. section 4.3.4
Check section alignment and inspect the final memory layout with cargo objdump. In a Cargo project, rustflags in .cargo/config.toml can select a script such as link.x for each build. section 4.1.1 section 3.2.3
