Update copyright notices with scripts/update-copyrights
[glibc.git] / elf / pldd-xx.c
blobe7d87c88d482321bb4d5b25be603c97292e24934
1 /* Copyright (C) 2011-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
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.
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.
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/>. */
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
26 #define pldd_assert(name, exp) \
27 typedef int __assert_##name[((exp) != 0) - 1]
30 struct E(link_map)
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;
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
51 struct E(libname_list)
53 EW(Addr) name;
54 EW(Addr) next;
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
63 struct E(r_debug)
65 int r_version;
66 #if CLASS == 64
67 int pad;
68 #endif
69 EW(Addr) r_map;
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
79 static int
81 E(find_maps) (pid_t pid, void *auxv, size_t auxv_size)
83 EW(Addr) phdr = 0;
84 unsigned int phnum = 0;
85 unsigned int phent = 0;
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)
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;
104 if (phdr == 0 || phnum == 0 || phent == 0)
105 error (EXIT_FAILURE, 0, gettext ("cannot find program header of process"));
107 EW(Phdr) *p = alloca (phnum * phent);
108 if (pread64 (memfd, p, phnum * phent, phdr) != phnum * phent)
110 error (0, 0, gettext ("cannot read program header"));
111 return EXIT_FAILURE;
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)
122 offset = phdr - p[i].p_vaddr;
123 break;
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)
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)
135 error (0, 0, gettext ("cannot read dynamic section"));
136 return EXIT_FAILURE;
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)
143 struct E(r_debug) r;
144 if (pread64 (memfd, &r, sizeof (r), dyn[j].d_un.d_ptr)
145 != sizeof (r))
147 error (0, 0, gettext ("cannot read r_debug"));
148 return EXIT_FAILURE;
151 if (r.r_map != 0)
153 list = r.r_map;
154 break;
158 free (dyn);
159 break;
161 else if (p[i].p_type == PT_INTERP)
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)
167 error (0, 0, gettext ("cannot read program interpreter"));
168 return EXIT_FAILURE;
172 if (list == 0)
174 if (interp == NULL)
176 // XXX check whether the executable itself is the loader
177 return EXIT_FAILURE;
180 // XXX perhaps try finding ld.so and _r_debug in it
182 return EXIT_FAILURE;
185 /* Print the PID and program name first. */
186 printf ("%lu:\t%s\n", (unsigned long int) pid, exe);
188 /* Iterate over the list of objects and print the information. */
189 size_t strsize = 256;
190 char *str = alloca (strsize);
193 struct E(link_map) m;
194 if (pread64 (memfd, &m, sizeof (m), list) != sizeof (m))
196 error (0, 0, gettext ("cannot read link map"));
197 return EXIT_FAILURE;
200 EW(Addr) name_offset = m.l_name;
201 again:
202 while (1)
204 ssize_t n = pread64 (memfd, str, strsize, name_offset);
205 if (n == -1)
207 error (0, 0, gettext ("cannot read object name"));
208 return EXIT_FAILURE;
211 if (memchr (str, '\0', n) != NULL)
212 break;
214 str = extend_alloca (str, strsize, strsize * 2);
217 if (str[0] == '\0' && name_offset == m.l_name
218 && m.l_libname != 0)
220 /* Try the l_libname element. */
221 struct E(libname_list) ln;
222 if (pread64 (memfd, &ln, sizeof (ln), m.l_libname) == sizeof (ln))
224 name_offset = ln.name;
225 goto again;
229 /* Skip over the executable. */
230 if (str[0] != '\0')
231 printf ("%s\n", str);
233 list = m.l_next;
235 while (list != 0);
237 return 0;
241 #undef CLASS