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