1 /* elfcomm.c -- common code for ELF format file.
2 Copyright (C) 2010-2022 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
, unsigned int);
69 byte_put_little_endian (unsigned char * field
, elf_vma value
, unsigned int size
)
71 if (size
> sizeof (elf_vma
))
73 error (_("Unhandled data length: %d\n"), size
);
78 *field
++ = value
& 0xff;
84 byte_put_big_endian (unsigned char * field
, elf_vma value
, unsigned int size
)
86 if (size
> sizeof (elf_vma
))
88 error (_("Unhandled data length: %d\n"), size
);
93 field
[size
] = value
& 0xff;
98 elf_vma (*byte_get
) (const unsigned char *, unsigned int);
101 byte_get_little_endian (const unsigned char *field
, unsigned 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);
133 if (sizeof (elf_vma
) >= 8)
134 return ((elf_vma
) (field
[0]))
135 | (((elf_vma
) (field
[1])) << 8)
136 | (((elf_vma
) (field
[2])) << 16)
137 | (((elf_vma
) (field
[3])) << 24)
138 | (((elf_vma
) (field
[4])) << 32)
139 | (((elf_vma
) (field
[5])) << 40);
143 if (sizeof (elf_vma
) >= 8)
144 return ((elf_vma
) (field
[0]))
145 | (((elf_vma
) (field
[1])) << 8)
146 | (((elf_vma
) (field
[2])) << 16)
147 | (((elf_vma
) (field
[3])) << 24)
148 | (((elf_vma
) (field
[4])) << 32)
149 | (((elf_vma
) (field
[5])) << 40)
150 | (((elf_vma
) (field
[6])) << 48);
154 if (sizeof (elf_vma
) >= 8)
155 return ((elf_vma
) (field
[0]))
156 | (((elf_vma
) (field
[1])) << 8)
157 | (((elf_vma
) (field
[2])) << 16)
158 | (((elf_vma
) (field
[3])) << 24)
159 | (((elf_vma
) (field
[4])) << 32)
160 | (((elf_vma
) (field
[5])) << 40)
161 | (((elf_vma
) (field
[6])) << 48)
162 | (((elf_vma
) (field
[7])) << 56);
166 error (_("Unhandled data length: %d\n"), size
);
172 byte_get_big_endian (const unsigned char *field
, unsigned int size
)
180 return ((unsigned int) (field
[1])) | (((int) (field
[0])) << 8);
183 return ((unsigned long) (field
[2]))
184 | (((unsigned long) (field
[1])) << 8)
185 | (((unsigned long) (field
[0])) << 16);
188 return ((unsigned long) (field
[3]))
189 | (((unsigned long) (field
[2])) << 8)
190 | (((unsigned long) (field
[1])) << 16)
191 | (((unsigned long) (field
[0])) << 24);
194 if (sizeof (elf_vma
) >= 8)
195 return ((elf_vma
) (field
[4]))
196 | (((elf_vma
) (field
[3])) << 8)
197 | (((elf_vma
) (field
[2])) << 16)
198 | (((elf_vma
) (field
[1])) << 24)
199 | (((elf_vma
) (field
[0])) << 32);
203 if (sizeof (elf_vma
) >= 8)
204 return ((elf_vma
) (field
[5]))
205 | (((elf_vma
) (field
[4])) << 8)
206 | (((elf_vma
) (field
[3])) << 16)
207 | (((elf_vma
) (field
[2])) << 24)
208 | (((elf_vma
) (field
[1])) << 32)
209 | (((elf_vma
) (field
[0])) << 40);
213 if (sizeof (elf_vma
) >= 8)
214 return ((elf_vma
) (field
[6]))
215 | (((elf_vma
) (field
[5])) << 8)
216 | (((elf_vma
) (field
[4])) << 16)
217 | (((elf_vma
) (field
[3])) << 24)
218 | (((elf_vma
) (field
[2])) << 32)
219 | (((elf_vma
) (field
[1])) << 40)
220 | (((elf_vma
) (field
[0])) << 48);
224 if (sizeof (elf_vma
) >= 8)
225 return ((elf_vma
) (field
[7]))
226 | (((elf_vma
) (field
[6])) << 8)
227 | (((elf_vma
) (field
[5])) << 16)
228 | (((elf_vma
) (field
[4])) << 24)
229 | (((elf_vma
) (field
[3])) << 32)
230 | (((elf_vma
) (field
[2])) << 40)
231 | (((elf_vma
) (field
[1])) << 48)
232 | (((elf_vma
) (field
[0])) << 56);
236 error (_("Unhandled data length: %d\n"), size
);
242 byte_get_signed (const unsigned char *field
, unsigned int size
)
244 elf_vma x
= byte_get (field
, size
);
249 return (x
^ 0x80) - 0x80;
251 return (x
^ 0x8000) - 0x8000;
253 return (x
^ 0x800000) - 0x800000;
255 return (x
^ 0x80000000) - 0x80000000;
260 /* Reads of 5-, 6-, and 7-byte numbers are the result of
261 trying to read past the end of a buffer, and will therefore
262 not have meaningful values, so we don't try to deal with
263 the sign in these cases. */
270 /* Return the path name for a proxy entry in a thin archive, adjusted
271 relative to the path name of the thin archive itself if necessary.
272 Always returns a pointer to malloc'ed memory. */
275 adjust_relative_path (const char *file_name
, const char *name
,
276 unsigned long name_len
)
278 char * member_file_name
;
279 const char * base_name
= lbasename (file_name
);
282 /* This is a proxy entry for a thin archive member.
283 If the extended name table contains an absolute path
284 name, or if the archive is in the current directory,
285 use the path name as given. Otherwise, we need to
286 find the member relative to the directory where the
287 archive is located. */
288 if (IS_ABSOLUTE_PATH (name
) || base_name
== file_name
)
293 member_file_name
= (char *) malloc (amt
);
294 if (member_file_name
== NULL
)
296 error (_("Out of memory\n"));
299 memcpy (member_file_name
, name
, name_len
);
300 member_file_name
[name_len
] = '\0';
304 /* Concatenate the path components of the archive file name
305 to the relative path name from the extended name table. */
306 size_t prefix_len
= base_name
- file_name
;
308 amt
= prefix_len
+ name_len
+ 1;
309 /* PR 17531: file: 2896dc8b
311 if (amt
< prefix_len
|| amt
< name_len
)
313 error (_("Abnormal length of thin archive member name: %lx\n"),
318 member_file_name
= (char *) malloc (amt
);
319 if (member_file_name
== NULL
)
321 error (_("Out of memory\n"));
324 memcpy (member_file_name
, file_name
, prefix_len
);
325 memcpy (member_file_name
+ prefix_len
, name
, name_len
);
326 member_file_name
[prefix_len
+ name_len
] = '\0';
328 return member_file_name
;
331 /* Processes the archive index table and symbol table in ARCH.
332 Entries in the index table are SIZEOF_AR_INDEX bytes long.
333 Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
334 If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
335 ARCH->sym_size and ARCH->sym_table.
336 It is the caller's responsibility to free ARCH->index_array and
338 Returns 1 upon success, 0 otherwise.
339 If failure occurs an error message is printed. */
342 process_archive_index_and_symbols (struct archive_info
*arch
,
343 unsigned int sizeof_ar_index
,
350 fmag_save
= arch
->arhdr
.ar_fmag
[0];
351 arch
->arhdr
.ar_fmag
[0] = 0;
352 size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
353 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
354 /* PR 17531: file: 912bd7de. */
355 if ((signed long) size
< 0)
357 error (_("%s: invalid archive header size: %ld\n"),
358 arch
->file_name
, size
);
362 size
= size
+ (size
& 1);
364 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ size
;
368 if (fseek (arch
->file
, size
, SEEK_CUR
) != 0)
370 error (_("%s: failed to skip archive symbol table\n"),
378 /* A buffer used to hold numbers read in from an archive index.
379 These are always SIZEOF_AR_INDEX bytes long and stored in
380 big-endian format. */
381 unsigned char integer_buffer
[sizeof arch
->index_num
];
382 unsigned char * index_buffer
;
384 assert (sizeof_ar_index
<= sizeof integer_buffer
);
386 /* Check the size of the archive index. */
387 if (size
< sizeof_ar_index
)
389 error (_("%s: the archive index is empty\n"), arch
->file_name
);
393 /* Read the number of entries in the archive index. */
394 got
= fread (integer_buffer
, 1, sizeof_ar_index
, arch
->file
);
395 if (got
!= sizeof_ar_index
)
397 error (_("%s: failed to read archive index\n"), arch
->file_name
);
401 arch
->index_num
= byte_get_big_endian (integer_buffer
, sizeof_ar_index
);
402 size
-= sizeof_ar_index
;
404 if (size
< arch
->index_num
* sizeof_ar_index
405 /* PR 17531: file: 585515d1. */
406 || size
< arch
->index_num
)
408 error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
409 arch
->file_name
, (long) arch
->index_num
, sizeof_ar_index
, size
);
413 /* Read in the archive index. */
414 index_buffer
= (unsigned char *)
415 malloc (arch
->index_num
* sizeof_ar_index
);
416 if (index_buffer
== NULL
)
418 error (_("Out of memory whilst trying to read archive symbol index\n"));
422 got
= fread (index_buffer
, sizeof_ar_index
, arch
->index_num
, arch
->file
);
423 if (got
!= arch
->index_num
)
426 error (_("%s: failed to read archive index\n"), arch
->file_name
);
430 size
-= arch
->index_num
* sizeof_ar_index
;
432 /* Convert the index numbers into the host's numeric format. */
433 arch
->index_array
= (elf_vma
*)
434 malloc (arch
->index_num
* sizeof (* arch
->index_array
));
435 if (arch
->index_array
== NULL
)
438 error (_("Out of memory whilst trying to convert the archive symbol index\n"));
442 for (i
= 0; i
< arch
->index_num
; i
++)
443 arch
->index_array
[i
] =
444 byte_get_big_endian ((unsigned char *) (index_buffer
+ (i
* sizeof_ar_index
)),
448 /* The remaining space in the header is taken up by the symbol table. */
451 error (_("%s: the archive has an index but no symbols\n"),
456 arch
->sym_table
= (char *) malloc (size
);
457 if (arch
->sym_table
== NULL
)
459 error (_("Out of memory whilst trying to read archive index symbol table\n"));
463 arch
->sym_size
= size
;
464 got
= fread (arch
->sym_table
, 1, size
, arch
->file
);
467 error (_("%s: failed to read archive index symbol table\n"),
473 /* Read the next archive header. */
474 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
475 if (got
!= sizeof arch
->arhdr
&& got
!= 0)
477 error (_("%s: failed to read archive header following archive index\n"),
485 /* Read the symbol table and long-name table from an archive. */
488 setup_archive (struct archive_info
*arch
, const char *file_name
,
489 FILE *file
, off_t file_size
,
490 int is_thin_archive
, int read_symbols
)
494 arch
->file_name
= strdup (file_name
);
497 arch
->index_array
= NULL
;
498 arch
->sym_table
= NULL
;
500 arch
->longnames
= NULL
;
501 arch
->longnames_size
= 0;
502 arch
->nested_member_origin
= 0;
503 arch
->is_thin_archive
= is_thin_archive
;
504 arch
->uses_64bit_indices
= 0;
505 arch
->next_arhdr_offset
= SARMAG
;
507 /* Read the first archive member header. */
508 if (fseek (file
, SARMAG
, SEEK_SET
) != 0)
510 error (_("%s: failed to seek to first archive header\n"), file_name
);
513 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, file
);
514 if (got
!= sizeof arch
->arhdr
)
519 error (_("%s: failed to read archive header\n"), file_name
);
523 /* See if this is the archive symbol table. */
524 if (startswith (arch
->arhdr
.ar_name
, "/ "))
526 if (! process_archive_index_and_symbols (arch
, 4, read_symbols
))
529 else if (startswith (arch
->arhdr
.ar_name
, "/SYM64/ "))
531 arch
->uses_64bit_indices
= 1;
532 if (! process_archive_index_and_symbols (arch
, 8, read_symbols
))
535 else if (read_symbols
)
536 printf (_("%s has no archive index\n"), file_name
);
538 if (startswith (arch
->arhdr
.ar_name
, "// "))
540 /* This is the archive string table holding long member names. */
541 char fmag_save
= arch
->arhdr
.ar_fmag
[0];
542 arch
->arhdr
.ar_fmag
[0] = 0;
543 arch
->longnames_size
= strtoul (arch
->arhdr
.ar_size
, NULL
, 10);
544 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
545 /* PR 17531: file: 01068045. */
546 if (arch
->longnames_size
< 8)
548 error (_("%s: long name table is too small, (size = %ld)\n"),
549 file_name
, arch
->longnames_size
);
552 /* PR 17531: file: 639d6a26. */
553 if ((off_t
) arch
->longnames_size
> file_size
554 || (signed long) arch
->longnames_size
< 0)
556 error (_("%s: long name table is too big, (size = 0x%lx)\n"),
557 file_name
, arch
->longnames_size
);
561 arch
->next_arhdr_offset
+= sizeof arch
->arhdr
+ arch
->longnames_size
;
563 /* Plus one to allow for a string terminator. */
564 arch
->longnames
= (char *) malloc (arch
->longnames_size
+ 1);
565 if (arch
->longnames
== NULL
)
567 error (_("Out of memory reading long symbol names in archive\n"));
571 if (fread (arch
->longnames
, arch
->longnames_size
, 1, file
) != 1)
573 free (arch
->longnames
);
574 arch
->longnames
= NULL
;
575 error (_("%s: failed to read long symbol name string table\n"),
580 if ((arch
->longnames_size
& 1) != 0)
583 arch
->longnames
[arch
->longnames_size
] = 0;
589 /* Open and setup a nested archive, if not already open. */
592 setup_nested_archive (struct archive_info
*nested_arch
,
593 const char *member_file_name
)
598 /* Have we already setup this archive? */
599 if (nested_arch
->file_name
!= NULL
600 && streq (nested_arch
->file_name
, member_file_name
))
603 /* Close previous file and discard cached information. */
604 if (nested_arch
->file
!= NULL
)
606 fclose (nested_arch
->file
);
607 nested_arch
->file
= NULL
;
609 release_archive (nested_arch
);
611 member_file
= fopen (member_file_name
, "rb");
612 if (member_file
== NULL
)
614 if (fstat (fileno (member_file
), &statbuf
) < 0)
616 return setup_archive (nested_arch
, member_file_name
, member_file
,
617 statbuf
.st_size
, 0, 0);
620 /* Release the memory used for the archive information. */
623 release_archive (struct archive_info
* arch
)
625 free (arch
->file_name
);
626 free (arch
->index_array
);
627 free (arch
->sym_table
);
628 free (arch
->longnames
);
629 arch
->file_name
= NULL
;
630 arch
->index_array
= NULL
;
631 arch
->sym_table
= NULL
;
632 arch
->longnames
= NULL
;
635 /* Get the name of an archive member from the current archive header.
636 For simple names, this will modify the ar_name field of the current
637 archive header. For long names, it will return a pointer to the
638 longnames table. For nested archives, it will open the nested archive
639 and get the name recursively. NESTED_ARCH is a single-entry cache so
640 we don't keep rereading the same information from a nested archive. */
643 get_archive_member_name (struct archive_info
*arch
,
644 struct archive_info
*nested_arch
)
648 if (arch
->arhdr
.ar_name
[0] == '/')
650 /* We have a long name. */
652 char *member_file_name
;
656 if (arch
->longnames
== NULL
|| arch
->longnames_size
== 0)
658 error (_("Archive member uses long names, but no longname table found\n"));
662 arch
->nested_member_origin
= 0;
663 fmag_save
= arch
->arhdr
.ar_fmag
[0];
664 arch
->arhdr
.ar_fmag
[0] = 0;
665 k
= j
= strtoul (arch
->arhdr
.ar_name
+ 1, &endp
, 10);
666 if (arch
->is_thin_archive
&& endp
!= NULL
&& * endp
== ':')
667 arch
->nested_member_origin
= strtoul (endp
+ 1, NULL
, 10);
668 arch
->arhdr
.ar_fmag
[0] = fmag_save
;
670 if (j
> arch
->longnames_size
)
672 error (_("Found long name index (%ld) beyond end of long name table\n"),j
);
675 while ((j
< arch
->longnames_size
)
676 && (arch
->longnames
[j
] != '\n')
677 && (arch
->longnames
[j
] != '\0'))
679 if (j
> 0 && arch
->longnames
[j
-1] == '/')
681 if (j
> arch
->longnames_size
)
682 j
= arch
->longnames_size
;
683 arch
->longnames
[j
] = '\0';
685 if (!arch
->is_thin_archive
|| arch
->nested_member_origin
== 0)
686 return xstrdup (arch
->longnames
+ k
);
688 /* PR 17531: file: 2896dc8b. */
691 error (_("Invalid Thin archive member name\n"));
695 /* This is a proxy for a member of a nested archive.
696 Find the name of the member in that archive. */
697 member_file_name
= adjust_relative_path (arch
->file_name
,
698 arch
->longnames
+ k
, j
- k
);
699 if (member_file_name
!= NULL
700 && setup_nested_archive (nested_arch
, member_file_name
) == 0)
702 member_name
= get_archive_member_name_at (nested_arch
,
703 arch
->nested_member_origin
,
705 if (member_name
!= NULL
)
707 free (member_file_name
);
711 free (member_file_name
);
713 /* Last resort: just return the name of the nested archive. */
714 return xstrdup (arch
->longnames
+ k
);
717 /* We have a normal (short) name. */
718 for (j
= 0; j
< sizeof (arch
->arhdr
.ar_name
); j
++)
719 if (arch
->arhdr
.ar_name
[j
] == '/')
721 arch
->arhdr
.ar_name
[j
] = '\0';
722 return xstrdup (arch
->arhdr
.ar_name
);
725 /* The full ar_name field is used. Don't rely on ar_date starting
728 char *name
= xmalloc (sizeof (arch
->arhdr
.ar_name
) + 1);
729 memcpy (name
, arch
->arhdr
.ar_name
, sizeof (arch
->arhdr
.ar_name
));
730 name
[sizeof (arch
->arhdr
.ar_name
)] = '\0';
735 /* Get the name of an archive member at a given OFFSET within an archive
739 get_archive_member_name_at (struct archive_info
*arch
,
740 unsigned long offset
,
741 struct archive_info
*nested_arch
)
745 if (fseek (arch
->file
, offset
, SEEK_SET
) != 0)
747 error (_("%s: failed to seek to next file name\n"), arch
->file_name
);
750 got
= fread (&arch
->arhdr
, 1, sizeof arch
->arhdr
, arch
->file
);
751 if (got
!= sizeof arch
->arhdr
)
753 error (_("%s: failed to read archive header\n"), arch
->file_name
);
756 if (memcmp (arch
->arhdr
.ar_fmag
, ARFMAG
, 2) != 0)
758 error (_("%s: did not find a valid archive header\n"),
763 return get_archive_member_name (arch
, nested_arch
);
766 /* Construct a string showing the name of the archive member, qualified
767 with the name of the containing archive file. For thin archives, we
768 use square brackets to denote the indirection. For nested archives,
769 we show the qualified name of the external member inside the square
770 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
773 make_qualified_name (struct archive_info
* arch
,
774 struct archive_info
* nested_arch
,
775 const char *member_name
)
777 const char * error_name
= _("<corrupt>");
781 len
= strlen (arch
->file_name
) + strlen (member_name
) + 3;
782 if (arch
->is_thin_archive
783 && arch
->nested_member_origin
!= 0)
785 /* PR 15140: Allow for corrupt thin archives. */
786 if (nested_arch
->file_name
)
787 len
+= strlen (nested_arch
->file_name
) + 2;
789 len
+= strlen (error_name
) + 2;
792 name
= (char *) malloc (len
);
795 error (_("Out of memory\n"));
799 if (arch
->is_thin_archive
800 && arch
->nested_member_origin
!= 0)
802 if (nested_arch
->file_name
)
803 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
804 nested_arch
->file_name
, member_name
);
806 snprintf (name
, len
, "%s[%s(%s)]", arch
->file_name
,
807 error_name
, member_name
);
809 else if (arch
->is_thin_archive
)
810 snprintf (name
, len
, "%s[%s]", arch
->file_name
, member_name
);
812 snprintf (name
, len
, "%s(%s)", arch
->file_name
, member_name
);