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