b61eb4
From 4e6db99c665d3b82a70a3e218860ef087b1555b4 Mon Sep 17 00:00:00 2001
b61eb4
From: Florian Weimer <fweimer@redhat.com>
b61eb4
Date: Mon, 15 Mar 2021 10:33:43 +0100
b61eb4
Subject: elf: ld.so --help calls _dl_init_paths without a main map [BZ #27577]
b61eb4
b61eb4
In this case, use the link map of the dynamic loader itself as
b61eb4
a replacement.  This is more than just a hack: if we ever support
b61eb4
DT_RUNPATH/DT_RPATH for the dynamic loader, reporting it for
b61eb4
ld.so --help (without further command line arguments) would be the
b61eb4
right thing to do.
b61eb4
b61eb4
Fixes commit 332421312576bd7095e70589154af99b124dd2d1 ("elf: Always
b61eb4
set l in _dl_init_paths (bug 23462)").
b61eb4
b61eb4
diff --git a/elf/Makefile b/elf/Makefile
b61eb4
index 4c9e63dac9..ba4689a7fa 100644
b61eb4
--- a/elf/Makefile
b61eb4
+++ b/elf/Makefile
b61eb4
@@ -231,7 +231,7 @@ tests += $(tests-execstack-$(have-z-execstack))
b61eb4
 ifeq ($(run-built-tests),yes)
b61eb4
 tests-special += $(objpfx)tst-leaks1-mem.out \
b61eb4
 		 $(objpfx)tst-leaks1-static-mem.out $(objpfx)noload-mem.out \
b61eb4
-		 $(objpfx)tst-ldconfig-X.out
b61eb4
+		 $(objpfx)tst-ldconfig-X.out $(objpfx)tst-rtld-help.out
b61eb4
 endif
b61eb4
 tlsmod17a-suffixes = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
b61eb4
 tlsmod18a-suffixes = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
b61eb4
@@ -409,7 +409,8 @@ endif
b61eb4
 ifeq (yes,$(build-shared))
b61eb4
 ifeq ($(run-built-tests),yes)
b61eb4
 tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \
b61eb4
-		 $(objpfx)tst-rtld-preload.out $(objpfx)argv0test.out
b61eb4
+		 $(objpfx)tst-rtld-preload.out $(objpfx)argv0test.out \
b61eb4
+		 $(objpfx)tst-rtld-help.out
b61eb4
 endif
b61eb4
 tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \
b61eb4
 		 $(objpfx)check-wx-segment.out \
b61eb4
@@ -1814,3 +1815,16 @@ $(objpfx)list-tunables.out: tst-rtld-list-tunables.sh $(objpfx)ld.so
b61eb4
 $(objpfx)tst-glibc-hwcaps-cache.out: $(objpfx)tst-glibc-hwcaps
b61eb4
 
b61eb4
 tst-dst-static-ENV = LD_LIBRARY_PATH='$$ORIGIN'
b61eb4
+
b61eb4
+$(objpfx)tst-rtld-help.out: $(objpfx)ld.so
b61eb4
+	$(test-wrapper) $(rtld-prefix) --help > $@; \
b61eb4
+	status=$$?; \
b61eb4
+	echo "info: ld.so exit status: $$status" >> $@; \
b61eb4
+	if ! grep -q 'Legacy HWCAP subdirectories under library search path directories' $@; then \
b61eb4
+	  echo "error: missing subdirectory pattern" >> $@; \
b61eb4
+	  if test $$status -eq 0; then \
b61eb4
+	    status=1; \
b61eb4
+	  fi; \
b61eb4
+	fi; \
b61eb4
+	(exit $$status); \
b61eb4
+	$(evaluate-test)
b61eb4
diff --git a/elf/dl-load.c b/elf/dl-load.c
b61eb4
index 376a2e64d6..2f760503c5 100644
b61eb4
--- a/elf/dl-load.c
b61eb4
+++ b/elf/dl-load.c
b61eb4
@@ -758,8 +758,14 @@ _dl_init_paths (const char *llp, const char *source,
b61eb4
   max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
b61eb4
   *aelem = NULL;
b61eb4
 
b61eb4
-  /* This points to the map of the main object.  */
b61eb4
+  /* This points to the map of the main object.  If there is no main
b61eb4
+     object (e.g., under --help, use the dynamic loader itself as a
b61eb4
+     stand-in.  */
b61eb4
   l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
b61eb4
+#ifdef SHARED
b61eb4
+  if (l == NULL)
b61eb4
+    l = &GL (dl_rtld_map);
b61eb4
+#endif
b61eb4
   assert (l->l_type != lt_loaded);
b61eb4
 
b61eb4
   if (l->l_info[DT_RUNPATH])