liveBook · Manning

Memory location

As explained in Embedded Software with Rust

A memory region is a contiguous address range with a defined role or access behavior in a processor's address space.

A memory region is a contiguous range of addresses with a defined role or access behavior. In embedded firmware, regions can hold program code, changing data, or memory-mapped peripheral registers. A program’s sections must be placed in regions that support how they are used. section 5.1.1 section 5.4.4

On Cortex-M, the address space includes code, SRAM, peripheral, external memory, and system regions. The broad map provides predictable ranges; the device documentation gives the actual addresses and available memory. section 5.1.1 section 4.1.2

Table 5.1 Arm Cortex-M memory map
Address Range Region Description

0x0000_0000–0x1FFF_FFFF

Code Region (Flash/ROM)

Holds program instructions

0x2000_0000–0x3FFF_FFFF

SRAM Region

RAM (stack, data, heap)

0x4000_0000–0x5FFF_FFFF

Peripheral Region

Memory-mapped peripheral registers

0x6000_0000–0x9FFF_FFFF

External RAM (optional)

External SRAM or PSRAM

0xA000_0000–0xDFFF_FFFF

External Devices

Memory-mapped external devices

0xE000_0000–0xFFFF_FFFF

System Region

NVIC, debug, SysTick, ITM, etc.

Compare the address ranges and roles of the Cortex-M memory regions. section 5.1.1

A linker script describes the physical regions available to the program and maps sections into them. In a MEMORY block, ORIGIN gives a region’s starting address and LENGTH gives its size. Obtain both values from the microcontroller datasheet: incorrect values can corrupt program code or data. section 4.1.2 section 5.4.4

MEMORY
{
    FLASH : ORIGIN = 0x08000000, LENGTH = 512K
    RAM   : ORIGIN = 0x20000000, LENGTH = 128K
}

This example defines FLASH and RAM; verify the addresses and sizes against the target device before using them. section 4.1.2

FLASH retains its contents across a power cycle; RAM is volatile and holds data that must change or be initialized. Code and read-only data generally stay in FLASH. Initialized static data in .data is stored in FLASH but copied to RAM before the application runs; .bss is allocated in RAM and zeroed at startup. section 4.1.1

The load memory address (LMA) identifies where a section is stored after loading, while the virtual memory address (VMA) identifies where it resides during execution. A .data section can therefore have a FLASH LMA and a RAM VMA. On bare metal, the reset handler prepares memory before transferring control to the application; runtime crates can perform the copy and zeroing steps. section 4.1.1 section 4.3 section 1.3.3

After building, run `cargo objdump --bin your_app_name -- -h` to inspect section headers and verify placement. Check that the vector table, code, and constants are in FLASH, and that .data and .bss have RAM execution addresses. section 4.1.5

Idx Name         Size     VMA        LMA        File off  Algn
  0 .vector_table 00000040 08000000  08000000   00010000  2**2
  1 .text         00001a00 08000040  08000040   00010040  2**2
  2 .rodata       00000200 08001a40  08001a40   00011a40  2**2
  3 .data         00000100 20000000  08001c40   00014000  2**2
  4 .bss          00000080 20000100  20000100   00014100  2**2

The example output shows .data with a RAM VMA and a FLASH LMA. section 4.1.5

For custom placement, `#[unsafe(link_section = ".tcm_data")]` tags a Rust symbol with a section name; it does not choose the address. The linker script must map that section to a suitable region. Placement is a safety responsibility: mutable data in read-only FLASH or code in a non-executable region can violate invariants. section 5.4.4

MEMORY {
  ITCM     : ORIGIN = 0x00000000, LENGTH = 64K
  DTCM     : ORIGIN = 0x20000000, LENGTH = 64K
  FLASH    : ORIGIN = 0x08000000, LENGTH = 1M
}

SECTIONS {
  .tcm_data (NOLOAD) : ALIGN(4) {
    KEEP(*(.tcm_data*))
  } > DTCM
}

This linker example maps a custom data section into DTCM for a Cortex-M7 device that provides it; STM32F103 does not have TCM. section 5.4.4

How Embedded Software with Rust covers it

How deep it goes
Explains region definitions, startup initialization, section placement, and inspection of load and execution addresses.
Examples
Uses STM32 FLASH and RAM definitions and a Cortex-M7 DTCM buffer placement example.
What you can do after
Operate and configure region definitions and section placement with a linker script, then inspect the result with cargo objdump.
How it connects
Connects linker scripts, memory-mapped I/O, startup initialization, and load and virtual memory addresses.

Also explained in

Compare how 4 books explain memory location →

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.