[13/28] elf: Add library search path information to ld.so --help

Message ID eb770614c0c8080d999f755aa4cd52bd21b91155.1601569371.git.fweimer@redhat.com
State New
Headers show
Series
  • glibc-hwcaps support
Related show

Commit Message

Florian Weimer via Libc-alpha Oct. 1, 2020, 4:32 p.m.
---
 elf/dl-usage.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill

Comments

Florian Weimer via Libc-alpha Oct. 8, 2020, 4:22 p.m. | #1
LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>


On 01/10/2020 13:32, Florian Weimer via Libc-alpha wrote:
> ---

>  elf/dl-usage.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++

>  1 file changed, 56 insertions(+)

> 

> diff --git a/elf/dl-usage.c b/elf/dl-usage.c

> index 0a62e8f7cf..20aa715cb1 100644

> --- a/elf/dl-usage.c

> +++ b/elf/dl-usage.c

> @@ -46,6 +46,61 @@ PARTICULAR PURPOSE.\n\

>    _exit (0);

>  }

>  

> +/* Print part of the library search path (from a single source).  */

> +static void

> +print_search_path_for_help_1 (struct r_search_path_elem **list)

> +{

> +  if (list == NULL || list == (void *) -1)

> +    /* Path is missing or marked as inactive.  */

> +    return;

> +

> +  for (; *list != NULL; ++list)

> +    {

> +      (void) _dl_write (STDOUT_FILENO, "  ", 2);

> +      const char *name = (*list)->dirname;

> +      size_t namelen = (*list)->dirnamelen;

> +      if (namelen == 0)

> +        {

> +          /* The empty string denotes the current directory.  */

> +          name = ".";

> +          namelen = 1;

> +        }

> +      else if (namelen > 1)

> +        /* Remove the trailing slash.  */

> +        --namelen;

> +      (void) _dl_write (STDOUT_FILENO, name, namelen);

> +      _dl_printf (" (%s)\n", (*list)->what);

> +    }

> +}

> +


Do we need the (void) casts? 

> +/* Prints the library search path.  See _dl_init_paths in dl-load.c

> +   how this information is populated.  */

> +static void

> +print_search_path_for_help (struct dl_main_state *state)

> +{

> +  if (__rtld_search_dirs.dirs == NULL)

> +    /* The run-time search paths have not yet been initialized.  */

> +    _dl_init_paths (state->library_path, state->library_path_source);

> +

> +  _dl_printf ("\nShared library search path:\n");

> +

> +  /* The print order should reflect the processing in

> +     _dl_map_object.  */

> +

> +  struct link_map *map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;

> +  if (map != NULL)

> +    print_search_path_for_help_1 (map->l_rpath_dirs.dirs);

> +

> +  print_search_path_for_help_1 (__rtld_env_path_list.dirs);

> +

> +  if (map != NULL)

> +    print_search_path_for_help_1 (map->l_runpath_dirs.dirs);

> +

> +  _dl_printf ("  (libraries located via %s)\n", LD_SO_CACHE);

> +

> +  print_search_path_for_help_1 (__rtld_search_dirs.dirs);

> +}

> +

>  void

>  _dl_help (const char *argv0, struct dl_main_state *state)

>  {

> @@ -80,5 +135,6 @@ setting environment variables (which would be inherted by subprocesses).\n\

>  This program interpreter self-identifies as: " RTLD "\n\

>  ",

>                argv0);

> +  print_search_path_for_help (state);

>    _exit (0);

>  }

> 


Ok.

Patch

diff --git a/elf/dl-usage.c b/elf/dl-usage.c
index 0a62e8f7cf..20aa715cb1 100644
--- a/elf/dl-usage.c
+++ b/elf/dl-usage.c
@@ -46,6 +46,61 @@  PARTICULAR PURPOSE.\n\
   _exit (0);
 }
 
+/* Print part of the library search path (from a single source).  */
+static void
+print_search_path_for_help_1 (struct r_search_path_elem **list)
+{
+  if (list == NULL || list == (void *) -1)
+    /* Path is missing or marked as inactive.  */
+    return;
+
+  for (; *list != NULL; ++list)
+    {
+      (void) _dl_write (STDOUT_FILENO, "  ", 2);
+      const char *name = (*list)->dirname;
+      size_t namelen = (*list)->dirnamelen;
+      if (namelen == 0)
+        {
+          /* The empty string denotes the current directory.  */
+          name = ".";
+          namelen = 1;
+        }
+      else if (namelen > 1)
+        /* Remove the trailing slash.  */
+        --namelen;
+      (void) _dl_write (STDOUT_FILENO, name, namelen);
+      _dl_printf (" (%s)\n", (*list)->what);
+    }
+}
+
+/* Prints the library search path.  See _dl_init_paths in dl-load.c
+   how this information is populated.  */
+static void
+print_search_path_for_help (struct dl_main_state *state)
+{
+  if (__rtld_search_dirs.dirs == NULL)
+    /* The run-time search paths have not yet been initialized.  */
+    _dl_init_paths (state->library_path, state->library_path_source);
+
+  _dl_printf ("\nShared library search path:\n");
+
+  /* The print order should reflect the processing in
+     _dl_map_object.  */
+
+  struct link_map *map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
+  if (map != NULL)
+    print_search_path_for_help_1 (map->l_rpath_dirs.dirs);
+
+  print_search_path_for_help_1 (__rtld_env_path_list.dirs);
+
+  if (map != NULL)
+    print_search_path_for_help_1 (map->l_runpath_dirs.dirs);
+
+  _dl_printf ("  (libraries located via %s)\n", LD_SO_CACHE);
+
+  print_search_path_for_help_1 (__rtld_search_dirs.dirs);
+}
+
 void
 _dl_help (const char *argv0, struct dl_main_state *state)
 {
@@ -80,5 +135,6 @@  setting environment variables (which would be inherted by subprocesses).\n\
 This program interpreter self-identifies as: " RTLD "\n\
 ",
               argv0);
+  print_search_path_for_help (state);
   _exit (0);
 }