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