076f82
commit 91c2e6c3db44297bf4cb3a2e3c40236c5b6a0b23
076f82
Author: Florian Weimer <fweimer@redhat.com>
076f82
Date:   Fri Apr 29 17:00:53 2022 +0200
076f82
076f82
    dlfcn: Implement the RTLD_DI_PHDR request type for dlinfo
076f82
    
076f82
    The information is theoretically available via dl_iterate_phdr as
076f82
    well, but that approach is very slow if there are many shared
076f82
    objects.
076f82
    
076f82
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
076f82
    Tested-by: Carlos O'Donell <carlos@rehdat.com>
076f82
    (cherry picked from commit d056c212130280c0a54d9a4f72170ec621b70ce5)
076f82
076f82
diff --git a/dlfcn/Makefile b/dlfcn/Makefile
076f82
index 6bbfbb8344da05cb..d3965427dabed898 100644
076f82
--- a/dlfcn/Makefile
076f82
+++ b/dlfcn/Makefile
076f82
@@ -73,6 +73,10 @@ tststatic3-ENV = $(tststatic-ENV)
076f82
 tststatic4-ENV = $(tststatic-ENV)
076f82
 tststatic5-ENV = $(tststatic-ENV)
076f82
 
076f82
+tests-internal += \
076f82
+  tst-dlinfo-phdr \
076f82
+  # tests-internal
076f82
+
076f82
 ifneq (,$(CXX))
076f82
 modules-names += bug-atexit3-lib
076f82
 else
076f82
diff --git a/dlfcn/dlfcn.h b/dlfcn/dlfcn.h
076f82
index 4a3b870a487ea789..24388cfedae4dd67 100644
076f82
--- a/dlfcn/dlfcn.h
076f82
+++ b/dlfcn/dlfcn.h
076f82
@@ -162,7 +162,12 @@ enum
076f82
        segment, or if the calling thread has not allocated a block for it.  */
076f82
     RTLD_DI_TLS_DATA = 10,
076f82
 
076f82
-    RTLD_DI_MAX = 10
076f82
+    /* Treat ARG as const ElfW(Phdr) **, and store the address of the
076f82
+       program header array at that location.  The dlinfo call returns
076f82
+       the number of program headers in the array.  */
076f82
+    RTLD_DI_PHDR = 11,
076f82
+
076f82
+    RTLD_DI_MAX = 11
076f82
   };
076f82
 
076f82
 
076f82
diff --git a/dlfcn/dlinfo.c b/dlfcn/dlinfo.c
076f82
index 47d2daa96fa5986f..1842925fb7c594dd 100644
076f82
--- a/dlfcn/dlinfo.c
076f82
+++ b/dlfcn/dlinfo.c
076f82
@@ -28,6 +28,10 @@ struct dlinfo_args
076f82
   void *handle;
076f82
   int request;
076f82
   void *arg;
076f82
+
076f82
+  /* This is the value that is returned from dlinfo if no error is
076f82
+     signaled.  */
076f82
+  int result;
076f82
 };
076f82
 
076f82
 static void
076f82
@@ -40,6 +44,7 @@ dlinfo_doit (void *argsblock)
076f82
     {
076f82
     case RTLD_DI_CONFIGADDR:
076f82
     default:
076f82
+      args->result = -1;
076f82
       _dl_signal_error (0, NULL, NULL, N_("unsupported dlinfo request"));
076f82
       break;
076f82
 
076f82
@@ -75,6 +80,11 @@ dlinfo_doit (void *argsblock)
076f82
 	*(void **) args->arg = data;
076f82
 	break;
076f82
       }
076f82
+
076f82
+    case RTLD_DI_PHDR:
076f82
+      *(const ElfW(Phdr) **) args->arg = l->l_phdr;
076f82
+      args->result = l->l_phnum;
076f82
+      break;
076f82
     }
076f82
 }
076f82
 
076f82
@@ -82,7 +92,8 @@ static int
076f82
 dlinfo_implementation (void *handle, int request, void *arg)
076f82
 {
076f82
   struct dlinfo_args args = { handle, request, arg };
076f82
-  return _dlerror_run (&dlinfo_doit, &args) ? -1 : 0;
076f82
+  _dlerror_run (&dlinfo_doit, &args);
076f82
+  return args.result;
076f82
 }
076f82
 
076f82
 #ifdef SHARED
076f82
diff --git a/dlfcn/tst-dlinfo-phdr.c b/dlfcn/tst-dlinfo-phdr.c
076f82
new file mode 100644
076f82
index 0000000000000000..a15a7d48ebd3b976
076f82
--- /dev/null
076f82
+++ b/dlfcn/tst-dlinfo-phdr.c
076f82
@@ -0,0 +1,125 @@
076f82
+/* Test for dlinfo (RTLD_DI_PHDR).
076f82
+   Copyright (C) 2022 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#include <dlfcn.h>
076f82
+#include <link.h>
076f82
+#include <stdbool.h>
076f82
+#include <stdio.h>
076f82
+#include <string.h>
076f82
+#include <sys/auxv.h>
076f82
+
076f82
+#include <support/check.h>
076f82
+#include <support/xdlfcn.h>
076f82
+
076f82
+/* Used to verify that the program header array appears as expected
076f82
+   among the dl_iterate_phdr callback invocations.  */
076f82
+
076f82
+struct dlip_callback_args
076f82
+{
076f82
+  struct link_map *l;           /* l->l_addr is used to find the object.  */
076f82
+  const ElfW(Phdr) *phdr;       /* Expected program header pointed.  */
076f82
+  int phnum;                    /* Expected program header count.  */
076f82
+  bool found;                   /* True if l->l_addr has been found.  */
076f82
+};
076f82
+
076f82
+static int
076f82
+dlip_callback (struct dl_phdr_info *dlpi, size_t size, void *closure)
076f82
+{
076f82
+  TEST_COMPARE (sizeof (*dlpi), size);
076f82
+  struct dlip_callback_args *args = closure;
076f82
+
076f82
+  if (dlpi->dlpi_addr == args->l->l_addr)
076f82
+    {
076f82
+      TEST_VERIFY (!args->found);
076f82
+      args->found = true;
076f82
+      TEST_VERIFY (args->phdr == dlpi->dlpi_phdr);
076f82
+      TEST_COMPARE (args->phnum, dlpi->dlpi_phnum);
076f82
+    }
076f82
+
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+static int
076f82
+do_test (void)
076f82
+{
076f82
+  /* Avoid a copy relocation.  */
076f82
+  struct r_debug *debug = xdlsym (RTLD_DEFAULT, "_r_debug");
076f82
+  struct link_map *l = (struct link_map *) debug->r_map;
076f82
+  TEST_VERIFY_EXIT (l != NULL);
076f82
+
076f82
+  do
076f82
+    {
076f82
+      printf ("info: checking link map %p (%p) for \"%s\"\n",
076f82
+              l, l->l_phdr, l->l_name);
076f82
+
076f82
+      /* Cause dlerror () to return an error message.  */
076f82
+      dlsym (RTLD_DEFAULT, "does-not-exist");
076f82
+
076f82
+      /* Use the extension that link maps are valid dlopen handles.  */
076f82
+      const ElfW(Phdr) *phdr;
076f82
+      int phnum = dlinfo (l, RTLD_DI_PHDR, &phdr);
076f82
+      TEST_VERIFY (phnum >= 0);
076f82
+      /* Verify that the error message has been cleared.  */
076f82
+      TEST_COMPARE_STRING (dlerror (), NULL);
076f82
+
076f82
+      TEST_VERIFY (phdr == l->l_phdr);
076f82
+      TEST_COMPARE (phnum, l->l_phnum);
076f82
+
076f82
+      /* Check that we can find PT_DYNAMIC among the array.  */
076f82
+      {
076f82
+        bool dynamic_found = false;
076f82
+        for (int i = 0; i < phnum; ++i)
076f82
+          if (phdr[i].p_type == PT_DYNAMIC)
076f82
+            {
076f82
+              dynamic_found = true;
076f82
+              TEST_COMPARE ((ElfW(Addr)) l->l_ld, l->l_addr + phdr[i].p_vaddr);
076f82
+            }
076f82
+        TEST_VERIFY (dynamic_found);
076f82
+      }
076f82
+
076f82
+      /* Check that dl_iterate_phdr finds the link map with the same
076f82
+         program headers.  */
076f82
+      {
076f82
+        struct dlip_callback_args args =
076f82
+          {
076f82
+            .l =  l,
076f82
+            .phdr = phdr,
076f82
+            .phnum = phnum,
076f82
+            .found = false,
076f82
+          };
076f82
+        TEST_COMPARE (dl_iterate_phdr (dlip_callback, &args), 0);
076f82
+        TEST_VERIFY (args.found);
076f82
+      }
076f82
+
076f82
+      if (l->l_prev == NULL)
076f82
+        {
076f82
+          /* This is the executable, so the information is also
076f82
+             available via getauxval.  */
076f82
+          TEST_COMPARE_STRING (l->l_name, "");
076f82
+          TEST_VERIFY (phdr == (const ElfW(Phdr) *) getauxval (AT_PHDR));
076f82
+          TEST_COMPARE (phnum, getauxval (AT_PHNUM));
076f82
+        }
076f82
+
076f82
+      l = l->l_next;
076f82
+    }
076f82
+  while (l != NULL);
076f82
+
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+#include <support/test-driver.c>
076f82
diff --git a/manual/dynlink.texi b/manual/dynlink.texi
076f82
index dbf3de11769d8e57..7dcac64889e389fd 100644
076f82
--- a/manual/dynlink.texi
076f82
+++ b/manual/dynlink.texi
076f82
@@ -30,9 +30,9 @@ location @var{arg}, based on @var{request}.  The @var{handle} argument
076f82
 must be a pointer returned by @code{dlopen} or @code{dlmopen}; it must
076f82
 not have been closed by @code{dlclose}.
076f82
 
076f82
-On success, @code{dlinfo} returns 0.  If there is an error, the function
076f82
-returns @math{-1}, and @code{dlerror} can be used to obtain a
076f82
-corresponding error message.
076f82
+On success, @code{dlinfo} returns 0 for most request types; exceptions
076f82
+are noted below.  If there is an error, the function returns @math{-1},
076f82
+and @code{dlerror} can be used to obtain a corresponding error message.
076f82
 
076f82
 The following operations are defined for use with @var{request}:
076f82
 
076f82
@@ -84,6 +84,15 @@ This request writes the TLS module ID for the shared object @var{handle}
076f82
 to @code{*@var{arg}}.  The argument @var{arg} must be the address of an
076f82
 object of type @code{size_t}.  The module ID is zero if the object
076f82
 does not have an associated TLS block.
076f82
+
076f82
+@item RTLD_DI_PHDR
076f82
+This request writes the address of the program header array to
076f82
+@code{*@var{arg}}.  The argument @var{arg} must be the address of an
076f82
+object of type @code{const ElfW(Phdr) *} (that is,
076f82
+@code{const Elf32_Phdr *} or @code{const Elf64_Phdr *}, as appropriate
076f82
+for the current architecture).  For this request, the value returned by
076f82
+@code{dlinfo} is the number of program headers in the program header
076f82
+array.
076f82
 @end vtable
076f82
 
076f82
 The @code{dlinfo} function is a GNU extension.