[PATCH] riscv: Terminate the frame-pointer chain at entry points [BZ #34950]
Tommy Chen
gcchen@google.com
Wed Nov 11 19:40:00 GMT 2026
_start, __thread_start and __thread_start_clone3 do not clear s0, so
the outermost frame record can link to a stale value instead of NULL.
The RISC-V psABI requires "The end of the frame record chain is
indicated by a NULL pointer appearing as the next link in the chain"
[1]. Frame-pointer unwinders such as perf --call-graph fp follow the
stale link into unrelated memory.
Clear s0 at the three entry points, as x86_64 (xorl %ebp, %ebp) and
aarch64 (mov x29, 0) already do in start.S, clone.S and clone3.S.
[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/main/riscv-cc.adoc#frame-pointer-convention
Signed-off-by: Tommy Chen <gcchen@google.com>
---
sysdeps/riscv/start.S | 2 ++
sysdeps/unix/sysv/linux/riscv/clone.S | 2 ++
sysdeps/unix/sysv/linux/riscv/clone3.S | 2 ++
3 files changed, 6 insertions(+)
diff --git a/sysdeps/riscv/start.S b/sysdeps/riscv/start.S
index bc3bc04219..328b54ef67 100644
--- a/sysdeps/riscv/start.S
+++ b/sysdeps/riscv/start.S
@@ -47,6 +47,8 @@ ENTRY (ENTRY_POINT)
.cfi_label to force starting the FDE. */
.cfi_label .Ldummy
cfi_undefined (ra)
+ /* Terminate the frame-pointer chain. */
+ li s0, 0
call load_gp
mv a5, a0 /* rtld_fini. */
/* main may be in a shared library. */
diff --git a/sysdeps/unix/sysv/linux/riscv/clone.S b/sysdeps/unix/sysv/linux/riscv/clone.S
index 1ce930a97e..a39d77eb6f 100644
--- a/sysdeps/unix/sysv/linux/riscv/clone.S
+++ b/sysdeps/unix/sysv/linux/riscv/clone.S
@@ -76,6 +76,8 @@ L (thread_start):
.cfi_label to force starting the FDE. */
.cfi_label .Ldummy
cfi_undefined (ra)
+ /* Terminate the frame-pointer chain. */
+ li s0, 0
/* Restore the arg for user's function. */
REG_L a1,0(sp) /* Function pointer. */
diff --git a/sysdeps/unix/sysv/linux/riscv/clone3.S b/sysdeps/unix/sysv/linux/riscv/clone3.S
index 7fe4e1c706..d8455da380 100644
--- a/sysdeps/unix/sysv/linux/riscv/clone3.S
+++ b/sysdeps/unix/sysv/linux/riscv/clone3.S
@@ -64,6 +64,8 @@ L(thread_start):
.cfi_label to force starting the FDE. */
.cfi_label .Ldummy
cfi_undefined (ra)
+ /* Terminate the frame-pointer chain. */
+ li s0, 0
/* Restore the arg for user's function and call the user's
function. */
--
2.56.0.679.gc527c65306-goog
More information about the Libc-alpha
mailing list