076f82
commit 4b9cd5465d5158dad7b4f0762bc70a3a1209b481
076f82
Author: Florian Weimer <fweimer@redhat.com>
076f82
Date:   Thu Feb 3 10:58:59 2022 +0100
076f82
076f82
    Linux: Consolidate auxiliary vector parsing
076f82
    
076f82
    And optimize it slightly.
076f82
    
076f82
    The large switch statement in _dl_sysdep_start can be replaced with
076f82
    a large array.  This reduces source code and binary size.  On
076f82
    i686-linux-gnu:
076f82
    
076f82
    Before:
076f82
    
076f82
       text    data     bss     dec     hex filename
076f82
       7791      12       0    7803    1e7b elf/dl-sysdep.os
076f82
    
076f82
    After:
076f82
    
076f82
       text    data     bss     dec     hex filename
076f82
       7135      12       0    7147    1beb elf/dl-sysdep.os
076f82
    
076f82
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
076f82
    (cherry picked from commit 8c8510ab2790039e58995ef3a22309582413d3ff)
076f82
076f82
diff --git a/elf/dl-support.c b/elf/dl-support.c
076f82
index f29dc965f4d10648..40ef07521336857d 100644
076f82
--- a/elf/dl-support.c
076f82
+++ b/elf/dl-support.c
076f82
@@ -241,93 +241,21 @@ __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 = { 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..b3d82f69946d6d2c
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 AUX_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 1829dab4f38b560c..80aa9f6f4acb7e3c 100644
076f82
--- a/sysdeps/unix/sysv/linux/dl-sysdep.c
076f82
+++ b/sysdeps/unix/sysv/linux/dl-sysdep.c
076f82
@@ -21,13 +21,12 @@
076f82
 #include <dl-auxv.h>
076f82
 #include <dl-hwcap-check.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
@@ -63,24 +62,24 @@ 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
-  for (char **tmp = _environ + 1; ; ++tmp)
076f82
+  for (char **tmp = _environ; ; ++tmp)
076f82
     if (*tmp == NULL)
076f82
       {
076f82
 	/* Another necessary aliasing violation.  */
076f82
@@ -88,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
@@ -187,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;