Skip to content

Commit 4a97063

Browse files
committed
riscv/crt0: Avoid clobbering the caller's stack in _trap
Currently the trap frame will overwrite previously saved registers, so the GDB backtrace is somewhat unreliable. Instead of writing the trap frame to the top of the stack write it to the end of the heap instead. While this could still corrupt some state it is hopefully still unused and will definitely not overwrite previous return addresses. In the previous example I now get a backtrace all the way up to main(): ``` $ gdb -q test/test-fopen '-ex=b crt0.c:169' '-ex=target remote :1234' -ex=c -ex=bt Reading symbols from test/test-fopen... Breakpoint 1 at 0x800001c0: file ../../picolibc/picocrt/machine/riscv/crt0.c, line 168. Remote debugging using :1234 0x0000000000001000 in ?? () Continuing. Breakpoint 1, _trap () at ../../picolibc/picocrt/machine/riscv/crt0.c:169 169 __asm__("j _ctrap"); #0 _trap () at ../../picolibc/picocrt/machine/riscv/crt0.c:168 #1 0x00000000800013d0 in fclose (f=0x804008c0) at ../../picolibc/newlib/libc/tinystdio/fclose.c:39 #2 0x0000000080000680 in main () at ../../picolibc/test/test-fopen.c:85 ```
1 parent d81b8cc commit 4a97063

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

picocrt/machine/riscv/crt0.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ _trap(void)
104104
__asm__(".option push\n"
105105
".option norelax\n"
106106
"csrrw sp, mscratch, sp\n"
107-
"la sp, __stack\n"
107+
"la sp, __heap_end\n"
108108
".option pop");
109109

110110
/* Make space for saved registers */

0 commit comments

Comments
 (0)