From 2bb8bc39112f630246d4f73fc778e03ba616458f Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 11 Feb 2023 02:10:43 +0100 Subject: [PATCH 1/2] cortex-m-rt: ensure the stack is 8-byte aligned. Stack must be 8-byte aligned on ARM. Pushing 1 word makes it not aligned, so we push 2 words. This was breaking code that used LDRD/STRD on stack, since that needs 8-byte alignment. --- cortex-m-rt/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cortex-m-rt/src/lib.rs b/cortex-m-rt/src/lib.rs index 6f721975..b88b95a2 100644 --- a/cortex-m-rt/src/lib.rs +++ b/cortex-m-rt/src/lib.rs @@ -573,10 +573,11 @@ cfg_global_asm! { isb", // Push `lr` to the stack for debuggers, to prevent them unwinding past Reset. + // Push a dummy `r4` (which is always 0xFFFF_FFFF) to ensure the stack stays 8-byte aligned. // See https://sourceware.org/binutils/docs/as/CFI-directives.html. ".cfi_def_cfa sp, 0 - push {{lr}} - .cfi_offset lr, 0", + push {{r4, lr}} + .cfi_offset lr, 4", // Jump to user main function. // `bl` is used for the extended range, but the user main function should not return, From 6681bc7d872d36589960f788a47eb30d2e893f28 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 13 Feb 2023 11:07:26 +0100 Subject: [PATCH 2/2] Update cortex-m-rt/src/lib.rs Co-authored-by: James Munns --- cortex-m-rt/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cortex-m-rt/src/lib.rs b/cortex-m-rt/src/lib.rs index b88b95a2..c61c1e8e 100644 --- a/cortex-m-rt/src/lib.rs +++ b/cortex-m-rt/src/lib.rs @@ -574,7 +574,8 @@ cfg_global_asm! { // Push `lr` to the stack for debuggers, to prevent them unwinding past Reset. // Push a dummy `r4` (which is always 0xFFFF_FFFF) to ensure the stack stays 8-byte aligned. - // See https://sourceware.org/binutils/docs/as/CFI-directives.html. + // For CFI information, see https://sourceware.org/binutils/docs/as/CFI-directives.html. + // For 8 byte alignment, see https://github.com/ARM-software/abi-aa/blob/edd7460d87493fff124b8b5713acf71ffc06ee91/aapcs32/aapcs32.rst#6212stack-constraints-at-a-public-interface. ".cfi_def_cfa sp, 0 push {{r4, lr}} .cfi_offset lr, 4",