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