Fix grouping and reuse other locales in various locales
[glibc.git] / elf / pldd-xx.c
blob1e84ec211b3468bf5eb372f8a5e58cf34db41dfc
1 /* Copyright (C) 2011 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #define E(name) E_(name, CLASS)
21 #define E_(name, cl) E__(name, cl)
22 #define E__(name, cl) name##cl
23 #define EW(type) EW_(Elf, CLASS, type)
24 #define EW_(e, w, t) EW__(e, w, _##t)
25 #define EW__(e, w, t) e##w##t
27 #define static_assert(name, exp) \
28 typedef int __assert_##name[((exp) != 0) - 1]
31 struct E(link_map)
33 EW(Addr) l_addr;
34 EW(Addr) l_name;
35 EW(Addr) l_ld;
36 EW(Addr) l_next;
37 EW(Addr) l_prev;
38 EW(Addr) l_real;
39 Lmid_t l_ns;
40 EW(Addr) l_libname;
42 #if CLASS == __ELF_NATIVE_CLASS
43 static_assert (l_addr, (offsetof (struct link_map, l_addr)
44 == offsetof (struct E(link_map), l_addr)));
45 static_assert (l_name, (offsetof (struct link_map, l_name)
46 == offsetof (struct E(link_map), l_name)));
47 static_assert (l_next, (offsetof (struct link_map, l_next)
48 == offsetof (struct E(link_map), l_next)));
49 #endif
52 struct E(libname_list)
54 EW(Addr) name;
55 EW(Addr) next;
57 #if CLASS == __ELF_NATIVE_CLASS
58 static_assert (name, (offsetof (struct libname_list, name)
59 == offsetof (struct E(libname_list), name)));
60 static_assert (next, (offsetof (struct libname_list, next)
61 == offsetof (struct E(libname_list), next)));
62 #endif
64 struct E(r_debug)
66 int r_version;
67 #if CLASS == 64
68 int pad;
69 #endif
70 EW(Addr) r_map;
72 #if CLASS == __ELF_NATIVE_CLASS
73 static_assert (r_version, (offsetof (struct r_debug, r_version)
74 == offsetof (struct E(r_debug), r_version)));
75 static_assert (r_map, (offsetof (struct r_debug, r_map)
76 == offsetof (struct E(r_debug), r_map)));
77 #endif
80 static int
82 E(find_maps) (pid_t pid, void *auxv, size_t auxv_size)
84 EW(Addr) phdr = 0;
85 unsigned int phnum = 0;
86 unsigned int phent = 0;
88 EW(auxv_t) *auxvXX = (EW(auxv_t) *) auxv;
89 for (int i = 0; i < auxv_size / sizeof (EW(auxv_t)); ++i)
90 switch (auxvXX[i].a_type)
92 case AT_PHDR:
93 phdr = auxvXX[i].a_un.a_val;
94 break;
95 case AT_PHNUM:
96 phnum = auxvXX[i].a_un.a_val;
97 break;
98 case AT_PHENT:
99 phent = auxvXX[i].a_un.a_val;
100 break;
101 default:
102 break;
105 if (phdr == 0 || phnum == 0 || phent == 0)
106 error (EXIT_FAILURE, 0, gettext ("cannot find program header of process"));
108 EW(Phdr) *p = alloca (phnum * phent);
109 if (pread64 (memfd, p, phnum * phent, phdr) != phnum * phent)
111 error (0, 0, gettext ("cannot read program header"));
112 return EXIT_FAILURE;
115 /* Determine the load offset. We need this for interpreting the
116 other program header entries so we do this in a separate loop.
117 Fortunately it is the first time unless someone does something
118 stupid when linking the application. */
119 EW(Addr) offset = 0;
120 for (unsigned int i = 0; i < phnum; ++i)
121 if (p[i].p_type == PT_PHDR)
123 offset = phdr - p[i].p_vaddr;
124 break;
127 EW(Addr) list = 0;
128 char *interp = NULL;
129 for (unsigned int i = 0; i < phnum; ++i)
130 if (p[i].p_type == PT_DYNAMIC)
132 EW(Dyn) *dyn = xmalloc (p[i].p_filesz);
133 if (pread64 (memfd, dyn, p[i].p_filesz, offset + p[i].p_vaddr)
134 != p[i].p_filesz)
136 error (0, 0, gettext ("cannot read dynamic section"));
137 return EXIT_FAILURE;
140 /* Search for the DT_DEBUG entry. */
141 for (unsigned int j = 0; j < p[i].p_filesz / sizeof (EW(Dyn)); ++j)
142 if (dyn[j].d_tag == DT_DEBUG && dyn[j].d_un.d_ptr != 0)
144 struct E(r_debug) r;
145 if (pread64 (memfd, &r, sizeof (r), dyn[j].d_un.d_ptr)
146 != sizeof (r))
148 error (0, 0, gettext ("cannot read r_debug"));
149 return EXIT_FAILURE;
152 if (r.r_map != 0)
154 list = r.r_map;
155 break;
159 free (dyn);
160 break;
162 else if (p[i].p_type == PT_INTERP)
164 interp = alloca (p[i].p_filesz);
165 if (pread64 (memfd, interp, p[i].p_filesz, offset + p[i].p_vaddr)
166 != p[i].p_filesz)
168 error (0, 0, gettext ("cannot read program interpreter"));
169 return EXIT_FAILURE;
173 if (list == 0)
175 if (interp == NULL)
177 // XXX check whether the executable itself is the loader
178 return EXIT_FAILURE;
181 // XXX perhaps try finding ld.so and _r_debug in it
183 return EXIT_FAILURE;
186 /* Print the PID and program name first. */
187 printf ("%lu:\t%s\n", (unsigned long int) pid, exe);
189 /* Iterate over the list of objects and print the information. */
190 size_t strsize = 256;
191 char *str = alloca (strsize);
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 return EXIT_FAILURE;
201 EW(Addr) name_offset = m.l_name;
202 again:
203 while (1)
205 ssize_t n = pread64 (memfd, str, strsize, name_offset);
206 if (n == -1)
208 error (0, 0, gettext ("cannot read object name"));
209 return EXIT_FAILURE;
212 if (memchr (str, '\0', n) != NULL)
213 break;
215 str = extend_alloca (str, strsize, strsize * 2);
218 if (str[0] == '\0' && name_offset == m.l_name
219 && m.l_libname != 0)
221 /* Try the l_libname element. */
222 struct E(libname_list) ln;
223 if (pread64 (memfd, &ln, sizeof (ln), m.l_libname) == sizeof (ln))
225 name_offset = ln.name;
226 goto again;
230 /* Skip over the executable. */
231 if (str[0] != '\0')
232 printf ("%s\n", str);
234 list = m.l_next;
236 while (list != 0);
238 return 0;
242 #undef CLASS