]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 2011-2014 Free Software Foundation, Inc. |
11988f8f UD |
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 | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
11988f8f UD |
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 | ||
750b5926 | 26 | #define pldd_assert(name, exp) \ |
11988f8f UD |
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 | |
750b5926 | 42 | pldd_assert (l_addr, (offsetof (struct link_map, l_addr) |
11988f8f | 43 | == offsetof (struct E(link_map), l_addr))); |
750b5926 | 44 | pldd_assert (l_name, (offsetof (struct link_map, l_name) |
11988f8f | 45 | == offsetof (struct E(link_map), l_name))); |
750b5926 | 46 | pldd_assert (l_next, (offsetof (struct link_map, l_next) |
11988f8f UD |
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 | |
750b5926 | 57 | pldd_assert (name, (offsetof (struct libname_list, name) |
11988f8f | 58 | == offsetof (struct E(libname_list), name))); |
750b5926 | 59 | pldd_assert (next, (offsetof (struct libname_list, next) |
11988f8f UD |
60 | == offsetof (struct E(libname_list), next))); |
61 | #endif | |
62 | ||
63 | struct E(r_debug) | |
64 | { | |
65 | int r_version; | |
c5305d88 UD |
66 | #if CLASS == 64 |
67 | int pad; | |
68 | #endif | |
11988f8f UD |
69 | EW(Addr) r_map; |
70 | }; | |
71 | #if CLASS == __ELF_NATIVE_CLASS | |
750b5926 | 72 | pldd_assert (r_version, (offsetof (struct r_debug, r_version) |
11988f8f | 73 | == offsetof (struct E(r_debug), r_version))); |
750b5926 | 74 | pldd_assert (r_map, (offsetof (struct r_debug, r_map) |
11988f8f UD |
75 | == offsetof (struct E(r_debug), r_map))); |
76 | #endif | |
77 | ||
78 | ||
79 | static int | |
c5305d88 | 80 | |
6585cb60 | 81 | E(find_maps) (pid_t pid, void *auxv, size_t auxv_size) |
11988f8f UD |
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 | size_t strsize = 256; | |
190 | char *str = alloca (strsize); | |
191 | do | |
192 | { | |
193 | struct E(link_map) m; | |
194 | if (pread64 (memfd, &m, sizeof (m), list) != sizeof (m)) | |
195 | { | |
196 | error (0, 0, gettext ("cannot read link map")); | |
197 | return EXIT_FAILURE; | |
198 | } | |
199 | ||
200 | EW(Addr) name_offset = m.l_name; | |
201 | again: | |
202 | while (1) | |
203 | { | |
204 | ssize_t n = pread64 (memfd, str, strsize, name_offset); | |
205 | if (n == -1) | |
206 | { | |
207 | error (0, 0, gettext ("cannot read object name")); | |
208 | return EXIT_FAILURE; | |
209 | } | |
210 | ||
211 | if (memchr (str, '\0', n) != NULL) | |
212 | break; | |
213 | ||
214 | str = extend_alloca (str, strsize, strsize * 2); | |
215 | } | |
216 | ||
217 | if (str[0] == '\0' && name_offset == m.l_name | |
218 | && m.l_libname != 0) | |
219 | { | |
220 | /* Try the l_libname element. */ | |
221 | struct E(libname_list) ln; | |
222 | if (pread64 (memfd, &ln, sizeof (ln), m.l_libname) == sizeof (ln)) | |
223 | { | |
224 | name_offset = ln.name; | |
225 | goto again; | |
226 | } | |
227 | } | |
228 | ||
229 | /* Skip over the executable. */ | |
230 | if (str[0] != '\0') | |
231 | printf ("%s\n", str); | |
232 | ||
233 | list = m.l_next; | |
234 | } | |
235 | while (list != 0); | |
236 | ||
237 | return 0; | |
238 | } | |
239 | ||
240 | ||
241 | #undef CLASS |