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