08c3a6
commit f6a54a304223666ea4af73260c99c830d7726eca
08c3a6
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
08c3a6
Date:   Fri Oct 15 14:35:31 2021 -0300
08c3a6
08c3a6
    elf: Fix elf_get_dynamic_info() for bootstrap
08c3a6
    
08c3a6
    THe d6d89608ac8c broke powerpc for --enable-bind-now because it turned
08c3a6
    out that different than patch assumption rtld elf_get_dynamic_info()
08c3a6
    does require to handle RTLD_BOOTSTRAP to avoid DT_FLAGS and
08c3a6
    DT_RUNPATH (more specially the GLRO usage which is not reallocate
08c3a6
    yet).
08c3a6
    
08c3a6
    This patch fixes by passing two arguments to elf_get_dynamic_info()
08c3a6
    to inform that by rtld (bootstrap) or static pie initialization
08c3a6
    (static_pie_bootstrap).  I think using explicit argument is way more
08c3a6
    clear and burried C preprocessor, and compiler should remove the
08c3a6
    dead code.
08c3a6
    
08c3a6
    I checked on x86_64 and i686 with default options, --enable-bind-now,
08c3a6
    and --enable-bind-now and --enable--static-pie.  I also check on
08c3a6
    aarch64, armhf, powerpc64, and powerpc with default and
08c3a6
    --enable-bind-now.
08c3a6
    
08c3a6
    (cherry picked from commit 5118dcac68c4eadfd6304bb33adde63d062dc07f)
08c3a6
    
08c3a6
    Resolved conflicts:
08c3a6
            elf/rtld.c - Manual merge.
08c3a6
08c3a6
diff --git a/elf/dl-load.c b/elf/dl-load.c
08c3a6
index fb3da5aa565908a6..a920b12a906a9dec 100644
08c3a6
--- a/elf/dl-load.c
08c3a6
+++ b/elf/dl-load.c
08c3a6
@@ -1296,7 +1296,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
08c3a6
   if (l->l_ld != 0)
08c3a6
     l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
08c3a6
 
08c3a6
-  elf_get_dynamic_info (l);
08c3a6
+  elf_get_dynamic_info (l, false, false);
08c3a6
 
08c3a6
   /* Make sure we are not dlopen'ing an object that has the
08c3a6
      DF_1_NOOPEN flag set, or a PIE object.  */
08c3a6
diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c
08c3a6
index ababafcf98f9945d..757205affe65d9e1 100644
08c3a6
--- a/elf/dl-reloc-static-pie.c
08c3a6
+++ b/elf/dl-reloc-static-pie.c
08c3a6
@@ -25,7 +25,6 @@
08c3a6
 
08c3a6
 #include <dl-machine.h>
08c3a6
 
08c3a6
-#define STATIC_PIE_BOOTSTRAP
08c3a6
 #define RESOLVE_MAP(map, scope, sym, version, flags) map
08c3a6
 #include "dynamic-link.h"
08c3a6
 #include "get-dynamic-info.h"
08c3a6
@@ -52,7 +51,7 @@ _dl_relocate_static_pie (void)
08c3a6
 	break;
08c3a6
       }
08c3a6
 
08c3a6
-  elf_get_dynamic_info (main_map);
08c3a6
+  elf_get_dynamic_info (main_map, false, true);
08c3a6
 
08c3a6
 # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
08c3a6
   ELF_MACHINE_BEFORE_RTLD_RELOC (main_map, main_map->l_info);
08c3a6
diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h
08c3a6
index 1ac0663d1ff5de24..f63e07dc6d2cd5e6 100644
08c3a6
--- a/elf/get-dynamic-info.h
08c3a6
+++ b/elf/get-dynamic-info.h
08c3a6
@@ -26,7 +26,8 @@
08c3a6
 #include <libc-diag.h>
08c3a6
 
08c3a6
 static inline void __attribute__ ((unused, always_inline))
08c3a6
-elf_get_dynamic_info (struct link_map *l)
08c3a6
+elf_get_dynamic_info (struct link_map *l, bool bootstrap,
08c3a6
+		      bool static_pie_bootstrap)
08c3a6
 {
08c3a6
 #if __ELF_NATIVE_CLASS == 32
08c3a6
   typedef Elf32_Word d_tag_utype;
08c3a6
@@ -35,7 +36,7 @@ elf_get_dynamic_info (struct link_map *l)
08c3a6
 #endif
08c3a6
 
08c3a6
 #ifndef STATIC_PIE_BOOTSTRAP
08c3a6
-  if (l->l_ld == NULL)
08c3a6
+  if (!bootstrap && l->l_ld == NULL)
08c3a6
     return;
08c3a6
 #endif
08c3a6
 
08c3a6
@@ -112,47 +113,63 @@ elf_get_dynamic_info (struct link_map *l)
08c3a6
   if (info[DT_REL] != NULL)
08c3a6
     assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
08c3a6
 #endif
08c3a6
-#ifdef STATIC_PIE_BOOTSTRAP
08c3a6
-  assert (info[DT_RUNPATH] == NULL);
08c3a6
-  assert (info[DT_RPATH] == NULL);
08c3a6
-#endif
08c3a6
-  if (info[DT_FLAGS] != NULL)
08c3a6
+  if (bootstrap || static_pie_bootstrap)
08c3a6
     {
08c3a6
-      /* Flags are used.  Translate to the old form where available.
08c3a6
-	 Since these l_info entries are only tested for NULL pointers it
08c3a6
-	 is ok if they point to the DT_FLAGS entry.  */
08c3a6
-      l->l_flags = info[DT_FLAGS]->d_un.d_val;
08c3a6
-
08c3a6
-      if (l->l_flags & DF_SYMBOLIC)
08c3a6
-	info[DT_SYMBOLIC] = info[DT_FLAGS];
08c3a6
-      if (l->l_flags & DF_TEXTREL)
08c3a6
-	info[DT_TEXTREL] = info[DT_FLAGS];
08c3a6
-      if (l->l_flags & DF_BIND_NOW)
08c3a6
-	info[DT_BIND_NOW] = info[DT_FLAGS];
08c3a6
+      assert (info[DT_RUNPATH] == NULL);
08c3a6
+      assert (info[DT_RPATH] == NULL);
08c3a6
     }
08c3a6
-  if (info[VERSYMIDX (DT_FLAGS_1)] != NULL)
08c3a6
+  if (bootstrap)
08c3a6
     {
08c3a6
-      l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val;
08c3a6
-      if (l->l_flags_1 & DF_1_NODELETE)
08c3a6
-	l->l_nodelete_pending = true;
08c3a6
-
08c3a6
-      /* Only DT_1_SUPPORTED_MASK bits are supported, and we would like
08c3a6
-	 to assert this, but we can't. Users have been setting
08c3a6
-	 unsupported DF_1_* flags for a long time and glibc has ignored
08c3a6
-	 them. Therefore to avoid breaking existing applications the
08c3a6
-	 best we can do is add a warning during debugging with the
08c3a6
-	 intent of notifying the user of the problem.  */
08c3a6
-      if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0)
08c3a6
-	  && l->l_flags_1 & ~DT_1_SUPPORTED_MASK)
08c3a6
-	_dl_debug_printf ("\nWARNING: Unsupported flag value(s) of 0x%x in DT_FLAGS_1.\n",
08c3a6
-			  l->l_flags_1 & ~DT_1_SUPPORTED_MASK);
08c3a6
-
08c3a6
-      if (l->l_flags_1 & DF_1_NOW)
08c3a6
-	info[DT_BIND_NOW] = info[VERSYMIDX (DT_FLAGS_1)];
08c3a6
+      /* Only the bind now flags are allowed.  */
08c3a6
+      assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
08c3a6
+	      || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0);
08c3a6
+      /* Flags must not be set for ld.so.  */
08c3a6
+      assert (info[DT_FLAGS] == NULL
08c3a6
+	      || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0);
08c3a6
     }
08c3a6
-  if (info[DT_RUNPATH] != NULL)
08c3a6
-    /* If both RUNPATH and RPATH are given, the latter is ignored.  */
08c3a6
-    info[DT_RPATH] = NULL;
08c3a6
+  else
08c3a6
+    {
08c3a6
+      if (info[DT_FLAGS] != NULL)
08c3a6
+	{
08c3a6
+	  /* Flags are used.  Translate to the old form where available.
08c3a6
+	     Since these l_info entries are only tested for NULL pointers it
08c3a6
+	     is ok if they point to the DT_FLAGS entry.  */
08c3a6
+	  l->l_flags = info[DT_FLAGS]->d_un.d_val;
08c3a6
+
08c3a6
+	  if (l->l_flags & DF_SYMBOLIC)
08c3a6
+	    info[DT_SYMBOLIC] = info[DT_FLAGS];
08c3a6
+	  if (l->l_flags & DF_TEXTREL)
08c3a6
+	    info[DT_TEXTREL] = info[DT_FLAGS];
08c3a6
+	  if (l->l_flags & DF_BIND_NOW)
08c3a6
+	    info[DT_BIND_NOW] = info[DT_FLAGS];
08c3a6
+	}
08c3a6
+
08c3a6
+      if (info[VERSYMIDX (DT_FLAGS_1)] != NULL)
08c3a6
+	{
08c3a6
+	  l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val;
08c3a6
+	  if (l->l_flags_1 & DF_1_NODELETE)
08c3a6
+	    l->l_nodelete_pending = true;
08c3a6
+
08c3a6
+	  /* Only DT_1_SUPPORTED_MASK bits are supported, and we would like
08c3a6
+	     to assert this, but we can't. Users have been setting
08c3a6
+	     unsupported DF_1_* flags for a long time and glibc has ignored
08c3a6
+	     them. Therefore to avoid breaking existing applications the
08c3a6
+	     best we can do is add a warning during debugging with the
08c3a6
+	     intent of notifying the user of the problem.  */
08c3a6
+	  if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0)
08c3a6
+	      && l->l_flags_1 & ~DT_1_SUPPORTED_MASK)
08c3a6
+	    _dl_debug_printf ("\nWARNING: Unsupported flag value(s) of 0x%x "
08c3a6
+			      "in DT_FLAGS_1.\n",
08c3a6
+			     l->l_flags_1 & ~DT_1_SUPPORTED_MASK);
08c3a6
+
08c3a6
+	 if (l->l_flags_1 & DF_1_NOW)
08c3a6
+	   info[DT_BIND_NOW] = info[VERSYMIDX (DT_FLAGS_1)];
08c3a6
+       }
08c3a6
+
08c3a6
+    if (info[DT_RUNPATH] != NULL)
08c3a6
+      /* If both RUNPATH and RPATH are given, the latter is ignored.  */
08c3a6
+      info[DT_RPATH] = NULL;
08c3a6
+   }
08c3a6
 }
08c3a6
 
08c3a6
 #endif
08c3a6
diff --git a/elf/rtld.c b/elf/rtld.c
08c3a6
index 37d28d5a66d7b5d6..ad5ddb2a0ab94e7f 100644
08c3a6
--- a/elf/rtld.c
08c3a6
+++ b/elf/rtld.c
08c3a6
@@ -549,7 +549,7 @@ _dl_start (void *arg)
08c3a6
   /* Read our own dynamic section and fill in the info array.  */
08c3a6
   bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
08c3a6
   bootstrap_map.l_ld_readonly = DL_RO_DYN_SECTION;
08c3a6
-  elf_get_dynamic_info (&bootstrap_map);
08c3a6
+  elf_get_dynamic_info (&bootstrap_map, true, false);
08c3a6
 
08c3a6
 #if NO_TLS_OFFSET != 0
08c3a6
   bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
08c3a6
@@ -1653,7 +1653,7 @@ dl_main (const ElfW(Phdr) *phdr,
08c3a6
   if (! rtld_is_main)
08c3a6
     {
08c3a6
       /* Extract the contents of the dynamic section for easy access.  */
08c3a6
-      elf_get_dynamic_info (main_map);
08c3a6
+      elf_get_dynamic_info (main_map, false, false);
08c3a6
 
08c3a6
       /* If the main map is libc.so, update the base namespace to
08c3a6
 	 refer to this map.  If libc.so is loaded later, this happens
08c3a6
diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h
08c3a6
index f44748bc9858e5fd..3f20578046de76ed 100644
08c3a6
--- a/elf/setup-vdso.h
08c3a6
+++ b/elf/setup-vdso.h
08c3a6
@@ -64,7 +64,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)),
08c3a6
       l->l_map_end += l->l_addr;
08c3a6
       l->l_text_end += l->l_addr;
08c3a6
       l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr);
08c3a6
-      elf_get_dynamic_info (l);
08c3a6
+      elf_get_dynamic_info (l, false, false);
08c3a6
       _dl_setup_hash (l);
08c3a6
       l->l_relocated = 1;
08c3a6