446cf2
commit 10b39a5124aea509dfeef2f39a0835adb0fb2296
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Fri Oct 9 10:13:14 2020 +0200
446cf2
446cf2
    elf: Add library search path information to ld.so --help
446cf2
    
446cf2
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
446cf2
446cf2
diff --git a/elf/dl-usage.c b/elf/dl-usage.c
446cf2
index 1003a435bfc2b39e..8d39bc9d5442bc59 100644
446cf2
--- a/elf/dl-usage.c
446cf2
+++ b/elf/dl-usage.c
446cf2
@@ -46,6 +46,61 @@ PARTICULAR PURPOSE.\n\
446cf2
   _exit (EXIT_SUCCESS);
446cf2
 }
446cf2
 
446cf2
+/* Print part of the library search path (from a single source).  */
446cf2
+static void
446cf2
+print_search_path_for_help_1 (struct r_search_path_elem **list)
446cf2
+{
446cf2
+  if (list == NULL || list == (void *) -1)
446cf2
+    /* Path is missing or marked as inactive.  */
446cf2
+    return;
446cf2
+
446cf2
+  for (; *list != NULL; ++list)
446cf2
+    {
446cf2
+      _dl_write (STDOUT_FILENO, "  ", 2);
446cf2
+      const char *name = (*list)->dirname;
446cf2
+      size_t namelen = (*list)->dirnamelen;
446cf2
+      if (namelen == 0)
446cf2
+        {
446cf2
+          /* The empty string denotes the current directory.  */
446cf2
+          name = ".";
446cf2
+          namelen = 1;
446cf2
+        }
446cf2
+      else if (namelen > 1)
446cf2
+        /* Remove the trailing slash.  */
446cf2
+        --namelen;
446cf2
+      _dl_write (STDOUT_FILENO, name, namelen);
446cf2
+      _dl_printf (" (%s)\n", (*list)->what);
446cf2
+    }
446cf2
+}
446cf2
+
446cf2
+/* Prints the library search path.  See _dl_init_paths in dl-load.c
446cf2
+   how this information is populated.  */
446cf2
+static void
446cf2
+print_search_path_for_help (struct dl_main_state *state)
446cf2
+{
446cf2
+  if (__rtld_search_dirs.dirs == NULL)
446cf2
+    /* The run-time search paths have not yet been initialized.  */
446cf2
+    _dl_init_paths (state->library_path, state->library_path_source);
446cf2
+
446cf2
+  _dl_printf ("\nShared library search path:\n");
446cf2
+
446cf2
+  /* The print order should reflect the processing in
446cf2
+     _dl_map_object.  */
446cf2
+
446cf2
+  struct link_map *map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
446cf2
+  if (map != NULL)
446cf2
+    print_search_path_for_help_1 (map->l_rpath_dirs.dirs);
446cf2
+
446cf2
+  print_search_path_for_help_1 (__rtld_env_path_list.dirs);
446cf2
+
446cf2
+  if (map != NULL)
446cf2
+    print_search_path_for_help_1 (map->l_runpath_dirs.dirs);
446cf2
+
446cf2
+  _dl_printf ("  (libraries located via %s)\n", LD_SO_CACHE);
446cf2
+
446cf2
+  print_search_path_for_help_1 (__rtld_search_dirs.dirs);
446cf2
+}
446cf2
+
446cf2
 void
446cf2
 _dl_help (const char *argv0, struct dl_main_state *state)
446cf2
 {
446cf2
@@ -80,5 +135,6 @@ setting environment variables (which would be inherited by subprocesses).\n\
446cf2
 This program interpreter self-identifies as: " RTLD "\n\
446cf2
 ",
446cf2
               argv0);
446cf2
+  print_search_path_for_help (state);
446cf2
   _exit (EXIT_SUCCESS);
446cf2
 }