[PATCH] RISC-V: Fix SIGSEGV of --static-pie binaries on riscv64 [BZ #33911]
daichengrong
daichengrong@iscas.ac.cn
Fri Mar 6 08:00:01 GMT 2026
Signed-off-by: daichengrong <daichengrong@iscas.ac.cn>
---
sysdeps/riscv/cpu-features.c | 65 ++++++++++++++++++++++++++++++++++++
sysdeps/riscv/dl-machine.h | 4 +--
sysdeps/riscv/libc-start.c | 36 ++++++++++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)
create mode 100644 sysdeps/riscv/cpu-features.c
create mode 100644 sysdeps/riscv/libc-start.c
diff --git a/sysdeps/riscv/cpu-features.c b/sysdeps/riscv/cpu-features.c
new file mode 100644
index 0000000000..a7a0b040f0
--- /dev/null
+++ b/sysdeps/riscv/cpu-features.c
@@ -0,0 +1,65 @@
+/* Initialize CPU feature data.
+ This file is part of the GNU C Library.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+# pragma GCC visibility push(hidden)
+#include <assert.h>
+#include <unistd.h>
+#include <ldsodefs.h>
+
+#include <dl-machine.h>
+#include <dl-debug.h>
+
+#define RESOLVE_MAP(map, scope, sym, version, flags) map
+#include "elf/dynamic-link.h"
+#include "elf/get-dynamic-info.h"
+
+/* Relocate static executable with PIE for local ifunc symbol. */
+static void
+init_cpu_features (void)
+{
+ struct link_map *main_map = _dl_get_dl_main_map ();
+
+ ElfW(Addr) file_p_vaddr = 0;
+ const ElfW(Phdr) *ph, *phdr = GL(dl_phdr);
+ size_t phnum = GL(dl_phnum);
+ for (ph = phdr; ph < &phdr[phnum]; ++ph)
+ switch (ph->p_type)
+ {
+ case PT_LOAD:
+ /* Skip the empty PT_LOAD segment at offset 0. */
+ if (ph->p_filesz != 0 && ph->p_offset == 0)
+ file_p_vaddr = ph->p_vaddr;
+ break;
+ case PT_DYNAMIC:
+ main_map->l_ld_readonly = (ph->p_flags & PF_W) == 0;
+ break;
+ default:
+ break;
+ }
+
+ /* Figure out the run-time load address of static PIE. */
+ ElfW(Addr) l_addr = elf_machine_load_address ();
+ main_map->l_addr = l_addr - file_p_vaddr;
+
+ /* Read our own dynamic section and fill in the info array. */
+ main_map->l_ld = ((void *) l_addr + elf_machine_dynamic ());
+
+ elf_get_dynamic_info (main_map, false, true);
+
+ _ELF_DYNAMIC_DO_RELOC (RELA, Rela, main_map, 0, 0, 0, _ELF_CHECK_REL);
+}
diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
index 8c7312ad98..dfb202157c 100644
--- a/sysdeps/riscv/dl-machine.h
+++ b/sysdeps/riscv/dl-machine.h
@@ -1,5 +1,5 @@
/* Machine-dependent ELF dynamic relocation inline functions. RISC-V version.
- Copyright (C) 2011-2026 Free Software Foundation, Inc.
+ Copyright (C) 2011-2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -26,7 +26,7 @@
#include <sys/asm.h>
#include <dl-tls.h>
#include <dl-irel.h>
-#include <dl-static-tls.h>
+#include <elf/dl-static-tls.h>
#include <dl-machine-rel.h>
#ifndef _RTLD_PROLOGUE
diff --git a/sysdeps/riscv/libc-start.c b/sysdeps/riscv/libc-start.c
new file mode 100644
index 0000000000..fb70d26bb6
--- /dev/null
+++ b/sysdeps/riscv/libc-start.c
@@ -0,0 +1,36 @@
+/* Override csu/libc-start.c on riscv.
+ Copyright (C) 2023-2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+# if ENABLE_STATIC_PIE
+#ifndef SHARED
+/* Mark symbols hidden in static PIE for early self relocation to work. */
+# if BUILD_PIE_DEFAULT
+# pragma GCC visibility push(hidden)
+# endif
+/* Define I386_USE_SYSENTER to support syscall during startup in static
+ PIE. */
+# include <startup.h>
+# include <ldsodefs.h>
+# include <cpu-features.c>
+
+
+# define ARCH_INIT_CPU_FEATURES() init_cpu_features()
+
+#endif /* !SHARED */
+#endif
+#include <csu/libc-start.c>
--
2.25.1
More information about the Libc-alpha
mailing list