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