[PATCH] x86_64: Simplify elf_machine_{load_address,dynamic}

H.J. Lu hjl.tools@gmail.com
Tue Aug 17 12:14:39 GMT 2021


On Mon, Aug 16, 2021 at 11:24 PM Fangrui Song via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> and drop reliance on _GLOBAL_OFFSET_TABLE_[0].
>
> &__ehdr_start is a better way to get the load address.
> ---
>  sysdeps/x86_64/dl-machine.h | 25 +++++++++----------------
>  1 file changed, 9 insertions(+), 16 deletions(-)
>
> diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h
> index a8596aa3fa..ceee50734e 100644
> --- a/sysdeps/x86_64/dl-machine.h
> +++ b/sysdeps/x86_64/dl-machine.h
> @@ -35,27 +35,20 @@ elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
>  }
>
>
> -/* Return the link-time address of _DYNAMIC.  Conveniently, this is the
> -   first element of the GOT.  This must be inlined in a function which
> -   uses global data.  */
> -static inline ElfW(Addr) __attribute__ ((unused))
> -elf_machine_dynamic (void)
> -{
> -  /* This produces an IP-relative reloc which is resolved at link time. */
> -  extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
> -  return _GLOBAL_OFFSET_TABLE_[0];
> -}
> -
> -
>  /* Return the run-time load address of the shared object.  */
>  static inline ElfW(Addr) __attribute__ ((unused))
>  elf_machine_load_address (void)
>  {
> -  /* Compute the difference between the runtime address of _DYNAMIC as seen
> -     by an IP-relative reference, and the link-time address found in the
> -     special unrelocated first GOT entry.  */
> +  extern const ElfW(Ehdr) __ehdr_start attribute_hidden;
> +  return (ElfW(Addr)) &__ehdr_start;
> +}
> +
> +/* Return the link-time address of _DYNAMIC.  */
> +static inline ElfW(Addr) __attribute__ ((unused))
> +elf_machine_dynamic (void)
> +{
>    extern ElfW(Dyn) _DYNAMIC[] attribute_hidden;
> -  return (ElfW(Addr)) &_DYNAMIC - elf_machine_dynamic ();
> +  return (ElfW(Addr)) _DYNAMIC - elf_machine_load_address ();
>  }
>
>  /* Set up the loaded object described by L so its unrelocated PLT
> --
> 2.33.0.rc1.237.g0d66db33f3-goog
>

Please provide comparison of ld.so with and without the change, in
terms of code size, code sequence as well as dynamic relocation.

Thanks.

-- 
H.J.


More information about the Libc-alpha mailing list