Commit | Line | Data |
---|---|---|
3284fe0c | 1 | /* elfcomm.c -- common code for ELF format file. |
b3adc24a | 2 | Copyright (C) 2010-2020 Free Software Foundation, Inc. |
3284fe0c L |
3 | |
4 | Originally developed by Eric Youngdale <eric@andante.jic.com> | |
5 | Modifications by Nick Clifton <nickc@redhat.com> | |
6 | ||
7 | This file is part of GNU Binutils. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA | |
22 | 02110-1301, USA. */ | |
23 | ||
645ba681 AM |
24 | /* Do not use BFD types in this file that differ in size depending on |
25 | whether BFD64 is defined. Functions in this file are used by | |
26 | readelf.c and elfedit.c which define BFD64, and by objdump.c which | |
27 | doesn't. */ | |
28 | ||
3284fe0c L |
29 | #include "sysdep.h" |
30 | #include "libiberty.h" | |
31 | #include "filenames.h" | |
32 | #include "bfd.h" | |
33 | #include "aout/ar.h" | |
34 | #include "bucomm.h" | |
35 | #include "elfcomm.h" | |
c2a7d3f5 | 36 | #include <assert.h> |
3284fe0c L |
37 | |
38 | void | |
39 | error (const char *message, ...) | |
40 | { | |
41 | va_list args; | |
42 | ||
fafd911d NC |
43 | /* Try to keep error messages in sync with the program's normal output. */ |
44 | fflush (stdout); | |
45 | ||
3284fe0c L |
46 | va_start (args, message); |
47 | fprintf (stderr, _("%s: Error: "), program_name); | |
48 | vfprintf (stderr, message, args); | |
49 | va_end (args); | |
50 | } | |
51 | ||
52 | void | |
53 | warn (const char *message, ...) | |
54 | { | |
55 | va_list args; | |
56 | ||
fafd911d NC |
57 | /* Try to keep warning messages in sync with the program's normal output. */ |
58 | fflush (stdout); | |
3aade688 | 59 | |
3284fe0c L |
60 | va_start (args, message); |
61 | fprintf (stderr, _("%s: Warning: "), program_name); | |
62 | vfprintf (stderr, message, args); | |
63 | va_end (args); | |
64 | } | |
65 | ||
66 | void (*byte_put) (unsigned char *, elf_vma, int); | |
67 | ||
68 | void | |
69 | byte_put_little_endian (unsigned char * field, elf_vma value, int size) | |
70 | { | |
71 | switch (size) | |
72 | { | |
73 | case 8: | |
74 | field[7] = (((value >> 24) >> 24) >> 8) & 0xff; | |
75 | field[6] = ((value >> 24) >> 24) & 0xff; | |
76 | field[5] = ((value >> 24) >> 16) & 0xff; | |
77 | field[4] = ((value >> 24) >> 8) & 0xff; | |
78 | /* Fall through. */ | |
79 | case 4: | |
80 | field[3] = (value >> 24) & 0xff; | |
81 | /* Fall through. */ | |
82 | case 3: | |
83 | field[2] = (value >> 16) & 0xff; | |
84 | /* Fall through. */ | |
85 | case 2: | |
86 | field[1] = (value >> 8) & 0xff; | |
87 | /* Fall through. */ | |
88 | case 1: | |
89 | field[0] = value & 0xff; | |
90 | break; | |
91 | ||
92 | default: | |
93 | error (_("Unhandled data length: %d\n"), size); | |
94 | abort (); | |
95 | } | |
96 | } | |
97 | ||
98 | void | |
99 | byte_put_big_endian (unsigned char * field, elf_vma value, int size) | |
100 | { | |
101 | switch (size) | |
102 | { | |
103 | case 8: | |
104 | field[7] = value & 0xff; | |
105 | field[6] = (value >> 8) & 0xff; | |
106 | field[5] = (value >> 16) & 0xff; | |
107 | field[4] = (value >> 24) & 0xff; | |
108 | value >>= 16; | |
109 | value >>= 16; | |
110 | /* Fall through. */ | |
111 | case 4: | |
112 | field[3] = value & 0xff; | |
113 | value >>= 8; | |
114 | /* Fall through. */ | |
115 | case 3: | |
116 | field[2] = value & 0xff; | |
117 | value >>= 8; | |
118 | /* Fall through. */ | |
119 | case 2: | |
120 | field[1] = value & 0xff; | |
121 | value >>= 8; | |
122 | /* Fall through. */ | |
123 | case 1: | |
124 | field[0] = value & 0xff; | |
125 | break; | |
126 | ||
127 | default: | |
128 | error (_("Unhandled data length: %d\n"), size); | |
129 | abort (); | |
130 | } | |
131 | } | |
132 | ||
dda8d76d | 133 | elf_vma (*byte_get) (const unsigned char *, int); |
3284fe0c L |
134 | |
135 | elf_vma | |
dda8d76d | 136 | byte_get_little_endian (const unsigned char *field, int size) |
3284fe0c L |
137 | { |
138 | switch (size) | |
139 | { | |
140 | case 1: | |
141 | return *field; | |
142 | ||
143 | case 2: | |
144 | return ((unsigned int) (field[0])) | |
145 | | (((unsigned int) (field[1])) << 8); | |
146 | ||
147 | case 3: | |
148 | return ((unsigned long) (field[0])) | |
149 | | (((unsigned long) (field[1])) << 8) | |
150 | | (((unsigned long) (field[2])) << 16); | |
151 | ||
152 | case 4: | |
153 | return ((unsigned long) (field[0])) | |
154 | | (((unsigned long) (field[1])) << 8) | |
155 | | (((unsigned long) (field[2])) << 16) | |
156 | | (((unsigned long) (field[3])) << 24); | |
157 | ||
87bc83b3 CC |
158 | case 5: |
159 | if (sizeof (elf_vma) == 8) | |
160 | return ((elf_vma) (field[0])) | |
161 | | (((elf_vma) (field[1])) << 8) | |
162 | | (((elf_vma) (field[2])) << 16) | |
163 | | (((elf_vma) (field[3])) << 24) | |
164 | | (((elf_vma) (field[4])) << 32); | |
165 | else if (sizeof (elf_vma) == 4) | |
166 | /* We want to extract data from an 8 byte wide field and | |
167 | place it into a 4 byte wide field. Since this is a little | |
168 | endian source we can just use the 4 byte extraction code. */ | |
169 | return ((unsigned long) (field[0])) | |
170 | | (((unsigned long) (field[1])) << 8) | |
171 | | (((unsigned long) (field[2])) << 16) | |
172 | | (((unsigned long) (field[3])) << 24); | |
1a0670f3 | 173 | /* Fall through. */ |
87bc83b3 CC |
174 | |
175 | case 6: | |
176 | if (sizeof (elf_vma) == 8) | |
177 | return ((elf_vma) (field[0])) | |
178 | | (((elf_vma) (field[1])) << 8) | |
179 | | (((elf_vma) (field[2])) << 16) | |
180 | | (((elf_vma) (field[3])) << 24) | |
181 | | (((elf_vma) (field[4])) << 32) | |
182 | | (((elf_vma) (field[5])) << 40); | |
183 | else if (sizeof (elf_vma) == 4) | |
184 | /* We want to extract data from an 8 byte wide field and | |
185 | place it into a 4 byte wide field. Since this is a little | |
186 | endian source we can just use the 4 byte extraction code. */ | |
187 | return ((unsigned long) (field[0])) | |
188 | | (((unsigned long) (field[1])) << 8) | |
189 | | (((unsigned long) (field[2])) << 16) | |
190 | | (((unsigned long) (field[3])) << 24); | |
1a0670f3 | 191 | /* Fall through. */ |
87bc83b3 CC |
192 | |
193 | case 7: | |
194 | if (sizeof (elf_vma) == 8) | |
195 | return ((elf_vma) (field[0])) | |
196 | | (((elf_vma) (field[1])) << 8) | |
197 | | (((elf_vma) (field[2])) << 16) | |
198 | | (((elf_vma) (field[3])) << 24) | |
199 | | (((elf_vma) (field[4])) << 32) | |
200 | | (((elf_vma) (field[5])) << 40) | |
201 | | (((elf_vma) (field[6])) << 48); | |
202 | else if (sizeof (elf_vma) == 4) | |
203 | /* We want to extract data from an 8 byte wide field and | |
204 | place it into a 4 byte wide field. Since this is a little | |
205 | endian source we can just use the 4 byte extraction code. */ | |
206 | return ((unsigned long) (field[0])) | |
207 | | (((unsigned long) (field[1])) << 8) | |
208 | | (((unsigned long) (field[2])) << 16) | |
209 | | (((unsigned long) (field[3])) << 24); | |
1a0670f3 | 210 | /* Fall through. */ |
87bc83b3 | 211 | |
3284fe0c L |
212 | case 8: |
213 | if (sizeof (elf_vma) == 8) | |
214 | return ((elf_vma) (field[0])) | |
215 | | (((elf_vma) (field[1])) << 8) | |
216 | | (((elf_vma) (field[2])) << 16) | |
217 | | (((elf_vma) (field[3])) << 24) | |
218 | | (((elf_vma) (field[4])) << 32) | |
219 | | (((elf_vma) (field[5])) << 40) | |
220 | | (((elf_vma) (field[6])) << 48) | |
221 | | (((elf_vma) (field[7])) << 56); | |
222 | else if (sizeof (elf_vma) == 4) | |
223 | /* We want to extract data from an 8 byte wide field and | |
224 | place it into a 4 byte wide field. Since this is a little | |
225 | endian source we can just use the 4 byte extraction code. */ | |
226 | return ((unsigned long) (field[0])) | |
227 | | (((unsigned long) (field[1])) << 8) | |
228 | | (((unsigned long) (field[2])) << 16) | |
229 | | (((unsigned long) (field[3])) << 24); | |
1a0670f3 | 230 | /* Fall through. */ |
3284fe0c L |
231 | |
232 | default: | |
233 | error (_("Unhandled data length: %d\n"), size); | |
234 | abort (); | |
235 | } | |
236 | } | |
237 | ||
238 | elf_vma | |
dda8d76d | 239 | byte_get_big_endian (const unsigned char *field, int size) |
3284fe0c L |
240 | { |
241 | switch (size) | |
242 | { | |
243 | case 1: | |
244 | return *field; | |
245 | ||
246 | case 2: | |
247 | return ((unsigned int) (field[1])) | (((int) (field[0])) << 8); | |
248 | ||
249 | case 3: | |
250 | return ((unsigned long) (field[2])) | |
251 | | (((unsigned long) (field[1])) << 8) | |
252 | | (((unsigned long) (field[0])) << 16); | |
253 | ||
254 | case 4: | |
255 | return ((unsigned long) (field[3])) | |
256 | | (((unsigned long) (field[2])) << 8) | |
257 | | (((unsigned long) (field[1])) << 16) | |
258 | | (((unsigned long) (field[0])) << 24); | |
259 | ||
87bc83b3 CC |
260 | case 5: |
261 | if (sizeof (elf_vma) == 8) | |
262 | return ((elf_vma) (field[4])) | |
263 | | (((elf_vma) (field[3])) << 8) | |
264 | | (((elf_vma) (field[2])) << 16) | |
265 | | (((elf_vma) (field[1])) << 24) | |
266 | | (((elf_vma) (field[0])) << 32); | |
267 | else if (sizeof (elf_vma) == 4) | |
268 | { | |
269 | /* Although we are extracting data from an 8 byte wide field, | |
270 | we are returning only 4 bytes of data. */ | |
271 | field += 1; | |
272 | return ((unsigned long) (field[3])) | |
273 | | (((unsigned long) (field[2])) << 8) | |
274 | | (((unsigned long) (field[1])) << 16) | |
275 | | (((unsigned long) (field[0])) << 24); | |
276 | } | |
1a0670f3 | 277 | /* Fall through. */ |
87bc83b3 CC |
278 | |
279 | case 6: | |
280 | if (sizeof (elf_vma) == 8) | |
281 | return ((elf_vma) (field[5])) | |
282 | | (((elf_vma) (field[4])) << 8) | |
283 | | (((elf_vma) (field[3])) << 16) | |
284 | | (((elf_vma) (field[2])) << 24) | |
285 | | (((elf_vma) (field[1])) << 32) | |
286 | | (((elf_vma) (field[0])) << 40); | |
287 | else if (sizeof (elf_vma) == 4) | |
288 | { | |
289 | /* Although we are extracting data from an 8 byte wide field, | |
290 | we are returning only 4 bytes of data. */ | |
291 | field += 2; | |
292 | return ((unsigned long) (field[3])) | |
293 | | (((unsigned long) (field[2])) << 8) | |
294 | | (((unsigned long) (field[1])) << 16) | |
295 | | (((unsigned long) (field[0])) << 24); | |
296 | } | |
1a0670f3 | 297 | /* Fall through. */ |
87bc83b3 CC |
298 | |
299 | case 7: | |
300 | if (sizeof (elf_vma) == 8) | |
301 | return ((elf_vma) (field[6])) | |
302 | | (((elf_vma) (field[5])) << 8) | |
303 | | (((elf_vma) (field[4])) << 16) | |
304 | | (((elf_vma) (field[3])) << 24) | |
305 | | (((elf_vma) (field[2])) << 32) | |
306 | | (((elf_vma) (field[1])) << 40) | |
307 | | (((elf_vma) (field[0])) << 48); | |
308 | else if (sizeof (elf_vma) == 4) | |
309 | { | |
310 | /* Although we are extracting data from an 8 byte wide field, | |
311 | we are returning only 4 bytes of data. */ | |
312 | field += 3; | |
313 | return ((unsigned long) (field[3])) | |
314 | | (((unsigned long) (field[2])) << 8) | |
315 | | (((unsigned long) (field[1])) << 16) | |
316 | | (((unsigned long) (field[0])) << 24); | |
317 | } | |
1a0670f3 | 318 | /* Fall through. */ |
87bc83b3 | 319 | |
3284fe0c L |
320 | case 8: |
321 | if (sizeof (elf_vma) == 8) | |
322 | return ((elf_vma) (field[7])) | |
323 | | (((elf_vma) (field[6])) << 8) | |
324 | | (((elf_vma) (field[5])) << 16) | |
325 | | (((elf_vma) (field[4])) << 24) | |
326 | | (((elf_vma) (field[3])) << 32) | |
327 | | (((elf_vma) (field[2])) << 40) | |
328 | | (((elf_vma) (field[1])) << 48) | |
329 | | (((elf_vma) (field[0])) << 56); | |
330 | else if (sizeof (elf_vma) == 4) | |
331 | { | |
87bc83b3 | 332 | /* Although we are extracting data from an 8 byte wide field, |
3284fe0c L |
333 | we are returning only 4 bytes of data. */ |
334 | field += 4; | |
335 | return ((unsigned long) (field[3])) | |
336 | | (((unsigned long) (field[2])) << 8) | |
337 | | (((unsigned long) (field[1])) << 16) | |
338 | | (((unsigned long) (field[0])) << 24); | |
339 | } | |
1a0670f3 | 340 | /* Fall through. */ |
3284fe0c L |
341 | |
342 | default: | |
343 | error (_("Unhandled data length: %d\n"), size); | |
344 | abort (); | |
345 | } | |
346 | } | |
347 | ||
348 | elf_vma | |
dda8d76d | 349 | byte_get_signed (const unsigned char *field, int size) |
3284fe0c L |
350 | { |
351 | elf_vma x = byte_get (field, size); | |
352 | ||
353 | switch (size) | |
354 | { | |
355 | case 1: | |
356 | return (x ^ 0x80) - 0x80; | |
357 | case 2: | |
358 | return (x ^ 0x8000) - 0x8000; | |
87bc83b3 CC |
359 | case 3: |
360 | return (x ^ 0x800000) - 0x800000; | |
3284fe0c L |
361 | case 4: |
362 | return (x ^ 0x80000000) - 0x80000000; | |
87bc83b3 CC |
363 | case 5: |
364 | case 6: | |
365 | case 7: | |
3284fe0c | 366 | case 8: |
87bc83b3 CC |
367 | /* Reads of 5-, 6-, and 7-byte numbers are the result of |
368 | trying to read past the end of a buffer, and will therefore | |
369 | not have meaningful values, so we don't try to deal with | |
370 | the sign in these cases. */ | |
3284fe0c L |
371 | return x; |
372 | default: | |
373 | abort (); | |
374 | } | |
375 | } | |
376 | ||
74bc6052 CC |
377 | /* Return the high-order 32-bits and the low-order 32-bits |
378 | of an 8-byte value separately. */ | |
379 | ||
380 | void | |
dda8d76d | 381 | byte_get_64 (const unsigned char *field, elf_vma *high, elf_vma *low) |
74bc6052 CC |
382 | { |
383 | if (byte_get == byte_get_big_endian) | |
384 | { | |
385 | *high = byte_get_big_endian (field, 4); | |
386 | *low = byte_get_big_endian (field + 4, 4); | |
387 | } | |
388 | else | |
389 | { | |
390 | *high = byte_get_little_endian (field + 4, 4); | |
391 | *low = byte_get_little_endian (field, 4); | |
392 | } | |
393 | return; | |
394 | } | |
395 | ||
3284fe0c L |
396 | /* Return the path name for a proxy entry in a thin archive, adjusted |
397 | relative to the path name of the thin archive itself if necessary. | |
398 | Always returns a pointer to malloc'ed memory. */ | |
399 | ||
400 | char * | |
401 | adjust_relative_path (const char *file_name, const char *name, | |
591f7597 | 402 | unsigned long name_len) |
3284fe0c L |
403 | { |
404 | char * member_file_name; | |
405 | const char * base_name = lbasename (file_name); | |
591f7597 | 406 | size_t amt; |
3284fe0c L |
407 | |
408 | /* This is a proxy entry for a thin archive member. | |
409 | If the extended name table contains an absolute path | |
410 | name, or if the archive is in the current directory, | |
411 | use the path name as given. Otherwise, we need to | |
412 | find the member relative to the directory where the | |
413 | archive is located. */ | |
414 | if (IS_ABSOLUTE_PATH (name) || base_name == file_name) | |
415 | { | |
591f7597 NC |
416 | amt = name_len + 1; |
417 | if (amt == 0) | |
418 | return NULL; | |
419 | member_file_name = (char *) malloc (amt); | |
3284fe0c L |
420 | if (member_file_name == NULL) |
421 | { | |
422 | error (_("Out of memory\n")); | |
423 | return NULL; | |
424 | } | |
425 | memcpy (member_file_name, name, name_len); | |
426 | member_file_name[name_len] = '\0'; | |
427 | } | |
428 | else | |
429 | { | |
430 | /* Concatenate the path components of the archive file name | |
431 | to the relative path name from the extended name table. */ | |
432 | size_t prefix_len = base_name - file_name; | |
591f7597 NC |
433 | |
434 | amt = prefix_len + name_len + 1; | |
435 | /* PR 17531: file: 2896dc8b | |
436 | Catch wraparound. */ | |
437 | if (amt < prefix_len || amt < name_len) | |
438 | { | |
439 | error (_("Abnormal length of thin archive member name: %lx\n"), | |
440 | name_len); | |
441 | return NULL; | |
442 | } | |
3aade688 | 443 | |
591f7597 | 444 | member_file_name = (char *) malloc (amt); |
3284fe0c L |
445 | if (member_file_name == NULL) |
446 | { | |
447 | error (_("Out of memory\n")); | |
448 | return NULL; | |
449 | } | |
450 | memcpy (member_file_name, file_name, prefix_len); | |
451 | memcpy (member_file_name + prefix_len, name, name_len); | |
452 | member_file_name[prefix_len + name_len] = '\0'; | |
453 | } | |
454 | return member_file_name; | |
455 | } | |
456 | ||
c2a7d3f5 NC |
457 | /* Processes the archive index table and symbol table in ARCH. |
458 | Entries in the index table are SIZEOF_AR_INDEX bytes long. | |
459 | Fills in ARCH->next_arhdr_offset and ARCH->arhdr. | |
460 | If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array, | |
461 | ARCH->sym_size and ARCH->sym_table. | |
462 | It is the caller's responsibility to free ARCH->index_array and | |
463 | ARCH->sym_table. | |
464 | Returns TRUE upon success, FALSE otherwise. | |
465 | If failure occurs an error message is printed. */ | |
466 | ||
467 | static bfd_boolean | |
468 | process_archive_index_and_symbols (struct archive_info * arch, | |
469 | unsigned int sizeof_ar_index, | |
470 | bfd_boolean read_symbols) | |
471 | { | |
472 | size_t got; | |
473 | unsigned long size; | |
d91f0b20 | 474 | char fmag_save; |
c2a7d3f5 | 475 | |
d91f0b20 AM |
476 | fmag_save = arch->arhdr.ar_fmag[0]; |
477 | arch->arhdr.ar_fmag[0] = 0; | |
c2a7d3f5 | 478 | size = strtoul (arch->arhdr.ar_size, NULL, 10); |
d91f0b20 | 479 | arch->arhdr.ar_fmag[0] = fmag_save; |
591f7597 NC |
480 | /* PR 17531: file: 912bd7de. */ |
481 | if ((signed long) size < 0) | |
482 | { | |
483 | error (_("%s: invalid archive header size: %ld\n"), | |
484 | arch->file_name, size); | |
485 | return FALSE; | |
486 | } | |
487 | ||
c2a7d3f5 NC |
488 | size = size + (size & 1); |
489 | ||
490 | arch->next_arhdr_offset += sizeof arch->arhdr + size; | |
491 | ||
492 | if (! read_symbols) | |
493 | { | |
494 | if (fseek (arch->file, size, SEEK_CUR) != 0) | |
495 | { | |
496 | error (_("%s: failed to skip archive symbol table\n"), | |
497 | arch->file_name); | |
498 | return FALSE; | |
499 | } | |
500 | } | |
501 | else | |
502 | { | |
503 | unsigned long i; | |
504 | /* A buffer used to hold numbers read in from an archive index. | |
505 | These are always SIZEOF_AR_INDEX bytes long and stored in | |
506 | big-endian format. */ | |
507 | unsigned char integer_buffer[sizeof arch->index_num]; | |
508 | unsigned char * index_buffer; | |
509 | ||
510 | assert (sizeof_ar_index <= sizeof integer_buffer); | |
3aade688 | 511 | |
c2a7d3f5 NC |
512 | /* Check the size of the archive index. */ |
513 | if (size < sizeof_ar_index) | |
514 | { | |
515 | error (_("%s: the archive index is empty\n"), arch->file_name); | |
516 | return FALSE; | |
517 | } | |
518 | ||
519 | /* Read the number of entries in the archive index. */ | |
520 | got = fread (integer_buffer, 1, sizeof_ar_index, arch->file); | |
521 | if (got != sizeof_ar_index) | |
522 | { | |
523 | error (_("%s: failed to read archive index\n"), arch->file_name); | |
524 | return FALSE; | |
525 | } | |
526 | ||
527 | arch->index_num = byte_get_big_endian (integer_buffer, sizeof_ar_index); | |
528 | size -= sizeof_ar_index; | |
529 | ||
53774b7e NC |
530 | if (size < arch->index_num * sizeof_ar_index |
531 | /* PR 17531: file: 585515d1. */ | |
532 | || size < arch->index_num) | |
c2a7d3f5 | 533 | { |
53774b7e | 534 | error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"), |
c2a7d3f5 NC |
535 | arch->file_name, (long) arch->index_num, sizeof_ar_index, size); |
536 | return FALSE; | |
537 | } | |
538 | ||
539 | /* Read in the archive index. */ | |
540 | index_buffer = (unsigned char *) | |
541 | malloc (arch->index_num * sizeof_ar_index); | |
542 | if (index_buffer == NULL) | |
543 | { | |
544 | error (_("Out of memory whilst trying to read archive symbol index\n")); | |
545 | return FALSE; | |
546 | } | |
547 | ||
548 | got = fread (index_buffer, sizeof_ar_index, arch->index_num, arch->file); | |
549 | if (got != arch->index_num) | |
550 | { | |
551 | free (index_buffer); | |
552 | error (_("%s: failed to read archive index\n"), arch->file_name); | |
553 | return FALSE; | |
554 | } | |
555 | ||
556 | size -= arch->index_num * sizeof_ar_index; | |
557 | ||
558 | /* Convert the index numbers into the host's numeric format. */ | |
559 | arch->index_array = (elf_vma *) | |
560 | malloc (arch->index_num * sizeof (* arch->index_array)); | |
561 | if (arch->index_array == NULL) | |
562 | { | |
563 | free (index_buffer); | |
564 | error (_("Out of memory whilst trying to convert the archive symbol index\n")); | |
565 | return FALSE; | |
566 | } | |
567 | ||
568 | for (i = 0; i < arch->index_num; i++) | |
569 | arch->index_array[i] = | |
570 | byte_get_big_endian ((unsigned char *) (index_buffer + (i * sizeof_ar_index)), | |
571 | sizeof_ar_index); | |
572 | free (index_buffer); | |
573 | ||
574 | /* The remaining space in the header is taken up by the symbol table. */ | |
575 | if (size < 1) | |
576 | { | |
577 | error (_("%s: the archive has an index but no symbols\n"), | |
578 | arch->file_name); | |
579 | return FALSE; | |
580 | } | |
581 | ||
582 | arch->sym_table = (char *) malloc (size); | |
583 | if (arch->sym_table == NULL) | |
584 | { | |
585 | error (_("Out of memory whilst trying to read archive index symbol table\n")); | |
586 | return FALSE; | |
587 | } | |
588 | ||
589 | arch->sym_size = size; | |
590 | got = fread (arch->sym_table, 1, size, arch->file); | |
591 | if (got != size) | |
592 | { | |
593 | error (_("%s: failed to read archive index symbol table\n"), | |
594 | arch->file_name); | |
595 | return FALSE; | |
596 | } | |
597 | } | |
598 | ||
599 | /* Read the next archive header. */ | |
600 | got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file); | |
601 | if (got != sizeof arch->arhdr && got != 0) | |
602 | { | |
603 | error (_("%s: failed to read archive header following archive index\n"), | |
604 | arch->file_name); | |
605 | return FALSE; | |
606 | } | |
607 | ||
608 | return TRUE; | |
609 | } | |
610 | ||
3284fe0c L |
611 | /* Read the symbol table and long-name table from an archive. */ |
612 | ||
613 | int | |
614 | setup_archive (struct archive_info *arch, const char *file_name, | |
645ba681 | 615 | FILE *file, off_t file_size, |
780f96ae | 616 | bfd_boolean is_thin_archive, bfd_boolean read_symbols) |
3284fe0c L |
617 | { |
618 | size_t got; | |
3284fe0c L |
619 | |
620 | arch->file_name = strdup (file_name); | |
621 | arch->file = file; | |
622 | arch->index_num = 0; | |
623 | arch->index_array = NULL; | |
624 | arch->sym_table = NULL; | |
625 | arch->sym_size = 0; | |
626 | arch->longnames = NULL; | |
627 | arch->longnames_size = 0; | |
628 | arch->nested_member_origin = 0; | |
629 | arch->is_thin_archive = is_thin_archive; | |
67ce483b | 630 | arch->uses_64bit_indices = FALSE; |
3284fe0c L |
631 | arch->next_arhdr_offset = SARMAG; |
632 | ||
633 | /* Read the first archive member header. */ | |
634 | if (fseek (file, SARMAG, SEEK_SET) != 0) | |
635 | { | |
636 | error (_("%s: failed to seek to first archive header\n"), file_name); | |
637 | return 1; | |
638 | } | |
639 | got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file); | |
640 | if (got != sizeof arch->arhdr) | |
641 | { | |
642 | if (got == 0) | |
643 | return 0; | |
644 | ||
645 | error (_("%s: failed to read archive header\n"), file_name); | |
646 | return 1; | |
647 | } | |
648 | ||
649 | /* See if this is the archive symbol table. */ | |
c2a7d3f5 | 650 | if (const_strneq (arch->arhdr.ar_name, "/ ")) |
3284fe0c | 651 | { |
c2a7d3f5 NC |
652 | if (! process_archive_index_and_symbols (arch, 4, read_symbols)) |
653 | return 1; | |
654 | } | |
655 | else if (const_strneq (arch->arhdr.ar_name, "/SYM64/ ")) | |
656 | { | |
67ce483b | 657 | arch->uses_64bit_indices = TRUE; |
c2a7d3f5 NC |
658 | if (! process_archive_index_and_symbols (arch, 8, read_symbols)) |
659 | return 1; | |
3284fe0c L |
660 | } |
661 | else if (read_symbols) | |
662 | printf (_("%s has no archive index\n"), file_name); | |
663 | ||
664 | if (const_strneq (arch->arhdr.ar_name, "// ")) | |
665 | { | |
666 | /* This is the archive string table holding long member names. */ | |
d91f0b20 AM |
667 | char fmag_save = arch->arhdr.ar_fmag[0]; |
668 | arch->arhdr.ar_fmag[0] = 0; | |
3284fe0c | 669 | arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10); |
d91f0b20 | 670 | arch->arhdr.ar_fmag[0] = fmag_save; |
591f7597 NC |
671 | /* PR 17531: file: 01068045. */ |
672 | if (arch->longnames_size < 8) | |
673 | { | |
674 | error (_("%s: long name table is too small, (size = %ld)\n"), | |
675 | file_name, arch->longnames_size); | |
676 | return 1; | |
677 | } | |
058037d3 | 678 | /* PR 17531: file: 639d6a26. */ |
645ba681 | 679 | if ((off_t) arch->longnames_size > file_size |
780f96ae | 680 | || (signed long) arch->longnames_size < 0) |
058037d3 NC |
681 | { |
682 | error (_("%s: long name table is too big, (size = 0x%lx)\n"), | |
683 | file_name, arch->longnames_size); | |
684 | return 1; | |
685 | } | |
686 | ||
3284fe0c L |
687 | arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size; |
688 | ||
591f7597 NC |
689 | /* Plus one to allow for a string terminator. */ |
690 | arch->longnames = (char *) malloc (arch->longnames_size + 1); | |
3284fe0c L |
691 | if (arch->longnames == NULL) |
692 | { | |
693 | error (_("Out of memory reading long symbol names in archive\n")); | |
694 | return 1; | |
695 | } | |
696 | ||
697 | if (fread (arch->longnames, arch->longnames_size, 1, file) != 1) | |
698 | { | |
699 | free (arch->longnames); | |
700 | arch->longnames = NULL; | |
701 | error (_("%s: failed to read long symbol name string table\n"), | |
702 | file_name); | |
703 | return 1; | |
704 | } | |
705 | ||
706 | if ((arch->longnames_size & 1) != 0) | |
707 | getc (file); | |
058037d3 NC |
708 | |
709 | arch->longnames[arch->longnames_size] = 0; | |
3284fe0c L |
710 | } |
711 | ||
712 | return 0; | |
713 | } | |
714 | ||
715 | /* Open and setup a nested archive, if not already open. */ | |
716 | ||
717 | int | |
718 | setup_nested_archive (struct archive_info *nested_arch, | |
719 | const char *member_file_name) | |
720 | { | |
721 | FILE * member_file; | |
780f96ae | 722 | struct stat statbuf; |
3284fe0c L |
723 | |
724 | /* Have we already setup this archive? */ | |
725 | if (nested_arch->file_name != NULL | |
726 | && streq (nested_arch->file_name, member_file_name)) | |
727 | return 0; | |
728 | ||
729 | /* Close previous file and discard cached information. */ | |
730 | if (nested_arch->file != NULL) | |
731 | fclose (nested_arch->file); | |
732 | release_archive (nested_arch); | |
733 | ||
734 | member_file = fopen (member_file_name, "rb"); | |
735 | if (member_file == NULL) | |
736 | return 1; | |
780f96ae AM |
737 | if (fstat (fileno (member_file), &statbuf) < 0) |
738 | return 1; | |
3284fe0c | 739 | return setup_archive (nested_arch, member_file_name, member_file, |
780f96ae | 740 | statbuf.st_size, FALSE, FALSE); |
3284fe0c L |
741 | } |
742 | ||
743 | /* Release the memory used for the archive information. */ | |
744 | ||
745 | void | |
746 | release_archive (struct archive_info * arch) | |
747 | { | |
748 | if (arch->file_name != NULL) | |
749 | free (arch->file_name); | |
750 | if (arch->index_array != NULL) | |
751 | free (arch->index_array); | |
752 | if (arch->sym_table != NULL) | |
753 | free (arch->sym_table); | |
754 | if (arch->longnames != NULL) | |
755 | free (arch->longnames); | |
756 | } | |
757 | ||
758 | /* Get the name of an archive member from the current archive header. | |
759 | For simple names, this will modify the ar_name field of the current | |
760 | archive header. For long names, it will return a pointer to the | |
761 | longnames table. For nested archives, it will open the nested archive | |
762 | and get the name recursively. NESTED_ARCH is a single-entry cache so | |
763 | we don't keep rereading the same information from a nested archive. */ | |
764 | ||
765 | char * | |
766 | get_archive_member_name (struct archive_info *arch, | |
767 | struct archive_info *nested_arch) | |
768 | { | |
769 | unsigned long j, k; | |
770 | ||
771 | if (arch->arhdr.ar_name[0] == '/') | |
772 | { | |
773 | /* We have a long name. */ | |
774 | char *endp; | |
775 | char *member_file_name; | |
776 | char *member_name; | |
d91f0b20 | 777 | char fmag_save; |
3284fe0c | 778 | |
907b01b7 NC |
779 | if (arch->longnames == NULL || arch->longnames_size == 0) |
780 | { | |
781 | error (_("Archive member uses long names, but no longname table found\n")); | |
782 | return NULL; | |
783 | } | |
3aade688 | 784 | |
3284fe0c | 785 | arch->nested_member_origin = 0; |
d91f0b20 AM |
786 | fmag_save = arch->arhdr.ar_fmag[0]; |
787 | arch->arhdr.ar_fmag[0] = 0; | |
3284fe0c L |
788 | k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10); |
789 | if (arch->is_thin_archive && endp != NULL && * endp == ':') | |
790 | arch->nested_member_origin = strtoul (endp + 1, NULL, 10); | |
d91f0b20 | 791 | arch->arhdr.ar_fmag[0] = fmag_save; |
3284fe0c | 792 | |
591f7597 NC |
793 | if (j > arch->longnames_size) |
794 | { | |
795 | error (_("Found long name index (%ld) beyond end of long name table\n"),j); | |
796 | return NULL; | |
797 | } | |
3284fe0c L |
798 | while ((j < arch->longnames_size) |
799 | && (arch->longnames[j] != '\n') | |
800 | && (arch->longnames[j] != '\0')) | |
801 | j++; | |
591f7597 | 802 | if (j > 0 && arch->longnames[j-1] == '/') |
3284fe0c | 803 | j--; |
591f7597 NC |
804 | if (j > arch->longnames_size) |
805 | j = arch->longnames_size; | |
3284fe0c L |
806 | arch->longnames[j] = '\0'; |
807 | ||
808 | if (!arch->is_thin_archive || arch->nested_member_origin == 0) | |
fd486f32 | 809 | return xstrdup (arch->longnames + k); |
3284fe0c | 810 | |
591f7597 NC |
811 | /* PR 17531: file: 2896dc8b. */ |
812 | if (k >= j) | |
813 | { | |
814 | error (_("Invalid Thin archive member name\n")); | |
815 | return NULL; | |
816 | } | |
3aade688 | 817 | |
3284fe0c L |
818 | /* This is a proxy for a member of a nested archive. |
819 | Find the name of the member in that archive. */ | |
820 | member_file_name = adjust_relative_path (arch->file_name, | |
821 | arch->longnames + k, j - k); | |
822 | if (member_file_name != NULL | |
823 | && setup_nested_archive (nested_arch, member_file_name) == 0) | |
824 | { | |
fd486f32 | 825 | member_name = get_archive_member_name_at (nested_arch, |
3284fe0c L |
826 | arch->nested_member_origin, |
827 | NULL); | |
828 | if (member_name != NULL) | |
829 | { | |
830 | free (member_file_name); | |
831 | return member_name; | |
832 | } | |
833 | } | |
834 | free (member_file_name); | |
835 | ||
836 | /* Last resort: just return the name of the nested archive. */ | |
fd486f32 | 837 | return xstrdup (arch->longnames + k); |
3284fe0c L |
838 | } |
839 | ||
840 | /* We have a normal (short) name. */ | |
841 | for (j = 0; j < sizeof (arch->arhdr.ar_name); j++) | |
842 | if (arch->arhdr.ar_name[j] == '/') | |
843 | { | |
844 | arch->arhdr.ar_name[j] = '\0'; | |
fd486f32 | 845 | return xstrdup (arch->arhdr.ar_name); |
3284fe0c L |
846 | } |
847 | ||
848 | /* The full ar_name field is used. Don't rely on ar_date starting | |
849 | with a zero byte. */ | |
850 | { | |
851 | char *name = xmalloc (sizeof (arch->arhdr.ar_name) + 1); | |
852 | memcpy (name, arch->arhdr.ar_name, sizeof (arch->arhdr.ar_name)); | |
853 | name[sizeof (arch->arhdr.ar_name)] = '\0'; | |
854 | return name; | |
855 | } | |
856 | } | |
857 | ||
858 | /* Get the name of an archive member at a given OFFSET within an archive | |
859 | ARCH. */ | |
860 | ||
861 | char * | |
862 | get_archive_member_name_at (struct archive_info *arch, | |
863 | unsigned long offset, | |
864 | struct archive_info *nested_arch) | |
865 | { | |
866 | size_t got; | |
867 | ||
868 | if (fseek (arch->file, offset, SEEK_SET) != 0) | |
869 | { | |
870 | error (_("%s: failed to seek to next file name\n"), arch->file_name); | |
871 | return NULL; | |
872 | } | |
873 | got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file); | |
874 | if (got != sizeof arch->arhdr) | |
875 | { | |
876 | error (_("%s: failed to read archive header\n"), arch->file_name); | |
877 | return NULL; | |
878 | } | |
879 | if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0) | |
880 | { | |
881 | error (_("%s: did not find a valid archive header\n"), | |
882 | arch->file_name); | |
883 | return NULL; | |
884 | } | |
885 | ||
886 | return get_archive_member_name (arch, nested_arch); | |
887 | } | |
888 | ||
889 | /* Construct a string showing the name of the archive member, qualified | |
890 | with the name of the containing archive file. For thin archives, we | |
891 | use square brackets to denote the indirection. For nested archives, | |
892 | we show the qualified name of the external member inside the square | |
893 | brackets (e.g., "thin.a[normal.a(foo.o)]"). */ | |
894 | ||
895 | char * | |
896 | make_qualified_name (struct archive_info * arch, | |
897 | struct archive_info * nested_arch, | |
898 | const char *member_name) | |
899 | { | |
a043396b | 900 | const char * error_name = _("<corrupt>"); |
3284fe0c L |
901 | size_t len; |
902 | char * name; | |
903 | ||
904 | len = strlen (arch->file_name) + strlen (member_name) + 3; | |
a043396b NC |
905 | if (arch->is_thin_archive |
906 | && arch->nested_member_origin != 0) | |
907 | { | |
908 | /* PR 15140: Allow for corrupt thin archives. */ | |
909 | if (nested_arch->file_name) | |
910 | len += strlen (nested_arch->file_name) + 2; | |
911 | else | |
912 | len += strlen (error_name) + 2; | |
913 | } | |
3284fe0c L |
914 | |
915 | name = (char *) malloc (len); | |
916 | if (name == NULL) | |
917 | { | |
918 | error (_("Out of memory\n")); | |
919 | return NULL; | |
920 | } | |
921 | ||
a043396b NC |
922 | if (arch->is_thin_archive |
923 | && arch->nested_member_origin != 0) | |
924 | { | |
925 | if (nested_arch->file_name) | |
926 | snprintf (name, len, "%s[%s(%s)]", arch->file_name, | |
927 | nested_arch->file_name, member_name); | |
928 | else | |
929 | snprintf (name, len, "%s[%s(%s)]", arch->file_name, | |
3aade688 | 930 | error_name, member_name); |
a043396b | 931 | } |
3284fe0c L |
932 | else if (arch->is_thin_archive) |
933 | snprintf (name, len, "%s[%s]", arch->file_name, member_name); | |
934 | else | |
935 | snprintf (name, len, "%s(%s)", arch->file_name, member_name); | |
936 | ||
937 | return name; | |
938 | } |