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