076f82
commit ff900fad89df7fa12750c018993a12cc02474646
076f82
Author: Florian Weimer <fweimer@redhat.com>
076f82
Date:   Mon Feb 28 11:50:41 2022 +0100
076f82
076f82
    Linux: Consolidate auxiliary vector parsing (redo)
076f82
    
076f82
    And optimize it slightly.
076f82
    
076f82
    This is commit 8c8510ab2790039e58995ef3a22309582413d3ff revised.
076f82
    
076f82
    In _dl_aux_init in elf/dl-support.c, use an explicit loop
076f82
    and -fno-tree-loop-distribute-patterns to avoid memset.
076f82
    
076f82
    Reviewed-by: Szabolcs Nagy <szabolcs.nagy@arm.com>
076f82
    (cherry picked from commit 73fc4e28b9464f0e13edc719a5372839970e7ddb)
076f82
076f82
diff --git a/elf/Makefile b/elf/Makefile
076f82
index c89a6a58690646ee..6423ebbdd7708a14 100644
076f82
--- a/elf/Makefile
076f82
+++ b/elf/Makefile
076f82
@@ -148,6 +148,11 @@ ifeq (yes,$(have-loop-to-function))
076f82
 CFLAGS-rtld.c += -fno-tree-loop-distribute-patterns
076f82
 endif
076f82
 
076f82
+ifeq (yes,$(have-loop-to-function))
076f82
+# Likewise, during static library startup, memset is not yet available.
076f82
+CFLAGS-dl-support.c = -fno-tree-loop-distribute-patterns
076f82
+endif
076f82
+
076f82
 # Compile rtld itself without stack protection.
076f82
 # Also compile all routines in the static library that are elided from
076f82
 # the shared libc because they are in libc.a in the same way.
076f82
diff --git a/elf/dl-support.c b/elf/dl-support.c
076f82
index f29dc965f4d10648..a2e45e7b14e3a6b9 100644
076f82
--- a/elf/dl-support.c
076f82
+++ b/elf/dl-support.c
076f82
@@ -43,6 +43,7 @@
076f82
 #include <dl-vdso.h>
076f82
 #include <dl-vdso-setup.h>
076f82
 #include <dl-auxv.h>
076f82
+#include <array_length.h>
076f82
 
076f82
 extern char *__progname;
076f82
 char **_dl_argv = &__progname;	/* This is checked for some error messages.  */
076f82
@@ -241,93 +242,25 @@ __rtld_lock_define_initialized_recursive (, _dl_load_tls_lock)
076f82
 
076f82
 
076f82
 #ifdef HAVE_AUX_VECTOR
076f82
+#include <dl-parse_auxv.h>
076f82
+
076f82
 int _dl_clktck;
076f82
 
076f82
 void
076f82
 _dl_aux_init (ElfW(auxv_t) *av)
076f82
 {
076f82
-  int seen = 0;
076f82
-  uid_t uid = 0;
076f82
-  gid_t gid = 0;
076f82
-
076f82
 #ifdef NEED_DL_SYSINFO
076f82
   /* NB: Avoid RELATIVE relocation in static PIE.  */
076f82
   GL(dl_sysinfo) = DL_SYSINFO_DEFAULT;
076f82
 #endif
076f82
 
076f82
   _dl_auxv = av;
076f82
-  for (; av->a_type != AT_NULL; ++av)
076f82
-    switch (av->a_type)
076f82
-      {
076f82
-      case AT_PAGESZ:
076f82
-	if (av->a_un.a_val != 0)
076f82
-	  GLRO(dl_pagesize) = av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_CLKTCK:
076f82
-	GLRO(dl_clktck) = av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_PHDR:
076f82
-	GL(dl_phdr) = (const void *) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_PHNUM:
076f82
-	GL(dl_phnum) = av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_PLATFORM:
076f82
-	GLRO(dl_platform) = (void *) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_HWCAP:
076f82
-	GLRO(dl_hwcap) = (unsigned long int) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_HWCAP2:
076f82
-	GLRO(dl_hwcap2) = (unsigned long int) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_FPUCW:
076f82
-	GLRO(dl_fpu_control) = av->a_un.a_val;
076f82
-	break;
076f82
-#ifdef NEED_DL_SYSINFO
076f82
-      case AT_SYSINFO:
076f82
-	GL(dl_sysinfo) = av->a_un.a_val;
076f82
-	break;
076f82
-#endif
076f82
-#ifdef NEED_DL_SYSINFO_DSO
076f82
-      case AT_SYSINFO_EHDR:
076f82
-	GL(dl_sysinfo_dso) = (void *) av->a_un.a_val;
076f82
-	break;
076f82
-#endif
076f82
-      case AT_UID:
076f82
-	uid ^= av->a_un.a_val;
076f82
-	seen |= 1;
076f82
-	break;
076f82
-      case AT_EUID:
076f82
-	uid ^= av->a_un.a_val;
076f82
-	seen |= 2;
076f82
-	break;
076f82
-      case AT_GID:
076f82
-	gid ^= av->a_un.a_val;
076f82
-	seen |= 4;
076f82
-	break;
076f82
-      case AT_EGID:
076f82
-	gid ^= av->a_un.a_val;
076f82
-	seen |= 8;
076f82
-	break;
076f82
-      case AT_SECURE:
076f82
-	seen = -1;
076f82
-	__libc_enable_secure = av->a_un.a_val;
076f82
-	__libc_enable_secure_decided = 1;
076f82
-	break;
076f82
-      case AT_RANDOM:
076f82
-	_dl_random = (void *) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_MINSIGSTKSZ:
076f82
-	_dl_minsigstacksize = av->a_un.a_val;
076f82
-	break;
076f82
-      DL_PLATFORM_AUXV
076f82
-      }
076f82
-  if (seen == 0xf)
076f82
-    {
076f82
-      __libc_enable_secure = uid != 0 || gid != 0;
076f82
-      __libc_enable_secure_decided = 1;
076f82
-    }
076f82
+  dl_parse_auxv_t auxv_values;
076f82
+  /* Use an explicit initialization loop here because memset may not
076f82
+     be available yet.  */
076f82
+  for (int i = 0; i < array_length (auxv_values); ++i)
076f82
+    auxv_values[i] = 0;
076f82
+  _dl_parse_auxv (av, auxv_values);
076f82
 }
076f82
 #endif
076f82
 
076f82
diff --git a/sysdeps/unix/sysv/linux/alpha/dl-auxv.h b/sysdeps/unix/sysv/linux/alpha/dl-auxv.h
076f82
index 1aa9dca80d189ebe..8c99e776a0af9cef 100644
076f82
--- a/sysdeps/unix/sysv/linux/alpha/dl-auxv.h
076f82
+++ b/sysdeps/unix/sysv/linux/alpha/dl-auxv.h
076f82
@@ -20,16 +20,8 @@
076f82
 
076f82
 extern long __libc_alpha_cache_shape[4];
076f82
 
076f82
-#define DL_PLATFORM_AUXV				\
076f82
-      case AT_L1I_CACHESHAPE:				\
076f82
-	__libc_alpha_cache_shape[0] = av->a_un.a_val;	\
076f82
-	break;						\
076f82
-      case AT_L1D_CACHESHAPE:				\
076f82
-	__libc_alpha_cache_shape[1] = av->a_un.a_val;	\
076f82
-	break;						\
076f82
-      case AT_L2_CACHESHAPE:				\
076f82
-	__libc_alpha_cache_shape[2] = av->a_un.a_val;	\
076f82
-	break;						\
076f82
-      case AT_L3_CACHESHAPE:				\
076f82
-	__libc_alpha_cache_shape[3] = av->a_un.a_val;	\
076f82
-	break;
076f82
+#define DL_PLATFORM_AUXV					\
076f82
+  __libc_alpha_cache_shape[0] = auxv_values[AT_L1I_CACHESHAPE]; \
076f82
+  __libc_alpha_cache_shape[1] = auxv_values[AT_L1D_CACHESHAPE]; \
076f82
+  __libc_alpha_cache_shape[2] = auxv_values[AT_L2_CACHESHAPE];	\
076f82
+  __libc_alpha_cache_shape[3] = auxv_values[AT_L3_CACHESHAPE];
076f82
diff --git a/sysdeps/unix/sysv/linux/dl-parse_auxv.h b/sysdeps/unix/sysv/linux/dl-parse_auxv.h
076f82
new file mode 100644
076f82
index 0000000000000000..bf9374371eb217fc
076f82
--- /dev/null
076f82
+++ b/sysdeps/unix/sysv/linux/dl-parse_auxv.h
076f82
@@ -0,0 +1,61 @@
076f82
+/* Parse the Linux auxiliary vector.
076f82
+   Copyright (C) 1995-2022 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#include <elf.h>
076f82
+#include <entry.h>
076f82
+#include <fpu_control.h>
076f82
+#include <ldsodefs.h>
076f82
+#include <link.h>
076f82
+
076f82
+typedef ElfW(Addr) dl_parse_auxv_t[AT_MINSIGSTKSZ + 1];
076f82
+
076f82
+/* Copy the auxiliary vector into AUXV_VALUES and set up GLRO
076f82
+   variables.  */
076f82
+static inline
076f82
+void _dl_parse_auxv (ElfW(auxv_t) *av, dl_parse_auxv_t auxv_values)
076f82
+{
076f82
+  auxv_values[AT_ENTRY] = (ElfW(Addr)) ENTRY_POINT;
076f82
+  auxv_values[AT_PAGESZ] = EXEC_PAGESIZE;
076f82
+  auxv_values[AT_FPUCW] = _FPU_DEFAULT;
076f82
+
076f82
+  /* NB: Default to a constant CONSTANT_MINSIGSTKSZ.  */
076f82
+  _Static_assert (__builtin_constant_p (CONSTANT_MINSIGSTKSZ),
076f82
+                  "CONSTANT_MINSIGSTKSZ is constant");
076f82
+  auxv_values[AT_MINSIGSTKSZ] = CONSTANT_MINSIGSTKSZ;
076f82
+
076f82
+  for (; av->a_type != AT_NULL; av++)
076f82
+    if (av->a_type <= AT_MINSIGSTKSZ)
076f82
+      auxv_values[av->a_type] = av->a_un.a_val;
076f82
+
076f82
+  GLRO(dl_pagesize) = auxv_values[AT_PAGESZ];
076f82
+  __libc_enable_secure = auxv_values[AT_SECURE];
076f82
+  GLRO(dl_platform) = (void *) auxv_values[AT_PLATFORM];
076f82
+  GLRO(dl_hwcap) = auxv_values[AT_HWCAP];
076f82
+  GLRO(dl_hwcap2) = auxv_values[AT_HWCAP2];
076f82
+  GLRO(dl_clktck) = auxv_values[AT_CLKTCK];
076f82
+  GLRO(dl_fpu_control) = auxv_values[AT_FPUCW];
076f82
+  _dl_random = (void *) auxv_values[AT_RANDOM];
076f82
+  GLRO(dl_minsigstacksize) = auxv_values[AT_MINSIGSTKSZ];
076f82
+  GLRO(dl_sysinfo_dso) = (void *) auxv_values[AT_SYSINFO_EHDR];
076f82
+#ifdef NEED_DL_SYSINFO
076f82
+  if (GLRO(dl_sysinfo_dso) != NULL)
076f82
+    GLRO(dl_sysinfo) = auxv_values[AT_SYSINFO];
076f82
+#endif
076f82
+
076f82
+  DL_PLATFORM_AUXV
076f82
+}
076f82
diff --git a/sysdeps/unix/sysv/linux/dl-sysdep.c b/sysdeps/unix/sysv/linux/dl-sysdep.c
076f82
index 3487976b06ad7f58..56db828fc6985de6 100644
076f82
--- a/sysdeps/unix/sysv/linux/dl-sysdep.c
076f82
+++ b/sysdeps/unix/sysv/linux/dl-sysdep.c
076f82
@@ -18,15 +18,14 @@
076f82
 
076f82
 #include <_itoa.h>
076f82
 #include <assert.h>
076f82
-#include <dl-hwcap-check.h>
076f82
+#include <dl-auxv.h>
076f82
 #include <dl-osinfo.h>
076f82
+#include <dl-parse_auxv.h>
076f82
 #include <dl-procinfo.h>
076f82
 #include <dl-tunables.h>
076f82
 #include <elf.h>
076f82
-#include <entry.h>
076f82
 #include <errno.h>
076f82
 #include <fcntl.h>
076f82
-#include <fpu_control.h>
076f82
 #include <ldsodefs.h>
076f82
 #include <libc-internal.h>
076f82
 #include <libintl.h>
076f82
@@ -43,10 +42,9 @@
076f82
 #include <unistd.h>
076f82
 
076f82
 #include <dl-machine.h>
076f82
+#include <dl-hwcap-check.h>
076f82
 
076f82
 #ifdef SHARED
076f82
-# include <dl-auxv.h>
076f82
-
076f82
 extern char **_environ attribute_hidden;
076f82
 extern char _end[] attribute_hidden;
076f82
 
076f82
@@ -64,20 +62,20 @@ void *_dl_random attribute_relro = NULL;
076f82
 # define DL_STACK_END(cookie) ((void *) (cookie))
076f82
 #endif
076f82
 
076f82
-ElfW(Addr)
076f82
-_dl_sysdep_start (void **start_argptr,
076f82
-		  void (*dl_main) (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
076f82
-				   ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv))
076f82
+/* Arguments passed to dl_main.  */
076f82
+struct dl_main_arguments
076f82
 {
076f82
-  const ElfW(Phdr) *phdr = NULL;
076f82
-  ElfW(Word) phnum = 0;
076f82
+  const ElfW(Phdr) *phdr;
076f82
+  ElfW(Word) phnum;
076f82
   ElfW(Addr) user_entry;
076f82
-  ElfW(auxv_t) *av;
076f82
-#ifdef NEED_DL_SYSINFO
076f82
-  uintptr_t new_sysinfo = 0;
076f82
-#endif
076f82
+};
076f82
 
076f82
-  __libc_stack_end = DL_STACK_END (start_argptr);
076f82
+/* Separate function, so that dl_main can be called without the large
076f82
+   array on the stack.  */
076f82
+static void
076f82
+_dl_sysdep_parse_arguments (void **start_argptr,
076f82
+			    struct dl_main_arguments *args)
076f82
+{
076f82
   _dl_argc = (intptr_t) *start_argptr;
076f82
   _dl_argv = (char **) (start_argptr + 1); /* Necessary aliasing violation.  */
076f82
   _environ = _dl_argv + _dl_argc + 1;
076f82
@@ -89,74 +87,25 @@ _dl_sysdep_start (void **start_argptr,
076f82
 	break;
076f82
       }
076f82
 
076f82
-  user_entry = (ElfW(Addr)) ENTRY_POINT;
076f82
-  GLRO(dl_platform) = NULL; /* Default to nothing known about the platform.  */
076f82
+  dl_parse_auxv_t auxv_values = { 0, };
076f82
+  _dl_parse_auxv (GLRO(dl_auxv), auxv_values);
076f82
 
076f82
-  /* NB: Default to a constant CONSTANT_MINSIGSTKSZ.  */
076f82
-  _Static_assert (__builtin_constant_p (CONSTANT_MINSIGSTKSZ),
076f82
-		  "CONSTANT_MINSIGSTKSZ is constant");
076f82
-  GLRO(dl_minsigstacksize) = CONSTANT_MINSIGSTKSZ;
076f82
+  args->phdr = (const ElfW(Phdr) *) auxv_values[AT_PHDR];
076f82
+  args->phnum = auxv_values[AT_PHNUM];
076f82
+  args->user_entry = auxv_values[AT_ENTRY];
076f82
+}
076f82
 
076f82
-  for (av = GLRO(dl_auxv); av->a_type != AT_NULL; av++)
076f82
-    switch (av->a_type)
076f82
-      {
076f82
-      case AT_PHDR:
076f82
-	phdr = (void *) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_PHNUM:
076f82
-	phnum = av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_PAGESZ:
076f82
-	GLRO(dl_pagesize) = av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_ENTRY:
076f82
-	user_entry = av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_SECURE:
076f82
-	__libc_enable_secure = av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_PLATFORM:
076f82
-	GLRO(dl_platform) = (void *) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_HWCAP:
076f82
-	GLRO(dl_hwcap) = (unsigned long int) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_HWCAP2:
076f82
-	GLRO(dl_hwcap2) = (unsigned long int) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_CLKTCK:
076f82
-	GLRO(dl_clktck) = av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_FPUCW:
076f82
-	GLRO(dl_fpu_control) = av->a_un.a_val;
076f82
-	break;
076f82
-#ifdef NEED_DL_SYSINFO
076f82
-      case AT_SYSINFO:
076f82
-	new_sysinfo = av->a_un.a_val;
076f82
-	break;
076f82
-#endif
076f82
-      case AT_SYSINFO_EHDR:
076f82
-	GLRO(dl_sysinfo_dso) = (void *) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_RANDOM:
076f82
-	_dl_random = (void *) av->a_un.a_val;
076f82
-	break;
076f82
-      case AT_MINSIGSTKSZ:
076f82
-	GLRO(dl_minsigstacksize) = av->a_un.a_val;
076f82
-	break;
076f82
-      DL_PLATFORM_AUXV
076f82
-      }
076f82
+ElfW(Addr)
076f82
+_dl_sysdep_start (void **start_argptr,
076f82
+		  void (*dl_main) (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
076f82
+				   ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv))
076f82
+{
076f82
+  __libc_stack_end = DL_STACK_END (start_argptr);
076f82
 
076f82
-  dl_hwcap_check ();
076f82
+  struct dl_main_arguments dl_main_args;
076f82
+  _dl_sysdep_parse_arguments (start_argptr, &dl_main_args);
076f82
 
076f82
-#ifdef NEED_DL_SYSINFO
076f82
-  if (new_sysinfo != 0)
076f82
-    {
076f82
-      /* Only set the sysinfo value if we also have the vsyscall DSO.  */
076f82
-      if (GLRO(dl_sysinfo_dso) != 0)
076f82
-        GLRO(dl_sysinfo) = new_sysinfo;
076f82
-    }
076f82
-#endif
076f82
+  dl_hwcap_check ();
076f82
 
076f82
   __tunables_init (_environ);
076f82
 
076f82
@@ -188,8 +137,9 @@ _dl_sysdep_start (void **start_argptr,
076f82
   if (__builtin_expect (__libc_enable_secure, 0))
076f82
     __libc_check_standard_fds ();
076f82
 
076f82
-  (*dl_main) (phdr, phnum, &user_entry, GLRO(dl_auxv));
076f82
-  return user_entry;
076f82
+  (*dl_main) (dl_main_args.phdr, dl_main_args.phnum,
076f82
+              &dl_main_args.user_entry, GLRO(dl_auxv));
076f82
+  return dl_main_args.user_entry;
076f82
 }
076f82
 
076f82
 void
076f82
diff --git a/sysdeps/unix/sysv/linux/powerpc/dl-auxv.h b/sysdeps/unix/sysv/linux/powerpc/dl-auxv.h
076f82
index 36ba0f3e9e45f3e2..7f35fb531ba22098 100644
076f82
--- a/sysdeps/unix/sysv/linux/powerpc/dl-auxv.h
076f82
+++ b/sysdeps/unix/sysv/linux/powerpc/dl-auxv.h
076f82
@@ -16,15 +16,5 @@
076f82
    License along with the GNU C Library; if not, see
076f82
    <https://www.gnu.org/licenses/>.  */
076f82
 
076f82
-#include <ldsodefs.h>
076f82
-
076f82
-#if IS_IN (libc) && !defined SHARED
076f82
-int GLRO(dl_cache_line_size);
076f82
-#endif
076f82
-
076f82
-/* Scan the Aux Vector for the "Data Cache Block Size" entry and assign it
076f82
-   to dl_cache_line_size.  */
076f82
-#define DL_PLATFORM_AUXV						      \
076f82
-      case AT_DCACHEBSIZE:						      \
076f82
-	GLRO(dl_cache_line_size) = av->a_un.a_val;			      \
076f82
-	break;
076f82
+#define DL_PLATFORM_AUXV \
076f82
+  GLRO(dl_cache_line_size) = auxv_values[AT_DCACHEBSIZE];
076f82
diff --git a/sysdeps/unix/sysv/linux/powerpc/dl-support.c b/sysdeps/unix/sysv/linux/powerpc/dl-support.c
076f82
new file mode 100644
076f82
index 0000000000000000..abe68a704946b90f
076f82
--- /dev/null
076f82
+++ b/sysdeps/unix/sysv/linux/powerpc/dl-support.c
076f82
@@ -0,0 +1,4 @@
076f82
+#include <elf/dl-support.c>
076f82
+
076f82
+/* Populated from the auxiliary vector.  */
076f82
+int _dl_cache_line_size;