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
;
37 /* FIXME: This definition really ought to be in ansidecl.h. */
38 #ifndef ATTRIBUTE_WEAK
39 #define ATTRIBUTE_WEAK __attribute__((weak))
42 /* Allow the following two functions to be overridden if desired. */
43 void error (const char *, ...) ATTRIBUTE_WEAK
;
44 void warn (const char *, ...) ATTRIBUTE_WEAK
;
47 error (const char *message
, ...)
51 /* Try to keep error messages in sync with the program's normal output. */
54 va_start (args
, message
);
55 fprintf (stderr
, _("%s: Error: "), program_name
);
56 vfprintf (stderr
, message
, args
);
61 warn (const char *message
, ...)
65 /* Try to keep warning messages in sync with the program's normal output. */
68 va_start (args
, message
);
69 fprintf (stderr
, _("%s: Warning: "), program_name
);
70 vfprintf (stderr
, message
, args
);
74 void (*byte_put
) (unsigned char *, elf_vma
, int);
77 byte_put_little_endian (unsigned char * field
, elf_vma value
, int size
)
82 field
[7] = (((value
>> 24) >> 24) >> 8) & 0xff;
83 field
[6] = ((value
>> 24) >> 24) & 0xff;
84 field
[5] = ((value
>> 24) >> 16) & 0xff;
85 field
[4] = ((value
>> 24) >> 8) & 0xff;
88 field
[3] = (value
>> 24) & 0xff;
91 field
[2] = (value
>> 16) & 0xff;
94 field
[1] = (value
>> 8) & 0xff;
97 field
[0] = value
& 0xff;
101 error (_("Unhandled data length: %d\n"), size
);
107 byte_put_big_endian (unsigned char * field
, elf_vma value
, int size
)
112 field
[7] = value
& 0xff;
113 field
[6] = (value
>> 8) & 0xff;
114 field
[5] = (value
>> 16) & 0xff;
115 field
[4] = (value
>> 24) & 0xff;
120 field
[3] = value
& 0xff;
124 field
[2] = value
& 0xff;
128 field
[1] = value
& 0xff;
132 field
[0] = value
& 0xff;
136 error (_("Unhandled data length: %d\n"), size
);
141 elf_vma (*byte_get
) (const unsigned char *, int);
144 byte_get_little_endian (const unsigned char *field
, int size
)
152 return ((unsigned int) (field
[0]))
153 | (((unsigned int) (field
[1])) << 8);
156 return ((unsigned long) (field
[0]))
157 | (((unsigned long) (field
[1])) << 8)
158 | (((unsigned long) (field
[2])) << 16);
161 return ((unsigned long) (field
[0]))
162 | (((unsigned long) (field
[1])) << 8)
163 | (((unsigned long) (field
[2])) << 16)
164 | (((unsigned long) (field
[3])) << 24);
167 if (sizeof (elf_vma
) == 8)
168 return ((elf_vma
) (field
[0]))
169 | (((elf_vma
) (field
[1])) << 8)
170 | (((elf_vma
) (field
[2])) << 16)
171 | (((elf_vma
) (field
[3])) << 24)
172 | (((elf_vma
) (field
[4])) << 32);
173 else if (sizeof (elf_vma
) == 4)
174 /* We want to extract data from an 8 byte wide field and
175 place it into a 4 byte wide field. Since this is a little
176 endian source we can just use the 4 byte extraction code. */
177 return ((unsigned long) (field
[0]))
178 | (((unsigned long) (field
[1])) << 8)
179 | (((unsigned long) (field
[2])) << 16)
180 | (((unsigned long) (field
[3])) << 24);
184 if (sizeof (elf_vma
) == 8)
185 return ((elf_vma
) (field
[0]))
186 | (((elf_vma
) (field
[1])) << 8)
187 | (((elf_vma
) (field
[2])) << 16)
188 | (((elf_vma
) (field
[3])) << 24)
189 | (((elf_vma
) (field
[4])) << 32)
190 | (((elf_vma
) (field
[5])) << 40);
191 else if (sizeof (elf_vma
) == 4)
192 /* We want to extract data from an 8 byte wide field and
193 place it into a 4 byte wide field. Since this is a little
194 endian source we can just use the 4 byte extraction code. */
195 return ((unsigned long) (field
[0]))
196 | (((unsigned long) (field
[1])) << 8)
197 | (((unsigned long) (field
[2])) << 16)
198 | (((unsigned long) (field
[3])) << 24);
202 if (sizeof (elf_vma
) == 8)
203 return ((elf_vma
) (field
[0]))
204 | (((elf_vma
) (field
[1])) << 8)
205 | (((elf_vma
) (field
[2])) << 16)
206 | (((elf_vma
) (field
[3])) << 24)
207 | (((elf_vma
) (field
[4])) << 32)
208 | (((elf_vma
) (field
[5])) << 40)
209 | (((elf_vma
) (field
[6])) << 48);
210 else if (sizeof (elf_vma
) == 4)
211 /* We want to extract data from an 8 byte wide field and
212 place it into a 4 byte wide field. Since this is a little
213 endian source we can just use the 4 byte extraction code. */
214 return ((unsigned long) (field
[0]))
215 | (((unsigned long) (field
[1])) << 8)
216 | (((unsigned long) (field
[2])) << 16)
217 | (((unsigned long) (field
[3])) << 24);
221 if (sizeof (elf_vma
) == 8)
222 return ((elf_vma
) (field
[0]))
223 | (((elf_vma
) (field
[1])) << 8)
224 | (((elf_vma
) (field
[2])) << 16)
225 | (((elf_vma
) (field
[3])) << 24)
226 | (((elf_vma
) (field
[4])) << 32)
227 | (((elf_vma
) (field
[5])) << 40)
228 | (((elf_vma
) (field
[6])) << 48)
229 | (((elf_vma
) (field
[7])) << 56);
230 else if (sizeof (elf_vma
) == 4)
231 /* We want to extract data from an 8 byte wide field and
232 place it into a 4 byte wide field. Since this is a little
233 endian source we can just use the 4 byte extraction code. */
234 return ((unsigned long) (field
[0]))
235 | (((unsigned long) (field
[1])) << 8)
236 | (((unsigned long) (field
[2])) << 16)
237 | (((unsigned long) (field
[3])) << 24);
241 error (_("Unhandled data length: %d\n"), size
);
247 byte_get_big_endian (const unsigned char *field
, int size
)
255 return ((unsigned int) (field
[1])) | (((int) (field
[0])) << 8);
258 return ((unsigned long) (field
[2]))
259 | (((unsigned long) (field
[1])) << 8)
260 | (((unsigned long) (field
[0])) << 16);
263 return ((unsigned long) (field
[3]))
264 | (((unsigned long) (field
[2])) << 8)
265 | (((unsigned long) (field
[1])) << 16)
266 | (((unsigned long) (field
[0])) << 24);
269 if (sizeof (elf_vma
) == 8)
270 return ((elf_vma
) (field
[4]))
271 | (((elf_vma
) (field
[3])) << 8)
272 | (((elf_vma
) (field
[2])) << 16)
273 | (((elf_vma
) (field
[1])) << 24)
274 | (((elf_vma
) (field
[0])) << 32);
275 else if (sizeof (elf_vma
) == 4)
277 /* Although we are extracting data from an 8 byte wide field,
278 we are returning only 4 bytes of data. */
280 return ((unsigned long) (field
[3]))
281 | (((unsigned long) (field
[2])) << 8)
282 | (((unsigned long) (field
[1])) << 16)
283 | (((unsigned long) (field
[0])) << 24);
288 if (sizeof (elf_vma
) == 8)
289 return ((elf_vma
) (field
[5]))
290 | (((elf_vma
) (field
[4])) << 8)
291 | (((elf_vma
) (field
[3])) << 16)
292 | (((elf_vma
) (field
[2])) << 24)
293 | (((elf_vma
) (field
[1])) << 32)
294 | (((elf_vma
) (field
[0])) << 40);
295 else if (sizeof (elf_vma
) == 4)
297 /* Although we are extracting data from an 8 byte wide field,
298 we are returning only 4 bytes of data. */
300 return ((unsigned long) (field
[3]))
301 | (((unsigned long) (field
[2])) << 8)
302 | (((unsigned long) (field
[1])) << 16)
303 | (((unsigned long) (field
[0])) << 24);
308 if (sizeof (elf_vma
) == 8)
309 return ((elf_vma
) (field
[6]))
310 | (((elf_vma
) (field
[5])) << 8)
311 | (((elf_vma
) (field
[4])) << 16)
312 | (((elf_vma
) (field
[3])) << 24)
313 | (((elf_vma
) (field
[2])) << 32)
314 | (((elf_vma
) (field
[1])) << 40)
315 | (((elf_vma
) (field
[0])) << 48);
316 else if (sizeof (elf_vma
) == 4)
318 /* Although we are extracting data from an 8 byte wide field,
319 we are returning only 4 bytes of data. */
321 return ((unsigned long) (field
[3]))
322 | (((unsigned long) (field
[2])) << 8)
323 | (((unsigned long) (field
[1])) << 16)
324 | (((unsigned long) (field
[0])) << 24);
329 if (sizeof (elf_vma
) == 8)
330 return ((elf_vma
) (field
[7]))
331 | (((elf_vma
) (field
[6])) << 8)
332 | (((elf_vma
) (field
[5])) << 16)
333 | (((elf_vma
) (field
[4])) << 24)
334 | (((elf_vma
) (field
[3])) << 32)
335 | (((elf_vma
) (field
[2])) << 40)
336 | (((elf_vma
) (field
[1])) << 48)
337 | (((elf_vma
) (field
[0])) << 56);
338 else if (sizeof (elf_vma
) == 4)
340 /* Although we are extracting data from an 8 byte wide field,
341 we are returning only 4 bytes of data. */
343 return ((unsigned long) (field
[3]))
344 | (((unsigned long) (field
[2])) << 8)
345 | (((unsigned long) (field
[1])) << 16)
346 | (((unsigned long) (field
[0])) << 24);
351 error (_("Unhandled data length: %d\n"), size
);
357 byte_get_signed (const unsigned char *field
, int size
)
359 elf_vma x
= byte_get (field
, size
);
364 return (x
^ 0x80) - 0x80;
366 return (x
^ 0x8000) - 0x8000;
368 return (x
^ 0x800000) - 0x800000;
370 return (x
^ 0x80000000) - 0x80000000;
375 /* Reads of 5-, 6-, and 7-byte numbers are the result of
376 trying to read past the end of a buffer, and will therefore
377 not have meaningful values, so we don't try to deal with
378 the sign in these cases. */
385 /* Return the high-order 32-bits and the low-order 32-bits
386 of an 8-byte value separately. */
389 byte_get_64 (const unsigned char *field
, elf_vma
*high
, elf_vma
*low
)
391 if (byte_get
== byte_get_big_endian
)
393 *high
= byte_get_big_endian (field
, 4);
394 *low
= byte_get_big_endian (field
+ 4, 4);
398 *high
= byte_get_little_endian (field
+ 4, 4);
399 *low
= byte_get_little_endian (field
, 4);
404 /* Return the path name for a proxy entry in a thin archive, adjusted
405 relative to the path name of the thin archive itself if necessary.
406 Always returns a pointer to malloc'ed memory. */
409 adjust_relative_path (const char *file_name
, const char *name
,
410 unsigned long name_len
)
412 char * member_file_name
;
413 const char * base_name
= lbasename (file_name
);
416 /* This is a proxy entry for a thin archive member.
417 If the extended name table contains an absolute path
418 name, or if the archive is in the current directory,
419 use the path name as given. Otherwise, we need to
420 find the member relative to the directory where the
421 archive is located. */
422 if (IS_ABSOLUTE_PATH (name
) || base_name
== file_name
)
427 member_file_name
= (char *) malloc (amt
);
428 if (member_file_name
== NULL
)
430 error (_("Out of memory\n"));
433 memcpy (member_file_name
, name
, name_len
);
434 member_file_name
[name_len
] = '\0';
438 /* Concatenate the path components of the archive file name
439 to the relative path name from the extended name table. */
440 size_t prefix_len
= base_name
- file_name
;
442 amt
= prefix_len
+ name_len
+ 1;
443 /* PR 17531: file: 2896dc8b
445 if (amt
< prefix_len
|| amt
< name_len
)
447 error (_("Abnormal length of thin archive member name: %lx\n"),
452 member_file_name
= (char *) malloc (amt
);
453 if (member_file_name
== NULL
)
455 error (_("Out of memory\n"));
458 memcpy (member_file_name
, file_name
, prefix_len
);
459 memcpy (member_file_name
+ prefix_len
, name
, name_len
);
460 member_file_name
[prefix_len
+ name_len
] = '\0';
462 return member_file_name
;
465 /* Processes the archive index table and symbol table in ARCH.
466 Entries in the index table are SIZEOF_AR_INDEX bytes long.
467 Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
468 If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
469 ARCH->sym_size and ARCH->sym_table.
470 It is the caller's responsibility to free ARCH->index_array and
472 Returns 1 upon success, 0 otherwise.
473 If failure occurs an error message is printed. */
476 process_archive_index_and_symbols (struct archive_info
*arch
,
477 unsigned int sizeof_ar_index
,
484 fmag_save
= arch
->arhdr
.ar_fmag
[0];
485 arch
->arhdr
.ar_fmag
[0] = 0;
486 size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
487 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
488 /* PR 17531: file: 912bd7de. */
489 if ((signed long) size
< 0)
491 error (_("%s: invalid archive header size: %ld\n"),
492 arch
->file_name
, size
);
496 size
= size
+ (size
& 1);
498 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ size
;
502 if (fseek (arch
->file
, size
, SEEK_CUR
) != 0)
504 error (_("%s: failed to skip archive symbol table\n"),
512 /* A buffer used to hold numbers read in from an archive index.
513 These are always SIZEOF_AR_INDEX bytes long and stored in
514 big-endian format. */
515 unsigned char integer_buffer
[sizeof arch
->index_num
];
516 unsigned char * index_buffer
;
518 assert (sizeof_ar_index
<= sizeof integer_buffer
);
520 /* Check the size of the archive index. */
521 if (size
< sizeof_ar_index
)
523 error (_("%s: the archive index is empty\n"), arch
->file_name
);
527 /* Read the number of entries in the archive index. */
528 got
= fread (integer_buffer
, 1, sizeof_ar_index
, arch
->file
);
529 if (got
!= sizeof_ar_index
)
531 error (_("%s: failed to read archive index\n"), arch
->file_name
);
535 arch
->index_num
= byte_get_big_endian (integer_buffer
, sizeof_ar_index
);
536 size
-= sizeof_ar_index
;
538 if (size
< arch
->index_num
* sizeof_ar_index
539 /* PR 17531: file: 585515d1. */
540 || size
< arch
->index_num
)
542 error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
543 arch
->file_name
, (long) arch
->index_num
, sizeof_ar_index
, size
);
547 /* Read in the archive index. */
548 index_buffer
= (unsigned char *)
549 malloc (arch
->index_num
* sizeof_ar_index
);
550 if (index_buffer
== NULL
)
552 error (_("Out of memory whilst trying to read archive symbol index\n"));
556 got
= fread (index_buffer
, sizeof_ar_index
, arch
->index_num
, arch
->file
);
557 if (got
!= arch
->index_num
)
560 error (_("%s: failed to read archive index\n"), arch
->file_name
);
564 size
-= arch
->index_num
* sizeof_ar_index
;
566 /* Convert the index numbers into the host's numeric format. */
567 arch
->index_array
= (elf_vma
*)
568 malloc (arch
->index_num
* sizeof (* arch
->index_array
));
569 if (arch
->index_array
== NULL
)
572 error (_("Out of memory whilst trying to convert the archive symbol index\n"));
576 for (i
= 0; i
< arch
->index_num
; i
++)
577 arch
->index_array
[i
] =
578 byte_get_big_endian ((unsigned char *) (index_buffer
+ (i
* sizeof_ar_index
)),
582 /* The remaining space in the header is taken up by the symbol table. */
585 error (_("%s: the archive has an index but no symbols\n"),
590 arch
->sym_table
= (char *) malloc (size
);
591 if (arch
->sym_table
== NULL
)
593 error (_("Out of memory whilst trying to read archive index symbol table\n"));
597 arch
->sym_size
= size
;
598 got
= fread (arch
->sym_table
, 1, size
, arch
->file
);
601 error (_("%s: failed to read archive index symbol table\n"),
607 /* Read the next archive header. */
608 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
609 if (got
!= sizeof arch
->arhdr
&& got
!= 0)
611 error (_("%s: failed to read archive header following archive index\n"),
619 /* Read the symbol table and long-name table from an archive. */
622 setup_archive (struct archive_info
*arch
, const char *file_name
,
623 FILE *file
, off_t file_size
,
624 int is_thin_archive
, int read_symbols
)
628 arch
->file_name
= strdup (file_name
);
631 arch
->index_array
= NULL
;
632 arch
->sym_table
= NULL
;
634 arch
->longnames
= NULL
;
635 arch
->longnames_size
= 0;
636 arch
->nested_member_origin
= 0;
637 arch
->is_thin_archive
= is_thin_archive
;
638 arch
->uses_64bit_indices
= 0;
639 arch
->next_arhdr_offset
= SARMAG
;
641 /* Read the first archive member header. */
642 if (fseek (file
, SARMAG
, SEEK_SET
) != 0)
644 error (_("%s: failed to seek to first archive header\n"), file_name
);
647 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, file
);
648 if (got
!= sizeof arch
->arhdr
)
653 error (_("%s: failed to read archive header\n"), file_name
);
657 /* See if this is the archive symbol table. */
658 if (const_strneq (arch
->arhdr
.ar_name
, "/ "))
660 if (! process_archive_index_and_symbols (arch
, 4, read_symbols
))
663 else if (const_strneq (arch
->arhdr
.ar_name
, "/SYM64/ "))
665 arch
->uses_64bit_indices
= 1;
666 if (! process_archive_index_and_symbols (arch
, 8, read_symbols
))
669 else if (read_symbols
)
670 printf (_("%s has no archive index\n"), file_name
);
672 if (const_strneq (arch
->arhdr
.ar_name
, "// "))
674 /* This is the archive string table holding long member names. */
675 char fmag_save
= arch
->arhdr
.ar_fmag
[0];
676 arch
->arhdr
.ar_fmag
[0] = 0;
677 arch
->longnames_size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
678 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
679 /* PR 17531: file: 01068045. */
680 if (arch
->longnames_size
< 8)
682 error (_("%s: long name table is too small, (size = %ld)\n"),
683 file_name
, arch
->longnames_size
);
686 /* PR 17531: file: 639d6a26. */
687 if ((off_t
) arch
->longnames_size
> file_size
688 || (signed long) arch
->longnames_size
< 0)
690 error (_("%s: long name table is too big, (size = 0x%lx)\n"),
691 file_name
, arch
->longnames_size
);
695 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ arch
->longnames_size
;
697 /* Plus one to allow for a string terminator. */
698 arch
->longnames
= (char *) malloc (arch
->longnames_size
+ 1);
699 if (arch
->longnames
== NULL
)
701 error (_("Out of memory reading long symbol names in archive\n"));
705 if (fread (arch
->longnames
, arch
->longnames_size
, 1, file
) != 1)
707 free (arch
->longnames
);
708 arch
->longnames
= NULL
;
709 error (_("%s: failed to read long symbol name string table\n"),
714 if ((arch
->longnames_size
& 1) != 0)
717 arch
->longnames
[arch
->longnames_size
] = 0;
723 /* Open and setup a nested archive, if not already open. */
726 setup_nested_archive (struct archive_info
*nested_arch
,
727 const char *member_file_name
)
732 /* Have we already setup this archive? */
733 if (nested_arch
->file_name
!= NULL
734 && streq (nested_arch
->file_name
, member_file_name
))
737 /* Close previous file and discard cached information. */
738 if (nested_arch
->file
!= NULL
)
739 fclose (nested_arch
->file
);
740 release_archive (nested_arch
);
742 member_file
= fopen (member_file_name
, "rb");
743 if (member_file
== NULL
)
745 if (fstat (fileno (member_file
), &statbuf
) < 0)
747 return setup_archive (nested_arch
, member_file_name
, member_file
,
748 statbuf
.st_size
, 0, 0);
751 /* Release the memory used for the archive information. */
754 release_archive (struct archive_info
* arch
)
756 if (arch
->file_name
!= NULL
)
757 free (arch
->file_name
);
758 if (arch
->index_array
!= NULL
)
759 free (arch
->index_array
);
760 if (arch
->sym_table
!= NULL
)
761 free (arch
->sym_table
);
762 if (arch
->longnames
!= NULL
)
763 free (arch
->longnames
);
766 /* Get the name of an archive member from the current archive header.
767 For simple names, this will modify the ar_name field of the current
768 archive header. For long names, it will return a pointer to the
769 longnames table. For nested archives, it will open the nested archive
770 and get the name recursively. NESTED_ARCH is a single-entry cache so
771 we don't keep rereading the same information from a nested archive. */
774 get_archive_member_name (struct archive_info
*arch
,
775 struct archive_info
*nested_arch
)
779 if (arch
->arhdr
.ar_name
[0] == '/')
781 /* We have a long name. */
783 char *member_file_name
;
787 if (arch
->longnames
== NULL
|| arch
->longnames_size
== 0)
789 error (_("Archive member uses long names, but no longname table found\n"));
793 arch
->nested_member_origin
= 0;
794 fmag_save
= arch
->arhdr
.ar_fmag
[0];
795 arch
->arhdr
.ar_fmag
[0] = 0;
796 k
= j
= strtoul (arch
->arhdr
.ar_name
+ 1, &endp
, 10);
797 if (arch
->is_thin_archive
&& endp
!= NULL
&& * endp
== ':')
798 arch
->nested_member_origin
= strtoul (endp
+ 1, NULL
, 10);
799 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
801 if (j
> arch
->longnames_size
)
803 error (_("Found long name index (%ld) beyond end of long name table\n"),j
);
806 while ((j
< arch
->longnames_size
)
807 && (arch
->longnames
[j
] != '\n')
808 && (arch
->longnames
[j
] != '\0'))
810 if (j
> 0 && arch
->longnames
[j
-1] == '/')
812 if (j
> arch
->longnames_size
)
813 j
= arch
->longnames_size
;
814 arch
->longnames
[j
] = '\0';
816 if (!arch
->is_thin_archive
|| arch
->nested_member_origin
== 0)
817 return xstrdup (arch
->longnames
+ k
);
819 /* PR 17531: file: 2896dc8b. */
822 error (_("Invalid Thin archive member name\n"));
826 /* This is a proxy for a member of a nested archive.
827 Find the name of the member in that archive. */
828 member_file_name
= adjust_relative_path (arch
->file_name
,
829 arch
->longnames
+ k
, j
- k
);
830 if (member_file_name
!= NULL
831 && setup_nested_archive (nested_arch
, member_file_name
) == 0)
833 member_name
= get_archive_member_name_at (nested_arch
,
834 arch
->nested_member_origin
,
836 if (member_name
!= NULL
)
838 free (member_file_name
);
842 free (member_file_name
);
844 /* Last resort: just return the name of the nested archive. */
845 return xstrdup (arch
->longnames
+ k
);
848 /* We have a normal (short) name. */
849 for (j
= 0; j
< sizeof (arch
->arhdr
.ar_name
); j
++)
850 if (arch
->arhdr
.ar_name
[j
] == '/')
852 arch
->arhdr
.ar_name
[j
] = '\0';
853 return xstrdup (arch
->arhdr
.ar_name
);
856 /* The full ar_name field is used. Don't rely on ar_date starting
859 char *name
= xmalloc (sizeof (arch
->arhdr
.ar_name
) + 1);
860 memcpy (name
, arch
->arhdr
.ar_name
, sizeof (arch
->arhdr
.ar_name
));
861 name
[sizeof (arch
->arhdr
.ar_name
)] = '\0';
866 /* Get the name of an archive member at a given OFFSET within an archive
870 get_archive_member_name_at (struct archive_info
*arch
,
871 unsigned long offset
,
872 struct archive_info
*nested_arch
)
876 if (fseek (arch
->file
, offset
, SEEK_SET
) != 0)
878 error (_("%s: failed to seek to next file name\n"), arch
->file_name
);
881 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
882 if (got
!= sizeof arch
->arhdr
)
884 error (_("%s: failed to read archive header\n"), arch
->file_name
);
887 if (memcmp (arch
->arhdr
.ar_fmag
, ARFMAG
, 2) != 0)
889 error (_("%s: did not find a valid archive header\n"),
894 return get_archive_member_name (arch
, nested_arch
);
897 /* Construct a string showing the name of the archive member, qualified
898 with the name of the containing archive file. For thin archives, we
899 use square brackets to denote the indirection. For nested archives,
900 we show the qualified name of the external member inside the square
901 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
904 make_qualified_name (struct archive_info
* arch
,
905 struct archive_info
* nested_arch
,
906 const char *member_name
)
908 const char * error_name
= _("<corrupt>");
912 len
= strlen (arch
->file_name
) + strlen (member_name
) + 3;
913 if (arch
->is_thin_archive
914 && arch
->nested_member_origin
!= 0)
916 /* PR 15140: Allow for corrupt thin archives. */
917 if (nested_arch
->file_name
)
918 len
+= strlen (nested_arch
->file_name
) + 2;
920 len
+= strlen (error_name
) + 2;
923 name
= (char *) malloc (len
);
926 error (_("Out of memory\n"));
930 if (arch
->is_thin_archive
931 && arch
->nested_member_origin
!= 0)
933 if (nested_arch
->file_name
)
934 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
935 nested_arch
->file_name
, member_name
);
937 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
938 error_name
, member_name
);
940 else if (arch
->is_thin_archive
)
941 snprintf (name
, len
, "%s[%s]", arch
->file_name
, member_name
);
943 snprintf (name
, len
, "%s(%s)", arch
->file_name
, member_name
);
This page took 0.054452 seconds and 4 git commands to generate.