liveBook · Manning

Alignment

As explained in Embedded Software with Rust

Memory alignment is the placement of data or code at addresses satisfying the alignment requirements of the target architecture.

Memory alignment places data at addresses that satisfy the target architecture’s requirements. An address aligned to 8 bytes is a multiple of 8. In bare-metal Rust, alignment is part of the memory layout established before main() runs. section 4.1.1 section 4.1.3

On Arm Cortex-M, the initial stack pointer must be 8-byte aligned under the Arm ABI followed by Rust. This matters for 64-bit local variables and function arguments such as u64 and f64. Incorrect alignment can cause unaligned accesses, faults, or data corruption. section 4.1.3

The .bss section should also be aligned to an 8-byte boundary, as should other data sections where possible, so that 64-bit static variables are correctly aligned. A linker script can use ALIGN(8) to align its location counter before placing section contents. section 4.1.3

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

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

The linker script aligns the location counter before placing .bss inputs. It also places .data in RAM with its initial contents loaded from FLASH. section 4.1.3

Alignment does not replace startup initialization. Before main(), startup code must copy .data from FLASH to RAM and zero .bss, unless a runtime crate handles those steps. Check the final memory layout with cargo objdump, and verify the stack address separately from the data sections. section 4.1.1 section 4.1.3

How Embedded Software with Rust covers it

How deep it goes
Explains 8-byte stack and data-section alignment, associated faults, and the linker directive used for section placement.
Examples
Uses 64-bit stack variables and a linker script aligning .bss before placing its input sections.
What you can do after
Operate/configure a linker script to align .bss and inspect the final memory layout with cargo objdump.
How it connects
Connects alignment to linker scripts, stack pointers, static variables, startup initialization, and processor faults.

Also explained in

Compare how 5 books explain alignment →

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.