RISC-V: Fix the assert fail when linking discarded sections under -pie for got
Considering the following case,
% cat tmp.s
.option pic
.text
.global _start
_start:
nop
.section .discard.s, "ax"
la x1, _start
% cat tmp.ld
OUTPUT_ARCH(riscv)
ENTRY(_start)
SECTIONS
{
/DISCARD/ : { *(.discard.*) }
. = 0x10000;
.text : { *(.text) }
. = 0x20000;
.got : { *(.got) *(.got.plt)}
. = 0x30000;
.data : { *(.data) *(.data.*) }
}
% riscv64-unknown-linux-gnu-as tmp.s -o tmp.o
% riscv64-unknown-linux-gnu-ld -pie -Ttmp.ld tmp.o
riscv64-unknown-linux-gnu-ld: BFD (GNU Binutils) 2.44.50.20250624 assertion fail binutils-gdb/bfd/elfnn-riscv.c:3638
This happens when pie and the input sections, which refers to the global
symbol by got, are all discarded. Since referenced sections are all discarded,
we won't go into relocate_section for those sections, the got entry also won't
be initialized. Therefore, we will get assert fail when adding the RELATIVE
reloc in the finish_dynamic_symbol.
After seeing other target codes, there are two root causes as follows,
1. risc-v may call bfd_elf_link_record_dynamic_symbol in the allocate_dynrelocs
for not only undefweak symbols.
2. risc-v is missing the code to add RELATIVE to R_RISCV_GOT entries in the
relocate_section if a symbol is not dynamic and is not undefined weak under
pic and pie.
If we call bfd_elf_link_record_dynamic_symbol, then the global symbol will be
forced to dynamic, so the h->dynindx will forced to be a number rather than -1,
even it should be -1. Once h->dynindx != -1 and pic/pie, it will go into
finish_dynamic_symbol and insert RELATIVE/64 relocs for the got entry; For the
above case there are two issues,
1. The global symbol _start is forced to be dynamic in the allocate_dynrelocs.
when pie and all the referenced section are discarded, it won't go into
relocate_section to initialize the got entry, so it will cause assert fail
when adding RELATIVE reloc in the finish_dynamic_symbol. The assert fail
represents another problem - if we don't initialize the got entry in the
relocate_section under pie, which means we don't need to go into the
finish_dynamic_symbol and don't need a RELATIVE reloc for the got entry,
it should be NONE reloc.
2. Without linking any discarded section, it originally forces every RELATIVE
relocs added for every got by the finish_dynamic_symbol. Even The final
result looks correct under pie (genearte a RELATIVE reloc for got entry),
not sure if it may cause other problems for some special cases, excpet the
above one.
Therefore, this patch try to fix the above assert fail, and also clarify the
behavior of the allocate_dynrelocs which should only call bfd_elf_link_record_dynamic_symbol
for undefweak symbols, and add the missing code to generate RELATIVE reloc to
R_RISCV_GOT entries in the relocate_section if a symbol is not dynamic and is
not undefined weak under pic and pie.
Passed the gcc/binutils regressions of riscv-gnu-toolchain at least.
7 files changed