public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] csu: Skip ARCH_SETUP_IREL if _dl_relocate_static_pie applied IRELATIVE relocations [BZ #27164]
@ 2021-07-08 22:10 Fangrui Song
  2021-07-08 23:27 ` H.J. Lu
  2021-07-12 14:08 ` Carlos O'Donell
  0 siblings, 2 replies; 13+ messages in thread
From: Fangrui Song @ 2021-07-08 22:10 UTC (permalink / raw)
  To: libc-alpha; +Cc: Carlos O'Donell, H.J. Lu, Siva Chandra Reddy, Fangrui Song

From: Siva Chandra Reddy <sivachandra@google.com>

For a static pie, _dl_relocate_static_pie applies IRELATIVE relocations
so ARCH_SETUP_IREL should not apply relocations again. The code
currently relies on ld -pie not defining
__rela_iplt_start/__rela_iplt_end (they end up as 0 as unresolved
undefined weak symbols).

However, LLD defines __rela_iplt_start/__rela_iplt_end regardless of
-no-pie or -pie, so in an LLD linked static pie, ARCH_SETUP_IREL would
re-apply the relocations in the range of [__rela_iplt_start,
__rela_iplt_end), causing a segfault.

Change _dl_relocate_static_pie to return an int, indicating whether the
relocations have been applied. This makes the intention clearer and
makes glibc buildable with LLD>=9.0 if we allow LLD at configure time.

In addition, this enables a future simplification to GNU ld: we can drop
a linker script difference between -no-pie and -pie.

Co-authored-by: Fangrui Song <maskray@google.com>
---
 csu/libc-start.c           | 8 +++++---
 csu/static-reloc.c         | 3 ++-
 elf/dl-reloc-static-pie.c  | 4 +++-
 sysdeps/generic/ldsodefs.h | 7 ++++---
 4 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/csu/libc-start.c b/csu/libc-start.c
index 5b5913e7bf..32a69c58a2 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -296,10 +296,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   /* Do static pie self relocation after tunables and cpu features
      are setup for ifunc resolvers. Before this point relocations
      must be avoided.  */
-  _dl_relocate_static_pie ();
+  int relocs_applied = _dl_relocate_static_pie ();
 
   /* Perform IREL{,A} relocations.  */
-  ARCH_SETUP_IREL ();
+  if (!relocs_applied)
+    ARCH_SETUP_IREL ();
 
   /* The stack guard goes into the TCB, so initialize it early.  */
   ARCH_SETUP_TLS ();
@@ -307,7 +308,8 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   /* In some architectures, IREL{,A} relocations happen after TLS setup in
      order to let IFUNC resolvers benefit from TCB information, e.g. powerpc's
      hwcap and platform fields available in the TCB.  */
-  ARCH_APPLY_IREL ();
+  if (!relocs_applied)
+    ARCH_APPLY_IREL ();
 
   /* Set up the stack checker's canary.  */
   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
diff --git a/csu/static-reloc.c b/csu/static-reloc.c
index 972c524f28..9046d9f6a3 100644
--- a/csu/static-reloc.c
+++ b/csu/static-reloc.c
@@ -19,8 +19,9 @@
 #if ENABLE_STATIC_PIE
 #include <ldsodefs.h>
 
-void
+int
 _dl_relocate_static_pie (void)
 {
+  return 0;
 }
 #endif
diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c
index d5bd2f31e9..b707ef4bf1 100644
--- a/elf/dl-reloc-static-pie.c
+++ b/elf/dl-reloc-static-pie.c
@@ -25,7 +25,7 @@
 
 /* Relocate static executable with PIE.  */
 
-void
+int
 _dl_relocate_static_pie (void)
 {
   struct link_map *main_map = _dl_get_dl_main_map ();
@@ -66,5 +66,7 @@ _dl_relocate_static_pie (void)
        with the run-time address of the r_debug structure  */
     main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
 # endif
+
+  return 1;
 }
 #endif
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index 176394de4d..a3996808f3 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -1200,14 +1200,15 @@ void __tls_init_tp (void) attribute_hidden;
 void __libc_setup_tls (void);
 
 # if ENABLE_STATIC_PIE
-/* Relocate static executable with PIE.  */
-extern void _dl_relocate_static_pie (void) attribute_hidden;
+/* Relocate static executable with PIE.  Returns 1 if relocations have
+   been applied.  */
+extern int _dl_relocate_static_pie (void) attribute_hidden;
 
 /* Get a pointer to _dl_main_map.  */
 extern struct link_map * _dl_get_dl_main_map (void)
   __attribute__ ((visibility ("hidden")));
 # else
-#  define _dl_relocate_static_pie()
+#  define _dl_relocate_static_pie() 0
 # endif
 #endif
 
-- 
2.32.0.93.g670b81a890-goog


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

end of thread, other threads:[~2021-09-06  6:30 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-08 22:10 [PATCH] csu: Skip ARCH_SETUP_IREL if _dl_relocate_static_pie applied IRELATIVE relocations [BZ #27164] Fangrui Song
2021-07-08 23:27 ` H.J. Lu
2021-07-12 14:08 ` Carlos O'Donell
2021-07-13  8:06   ` Fangrui Song
2021-07-13  8:33     ` Siddhesh Poyarekar
2021-07-13 23:06       ` Fangrui Song
2021-07-13 23:20         ` H.J. Lu
2021-07-13 23:31           ` Fāng-ruì Sòng
2021-07-13 23:47             ` H.J. Lu
2021-07-13 23:57               ` Fāng-ruì Sòng
2021-07-14  1:17                 ` H.J. Lu
2021-07-14  2:15         ` Siddhesh Poyarekar
2021-09-06  6:30           ` Fāng-ruì Sòng

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