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