Fix gen-tgmath-tests.py for _Float64, _Float64x testing.
[glibc.git] / elf / pldd-xx.c
blob5ce0bce4c91c426e387901bddc814c838e8140ec
1 /* Copyright (C) 2011-2017 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 struct scratch_buffer tmpbuf;
190 scratch_buffer_init (&tmpbuf);
191 int status = 0;
194 struct E(link_map) m;
195 if (pread64 (memfd, &m, sizeof (m), list) != sizeof (m))
197 error (0, 0, gettext ("cannot read link map"));
198 status = EXIT_FAILURE;
199 goto out;
202 EW(Addr) name_offset = m.l_name;
203 again:
204 while (1)
206 ssize_t n = pread64 (memfd, tmpbuf.data, tmpbuf.length, name_offset);
207 if (n == -1)
209 error (0, 0, gettext ("cannot read object name"));
210 status = EXIT_FAILURE;
211 goto out;
214 if (memchr (tmpbuf.data, '\0', n) != NULL)
215 break;
217 if (!scratch_buffer_grow (&tmpbuf))
219 error (0, 0, gettext ("cannot allocate buffer for object name"));
220 status = EXIT_FAILURE;
221 goto out;
225 if (((char *)tmpbuf.data)[0] == '\0' && name_offset == m.l_name
226 && m.l_libname != 0)
228 /* Try the l_libname element. */
229 struct E(libname_list) ln;
230 if (pread64 (memfd, &ln, sizeof (ln), m.l_libname) == sizeof (ln))
232 name_offset = ln.name;
233 goto again;
237 /* Skip over the executable. */
238 if (((char *)tmpbuf.data)[0] != '\0')
239 printf ("%s\n", (char *)tmpbuf.data);
241 list = m.l_next;
243 while (list != 0);
245 out:
246 scratch_buffer_free (&tmpbuf);
247 return status;
251 #undef CLASS