liveBook · Manning

Memory layout

As explained in Embedded Software with Rust

A memory layout specifies the placement of a program's code, data, stack, and other regions in memory.

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

How Embedded Software with Rust covers it

How deep it goes
Works through region definitions, section mapping, startup initialization, and struct alignment, with advanced linker-script troubleshooting deferred.
Examples
Uses STM32 FLASH and RAM maps, a complete linker script, and a USART register struct.
What you can do after
Operate/configure a microcontroller’s memory layout and inspect the resulting firmware placement with cargo objdump.
How it connects
Connects linker scripts, startup initialization, alignment, volatile access, and Rust-C interoperability.

Also explained in

Compare how 5 books explain memory layout →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.