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