https://sourceware.org/bugzilla/show_bug.cgi?id=35082
Bug ID: 35082
Summary: ld riscv: segfault / BFD assertion fail in
riscv_elf_relocate_section after bad_static_reloc in
check_relocs
Product: binutils
Version: 2.48 (HEAD)
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: ld
Assignee: unassigned at sourceware dot org
Reporter: h.dwwwwww at gmail dot com
Target Milestone: ---
When linking a non-PIC / PIE object containing an invalid static relocation
(such as R_RISCV_TPREL_HI20) into a shared library (`-shared`), GNU ld on
RISC-V crashes with SIGSEGV (or hits a BFD assertion failure) instead of
exiting cleanly with an error.
Environment
-----------
- Target: riscv64-unknown-linux-gnu
- Tested on: GNU ld (GNU Binutils) 2.47
- Also verified by code inspection on master (commit
a113774764f69be076eff25884d161f35eb729fd).
Minimal Reproducer 1: SIGSEGV (NULL sreloc dereference)
-------------------------------------------------------
$ cat repro_segv.s
.section .tbss,"awT",@nobits
x:
.zero 4
.text
.globl foo
foo:
lui a0, %tprel_hi(x)
ret
.section .data.rel.ro,"aw"
.globl fp
fp:
.dword ext_fn
$ as -march=rv64gc -o repro_segv.o repro_segv.s
$ ld -shared -o repro_segv.so repro_segv.o
ld: repro_segv.o: relocation R_RISCV_TPREL_HI20 against `a local symbol' can
not be used when making a shared object; recompile with -fPIC
Segmentation fault (core dumped)
(Equivalent 4-line C reproducer with GCC 16:
static __thread int x = 0;
extern void ext_fn(void);
void (*const fp)(void) = ext_fn;
int foo(void) { ext_fn(); return ++x; }
compiled with `gcc -O2 -fPIE -c repro.c && gcc -shared -o repro.so repro.o`
produces:
/usr/bin/ld: repro.o: relocation R_RISCV_TPREL_HI20 against `a local symbol'
can not be used when making a shared object; recompile with -fPIC
collect2: fatal error: ld terminated with signal 11 [Segmentation fault],
core dumped
On x86_64, GNU ld exits cleanly with status 1 instead of crashing.)
Minimal Reproducer 2: BFD assertion fail (unallocated GOT slot)
---------------------------------------------------------------
$ cat repro_got.s
.section .tbss,"awT",@nobits
x:
.zero 4
.section .text.f1,"ax"
.globl f1
f1:
lui a0, %tprel_hi(x)
ret
.section .text.f2,"ax"
.globl f2
f2:
.L1:
auipc a0, %got_pcrel_hi(ext_var)
ld a0, %pcrel_lo(.L1)(a0)
ret
$ as -march=rv64gc -o repro_got.o repro_got.s
$ ld -shared -o repro_got.so repro_got.o
ld: repro_got.o: relocation R_RISCV_TPREL_HI20 against `a local symbol' can not
be used when making a shared object; recompile with -fPIC
ld: BFD (GNU Binutils) 2.47 assertion fail bfd/elfnn-riscv.c:2860
Minimal Reproducer 3: Spurious R_RISCV_CALL_PLT error
-----------------------------------------------------
$ cat repro_plt.s
.section .tbss,"awT",@nobits
x:
.zero 4
.text
.globl foo
foo:
lui a0, %tprel_hi(x)
call ext_fn@plt
ret
$ as -march=rv64gc -o repro_plt.o repro_plt.s
$ ld -shared -o repro_plt.so repro_plt.o
ld: repro_plt.o: relocation R_RISCV_TPREL_HI20 against `a local symbol' can not
be used when making a shared object; recompile with -fPIC
ld: relocation R_RISCV_CALL_PLT against `ext_fn' which may bind externally can
not be used when making a shared object; recompile with -fPIC
(When linked against a shared library defining the symbol, e.g. libc, this
instead prints a spurious:
unresolvable R_RISCV_CALL_PLT relocation against symbol
`_setjmp@@GLIBC_2.27')
Root Cause Analysis
-------------------
1. In `riscv_elf_check_relocs` (`bfd/elfnn-riscv.c`), encountering an illegal
static relocation such as `R_RISCV_TPREL_HI20` in a shared link immediately
returns `bad_static_reloc (...)` (`false`) (master line 1644; 2.47 line 1137).
2. `_bfd_elf_link_iterate_on_relocs` (`bfd/elflink.c:4372`) sees `!ok` and
aborts scanning the remaining relocations and remaining sections of that input
BFD.
3. `lang_check_relocs` (`ld/ldlang.c:8497`) sets `config.make_executable =
false` and intentionally continues linking to report further errors, eventually
invoking `riscv_elf_relocate_section` on sections of the failed BFD that were
never checked by `riscv_elf_check_relocs`.
4. As a result, in `riscv_elf_relocate_section`:
- For `R_RISCV_64` (and `R_RISCV_32` on RV32) at
`bfd/elfnn-riscv.c:4400-4401` (2.47 line 3205):
sreloc = elf_section_data (input_section)->sreloc;
riscv_elf_append_rela (info->output_bfd, sreloc, &outrel);
`sreloc` is NULL, so `riscv_elf_append_rela` dereferences
`sreloc->reloc_count` (offset 0x70) and crashes with SIGSEGV.
By comparison, both `bfd/elf64-x86-64.c:4139` and
`bfd/elfnn-aarch64.c:6116` explicitly check:
if (sreloc == NULL || sreloc->contents == NULL)
- For `R_RISCV_GOT_HI20` at `bfd/elfnn-riscv.c:3988` (2.47 line 2860),
`h->got.offset == (bfd_vma) -1` triggers `BFD_ASSERT (off != (bfd_vma) -1)`
(and similarly `local_got_offsets == NULL` at line 4024).
- For `R_RISCV_CALL_PLT` later in the same section/object, `h->plt.refcount`
was never incremented, producing misleading `unresolvable R_RISCV_CALL_PLT` /
`recompile with -fPIC` diagnostics for valid PLT calls.
Expected Behavior
-----------------
`ld` should report the invalid static relocation error(s) and exit cleanly with
a non-zero exit status, without segfaulting or triggering BFD assertion
failures.
Possible Fix Directions
-----------------------
1. In `riscv_elf_relocate_section`, guard `sreloc == NULL || sreloc->contents
== NULL` before calling `riscv_elf_append_rela` (matching `elf64-x86-64.c` and
`elfnn-aarch64.c`), and avoid crashing/asserting when GOT/TLS structures were
not allocated due to an earlier `check_relocs` failure.
2. Additionally, either mark `check_relocs_failed` (as in `elf64-x86-64.c`) or
avoid returning early on `bad_static_reloc` inside `riscv_elf_check_relocs` so
the remaining relocations/sections are handled consistently without cascading
assertions or bogus `R_RISCV_CALL_PLT` errors.
--
You are receiving this mail because:
You are on the CC list for the bug.