e354a5
commit 03e187a41d91069543cfcf33469a05912e555447
e354a5
Author: Florian Weimer <fweimer@redhat.com>
e354a5
Date:   Wed Apr 29 15:44:03 2020 +0200
e354a5
e354a5
    elf: Add initial flag argument to __libc_early_init
e354a5
    
e354a5
    The rseq initialization should happen only for the libc in the base
e354a5
    namespace (in the dynamic case) or the statically linked libc.  The
e354a5
    __libc_multiple_libcs flag does not quite cover this case at present,
e354a5
    so this commit introduces a flag argument to __libc_early_init,
e354a5
    indicating whether the libc being libc is the primary one (of the main
e354a5
    program).
e354a5
    
e354a5
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
e354a5
e354a5
diff --git a/csu/libc-start.c b/csu/libc-start.c
e354a5
index d9c3248219d8f84f..fd0f8640eaeae34c 100644
e354a5
--- a/csu/libc-start.c
e354a5
+++ b/csu/libc-start.c
e354a5
@@ -23,6 +23,7 @@
e354a5
 #include <exit-thread.h>
e354a5
 #include <libc-internal.h>
e354a5
 #include <elf/libc-early-init.h>
e354a5
+#include <stdbool.h>
e354a5
 
e354a5
 #include <elf/dl-tunables.h>
e354a5
 
e354a5
@@ -241,7 +242,7 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
e354a5
 #ifndef SHARED
e354a5
   /* Perform early initialization.  In the shared case, this function
e354a5
      is called from the dynamic loader as early as possible.  */
e354a5
-  __libc_early_init ();
e354a5
+  __libc_early_init (true);
e354a5
 
e354a5
   /* Call the initializer of the libc.  This is only needed here if we
e354a5
      are compiling for the static library in which case we haven't
e354a5
diff --git a/elf/dl-call-libc-early-init.c b/elf/dl-call-libc-early-init.c
e354a5
index 41e9ad9aad8b5b46..9a84680a1ceafba2 100644
e354a5
--- a/elf/dl-call-libc-early-init.c
e354a5
+++ b/elf/dl-call-libc-early-init.c
e354a5
@@ -23,7 +23,7 @@
e354a5
 #include <stddef.h>
e354a5
 
e354a5
 void
e354a5
-_dl_call_libc_early_init (struct link_map *libc_map)
e354a5
+_dl_call_libc_early_init (struct link_map *libc_map, _Bool initial)
e354a5
 {
e354a5
   /* There is nothing to do if we did not actually load libc.so.  */
e354a5
   if (libc_map == NULL)
e354a5
@@ -37,5 +37,5 @@ _dl_call_libc_early_init (struct link_map *libc_map)
e354a5
   assert (sym != NULL);
e354a5
   __typeof (__libc_early_init) *early_init
e354a5
     = DL_SYMBOL_ADDRESS (libc_map, sym);
e354a5
-  early_init ();
e354a5
+  early_init (initial);
e354a5
 }
e354a5
diff --git a/elf/dl-open.c b/elf/dl-open.c
e354a5
index 1a77ec833cad6c55..3d49a84596e99bf6 100644
e354a5
--- a/elf/dl-open.c
e354a5
+++ b/elf/dl-open.c
e354a5
@@ -748,9 +748,22 @@ dl_open_worker (void *a)
e354a5
     LIBC_PROBE (reloc_complete, 3, args->nsid, r, new);
e354a5
 
e354a5
   /* If libc.so was not there before, attempt to call its early
e354a5
-     initialization routine.  */
e354a5
+     initialization routine.  Indicate to the initialization routine
e354a5
+     whether the libc being initialized is the one in the base
e354a5
+     namespace.  */
e354a5
   if (!args->libc_already_loaded)
e354a5
-    _dl_call_libc_early_init (GL(dl_ns)[args->nsid].libc_map);
e354a5
+    {
e354a5
+      struct link_map *libc_map = GL(dl_ns)[args->nsid].libc_map;
e354a5
+#ifdef SHARED
e354a5
+      bool initial = libc_map->l_ns == LM_ID_BASE;
e354a5
+#else
e354a5
+      /* In the static case, there is only one namespace, but it
e354a5
+	 contains a secondary libc (the primary libc is statically
e354a5
+	 linked).  */
e354a5
+      bool initial = false;
e354a5
+#endif
e354a5
+      _dl_call_libc_early_init (libc_map, initial);
e354a5
+    }
e354a5
 
e354a5
 #ifndef SHARED
e354a5
   DL_STATIC_INIT (new);
e354a5
diff --git a/elf/libc-early-init.h b/elf/libc-early-init.h
e354a5
index 5185fa8895c0e11a..8f7836dceaeecd5a 100644
e354a5
--- a/elf/libc-early-init.h
e354a5
+++ b/elf/libc-early-init.h
e354a5
@@ -22,14 +22,17 @@
e354a5
 struct link_map;
e354a5
 
e354a5
 /* If LIBC_MAP is not NULL, look up the __libc_early_init symbol in it
e354a5
-   and call this function.  */
e354a5
-void _dl_call_libc_early_init (struct link_map *libc_map) attribute_hidden;
e354a5
+   and call this function, with INITIAL as the argument.  */
e354a5
+void _dl_call_libc_early_init (struct link_map *libc_map, _Bool initial)
e354a5
+  attribute_hidden;
e354a5
 
e354a5
 /* In the shared case, this function is defined in libc.so and invoked
e354a5
    from ld.so (or on the fist static dlopen) after complete relocation
e354a5
    of a new loaded libc.so, but before user-defined ELF constructors
e354a5
    run.  In the static case, this function is called directly from the
e354a5
-   startup code.  */
e354a5
-void __libc_early_init (void);
e354a5
+   startup code.  If INITIAL is true, the libc being initialized is
e354a5
+   the libc for the main program.  INITIAL is false for libcs loaded
e354a5
+   for audit modules, dlmopen, and static dlopen.  */
e354a5
+void __libc_early_init (_Bool initial);
e354a5
 
e354a5
 #endif /* _LIBC_EARLY_INIT_H */
e354a5
diff --git a/elf/libc_early_init.c b/elf/libc_early_init.c
e354a5
index 7f4ca332b805a22c..e6c64fb526600fae 100644
e354a5
--- a/elf/libc_early_init.c
e354a5
+++ b/elf/libc_early_init.c
e354a5
@@ -20,7 +20,7 @@
e354a5
 #include <libc-early-init.h>
e354a5
 
e354a5
 void
e354a5
-__libc_early_init (void)
e354a5
+__libc_early_init (_Bool initial)
e354a5
 {
e354a5
   /* Initialize ctype data.  */
e354a5
   __ctype_init ();
e354a5
diff --git a/elf/rtld.c b/elf/rtld.c
e354a5
index a40d5f17db0dac8b..772aff5160359b7b 100644
e354a5
--- a/elf/rtld.c
e354a5
+++ b/elf/rtld.c
e354a5
@@ -2366,8 +2366,10 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
e354a5
       rtld_timer_accum (&relocate_time, start);
e354a5
     }
e354a5
 
e354a5
-  /* Relocation is complete.  Perform early libc initialization.  */
e354a5
-  _dl_call_libc_early_init (GL(dl_ns)[LM_ID_BASE].libc_map);
e354a5
+  /* Relocation is complete.  Perform early libc initialization.  This
e354a5
+     is the initial libc, even if audit modules have been loaded with
e354a5
+     other libcs.  */
e354a5
+  _dl_call_libc_early_init (GL(dl_ns)[LM_ID_BASE].libc_map, true);
e354a5
 
e354a5
   /* Do any necessary cleanups for the startup OS interface code.
e354a5
      We do these now so that no calls are made after rtld re-relocation