From e1b7dd6471665ba726e7fe624f9395e1ecc5a108 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 22 Sep 2026 22:23:49 +0200 Subject: [PATCH] rt: automatically align .text by default --- cortex-m-rt/CHANGELOG.md | 4 ++++ cortex-m-rt/link.x.in | 6 +++++- cortex-m-rt/src/lib.rs | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cortex-m-rt/CHANGELOG.md b/cortex-m-rt/CHANGELOG.md index a8ba0baa..c2afd59b 100644 --- a/cortex-m-rt/CHANGELOG.md +++ b/cortex-m-rt/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +- Align the default `_stext` to the alignment required by the `.text` section. Fixes + the lld warning `address (...) of section .text is not a multiple of alignment (...)`. + - Note: `_stext` is not auto-aligned if it's explicitly overridden by the user. It's their responsibility to align it, else the warning will show. + ## [v0.7.6] - Mark `pre_init` as deprecated diff --git a/cortex-m-rt/link.x.in b/cortex-m-rt/link.x.in index 39433827..54c94fde 100644 --- a/cortex-m-rt/link.x.in +++ b/cortex-m-rt/link.x.in @@ -89,7 +89,11 @@ SECTIONS KEEP(*(.vector_table.interrupts)); /* this is the `__INTERRUPTS` symbol */ } > FLASH - PROVIDE(_stext = ADDR(.vector_table) + SIZEOF(.vector_table)); + /* The alignment of .text is the maximum alignment required by its input sections, which + can be more than 4 (e.g. `.p2align` directives inside inline assembly). Since the + start address of .text is set explicitly, the linker won't align it automatically, so do it + here. */ + PROVIDE(_stext = ALIGN(ADDR(.vector_table) + SIZEOF(.vector_table), ALIGNOF(.text))); /* ### .text */ .text _stext : diff --git a/cortex-m-rt/src/lib.rs b/cortex-m-rt/src/lib.rs index 631faedc..7283ad80 100644 --- a/cortex-m-rt/src/lib.rs +++ b/cortex-m-rt/src/lib.rs @@ -113,6 +113,10 @@ //! _stext = ORIGIN(FLASH) + 0x40C; //! ``` //! +//! It is your responsibility to ensure `_stext` is aligned to what the `.text` section requires. +//! This is typically 4 but can ocasionally be higher, such as when using `.p2align` directives inside ASM. +//! If you fail to do so you will see a linker warning like `address (...) of section .text is not a multiple of alignment (...)`. +//! //! # An example //! //! This section presents a minimal application built on top of `cortex-m-rt`. Apart from the