expression

Account for alignment when estimating a section layout

A linker may insert padding to meet a section or object alignment. The example uses small fixed offsets and a power-of-two alignment. A real general calculation must check overflow and validate alignment; inspect the actual link map instead of treating this estimate as measured firmware size. Expected host stdout (not executed): next: 16, padding: 3 Common mistake: Adding only the lengths of objects and ignoring padding or reserved memory.

Formal Syntax & Structure

rust syntaxrust
aligned = (offset + align - 1) & !(align - 1)

Working Example

Example Snippetrust
fn main() {    let used = 13usize;    let alignment = 8usize;    let next = (used + alignment - 1) & !(alignment - 1);    println!("next: {next}, padding: {}", next - used);}
Rust Reference