diff mbox series

[v2] i386: Port elf_machine_{load_address,dynamic} from x86-64

Message ID 20210924160948.4123839-1-maskray@google.com
State Committed
Headers show
Series [v2] i386: Port elf_machine_{load_address,dynamic} from x86-64 | expand

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Fāng-ruì Sòng Sept. 24, 2021, 4:09 p.m. UTC
This drops reliance on _GLOBAL_OFFSET_TABLE_[0] being the link-time
address of _DYNAMIC.

The code sequence length does not change.
---
Changes from v1:
* Change ElfW(xxx) to Elf32_xxx
---
 sysdeps/i386/dl-machine.h | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)

Comments

H.J. Lu Sept. 24, 2021, 4:12 p.m. UTC | #1
On Fri, Sep 24, 2021 at 9:10 AM Fangrui Song <maskray@google.com> wrote:
>
> This drops reliance on _GLOBAL_OFFSET_TABLE_[0] being the link-time
> address of _DYNAMIC.
>
> The code sequence length does not change.
> ---
> Changes from v1:
> * Change ElfW(xxx) to Elf32_xxx
> ---
>  sysdeps/i386/dl-machine.h | 27 ++++++++++-----------------
>  1 file changed, 10 insertions(+), 17 deletions(-)
>
> diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
> index 590b41d8d7..0401888508 100644
> --- a/sysdeps/i386/dl-machine.h
> +++ b/sysdeps/i386/dl-machine.h
> @@ -34,27 +34,20 @@ elf_machine_matches_host (const Elf32_Ehdr *ehdr)
>  }
>
>
> -/* Return the link-time address of _DYNAMIC.  Conveniently, this is the
> -   first element of the GOT, a special entry that is never relocated.  */
> -static inline Elf32_Addr __attribute__ ((unused, const))
> -elf_machine_dynamic (void)
> -{
> -  /* This produces a GOTOFF reloc that resolves to zero at link time, so in
> -     fact just loads from the GOT register directly.  By doing it without
> -     an asm we can let the compiler choose any register.  */
> -  extern const Elf32_Addr _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
> -  return _GLOBAL_OFFSET_TABLE_[0];
> -}
> -
>  /* Return the run-time load address of the shared object.  */
>  static inline Elf32_Addr __attribute__ ((unused))
>  elf_machine_load_address (void)
>  {
> -  /* Compute the difference between the runtime address of _DYNAMIC as seen
> -     by a GOTOFF reference, and the link-time address found in the special
> -     unrelocated first GOT entry.  */
> -  extern Elf32_Dyn bygotoff[] asm ("_DYNAMIC") attribute_hidden;
> -  return (Elf32_Addr) &bygotoff - elf_machine_dynamic ();
> +  extern const Elf32_Ehdr __ehdr_start attribute_hidden;
> +  return (Elf32_Addr) &__ehdr_start;
> +}
> +
> +/* Return the link-time address of _DYNAMIC.  */
> +static inline Elf32_Addr __attribute__ ((unused))
> +elf_machine_dynamic (void)
> +{
> +  extern Elf32_Dyn _DYNAMIC[] attribute_hidden;
> +  return (Elf32_Addr) _DYNAMIC - elf_machine_load_address ();
>  }
>
>  /* Set up the loaded object described by L so its unrelocated PLT
> --
> 2.33.0.685.g46640cef36-goog
>

LGTM.

Reviewed-by: H.J. Lu <hjl.tools@gmail.com>

Thanks.
diff mbox series

Patch

diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
index 590b41d8d7..0401888508 100644
--- a/sysdeps/i386/dl-machine.h
+++ b/sysdeps/i386/dl-machine.h
@@ -34,27 +34,20 @@  elf_machine_matches_host (const Elf32_Ehdr *ehdr)
 }
 
 
-/* Return the link-time address of _DYNAMIC.  Conveniently, this is the
-   first element of the GOT, a special entry that is never relocated.  */
-static inline Elf32_Addr __attribute__ ((unused, const))
-elf_machine_dynamic (void)
-{
-  /* This produces a GOTOFF reloc that resolves to zero at link time, so in
-     fact just loads from the GOT register directly.  By doing it without
-     an asm we can let the compiler choose any register.  */
-  extern const Elf32_Addr _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
-  return _GLOBAL_OFFSET_TABLE_[0];
-}
-
 /* Return the run-time load address of the shared object.  */
 static inline Elf32_Addr __attribute__ ((unused))
 elf_machine_load_address (void)
 {
-  /* Compute the difference between the runtime address of _DYNAMIC as seen
-     by a GOTOFF reference, and the link-time address found in the special
-     unrelocated first GOT entry.  */
-  extern Elf32_Dyn bygotoff[] asm ("_DYNAMIC") attribute_hidden;
-  return (Elf32_Addr) &bygotoff - elf_machine_dynamic ();
+  extern const Elf32_Ehdr __ehdr_start attribute_hidden;
+  return (Elf32_Addr) &__ehdr_start;
+}
+
+/* Return the link-time address of _DYNAMIC.  */
+static inline Elf32_Addr __attribute__ ((unused))
+elf_machine_dynamic (void)
+{
+  extern Elf32_Dyn _DYNAMIC[] attribute_hidden;
+  return (Elf32_Addr) _DYNAMIC - elf_machine_load_address ();
 }
 
 /* Set up the loaded object described by L so its unrelocated PLT