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