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