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