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