abe59f
commit 3908fa933a4354309225af616d9242f595e11ccf
abe59f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
abe59f
Date:   Wed Jun 30 00:21:18 2021 -0300
abe59f
abe59f
    elf: Fix audit regression
abe59f
    
abe59f
    Commit 03e187a41d9 added a regression when an audit module does not have
abe59f
    libc as DT_NEEDED (although unusual it is possible).
abe59f
    
abe59f
    Checked on x86_64-linux-gnu.
abe59f
abe59f
Conflicts:
abe59f
	elf/Makefile
abe59f
abe59f
diff --git a/elf/Makefile b/elf/Makefile
abe59f
index 30417c3ce15abcb4..6262a4a65cfd2148 100644
abe59f
--- a/elf/Makefile
abe59f
+++ b/elf/Makefile
abe59f
@@ -218,6 +218,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
abe59f
 	 tst-rtld-run-static \
abe59f
 	 tst-dlmopen-dlerror \
abe59f
 	 tst-dlmopen-gethostbyname \
abe59f
+	 tst-audit17 \
abe59f
 #	 reldep9
abe59f
 tests-internal += loadtest unload unload2 circleload1 \
abe59f
 	 neededtest neededtest2 neededtest3 neededtest4 \
abe59f
@@ -1527,6 +1528,16 @@ $(objpfx)tst-auditlogmod-3.so: $(libsupport)
abe59f
 $(objpfx)tst-audit16.out: \
abe59f
   $(objpfx)tst-auditlogmod-1.so $(objpfx)tst-auditlogmod-2.so \
abe59f
   $(objpfx)tst-auditlogmod-3.so
abe59f
+$(objpfx)tst-audit17.out: $(objpfx)tst-auditmod17.so
abe59f
+# The test check if a audit library without libc.so on DT_NEEDED works as
abe59f
+# intended, so it uses an explicit link rule.
abe59f
+$(objpfx)tst-auditmod17.so: $(objpfx)tst-auditmod17.os
abe59f
+	$(CC) -nostdlib -nostartfiles -shared -o $@.new \
abe59f
+	$(filter-out $(map-file),$^)
abe59f
+	$(call after-link,$@.new)
abe59f
+	mv -f $@.new $@
abe59f
+CFLAGS-.os += $(call elide-stack-protector,.os,tst-auditmod17)
abe59f
+tst-audit17-ENV = LD_AUDIT=$(objpfx)tst-auditmod17.so
abe59f
 
abe59f
 # tst-sonamemove links against an older implementation of the library.
abe59f
 LDFLAGS-tst-sonamemove-linkmod1.so = \
abe59f
diff --git a/elf/dl-open.c b/elf/dl-open.c
abe59f
index 736df62ce6e46d34..661a2172d1789b26 100644
abe59f
--- a/elf/dl-open.c
abe59f
+++ b/elf/dl-open.c
abe59f
@@ -759,16 +759,9 @@ dl_open_worker_begin (void *a)
abe59f
      namespace.  */
abe59f
   if (!args->libc_already_loaded)
abe59f
     {
abe59f
+      /* dlopen cannot be used to load an initial libc by design.  */
abe59f
       struct link_map *libc_map = GL(dl_ns)[args->nsid].libc_map;
abe59f
-#ifdef SHARED
abe59f
-      bool initial = libc_map->l_ns == LM_ID_BASE;
abe59f
-#else
abe59f
-      /* In the static case, there is only one namespace, but it
abe59f
-	 contains a secondary libc (the primary libc is statically
abe59f
-	 linked).  */
abe59f
-      bool initial = false;
abe59f
-#endif
abe59f
-      _dl_call_libc_early_init (libc_map, initial);
abe59f
+      _dl_call_libc_early_init (libc_map, false);
abe59f
     }
abe59f
 
abe59f
 #ifndef SHARED
abe59f
diff --git a/elf/tst-audit17.c b/elf/tst-audit17.c
abe59f
new file mode 100644
abe59f
index 0000000000000000..92986699d497845f
abe59f
--- /dev/null
abe59f
+++ b/elf/tst-audit17.c
abe59f
@@ -0,0 +1,25 @@
abe59f
+/* Check DT_AUDIT with audit not linked against libc.
abe59f
+   Copyright (C) 2021 Free Software Foundation, Inc.
abe59f
+   This file is part of the GNU C Library.
abe59f
+
abe59f
+   The GNU C Library is free software; you can redistribute it and/or
abe59f
+   modify it under the terms of the GNU Lesser General Public
abe59f
+   License as published by the Free Software Foundation; either
abe59f
+   version 2.1 of the License, or (at your option) any later version.
abe59f
+
abe59f
+   The GNU C Library is distributed in the hope that it will be useful,
abe59f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
abe59f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
abe59f
+   Lesser General Public License for more details.
abe59f
+
abe59f
+   You should have received a copy of the GNU Lesser General Public
abe59f
+   License along with the GNU C Library; if not, see
abe59f
+   <https://www.gnu.org/licenses/>.  */
abe59f
+
abe59f
+static int
abe59f
+do_test (void)
abe59f
+{
abe59f
+  return 0;
abe59f
+}
abe59f
+
abe59f
+#include <support/test-driver.c>
abe59f
diff --git a/elf/tst-auditmod17.c b/elf/tst-auditmod17.c
abe59f
new file mode 100644
abe59f
index 0000000000000000..7a4467f597b56cf4
abe59f
--- /dev/null
abe59f
+++ b/elf/tst-auditmod17.c
abe59f
@@ -0,0 +1,23 @@
abe59f
+/* Check DT_AUDIT with audit not linked against libc.
abe59f
+   Copyright (C) 2021 Free Software Foundation, Inc.
abe59f
+   This file is part of the GNU C Library.
abe59f
+
abe59f
+   The GNU C Library is free software; you can redistribute it and/or
abe59f
+   modify it under the terms of the GNU Lesser General Public
abe59f
+   License as published by the Free Software Foundation; either
abe59f
+   version 2.1 of the License, or (at your option) any later version.
abe59f
+
abe59f
+   The GNU C Library is distributed in the hope that it will be useful,
abe59f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
abe59f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
abe59f
+   Lesser General Public License for more details.
abe59f
+
abe59f
+   You should have received a copy of the GNU Lesser General Public
abe59f
+   License along with the GNU C Library; if not, see
abe59f
+   <https://www.gnu.org/licenses/>.  */
abe59f
+
abe59f
+unsigned int
abe59f
+la_version (unsigned int version)
abe59f
+{
abe59f
+  return version;
abe59f
+}