Skip to content

Commit ecf840b

Browse files
Merge #337
337: Fix unwinding through `Reset` r=thejpster a=jonas-schievink Unwinders may detect the end of the program by seeing `0xFFFFFFFF` in `lr`, which is why code to ensure that it has that value was added in rust-embedded/cortex-m-rt#293. However, the `bl main` overwrites that value with the current program counter. This PR saves the old `lr` value on the stack, and adds debuginfo entries to allow an external unwinder to restore the value. This fixes knurling-rs/probe-run#277 Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents 008fd73 + b38af9d commit ecf840b

File tree

10 files changed

+18
-12
lines changed

10 files changed

+18
-12
lines changed

cortex-m-rt/asm.S

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ Reset:
9494
#endif
9595

9696
4:
97+
# Preserve `lr` and emit debuginfo that lets external tools restore it.
98+
# This fixes unwinding past the `Reset` handler.
99+
# See https://sourceware.org/binutils/docs/as/CFI-directives.html for an
100+
# explanation of the directives.
101+
.cfi_def_cfa sp, 0
102+
push {lr}
103+
.cfi_offset lr, 0
104+
97105
# Jump to user main function. We use bl for the extended range, but the
98106
# user main function may not return.
99107
bl main

cortex-m-rt/assemble.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ crate=cortex-m-rt
99
# remove existing blobs because otherwise this will append object files to the old blobs
1010
rm -f bin/*.a
1111

12-
arm-none-eabi-gcc -c -march=armv6s-m asm.S -o bin/$crate.o
12+
arm-none-eabi-gcc -g -c -march=armv6s-m asm.S -o bin/$crate.o
1313
ar crs bin/thumbv6m-none-eabi.a bin/$crate.o
1414

15-
arm-none-eabi-gcc -c -march=armv7-m asm.S -o bin/$crate.o
15+
arm-none-eabi-gcc -g -c -march=armv7-m asm.S -o bin/$crate.o
1616
ar crs bin/thumbv7m-none-eabi.a bin/$crate.o
1717

18-
arm-none-eabi-gcc -c -march=armv7e-m asm.S -o bin/$crate.o
18+
arm-none-eabi-gcc -g -c -march=armv7e-m asm.S -o bin/$crate.o
1919
ar crs bin/thumbv7em-none-eabi.a bin/$crate.o
2020

21-
arm-none-eabi-gcc -c -march=armv7e-m asm.S -DHAS_FPU -o bin/$crate.o
21+
arm-none-eabi-gcc -g -c -march=armv7e-m asm.S -DHAS_FPU -o bin/$crate.o
2222
ar crs bin/thumbv7em-none-eabihf.a bin/$crate.o
2323

24-
arm-none-eabi-gcc -c -march=armv8-m.base asm.S -o bin/$crate.o
24+
arm-none-eabi-gcc -g -c -march=armv8-m.base asm.S -o bin/$crate.o
2525
ar crs bin/thumbv8m.base-none-eabi.a bin/$crate.o
2626

27-
arm-none-eabi-gcc -c -march=armv8-m.main asm.S -o bin/$crate.o
27+
arm-none-eabi-gcc -g -c -march=armv8-m.main asm.S -o bin/$crate.o
2828
ar crs bin/thumbv8m.main-none-eabi.a bin/$crate.o
2929

30-
arm-none-eabi-gcc -c -march=armv8-m.main -DHAS_FPU asm.S -o bin/$crate.o
30+
arm-none-eabi-gcc -g -c -march=armv8-m.main -DHAS_FPU asm.S -o bin/$crate.o
3131
ar crs bin/thumbv8m.main-none-eabihf.a bin/$crate.o
3232

3333
rm bin/$crate.o
1.09 KB
Binary file not shown.
1.12 KB
Binary file not shown.
1.13 KB
Binary file not shown.
1.12 KB
Binary file not shown.
1.09 KB
Binary file not shown.
1.12 KB
Binary file not shown.
1.13 KB
Binary file not shown.

cortex-m-rt/build.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ fn main() {
1919
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
2020

2121
if target.starts_with("thumbv") {
22-
fs::copy(
23-
format!("bin/{}.a", target),
24-
out_dir.join("libcortex-m-rt.a"),
25-
)
26-
.unwrap();
22+
let lib_path = format!("bin/{}.a", target);
23+
fs::copy(&lib_path, out_dir.join("libcortex-m-rt.a")).unwrap();
2724
println!("cargo:rustc-link-lib=static=cortex-m-rt");
25+
println!("cargo:rerun-if-changed={}", lib_path);
2826
}
2927

3028
// Put the linker script somewhere the linker can find it

0 commit comments

Comments
 (0)