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
)
70 if (size
<= 0 || size
> 8)
72 error (_("Unhandled data length: %d\n"), size
);
77 *field
++ = value
& 0xff;
83 byte_put_big_endian (unsigned char * field
, elf_vma value
, int size
)
85 if (size
<= 0 || size
> 8)
87 error (_("Unhandled data length: %d\n"), size
);
92 field
[size
] = value
& 0xff;
97 elf_vma (*byte_get
) (const unsigned char *, int);
100 byte_get_little_endian (const unsigned char *field
, int size
)
108 return ((unsigned int) (field
[0]))
109 | (((unsigned int) (field
[1])) << 8);
112 return ((unsigned long) (field
[0]))
113 | (((unsigned long) (field
[1])) << 8)
114 | (((unsigned long) (field
[2])) << 16);
117 return ((unsigned long) (field
[0]))
118 | (((unsigned long) (field
[1])) << 8)
119 | (((unsigned long) (field
[2])) << 16)
120 | (((unsigned long) (field
[3])) << 24);
123 if (sizeof (elf_vma
) == 8)
124 return ((elf_vma
) (field
[0]))
125 | (((elf_vma
) (field
[1])) << 8)
126 | (((elf_vma
) (field
[2])) << 16)
127 | (((elf_vma
) (field
[3])) << 24)
128 | (((elf_vma
) (field
[4])) << 32);
129 else if (sizeof (elf_vma
) == 4)
130 /* We want to extract data from an 8 byte wide field and
131 place it into a 4 byte wide field. Since this is a little
132 endian source we can just use the 4 byte extraction code. */
133 return ((unsigned long) (field
[0]))
134 | (((unsigned long) (field
[1])) << 8)
135 | (((unsigned long) (field
[2])) << 16)
136 | (((unsigned long) (field
[3])) << 24);
140 if (sizeof (elf_vma
) == 8)
141 return ((elf_vma
) (field
[0]))
142 | (((elf_vma
) (field
[1])) << 8)
143 | (((elf_vma
) (field
[2])) << 16)
144 | (((elf_vma
) (field
[3])) << 24)
145 | (((elf_vma
) (field
[4])) << 32)
146 | (((elf_vma
) (field
[5])) << 40);
147 else if (sizeof (elf_vma
) == 4)
148 /* We want to extract data from an 8 byte wide field and
149 place it into a 4 byte wide field. Since this is a little
150 endian source we can just use the 4 byte extraction code. */
151 return ((unsigned long) (field
[0]))
152 | (((unsigned long) (field
[1])) << 8)
153 | (((unsigned long) (field
[2])) << 16)
154 | (((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 | (((elf_vma
) (field
[5])) << 40)
165 | (((elf_vma
) (field
[6])) << 48);
166 else if (sizeof (elf_vma
) == 4)
167 /* We want to extract data from an 8 byte wide field and
168 place it into a 4 byte wide field. Since this is a little
169 endian source we can just use the 4 byte extraction code. */
170 return ((unsigned long) (field
[0]))
171 | (((unsigned long) (field
[1])) << 8)
172 | (((unsigned long) (field
[2])) << 16)
173 | (((unsigned long) (field
[3])) << 24);
177 if (sizeof (elf_vma
) == 8)
178 return ((elf_vma
) (field
[0]))
179 | (((elf_vma
) (field
[1])) << 8)
180 | (((elf_vma
) (field
[2])) << 16)
181 | (((elf_vma
) (field
[3])) << 24)
182 | (((elf_vma
) (field
[4])) << 32)
183 | (((elf_vma
) (field
[5])) << 40)
184 | (((elf_vma
) (field
[6])) << 48)
185 | (((elf_vma
) (field
[7])) << 56);
186 else if (sizeof (elf_vma
) == 4)
187 /* We want to extract data from an 8 byte wide field and
188 place it into a 4 byte wide field. Since this is a little
189 endian source we can just use the 4 byte extraction code. */
190 return ((unsigned long) (field
[0]))
191 | (((unsigned long) (field
[1])) << 8)
192 | (((unsigned long) (field
[2])) << 16)
193 | (((unsigned long) (field
[3])) << 24);
197 error (_("Unhandled data length: %d\n"), size
);
203 byte_get_big_endian (const unsigned char *field
, int size
)
211 return ((unsigned int) (field
[1])) | (((int) (field
[0])) << 8);
214 return ((unsigned long) (field
[2]))
215 | (((unsigned long) (field
[1])) << 8)
216 | (((unsigned long) (field
[0])) << 16);
219 return ((unsigned long) (field
[3]))
220 | (((unsigned long) (field
[2])) << 8)
221 | (((unsigned long) (field
[1])) << 16)
222 | (((unsigned long) (field
[0])) << 24);
225 if (sizeof (elf_vma
) == 8)
226 return ((elf_vma
) (field
[4]))
227 | (((elf_vma
) (field
[3])) << 8)
228 | (((elf_vma
) (field
[2])) << 16)
229 | (((elf_vma
) (field
[1])) << 24)
230 | (((elf_vma
) (field
[0])) << 32);
231 else if (sizeof (elf_vma
) == 4)
233 /* Although we are extracting data from an 8 byte wide field,
234 we are returning only 4 bytes of data. */
236 return ((unsigned long) (field
[3]))
237 | (((unsigned long) (field
[2])) << 8)
238 | (((unsigned long) (field
[1])) << 16)
239 | (((unsigned long) (field
[0])) << 24);
244 if (sizeof (elf_vma
) == 8)
245 return ((elf_vma
) (field
[5]))
246 | (((elf_vma
) (field
[4])) << 8)
247 | (((elf_vma
) (field
[3])) << 16)
248 | (((elf_vma
) (field
[2])) << 24)
249 | (((elf_vma
) (field
[1])) << 32)
250 | (((elf_vma
) (field
[0])) << 40);
251 else if (sizeof (elf_vma
) == 4)
253 /* Although we are extracting data from an 8 byte wide field,
254 we are returning only 4 bytes of data. */
256 return ((unsigned long) (field
[3]))
257 | (((unsigned long) (field
[2])) << 8)
258 | (((unsigned long) (field
[1])) << 16)
259 | (((unsigned long) (field
[0])) << 24);
264 if (sizeof (elf_vma
) == 8)
265 return ((elf_vma
) (field
[6]))
266 | (((elf_vma
) (field
[5])) << 8)
267 | (((elf_vma
) (field
[4])) << 16)
268 | (((elf_vma
) (field
[3])) << 24)
269 | (((elf_vma
) (field
[2])) << 32)
270 | (((elf_vma
) (field
[1])) << 40)
271 | (((elf_vma
) (field
[0])) << 48);
272 else if (sizeof (elf_vma
) == 4)
274 /* Although we are extracting data from an 8 byte wide field,
275 we are returning only 4 bytes of data. */
277 return ((unsigned long) (field
[3]))
278 | (((unsigned long) (field
[2])) << 8)
279 | (((unsigned long) (field
[1])) << 16)
280 | (((unsigned long) (field
[0])) << 24);
285 if (sizeof (elf_vma
) == 8)
286 return ((elf_vma
) (field
[7]))
287 | (((elf_vma
) (field
[6])) << 8)
288 | (((elf_vma
) (field
[5])) << 16)
289 | (((elf_vma
) (field
[4])) << 24)
290 | (((elf_vma
) (field
[3])) << 32)
291 | (((elf_vma
) (field
[2])) << 40)
292 | (((elf_vma
) (field
[1])) << 48)
293 | (((elf_vma
) (field
[0])) << 56);
294 else if (sizeof (elf_vma
) == 4)
296 /* Although we are extracting data from an 8 byte wide field,
297 we are returning only 4 bytes of data. */
299 return ((unsigned long) (field
[3]))
300 | (((unsigned long) (field
[2])) << 8)
301 | (((unsigned long) (field
[1])) << 16)
302 | (((unsigned long) (field
[0])) << 24);
307 error (_("Unhandled data length: %d\n"), size
);
313 byte_get_signed (const unsigned char *field
, int size
)
315 elf_vma x
= byte_get (field
, size
);
320 return (x
^ 0x80) - 0x80;
322 return (x
^ 0x8000) - 0x8000;
324 return (x
^ 0x800000) - 0x800000;
326 return (x
^ 0x80000000) - 0x80000000;
331 /* Reads of 5-, 6-, and 7-byte numbers are the result of
332 trying to read past the end of a buffer, and will therefore
333 not have meaningful values, so we don't try to deal with
334 the sign in these cases. */
341 /* Return the high-order 32-bits and the low-order 32-bits
342 of an 8-byte value separately. */
345 byte_get_64 (const unsigned char *field
, elf_vma
*high
, elf_vma
*low
)
347 if (byte_get
== byte_get_big_endian
)
349 *high
= byte_get_big_endian (field
, 4);
350 *low
= byte_get_big_endian (field
+ 4, 4);
354 *high
= byte_get_little_endian (field
+ 4, 4);
355 *low
= byte_get_little_endian (field
, 4);
360 /* Return the path name for a proxy entry in a thin archive, adjusted
361 relative to the path name of the thin archive itself if necessary.
362 Always returns a pointer to malloc'ed memory. */
365 adjust_relative_path (const char *file_name
, const char *name
,
366 unsigned long name_len
)
368 char * member_file_name
;
369 const char * base_name
= lbasename (file_name
);
372 /* This is a proxy entry for a thin archive member.
373 If the extended name table contains an absolute path
374 name, or if the archive is in the current directory,
375 use the path name as given. Otherwise, we need to
376 find the member relative to the directory where the
377 archive is located. */
378 if (IS_ABSOLUTE_PATH (name
) || base_name
== file_name
)
383 member_file_name
= (char *) malloc (amt
);
384 if (member_file_name
== NULL
)
386 error (_("Out of memory\n"));
389 memcpy (member_file_name
, name
, name_len
);
390 member_file_name
[name_len
] = '\0';
394 /* Concatenate the path components of the archive file name
395 to the relative path name from the extended name table. */
396 size_t prefix_len
= base_name
- file_name
;
398 amt
= prefix_len
+ name_len
+ 1;
399 /* PR 17531: file: 2896dc8b
401 if (amt
< prefix_len
|| amt
< name_len
)
403 error (_("Abnormal length of thin archive member name: %lx\n"),
408 member_file_name
= (char *) malloc (amt
);
409 if (member_file_name
== NULL
)
411 error (_("Out of memory\n"));
414 memcpy (member_file_name
, file_name
, prefix_len
);
415 memcpy (member_file_name
+ prefix_len
, name
, name_len
);
416 member_file_name
[prefix_len
+ name_len
] = '\0';
418 return member_file_name
;
421 /* Processes the archive index table and symbol table in ARCH.
422 Entries in the index table are SIZEOF_AR_INDEX bytes long.
423 Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
424 If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
425 ARCH->sym_size and ARCH->sym_table.
426 It is the caller's responsibility to free ARCH->index_array and
428 Returns 1 upon success, 0 otherwise.
429 If failure occurs an error message is printed. */
432 process_archive_index_and_symbols (struct archive_info
*arch
,
433 unsigned int sizeof_ar_index
,
440 fmag_save
= arch
->arhdr
.ar_fmag
[0];
441 arch
->arhdr
.ar_fmag
[0] = 0;
442 size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
443 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
444 /* PR 17531: file: 912bd7de. */
445 if ((signed long) size
< 0)
447 error (_("%s: invalid archive header size: %ld\n"),
448 arch
->file_name
, size
);
452 size
= size
+ (size
& 1);
454 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ size
;
458 if (fseek (arch
->file
, size
, SEEK_CUR
) != 0)
460 error (_("%s: failed to skip archive symbol table\n"),
468 /* A buffer used to hold numbers read in from an archive index.
469 These are always SIZEOF_AR_INDEX bytes long and stored in
470 big-endian format. */
471 unsigned char integer_buffer
[sizeof arch
->index_num
];
472 unsigned char * index_buffer
;
474 assert (sizeof_ar_index
<= sizeof integer_buffer
);
476 /* Check the size of the archive index. */
477 if (size
< sizeof_ar_index
)
479 error (_("%s: the archive index is empty\n"), arch
->file_name
);
483 /* Read the number of entries in the archive index. */
484 got
= fread (integer_buffer
, 1, sizeof_ar_index
, arch
->file
);
485 if (got
!= sizeof_ar_index
)
487 error (_("%s: failed to read archive index\n"), arch
->file_name
);
491 arch
->index_num
= byte_get_big_endian (integer_buffer
, sizeof_ar_index
);
492 size
-= sizeof_ar_index
;
494 if (size
< arch
->index_num
* sizeof_ar_index
495 /* PR 17531: file: 585515d1. */
496 || size
< arch
->index_num
)
498 error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
499 arch
->file_name
, (long) arch
->index_num
, sizeof_ar_index
, size
);
503 /* Read in the archive index. */
504 index_buffer
= (unsigned char *)
505 malloc (arch
->index_num
* sizeof_ar_index
);
506 if (index_buffer
== NULL
)
508 error (_("Out of memory whilst trying to read archive symbol index\n"));
512 got
= fread (index_buffer
, sizeof_ar_index
, arch
->index_num
, arch
->file
);
513 if (got
!= arch
->index_num
)
516 error (_("%s: failed to read archive index\n"), arch
->file_name
);
520 size
-= arch
->index_num
* sizeof_ar_index
;
522 /* Convert the index numbers into the host's numeric format. */
523 arch
->index_array
= (elf_vma
*)
524 malloc (arch
->index_num
* sizeof (* arch
->index_array
));
525 if (arch
->index_array
== NULL
)
528 error (_("Out of memory whilst trying to convert the archive symbol index\n"));
532 for (i
= 0; i
< arch
->index_num
; i
++)
533 arch
->index_array
[i
] =
534 byte_get_big_endian ((unsigned char *) (index_buffer
+ (i
* sizeof_ar_index
)),
538 /* The remaining space in the header is taken up by the symbol table. */
541 error (_("%s: the archive has an index but no symbols\n"),
546 arch
->sym_table
= (char *) malloc (size
);
547 if (arch
->sym_table
== NULL
)
549 error (_("Out of memory whilst trying to read archive index symbol table\n"));
553 arch
->sym_size
= size
;
554 got
= fread (arch
->sym_table
, 1, size
, arch
->file
);
557 error (_("%s: failed to read archive index symbol table\n"),
563 /* Read the next archive header. */
564 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
565 if (got
!= sizeof arch
->arhdr
&& got
!= 0)
567 error (_("%s: failed to read archive header following archive index\n"),
575 /* Read the symbol table and long-name table from an archive. */
578 setup_archive (struct archive_info
*arch
, const char *file_name
,
579 FILE *file
, off_t file_size
,
580 int is_thin_archive
, int read_symbols
)
584 arch
->file_name
= strdup (file_name
);
587 arch
->index_array
= NULL
;
588 arch
->sym_table
= NULL
;
590 arch
->longnames
= NULL
;
591 arch
->longnames_size
= 0;
592 arch
->nested_member_origin
= 0;
593 arch
->is_thin_archive
= is_thin_archive
;
594 arch
->uses_64bit_indices
= 0;
595 arch
->next_arhdr_offset
= SARMAG
;
597 /* Read the first archive member header. */
598 if (fseek (file
, SARMAG
, SEEK_SET
) != 0)
600 error (_("%s: failed to seek to first archive header\n"), file_name
);
603 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, file
);
604 if (got
!= sizeof arch
->arhdr
)
609 error (_("%s: failed to read archive header\n"), file_name
);
613 /* See if this is the archive symbol table. */
614 if (const_strneq (arch
->arhdr
.ar_name
, "/ "))
616 if (! process_archive_index_and_symbols (arch
, 4, read_symbols
))
619 else if (const_strneq (arch
->arhdr
.ar_name
, "/SYM64/ "))
621 arch
->uses_64bit_indices
= 1;
622 if (! process_archive_index_and_symbols (arch
, 8, read_symbols
))
625 else if (read_symbols
)
626 printf (_("%s has no archive index\n"), file_name
);
628 if (const_strneq (arch
->arhdr
.ar_name
, "// "))
630 /* This is the archive string table holding long member names. */
631 char fmag_save
= arch
->arhdr
.ar_fmag
[0];
632 arch
->arhdr
.ar_fmag
[0] = 0;
633 arch
->longnames_size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
634 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
635 /* PR 17531: file: 01068045. */
636 if (arch
->longnames_size
< 8)
638 error (_("%s: long name table is too small, (size = %ld)\n"),
639 file_name
, arch
->longnames_size
);
642 /* PR 17531: file: 639d6a26. */
643 if ((off_t
) arch
->longnames_size
> file_size
644 || (signed long) arch
->longnames_size
< 0)
646 error (_("%s: long name table is too big, (size = 0x%lx)\n"),
647 file_name
, arch
->longnames_size
);
651 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ arch
->longnames_size
;
653 /* Plus one to allow for a string terminator. */
654 arch
->longnames
= (char *) malloc (arch
->longnames_size
+ 1);
655 if (arch
->longnames
== NULL
)
657 error (_("Out of memory reading long symbol names in archive\n"));
661 if (fread (arch
->longnames
, arch
->longnames_size
, 1, file
) != 1)
663 free (arch
->longnames
);
664 arch
->longnames
= NULL
;
665 error (_("%s: failed to read long symbol name string table\n"),
670 if ((arch
->longnames_size
& 1) != 0)
673 arch
->longnames
[arch
->longnames_size
] = 0;
679 /* Open and setup a nested archive, if not already open. */
682 setup_nested_archive (struct archive_info
*nested_arch
,
683 const char *member_file_name
)
688 /* Have we already setup this archive? */
689 if (nested_arch
->file_name
!= NULL
690 && streq (nested_arch
->file_name
, member_file_name
))
693 /* Close previous file and discard cached information. */
694 if (nested_arch
->file
!= NULL
)
696 fclose (nested_arch
->file
);
697 nested_arch
->file
= NULL
;
699 release_archive (nested_arch
);
701 member_file
= fopen (member_file_name
, "rb");
702 if (member_file
== NULL
)
704 if (fstat (fileno (member_file
), &statbuf
) < 0)
706 return setup_archive (nested_arch
, member_file_name
, member_file
,
707 statbuf
.st_size
, 0, 0);
710 /* Release the memory used for the archive information. */
713 release_archive (struct archive_info
* arch
)
715 free (arch
->file_name
);
716 free (arch
->index_array
);
717 free (arch
->sym_table
);
718 free (arch
->longnames
);
719 arch
->file_name
= NULL
;
720 arch
->index_array
= NULL
;
721 arch
->sym_table
= NULL
;
722 arch
->longnames
= NULL
;
725 /* Get the name of an archive member from the current archive header.
726 For simple names, this will modify the ar_name field of the current
727 archive header. For long names, it will return a pointer to the
728 longnames table. For nested archives, it will open the nested archive
729 and get the name recursively. NESTED_ARCH is a single-entry cache so
730 we don't keep rereading the same information from a nested archive. */
733 get_archive_member_name (struct archive_info
*arch
,
734 struct archive_info
*nested_arch
)
738 if (arch
->arhdr
.ar_name
[0] == '/')
740 /* We have a long name. */
742 char *member_file_name
;
746 if (arch
->longnames
== NULL
|| arch
->longnames_size
== 0)
748 error (_("Archive member uses long names, but no longname table found\n"));
752 arch
->nested_member_origin
= 0;
753 fmag_save
= arch
->arhdr
.ar_fmag
[0];
754 arch
->arhdr
.ar_fmag
[0] = 0;
755 k
= j
= strtoul (arch
->arhdr
.ar_name
+ 1, &endp
, 10);
756 if (arch
->is_thin_archive
&& endp
!= NULL
&& * endp
== ':')
757 arch
->nested_member_origin
= strtoul (endp
+ 1, NULL
, 10);
758 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
760 if (j
> arch
->longnames_size
)
762 error (_("Found long name index (%ld) beyond end of long name table\n"),j
);
765 while ((j
< arch
->longnames_size
)
766 && (arch
->longnames
[j
] != '\n')
767 && (arch
->longnames
[j
] != '\0'))
769 if (j
> 0 && arch
->longnames
[j
-1] == '/')
771 if (j
> arch
->longnames_size
)
772 j
= arch
->longnames_size
;
773 arch
->longnames
[j
] = '\0';
775 if (!arch
->is_thin_archive
|| arch
->nested_member_origin
== 0)
776 return xstrdup (arch
->longnames
+ k
);
778 /* PR 17531: file: 2896dc8b. */
781 error (_("Invalid Thin archive member name\n"));
785 /* This is a proxy for a member of a nested archive.
786 Find the name of the member in that archive. */
787 member_file_name
= adjust_relative_path (arch
->file_name
,
788 arch
->longnames
+ k
, j
- k
);
789 if (member_file_name
!= NULL
790 && setup_nested_archive (nested_arch
, member_file_name
) == 0)
792 member_name
= get_archive_member_name_at (nested_arch
,
793 arch
->nested_member_origin
,
795 if (member_name
!= NULL
)
797 free (member_file_name
);
801 free (member_file_name
);
803 /* Last resort: just return the name of the nested archive. */
804 return xstrdup (arch
->longnames
+ k
);
807 /* We have a normal (short) name. */
808 for (j
= 0; j
< sizeof (arch
->arhdr
.ar_name
); j
++)
809 if (arch
->arhdr
.ar_name
[j
] == '/')
811 arch
->arhdr
.ar_name
[j
] = '\0';
812 return xstrdup (arch
->arhdr
.ar_name
);
815 /* The full ar_name field is used. Don't rely on ar_date starting
818 char *name
= xmalloc (sizeof (arch
->arhdr
.ar_name
) + 1);
819 memcpy (name
, arch
->arhdr
.ar_name
, sizeof (arch
->arhdr
.ar_name
));
820 name
[sizeof (arch
->arhdr
.ar_name
)] = '\0';
825 /* Get the name of an archive member at a given OFFSET within an archive
829 get_archive_member_name_at (struct archive_info
*arch
,
830 unsigned long offset
,
831 struct archive_info
*nested_arch
)
835 if (fseek (arch
->file
, offset
, SEEK_SET
) != 0)
837 error (_("%s: failed to seek to next file name\n"), arch
->file_name
);
840 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
841 if (got
!= sizeof arch
->arhdr
)
843 error (_("%s: failed to read archive header\n"), arch
->file_name
);
846 if (memcmp (arch
->arhdr
.ar_fmag
, ARFMAG
, 2) != 0)
848 error (_("%s: did not find a valid archive header\n"),
853 return get_archive_member_name (arch
, nested_arch
);
856 /* Construct a string showing the name of the archive member, qualified
857 with the name of the containing archive file. For thin archives, we
858 use square brackets to denote the indirection. For nested archives,
859 we show the qualified name of the external member inside the square
860 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
863 make_qualified_name (struct archive_info
* arch
,
864 struct archive_info
* nested_arch
,
865 const char *member_name
)
867 const char * error_name
= _("<corrupt>");
871 len
= strlen (arch
->file_name
) + strlen (member_name
) + 3;
872 if (arch
->is_thin_archive
873 && arch
->nested_member_origin
!= 0)
875 /* PR 15140: Allow for corrupt thin archives. */
876 if (nested_arch
->file_name
)
877 len
+= strlen (nested_arch
->file_name
) + 2;
879 len
+= strlen (error_name
) + 2;
882 name
= (char *) malloc (len
);
885 error (_("Out of memory\n"));
889 if (arch
->is_thin_archive
890 && arch
->nested_member_origin
!= 0)
892 if (nested_arch
->file_name
)
893 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
894 nested_arch
->file_name
, member_name
);
896 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
897 error_name
, member_name
);
899 else if (arch
->is_thin_archive
)
900 snprintf (name
, len
, "%s[%s]", arch
->file_name
, member_name
);
902 snprintf (name
, len
, "%s(%s)", arch
->file_name
, member_name
);