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