Branch data Line data Source code
1 : : /* Return symbol table of archive.
2 : : Copyright (C) 1998-2000, 2002, 2005, 2009, 2012, 2014, 2015 Red Hat, Inc.
3 : : This file is part of elfutils.
4 : : Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5 : :
6 : : This file is free software; you can redistribute it and/or modify
7 : : it under the terms of either
8 : :
9 : : * the GNU Lesser General Public License as published by the Free
10 : : Software Foundation; either version 3 of the License, or (at
11 : : your option) any later version
12 : :
13 : : or
14 : :
15 : : * the GNU General Public License as published by the Free
16 : : Software Foundation; either version 2 of the License, or (at
17 : : your option) any later version
18 : :
19 : : or both in parallel, as here.
20 : :
21 : : elfutils is distributed in the hope that it will be useful, but
22 : : WITHOUT ANY WARRANTY; without even the implied warranty of
23 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : : General Public License for more details.
25 : :
26 : : You should have received copies of the GNU General Public License and
27 : : the GNU Lesser General Public License along with this program. If
28 : : not, see <http://www.gnu.org/licenses/>. */
29 : :
30 : : #ifdef HAVE_CONFIG_H
31 : : # include <config.h>
32 : : #endif
33 : :
34 : : #include <assert.h>
35 : : #include <errno.h>
36 : : #include <stdbool.h>
37 : : #include <stdint.h>
38 : : #include <stdlib.h>
39 : : #include <string.h>
40 : :
41 : : #include <dl-hash.h>
42 : : #include "libelfP.h"
43 : :
44 : :
45 : : static int
46 : 16 : read_number_entries (uint64_t *nump, Elf *elf, size_t *offp, bool index64_p)
47 : : {
48 : 16 : union u
49 : : {
50 : : uint64_t ret64;
51 : : uint32_t ret32;
52 : : } u;
53 : :
54 [ + + ]: 16 : size_t w = index64_p ? 8 : 4;
55 [ + + ]: 16 : if (elf->map_address != NULL)
56 : : {
57 [ + + ]: 8 : if (*offp + w > elf->start_offset + elf->maximum_size)
58 : : return -1;
59 : : /* Use memcpy instead of pointer dereference so as not to assume the
60 : : field is naturally aligned within the file. */
61 : 6 : memcpy (&u, elf->map_address + *offp, w);
62 : : }
63 [ - + ]: 8 : else if ((size_t) pread_retry (elf->fildes, &u, w, *offp) != w)
64 : : return -1;
65 : :
66 : 14 : *offp += w;
67 : :
68 : 14 : if (BYTE_ORDER == LITTLE_ENDIAN)
69 [ + + ]: 14 : *nump = index64_p ? bswap_64 (u.ret64) : bswap_32 (u.ret32);
70 : : else
71 : : *nump = index64_p ? u.ret64 : u.ret32;
72 : :
73 : 14 : return 0;
74 : : }
75 : :
76 : : Elf_Arsym *
77 : 16 : elf_getarsym (Elf *elf, size_t *ptr)
78 : : {
79 [ - + ]: 16 : if (elf->kind != ELF_K_AR)
80 : : {
81 : : /* This is no archive. */
82 : 0 : __libelf_seterrno (ELF_E_NO_ARCHIVE);
83 : 0 : return NULL;
84 : : }
85 : :
86 [ + - ]: 16 : if (ptr != NULL)
87 : : /* In case of an error or when we know the value store the expected
88 : : value now. Doing this allows us easier exits in an error case. */
89 : 16 : *ptr = elf->state.ar.ar_sym_num;
90 : :
91 [ - + ]: 16 : if (elf->state.ar.ar_sym == (Elf_Arsym *) -1l)
92 : : {
93 : : /* There is no index. */
94 : 0 : __libelf_seterrno (ELF_E_NO_INDEX);
95 : 0 : return NULL;
96 : : }
97 : :
98 : 16 : Elf_Arsym *result = elf->state.ar.ar_sym;
99 [ + - ]: 16 : if (result == NULL)
100 : : {
101 : : /* We have not yet read the index. */
102 : 16 : rwlock_wrlock (elf->lock);
103 : :
104 : : /* In case we find no index remember this for the next call. */
105 : 16 : elf->state.ar.ar_sym = (Elf_Arsym *) -1l;
106 : :
107 : : /* We might have to allocate some temporary data for reading. */
108 : 16 : void *temp_data = NULL;
109 : :
110 : 16 : struct ar_hdr *index_hdr;
111 [ + + ]: 16 : if (elf->map_address == NULL)
112 : : {
113 : : /* We must read index from the file. */
114 [ - + ]: 8 : assert (elf->fildes != -1);
115 [ - + ]: 8 : if (pread_retry (elf->fildes, &elf->state.ar.ar_hdr,
116 : 8 : sizeof (struct ar_hdr), elf->start_offset + SARMAG)
117 : : != sizeof (struct ar_hdr))
118 : : {
119 : : /* It is not possible to read the index. Maybe it does not
120 : : exist. */
121 : 0 : __libelf_seterrno (ELF_E_READ_ERROR);
122 : 0 : goto out;
123 : : }
124 : :
125 : : index_hdr = &elf->state.ar.ar_hdr;
126 : : }
127 : : else
128 : : {
129 [ - + ]: 8 : if (SARMAG + sizeof (struct ar_hdr) > elf->maximum_size)
130 : : {
131 : : /* There is no room for the full archive. */
132 : 0 : __libelf_seterrno (ELF_E_NO_INDEX);
133 : 0 : goto out;
134 : : }
135 : :
136 : 8 : index_hdr = (struct ar_hdr *) (elf->map_address
137 : 8 : + elf->start_offset + SARMAG);
138 : : }
139 : :
140 : : /* Now test whether this really is an archive. */
141 [ - + ]: 16 : if (memcmp (index_hdr->ar_fmag, ARFMAG, 2) != 0)
142 : : {
143 : : /* Invalid magic bytes. */
144 : 0 : __libelf_seterrno (ELF_E_ARCHIVE_FMAG);
145 : 0 : goto out;
146 : : }
147 : :
148 : 16 : bool index64_p;
149 : : /* Now test whether this is the index. If the name is "/", this
150 : : is 32-bit index, if it's "/SYM64/", it's 64-bit index.
151 : :
152 : : XXX This is not entirely true. There are some more forms.
153 : : Which of them shall we handle? */
154 [ + + ]: 16 : if (memcmp (index_hdr->ar_name, "/ ", 16) == 0)
155 : : index64_p = false;
156 [ - + ]: 2 : else if (memcmp (index_hdr->ar_name, "/SYM64/ ", 16) == 0)
157 : : index64_p = true;
158 : : else
159 : : {
160 : : /* If the index is not the first entry, there is no index.
161 : :
162 : : XXX Is this true? */
163 : 0 : __libelf_seterrno (ELF_E_NO_INDEX);
164 : 0 : goto out;
165 : : }
166 : 16 : size_t w = index64_p ? 8 : 4;
167 : :
168 : : /* We have an archive. The first word in there is the number of
169 : : entries in the table. */
170 : 16 : uint64_t n = 0;
171 : 16 : size_t off = elf->start_offset + SARMAG + sizeof (struct ar_hdr);
172 [ + + ]: 16 : if (read_number_entries (&n, elf, &off, index64_p) < 0)
173 : : {
174 : : /* Cannot read the number of entries. */
175 : 2 : __libelf_seterrno (ELF_E_NO_INDEX);
176 : 2 : goto out;
177 : : }
178 : :
179 : : /* Now we can perform some first tests on whether all the data
180 : : needed for the index is available. */
181 : 14 : char tmpbuf[17];
182 : 14 : memcpy (tmpbuf, index_hdr->ar_size, 10);
183 : 14 : tmpbuf[10] = '\0';
184 : 14 : size_t index_size = atol (tmpbuf);
185 : :
186 [ + - ]: 14 : if (index_size > elf->maximum_size
187 [ + - ]: 14 : || elf->maximum_size - index_size < SARMAG + sizeof (struct ar_hdr)
188 : : #if SIZE_MAX <= 4294967295U
189 : : || n >= SIZE_MAX / sizeof (Elf_Arsym)
190 : : #endif
191 [ + - ]: 14 : || index_size < w
192 [ - + ]: 14 : || n > (index_size - w) / w)
193 : : {
194 : : /* This index table cannot be right since it does not fit into
195 : : the file. */
196 : 0 : __libelf_seterrno (ELF_E_NO_INDEX);
197 : 0 : goto out;
198 : : }
199 : :
200 : : /* Now we can allocate the arrays needed to store the index. */
201 : 14 : size_t ar_sym_len = (n + 1) * sizeof (Elf_Arsym);
202 : 14 : elf->state.ar.ar_sym = malloc (ar_sym_len);
203 [ + - ]: 14 : if (elf->state.ar.ar_sym != NULL)
204 : : {
205 : 14 : void *file_data; /* unit32_t[n] or uint64_t[n] */
206 : 14 : char *str_data;
207 : 14 : char *str_end;
208 : 14 : size_t sz = n * w;
209 : :
210 [ + + ]: 14 : if (elf->map_address == NULL)
211 : : {
212 : 8 : temp_data = malloc (sz);
213 [ - + ]: 8 : if (unlikely (temp_data == NULL))
214 : : {
215 : 0 : __libelf_seterrno (ELF_E_NOMEM);
216 : 0 : goto out;
217 : : }
218 : 8 : file_data = temp_data;
219 : :
220 : 8 : ar_sym_len += index_size - n * w;
221 : 8 : Elf_Arsym *newp = realloc (elf->state.ar.ar_sym, ar_sym_len);
222 [ - + ]: 8 : if (newp == NULL)
223 : : {
224 : 0 : free (elf->state.ar.ar_sym);
225 : 0 : elf->state.ar.ar_sym = NULL;
226 : 0 : __libelf_seterrno (ELF_E_NOMEM);
227 : 0 : goto out;
228 : : }
229 : 8 : elf->state.ar.ar_sym = newp;
230 : :
231 : 8 : char *new_str = (char *) (elf->state.ar.ar_sym + n + 1);
232 : :
233 : : /* Now read the data from the file. */
234 [ + - ]: 8 : if ((size_t) pread_retry (elf->fildes, file_data, sz, off) != sz
235 : 16 : || ((size_t) pread_retry (elf->fildes, new_str,
236 : 8 : index_size - sz, off + sz)
237 [ - + ]: 8 : != index_size - sz))
238 : : {
239 : : /* We were not able to read the data. */
240 : 0 : free (elf->state.ar.ar_sym);
241 : 0 : elf->state.ar.ar_sym = NULL;
242 : 0 : __libelf_seterrno (ELF_E_NO_INDEX);
243 : 0 : goto out;
244 : : }
245 : :
246 : 8 : str_data = (char *) new_str;
247 : 8 : str_end = new_str + (index_size - sz);
248 : : }
249 : : else
250 : : {
251 : 6 : file_data = (void *) (elf->map_address + off);
252 : 6 : if (!ALLOW_UNALIGNED
253 : : && ((uintptr_t) file_data & -(uintptr_t) n) != 0)
254 : : {
255 : : temp_data = malloc (sz);
256 : : if (unlikely (temp_data == NULL))
257 : : {
258 : : __libelf_seterrno (ELF_E_NOMEM);
259 : : goto out;
260 : : }
261 : : file_data = memcpy (temp_data, elf->map_address + off, sz);
262 : : }
263 : 6 : str_data = (char *) (elf->map_address + off + sz);
264 : 6 : str_end = (char *) (elf->map_address + off + index_size - w);
265 : : }
266 : :
267 : : /* Now we can build the data structure. */
268 : 14 : Elf_Arsym *arsym = elf->state.ar.ar_sym;
269 : 14 : uint64_t (*u64)[n+1] = file_data;
270 : 14 : uint32_t (*u32)[n+1] = file_data;
271 [ + + ]: 426 : for (size_t cnt = 0; cnt < n; ++cnt)
272 : : {
273 : 412 : arsym[cnt].as_name = str_data;
274 [ + + ]: 412 : if (index64_p)
275 : : {
276 : 12 : uint64_t tmp = (*u64)[cnt];
277 : 12 : if (BYTE_ORDER == LITTLE_ENDIAN)
278 : 12 : tmp = bswap_64 (tmp);
279 : :
280 : 12 : arsym[cnt].as_off = tmp;
281 : :
282 : : /* Check whether 64-bit offset fits into 32-bit
283 : : size_t. */
284 : 12 : if (sizeof (arsym[cnt].as_off) < 8
285 : : && arsym[cnt].as_off != tmp)
286 : : {
287 : : if (elf->map_address == NULL)
288 : : {
289 : : free (elf->state.ar.ar_sym);
290 : : elf->state.ar.ar_sym = NULL;
291 : : }
292 : :
293 : : __libelf_seterrno (ELF_E_RANGE);
294 : : goto out;
295 : : }
296 : : }
297 : 400 : else if (BYTE_ORDER == LITTLE_ENDIAN)
298 : 400 : arsym[cnt].as_off = bswap_32 ((*u32)[cnt]);
299 : : else
300 : : arsym[cnt].as_off = (*u32)[cnt];
301 : :
302 : : /* The symbol name must be NUL terminated within the string
303 : : table. Otherwise the archive symbol table is corrupt and
304 : : hashing or scanning the name would read out of bounds. */
305 : 824 : char *endp = (str_data < str_end
306 : 412 : ? memchr (str_data, '\0', str_end - str_data)
307 [ + - ]: 412 : : NULL);
308 [ - + ]: 412 : if (unlikely (endp == NULL))
309 : : {
310 [ # # ]: 0 : if (elf->map_address == NULL)
311 : : {
312 : 0 : free (elf->state.ar.ar_sym);
313 : 0 : elf->state.ar.ar_sym = NULL;
314 : : }
315 : 0 : __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
316 : 0 : goto out;
317 : : }
318 : :
319 : 412 : arsym[cnt].as_hash = _dl_elf_hash (str_data);
320 : 412 : str_data = endp + 1;
321 : : }
322 : :
323 : : /* At the end a special entry. */
324 : 14 : arsym[n].as_name = NULL;
325 : 14 : arsym[n].as_off = 0;
326 : 14 : arsym[n].as_hash = ~0UL;
327 : :
328 : : /* Tell the caller how many entries we have. */
329 : 14 : elf->state.ar.ar_sym_num = n + 1;
330 : : }
331 : :
332 : 14 : result = elf->state.ar.ar_sym;
333 : :
334 : 16 : out:
335 : 16 : free (temp_data);
336 : 16 : rwlock_unlock (elf->lock);
337 : : }
338 : :
339 [ + - ]: 16 : if (ptr != NULL)
340 : 16 : *ptr = elf->state.ar.ar_sym_num;
341 : :
342 : : return result;
343 : : }
|