1 /* elfcomm.c -- common code for ELF format file.
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
7 This file is part of GNU Binutils.
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.
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.
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
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
29 #include "libiberty.h"
30 #include "filenames.h"
35 extern char *program_name
;
38 error (const char *message
, ...)
42 /* Try to keep error messages in sync with the program's normal output. */
45 va_start (args
, message
);
46 fprintf (stderr
, _("%s: Error: "), program_name
);
47 vfprintf (stderr
, message
, args
);
52 warn (const char *message
, ...)
56 /* Try to keep warning messages in sync with the program's normal output. */
59 va_start (args
, message
);
60 fprintf (stderr
, _("%s: Warning: "), program_name
);
61 vfprintf (stderr
, message
, args
);
65 void (*byte_put
) (unsigned char *, elf_vma
, int);
68 byte_put_little_endian (unsigned char * field
, elf_vma value
, int size
)
73 field
[7] = (((value
>> 24) >> 24) >> 8) & 0xff;
74 field
[6] = ((value
>> 24) >> 24) & 0xff;
75 field
[5] = ((value
>> 24) >> 16) & 0xff;
76 field
[4] = ((value
>> 24) >> 8) & 0xff;
79 field
[3] = (value
>> 24) & 0xff;
82 field
[2] = (value
>> 16) & 0xff;
85 field
[1] = (value
>> 8) & 0xff;
88 field
[0] = value
& 0xff;
92 error (_("Unhandled data length: %d\n"), size
);
98 byte_put_big_endian (unsigned char * field
, elf_vma value
, int size
)
103 field
[7] = value
& 0xff;
104 field
[6] = (value
>> 8) & 0xff;
105 field
[5] = (value
>> 16) & 0xff;
106 field
[4] = (value
>> 24) & 0xff;
111 field
[3] = value
& 0xff;
115 field
[2] = value
& 0xff;
119 field
[1] = value
& 0xff;
123 field
[0] = value
& 0xff;
127 error (_("Unhandled data length: %d\n"), size
);
132 elf_vma (*byte_get
) (const unsigned char *, int);
135 byte_get_little_endian (const unsigned char *field
, int size
)
143 return ((unsigned int) (field
[0]))
144 | (((unsigned int) (field
[1])) << 8);
147 return ((unsigned long) (field
[0]))
148 | (((unsigned long) (field
[1])) << 8)
149 | (((unsigned long) (field
[2])) << 16);
152 return ((unsigned long) (field
[0]))
153 | (((unsigned long) (field
[1])) << 8)
154 | (((unsigned long) (field
[2])) << 16)
155 | (((unsigned long) (field
[3])) << 24);
158 if (sizeof (elf_vma
) == 8)
159 return ((elf_vma
) (field
[0]))
160 | (((elf_vma
) (field
[1])) << 8)
161 | (((elf_vma
) (field
[2])) << 16)
162 | (((elf_vma
) (field
[3])) << 24)
163 | (((elf_vma
) (field
[4])) << 32);
164 else if (sizeof (elf_vma
) == 4)
165 /* We want to extract data from an 8 byte wide field and
166 place it into a 4 byte wide field. Since this is a little
167 endian source we can just use the 4 byte extraction code. */
168 return ((unsigned long) (field
[0]))
169 | (((unsigned long) (field
[1])) << 8)
170 | (((unsigned long) (field
[2])) << 16)
171 | (((unsigned long) (field
[3])) << 24);
175 if (sizeof (elf_vma
) == 8)
176 return ((elf_vma
) (field
[0]))
177 | (((elf_vma
) (field
[1])) << 8)
178 | (((elf_vma
) (field
[2])) << 16)
179 | (((elf_vma
) (field
[3])) << 24)
180 | (((elf_vma
) (field
[4])) << 32)
181 | (((elf_vma
) (field
[5])) << 40);
182 else if (sizeof (elf_vma
) == 4)
183 /* We want to extract data from an 8 byte wide field and
184 place it into a 4 byte wide field. Since this is a little
185 endian source we can just use the 4 byte extraction code. */
186 return ((unsigned long) (field
[0]))
187 | (((unsigned long) (field
[1])) << 8)
188 | (((unsigned long) (field
[2])) << 16)
189 | (((unsigned long) (field
[3])) << 24);
193 if (sizeof (elf_vma
) == 8)
194 return ((elf_vma
) (field
[0]))
195 | (((elf_vma
) (field
[1])) << 8)
196 | (((elf_vma
) (field
[2])) << 16)
197 | (((elf_vma
) (field
[3])) << 24)
198 | (((elf_vma
) (field
[4])) << 32)
199 | (((elf_vma
) (field
[5])) << 40)
200 | (((elf_vma
) (field
[6])) << 48);
201 else if (sizeof (elf_vma
) == 4)
202 /* We want to extract data from an 8 byte wide field and
203 place it into a 4 byte wide field. Since this is a little
204 endian source we can just use the 4 byte extraction code. */
205 return ((unsigned long) (field
[0]))
206 | (((unsigned long) (field
[1])) << 8)
207 | (((unsigned long) (field
[2])) << 16)
208 | (((unsigned long) (field
[3])) << 24);
212 if (sizeof (elf_vma
) == 8)
213 return ((elf_vma
) (field
[0]))
214 | (((elf_vma
) (field
[1])) << 8)
215 | (((elf_vma
) (field
[2])) << 16)
216 | (((elf_vma
) (field
[3])) << 24)
217 | (((elf_vma
) (field
[4])) << 32)
218 | (((elf_vma
) (field
[5])) << 40)
219 | (((elf_vma
) (field
[6])) << 48)
220 | (((elf_vma
) (field
[7])) << 56);
221 else if (sizeof (elf_vma
) == 4)
222 /* We want to extract data from an 8 byte wide field and
223 place it into a 4 byte wide field. Since this is a little
224 endian source we can just use the 4 byte extraction code. */
225 return ((unsigned long) (field
[0]))
226 | (((unsigned long) (field
[1])) << 8)
227 | (((unsigned long) (field
[2])) << 16)
228 | (((unsigned long) (field
[3])) << 24);
232 error (_("Unhandled data length: %d\n"), size
);
238 byte_get_big_endian (const unsigned char *field
, int size
)
246 return ((unsigned int) (field
[1])) | (((int) (field
[0])) << 8);
249 return ((unsigned long) (field
[2]))
250 | (((unsigned long) (field
[1])) << 8)
251 | (((unsigned long) (field
[0])) << 16);
254 return ((unsigned long) (field
[3]))
255 | (((unsigned long) (field
[2])) << 8)
256 | (((unsigned long) (field
[1])) << 16)
257 | (((unsigned long) (field
[0])) << 24);
260 if (sizeof (elf_vma
) == 8)
261 return ((elf_vma
) (field
[4]))
262 | (((elf_vma
) (field
[3])) << 8)
263 | (((elf_vma
) (field
[2])) << 16)
264 | (((elf_vma
) (field
[1])) << 24)
265 | (((elf_vma
) (field
[0])) << 32);
266 else if (sizeof (elf_vma
) == 4)
268 /* Although we are extracting data from an 8 byte wide field,
269 we are returning only 4 bytes of data. */
271 return ((unsigned long) (field
[3]))
272 | (((unsigned long) (field
[2])) << 8)
273 | (((unsigned long) (field
[1])) << 16)
274 | (((unsigned long) (field
[0])) << 24);
279 if (sizeof (elf_vma
) == 8)
280 return ((elf_vma
) (field
[5]))
281 | (((elf_vma
) (field
[4])) << 8)
282 | (((elf_vma
) (field
[3])) << 16)
283 | (((elf_vma
) (field
[2])) << 24)
284 | (((elf_vma
) (field
[1])) << 32)
285 | (((elf_vma
) (field
[0])) << 40);
286 else if (sizeof (elf_vma
) == 4)
288 /* Although we are extracting data from an 8 byte wide field,
289 we are returning only 4 bytes of data. */
291 return ((unsigned long) (field
[3]))
292 | (((unsigned long) (field
[2])) << 8)
293 | (((unsigned long) (field
[1])) << 16)
294 | (((unsigned long) (field
[0])) << 24);
299 if (sizeof (elf_vma
) == 8)
300 return ((elf_vma
) (field
[6]))
301 | (((elf_vma
) (field
[5])) << 8)
302 | (((elf_vma
) (field
[4])) << 16)
303 | (((elf_vma
) (field
[3])) << 24)
304 | (((elf_vma
) (field
[2])) << 32)
305 | (((elf_vma
) (field
[1])) << 40)
306 | (((elf_vma
) (field
[0])) << 48);
307 else if (sizeof (elf_vma
) == 4)
309 /* Although we are extracting data from an 8 byte wide field,
310 we are returning only 4 bytes of data. */
312 return ((unsigned long) (field
[3]))
313 | (((unsigned long) (field
[2])) << 8)
314 | (((unsigned long) (field
[1])) << 16)
315 | (((unsigned long) (field
[0])) << 24);
320 if (sizeof (elf_vma
) == 8)
321 return ((elf_vma
) (field
[7]))
322 | (((elf_vma
) (field
[6])) << 8)
323 | (((elf_vma
) (field
[5])) << 16)
324 | (((elf_vma
) (field
[4])) << 24)
325 | (((elf_vma
) (field
[3])) << 32)
326 | (((elf_vma
) (field
[2])) << 40)
327 | (((elf_vma
) (field
[1])) << 48)
328 | (((elf_vma
) (field
[0])) << 56);
329 else if (sizeof (elf_vma
) == 4)
331 /* Although we are extracting data from an 8 byte wide field,
332 we are returning only 4 bytes of data. */
334 return ((unsigned long) (field
[3]))
335 | (((unsigned long) (field
[2])) << 8)
336 | (((unsigned long) (field
[1])) << 16)
337 | (((unsigned long) (field
[0])) << 24);
342 error (_("Unhandled data length: %d\n"), size
);
348 byte_get_signed (const unsigned char *field
, int size
)
350 elf_vma x
= byte_get (field
, size
);
355 return (x
^ 0x80) - 0x80;
357 return (x
^ 0x8000) - 0x8000;
359 return (x
^ 0x800000) - 0x800000;
361 return (x
^ 0x80000000) - 0x80000000;
366 /* Reads of 5-, 6-, and 7-byte numbers are the result of
367 trying to read past the end of a buffer, and will therefore
368 not have meaningful values, so we don't try to deal with
369 the sign in these cases. */
376 /* Return the high-order 32-bits and the low-order 32-bits
377 of an 8-byte value separately. */
380 byte_get_64 (const unsigned char *field
, elf_vma
*high
, elf_vma
*low
)
382 if (byte_get
== byte_get_big_endian
)
384 *high
= byte_get_big_endian (field
, 4);
385 *low
= byte_get_big_endian (field
+ 4, 4);
389 *high
= byte_get_little_endian (field
+ 4, 4);
390 *low
= byte_get_little_endian (field
, 4);
395 /* Return the path name for a proxy entry in a thin archive, adjusted
396 relative to the path name of the thin archive itself if necessary.
397 Always returns a pointer to malloc'ed memory. */
400 adjust_relative_path (const char *file_name
, const char *name
,
401 unsigned long name_len
)
403 char * member_file_name
;
404 const char * base_name
= lbasename (file_name
);
407 /* This is a proxy entry for a thin archive member.
408 If the extended name table contains an absolute path
409 name, or if the archive is in the current directory,
410 use the path name as given. Otherwise, we need to
411 find the member relative to the directory where the
412 archive is located. */
413 if (IS_ABSOLUTE_PATH (name
) || base_name
== file_name
)
418 member_file_name
= (char *) malloc (amt
);
419 if (member_file_name
== NULL
)
421 error (_("Out of memory\n"));
424 memcpy (member_file_name
, name
, name_len
);
425 member_file_name
[name_len
] = '\0';
429 /* Concatenate the path components of the archive file name
430 to the relative path name from the extended name table. */
431 size_t prefix_len
= base_name
- file_name
;
433 amt
= prefix_len
+ name_len
+ 1;
434 /* PR 17531: file: 2896dc8b
436 if (amt
< prefix_len
|| amt
< name_len
)
438 error (_("Abnormal length of thin archive member name: %lx\n"),
443 member_file_name
= (char *) malloc (amt
);
444 if (member_file_name
== NULL
)
446 error (_("Out of memory\n"));
449 memcpy (member_file_name
, file_name
, prefix_len
);
450 memcpy (member_file_name
+ prefix_len
, name
, name_len
);
451 member_file_name
[prefix_len
+ name_len
] = '\0';
453 return member_file_name
;
456 /* Processes the archive index table and symbol table in ARCH.
457 Entries in the index table are SIZEOF_AR_INDEX bytes long.
458 Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
459 If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
460 ARCH->sym_size and ARCH->sym_table.
461 It is the caller's responsibility to free ARCH->index_array and
463 Returns 1 upon success, 0 otherwise.
464 If failure occurs an error message is printed. */
467 process_archive_index_and_symbols (struct archive_info
*arch
,
468 unsigned int sizeof_ar_index
,
475 fmag_save
= arch
->arhdr
.ar_fmag
[0];
476 arch
->arhdr
.ar_fmag
[0] = 0;
477 size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
478 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
479 /* PR 17531: file: 912bd7de. */
480 if ((signed long) size
< 0)
482 error (_("%s: invalid archive header size: %ld\n"),
483 arch
->file_name
, size
);
487 size
= size
+ (size
& 1);
489 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ size
;
493 if (fseek (arch
->file
, size
, SEEK_CUR
) != 0)
495 error (_("%s: failed to skip archive symbol table\n"),
503 /* A buffer used to hold numbers read in from an archive index.
504 These are always SIZEOF_AR_INDEX bytes long and stored in
505 big-endian format. */
506 unsigned char integer_buffer
[sizeof arch
->index_num
];
507 unsigned char * index_buffer
;
509 assert (sizeof_ar_index
<= sizeof integer_buffer
);
511 /* Check the size of the archive index. */
512 if (size
< sizeof_ar_index
)
514 error (_("%s: the archive index is empty\n"), arch
->file_name
);
518 /* Read the number of entries in the archive index. */
519 got
= fread (integer_buffer
, 1, sizeof_ar_index
, arch
->file
);
520 if (got
!= sizeof_ar_index
)
522 error (_("%s: failed to read archive index\n"), arch
->file_name
);
526 arch
->index_num
= byte_get_big_endian (integer_buffer
, sizeof_ar_index
);
527 size
-= sizeof_ar_index
;
529 if (size
< arch
->index_num
* sizeof_ar_index
530 /* PR 17531: file: 585515d1. */
531 || size
< arch
->index_num
)
533 error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
534 arch
->file_name
, (long) arch
->index_num
, sizeof_ar_index
, size
);
538 /* Read in the archive index. */
539 index_buffer
= (unsigned char *)
540 malloc (arch
->index_num
* sizeof_ar_index
);
541 if (index_buffer
== NULL
)
543 error (_("Out of memory whilst trying to read archive symbol index\n"));
547 got
= fread (index_buffer
, sizeof_ar_index
, arch
->index_num
, arch
->file
);
548 if (got
!= arch
->index_num
)
551 error (_("%s: failed to read archive index\n"), arch
->file_name
);
555 size
-= arch
->index_num
* sizeof_ar_index
;
557 /* Convert the index numbers into the host's numeric format. */
558 arch
->index_array
= (elf_vma
*)
559 malloc (arch
->index_num
* sizeof (* arch
->index_array
));
560 if (arch
->index_array
== NULL
)
563 error (_("Out of memory whilst trying to convert the archive symbol index\n"));
567 for (i
= 0; i
< arch
->index_num
; i
++)
568 arch
->index_array
[i
] =
569 byte_get_big_endian ((unsigned char *) (index_buffer
+ (i
* sizeof_ar_index
)),
573 /* The remaining space in the header is taken up by the symbol table. */
576 error (_("%s: the archive has an index but no symbols\n"),
581 arch
->sym_table
= (char *) malloc (size
);
582 if (arch
->sym_table
== NULL
)
584 error (_("Out of memory whilst trying to read archive index symbol table\n"));
588 arch
->sym_size
= size
;
589 got
= fread (arch
->sym_table
, 1, size
, arch
->file
);
592 error (_("%s: failed to read archive index symbol table\n"),
598 /* Read the next archive header. */
599 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
600 if (got
!= sizeof arch
->arhdr
&& got
!= 0)
602 error (_("%s: failed to read archive header following archive index\n"),
610 /* Read the symbol table and long-name table from an archive. */
613 setup_archive (struct archive_info
*arch
, const char *file_name
,
614 FILE *file
, off_t file_size
,
615 int is_thin_archive
, int read_symbols
)
619 arch
->file_name
= strdup (file_name
);
622 arch
->index_array
= NULL
;
623 arch
->sym_table
= NULL
;
625 arch
->longnames
= NULL
;
626 arch
->longnames_size
= 0;
627 arch
->nested_member_origin
= 0;
628 arch
->is_thin_archive
= is_thin_archive
;
629 arch
->uses_64bit_indices
= 0;
630 arch
->next_arhdr_offset
= SARMAG
;
632 /* Read the first archive member header. */
633 if (fseek (file
, SARMAG
, SEEK_SET
) != 0)
635 error (_("%s: failed to seek to first archive header\n"), file_name
);
638 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, file
);
639 if (got
!= sizeof arch
->arhdr
)
644 error (_("%s: failed to read archive header\n"), file_name
);
648 /* See if this is the archive symbol table. */
649 if (const_strneq (arch
->arhdr
.ar_name
, "/ "))
651 if (! process_archive_index_and_symbols (arch
, 4, read_symbols
))
654 else if (const_strneq (arch
->arhdr
.ar_name
, "/SYM64/ "))
656 arch
->uses_64bit_indices
= 1;
657 if (! process_archive_index_and_symbols (arch
, 8, read_symbols
))
660 else if (read_symbols
)
661 printf (_("%s has no archive index\n"), file_name
);
663 if (const_strneq (arch
->arhdr
.ar_name
, "// "))
665 /* This is the archive string table holding long member names. */
666 char fmag_save
= arch
->arhdr
.ar_fmag
[0];
667 arch
->arhdr
.ar_fmag
[0] = 0;
668 arch
->longnames_size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
669 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
670 /* PR 17531: file: 01068045. */
671 if (arch
->longnames_size
< 8)
673 error (_("%s: long name table is too small, (size = %ld)\n"),
674 file_name
, arch
->longnames_size
);
677 /* PR 17531: file: 639d6a26. */
678 if ((off_t
) arch
->longnames_size
> file_size
679 || (signed long) arch
->longnames_size
< 0)
681 error (_("%s: long name table is too big, (size = 0x%lx)\n"),
682 file_name
, arch
->longnames_size
);
686 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ arch
->longnames_size
;
688 /* Plus one to allow for a string terminator. */
689 arch
->longnames
= (char *) malloc (arch
->longnames_size
+ 1);
690 if (arch
->longnames
== NULL
)
692 error (_("Out of memory reading long symbol names in archive\n"));
696 if (fread (arch
->longnames
, arch
->longnames_size
, 1, file
) != 1)
698 free (arch
->longnames
);
699 arch
->longnames
= NULL
;
700 error (_("%s: failed to read long symbol name string table\n"),
705 if ((arch
->longnames_size
& 1) != 0)
708 arch
->longnames
[arch
->longnames_size
] = 0;
714 /* Open and setup a nested archive, if not already open. */
717 setup_nested_archive (struct archive_info
*nested_arch
,
718 const char *member_file_name
)
723 /* Have we already setup this archive? */
724 if (nested_arch
->file_name
!= NULL
725 && streq (nested_arch
->file_name
, member_file_name
))
728 /* Close previous file and discard cached information. */
729 if (nested_arch
->file
!= NULL
)
730 fclose (nested_arch
->file
);
731 release_archive (nested_arch
);
733 member_file
= fopen (member_file_name
, "rb");
734 if (member_file
== NULL
)
736 if (fstat (fileno (member_file
), &statbuf
) < 0)
738 return setup_archive (nested_arch
, member_file_name
, member_file
,
739 statbuf
.st_size
, 0, 0);
742 /* Release the memory used for the archive information. */
745 release_archive (struct archive_info
* arch
)
747 free (arch
->file_name
);
748 free (arch
->index_array
);
749 free (arch
->sym_table
);
750 free (arch
->longnames
);
753 /* Get the name of an archive member from the current archive header.
754 For simple names, this will modify the ar_name field of the current
755 archive header. For long names, it will return a pointer to the
756 longnames table. For nested archives, it will open the nested archive
757 and get the name recursively. NESTED_ARCH is a single-entry cache so
758 we don't keep rereading the same information from a nested archive. */
761 get_archive_member_name (struct archive_info
*arch
,
762 struct archive_info
*nested_arch
)
766 if (arch
->arhdr
.ar_name
[0] == '/')
768 /* We have a long name. */
770 char *member_file_name
;
774 if (arch
->longnames
== NULL
|| arch
->longnames_size
== 0)
776 error (_("Archive member uses long names, but no longname table found\n"));
780 arch
->nested_member_origin
= 0;
781 fmag_save
= arch
->arhdr
.ar_fmag
[0];
782 arch
->arhdr
.ar_fmag
[0] = 0;
783 k
= j
= strtoul (arch
->arhdr
.ar_name
+ 1, &endp
, 10);
784 if (arch
->is_thin_archive
&& endp
!= NULL
&& * endp
== ':')
785 arch
->nested_member_origin
= strtoul (endp
+ 1, NULL
, 10);
786 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
788 if (j
> arch
->longnames_size
)
790 error (_("Found long name index (%ld) beyond end of long name table\n"),j
);
793 while ((j
< arch
->longnames_size
)
794 && (arch
->longnames
[j
] != '\n')
795 && (arch
->longnames
[j
] != '\0'))
797 if (j
> 0 && arch
->longnames
[j
-1] == '/')
799 if (j
> arch
->longnames_size
)
800 j
= arch
->longnames_size
;
801 arch
->longnames
[j
] = '\0';
803 if (!arch
->is_thin_archive
|| arch
->nested_member_origin
== 0)
804 return xstrdup (arch
->longnames
+ k
);
806 /* PR 17531: file: 2896dc8b. */
809 error (_("Invalid Thin archive member name\n"));
813 /* This is a proxy for a member of a nested archive.
814 Find the name of the member in that archive. */
815 member_file_name
= adjust_relative_path (arch
->file_name
,
816 arch
->longnames
+ k
, j
- k
);
817 if (member_file_name
!= NULL
818 && setup_nested_archive (nested_arch
, member_file_name
) == 0)
820 member_name
= get_archive_member_name_at (nested_arch
,
821 arch
->nested_member_origin
,
823 if (member_name
!= NULL
)
825 free (member_file_name
);
829 free (member_file_name
);
831 /* Last resort: just return the name of the nested archive. */
832 return xstrdup (arch
->longnames
+ k
);
835 /* We have a normal (short) name. */
836 for (j
= 0; j
< sizeof (arch
->arhdr
.ar_name
); j
++)
837 if (arch
->arhdr
.ar_name
[j
] == '/')
839 arch
->arhdr
.ar_name
[j
] = '\0';
840 return xstrdup (arch
->arhdr
.ar_name
);
843 /* The full ar_name field is used. Don't rely on ar_date starting
846 char *name
= xmalloc (sizeof (arch
->arhdr
.ar_name
) + 1);
847 memcpy (name
, arch
->arhdr
.ar_name
, sizeof (arch
->arhdr
.ar_name
));
848 name
[sizeof (arch
->arhdr
.ar_name
)] = '\0';
853 /* Get the name of an archive member at a given OFFSET within an archive
857 get_archive_member_name_at (struct archive_info
*arch
,
858 unsigned long offset
,
859 struct archive_info
*nested_arch
)
863 if (fseek (arch
->file
, offset
, SEEK_SET
) != 0)
865 error (_("%s: failed to seek to next file name\n"), arch
->file_name
);
868 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
869 if (got
!= sizeof arch
->arhdr
)
871 error (_("%s: failed to read archive header\n"), arch
->file_name
);
874 if (memcmp (arch
->arhdr
.ar_fmag
, ARFMAG
, 2) != 0)
876 error (_("%s: did not find a valid archive header\n"),
881 return get_archive_member_name (arch
, nested_arch
);
884 /* Construct a string showing the name of the archive member, qualified
885 with the name of the containing archive file. For thin archives, we
886 use square brackets to denote the indirection. For nested archives,
887 we show the qualified name of the external member inside the square
888 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
891 make_qualified_name (struct archive_info
* arch
,
892 struct archive_info
* nested_arch
,
893 const char *member_name
)
895 const char * error_name
= _("<corrupt>");
899 len
= strlen (arch
->file_name
) + strlen (member_name
) + 3;
900 if (arch
->is_thin_archive
901 && arch
->nested_member_origin
!= 0)
903 /* PR 15140: Allow for corrupt thin archives. */
904 if (nested_arch
->file_name
)
905 len
+= strlen (nested_arch
->file_name
) + 2;
907 len
+= strlen (error_name
) + 2;
910 name
= (char *) malloc (len
);
913 error (_("Out of memory\n"));
917 if (arch
->is_thin_archive
918 && arch
->nested_member_origin
!= 0)
920 if (nested_arch
->file_name
)
921 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
922 nested_arch
->file_name
, member_name
);
924 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
925 error_name
, member_name
);
927 else if (arch
->is_thin_archive
)
928 snprintf (name
, len
, "%s[%s]", arch
->file_name
, member_name
);
930 snprintf (name
, len
, "%s(%s)", arch
->file_name
, member_name
);