public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/nsz/bug27072] csu: Move static pie self relocation later [BZ #27072]
@ 2021-01-11 10:48 Szabolcs Nagy
  0 siblings, 0 replies; 6+ messages in thread
From: Szabolcs Nagy @ 2021-01-11 10:48 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c63c32393ed1e781a2bae0ed6c7a11bfeed90f93

commit c63c32393ed1e781a2bae0ed6c7a11bfeed90f93
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Wed Jan 6 14:28:02 2021 +0000

    csu: Move static pie self relocation later [BZ #27072]
    
    IFUNC resolvers may depend on tunables and cpu feature setup so
    move static pie self relocation after those.
    
    It is hard to guarantee that the ealy startup code does not rely
    on relocations so this is a bit fragile. It would be more robust
    to handle RELATIVE relocs early and only IRELATIVE relocs later,
    but the current relocation processing code cannot do that.
    
    The early startup code before relocation processing includes
    
      _dl_aux_init (auxvec);
      __libc_init_secure ();
      __tunables_init (__environ);
      ARCH_INIT_CPU_FEATURES ();
    
    These are simple enough that RELATIVE relocs can be avoided.
    
    __ehdr_start may require RELATIVE relocation so it was moved
    later, fortunately ehdr and phdr are not used in the early code.

Diff:
---
 csu/libc-start.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/csu/libc-start.c b/csu/libc-start.c
index db859c3bed..fb64cdb2c9 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -142,8 +142,6 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   int result;
 
 #ifndef SHARED
-  _dl_relocate_static_pie ();
-
   char **ev = &argv[argc + 1];
 
   __environ = ev;
@@ -165,24 +163,7 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   }
 #  endif
   _dl_aux_init (auxvec);
-  if (GL(dl_phdr) == NULL)
 # endif
-    {
-      /* Starting from binutils-2.23, the linker will define the
-         magic symbol __ehdr_start to point to our own ELF header
-         if it is visible in a segment that also includes the phdrs.
-         So we can set up _dl_phdr and _dl_phnum even without any
-         information from auxv.  */
-
-      extern const ElfW(Ehdr) __ehdr_start
-	__attribute__ ((weak, visibility ("hidden")));
-      if (&__ehdr_start != NULL)
-        {
-          assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
-          GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
-          GL(dl_phnum) = __ehdr_start.e_phnum;
-        }
-    }
 
   /* Initialize very early so that tunables can use it.  */
   __libc_init_secure ();
@@ -191,6 +172,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
 
   ARCH_INIT_CPU_FEATURES ();
 
+  /* 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 ();
+
   /* Perform IREL{,A} relocations.  */
   ARCH_SETUP_IREL ();
 
@@ -202,6 +188,26 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
      hwcap and platform fields available in the TCB.  */
   ARCH_APPLY_IREL ();
 
+# ifdef HAVE_AUX_VECTOR
+  if (GL(dl_phdr) == NULL)
+# endif
+    {
+      /* Starting from binutils-2.23, the linker will define the
+         magic symbol __ehdr_start to point to our own ELF header
+         if it is visible in a segment that also includes the phdrs.
+         So we can set up _dl_phdr and _dl_phnum even without any
+         information from auxv.  */
+
+      extern const ElfW(Ehdr) __ehdr_start
+	__attribute__ ((weak, visibility ("hidden")));
+      if (&__ehdr_start != NULL)
+        {
+          assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
+          GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
+          GL(dl_phnum) = __ehdr_start.e_phnum;
+        }
+    }
+
   /* Set up the stack checker's canary.  */
   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
 # ifdef THREAD_SET_STACK_GUARD


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

* [glibc/nsz/bug27072] csu: Move static pie self relocation later [BZ #27072]
@ 2021-01-20 15:23 Szabolcs Nagy
  0 siblings, 0 replies; 6+ messages in thread
From: Szabolcs Nagy @ 2021-01-20 15:23 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=b5dbaa167f4317c312e22fb761036d3570e6614a

commit b5dbaa167f4317c312e22fb761036d3570e6614a
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Tue Jan 19 17:17:01 2021 +0000

    csu: Move static pie self relocation later [BZ #27072]
    
    IFUNC resolvers may depend on tunables and cpu feature setup so
    move static pie self relocation after those.
    
    It is hard to guarantee that the ealy startup code does not rely
    on relocations so this is a bit fragile. It would be more robust
    to handle RELATIVE relocs early and only IRELATIVE relocs later,
    but the current relocation processing code cannot do that.
    
    The early startup code up to relocation processing includes
    
      _dl_aux_init (auxvec);
      __libc_init_secure ();
      __tunables_init (__environ);
      ARCH_INIT_CPU_FEATURES ();
      _dl_relocate_static_pie ();
    
    These are simple enough that RELATIVE relocs can be avoided.
    
    The following steps include
    
      ARCH_SETUP_IREL ();
      ARCH_SETUP_TLS ();
      ARCH_APPLY_IREL ();
    
    On some targets IRELATIVE processing relies on TLS setup on
    others TLS setup relies on IRELATIVE relocs, so the right
    position for _dl_relocate_static_pie is target dependent.
    For now move self relocation as early as possible on targets
    that support static PIE.
    
    Fixes bug 27072.

Diff:
---
 csu/libc-start.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/csu/libc-start.c b/csu/libc-start.c
index a2f6e12728..feb0d7ce11 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -146,8 +146,6 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   int result;
 
 #ifndef SHARED
-  _dl_relocate_static_pie ();
-
   char **ev = &argv[argc + 1];
 
   __environ = ev;
@@ -199,6 +197,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
 
   ARCH_INIT_CPU_FEATURES ();
 
+  /* 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 ();
+
   /* Perform IREL{,A} relocations.  */
   ARCH_SETUP_IREL ();


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

* [glibc/nsz/bug27072] csu: Move static pie self relocation later [BZ #27072]
@ 2021-01-19 15:59 Szabolcs Nagy
  0 siblings, 0 replies; 6+ messages in thread
From: Szabolcs Nagy @ 2021-01-19 15:59 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=6b468d65b6c580acd82218b88b4150729c8921b2

commit 6b468d65b6c580acd82218b88b4150729c8921b2
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Wed Jan 6 14:28:02 2021 +0000

    csu: Move static pie self relocation later [BZ #27072]
    
    IFUNC resolvers may depend on tunables and cpu feature setup so
    move static pie self relocation after those.
    
    It is hard to guarantee that the ealy startup code does not rely
    on relocations so this is a bit fragile. It would be more robust
    to handle RELATIVE relocs early and only IRELATIVE relocs later,
    but the current relocation processing code cannot do that.
    
    The early startup code up to relocation processing includes
    
      _dl_aux_init (auxvec);
      __libc_init_secure ();
      __tunables_init (__environ);
      ARCH_INIT_CPU_FEATURES ();
      _dl_relocate_static_pie ();
    
    These are simple enough that RELATIVE relocs can be avoided.
    
    The following steps include
    
      ARCH_SETUP_IREL ();
      ARCH_SETUP_TLS ();
      ARCH_APPLY_IREL ();
    
    __ehdr_start may require RELATIVE relocation so it was moved
    after relocation processing but before tls setup which needs
    the program headers. This also means that targets that need
    ifunc resolution to be after tls setup cannot support static
    pie without splitting RELATIVE and IRELATIVE processing (or
    removing some of the dependencies between these steps).
    
    Fixes bug 27072.

Diff:
---
 csu/libc-start.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/csu/libc-start.c b/csu/libc-start.c
index 1e90dcb0a7..3239125202 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -146,8 +146,6 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   int result;
 
 #ifndef SHARED
-  _dl_relocate_static_pie ();
-
   char **ev = &argv[argc + 1];
 
   __environ = ev;
@@ -169,6 +167,24 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   }
 #  endif
   _dl_aux_init (auxvec);
+# endif
+
+  /* Initialize very early so that tunables can use it.  */
+  __libc_init_secure ();
+
+  __tunables_init (__environ);
+
+  ARCH_INIT_CPU_FEATURES ();
+
+  /* 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 ();
+
+  /* Perform IREL{,A} relocations.  */
+  ARCH_SETUP_IREL ();
+
+# ifdef HAVE_AUX_VECTOR
   if (GL(dl_phdr) == NULL)
 # endif
     {
@@ -188,16 +204,6 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
         }
     }
 
-  /* Initialize very early so that tunables can use it.  */
-  __libc_init_secure ();
-
-  __tunables_init (__environ);
-
-  ARCH_INIT_CPU_FEATURES ();
-
-  /* Perform IREL{,A} relocations.  */
-  ARCH_SETUP_IREL ();
-
   /* The stack guard goes into the TCB, so initialize it early.  */
   ARCH_SETUP_TLS ();


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

* [glibc/nsz/bug27072] csu: Move static pie self relocation later [BZ #27072]
@ 2021-01-18 16:15 Szabolcs Nagy
  0 siblings, 0 replies; 6+ messages in thread
From: Szabolcs Nagy @ 2021-01-18 16:15 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=4224b7c0428492696fe6d6c01739adcf69fc677d

commit 4224b7c0428492696fe6d6c01739adcf69fc677d
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Wed Jan 6 14:28:02 2021 +0000

    csu: Move static pie self relocation later [BZ #27072]
    
    IFUNC resolvers may depend on tunables and cpu feature setup so
    move static pie self relocation after those.
    
    It is hard to guarantee that the ealy startup code does not rely
    on relocations so this is a bit fragile. It would be more robust
    to handle RELATIVE relocs early and only IRELATIVE relocs later,
    but the current relocation processing code cannot do that.
    
    The early startup code before relocation processing includes
    
      _dl_aux_init (auxvec);
      __libc_init_secure ();
      __tunables_init (__environ);
      ARCH_INIT_CPU_FEATURES ();
    
    These are simple enough that RELATIVE relocs can be avoided.
    
    __ehdr_start may require RELATIVE relocation so it was moved
    later, fortunately ehdr and phdr are not used in the early code.
    
    Fixes bug 27072.

Diff:
---
 csu/libc-start.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/csu/libc-start.c b/csu/libc-start.c
index 1e90dcb0a7..c2b59431a3 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -146,8 +146,6 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   int result;
 
 #ifndef SHARED
-  _dl_relocate_static_pie ();
-
   char **ev = &argv[argc + 1];
 
   __environ = ev;
@@ -169,24 +167,7 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   }
 #  endif
   _dl_aux_init (auxvec);
-  if (GL(dl_phdr) == NULL)
 # endif
-    {
-      /* Starting from binutils-2.23, the linker will define the
-         magic symbol __ehdr_start to point to our own ELF header
-         if it is visible in a segment that also includes the phdrs.
-         So we can set up _dl_phdr and _dl_phnum even without any
-         information from auxv.  */
-
-      extern const ElfW(Ehdr) __ehdr_start
-	__attribute__ ((weak, visibility ("hidden")));
-      if (&__ehdr_start != NULL)
-        {
-          assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
-          GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
-          GL(dl_phnum) = __ehdr_start.e_phnum;
-        }
-    }
 
   /* Initialize very early so that tunables can use it.  */
   __libc_init_secure ();
@@ -195,6 +176,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
 
   ARCH_INIT_CPU_FEATURES ();
 
+  /* 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 ();
+
   /* Perform IREL{,A} relocations.  */
   ARCH_SETUP_IREL ();
 
@@ -206,6 +192,26 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
      hwcap and platform fields available in the TCB.  */
   ARCH_APPLY_IREL ();
 
+# ifdef HAVE_AUX_VECTOR
+  if (GL(dl_phdr) == NULL)
+# endif
+    {
+      /* Starting from binutils-2.23, the linker will define the
+         magic symbol __ehdr_start to point to our own ELF header
+         if it is visible in a segment that also includes the phdrs.
+         So we can set up _dl_phdr and _dl_phnum even without any
+         information from auxv.  */
+
+      extern const ElfW(Ehdr) __ehdr_start
+	__attribute__ ((weak, visibility ("hidden")));
+      if (&__ehdr_start != NULL)
+        {
+          assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
+          GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
+          GL(dl_phnum) = __ehdr_start.e_phnum;
+        }
+    }
+
   /* Set up the stack checker's canary.  */
   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
 # ifdef THREAD_SET_STACK_GUARD


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

* [glibc/nsz/bug27072] csu: Move static pie self relocation later [BZ #27072]
@ 2021-01-14 11:12 Szabolcs Nagy
  0 siblings, 0 replies; 6+ messages in thread
From: Szabolcs Nagy @ 2021-01-14 11:12 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=413ed92e774ac0b8056b5e7d72bf66b5eab03801

commit 413ed92e774ac0b8056b5e7d72bf66b5eab03801
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Wed Jan 6 14:28:02 2021 +0000

    csu: Move static pie self relocation later [BZ #27072]
    
    IFUNC resolvers may depend on tunables and cpu feature setup so
    move static pie self relocation after those.
    
    It is hard to guarantee that the ealy startup code does not rely
    on relocations so this is a bit fragile. It would be more robust
    to handle RELATIVE relocs early and only IRELATIVE relocs later,
    but the current relocation processing code cannot do that.
    
    The early startup code before relocation processing includes
    
      _dl_aux_init (auxvec);
      __libc_init_secure ();
      __tunables_init (__environ);
      ARCH_INIT_CPU_FEATURES ();
    
    These are simple enough that RELATIVE relocs can be avoided.
    
    __ehdr_start may require RELATIVE relocation so it was moved
    later, fortunately ehdr and phdr are not used in the early code.
    
    Fixes bug 27072.

Diff:
---
 csu/libc-start.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/csu/libc-start.c b/csu/libc-start.c
index db859c3bed..fb64cdb2c9 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -142,8 +142,6 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   int result;
 
 #ifndef SHARED
-  _dl_relocate_static_pie ();
-
   char **ev = &argv[argc + 1];
 
   __environ = ev;
@@ -165,24 +163,7 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   }
 #  endif
   _dl_aux_init (auxvec);
-  if (GL(dl_phdr) == NULL)
 # endif
-    {
-      /* Starting from binutils-2.23, the linker will define the
-         magic symbol __ehdr_start to point to our own ELF header
-         if it is visible in a segment that also includes the phdrs.
-         So we can set up _dl_phdr and _dl_phnum even without any
-         information from auxv.  */
-
-      extern const ElfW(Ehdr) __ehdr_start
-	__attribute__ ((weak, visibility ("hidden")));
-      if (&__ehdr_start != NULL)
-        {
-          assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
-          GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
-          GL(dl_phnum) = __ehdr_start.e_phnum;
-        }
-    }
 
   /* Initialize very early so that tunables can use it.  */
   __libc_init_secure ();
@@ -191,6 +172,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
 
   ARCH_INIT_CPU_FEATURES ();
 
+  /* 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 ();
+
   /* Perform IREL{,A} relocations.  */
   ARCH_SETUP_IREL ();
 
@@ -202,6 +188,26 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
      hwcap and platform fields available in the TCB.  */
   ARCH_APPLY_IREL ();
 
+# ifdef HAVE_AUX_VECTOR
+  if (GL(dl_phdr) == NULL)
+# endif
+    {
+      /* Starting from binutils-2.23, the linker will define the
+         magic symbol __ehdr_start to point to our own ELF header
+         if it is visible in a segment that also includes the phdrs.
+         So we can set up _dl_phdr and _dl_phnum even without any
+         information from auxv.  */
+
+      extern const ElfW(Ehdr) __ehdr_start
+	__attribute__ ((weak, visibility ("hidden")));
+      if (&__ehdr_start != NULL)
+        {
+          assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
+          GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
+          GL(dl_phnum) = __ehdr_start.e_phnum;
+        }
+    }
+
   /* Set up the stack checker's canary.  */
   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
 # ifdef THREAD_SET_STACK_GUARD


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

* [glibc/nsz/bug27072] csu: Move static pie self relocation later [BZ #27072]
@ 2021-01-12 17:18 Szabolcs Nagy
  0 siblings, 0 replies; 6+ messages in thread
From: Szabolcs Nagy @ 2021-01-12 17:18 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=fe1030f786182083e68dc24781b9cfbffd6b1986

commit fe1030f786182083e68dc24781b9cfbffd6b1986
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Wed Jan 6 14:28:02 2021 +0000

    csu: Move static pie self relocation later [BZ #27072]
    
    IFUNC resolvers may depend on tunables and cpu feature setup so
    move static pie self relocation after those.
    
    It is hard to guarantee that the ealy startup code does not rely
    on relocations so this is a bit fragile. It would be more robust
    to handle RELATIVE relocs early and only IRELATIVE relocs later,
    but the current relocation processing code cannot do that.
    
    The early startup code before relocation processing includes
    
      _dl_aux_init (auxvec);
      __libc_init_secure ();
      __tunables_init (__environ);
      ARCH_INIT_CPU_FEATURES ();
    
    These are simple enough that RELATIVE relocs can be avoided.
    
    __ehdr_start may require RELATIVE relocation so it was moved
    later, fortunately ehdr and phdr are not used in the early code.

Diff:
---
 csu/libc-start.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/csu/libc-start.c b/csu/libc-start.c
index db859c3bed..fb64cdb2c9 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -142,8 +142,6 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   int result;
 
 #ifndef SHARED
-  _dl_relocate_static_pie ();
-
   char **ev = &argv[argc + 1];
 
   __environ = ev;
@@ -165,24 +163,7 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   }
 #  endif
   _dl_aux_init (auxvec);
-  if (GL(dl_phdr) == NULL)
 # endif
-    {
-      /* Starting from binutils-2.23, the linker will define the
-         magic symbol __ehdr_start to point to our own ELF header
-         if it is visible in a segment that also includes the phdrs.
-         So we can set up _dl_phdr and _dl_phnum even without any
-         information from auxv.  */
-
-      extern const ElfW(Ehdr) __ehdr_start
-	__attribute__ ((weak, visibility ("hidden")));
-      if (&__ehdr_start != NULL)
-        {
-          assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
-          GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
-          GL(dl_phnum) = __ehdr_start.e_phnum;
-        }
-    }
 
   /* Initialize very early so that tunables can use it.  */
   __libc_init_secure ();
@@ -191,6 +172,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
 
   ARCH_INIT_CPU_FEATURES ();
 
+  /* 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 ();
+
   /* Perform IREL{,A} relocations.  */
   ARCH_SETUP_IREL ();
 
@@ -202,6 +188,26 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
      hwcap and platform fields available in the TCB.  */
   ARCH_APPLY_IREL ();
 
+# ifdef HAVE_AUX_VECTOR
+  if (GL(dl_phdr) == NULL)
+# endif
+    {
+      /* Starting from binutils-2.23, the linker will define the
+         magic symbol __ehdr_start to point to our own ELF header
+         if it is visible in a segment that also includes the phdrs.
+         So we can set up _dl_phdr and _dl_phnum even without any
+         information from auxv.  */
+
+      extern const ElfW(Ehdr) __ehdr_start
+	__attribute__ ((weak, visibility ("hidden")));
+      if (&__ehdr_start != NULL)
+        {
+          assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
+          GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
+          GL(dl_phnum) = __ehdr_start.e_phnum;
+        }
+    }
+
   /* Set up the stack checker's canary.  */
   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
 # ifdef THREAD_SET_STACK_GUARD


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

end of thread, other threads:[~2021-01-20 15:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-11 10:48 [glibc/nsz/bug27072] csu: Move static pie self relocation later [BZ #27072] Szabolcs Nagy
2021-01-12 17:18 Szabolcs Nagy
2021-01-14 11:12 Szabolcs Nagy
2021-01-18 16:15 Szabolcs Nagy
2021-01-19 15:59 Szabolcs Nagy
2021-01-20 15:23 Szabolcs Nagy

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