A memory layout specifies where firmware components reside in memory. On a bare-metal microcontroller, a linker script places code, constants, variables, the stack, and the interrupt vector table in the device’s available memory regions. Unlike a desktop program, firmware cannot rely on an operating system to manage this placement. section 4.1
Start with the microcontroller’s datasheet or technical reference manual. Define each region’s starting address (ORIGIN), size (LENGTH), and applicable permissions in a linker script. Incorrect values can corrupt data or cause runtime faults. section 3.2.1 section 4.1.2
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
}
This STM32 example defines 64 KB of executable FLASH and 20 KB of writable RAM. Use the values for the actual device rather than copying them blindly. section 3.2.1
With runtime crates such as cortex-m-rt, a project typically supplies memory.x at the crate root; the runtime-generated link.x includes it. The linker uses these region definitions to place program sections and the stack. section 3.2.1
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
}
A minimal linker script maps the vector table, code, and constants to FLASH; initialized data runs in RAM but loads from FLASH; and .bss occupies RAM. Its initial stack address is the top of RAM. section 4.1.4
Placement also determines startup work. Before main runs, startup code copies initialized data from its load memory address (LMA) in FLASH to its execution address (VMA) in RAM and zeros .bss. A runtime crate can perform these steps; otherwise, they must be handled manually. Inspect the resulting layout with cargo objdump. section 4.1.1
Layout also matters within a value. Alignment can require padding, increasing a struct’s size. Rust’s default struct layout is not guaranteed to be stable across targets or compiler versions. For a hardware register block or a struct shared with C, #[repr(C)] uses C-compatible field ordering and padding; it does not by itself make register access volatile-safe. section 5.4.1 section 5.4.2 section 8.2.3
#[repr(C)]
pub struct UsartRegisters {
// Status Register @ 0x00
sr: vcell::VolatileCell<u32>,
// Data Register @ 0x04
dr: vcell::VolatileCell<u32>,
// Baud Rate Register @ 0x08
brr: vcell::VolatileCell<u32>,
// Control Register 1 @ 0x0C
cr1: vcell::VolatileCell<u32>,
// Control Register 2 @ 0x10
cr2: vcell::VolatileCell<u32>,
// Control Register 3 @ 0x14
cr3: vcell::VolatileCell<u32>,
// Guard Time and Prescaler Register @ 0x18
gtpr: vcell::VolatileCell<u32>,
}
This register block combines #[repr(C)] for field layout with VolatileCell for volatile register reads and writes. section 5.4.2




