Skip to content

Commit 9d63aa9

Browse files
bors[bot]adamgreig
andauthored
Merge #465
465: Enforce 8-byte initial stack pointer alignment r=adamgreig a=adamgreig After #463 we discovered that adding a second linker script via another compiler flag could be used to override `_stack_start` without triggering the assert in the main linker script. By masking the value, we force alignment even when the assert doesn't otherwise trigger. Co-authored-by: Adam Greig <[email protected]>
2 parents 2bdd95f + 4e86db7 commit 9d63aa9

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

cortex-m-rt/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
- A linker error is generated if the initial stack pointer is not 8-byte aligned
11+
- The initial stack pointer is now forced to be 8-byte aligned in the linker script,
12+
to defend against it being overridden outside of the cortex-m-rt linker script
13+
1014
## [v0.7.2]
1115

1216
- MSRV is now Rust 1.59.

cortex-m-rt/link.x.in

+6-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ SECTIONS
6868
{
6969
__vector_table = .;
7070

71-
/* Initial Stack Pointer (SP) value */
72-
LONG(_stack_start);
71+
/* Initial Stack Pointer (SP) value.
72+
* We mask the bottom three bits to force 8-byte alignment.
73+
* Despite having an assert for this later, it's possible that a separate
74+
* linker script could override _stack_start after the assert is checked.
75+
*/
76+
LONG(_stack_start & 0xFFFFFFF8);
7377

7478
/* Reset vector */
7579
KEEP(*(.vector_table.reset_vector)); /* this is the `__RESET_VECTOR` symbol */

cortex-m-rt/src/lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@
5656
//!
5757
//! This optional symbol can be used to indicate where the call stack of the program should be
5858
//! placed. If this symbol is not used then the stack will be placed at the *end* of the `RAM`
59-
//! region -- the stack grows downwards towards smaller address. This symbol can be used to place
60-
//! the stack in a different memory region, for example:
59+
//! region -- the stack grows downwards towards smaller address.
60+
//!
61+
//! For Cortex-M, the `_stack_start` must always be aligned to 8 bytes, which is enforced by
62+
//! the linker script. If you override it, ensure that whatever value you set is a multiple
63+
//! of 8 bytes.
64+
//!
65+
//! This symbol can be used to place the stack in a different memory region, for example:
6166
//!
6267
//! ```text
6368
//! /* Linker script for the STM32F303VCT6 */

0 commit comments

Comments
 (0)