1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/* Copyright (C) 2011-2018 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#define E(name) E_(name, CLASS)
#define E_(name, cl) E__(name, cl)
#define E__(name, cl) name##cl
#define EW(type) EW_(Elf, CLASS, type)
#define EW_(e, w, t) EW__(e, w, _##t)
#define EW__(e, w, t) e##w##t
#define pldd_assert(name, exp) \
typedef int __assert_##name[((exp) != 0) - 1]
struct E(link_map)
{
EW(Addr) l_addr;
EW(Addr) l_name;
EW(Addr) l_ld;
EW(Addr) l_next;
EW(Addr) l_prev;
EW(Addr) l_real;
Lmid_t l_ns;
EW(Addr) l_libname;
};
#if CLASS == __ELF_NATIVE_CLASS
pldd_assert (l_addr, (offsetof (struct link_map, l_addr)
== offsetof (struct E(link_map), l_addr)));
pldd_assert (l_name, (offsetof (struct link_map, l_name)
== offsetof (struct E(link_map), l_name)));
pldd_assert (l_next, (offsetof (struct link_map, l_next)
== offsetof (struct E(link_map), l_next)));
#endif
struct E(libname_list)
{
EW(Addr) name;
EW(Addr) next;
};
#if CLASS == __ELF_NATIVE_CLASS
pldd_assert (name, (offsetof (struct libname_list, name)
== offsetof (struct E(libname_list), name)));
pldd_assert (next, (offsetof (struct libname_list, next)
== offsetof (struct E(libname_list), next)));
#endif
struct E(r_debug)
{
int r_version;
#if CLASS == 64
int pad;
#endif
EW(Addr) r_map;
};
#if CLASS == __ELF_NATIVE_CLASS
pldd_assert (r_version, (offsetof (struct r_debug, r_version)
== offsetof (struct E(r_debug), r_version)));
pldd_assert (r_map, (offsetof (struct r_debug, r_map)
== offsetof (struct E(r_debug), r_map)));
#endif
static int
E(find_maps) (pid_t pid, void *auxv, size_t auxv_size)
{
EW(Addr) phdr = 0;
unsigned int phnum = 0;
unsigned int phent = 0;
EW(auxv_t) *auxvXX = (EW(auxv_t) *) auxv;
for (int i = 0; i < auxv_size / sizeof (EW(auxv_t)); ++i)
switch (auxvXX[i].a_type)
{
case AT_PHDR:
phdr = auxvXX[i].a_un.a_val;
break;
case AT_PHNUM:
phnum = auxvXX[i].a_un.a_val;
break;
case AT_PHENT:
phent = auxvXX[i].a_un.a_val;
break;
default:
break;
}
if (phdr == 0 || phnum == 0 || phent == 0)
error (EXIT_FAILURE, 0, gettext ("cannot find program header of process"));
EW(Phdr) *p = alloca (phnum * phent);
if (pread64 (memfd, p, phnum * phent, phdr) != phnum * phent)
{
error (0, 0, gettext ("cannot read program header"));
return EXIT_FAILURE;
}
/* Determine the load offset. We need this for interpreting the
other program header entries so we do this in a separate loop.
Fortunately it is the first time unless someone does something
stupid when linking the application. */
EW(Addr) offset = 0;
for (unsigned int i = 0; i < phnum; ++i)
if (p[i].p_type == PT_PHDR)
{
offset = phdr - p[i].p_vaddr;
break;
}
EW(Addr) list = 0;
char *interp = NULL;
for (unsigned int i = 0; i < phnum; ++i)
if (p[i].p_type == PT_DYNAMIC)
{
EW(Dyn) *dyn = xmalloc (p[i].p_filesz);
if (pread64 (memfd, dyn, p[i].p_filesz, offset + p[i].p_vaddr)
!= p[i].p_filesz)
{
error (0, 0, gettext ("cannot read dynamic section"));
return EXIT_FAILURE;
}
/* Search for the DT_DEBUG entry. */
for (unsigned int j = 0; j < p[i].p_filesz / sizeof (EW(Dyn)); ++j)
if (dyn[j].d_tag == DT_DEBUG && dyn[j].d_un.d_ptr != 0)
{
struct E(r_debug) r;
if (pread64 (memfd, &r, sizeof (r), dyn[j].d_un.d_ptr)
!= sizeof (r))
{
error (0, 0, gettext ("cannot read r_debug"));
return EXIT_FAILURE;
}
if (r.r_map != 0)
{
list = r.r_map;
break;
}
}
free (dyn);
break;
}
else if (p[i].p_type == PT_INTERP)
{
interp = alloca (p[i].p_filesz);
if (pread64 (memfd, interp, p[i].p_filesz, offset + p[i].p_vaddr)
!= p[i].p_filesz)
{
error (0, 0, gettext ("cannot read program interpreter"));
return EXIT_FAILURE;
}
}
if (list == 0)
{
if (interp == NULL)
{
// XXX check whether the executable itself is the loader
return EXIT_FAILURE;
}
// XXX perhaps try finding ld.so and _r_debug in it
return EXIT_FAILURE;
}
/* Print the PID and program name first. */
printf ("%lu:\t%s\n", (unsigned long int) pid, exe);
/* Iterate over the list of objects and print the information. */
struct scratch_buffer tmpbuf;
scratch_buffer_init (&tmpbuf);
int status = 0;
do
{
struct E(link_map) m;
if (pread64 (memfd, &m, sizeof (m), list) != sizeof (m))
{
error (0, 0, gettext ("cannot read link map"));
status = EXIT_FAILURE;
goto out;
}
EW(Addr) name_offset = m.l_name;
again:
while (1)
{
ssize_t n = pread64 (memfd, tmpbuf.data, tmpbuf.length, name_offset);
if (n == -1)
{
error (0, 0, gettext ("cannot read object name"));
status = EXIT_FAILURE;
goto out;
}
if (memchr (tmpbuf.data, '\0', n) != NULL)
break;
if (!scratch_buffer_grow (&tmpbuf))
{
error (0, 0, gettext ("cannot allocate buffer for object name"));
status = EXIT_FAILURE;
goto out;
}
}
if (((char *)tmpbuf.data)[0] == '\0' && name_offset == m.l_name
&& m.l_libname != 0)
{
/* Try the l_libname element. */
struct E(libname_list) ln;
if (pread64 (memfd, &ln, sizeof (ln), m.l_libname) == sizeof (ln))
{
name_offset = ln.name;
goto again;
}
}
/* Skip over the executable. */
if (((char *)tmpbuf.data)[0] != '\0')
printf ("%s\n", (char *)tmpbuf.data);
list = m.l_next;
}
while (list != 0);
out:
scratch_buffer_free (&tmpbuf);
return status;
}
#undef CLASS
|