[PATCH v2] RISC-V: Fix SIGSEGV of --static-pie binaries on riscv64 [BZ #33911]
Jeffrey Law
jeffrey.law@oss.qualcomm.com
Fri Mar 13 19:10:16 GMT 2026
On 3/6/2026 9:42 AM, Adhemerval Zanella Netto wrote:
>
> On 06/03/26 12:10, daichengrong wrote:
>> This patch fixes a SIGSEGV observed in --static-pie binaries on riscv64,
>> as reported in BZ #33911. The root cause is a recent memset optimization
>> that introduced incorrect behavior when resolving symbols for static PIE
>> executables.
>>
>> Problem:
>> --------
>> In static PIE binaries, an early call to ifunc memset could use GOT entries
>> that were not yet properly initialized. This results in jumping to
>> invalid addresses, causing SIGSEGV.
> This is exactly what <dl-symbol-redir-ifunc.h> is for.
Right.
>
>> Solution:
>> ---------
>> This patch ensures that GOT entries for static PIE binaries are
>> correctly set up before ifunc memset calls occur.
> Sorry, but this kind of code duplication for arch-specific workarounds are not
> acceptable. And this issue is not RISCV specific, other ABIs might implement
> memset through ifunc and being subject to the very issue if the compiler starts
> to optimize more construction to libcalls.
>
> One solution could to implement ELF_MACHINE_BEFORE_RTLD_RELOC for RISCV, or
> restructure the generic elf/dl-reloc-static-pie.c to add another arch-specific
> hook. Worse scenario, we just build the TU with -ftree-loop-distribute-patterns
> to avoid any libcalls (and I would prefer to avoid it because this is a gcc
> specific fix).
>
> In any case, I think we can just fix it with the following patch. At least on
> qemu I don't see any more SEGFAULTs with -Os.
>
> diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c
> index bdff2b5ee2..63ce609024 100644
> --- a/elf/dl-reloc-static-pie.c
> +++ b/elf/dl-reloc-static-pie.c
> @@ -25,6 +25,7 @@
>
> #include <dl-machine.h>
> #include <dl-debug.h>
> +#include <dl-symbol-redir-ifunc.h>
>
> #define RESOLVE_MAP(map, scope, sym, version, flags) map
> #include "dynamic-link.h"
> diff --git a/sysdeps/riscv/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/riscv/multiarch/dl-symbol-redir-ifunc.h
> index 24b78711e3..69a0790838 100644
> --- a/sysdeps/riscv/multiarch/dl-symbol-redir-ifunc.h
> +++ b/sysdeps/riscv/multiarch/dl-symbol-redir-ifunc.h
> @@ -21,6 +21,7 @@
>
> #ifndef SHARED
> asm ("memset = __memset_generic");
> +asm ("memcpy = __memcpy_generic");
> #endif
I'm pretty sure I've got this in my local tree as well.
I'll spin your full patch up momentarily on my BPI for testing. It
generally looks correct to me.
jeff
More information about the Libc-alpha
mailing list