Fix parsing of /sys/devices/system/cpu/online (bug 25859)
[glibc.git] / elf / pldd-xx.c
blob657b393f20e3e80a9036f898b1db3379bf47e32f
1 /* Copyright (C) 2011-2020 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 <https://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 struct E(link_map)
28 EW(Addr) l_addr;
29 EW(Addr) l_name;
30 EW(Addr) l_ld;
31 EW(Addr) l_next;
32 EW(Addr) l_prev;
33 EW(Addr) l_real;
34 Lmid_t l_ns;
35 EW(Addr) l_libname;
37 #if CLASS == __ELF_NATIVE_CLASS
38 _Static_assert (offsetof (struct link_map, l_addr)
39 == offsetof (struct E(link_map), l_addr), "l_addr");
40 _Static_assert (offsetof (struct link_map, l_name)
41 == offsetof (struct E(link_map), l_name), "l_name");
42 _Static_assert (offsetof (struct link_map, l_next)
43 == offsetof (struct E(link_map), l_next), "l_next");
44 #endif
47 struct E(libname_list)
49 EW(Addr) name;
50 EW(Addr) next;
52 #if CLASS == __ELF_NATIVE_CLASS
53 _Static_assert (offsetof (struct libname_list, name)
54 == offsetof (struct E(libname_list), name), "name");
55 _Static_assert (offsetof (struct libname_list, next)
56 == offsetof (struct E(libname_list), next), "next");
57 #endif
59 struct E(r_debug)
61 int r_version;
62 #if CLASS == 64
63 int pad;
64 #endif
65 EW(Addr) r_map;
67 #if CLASS == __ELF_NATIVE_CLASS
68 _Static_assert (offsetof (struct r_debug, r_version)
69 == offsetof (struct E(r_debug), r_version), "r_version");
70 _Static_assert (offsetof (struct r_debug, r_map)
71 == offsetof (struct E(r_debug), r_map), "r_map");
72 #endif
75 static int
77 E(find_maps) (const char *exe, int memfd, pid_t pid, void *auxv,
78 size_t auxv_size)
80 EW(Addr) phdr = 0;
81 unsigned int phnum = 0;
82 unsigned int phent = 0;
84 EW(auxv_t) *auxvXX = (EW(auxv_t) *) auxv;
85 for (int i = 0; i < auxv_size / sizeof (EW(auxv_t)); ++i)
86 switch (auxvXX[i].a_type)
88 case AT_PHDR:
89 phdr = auxvXX[i].a_un.a_val;
90 break;
91 case AT_PHNUM:
92 phnum = auxvXX[i].a_un.a_val;
93 break;
94 case AT_PHENT:
95 phent = auxvXX[i].a_un.a_val;
96 break;
97 default:
98 break;
101 if (phdr == 0 || phnum == 0 || phent == 0)
102 error (EXIT_FAILURE, 0, gettext ("cannot find program header of process"));
104 EW(Phdr) *p = xmalloc (phnum * phent);
105 if (pread (memfd, p, phnum * phent, phdr) != phnum * phent)
106 error (EXIT_FAILURE, 0, gettext ("cannot read program header"));
108 /* Determine the load offset. We need this for interpreting the
109 other program header entries so we do this in a separate loop.
110 Fortunately it is the first time unless someone does something
111 stupid when linking the application. */
112 EW(Addr) offset = 0;
113 for (unsigned int i = 0; i < phnum; ++i)
114 if (p[i].p_type == PT_PHDR)
116 offset = phdr - p[i].p_vaddr;
117 break;
120 EW(Addr) list = 0;
121 char *interp = NULL;
122 for (unsigned int i = 0; i < phnum; ++i)
123 if (p[i].p_type == PT_DYNAMIC)
125 EW(Dyn) *dyn = xmalloc (p[i].p_filesz);
126 if (pread (memfd, dyn, p[i].p_filesz, offset + p[i].p_vaddr)
127 != p[i].p_filesz)
128 error (EXIT_FAILURE, 0, gettext ("cannot read dynamic section"));
130 /* Search for the DT_DEBUG entry. */
131 for (unsigned int j = 0; j < p[i].p_filesz / sizeof (EW(Dyn)); ++j)
132 if (dyn[j].d_tag == DT_DEBUG && dyn[j].d_un.d_ptr != 0)
134 struct E(r_debug) r;
135 if (pread (memfd, &r, sizeof (r), dyn[j].d_un.d_ptr)
136 != sizeof (r))
137 error (EXIT_FAILURE, 0, gettext ("cannot read r_debug"));
139 if (r.r_map != 0)
141 list = r.r_map;
142 break;
146 free (dyn);
147 break;
149 else if (p[i].p_type == PT_INTERP)
151 interp = xmalloc (p[i].p_filesz);
152 if (pread (memfd, interp, p[i].p_filesz, offset + p[i].p_vaddr)
153 != p[i].p_filesz)
154 error (EXIT_FAILURE, 0, gettext ("cannot read program interpreter"));
157 if (list == 0)
159 if (interp == NULL)
161 // XXX check whether the executable itself is the loader
162 exit (EXIT_FAILURE);
165 // XXX perhaps try finding ld.so and _r_debug in it
166 exit (EXIT_FAILURE);
169 free (p);
170 free (interp);
172 /* Print the PID and program name first. */
173 printf ("%lu:\t%s\n", (unsigned long int) pid, exe);
175 /* Iterate over the list of objects and print the information. */
176 struct scratch_buffer tmpbuf;
177 scratch_buffer_init (&tmpbuf);
178 int status = 0;
181 struct E(link_map) m;
182 if (pread (memfd, &m, sizeof (m), list) != sizeof (m))
183 error (EXIT_FAILURE, 0, gettext ("cannot read link map"));
185 EW(Addr) name_offset = m.l_name;
186 while (1)
188 ssize_t n = pread (memfd, tmpbuf.data, tmpbuf.length, name_offset);
189 if (n == -1)
190 error (EXIT_FAILURE, 0, gettext ("cannot read object name"));
192 if (memchr (tmpbuf.data, '\0', n) != NULL)
193 break;
195 if (!scratch_buffer_grow (&tmpbuf))
196 error (EXIT_FAILURE, 0,
197 gettext ("cannot allocate buffer for object name"));
200 /* The m.l_name and m.l_libname.name for loader linkmap points to same
201 values (since BZ#387 fix). Trying to use l_libname name as the
202 shared object name might lead to an infinite loop (BZ#18035). */
204 /* Skip over the executable. */
205 if (((char *)tmpbuf.data)[0] != '\0')
206 printf ("%s\n", (char *)tmpbuf.data);
208 list = m.l_next;
210 while (list != 0);
212 scratch_buffer_free (&tmpbuf);
213 return status;
217 #undef CLASS