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