RISC-V: Use the right PLT address when making a new entry

Message ID 20230904061257.17425-1-hau.hsu@sifive.com
State New
Headers
Series RISC-V: Use the right PLT address when making a new entry |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_check--master-arm fail Patch failed to apply

Commit Message

Hau Hsu Sept. 4, 2023, 6:12 a.m. UTC
  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

Hau Hsu Sept. 4, 2023, 7:02 a.m. UTC | #1
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
>
  

Patch

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;