public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] RISC-V: Ouput __global_pointer$ as dynamic symbol when generating dynamic PDE.
@ 2020-12-16  3:03 Nelson Chu
  2020-12-16  3:07 ` Nelson Chu
  2021-01-04 23:13 ` Jim Wilson
  0 siblings, 2 replies; 4+ messages in thread
From: Nelson Chu @ 2020-12-16  3:03 UTC (permalink / raw)
  To: binutils, jimw, andrew, palmer; +Cc: kito.cheng, vincent.chen, nelson.chu

When the ifunc resolver is in the executable, we may relax the variables
to gp-relative access instruction in the ifunc resolver, or in other functions
that called by the ifunc resolver.  But this will cause the uninitialized
gp problem since the ifunc need to be resolved at the early runtime, that
is at the pre-load stage, but we set the gp until the startup code.

At first, we try to add a new dynamic tag, DT_RISCV_GP, to stroe the gp value
and let ld.so can init the gp register early, before the pre-load stage.  But
we need to extend the ABI if we want to add a new dynamic tag.  Therefore,
in the psabi discussion, we try another solution, which was suggested by the
lld and FreeBSD linker experts, to let ld.so set the gp earlier - make sure
__global_pointer$ is output as a dynamic symbol when we are generating pde,
since we only do the relaxation for it.  Afterwards, ld.so can search the
DT_SYMTAB to get the gp value, and set the gp register before resolving ifunc.

bfd/
    * elfnn-riscv.c (allocate_dynrelocs): When we are generating pde, make
      sure gp symbol is output as a dynamic symbol.
---
 bfd/elfnn-riscv.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 20944c8..599313a 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -1083,6 +1083,15 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   htab = riscv_elf_hash_table (info);
   BFD_ASSERT (htab != NULL);
 
+  /* When we are generating pde, make sure gp symbol is output as a
+     dynamic symbol.  Then ld.so can set the gp register earlier, before
+     resolving the ifunc.  */
+  if (!bfd_link_pic (info)
+      && htab->elf.dynamic_sections_created
+      && strcmp (h->root.root.string, RISCV_GP_SYMBOL) == 0
+      && !bfd_elf_link_record_dynamic_symbol (info, h))
+    return FALSE;
+
   /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
      in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
      if they are defined and referenced in a non-shared object.  */
-- 
2.7.4


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

* Re: [PATCH] RISC-V: Ouput __global_pointer$ as dynamic symbol when generating dynamic PDE.
  2020-12-16  3:03 [PATCH] RISC-V: Ouput __global_pointer$ as dynamic symbol when generating dynamic PDE Nelson Chu
@ 2020-12-16  3:07 ` Nelson Chu
  2021-01-04 23:13 ` Jim Wilson
  1 sibling, 0 replies; 4+ messages in thread
From: Nelson Chu @ 2020-12-16  3:07 UTC (permalink / raw)
  To: Binutils, Jim Wilson, Andrew Waterman, Palmer Dabbelt
  Cc: Kito Cheng, Vincent Chen

Hi Guys,

Here is the first approach in glibc - add DT_RISCV_GP,
https://sourceware.org/pipermail/libc-alpha/2020-December/120238.html

And the following is the last approach in glibc - search FT_SYMTAB to
get gp value
https://sourceware.org/pipermail/libc-alpha/2020-December/120715.html


Thanks
Nelson

On Wed, Dec 16, 2020 at 11:03 AM Nelson Chu <nelson.chu@sifive.com> wrote:
>
> When the ifunc resolver is in the executable, we may relax the variables
> to gp-relative access instruction in the ifunc resolver, or in other functions
> that called by the ifunc resolver.  But this will cause the uninitialized
> gp problem since the ifunc need to be resolved at the early runtime, that
> is at the pre-load stage, but we set the gp until the startup code.
>
> At first, we try to add a new dynamic tag, DT_RISCV_GP, to stroe the gp value
> and let ld.so can init the gp register early, before the pre-load stage.  But
> we need to extend the ABI if we want to add a new dynamic tag.  Therefore,
> in the psabi discussion, we try another solution, which was suggested by the
> lld and FreeBSD linker experts, to let ld.so set the gp earlier - make sure
> __global_pointer$ is output as a dynamic symbol when we are generating pde,
> since we only do the relaxation for it.  Afterwards, ld.so can search the
> DT_SYMTAB to get the gp value, and set the gp register before resolving ifunc.
>
> bfd/
>     * elfnn-riscv.c (allocate_dynrelocs): When we are generating pde, make
>       sure gp symbol is output as a dynamic symbol.
> ---
>  bfd/elfnn-riscv.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
> index 20944c8..599313a 100644
> --- a/bfd/elfnn-riscv.c
> +++ b/bfd/elfnn-riscv.c
> @@ -1083,6 +1083,15 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
>    htab = riscv_elf_hash_table (info);
>    BFD_ASSERT (htab != NULL);
>
> +  /* When we are generating pde, make sure gp symbol is output as a
> +     dynamic symbol.  Then ld.so can set the gp register earlier, before
> +     resolving the ifunc.  */
> +  if (!bfd_link_pic (info)
> +      && htab->elf.dynamic_sections_created
> +      && strcmp (h->root.root.string, RISCV_GP_SYMBOL) == 0
> +      && !bfd_elf_link_record_dynamic_symbol (info, h))
> +    return FALSE;
> +
>    /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
>       in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
>       if they are defined and referenced in a non-shared object.  */
> --
> 2.7.4
>

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

* Re: [PATCH] RISC-V: Ouput __global_pointer$ as dynamic symbol when generating dynamic PDE.
  2020-12-16  3:03 [PATCH] RISC-V: Ouput __global_pointer$ as dynamic symbol when generating dynamic PDE Nelson Chu
  2020-12-16  3:07 ` Nelson Chu
@ 2021-01-04 23:13 ` Jim Wilson
  2021-01-05  2:28   ` Nelson Chu
  1 sibling, 1 reply; 4+ messages in thread
From: Jim Wilson @ 2021-01-04 23:13 UTC (permalink / raw)
  To: Nelson Chu
  Cc: Binutils, Andrew Waterman, Palmer Dabbelt, Kito Cheng, Vincent Chen

On Tue, Dec 15, 2020 at 7:03 PM Nelson Chu <nelson.chu@sifive.com> wrote:

> bfd/
>     * elfnn-riscv.c (allocate_dynrelocs): When we are generating pde, make
>       sure gp symbol is output as a dynamic symbol.
>

This looks OK to me and is OK with the ABI committee.  And we do need it
for the glibc ifunc support to work right, so lets add this before the
release branch is made.

Jim

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

* Re: [PATCH] RISC-V: Ouput __global_pointer$ as dynamic symbol when generating dynamic PDE.
  2021-01-04 23:13 ` Jim Wilson
@ 2021-01-05  2:28   ` Nelson Chu
  0 siblings, 0 replies; 4+ messages in thread
From: Nelson Chu @ 2021-01-05  2:28 UTC (permalink / raw)
  To: Jim Wilson
  Cc: Binutils, Andrew Waterman, Palmer Dabbelt, Kito Cheng, Vincent Chen

On Tue, Jan 5, 2021 at 7:13 AM Jim Wilson <jimw@sifive.com> wrote:
> On Tue, Dec 15, 2020 at 7:03 PM Nelson Chu <nelson.chu@sifive.com> wrote:
>> bfd/
>>     * elfnn-riscv.c (allocate_dynrelocs): When we are generating pde, make
>>       sure gp symbol is output as a dynamic symbol.
>
> This looks OK to me and is OK with the ABI committee.  And we do need it for the glibc ifunc support to work right, so lets add this before the release branch is made.

Yeah we do need this for ifunc glibc patches.  Committed, thanks.

Nelson

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

end of thread, other threads:[~2021-01-05  2:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-16  3:03 [PATCH] RISC-V: Ouput __global_pointer$ as dynamic symbol when generating dynamic PDE Nelson Chu
2020-12-16  3:07 ` Nelson Chu
2021-01-04 23:13 ` Jim Wilson
2021-01-05  2:28   ` Nelson Chu

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).