Blame elf/pldd-xx.c

Packit 6c4009
/* Copyright (C) 2011-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#define E(name) E_(name, CLASS)
Packit 6c4009
#define E_(name, cl) E__(name, cl)
Packit 6c4009
#define E__(name, cl) name##cl
Packit 6c4009
#define EW(type) EW_(Elf, CLASS, type)
Packit 6c4009
#define EW_(e, w, t) EW__(e, w, _##t)
Packit 6c4009
#define EW__(e, w, t) e##w##t
Packit 6c4009
Packit 6c4009
struct E(link_map)
Packit 6c4009
{
Packit 6c4009
  EW(Addr) l_addr;
Packit 6c4009
  EW(Addr) l_name;
Packit 6c4009
  EW(Addr) l_ld;
Packit 6c4009
  EW(Addr) l_next;
Packit 6c4009
  EW(Addr) l_prev;
Packit 6c4009
  EW(Addr) l_real;
Packit 6c4009
  Lmid_t l_ns;
Packit 6c4009
  EW(Addr) l_libname;
Packit 6c4009
};
Packit 6c4009
#if CLASS == __ELF_NATIVE_CLASS
Packit Service 2725fa
_Static_assert (offsetof (struct link_map, l_addr)
Packit Service 2725fa
               == offsetof (struct E(link_map), l_addr), "l_addr");
Packit Service 2725fa
_Static_assert (offsetof (struct link_map, l_name)
Packit Service 2725fa
               == offsetof (struct E(link_map), l_name), "l_name");
Packit Service 2725fa
_Static_assert (offsetof (struct link_map, l_next)
Packit Service 2725fa
               == offsetof (struct E(link_map), l_next), "l_next");
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
struct E(libname_list)
Packit 6c4009
{
Packit 6c4009
  EW(Addr) name;
Packit 6c4009
  EW(Addr) next;
Packit 6c4009
};
Packit 6c4009
#if CLASS == __ELF_NATIVE_CLASS
Packit Service 2725fa
_Static_assert (offsetof (struct libname_list, name)
Packit Service 2725fa
               == offsetof (struct E(libname_list), name), "name");
Packit Service 2725fa
_Static_assert (offsetof (struct libname_list, next)
Packit Service 2725fa
               == offsetof (struct E(libname_list), next), "next");
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
struct E(r_debug)
Packit 6c4009
{
Packit 6c4009
  int r_version;
Packit 6c4009
#if CLASS == 64
Packit 6c4009
  int pad;
Packit 6c4009
#endif
Packit 6c4009
  EW(Addr) r_map;
Packit 6c4009
};
Packit 6c4009
#if CLASS == __ELF_NATIVE_CLASS
Packit Service 2725fa
_Static_assert (offsetof (struct r_debug, r_version)
Packit Service 2725fa
               == offsetof (struct E(r_debug), r_version), "r_version");
Packit Service 2725fa
_Static_assert (offsetof (struct r_debug, r_map)
Packit Service 2725fa
               == offsetof (struct E(r_debug), r_map), "r_map");
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
Packit Service 2725fa
E(find_maps) (const char *exe, int memfd, pid_t pid, void *auxv,
Packit Service 2725fa
             size_t auxv_size)
Packit 6c4009
{
Packit 6c4009
  EW(Addr) phdr = 0;
Packit 6c4009
  unsigned int phnum = 0;
Packit 6c4009
  unsigned int phent = 0;
Packit 6c4009
Packit 6c4009
  EW(auxv_t) *auxvXX = (EW(auxv_t) *) auxv;
Packit 6c4009
  for (int i = 0; i < auxv_size / sizeof (EW(auxv_t)); ++i)
Packit 6c4009
    switch (auxvXX[i].a_type)
Packit 6c4009
      {
Packit 6c4009
      case AT_PHDR:
Packit 6c4009
	phdr = auxvXX[i].a_un.a_val;
Packit 6c4009
	break;
Packit 6c4009
      case AT_PHNUM:
Packit 6c4009
	phnum = auxvXX[i].a_un.a_val;
Packit 6c4009
	break;
Packit 6c4009
      case AT_PHENT:
Packit 6c4009
	phent = auxvXX[i].a_un.a_val;
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  if (phdr == 0 || phnum == 0 || phent == 0)
Packit 6c4009
    error (EXIT_FAILURE, 0, gettext ("cannot find program header of process"));
Packit 6c4009
Packit Service 2725fa
  EW(Phdr) *p = xmalloc (phnum * phent);
Packit Service 2725fa
  if (pread (memfd, p, phnum * phent, phdr) != phnum * phent)
Packit Service 2725fa
    error (EXIT_FAILURE, 0, gettext ("cannot read program header"));
Packit 6c4009
Packit 6c4009
  /* Determine the load offset.  We need this for interpreting the
Packit 6c4009
     other program header entries so we do this in a separate loop.
Packit 6c4009
     Fortunately it is the first time unless someone does something
Packit 6c4009
     stupid when linking the application.  */
Packit 6c4009
  EW(Addr) offset = 0;
Packit 6c4009
  for (unsigned int i = 0; i < phnum; ++i)
Packit 6c4009
    if (p[i].p_type == PT_PHDR)
Packit 6c4009
      {
Packit 6c4009
	offset = phdr - p[i].p_vaddr;
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  EW(Addr) list = 0;
Packit 6c4009
  char *interp = NULL;
Packit 6c4009
  for (unsigned int i = 0; i < phnum; ++i)
Packit 6c4009
    if (p[i].p_type == PT_DYNAMIC)
Packit 6c4009
      {
Packit 6c4009
	EW(Dyn) *dyn = xmalloc (p[i].p_filesz);
Packit Service 2725fa
       if (pread (memfd, dyn, p[i].p_filesz, offset + p[i].p_vaddr)
Packit 6c4009
	    != p[i].p_filesz)
Packit Service 2725fa
         error (EXIT_FAILURE, 0, gettext ("cannot read dynamic section"));
Packit 6c4009
Packit 6c4009
	/* Search for the DT_DEBUG entry.  */
Packit 6c4009
	for (unsigned int j = 0; j < p[i].p_filesz / sizeof (EW(Dyn)); ++j)
Packit 6c4009
	  if (dyn[j].d_tag == DT_DEBUG && dyn[j].d_un.d_ptr != 0)
Packit 6c4009
	    {
Packit 6c4009
	      struct E(r_debug) r;
Packit Service 2725fa
             if (pread (memfd, &r, sizeof (r), dyn[j].d_un.d_ptr)
Packit 6c4009
		  != sizeof (r))
Packit Service 2725fa
               error (EXIT_FAILURE, 0, gettext ("cannot read r_debug"));
Packit 6c4009
Packit 6c4009
	      if (r.r_map != 0)
Packit 6c4009
		{
Packit 6c4009
		  list = r.r_map;
Packit 6c4009
		  break;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	free (dyn);
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
    else if (p[i].p_type == PT_INTERP)
Packit 6c4009
      {
Packit Service 2725fa
       interp = xmalloc (p[i].p_filesz);
Packit Service 2725fa
       if (pread (memfd, interp, p[i].p_filesz, offset + p[i].p_vaddr)
Packit 6c4009
	    != p[i].p_filesz)
Packit Service 2725fa
         error (EXIT_FAILURE, 0, gettext ("cannot read program interpreter"));
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  if (list == 0)
Packit 6c4009
    {
Packit 6c4009
      if (interp == NULL)
Packit 6c4009
	{
Packit 6c4009
	  // XXX check whether the executable itself is the loader
Packit Service 2725fa
         exit (EXIT_FAILURE);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      // XXX perhaps try finding ld.so and _r_debug in it
Packit Service 2725fa
      exit (EXIT_FAILURE);
Packit Service 6de65a
    }
Packit Service e8569b
Packit Service 2725fa
  free (p);
Packit Service 2725fa
  free (interp);
Packit Service 2725fa
Packit 6c4009
  /* Print the PID and program name first.  */
Packit 6c4009
  printf ("%lu:\t%s\n", (unsigned long int) pid, exe);
Packit 6c4009
Packit 6c4009
  /* Iterate over the list of objects and print the information.  */
Packit 6c4009
  struct scratch_buffer tmpbuf;
Packit 6c4009
  scratch_buffer_init (&tmpbuf);
Packit 6c4009
  int status = 0;
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      struct E(link_map) m;
Packit Service 2725fa
      if (pread (memfd, &m, sizeof (m), list) != sizeof (m))
Packit Service 2725fa
       error (EXIT_FAILURE, 0, gettext ("cannot read link map"));
Packit 6c4009
Packit 6c4009
      EW(Addr) name_offset = m.l_name;
Packit 6c4009
      while (1)
Packit 6c4009
	{
Packit Service 2725fa
         ssize_t n = pread (memfd, tmpbuf.data, tmpbuf.length, name_offset);
Packit 6c4009
	  if (n == -1)
Packit Service 2725fa
           error (EXIT_FAILURE, 0, gettext ("cannot read object name"));
Packit 6c4009
Packit 6c4009
	  if (memchr (tmpbuf.data, '\0', n) != NULL)
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  if (!scratch_buffer_grow (&tmpbuf))
Packit Service 2725fa
           error (EXIT_FAILURE, 0,
Packit Service 2725fa
                  gettext ("cannot allocate buffer for object name"));
Packit 6c4009
	}
Packit 6c4009
Packit Service 2725fa
      /* The m.l_name and m.l_libname.name for loader linkmap points to same
Packit Service 2725fa
        values (since BZ#387 fix).  Trying to use l_libname name as the
Packit Service 2725fa
        shared object name might lead to an infinite loop (BZ#18035).  */
Packit 6c4009
Packit 6c4009
      /* Skip over the executable.  */
Packit 6c4009
      if (((char *)tmpbuf.data)[0] != '\0')
Packit 6c4009
	printf ("%s\n", (char *)tmpbuf.data);
Packit 6c4009
Packit 6c4009
      list = m.l_next;
Packit 6c4009
    }
Packit 6c4009
  while (list != 0);
Packit 6c4009
Packit 6c4009
  scratch_buffer_free (&tmpbuf);
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#undef CLASS