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