bdc76f
This patch is a rework of the following upstream patch:
bdc76f
bdc76f
commit 1a4c27355e146b6d8cc6487b998462c7fdd1048f
bdc76f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
bdc76f
Date:   Thu Apr 11 18:12:00 2019 -0300
bdc76f
bdc76f
    elf: Fix pldd (BZ#18035)
bdc76f
bdc76f
    Since 9182aa67994 (Fix vDSO l_name for GDB's, BZ#387) the initial link_map
bdc76f
    for executable itself and loader will have both l_name and l_libname->name
bdc76f
    holding the same value due:
bdc76f
bdc76f
     elf/dl-object.c
bdc76f
bdc76f
     95   new->l_name = *realname ? realname : (char *) newname->name + libname_len - 1;
bdc76f
bdc76f
    Since newname->name points to new->l_libname->name.
bdc76f
bdc76f
    This leads to pldd to an infinite call at:
bdc76f
bdc76f
     elf/pldd-xx.c
bdc76f
bdc76f
    203     again:
bdc76f
    204       while (1)
bdc76f
    205         {
bdc76f
    206           ssize_t n = pread64 (memfd, tmpbuf.data, tmpbuf.length, name_offset);
bdc76f
bdc76f
    228           /* Try the l_libname element.  */
bdc76f
    229           struct E(libname_list) ln;
bdc76f
    230           if (pread64 (memfd, &ln, sizeof (ln), m.l_libname) == sizeof (ln))
bdc76f
    231             {
bdc76f
    232               name_offset = ln.name;
bdc76f
    233               goto again;
bdc76f
    234             }
bdc76f
bdc76f
    Since the value at ln.name (l_libname->name) will be the same as previously
bdc76f
    read. The straightforward fix is just avoid the check and read the new list
bdc76f
    entry.
bdc76f
bdc76f
    I checked also against binaries issues with old loaders with fix for BZ#387,
bdc76f
    and pldd could dump the shared objects.
bdc76f
bdc76f
    Checked on x86_64-linux-gnu, i686-linux-gnu, aarch64-linux-gnu, and
bdc76f
    powerpc64le-linux-gnu.
bdc76f
bdc76f
diff -Nrup a/elf/Makefile b/elf/Makefile
bdc76f
--- a/elf/Makefile	2019-05-17 12:35:12.663074766 -0400
bdc76f
+++ b/elf/Makefile	2019-05-17 12:35:45.816147975 -0400
bdc76f
@@ -201,6 +201,7 @@ tests-internal += loadtest unload unload
bdc76f
 	 neededtest neededtest2 neededtest3 neededtest4 \
bdc76f
 	 tst-tls3 tst-tls6 tst-tls7 tst-tls8 tst-dlmopen2 \
bdc76f
 	 tst-ptrguard1 tst-stackguard1 tst-libc_dlvsym
bdc76f
+tests-container += tst-pldd
bdc76f
 ifeq ($(build-hardcoded-path-in-tests),yes)
bdc76f
 tests += tst-dlopen-aout
bdc76f
 tst-dlopen-aout-no-pie = yes
bdc76f
diff -Nrup a/elf/pldd.c b/elf/pldd.c
bdc76f
--- a/elf/pldd.c	2018-08-01 01:10:47.000000000 -0400
bdc76f
+++ b/elf/pldd.c	2019-05-17 12:35:45.817147947 -0400
bdc76f
@@ -17,23 +17,17 @@
bdc76f
    License along with the GNU C Library; if not, see
bdc76f
    <http://www.gnu.org/licenses/>.  */
bdc76f
 
bdc76f
-#include <alloca.h>
bdc76f
+#define _FILE_OFFSET_BITS 64
bdc76f
+
bdc76f
 #include <argp.h>
bdc76f
-#include <assert.h>
bdc76f
 #include <dirent.h>
bdc76f
-#include <elf.h>
bdc76f
-#include <errno.h>
bdc76f
 #include <error.h>
bdc76f
 #include <fcntl.h>
bdc76f
 #include <libintl.h>
bdc76f
-#include <link.h>
bdc76f
-#include <stddef.h>
bdc76f
 #include <stdio.h>
bdc76f
 #include <stdlib.h>
bdc76f
-#include <string.h>
bdc76f
 #include <unistd.h>
bdc76f
 #include <sys/ptrace.h>
bdc76f
-#include <sys/stat.h>
bdc76f
 #include <sys/wait.h>
bdc76f
 #include <scratch_buffer.h>
bdc76f
 
bdc76f
@@ -76,14 +70,8 @@ static struct argp argp =
bdc76f
   options, parse_opt, args_doc, doc, NULL, more_help, NULL
bdc76f
 };
bdc76f
 
bdc76f
-// File descriptor of /proc/*/mem file.
bdc76f
-static int memfd;
bdc76f
-
bdc76f
-/* Name of the executable  */
bdc76f
-static char *exe;
bdc76f
-
bdc76f
 /* Local functions.  */
bdc76f
-static int get_process_info (int dfd, long int pid);
bdc76f
+static int get_process_info (const char *exe, int dfd, long int pid);
bdc76f
 static void wait_for_ptrace_stop (long int pid);
bdc76f
 
bdc76f
 
bdc76f
@@ -102,8 +90,10 @@ main (int argc, char *argv[])
bdc76f
       return 1;
bdc76f
     }
bdc76f
 
bdc76f
-  assert (sizeof (pid_t) == sizeof (int)
bdc76f
-	  || sizeof (pid_t) == sizeof (long int));
bdc76f
+  _Static_assert (sizeof (pid_t) == sizeof (int)
bdc76f
+                 || sizeof (pid_t) == sizeof (long int),
bdc76f
+                 "sizeof (pid_t) != sizeof (int) or sizeof (long int)");
bdc76f
+
bdc76f
   char *endp;
bdc76f
   errno = 0;
bdc76f
   long int pid = strtol (argv[remaining], &endp, 10);
bdc76f
@@ -119,25 +109,24 @@ main (int argc, char *argv[])
bdc76f
   if (dfd == -1)
bdc76f
     error (EXIT_FAILURE, errno, gettext ("cannot open %s"), buf);
bdc76f
 
bdc76f
-  struct scratch_buffer exebuf;
bdc76f
-  scratch_buffer_init (&exebuf);
bdc76f
+  /* Name of the executable  */
bdc76f
+  struct scratch_buffer exe;
bdc76f
+  scratch_buffer_init (&exe;;
bdc76f
   ssize_t nexe;
bdc76f
   while ((nexe = readlinkat (dfd, "exe",
bdc76f
-			     exebuf.data, exebuf.length)) == exebuf.length)
bdc76f
+                            exe.data, exe.length)) == exe.length)
bdc76f
     {
bdc76f
-      if (!scratch_buffer_grow (&exebuf))
bdc76f
+      if (!scratch_buffer_grow (&exe))
bdc76f
 	{
bdc76f
 	  nexe = -1;
bdc76f
 	  break;
bdc76f
 	}
bdc76f
     }
bdc76f
   if (nexe == -1)
bdc76f
-    exe = (char *) "<program name undetermined>";
bdc76f
+    /* Default stack allocation is at least 1024.  */
bdc76f
+    snprintf (exe.data, exe.length, "<program name undetermined>");
bdc76f
   else
bdc76f
-    {
bdc76f
-      exe = exebuf.data;
bdc76f
-      exe[nexe] = '\0';
bdc76f
-    }
bdc76f
+    ((char*)exe.data)[nexe] = '\0';
bdc76f
 
bdc76f
   /* Stop all threads since otherwise the list of loaded modules might
bdc76f
      change while we are reading it.  */
bdc76f
@@ -155,8 +144,8 @@ main (int argc, char *argv[])
bdc76f
     error (EXIT_FAILURE, errno, gettext ("cannot prepare reading %s/task"),
bdc76f
 	   buf);
bdc76f
 
bdc76f
-  struct dirent64 *d;
bdc76f
-  while ((d = readdir64 (dir)) != NULL)
bdc76f
+  struct dirent *d;
bdc76f
+  while ((d = readdir (dir)) != NULL)
bdc76f
     {
bdc76f
       if (! isdigit (d->d_name[0]))
bdc76f
 	continue;
bdc76f
@@ -182,7 +171,7 @@ main (int argc, char *argv[])
bdc76f
 
bdc76f
       wait_for_ptrace_stop (tid);
bdc76f
 
bdc76f
-      struct thread_list *newp = alloca (sizeof (*newp));
bdc76f
+      struct thread_list *newp = xmalloc (sizeof (*newp));
bdc76f
       newp->tid = tid;
bdc76f
       newp->next = thread_list;
bdc76f
       thread_list = newp;
bdc76f
@@ -190,17 +179,22 @@ main (int argc, char *argv[])
bdc76f
 
bdc76f
   closedir (dir);
bdc76f
 
bdc76f
-  int status = get_process_info (dfd, pid);
bdc76f
+  if (thread_list == NULL)
bdc76f
+    error (EXIT_FAILURE, 0, gettext ("no valid %s/task entries"), buf);
bdc76f
+
bdc76f
+  int status = get_process_info (exe.data, dfd, pid);
bdc76f
 
bdc76f
-  assert (thread_list != NULL);
bdc76f
   do
bdc76f
     {
bdc76f
       ptrace (PTRACE_DETACH, thread_list->tid, NULL, NULL);
bdc76f
+      struct thread_list *prev = thread_list;
bdc76f
       thread_list = thread_list->next;
bdc76f
+      free (prev);
bdc76f
     }
bdc76f
   while (thread_list != NULL);
bdc76f
 
bdc76f
   close (dfd);
bdc76f
+  scratch_buffer_free (&exe;;
bdc76f
 
bdc76f
   return status;
bdc76f
 }
bdc76f
@@ -281,9 +275,10 @@ warranty; not even for MERCHANTABILITY o
bdc76f
 
bdc76f
 
bdc76f
 static int
bdc76f
-get_process_info (int dfd, long int pid)
bdc76f
+get_process_info (const char *exe, int dfd, long int pid)
bdc76f
 {
bdc76f
-  memfd = openat (dfd, "mem", O_RDONLY);
bdc76f
+  /* File descriptor of /proc/<pid>/mem file.  */
bdc76f
+  int memfd = openat (dfd, "mem", O_RDONLY);
bdc76f
   if (memfd == -1)
bdc76f
     goto no_info;
bdc76f
 
bdc76f
@@ -333,9 +328,9 @@ get_process_info (int dfd, long int pid)
bdc76f
 
bdc76f
   int retval;
bdc76f
   if (e_ident[EI_CLASS] == ELFCLASS32)
bdc76f
-    retval = find_maps32 (pid, auxv, auxv_size);
bdc76f
+    retval = find_maps32 (exe, memfd, pid, auxv, auxv_size);
bdc76f
   else
bdc76f
-    retval = find_maps64 (pid, auxv, auxv_size);
bdc76f
+    retval = find_maps64 (exe, memfd, pid, auxv, auxv_size);
bdc76f
 
bdc76f
   free (auxv);
bdc76f
   close (memfd);
bdc76f
diff -Nrup a/elf/pldd-xx.c b/elf/pldd-xx.c
bdc76f
--- a/elf/pldd-xx.c	2018-08-01 01:10:47.000000000 -0400
bdc76f
+++ b/elf/pldd-xx.c	2019-05-17 13:05:29.587147445 -0400
bdc76f
@@ -23,10 +23,6 @@
bdc76f
 #define EW_(e, w, t) EW__(e, w, _##t)
bdc76f
 #define EW__(e, w, t) e##w##t
bdc76f
 
bdc76f
-#define pldd_assert(name, exp) \
bdc76f
-  typedef int __assert_##name[((exp) != 0) - 1]
bdc76f
-
bdc76f
-
bdc76f
 struct E(link_map)
bdc76f
 {
bdc76f
   EW(Addr) l_addr;
bdc76f
@@ -39,12 +35,12 @@ struct E(link_map)
bdc76f
   EW(Addr) l_libname;
bdc76f
 };
bdc76f
 #if CLASS == __ELF_NATIVE_CLASS
bdc76f
-pldd_assert (l_addr, (offsetof (struct link_map, l_addr)
bdc76f
-			== offsetof (struct E(link_map), l_addr)));
bdc76f
-pldd_assert (l_name, (offsetof (struct link_map, l_name)
bdc76f
-			== offsetof (struct E(link_map), l_name)));
bdc76f
-pldd_assert (l_next, (offsetof (struct link_map, l_next)
bdc76f
-			== offsetof (struct E(link_map), l_next)));
bdc76f
+_Static_assert (offsetof (struct link_map, l_addr)
bdc76f
+               == offsetof (struct E(link_map), l_addr), "l_addr");
bdc76f
+_Static_assert (offsetof (struct link_map, l_name)
bdc76f
+               == offsetof (struct E(link_map), l_name), "l_name");
bdc76f
+_Static_assert (offsetof (struct link_map, l_next)
bdc76f
+               == offsetof (struct E(link_map), l_next), "l_next");
bdc76f
 #endif
bdc76f
 
bdc76f
 
bdc76f
@@ -54,10 +50,10 @@ struct E(libname_list)
bdc76f
   EW(Addr) next;
bdc76f
 };
bdc76f
 #if CLASS == __ELF_NATIVE_CLASS
bdc76f
-pldd_assert (name, (offsetof (struct libname_list, name)
bdc76f
-		      == offsetof (struct E(libname_list), name)));
bdc76f
-pldd_assert (next, (offsetof (struct libname_list, next)
bdc76f
-		      == offsetof (struct E(libname_list), next)));
bdc76f
+_Static_assert (offsetof (struct libname_list, name)
bdc76f
+               == offsetof (struct E(libname_list), name), "name");
bdc76f
+_Static_assert (offsetof (struct libname_list, next)
bdc76f
+               == offsetof (struct E(libname_list), next), "next");
bdc76f
 #endif
bdc76f
 
bdc76f
 struct E(r_debug)
bdc76f
@@ -69,16 +65,17 @@ struct E(r_debug)
bdc76f
   EW(Addr) r_map;
bdc76f
 };
bdc76f
 #if CLASS == __ELF_NATIVE_CLASS
bdc76f
-pldd_assert (r_version, (offsetof (struct r_debug, r_version)
bdc76f
-			   == offsetof (struct E(r_debug), r_version)));
bdc76f
-pldd_assert (r_map, (offsetof (struct r_debug, r_map)
bdc76f
-		       == offsetof (struct E(r_debug), r_map)));
bdc76f
+_Static_assert (offsetof (struct r_debug, r_version)
bdc76f
+               == offsetof (struct E(r_debug), r_version), "r_version");
bdc76f
+_Static_assert (offsetof (struct r_debug, r_map)
bdc76f
+               == offsetof (struct E(r_debug), r_map), "r_map");
bdc76f
 #endif
bdc76f
 
bdc76f
 
bdc76f
 static int
bdc76f
 
bdc76f
-E(find_maps) (pid_t pid, void *auxv, size_t auxv_size)
bdc76f
+E(find_maps) (const char *exe, int memfd, pid_t pid, void *auxv,
bdc76f
+             size_t auxv_size)
bdc76f
 {
bdc76f
   EW(Addr) phdr = 0;
bdc76f
   unsigned int phnum = 0;
bdc76f
@@ -104,12 +101,9 @@ E(find_maps) (pid_t pid, void *auxv, siz
bdc76f
   if (phdr == 0 || phnum == 0 || phent == 0)
bdc76f
     error (EXIT_FAILURE, 0, gettext ("cannot find program header of process"));
bdc76f
 
bdc76f
-  EW(Phdr) *p = alloca (phnum * phent);
bdc76f
-  if (pread64 (memfd, p, phnum * phent, phdr) != phnum * phent)
bdc76f
-    {
bdc76f
-      error (0, 0, gettext ("cannot read program header"));
bdc76f
-      return EXIT_FAILURE;
bdc76f
-    }
bdc76f
+  EW(Phdr) *p = xmalloc (phnum * phent);
bdc76f
+  if (pread (memfd, p, phnum * phent, phdr) != phnum * phent)
bdc76f
+    error (EXIT_FAILURE, 0, gettext ("cannot read program header"));
bdc76f
 
bdc76f
   /* Determine the load offset.  We need this for interpreting the
bdc76f
      other program header entries so we do this in a separate loop.
bdc76f
@@ -129,24 +123,18 @@ E(find_maps) (pid_t pid, void *auxv, siz
bdc76f
     if (p[i].p_type == PT_DYNAMIC)
bdc76f
       {
bdc76f
 	EW(Dyn) *dyn = xmalloc (p[i].p_filesz);
bdc76f
-	if (pread64 (memfd, dyn, p[i].p_filesz, offset + p[i].p_vaddr)
bdc76f
+       if (pread (memfd, dyn, p[i].p_filesz, offset + p[i].p_vaddr)
bdc76f
 	    != p[i].p_filesz)
bdc76f
-	  {
bdc76f
-	    error (0, 0, gettext ("cannot read dynamic section"));
bdc76f
-	    return EXIT_FAILURE;
bdc76f
-	  }
bdc76f
+         error (EXIT_FAILURE, 0, gettext ("cannot read dynamic section"));
bdc76f
 
bdc76f
 	/* Search for the DT_DEBUG entry.  */
bdc76f
 	for (unsigned int j = 0; j < p[i].p_filesz / sizeof (EW(Dyn)); ++j)
bdc76f
 	  if (dyn[j].d_tag == DT_DEBUG && dyn[j].d_un.d_ptr != 0)
bdc76f
 	    {
bdc76f
 	      struct E(r_debug) r;
bdc76f
-	      if (pread64 (memfd, &r, sizeof (r), dyn[j].d_un.d_ptr)
bdc76f
+             if (pread (memfd, &r, sizeof (r), dyn[j].d_un.d_ptr)
bdc76f
 		  != sizeof (r))
bdc76f
-		{
bdc76f
-		  error (0, 0, gettext ("cannot read r_debug"));
bdc76f
-		  return EXIT_FAILURE;
bdc76f
-		}
bdc76f
+               error (EXIT_FAILURE, 0, gettext ("cannot read r_debug"));
bdc76f
 
bdc76f
 	      if (r.r_map != 0)
bdc76f
 		{
bdc76f
@@ -160,13 +148,10 @@ E(find_maps) (pid_t pid, void *auxv, siz
bdc76f
       }
bdc76f
     else if (p[i].p_type == PT_INTERP)
bdc76f
       {
bdc76f
-	interp = alloca (p[i].p_filesz);
bdc76f
-	if (pread64 (memfd, interp, p[i].p_filesz, offset + p[i].p_vaddr)
bdc76f
+       interp = xmalloc (p[i].p_filesz);
bdc76f
+       if (pread (memfd, interp, p[i].p_filesz, offset + p[i].p_vaddr)
bdc76f
 	    != p[i].p_filesz)
bdc76f
-	  {
bdc76f
-	    error (0, 0, gettext ("cannot read program interpreter"));
bdc76f
-	    return EXIT_FAILURE;
bdc76f
-	  }
bdc76f
+         error (EXIT_FAILURE, 0, gettext ("cannot read program interpreter"));
bdc76f
       }
bdc76f
 
bdc76f
   if (list == 0)
bdc76f
@@ -174,14 +159,16 @@ E(find_maps) (pid_t pid, void *auxv, siz
bdc76f
       if (interp == NULL)
bdc76f
 	{
bdc76f
 	  // XXX check whether the executable itself is the loader
bdc76f
-	  return EXIT_FAILURE;
bdc76f
+         exit (EXIT_FAILURE);
bdc76f
 	}
bdc76f
 
bdc76f
       // XXX perhaps try finding ld.so and _r_debug in it
bdc76f
-
bdc76f
-      return EXIT_FAILURE;
bdc76f
+      exit (EXIT_FAILURE);
bdc76f
     }
bdc76f
 
bdc76f
+  free (p);
bdc76f
+  free (interp);
bdc76f
+
bdc76f
   /* Print the PID and program name first.  */
bdc76f
   printf ("%lu:\t%s\n", (unsigned long int) pid, exe);
bdc76f
 
bdc76f
@@ -192,47 +179,27 @@ E(find_maps) (pid_t pid, void *auxv, siz
bdc76f
   do
bdc76f
     {
bdc76f
       struct E(link_map) m;
bdc76f
-      if (pread64 (memfd, &m, sizeof (m), list) != sizeof (m))
bdc76f
-	{
bdc76f
-	  error (0, 0, gettext ("cannot read link map"));
bdc76f
-	  status = EXIT_FAILURE;
bdc76f
-	  goto out;
bdc76f
-	}
bdc76f
+      if (pread (memfd, &m, sizeof (m), list) != sizeof (m))
bdc76f
+       error (EXIT_FAILURE, 0, gettext ("cannot read link map"));
bdc76f
 
bdc76f
       EW(Addr) name_offset = m.l_name;
bdc76f
-    again:
bdc76f
       while (1)
bdc76f
 	{
bdc76f
-	  ssize_t n = pread64 (memfd, tmpbuf.data, tmpbuf.length, name_offset);
bdc76f
+         ssize_t n = pread (memfd, tmpbuf.data, tmpbuf.length, name_offset);
bdc76f
 	  if (n == -1)
bdc76f
-	    {
bdc76f
-	      error (0, 0, gettext ("cannot read object name"));
bdc76f
-	      status = EXIT_FAILURE;
bdc76f
-	      goto out;
bdc76f
-	    }
bdc76f
+           error (EXIT_FAILURE, 0, gettext ("cannot read object name"));
bdc76f
 
bdc76f
 	  if (memchr (tmpbuf.data, '\0', n) != NULL)
bdc76f
 	    break;
bdc76f
 
bdc76f
 	  if (!scratch_buffer_grow (&tmpbuf))
bdc76f
-	    {
bdc76f
-	      error (0, 0, gettext ("cannot allocate buffer for object name"));
bdc76f
-	      status = EXIT_FAILURE;
bdc76f
-	      goto out;
bdc76f
-	    }
bdc76f
+           error (EXIT_FAILURE, 0,
bdc76f
+                  gettext ("cannot allocate buffer for object name"));
bdc76f
 	}
bdc76f
 
bdc76f
-      if (((char *)tmpbuf.data)[0] == '\0' && name_offset == m.l_name
bdc76f
-	  && m.l_libname != 0)
bdc76f
-	{
bdc76f
-	  /* Try the l_libname element.  */
bdc76f
-	  struct E(libname_list) ln;
bdc76f
-	  if (pread64 (memfd, &ln, sizeof (ln), m.l_libname) == sizeof (ln))
bdc76f
-	    {
bdc76f
-	      name_offset = ln.name;
bdc76f
-	      goto again;
bdc76f
-	    }
bdc76f
-	}
bdc76f
+      /* The m.l_name and m.l_libname.name for loader linkmap points to same
bdc76f
+        values (since BZ#387 fix).  Trying to use l_libname name as the
bdc76f
+        shared object name might lead to an infinite loop (BZ#18035).  */
bdc76f
 
bdc76f
       /* Skip over the executable.  */
bdc76f
       if (((char *)tmpbuf.data)[0] != '\0')
bdc76f
@@ -242,7 +209,6 @@ E(find_maps) (pid_t pid, void *auxv, siz
bdc76f
     }
bdc76f
   while (list != 0);
bdc76f
 
bdc76f
- out:
bdc76f
   scratch_buffer_free (&tmpbuf);
bdc76f
   return status;
bdc76f
 }
bdc76f
diff -Nrup a/elf/tst-pldd.c b/elf/tst-pldd.c
bdc76f
--- a/elf/tst-pldd.c	1969-12-31 19:00:00.000000000 -0500
bdc76f
+++ b/elf/tst-pldd.c	2019-05-17 12:35:45.817147947 -0400
bdc76f
@@ -0,0 +1,118 @@
bdc76f
+/* Basic tests for pldd program.
bdc76f
+   Copyright (C) 2019 Free Software Foundation, Inc.
bdc76f
+   This file is part of the GNU C Library.
bdc76f
+
bdc76f
+   The GNU C Library is free software; you can redistribute it and/or
bdc76f
+   modify it under the terms of the GNU Lesser General Public
bdc76f
+   License as published by the Free Software Foundation; either
bdc76f
+   version 2.1 of the License, or (at your option) any later version.
bdc76f
+
bdc76f
+   The GNU C Library is distributed in the hope that it will be useful,
bdc76f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bdc76f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bdc76f
+   Lesser General Public License for more details.
bdc76f
+
bdc76f
+   You should have received a copy of the GNU Lesser General Public
bdc76f
+   License along with the GNU C Library; if not, see
bdc76f
+   <http://www.gnu.org/licenses/>.  */
bdc76f
+
bdc76f
+#include <stdio.h>
bdc76f
+#include <string.h>
bdc76f
+#include <unistd.h>
bdc76f
+#include <stdint.h>
bdc76f
+#include <libgen.h>
bdc76f
+#include <stdbool.h>
bdc76f
+
bdc76f
+#include <array_length.h>
bdc76f
+#include <gnu/lib-names.h>
bdc76f
+
bdc76f
+#include <support/subprocess.h>
bdc76f
+#include <support/capture_subprocess.h>
bdc76f
+#include <support/check.h>
bdc76f
+
bdc76f
+static void
bdc76f
+target_process (void *arg)
bdc76f
+{
bdc76f
+  pause ();
bdc76f
+}
bdc76f
+
bdc76f
+/* The test runs in a container because pldd does not support tracing
bdc76f
+   a binary started by the loader iself (as with testrun.sh).  */
bdc76f
+
bdc76f
+static int
bdc76f
+do_test (void)
bdc76f
+{
bdc76f
+  /* Create a copy of current test to check with pldd.  */
bdc76f
+  struct support_subprocess target = support_subprocess (target_process, NULL);
bdc76f
+
bdc76f
+  /* Run 'pldd' on test subprocess.  */
bdc76f
+  struct support_capture_subprocess pldd;
bdc76f
+  {
bdc76f
+    /* Three digits per byte plus null terminator.  */
bdc76f
+    char pid[3 * sizeof (uint32_t) + 1];
bdc76f
+    snprintf (pid, array_length (pid), "%d", target.pid);
bdc76f
+
bdc76f
+    const char prog[] = "/usr/bin/pldd";
bdc76f
+
bdc76f
+    pldd = support_capture_subprogram (prog,
bdc76f
+      (char *const []) { (char *) prog, pid, NULL });
bdc76f
+
bdc76f
+    support_capture_subprocess_check (&pldd, "pldd", 0, sc_allow_stdout);
bdc76f
+  }
bdc76f
+
bdc76f
+  /* Check 'pldd' output.  The test is expected to be linked against only
bdc76f
+     loader and libc.  */
bdc76f
+  {
bdc76f
+    pid_t pid;
bdc76f
+    char buffer[512];
bdc76f
+#define STRINPUT(size) "%" # size "s"
bdc76f
+
bdc76f
+    FILE *out = fmemopen (pldd.out.buffer, pldd.out.length, "r");
bdc76f
+    TEST_VERIFY (out != NULL);
bdc76f
+
bdc76f
+    /* First line is in the form of <pid>: <full path of executable>  */
bdc76f
+    TEST_COMPARE (fscanf (out, "%u: " STRINPUT (512), &pid, buffer), 2);
bdc76f
+
bdc76f
+    TEST_COMPARE (pid, target.pid);
bdc76f
+    TEST_COMPARE (strcmp (basename (buffer), "tst-pldd"), 0);
bdc76f
+
bdc76f
+    /* It expects only one loader and libc loaded by the program.  */
bdc76f
+    bool interpreter_found = false, libc_found = false;
bdc76f
+    while (fgets (buffer, array_length (buffer), out) != NULL)
bdc76f
+      {
bdc76f
+       /* Ignore vDSO.  */
bdc76f
+        if (buffer[0] != '/')
bdc76f
+         continue;
bdc76f
+
bdc76f
+       /* Remove newline so baseline (buffer) can compare against the
bdc76f
+          LD_SO and LIBC_SO macros unmodified.  */
bdc76f
+       if (buffer[strlen(buffer)-1] == '\n')
bdc76f
+         buffer[strlen(buffer)-1] = '\0';
bdc76f
+
bdc76f
+       if (strcmp (basename (buffer), LD_SO) == 0)
bdc76f
+         {
bdc76f
+           TEST_COMPARE (interpreter_found, false);
bdc76f
+           interpreter_found = true;
bdc76f
+           continue;
bdc76f
+         }
bdc76f
+
bdc76f
+       if (strcmp (basename (buffer), LIBC_SO) == 0)
bdc76f
+         {
bdc76f
+           TEST_COMPARE (libc_found, false);
bdc76f
+           libc_found = true;
bdc76f
+           continue;
bdc76f
+         }
bdc76f
+      }
bdc76f
+    TEST_COMPARE (interpreter_found, true);
bdc76f
+    TEST_COMPARE (libc_found, true);
bdc76f
+
bdc76f
+    fclose (out);
bdc76f
+  }
bdc76f
+
bdc76f
+  support_capture_subprocess_free (&pldd);
bdc76f
+  support_process_terminate (&target);
bdc76f
+
bdc76f
+  return 0;
bdc76f
+}
bdc76f
+
bdc76f
+#include <support/test-driver.c>