public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] RISC-V: Use the right PLT address when making a new entry
@ 2023-09-04  6:12 Hau Hsu
  2023-09-04  7:02 ` Hau Hsu
  0 siblings, 1 reply; 2+ messages in thread
From: Hau Hsu @ 2023-09-04  6:12 UTC (permalink / raw)
  To: hau.hsu, binutils, kito.cheng

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] RISC-V: Use the right PLT address when making a new entry
  2023-09-04  6:12 [PATCH] RISC-V: Use the right PLT address when making a new entry Hau Hsu
@ 2023-09-04  7:02 ` Hau Hsu
  0 siblings, 0 replies; 2+ messages in thread
From: Hau Hsu @ 2023-09-04  7:02 UTC (permalink / raw)
  To: Hau Hsu, binutils, kito.cheng

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
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-09-04  7:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-04  6:12 [PATCH] RISC-V: Use the right PLT address when making a new entry Hau Hsu
2023-09-04  7:02 ` Hau Hsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).