RISC-V: Use the right PLT address when making a new entry
Checks
Commit Message
When building static executable for IFUNC we should fill .igot.plt
instead of .got.plt.
.got.plt might be NULL and fail following tests with segmentation fault:
FAIL: ifunc-reloc-call-01 (rv32-exe)
FAIL: ifunc-reloc-call-02 (rv32-exe)
FAIL: ifunc-reloc-pcrel (rv32-exe)
FAIL: ifunc-reloc-pcrel (rv64-exe)
Since we already choose which plt setion to be used earlier:
if (htab->elf.splt != NULL)
{
plt = htab->elf.splt;
gotplt = htab->elf.sgotplt;
relplt = htab->elf.srelplt;
}
else
{
plt = htab->elf.iplt;
gotplt = htab->elf.igotplt;
relplt = htab->elf.irelplt;
}
use the variable `gotplt` to fill the plt entry.
More detail:
riscv_make_plt_entry() has a parameter `got` that is used for compact
plt, but not used in normal mode.
With older GCC, the address is random, but since `got` is not used, the
random address doesn't matter.
Starting from GCC 12, GCC will initialize stack variables to 0, which
triggers segmentation fault when calling riscv_make_plt_entry()
immediately.
---
bfd/elfnn-riscv.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
Sorry I accidentally sent a patch for fixing an internal bug. Please ignore this mail ...
> Hau Hsu <hau.hsu@sifive.com> 於 2023年9月4日 下午2:12 寫道:
>
> When building static executable for IFUNC we should fill .igot.plt
> instead of .got.plt.
>
> .got.plt might be NULL and fail following tests with segmentation fault:
> FAIL: ifunc-reloc-call-01 (rv32-exe)
> FAIL: ifunc-reloc-call-02 (rv32-exe)
> FAIL: ifunc-reloc-pcrel (rv32-exe)
> FAIL: ifunc-reloc-pcrel (rv64-exe)
>
> Since we already choose which plt setion to be used earlier:
>
> if (htab->elf.splt != NULL)
> {
> plt = htab->elf.splt;
> gotplt = htab->elf.sgotplt;
> relplt = htab->elf.srelplt;
> }
> else
> {
> plt = htab->elf.iplt;
> gotplt = htab->elf.igotplt;
> relplt = htab->elf.irelplt;
> }
>
> use the variable `gotplt` to fill the plt entry.
>
> More detail:
> riscv_make_plt_entry() has a parameter `got` that is used for compact
> plt, but not used in normal mode.
>
> With older GCC, the address is random, but since `got` is not used, the
> random address doesn't matter.
>
> Starting from GCC 12, GCC will initialize stack variables to 0, which
> triggers segmentation fault when calling riscv_make_plt_entry()
> immediately.
>
> ---
> bfd/elfnn-riscv.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
> index 3d228e95375..d90e598ec83 100644
> --- a/bfd/elfnn-riscv.c
> +++ b/bfd/elfnn-riscv.c
> @@ -3549,7 +3549,7 @@ riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
>
> /* Fill in the PLT entry itself. */
> if (! riscv_make_plt_entry (output_bfd,
> - sec_addr (htab->elf.sgotplt), got_address,
> + sec_addr (gotplt), got_address,
> header_address, h->plt.offset,
> plt_entry))
> return false;
> --
> 2.40.1
>
@@ -3549,7 +3549,7 @@ riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
/* Fill in the PLT entry itself. */
if (! riscv_make_plt_entry (output_bfd,
- sec_addr (htab->elf.sgotplt), got_address,
+ sec_addr (gotplt), got_address,
header_address, h->plt.offset,
plt_entry))
return false;