unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Fangrui Song via Libc-alpha <libc-alpha@sourceware.org>
To: libc-alpha@sourceware.org, Joseph Myers <joseph@codesourcery.com>
Subject: [PATCH] arm: Simplify elf_machine_{load_address,dynamic}
Date: Tue, 17 Aug 2021 22:55:56 -0700	[thread overview]
Message-ID: <20210818055556.2119205-1-maskray@google.com> (raw)

and drop reliance on _GLOBAL_OFFSET_TABLE_[0] being the link-time
address of _DYNAMIC. &__ehdr_start is a better way to get the load address.

This is similar to commits b37b75d269883a2c553bb7019a813094eb4e2dd1
(x86-64) and 43d06ed218fc8be58987bdfd00e21e5720f0b862 (aarch64).
---
 sysdeps/arm/dl-machine.h | 47 +++++++++-------------------------------
 1 file changed, 10 insertions(+), 37 deletions(-)

diff --git a/sysdeps/arm/dl-machine.h b/sysdeps/arm/dl-machine.h
index ff5e09e207..eb13cb8b57 100644
--- a/sysdeps/arm/dl-machine.h
+++ b/sysdeps/arm/dl-machine.h
@@ -37,48 +37,21 @@ elf_machine_matches_host (const Elf32_Ehdr *ehdr)
   return ehdr->e_machine == EM_ARM;
 }
 
-
-/* Return the link-time address of _DYNAMIC.  Conveniently, this is the
-   first element of the GOT.  */
-static inline Elf32_Addr __attribute__ ((unused))
-elf_machine_dynamic (void)
-{
-  /* Declaring this hidden ensures that a PC-relative reference is used.  */
-  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))
+static inline ElfW(Addr) __attribute__ ((unused))
 elf_machine_load_address (void)
 {
-  Elf32_Addr pcrel_addr;
-#ifdef SHARED
-  extern Elf32_Addr __dl_start (void *) asm ("_dl_start");
-  Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
-  asm ("adr %0, _dl_start" : "=r" (pcrel_addr));
-#else
-  extern Elf32_Addr __dl_relocate_static_pie (void *)
-    asm ("_dl_relocate_static_pie") attribute_hidden;
-  Elf32_Addr got_addr = (Elf32_Addr) &__dl_relocate_static_pie;
-  asm ("adr %0, _dl_relocate_static_pie" : "=r" (pcrel_addr));
-#endif
-#ifdef __thumb__
-  /* Clear the low bit of the function address.
-
-     NOTE: got_addr is from GOT table whose lsb is always set by linker if it's
-     Thumb function address.  PCREL_ADDR comes from PC-relative calculation
-     which will finish during assembling.  GAS assembler before the fix for
-     PR gas/21458 was not setting the lsb but does after that.  Always do the
-     strip for both, so the code works with various combinations of glibc and
-     Binutils.  */
-  got_addr &= ~(Elf32_Addr) 1;
-  pcrel_addr &= ~(Elf32_Addr) 1;
-#endif
-  return pcrel_addr - got_addr;
+  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_load_address ();
+}
 
 /* Set up the loaded object described by L so its unrelocated PLT
    entries will jump to the on-demand fixup code in dl-runtime.c.  */
-- 
2.33.0.rc2.250.ged5fa647cd-goog


             reply	other threads:[~2021-08-18  5:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-18  5:55 Fangrui Song via Libc-alpha [this message]
2021-08-18 17:32 ` [PATCH] arm: Simplify elf_machine_{load_address,dynamic} Joseph Myers
2021-08-30  8:11   ` Lukasz Majewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210818055556.2119205-1-maskray@google.com \
    --to=libc-alpha@sourceware.org \
    --cc=joseph@codesourcery.com \
    --cc=maskray@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).