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