liveBook · Manning

Data section

As explained in Embedded Software with Rust

The .data section contains initialized writable data whose initial values are copied to writable memory during startup.

The .data section holds initialized writable static data. Its initial values are stored in nonvolatile FLASH, but the data must reside in RAM while the program runs. Before application code uses it, startup code copies those values into RAM. section 4.1.1 section 1.2.2

The load memory address (LMA) identifies the FLASH location holding the initial values; the virtual memory address (VMA) identifies their execution location in RAM. In contrast, .bss occupies RAM and is initialized to zero, while code and read-only data generally remain in FLASH. section 4.1.1

An initialized mutable static

In Rust, `static mut X: u32 = 42;` is an example of data placed in .data. Startup must copy the initial value 42 from FLASH to X’s RAM location before application code uses it. section 4.1.1

A linker script assigns .data to RAM while specifying FLASH as its load region. The startup routine uses linker-exported symbols to locate the FLASH source and the RAM boundaries, copies the data, and then zeroes .bss before transferring control to main. section 4.1.3 section 4.3.2

.data :
  {
    *(.data .data.*)
  } > RAM AT > FLASH

  .bss (NOLOAD) :
  {
    . = ALIGN(8);
    *(.bss .bss.*)
  } > RAM
}

The linker directives place .data in RAM with a FLASH load address and mark .bss as NOLOAD. section 4.1.3

After building, run `cargo objdump --bin your_app_name -- -h` to inspect section headers. Check that .data has a RAM VMA and a FLASH LMA. Runtime crates such as cortex-m-rt and riscv-rt can perform the copy and .bss initialization instead of requiring a custom reset handler. section 4.1.5 section 1.3.3

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 headers show .data with VMA 0x20000000 in RAM and LMA 0x08001c40 in FLASH. section 4.1.5

How Embedded Software with Rust covers it

How deep it goes
Explains load and execution addresses, linker placement, reset-handler copying, and inspection of the resulting section headers.
Examples
Uses a mutable Rust static initialized to 42, linker directives, a reset routine, and example objdump headers.
What you can do after
Operate and configure startup placement, then verify .data addresses with cargo objdump.
How it connects
Connects .data to linker scripts, reset handlers, load and virtual memory addresses, .bss, and read-only data.

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.