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 use BFD types in this file that differ in size depending on
25 whether BFD64 is defined. Functions in this file are used by
26 readelf.c and elfedit.c which define BFD64, and by objdump.c which
30 #include "libiberty.h"
31 #include "filenames.h"
39 error (const char *message
, ...)
43 /* Try to keep error messages in sync with the program's normal output. */
46 va_start (args
, message
);
47 fprintf (stderr
, _("%s: Error: "), program_name
);
48 vfprintf (stderr
, message
, args
);
53 warn (const char *message
, ...)
57 /* Try to keep warning messages in sync with the program's normal output. */
60 va_start (args
, message
);
61 fprintf (stderr
, _("%s: Warning: "), program_name
);
62 vfprintf (stderr
, message
, args
);
66 void (*byte_put
) (unsigned char *, elf_vma
, int);
69 byte_put_little_endian (unsigned char * field
, elf_vma value
, int size
)
74 field
[7] = (((value
>> 24) >> 24) >> 8) & 0xff;
75 field
[6] = ((value
>> 24) >> 24) & 0xff;
76 field
[5] = ((value
>> 24) >> 16) & 0xff;
77 field
[4] = ((value
>> 24) >> 8) & 0xff;
80 field
[3] = (value
>> 24) & 0xff;
83 field
[2] = (value
>> 16) & 0xff;
86 field
[1] = (value
>> 8) & 0xff;
89 field
[0] = value
& 0xff;
93 error (_("Unhandled data length: %d\n"), size
);
99 byte_put_big_endian (unsigned char * field
, elf_vma value
, int size
)
104 field
[7] = value
& 0xff;
105 field
[6] = (value
>> 8) & 0xff;
106 field
[5] = (value
>> 16) & 0xff;
107 field
[4] = (value
>> 24) & 0xff;
112 field
[3] = value
& 0xff;
116 field
[2] = value
& 0xff;
120 field
[1] = value
& 0xff;
124 field
[0] = value
& 0xff;
128 error (_("Unhandled data length: %d\n"), size
);
133 elf_vma (*byte_get
) (const unsigned char *, int);
136 byte_get_little_endian (const unsigned char *field
, int size
)
144 return ((unsigned int) (field
[0]))
145 | (((unsigned int) (field
[1])) << 8);
148 return ((unsigned long) (field
[0]))
149 | (((unsigned long) (field
[1])) << 8)
150 | (((unsigned long) (field
[2])) << 16);
153 return ((unsigned long) (field
[0]))
154 | (((unsigned long) (field
[1])) << 8)
155 | (((unsigned long) (field
[2])) << 16)
156 | (((unsigned long) (field
[3])) << 24);
159 if (sizeof (elf_vma
) == 8)
160 return ((elf_vma
) (field
[0]))
161 | (((elf_vma
) (field
[1])) << 8)
162 | (((elf_vma
) (field
[2])) << 16)
163 | (((elf_vma
) (field
[3])) << 24)
164 | (((elf_vma
) (field
[4])) << 32);
165 else if (sizeof (elf_vma
) == 4)
166 /* We want to extract data from an 8 byte wide field and
167 place it into a 4 byte wide field. Since this is a little
168 endian source we can just use the 4 byte extraction code. */
169 return ((unsigned long) (field
[0]))
170 | (((unsigned long) (field
[1])) << 8)
171 | (((unsigned long) (field
[2])) << 16)
172 | (((unsigned long) (field
[3])) << 24);
176 if (sizeof (elf_vma
) == 8)
177 return ((elf_vma
) (field
[0]))
178 | (((elf_vma
) (field
[1])) << 8)
179 | (((elf_vma
) (field
[2])) << 16)
180 | (((elf_vma
) (field
[3])) << 24)
181 | (((elf_vma
) (field
[4])) << 32)
182 | (((elf_vma
) (field
[5])) << 40);
183 else if (sizeof (elf_vma
) == 4)
184 /* We want to extract data from an 8 byte wide field and
185 place it into a 4 byte wide field. Since this is a little
186 endian source we can just use the 4 byte extraction code. */
187 return ((unsigned long) (field
[0]))
188 | (((unsigned long) (field
[1])) << 8)
189 | (((unsigned long) (field
[2])) << 16)
190 | (((unsigned long) (field
[3])) << 24);
194 if (sizeof (elf_vma
) == 8)
195 return ((elf_vma
) (field
[0]))
196 | (((elf_vma
) (field
[1])) << 8)
197 | (((elf_vma
) (field
[2])) << 16)
198 | (((elf_vma
) (field
[3])) << 24)
199 | (((elf_vma
) (field
[4])) << 32)
200 | (((elf_vma
) (field
[5])) << 40)
201 | (((elf_vma
) (field
[6])) << 48);
202 else if (sizeof (elf_vma
) == 4)
203 /* We want to extract data from an 8 byte wide field and
204 place it into a 4 byte wide field. Since this is a little
205 endian source we can just use the 4 byte extraction code. */
206 return ((unsigned long) (field
[0]))
207 | (((unsigned long) (field
[1])) << 8)
208 | (((unsigned long) (field
[2])) << 16)
209 | (((unsigned long) (field
[3])) << 24);
213 if (sizeof (elf_vma
) == 8)
214 return ((elf_vma
) (field
[0]))
215 | (((elf_vma
) (field
[1])) << 8)
216 | (((elf_vma
) (field
[2])) << 16)
217 | (((elf_vma
) (field
[3])) << 24)
218 | (((elf_vma
) (field
[4])) << 32)
219 | (((elf_vma
) (field
[5])) << 40)
220 | (((elf_vma
) (field
[6])) << 48)
221 | (((elf_vma
) (field
[7])) << 56);
222 else if (sizeof (elf_vma
) == 4)
223 /* We want to extract data from an 8 byte wide field and
224 place it into a 4 byte wide field. Since this is a little
225 endian source we can just use the 4 byte extraction code. */
226 return ((unsigned long) (field
[0]))
227 | (((unsigned long) (field
[1])) << 8)
228 | (((unsigned long) (field
[2])) << 16)
229 | (((unsigned long) (field
[3])) << 24);
233 error (_("Unhandled data length: %d\n"), size
);
239 byte_get_big_endian (const unsigned char *field
, int size
)
247 return ((unsigned int) (field
[1])) | (((int) (field
[0])) << 8);
250 return ((unsigned long) (field
[2]))
251 | (((unsigned long) (field
[1])) << 8)
252 | (((unsigned long) (field
[0])) << 16);
255 return ((unsigned long) (field
[3]))
256 | (((unsigned long) (field
[2])) << 8)
257 | (((unsigned long) (field
[1])) << 16)
258 | (((unsigned long) (field
[0])) << 24);
261 if (sizeof (elf_vma
) == 8)
262 return ((elf_vma
) (field
[4]))
263 | (((elf_vma
) (field
[3])) << 8)
264 | (((elf_vma
) (field
[2])) << 16)
265 | (((elf_vma
) (field
[1])) << 24)
266 | (((elf_vma
) (field
[0])) << 32);
267 else if (sizeof (elf_vma
) == 4)
269 /* Although we are extracting data from an 8 byte wide field,
270 we are returning only 4 bytes of data. */
272 return ((unsigned long) (field
[3]))
273 | (((unsigned long) (field
[2])) << 8)
274 | (((unsigned long) (field
[1])) << 16)
275 | (((unsigned long) (field
[0])) << 24);
280 if (sizeof (elf_vma
) == 8)
281 return ((elf_vma
) (field
[5]))
282 | (((elf_vma
) (field
[4])) << 8)
283 | (((elf_vma
) (field
[3])) << 16)
284 | (((elf_vma
) (field
[2])) << 24)
285 | (((elf_vma
) (field
[1])) << 32)
286 | (((elf_vma
) (field
[0])) << 40);
287 else if (sizeof (elf_vma
) == 4)
289 /* Although we are extracting data from an 8 byte wide field,
290 we are returning only 4 bytes of data. */
292 return ((unsigned long) (field
[3]))
293 | (((unsigned long) (field
[2])) << 8)
294 | (((unsigned long) (field
[1])) << 16)
295 | (((unsigned long) (field
[0])) << 24);
300 if (sizeof (elf_vma
) == 8)
301 return ((elf_vma
) (field
[6]))
302 | (((elf_vma
) (field
[5])) << 8)
303 | (((elf_vma
) (field
[4])) << 16)
304 | (((elf_vma
) (field
[3])) << 24)
305 | (((elf_vma
) (field
[2])) << 32)
306 | (((elf_vma
) (field
[1])) << 40)
307 | (((elf_vma
) (field
[0])) << 48);
308 else if (sizeof (elf_vma
) == 4)
310 /* Although we are extracting data from an 8 byte wide field,
311 we are returning only 4 bytes of data. */
313 return ((unsigned long) (field
[3]))
314 | (((unsigned long) (field
[2])) << 8)
315 | (((unsigned long) (field
[1])) << 16)
316 | (((unsigned long) (field
[0])) << 24);
321 if (sizeof (elf_vma
) == 8)
322 return ((elf_vma
) (field
[7]))
323 | (((elf_vma
) (field
[6])) << 8)
324 | (((elf_vma
) (field
[5])) << 16)
325 | (((elf_vma
) (field
[4])) << 24)
326 | (((elf_vma
) (field
[3])) << 32)
327 | (((elf_vma
) (field
[2])) << 40)
328 | (((elf_vma
) (field
[1])) << 48)
329 | (((elf_vma
) (field
[0])) << 56);
330 else if (sizeof (elf_vma
) == 4)
332 /* Although we are extracting data from an 8 byte wide field,
333 we are returning only 4 bytes of data. */
335 return ((unsigned long) (field
[3]))
336 | (((unsigned long) (field
[2])) << 8)
337 | (((unsigned long) (field
[1])) << 16)
338 | (((unsigned long) (field
[0])) << 24);
343 error (_("Unhandled data length: %d\n"), size
);
349 byte_get_signed (const unsigned char *field
, int size
)
351 elf_vma x
= byte_get (field
, size
);
356 return (x
^ 0x80) - 0x80;
358 return (x
^ 0x8000) - 0x8000;
360 return (x
^ 0x800000) - 0x800000;
362 return (x
^ 0x80000000) - 0x80000000;
367 /* Reads of 5-, 6-, and 7-byte numbers are the result of
368 trying to read past the end of a buffer, and will therefore
369 not have meaningful values, so we don't try to deal with
370 the sign in these cases. */
377 /* Return the high-order 32-bits and the low-order 32-bits
378 of an 8-byte value separately. */
381 byte_get_64 (const unsigned char *field
, elf_vma
*high
, elf_vma
*low
)
383 if (byte_get
== byte_get_big_endian
)
385 *high
= byte_get_big_endian (field
, 4);
386 *low
= byte_get_big_endian (field
+ 4, 4);
390 *high
= byte_get_little_endian (field
+ 4, 4);
391 *low
= byte_get_little_endian (field
, 4);
396 /* Return the path name for a proxy entry in a thin archive, adjusted
397 relative to the path name of the thin archive itself if necessary.
398 Always returns a pointer to malloc'ed memory. */
401 adjust_relative_path (const char *file_name
, const char *name
,
402 unsigned long name_len
)
404 char * member_file_name
;
405 const char * base_name
= lbasename (file_name
);
408 /* This is a proxy entry for a thin archive member.
409 If the extended name table contains an absolute path
410 name, or if the archive is in the current directory,
411 use the path name as given. Otherwise, we need to
412 find the member relative to the directory where the
413 archive is located. */
414 if (IS_ABSOLUTE_PATH (name
) || base_name
== file_name
)
419 member_file_name
= (char *) malloc (amt
);
420 if (member_file_name
== NULL
)
422 error (_("Out of memory\n"));
425 memcpy (member_file_name
, name
, name_len
);
426 member_file_name
[name_len
] = '\0';
430 /* Concatenate the path components of the archive file name
431 to the relative path name from the extended name table. */
432 size_t prefix_len
= base_name
- file_name
;
434 amt
= prefix_len
+ name_len
+ 1;
435 /* PR 17531: file: 2896dc8b
437 if (amt
< prefix_len
|| amt
< name_len
)
439 error (_("Abnormal length of thin archive member name: %lx\n"),
444 member_file_name
= (char *) malloc (amt
);
445 if (member_file_name
== NULL
)
447 error (_("Out of memory\n"));
450 memcpy (member_file_name
, file_name
, prefix_len
);
451 memcpy (member_file_name
+ prefix_len
, name
, name_len
);
452 member_file_name
[prefix_len
+ name_len
] = '\0';
454 return member_file_name
;
457 /* Processes the archive index table and symbol table in ARCH.
458 Entries in the index table are SIZEOF_AR_INDEX bytes long.
459 Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
460 If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
461 ARCH->sym_size and ARCH->sym_table.
462 It is the caller's responsibility to free ARCH->index_array and
464 Returns TRUE upon success, FALSE otherwise.
465 If failure occurs an error message is printed. */
468 process_archive_index_and_symbols (struct archive_info
* arch
,
469 unsigned int sizeof_ar_index
,
470 bfd_boolean read_symbols
)
476 fmag_save
= arch
->arhdr
.ar_fmag
[0];
477 arch
->arhdr
.ar_fmag
[0] = 0;
478 size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
479 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
480 /* PR 17531: file: 912bd7de. */
481 if ((signed long) size
< 0)
483 error (_("%s: invalid archive header size: %ld\n"),
484 arch
->file_name
, size
);
488 size
= size
+ (size
& 1);
490 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ size
;
494 if (fseek (arch
->file
, size
, SEEK_CUR
) != 0)
496 error (_("%s: failed to skip archive symbol table\n"),
504 /* A buffer used to hold numbers read in from an archive index.
505 These are always SIZEOF_AR_INDEX bytes long and stored in
506 big-endian format. */
507 unsigned char integer_buffer
[sizeof arch
->index_num
];
508 unsigned char * index_buffer
;
510 assert (sizeof_ar_index
<= sizeof integer_buffer
);
512 /* Check the size of the archive index. */
513 if (size
< sizeof_ar_index
)
515 error (_("%s: the archive index is empty\n"), arch
->file_name
);
519 /* Read the number of entries in the archive index. */
520 got
= fread (integer_buffer
, 1, sizeof_ar_index
, arch
->file
);
521 if (got
!= sizeof_ar_index
)
523 error (_("%s: failed to read archive index\n"), arch
->file_name
);
527 arch
->index_num
= byte_get_big_endian (integer_buffer
, sizeof_ar_index
);
528 size
-= sizeof_ar_index
;
530 if (size
< arch
->index_num
* sizeof_ar_index
531 /* PR 17531: file: 585515d1. */
532 || size
< arch
->index_num
)
534 error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
535 arch
->file_name
, (long) arch
->index_num
, sizeof_ar_index
, size
);
539 /* Read in the archive index. */
540 index_buffer
= (unsigned char *)
541 malloc (arch
->index_num
* sizeof_ar_index
);
542 if (index_buffer
== NULL
)
544 error (_("Out of memory whilst trying to read archive symbol index\n"));
548 got
= fread (index_buffer
, sizeof_ar_index
, arch
->index_num
, arch
->file
);
549 if (got
!= arch
->index_num
)
552 error (_("%s: failed to read archive index\n"), arch
->file_name
);
556 size
-= arch
->index_num
* sizeof_ar_index
;
558 /* Convert the index numbers into the host's numeric format. */
559 arch
->index_array
= (elf_vma
*)
560 malloc (arch
->index_num
* sizeof (* arch
->index_array
));
561 if (arch
->index_array
== NULL
)
564 error (_("Out of memory whilst trying to convert the archive symbol index\n"));
568 for (i
= 0; i
< arch
->index_num
; i
++)
569 arch
->index_array
[i
] =
570 byte_get_big_endian ((unsigned char *) (index_buffer
+ (i
* sizeof_ar_index
)),
574 /* The remaining space in the header is taken up by the symbol table. */
577 error (_("%s: the archive has an index but no symbols\n"),
582 arch
->sym_table
= (char *) malloc (size
);
583 if (arch
->sym_table
== NULL
)
585 error (_("Out of memory whilst trying to read archive index symbol table\n"));
589 arch
->sym_size
= size
;
590 got
= fread (arch
->sym_table
, 1, size
, arch
->file
);
593 error (_("%s: failed to read archive index symbol table\n"),
599 /* Read the next archive header. */
600 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
601 if (got
!= sizeof arch
->arhdr
&& got
!= 0)
603 error (_("%s: failed to read archive header following archive index\n"),
611 /* Read the symbol table and long-name table from an archive. */
614 setup_archive (struct archive_info
*arch
, const char *file_name
,
615 FILE *file
, off_t file_size
,
616 bfd_boolean is_thin_archive
, bfd_boolean read_symbols
)
620 arch
->file_name
= strdup (file_name
);
623 arch
->index_array
= NULL
;
624 arch
->sym_table
= NULL
;
626 arch
->longnames
= NULL
;
627 arch
->longnames_size
= 0;
628 arch
->nested_member_origin
= 0;
629 arch
->is_thin_archive
= is_thin_archive
;
630 arch
->uses_64bit_indices
= FALSE
;
631 arch
->next_arhdr_offset
= SARMAG
;
633 /* Read the first archive member header. */
634 if (fseek (file
, SARMAG
, SEEK_SET
) != 0)
636 error (_("%s: failed to seek to first archive header\n"), file_name
);
639 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, file
);
640 if (got
!= sizeof arch
->arhdr
)
645 error (_("%s: failed to read archive header\n"), file_name
);
649 /* See if this is the archive symbol table. */
650 if (const_strneq (arch
->arhdr
.ar_name
, "/ "))
652 if (! process_archive_index_and_symbols (arch
, 4, read_symbols
))
655 else if (const_strneq (arch
->arhdr
.ar_name
, "/SYM64/ "))
657 arch
->uses_64bit_indices
= TRUE
;
658 if (! process_archive_index_and_symbols (arch
, 8, read_symbols
))
661 else if (read_symbols
)
662 printf (_("%s has no archive index\n"), file_name
);
664 if (const_strneq (arch
->arhdr
.ar_name
, "// "))
666 /* This is the archive string table holding long member names. */
667 char fmag_save
= arch
->arhdr
.ar_fmag
[0];
668 arch
->arhdr
.ar_fmag
[0] = 0;
669 arch
->longnames_size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
670 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
671 /* PR 17531: file: 01068045. */
672 if (arch
->longnames_size
< 8)
674 error (_("%s: long name table is too small, (size = %ld)\n"),
675 file_name
, arch
->longnames_size
);
678 /* PR 17531: file: 639d6a26. */
679 if ((off_t
) arch
->longnames_size
> file_size
680 || (signed long) arch
->longnames_size
< 0)
682 error (_("%s: long name table is too big, (size = 0x%lx)\n"),
683 file_name
, arch
->longnames_size
);
687 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ arch
->longnames_size
;
689 /* Plus one to allow for a string terminator. */
690 arch
->longnames
= (char *) malloc (arch
->longnames_size
+ 1);
691 if (arch
->longnames
== NULL
)
693 error (_("Out of memory reading long symbol names in archive\n"));
697 if (fread (arch
->longnames
, arch
->longnames_size
, 1, file
) != 1)
699 free (arch
->longnames
);
700 arch
->longnames
= NULL
;
701 error (_("%s: failed to read long symbol name string table\n"),
706 if ((arch
->longnames_size
& 1) != 0)
709 arch
->longnames
[arch
->longnames_size
] = 0;
715 /* Open and setup a nested archive, if not already open. */
718 setup_nested_archive (struct archive_info
*nested_arch
,
719 const char *member_file_name
)
724 /* Have we already setup this archive? */
725 if (nested_arch
->file_name
!= NULL
726 && streq (nested_arch
->file_name
, member_file_name
))
729 /* Close previous file and discard cached information. */
730 if (nested_arch
->file
!= NULL
)
731 fclose (nested_arch
->file
);
732 release_archive (nested_arch
);
734 member_file
= fopen (member_file_name
, "rb");
735 if (member_file
== NULL
)
737 if (fstat (fileno (member_file
), &statbuf
) < 0)
739 return setup_archive (nested_arch
, member_file_name
, member_file
,
740 statbuf
.st_size
, FALSE
, FALSE
);
743 /* Release the memory used for the archive information. */
746 release_archive (struct archive_info
* arch
)
748 if (arch
->file_name
!= NULL
)
749 free (arch
->file_name
);
750 if (arch
->index_array
!= NULL
)
751 free (arch
->index_array
);
752 if (arch
->sym_table
!= NULL
)
753 free (arch
->sym_table
);
754 if (arch
->longnames
!= NULL
)
755 free (arch
->longnames
);
758 /* Get the name of an archive member from the current archive header.
759 For simple names, this will modify the ar_name field of the current
760 archive header. For long names, it will return a pointer to the
761 longnames table. For nested archives, it will open the nested archive
762 and get the name recursively. NESTED_ARCH is a single-entry cache so
763 we don't keep rereading the same information from a nested archive. */
766 get_archive_member_name (struct archive_info
*arch
,
767 struct archive_info
*nested_arch
)
771 if (arch
->arhdr
.ar_name
[0] == '/')
773 /* We have a long name. */
775 char *member_file_name
;
779 if (arch
->longnames
== NULL
|| arch
->longnames_size
== 0)
781 error (_("Archive member uses long names, but no longname table found\n"));
785 arch
->nested_member_origin
= 0;
786 fmag_save
= arch
->arhdr
.ar_fmag
[0];
787 arch
->arhdr
.ar_fmag
[0] = 0;
788 k
= j
= strtoul (arch
->arhdr
.ar_name
+ 1, &endp
, 10);
789 if (arch
->is_thin_archive
&& endp
!= NULL
&& * endp
== ':')
790 arch
->nested_member_origin
= strtoul (endp
+ 1, NULL
, 10);
791 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
793 if (j
> arch
->longnames_size
)
795 error (_("Found long name index (%ld) beyond end of long name table\n"),j
);
798 while ((j
< arch
->longnames_size
)
799 && (arch
->longnames
[j
] != '\n')
800 && (arch
->longnames
[j
] != '\0'))
802 if (j
> 0 && arch
->longnames
[j
-1] == '/')
804 if (j
> arch
->longnames_size
)
805 j
= arch
->longnames_size
;
806 arch
->longnames
[j
] = '\0';
808 if (!arch
->is_thin_archive
|| arch
->nested_member_origin
== 0)
809 return xstrdup (arch
->longnames
+ k
);
811 /* PR 17531: file: 2896dc8b. */
814 error (_("Invalid Thin archive member name\n"));
818 /* This is a proxy for a member of a nested archive.
819 Find the name of the member in that archive. */
820 member_file_name
= adjust_relative_path (arch
->file_name
,
821 arch
->longnames
+ k
, j
- k
);
822 if (member_file_name
!= NULL
823 && setup_nested_archive (nested_arch
, member_file_name
) == 0)
825 member_name
= get_archive_member_name_at (nested_arch
,
826 arch
->nested_member_origin
,
828 if (member_name
!= NULL
)
830 free (member_file_name
);
834 free (member_file_name
);
836 /* Last resort: just return the name of the nested archive. */
837 return xstrdup (arch
->longnames
+ k
);
840 /* We have a normal (short) name. */
841 for (j
= 0; j
< sizeof (arch
->arhdr
.ar_name
); j
++)
842 if (arch
->arhdr
.ar_name
[j
] == '/')
844 arch
->arhdr
.ar_name
[j
] = '\0';
845 return xstrdup (arch
->arhdr
.ar_name
);
848 /* The full ar_name field is used. Don't rely on ar_date starting
851 char *name
= xmalloc (sizeof (arch
->arhdr
.ar_name
) + 1);
852 memcpy (name
, arch
->arhdr
.ar_name
, sizeof (arch
->arhdr
.ar_name
));
853 name
[sizeof (arch
->arhdr
.ar_name
)] = '\0';
858 /* Get the name of an archive member at a given OFFSET within an archive
862 get_archive_member_name_at (struct archive_info
*arch
,
863 unsigned long offset
,
864 struct archive_info
*nested_arch
)
868 if (fseek (arch
->file
, offset
, SEEK_SET
) != 0)
870 error (_("%s: failed to seek to next file name\n"), arch
->file_name
);
873 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
874 if (got
!= sizeof arch
->arhdr
)
876 error (_("%s: failed to read archive header\n"), arch
->file_name
);
879 if (memcmp (arch
->arhdr
.ar_fmag
, ARFMAG
, 2) != 0)
881 error (_("%s: did not find a valid archive header\n"),
886 return get_archive_member_name (arch
, nested_arch
);
889 /* Construct a string showing the name of the archive member, qualified
890 with the name of the containing archive file. For thin archives, we
891 use square brackets to denote the indirection. For nested archives,
892 we show the qualified name of the external member inside the square
893 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
896 make_qualified_name (struct archive_info
* arch
,
897 struct archive_info
* nested_arch
,
898 const char *member_name
)
900 const char * error_name
= _("<corrupt>");
904 len
= strlen (arch
->file_name
) + strlen (member_name
) + 3;
905 if (arch
->is_thin_archive
906 && arch
->nested_member_origin
!= 0)
908 /* PR 15140: Allow for corrupt thin archives. */
909 if (nested_arch
->file_name
)
910 len
+= strlen (nested_arch
->file_name
) + 2;
912 len
+= strlen (error_name
) + 2;
915 name
= (char *) malloc (len
);
918 error (_("Out of memory\n"));
922 if (arch
->is_thin_archive
923 && arch
->nested_member_origin
!= 0)
925 if (nested_arch
->file_name
)
926 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
927 nested_arch
->file_name
, member_name
);
929 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
930 error_name
, member_name
);
932 else if (arch
->is_thin_archive
)
933 snprintf (name
, len
, "%s[%s]", arch
->file_name
, member_name
);
935 snprintf (name
, len
, "%s(%s)", arch
->file_name
, member_name
);