1 /* elfcomm.c -- common code for ELF format file.
2 Copyright (C) 2010-2021 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"
31 #include "filenames.h"
36 extern char *program_name
;
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
)
71 if (size
<= 0 || size
> 8)
73 error (_("Unhandled data length: %d\n"), size
);
78 *field
++ = value
& 0xff;
84 byte_put_big_endian (unsigned char * field
, elf_vma value
, int size
)
86 if (size
<= 0 || size
> 8)
88 error (_("Unhandled data length: %d\n"), size
);
93 field
[size
] = value
& 0xff;
98 elf_vma (*byte_get
) (const unsigned char *, int);
101 byte_get_little_endian (const unsigned char *field
, int size
)
109 return ((unsigned int) (field
[0]))
110 | (((unsigned int) (field
[1])) << 8);
113 return ((unsigned long) (field
[0]))
114 | (((unsigned long) (field
[1])) << 8)
115 | (((unsigned long) (field
[2])) << 16);
118 return ((unsigned long) (field
[0]))
119 | (((unsigned long) (field
[1])) << 8)
120 | (((unsigned long) (field
[2])) << 16)
121 | (((unsigned long) (field
[3])) << 24);
124 if (sizeof (elf_vma
) == 8)
125 return ((elf_vma
) (field
[0]))
126 | (((elf_vma
) (field
[1])) << 8)
127 | (((elf_vma
) (field
[2])) << 16)
128 | (((elf_vma
) (field
[3])) << 24)
129 | (((elf_vma
) (field
[4])) << 32);
130 else if (sizeof (elf_vma
) == 4)
131 /* We want to extract data from an 8 byte wide field and
132 place it into a 4 byte wide field. Since this is a little
133 endian source we can just use the 4 byte extraction code. */
134 return ((unsigned long) (field
[0]))
135 | (((unsigned long) (field
[1])) << 8)
136 | (((unsigned long) (field
[2])) << 16)
137 | (((unsigned long) (field
[3])) << 24);
141 if (sizeof (elf_vma
) == 8)
142 return ((elf_vma
) (field
[0]))
143 | (((elf_vma
) (field
[1])) << 8)
144 | (((elf_vma
) (field
[2])) << 16)
145 | (((elf_vma
) (field
[3])) << 24)
146 | (((elf_vma
) (field
[4])) << 32)
147 | (((elf_vma
) (field
[5])) << 40);
148 else if (sizeof (elf_vma
) == 4)
149 /* We want to extract data from an 8 byte wide field and
150 place it into a 4 byte wide field. Since this is a little
151 endian source we can just use the 4 byte extraction code. */
152 return ((unsigned long) (field
[0]))
153 | (((unsigned long) (field
[1])) << 8)
154 | (((unsigned long) (field
[2])) << 16)
155 | (((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 | (((elf_vma
) (field
[5])) << 40)
166 | (((elf_vma
) (field
[6])) << 48);
167 else if (sizeof (elf_vma
) == 4)
168 /* We want to extract data from an 8 byte wide field and
169 place it into a 4 byte wide field. Since this is a little
170 endian source we can just use the 4 byte extraction code. */
171 return ((unsigned long) (field
[0]))
172 | (((unsigned long) (field
[1])) << 8)
173 | (((unsigned long) (field
[2])) << 16)
174 | (((unsigned long) (field
[3])) << 24);
178 if (sizeof (elf_vma
) == 8)
179 return ((elf_vma
) (field
[0]))
180 | (((elf_vma
) (field
[1])) << 8)
181 | (((elf_vma
) (field
[2])) << 16)
182 | (((elf_vma
) (field
[3])) << 24)
183 | (((elf_vma
) (field
[4])) << 32)
184 | (((elf_vma
) (field
[5])) << 40)
185 | (((elf_vma
) (field
[6])) << 48)
186 | (((elf_vma
) (field
[7])) << 56);
187 else if (sizeof (elf_vma
) == 4)
188 /* We want to extract data from an 8 byte wide field and
189 place it into a 4 byte wide field. Since this is a little
190 endian source we can just use the 4 byte extraction code. */
191 return ((unsigned long) (field
[0]))
192 | (((unsigned long) (field
[1])) << 8)
193 | (((unsigned long) (field
[2])) << 16)
194 | (((unsigned long) (field
[3])) << 24);
198 error (_("Unhandled data length: %d\n"), size
);
204 byte_get_big_endian (const unsigned char *field
, int size
)
212 return ((unsigned int) (field
[1])) | (((int) (field
[0])) << 8);
215 return ((unsigned long) (field
[2]))
216 | (((unsigned long) (field
[1])) << 8)
217 | (((unsigned long) (field
[0])) << 16);
220 return ((unsigned long) (field
[3]))
221 | (((unsigned long) (field
[2])) << 8)
222 | (((unsigned long) (field
[1])) << 16)
223 | (((unsigned long) (field
[0])) << 24);
226 if (sizeof (elf_vma
) == 8)
227 return ((elf_vma
) (field
[4]))
228 | (((elf_vma
) (field
[3])) << 8)
229 | (((elf_vma
) (field
[2])) << 16)
230 | (((elf_vma
) (field
[1])) << 24)
231 | (((elf_vma
) (field
[0])) << 32);
232 else if (sizeof (elf_vma
) == 4)
234 /* Although we are extracting data from an 8 byte wide field,
235 we are returning only 4 bytes of data. */
237 return ((unsigned long) (field
[3]))
238 | (((unsigned long) (field
[2])) << 8)
239 | (((unsigned long) (field
[1])) << 16)
240 | (((unsigned long) (field
[0])) << 24);
245 if (sizeof (elf_vma
) == 8)
246 return ((elf_vma
) (field
[5]))
247 | (((elf_vma
) (field
[4])) << 8)
248 | (((elf_vma
) (field
[3])) << 16)
249 | (((elf_vma
) (field
[2])) << 24)
250 | (((elf_vma
) (field
[1])) << 32)
251 | (((elf_vma
) (field
[0])) << 40);
252 else if (sizeof (elf_vma
) == 4)
254 /* Although we are extracting data from an 8 byte wide field,
255 we are returning only 4 bytes of data. */
257 return ((unsigned long) (field
[3]))
258 | (((unsigned long) (field
[2])) << 8)
259 | (((unsigned long) (field
[1])) << 16)
260 | (((unsigned long) (field
[0])) << 24);
265 if (sizeof (elf_vma
) == 8)
266 return ((elf_vma
) (field
[6]))
267 | (((elf_vma
) (field
[5])) << 8)
268 | (((elf_vma
) (field
[4])) << 16)
269 | (((elf_vma
) (field
[3])) << 24)
270 | (((elf_vma
) (field
[2])) << 32)
271 | (((elf_vma
) (field
[1])) << 40)
272 | (((elf_vma
) (field
[0])) << 48);
273 else if (sizeof (elf_vma
) == 4)
275 /* Although we are extracting data from an 8 byte wide field,
276 we are returning only 4 bytes of data. */
278 return ((unsigned long) (field
[3]))
279 | (((unsigned long) (field
[2])) << 8)
280 | (((unsigned long) (field
[1])) << 16)
281 | (((unsigned long) (field
[0])) << 24);
286 if (sizeof (elf_vma
) == 8)
287 return ((elf_vma
) (field
[7]))
288 | (((elf_vma
) (field
[6])) << 8)
289 | (((elf_vma
) (field
[5])) << 16)
290 | (((elf_vma
) (field
[4])) << 24)
291 | (((elf_vma
) (field
[3])) << 32)
292 | (((elf_vma
) (field
[2])) << 40)
293 | (((elf_vma
) (field
[1])) << 48)
294 | (((elf_vma
) (field
[0])) << 56);
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 error (_("Unhandled data length: %d\n"), size
);
314 byte_get_signed (const unsigned char *field
, int size
)
316 elf_vma x
= byte_get (field
, size
);
321 return (x
^ 0x80) - 0x80;
323 return (x
^ 0x8000) - 0x8000;
325 return (x
^ 0x800000) - 0x800000;
327 return (x
^ 0x80000000) - 0x80000000;
332 /* Reads of 5-, 6-, and 7-byte numbers are the result of
333 trying to read past the end of a buffer, and will therefore
334 not have meaningful values, so we don't try to deal with
335 the sign in these cases. */
342 /* Return the high-order 32-bits and the low-order 32-bits
343 of an 8-byte value separately. */
346 byte_get_64 (const unsigned char *field
, elf_vma
*high
, elf_vma
*low
)
348 if (byte_get
== byte_get_big_endian
)
350 *high
= byte_get_big_endian (field
, 4);
351 *low
= byte_get_big_endian (field
+ 4, 4);
355 *high
= byte_get_little_endian (field
+ 4, 4);
356 *low
= byte_get_little_endian (field
, 4);
361 /* Return the path name for a proxy entry in a thin archive, adjusted
362 relative to the path name of the thin archive itself if necessary.
363 Always returns a pointer to malloc'ed memory. */
366 adjust_relative_path (const char *file_name
, const char *name
,
367 unsigned long name_len
)
369 char * member_file_name
;
370 const char * base_name
= lbasename (file_name
);
373 /* This is a proxy entry for a thin archive member.
374 If the extended name table contains an absolute path
375 name, or if the archive is in the current directory,
376 use the path name as given. Otherwise, we need to
377 find the member relative to the directory where the
378 archive is located. */
379 if (IS_ABSOLUTE_PATH (name
) || base_name
== file_name
)
384 member_file_name
= (char *) malloc (amt
);
385 if (member_file_name
== NULL
)
387 error (_("Out of memory\n"));
390 memcpy (member_file_name
, name
, name_len
);
391 member_file_name
[name_len
] = '\0';
395 /* Concatenate the path components of the archive file name
396 to the relative path name from the extended name table. */
397 size_t prefix_len
= base_name
- file_name
;
399 amt
= prefix_len
+ name_len
+ 1;
400 /* PR 17531: file: 2896dc8b
402 if (amt
< prefix_len
|| amt
< name_len
)
404 error (_("Abnormal length of thin archive member name: %lx\n"),
409 member_file_name
= (char *) malloc (amt
);
410 if (member_file_name
== NULL
)
412 error (_("Out of memory\n"));
415 memcpy (member_file_name
, file_name
, prefix_len
);
416 memcpy (member_file_name
+ prefix_len
, name
, name_len
);
417 member_file_name
[prefix_len
+ name_len
] = '\0';
419 return member_file_name
;
422 /* Processes the archive index table and symbol table in ARCH.
423 Entries in the index table are SIZEOF_AR_INDEX bytes long.
424 Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
425 If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
426 ARCH->sym_size and ARCH->sym_table.
427 It is the caller's responsibility to free ARCH->index_array and
429 Returns 1 upon success, 0 otherwise.
430 If failure occurs an error message is printed. */
433 process_archive_index_and_symbols (struct archive_info
*arch
,
434 unsigned int sizeof_ar_index
,
441 fmag_save
= arch
->arhdr
.ar_fmag
[0];
442 arch
->arhdr
.ar_fmag
[0] = 0;
443 size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
444 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
445 /* PR 17531: file: 912bd7de. */
446 if ((signed long) size
< 0)
448 error (_("%s: invalid archive header size: %ld\n"),
449 arch
->file_name
, size
);
453 size
= size
+ (size
& 1);
455 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ size
;
459 if (fseek (arch
->file
, size
, SEEK_CUR
) != 0)
461 error (_("%s: failed to skip archive symbol table\n"),
469 /* A buffer used to hold numbers read in from an archive index.
470 These are always SIZEOF_AR_INDEX bytes long and stored in
471 big-endian format. */
472 unsigned char integer_buffer
[sizeof arch
->index_num
];
473 unsigned char * index_buffer
;
475 assert (sizeof_ar_index
<= sizeof integer_buffer
);
477 /* Check the size of the archive index. */
478 if (size
< sizeof_ar_index
)
480 error (_("%s: the archive index is empty\n"), arch
->file_name
);
484 /* Read the number of entries in the archive index. */
485 got
= fread (integer_buffer
, 1, sizeof_ar_index
, arch
->file
);
486 if (got
!= sizeof_ar_index
)
488 error (_("%s: failed to read archive index\n"), arch
->file_name
);
492 arch
->index_num
= byte_get_big_endian (integer_buffer
, sizeof_ar_index
);
493 size
-= sizeof_ar_index
;
495 if (size
< arch
->index_num
* sizeof_ar_index
496 /* PR 17531: file: 585515d1. */
497 || size
< arch
->index_num
)
499 error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
500 arch
->file_name
, (long) arch
->index_num
, sizeof_ar_index
, size
);
504 /* Read in the archive index. */
505 index_buffer
= (unsigned char *)
506 malloc (arch
->index_num
* sizeof_ar_index
);
507 if (index_buffer
== NULL
)
509 error (_("Out of memory whilst trying to read archive symbol index\n"));
513 got
= fread (index_buffer
, sizeof_ar_index
, arch
->index_num
, arch
->file
);
514 if (got
!= arch
->index_num
)
517 error (_("%s: failed to read archive index\n"), arch
->file_name
);
521 size
-= arch
->index_num
* sizeof_ar_index
;
523 /* Convert the index numbers into the host's numeric format. */
524 arch
->index_array
= (elf_vma
*)
525 malloc (arch
->index_num
* sizeof (* arch
->index_array
));
526 if (arch
->index_array
== NULL
)
529 error (_("Out of memory whilst trying to convert the archive symbol index\n"));
533 for (i
= 0; i
< arch
->index_num
; i
++)
534 arch
->index_array
[i
] =
535 byte_get_big_endian ((unsigned char *) (index_buffer
+ (i
* sizeof_ar_index
)),
539 /* The remaining space in the header is taken up by the symbol table. */
542 error (_("%s: the archive has an index but no symbols\n"),
547 arch
->sym_table
= (char *) malloc (size
);
548 if (arch
->sym_table
== NULL
)
550 error (_("Out of memory whilst trying to read archive index symbol table\n"));
554 arch
->sym_size
= size
;
555 got
= fread (arch
->sym_table
, 1, size
, arch
->file
);
558 error (_("%s: failed to read archive index symbol table\n"),
564 /* Read the next archive header. */
565 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
566 if (got
!= sizeof arch
->arhdr
&& got
!= 0)
568 error (_("%s: failed to read archive header following archive index\n"),
576 /* Read the symbol table and long-name table from an archive. */
579 setup_archive (struct archive_info
*arch
, const char *file_name
,
580 FILE *file
, off_t file_size
,
581 int is_thin_archive
, int read_symbols
)
585 arch
->file_name
= strdup (file_name
);
588 arch
->index_array
= NULL
;
589 arch
->sym_table
= NULL
;
591 arch
->longnames
= NULL
;
592 arch
->longnames_size
= 0;
593 arch
->nested_member_origin
= 0;
594 arch
->is_thin_archive
= is_thin_archive
;
595 arch
->uses_64bit_indices
= 0;
596 arch
->next_arhdr_offset
= SARMAG
;
598 /* Read the first archive member header. */
599 if (fseek (file
, SARMAG
, SEEK_SET
) != 0)
601 error (_("%s: failed to seek to first archive header\n"), file_name
);
604 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, file
);
605 if (got
!= sizeof arch
->arhdr
)
610 error (_("%s: failed to read archive header\n"), file_name
);
614 /* See if this is the archive symbol table. */
615 if (startswith (arch
->arhdr
.ar_name
, "/ "))
617 if (! process_archive_index_and_symbols (arch
, 4, read_symbols
))
620 else if (startswith (arch
->arhdr
.ar_name
, "/SYM64/ "))
622 arch
->uses_64bit_indices
= 1;
623 if (! process_archive_index_and_symbols (arch
, 8, read_symbols
))
626 else if (read_symbols
)
627 printf (_("%s has no archive index\n"), file_name
);
629 if (startswith (arch
->arhdr
.ar_name
, "// "))
631 /* This is the archive string table holding long member names. */
632 char fmag_save
= arch
->arhdr
.ar_fmag
[0];
633 arch
->arhdr
.ar_fmag
[0] = 0;
634 arch
->longnames_size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
635 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
636 /* PR 17531: file: 01068045. */
637 if (arch
->longnames_size
< 8)
639 error (_("%s: long name table is too small, (size = %ld)\n"),
640 file_name
, arch
->longnames_size
);
643 /* PR 17531: file: 639d6a26. */
644 if ((off_t
) arch
->longnames_size
> file_size
645 || (signed long) arch
->longnames_size
< 0)
647 error (_("%s: long name table is too big, (size = 0x%lx)\n"),
648 file_name
, arch
->longnames_size
);
652 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ arch
->longnames_size
;
654 /* Plus one to allow for a string terminator. */
655 arch
->longnames
= (char *) malloc (arch
->longnames_size
+ 1);
656 if (arch
->longnames
== NULL
)
658 error (_("Out of memory reading long symbol names in archive\n"));
662 if (fread (arch
->longnames
, arch
->longnames_size
, 1, file
) != 1)
664 free (arch
->longnames
);
665 arch
->longnames
= NULL
;
666 error (_("%s: failed to read long symbol name string table\n"),
671 if ((arch
->longnames_size
& 1) != 0)
674 arch
->longnames
[arch
->longnames_size
] = 0;
680 /* Open and setup a nested archive, if not already open. */
683 setup_nested_archive (struct archive_info
*nested_arch
,
684 const char *member_file_name
)
689 /* Have we already setup this archive? */
690 if (nested_arch
->file_name
!= NULL
691 && streq (nested_arch
->file_name
, member_file_name
))
694 /* Close previous file and discard cached information. */
695 if (nested_arch
->file
!= NULL
)
697 fclose (nested_arch
->file
);
698 nested_arch
->file
= NULL
;
700 release_archive (nested_arch
);
702 member_file
= fopen (member_file_name
, "rb");
703 if (member_file
== NULL
)
705 if (fstat (fileno (member_file
), &statbuf
) < 0)
707 return setup_archive (nested_arch
, member_file_name
, member_file
,
708 statbuf
.st_size
, 0, 0);
711 /* Release the memory used for the archive information. */
714 release_archive (struct archive_info
* arch
)
716 free (arch
->file_name
);
717 free (arch
->index_array
);
718 free (arch
->sym_table
);
719 free (arch
->longnames
);
720 arch
->file_name
= NULL
;
721 arch
->index_array
= NULL
;
722 arch
->sym_table
= NULL
;
723 arch
->longnames
= NULL
;
726 /* Get the name of an archive member from the current archive header.
727 For simple names, this will modify the ar_name field of the current
728 archive header. For long names, it will return a pointer to the
729 longnames table. For nested archives, it will open the nested archive
730 and get the name recursively. NESTED_ARCH is a single-entry cache so
731 we don't keep rereading the same information from a nested archive. */
734 get_archive_member_name (struct archive_info
*arch
,
735 struct archive_info
*nested_arch
)
739 if (arch
->arhdr
.ar_name
[0] == '/')
741 /* We have a long name. */
743 char *member_file_name
;
747 if (arch
->longnames
== NULL
|| arch
->longnames_size
== 0)
749 error (_("Archive member uses long names, but no longname table found\n"));
753 arch
->nested_member_origin
= 0;
754 fmag_save
= arch
->arhdr
.ar_fmag
[0];
755 arch
->arhdr
.ar_fmag
[0] = 0;
756 k
= j
= strtoul (arch
->arhdr
.ar_name
+ 1, &endp
, 10);
757 if (arch
->is_thin_archive
&& endp
!= NULL
&& * endp
== ':')
758 arch
->nested_member_origin
= strtoul (endp
+ 1, NULL
, 10);
759 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
761 if (j
> arch
->longnames_size
)
763 error (_("Found long name index (%ld) beyond end of long name table\n"),j
);
766 while ((j
< arch
->longnames_size
)
767 && (arch
->longnames
[j
] != '\n')
768 && (arch
->longnames
[j
] != '\0'))
770 if (j
> 0 && arch
->longnames
[j
-1] == '/')
772 if (j
> arch
->longnames_size
)
773 j
= arch
->longnames_size
;
774 arch
->longnames
[j
] = '\0';
776 if (!arch
->is_thin_archive
|| arch
->nested_member_origin
== 0)
777 return xstrdup (arch
->longnames
+ k
);
779 /* PR 17531: file: 2896dc8b. */
782 error (_("Invalid Thin archive member name\n"));
786 /* This is a proxy for a member of a nested archive.
787 Find the name of the member in that archive. */
788 member_file_name
= adjust_relative_path (arch
->file_name
,
789 arch
->longnames
+ k
, j
- k
);
790 if (member_file_name
!= NULL
791 && setup_nested_archive (nested_arch
, member_file_name
) == 0)
793 member_name
= get_archive_member_name_at (nested_arch
,
794 arch
->nested_member_origin
,
796 if (member_name
!= NULL
)
798 free (member_file_name
);
802 free (member_file_name
);
804 /* Last resort: just return the name of the nested archive. */
805 return xstrdup (arch
->longnames
+ k
);
808 /* We have a normal (short) name. */
809 for (j
= 0; j
< sizeof (arch
->arhdr
.ar_name
); j
++)
810 if (arch
->arhdr
.ar_name
[j
] == '/')
812 arch
->arhdr
.ar_name
[j
] = '\0';
813 return xstrdup (arch
->arhdr
.ar_name
);
816 /* The full ar_name field is used. Don't rely on ar_date starting
819 char *name
= xmalloc (sizeof (arch
->arhdr
.ar_name
) + 1);
820 memcpy (name
, arch
->arhdr
.ar_name
, sizeof (arch
->arhdr
.ar_name
));
821 name
[sizeof (arch
->arhdr
.ar_name
)] = '\0';
826 /* Get the name of an archive member at a given OFFSET within an archive
830 get_archive_member_name_at (struct archive_info
*arch
,
831 unsigned long offset
,
832 struct archive_info
*nested_arch
)
836 if (fseek (arch
->file
, offset
, SEEK_SET
) != 0)
838 error (_("%s: failed to seek to next file name\n"), arch
->file_name
);
841 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
842 if (got
!= sizeof arch
->arhdr
)
844 error (_("%s: failed to read archive header\n"), arch
->file_name
);
847 if (memcmp (arch
->arhdr
.ar_fmag
, ARFMAG
, 2) != 0)
849 error (_("%s: did not find a valid archive header\n"),
854 return get_archive_member_name (arch
, nested_arch
);
857 /* Construct a string showing the name of the archive member, qualified
858 with the name of the containing archive file. For thin archives, we
859 use square brackets to denote the indirection. For nested archives,
860 we show the qualified name of the external member inside the square
861 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
864 make_qualified_name (struct archive_info
* arch
,
865 struct archive_info
* nested_arch
,
866 const char *member_name
)
868 const char * error_name
= _("<corrupt>");
872 len
= strlen (arch
->file_name
) + strlen (member_name
) + 3;
873 if (arch
->is_thin_archive
874 && arch
->nested_member_origin
!= 0)
876 /* PR 15140: Allow for corrupt thin archives. */
877 if (nested_arch
->file_name
)
878 len
+= strlen (nested_arch
->file_name
) + 2;
880 len
+= strlen (error_name
) + 2;
883 name
= (char *) malloc (len
);
886 error (_("Out of memory\n"));
890 if (arch
->is_thin_archive
891 && arch
->nested_member_origin
!= 0)
893 if (nested_arch
->file_name
)
894 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
895 nested_arch
->file_name
, member_name
);
897 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
898 error_name
, member_name
);
900 else if (arch
->is_thin_archive
)
901 snprintf (name
, len
, "%s[%s]", arch
->file_name
, member_name
);
903 snprintf (name
, len
, "%s(%s)", arch
->file_name
, member_name
);
This page took 0.063243 seconds and 4 git commands to generate.