readelf: Add option to display the names of sections referenced by symbols.
[binutils-gdb.git] / binutils / readelf.c
blob5c69efcbcd78e6632d03f065374af1da9c44271e
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2023 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
22 02110-1301, USA. */
24 /* The difference between readelf and objdump:
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #ifdef HAVE_ZSTD
48 #include <zstd.h>
49 #endif
50 #include <wchar.h>
52 #if defined HAVE_MSGPACK
53 #include <msgpack.h>
54 #endif
56 /* Define BFD64 here, even if our default architecture is 32 bit ELF
57 as this will allow us to read in and parse 64bit and 32bit ELF files. */
58 #define BFD64
60 #include "bfd.h"
61 #include "bucomm.h"
62 #include "elfcomm.h"
63 #include "demanguse.h"
64 #include "dwarf.h"
65 #include "ctf-api.h"
66 #include "sframe-api.h"
67 #include "demangle.h"
69 #include "elf/common.h"
70 #include "elf/external.h"
71 #include "elf/internal.h"
74 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
75 we can obtain the H8 reloc numbers. We need these for the
76 get_reloc_size() function. We include h8.h again after defining
77 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
79 #include "elf/h8.h"
80 #undef _ELF_H8_H
82 /* Undo the effects of #including reloc-macros.h. */
84 #undef START_RELOC_NUMBERS
85 #undef RELOC_NUMBER
86 #undef FAKE_RELOC
87 #undef EMPTY_RELOC
88 #undef END_RELOC_NUMBERS
89 #undef _RELOC_MACROS_H
91 /* The following headers use the elf/reloc-macros.h file to
92 automatically generate relocation recognition functions
93 such as elf_mips_reloc_type() */
95 #define RELOC_MACROS_GEN_FUNC
97 #include "elf/aarch64.h"
98 #include "elf/alpha.h"
99 #include "elf/amdgpu.h"
100 #include "elf/arc.h"
101 #include "elf/arm.h"
102 #include "elf/avr.h"
103 #include "elf/bfin.h"
104 #include "elf/cr16.h"
105 #include "elf/cris.h"
106 #include "elf/crx.h"
107 #include "elf/csky.h"
108 #include "elf/d10v.h"
109 #include "elf/d30v.h"
110 #include "elf/dlx.h"
111 #include "elf/bpf.h"
112 #include "elf/epiphany.h"
113 #include "elf/fr30.h"
114 #include "elf/frv.h"
115 #include "elf/ft32.h"
116 #include "elf/h8.h"
117 #include "elf/hppa.h"
118 #include "elf/i386.h"
119 #include "elf/i370.h"
120 #include "elf/i860.h"
121 #include "elf/i960.h"
122 #include "elf/ia64.h"
123 #include "elf/ip2k.h"
124 #include "elf/kvx.h"
125 #include "elf/lm32.h"
126 #include "elf/iq2000.h"
127 #include "elf/m32c.h"
128 #include "elf/m32r.h"
129 #include "elf/m68k.h"
130 #include "elf/m68hc11.h"
131 #include "elf/s12z.h"
132 #include "elf/mcore.h"
133 #include "elf/mep.h"
134 #include "elf/metag.h"
135 #include "elf/microblaze.h"
136 #include "elf/mips.h"
137 #include "elf/mmix.h"
138 #include "elf/mn10200.h"
139 #include "elf/mn10300.h"
140 #include "elf/moxie.h"
141 #include "elf/mt.h"
142 #include "elf/msp430.h"
143 #include "elf/nds32.h"
144 #include "elf/nfp.h"
145 #include "elf/nios2.h"
146 #include "elf/or1k.h"
147 #include "elf/pj.h"
148 #include "elf/ppc.h"
149 #include "elf/ppc64.h"
150 #include "elf/pru.h"
151 #include "elf/riscv.h"
152 #include "elf/rl78.h"
153 #include "elf/rx.h"
154 #include "elf/s390.h"
155 #include "elf/score.h"
156 #include "elf/sh.h"
157 #include "elf/sparc.h"
158 #include "elf/spu.h"
159 #include "elf/tic6x.h"
160 #include "elf/tilegx.h"
161 #include "elf/tilepro.h"
162 #include "elf/v850.h"
163 #include "elf/vax.h"
164 #include "elf/visium.h"
165 #include "elf/wasm32.h"
166 #include "elf/x86-64.h"
167 #include "elf/xgate.h"
168 #include "elf/xstormy16.h"
169 #include "elf/xtensa.h"
170 #include "elf/z80.h"
171 #include "elf/loongarch.h"
172 #include "elf/bpf.h"
174 #include "getopt.h"
175 #include "libiberty.h"
176 #include "safe-ctype.h"
177 #include "filenames.h"
179 #ifndef offsetof
180 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
181 #endif
183 typedef struct elf_section_list
185 Elf_Internal_Shdr * hdr;
186 struct elf_section_list * next;
187 } elf_section_list;
189 /* Flag bits indicating particular types of dump. */
190 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
191 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
192 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
193 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
194 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
195 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
196 #define SFRAME_DUMP (1 << 6) /* The --sframe command line switch. */
198 typedef unsigned char dump_type;
200 /* A linked list of the section names for which dumps were requested. */
201 struct dump_list_entry
203 char * name;
204 dump_type type;
205 struct dump_list_entry * next;
208 /* A dynamic array of flags indicating for which sections a dump
209 has been requested via command line switches. */
210 struct dump_data
212 dump_type * dump_sects;
213 unsigned int num_dump_sects;
216 static struct dump_data cmdline;
218 static struct dump_list_entry * dump_sects_byname;
220 char * program_name = "readelf";
222 static bool show_name = false;
223 static bool do_dynamic = false;
224 static bool do_syms = false;
225 static bool do_dyn_syms = false;
226 static bool do_lto_syms = false;
227 static bool do_reloc = false;
228 static bool do_sections = false;
229 static bool do_section_groups = false;
230 static bool do_section_details = false;
231 static bool do_segments = false;
232 static bool do_unwind = false;
233 static bool do_using_dynamic = false;
234 static bool do_header = false;
235 static bool do_dump = false;
236 static bool do_version = false;
237 static bool do_histogram = false;
238 static bool do_debugging = false;
239 static bool do_ctf = false;
240 static bool do_sframe = false;
241 static bool do_arch = false;
242 static bool do_notes = false;
243 static bool do_archive_index = false;
244 static bool check_all = false;
245 static bool is_32bit_elf = false;
246 static bool decompress_dumps = false;
247 static bool do_not_show_symbol_truncation = false;
248 static bool do_demangle = false; /* Pretty print C++ symbol names. */
249 static bool process_links = false;
250 static bool dump_any_debugging = false;
251 static bool extra_sym_info = false;
252 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
253 static int sym_base = 0;
255 static char *dump_ctf_parent_name;
256 static char *dump_ctf_symtab_name;
257 static char *dump_ctf_strtab_name;
259 struct group_list
261 struct group_list * next;
262 unsigned int section_index;
265 struct group
267 struct group_list * root;
268 unsigned int group_index;
271 typedef struct filedata
273 const char * file_name;
274 bool is_separate;
275 FILE * handle;
276 uint64_t file_size;
277 Elf_Internal_Ehdr file_header;
278 uint64_t archive_file_offset;
279 uint64_t archive_file_size;
280 /* Everything below this point is cleared out by free_filedata. */
281 Elf_Internal_Shdr * section_headers;
282 Elf_Internal_Phdr * program_headers;
283 char * string_table;
284 uint64_t string_table_length;
285 uint64_t dynamic_addr;
286 uint64_t dynamic_size;
287 uint64_t dynamic_nent;
288 Elf_Internal_Dyn * dynamic_section;
289 Elf_Internal_Shdr * dynamic_strtab_section;
290 char * dynamic_strings;
291 uint64_t dynamic_strings_length;
292 Elf_Internal_Shdr * dynamic_symtab_section;
293 uint64_t num_dynamic_syms;
294 Elf_Internal_Sym * dynamic_symbols;
295 uint64_t version_info[16];
296 unsigned int dynamic_syminfo_nent;
297 Elf_Internal_Syminfo * dynamic_syminfo;
298 uint64_t dynamic_syminfo_offset;
299 uint64_t nbuckets;
300 uint64_t nchains;
301 uint64_t * buckets;
302 uint64_t * chains;
303 uint64_t ngnubuckets;
304 uint64_t ngnuchains;
305 uint64_t * gnubuckets;
306 uint64_t * gnuchains;
307 uint64_t * mipsxlat;
308 uint64_t gnusymidx;
309 char * program_interpreter;
310 uint64_t dynamic_info[DT_RELRENT + 1];
311 uint64_t dynamic_info_DT_GNU_HASH;
312 uint64_t dynamic_info_DT_MIPS_XHASH;
313 elf_section_list * symtab_shndx_list;
314 size_t group_count;
315 struct group * section_groups;
316 struct group ** section_headers_groups;
317 /* A dynamic array of flags indicating for which sections a dump of
318 some kind has been requested. It is reset on a per-object file
319 basis and then initialised from the cmdline_dump_sects array,
320 the results of interpreting the -w switch, and the
321 dump_sects_byname list. */
322 struct dump_data dump;
323 } Filedata;
325 /* How to print a vma value. */
326 typedef enum print_mode
328 HEX,
329 HEX_5,
330 DEC,
331 DEC_5,
332 UNSIGNED,
333 UNSIGNED_5,
334 PREFIX_HEX,
335 PREFIX_HEX_5,
336 FULL_HEX,
337 LONG_HEX,
338 OCTAL,
339 OCTAL_5
341 print_mode;
343 typedef enum unicode_display_type
345 unicode_default = 0,
346 unicode_locale,
347 unicode_escape,
348 unicode_hex,
349 unicode_highlight,
350 unicode_invalid
351 } unicode_display_type;
353 static unicode_display_type unicode_display = unicode_default;
355 typedef enum
357 reltype_unknown,
358 reltype_rel,
359 reltype_rela,
360 reltype_relr
361 } relocation_type;
363 /* Versioned symbol info. */
364 enum versioned_symbol_info
366 symbol_undefined,
367 symbol_hidden,
368 symbol_public
371 static int
372 fseek64 (FILE *stream, int64_t offset, int whence)
374 #if defined (HAVE_FSEEKO64)
375 off64_t o = offset;
376 if (o != offset)
378 errno = EINVAL;
379 return -1;
381 return fseeko64 (stream, o, whence);
382 #elif defined (HAVE_FSEEKO)
383 off_t o = offset;
384 if (o != offset)
386 errno = EINVAL;
387 return -1;
389 return fseeko (stream, o, whence);
390 #else
391 long o = offset;
392 if (o != offset)
394 errno = EINVAL;
395 return -1;
397 return fseek (stream, o, whence);
398 #endif
401 static const char * get_symbol_version_string
402 (Filedata *, bool, const char *, size_t, unsigned,
403 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
405 #define UNKNOWN -1
407 static inline const char *
408 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
410 return filedata->string_table + hdr->sh_name;
413 static inline bool
414 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
416 return (filedata != NULL
417 && hdr != NULL
418 && filedata->string_table != NULL
419 && hdr->sh_name < filedata->string_table_length);
422 /* Returns true if the given index is real/valid. Note: "real" here
423 means "references a real section in the section header" and not
424 "is a valid section index as per the ELF standard". */
426 static inline bool
427 section_index_real (const Filedata *filedata, unsigned int ndx)
429 return (filedata != NULL
430 && filedata->section_headers != NULL
431 && ndx < filedata->file_header.e_shnum
432 && ndx > 0);
435 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
437 static inline bool
438 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
440 return strtab != NULL && offset < strtab_size;
443 static inline bool
444 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
446 return valid_symbol_name (filedata->dynamic_strings,
447 filedata->dynamic_strings_length, offset);
450 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
451 already been called and verified that the string exists. */
452 static inline const char *
453 get_dynamic_name (const Filedata *filedata, size_t offset)
455 return filedata->dynamic_strings + offset;
458 #define REMOVE_ARCH_BITS(ADDR) \
459 do \
461 if (filedata->file_header.e_machine == EM_ARM) \
462 (ADDR) &= ~1; \
464 while (0)
466 /* Get the correct GNU hash section name. */
467 #define GNU_HASH_SECTION_NAME(filedata) \
468 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
470 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
471 OFFSET + the offset of the current archive member, if we are examining an
472 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
473 allocate a buffer using malloc and fill that. In either case return the
474 pointer to the start of the retrieved data or NULL if something went wrong.
475 If something does go wrong and REASON is not NULL then emit an error
476 message using REASON as part of the context. */
478 static void *
479 get_data (void *var,
480 Filedata *filedata,
481 uint64_t offset,
482 uint64_t size,
483 uint64_t nmemb,
484 const char *reason)
486 void * mvar;
487 uint64_t amt = size * nmemb;
489 if (size == 0 || nmemb == 0)
490 return NULL;
492 /* If size_t is smaller than uint64_t, eg because you are building
493 on a 32-bit host, then make sure that when the sizes are cast to
494 size_t no information is lost. */
495 if ((size_t) size != size
496 || (size_t) nmemb != nmemb
497 || (size_t) amt != amt
498 || amt / size != nmemb
499 || (size_t) amt + 1 == 0)
501 if (reason)
502 error (_("Size overflow prevents reading %" PRIu64
503 " elements of size %" PRIu64 " for %s\n"),
504 nmemb, size, reason);
505 return NULL;
508 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
509 attempting to allocate memory when the read is bound to fail. */
510 if (filedata->archive_file_offset > filedata->file_size
511 || offset > filedata->file_size - filedata->archive_file_offset
512 || amt > filedata->file_size - filedata->archive_file_offset - offset)
514 if (reason)
515 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
516 amt, reason);
517 return NULL;
520 if (fseek64 (filedata->handle, filedata->archive_file_offset + offset,
521 SEEK_SET))
523 if (reason)
524 error (_("Unable to seek to %#" PRIx64 " for %s\n"),
525 filedata->archive_file_offset + offset, reason);
526 return NULL;
529 mvar = var;
530 if (mvar == NULL)
532 /* + 1 so that we can '\0' terminate invalid string table sections. */
533 mvar = malloc ((size_t) amt + 1);
535 if (mvar == NULL)
537 if (reason)
538 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
539 amt, reason);
540 return NULL;
543 ((char *) mvar)[amt] = '\0';
546 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
548 if (reason)
549 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
550 amt, reason);
551 if (mvar != var)
552 free (mvar);
553 return NULL;
556 return mvar;
559 /* Print a VMA value in the MODE specified.
560 Returns the number of characters displayed. */
562 static unsigned int
563 print_vma (uint64_t vma, print_mode mode)
565 unsigned int nc = 0;
567 switch (mode)
569 case FULL_HEX:
570 nc = printf ("0x");
571 /* Fall through. */
572 case LONG_HEX:
573 if (!is_32bit_elf)
574 return nc + printf ("%16.16" PRIx64, vma);
575 return nc + printf ("%8.8" PRIx64, vma);
577 case DEC_5:
578 if (vma <= 99999)
579 return printf ("%5" PRId64, vma);
580 /* Fall through. */
581 case PREFIX_HEX:
582 nc = printf ("0x");
583 /* Fall through. */
584 case HEX:
585 return nc + printf ("%" PRIx64, vma);
587 case PREFIX_HEX_5:
588 nc = printf ("0x");
589 /* Fall through. */
590 case HEX_5:
591 return nc + printf ("%05" PRIx64, vma);
593 case DEC:
594 return printf ("%" PRId64, vma);
596 case UNSIGNED:
597 return printf ("%" PRIu64, vma);
599 case UNSIGNED_5:
600 return printf ("%5" PRIu64, vma);
602 case OCTAL:
603 return printf ("%" PRIo64, vma);
605 case OCTAL_5:
606 return printf ("%5" PRIo64, vma);
608 default:
609 /* FIXME: Report unrecognised mode ? */
610 return 0;
615 /* Display a symbol on stdout. Handles the display of control characters and
616 multibye characters (assuming the host environment supports them).
618 Display at most abs(WIDTH) characters, truncating as necessary,
619 unless do_wide or extra_sym_info is true.
621 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
622 abs(WIDTH) - 5 characters followed by "[...]".
624 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
625 padding as necessary.
627 Returns the number of emitted characters. */
629 static unsigned int
630 print_symbol_name (signed int width, const char * symbol)
632 bool extra_padding = false;
633 bool do_dots = false;
634 signed int num_printed = 0;
635 #ifdef HAVE_MBSTATE_T
636 mbstate_t state;
637 #endif
638 unsigned int width_remaining;
639 const void * alloced_symbol = NULL;
641 if (width < 0)
643 /* Keep the width positive. This helps the code below. */
644 width = - width;
645 extra_padding = true;
647 else if (width == 0)
648 return 0;
650 if (do_wide || extra_sym_info)
651 /* Set the remaining width to a very large value.
652 This simplifies the code below. */
653 width_remaining = INT_MAX;
654 else
656 width_remaining = width;
658 if (! do_not_show_symbol_truncation
659 && (int) strlen (symbol) > width)
661 width_remaining -= 5;
662 if ((int) width_remaining < 0)
663 width_remaining = 0;
664 do_dots = true;
668 #ifdef HAVE_MBSTATE_T
669 /* Initialise the multibyte conversion state. */
670 memset (& state, 0, sizeof (state));
671 #endif
673 if (do_demangle && *symbol)
675 const char * res = cplus_demangle (symbol, demangle_flags);
677 if (res != NULL)
678 alloced_symbol = symbol = res;
681 while (width_remaining)
683 size_t n;
684 const char c = *symbol++;
686 if (c == 0)
687 break;
689 if (ISPRINT (c))
691 putchar (c);
692 width_remaining --;
693 num_printed ++;
695 else if (ISCNTRL (c))
697 /* Do not print control characters directly as they can affect terminal
698 settings. Such characters usually appear in the names generated
699 by the assembler for local labels. */
701 if (width_remaining < 2)
702 break;
704 printf ("^%c", c + 0x40);
705 width_remaining -= 2;
706 num_printed += 2;
708 else if (c == 0x7f)
710 if (width_remaining < 5)
711 break;
712 printf ("<DEL>");
713 width_remaining -= 5;
714 num_printed += 5;
716 else if (unicode_display != unicode_locale
717 && unicode_display != unicode_default)
719 /* Display unicode characters as something else. */
720 unsigned char bytes[4];
721 bool is_utf8;
722 unsigned int nbytes;
724 bytes[0] = c;
726 if (bytes[0] < 0xc0)
728 nbytes = 1;
729 is_utf8 = false;
731 else
733 bytes[1] = *symbol++;
735 if ((bytes[1] & 0xc0) != 0x80)
737 is_utf8 = false;
738 /* Do not consume this character. It may only
739 be the first byte in the sequence that was
740 corrupt. */
741 --symbol;
742 nbytes = 1;
744 else if ((bytes[0] & 0x20) == 0)
746 is_utf8 = true;
747 nbytes = 2;
749 else
751 bytes[2] = *symbol++;
753 if ((bytes[2] & 0xc0) != 0x80)
755 is_utf8 = false;
756 symbol -= 2;
757 nbytes = 1;
759 else if ((bytes[0] & 0x10) == 0)
761 is_utf8 = true;
762 nbytes = 3;
764 else
766 bytes[3] = *symbol++;
768 nbytes = 4;
770 if ((bytes[3] & 0xc0) != 0x80)
772 is_utf8 = false;
773 symbol -= 3;
774 nbytes = 1;
776 else
777 is_utf8 = true;
782 if (unicode_display == unicode_invalid)
783 is_utf8 = false;
785 if (unicode_display == unicode_hex || ! is_utf8)
787 unsigned int i;
789 if (width_remaining < (nbytes * 2) + 2)
790 break;
792 putchar (is_utf8 ? '<' : '{');
793 printf ("0x");
794 for (i = 0; i < nbytes; i++)
795 printf ("%02x", bytes[i]);
796 putchar (is_utf8 ? '>' : '}');
798 else
800 if (unicode_display == unicode_highlight && isatty (1))
801 printf ("\x1B[31;47m"); /* Red. */
803 switch (nbytes)
805 case 2:
806 if (width_remaining < 6)
807 break;
808 printf ("\\u%02x%02x",
809 (bytes[0] & 0x1c) >> 2,
810 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
811 break;
812 case 3:
813 if (width_remaining < 6)
814 break;
815 printf ("\\u%02x%02x",
816 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
817 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
818 break;
819 case 4:
820 if (width_remaining < 8)
821 break;
822 printf ("\\u%02x%02x%02x",
823 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
824 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
825 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
827 break;
828 default:
829 /* URG. */
830 break;
833 if (unicode_display == unicode_highlight && isatty (1))
834 printf ("\033[0m"); /* Default colour. */
837 if (bytes[nbytes - 1] == 0)
838 break;
840 else
842 #ifdef HAVE_MBSTATE_T
843 wchar_t w;
844 #endif
845 /* Let printf do the hard work of displaying multibyte characters. */
846 printf ("%.1s", symbol - 1);
847 width_remaining --;
848 num_printed ++;
850 #ifdef HAVE_MBSTATE_T
851 /* Try to find out how many bytes made up the character that was
852 just printed. Advance the symbol pointer past the bytes that
853 were displayed. */
854 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
855 #else
856 n = 1;
857 #endif
858 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
859 symbol += (n - 1);
863 if (do_dots)
864 num_printed += printf ("[...]");
866 if (extra_padding && num_printed < width)
868 /* Fill in the remaining spaces. */
869 printf ("%-*s", width - num_printed, " ");
870 num_printed = width;
873 free ((void *) alloced_symbol);
874 return num_printed;
877 /* Returns a pointer to a static buffer containing a printable version of
878 the given section's name. Like print_symbol, except that it does not try
879 to print multibyte characters, it just interprets them as hex values. */
881 static const char *
882 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
884 #define NUM_SEC_NAME_BUFS 5
885 #define MAX_PRINT_SEC_NAME_LEN 256
887 static int sec_name_buf_index = 0;
888 /* We use a rotating array of static buffers, so that multiple successive calls
889 to printable_section_name() will still work. eg when used in a printf. */
890 static char sec_name_buf [NUM_SEC_NAME_BUFS][MAX_PRINT_SEC_NAME_LEN + 1];
892 const char * name;
893 char * buf;
894 char * buf_start;
895 char c;
896 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
898 /* Validate the input parameters. */
899 if (filedata == NULL)
900 return _("<internal error>");
901 if (sec == NULL)
902 return _("<none>");
903 if (filedata->string_table == NULL)
904 return _("<no-strings>");
905 if (sec->sh_name >= filedata->string_table_length)
906 return _("<corrupt>");
908 /* Select a buffer to use. */
909 buf_start = buf = sec_name_buf[sec_name_buf_index];
910 if (++sec_name_buf_index >= NUM_SEC_NAME_BUFS)
911 sec_name_buf_index = 0;
913 name = section_name (filedata, sec);
915 while ((c = * name ++) != 0)
917 if (ISCNTRL (c))
919 if (remaining < 2)
920 break;
922 * buf ++ = '^';
923 * buf ++ = c + 0x40;
924 remaining -= 2;
926 else if (ISPRINT (c))
928 * buf ++ = c;
929 remaining -= 1;
931 else
933 static char hex[17] = "0123456789ABCDEF";
935 if (remaining < 4)
936 break;
937 * buf ++ = '<';
938 * buf ++ = hex[(c & 0xf0) >> 4];
939 * buf ++ = hex[c & 0x0f];
940 * buf ++ = '>';
941 remaining -= 4;
944 if (remaining == 0)
945 break;
948 * buf = 0;
949 return buf_start;
952 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
953 This OS has so many departures from the ELF standard that we test it at
954 many places. */
956 static inline bool
957 is_ia64_vms (Filedata * filedata)
959 return filedata->file_header.e_machine == EM_IA_64
960 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
963 static const char *
964 printable_section_name_from_index (Filedata * filedata,
965 size_t ndx,
966 bool * is_special)
968 if (is_special != NULL)
969 * is_special = true;
971 switch (ndx)
973 case SHN_UNDEF: return "UND";
974 case SHN_ABS: return "ABS";
975 case SHN_COMMON: return "COM";
976 break;
979 if (filedata != NULL)
981 switch (filedata->file_header.e_machine)
983 case EM_MIPS:
984 if (ndx == SHN_MIPS_SCOMMON)
985 return "SCOMMON";
986 if (ndx == SHN_MIPS_SUNDEFINED)
987 return "SUNDEF";
988 break;
990 case EM_TI_C6000:
991 if (ndx == SHN_TIC6X_SCOMMON)
992 return "SCOM";
993 break;
995 case EM_X86_64:
996 case EM_L1OM:
997 case EM_K1OM:
998 if (ndx == SHN_X86_64_LCOMMON)
999 return "LARGE_COM";
1000 break;
1002 case EM_IA_64:
1003 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1004 && ndx == SHN_IA_64_ANSI_COMMON)
1005 return "ANSI_COM";
1007 if (is_ia64_vms (filedata) && ndx == SHN_IA_64_VMS_SYMVEC)
1008 return "VMS_SYMVEC";
1009 break;
1011 default:
1012 break;
1015 if (filedata->section_headers != NULL
1016 && ndx < filedata->file_header.e_shnum)
1018 const char * res;
1020 res = printable_section_name (filedata, filedata->section_headers + ndx);
1021 if (is_special != NULL)
1022 * is_special = (res[0] == '<');
1024 return res;
1028 static char name_buf[40];
1029 unsigned int short_ndx = (unsigned int) (ndx & 0xffff);
1031 if (ndx >= SHN_LOPROC && ndx <= SHN_HIPROC)
1032 sprintf (name_buf, "PRC[0x%04x]", short_ndx);
1033 else if (ndx >= SHN_LOOS && ndx <= SHN_HIOS)
1034 sprintf (name_buf, "OS [0x%04x]", short_ndx);
1035 else if (ndx >= SHN_LORESERVE)
1036 sprintf (name_buf, "RSV[0x%04x]", short_ndx);
1037 else if (filedata->file_header.e_shnum != 0
1038 && ndx >= filedata->file_header.e_shnum)
1039 sprintf (name_buf, _("BAD[0x%lx]"), (long) ndx);
1040 else
1041 sprintf (name_buf, "<section 0x%lx>", (long) ndx);
1043 return name_buf;
1046 /* Return a pointer to section NAME, or NULL if no such section exists. */
1048 static Elf_Internal_Shdr *
1049 find_section (Filedata * filedata, const char * name)
1051 unsigned int i;
1053 if (filedata->section_headers == NULL)
1054 return NULL;
1056 for (i = 0; i < filedata->file_header.e_shnum; i++)
1057 if (section_name_valid (filedata, filedata->section_headers + i)
1058 && streq (section_name (filedata, filedata->section_headers + i),
1059 name))
1060 return filedata->section_headers + i;
1062 return NULL;
1065 /* Return a pointer to a section containing ADDR, or NULL if no such
1066 section exists. */
1068 static Elf_Internal_Shdr *
1069 find_section_by_address (Filedata * filedata, uint64_t addr)
1071 unsigned int i;
1073 if (filedata->section_headers == NULL)
1074 return NULL;
1076 for (i = 0; i < filedata->file_header.e_shnum; i++)
1078 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1080 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
1081 return sec;
1084 return NULL;
1087 static Elf_Internal_Shdr *
1088 find_section_by_type (Filedata * filedata, unsigned int type)
1090 unsigned int i;
1092 if (filedata->section_headers == NULL)
1093 return NULL;
1095 for (i = 0; i < filedata->file_header.e_shnum; i++)
1097 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1099 if (sec->sh_type == type)
1100 return sec;
1103 return NULL;
1106 /* Return a pointer to section NAME, or NULL if no such section exists,
1107 restricted to the list of sections given in SET. */
1109 static Elf_Internal_Shdr *
1110 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
1112 unsigned int i;
1114 if (filedata->section_headers == NULL)
1115 return NULL;
1117 if (set != NULL)
1119 while ((i = *set++) > 0)
1121 /* See PR 21156 for a reproducer. */
1122 if (i >= filedata->file_header.e_shnum)
1123 continue; /* FIXME: Should we issue an error message ? */
1125 if (section_name_valid (filedata, filedata->section_headers + i)
1126 && streq (section_name (filedata, filedata->section_headers + i),
1127 name))
1128 return filedata->section_headers + i;
1132 return find_section (filedata, name);
1135 /* Guess the relocation size commonly used by the specific machines. */
1137 static bool
1138 guess_is_rela (unsigned int e_machine)
1140 switch (e_machine)
1142 /* Targets that use REL relocations. */
1143 case EM_386:
1144 case EM_IAMCU:
1145 case EM_960:
1146 case EM_ARM:
1147 case EM_D10V:
1148 case EM_CYGNUS_D10V:
1149 case EM_DLX:
1150 case EM_MIPS:
1151 case EM_MIPS_RS3_LE:
1152 case EM_CYGNUS_M32R:
1153 case EM_SCORE:
1154 case EM_XGATE:
1155 case EM_NFP:
1156 case EM_BPF:
1157 return false;
1159 /* Targets that use RELA relocations. */
1160 case EM_68K:
1161 case EM_860:
1162 case EM_AARCH64:
1163 case EM_ADAPTEVA_EPIPHANY:
1164 case EM_ALPHA:
1165 case EM_ALTERA_NIOS2:
1166 case EM_ARC:
1167 case EM_ARC_COMPACT:
1168 case EM_ARC_COMPACT2:
1169 case EM_ARC_COMPACT3:
1170 case EM_ARC_COMPACT3_64:
1171 case EM_AVR:
1172 case EM_AVR_OLD:
1173 case EM_BLACKFIN:
1174 case EM_CR16:
1175 case EM_CRIS:
1176 case EM_CRX:
1177 case EM_CSKY:
1178 case EM_D30V:
1179 case EM_CYGNUS_D30V:
1180 case EM_FR30:
1181 case EM_FT32:
1182 case EM_CYGNUS_FR30:
1183 case EM_CYGNUS_FRV:
1184 case EM_H8S:
1185 case EM_H8_300:
1186 case EM_H8_300H:
1187 case EM_IA_64:
1188 case EM_IP2K:
1189 case EM_IP2K_OLD:
1190 case EM_IQ2000:
1191 case EM_KVX:
1192 case EM_LATTICEMICO32:
1193 case EM_M32C_OLD:
1194 case EM_M32C:
1195 case EM_M32R:
1196 case EM_MCORE:
1197 case EM_CYGNUS_MEP:
1198 case EM_METAG:
1199 case EM_MMIX:
1200 case EM_MN10200:
1201 case EM_CYGNUS_MN10200:
1202 case EM_MN10300:
1203 case EM_CYGNUS_MN10300:
1204 case EM_MOXIE:
1205 case EM_MSP430:
1206 case EM_MSP430_OLD:
1207 case EM_MT:
1208 case EM_NDS32:
1209 case EM_NIOS32:
1210 case EM_OR1K:
1211 case EM_PPC64:
1212 case EM_PPC:
1213 case EM_TI_PRU:
1214 case EM_RISCV:
1215 case EM_RL78:
1216 case EM_RX:
1217 case EM_S390:
1218 case EM_S390_OLD:
1219 case EM_SH:
1220 case EM_SPARC:
1221 case EM_SPARC32PLUS:
1222 case EM_SPARCV9:
1223 case EM_SPU:
1224 case EM_TI_C6000:
1225 case EM_TILEGX:
1226 case EM_TILEPRO:
1227 case EM_V800:
1228 case EM_V850:
1229 case EM_CYGNUS_V850:
1230 case EM_VAX:
1231 case EM_VISIUM:
1232 case EM_X86_64:
1233 case EM_L1OM:
1234 case EM_K1OM:
1235 case EM_XSTORMY16:
1236 case EM_XTENSA:
1237 case EM_XTENSA_OLD:
1238 case EM_MICROBLAZE:
1239 case EM_MICROBLAZE_OLD:
1240 case EM_WEBASSEMBLY:
1241 return true;
1243 case EM_68HC05:
1244 case EM_68HC08:
1245 case EM_68HC11:
1246 case EM_68HC16:
1247 case EM_FX66:
1248 case EM_ME16:
1249 case EM_MMA:
1250 case EM_NCPU:
1251 case EM_NDR1:
1252 case EM_PCP:
1253 case EM_ST100:
1254 case EM_ST19:
1255 case EM_ST7:
1256 case EM_ST9PLUS:
1257 case EM_STARCORE:
1258 case EM_SVX:
1259 case EM_TINYJ:
1260 default:
1261 warn (_("Don't know about relocations on this machine architecture\n"));
1262 return false;
1266 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1267 Returns TRUE upon success, FALSE otherwise. If successful then a
1268 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1269 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1270 responsibility to free the allocated buffer. */
1272 static bool
1273 slurp_rela_relocs (Filedata *filedata,
1274 uint64_t rel_offset,
1275 uint64_t rel_size,
1276 Elf_Internal_Rela **relasp,
1277 uint64_t *nrelasp)
1279 Elf_Internal_Rela * relas;
1280 uint64_t nrelas;
1281 unsigned int i;
1283 if (is_32bit_elf)
1285 Elf32_External_Rela * erelas;
1287 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1288 rel_size, _("32-bit relocation data"));
1289 if (!erelas)
1290 return false;
1292 nrelas = rel_size / sizeof (Elf32_External_Rela);
1294 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1295 sizeof (Elf_Internal_Rela));
1297 if (relas == NULL)
1299 free (erelas);
1300 error (_("out of memory parsing relocs\n"));
1301 return false;
1304 for (i = 0; i < nrelas; i++)
1306 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1307 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1308 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1311 free (erelas);
1313 else
1315 Elf64_External_Rela * erelas;
1317 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1318 rel_size, _("64-bit relocation data"));
1319 if (!erelas)
1320 return false;
1322 nrelas = rel_size / sizeof (Elf64_External_Rela);
1324 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1325 sizeof (Elf_Internal_Rela));
1327 if (relas == NULL)
1329 free (erelas);
1330 error (_("out of memory parsing relocs\n"));
1331 return false;
1334 for (i = 0; i < nrelas; i++)
1336 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1337 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1338 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1340 if (filedata->file_header.e_machine == EM_MIPS
1341 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1343 /* In little-endian objects, r_info isn't really a
1344 64-bit little-endian value: it has a 32-bit
1345 little-endian symbol index followed by four
1346 individual byte fields. Reorder INFO
1347 accordingly. */
1348 uint64_t inf = relas[i].r_info;
1349 inf = (((inf & 0xffffffff) << 32)
1350 | ((inf >> 56) & 0xff)
1351 | ((inf >> 40) & 0xff00)
1352 | ((inf >> 24) & 0xff0000)
1353 | ((inf >> 8) & 0xff000000));
1354 relas[i].r_info = inf;
1358 free (erelas);
1361 *relasp = relas;
1362 *nrelasp = nrelas;
1363 return true;
1366 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1367 Returns TRUE upon success, FALSE otherwise. If successful then a
1368 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1369 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1370 responsibility to free the allocated buffer. */
1372 static bool
1373 slurp_rel_relocs (Filedata *filedata,
1374 uint64_t rel_offset,
1375 uint64_t rel_size,
1376 Elf_Internal_Rela **relsp,
1377 uint64_t *nrelsp)
1379 Elf_Internal_Rela * rels;
1380 uint64_t nrels;
1381 unsigned int i;
1383 if (is_32bit_elf)
1385 Elf32_External_Rel * erels;
1387 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1388 rel_size, _("32-bit relocation data"));
1389 if (!erels)
1390 return false;
1392 nrels = rel_size / sizeof (Elf32_External_Rel);
1394 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1396 if (rels == NULL)
1398 free (erels);
1399 error (_("out of memory parsing relocs\n"));
1400 return false;
1403 for (i = 0; i < nrels; i++)
1405 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1406 rels[i].r_info = BYTE_GET (erels[i].r_info);
1407 rels[i].r_addend = 0;
1410 free (erels);
1412 else
1414 Elf64_External_Rel * erels;
1416 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1417 rel_size, _("64-bit relocation data"));
1418 if (!erels)
1419 return false;
1421 nrels = rel_size / sizeof (Elf64_External_Rel);
1423 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1425 if (rels == NULL)
1427 free (erels);
1428 error (_("out of memory parsing relocs\n"));
1429 return false;
1432 for (i = 0; i < nrels; i++)
1434 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1435 rels[i].r_info = BYTE_GET (erels[i].r_info);
1436 rels[i].r_addend = 0;
1438 if (filedata->file_header.e_machine == EM_MIPS
1439 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1441 /* In little-endian objects, r_info isn't really a
1442 64-bit little-endian value: it has a 32-bit
1443 little-endian symbol index followed by four
1444 individual byte fields. Reorder INFO
1445 accordingly. */
1446 uint64_t inf = rels[i].r_info;
1447 inf = (((inf & 0xffffffff) << 32)
1448 | ((inf >> 56) & 0xff)
1449 | ((inf >> 40) & 0xff00)
1450 | ((inf >> 24) & 0xff0000)
1451 | ((inf >> 8) & 0xff000000));
1452 rels[i].r_info = inf;
1456 free (erels);
1459 *relsp = rels;
1460 *nrelsp = nrels;
1461 return true;
1464 static bool
1465 slurp_relr_relocs (Filedata *filedata,
1466 uint64_t relr_offset,
1467 uint64_t relr_size,
1468 uint64_t **relrsp,
1469 uint64_t *nrelrsp)
1471 void *relrs;
1472 size_t size = 0, nentries, i;
1473 uint64_t base = 0, addr, entry;
1475 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1476 _("RELR relocation data"));
1477 if (!relrs)
1478 return false;
1480 if (is_32bit_elf)
1481 nentries = relr_size / sizeof (Elf32_External_Relr);
1482 else
1483 nentries = relr_size / sizeof (Elf64_External_Relr);
1484 for (i = 0; i < nentries; i++)
1486 if (is_32bit_elf)
1487 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1488 else
1489 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1490 if ((entry & 1) == 0)
1491 size++;
1492 else
1493 while ((entry >>= 1) != 0)
1494 if ((entry & 1) == 1)
1495 size++;
1498 *relrsp = malloc (size * sizeof (**relrsp));
1499 if (*relrsp == NULL)
1501 free (relrs);
1502 error (_("out of memory parsing relocs\n"));
1503 return false;
1506 size = 0;
1507 for (i = 0; i < nentries; i++)
1509 const uint64_t entry_bytes = is_32bit_elf ? 4 : 8;
1511 if (is_32bit_elf)
1512 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1513 else
1514 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1515 if ((entry & 1) == 0)
1517 (*relrsp)[size++] = entry;
1518 base = entry + entry_bytes;
1520 else
1522 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1523 if ((entry & 1) != 0)
1524 (*relrsp)[size++] = addr;
1525 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1529 *nrelrsp = size;
1530 free (relrs);
1531 return true;
1534 /* Returns the reloc type extracted from the reloc info field. */
1536 static unsigned int
1537 get_reloc_type (Filedata * filedata, uint64_t reloc_info)
1539 if (is_32bit_elf)
1540 return ELF32_R_TYPE (reloc_info);
1542 switch (filedata->file_header.e_machine)
1544 case EM_MIPS:
1545 /* Note: We assume that reloc_info has already been adjusted for us. */
1546 return ELF64_MIPS_R_TYPE (reloc_info);
1548 case EM_SPARCV9:
1549 return ELF64_R_TYPE_ID (reloc_info);
1551 default:
1552 return ELF64_R_TYPE (reloc_info);
1556 /* Return the symbol index extracted from the reloc info field. */
1558 static uint64_t
1559 get_reloc_symindex (uint64_t reloc_info)
1561 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1564 static inline bool
1565 uses_msp430x_relocs (Filedata * filedata)
1567 return
1568 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1569 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1570 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1571 /* TI compiler uses ELFOSABI_NONE. */
1572 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1575 /* Display the contents of the relocation data found at the specified
1576 offset. */
1578 static bool
1579 dump_relocations (Filedata *filedata,
1580 uint64_t rel_offset,
1581 uint64_t rel_size,
1582 Elf_Internal_Sym *symtab,
1583 uint64_t nsyms,
1584 char *strtab,
1585 uint64_t strtablen,
1586 relocation_type rel_type,
1587 bool is_dynsym)
1589 size_t i;
1590 Elf_Internal_Rela * rels;
1591 bool res = true;
1593 if (rel_type == reltype_unknown)
1594 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1596 if (rel_type == reltype_rela)
1598 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1599 return false;
1601 else if (rel_type == reltype_rel)
1603 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1604 return false;
1606 else if (rel_type == reltype_relr)
1608 uint64_t * relrs;
1609 const char *format
1610 = is_32bit_elf ? "%08" PRIx64 "\n" : "%016" PRIx64 "\n";
1612 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1613 &rel_size))
1614 return false;
1616 printf (ngettext (" %" PRIu64 " offset\n",
1617 " %" PRIu64 " offsets\n", rel_size),
1618 rel_size);
1619 for (i = 0; i < rel_size; i++)
1620 printf (format, relrs[i]);
1621 free (relrs);
1622 return true;
1625 if (is_32bit_elf)
1627 if (rel_type == reltype_rela)
1629 if (do_wide)
1630 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1631 else
1632 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1634 else
1636 if (do_wide)
1637 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1638 else
1639 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1642 else
1644 if (rel_type == reltype_rela)
1646 if (do_wide)
1647 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1648 else
1649 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1651 else
1653 if (do_wide)
1654 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1655 else
1656 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1660 for (i = 0; i < rel_size; i++)
1662 const char * rtype;
1663 uint64_t offset;
1664 uint64_t inf;
1665 uint64_t symtab_index;
1666 uint64_t type;
1668 offset = rels[i].r_offset;
1669 inf = rels[i].r_info;
1671 type = get_reloc_type (filedata, inf);
1672 symtab_index = get_reloc_symindex (inf);
1674 if (is_32bit_elf)
1676 printf ("%8.8lx %8.8lx ",
1677 (unsigned long) offset & 0xffffffff,
1678 (unsigned long) inf & 0xffffffff);
1680 else
1682 printf (do_wide
1683 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
1684 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
1685 offset, inf);
1688 switch (filedata->file_header.e_machine)
1690 default:
1691 rtype = NULL;
1692 break;
1694 case EM_AARCH64:
1695 rtype = elf_aarch64_reloc_type (type);
1696 break;
1698 case EM_M32R:
1699 case EM_CYGNUS_M32R:
1700 rtype = elf_m32r_reloc_type (type);
1701 break;
1703 case EM_386:
1704 case EM_IAMCU:
1705 rtype = elf_i386_reloc_type (type);
1706 break;
1708 case EM_68HC11:
1709 case EM_68HC12:
1710 rtype = elf_m68hc11_reloc_type (type);
1711 break;
1713 case EM_S12Z:
1714 rtype = elf_s12z_reloc_type (type);
1715 break;
1717 case EM_68K:
1718 rtype = elf_m68k_reloc_type (type);
1719 break;
1721 case EM_960:
1722 rtype = elf_i960_reloc_type (type);
1723 break;
1725 case EM_AVR:
1726 case EM_AVR_OLD:
1727 rtype = elf_avr_reloc_type (type);
1728 break;
1730 case EM_OLD_SPARCV9:
1731 case EM_SPARC32PLUS:
1732 case EM_SPARCV9:
1733 case EM_SPARC:
1734 rtype = elf_sparc_reloc_type (type);
1735 break;
1737 case EM_SPU:
1738 rtype = elf_spu_reloc_type (type);
1739 break;
1741 case EM_V800:
1742 rtype = v800_reloc_type (type);
1743 break;
1744 case EM_V850:
1745 case EM_CYGNUS_V850:
1746 rtype = v850_reloc_type (type);
1747 break;
1749 case EM_D10V:
1750 case EM_CYGNUS_D10V:
1751 rtype = elf_d10v_reloc_type (type);
1752 break;
1754 case EM_D30V:
1755 case EM_CYGNUS_D30V:
1756 rtype = elf_d30v_reloc_type (type);
1757 break;
1759 case EM_DLX:
1760 rtype = elf_dlx_reloc_type (type);
1761 break;
1763 case EM_SH:
1764 rtype = elf_sh_reloc_type (type);
1765 break;
1767 case EM_MN10300:
1768 case EM_CYGNUS_MN10300:
1769 rtype = elf_mn10300_reloc_type (type);
1770 break;
1772 case EM_MN10200:
1773 case EM_CYGNUS_MN10200:
1774 rtype = elf_mn10200_reloc_type (type);
1775 break;
1777 case EM_FR30:
1778 case EM_CYGNUS_FR30:
1779 rtype = elf_fr30_reloc_type (type);
1780 break;
1782 case EM_CYGNUS_FRV:
1783 rtype = elf_frv_reloc_type (type);
1784 break;
1786 case EM_CSKY:
1787 rtype = elf_csky_reloc_type (type);
1788 break;
1790 case EM_FT32:
1791 rtype = elf_ft32_reloc_type (type);
1792 break;
1794 case EM_MCORE:
1795 rtype = elf_mcore_reloc_type (type);
1796 break;
1798 case EM_MMIX:
1799 rtype = elf_mmix_reloc_type (type);
1800 break;
1802 case EM_MOXIE:
1803 rtype = elf_moxie_reloc_type (type);
1804 break;
1806 case EM_MSP430:
1807 if (uses_msp430x_relocs (filedata))
1809 rtype = elf_msp430x_reloc_type (type);
1810 break;
1812 /* Fall through. */
1813 case EM_MSP430_OLD:
1814 rtype = elf_msp430_reloc_type (type);
1815 break;
1817 case EM_NDS32:
1818 rtype = elf_nds32_reloc_type (type);
1819 break;
1821 case EM_PPC:
1822 rtype = elf_ppc_reloc_type (type);
1823 break;
1825 case EM_PPC64:
1826 rtype = elf_ppc64_reloc_type (type);
1827 break;
1829 case EM_MIPS:
1830 case EM_MIPS_RS3_LE:
1831 rtype = elf_mips_reloc_type (type);
1832 break;
1834 case EM_RISCV:
1835 rtype = elf_riscv_reloc_type (type);
1836 break;
1838 case EM_ALPHA:
1839 rtype = elf_alpha_reloc_type (type);
1840 break;
1842 case EM_ARM:
1843 rtype = elf_arm_reloc_type (type);
1844 break;
1846 case EM_ARC:
1847 case EM_ARC_COMPACT:
1848 case EM_ARC_COMPACT2:
1849 case EM_ARC_COMPACT3:
1850 case EM_ARC_COMPACT3_64:
1851 rtype = elf_arc_reloc_type (type);
1852 break;
1854 case EM_PARISC:
1855 rtype = elf_hppa_reloc_type (type);
1856 break;
1858 case EM_H8_300:
1859 case EM_H8_300H:
1860 case EM_H8S:
1861 rtype = elf_h8_reloc_type (type);
1862 break;
1864 case EM_OR1K:
1865 rtype = elf_or1k_reloc_type (type);
1866 break;
1868 case EM_PJ:
1869 case EM_PJ_OLD:
1870 rtype = elf_pj_reloc_type (type);
1871 break;
1872 case EM_IA_64:
1873 rtype = elf_ia64_reloc_type (type);
1874 break;
1876 case EM_KVX:
1877 rtype = elf_kvx_reloc_type (type);
1878 break;
1880 case EM_CRIS:
1881 rtype = elf_cris_reloc_type (type);
1882 break;
1884 case EM_860:
1885 rtype = elf_i860_reloc_type (type);
1886 break;
1888 case EM_X86_64:
1889 case EM_L1OM:
1890 case EM_K1OM:
1891 rtype = elf_x86_64_reloc_type (type);
1892 break;
1894 case EM_S370:
1895 rtype = i370_reloc_type (type);
1896 break;
1898 case EM_S390_OLD:
1899 case EM_S390:
1900 rtype = elf_s390_reloc_type (type);
1901 break;
1903 case EM_SCORE:
1904 rtype = elf_score_reloc_type (type);
1905 break;
1907 case EM_XSTORMY16:
1908 rtype = elf_xstormy16_reloc_type (type);
1909 break;
1911 case EM_CRX:
1912 rtype = elf_crx_reloc_type (type);
1913 break;
1915 case EM_VAX:
1916 rtype = elf_vax_reloc_type (type);
1917 break;
1919 case EM_VISIUM:
1920 rtype = elf_visium_reloc_type (type);
1921 break;
1923 case EM_BPF:
1924 rtype = elf_bpf_reloc_type (type);
1925 break;
1927 case EM_ADAPTEVA_EPIPHANY:
1928 rtype = elf_epiphany_reloc_type (type);
1929 break;
1931 case EM_IP2K:
1932 case EM_IP2K_OLD:
1933 rtype = elf_ip2k_reloc_type (type);
1934 break;
1936 case EM_IQ2000:
1937 rtype = elf_iq2000_reloc_type (type);
1938 break;
1940 case EM_XTENSA_OLD:
1941 case EM_XTENSA:
1942 rtype = elf_xtensa_reloc_type (type);
1943 break;
1945 case EM_LATTICEMICO32:
1946 rtype = elf_lm32_reloc_type (type);
1947 break;
1949 case EM_M32C_OLD:
1950 case EM_M32C:
1951 rtype = elf_m32c_reloc_type (type);
1952 break;
1954 case EM_MT:
1955 rtype = elf_mt_reloc_type (type);
1956 break;
1958 case EM_BLACKFIN:
1959 rtype = elf_bfin_reloc_type (type);
1960 break;
1962 case EM_CYGNUS_MEP:
1963 rtype = elf_mep_reloc_type (type);
1964 break;
1966 case EM_CR16:
1967 rtype = elf_cr16_reloc_type (type);
1968 break;
1970 case EM_MICROBLAZE:
1971 case EM_MICROBLAZE_OLD:
1972 rtype = elf_microblaze_reloc_type (type);
1973 break;
1975 case EM_RL78:
1976 rtype = elf_rl78_reloc_type (type);
1977 break;
1979 case EM_RX:
1980 rtype = elf_rx_reloc_type (type);
1981 break;
1983 case EM_METAG:
1984 rtype = elf_metag_reloc_type (type);
1985 break;
1987 case EM_TI_C6000:
1988 rtype = elf_tic6x_reloc_type (type);
1989 break;
1991 case EM_TILEGX:
1992 rtype = elf_tilegx_reloc_type (type);
1993 break;
1995 case EM_TILEPRO:
1996 rtype = elf_tilepro_reloc_type (type);
1997 break;
1999 case EM_WEBASSEMBLY:
2000 rtype = elf_wasm32_reloc_type (type);
2001 break;
2003 case EM_XGATE:
2004 rtype = elf_xgate_reloc_type (type);
2005 break;
2007 case EM_ALTERA_NIOS2:
2008 rtype = elf_nios2_reloc_type (type);
2009 break;
2011 case EM_TI_PRU:
2012 rtype = elf_pru_reloc_type (type);
2013 break;
2015 case EM_NFP:
2016 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
2017 rtype = elf_nfp3200_reloc_type (type);
2018 else
2019 rtype = elf_nfp_reloc_type (type);
2020 break;
2022 case EM_Z80:
2023 rtype = elf_z80_reloc_type (type);
2024 break;
2026 case EM_LOONGARCH:
2027 rtype = elf_loongarch_reloc_type (type);
2028 break;
2030 case EM_AMDGPU:
2031 rtype = elf_amdgpu_reloc_type (type);
2032 break;
2035 if (rtype == NULL)
2036 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
2037 else
2038 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
2040 if (filedata->file_header.e_machine == EM_ALPHA
2041 && rtype != NULL
2042 && streq (rtype, "R_ALPHA_LITUSE")
2043 && rel_type == reltype_rela)
2045 switch (rels[i].r_addend)
2047 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
2048 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
2049 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
2050 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
2051 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
2052 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
2053 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
2054 default: rtype = NULL;
2057 if (rtype)
2058 printf (" (%s)", rtype);
2059 else
2061 putchar (' ');
2062 printf (_("<unknown addend: %" PRIx64 ">"),
2063 rels[i].r_addend);
2064 res = false;
2067 else if (symtab_index)
2069 if (symtab == NULL || symtab_index >= nsyms)
2071 error (_(" bad symbol index: %08lx in reloc\n"),
2072 (unsigned long) symtab_index);
2073 res = false;
2075 else
2077 Elf_Internal_Sym * psym;
2078 const char * version_string;
2079 enum versioned_symbol_info sym_info;
2080 unsigned short vna_other;
2082 psym = symtab + symtab_index;
2084 version_string
2085 = get_symbol_version_string (filedata, is_dynsym,
2086 strtab, strtablen,
2087 symtab_index,
2088 psym,
2089 &sym_info,
2090 &vna_other);
2092 printf (" ");
2094 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
2096 const char * name;
2097 unsigned int len;
2098 unsigned int width = is_32bit_elf ? 8 : 14;
2100 /* Relocations against GNU_IFUNC symbols do not use the value
2101 of the symbol as the address to relocate against. Instead
2102 they invoke the function named by the symbol and use its
2103 result as the address for relocation.
2105 To indicate this to the user, do not display the value of
2106 the symbol in the "Symbols's Value" field. Instead show
2107 its name followed by () as a hint that the symbol is
2108 invoked. */
2110 if (strtab == NULL
2111 || psym->st_name == 0
2112 || psym->st_name >= strtablen)
2113 name = "??";
2114 else
2115 name = strtab + psym->st_name;
2117 len = print_symbol_name (width, name);
2118 if (version_string)
2119 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2120 version_string);
2121 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
2123 else
2125 print_vma (psym->st_value, LONG_HEX);
2127 printf (is_32bit_elf ? " " : " ");
2130 if (psym->st_name == 0)
2132 const char * sec_name = "<null>";
2134 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2135 sec_name = printable_section_name_from_index
2136 (filedata, psym->st_shndx, NULL);
2138 print_symbol_name (22, sec_name);
2140 else if (strtab == NULL)
2141 printf (_("<string table index: %3ld>"), psym->st_name);
2142 else if (psym->st_name >= strtablen)
2144 error (_("<corrupt string table index: %3ld>\n"),
2145 psym->st_name);
2146 res = false;
2148 else
2150 print_symbol_name (22, strtab + psym->st_name);
2151 if (version_string)
2152 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2153 version_string);
2156 if (rel_type == reltype_rela)
2158 uint64_t off = rels[i].r_addend;
2160 if ((int64_t) off < 0)
2161 printf (" - %" PRIx64, -off);
2162 else
2163 printf (" + %" PRIx64, off);
2167 else if (rel_type == reltype_rela)
2169 uint64_t off = rels[i].r_addend;
2171 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2172 if ((int64_t) off < 0)
2173 printf ("-%" PRIx64, -off);
2174 else
2175 printf ("%" PRIx64, off);
2178 if (filedata->file_header.e_machine == EM_SPARCV9
2179 && rtype != NULL
2180 && streq (rtype, "R_SPARC_OLO10"))
2181 printf (" + %" PRIx64, ELF64_R_TYPE_DATA (inf));
2183 putchar ('\n');
2185 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2187 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2188 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2189 const char * rtype2 = elf_mips_reloc_type (type2);
2190 const char * rtype3 = elf_mips_reloc_type (type3);
2192 printf (" Type2: ");
2194 if (rtype2 == NULL)
2195 printf (_("unrecognized: %-7lx"),
2196 (unsigned long) type2 & 0xffffffff);
2197 else
2198 printf ("%-17.17s", rtype2);
2200 printf ("\n Type3: ");
2202 if (rtype3 == NULL)
2203 printf (_("unrecognized: %-7lx"),
2204 (unsigned long) type3 & 0xffffffff);
2205 else
2206 printf ("%-17.17s", rtype3);
2208 putchar ('\n');
2212 free (rels);
2214 return res;
2217 static const char *
2218 get_aarch64_dynamic_type (unsigned long type)
2220 switch (type)
2222 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2223 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2224 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2225 default:
2226 return NULL;
2230 static const char *
2231 get_mips_dynamic_type (unsigned long type)
2233 switch (type)
2235 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2236 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2237 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2238 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2239 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2240 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2241 case DT_MIPS_MSYM: return "MIPS_MSYM";
2242 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2243 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2244 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2245 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2246 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2247 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2248 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2249 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2250 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2251 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2252 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2253 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2254 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2255 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2256 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2257 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2258 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2259 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2260 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2261 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2262 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2263 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2264 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2265 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2266 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2267 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2268 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2269 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2270 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2271 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2272 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2273 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2274 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2275 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2276 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2277 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2278 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2279 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2280 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2281 case DT_MIPS_XHASH: return "MIPS_XHASH";
2282 default:
2283 return NULL;
2287 static const char *
2288 get_sparc64_dynamic_type (unsigned long type)
2290 switch (type)
2292 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2293 default:
2294 return NULL;
2298 static const char *
2299 get_ppc_dynamic_type (unsigned long type)
2301 switch (type)
2303 case DT_PPC_GOT: return "PPC_GOT";
2304 case DT_PPC_OPT: return "PPC_OPT";
2305 default:
2306 return NULL;
2310 static const char *
2311 get_ppc64_dynamic_type (unsigned long type)
2313 switch (type)
2315 case DT_PPC64_GLINK: return "PPC64_GLINK";
2316 case DT_PPC64_OPD: return "PPC64_OPD";
2317 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2318 case DT_PPC64_OPT: return "PPC64_OPT";
2319 default:
2320 return NULL;
2324 static const char *
2325 get_parisc_dynamic_type (unsigned long type)
2327 switch (type)
2329 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2330 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2331 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2332 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2333 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2334 case DT_HP_PREINIT: return "HP_PREINIT";
2335 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2336 case DT_HP_NEEDED: return "HP_NEEDED";
2337 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2338 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2339 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2340 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2341 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2342 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2343 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2344 case DT_HP_FILTERED: return "HP_FILTERED";
2345 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2346 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2347 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2348 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2349 case DT_PLT: return "PLT";
2350 case DT_PLT_SIZE: return "PLT_SIZE";
2351 case DT_DLT: return "DLT";
2352 case DT_DLT_SIZE: return "DLT_SIZE";
2353 default:
2354 return NULL;
2358 static const char *
2359 get_ia64_dynamic_type (unsigned long type)
2361 switch (type)
2363 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2364 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2365 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2366 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2367 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2368 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2369 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2370 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2371 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2372 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2373 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2374 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2375 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2376 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2377 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2378 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2379 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2380 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2381 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2382 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2383 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2384 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2385 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2386 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2387 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2388 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2389 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2390 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2391 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2392 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2393 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2394 default:
2395 return NULL;
2399 static const char *
2400 get_solaris_section_type (unsigned long type)
2402 switch (type)
2404 case 0x6fffffee: return "SUNW_ancillary";
2405 case 0x6fffffef: return "SUNW_capchain";
2406 case 0x6ffffff0: return "SUNW_capinfo";
2407 case 0x6ffffff1: return "SUNW_symsort";
2408 case 0x6ffffff2: return "SUNW_tlssort";
2409 case 0x6ffffff3: return "SUNW_LDYNSYM";
2410 case 0x6ffffff4: return "SUNW_dof";
2411 case 0x6ffffff5: return "SUNW_cap";
2412 case 0x6ffffff6: return "SUNW_SIGNATURE";
2413 case 0x6ffffff7: return "SUNW_ANNOTATE";
2414 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2415 case 0x6ffffff9: return "SUNW_DEBUG";
2416 case 0x6ffffffa: return "SUNW_move";
2417 case 0x6ffffffb: return "SUNW_COMDAT";
2418 case 0x6ffffffc: return "SUNW_syminfo";
2419 case 0x6ffffffd: return "SUNW_verdef";
2420 case 0x6ffffffe: return "SUNW_verneed";
2421 case 0x6fffffff: return "SUNW_versym";
2422 case 0x70000000: return "SPARC_GOTDATA";
2423 default: return NULL;
2427 static const char *
2428 get_alpha_dynamic_type (unsigned long type)
2430 switch (type)
2432 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2433 default: return NULL;
2437 static const char *
2438 get_score_dynamic_type (unsigned long type)
2440 switch (type)
2442 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2443 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2444 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2445 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2446 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2447 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2448 default: return NULL;
2452 static const char *
2453 get_tic6x_dynamic_type (unsigned long type)
2455 switch (type)
2457 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2458 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2459 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2460 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2461 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2462 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2463 default: return NULL;
2467 static const char *
2468 get_nios2_dynamic_type (unsigned long type)
2470 switch (type)
2472 case DT_NIOS2_GP: return "NIOS2_GP";
2473 default: return NULL;
2477 static const char *
2478 get_solaris_dynamic_type (unsigned long type)
2480 switch (type)
2482 case 0x6000000d: return "SUNW_AUXILIARY";
2483 case 0x6000000e: return "SUNW_RTLDINF";
2484 case 0x6000000f: return "SUNW_FILTER";
2485 case 0x60000010: return "SUNW_CAP";
2486 case 0x60000011: return "SUNW_SYMTAB";
2487 case 0x60000012: return "SUNW_SYMSZ";
2488 case 0x60000013: return "SUNW_SORTENT";
2489 case 0x60000014: return "SUNW_SYMSORT";
2490 case 0x60000015: return "SUNW_SYMSORTSZ";
2491 case 0x60000016: return "SUNW_TLSSORT";
2492 case 0x60000017: return "SUNW_TLSSORTSZ";
2493 case 0x60000018: return "SUNW_CAPINFO";
2494 case 0x60000019: return "SUNW_STRPAD";
2495 case 0x6000001a: return "SUNW_CAPCHAIN";
2496 case 0x6000001b: return "SUNW_LDMACH";
2497 case 0x6000001d: return "SUNW_CAPCHAINENT";
2498 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2499 case 0x60000021: return "SUNW_PARENT";
2500 case 0x60000023: return "SUNW_ASLR";
2501 case 0x60000025: return "SUNW_RELAX";
2502 case 0x60000029: return "SUNW_NXHEAP";
2503 case 0x6000002b: return "SUNW_NXSTACK";
2505 case 0x70000001: return "SPARC_REGISTER";
2506 case 0x7ffffffd: return "AUXILIARY";
2507 case 0x7ffffffe: return "USED";
2508 case 0x7fffffff: return "FILTER";
2510 default: return NULL;
2514 static const char *
2515 get_riscv_dynamic_type (unsigned long type)
2517 switch (type)
2519 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2520 default:
2521 return NULL;
2525 static const char *
2526 get_dynamic_type (Filedata * filedata, unsigned long type)
2528 static char buff[64];
2530 switch (type)
2532 case DT_NULL: return "NULL";
2533 case DT_NEEDED: return "NEEDED";
2534 case DT_PLTRELSZ: return "PLTRELSZ";
2535 case DT_PLTGOT: return "PLTGOT";
2536 case DT_HASH: return "HASH";
2537 case DT_STRTAB: return "STRTAB";
2538 case DT_SYMTAB: return "SYMTAB";
2539 case DT_RELA: return "RELA";
2540 case DT_RELASZ: return "RELASZ";
2541 case DT_RELAENT: return "RELAENT";
2542 case DT_STRSZ: return "STRSZ";
2543 case DT_SYMENT: return "SYMENT";
2544 case DT_INIT: return "INIT";
2545 case DT_FINI: return "FINI";
2546 case DT_SONAME: return "SONAME";
2547 case DT_RPATH: return "RPATH";
2548 case DT_SYMBOLIC: return "SYMBOLIC";
2549 case DT_REL: return "REL";
2550 case DT_RELSZ: return "RELSZ";
2551 case DT_RELENT: return "RELENT";
2552 case DT_RELR: return "RELR";
2553 case DT_RELRSZ: return "RELRSZ";
2554 case DT_RELRENT: return "RELRENT";
2555 case DT_PLTREL: return "PLTREL";
2556 case DT_DEBUG: return "DEBUG";
2557 case DT_TEXTREL: return "TEXTREL";
2558 case DT_JMPREL: return "JMPREL";
2559 case DT_BIND_NOW: return "BIND_NOW";
2560 case DT_INIT_ARRAY: return "INIT_ARRAY";
2561 case DT_FINI_ARRAY: return "FINI_ARRAY";
2562 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2563 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2564 case DT_RUNPATH: return "RUNPATH";
2565 case DT_FLAGS: return "FLAGS";
2567 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2568 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2569 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2571 case DT_CHECKSUM: return "CHECKSUM";
2572 case DT_PLTPADSZ: return "PLTPADSZ";
2573 case DT_MOVEENT: return "MOVEENT";
2574 case DT_MOVESZ: return "MOVESZ";
2575 case DT_FEATURE: return "FEATURE";
2576 case DT_POSFLAG_1: return "POSFLAG_1";
2577 case DT_SYMINSZ: return "SYMINSZ";
2578 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2580 case DT_ADDRRNGLO: return "ADDRRNGLO";
2581 case DT_CONFIG: return "CONFIG";
2582 case DT_DEPAUDIT: return "DEPAUDIT";
2583 case DT_AUDIT: return "AUDIT";
2584 case DT_PLTPAD: return "PLTPAD";
2585 case DT_MOVETAB: return "MOVETAB";
2586 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2588 case DT_VERSYM: return "VERSYM";
2590 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2591 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2592 case DT_RELACOUNT: return "RELACOUNT";
2593 case DT_RELCOUNT: return "RELCOUNT";
2594 case DT_FLAGS_1: return "FLAGS_1";
2595 case DT_VERDEF: return "VERDEF";
2596 case DT_VERDEFNUM: return "VERDEFNUM";
2597 case DT_VERNEED: return "VERNEED";
2598 case DT_VERNEEDNUM: return "VERNEEDNUM";
2600 case DT_AUXILIARY: return "AUXILIARY";
2601 case DT_USED: return "USED";
2602 case DT_FILTER: return "FILTER";
2604 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2605 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2606 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2607 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2608 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2609 case DT_GNU_HASH: return "GNU_HASH";
2610 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2612 default:
2613 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2615 const char * result;
2617 switch (filedata->file_header.e_machine)
2619 case EM_AARCH64:
2620 result = get_aarch64_dynamic_type (type);
2621 break;
2622 case EM_MIPS:
2623 case EM_MIPS_RS3_LE:
2624 result = get_mips_dynamic_type (type);
2625 break;
2626 case EM_SPARCV9:
2627 result = get_sparc64_dynamic_type (type);
2628 break;
2629 case EM_PPC:
2630 result = get_ppc_dynamic_type (type);
2631 break;
2632 case EM_PPC64:
2633 result = get_ppc64_dynamic_type (type);
2634 break;
2635 case EM_IA_64:
2636 result = get_ia64_dynamic_type (type);
2637 break;
2638 case EM_ALPHA:
2639 result = get_alpha_dynamic_type (type);
2640 break;
2641 case EM_SCORE:
2642 result = get_score_dynamic_type (type);
2643 break;
2644 case EM_TI_C6000:
2645 result = get_tic6x_dynamic_type (type);
2646 break;
2647 case EM_ALTERA_NIOS2:
2648 result = get_nios2_dynamic_type (type);
2649 break;
2650 case EM_RISCV:
2651 result = get_riscv_dynamic_type (type);
2652 break;
2653 default:
2654 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2655 result = get_solaris_dynamic_type (type);
2656 else
2657 result = NULL;
2658 break;
2661 if (result != NULL)
2662 return result;
2664 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2666 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2667 || (filedata->file_header.e_machine == EM_PARISC
2668 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2670 const char * result;
2672 switch (filedata->file_header.e_machine)
2674 case EM_PARISC:
2675 result = get_parisc_dynamic_type (type);
2676 break;
2677 case EM_IA_64:
2678 result = get_ia64_dynamic_type (type);
2679 break;
2680 default:
2681 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2682 result = get_solaris_dynamic_type (type);
2683 else
2684 result = NULL;
2685 break;
2688 if (result != NULL)
2689 return result;
2691 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2692 type);
2694 else
2695 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2697 return buff;
2701 static bool get_program_headers (Filedata *);
2702 static bool get_dynamic_section (Filedata *);
2704 static void
2705 locate_dynamic_section (Filedata *filedata)
2707 uint64_t dynamic_addr = 0;
2708 uint64_t dynamic_size = 0;
2710 if (filedata->file_header.e_phnum != 0
2711 && get_program_headers (filedata))
2713 Elf_Internal_Phdr *segment;
2714 unsigned int i;
2716 for (i = 0, segment = filedata->program_headers;
2717 i < filedata->file_header.e_phnum;
2718 i++, segment++)
2720 if (segment->p_type == PT_DYNAMIC)
2722 dynamic_addr = segment->p_offset;
2723 dynamic_size = segment->p_filesz;
2725 if (filedata->section_headers != NULL)
2727 Elf_Internal_Shdr *sec;
2729 sec = find_section (filedata, ".dynamic");
2730 if (sec != NULL)
2732 if (sec->sh_size == 0
2733 || sec->sh_type == SHT_NOBITS)
2735 dynamic_addr = 0;
2736 dynamic_size = 0;
2738 else
2740 dynamic_addr = sec->sh_offset;
2741 dynamic_size = sec->sh_size;
2746 if (dynamic_addr > filedata->file_size
2747 || (dynamic_size > filedata->file_size - dynamic_addr))
2749 dynamic_addr = 0;
2750 dynamic_size = 0;
2752 break;
2756 filedata->dynamic_addr = dynamic_addr;
2757 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2760 static bool
2761 is_pie (Filedata *filedata)
2763 Elf_Internal_Dyn *entry;
2765 if (filedata->dynamic_size == 0)
2766 locate_dynamic_section (filedata);
2767 if (filedata->dynamic_size <= 1)
2768 return false;
2770 if (!get_dynamic_section (filedata))
2771 return false;
2773 for (entry = filedata->dynamic_section;
2774 entry < filedata->dynamic_section + filedata->dynamic_nent;
2775 entry++)
2777 if (entry->d_tag == DT_FLAGS_1)
2779 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2780 return true;
2781 break;
2784 return false;
2787 static char *
2788 get_file_type (Filedata *filedata)
2790 unsigned e_type = filedata->file_header.e_type;
2791 static char buff[64];
2793 switch (e_type)
2795 case ET_NONE: return _("NONE (None)");
2796 case ET_REL: return _("REL (Relocatable file)");
2797 case ET_EXEC: return _("EXEC (Executable file)");
2798 case ET_DYN:
2799 if (is_pie (filedata))
2800 return _("DYN (Position-Independent Executable file)");
2801 else
2802 return _("DYN (Shared object file)");
2803 case ET_CORE: return _("CORE (Core file)");
2805 default:
2806 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2807 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2808 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2809 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2810 else
2811 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2812 return buff;
2816 static char *
2817 get_machine_name (unsigned e_machine)
2819 static char buff[64]; /* XXX */
2821 switch (e_machine)
2823 /* Please keep this switch table sorted by increasing EM_ value. */
2824 /* 0 */
2825 case EM_NONE: return _("None");
2826 case EM_M32: return "WE32100";
2827 case EM_SPARC: return "Sparc";
2828 case EM_386: return "Intel 80386";
2829 case EM_68K: return "MC68000";
2830 case EM_88K: return "MC88000";
2831 case EM_IAMCU: return "Intel MCU";
2832 case EM_860: return "Intel 80860";
2833 case EM_MIPS: return "MIPS R3000";
2834 case EM_S370: return "IBM System/370";
2835 /* 10 */
2836 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2837 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2838 case EM_PARISC: return "HPPA";
2839 case EM_VPP550: return "Fujitsu VPP500";
2840 case EM_SPARC32PLUS: return "Sparc v8+" ;
2841 case EM_960: return "Intel 80960";
2842 case EM_PPC: return "PowerPC";
2843 /* 20 */
2844 case EM_PPC64: return "PowerPC64";
2845 case EM_S390_OLD:
2846 case EM_S390: return "IBM S/390";
2847 case EM_SPU: return "SPU";
2848 /* 30 */
2849 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2850 case EM_FR20: return "Fujitsu FR20";
2851 case EM_RH32: return "TRW RH32";
2852 case EM_MCORE: return "MCORE";
2853 /* 40 */
2854 case EM_ARM: return "ARM";
2855 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2856 case EM_SH: return "Renesas / SuperH SH";
2857 case EM_SPARCV9: return "Sparc v9";
2858 case EM_TRICORE: return "Siemens Tricore";
2859 case EM_ARC: return "ARC";
2860 case EM_H8_300: return "Renesas H8/300";
2861 case EM_H8_300H: return "Renesas H8/300H";
2862 case EM_H8S: return "Renesas H8S";
2863 case EM_H8_500: return "Renesas H8/500";
2864 /* 50 */
2865 case EM_IA_64: return "Intel IA-64";
2866 case EM_MIPS_X: return "Stanford MIPS-X";
2867 case EM_COLDFIRE: return "Motorola Coldfire";
2868 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2869 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2870 case EM_PCP: return "Siemens PCP";
2871 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2872 case EM_NDR1: return "Denso NDR1 microprocessor";
2873 case EM_STARCORE: return "Motorola Star*Core processor";
2874 case EM_ME16: return "Toyota ME16 processor";
2875 /* 60 */
2876 case EM_ST100: return "STMicroelectronics ST100 processor";
2877 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2878 case EM_X86_64: return "Advanced Micro Devices X86-64";
2879 case EM_PDSP: return "Sony DSP processor";
2880 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2881 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2882 case EM_FX66: return "Siemens FX66 microcontroller";
2883 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2884 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2885 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2886 /* 70 */
2887 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2888 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2889 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2890 case EM_SVX: return "Silicon Graphics SVx";
2891 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2892 case EM_VAX: return "Digital VAX";
2893 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2894 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2895 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2896 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2897 /* 80 */
2898 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2899 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2900 case EM_PRISM: return "Vitesse Prism";
2901 case EM_AVR_OLD:
2902 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2903 case EM_CYGNUS_FR30:
2904 case EM_FR30: return "Fujitsu FR30";
2905 case EM_CYGNUS_D10V:
2906 case EM_D10V: return "d10v";
2907 case EM_CYGNUS_D30V:
2908 case EM_D30V: return "d30v";
2909 case EM_CYGNUS_V850:
2910 case EM_V850: return "Renesas V850";
2911 case EM_CYGNUS_M32R:
2912 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2913 case EM_CYGNUS_MN10300:
2914 case EM_MN10300: return "mn10300";
2915 /* 90 */
2916 case EM_CYGNUS_MN10200:
2917 case EM_MN10200: return "mn10200";
2918 case EM_PJ: return "picoJava";
2919 case EM_OR1K: return "OpenRISC 1000";
2920 case EM_ARC_COMPACT: return "ARCompact";
2921 case EM_XTENSA_OLD:
2922 case EM_XTENSA: return "Tensilica Xtensa Processor";
2923 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2924 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2925 case EM_NS32K: return "National Semiconductor 32000 series";
2926 case EM_TPC: return "Tenor Network TPC processor";
2927 case EM_SNP1K: return "Trebia SNP 1000 processor";
2928 /* 100 */
2929 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2930 case EM_IP2K_OLD:
2931 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2932 case EM_MAX: return "MAX Processor";
2933 case EM_CR: return "National Semiconductor CompactRISC";
2934 case EM_F2MC16: return "Fujitsu F2MC16";
2935 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2936 case EM_BLACKFIN: return "Analog Devices Blackfin";
2937 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2938 case EM_SEP: return "Sharp embedded microprocessor";
2939 case EM_ARCA: return "Arca RISC microprocessor";
2940 /* 110 */
2941 case EM_UNICORE: return "Unicore";
2942 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2943 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2944 case EM_ALTERA_NIOS2: return "Altera Nios II";
2945 case EM_CRX: return "National Semiconductor CRX microprocessor";
2946 case EM_XGATE: return "Motorola XGATE embedded processor";
2947 case EM_C166:
2948 case EM_XC16X: return "Infineon Technologies xc16x";
2949 case EM_M16C: return "Renesas M16C series microprocessors";
2950 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2951 case EM_CE: return "Freescale Communication Engine RISC core";
2952 /* 120 */
2953 case EM_M32C: return "Renesas M32c";
2954 /* 130 */
2955 case EM_TSK3000: return "Altium TSK3000 core";
2956 case EM_RS08: return "Freescale RS08 embedded processor";
2957 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2958 case EM_SCORE: return "SUNPLUS S+Core";
2959 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2960 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2961 case EM_LATTICEMICO32: return "Lattice Mico32";
2962 case EM_SE_C17: return "Seiko Epson C17 family";
2963 /* 140 */
2964 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2965 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2966 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2967 case EM_TI_PRU: return "TI PRU I/O processor";
2968 /* 160 */
2969 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2970 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2971 case EM_R32C: return "Renesas R32C series microprocessors";
2972 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2973 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2974 case EM_8051: return "Intel 8051 and variants";
2975 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2976 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2977 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2978 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2979 /* 170 */
2980 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2981 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2982 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2983 case EM_RX: return "Renesas RX";
2984 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2985 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2986 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2987 case EM_CR16:
2988 case EM_MICROBLAZE:
2989 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2990 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2991 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2992 /* 180 */
2993 case EM_L1OM: return "Intel L1OM";
2994 case EM_K1OM: return "Intel K1OM";
2995 case EM_INTEL182: return "Intel (reserved)";
2996 case EM_AARCH64: return "AArch64";
2997 case EM_ARM184: return "ARM (reserved)";
2998 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2999 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
3000 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
3001 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
3002 /* 190 */
3003 case EM_CUDA: return "NVIDIA CUDA architecture";
3004 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
3005 case EM_CLOUDSHIELD: return "CloudShield architecture family";
3006 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
3007 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
3008 case EM_ARC_COMPACT2: return "ARCv2";
3009 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
3010 case EM_RL78: return "Renesas RL78";
3011 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
3012 case EM_78K0R: return "Renesas 78K0R";
3013 /* 200 */
3014 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
3015 case EM_BA1: return "Beyond BA1 CPU architecture";
3016 case EM_BA2: return "Beyond BA2 CPU architecture";
3017 case EM_XCORE: return "XMOS xCORE processor family";
3018 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
3019 case EM_INTELGT: return "Intel Graphics Technology";
3020 /* 210 */
3021 case EM_KM32: return "KM211 KM32 32-bit processor";
3022 case EM_KMX32: return "KM211 KMX32 32-bit processor";
3023 case EM_KMX16: return "KM211 KMX16 16-bit processor";
3024 case EM_KMX8: return "KM211 KMX8 8-bit processor";
3025 case EM_KVARC: return "KM211 KVARC processor";
3026 case EM_CDP: return "Paneve CDP architecture family";
3027 case EM_COGE: return "Cognitive Smart Memory Processor";
3028 case EM_COOL: return "Bluechip Systems CoolEngine";
3029 case EM_NORC: return "Nanoradio Optimized RISC";
3030 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
3031 /* 220 */
3032 case EM_Z80: return "Zilog Z80";
3033 case EM_VISIUM: return "CDS VISIUMcore processor";
3034 case EM_FT32: return "FTDI Chip FT32";
3035 case EM_MOXIE: return "Moxie";
3036 case EM_AMDGPU: return "AMD GPU";
3037 /* 230 (all reserved) */
3038 /* 240 */
3039 case EM_RISCV: return "RISC-V";
3040 case EM_LANAI: return "Lanai 32-bit processor";
3041 case EM_CEVA: return "CEVA Processor Architecture Family";
3042 case EM_CEVA_X2: return "CEVA X2 Processor Family";
3043 case EM_BPF: return "Linux BPF";
3044 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
3045 case EM_IMG1: return "Imagination Technologies";
3046 /* 250 */
3047 case EM_NFP: return "Netronome Flow Processor";
3048 case EM_VE: return "NEC Vector Engine";
3049 case EM_CSKY: return "C-SKY";
3050 case EM_ARC_COMPACT3_64: return "Synopsys ARCv3 64-bit processor";
3051 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
3052 case EM_ARC_COMPACT3: return "Synopsys ARCv3 32-bit processor";
3053 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
3054 case EM_65816: return "WDC 65816/65C816";
3055 case EM_LOONGARCH: return "LoongArch";
3056 case EM_KF32: return "ChipON KungFu32";
3058 /* Large numbers... */
3059 case EM_MT: return "Morpho Techologies MT processor";
3060 case EM_ALPHA: return "Alpha";
3061 case EM_WEBASSEMBLY: return "Web Assembly";
3062 case EM_DLX: return "OpenDLX";
3063 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
3064 case EM_IQ2000: return "Vitesse IQ2000";
3065 case EM_M32C_OLD:
3066 case EM_NIOS32: return "Altera Nios";
3067 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
3068 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
3069 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
3070 case EM_S12Z: return "Freescale S12Z";
3072 default:
3073 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
3074 return buff;
3078 static char *
3079 decode_ARC_machine_flags (char *out, unsigned e_flags, unsigned e_machine)
3081 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
3082 other compilers don't specify an architecture type in the e_flags, and
3083 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3084 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3085 architectures.
3087 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3088 but also sets a specific architecture type in the e_flags field.
3090 However, when decoding the flags we don't worry if we see an
3091 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3092 ARCEM architecture type. */
3094 switch (e_flags & EF_ARC_MACH_MSK)
3096 /* We only expect these to occur for EM_ARC_COMPACT2. */
3097 case EF_ARC_CPU_ARCV2EM:
3098 out = stpcpy (out, ", ARC EM");
3099 break;
3100 case EF_ARC_CPU_ARCV2HS:
3101 out = stpcpy (out, ", ARC HS");
3102 break;
3104 /* We only expect these to occur for EM_ARC_COMPACT. */
3105 case E_ARC_MACH_ARC600:
3106 out = stpcpy (out, ", ARC600");
3107 break;
3108 case E_ARC_MACH_ARC601:
3109 out = stpcpy (out, ", ARC601");
3110 break;
3111 case E_ARC_MACH_ARC700:
3112 out = stpcpy (out, ", ARC700");
3113 break;
3115 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3116 new ELF with new architecture being read by an old version of
3117 readelf, or (c) An ELF built with non-GNU compiler that does not
3118 set the architecture in the e_flags. */
3119 default:
3120 if (e_machine == EM_ARC_COMPACT)
3121 out = stpcpy (out, ", Unknown ARCompact");
3122 else
3123 out = stpcpy (out, ", Unknown ARC");
3124 break;
3127 switch (e_flags & EF_ARC_OSABI_MSK)
3129 case E_ARC_OSABI_ORIG:
3130 out = stpcpy (out, ", (ABI:legacy)");
3131 break;
3132 case E_ARC_OSABI_V2:
3133 out = stpcpy (out, ", (ABI:v2)");
3134 break;
3135 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3136 case E_ARC_OSABI_V3:
3137 out = stpcpy (out, ", v3 no-legacy-syscalls ABI");
3138 break;
3139 case E_ARC_OSABI_V4:
3140 out = stpcpy (out, ", v4 ABI");
3141 break;
3142 default:
3143 out = stpcpy (out, ", unrecognised ARC OSABI flag");
3144 break;
3146 return out;
3149 static char *
3150 decode_ARM_machine_flags (char *out, unsigned e_flags)
3152 unsigned eabi;
3153 bool unknown = false;
3155 eabi = EF_ARM_EABI_VERSION (e_flags);
3156 e_flags &= ~ EF_ARM_EABIMASK;
3158 /* Handle "generic" ARM flags. */
3159 if (e_flags & EF_ARM_RELEXEC)
3161 out = stpcpy (out, ", relocatable executable");
3162 e_flags &= ~ EF_ARM_RELEXEC;
3165 if (e_flags & EF_ARM_PIC)
3167 out = stpcpy (out, ", position independent");
3168 e_flags &= ~ EF_ARM_PIC;
3171 /* Now handle EABI specific flags. */
3172 switch (eabi)
3174 default:
3175 out = stpcpy (out, ", <unrecognized EABI>");
3176 if (e_flags)
3177 unknown = true;
3178 break;
3180 case EF_ARM_EABI_VER1:
3181 out = stpcpy (out, ", Version1 EABI");
3182 while (e_flags)
3184 unsigned flag;
3186 /* Process flags one bit at a time. */
3187 flag = e_flags & - e_flags;
3188 e_flags &= ~ flag;
3190 switch (flag)
3192 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3193 out = stpcpy (out, ", sorted symbol tables");
3194 break;
3196 default:
3197 unknown = true;
3198 break;
3201 break;
3203 case EF_ARM_EABI_VER2:
3204 out = stpcpy (out, ", Version2 EABI");
3205 while (e_flags)
3207 unsigned flag;
3209 /* Process flags one bit at a time. */
3210 flag = e_flags & - e_flags;
3211 e_flags &= ~ flag;
3213 switch (flag)
3215 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3216 out = stpcpy (out, ", sorted symbol tables");
3217 break;
3219 case EF_ARM_DYNSYMSUSESEGIDX:
3220 out = stpcpy (out, ", dynamic symbols use segment index");
3221 break;
3223 case EF_ARM_MAPSYMSFIRST:
3224 out = stpcpy (out, ", mapping symbols precede others");
3225 break;
3227 default:
3228 unknown = true;
3229 break;
3232 break;
3234 case EF_ARM_EABI_VER3:
3235 out = stpcpy (out, ", Version3 EABI");
3236 break;
3238 case EF_ARM_EABI_VER4:
3239 out = stpcpy (out, ", Version4 EABI");
3240 while (e_flags)
3242 unsigned flag;
3244 /* Process flags one bit at a time. */
3245 flag = e_flags & - e_flags;
3246 e_flags &= ~ flag;
3248 switch (flag)
3250 case EF_ARM_BE8:
3251 out = stpcpy (out, ", BE8");
3252 break;
3254 case EF_ARM_LE8:
3255 out = stpcpy (out, ", LE8");
3256 break;
3258 default:
3259 unknown = true;
3260 break;
3263 break;
3265 case EF_ARM_EABI_VER5:
3266 out = stpcpy (out, ", Version5 EABI");
3267 while (e_flags)
3269 unsigned flag;
3271 /* Process flags one bit at a time. */
3272 flag = e_flags & - e_flags;
3273 e_flags &= ~ flag;
3275 switch (flag)
3277 case EF_ARM_BE8:
3278 out = stpcpy (out, ", BE8");
3279 break;
3281 case EF_ARM_LE8:
3282 out = stpcpy (out, ", LE8");
3283 break;
3285 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3286 out = stpcpy (out, ", soft-float ABI");
3287 break;
3289 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3290 out = stpcpy (out, ", hard-float ABI");
3291 break;
3293 default:
3294 unknown = true;
3295 break;
3298 break;
3300 case EF_ARM_EABI_UNKNOWN:
3301 out = stpcpy (out, ", GNU EABI");
3302 while (e_flags)
3304 unsigned flag;
3306 /* Process flags one bit at a time. */
3307 flag = e_flags & - e_flags;
3308 e_flags &= ~ flag;
3310 switch (flag)
3312 case EF_ARM_INTERWORK:
3313 out = stpcpy (out, ", interworking enabled");
3314 break;
3316 case EF_ARM_APCS_26:
3317 out = stpcpy (out, ", uses APCS/26");
3318 break;
3320 case EF_ARM_APCS_FLOAT:
3321 out = stpcpy (out, ", uses APCS/float");
3322 break;
3324 case EF_ARM_PIC:
3325 out = stpcpy (out, ", position independent");
3326 break;
3328 case EF_ARM_ALIGN8:
3329 out = stpcpy (out, ", 8 bit structure alignment");
3330 break;
3332 case EF_ARM_NEW_ABI:
3333 out = stpcpy (out, ", uses new ABI");
3334 break;
3336 case EF_ARM_OLD_ABI:
3337 out = stpcpy (out, ", uses old ABI");
3338 break;
3340 case EF_ARM_SOFT_FLOAT:
3341 out = stpcpy (out, ", software FP");
3342 break;
3344 case EF_ARM_VFP_FLOAT:
3345 out = stpcpy (out, ", VFP");
3346 break;
3348 case EF_ARM_MAVERICK_FLOAT:
3349 out = stpcpy (out, ", Maverick FP");
3350 break;
3352 default:
3353 unknown = true;
3354 break;
3359 if (unknown)
3360 out = stpcpy (out,_(", <unknown>"));
3361 return out;
3364 static char *
3365 decode_AVR_machine_flags (char *out, unsigned e_flags)
3367 switch (e_flags & EF_AVR_MACH)
3369 case E_AVR_MACH_AVR1:
3370 out = stpcpy (out, ", avr:1");
3371 break;
3372 case E_AVR_MACH_AVR2:
3373 out = stpcpy (out, ", avr:2");
3374 break;
3375 case E_AVR_MACH_AVR25:
3376 out = stpcpy (out, ", avr:25");
3377 break;
3378 case E_AVR_MACH_AVR3:
3379 out = stpcpy (out, ", avr:3");
3380 break;
3381 case E_AVR_MACH_AVR31:
3382 out = stpcpy (out, ", avr:31");
3383 break;
3384 case E_AVR_MACH_AVR35:
3385 out = stpcpy (out, ", avr:35");
3386 break;
3387 case E_AVR_MACH_AVR4:
3388 out = stpcpy (out, ", avr:4");
3389 break;
3390 case E_AVR_MACH_AVR5:
3391 out = stpcpy (out, ", avr:5");
3392 break;
3393 case E_AVR_MACH_AVR51:
3394 out = stpcpy (out, ", avr:51");
3395 break;
3396 case E_AVR_MACH_AVR6:
3397 out = stpcpy (out, ", avr:6");
3398 break;
3399 case E_AVR_MACH_AVRTINY:
3400 out = stpcpy (out, ", avr:100");
3401 break;
3402 case E_AVR_MACH_XMEGA1:
3403 out = stpcpy (out, ", avr:101");
3404 break;
3405 case E_AVR_MACH_XMEGA2:
3406 out = stpcpy (out, ", avr:102");
3407 break;
3408 case E_AVR_MACH_XMEGA3:
3409 out = stpcpy (out, ", avr:103");
3410 break;
3411 case E_AVR_MACH_XMEGA4:
3412 out = stpcpy (out, ", avr:104");
3413 break;
3414 case E_AVR_MACH_XMEGA5:
3415 out = stpcpy (out, ", avr:105");
3416 break;
3417 case E_AVR_MACH_XMEGA6:
3418 out = stpcpy (out, ", avr:106");
3419 break;
3420 case E_AVR_MACH_XMEGA7:
3421 out = stpcpy (out, ", avr:107");
3422 break;
3423 default:
3424 out = stpcpy (out, ", avr:<unknown>");
3425 break;
3428 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3429 out = stpcpy (out, ", link-relax");
3430 return out;
3433 static char *
3434 decode_BLACKFIN_machine_flags (char *out, unsigned e_flags)
3436 if (e_flags & EF_BFIN_PIC)
3437 out = stpcpy (out, ", PIC");
3439 if (e_flags & EF_BFIN_FDPIC)
3440 out = stpcpy (out, ", FDPIC");
3442 if (e_flags & EF_BFIN_CODE_IN_L1)
3443 out = stpcpy (out, ", code in L1");
3445 if (e_flags & EF_BFIN_DATA_IN_L1)
3446 out = stpcpy (out, ", data in L1");
3447 return out;
3450 static char *
3451 decode_FRV_machine_flags (char *out, unsigned e_flags)
3453 switch (e_flags & EF_FRV_CPU_MASK)
3455 case EF_FRV_CPU_GENERIC:
3456 break;
3458 default:
3459 out = stpcpy (out, ", fr???");
3460 break;
3462 case EF_FRV_CPU_FR300:
3463 out = stpcpy (out, ", fr300");
3464 break;
3466 case EF_FRV_CPU_FR400:
3467 out = stpcpy (out, ", fr400");
3468 break;
3469 case EF_FRV_CPU_FR405:
3470 out = stpcpy (out, ", fr405");
3471 break;
3473 case EF_FRV_CPU_FR450:
3474 out = stpcpy (out, ", fr450");
3475 break;
3477 case EF_FRV_CPU_FR500:
3478 out = stpcpy (out, ", fr500");
3479 break;
3480 case EF_FRV_CPU_FR550:
3481 out = stpcpy (out, ", fr550");
3482 break;
3484 case EF_FRV_CPU_SIMPLE:
3485 out = stpcpy (out, ", simple");
3486 break;
3487 case EF_FRV_CPU_TOMCAT:
3488 out = stpcpy (out, ", tomcat");
3489 break;
3491 return out;
3494 static char *
3495 decode_IA64_machine_flags (char *out, unsigned e_flags, Filedata *filedata)
3497 if ((e_flags & EF_IA_64_ABI64))
3498 out = stpcpy (out, ", 64-bit");
3499 else
3500 out = stpcpy (out, ", 32-bit");
3501 if ((e_flags & EF_IA_64_REDUCEDFP))
3502 out = stpcpy (out, ", reduced fp model");
3503 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3504 out = stpcpy (out, ", no function descriptors, constant gp");
3505 else if ((e_flags & EF_IA_64_CONS_GP))
3506 out = stpcpy (out, ", constant gp");
3507 if ((e_flags & EF_IA_64_ABSOLUTE))
3508 out = stpcpy (out, ", absolute");
3509 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3511 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3512 out = stpcpy (out, ", vms_linkages");
3513 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3515 case EF_IA_64_VMS_COMCOD_SUCCESS:
3516 break;
3517 case EF_IA_64_VMS_COMCOD_WARNING:
3518 out = stpcpy (out, ", warning");
3519 break;
3520 case EF_IA_64_VMS_COMCOD_ERROR:
3521 out = stpcpy (out, ", error");
3522 break;
3523 case EF_IA_64_VMS_COMCOD_ABORT:
3524 out = stpcpy (out, ", abort");
3525 break;
3526 default:
3527 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3528 e_flags & EF_IA_64_VMS_COMCOD);
3529 out = stpcpy (out, ", <unknown>");
3532 return out;
3535 static char *
3536 decode_LOONGARCH_machine_flags (char *out, unsigned int e_flags)
3538 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
3539 out = stpcpy (out, ", SOFT-FLOAT");
3540 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
3541 out = stpcpy (out, ", SINGLE-FLOAT");
3542 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
3543 out = stpcpy (out, ", DOUBLE-FLOAT");
3545 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
3546 out = stpcpy (out, ", OBJ-v0");
3547 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
3548 out = stpcpy (out, ", OBJ-v1");
3549 return out;
3552 static char *
3553 decode_M68K_machine_flags (char *out, unsigned int e_flags)
3555 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3556 out = stpcpy (out, ", m68000");
3557 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3558 out = stpcpy (out, ", cpu32");
3559 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3560 out = stpcpy (out, ", fido_a");
3561 else
3563 char const *isa = _("unknown");
3564 char const *mac = _("unknown mac");
3565 char const *additional = NULL;
3567 switch (e_flags & EF_M68K_CF_ISA_MASK)
3569 case EF_M68K_CF_ISA_A_NODIV:
3570 isa = "A";
3571 additional = ", nodiv";
3572 break;
3573 case EF_M68K_CF_ISA_A:
3574 isa = "A";
3575 break;
3576 case EF_M68K_CF_ISA_A_PLUS:
3577 isa = "A+";
3578 break;
3579 case EF_M68K_CF_ISA_B_NOUSP:
3580 isa = "B";
3581 additional = ", nousp";
3582 break;
3583 case EF_M68K_CF_ISA_B:
3584 isa = "B";
3585 break;
3586 case EF_M68K_CF_ISA_C:
3587 isa = "C";
3588 break;
3589 case EF_M68K_CF_ISA_C_NODIV:
3590 isa = "C";
3591 additional = ", nodiv";
3592 break;
3594 out = stpcpy (out, ", cf, isa ");
3595 out = stpcpy (out, isa);
3596 if (additional)
3597 out = stpcpy (out, additional);
3598 if (e_flags & EF_M68K_CF_FLOAT)
3599 out = stpcpy (out, ", float");
3600 switch (e_flags & EF_M68K_CF_MAC_MASK)
3602 case 0:
3603 mac = NULL;
3604 break;
3605 case EF_M68K_CF_MAC:
3606 mac = "mac";
3607 break;
3608 case EF_M68K_CF_EMAC:
3609 mac = "emac";
3610 break;
3611 case EF_M68K_CF_EMAC_B:
3612 mac = "emac_b";
3613 break;
3615 if (mac)
3617 out = stpcpy (out, ", ");
3618 out = stpcpy (out, mac);
3621 return out;
3624 static char *
3625 decode_MeP_machine_flags (char *out, unsigned int e_flags)
3627 switch (e_flags & EF_MEP_CPU_MASK)
3629 case EF_MEP_CPU_MEP:
3630 out = stpcpy (out, ", generic MeP");
3631 break;
3632 case EF_MEP_CPU_C2:
3633 out = stpcpy (out, ", MeP C2");
3634 break;
3635 case EF_MEP_CPU_C3:
3636 out = stpcpy (out, ", MeP C3");
3637 break;
3638 case EF_MEP_CPU_C4:
3639 out = stpcpy (out, ", MeP C4");
3640 break;
3641 case EF_MEP_CPU_C5:
3642 out = stpcpy (out, ", MeP C5");
3643 break;
3644 case EF_MEP_CPU_H1:
3645 out = stpcpy (out, ", MeP H1");
3646 break;
3647 default:
3648 out = stpcpy (out, _(", <unknown MeP cpu type>"));
3649 break;
3652 switch (e_flags & EF_MEP_COP_MASK)
3654 case EF_MEP_COP_NONE:
3655 break;
3656 case EF_MEP_COP_AVC:
3657 out = stpcpy (out, ", AVC coprocessor");
3658 break;
3659 case EF_MEP_COP_AVC2:
3660 out = stpcpy (out, ", AVC2 coprocessor");
3661 break;
3662 case EF_MEP_COP_FMAX:
3663 out = stpcpy (out, ", FMAX coprocessor");
3664 break;
3665 case EF_MEP_COP_IVC2:
3666 out = stpcpy (out, ", IVC2 coprocessor");
3667 break;
3668 default:
3669 out = stpcpy (out, _("<unknown MeP copro type>"));
3670 break;
3673 if (e_flags & EF_MEP_LIBRARY)
3674 out = stpcpy (out, ", Built for Library");
3676 if (e_flags & EF_MEP_INDEX_MASK)
3677 out += sprintf (out, ", Configuration Index: %#x",
3678 e_flags & EF_MEP_INDEX_MASK);
3680 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3681 out += sprintf (out, _(", unknown flags bits: %#x"),
3682 e_flags & ~ EF_MEP_ALL_FLAGS);
3683 return out;
3686 static char *
3687 decode_MIPS_machine_flags (char *out, unsigned int e_flags)
3689 if (e_flags & EF_MIPS_NOREORDER)
3690 out = stpcpy (out, ", noreorder");
3692 if (e_flags & EF_MIPS_PIC)
3693 out = stpcpy (out, ", pic");
3695 if (e_flags & EF_MIPS_CPIC)
3696 out = stpcpy (out, ", cpic");
3698 if (e_flags & EF_MIPS_UCODE)
3699 out = stpcpy (out, ", ugen_reserved");
3701 if (e_flags & EF_MIPS_ABI2)
3702 out = stpcpy (out, ", abi2");
3704 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3705 out = stpcpy (out, ", odk first");
3707 if (e_flags & EF_MIPS_32BITMODE)
3708 out = stpcpy (out, ", 32bitmode");
3710 if (e_flags & EF_MIPS_NAN2008)
3711 out = stpcpy (out, ", nan2008");
3713 if (e_flags & EF_MIPS_FP64)
3714 out = stpcpy (out, ", fp64");
3716 switch ((e_flags & EF_MIPS_MACH))
3718 case E_MIPS_MACH_3900:
3719 out = stpcpy (out, ", 3900");
3720 break;
3721 case E_MIPS_MACH_4010:
3722 out = stpcpy (out, ", 4010");
3723 break;
3724 case E_MIPS_MACH_4100:
3725 out = stpcpy (out, ", 4100");
3726 break;
3727 case E_MIPS_MACH_4111:
3728 out = stpcpy (out, ", 4111");
3729 break;
3730 case E_MIPS_MACH_4120:
3731 out = stpcpy (out, ", 4120");
3732 break;
3733 case E_MIPS_MACH_4650:
3734 out = stpcpy (out, ", 4650");
3735 break;
3736 case E_MIPS_MACH_5400:
3737 out = stpcpy (out, ", 5400");
3738 break;
3739 case E_MIPS_MACH_5500:
3740 out = stpcpy (out, ", 5500");
3741 break;
3742 case E_MIPS_MACH_5900:
3743 out = stpcpy (out, ", 5900");
3744 break;
3745 case E_MIPS_MACH_SB1:
3746 out = stpcpy (out, ", sb1");
3747 break;
3748 case E_MIPS_MACH_9000:
3749 out = stpcpy (out, ", 9000");
3750 break;
3751 case E_MIPS_MACH_LS2E:
3752 out = stpcpy (out, ", loongson-2e");
3753 break;
3754 case E_MIPS_MACH_LS2F:
3755 out = stpcpy (out, ", loongson-2f");
3756 break;
3757 case E_MIPS_MACH_GS464:
3758 out = stpcpy (out, ", gs464");
3759 break;
3760 case E_MIPS_MACH_GS464E:
3761 out = stpcpy (out, ", gs464e");
3762 break;
3763 case E_MIPS_MACH_GS264E:
3764 out = stpcpy (out, ", gs264e");
3765 break;
3766 case E_MIPS_MACH_OCTEON:
3767 out = stpcpy (out, ", octeon");
3768 break;
3769 case E_MIPS_MACH_OCTEON2:
3770 out = stpcpy (out, ", octeon2");
3771 break;
3772 case E_MIPS_MACH_OCTEON3:
3773 out = stpcpy (out, ", octeon3");
3774 break;
3775 case E_MIPS_MACH_XLR:
3776 out = stpcpy (out, ", xlr");
3777 break;
3778 case E_MIPS_MACH_IAMR2:
3779 out = stpcpy (out, ", interaptiv-mr2");
3780 break;
3781 case E_MIPS_MACH_ALLEGREX:
3782 out = stpcpy (out, ", allegrex");
3783 break;
3784 case 0:
3785 /* We simply ignore the field in this case to avoid confusion:
3786 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3787 extension. */
3788 break;
3789 default:
3790 out = stpcpy (out, _(", unknown CPU"));
3791 break;
3794 switch ((e_flags & EF_MIPS_ABI))
3796 case E_MIPS_ABI_O32:
3797 out = stpcpy (out, ", o32");
3798 break;
3799 case E_MIPS_ABI_O64:
3800 out = stpcpy (out, ", o64");
3801 break;
3802 case E_MIPS_ABI_EABI32:
3803 out = stpcpy (out, ", eabi32");
3804 break;
3805 case E_MIPS_ABI_EABI64:
3806 out = stpcpy (out, ", eabi64");
3807 break;
3808 case 0:
3809 /* We simply ignore the field in this case to avoid confusion:
3810 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3811 This means it is likely to be an o32 file, but not for
3812 sure. */
3813 break;
3814 default:
3815 out = stpcpy (out, _(", unknown ABI"));
3816 break;
3819 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3820 out = stpcpy (out, ", mdmx");
3822 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3823 out = stpcpy (out, ", mips16");
3825 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3826 out = stpcpy (out, ", micromips");
3828 switch ((e_flags & EF_MIPS_ARCH))
3830 case E_MIPS_ARCH_1:
3831 out = stpcpy (out, ", mips1");
3832 break;
3833 case E_MIPS_ARCH_2:
3834 out = stpcpy (out, ", mips2");
3835 break;
3836 case E_MIPS_ARCH_3:
3837 out = stpcpy (out, ", mips3");
3838 break;
3839 case E_MIPS_ARCH_4:
3840 out = stpcpy (out, ", mips4");
3841 break;
3842 case E_MIPS_ARCH_5:
3843 out = stpcpy (out, ", mips5");
3844 break;
3845 case E_MIPS_ARCH_32:
3846 out = stpcpy (out, ", mips32");
3847 break;
3848 case E_MIPS_ARCH_32R2:
3849 out = stpcpy (out, ", mips32r2");
3850 break;
3851 case E_MIPS_ARCH_32R6:
3852 out = stpcpy (out, ", mips32r6");
3853 break;
3854 case E_MIPS_ARCH_64:
3855 out = stpcpy (out, ", mips64");
3856 break;
3857 case E_MIPS_ARCH_64R2:
3858 out = stpcpy (out, ", mips64r2");
3859 break;
3860 case E_MIPS_ARCH_64R6:
3861 out = stpcpy (out, ", mips64r6");
3862 break;
3863 default:
3864 out = stpcpy (out, _(", unknown ISA"));
3865 break;
3867 return out;
3870 static char *
3871 decode_MSP430_machine_flags (char *out, unsigned e_flags)
3873 out = stpcpy (out, _(": architecture variant: "));
3874 switch (e_flags & EF_MSP430_MACH)
3876 case E_MSP430_MACH_MSP430x11:
3877 out = stpcpy (out, "MSP430x11");
3878 break;
3879 case E_MSP430_MACH_MSP430x11x1:
3880 out = stpcpy (out, "MSP430x11x1 ");
3881 break;
3882 case E_MSP430_MACH_MSP430x12:
3883 out = stpcpy (out, "MSP430x12");
3884 break;
3885 case E_MSP430_MACH_MSP430x13:
3886 out = stpcpy (out, "MSP430x13");
3887 break;
3888 case E_MSP430_MACH_MSP430x14:
3889 out = stpcpy (out, "MSP430x14");
3890 break;
3891 case E_MSP430_MACH_MSP430x15:
3892 out = stpcpy (out, "MSP430x15");
3893 break;
3894 case E_MSP430_MACH_MSP430x16:
3895 out = stpcpy (out, "MSP430x16");
3896 break;
3897 case E_MSP430_MACH_MSP430x31:
3898 out = stpcpy (out, "MSP430x31");
3899 break;
3900 case E_MSP430_MACH_MSP430x32:
3901 out = stpcpy (out, "MSP430x32");
3902 break;
3903 case E_MSP430_MACH_MSP430x33:
3904 out = stpcpy (out, "MSP430x33");
3905 break;
3906 case E_MSP430_MACH_MSP430x41:
3907 out = stpcpy (out, "MSP430x41");
3908 break;
3909 case E_MSP430_MACH_MSP430x42:
3910 out = stpcpy (out, "MSP430x42");
3911 break;
3912 case E_MSP430_MACH_MSP430x43:
3913 out = stpcpy (out, "MSP430x43");
3914 break;
3915 case E_MSP430_MACH_MSP430x44:
3916 out = stpcpy (out, "MSP430x44");
3917 break;
3918 case E_MSP430_MACH_MSP430X :
3919 out = stpcpy (out, "MSP430X");
3920 break;
3921 default:
3922 out = stpcpy (out, _(": unknown"));
3923 break;
3926 if (e_flags & ~ EF_MSP430_MACH)
3927 out = stpcpy (out, _(": unknown extra flag bits also present"));
3928 return out;
3931 static char *
3932 decode_NDS32_machine_flags (char *out, unsigned e_flags)
3934 unsigned abi;
3935 unsigned arch;
3936 unsigned config;
3937 unsigned version;
3938 bool has_fpu = false;
3940 static const char *ABI_STRINGS[] =
3942 "ABI v0", /* use r5 as return register; only used in N1213HC */
3943 "ABI v1", /* use r0 as return register */
3944 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3945 "ABI v2fp", /* for FPU */
3946 "AABI",
3947 "ABI2 FP+"
3949 static const char *VER_STRINGS[] =
3951 "Andes ELF V1.3 or older",
3952 "Andes ELF V1.3.1",
3953 "Andes ELF V1.4"
3955 static const char *ARCH_STRINGS[] =
3958 "Andes Star v1.0",
3959 "Andes Star v2.0",
3960 "Andes Star v3.0",
3961 "Andes Star v3.0m"
3964 abi = EF_NDS_ABI & e_flags;
3965 arch = EF_NDS_ARCH & e_flags;
3966 config = EF_NDS_INST & e_flags;
3967 version = EF_NDS32_ELF_VERSION & e_flags;
3969 switch (abi)
3971 case E_NDS_ABI_V0:
3972 case E_NDS_ABI_V1:
3973 case E_NDS_ABI_V2:
3974 case E_NDS_ABI_V2FP:
3975 case E_NDS_ABI_AABI:
3976 case E_NDS_ABI_V2FP_PLUS:
3977 /* In case there are holes in the array. */
3978 out += sprintf (out, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3979 break;
3981 default:
3982 out = stpcpy (out, ", <unrecognized ABI>");
3983 break;
3986 switch (version)
3988 case E_NDS32_ELF_VER_1_2:
3989 case E_NDS32_ELF_VER_1_3:
3990 case E_NDS32_ELF_VER_1_4:
3991 out += sprintf (out, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3992 break;
3994 default:
3995 out = stpcpy (out, ", <unrecognized ELF version number>");
3996 break;
3999 if (E_NDS_ABI_V0 == abi)
4001 /* OLD ABI; only used in N1213HC, has performance extension 1. */
4002 out = stpcpy (out, ", Andes Star v1.0, N1213HC, MAC, PERF1");
4003 if (arch == E_NDS_ARCH_STAR_V1_0)
4004 out = stpcpy (out, ", 16b"); /* has 16-bit instructions */
4005 return out;
4008 switch (arch)
4010 case E_NDS_ARCH_STAR_V1_0:
4011 case E_NDS_ARCH_STAR_V2_0:
4012 case E_NDS_ARCH_STAR_V3_0:
4013 case E_NDS_ARCH_STAR_V3_M:
4014 out += sprintf (out, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
4015 break;
4017 default:
4018 out = stpcpy (out, ", <unrecognized architecture>");
4019 /* ARCH version determines how the e_flags are interpreted.
4020 If it is unknown, we cannot proceed. */
4021 return out;
4024 /* Newer ABI; Now handle architecture specific flags. */
4025 if (arch == E_NDS_ARCH_STAR_V1_0)
4027 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4028 out = stpcpy (out, ", MFUSR_PC");
4030 if (!(config & E_NDS32_HAS_NO_MAC_INST))
4031 out = stpcpy (out, ", MAC");
4033 if (config & E_NDS32_HAS_DIV_INST)
4034 out = stpcpy (out, ", DIV");
4036 if (config & E_NDS32_HAS_16BIT_INST)
4037 out = stpcpy (out, ", 16b");
4039 else
4041 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4043 if (version <= E_NDS32_ELF_VER_1_3)
4044 out = stpcpy (out, ", [B8]");
4045 else
4046 out = stpcpy (out, ", EX9");
4049 if (config & E_NDS32_HAS_MAC_DX_INST)
4050 out = stpcpy (out, ", MAC_DX");
4052 if (config & E_NDS32_HAS_DIV_DX_INST)
4053 out = stpcpy (out, ", DIV_DX");
4055 if (config & E_NDS32_HAS_16BIT_INST)
4057 if (version <= E_NDS32_ELF_VER_1_3)
4058 out = stpcpy (out, ", 16b");
4059 else
4060 out = stpcpy (out, ", IFC");
4064 if (config & E_NDS32_HAS_EXT_INST)
4065 out = stpcpy (out, ", PERF1");
4067 if (config & E_NDS32_HAS_EXT2_INST)
4068 out = stpcpy (out, ", PERF2");
4070 if (config & E_NDS32_HAS_FPU_INST)
4072 has_fpu = true;
4073 out = stpcpy (out, ", FPU_SP");
4076 if (config & E_NDS32_HAS_FPU_DP_INST)
4078 has_fpu = true;
4079 out = stpcpy (out, ", FPU_DP");
4082 if (config & E_NDS32_HAS_FPU_MAC_INST)
4084 has_fpu = true;
4085 out = stpcpy (out, ", FPU_MAC");
4088 if (has_fpu)
4090 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
4092 case E_NDS32_FPU_REG_8SP_4DP:
4093 out = stpcpy (out, ", FPU_REG:8/4");
4094 break;
4095 case E_NDS32_FPU_REG_16SP_8DP:
4096 out = stpcpy (out, ", FPU_REG:16/8");
4097 break;
4098 case E_NDS32_FPU_REG_32SP_16DP:
4099 out = stpcpy (out, ", FPU_REG:32/16");
4100 break;
4101 case E_NDS32_FPU_REG_32SP_32DP:
4102 out = stpcpy (out, ", FPU_REG:32/32");
4103 break;
4107 if (config & E_NDS32_HAS_AUDIO_INST)
4108 out = stpcpy (out, ", AUDIO");
4110 if (config & E_NDS32_HAS_STRING_INST)
4111 out = stpcpy (out, ", STR");
4113 if (config & E_NDS32_HAS_REDUCED_REGS)
4114 out = stpcpy (out, ", 16REG");
4116 if (config & E_NDS32_HAS_VIDEO_INST)
4118 if (version <= E_NDS32_ELF_VER_1_3)
4119 out = stpcpy (out, ", VIDEO");
4120 else
4121 out = stpcpy (out, ", SATURATION");
4124 if (config & E_NDS32_HAS_ENCRIPT_INST)
4125 out = stpcpy (out, ", ENCRP");
4127 if (config & E_NDS32_HAS_L2C_INST)
4128 out = stpcpy (out, ", L2C");
4130 return out;
4133 static char *
4134 decode_PARISC_machine_flags (char *out, unsigned e_flags)
4136 switch (e_flags & EF_PARISC_ARCH)
4138 case EFA_PARISC_1_0:
4139 out = stpcpy (out, ", PA-RISC 1.0");
4140 break;
4141 case EFA_PARISC_1_1:
4142 out = stpcpy (out, ", PA-RISC 1.1");
4143 break;
4144 case EFA_PARISC_2_0:
4145 out = stpcpy (out, ", PA-RISC 2.0");
4146 break;
4147 default:
4148 break;
4150 if (e_flags & EF_PARISC_TRAPNIL)
4151 out = stpcpy (out, ", trapnil");
4152 if (e_flags & EF_PARISC_EXT)
4153 out = stpcpy (out, ", ext");
4154 if (e_flags & EF_PARISC_LSB)
4155 out = stpcpy (out, ", lsb");
4156 if (e_flags & EF_PARISC_WIDE)
4157 out = stpcpy (out, ", wide");
4158 if (e_flags & EF_PARISC_NO_KABP)
4159 out = stpcpy (out, ", no kabp");
4160 if (e_flags & EF_PARISC_LAZYSWAP)
4161 out = stpcpy (out, ", lazyswap");
4162 return out;
4165 static char *
4166 decode_RISCV_machine_flags (char *out, unsigned e_flags)
4168 if (e_flags & EF_RISCV_RVC)
4169 out = stpcpy (out, ", RVC");
4171 if (e_flags & EF_RISCV_RVE)
4172 out = stpcpy (out, ", RVE");
4174 if (e_flags & EF_RISCV_TSO)
4175 out = stpcpy (out, ", TSO");
4177 switch (e_flags & EF_RISCV_FLOAT_ABI)
4179 case EF_RISCV_FLOAT_ABI_SOFT:
4180 out = stpcpy (out, ", soft-float ABI");
4181 break;
4183 case EF_RISCV_FLOAT_ABI_SINGLE:
4184 out = stpcpy (out, ", single-float ABI");
4185 break;
4187 case EF_RISCV_FLOAT_ABI_DOUBLE:
4188 out = stpcpy (out, ", double-float ABI");
4189 break;
4191 case EF_RISCV_FLOAT_ABI_QUAD:
4192 out = stpcpy (out, ", quad-float ABI");
4193 break;
4195 return out;
4198 static char *
4199 decode_RL78_machine_flags (char *out, unsigned e_flags)
4201 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4203 case E_FLAG_RL78_ANY_CPU:
4204 break;
4205 case E_FLAG_RL78_G10:
4206 out = stpcpy (out, ", G10");
4207 break;
4208 case E_FLAG_RL78_G13:
4209 out = stpcpy (out, ", G13");
4210 break;
4211 case E_FLAG_RL78_G14:
4212 out = stpcpy (out, ", G14");
4213 break;
4215 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4216 out = stpcpy (out, ", 64-bit doubles");
4217 return out;
4220 static char *
4221 decode_RX_machine_flags (char *out, unsigned e_flags)
4223 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4224 out = stpcpy (out, ", 64-bit doubles");
4225 if (e_flags & E_FLAG_RX_DSP)
4226 out = stpcpy (out, ", dsp");
4227 if (e_flags & E_FLAG_RX_PID)
4228 out = stpcpy (out, ", pid");
4229 if (e_flags & E_FLAG_RX_ABI)
4230 out = stpcpy (out, ", RX ABI");
4231 if (e_flags & E_FLAG_RX_SINSNS_SET)
4232 out = stpcpy (out, (e_flags & E_FLAG_RX_SINSNS_YES
4233 ? ", uses String instructions"
4234 : ", bans String instructions"));
4235 if (e_flags & E_FLAG_RX_V2)
4236 out = stpcpy (out, ", V2");
4237 if (e_flags & E_FLAG_RX_V3)
4238 out = stpcpy (out, ", V3");
4239 return out;
4242 static char *
4243 decode_SH_machine_flags (char *out, unsigned e_flags)
4245 switch ((e_flags & EF_SH_MACH_MASK))
4247 case EF_SH1:
4248 out = stpcpy (out, ", sh1");
4249 break;
4250 case EF_SH2:
4251 out = stpcpy (out, ", sh2");
4252 break;
4253 case EF_SH3:
4254 out = stpcpy (out, ", sh3");
4255 break;
4256 case EF_SH_DSP:
4257 out = stpcpy (out, ", sh-dsp");
4258 break;
4259 case EF_SH3_DSP:
4260 out = stpcpy (out, ", sh3-dsp");
4261 break;
4262 case EF_SH4AL_DSP:
4263 out = stpcpy (out, ", sh4al-dsp");
4264 break;
4265 case EF_SH3E:
4266 out = stpcpy (out, ", sh3e");
4267 break;
4268 case EF_SH4:
4269 out = stpcpy (out, ", sh4");
4270 break;
4271 case EF_SH5:
4272 out = stpcpy (out, ", sh5");
4273 break;
4274 case EF_SH2E:
4275 out = stpcpy (out, ", sh2e");
4276 break;
4277 case EF_SH4A:
4278 out = stpcpy (out, ", sh4a");
4279 break;
4280 case EF_SH2A:
4281 out = stpcpy (out, ", sh2a");
4282 break;
4283 case EF_SH4_NOFPU:
4284 out = stpcpy (out, ", sh4-nofpu");
4285 break;
4286 case EF_SH4A_NOFPU:
4287 out = stpcpy (out, ", sh4a-nofpu");
4288 break;
4289 case EF_SH2A_NOFPU:
4290 out = stpcpy (out, ", sh2a-nofpu");
4291 break;
4292 case EF_SH3_NOMMU:
4293 out = stpcpy (out, ", sh3-nommu");
4294 break;
4295 case EF_SH4_NOMMU_NOFPU:
4296 out = stpcpy (out, ", sh4-nommu-nofpu");
4297 break;
4298 case EF_SH2A_SH4_NOFPU:
4299 out = stpcpy (out, ", sh2a-nofpu-or-sh4-nommu-nofpu");
4300 break;
4301 case EF_SH2A_SH3_NOFPU:
4302 out = stpcpy (out, ", sh2a-nofpu-or-sh3-nommu");
4303 break;
4304 case EF_SH2A_SH4:
4305 out = stpcpy (out, ", sh2a-or-sh4");
4306 break;
4307 case EF_SH2A_SH3E:
4308 out = stpcpy (out, ", sh2a-or-sh3e");
4309 break;
4310 default:
4311 out = stpcpy (out, _(", unknown ISA"));
4312 break;
4315 if (e_flags & EF_SH_PIC)
4316 out = stpcpy (out, ", pic");
4318 if (e_flags & EF_SH_FDPIC)
4319 out = stpcpy (out, ", fdpic");
4320 return out;
4323 static char *
4324 decode_SPARC_machine_flags (char *out, unsigned e_flags)
4326 if (e_flags & EF_SPARC_32PLUS)
4327 out = stpcpy (out, ", v8+");
4329 if (e_flags & EF_SPARC_SUN_US1)
4330 out = stpcpy (out, ", ultrasparcI");
4332 if (e_flags & EF_SPARC_SUN_US3)
4333 out = stpcpy (out, ", ultrasparcIII");
4335 if (e_flags & EF_SPARC_HAL_R1)
4336 out = stpcpy (out, ", halr1");
4338 if (e_flags & EF_SPARC_LEDATA)
4339 out = stpcpy (out, ", ledata");
4341 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4342 out = stpcpy (out, ", tso");
4344 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4345 out = stpcpy (out, ", pso");
4347 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4348 out = stpcpy (out, ", rmo");
4349 return out;
4352 static char *
4353 decode_V800_machine_flags (char *out, unsigned int e_flags)
4355 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
4356 out = stpcpy (out, ", RH850 ABI");
4358 if (e_flags & EF_V800_850E3)
4359 out = stpcpy (out, ", V3 architecture");
4361 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
4362 out = stpcpy (out, ", FPU not used");
4364 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
4365 out = stpcpy (out, ", regmode: COMMON");
4367 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
4368 out = stpcpy (out, ", r4 not used");
4370 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
4371 out = stpcpy (out, ", r30 not used");
4373 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
4374 out = stpcpy (out, ", r5 not used");
4376 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
4377 out = stpcpy (out, ", r2 not used");
4379 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
4381 switch (e_flags & - e_flags)
4383 case EF_RH850_FPU_DOUBLE:
4384 out = stpcpy (out, ", double precision FPU");
4385 break;
4386 case EF_RH850_FPU_SINGLE:
4387 out = stpcpy (out, ", single precision FPU");
4388 break;
4389 case EF_RH850_REGMODE22:
4390 out = stpcpy (out, ", regmode:22");
4391 break;
4392 case EF_RH850_REGMODE32:
4393 out = stpcpy (out, ", regmode:23");
4394 break;
4395 case EF_RH850_GP_FIX:
4396 out = stpcpy (out, ", r4 fixed");
4397 break;
4398 case EF_RH850_GP_NOFIX:
4399 out = stpcpy (out, ", r4 free");
4400 break;
4401 case EF_RH850_EP_FIX:
4402 out = stpcpy (out, ", r30 fixed");
4403 break;
4404 case EF_RH850_EP_NOFIX:
4405 out = stpcpy (out, ", r30 free");
4406 break;
4407 case EF_RH850_TP_FIX:
4408 out = stpcpy (out, ", r5 fixed");
4409 break;
4410 case EF_RH850_TP_NOFIX:
4411 out = stpcpy (out, ", r5 free");
4412 break;
4413 case EF_RH850_REG2_RESERVE:
4414 out = stpcpy (out, ", r2 fixed");
4415 break;
4416 case EF_RH850_REG2_NORESERVE:
4417 out = stpcpy (out, ", r2 free");
4418 break;
4419 default:
4420 break;
4423 return out;
4426 static char *
4427 decode_V850_machine_flags (char *out, unsigned int e_flags)
4429 switch (e_flags & EF_V850_ARCH)
4431 case E_V850E3V5_ARCH:
4432 out = stpcpy (out, ", v850e3v5");
4433 break;
4434 case E_V850E2V3_ARCH:
4435 out = stpcpy (out, ", v850e2v3");
4436 break;
4437 case E_V850E2_ARCH:
4438 out = stpcpy (out, ", v850e2");
4439 break;
4440 case E_V850E1_ARCH:
4441 out = stpcpy (out, ", v850e1");
4442 break;
4443 case E_V850E_ARCH:
4444 out = stpcpy (out, ", v850e");
4445 break;
4446 case E_V850_ARCH:
4447 out = stpcpy (out, ", v850");
4448 break;
4449 default:
4450 out = stpcpy (out, _(", unknown v850 architecture variant"));
4451 break;
4453 return out;
4456 static char *
4457 decode_Z80_machine_flags (char *out, unsigned int e_flags)
4459 switch (e_flags & EF_Z80_MACH_MSK)
4461 case EF_Z80_MACH_Z80:
4462 out = stpcpy (out, ", Z80");
4463 break;
4464 case EF_Z80_MACH_Z180:
4465 out = stpcpy (out, ", Z180");
4466 break;
4467 case EF_Z80_MACH_R800:
4468 out = stpcpy (out, ", R800");
4469 break;
4470 case EF_Z80_MACH_EZ80_Z80:
4471 out = stpcpy (out, ", EZ80");
4472 break;
4473 case EF_Z80_MACH_EZ80_ADL:
4474 out = stpcpy (out, ", EZ80, ADL");
4475 break;
4476 case EF_Z80_MACH_GBZ80:
4477 out = stpcpy (out, ", GBZ80");
4478 break;
4479 case EF_Z80_MACH_Z80N:
4480 out = stpcpy (out, ", Z80N");
4481 break;
4482 default:
4483 out = stpcpy (out, _(", unknown"));
4484 break;
4486 return out;
4489 static char *
4490 decode_AMDGPU_machine_flags (char *out, unsigned int e_flags, Filedata *filedata)
4492 unsigned char *e_ident = filedata->file_header.e_ident;
4493 unsigned char osabi = e_ident[EI_OSABI];
4494 unsigned char abiversion = e_ident[EI_ABIVERSION];
4495 unsigned int mach;
4497 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
4498 it has been deprecated for a while.
4500 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
4501 of writing, they use the same flags as HSA v3, so the code below uses that
4502 assumption. */
4503 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
4504 return out;
4506 mach = e_flags & EF_AMDGPU_MACH;
4507 switch (mach)
4509 #define AMDGPU_CASE(code, string) \
4510 case code: out = stpcpy (out, ", " string); break;
4511 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
4512 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
4513 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
4514 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
4515 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
4516 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
4517 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
4518 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
4519 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
4520 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
4521 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
4522 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
4523 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
4524 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
4525 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
4526 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
4527 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
4528 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
4529 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
4530 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
4531 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
4532 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
4533 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
4534 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
4535 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
4536 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
4537 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
4538 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
4539 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
4540 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
4541 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
4542 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
4543 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
4544 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
4545 default:
4546 out += sprintf (out, _(", <unknown AMDGPU GPU type: %#x>"), mach);
4547 break;
4548 #undef AMDGPU_CASE
4551 e_flags &= ~EF_AMDGPU_MACH;
4553 if ((osabi == ELFOSABI_AMDGPU_HSA
4554 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
4555 || osabi != ELFOSABI_AMDGPU_HSA)
4557 /* For HSA v3 and other OS ABIs. */
4558 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
4560 out = stpcpy (out, ", xnack on");
4561 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
4564 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
4566 out = stpcpy (out, ", sramecc on");
4567 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
4570 else
4572 /* For HSA v4+. */
4573 int xnack, sramecc;
4575 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
4576 switch (xnack)
4578 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
4579 break;
4581 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
4582 out = stpcpy (out, ", xnack any");
4583 break;
4585 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
4586 out = stpcpy (out, ", xnack off");
4587 break;
4589 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
4590 out = stpcpy (out, ", xnack on");
4591 break;
4593 default:
4594 out += sprintf (out, _(", <unknown xnack value: %#x>"), xnack);
4595 break;
4598 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
4600 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
4601 switch (sramecc)
4603 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
4604 break;
4606 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
4607 out = stpcpy (out, ", sramecc any");
4608 break;
4610 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
4611 out = stpcpy (out, ", sramecc off");
4612 break;
4614 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
4615 out = stpcpy (out, ", sramecc on");
4616 break;
4618 default:
4619 out += sprintf (out, _(", <unknown sramecc value: %#x>"), sramecc);
4620 break;
4623 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
4626 if (e_flags != 0)
4627 out += sprintf (out, _(", unknown flags bits: %#x"), e_flags);
4628 return out;
4631 static char *
4632 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
4634 static char buf[1024];
4635 char *out = buf;
4637 buf[0] = '\0';
4639 if (e_flags)
4641 switch (e_machine)
4643 default:
4644 break;
4646 case EM_ARC_COMPACT3:
4647 out = stpcpy (out, ", HS5x");
4648 break;
4650 case EM_ARC_COMPACT3_64:
4651 out = stpcpy (out, ", HS6x");
4652 break;
4654 case EM_ARC_COMPACT2:
4655 case EM_ARC_COMPACT:
4656 out = decode_ARC_machine_flags (out, e_flags, e_machine);
4657 break;
4659 case EM_ARM:
4660 out = decode_ARM_machine_flags (out, e_flags);
4661 break;
4663 case EM_AVR:
4664 out = decode_AVR_machine_flags (out, e_flags);
4665 break;
4667 case EM_BLACKFIN:
4668 out = decode_BLACKFIN_machine_flags (out, e_flags);
4669 break;
4671 case EM_CYGNUS_FRV:
4672 out = decode_FRV_machine_flags (out, e_flags);
4673 break;
4675 case EM_68K:
4676 out = decode_M68K_machine_flags (out, e_flags);
4677 break;
4679 case EM_AMDGPU:
4680 out = decode_AMDGPU_machine_flags (out, e_flags, filedata);
4681 break;
4683 case EM_CYGNUS_MEP:
4684 out = decode_MeP_machine_flags (out, e_flags);
4685 break;
4687 case EM_PPC:
4688 if (e_flags & EF_PPC_EMB)
4689 out = stpcpy (out, ", emb");
4691 if (e_flags & EF_PPC_RELOCATABLE)
4692 out = stpcpy (out, _(", relocatable"));
4694 if (e_flags & EF_PPC_RELOCATABLE_LIB)
4695 out = stpcpy (out, _(", relocatable-lib"));
4696 break;
4698 case EM_PPC64:
4699 if (e_flags & EF_PPC64_ABI)
4700 out += sprintf (out, ", abiv%d", e_flags & EF_PPC64_ABI);
4701 break;
4703 case EM_V800:
4704 out = decode_V800_machine_flags (out, e_flags);
4705 break;
4707 case EM_V850:
4708 case EM_CYGNUS_V850:
4709 out = decode_V850_machine_flags (out, e_flags);
4710 break;
4712 case EM_M32R:
4713 case EM_CYGNUS_M32R:
4714 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4715 out = stpcpy (out, ", m32r");
4716 break;
4718 case EM_MIPS:
4719 case EM_MIPS_RS3_LE:
4720 out = decode_MIPS_machine_flags (out, e_flags);
4721 break;
4723 case EM_NDS32:
4724 out = decode_NDS32_machine_flags (out, e_flags);
4725 break;
4727 case EM_NFP:
4728 switch (EF_NFP_MACH (e_flags))
4730 case E_NFP_MACH_3200:
4731 out = stpcpy (out, ", NFP-32xx");
4732 break;
4733 case E_NFP_MACH_6000:
4734 out = stpcpy (out, ", NFP-6xxx");
4735 break;
4737 break;
4739 case EM_RISCV:
4740 out = decode_RISCV_machine_flags (out, e_flags);
4741 break;
4743 case EM_SH:
4744 out = decode_SH_machine_flags (out, e_flags);
4745 break;
4747 case EM_OR1K:
4748 if (e_flags & EF_OR1K_NODELAY)
4749 out = stpcpy (out, ", no delay");
4750 break;
4752 case EM_BPF:
4753 out += sprintf (out, ", CPU Version: %u", e_flags & EF_BPF_CPUVER);
4754 break;
4756 case EM_SPARCV9:
4757 out = decode_SPARC_machine_flags (out, e_flags);
4758 break;
4760 case EM_PARISC:
4761 out = decode_PARISC_machine_flags (out, e_flags);
4762 break;
4764 case EM_PJ:
4765 case EM_PJ_OLD:
4766 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4767 out = stpcpy (out, ", new calling convention");
4769 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4770 out = stpcpy (out, ", gnu calling convention");
4771 break;
4773 case EM_IA_64:
4774 out = decode_IA64_machine_flags (out, e_flags, filedata);
4775 break;
4777 case EM_VAX:
4778 if ((e_flags & EF_VAX_NONPIC))
4779 out = stpcpy (out, ", non-PIC");
4780 if ((e_flags & EF_VAX_DFLOAT))
4781 out = stpcpy (out, ", D-Float");
4782 if ((e_flags & EF_VAX_GFLOAT))
4783 out = stpcpy (out, ", G-Float");
4784 break;
4786 case EM_VISIUM:
4787 if (e_flags & EF_VISIUM_ARCH_MCM)
4788 out = stpcpy (out, ", mcm");
4789 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4790 out = stpcpy (out, ", mcm24");
4791 if (e_flags & EF_VISIUM_ARCH_GR6)
4792 out = stpcpy (out, ", gr6");
4793 break;
4795 case EM_RL78:
4796 out = decode_RL78_machine_flags (out, e_flags);
4797 break;
4799 case EM_RX:
4800 out = decode_RX_machine_flags (out, e_flags);
4801 break;
4803 case EM_S390:
4804 if (e_flags & EF_S390_HIGH_GPRS)
4805 out = stpcpy (out, ", highgprs");
4806 break;
4808 case EM_TI_C6000:
4809 if ((e_flags & EF_C6000_REL))
4810 out = stpcpy (out, ", relocatable module");
4811 break;
4813 case EM_KVX:
4814 if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_1)
4815 strcat (buf, ", Kalray VLIW kv3-1");
4816 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_2)
4817 strcat (buf, ", Kalray VLIW kv3-2");
4818 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV4_1)
4819 strcat (buf, ", Kalray VLIW kv4-1");
4820 else
4821 strcat (buf, ", unknown KVX MPPA");
4822 break;
4824 case EM_MSP430:
4825 out = decode_MSP430_machine_flags (out, e_flags);
4826 break;
4828 case EM_Z80:
4829 out = decode_Z80_machine_flags (out, e_flags);
4830 break;
4832 case EM_LOONGARCH:
4833 out = decode_LOONGARCH_machine_flags (out, e_flags);
4834 break;
4838 return buf;
4841 static const char *
4842 get_osabi_name (Filedata * filedata, unsigned int osabi)
4844 static char buff[32];
4846 switch (osabi)
4848 case ELFOSABI_NONE: return "UNIX - System V";
4849 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4850 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4851 case ELFOSABI_GNU: return "UNIX - GNU";
4852 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4853 case ELFOSABI_AIX: return "UNIX - AIX";
4854 case ELFOSABI_IRIX: return "UNIX - IRIX";
4855 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4856 case ELFOSABI_TRU64: return "UNIX - TRU64";
4857 case ELFOSABI_MODESTO: return "Novell - Modesto";
4858 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4859 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4860 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4861 case ELFOSABI_AROS: return "AROS";
4862 case ELFOSABI_FENIXOS: return "FenixOS";
4863 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4864 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4865 default:
4866 if (osabi >= 64)
4867 switch (filedata->file_header.e_machine)
4869 case EM_AMDGPU:
4870 switch (osabi)
4872 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
4873 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
4874 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4875 default:
4876 break;
4878 break;
4880 case EM_ARM:
4881 switch (osabi)
4883 case ELFOSABI_ARM: return "ARM";
4884 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4885 default:
4886 break;
4888 break;
4890 case EM_MSP430:
4891 case EM_MSP430_OLD:
4892 case EM_VISIUM:
4893 switch (osabi)
4895 case ELFOSABI_STANDALONE: return _("Standalone App");
4896 default:
4897 break;
4899 break;
4901 case EM_TI_C6000:
4902 switch (osabi)
4904 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4905 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4906 default:
4907 break;
4909 break;
4911 default:
4912 break;
4914 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4915 return buff;
4919 static const char *
4920 get_aarch64_segment_type (unsigned long type)
4922 switch (type)
4924 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4925 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
4926 default: return NULL;
4930 static const char *
4931 get_arm_segment_type (unsigned long type)
4933 switch (type)
4935 case PT_ARM_EXIDX: return "EXIDX";
4936 default: return NULL;
4940 static const char *
4941 get_s390_segment_type (unsigned long type)
4943 switch (type)
4945 case PT_S390_PGSTE: return "S390_PGSTE";
4946 default: return NULL;
4950 static const char *
4951 get_mips_segment_type (unsigned long type)
4953 switch (type)
4955 case PT_MIPS_REGINFO: return "REGINFO";
4956 case PT_MIPS_RTPROC: return "RTPROC";
4957 case PT_MIPS_OPTIONS: return "OPTIONS";
4958 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4959 default: return NULL;
4963 static const char *
4964 get_parisc_segment_type (unsigned long type)
4966 switch (type)
4968 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4969 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4970 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4971 default: return NULL;
4975 static const char *
4976 get_ia64_segment_type (unsigned long type)
4978 switch (type)
4980 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4981 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4982 default: return NULL;
4986 static const char *
4987 get_tic6x_segment_type (unsigned long type)
4989 switch (type)
4991 case PT_C6000_PHATTR: return "C6000_PHATTR";
4992 default: return NULL;
4996 static const char *
4997 get_riscv_segment_type (unsigned long type)
4999 switch (type)
5001 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5002 default: return NULL;
5006 static const char *
5007 get_hpux_segment_type (unsigned long type, unsigned e_machine)
5009 if (e_machine == EM_PARISC)
5010 switch (type)
5012 case PT_HP_TLS: return "HP_TLS";
5013 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
5014 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
5015 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
5016 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
5017 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
5018 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
5019 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
5020 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
5021 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
5022 case PT_HP_PARALLEL: return "HP_PARALLEL";
5023 case PT_HP_FASTBIND: return "HP_FASTBIND";
5024 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
5025 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
5026 case PT_HP_STACK: return "HP_STACK";
5027 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
5028 default: return NULL;
5031 if (e_machine == EM_IA_64)
5032 switch (type)
5034 case PT_HP_TLS: return "HP_TLS";
5035 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
5036 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
5037 case PT_IA_64_HP_STACK: return "HP_STACK";
5038 default: return NULL;
5041 return NULL;
5044 static const char *
5045 get_solaris_segment_type (unsigned long type)
5047 switch (type)
5049 case 0x6464e550: return "PT_SUNW_UNWIND";
5050 case 0x6474e550: return "PT_SUNW_EH_FRAME";
5051 case 0x6ffffff7: return "PT_LOSUNW";
5052 case 0x6ffffffa: return "PT_SUNWBSS";
5053 case 0x6ffffffb: return "PT_SUNWSTACK";
5054 case 0x6ffffffc: return "PT_SUNWDTRACE";
5055 case 0x6ffffffd: return "PT_SUNWCAP";
5056 case 0x6fffffff: return "PT_HISUNW";
5057 default: return NULL;
5061 static const char *
5062 get_segment_type (Filedata * filedata, unsigned long p_type)
5064 static char buff[32];
5066 switch (p_type)
5068 case PT_NULL: return "NULL";
5069 case PT_LOAD: return "LOAD";
5070 case PT_DYNAMIC: return "DYNAMIC";
5071 case PT_INTERP: return "INTERP";
5072 case PT_NOTE: return "NOTE";
5073 case PT_SHLIB: return "SHLIB";
5074 case PT_PHDR: return "PHDR";
5075 case PT_TLS: return "TLS";
5076 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
5077 case PT_GNU_STACK: return "GNU_STACK";
5078 case PT_GNU_RELRO: return "GNU_RELRO";
5079 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
5080 case PT_GNU_SFRAME: return "GNU_SFRAME";
5082 case PT_OPENBSD_MUTABLE: return "OPENBSD_MUTABLE";
5083 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
5084 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
5085 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
5087 default:
5088 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
5090 const char * result;
5092 switch (filedata->file_header.e_machine)
5094 case EM_AARCH64:
5095 result = get_aarch64_segment_type (p_type);
5096 break;
5097 case EM_ARM:
5098 result = get_arm_segment_type (p_type);
5099 break;
5100 case EM_MIPS:
5101 case EM_MIPS_RS3_LE:
5102 result = get_mips_segment_type (p_type);
5103 break;
5104 case EM_PARISC:
5105 result = get_parisc_segment_type (p_type);
5106 break;
5107 case EM_IA_64:
5108 result = get_ia64_segment_type (p_type);
5109 break;
5110 case EM_TI_C6000:
5111 result = get_tic6x_segment_type (p_type);
5112 break;
5113 case EM_S390:
5114 case EM_S390_OLD:
5115 result = get_s390_segment_type (p_type);
5116 break;
5117 case EM_RISCV:
5118 result = get_riscv_segment_type (p_type);
5119 break;
5120 default:
5121 result = NULL;
5122 break;
5125 if (result != NULL)
5126 return result;
5128 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
5130 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
5132 const char * result = NULL;
5134 switch (filedata->file_header.e_ident[EI_OSABI])
5136 case ELFOSABI_GNU:
5137 case ELFOSABI_FREEBSD:
5138 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
5140 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
5141 result = buff;
5143 break;
5144 case ELFOSABI_HPUX:
5145 result = get_hpux_segment_type (p_type,
5146 filedata->file_header.e_machine);
5147 break;
5148 case ELFOSABI_SOLARIS:
5149 result = get_solaris_segment_type (p_type);
5150 break;
5151 default:
5152 break;
5154 if (result != NULL)
5155 return result;
5157 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
5159 else
5160 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
5162 return buff;
5166 static const char *
5167 get_arc_section_type_name (unsigned int sh_type)
5169 switch (sh_type)
5171 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
5172 default:
5173 break;
5175 return NULL;
5178 static const char *
5179 get_mips_section_type_name (unsigned int sh_type)
5181 switch (sh_type)
5183 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
5184 case SHT_MIPS_MSYM: return "MIPS_MSYM";
5185 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
5186 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
5187 case SHT_MIPS_UCODE: return "MIPS_UCODE";
5188 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
5189 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
5190 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
5191 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
5192 case SHT_MIPS_RELD: return "MIPS_RELD";
5193 case SHT_MIPS_IFACE: return "MIPS_IFACE";
5194 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
5195 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
5196 case SHT_MIPS_SHDR: return "MIPS_SHDR";
5197 case SHT_MIPS_FDESC: return "MIPS_FDESC";
5198 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
5199 case SHT_MIPS_DENSE: return "MIPS_DENSE";
5200 case SHT_MIPS_PDESC: return "MIPS_PDESC";
5201 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
5202 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
5203 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
5204 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
5205 case SHT_MIPS_LINE: return "MIPS_LINE";
5206 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
5207 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
5208 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
5209 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
5210 case SHT_MIPS_DWARF: return "MIPS_DWARF";
5211 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
5212 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
5213 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
5214 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
5215 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
5216 case SHT_MIPS_XLATE: return "MIPS_XLATE";
5217 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
5218 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
5219 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
5220 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
5221 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
5222 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
5223 case SHT_MIPS_XHASH: return "MIPS_XHASH";
5224 default:
5225 break;
5227 return NULL;
5230 static const char *
5231 get_parisc_section_type_name (unsigned int sh_type)
5233 switch (sh_type)
5235 case SHT_PARISC_EXT: return "PARISC_EXT";
5236 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
5237 case SHT_PARISC_DOC: return "PARISC_DOC";
5238 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
5239 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
5240 case SHT_PARISC_STUBS: return "PARISC_STUBS";
5241 case SHT_PARISC_DLKM: return "PARISC_DLKM";
5242 default: return NULL;
5246 static const char *
5247 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
5249 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
5250 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
5251 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
5253 switch (sh_type)
5255 case SHT_IA_64_EXT: return "IA_64_EXT";
5256 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
5257 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
5258 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
5259 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
5260 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
5261 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
5262 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
5263 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
5264 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
5265 default:
5266 break;
5268 return NULL;
5271 static const char *
5272 get_x86_64_section_type_name (unsigned int sh_type)
5274 switch (sh_type)
5276 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
5277 default: return NULL;
5281 static const char *
5282 get_aarch64_section_type_name (unsigned int sh_type)
5284 switch (sh_type)
5286 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
5287 default: return NULL;
5291 static const char *
5292 get_arm_section_type_name (unsigned int sh_type)
5294 switch (sh_type)
5296 case SHT_ARM_EXIDX: return "ARM_EXIDX";
5297 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
5298 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
5299 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
5300 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
5301 default: return NULL;
5305 static const char *
5306 get_tic6x_section_type_name (unsigned int sh_type)
5308 switch (sh_type)
5310 case SHT_C6000_UNWIND: return "C6000_UNWIND";
5311 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
5312 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
5313 case SHT_TI_ICODE: return "TI_ICODE";
5314 case SHT_TI_XREF: return "TI_XREF";
5315 case SHT_TI_HANDLER: return "TI_HANDLER";
5316 case SHT_TI_INITINFO: return "TI_INITINFO";
5317 case SHT_TI_PHATTRS: return "TI_PHATTRS";
5318 default: return NULL;
5322 static const char *
5323 get_msp430_section_type_name (unsigned int sh_type)
5325 switch (sh_type)
5327 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
5328 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
5329 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
5330 default: return NULL;
5334 static const char *
5335 get_nfp_section_type_name (unsigned int sh_type)
5337 switch (sh_type)
5339 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
5340 case SHT_NFP_INITREG: return "NFP_INITREG";
5341 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
5342 default: return NULL;
5346 static const char *
5347 get_v850_section_type_name (unsigned int sh_type)
5349 switch (sh_type)
5351 case SHT_V850_SCOMMON: return "V850 Small Common";
5352 case SHT_V850_TCOMMON: return "V850 Tiny Common";
5353 case SHT_V850_ZCOMMON: return "V850 Zero Common";
5354 case SHT_RENESAS_IOP: return "RENESAS IOP";
5355 case SHT_RENESAS_INFO: return "RENESAS INFO";
5356 default: return NULL;
5360 static const char *
5361 get_riscv_section_type_name (unsigned int sh_type)
5363 switch (sh_type)
5365 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5366 default: return NULL;
5370 static const char *
5371 get_csky_section_type_name (unsigned int sh_type)
5373 switch (sh_type)
5375 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
5376 default: return NULL;
5380 static const char *
5381 get_section_type_name (Filedata * filedata, unsigned int sh_type)
5383 static char buff[32];
5384 const char * result;
5386 switch (sh_type)
5388 case SHT_NULL: return "NULL";
5389 case SHT_PROGBITS: return "PROGBITS";
5390 case SHT_SYMTAB: return "SYMTAB";
5391 case SHT_STRTAB: return "STRTAB";
5392 case SHT_RELA: return "RELA";
5393 case SHT_RELR: return "RELR";
5394 case SHT_HASH: return "HASH";
5395 case SHT_DYNAMIC: return "DYNAMIC";
5396 case SHT_NOTE: return "NOTE";
5397 case SHT_NOBITS: return "NOBITS";
5398 case SHT_REL: return "REL";
5399 case SHT_SHLIB: return "SHLIB";
5400 case SHT_DYNSYM: return "DYNSYM";
5401 case SHT_INIT_ARRAY: return "INIT_ARRAY";
5402 case SHT_FINI_ARRAY: return "FINI_ARRAY";
5403 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
5404 case SHT_GNU_HASH: return "GNU_HASH";
5405 case SHT_GROUP: return "GROUP";
5406 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
5407 case SHT_GNU_verdef: return "VERDEF";
5408 case SHT_GNU_verneed: return "VERNEED";
5409 case SHT_GNU_versym: return "VERSYM";
5410 case 0x6ffffff0: return "VERSYM";
5411 case 0x6ffffffc: return "VERDEF";
5412 case 0x7ffffffd: return "AUXILIARY";
5413 case 0x7fffffff: return "FILTER";
5414 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
5416 default:
5417 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
5419 switch (filedata->file_header.e_machine)
5421 case EM_ARC:
5422 case EM_ARC_COMPACT:
5423 case EM_ARC_COMPACT2:
5424 case EM_ARC_COMPACT3:
5425 case EM_ARC_COMPACT3_64:
5426 result = get_arc_section_type_name (sh_type);
5427 break;
5428 case EM_MIPS:
5429 case EM_MIPS_RS3_LE:
5430 result = get_mips_section_type_name (sh_type);
5431 break;
5432 case EM_PARISC:
5433 result = get_parisc_section_type_name (sh_type);
5434 break;
5435 case EM_IA_64:
5436 result = get_ia64_section_type_name (filedata, sh_type);
5437 break;
5438 case EM_X86_64:
5439 case EM_L1OM:
5440 case EM_K1OM:
5441 result = get_x86_64_section_type_name (sh_type);
5442 break;
5443 case EM_AARCH64:
5444 result = get_aarch64_section_type_name (sh_type);
5445 break;
5446 case EM_ARM:
5447 result = get_arm_section_type_name (sh_type);
5448 break;
5449 case EM_TI_C6000:
5450 result = get_tic6x_section_type_name (sh_type);
5451 break;
5452 case EM_MSP430:
5453 result = get_msp430_section_type_name (sh_type);
5454 break;
5455 case EM_NFP:
5456 result = get_nfp_section_type_name (sh_type);
5457 break;
5458 case EM_V800:
5459 case EM_V850:
5460 case EM_CYGNUS_V850:
5461 result = get_v850_section_type_name (sh_type);
5462 break;
5463 case EM_RISCV:
5464 result = get_riscv_section_type_name (sh_type);
5465 break;
5466 case EM_CSKY:
5467 result = get_csky_section_type_name (sh_type);
5468 break;
5469 default:
5470 result = NULL;
5471 break;
5474 if (result != NULL)
5475 return result;
5477 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
5479 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5481 switch (filedata->file_header.e_machine)
5483 case EM_IA_64:
5484 result = get_ia64_section_type_name (filedata, sh_type);
5485 break;
5486 default:
5487 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5488 result = get_solaris_section_type (sh_type);
5489 else
5491 switch (sh_type)
5493 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5494 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5495 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5496 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5497 default:
5498 result = NULL;
5499 break;
5502 break;
5505 if (result != NULL)
5506 return result;
5508 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5510 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5512 switch (filedata->file_header.e_machine)
5514 case EM_V800:
5515 case EM_V850:
5516 case EM_CYGNUS_V850:
5517 result = get_v850_section_type_name (sh_type);
5518 break;
5519 default:
5520 result = NULL;
5521 break;
5524 if (result != NULL)
5525 return result;
5527 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5529 else
5530 /* This message is probably going to be displayed in a 15
5531 character wide field, so put the hex value first. */
5532 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5534 return buff;
5538 enum long_option_values
5540 OPTION_DEBUG_DUMP = 512,
5541 OPTION_DYN_SYMS,
5542 OPTION_LTO_SYMS,
5543 OPTION_DWARF_DEPTH,
5544 OPTION_DWARF_START,
5545 OPTION_DWARF_CHECK,
5546 OPTION_CTF_DUMP,
5547 OPTION_CTF_PARENT,
5548 OPTION_CTF_SYMBOLS,
5549 OPTION_CTF_STRINGS,
5550 OPTION_SFRAME_DUMP,
5551 OPTION_WITH_SYMBOL_VERSIONS,
5552 OPTION_RECURSE_LIMIT,
5553 OPTION_NO_RECURSE_LIMIT,
5554 OPTION_NO_DEMANGLING,
5555 OPTION_NO_EXTRA_SYM_INFO,
5556 OPTION_SYM_BASE
5559 static struct option options[] =
5561 /* Note - This table is alpha-sorted on the 'val'
5562 field in order to make adding new options easier. */
5563 {"arch-specific", no_argument, 0, 'A'},
5564 {"all", no_argument, 0, 'a'},
5565 {"demangle", optional_argument, 0, 'C'},
5566 {"archive-index", no_argument, 0, 'c'},
5567 {"use-dynamic", no_argument, 0, 'D'},
5568 {"dynamic", no_argument, 0, 'd'},
5569 {"headers", no_argument, 0, 'e'},
5570 {"section-groups", no_argument, 0, 'g'},
5571 {"help", no_argument, 0, 'H'},
5572 {"file-header", no_argument, 0, 'h'},
5573 {"histogram", no_argument, 0, 'I'},
5574 {"lint", no_argument, 0, 'L'},
5575 {"enable-checks", no_argument, 0, 'L'},
5576 {"program-headers", no_argument, 0, 'l'},
5577 {"segments", no_argument, 0, 'l'},
5578 {"full-section-name",no_argument, 0, 'N'},
5579 {"notes", no_argument, 0, 'n'},
5580 {"process-links", no_argument, 0, 'P'},
5581 {"string-dump", required_argument, 0, 'p'},
5582 {"relocated-dump", required_argument, 0, 'R'},
5583 {"relocs", no_argument, 0, 'r'},
5584 {"section-headers", no_argument, 0, 'S'},
5585 {"sections", no_argument, 0, 'S'},
5586 {"symbols", no_argument, 0, 's'},
5587 {"syms", no_argument, 0, 's'},
5588 {"silent-truncation",no_argument, 0, 'T'},
5589 {"section-details", no_argument, 0, 't'},
5590 {"unicode", required_argument, NULL, 'U'},
5591 {"unwind", no_argument, 0, 'u'},
5592 {"version-info", no_argument, 0, 'V'},
5593 {"version", no_argument, 0, 'v'},
5594 {"wide", no_argument, 0, 'W'},
5595 {"extra-sym-info", no_argument, 0, 'X'},
5596 {"hex-dump", required_argument, 0, 'x'},
5597 {"decompress", no_argument, 0, 'z'},
5599 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
5600 {"no-extra-sym-info",no_argument, 0, OPTION_NO_EXTRA_SYM_INFO},
5601 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5602 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5603 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5604 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5605 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5606 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5607 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5608 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5609 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5610 #ifdef ENABLE_LIBCTF
5611 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5612 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5613 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5614 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5615 #endif
5616 {"sframe", optional_argument, 0, OPTION_SFRAME_DUMP},
5617 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5619 {0, no_argument, 0, 0}
5622 static void
5623 usage (FILE * stream)
5625 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5626 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5627 fprintf (stream, _(" Options are:\n"));
5628 fprintf (stream, _("\
5629 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5630 fprintf (stream, _("\
5631 -h --file-header Display the ELF file header\n"));
5632 fprintf (stream, _("\
5633 -l --program-headers Display the program headers\n"));
5634 fprintf (stream, _("\
5635 --segments An alias for --program-headers\n"));
5636 fprintf (stream, _("\
5637 -S --section-headers Display the sections' header\n"));
5638 fprintf (stream, _("\
5639 --sections An alias for --section-headers\n"));
5640 fprintf (stream, _("\
5641 -g --section-groups Display the section groups\n"));
5642 fprintf (stream, _("\
5643 -t --section-details Display the section details\n"));
5644 fprintf (stream, _("\
5645 -e --headers Equivalent to: -h -l -S\n"));
5646 fprintf (stream, _("\
5647 -s --syms Display the symbol table\n"));
5648 fprintf (stream, _("\
5649 --symbols An alias for --syms\n"));
5650 fprintf (stream, _("\
5651 --dyn-syms Display the dynamic symbol table\n"));
5652 fprintf (stream, _("\
5653 --lto-syms Display LTO symbol tables\n"));
5654 fprintf (stream, _("\
5655 --sym-base=[0|8|10|16] \n\
5656 Force base for symbol sizes. The options are \n\
5657 mixed (the default), octal, decimal, hexadecimal.\n"));
5658 fprintf (stream, _("\
5659 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5660 display_demangler_styles (stream, _("\
5661 STYLE can be "));
5662 fprintf (stream, _("\
5663 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5664 fprintf (stream, _("\
5665 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5666 fprintf (stream, _("\
5667 --no-recurse-limit Disable a demangling recursion limit\n"));
5668 fprintf (stream, _("\
5669 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5670 Display unicode characters as determined by the current locale\n\
5671 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5672 escape sequences, or treat them as invalid and display as\n\
5673 \"{hex sequences}\"\n"));
5674 fprintf (stream, _("\
5675 -X --extra-sym-info Display extra information when showing symbols\n"));
5676 fprintf (stream, _("\
5677 --no-extra-sym-info Do not display extra information when showing symbols (default)\n"));
5678 fprintf (stream, _("\
5679 -n --notes Display the contents of note sections (if present)\n"));
5680 fprintf (stream, _("\
5681 -r --relocs Display the relocations (if present)\n"));
5682 fprintf (stream, _("\
5683 -u --unwind Display the unwind info (if present)\n"));
5684 fprintf (stream, _("\
5685 -d --dynamic Display the dynamic section (if present)\n"));
5686 fprintf (stream, _("\
5687 -V --version-info Display the version sections (if present)\n"));
5688 fprintf (stream, _("\
5689 -A --arch-specific Display architecture specific information (if any)\n"));
5690 fprintf (stream, _("\
5691 -c --archive-index Display the symbol/file index in an archive\n"));
5692 fprintf (stream, _("\
5693 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5694 fprintf (stream, _("\
5695 -L --lint|--enable-checks\n\
5696 Display warning messages for possible problems\n"));
5697 fprintf (stream, _("\
5698 -x --hex-dump=<number|name>\n\
5699 Dump the contents of section <number|name> as bytes\n"));
5700 fprintf (stream, _("\
5701 -p --string-dump=<number|name>\n\
5702 Dump the contents of section <number|name> as strings\n"));
5703 fprintf (stream, _("\
5704 -R --relocated-dump=<number|name>\n\
5705 Dump the relocated contents of section <number|name>\n"));
5706 fprintf (stream, _("\
5707 -z --decompress Decompress section before dumping it\n"));
5708 fprintf (stream, _("\
5709 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5710 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5711 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5712 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5713 U/=trace_info]\n\
5714 Display the contents of DWARF debug sections\n"));
5715 fprintf (stream, _("\
5716 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5717 debuginfo files\n"));
5718 fprintf (stream, _("\
5719 -P --process-links Display the contents of non-debug sections in separate\n\
5720 debuginfo files. (Implies -wK)\n"));
5721 #if DEFAULT_FOR_FOLLOW_LINKS
5722 fprintf (stream, _("\
5723 -wK --debug-dump=follow-links\n\
5724 Follow links to separate debug info files (default)\n"));
5725 fprintf (stream, _("\
5726 -wN --debug-dump=no-follow-links\n\
5727 Do not follow links to separate debug info files\n"));
5728 #else
5729 fprintf (stream, _("\
5730 -wK --debug-dump=follow-links\n\
5731 Follow links to separate debug info files\n"));
5732 fprintf (stream, _("\
5733 -wN --debug-dump=no-follow-links\n\
5734 Do not follow links to separate debug info files\n\
5735 (default)\n"));
5736 #endif
5737 #if HAVE_LIBDEBUGINFOD
5738 fprintf (stream, _("\
5739 -wD --debug-dump=use-debuginfod\n\
5740 When following links, also query debuginfod servers (default)\n"));
5741 fprintf (stream, _("\
5742 -wE --debug-dump=do-not-use-debuginfod\n\
5743 When following links, do not query debuginfod servers\n"));
5744 #endif
5745 fprintf (stream, _("\
5746 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5747 fprintf (stream, _("\
5748 --dwarf-start=N Display DIEs starting at offset N\n"));
5749 #ifdef ENABLE_LIBCTF
5750 fprintf (stream, _("\
5751 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5752 fprintf (stream, _("\
5753 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
5754 fprintf (stream, _("\
5755 --ctf-symbols=<number|name>\n\
5756 Use section <number|name> as the CTF external symtab\n"));
5757 fprintf (stream, _("\
5758 --ctf-strings=<number|name>\n\
5759 Use section <number|name> as the CTF external strtab\n"));
5760 #endif
5761 fprintf (stream, _("\
5762 --sframe[=NAME] Display SFrame info from section NAME, (default '.sframe')\n"));
5764 #ifdef SUPPORT_DISASSEMBLY
5765 fprintf (stream, _("\
5766 -i --instruction-dump=<number|name>\n\
5767 Disassemble the contents of section <number|name>\n"));
5768 #endif
5769 fprintf (stream, _("\
5770 -I --histogram Display histogram of bucket list lengths\n"));
5771 fprintf (stream, _("\
5772 -W --wide Allow output width to exceed 80 characters\n"));
5773 fprintf (stream, _("\
5774 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5775 fprintf (stream, _("\
5776 @<file> Read options from <file>\n"));
5777 fprintf (stream, _("\
5778 -H --help Display this information\n"));
5779 fprintf (stream, _("\
5780 -v --version Display the version number of readelf\n"));
5782 if (REPORT_BUGS_TO[0] && stream == stdout)
5783 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5785 exit (stream == stdout ? 0 : 1);
5788 /* Record the fact that the user wants the contents of section number
5789 SECTION to be displayed using the method(s) encoded as flags bits
5790 in TYPE. Note, TYPE can be zero if we are creating the array for
5791 the first time. */
5793 static void
5794 request_dump_bynumber (struct dump_data *dumpdata,
5795 unsigned int section, dump_type type)
5797 if (section >= dumpdata->num_dump_sects)
5799 dump_type * new_dump_sects;
5801 new_dump_sects = (dump_type *) calloc (section + 1,
5802 sizeof (* new_dump_sects));
5804 if (new_dump_sects == NULL)
5805 error (_("Out of memory allocating dump request table.\n"));
5806 else
5808 if (dumpdata->dump_sects)
5810 /* Copy current flag settings. */
5811 memcpy (new_dump_sects, dumpdata->dump_sects,
5812 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5814 free (dumpdata->dump_sects);
5817 dumpdata->dump_sects = new_dump_sects;
5818 dumpdata->num_dump_sects = section + 1;
5822 if (dumpdata->dump_sects)
5823 dumpdata->dump_sects[section] |= type;
5826 /* Request a dump by section name. */
5828 static void
5829 request_dump_byname (const char * section, dump_type type)
5831 struct dump_list_entry * new_request;
5833 new_request = (struct dump_list_entry *)
5834 malloc (sizeof (struct dump_list_entry));
5835 if (!new_request)
5836 error (_("Out of memory allocating dump request table.\n"));
5838 new_request->name = strdup (section);
5839 if (!new_request->name)
5840 error (_("Out of memory allocating dump request table.\n"));
5842 new_request->type = type;
5844 new_request->next = dump_sects_byname;
5845 dump_sects_byname = new_request;
5848 static inline void
5849 request_dump (struct dump_data *dumpdata, dump_type type)
5851 int section;
5852 char * cp;
5854 do_dump = true;
5855 section = strtoul (optarg, & cp, 0);
5857 if (! *cp && section >= 0)
5858 request_dump_bynumber (dumpdata, section, type);
5859 else
5860 request_dump_byname (optarg, type);
5863 static void
5864 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5866 int c;
5868 if (argc < 2)
5869 usage (stderr);
5871 while ((c = getopt_long
5872 (argc, argv, "ACDHILNPR:STU:VWXacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5874 switch (c)
5876 case 0:
5877 /* Long options. */
5878 break;
5879 case 'H':
5880 usage (stdout);
5881 break;
5883 case 'a':
5884 do_syms = true;
5885 do_reloc = true;
5886 do_unwind = true;
5887 do_dynamic = true;
5888 do_header = true;
5889 do_sections = true;
5890 do_section_groups = true;
5891 do_segments = true;
5892 do_version = true;
5893 do_histogram = true;
5894 do_arch = true;
5895 do_notes = true;
5896 break;
5898 case 'g':
5899 do_section_groups = true;
5900 break;
5901 case 't':
5902 case 'N':
5903 do_sections = true;
5904 do_section_details = true;
5905 break;
5906 case 'e':
5907 do_header = true;
5908 do_sections = true;
5909 do_segments = true;
5910 break;
5911 case 'A':
5912 do_arch = true;
5913 break;
5914 case 'D':
5915 do_using_dynamic = true;
5916 break;
5917 case 'r':
5918 do_reloc = true;
5919 break;
5920 case 'u':
5921 do_unwind = true;
5922 break;
5923 case 'h':
5924 do_header = true;
5925 break;
5926 case 'l':
5927 do_segments = true;
5928 break;
5929 case 's':
5930 do_syms = true;
5931 break;
5932 case 'S':
5933 do_sections = true;
5934 break;
5935 case 'd':
5936 do_dynamic = true;
5937 break;
5938 case 'I':
5939 do_histogram = true;
5940 break;
5941 case 'n':
5942 do_notes = true;
5943 break;
5944 case 'c':
5945 do_archive_index = true;
5946 break;
5947 case 'L':
5948 do_checks = true;
5949 break;
5950 case 'P':
5951 process_links = true;
5952 do_follow_links = true;
5953 dump_any_debugging = true;
5954 break;
5955 case 'x':
5956 request_dump (dumpdata, HEX_DUMP);
5957 break;
5958 case 'p':
5959 request_dump (dumpdata, STRING_DUMP);
5960 break;
5961 case 'R':
5962 request_dump (dumpdata, RELOC_DUMP);
5963 break;
5964 case 'z':
5965 decompress_dumps = true;
5966 break;
5967 case 'w':
5968 if (optarg == NULL)
5970 do_debugging = true;
5971 do_dump = true;
5972 dump_any_debugging = true;
5973 dwarf_select_sections_all ();
5975 else
5977 do_debugging = false;
5978 if (dwarf_select_sections_by_letters (optarg))
5980 do_dump = true;
5981 dump_any_debugging = true;
5984 break;
5985 case OPTION_DEBUG_DUMP:
5986 if (optarg == NULL)
5988 do_dump = true;
5989 do_debugging = true;
5990 dump_any_debugging = true;
5991 dwarf_select_sections_all ();
5993 else
5995 do_debugging = false;
5996 if (dwarf_select_sections_by_names (optarg))
5998 do_dump = true;
5999 dump_any_debugging = true;
6002 break;
6003 case OPTION_DWARF_DEPTH:
6005 char *cp;
6007 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
6009 break;
6010 case OPTION_DWARF_START:
6012 char *cp;
6014 dwarf_start_die = strtoul (optarg, & cp, 0);
6016 break;
6017 case OPTION_DWARF_CHECK:
6018 dwarf_check = true;
6019 break;
6020 case OPTION_CTF_DUMP:
6021 do_ctf = true;
6022 request_dump (dumpdata, CTF_DUMP);
6023 break;
6024 case OPTION_CTF_SYMBOLS:
6025 free (dump_ctf_symtab_name);
6026 dump_ctf_symtab_name = strdup (optarg);
6027 break;
6028 case OPTION_CTF_STRINGS:
6029 free (dump_ctf_strtab_name);
6030 dump_ctf_strtab_name = strdup (optarg);
6031 break;
6032 case OPTION_CTF_PARENT:
6033 free (dump_ctf_parent_name);
6034 dump_ctf_parent_name = strdup (optarg);
6035 break;
6036 case OPTION_SFRAME_DUMP:
6037 do_sframe = true;
6038 /* Providing section name is optional. request_dump (), however,
6039 thrives on non NULL optarg. Handle it explicitly here. */
6040 if (optarg != NULL)
6041 request_dump (dumpdata, SFRAME_DUMP);
6042 else
6044 do_dump = true;
6045 const char *sframe_sec_name = strdup (".sframe");
6046 request_dump_byname (sframe_sec_name, SFRAME_DUMP);
6048 break;
6049 case OPTION_DYN_SYMS:
6050 do_dyn_syms = true;
6051 break;
6052 case OPTION_LTO_SYMS:
6053 do_lto_syms = true;
6054 break;
6055 case 'X':
6056 extra_sym_info = true;
6057 break;
6058 case OPTION_NO_EXTRA_SYM_INFO:
6059 extra_sym_info = false;
6060 break;
6062 #ifdef SUPPORT_DISASSEMBLY
6063 case 'i':
6064 request_dump (dumpdata, DISASS_DUMP);
6065 break;
6066 #endif
6067 case 'v':
6068 print_version (program_name);
6069 break;
6070 case 'V':
6071 do_version = true;
6072 break;
6073 case 'W':
6074 do_wide = true;
6075 break;
6076 case 'T':
6077 do_not_show_symbol_truncation = true;
6078 break;
6079 case 'C':
6080 do_demangle = true;
6081 if (optarg != NULL)
6083 enum demangling_styles style;
6085 style = cplus_demangle_name_to_style (optarg);
6086 if (style == unknown_demangling)
6087 error (_("unknown demangling style `%s'"), optarg);
6089 cplus_demangle_set_style (style);
6091 break;
6092 case OPTION_NO_DEMANGLING:
6093 do_demangle = false;
6094 break;
6095 case OPTION_RECURSE_LIMIT:
6096 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
6097 break;
6098 case OPTION_NO_RECURSE_LIMIT:
6099 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
6100 break;
6101 case OPTION_WITH_SYMBOL_VERSIONS:
6102 /* Ignored for backward compatibility. */
6103 break;
6105 case 'U':
6106 if (optarg == NULL)
6107 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
6108 else if (streq (optarg, "default") || streq (optarg, "d"))
6109 unicode_display = unicode_default;
6110 else if (streq (optarg, "locale") || streq (optarg, "l"))
6111 unicode_display = unicode_locale;
6112 else if (streq (optarg, "escape") || streq (optarg, "e"))
6113 unicode_display = unicode_escape;
6114 else if (streq (optarg, "invalid") || streq (optarg, "i"))
6115 unicode_display = unicode_invalid;
6116 else if (streq (optarg, "hex") || streq (optarg, "x"))
6117 unicode_display = unicode_hex;
6118 else if (streq (optarg, "highlight") || streq (optarg, "h"))
6119 unicode_display = unicode_highlight;
6120 else
6121 error (_("invalid argument to -U/--unicode: %s"), optarg);
6122 break;
6124 case OPTION_SYM_BASE:
6125 sym_base = 0;
6126 if (optarg != NULL)
6128 sym_base = strtoul (optarg, NULL, 0);
6129 switch (sym_base)
6131 case 0:
6132 case 8:
6133 case 10:
6134 case 16:
6135 break;
6137 default:
6138 sym_base = 0;
6139 break;
6142 break;
6144 default:
6145 /* xgettext:c-format */
6146 error (_("Invalid option '-%c'\n"), c);
6147 /* Fall through. */
6148 case '?':
6149 usage (stderr);
6153 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
6154 && !do_segments && !do_header && !do_dump && !do_version
6155 && !do_histogram && !do_debugging && !do_arch && !do_notes
6156 && !do_section_groups && !do_archive_index
6157 && !do_dyn_syms && !do_lto_syms)
6159 if (do_checks)
6161 check_all = true;
6162 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
6163 do_segments = do_header = do_dump = do_version = true;
6164 do_histogram = do_debugging = do_arch = do_notes = true;
6165 do_section_groups = do_archive_index = do_dyn_syms = true;
6166 do_lto_syms = true;
6168 else
6169 usage (stderr);
6173 static const char *
6174 get_elf_class (unsigned int elf_class)
6176 static char buff[32];
6178 switch (elf_class)
6180 case ELFCLASSNONE: return _("none");
6181 case ELFCLASS32: return "ELF32";
6182 case ELFCLASS64: return "ELF64";
6183 default:
6184 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
6185 return buff;
6189 static const char *
6190 get_data_encoding (unsigned int encoding)
6192 static char buff[32];
6194 switch (encoding)
6196 case ELFDATANONE: return _("none");
6197 case ELFDATA2LSB: return _("2's complement, little endian");
6198 case ELFDATA2MSB: return _("2's complement, big endian");
6199 default:
6200 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
6201 return buff;
6205 static bool
6206 check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
6208 if (header->e_ident[EI_MAG0] == ELFMAG0
6209 && header->e_ident[EI_MAG1] == ELFMAG1
6210 && header->e_ident[EI_MAG2] == ELFMAG2
6211 && header->e_ident[EI_MAG3] == ELFMAG3)
6212 return true;
6214 /* Some compilers produce object files that are not in the ELF file format.
6215 As an aid to users of readelf, try to identify these cases and suggest
6216 alternative tools.
6218 FIXME: It is not clear if all four bytes are used as constant magic
6219 valus by all compilers. It may be necessary to recode this function if
6220 different tools use different length sequences. */
6222 static struct
6224 unsigned char magic[4];
6225 const char * obj_message;
6226 const char * ar_message;
6228 known_magic[] =
6230 { { 'B', 'C', 0xc0, 0xde },
6231 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
6232 N_("This is a LLVM bitcode file - try extracting and then using llvm-bcanalyzer\n")
6234 { { 'g', 'o', ' ', 'o' },
6235 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
6236 NULL
6239 int i;
6241 for (i = ARRAY_SIZE (known_magic); i--;)
6243 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
6244 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
6245 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
6246 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
6248 /* Some compiler's analyzer tools do not handle archives,
6249 so we provide two different kinds of error message. */
6250 if (filedata->archive_file_size > 0
6251 && known_magic[i].ar_message != NULL)
6252 error ("%s", known_magic[i].ar_message);
6253 else
6254 error ("%s", known_magic[i].obj_message);
6255 return false;
6259 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
6260 return false;
6263 /* Decode the data held in 'filedata->file_header'. */
6265 static bool
6266 process_file_header (Filedata * filedata)
6268 Elf_Internal_Ehdr * header = & filedata->file_header;
6270 if (! check_magic_number (filedata, header))
6271 return false;
6273 if (! filedata->is_separate)
6274 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
6276 if (do_header)
6278 unsigned i;
6280 if (filedata->is_separate)
6281 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
6282 else
6283 printf (_("ELF Header:\n"));
6284 printf (_(" Magic: "));
6285 for (i = 0; i < EI_NIDENT; i++)
6286 printf ("%2.2x ", header->e_ident[i]);
6287 printf ("\n");
6288 printf (_(" Class: %s\n"),
6289 get_elf_class (header->e_ident[EI_CLASS]));
6290 printf (_(" Data: %s\n"),
6291 get_data_encoding (header->e_ident[EI_DATA]));
6292 printf (_(" Version: %d%s\n"),
6293 header->e_ident[EI_VERSION],
6294 (header->e_ident[EI_VERSION] == EV_CURRENT
6295 ? _(" (current)")
6296 : (header->e_ident[EI_VERSION] != EV_NONE
6297 ? _(" <unknown>")
6298 : "")));
6299 printf (_(" OS/ABI: %s\n"),
6300 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
6301 printf (_(" ABI Version: %d\n"),
6302 header->e_ident[EI_ABIVERSION]);
6303 printf (_(" Type: %s\n"),
6304 get_file_type (filedata));
6305 printf (_(" Machine: %s\n"),
6306 get_machine_name (header->e_machine));
6307 printf (_(" Version: 0x%lx\n"),
6308 header->e_version);
6310 printf (_(" Entry point address: "));
6311 print_vma (header->e_entry, PREFIX_HEX);
6312 printf (_("\n Start of program headers: "));
6313 print_vma (header->e_phoff, DEC);
6314 printf (_(" (bytes into file)\n Start of section headers: "));
6315 print_vma (header->e_shoff, DEC);
6316 printf (_(" (bytes into file)\n"));
6318 printf (_(" Flags: 0x%lx%s\n"),
6319 header->e_flags,
6320 get_machine_flags (filedata, header->e_flags, header->e_machine));
6321 printf (_(" Size of this header: %u (bytes)\n"),
6322 header->e_ehsize);
6323 printf (_(" Size of program headers: %u (bytes)\n"),
6324 header->e_phentsize);
6325 printf (_(" Number of program headers: %u"),
6326 header->e_phnum);
6327 if (filedata->section_headers != NULL
6328 && header->e_phnum == PN_XNUM
6329 && filedata->section_headers[0].sh_info != 0)
6330 printf (" (%u)", filedata->section_headers[0].sh_info);
6331 putc ('\n', stdout);
6332 printf (_(" Size of section headers: %u (bytes)\n"),
6333 header->e_shentsize);
6334 printf (_(" Number of section headers: %u"),
6335 header->e_shnum);
6336 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
6338 header->e_shnum = filedata->section_headers[0].sh_size;
6339 printf (" (%u)", header->e_shnum);
6341 putc ('\n', stdout);
6342 printf (_(" Section header string table index: %u"),
6343 header->e_shstrndx);
6344 if (filedata->section_headers != NULL
6345 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
6347 header->e_shstrndx = filedata->section_headers[0].sh_link;
6348 printf (" (%u)", header->e_shstrndx);
6350 if (header->e_shstrndx != SHN_UNDEF
6351 && header->e_shstrndx >= header->e_shnum)
6353 header->e_shstrndx = SHN_UNDEF;
6354 printf (_(" <corrupt: out of range>"));
6356 putc ('\n', stdout);
6359 if (filedata->section_headers != NULL)
6361 if (header->e_phnum == PN_XNUM
6362 && filedata->section_headers[0].sh_info != 0)
6364 /* Throw away any cached read of PN_XNUM headers. */
6365 free (filedata->program_headers);
6366 filedata->program_headers = NULL;
6367 header->e_phnum = filedata->section_headers[0].sh_info;
6369 if (header->e_shnum == SHN_UNDEF)
6370 header->e_shnum = filedata->section_headers[0].sh_size;
6371 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
6372 header->e_shstrndx = filedata->section_headers[0].sh_link;
6373 if (header->e_shstrndx >= header->e_shnum)
6374 header->e_shstrndx = SHN_UNDEF;
6377 return true;
6380 /* Read in the program headers from FILEDATA and store them in PHEADERS.
6381 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
6383 static bool
6384 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
6386 Elf32_External_Phdr * phdrs;
6387 Elf32_External_Phdr * external;
6388 Elf_Internal_Phdr * internal;
6389 unsigned int i;
6390 unsigned int size = filedata->file_header.e_phentsize;
6391 unsigned int num = filedata->file_header.e_phnum;
6393 /* PR binutils/17531: Cope with unexpected section header sizes. */
6394 if (size == 0 || num == 0)
6395 return false;
6396 if (size < sizeof * phdrs)
6398 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
6399 return false;
6401 if (size > sizeof * phdrs)
6402 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
6404 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
6405 size, num, _("program headers"));
6406 if (phdrs == NULL)
6407 return false;
6409 for (i = 0, internal = pheaders, external = phdrs;
6410 i < filedata->file_header.e_phnum;
6411 i++, internal++, external++)
6413 internal->p_type = BYTE_GET (external->p_type);
6414 internal->p_offset = BYTE_GET (external->p_offset);
6415 internal->p_vaddr = BYTE_GET (external->p_vaddr);
6416 internal->p_paddr = BYTE_GET (external->p_paddr);
6417 internal->p_filesz = BYTE_GET (external->p_filesz);
6418 internal->p_memsz = BYTE_GET (external->p_memsz);
6419 internal->p_flags = BYTE_GET (external->p_flags);
6420 internal->p_align = BYTE_GET (external->p_align);
6423 free (phdrs);
6424 return true;
6427 /* Read in the program headers from FILEDATA and store them in PHEADERS.
6428 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
6430 static bool
6431 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
6433 Elf64_External_Phdr * phdrs;
6434 Elf64_External_Phdr * external;
6435 Elf_Internal_Phdr * internal;
6436 unsigned int i;
6437 unsigned int size = filedata->file_header.e_phentsize;
6438 unsigned int num = filedata->file_header.e_phnum;
6440 /* PR binutils/17531: Cope with unexpected section header sizes. */
6441 if (size == 0 || num == 0)
6442 return false;
6443 if (size < sizeof * phdrs)
6445 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
6446 return false;
6448 if (size > sizeof * phdrs)
6449 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
6451 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
6452 size, num, _("program headers"));
6453 if (!phdrs)
6454 return false;
6456 for (i = 0, internal = pheaders, external = phdrs;
6457 i < filedata->file_header.e_phnum;
6458 i++, internal++, external++)
6460 internal->p_type = BYTE_GET (external->p_type);
6461 internal->p_flags = BYTE_GET (external->p_flags);
6462 internal->p_offset = BYTE_GET (external->p_offset);
6463 internal->p_vaddr = BYTE_GET (external->p_vaddr);
6464 internal->p_paddr = BYTE_GET (external->p_paddr);
6465 internal->p_filesz = BYTE_GET (external->p_filesz);
6466 internal->p_memsz = BYTE_GET (external->p_memsz);
6467 internal->p_align = BYTE_GET (external->p_align);
6470 free (phdrs);
6471 return true;
6474 /* Returns TRUE if the program headers were read into `program_headers'. */
6476 static bool
6477 get_program_headers (Filedata * filedata)
6479 Elf_Internal_Phdr * phdrs;
6481 /* Check cache of prior read. */
6482 if (filedata->program_headers != NULL)
6483 return true;
6485 /* Be kind to memory checkers by looking for
6486 e_phnum values which we know must be invalid. */
6487 if (filedata->file_header.e_phnum
6488 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
6489 >= filedata->file_size)
6491 error (_("Too many program headers - %#x - the file is not that big\n"),
6492 filedata->file_header.e_phnum);
6493 return false;
6496 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
6497 sizeof (Elf_Internal_Phdr));
6498 if (phdrs == NULL)
6500 error (_("Out of memory reading %u program headers\n"),
6501 filedata->file_header.e_phnum);
6502 return false;
6505 if (is_32bit_elf
6506 ? get_32bit_program_headers (filedata, phdrs)
6507 : get_64bit_program_headers (filedata, phdrs))
6509 filedata->program_headers = phdrs;
6510 return true;
6513 free (phdrs);
6514 return false;
6517 /* Print program header info and locate dynamic section. */
6519 static void
6520 process_program_headers (Filedata * filedata)
6522 Elf_Internal_Phdr * segment;
6523 unsigned int i;
6524 Elf_Internal_Phdr * previous_load = NULL;
6526 if (filedata->file_header.e_phnum == 0)
6528 /* PR binutils/12467. */
6529 if (filedata->file_header.e_phoff != 0)
6530 warn (_("possibly corrupt ELF header - it has a non-zero program"
6531 " header offset, but no program headers\n"));
6532 else if (do_segments)
6534 if (filedata->is_separate)
6535 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6536 filedata->file_name);
6537 else
6538 printf (_("\nThere are no program headers in this file.\n"));
6540 goto no_headers;
6543 if (do_segments && !do_header)
6545 if (filedata->is_separate)
6546 printf ("\nIn linked file '%s' the ELF file type is %s\n",
6547 filedata->file_name, get_file_type (filedata));
6548 else
6549 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6550 printf (_("Entry point 0x%" PRIx64 "\n"),
6551 filedata->file_header.e_entry);
6552 printf (ngettext ("There is %d program header,"
6553 " starting at offset %" PRIu64 "\n",
6554 "There are %d program headers,"
6555 " starting at offset %" PRIu64 "\n",
6556 filedata->file_header.e_phnum),
6557 filedata->file_header.e_phnum,
6558 filedata->file_header.e_phoff);
6561 if (! get_program_headers (filedata))
6562 goto no_headers;
6564 if (do_segments)
6566 if (filedata->file_header.e_phnum > 1)
6567 printf (_("\nProgram Headers:\n"));
6568 else
6569 printf (_("\nProgram Headers:\n"));
6571 if (is_32bit_elf)
6572 printf
6573 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6574 else if (do_wide)
6575 printf
6576 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6577 else
6579 printf
6580 (_(" Type Offset VirtAddr PhysAddr\n"));
6581 printf
6582 (_(" FileSiz MemSiz Flags Align\n"));
6586 uint64_t dynamic_addr = 0;
6587 uint64_t dynamic_size = 0;
6588 for (i = 0, segment = filedata->program_headers;
6589 i < filedata->file_header.e_phnum;
6590 i++, segment++)
6592 if (do_segments)
6594 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
6596 if (is_32bit_elf)
6598 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6599 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6600 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6601 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6602 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6603 printf ("%c%c%c ",
6604 (segment->p_flags & PF_R ? 'R' : ' '),
6605 (segment->p_flags & PF_W ? 'W' : ' '),
6606 (segment->p_flags & PF_X ? 'E' : ' '));
6607 printf ("%#lx", (unsigned long) segment->p_align);
6609 else if (do_wide)
6611 if ((unsigned long) segment->p_offset == segment->p_offset)
6612 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6613 else
6615 print_vma (segment->p_offset, FULL_HEX);
6616 putchar (' ');
6619 print_vma (segment->p_vaddr, FULL_HEX);
6620 putchar (' ');
6621 print_vma (segment->p_paddr, FULL_HEX);
6622 putchar (' ');
6624 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6625 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6626 else
6628 print_vma (segment->p_filesz, FULL_HEX);
6629 putchar (' ');
6632 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6633 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6634 else
6636 print_vma (segment->p_memsz, FULL_HEX);
6639 printf (" %c%c%c ",
6640 (segment->p_flags & PF_R ? 'R' : ' '),
6641 (segment->p_flags & PF_W ? 'W' : ' '),
6642 (segment->p_flags & PF_X ? 'E' : ' '));
6644 if ((unsigned long) segment->p_align == segment->p_align)
6645 printf ("%#lx", (unsigned long) segment->p_align);
6646 else
6648 print_vma (segment->p_align, PREFIX_HEX);
6651 else
6653 print_vma (segment->p_offset, FULL_HEX);
6654 putchar (' ');
6655 print_vma (segment->p_vaddr, FULL_HEX);
6656 putchar (' ');
6657 print_vma (segment->p_paddr, FULL_HEX);
6658 printf ("\n ");
6659 print_vma (segment->p_filesz, FULL_HEX);
6660 putchar (' ');
6661 print_vma (segment->p_memsz, FULL_HEX);
6662 printf (" %c%c%c ",
6663 (segment->p_flags & PF_R ? 'R' : ' '),
6664 (segment->p_flags & PF_W ? 'W' : ' '),
6665 (segment->p_flags & PF_X ? 'E' : ' '));
6666 print_vma (segment->p_align, PREFIX_HEX);
6669 putc ('\n', stdout);
6672 switch (segment->p_type)
6674 case PT_LOAD:
6675 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6676 required by the ELF standard, several programs, including the Linux
6677 kernel, make use of non-ordered segments. */
6678 if (previous_load
6679 && previous_load->p_vaddr > segment->p_vaddr)
6680 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6681 #endif
6682 if (segment->p_memsz < segment->p_filesz)
6683 error (_("the segment's file size is larger than its memory size\n"));
6684 previous_load = segment;
6685 break;
6687 case PT_PHDR:
6688 /* PR 20815 - Verify that the program header is loaded into memory. */
6689 if (i > 0 && previous_load != NULL)
6690 error (_("the PHDR segment must occur before any LOAD segment\n"));
6691 if (filedata->file_header.e_machine != EM_PARISC)
6693 unsigned int j;
6695 for (j = 1; j < filedata->file_header.e_phnum; j++)
6697 Elf_Internal_Phdr *load = filedata->program_headers + j;
6698 if (load->p_type == PT_LOAD
6699 && load->p_offset <= segment->p_offset
6700 && (load->p_offset + load->p_filesz
6701 >= segment->p_offset + segment->p_filesz)
6702 && load->p_vaddr <= segment->p_vaddr
6703 && (load->p_vaddr + load->p_filesz
6704 >= segment->p_vaddr + segment->p_filesz))
6705 break;
6707 if (j == filedata->file_header.e_phnum)
6708 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6710 break;
6712 case PT_DYNAMIC:
6713 if (dynamic_addr)
6714 error (_("more than one dynamic segment\n"));
6716 /* By default, assume that the .dynamic section is the first
6717 section in the DYNAMIC segment. */
6718 dynamic_addr = segment->p_offset;
6719 dynamic_size = segment->p_filesz;
6721 /* Try to locate the .dynamic section. If there is
6722 a section header table, we can easily locate it. */
6723 if (filedata->section_headers != NULL)
6725 Elf_Internal_Shdr * sec;
6727 sec = find_section (filedata, ".dynamic");
6728 if (sec == NULL || sec->sh_size == 0)
6730 /* A corresponding .dynamic section is expected, but on
6731 IA-64/OpenVMS it is OK for it to be missing. */
6732 if (!is_ia64_vms (filedata))
6733 error (_("no .dynamic section in the dynamic segment\n"));
6734 break;
6737 if (sec->sh_type == SHT_NOBITS)
6739 dynamic_addr = 0;
6740 dynamic_size = 0;
6741 break;
6744 dynamic_addr = sec->sh_offset;
6745 dynamic_size = sec->sh_size;
6747 /* The PT_DYNAMIC segment, which is used by the run-time
6748 loader, should exactly match the .dynamic section. */
6749 if (do_checks
6750 && (dynamic_addr != segment->p_offset
6751 || dynamic_size != segment->p_filesz))
6752 warn (_("\
6753 the .dynamic section is not the same as the dynamic segment\n"));
6756 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6757 segment. Check this after matching against the section headers
6758 so we don't warn on debuginfo file (which have NOBITS .dynamic
6759 sections). */
6760 if (dynamic_addr > filedata->file_size
6761 || (dynamic_size > filedata->file_size - dynamic_addr))
6763 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6764 dynamic_addr = 0;
6765 dynamic_size = 0;
6767 break;
6769 case PT_INTERP:
6770 if (segment->p_offset >= filedata->file_size
6771 || segment->p_filesz > filedata->file_size - segment->p_offset
6772 || segment->p_filesz - 1 >= (size_t) -2
6773 || fseek64 (filedata->handle,
6774 filedata->archive_file_offset + segment->p_offset,
6775 SEEK_SET))
6776 error (_("Unable to find program interpreter name\n"));
6777 else
6779 size_t len = segment->p_filesz;
6780 free (filedata->program_interpreter);
6781 filedata->program_interpreter = xmalloc (len + 1);
6782 len = fread (filedata->program_interpreter, 1, len,
6783 filedata->handle);
6784 filedata->program_interpreter[len] = 0;
6786 if (do_segments)
6787 printf (_(" [Requesting program interpreter: %s]\n"),
6788 filedata->program_interpreter);
6790 break;
6794 if (do_segments
6795 && filedata->section_headers != NULL
6796 && filedata->string_table != NULL)
6798 printf (_("\n Section to Segment mapping:\n"));
6799 printf (_(" Segment Sections...\n"));
6801 for (i = 0; i < filedata->file_header.e_phnum; i++)
6803 unsigned int j;
6804 Elf_Internal_Shdr * section;
6806 segment = filedata->program_headers + i;
6807 section = filedata->section_headers + 1;
6809 printf (" %2.2d ", i);
6811 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6813 if (!ELF_TBSS_SPECIAL (section, segment)
6814 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6815 printf ("%s ", printable_section_name (filedata, section));
6818 putc ('\n',stdout);
6822 filedata->dynamic_addr = dynamic_addr;
6823 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6824 return;
6826 no_headers:
6827 filedata->dynamic_addr = 0;
6828 filedata->dynamic_size = 1;
6832 /* Find the file offset corresponding to VMA by using the program headers. */
6834 static int64_t
6835 offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
6837 Elf_Internal_Phdr * seg;
6839 if (! get_program_headers (filedata))
6841 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6842 return (long) vma;
6845 for (seg = filedata->program_headers;
6846 seg < filedata->program_headers + filedata->file_header.e_phnum;
6847 ++seg)
6849 if (seg->p_type != PT_LOAD)
6850 continue;
6852 if (vma >= (seg->p_vaddr & -seg->p_align)
6853 && vma + size <= seg->p_vaddr + seg->p_filesz)
6854 return vma - seg->p_vaddr + seg->p_offset;
6857 warn (_("Virtual address %#" PRIx64
6858 " not located in any PT_LOAD segment.\n"), vma);
6859 return vma;
6863 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6864 If PROBE is true, this is just a probe and we do not generate any error
6865 messages if the load fails. */
6867 static bool
6868 get_32bit_section_headers (Filedata * filedata, bool probe)
6870 Elf32_External_Shdr * shdrs;
6871 Elf_Internal_Shdr * internal;
6872 unsigned int i;
6873 unsigned int size = filedata->file_header.e_shentsize;
6874 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6876 /* PR binutils/17531: Cope with unexpected section header sizes. */
6877 if (size == 0 || num == 0)
6878 return false;
6880 /* The section header cannot be at the start of the file - that is
6881 where the ELF file header is located. A file with absolutely no
6882 sections in it will use a shoff of 0. */
6883 if (filedata->file_header.e_shoff == 0)
6884 return false;
6886 if (size < sizeof * shdrs)
6888 if (! probe)
6889 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6890 return false;
6892 if (!probe && size > sizeof * shdrs)
6893 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6895 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6896 size, num,
6897 probe ? NULL : _("section headers"));
6898 if (shdrs == NULL)
6899 return false;
6901 filedata->section_headers = (Elf_Internal_Shdr *)
6902 cmalloc (num, sizeof (Elf_Internal_Shdr));
6903 if (filedata->section_headers == NULL)
6905 if (!probe)
6906 error (_("Out of memory reading %u section headers\n"), num);
6907 free (shdrs);
6908 return false;
6911 for (i = 0, internal = filedata->section_headers;
6912 i < num;
6913 i++, internal++)
6915 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6916 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6917 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6918 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6919 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6920 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6921 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6922 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6923 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6924 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6925 if (!probe && internal->sh_link > num)
6926 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6927 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6928 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6931 free (shdrs);
6932 return true;
6935 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6937 static bool
6938 get_64bit_section_headers (Filedata * filedata, bool probe)
6940 Elf64_External_Shdr * shdrs;
6941 Elf_Internal_Shdr * internal;
6942 unsigned int i;
6943 unsigned int size = filedata->file_header.e_shentsize;
6944 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6946 /* PR binutils/17531: Cope with unexpected section header sizes. */
6947 if (size == 0 || num == 0)
6948 return false;
6950 /* The section header cannot be at the start of the file - that is
6951 where the ELF file header is located. A file with absolutely no
6952 sections in it will use a shoff of 0. */
6953 if (filedata->file_header.e_shoff == 0)
6954 return false;
6956 if (size < sizeof * shdrs)
6958 if (! probe)
6959 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6960 return false;
6963 if (! probe && size > sizeof * shdrs)
6964 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6966 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6967 filedata->file_header.e_shoff,
6968 size, num,
6969 probe ? NULL : _("section headers"));
6970 if (shdrs == NULL)
6971 return false;
6973 filedata->section_headers = (Elf_Internal_Shdr *)
6974 cmalloc (num, sizeof (Elf_Internal_Shdr));
6975 if (filedata->section_headers == NULL)
6977 if (! probe)
6978 error (_("Out of memory reading %u section headers\n"), num);
6979 free (shdrs);
6980 return false;
6983 for (i = 0, internal = filedata->section_headers;
6984 i < num;
6985 i++, internal++)
6987 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6988 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6989 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6990 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6991 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6992 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6993 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6994 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6995 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6996 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6997 if (!probe && internal->sh_link > num)
6998 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6999 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
7000 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
7003 free (shdrs);
7004 return true;
7007 static bool
7008 get_section_headers (Filedata *filedata, bool probe)
7010 if (filedata->section_headers != NULL)
7011 return true;
7013 if (is_32bit_elf)
7014 return get_32bit_section_headers (filedata, probe);
7015 else
7016 return get_64bit_section_headers (filedata, probe);
7019 static Elf_Internal_Sym *
7020 get_32bit_elf_symbols (Filedata *filedata,
7021 Elf_Internal_Shdr *section,
7022 uint64_t *num_syms_return)
7024 uint64_t number = 0;
7025 Elf32_External_Sym * esyms = NULL;
7026 Elf_External_Sym_Shndx * shndx = NULL;
7027 Elf_Internal_Sym * isyms = NULL;
7028 Elf_Internal_Sym * psym;
7029 unsigned int j;
7030 elf_section_list * entry;
7032 if (section->sh_size == 0)
7034 if (num_syms_return != NULL)
7035 * num_syms_return = 0;
7036 return NULL;
7039 /* Run some sanity checks first. */
7040 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
7042 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
7043 printable_section_name (filedata, section),
7044 section->sh_entsize);
7045 goto exit_point;
7048 if (section->sh_size > filedata->file_size)
7050 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
7051 printable_section_name (filedata, section),
7052 section->sh_size);
7053 goto exit_point;
7056 number = section->sh_size / section->sh_entsize;
7058 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
7060 error (_("Size (%#" PRIx64 ") of section %s "
7061 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7062 section->sh_size,
7063 printable_section_name (filedata, section),
7064 section->sh_entsize);
7065 goto exit_point;
7068 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
7069 section->sh_size, _("symbols"));
7070 if (esyms == NULL)
7071 goto exit_point;
7073 shndx = NULL;
7074 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
7076 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
7077 continue;
7079 if (shndx != NULL)
7081 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7082 free (shndx);
7085 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7086 entry->hdr->sh_offset,
7087 1, entry->hdr->sh_size,
7088 _("symbol table section indices"));
7089 if (shndx == NULL)
7090 goto exit_point;
7092 /* PR17531: file: heap-buffer-overflow */
7093 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
7095 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
7096 printable_section_name (filedata, entry->hdr),
7097 entry->hdr->sh_size,
7098 section->sh_size);
7099 goto exit_point;
7103 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
7105 if (isyms == NULL)
7107 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
7108 goto exit_point;
7111 for (j = 0, psym = isyms; j < number; j++, psym++)
7113 psym->st_name = BYTE_GET (esyms[j].st_name);
7114 psym->st_value = BYTE_GET (esyms[j].st_value);
7115 psym->st_size = BYTE_GET (esyms[j].st_size);
7116 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
7117 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
7118 psym->st_shndx
7119 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
7120 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
7121 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
7122 psym->st_info = BYTE_GET (esyms[j].st_info);
7123 psym->st_other = BYTE_GET (esyms[j].st_other);
7126 exit_point:
7127 free (shndx);
7128 free (esyms);
7130 if (num_syms_return != NULL)
7131 * num_syms_return = isyms == NULL ? 0 : number;
7133 return isyms;
7136 static Elf_Internal_Sym *
7137 get_64bit_elf_symbols (Filedata *filedata,
7138 Elf_Internal_Shdr *section,
7139 uint64_t *num_syms_return)
7141 uint64_t number = 0;
7142 Elf64_External_Sym * esyms = NULL;
7143 Elf_External_Sym_Shndx * shndx = NULL;
7144 Elf_Internal_Sym * isyms = NULL;
7145 Elf_Internal_Sym * psym;
7146 unsigned int j;
7147 elf_section_list * entry;
7149 if (section->sh_size == 0)
7151 if (num_syms_return != NULL)
7152 * num_syms_return = 0;
7153 return NULL;
7156 /* Run some sanity checks first. */
7157 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
7159 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
7160 printable_section_name (filedata, section),
7161 section->sh_entsize);
7162 goto exit_point;
7165 if (section->sh_size > filedata->file_size)
7167 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
7168 printable_section_name (filedata, section),
7169 section->sh_size);
7170 goto exit_point;
7173 number = section->sh_size / section->sh_entsize;
7175 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
7177 error (_("Size (%#" PRIx64 ") of section %s "
7178 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7179 section->sh_size,
7180 printable_section_name (filedata, section),
7181 section->sh_entsize);
7182 goto exit_point;
7185 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
7186 section->sh_size, _("symbols"));
7187 if (!esyms)
7188 goto exit_point;
7190 shndx = NULL;
7191 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
7193 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
7194 continue;
7196 if (shndx != NULL)
7198 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7199 free (shndx);
7202 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7203 entry->hdr->sh_offset,
7204 1, entry->hdr->sh_size,
7205 _("symbol table section indices"));
7206 if (shndx == NULL)
7207 goto exit_point;
7209 /* PR17531: file: heap-buffer-overflow */
7210 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
7212 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
7213 printable_section_name (filedata, entry->hdr),
7214 entry->hdr->sh_size,
7215 section->sh_size);
7216 goto exit_point;
7220 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
7222 if (isyms == NULL)
7224 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
7225 goto exit_point;
7228 for (j = 0, psym = isyms; j < number; j++, psym++)
7230 psym->st_name = BYTE_GET (esyms[j].st_name);
7231 psym->st_info = BYTE_GET (esyms[j].st_info);
7232 psym->st_other = BYTE_GET (esyms[j].st_other);
7233 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
7235 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
7236 psym->st_shndx
7237 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
7238 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
7239 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
7241 psym->st_value = BYTE_GET (esyms[j].st_value);
7242 psym->st_size = BYTE_GET (esyms[j].st_size);
7245 exit_point:
7246 free (shndx);
7247 free (esyms);
7249 if (num_syms_return != NULL)
7250 * num_syms_return = isyms == NULL ? 0 : number;
7252 return isyms;
7255 static Elf_Internal_Sym *
7256 get_elf_symbols (Filedata *filedata,
7257 Elf_Internal_Shdr *section,
7258 uint64_t *num_syms_return)
7260 if (is_32bit_elf)
7261 return get_32bit_elf_symbols (filedata, section, num_syms_return);
7262 else
7263 return get_64bit_elf_symbols (filedata, section, num_syms_return);
7266 static const char *
7267 get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
7269 static char buff[1024];
7270 char * p = buff;
7271 unsigned int field_size = is_32bit_elf ? 8 : 16;
7272 signed int sindex;
7273 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
7274 uint64_t os_flags = 0;
7275 uint64_t proc_flags = 0;
7276 uint64_t unknown_flags = 0;
7277 static const struct
7279 const char * str;
7280 unsigned int len;
7282 flags [] =
7284 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
7285 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
7286 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
7287 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
7288 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
7289 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
7290 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
7291 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
7292 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
7293 /* 9 */ { STRING_COMMA_LEN ("TLS") },
7294 /* IA-64 specific. */
7295 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
7296 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
7297 /* IA-64 OpenVMS specific. */
7298 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
7299 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
7300 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
7301 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
7302 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
7303 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
7304 /* Generic. */
7305 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
7306 /* SPARC specific. */
7307 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
7308 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
7309 /* ARM specific. */
7310 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
7311 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
7312 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
7313 /* GNU specific. */
7314 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
7315 /* VLE specific. */
7316 /* 25 */ { STRING_COMMA_LEN ("VLE") },
7317 /* GNU specific. */
7318 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
7321 if (do_section_details)
7322 p += sprintf (p, "[%*.*lx]: ",
7323 field_size, field_size, (unsigned long) sh_flags);
7325 while (sh_flags)
7327 uint64_t flag;
7329 flag = sh_flags & - sh_flags;
7330 sh_flags &= ~ flag;
7332 if (do_section_details)
7334 switch (flag)
7336 case SHF_WRITE: sindex = 0; break;
7337 case SHF_ALLOC: sindex = 1; break;
7338 case SHF_EXECINSTR: sindex = 2; break;
7339 case SHF_MERGE: sindex = 3; break;
7340 case SHF_STRINGS: sindex = 4; break;
7341 case SHF_INFO_LINK: sindex = 5; break;
7342 case SHF_LINK_ORDER: sindex = 6; break;
7343 case SHF_OS_NONCONFORMING: sindex = 7; break;
7344 case SHF_GROUP: sindex = 8; break;
7345 case SHF_TLS: sindex = 9; break;
7346 case SHF_EXCLUDE: sindex = 18; break;
7347 case SHF_COMPRESSED: sindex = 20; break;
7349 default:
7350 sindex = -1;
7351 switch (filedata->file_header.e_machine)
7353 case EM_IA_64:
7354 if (flag == SHF_IA_64_SHORT)
7355 sindex = 10;
7356 else if (flag == SHF_IA_64_NORECOV)
7357 sindex = 11;
7358 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
7359 switch (flag)
7361 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
7362 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
7363 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
7364 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
7365 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
7366 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
7367 default: break;
7369 break;
7371 case EM_386:
7372 case EM_IAMCU:
7373 case EM_X86_64:
7374 case EM_L1OM:
7375 case EM_K1OM:
7376 case EM_OLD_SPARCV9:
7377 case EM_SPARC32PLUS:
7378 case EM_SPARCV9:
7379 case EM_SPARC:
7380 if (flag == SHF_ORDERED)
7381 sindex = 19;
7382 break;
7384 case EM_ARM:
7385 switch (flag)
7387 case SHF_ENTRYSECT: sindex = 21; break;
7388 case SHF_ARM_PURECODE: sindex = 22; break;
7389 case SHF_COMDEF: sindex = 23; break;
7390 default: break;
7392 break;
7393 case EM_PPC:
7394 if (flag == SHF_PPC_VLE)
7395 sindex = 25;
7396 break;
7397 default:
7398 break;
7401 switch (filedata->file_header.e_ident[EI_OSABI])
7403 case ELFOSABI_GNU:
7404 case ELFOSABI_FREEBSD:
7405 if (flag == SHF_GNU_RETAIN)
7406 sindex = 26;
7407 /* Fall through */
7408 case ELFOSABI_NONE:
7409 if (flag == SHF_GNU_MBIND)
7410 /* We should not recognize SHF_GNU_MBIND for
7411 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7412 not set the EI_OSABI header byte. */
7413 sindex = 24;
7414 break;
7415 default:
7416 break;
7418 break;
7421 if (sindex != -1)
7423 if (p != buff + field_size + 4)
7425 if (size < (10 + 2))
7427 warn (_("Internal error: not enough buffer room for section flag info"));
7428 return _("<unknown>");
7430 size -= 2;
7431 *p++ = ',';
7432 *p++ = ' ';
7435 size -= flags [sindex].len;
7436 p = stpcpy (p, flags [sindex].str);
7438 else if (flag & SHF_MASKOS)
7439 os_flags |= flag;
7440 else if (flag & SHF_MASKPROC)
7441 proc_flags |= flag;
7442 else
7443 unknown_flags |= flag;
7445 else
7447 switch (flag)
7449 case SHF_WRITE: *p = 'W'; break;
7450 case SHF_ALLOC: *p = 'A'; break;
7451 case SHF_EXECINSTR: *p = 'X'; break;
7452 case SHF_MERGE: *p = 'M'; break;
7453 case SHF_STRINGS: *p = 'S'; break;
7454 case SHF_INFO_LINK: *p = 'I'; break;
7455 case SHF_LINK_ORDER: *p = 'L'; break;
7456 case SHF_OS_NONCONFORMING: *p = 'O'; break;
7457 case SHF_GROUP: *p = 'G'; break;
7458 case SHF_TLS: *p = 'T'; break;
7459 case SHF_EXCLUDE: *p = 'E'; break;
7460 case SHF_COMPRESSED: *p = 'C'; break;
7462 default:
7463 if ((filedata->file_header.e_machine == EM_X86_64
7464 || filedata->file_header.e_machine == EM_L1OM
7465 || filedata->file_header.e_machine == EM_K1OM)
7466 && flag == SHF_X86_64_LARGE)
7467 *p = 'l';
7468 else if (filedata->file_header.e_machine == EM_ARM
7469 && flag == SHF_ARM_PURECODE)
7470 *p = 'y';
7471 else if (filedata->file_header.e_machine == EM_PPC
7472 && flag == SHF_PPC_VLE)
7473 *p = 'v';
7474 else if (flag & SHF_MASKOS)
7476 switch (filedata->file_header.e_ident[EI_OSABI])
7478 case ELFOSABI_GNU:
7479 case ELFOSABI_FREEBSD:
7480 if (flag == SHF_GNU_RETAIN)
7482 *p = 'R';
7483 break;
7485 /* Fall through */
7486 case ELFOSABI_NONE:
7487 if (flag == SHF_GNU_MBIND)
7489 /* We should not recognize SHF_GNU_MBIND for
7490 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7491 not set the EI_OSABI header byte. */
7492 *p = 'D';
7493 break;
7495 /* Fall through */
7496 default:
7497 *p = 'o';
7498 sh_flags &= ~SHF_MASKOS;
7499 break;
7502 else if (flag & SHF_MASKPROC)
7504 *p = 'p';
7505 sh_flags &= ~ SHF_MASKPROC;
7507 else
7508 *p = 'x';
7509 break;
7511 p++;
7515 if (do_section_details)
7517 if (os_flags)
7519 if (p != buff + field_size + 4)
7521 if (size < 2 + 5 + field_size + 1)
7523 warn (_("Internal error: not enough buffer room for section flag info"));
7524 return _("<unknown>");
7526 size -= 2;
7527 *p++ = ',';
7528 *p++ = ' ';
7530 size -= 5 + field_size;
7531 p += sprintf (p, "OS (%*.*lx)", field_size, field_size,
7532 (unsigned long) os_flags);
7534 if (proc_flags)
7536 if (p != buff + field_size + 4)
7538 if (size < 2 + 7 + field_size + 1)
7540 warn (_("Internal error: not enough buffer room for section flag info"));
7541 return _("<unknown>");
7543 size -= 2;
7544 *p++ = ',';
7545 *p++ = ' ';
7547 size -= 7 + field_size;
7548 p += sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7549 (unsigned long) proc_flags);
7551 if (unknown_flags)
7553 if (p != buff + field_size + 4)
7555 if (size < 2 + 10 + field_size + 1)
7557 warn (_("Internal error: not enough buffer room for section flag info"));
7558 return _("<unknown>");
7560 size -= 2;
7561 *p++ = ',';
7562 *p++ = ' ';
7564 size -= 10 + field_size;
7565 p += sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7566 (unsigned long) unknown_flags);
7570 *p = '\0';
7571 return buff;
7574 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7575 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
7576 uint64_t size)
7578 if (is_32bit_elf)
7580 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7582 if (size < sizeof (* echdr))
7584 error (_("Compressed section is too small even for a compression header\n"));
7585 return 0;
7588 chdr->ch_type = BYTE_GET (echdr->ch_type);
7589 chdr->ch_size = BYTE_GET (echdr->ch_size);
7590 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7591 return sizeof (*echdr);
7593 else
7595 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7597 if (size < sizeof (* echdr))
7599 error (_("Compressed section is too small even for a compression header\n"));
7600 return 0;
7603 chdr->ch_type = BYTE_GET (echdr->ch_type);
7604 chdr->ch_size = BYTE_GET (echdr->ch_size);
7605 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7606 return sizeof (*echdr);
7610 static bool
7611 process_section_headers (Filedata * filedata)
7613 Elf_Internal_Shdr * section;
7614 unsigned int i;
7616 if (filedata->file_header.e_shnum == 0)
7618 /* PR binutils/12467. */
7619 if (filedata->file_header.e_shoff != 0)
7621 warn (_("possibly corrupt ELF file header - it has a non-zero"
7622 " section header offset, but no section headers\n"));
7623 return false;
7625 else if (do_sections)
7626 printf (_("\nThere are no sections in this file.\n"));
7628 return true;
7631 if (do_sections && !do_header)
7633 if (filedata->is_separate && process_links)
7634 printf (_("In linked file '%s': "), filedata->file_name);
7635 if (! filedata->is_separate || process_links)
7636 printf (ngettext ("There is %d section header, "
7637 "starting at offset %#" PRIx64 ":\n",
7638 "There are %d section headers, "
7639 "starting at offset %#" PRIx64 ":\n",
7640 filedata->file_header.e_shnum),
7641 filedata->file_header.e_shnum,
7642 filedata->file_header.e_shoff);
7645 if (!get_section_headers (filedata, false))
7646 return false;
7648 /* Read in the string table, so that we have names to display. */
7649 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7650 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7652 section = filedata->section_headers + filedata->file_header.e_shstrndx;
7654 if (section->sh_size != 0)
7656 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7657 1, section->sh_size,
7658 _("string table"));
7660 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7664 /* Scan the sections for the dynamic symbol table
7665 and dynamic string table and debug sections. */
7666 eh_addr_size = is_32bit_elf ? 4 : 8;
7667 switch (filedata->file_header.e_machine)
7669 case EM_MIPS:
7670 case EM_MIPS_RS3_LE:
7671 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7672 FDE addresses. However, the ABI also has a semi-official ILP32
7673 variant for which the normal FDE address size rules apply.
7675 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7676 section, where XX is the size of longs in bits. Unfortunately,
7677 earlier compilers provided no way of distinguishing ILP32 objects
7678 from LP64 objects, so if there's any doubt, we should assume that
7679 the official LP64 form is being used. */
7680 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7681 && find_section (filedata, ".gcc_compiled_long32") == NULL)
7682 eh_addr_size = 8;
7683 break;
7685 case EM_H8_300:
7686 case EM_H8_300H:
7687 switch (filedata->file_header.e_flags & EF_H8_MACH)
7689 case E_H8_MACH_H8300:
7690 case E_H8_MACH_H8300HN:
7691 case E_H8_MACH_H8300SN:
7692 case E_H8_MACH_H8300SXN:
7693 eh_addr_size = 2;
7694 break;
7695 case E_H8_MACH_H8300H:
7696 case E_H8_MACH_H8300S:
7697 case E_H8_MACH_H8300SX:
7698 eh_addr_size = 4;
7699 break;
7701 break;
7703 case EM_M32C_OLD:
7704 case EM_M32C:
7705 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7707 case EF_M32C_CPU_M16C:
7708 eh_addr_size = 2;
7709 break;
7711 break;
7714 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7715 do \
7717 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
7718 if (section->sh_entsize != expected_entsize) \
7720 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
7721 i, section->sh_entsize); \
7722 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
7723 expected_entsize); \
7724 section->sh_entsize = expected_entsize; \
7727 while (0)
7729 #define CHECK_ENTSIZE(section, i, type) \
7730 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7731 sizeof (Elf64_External_##type))
7733 for (i = 0, section = filedata->section_headers;
7734 i < filedata->file_header.e_shnum;
7735 i++, section++)
7737 const char *name = printable_section_name (filedata, section);
7739 /* Run some sanity checks on the headers and
7740 possibly fill in some file data as well. */
7741 switch (section->sh_type)
7743 case SHT_DYNSYM:
7744 if (filedata->dynamic_symbols != NULL)
7746 error (_("File contains multiple dynamic symbol tables\n"));
7747 continue;
7750 CHECK_ENTSIZE (section, i, Sym);
7751 filedata->dynamic_symbols
7752 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7753 filedata->dynamic_symtab_section = section;
7754 break;
7756 case SHT_STRTAB:
7757 if (streq (name, ".dynstr"))
7759 if (filedata->dynamic_strings != NULL)
7761 error (_("File contains multiple dynamic string tables\n"));
7762 continue;
7765 filedata->dynamic_strings
7766 = (char *) get_data (NULL, filedata, section->sh_offset,
7767 1, section->sh_size, _("dynamic strings"));
7768 filedata->dynamic_strings_length
7769 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7770 filedata->dynamic_strtab_section = section;
7772 break;
7774 case SHT_SYMTAB_SHNDX:
7776 elf_section_list * entry = xmalloc (sizeof * entry);
7778 entry->hdr = section;
7779 entry->next = filedata->symtab_shndx_list;
7780 filedata->symtab_shndx_list = entry;
7782 break;
7784 case SHT_SYMTAB:
7785 CHECK_ENTSIZE (section, i, Sym);
7786 break;
7788 case SHT_GROUP:
7789 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7790 break;
7792 case SHT_REL:
7793 CHECK_ENTSIZE (section, i, Rel);
7794 if (do_checks && section->sh_size == 0)
7795 warn (_("Section '%s': zero-sized relocation section\n"), name);
7796 break;
7798 case SHT_RELA:
7799 CHECK_ENTSIZE (section, i, Rela);
7800 if (do_checks && section->sh_size == 0)
7801 warn (_("Section '%s': zero-sized relocation section\n"), name);
7802 break;
7804 case SHT_RELR:
7805 CHECK_ENTSIZE (section, i, Relr);
7806 break;
7808 case SHT_NOTE:
7809 case SHT_PROGBITS:
7810 /* Having a zero sized section is not illegal according to the
7811 ELF standard, but it might be an indication that something
7812 is wrong. So issue a warning if we are running in lint mode. */
7813 if (do_checks && section->sh_size == 0)
7814 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7815 break;
7817 default:
7818 break;
7821 if ((do_debugging || do_debug_info || do_debug_abbrevs
7822 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7823 || do_debug_aranges || do_debug_frames || do_debug_macinfo
7824 || do_debug_str || do_debug_str_offsets || do_debug_loc
7825 || do_debug_ranges
7826 || do_debug_addr || do_debug_cu_index || do_debug_links)
7827 && (startswith (name, ".debug_")
7828 || startswith (name, ".zdebug_")))
7830 if (name[1] == 'z')
7831 name += sizeof (".zdebug_") - 1;
7832 else
7833 name += sizeof (".debug_") - 1;
7835 if (do_debugging
7836 || (do_debug_info && startswith (name, "info"))
7837 || (do_debug_info && startswith (name, "types"))
7838 || (do_debug_abbrevs && startswith (name, "abbrev"))
7839 || (do_debug_lines && strcmp (name, "line") == 0)
7840 || (do_debug_lines && startswith (name, "line."))
7841 || (do_debug_pubnames && startswith (name, "pubnames"))
7842 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7843 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7844 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7845 || (do_debug_aranges && startswith (name, "aranges"))
7846 || (do_debug_ranges && startswith (name, "ranges"))
7847 || (do_debug_ranges && startswith (name, "rnglists"))
7848 || (do_debug_frames && startswith (name, "frame"))
7849 || (do_debug_macinfo && startswith (name, "macinfo"))
7850 || (do_debug_macinfo && startswith (name, "macro"))
7851 || (do_debug_str && startswith (name, "str"))
7852 || (do_debug_links && startswith (name, "sup"))
7853 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7854 || (do_debug_loc && startswith (name, "loc"))
7855 || (do_debug_loc && startswith (name, "loclists"))
7856 || (do_debug_addr && startswith (name, "addr"))
7857 || (do_debug_cu_index && startswith (name, "cu_index"))
7858 || (do_debug_cu_index && startswith (name, "tu_index"))
7860 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7862 /* Linkonce section to be combined with .debug_info at link time. */
7863 else if ((do_debugging || do_debug_info)
7864 && startswith (name, ".gnu.linkonce.wi."))
7865 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7866 else if (do_debug_frames && streq (name, ".eh_frame"))
7867 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7868 else if (do_gdb_index && (streq (name, ".gdb_index")
7869 || streq (name, ".debug_names")))
7870 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7871 /* Trace sections for Itanium VMS. */
7872 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7873 || do_trace_aranges)
7874 && startswith (name, ".trace_"))
7876 name += sizeof (".trace_") - 1;
7878 if (do_debugging
7879 || (do_trace_info && streq (name, "info"))
7880 || (do_trace_abbrevs && streq (name, "abbrev"))
7881 || (do_trace_aranges && streq (name, "aranges"))
7883 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7885 else if ((do_debugging || do_debug_links)
7886 && (startswith (name, ".gnu_debuglink")
7887 || startswith (name, ".gnu_debugaltlink")))
7888 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7891 if (! do_sections)
7892 return true;
7894 if (filedata->is_separate && ! process_links)
7895 return true;
7897 if (filedata->is_separate)
7898 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7899 else if (filedata->file_header.e_shnum > 1)
7900 printf (_("\nSection Headers:\n"));
7901 else
7902 printf (_("\nSection Header:\n"));
7904 if (is_32bit_elf)
7906 if (do_section_details)
7908 printf (_(" [Nr] Name\n"));
7909 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
7911 else
7912 printf
7913 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7915 else if (do_wide)
7917 if (do_section_details)
7919 printf (_(" [Nr] Name\n"));
7920 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
7922 else
7923 printf
7924 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7926 else
7928 if (do_section_details)
7930 printf (_(" [Nr] Name\n"));
7931 printf (_(" Type Address Offset Link\n"));
7932 printf (_(" Size EntSize Info Align\n"));
7934 else
7936 printf (_(" [Nr] Name Type Address Offset\n"));
7937 printf (_(" Size EntSize Flags Link Info Align\n"));
7941 if (do_section_details)
7942 printf (_(" Flags\n"));
7944 for (i = 0, section = filedata->section_headers;
7945 i < filedata->file_header.e_shnum;
7946 i++, section++)
7948 /* Run some sanity checks on the section header. */
7950 /* Check the sh_link field. */
7951 switch (section->sh_type)
7953 case SHT_REL:
7954 case SHT_RELA:
7955 if (section->sh_link == 0
7956 && (filedata->file_header.e_type == ET_EXEC
7957 || filedata->file_header.e_type == ET_DYN))
7958 /* A dynamic relocation section where all entries use a
7959 zero symbol index need not specify a symtab section. */
7960 break;
7961 /* Fall through. */
7962 case SHT_SYMTAB_SHNDX:
7963 case SHT_GROUP:
7964 case SHT_HASH:
7965 case SHT_GNU_HASH:
7966 case SHT_GNU_versym:
7967 if (section->sh_link == 0
7968 || section->sh_link >= filedata->file_header.e_shnum
7969 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7970 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7971 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7972 i, section->sh_link);
7973 break;
7975 case SHT_DYNAMIC:
7976 case SHT_SYMTAB:
7977 case SHT_DYNSYM:
7978 case SHT_GNU_verneed:
7979 case SHT_GNU_verdef:
7980 case SHT_GNU_LIBLIST:
7981 if (section->sh_link == 0
7982 || section->sh_link >= filedata->file_header.e_shnum
7983 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
7984 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7985 i, section->sh_link);
7986 break;
7988 case SHT_INIT_ARRAY:
7989 case SHT_FINI_ARRAY:
7990 case SHT_PREINIT_ARRAY:
7991 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7992 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7993 i, section->sh_link);
7994 break;
7996 default:
7997 /* FIXME: Add support for target specific section types. */
7998 #if 0 /* Currently we do not check other section types as there are too
7999 many special cases. Stab sections for example have a type
8000 of SHT_PROGBITS but an sh_link field that links to the .stabstr
8001 section. */
8002 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8003 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8004 i, section->sh_link);
8005 #endif
8006 break;
8009 /* Check the sh_info field. */
8010 switch (section->sh_type)
8012 case SHT_REL:
8013 case SHT_RELA:
8014 if (section->sh_info == 0
8015 && (filedata->file_header.e_type == ET_EXEC
8016 || filedata->file_header.e_type == ET_DYN))
8017 /* Dynamic relocations apply to segments, so they do not
8018 need to specify the section they relocate. */
8019 break;
8020 if (section->sh_info == 0
8021 || section->sh_info >= filedata->file_header.e_shnum
8022 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
8023 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
8024 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
8025 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
8026 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
8027 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
8028 /* FIXME: Are other section types valid ? */
8029 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
8030 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
8031 i, section->sh_info);
8032 break;
8034 case SHT_DYNAMIC:
8035 case SHT_HASH:
8036 case SHT_SYMTAB_SHNDX:
8037 case SHT_INIT_ARRAY:
8038 case SHT_FINI_ARRAY:
8039 case SHT_PREINIT_ARRAY:
8040 if (section->sh_info != 0)
8041 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8042 i, section->sh_info);
8043 break;
8045 case SHT_GROUP:
8046 case SHT_SYMTAB:
8047 case SHT_DYNSYM:
8048 /* A symbol index - we assume that it is valid. */
8049 break;
8051 default:
8052 /* FIXME: Add support for target specific section types. */
8053 if (section->sh_type == SHT_NOBITS)
8054 /* NOBITS section headers with non-zero sh_info fields can be
8055 created when a binary is stripped of everything but its debug
8056 information. The stripped sections have their headers
8057 preserved but their types set to SHT_NOBITS. So do not check
8058 this type of section. */
8060 else if (section->sh_flags & SHF_INFO_LINK)
8062 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
8063 warn (_("[%2u]: Expected link to another section in info field"), i);
8065 else if (section->sh_type < SHT_LOOS
8066 && (section->sh_flags & SHF_GNU_MBIND) == 0
8067 && section->sh_info != 0)
8068 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8069 i, section->sh_info);
8070 break;
8073 /* Check the sh_size field. */
8074 if (section->sh_size > filedata->file_size
8075 && section->sh_type != SHT_NOBITS
8076 && section->sh_type != SHT_NULL
8077 && section->sh_type < SHT_LOOS)
8078 warn (_("Size of section %u is larger than the entire file!\n"), i);
8080 printf (" [%2u] ", i);
8081 if (do_section_details)
8082 printf ("%s\n ", printable_section_name (filedata, section));
8083 else
8084 print_symbol_name (-17, printable_section_name (filedata, section));
8086 printf (do_wide ? " %-15s " : " %-15.15s ",
8087 get_section_type_name (filedata, section->sh_type));
8089 if (is_32bit_elf)
8091 const char * link_too_big = NULL;
8093 print_vma (section->sh_addr, LONG_HEX);
8095 printf ( " %6.6lx %6.6lx %2.2lx",
8096 (unsigned long) section->sh_offset,
8097 (unsigned long) section->sh_size,
8098 (unsigned long) section->sh_entsize);
8100 if (do_section_details)
8101 fputs (" ", stdout);
8102 else
8103 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
8105 if (section->sh_link >= filedata->file_header.e_shnum)
8107 link_too_big = "";
8108 /* The sh_link value is out of range. Normally this indicates
8109 an error but it can have special values in Solaris binaries. */
8110 switch (filedata->file_header.e_machine)
8112 case EM_386:
8113 case EM_IAMCU:
8114 case EM_X86_64:
8115 case EM_L1OM:
8116 case EM_K1OM:
8117 case EM_OLD_SPARCV9:
8118 case EM_SPARC32PLUS:
8119 case EM_SPARCV9:
8120 case EM_SPARC:
8121 if (section->sh_link == (SHN_BEFORE & 0xffff))
8122 link_too_big = "BEFORE";
8123 else if (section->sh_link == (SHN_AFTER & 0xffff))
8124 link_too_big = "AFTER";
8125 break;
8126 default:
8127 break;
8131 if (do_section_details)
8133 if (link_too_big != NULL && * link_too_big)
8134 printf ("<%s> ", link_too_big);
8135 else
8136 printf ("%2u ", section->sh_link);
8137 printf ("%3u %2lu\n", section->sh_info,
8138 (unsigned long) section->sh_addralign);
8140 else
8141 printf ("%2u %3u %2lu\n",
8142 section->sh_link,
8143 section->sh_info,
8144 (unsigned long) section->sh_addralign);
8146 if (link_too_big && ! * link_too_big)
8147 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
8148 i, section->sh_link);
8150 else if (do_wide)
8152 print_vma (section->sh_addr, LONG_HEX);
8154 if ((long) section->sh_offset == section->sh_offset)
8155 printf (" %6.6lx", (unsigned long) section->sh_offset);
8156 else
8158 putchar (' ');
8159 print_vma (section->sh_offset, LONG_HEX);
8162 if ((unsigned long) section->sh_size == section->sh_size)
8163 printf (" %6.6lx", (unsigned long) section->sh_size);
8164 else
8166 putchar (' ');
8167 print_vma (section->sh_size, LONG_HEX);
8170 if ((unsigned long) section->sh_entsize == section->sh_entsize)
8171 printf (" %2.2lx", (unsigned long) section->sh_entsize);
8172 else
8174 putchar (' ');
8175 print_vma (section->sh_entsize, LONG_HEX);
8178 if (do_section_details)
8179 fputs (" ", stdout);
8180 else
8181 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
8183 printf ("%2u %3u ", section->sh_link, section->sh_info);
8185 if ((unsigned long) section->sh_addralign == section->sh_addralign)
8186 printf ("%2lu\n", (unsigned long) section->sh_addralign);
8187 else
8189 print_vma (section->sh_addralign, DEC);
8190 putchar ('\n');
8193 else if (do_section_details)
8195 putchar (' ');
8196 print_vma (section->sh_addr, LONG_HEX);
8197 if ((long) section->sh_offset == section->sh_offset)
8198 printf (" %16.16lx", (unsigned long) section->sh_offset);
8199 else
8201 printf (" ");
8202 print_vma (section->sh_offset, LONG_HEX);
8204 printf (" %u\n ", section->sh_link);
8205 print_vma (section->sh_size, LONG_HEX);
8206 putchar (' ');
8207 print_vma (section->sh_entsize, LONG_HEX);
8209 printf (" %-16u %lu\n",
8210 section->sh_info,
8211 (unsigned long) section->sh_addralign);
8213 else
8215 putchar (' ');
8216 print_vma (section->sh_addr, LONG_HEX);
8217 if ((long) section->sh_offset == section->sh_offset)
8218 printf (" %8.8lx", (unsigned long) section->sh_offset);
8219 else
8221 printf (" ");
8222 print_vma (section->sh_offset, LONG_HEX);
8224 printf ("\n ");
8225 print_vma (section->sh_size, LONG_HEX);
8226 printf (" ");
8227 print_vma (section->sh_entsize, LONG_HEX);
8229 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
8231 printf (" %2u %3u %lu\n",
8232 section->sh_link,
8233 section->sh_info,
8234 (unsigned long) section->sh_addralign);
8237 if (do_section_details)
8239 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
8240 if ((section->sh_flags & SHF_COMPRESSED) != 0)
8242 /* Minimum section size is 12 bytes for 32-bit compression
8243 header + 12 bytes for compressed data header. */
8244 unsigned char buf[24];
8246 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
8247 if (get_data (&buf, filedata, section->sh_offset, 1,
8248 sizeof (buf), _("compression header")))
8250 Elf_Internal_Chdr chdr;
8252 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
8253 printf (_(" [<corrupt>]\n"));
8254 else
8256 if (chdr.ch_type == ch_compress_zlib)
8257 printf (" ZLIB, ");
8258 else if (chdr.ch_type == ch_compress_zstd)
8259 printf (" ZSTD, ");
8260 else
8261 printf (_(" [<unknown>: 0x%x], "),
8262 chdr.ch_type);
8263 print_vma (chdr.ch_size, LONG_HEX);
8264 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
8271 if (!do_section_details)
8273 /* The ordering of the letters shown here matches the ordering of the
8274 corresponding SHF_xxx values, and hence the order in which these
8275 letters will be displayed to the user. */
8276 printf (_("Key to Flags:\n\
8277 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
8278 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
8279 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
8280 switch (filedata->file_header.e_ident[EI_OSABI])
8282 case ELFOSABI_GNU:
8283 case ELFOSABI_FREEBSD:
8284 printf (_("R (retain), "));
8285 /* Fall through */
8286 case ELFOSABI_NONE:
8287 printf (_("D (mbind), "));
8288 break;
8289 default:
8290 break;
8292 if (filedata->file_header.e_machine == EM_X86_64
8293 || filedata->file_header.e_machine == EM_L1OM
8294 || filedata->file_header.e_machine == EM_K1OM)
8295 printf (_("l (large), "));
8296 else if (filedata->file_header.e_machine == EM_ARM)
8297 printf (_("y (purecode), "));
8298 else if (filedata->file_header.e_machine == EM_PPC)
8299 printf (_("v (VLE), "));
8300 printf ("p (processor specific)\n");
8303 return true;
8306 static bool
8307 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
8308 Elf_Internal_Sym **symtab, uint64_t *nsyms,
8309 char **strtab, uint64_t *strtablen)
8311 *strtab = NULL;
8312 *strtablen = 0;
8313 *symtab = get_elf_symbols (filedata, symsec, nsyms);
8315 if (*symtab == NULL)
8316 return false;
8318 if (symsec->sh_link != 0)
8320 Elf_Internal_Shdr *strsec;
8322 if (symsec->sh_link >= filedata->file_header.e_shnum)
8324 error (_("Bad sh_link in symbol table section\n"));
8325 free (*symtab);
8326 *symtab = NULL;
8327 *nsyms = 0;
8328 return false;
8331 strsec = filedata->section_headers + symsec->sh_link;
8333 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
8334 1, strsec->sh_size, _("string table"));
8335 if (*strtab == NULL)
8337 free (*symtab);
8338 *symtab = NULL;
8339 *nsyms = 0;
8340 return false;
8342 *strtablen = strsec->sh_size;
8344 return true;
8347 static const char *
8348 get_group_flags (unsigned int flags)
8350 static char buff[128];
8352 if (flags == 0)
8353 return "";
8354 else if (flags == GRP_COMDAT)
8355 return "COMDAT ";
8357 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
8358 flags,
8359 flags & GRP_MASKOS ? _("<OS specific>") : "",
8360 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
8361 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
8362 ? _("<unknown>") : ""));
8364 return buff;
8367 static bool
8368 process_section_groups (Filedata * filedata)
8370 Elf_Internal_Shdr * section;
8371 unsigned int i;
8372 struct group * group;
8373 Elf_Internal_Shdr * symtab_sec;
8374 Elf_Internal_Shdr * strtab_sec;
8375 Elf_Internal_Sym * symtab;
8376 uint64_t num_syms;
8377 char * strtab;
8378 size_t strtab_size;
8380 /* Don't process section groups unless needed. */
8381 if (!do_unwind && !do_section_groups)
8382 return true;
8384 if (filedata->file_header.e_shnum == 0)
8386 if (do_section_groups)
8388 if (filedata->is_separate)
8389 printf (_("\nThere are no sections group in linked file '%s'.\n"),
8390 filedata->file_name);
8391 else
8392 printf (_("\nThere are no section groups in this file.\n"));
8394 return true;
8397 if (filedata->section_headers == NULL)
8399 error (_("Section headers are not available!\n"));
8400 /* PR 13622: This can happen with a corrupt ELF header. */
8401 return false;
8404 filedata->section_headers_groups
8405 = (struct group **) calloc (filedata->file_header.e_shnum,
8406 sizeof (struct group *));
8408 if (filedata->section_headers_groups == NULL)
8410 error (_("Out of memory reading %u section group headers\n"),
8411 filedata->file_header.e_shnum);
8412 return false;
8415 /* Scan the sections for the group section. */
8416 filedata->group_count = 0;
8417 for (i = 0, section = filedata->section_headers;
8418 i < filedata->file_header.e_shnum;
8419 i++, section++)
8420 if (section->sh_type == SHT_GROUP)
8421 filedata->group_count++;
8423 if (filedata->group_count == 0)
8425 if (do_section_groups)
8427 if (filedata->is_separate)
8428 printf (_("\nThere are no section groups in linked file '%s'.\n"),
8429 filedata->file_name);
8430 else
8431 printf (_("\nThere are no section groups in this file.\n"));
8434 return true;
8437 filedata->section_groups = (struct group *) calloc (filedata->group_count,
8438 sizeof (struct group));
8440 if (filedata->section_groups == NULL)
8442 error (_("Out of memory reading %zu groups\n"), filedata->group_count);
8443 return false;
8446 symtab_sec = NULL;
8447 strtab_sec = NULL;
8448 symtab = NULL;
8449 num_syms = 0;
8450 strtab = NULL;
8451 strtab_size = 0;
8453 if (filedata->is_separate)
8454 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
8456 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
8457 i < filedata->file_header.e_shnum;
8458 i++, section++)
8460 if (section->sh_type == SHT_GROUP)
8462 const char * name = printable_section_name (filedata, section);
8463 const char * group_name;
8464 unsigned char * start;
8465 unsigned char * indices;
8466 unsigned int entry, j, size;
8467 Elf_Internal_Shdr * sec;
8468 Elf_Internal_Sym * sym;
8470 /* Get the symbol table. */
8471 if (section->sh_link >= filedata->file_header.e_shnum
8472 || ((sec = filedata->section_headers + section->sh_link)->sh_type
8473 != SHT_SYMTAB))
8475 error (_("Bad sh_link in group section `%s'\n"), name);
8476 continue;
8479 if (symtab_sec != sec)
8481 symtab_sec = sec;
8482 free (symtab);
8483 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
8486 if (symtab == NULL)
8488 error (_("Corrupt header in group section `%s'\n"), name);
8489 continue;
8492 if (section->sh_info >= num_syms)
8494 error (_("Bad sh_info in group section `%s'\n"), name);
8495 continue;
8498 sym = symtab + section->sh_info;
8500 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8502 if (sym->st_shndx == 0
8503 || sym->st_shndx >= filedata->file_header.e_shnum)
8505 error (_("Bad sh_info in group section `%s'\n"), name);
8506 continue;
8509 group_name = printable_section_name (filedata,
8510 filedata->section_headers
8511 + sym->st_shndx);
8512 strtab_sec = NULL;
8513 free (strtab);
8514 strtab = NULL;
8515 strtab_size = 0;
8517 else
8519 /* Get the string table. */
8520 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
8522 strtab_sec = NULL;
8523 free (strtab);
8524 strtab = NULL;
8525 strtab_size = 0;
8527 else if (strtab_sec
8528 != (sec = filedata->section_headers + symtab_sec->sh_link))
8530 strtab_sec = sec;
8531 free (strtab);
8533 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8534 1, strtab_sec->sh_size,
8535 _("string table"));
8536 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8538 group_name = sym->st_name < strtab_size
8539 ? strtab + sym->st_name : _("<corrupt>");
8542 /* PR 17531: file: loop. */
8543 if (section->sh_entsize > section->sh_size)
8545 error (_("Section %s has sh_entsize (%#" PRIx64 ")"
8546 " which is larger than its size (%#" PRIx64 ")\n"),
8547 printable_section_name (filedata, section),
8548 section->sh_entsize,
8549 section->sh_size);
8550 continue;
8553 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8554 1, section->sh_size,
8555 _("section data"));
8556 if (start == NULL)
8557 continue;
8559 indices = start;
8560 size = (section->sh_size / section->sh_entsize) - 1;
8561 entry = byte_get (indices, 4);
8562 indices += 4;
8564 if (do_section_groups)
8566 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8567 get_group_flags (entry), i, name, group_name, size);
8569 printf (_(" [Index] Name\n"));
8572 group->group_index = i;
8574 for (j = 0; j < size; j++)
8576 struct group_list * g;
8578 entry = byte_get (indices, 4);
8579 indices += 4;
8581 if (entry >= filedata->file_header.e_shnum)
8583 static unsigned num_group_errors = 0;
8585 if (num_group_errors ++ < 10)
8587 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8588 entry, i, filedata->file_header.e_shnum - 1);
8589 if (num_group_errors == 10)
8590 warn (_("Further error messages about overlarge group section indices suppressed\n"));
8592 continue;
8595 if (filedata->section_headers_groups [entry] != NULL)
8597 if (entry)
8599 static unsigned num_errs = 0;
8601 if (num_errs ++ < 10)
8603 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8604 entry, i,
8605 filedata->section_headers_groups [entry]->group_index);
8606 if (num_errs == 10)
8607 warn (_("Further error messages about already contained group sections suppressed\n"));
8609 continue;
8611 else
8613 /* Intel C/C++ compiler may put section 0 in a
8614 section group. We just warn it the first time
8615 and ignore it afterwards. */
8616 static bool warned = false;
8617 if (!warned)
8619 error (_("section 0 in group section [%5u]\n"),
8620 filedata->section_headers_groups [entry]->group_index);
8621 warned = true;
8626 filedata->section_headers_groups [entry] = group;
8628 if (do_section_groups)
8630 sec = filedata->section_headers + entry;
8631 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
8634 g = (struct group_list *) xmalloc (sizeof (struct group_list));
8635 g->section_index = entry;
8636 g->next = group->root;
8637 group->root = g;
8640 free (start);
8642 group++;
8646 free (symtab);
8647 free (strtab);
8648 return true;
8651 /* Data used to display dynamic fixups. */
8653 struct ia64_vms_dynfixup
8655 uint64_t needed_ident; /* Library ident number. */
8656 uint64_t needed; /* Index in the dstrtab of the library name. */
8657 uint64_t fixup_needed; /* Index of the library. */
8658 uint64_t fixup_rela_cnt; /* Number of fixups. */
8659 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
8662 /* Data used to display dynamic relocations. */
8664 struct ia64_vms_dynimgrela
8666 uint64_t img_rela_cnt; /* Number of relocations. */
8667 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
8670 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8671 library). */
8673 static bool
8674 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8675 struct ia64_vms_dynfixup * fixup,
8676 const char * strtab,
8677 unsigned int strtab_sz)
8679 Elf64_External_VMS_IMAGE_FIXUP * imfs;
8680 size_t i;
8681 const char * lib_name;
8683 imfs = get_data (NULL, filedata,
8684 filedata->dynamic_addr + fixup->fixup_rela_off,
8685 sizeof (*imfs), fixup->fixup_rela_cnt,
8686 _("dynamic section image fixups"));
8687 if (!imfs)
8688 return false;
8690 if (fixup->needed < strtab_sz)
8691 lib_name = strtab + fixup->needed;
8692 else
8694 warn (_("corrupt library name index of %#" PRIx64
8695 " found in dynamic entry"), fixup->needed);
8696 lib_name = "???";
8699 printf (_("\nImage fixups for needed library #%" PRId64
8700 ": %s - ident: %" PRIx64 "\n"),
8701 fixup->fixup_needed, lib_name, fixup->needed_ident);
8702 printf
8703 (_("Seg Offset Type SymVec DataType\n"));
8705 for (i = 0; i < (size_t) fixup->fixup_rela_cnt; i++)
8707 unsigned int type;
8708 const char *rtype;
8710 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8711 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
8712 type = BYTE_GET (imfs [i].type);
8713 rtype = elf_ia64_reloc_type (type);
8714 if (rtype == NULL)
8715 printf ("0x%08x ", type);
8716 else
8717 printf ("%-32s ", rtype);
8718 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8719 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8722 free (imfs);
8723 return true;
8726 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8728 static bool
8729 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8731 Elf64_External_VMS_IMAGE_RELA *imrs;
8732 size_t i;
8734 imrs = get_data (NULL, filedata,
8735 filedata->dynamic_addr + imgrela->img_rela_off,
8736 sizeof (*imrs), imgrela->img_rela_cnt,
8737 _("dynamic section image relocations"));
8738 if (!imrs)
8739 return false;
8741 printf (_("\nImage relocs\n"));
8742 printf
8743 (_("Seg Offset Type Addend Seg Sym Off\n"));
8745 for (i = 0; i < (size_t) imgrela->img_rela_cnt; i++)
8747 unsigned int type;
8748 const char *rtype;
8750 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8751 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
8752 type = BYTE_GET (imrs [i].type);
8753 rtype = elf_ia64_reloc_type (type);
8754 if (rtype == NULL)
8755 printf ("0x%08x ", type);
8756 else
8757 printf ("%-31s ", rtype);
8758 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8759 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8760 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
8763 free (imrs);
8764 return true;
8767 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
8769 static bool
8770 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8772 struct ia64_vms_dynfixup fixup;
8773 struct ia64_vms_dynimgrela imgrela;
8774 Elf_Internal_Dyn *entry;
8775 uint64_t strtab_off = 0;
8776 uint64_t strtab_sz = 0;
8777 char *strtab = NULL;
8778 bool res = true;
8780 memset (&fixup, 0, sizeof (fixup));
8781 memset (&imgrela, 0, sizeof (imgrela));
8783 /* Note: the order of the entries is specified by the OpenVMS specs. */
8784 for (entry = filedata->dynamic_section;
8785 entry < filedata->dynamic_section + filedata->dynamic_nent;
8786 entry++)
8788 switch (entry->d_tag)
8790 case DT_IA_64_VMS_STRTAB_OFFSET:
8791 strtab_off = entry->d_un.d_val;
8792 break;
8793 case DT_STRSZ:
8794 strtab_sz = entry->d_un.d_val;
8795 if (strtab == NULL)
8796 strtab = get_data (NULL, filedata,
8797 filedata->dynamic_addr + strtab_off,
8798 1, strtab_sz, _("dynamic string section"));
8799 if (strtab == NULL)
8800 strtab_sz = 0;
8801 break;
8803 case DT_IA_64_VMS_NEEDED_IDENT:
8804 fixup.needed_ident = entry->d_un.d_val;
8805 break;
8806 case DT_NEEDED:
8807 fixup.needed = entry->d_un.d_val;
8808 break;
8809 case DT_IA_64_VMS_FIXUP_NEEDED:
8810 fixup.fixup_needed = entry->d_un.d_val;
8811 break;
8812 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8813 fixup.fixup_rela_cnt = entry->d_un.d_val;
8814 break;
8815 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8816 fixup.fixup_rela_off = entry->d_un.d_val;
8817 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8818 res = false;
8819 break;
8820 case DT_IA_64_VMS_IMG_RELA_CNT:
8821 imgrela.img_rela_cnt = entry->d_un.d_val;
8822 break;
8823 case DT_IA_64_VMS_IMG_RELA_OFF:
8824 imgrela.img_rela_off = entry->d_un.d_val;
8825 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8826 res = false;
8827 break;
8829 default:
8830 break;
8834 free (strtab);
8836 return res;
8839 static struct
8841 const char * name;
8842 int reloc;
8843 int size;
8844 relocation_type rel_type;
8846 dynamic_relocations [] =
8848 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8849 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8850 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8851 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8854 /* Process the reloc section. */
8856 static bool
8857 process_relocs (Filedata * filedata)
8859 uint64_t rel_size;
8860 uint64_t rel_offset;
8862 if (!do_reloc)
8863 return true;
8865 if (do_using_dynamic)
8867 relocation_type rel_type;
8868 const char * name;
8869 bool has_dynamic_reloc;
8870 unsigned int i;
8872 has_dynamic_reloc = false;
8874 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8876 rel_type = dynamic_relocations [i].rel_type;
8877 name = dynamic_relocations [i].name;
8878 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8879 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8881 if (rel_size)
8882 has_dynamic_reloc = true;
8884 if (rel_type == reltype_unknown)
8886 if (dynamic_relocations [i].reloc == DT_JMPREL)
8887 switch (filedata->dynamic_info[DT_PLTREL])
8889 case DT_REL:
8890 rel_type = reltype_rel;
8891 break;
8892 case DT_RELA:
8893 rel_type = reltype_rela;
8894 break;
8898 if (rel_size)
8900 if (filedata->is_separate)
8901 printf
8902 (_("\nIn linked file '%s' section '%s' at offset %#" PRIx64
8903 " contains %" PRId64 " bytes:\n"),
8904 filedata->file_name, name, rel_offset, rel_size);
8905 else
8906 printf
8907 (_("\n'%s' relocation section at offset %#" PRIx64
8908 " contains %" PRId64 " bytes:\n"),
8909 name, rel_offset, rel_size);
8911 dump_relocations (filedata,
8912 offset_from_vma (filedata, rel_offset, rel_size),
8913 rel_size,
8914 filedata->dynamic_symbols,
8915 filedata->num_dynamic_syms,
8916 filedata->dynamic_strings,
8917 filedata->dynamic_strings_length,
8918 rel_type, true /* is_dynamic */);
8922 if (is_ia64_vms (filedata))
8923 if (process_ia64_vms_dynamic_relocs (filedata))
8924 has_dynamic_reloc = true;
8926 if (! has_dynamic_reloc)
8928 if (filedata->is_separate)
8929 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8930 filedata->file_name);
8931 else
8932 printf (_("\nThere are no dynamic relocations in this file.\n"));
8935 else
8937 Elf_Internal_Shdr * section;
8938 size_t i;
8939 bool found = false;
8941 for (i = 0, section = filedata->section_headers;
8942 i < filedata->file_header.e_shnum;
8943 i++, section++)
8945 if ( section->sh_type != SHT_RELA
8946 && section->sh_type != SHT_REL
8947 && section->sh_type != SHT_RELR)
8948 continue;
8950 rel_offset = section->sh_offset;
8951 rel_size = section->sh_size;
8953 if (rel_size)
8955 relocation_type rel_type;
8956 uint64_t num_rela;
8958 if (filedata->is_separate)
8959 printf (_("\nIn linked file '%s' relocation section "),
8960 filedata->file_name);
8961 else
8962 printf (_("\nRelocation section "));
8964 if (filedata->string_table == NULL)
8965 printf ("%d", section->sh_name);
8966 else
8967 printf ("'%s'", printable_section_name (filedata, section));
8969 num_rela = rel_size / section->sh_entsize;
8970 printf (ngettext (" at offset %#" PRIx64
8971 " contains %" PRIu64 " entry:\n",
8972 " at offset %#" PRIx64
8973 " contains %" PRId64 " entries:\n",
8974 num_rela),
8975 rel_offset, num_rela);
8977 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8978 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8980 if (section->sh_link != 0
8981 && section->sh_link < filedata->file_header.e_shnum)
8983 Elf_Internal_Shdr *symsec;
8984 Elf_Internal_Sym *symtab;
8985 uint64_t nsyms;
8986 uint64_t strtablen = 0;
8987 char *strtab = NULL;
8989 symsec = filedata->section_headers + section->sh_link;
8990 if (symsec->sh_type != SHT_SYMTAB
8991 && symsec->sh_type != SHT_DYNSYM)
8992 continue;
8994 if (!get_symtab (filedata, symsec,
8995 &symtab, &nsyms, &strtab, &strtablen))
8996 continue;
8998 dump_relocations (filedata, rel_offset, rel_size,
8999 symtab, nsyms, strtab, strtablen,
9000 rel_type,
9001 symsec->sh_type == SHT_DYNSYM);
9002 free (strtab);
9003 free (symtab);
9005 else
9006 dump_relocations (filedata, rel_offset, rel_size,
9007 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
9009 found = true;
9013 if (! found)
9015 /* Users sometimes forget the -D option, so try to be helpful. */
9016 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
9018 if (filedata->dynamic_info[dynamic_relocations [i].size])
9020 if (filedata->is_separate)
9021 printf (_("\nThere are no static relocations in linked file '%s'."),
9022 filedata->file_name);
9023 else
9024 printf (_("\nThere are no static relocations in this file."));
9025 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
9027 break;
9030 if (i == ARRAY_SIZE (dynamic_relocations))
9032 if (filedata->is_separate)
9033 printf (_("\nThere are no relocations in linked file '%s'.\n"),
9034 filedata->file_name);
9035 else
9036 printf (_("\nThere are no relocations in this file.\n"));
9041 return true;
9044 /* An absolute address consists of a section and an offset. If the
9045 section is NULL, the offset itself is the address, otherwise, the
9046 address equals to LOAD_ADDRESS(section) + offset. */
9048 struct absaddr
9050 unsigned short section;
9051 uint64_t offset;
9054 /* Find the nearest symbol at or below ADDR. Returns the symbol
9055 name, if found, and the offset from the symbol to ADDR. */
9057 static void
9058 find_symbol_for_address (Filedata *filedata,
9059 Elf_Internal_Sym *symtab,
9060 uint64_t nsyms,
9061 const char *strtab,
9062 uint64_t strtab_size,
9063 struct absaddr addr,
9064 const char **symname,
9065 uint64_t *offset)
9067 uint64_t dist = 0x100000;
9068 Elf_Internal_Sym * sym;
9069 Elf_Internal_Sym * beg;
9070 Elf_Internal_Sym * end;
9071 Elf_Internal_Sym * best = NULL;
9073 REMOVE_ARCH_BITS (addr.offset);
9074 beg = symtab;
9075 end = symtab + nsyms;
9077 while (beg < end)
9079 uint64_t value;
9081 sym = beg + (end - beg) / 2;
9083 value = sym->st_value;
9084 REMOVE_ARCH_BITS (value);
9086 if (sym->st_name != 0
9087 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
9088 && addr.offset >= value
9089 && addr.offset - value < dist)
9091 best = sym;
9092 dist = addr.offset - value;
9093 if (!dist)
9094 break;
9097 if (addr.offset < value)
9098 end = sym;
9099 else
9100 beg = sym + 1;
9103 if (best)
9105 *symname = (best->st_name >= strtab_size
9106 ? _("<corrupt>") : strtab + best->st_name);
9107 *offset = dist;
9108 return;
9111 *symname = NULL;
9112 *offset = addr.offset;
9115 static /* signed */ int
9116 symcmp (const void *p, const void *q)
9118 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
9119 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
9121 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
9124 /* Process the unwind section. */
9126 #include "unwind-ia64.h"
9128 struct ia64_unw_table_entry
9130 struct absaddr start;
9131 struct absaddr end;
9132 struct absaddr info;
9135 struct ia64_unw_aux_info
9137 struct ia64_unw_table_entry * table; /* Unwind table. */
9138 uint64_t table_len; /* Length of unwind table. */
9139 unsigned char * info; /* Unwind info. */
9140 uint64_t info_size; /* Size of unwind info. */
9141 uint64_t info_addr; /* Starting address of unwind info. */
9142 uint64_t seg_base; /* Starting address of segment. */
9143 Elf_Internal_Sym * symtab; /* The symbol table. */
9144 uint64_t nsyms; /* Number of symbols. */
9145 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9146 uint64_t nfuns; /* Number of entries in funtab. */
9147 char * strtab; /* The string table. */
9148 uint64_t strtab_size; /* Size of string table. */
9151 static bool
9152 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
9154 struct ia64_unw_table_entry * tp;
9155 size_t j, nfuns;
9156 int in_body;
9157 bool res = true;
9159 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9160 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9161 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9162 aux->funtab[nfuns++] = aux->symtab[j];
9163 aux->nfuns = nfuns;
9164 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9166 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9168 uint64_t stamp;
9169 uint64_t offset;
9170 const unsigned char * dp;
9171 const unsigned char * head;
9172 const unsigned char * end;
9173 const char * procname;
9175 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9176 aux->strtab_size, tp->start, &procname, &offset);
9178 fputs ("\n<", stdout);
9180 if (procname)
9182 fputs (procname, stdout);
9184 if (offset)
9185 printf ("+%" PRIx64, offset);
9188 fputs (">: [", stdout);
9189 print_vma (tp->start.offset, PREFIX_HEX);
9190 fputc ('-', stdout);
9191 print_vma (tp->end.offset, PREFIX_HEX);
9192 printf ("], info at +0x%" PRIx64 "\n",
9193 tp->info.offset - aux->seg_base);
9195 /* PR 17531: file: 86232b32. */
9196 if (aux->info == NULL)
9197 continue;
9199 offset = tp->info.offset;
9200 if (tp->info.section)
9202 if (tp->info.section >= filedata->file_header.e_shnum)
9204 warn (_("Invalid section %u in table entry %td\n"),
9205 tp->info.section, tp - aux->table);
9206 res = false;
9207 continue;
9209 offset += filedata->section_headers[tp->info.section].sh_addr;
9211 offset -= aux->info_addr;
9212 /* PR 17531: file: 0997b4d1. */
9213 if (offset >= aux->info_size
9214 || aux->info_size - offset < 8)
9216 warn (_("Invalid offset %" PRIx64 " in table entry %td\n"),
9217 tp->info.offset, tp - aux->table);
9218 res = false;
9219 continue;
9222 head = aux->info + offset;
9223 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
9225 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
9226 (unsigned) UNW_VER (stamp),
9227 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
9228 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
9229 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
9230 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
9232 if (UNW_VER (stamp) != 1)
9234 printf (_("\tUnknown version.\n"));
9235 continue;
9238 in_body = 0;
9239 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
9240 /* PR 17531: file: 16ceda89. */
9241 if (end > aux->info + aux->info_size)
9242 end = aux->info + aux->info_size;
9243 for (dp = head + 8; dp < end;)
9244 dp = unw_decode (dp, in_body, & in_body, end);
9247 free (aux->funtab);
9249 return res;
9252 static bool
9253 slurp_ia64_unwind_table (Filedata * filedata,
9254 struct ia64_unw_aux_info * aux,
9255 Elf_Internal_Shdr * sec)
9257 uint64_t size, nrelas, i;
9258 Elf_Internal_Phdr * seg;
9259 struct ia64_unw_table_entry * tep;
9260 Elf_Internal_Shdr * relsec;
9261 Elf_Internal_Rela * rela;
9262 Elf_Internal_Rela * rp;
9263 unsigned char * table;
9264 unsigned char * tp;
9265 Elf_Internal_Sym * sym;
9266 const char * relname;
9268 aux->table_len = 0;
9270 /* First, find the starting address of the segment that includes
9271 this section: */
9273 if (filedata->file_header.e_phnum)
9275 if (! get_program_headers (filedata))
9276 return false;
9278 for (seg = filedata->program_headers;
9279 seg < filedata->program_headers + filedata->file_header.e_phnum;
9280 ++seg)
9282 if (seg->p_type != PT_LOAD)
9283 continue;
9285 if (sec->sh_addr >= seg->p_vaddr
9286 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9288 aux->seg_base = seg->p_vaddr;
9289 break;
9294 /* Second, build the unwind table from the contents of the unwind section: */
9295 size = sec->sh_size;
9296 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9297 _("unwind table"));
9298 if (!table)
9299 return false;
9301 aux->table_len = size / (3 * eh_addr_size);
9302 aux->table = (struct ia64_unw_table_entry *)
9303 xcmalloc (aux->table_len, sizeof (aux->table[0]));
9304 tep = aux->table;
9306 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
9308 tep->start.section = SHN_UNDEF;
9309 tep->end.section = SHN_UNDEF;
9310 tep->info.section = SHN_UNDEF;
9311 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9312 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9313 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9314 tep->start.offset += aux->seg_base;
9315 tep->end.offset += aux->seg_base;
9316 tep->info.offset += aux->seg_base;
9318 free (table);
9320 /* Third, apply any relocations to the unwind table: */
9321 for (relsec = filedata->section_headers;
9322 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9323 ++relsec)
9325 if (relsec->sh_type != SHT_RELA
9326 || relsec->sh_info >= filedata->file_header.e_shnum
9327 || filedata->section_headers + relsec->sh_info != sec)
9328 continue;
9330 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9331 & rela, & nrelas))
9333 free (aux->table);
9334 aux->table = NULL;
9335 aux->table_len = 0;
9336 return false;
9339 for (rp = rela; rp < rela + nrelas; ++rp)
9341 unsigned int sym_ndx;
9342 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9343 relname = elf_ia64_reloc_type (r_type);
9345 /* PR 17531: file: 9fa67536. */
9346 if (relname == NULL)
9348 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9349 continue;
9352 if (! startswith (relname, "R_IA64_SEGREL"))
9354 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9355 continue;
9358 i = rp->r_offset / (3 * eh_addr_size);
9360 /* PR 17531: file: 5bc8d9bf. */
9361 if (i >= aux->table_len)
9363 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
9365 continue;
9368 sym_ndx = get_reloc_symindex (rp->r_info);
9369 if (sym_ndx >= aux->nsyms)
9371 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9372 sym_ndx);
9373 continue;
9375 sym = aux->symtab + sym_ndx;
9377 switch (rp->r_offset / eh_addr_size % 3)
9379 case 0:
9380 aux->table[i].start.section = sym->st_shndx;
9381 aux->table[i].start.offset = rp->r_addend + sym->st_value;
9382 break;
9383 case 1:
9384 aux->table[i].end.section = sym->st_shndx;
9385 aux->table[i].end.offset = rp->r_addend + sym->st_value;
9386 break;
9387 case 2:
9388 aux->table[i].info.section = sym->st_shndx;
9389 aux->table[i].info.offset = rp->r_addend + sym->st_value;
9390 break;
9391 default:
9392 break;
9396 free (rela);
9399 return true;
9402 static bool
9403 ia64_process_unwind (Filedata * filedata)
9405 Elf_Internal_Shdr * sec;
9406 Elf_Internal_Shdr * unwsec = NULL;
9407 uint64_t i, unwcount = 0, unwstart = 0;
9408 struct ia64_unw_aux_info aux;
9409 bool res = true;
9411 memset (& aux, 0, sizeof (aux));
9413 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9415 if (sec->sh_type == SHT_SYMTAB)
9417 if (aux.symtab)
9419 error (_("Multiple symbol tables encountered\n"));
9420 free (aux.symtab);
9421 aux.symtab = NULL;
9422 free (aux.strtab);
9423 aux.strtab = NULL;
9425 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9426 &aux.strtab, &aux.strtab_size))
9427 return false;
9429 else if (sec->sh_type == SHT_IA_64_UNWIND)
9430 unwcount++;
9433 if (!unwcount)
9434 printf (_("\nThere are no unwind sections in this file.\n"));
9436 while (unwcount-- > 0)
9438 const char *suffix;
9439 size_t len, len2;
9441 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
9442 i < filedata->file_header.e_shnum; ++i, ++sec)
9443 if (sec->sh_type == SHT_IA_64_UNWIND)
9445 unwsec = sec;
9446 break;
9448 /* We have already counted the number of SHT_IA64_UNWIND
9449 sections so the loop above should never fail. */
9450 assert (unwsec != NULL);
9452 unwstart = i + 1;
9453 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
9455 if ((unwsec->sh_flags & SHF_GROUP) != 0)
9457 /* We need to find which section group it is in. */
9458 struct group_list * g;
9460 if (filedata->section_headers_groups == NULL
9461 || filedata->section_headers_groups[i] == NULL)
9462 i = filedata->file_header.e_shnum;
9463 else
9465 g = filedata->section_headers_groups[i]->root;
9467 for (; g != NULL; g = g->next)
9469 sec = filedata->section_headers + g->section_index;
9471 if (section_name_valid (filedata, sec)
9472 && streq (section_name (filedata, sec),
9473 ELF_STRING_ia64_unwind_info))
9474 break;
9477 if (g == NULL)
9478 i = filedata->file_header.e_shnum;
9481 else if (section_name_valid (filedata, unwsec)
9482 && startswith (section_name (filedata, unwsec),
9483 ELF_STRING_ia64_unwind_once))
9485 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
9486 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
9487 suffix = section_name (filedata, unwsec) + len;
9488 for (i = 0, sec = filedata->section_headers;
9489 i < filedata->file_header.e_shnum;
9490 ++i, ++sec)
9491 if (section_name_valid (filedata, sec)
9492 && startswith (section_name (filedata, sec),
9493 ELF_STRING_ia64_unwind_info_once)
9494 && streq (section_name (filedata, sec) + len2, suffix))
9495 break;
9497 else
9499 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
9500 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
9501 len = sizeof (ELF_STRING_ia64_unwind) - 1;
9502 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
9503 suffix = "";
9504 if (section_name_valid (filedata, unwsec)
9505 && startswith (section_name (filedata, unwsec),
9506 ELF_STRING_ia64_unwind))
9507 suffix = section_name (filedata, unwsec) + len;
9508 for (i = 0, sec = filedata->section_headers;
9509 i < filedata->file_header.e_shnum;
9510 ++i, ++sec)
9511 if (section_name_valid (filedata, sec)
9512 && startswith (section_name (filedata, sec),
9513 ELF_STRING_ia64_unwind_info)
9514 && streq (section_name (filedata, sec) + len2, suffix))
9515 break;
9518 if (i == filedata->file_header.e_shnum)
9520 printf (_("\nCould not find unwind info section for "));
9522 if (filedata->string_table == NULL)
9523 printf ("%d", unwsec->sh_name);
9524 else
9525 printf ("'%s'", printable_section_name (filedata, unwsec));
9527 else
9529 aux.info_addr = sec->sh_addr;
9530 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9531 sec->sh_size,
9532 _("unwind info"));
9533 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9535 printf (_("\nUnwind section "));
9537 if (filedata->string_table == NULL)
9538 printf ("%d", unwsec->sh_name);
9539 else
9540 printf ("'%s'", printable_section_name (filedata, unwsec));
9542 printf (_(" at offset %#" PRIx64 " contains %" PRIu64 " entries:\n"),
9543 unwsec->sh_offset,
9544 unwsec->sh_size / (3 * eh_addr_size));
9546 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9547 && aux.table_len > 0)
9548 dump_ia64_unwind (filedata, & aux);
9550 free ((char *) aux.table);
9551 free ((char *) aux.info);
9552 aux.table = NULL;
9553 aux.info = NULL;
9557 free (aux.symtab);
9558 free ((char *) aux.strtab);
9560 return res;
9563 struct hppa_unw_table_entry
9565 struct absaddr start;
9566 struct absaddr end;
9567 unsigned int Cannot_unwind:1; /* 0 */
9568 unsigned int Millicode:1; /* 1 */
9569 unsigned int Millicode_save_sr0:1; /* 2 */
9570 unsigned int Region_description:2; /* 3..4 */
9571 unsigned int reserved1:1; /* 5 */
9572 unsigned int Entry_SR:1; /* 6 */
9573 unsigned int Entry_FR:4; /* Number saved 7..10 */
9574 unsigned int Entry_GR:5; /* Number saved 11..15 */
9575 unsigned int Args_stored:1; /* 16 */
9576 unsigned int Variable_Frame:1; /* 17 */
9577 unsigned int Separate_Package_Body:1; /* 18 */
9578 unsigned int Frame_Extension_Millicode:1; /* 19 */
9579 unsigned int Stack_Overflow_Check:1; /* 20 */
9580 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9581 unsigned int Ada_Region:1; /* 22 */
9582 unsigned int cxx_info:1; /* 23 */
9583 unsigned int cxx_try_catch:1; /* 24 */
9584 unsigned int sched_entry_seq:1; /* 25 */
9585 unsigned int reserved2:1; /* 26 */
9586 unsigned int Save_SP:1; /* 27 */
9587 unsigned int Save_RP:1; /* 28 */
9588 unsigned int Save_MRP_in_frame:1; /* 29 */
9589 unsigned int extn_ptr_defined:1; /* 30 */
9590 unsigned int Cleanup_defined:1; /* 31 */
9592 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9593 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9594 unsigned int Large_frame:1; /* 2 */
9595 unsigned int Pseudo_SP_Set:1; /* 3 */
9596 unsigned int reserved4:1; /* 4 */
9597 unsigned int Total_frame_size:27; /* 5..31 */
9600 struct hppa_unw_aux_info
9602 struct hppa_unw_table_entry * table; /* Unwind table. */
9603 uint64_t table_len; /* Length of unwind table. */
9604 uint64_t seg_base; /* Starting address of segment. */
9605 Elf_Internal_Sym * symtab; /* The symbol table. */
9606 uint64_t nsyms; /* Number of symbols. */
9607 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9608 uint64_t nfuns; /* Number of entries in funtab. */
9609 char * strtab; /* The string table. */
9610 uint64_t strtab_size; /* Size of string table. */
9613 static bool
9614 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9616 struct hppa_unw_table_entry * tp;
9617 uint64_t j, nfuns;
9618 bool res = true;
9620 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9621 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9622 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9623 aux->funtab[nfuns++] = aux->symtab[j];
9624 aux->nfuns = nfuns;
9625 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9627 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9629 uint64_t offset;
9630 const char * procname;
9632 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9633 aux->strtab_size, tp->start, &procname,
9634 &offset);
9636 fputs ("\n<", stdout);
9638 if (procname)
9640 fputs (procname, stdout);
9642 if (offset)
9643 printf ("+%" PRIx64, offset);
9646 fputs (">: [", stdout);
9647 print_vma (tp->start.offset, PREFIX_HEX);
9648 fputc ('-', stdout);
9649 print_vma (tp->end.offset, PREFIX_HEX);
9650 printf ("]\n\t");
9652 #define PF(_m) if (tp->_m) printf (#_m " ");
9653 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9654 PF(Cannot_unwind);
9655 PF(Millicode);
9656 PF(Millicode_save_sr0);
9657 /* PV(Region_description); */
9658 PF(Entry_SR);
9659 PV(Entry_FR);
9660 PV(Entry_GR);
9661 PF(Args_stored);
9662 PF(Variable_Frame);
9663 PF(Separate_Package_Body);
9664 PF(Frame_Extension_Millicode);
9665 PF(Stack_Overflow_Check);
9666 PF(Two_Instruction_SP_Increment);
9667 PF(Ada_Region);
9668 PF(cxx_info);
9669 PF(cxx_try_catch);
9670 PF(sched_entry_seq);
9671 PF(Save_SP);
9672 PF(Save_RP);
9673 PF(Save_MRP_in_frame);
9674 PF(extn_ptr_defined);
9675 PF(Cleanup_defined);
9676 PF(MPE_XL_interrupt_marker);
9677 PF(HP_UX_interrupt_marker);
9678 PF(Large_frame);
9679 PF(Pseudo_SP_Set);
9680 PV(Total_frame_size);
9681 #undef PF
9682 #undef PV
9685 printf ("\n");
9687 free (aux->funtab);
9689 return res;
9692 static bool
9693 slurp_hppa_unwind_table (Filedata * filedata,
9694 struct hppa_unw_aux_info * aux,
9695 Elf_Internal_Shdr * sec)
9697 uint64_t size, unw_ent_size, nentries, nrelas, i;
9698 Elf_Internal_Phdr * seg;
9699 struct hppa_unw_table_entry * tep;
9700 Elf_Internal_Shdr * relsec;
9701 Elf_Internal_Rela * rela;
9702 Elf_Internal_Rela * rp;
9703 unsigned char * table;
9704 unsigned char * tp;
9705 Elf_Internal_Sym * sym;
9706 const char * relname;
9708 /* First, find the starting address of the segment that includes
9709 this section. */
9710 if (filedata->file_header.e_phnum)
9712 if (! get_program_headers (filedata))
9713 return false;
9715 for (seg = filedata->program_headers;
9716 seg < filedata->program_headers + filedata->file_header.e_phnum;
9717 ++seg)
9719 if (seg->p_type != PT_LOAD)
9720 continue;
9722 if (sec->sh_addr >= seg->p_vaddr
9723 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9725 aux->seg_base = seg->p_vaddr;
9726 break;
9731 /* Second, build the unwind table from the contents of the unwind
9732 section. */
9733 size = sec->sh_size;
9734 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9735 _("unwind table"));
9736 if (!table)
9737 return false;
9739 unw_ent_size = 16;
9740 nentries = size / unw_ent_size;
9741 size = unw_ent_size * nentries;
9743 aux->table_len = nentries;
9744 tep = aux->table = (struct hppa_unw_table_entry *)
9745 xcmalloc (nentries, sizeof (aux->table[0]));
9747 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9749 unsigned int tmp1, tmp2;
9751 tep->start.section = SHN_UNDEF;
9752 tep->end.section = SHN_UNDEF;
9754 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9755 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9756 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9757 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9759 tep->start.offset += aux->seg_base;
9760 tep->end.offset += aux->seg_base;
9762 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9763 tep->Millicode = (tmp1 >> 30) & 0x1;
9764 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9765 tep->Region_description = (tmp1 >> 27) & 0x3;
9766 tep->reserved1 = (tmp1 >> 26) & 0x1;
9767 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9768 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9769 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9770 tep->Args_stored = (tmp1 >> 15) & 0x1;
9771 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9772 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9773 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9774 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9775 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9776 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9777 tep->cxx_info = (tmp1 >> 8) & 0x1;
9778 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9779 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9780 tep->reserved2 = (tmp1 >> 5) & 0x1;
9781 tep->Save_SP = (tmp1 >> 4) & 0x1;
9782 tep->Save_RP = (tmp1 >> 3) & 0x1;
9783 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9784 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9785 tep->Cleanup_defined = tmp1 & 0x1;
9787 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9788 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9789 tep->Large_frame = (tmp2 >> 29) & 0x1;
9790 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9791 tep->reserved4 = (tmp2 >> 27) & 0x1;
9792 tep->Total_frame_size = tmp2 & 0x7ffffff;
9794 free (table);
9796 /* Third, apply any relocations to the unwind table. */
9797 for (relsec = filedata->section_headers;
9798 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9799 ++relsec)
9801 if (relsec->sh_type != SHT_RELA
9802 || relsec->sh_info >= filedata->file_header.e_shnum
9803 || filedata->section_headers + relsec->sh_info != sec)
9804 continue;
9806 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9807 & rela, & nrelas))
9808 return false;
9810 for (rp = rela; rp < rela + nrelas; ++rp)
9812 unsigned int sym_ndx;
9813 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9814 relname = elf_hppa_reloc_type (r_type);
9816 if (relname == NULL)
9818 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9819 continue;
9822 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
9823 if (! startswith (relname, "R_PARISC_SEGREL"))
9825 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9826 continue;
9829 i = rp->r_offset / unw_ent_size;
9830 if (i >= aux->table_len)
9832 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
9834 continue;
9837 sym_ndx = get_reloc_symindex (rp->r_info);
9838 if (sym_ndx >= aux->nsyms)
9840 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9841 sym_ndx);
9842 continue;
9844 sym = aux->symtab + sym_ndx;
9846 switch ((rp->r_offset % unw_ent_size) / 4)
9848 case 0:
9849 aux->table[i].start.section = sym->st_shndx;
9850 aux->table[i].start.offset = sym->st_value + rp->r_addend;
9851 break;
9852 case 1:
9853 aux->table[i].end.section = sym->st_shndx;
9854 aux->table[i].end.offset = sym->st_value + rp->r_addend;
9855 break;
9856 default:
9857 break;
9861 free (rela);
9864 return true;
9867 static bool
9868 hppa_process_unwind (Filedata * filedata)
9870 struct hppa_unw_aux_info aux;
9871 Elf_Internal_Shdr * unwsec = NULL;
9872 Elf_Internal_Shdr * sec;
9873 size_t i;
9874 bool res = true;
9876 if (filedata->string_table == NULL)
9877 return false;
9879 memset (& aux, 0, sizeof (aux));
9881 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9883 if (sec->sh_type == SHT_SYMTAB)
9885 if (aux.symtab)
9887 error (_("Multiple symbol tables encountered\n"));
9888 free (aux.symtab);
9889 aux.symtab = NULL;
9890 free (aux.strtab);
9891 aux.strtab = NULL;
9893 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9894 &aux.strtab, &aux.strtab_size))
9895 return false;
9897 else if (section_name_valid (filedata, sec)
9898 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9899 unwsec = sec;
9902 if (!unwsec)
9903 printf (_("\nThere are no unwind sections in this file.\n"));
9905 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9907 if (section_name_valid (filedata, sec)
9908 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9910 uint64_t num_unwind = sec->sh_size / 16;
9912 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
9913 "contains %" PRIu64 " entry:\n",
9914 "\nUnwind section '%s' at offset %#" PRIx64 " "
9915 "contains %" PRIu64 " entries:\n",
9916 num_unwind),
9917 printable_section_name (filedata, sec),
9918 sec->sh_offset,
9919 num_unwind);
9921 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9922 res = false;
9924 if (res && aux.table_len > 0)
9926 if (! dump_hppa_unwind (filedata, &aux))
9927 res = false;
9930 free ((char *) aux.table);
9931 aux.table = NULL;
9935 free (aux.symtab);
9936 free ((char *) aux.strtab);
9938 return res;
9941 struct arm_section
9943 unsigned char * data; /* The unwind data. */
9944 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9945 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9946 uint64_t nrelas; /* The number of relocations. */
9947 unsigned int rel_type; /* REL or RELA ? */
9948 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
9951 struct arm_unw_aux_info
9953 Filedata * filedata; /* The file containing the unwind sections. */
9954 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9955 uint64_t nsyms; /* Number of symbols. */
9956 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9957 uint64_t nfuns; /* Number of these symbols. */
9958 char * strtab; /* The file's string table. */
9959 uint64_t strtab_size; /* Size of string table. */
9962 static const char *
9963 arm_print_vma_and_name (Filedata * filedata,
9964 struct arm_unw_aux_info * aux,
9965 uint64_t fn,
9966 struct absaddr addr)
9968 const char *procname;
9969 uint64_t sym_offset;
9971 if (addr.section == SHN_UNDEF)
9972 addr.offset = fn;
9974 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9975 aux->strtab_size, addr, &procname,
9976 &sym_offset);
9978 print_vma (fn, PREFIX_HEX);
9980 if (procname)
9982 fputs (" <", stdout);
9983 fputs (procname, stdout);
9985 if (sym_offset)
9986 printf ("+0x%" PRIx64, sym_offset);
9987 fputc ('>', stdout);
9990 return procname;
9993 static void
9994 arm_free_section (struct arm_section *arm_sec)
9996 free (arm_sec->data);
9997 free (arm_sec->rela);
10000 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
10001 cached section and install SEC instead.
10002 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
10003 and return its valued in * WORDP, relocating if necessary.
10004 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
10005 relocation's offset in ADDR.
10006 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
10007 into the string table of the symbol associated with the reloc. If no
10008 reloc was applied store -1 there.
10009 5) Return TRUE upon success, FALSE otherwise. */
10011 static bool
10012 get_unwind_section_word (Filedata * filedata,
10013 struct arm_unw_aux_info * aux,
10014 struct arm_section * arm_sec,
10015 Elf_Internal_Shdr * sec,
10016 uint64_t word_offset,
10017 unsigned int * wordp,
10018 struct absaddr * addr,
10019 uint64_t * sym_name)
10021 Elf_Internal_Rela *rp;
10022 Elf_Internal_Sym *sym;
10023 const char * relname;
10024 unsigned int word;
10025 bool wrapped;
10027 if (sec == NULL || arm_sec == NULL)
10028 return false;
10030 addr->section = SHN_UNDEF;
10031 addr->offset = 0;
10033 if (sym_name != NULL)
10034 *sym_name = (uint64_t) -1;
10036 /* If necessary, update the section cache. */
10037 if (sec != arm_sec->sec)
10039 Elf_Internal_Shdr *relsec;
10041 arm_free_section (arm_sec);
10043 arm_sec->sec = sec;
10044 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
10045 sec->sh_size, _("unwind data"));
10046 arm_sec->rela = NULL;
10047 arm_sec->nrelas = 0;
10049 for (relsec = filedata->section_headers;
10050 relsec < filedata->section_headers + filedata->file_header.e_shnum;
10051 ++relsec)
10053 if (relsec->sh_info >= filedata->file_header.e_shnum
10054 || filedata->section_headers + relsec->sh_info != sec
10055 /* PR 15745: Check the section type as well. */
10056 || (relsec->sh_type != SHT_REL
10057 && relsec->sh_type != SHT_RELA))
10058 continue;
10060 arm_sec->rel_type = relsec->sh_type;
10061 if (relsec->sh_type == SHT_REL)
10063 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
10064 relsec->sh_size,
10065 & arm_sec->rela, & arm_sec->nrelas))
10066 return false;
10068 else /* relsec->sh_type == SHT_RELA */
10070 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
10071 relsec->sh_size,
10072 & arm_sec->rela, & arm_sec->nrelas))
10073 return false;
10075 break;
10078 arm_sec->next_rela = arm_sec->rela;
10081 /* If there is no unwind data we can do nothing. */
10082 if (arm_sec->data == NULL)
10083 return false;
10085 /* If the offset is invalid then fail. */
10086 if (/* PR 21343 *//* PR 18879 */
10087 sec->sh_size < 4
10088 || word_offset > sec->sh_size - 4)
10089 return false;
10091 /* Get the word at the required offset. */
10092 word = byte_get (arm_sec->data + word_offset, 4);
10094 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
10095 if (arm_sec->rela == NULL)
10097 * wordp = word;
10098 return true;
10101 /* Look through the relocs to find the one that applies to the provided offset. */
10102 wrapped = false;
10103 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
10105 uint64_t prelval, offset;
10107 if (rp->r_offset > word_offset && !wrapped)
10109 rp = arm_sec->rela;
10110 wrapped = true;
10112 if (rp->r_offset > word_offset)
10113 break;
10115 if (rp->r_offset & 3)
10117 warn (_("Skipping unexpected relocation at offset %#" PRIx64 "\n"),
10118 rp->r_offset);
10119 continue;
10122 if (rp->r_offset < word_offset)
10123 continue;
10125 /* PR 17531: file: 027-161405-0.004 */
10126 if (aux->symtab == NULL)
10127 continue;
10129 if (arm_sec->rel_type == SHT_REL)
10131 offset = word & 0x7fffffff;
10132 if (offset & 0x40000000)
10133 offset |= ~ (uint64_t) 0x7fffffff;
10135 else if (arm_sec->rel_type == SHT_RELA)
10136 offset = rp->r_addend;
10137 else
10139 error (_("Unknown section relocation type %d encountered\n"),
10140 arm_sec->rel_type);
10141 break;
10144 /* PR 17531 file: 027-1241568-0.004. */
10145 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
10147 error (_("Bad symbol index in unwind relocation "
10148 "(%" PRIu64 " > %" PRIu64 ")\n"),
10149 ELF32_R_SYM (rp->r_info), aux->nsyms);
10150 break;
10153 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
10154 offset += sym->st_value;
10155 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
10157 /* Check that we are processing the expected reloc type. */
10158 if (filedata->file_header.e_machine == EM_ARM)
10160 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
10161 if (relname == NULL)
10163 warn (_("Skipping unknown ARM relocation type: %d\n"),
10164 (int) ELF32_R_TYPE (rp->r_info));
10165 continue;
10168 if (streq (relname, "R_ARM_NONE"))
10169 continue;
10171 if (! streq (relname, "R_ARM_PREL31"))
10173 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
10174 continue;
10177 else if (filedata->file_header.e_machine == EM_TI_C6000)
10179 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
10180 if (relname == NULL)
10182 warn (_("Skipping unknown C6000 relocation type: %d\n"),
10183 (int) ELF32_R_TYPE (rp->r_info));
10184 continue;
10187 if (streq (relname, "R_C6000_NONE"))
10188 continue;
10190 if (! streq (relname, "R_C6000_PREL31"))
10192 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
10193 continue;
10196 prelval >>= 1;
10198 else
10200 /* This function currently only supports ARM and TI unwinders. */
10201 warn (_("Only TI and ARM unwinders are currently supported\n"));
10202 break;
10205 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
10206 addr->section = sym->st_shndx;
10207 addr->offset = offset;
10209 if (sym_name)
10210 * sym_name = sym->st_name;
10211 break;
10214 *wordp = word;
10215 arm_sec->next_rela = rp;
10217 return true;
10220 static const char *tic6x_unwind_regnames[16] =
10222 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
10223 "A14", "A13", "A12", "A11", "A10",
10224 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
10227 static void
10228 decode_tic6x_unwind_regmask (unsigned int mask)
10230 int i;
10232 for (i = 12; mask; mask >>= 1, i--)
10234 if (mask & 1)
10236 fputs (tic6x_unwind_regnames[i], stdout);
10237 if (mask > 1)
10238 fputs (", ", stdout);
10243 #define ADVANCE \
10244 if (remaining == 0 && more_words) \
10246 data_offset += 4; \
10247 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
10248 data_offset, & word, & addr, NULL)) \
10249 return false; \
10250 remaining = 4; \
10251 more_words--; \
10254 #define GET_OP(OP) \
10255 ADVANCE; \
10256 if (remaining) \
10258 remaining--; \
10259 (OP) = word >> 24; \
10260 word <<= 8; \
10262 else \
10264 printf (_("[Truncated opcode]\n")); \
10265 return false; \
10267 printf ("0x%02x ", OP)
10269 static bool
10270 decode_arm_unwind_bytecode (Filedata * filedata,
10271 struct arm_unw_aux_info * aux,
10272 unsigned int word,
10273 unsigned int remaining,
10274 unsigned int more_words,
10275 uint64_t data_offset,
10276 Elf_Internal_Shdr * data_sec,
10277 struct arm_section * data_arm_sec)
10279 struct absaddr addr;
10280 bool res = true;
10282 /* Decode the unwinding instructions. */
10283 while (1)
10285 unsigned int op, op2;
10287 ADVANCE;
10288 if (remaining == 0)
10289 break;
10290 remaining--;
10291 op = word >> 24;
10292 word <<= 8;
10294 printf (" 0x%02x ", op);
10296 if ((op & 0xc0) == 0x00)
10298 int offset = ((op & 0x3f) << 2) + 4;
10300 printf (" vsp = vsp + %d", offset);
10302 else if ((op & 0xc0) == 0x40)
10304 int offset = ((op & 0x3f) << 2) + 4;
10306 printf (" vsp = vsp - %d", offset);
10308 else if ((op & 0xf0) == 0x80)
10310 GET_OP (op2);
10311 if (op == 0x80 && op2 == 0)
10312 printf (_("Refuse to unwind"));
10313 else
10315 unsigned int mask = ((op & 0x0f) << 8) | op2;
10316 bool first = true;
10317 int i;
10319 printf ("pop {");
10320 for (i = 0; i < 12; i++)
10321 if (mask & (1 << i))
10323 if (first)
10324 first = false;
10325 else
10326 printf (", ");
10327 printf ("r%d", 4 + i);
10329 printf ("}");
10332 else if ((op & 0xf0) == 0x90)
10334 if (op == 0x9d || op == 0x9f)
10335 printf (_(" [Reserved]"));
10336 else
10337 printf (" vsp = r%d", op & 0x0f);
10339 else if ((op & 0xf0) == 0xa0)
10341 int end = 4 + (op & 0x07);
10342 bool first = true;
10343 int i;
10345 printf (" pop {");
10346 for (i = 4; i <= end; i++)
10348 if (first)
10349 first = false;
10350 else
10351 printf (", ");
10352 printf ("r%d", i);
10354 if (op & 0x08)
10356 if (!first)
10357 printf (", ");
10358 printf ("r14");
10360 printf ("}");
10362 else if (op == 0xb0)
10363 printf (_(" finish"));
10364 else if (op == 0xb1)
10366 GET_OP (op2);
10367 if (op2 == 0 || (op2 & 0xf0) != 0)
10368 printf (_("[Spare]"));
10369 else
10371 unsigned int mask = op2 & 0x0f;
10372 bool first = true;
10373 int i;
10375 printf ("pop {");
10376 for (i = 0; i < 12; i++)
10377 if (mask & (1 << i))
10379 if (first)
10380 first = false;
10381 else
10382 printf (", ");
10383 printf ("r%d", i);
10385 printf ("}");
10388 else if (op == 0xb2)
10390 unsigned char buf[9];
10391 unsigned int i, len;
10392 uint64_t offset;
10394 for (i = 0; i < sizeof (buf); i++)
10396 GET_OP (buf[i]);
10397 if ((buf[i] & 0x80) == 0)
10398 break;
10400 if (i == sizeof (buf))
10402 error (_("corrupt change to vsp\n"));
10403 res = false;
10405 else
10407 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10408 assert (len == i + 1);
10409 offset = offset * 4 + 0x204;
10410 printf ("vsp = vsp + %" PRId64, offset);
10413 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
10415 unsigned int first, last;
10417 GET_OP (op2);
10418 first = op2 >> 4;
10419 last = op2 & 0x0f;
10420 if (op == 0xc8)
10421 first = first + 16;
10422 printf ("pop {D%d", first);
10423 if (last)
10424 printf ("-D%d", first + last);
10425 printf ("}");
10427 else if (op == 0xb4)
10428 printf (_(" pop {ra_auth_code}"));
10429 else if (op == 0xb5)
10430 printf (_(" vsp as modifier for PAC validation"));
10431 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
10433 unsigned int count = op & 0x07;
10435 printf ("pop {D8");
10436 if (count)
10437 printf ("-D%d", 8 + count);
10438 printf ("}");
10440 else if (op >= 0xc0 && op <= 0xc5)
10442 unsigned int count = op & 0x07;
10444 printf (" pop {wR10");
10445 if (count)
10446 printf ("-wR%d", 10 + count);
10447 printf ("}");
10449 else if (op == 0xc6)
10451 unsigned int first, last;
10453 GET_OP (op2);
10454 first = op2 >> 4;
10455 last = op2 & 0x0f;
10456 printf ("pop {wR%d", first);
10457 if (last)
10458 printf ("-wR%d", first + last);
10459 printf ("}");
10461 else if (op == 0xc7)
10463 GET_OP (op2);
10464 if (op2 == 0 || (op2 & 0xf0) != 0)
10465 printf (_("[Spare]"));
10466 else
10468 unsigned int mask = op2 & 0x0f;
10469 bool first = true;
10470 int i;
10472 printf ("pop {");
10473 for (i = 0; i < 4; i++)
10474 if (mask & (1 << i))
10476 if (first)
10477 first = false;
10478 else
10479 printf (", ");
10480 printf ("wCGR%d", i);
10482 printf ("}");
10485 else
10487 printf (_(" [unsupported opcode]"));
10488 res = false;
10491 printf ("\n");
10494 return res;
10497 static bool
10498 decode_tic6x_unwind_bytecode (Filedata * filedata,
10499 struct arm_unw_aux_info * aux,
10500 unsigned int word,
10501 unsigned int remaining,
10502 unsigned int more_words,
10503 uint64_t data_offset,
10504 Elf_Internal_Shdr * data_sec,
10505 struct arm_section * data_arm_sec)
10507 struct absaddr addr;
10509 /* Decode the unwinding instructions. */
10510 while (1)
10512 unsigned int op, op2;
10514 ADVANCE;
10515 if (remaining == 0)
10516 break;
10517 remaining--;
10518 op = word >> 24;
10519 word <<= 8;
10521 printf (" 0x%02x ", op);
10523 if ((op & 0xc0) == 0x00)
10525 int offset = ((op & 0x3f) << 3) + 8;
10526 printf (" sp = sp + %d", offset);
10528 else if ((op & 0xc0) == 0x80)
10530 GET_OP (op2);
10531 if (op == 0x80 && op2 == 0)
10532 printf (_("Refuse to unwind"));
10533 else
10535 unsigned int mask = ((op & 0x1f) << 8) | op2;
10536 if (op & 0x20)
10537 printf ("pop compact {");
10538 else
10539 printf ("pop {");
10541 decode_tic6x_unwind_regmask (mask);
10542 printf("}");
10545 else if ((op & 0xf0) == 0xc0)
10547 unsigned int reg;
10548 unsigned int nregs;
10549 unsigned int i;
10550 const char *name;
10551 struct
10553 unsigned int offset;
10554 unsigned int reg;
10555 } regpos[16];
10557 /* Scan entire instruction first so that GET_OP output is not
10558 interleaved with disassembly. */
10559 nregs = 0;
10560 for (i = 0; nregs < (op & 0xf); i++)
10562 GET_OP (op2);
10563 reg = op2 >> 4;
10564 if (reg != 0xf)
10566 regpos[nregs].offset = i * 2;
10567 regpos[nregs].reg = reg;
10568 nregs++;
10571 reg = op2 & 0xf;
10572 if (reg != 0xf)
10574 regpos[nregs].offset = i * 2 + 1;
10575 regpos[nregs].reg = reg;
10576 nregs++;
10580 printf (_("pop frame {"));
10581 if (nregs == 0)
10583 printf (_("*corrupt* - no registers specified"));
10585 else
10587 reg = nregs - 1;
10588 for (i = i * 2; i > 0; i--)
10590 if (regpos[reg].offset == i - 1)
10592 name = tic6x_unwind_regnames[regpos[reg].reg];
10593 if (reg > 0)
10594 reg--;
10596 else
10597 name = _("[pad]");
10599 fputs (name, stdout);
10600 if (i > 1)
10601 printf (", ");
10605 printf ("}");
10607 else if (op == 0xd0)
10608 printf (" MOV FP, SP");
10609 else if (op == 0xd1)
10610 printf (" __c6xabi_pop_rts");
10611 else if (op == 0xd2)
10613 unsigned char buf[9];
10614 unsigned int i, len;
10615 uint64_t offset;
10617 for (i = 0; i < sizeof (buf); i++)
10619 GET_OP (buf[i]);
10620 if ((buf[i] & 0x80) == 0)
10621 break;
10623 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10624 if (i == sizeof (buf))
10626 warn (_("Corrupt stack pointer adjustment detected\n"));
10627 return false;
10630 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10631 assert (len == i + 1);
10632 offset = offset * 8 + 0x408;
10633 printf (_("sp = sp + %" PRId64), offset);
10635 else if ((op & 0xf0) == 0xe0)
10637 if ((op & 0x0f) == 7)
10638 printf (" RETURN");
10639 else
10640 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10642 else
10644 printf (_(" [unsupported opcode]"));
10646 putchar ('\n');
10649 return true;
10652 static uint64_t
10653 arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
10655 uint64_t offset;
10657 offset = word & 0x7fffffff;
10658 if (offset & 0x40000000)
10659 offset |= ~ (uint64_t) 0x7fffffff;
10661 if (filedata->file_header.e_machine == EM_TI_C6000)
10662 offset <<= 1;
10664 return offset + where;
10667 static bool
10668 decode_arm_unwind (Filedata * filedata,
10669 struct arm_unw_aux_info * aux,
10670 unsigned int word,
10671 unsigned int remaining,
10672 uint64_t data_offset,
10673 Elf_Internal_Shdr * data_sec,
10674 struct arm_section * data_arm_sec)
10676 int per_index;
10677 unsigned int more_words = 0;
10678 struct absaddr addr;
10679 uint64_t sym_name = (uint64_t) -1;
10680 bool res = true;
10682 if (remaining == 0)
10684 /* Fetch the first word.
10685 Note - when decoding an object file the address extracted
10686 here will always be 0. So we also pass in the sym_name
10687 parameter so that we can find the symbol associated with
10688 the personality routine. */
10689 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10690 & word, & addr, & sym_name))
10691 return false;
10693 remaining = 4;
10695 else
10697 addr.section = SHN_UNDEF;
10698 addr.offset = 0;
10701 if ((word & 0x80000000) == 0)
10703 /* Expand prel31 for personality routine. */
10704 uint64_t fn;
10705 const char *procname;
10707 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
10708 printf (_(" Personality routine: "));
10709 if (fn == 0
10710 && addr.section == SHN_UNDEF && addr.offset == 0
10711 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
10713 procname = aux->strtab + sym_name;
10714 print_vma (fn, PREFIX_HEX);
10715 if (procname)
10717 fputs (" <", stdout);
10718 fputs (procname, stdout);
10719 fputc ('>', stdout);
10722 else
10723 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10724 fputc ('\n', stdout);
10726 /* The GCC personality routines use the standard compact
10727 encoding, starting with one byte giving the number of
10728 words. */
10729 if (procname != NULL
10730 && (startswith (procname, "__gcc_personality_v0")
10731 || startswith (procname, "__gxx_personality_v0")
10732 || startswith (procname, "__gcj_personality_v0")
10733 || startswith (procname, "__gnu_objc_personality_v0")))
10735 remaining = 0;
10736 more_words = 1;
10737 ADVANCE;
10738 if (!remaining)
10740 printf (_(" [Truncated data]\n"));
10741 return false;
10743 more_words = word >> 24;
10744 word <<= 8;
10745 remaining--;
10746 per_index = -1;
10748 else
10749 return true;
10751 else
10753 /* ARM EHABI Section 6.3:
10755 An exception-handling table entry for the compact model looks like:
10757 31 30-28 27-24 23-0
10758 -- ----- ----- ----
10759 1 0 index Data for personalityRoutine[index] */
10761 if (filedata->file_header.e_machine == EM_ARM
10762 && (word & 0x70000000))
10764 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10765 res = false;
10768 per_index = (word >> 24) & 0x7f;
10769 printf (_(" Compact model index: %d\n"), per_index);
10770 if (per_index == 0)
10772 more_words = 0;
10773 word <<= 8;
10774 remaining--;
10776 else if (per_index < 3)
10778 more_words = (word >> 16) & 0xff;
10779 word <<= 16;
10780 remaining -= 2;
10784 switch (filedata->file_header.e_machine)
10786 case EM_ARM:
10787 if (per_index < 3)
10789 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10790 data_offset, data_sec, data_arm_sec))
10791 res = false;
10793 else
10795 warn (_("Unknown ARM compact model index encountered\n"));
10796 printf (_(" [reserved]\n"));
10797 res = false;
10799 break;
10801 case EM_TI_C6000:
10802 if (per_index < 3)
10804 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10805 data_offset, data_sec, data_arm_sec))
10806 res = false;
10808 else if (per_index < 5)
10810 if (((word >> 17) & 0x7f) == 0x7f)
10811 printf (_(" Restore stack from frame pointer\n"));
10812 else
10813 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10814 printf (_(" Registers restored: "));
10815 if (per_index == 4)
10816 printf (" (compact) ");
10817 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10818 putchar ('\n');
10819 printf (_(" Return register: %s\n"),
10820 tic6x_unwind_regnames[word & 0xf]);
10822 else
10823 printf (_(" [reserved (%d)]\n"), per_index);
10824 break;
10826 default:
10827 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10828 filedata->file_header.e_machine);
10829 res = false;
10832 /* Decode the descriptors. Not implemented. */
10834 return res;
10837 static bool
10838 dump_arm_unwind (Filedata * filedata,
10839 struct arm_unw_aux_info * aux,
10840 Elf_Internal_Shdr * exidx_sec)
10842 struct arm_section exidx_arm_sec, extab_arm_sec;
10843 unsigned int i, exidx_len;
10844 uint64_t j, nfuns;
10845 bool res = true;
10847 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10848 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10849 exidx_len = exidx_sec->sh_size / 8;
10851 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10852 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10853 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10854 aux->funtab[nfuns++] = aux->symtab[j];
10855 aux->nfuns = nfuns;
10856 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10858 for (i = 0; i < exidx_len; i++)
10860 unsigned int exidx_fn, exidx_entry;
10861 struct absaddr fn_addr, entry_addr;
10862 uint64_t fn;
10864 fputc ('\n', stdout);
10866 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10867 8 * i, & exidx_fn, & fn_addr, NULL)
10868 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10869 8 * i + 4, & exidx_entry, & entry_addr, NULL))
10871 free (aux->funtab);
10872 arm_free_section (& exidx_arm_sec);
10873 arm_free_section (& extab_arm_sec);
10874 return false;
10877 /* ARM EHABI, Section 5:
10878 An index table entry consists of 2 words.
10879 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10880 if (exidx_fn & 0x80000000)
10882 warn (_("corrupt index table entry: %x\n"), exidx_fn);
10883 res = false;
10886 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10888 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10889 fputs (": ", stdout);
10891 if (exidx_entry == 1)
10893 print_vma (exidx_entry, PREFIX_HEX);
10894 fputs (" [cantunwind]\n", stdout);
10896 else if (exidx_entry & 0x80000000)
10898 print_vma (exidx_entry, PREFIX_HEX);
10899 fputc ('\n', stdout);
10900 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10902 else
10904 uint64_t table, table_offset = 0;
10905 Elf_Internal_Shdr *table_sec;
10907 fputs ("@", stdout);
10908 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10909 print_vma (table, PREFIX_HEX);
10910 printf ("\n");
10912 /* Locate the matching .ARM.extab. */
10913 if (entry_addr.section != SHN_UNDEF
10914 && entry_addr.section < filedata->file_header.e_shnum)
10916 table_sec = filedata->section_headers + entry_addr.section;
10917 table_offset = entry_addr.offset;
10918 /* PR 18879 */
10919 if (table_offset > table_sec->sh_size)
10921 warn (_("Unwind entry contains corrupt offset (%#" PRIx64 ") into section %s\n"),
10922 table_offset,
10923 printable_section_name (filedata, table_sec));
10924 res = false;
10925 continue;
10928 else
10930 table_sec = find_section_by_address (filedata, table);
10931 if (table_sec != NULL)
10932 table_offset = table - table_sec->sh_addr;
10935 if (table_sec == NULL)
10937 warn (_("Could not locate .ARM.extab section containing %#" PRIx64 ".\n"),
10938 table);
10939 res = false;
10940 continue;
10943 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10944 &extab_arm_sec))
10945 res = false;
10949 printf ("\n");
10951 free (aux->funtab);
10952 arm_free_section (&exidx_arm_sec);
10953 arm_free_section (&extab_arm_sec);
10955 return res;
10958 /* Used for both ARM and C6X unwinding tables. */
10960 static bool
10961 arm_process_unwind (Filedata * filedata)
10963 struct arm_unw_aux_info aux;
10964 Elf_Internal_Shdr *unwsec = NULL;
10965 Elf_Internal_Shdr *sec;
10966 size_t i;
10967 unsigned int sec_type;
10968 bool res = true;
10970 switch (filedata->file_header.e_machine)
10972 case EM_ARM:
10973 sec_type = SHT_ARM_EXIDX;
10974 break;
10976 case EM_TI_C6000:
10977 sec_type = SHT_C6000_UNWIND;
10978 break;
10980 default:
10981 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
10982 filedata->file_header.e_machine);
10983 return false;
10986 if (filedata->string_table == NULL)
10987 return false;
10989 memset (& aux, 0, sizeof (aux));
10990 aux.filedata = filedata;
10992 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10994 if (sec->sh_type == SHT_SYMTAB)
10996 if (aux.symtab)
10998 error (_("Multiple symbol tables encountered\n"));
10999 free (aux.symtab);
11000 aux.symtab = NULL;
11001 free (aux.strtab);
11002 aux.strtab = NULL;
11004 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
11005 &aux.strtab, &aux.strtab_size))
11006 return false;
11008 else if (sec->sh_type == sec_type)
11009 unwsec = sec;
11012 if (unwsec == NULL)
11013 printf (_("\nThere are no unwind sections in this file.\n"));
11014 else
11015 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
11017 if (sec->sh_type == sec_type)
11019 uint64_t num_unwind = sec->sh_size / (2 * eh_addr_size);
11020 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
11021 "contains %" PRIu64 " entry:\n",
11022 "\nUnwind section '%s' at offset %#" PRIx64 " "
11023 "contains %" PRIu64 " entries:\n",
11024 num_unwind),
11025 printable_section_name (filedata, sec),
11026 sec->sh_offset,
11027 num_unwind);
11029 if (! dump_arm_unwind (filedata, &aux, sec))
11030 res = false;
11034 free (aux.symtab);
11035 free ((char *) aux.strtab);
11037 return res;
11040 static bool
11041 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
11043 printf (_("No processor specific unwind information to decode\n"));
11044 return true;
11047 static bool
11048 process_unwind (Filedata * filedata)
11050 struct unwind_handler
11052 unsigned int machtype;
11053 bool (* handler)(Filedata *);
11054 } handlers[] =
11056 { EM_ARM, arm_process_unwind },
11057 { EM_IA_64, ia64_process_unwind },
11058 { EM_PARISC, hppa_process_unwind },
11059 { EM_TI_C6000, arm_process_unwind },
11060 { EM_386, no_processor_specific_unwind },
11061 { EM_X86_64, no_processor_specific_unwind },
11062 { 0, NULL }
11064 int i;
11066 if (!do_unwind)
11067 return true;
11069 for (i = 0; handlers[i].handler != NULL; i++)
11070 if (filedata->file_header.e_machine == handlers[i].machtype)
11071 return handlers[i].handler (filedata);
11073 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
11074 get_machine_name (filedata->file_header.e_machine));
11075 return true;
11078 static void
11079 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
11081 switch (entry->d_tag)
11083 case DT_AARCH64_BTI_PLT:
11084 case DT_AARCH64_PAC_PLT:
11085 break;
11086 default:
11087 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11088 break;
11090 putchar ('\n');
11093 static void
11094 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
11096 switch (entry->d_tag)
11098 case DT_MIPS_FLAGS:
11099 if (entry->d_un.d_val == 0)
11100 printf (_("NONE"));
11101 else
11103 static const char * opts[] =
11105 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
11106 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
11107 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
11108 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
11109 "RLD_ORDER_SAFE"
11111 unsigned int cnt;
11112 bool first = true;
11114 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
11115 if (entry->d_un.d_val & (1 << cnt))
11117 printf ("%s%s", first ? "" : " ", opts[cnt]);
11118 first = false;
11121 break;
11123 case DT_MIPS_IVERSION:
11124 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11125 printf (_("Interface Version: %s"),
11126 get_dynamic_name (filedata, entry->d_un.d_val));
11127 else
11128 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
11129 entry->d_un.d_ptr);
11130 break;
11132 case DT_MIPS_TIME_STAMP:
11134 char timebuf[128];
11135 struct tm * tmp;
11136 time_t atime = entry->d_un.d_val;
11138 tmp = gmtime (&atime);
11139 /* PR 17531: file: 6accc532. */
11140 if (tmp == NULL)
11141 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
11142 else
11143 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
11144 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11145 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11146 printf (_("Time Stamp: %s"), timebuf);
11148 break;
11150 case DT_MIPS_RLD_VERSION:
11151 case DT_MIPS_LOCAL_GOTNO:
11152 case DT_MIPS_CONFLICTNO:
11153 case DT_MIPS_LIBLISTNO:
11154 case DT_MIPS_SYMTABNO:
11155 case DT_MIPS_UNREFEXTNO:
11156 case DT_MIPS_HIPAGENO:
11157 case DT_MIPS_DELTA_CLASS_NO:
11158 case DT_MIPS_DELTA_INSTANCE_NO:
11159 case DT_MIPS_DELTA_RELOC_NO:
11160 case DT_MIPS_DELTA_SYM_NO:
11161 case DT_MIPS_DELTA_CLASSSYM_NO:
11162 case DT_MIPS_COMPACT_SIZE:
11163 print_vma (entry->d_un.d_val, DEC);
11164 break;
11166 case DT_MIPS_XHASH:
11167 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11168 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11169 /* Falls through. */
11171 default:
11172 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11174 putchar ('\n');
11177 static void
11178 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
11180 switch (entry->d_tag)
11182 case DT_HP_DLD_FLAGS:
11184 static struct
11186 unsigned int bit;
11187 const char * str;
11189 flags[] =
11191 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
11192 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
11193 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
11194 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
11195 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
11196 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
11197 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
11198 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
11199 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
11200 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
11201 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
11202 { DT_HP_GST, "HP_GST" },
11203 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
11204 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
11205 { DT_HP_NODELETE, "HP_NODELETE" },
11206 { DT_HP_GROUP, "HP_GROUP" },
11207 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
11209 bool first = true;
11210 size_t cnt;
11211 uint64_t val = entry->d_un.d_val;
11213 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
11214 if (val & flags[cnt].bit)
11216 if (! first)
11217 putchar (' ');
11218 fputs (flags[cnt].str, stdout);
11219 first = false;
11220 val ^= flags[cnt].bit;
11223 if (val != 0 || first)
11225 if (! first)
11226 putchar (' ');
11227 print_vma (val, HEX);
11230 break;
11232 default:
11233 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11234 break;
11236 putchar ('\n');
11239 /* VMS vs Unix time offset and factor. */
11241 #define VMS_EPOCH_OFFSET 35067168000000000LL
11242 #define VMS_GRANULARITY_FACTOR 10000000
11243 #ifndef INT64_MIN
11244 #define INT64_MIN (-9223372036854775807LL - 1)
11245 #endif
11247 /* Display a VMS time in a human readable format. */
11249 static void
11250 print_vms_time (int64_t vmstime)
11252 struct tm *tm = NULL;
11253 time_t unxtime;
11255 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
11257 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
11258 unxtime = vmstime;
11259 if (unxtime == vmstime)
11260 tm = gmtime (&unxtime);
11262 if (tm != NULL)
11263 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
11264 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
11265 tm->tm_hour, tm->tm_min, tm->tm_sec);
11268 static void
11269 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
11271 switch (entry->d_tag)
11273 case DT_IA_64_PLT_RESERVE:
11274 /* First 3 slots reserved. */
11275 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11276 printf (" -- ");
11277 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
11278 break;
11280 case DT_IA_64_VMS_LINKTIME:
11281 print_vms_time (entry->d_un.d_val);
11282 break;
11284 case DT_IA_64_VMS_LNKFLAGS:
11285 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11286 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
11287 printf (" CALL_DEBUG");
11288 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
11289 printf (" NOP0BUFS");
11290 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
11291 printf (" P0IMAGE");
11292 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
11293 printf (" MKTHREADS");
11294 if (entry->d_un.d_val & VMS_LF_UPCALLS)
11295 printf (" UPCALLS");
11296 if (entry->d_un.d_val & VMS_LF_IMGSTA)
11297 printf (" IMGSTA");
11298 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
11299 printf (" INITIALIZE");
11300 if (entry->d_un.d_val & VMS_LF_MAIN)
11301 printf (" MAIN");
11302 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
11303 printf (" EXE_INIT");
11304 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
11305 printf (" TBK_IN_IMG");
11306 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
11307 printf (" DBG_IN_IMG");
11308 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
11309 printf (" TBK_IN_DSF");
11310 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
11311 printf (" DBG_IN_DSF");
11312 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
11313 printf (" SIGNATURES");
11314 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
11315 printf (" REL_SEG_OFF");
11316 break;
11318 default:
11319 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11320 break;
11322 putchar ('\n');
11325 static bool
11326 get_32bit_dynamic_section (Filedata * filedata)
11328 Elf32_External_Dyn * edyn;
11329 Elf32_External_Dyn * ext;
11330 Elf_Internal_Dyn * entry;
11332 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
11333 filedata->dynamic_addr, 1,
11334 filedata->dynamic_size,
11335 _("dynamic section"));
11336 if (!edyn)
11337 return false;
11339 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
11340 might not have the luxury of section headers. Look for the DT_NULL
11341 terminator to determine the number of entries. */
11342 for (ext = edyn, filedata->dynamic_nent = 0;
11343 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
11344 ext++)
11346 filedata->dynamic_nent++;
11347 if (BYTE_GET (ext->d_tag) == DT_NULL)
11348 break;
11351 filedata->dynamic_section
11352 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
11353 if (filedata->dynamic_section == NULL)
11355 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11356 filedata->dynamic_nent);
11357 free (edyn);
11358 return false;
11361 for (ext = edyn, entry = filedata->dynamic_section;
11362 entry < filedata->dynamic_section + filedata->dynamic_nent;
11363 ext++, entry++)
11365 entry->d_tag = BYTE_GET (ext->d_tag);
11366 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
11369 free (edyn);
11371 return true;
11374 static bool
11375 get_64bit_dynamic_section (Filedata * filedata)
11377 Elf64_External_Dyn * edyn;
11378 Elf64_External_Dyn * ext;
11379 Elf_Internal_Dyn * entry;
11381 /* Read in the data. */
11382 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
11383 filedata->dynamic_addr, 1,
11384 filedata->dynamic_size,
11385 _("dynamic section"));
11386 if (!edyn)
11387 return false;
11389 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
11390 might not have the luxury of section headers. Look for the DT_NULL
11391 terminator to determine the number of entries. */
11392 for (ext = edyn, filedata->dynamic_nent = 0;
11393 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
11394 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
11395 ext++)
11397 filedata->dynamic_nent++;
11398 if (BYTE_GET (ext->d_tag) == DT_NULL)
11399 break;
11402 filedata->dynamic_section
11403 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
11404 if (filedata->dynamic_section == NULL)
11406 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11407 filedata->dynamic_nent);
11408 free (edyn);
11409 return false;
11412 /* Convert from external to internal formats. */
11413 for (ext = edyn, entry = filedata->dynamic_section;
11414 entry < filedata->dynamic_section + filedata->dynamic_nent;
11415 ext++, entry++)
11417 entry->d_tag = BYTE_GET (ext->d_tag);
11418 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
11421 free (edyn);
11423 return true;
11426 static bool
11427 get_dynamic_section (Filedata *filedata)
11429 if (filedata->dynamic_section)
11430 return true;
11432 if (is_32bit_elf)
11433 return get_32bit_dynamic_section (filedata);
11434 else
11435 return get_64bit_dynamic_section (filedata);
11438 static void
11439 print_dynamic_flags (uint64_t flags)
11441 bool first = true;
11443 while (flags)
11445 uint64_t flag;
11447 flag = flags & - flags;
11448 flags &= ~ flag;
11450 if (first)
11451 first = false;
11452 else
11453 putc (' ', stdout);
11455 switch (flag)
11457 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
11458 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
11459 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
11460 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
11461 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
11462 default: fputs (_("unknown"), stdout); break;
11465 puts ("");
11468 static uint64_t *
11469 get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
11471 unsigned char * e_data;
11472 uint64_t * i_data;
11474 /* If size_t is smaller than uint64_t, eg because you are building
11475 on a 32-bit host, then make sure that when number is cast to
11476 size_t no information is lost. */
11477 if ((size_t) number != number
11478 || ent_size * number / ent_size != number)
11480 error (_("Size overflow prevents reading %" PRIu64
11481 " elements of size %u\n"),
11482 number, ent_size);
11483 return NULL;
11486 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
11487 attempting to allocate memory when the read is bound to fail. */
11488 if (ent_size * number > filedata->file_size)
11490 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
11491 number);
11492 return NULL;
11495 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
11496 if (e_data == NULL)
11498 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
11499 number);
11500 return NULL;
11503 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
11505 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
11506 number * ent_size);
11507 free (e_data);
11508 return NULL;
11511 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
11512 if (i_data == NULL)
11514 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11515 number);
11516 free (e_data);
11517 return NULL;
11520 while (number--)
11521 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11523 free (e_data);
11525 return i_data;
11528 static uint64_t
11529 get_num_dynamic_syms (Filedata * filedata)
11531 uint64_t num_of_syms = 0;
11533 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11534 return num_of_syms;
11536 if (filedata->dynamic_info[DT_HASH])
11538 unsigned char nb[8];
11539 unsigned char nc[8];
11540 unsigned int hash_ent_size = 4;
11542 if ((filedata->file_header.e_machine == EM_ALPHA
11543 || filedata->file_header.e_machine == EM_S390
11544 || filedata->file_header.e_machine == EM_S390_OLD)
11545 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11546 hash_ent_size = 8;
11548 if (fseek64 (filedata->handle,
11549 (filedata->archive_file_offset
11550 + offset_from_vma (filedata,
11551 filedata->dynamic_info[DT_HASH],
11552 sizeof nb + sizeof nc)),
11553 SEEK_SET))
11555 error (_("Unable to seek to start of dynamic information\n"));
11556 goto no_hash;
11559 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11561 error (_("Failed to read in number of buckets\n"));
11562 goto no_hash;
11565 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11567 error (_("Failed to read in number of chains\n"));
11568 goto no_hash;
11571 filedata->nbuckets = byte_get (nb, hash_ent_size);
11572 filedata->nchains = byte_get (nc, hash_ent_size);
11574 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11576 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11577 hash_ent_size);
11578 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11579 hash_ent_size);
11581 if (filedata->buckets != NULL && filedata->chains != NULL)
11582 num_of_syms = filedata->nchains;
11584 no_hash:
11585 if (num_of_syms == 0)
11587 free (filedata->buckets);
11588 filedata->buckets = NULL;
11589 free (filedata->chains);
11590 filedata->chains = NULL;
11591 filedata->nbuckets = 0;
11595 if (filedata->dynamic_info_DT_GNU_HASH)
11597 unsigned char nb[16];
11598 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
11599 uint64_t buckets_vma;
11600 uint64_t hn;
11602 if (fseek64 (filedata->handle,
11603 (filedata->archive_file_offset
11604 + offset_from_vma (filedata,
11605 filedata->dynamic_info_DT_GNU_HASH,
11606 sizeof nb)),
11607 SEEK_SET))
11609 error (_("Unable to seek to start of dynamic information\n"));
11610 goto no_gnu_hash;
11613 if (fread (nb, 16, 1, filedata->handle) != 1)
11615 error (_("Failed to read in number of buckets\n"));
11616 goto no_gnu_hash;
11619 filedata->ngnubuckets = byte_get (nb, 4);
11620 filedata->gnusymidx = byte_get (nb + 4, 4);
11621 bitmaskwords = byte_get (nb + 8, 4);
11622 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11623 if (is_32bit_elf)
11624 buckets_vma += bitmaskwords * 4;
11625 else
11626 buckets_vma += bitmaskwords * 8;
11628 if (fseek64 (filedata->handle,
11629 (filedata->archive_file_offset
11630 + offset_from_vma (filedata, buckets_vma, 4)),
11631 SEEK_SET))
11633 error (_("Unable to seek to start of dynamic information\n"));
11634 goto no_gnu_hash;
11637 filedata->gnubuckets
11638 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11640 if (filedata->gnubuckets == NULL)
11641 goto no_gnu_hash;
11643 for (i = 0; i < filedata->ngnubuckets; i++)
11644 if (filedata->gnubuckets[i] != 0)
11646 if (filedata->gnubuckets[i] < filedata->gnusymidx)
11647 goto no_gnu_hash;
11649 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11650 maxchain = filedata->gnubuckets[i];
11653 if (maxchain == 0xffffffff)
11654 goto no_gnu_hash;
11656 maxchain -= filedata->gnusymidx;
11658 if (fseek64 (filedata->handle,
11659 (filedata->archive_file_offset
11660 + offset_from_vma (filedata,
11661 buckets_vma + 4 * (filedata->ngnubuckets
11662 + maxchain),
11663 4)),
11664 SEEK_SET))
11666 error (_("Unable to seek to start of dynamic information\n"));
11667 goto no_gnu_hash;
11672 if (fread (nb, 4, 1, filedata->handle) != 1)
11674 error (_("Failed to determine last chain length\n"));
11675 goto no_gnu_hash;
11678 if (maxchain + 1 == 0)
11679 goto no_gnu_hash;
11681 ++maxchain;
11683 while ((byte_get (nb, 4) & 1) == 0);
11685 if (fseek64 (filedata->handle,
11686 (filedata->archive_file_offset
11687 + offset_from_vma (filedata, (buckets_vma
11688 + 4 * filedata->ngnubuckets),
11689 4)),
11690 SEEK_SET))
11692 error (_("Unable to seek to start of dynamic information\n"));
11693 goto no_gnu_hash;
11696 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11697 filedata->ngnuchains = maxchain;
11699 if (filedata->gnuchains == NULL)
11700 goto no_gnu_hash;
11702 if (filedata->dynamic_info_DT_MIPS_XHASH)
11704 if (fseek64 (filedata->handle,
11705 (filedata->archive_file_offset
11706 + offset_from_vma (filedata, (buckets_vma
11707 + 4 * (filedata->ngnubuckets
11708 + maxchain)), 4)),
11709 SEEK_SET))
11711 error (_("Unable to seek to start of dynamic information\n"));
11712 goto no_gnu_hash;
11715 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11716 if (filedata->mipsxlat == NULL)
11717 goto no_gnu_hash;
11720 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11721 if (filedata->gnubuckets[hn] != 0)
11723 uint64_t si = filedata->gnubuckets[hn];
11724 uint64_t off = si - filedata->gnusymidx;
11728 if (filedata->dynamic_info_DT_MIPS_XHASH)
11730 if (off < filedata->ngnuchains
11731 && filedata->mipsxlat[off] >= num_of_syms)
11732 num_of_syms = filedata->mipsxlat[off] + 1;
11734 else
11736 if (si >= num_of_syms)
11737 num_of_syms = si + 1;
11739 si++;
11741 while (off < filedata->ngnuchains
11742 && (filedata->gnuchains[off++] & 1) == 0);
11745 if (num_of_syms == 0)
11747 no_gnu_hash:
11748 free (filedata->mipsxlat);
11749 filedata->mipsxlat = NULL;
11750 free (filedata->gnuchains);
11751 filedata->gnuchains = NULL;
11752 free (filedata->gnubuckets);
11753 filedata->gnubuckets = NULL;
11754 filedata->ngnubuckets = 0;
11755 filedata->ngnuchains = 0;
11759 return num_of_syms;
11762 /* Parse and display the contents of the dynamic section. */
11764 static bool
11765 process_dynamic_section (Filedata * filedata)
11767 Elf_Internal_Dyn * entry;
11769 if (filedata->dynamic_size <= 1)
11771 if (do_dynamic)
11773 if (filedata->is_separate)
11774 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11775 filedata->file_name);
11776 else
11777 printf (_("\nThere is no dynamic section in this file.\n"));
11780 return true;
11783 if (!get_dynamic_section (filedata))
11784 return false;
11786 /* Find the appropriate symbol table. */
11787 if (filedata->dynamic_symbols == NULL || do_histogram)
11789 uint64_t num_of_syms;
11791 for (entry = filedata->dynamic_section;
11792 entry < filedata->dynamic_section + filedata->dynamic_nent;
11793 ++entry)
11794 if (entry->d_tag == DT_SYMTAB)
11795 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11796 else if (entry->d_tag == DT_SYMENT)
11797 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11798 else if (entry->d_tag == DT_HASH)
11799 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11800 else if (entry->d_tag == DT_GNU_HASH)
11801 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11802 else if ((filedata->file_header.e_machine == EM_MIPS
11803 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11804 && entry->d_tag == DT_MIPS_XHASH)
11806 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11807 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11810 num_of_syms = get_num_dynamic_syms (filedata);
11812 if (num_of_syms != 0
11813 && filedata->dynamic_symbols == NULL
11814 && filedata->dynamic_info[DT_SYMTAB]
11815 && filedata->dynamic_info[DT_SYMENT])
11817 Elf_Internal_Phdr *seg;
11818 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
11820 if (! get_program_headers (filedata))
11822 error (_("Cannot interpret virtual addresses "
11823 "without program headers.\n"));
11824 return false;
11827 for (seg = filedata->program_headers;
11828 seg < filedata->program_headers + filedata->file_header.e_phnum;
11829 ++seg)
11831 if (seg->p_type != PT_LOAD)
11832 continue;
11834 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11836 /* See PR 21379 for a reproducer. */
11837 error (_("Invalid PT_LOAD entry\n"));
11838 return false;
11841 if (vma >= (seg->p_vaddr & -seg->p_align)
11842 && vma < seg->p_vaddr + seg->p_filesz)
11844 /* Since we do not know how big the symbol table is,
11845 we default to reading in up to the end of PT_LOAD
11846 segment and processing that. This is overkill, I
11847 know, but it should work. */
11848 Elf_Internal_Shdr section;
11849 section.sh_offset = (vma - seg->p_vaddr
11850 + seg->p_offset);
11851 section.sh_size = (num_of_syms
11852 * filedata->dynamic_info[DT_SYMENT]);
11853 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11855 if (do_checks
11856 && filedata->dynamic_symtab_section != NULL
11857 && ((filedata->dynamic_symtab_section->sh_offset
11858 != section.sh_offset)
11859 || (filedata->dynamic_symtab_section->sh_size
11860 != section.sh_size)
11861 || (filedata->dynamic_symtab_section->sh_entsize
11862 != section.sh_entsize)))
11863 warn (_("\
11864 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11866 section.sh_name = filedata->string_table_length;
11867 filedata->dynamic_symbols
11868 = get_elf_symbols (filedata, &section,
11869 &filedata->num_dynamic_syms);
11870 if (filedata->dynamic_symbols == NULL
11871 || filedata->num_dynamic_syms != num_of_syms)
11873 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11874 return false;
11876 break;
11882 /* Similarly find a string table. */
11883 if (filedata->dynamic_strings == NULL)
11884 for (entry = filedata->dynamic_section;
11885 entry < filedata->dynamic_section + filedata->dynamic_nent;
11886 ++entry)
11888 if (entry->d_tag == DT_STRTAB)
11889 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11891 if (entry->d_tag == DT_STRSZ)
11892 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11894 if (filedata->dynamic_info[DT_STRTAB]
11895 && filedata->dynamic_info[DT_STRSZ])
11897 uint64_t offset;
11898 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
11900 offset = offset_from_vma (filedata,
11901 filedata->dynamic_info[DT_STRTAB],
11902 str_tab_len);
11903 if (do_checks
11904 && filedata->dynamic_strtab_section
11905 && ((filedata->dynamic_strtab_section->sh_offset
11906 != (file_ptr) offset)
11907 || (filedata->dynamic_strtab_section->sh_size
11908 != str_tab_len)))
11909 warn (_("\
11910 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11912 filedata->dynamic_strings
11913 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11914 _("dynamic string table"));
11915 if (filedata->dynamic_strings == NULL)
11917 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11918 break;
11921 filedata->dynamic_strings_length = str_tab_len;
11922 break;
11926 /* And find the syminfo section if available. */
11927 if (filedata->dynamic_syminfo == NULL)
11929 uint64_t syminsz = 0;
11931 for (entry = filedata->dynamic_section;
11932 entry < filedata->dynamic_section + filedata->dynamic_nent;
11933 ++entry)
11935 if (entry->d_tag == DT_SYMINENT)
11937 /* Note: these braces are necessary to avoid a syntax
11938 error from the SunOS4 C compiler. */
11939 /* PR binutils/17531: A corrupt file can trigger this test.
11940 So do not use an assert, instead generate an error message. */
11941 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11942 error (_("Bad value (%d) for SYMINENT entry\n"),
11943 (int) entry->d_un.d_val);
11945 else if (entry->d_tag == DT_SYMINSZ)
11946 syminsz = entry->d_un.d_val;
11947 else if (entry->d_tag == DT_SYMINFO)
11948 filedata->dynamic_syminfo_offset
11949 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11952 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11954 Elf_External_Syminfo * extsyminfo;
11955 Elf_External_Syminfo * extsym;
11956 Elf_Internal_Syminfo * syminfo;
11958 /* There is a syminfo section. Read the data. */
11959 extsyminfo = (Elf_External_Syminfo *)
11960 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11961 1, syminsz, _("symbol information"));
11962 if (!extsyminfo)
11963 return false;
11965 if (filedata->dynamic_syminfo != NULL)
11967 error (_("Multiple dynamic symbol information sections found\n"));
11968 free (filedata->dynamic_syminfo);
11970 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11971 if (filedata->dynamic_syminfo == NULL)
11973 error (_("Out of memory allocating %" PRIu64
11974 " bytes for dynamic symbol info\n"),
11975 syminsz);
11976 return false;
11979 filedata->dynamic_syminfo_nent
11980 = syminsz / sizeof (Elf_External_Syminfo);
11981 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
11982 syminfo < (filedata->dynamic_syminfo
11983 + filedata->dynamic_syminfo_nent);
11984 ++syminfo, ++extsym)
11986 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11987 syminfo->si_flags = BYTE_GET (extsym->si_flags);
11990 free (extsyminfo);
11994 if (do_dynamic && filedata->dynamic_addr)
11996 if (filedata->is_separate)
11997 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
11998 "\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
11999 filedata->dynamic_nent),
12000 filedata->file_name,
12001 filedata->dynamic_addr,
12002 filedata->dynamic_nent);
12003 else
12004 printf (ngettext ("\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
12005 "\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
12006 filedata->dynamic_nent),
12007 filedata->dynamic_addr,
12008 filedata->dynamic_nent);
12010 if (do_dynamic)
12011 printf (_(" Tag Type Name/Value\n"));
12013 for (entry = filedata->dynamic_section;
12014 entry < filedata->dynamic_section + filedata->dynamic_nent;
12015 entry++)
12017 if (do_dynamic)
12019 const char * dtype;
12021 putchar (' ');
12022 print_vma (entry->d_tag, FULL_HEX);
12023 dtype = get_dynamic_type (filedata, entry->d_tag);
12024 printf (" (%s)%*s", dtype,
12025 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
12028 switch (entry->d_tag)
12030 case DT_FLAGS:
12031 if (do_dynamic)
12032 print_dynamic_flags (entry->d_un.d_val);
12033 break;
12035 case DT_AUXILIARY:
12036 case DT_FILTER:
12037 case DT_CONFIG:
12038 case DT_DEPAUDIT:
12039 case DT_AUDIT:
12040 if (do_dynamic)
12042 switch (entry->d_tag)
12044 case DT_AUXILIARY:
12045 printf (_("Auxiliary library"));
12046 break;
12048 case DT_FILTER:
12049 printf (_("Filter library"));
12050 break;
12052 case DT_CONFIG:
12053 printf (_("Configuration file"));
12054 break;
12056 case DT_DEPAUDIT:
12057 printf (_("Dependency audit library"));
12058 break;
12060 case DT_AUDIT:
12061 printf (_("Audit library"));
12062 break;
12065 if (valid_dynamic_name (filedata, entry->d_un.d_val))
12066 printf (": [%s]\n",
12067 get_dynamic_name (filedata, entry->d_un.d_val));
12068 else
12070 printf (": ");
12071 print_vma (entry->d_un.d_val, PREFIX_HEX);
12072 putchar ('\n');
12075 break;
12077 case DT_FEATURE:
12078 if (do_dynamic)
12080 printf (_("Flags:"));
12082 if (entry->d_un.d_val == 0)
12083 printf (_(" None\n"));
12084 else
12086 uint64_t val = entry->d_un.d_val;
12088 if (val & DTF_1_PARINIT)
12090 printf (" PARINIT");
12091 val ^= DTF_1_PARINIT;
12093 if (val & DTF_1_CONFEXP)
12095 printf (" CONFEXP");
12096 val ^= DTF_1_CONFEXP;
12098 if (val != 0)
12099 printf (" %" PRIx64, val);
12100 puts ("");
12103 break;
12105 case DT_POSFLAG_1:
12106 if (do_dynamic)
12108 printf (_("Flags:"));
12110 if (entry->d_un.d_val == 0)
12111 printf (_(" None\n"));
12112 else
12114 uint64_t val = entry->d_un.d_val;
12116 if (val & DF_P1_LAZYLOAD)
12118 printf (" LAZYLOAD");
12119 val ^= DF_P1_LAZYLOAD;
12121 if (val & DF_P1_GROUPPERM)
12123 printf (" GROUPPERM");
12124 val ^= DF_P1_GROUPPERM;
12126 if (val != 0)
12127 printf (" %" PRIx64, val);
12128 puts ("");
12131 break;
12133 case DT_FLAGS_1:
12134 if (do_dynamic)
12136 printf (_("Flags:"));
12137 if (entry->d_un.d_val == 0)
12138 printf (_(" None\n"));
12139 else
12141 uint64_t val = entry->d_un.d_val;
12143 if (val & DF_1_NOW)
12145 printf (" NOW");
12146 val ^= DF_1_NOW;
12148 if (val & DF_1_GLOBAL)
12150 printf (" GLOBAL");
12151 val ^= DF_1_GLOBAL;
12153 if (val & DF_1_GROUP)
12155 printf (" GROUP");
12156 val ^= DF_1_GROUP;
12158 if (val & DF_1_NODELETE)
12160 printf (" NODELETE");
12161 val ^= DF_1_NODELETE;
12163 if (val & DF_1_LOADFLTR)
12165 printf (" LOADFLTR");
12166 val ^= DF_1_LOADFLTR;
12168 if (val & DF_1_INITFIRST)
12170 printf (" INITFIRST");
12171 val ^= DF_1_INITFIRST;
12173 if (val & DF_1_NOOPEN)
12175 printf (" NOOPEN");
12176 val ^= DF_1_NOOPEN;
12178 if (val & DF_1_ORIGIN)
12180 printf (" ORIGIN");
12181 val ^= DF_1_ORIGIN;
12183 if (val & DF_1_DIRECT)
12185 printf (" DIRECT");
12186 val ^= DF_1_DIRECT;
12188 if (val & DF_1_TRANS)
12190 printf (" TRANS");
12191 val ^= DF_1_TRANS;
12193 if (val & DF_1_INTERPOSE)
12195 printf (" INTERPOSE");
12196 val ^= DF_1_INTERPOSE;
12198 if (val & DF_1_NODEFLIB)
12200 printf (" NODEFLIB");
12201 val ^= DF_1_NODEFLIB;
12203 if (val & DF_1_NODUMP)
12205 printf (" NODUMP");
12206 val ^= DF_1_NODUMP;
12208 if (val & DF_1_CONFALT)
12210 printf (" CONFALT");
12211 val ^= DF_1_CONFALT;
12213 if (val & DF_1_ENDFILTEE)
12215 printf (" ENDFILTEE");
12216 val ^= DF_1_ENDFILTEE;
12218 if (val & DF_1_DISPRELDNE)
12220 printf (" DISPRELDNE");
12221 val ^= DF_1_DISPRELDNE;
12223 if (val & DF_1_DISPRELPND)
12225 printf (" DISPRELPND");
12226 val ^= DF_1_DISPRELPND;
12228 if (val & DF_1_NODIRECT)
12230 printf (" NODIRECT");
12231 val ^= DF_1_NODIRECT;
12233 if (val & DF_1_IGNMULDEF)
12235 printf (" IGNMULDEF");
12236 val ^= DF_1_IGNMULDEF;
12238 if (val & DF_1_NOKSYMS)
12240 printf (" NOKSYMS");
12241 val ^= DF_1_NOKSYMS;
12243 if (val & DF_1_NOHDR)
12245 printf (" NOHDR");
12246 val ^= DF_1_NOHDR;
12248 if (val & DF_1_EDITED)
12250 printf (" EDITED");
12251 val ^= DF_1_EDITED;
12253 if (val & DF_1_NORELOC)
12255 printf (" NORELOC");
12256 val ^= DF_1_NORELOC;
12258 if (val & DF_1_SYMINTPOSE)
12260 printf (" SYMINTPOSE");
12261 val ^= DF_1_SYMINTPOSE;
12263 if (val & DF_1_GLOBAUDIT)
12265 printf (" GLOBAUDIT");
12266 val ^= DF_1_GLOBAUDIT;
12268 if (val & DF_1_SINGLETON)
12270 printf (" SINGLETON");
12271 val ^= DF_1_SINGLETON;
12273 if (val & DF_1_STUB)
12275 printf (" STUB");
12276 val ^= DF_1_STUB;
12278 if (val & DF_1_PIE)
12280 printf (" PIE");
12281 val ^= DF_1_PIE;
12283 if (val & DF_1_KMOD)
12285 printf (" KMOD");
12286 val ^= DF_1_KMOD;
12288 if (val & DF_1_WEAKFILTER)
12290 printf (" WEAKFILTER");
12291 val ^= DF_1_WEAKFILTER;
12293 if (val & DF_1_NOCOMMON)
12295 printf (" NOCOMMON");
12296 val ^= DF_1_NOCOMMON;
12298 if (val != 0)
12299 printf (" %" PRIx64, val);
12300 puts ("");
12303 break;
12305 case DT_PLTREL:
12306 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
12307 if (do_dynamic)
12308 puts (get_dynamic_type (filedata, entry->d_un.d_val));
12309 break;
12311 case DT_NULL :
12312 case DT_NEEDED :
12313 case DT_PLTGOT :
12314 case DT_HASH :
12315 case DT_STRTAB :
12316 case DT_SYMTAB :
12317 case DT_RELA :
12318 case DT_INIT :
12319 case DT_FINI :
12320 case DT_SONAME :
12321 case DT_RPATH :
12322 case DT_SYMBOLIC:
12323 case DT_REL :
12324 case DT_RELR :
12325 case DT_DEBUG :
12326 case DT_TEXTREL :
12327 case DT_JMPREL :
12328 case DT_RUNPATH :
12329 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
12331 if (do_dynamic)
12333 const char *name;
12335 if (valid_dynamic_name (filedata, entry->d_un.d_val))
12336 name = get_dynamic_name (filedata, entry->d_un.d_val);
12337 else
12338 name = NULL;
12340 if (name)
12342 switch (entry->d_tag)
12344 case DT_NEEDED:
12345 printf (_("Shared library: [%s]"), name);
12347 if (filedata->program_interpreter
12348 && streq (name, filedata->program_interpreter))
12349 printf (_(" program interpreter"));
12350 break;
12352 case DT_SONAME:
12353 printf (_("Library soname: [%s]"), name);
12354 break;
12356 case DT_RPATH:
12357 printf (_("Library rpath: [%s]"), name);
12358 break;
12360 case DT_RUNPATH:
12361 printf (_("Library runpath: [%s]"), name);
12362 break;
12364 default:
12365 print_vma (entry->d_un.d_val, PREFIX_HEX);
12366 break;
12369 else
12370 print_vma (entry->d_un.d_val, PREFIX_HEX);
12372 putchar ('\n');
12374 break;
12376 case DT_PLTRELSZ:
12377 case DT_RELASZ :
12378 case DT_STRSZ :
12379 case DT_RELSZ :
12380 case DT_RELAENT :
12381 case DT_RELRENT :
12382 case DT_RELRSZ :
12383 case DT_SYMENT :
12384 case DT_RELENT :
12385 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
12386 /* Fall through. */
12387 case DT_PLTPADSZ:
12388 case DT_MOVEENT :
12389 case DT_MOVESZ :
12390 case DT_PREINIT_ARRAYSZ:
12391 case DT_INIT_ARRAYSZ:
12392 case DT_FINI_ARRAYSZ:
12393 case DT_GNU_CONFLICTSZ:
12394 case DT_GNU_LIBLISTSZ:
12395 if (do_dynamic)
12397 print_vma (entry->d_un.d_val, UNSIGNED);
12398 printf (_(" (bytes)\n"));
12400 break;
12402 case DT_VERDEFNUM:
12403 case DT_VERNEEDNUM:
12404 case DT_RELACOUNT:
12405 case DT_RELCOUNT:
12406 if (do_dynamic)
12408 print_vma (entry->d_un.d_val, UNSIGNED);
12409 putchar ('\n');
12411 break;
12413 case DT_SYMINSZ:
12414 case DT_SYMINENT:
12415 case DT_SYMINFO:
12416 case DT_USED:
12417 case DT_INIT_ARRAY:
12418 case DT_FINI_ARRAY:
12419 if (do_dynamic)
12421 if (entry->d_tag == DT_USED
12422 && valid_dynamic_name (filedata, entry->d_un.d_val))
12424 const char *name
12425 = get_dynamic_name (filedata, entry->d_un.d_val);
12427 if (*name)
12429 printf (_("Not needed object: [%s]\n"), name);
12430 break;
12434 print_vma (entry->d_un.d_val, PREFIX_HEX);
12435 putchar ('\n');
12437 break;
12439 case DT_BIND_NOW:
12440 /* The value of this entry is ignored. */
12441 if (do_dynamic)
12442 putchar ('\n');
12443 break;
12445 case DT_GNU_PRELINKED:
12446 if (do_dynamic)
12448 struct tm * tmp;
12449 time_t atime = entry->d_un.d_val;
12451 tmp = gmtime (&atime);
12452 /* PR 17533 file: 041-1244816-0.004. */
12453 if (tmp == NULL)
12454 printf (_("<corrupt time val: %" PRIx64),
12455 (uint64_t) atime);
12456 else
12457 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
12458 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
12459 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
12462 break;
12464 case DT_GNU_HASH:
12465 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12466 if (do_dynamic)
12468 print_vma (entry->d_un.d_val, PREFIX_HEX);
12469 putchar ('\n');
12471 break;
12473 case DT_GNU_FLAGS_1:
12474 if (do_dynamic)
12476 printf (_("Flags:"));
12477 if (entry->d_un.d_val == 0)
12478 printf (_(" None\n"));
12479 else
12481 uint64_t val = entry->d_un.d_val;
12483 if (val & DF_GNU_1_UNIQUE)
12485 printf (" UNIQUE");
12486 val ^= DF_GNU_1_UNIQUE;
12488 if (val != 0)
12489 printf (" %" PRIx64, val);
12490 puts ("");
12493 break;
12495 default:
12496 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
12497 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
12498 = entry->d_un.d_val;
12500 if (do_dynamic)
12502 switch (filedata->file_header.e_machine)
12504 case EM_AARCH64:
12505 dynamic_section_aarch64_val (entry);
12506 break;
12507 case EM_MIPS:
12508 case EM_MIPS_RS3_LE:
12509 dynamic_section_mips_val (filedata, entry);
12510 break;
12511 case EM_PARISC:
12512 dynamic_section_parisc_val (entry);
12513 break;
12514 case EM_IA_64:
12515 dynamic_section_ia64_val (entry);
12516 break;
12517 default:
12518 print_vma (entry->d_un.d_val, PREFIX_HEX);
12519 putchar ('\n');
12522 break;
12526 return true;
12529 static char *
12530 get_ver_flags (unsigned int flags)
12532 static char buff[128];
12534 buff[0] = 0;
12536 if (flags == 0)
12537 return _("none");
12539 if (flags & VER_FLG_BASE)
12540 strcat (buff, "BASE");
12542 if (flags & VER_FLG_WEAK)
12544 if (flags & VER_FLG_BASE)
12545 strcat (buff, " | ");
12547 strcat (buff, "WEAK");
12550 if (flags & VER_FLG_INFO)
12552 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12553 strcat (buff, " | ");
12555 strcat (buff, "INFO");
12558 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12560 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12561 strcat (buff, " | ");
12563 strcat (buff, _("<unknown>"));
12566 return buff;
12569 /* Display the contents of the version sections. */
12571 static bool
12572 process_version_sections (Filedata * filedata)
12574 Elf_Internal_Shdr * section;
12575 unsigned i;
12576 bool found = false;
12578 if (! do_version)
12579 return true;
12581 for (i = 0, section = filedata->section_headers;
12582 i < filedata->file_header.e_shnum;
12583 i++, section++)
12585 switch (section->sh_type)
12587 case SHT_GNU_verdef:
12589 Elf_External_Verdef * edefs;
12590 size_t idx;
12591 size_t cnt;
12592 char * endbuf;
12594 found = true;
12596 if (filedata->is_separate)
12597 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12598 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12599 section->sh_info),
12600 filedata->file_name,
12601 printable_section_name (filedata, section),
12602 section->sh_info);
12603 else
12604 printf (ngettext ("\nVersion definition section '%s' "
12605 "contains %u entry:\n",
12606 "\nVersion definition section '%s' "
12607 "contains %u entries:\n",
12608 section->sh_info),
12609 printable_section_name (filedata, section),
12610 section->sh_info);
12612 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12613 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12614 section->sh_offset, section->sh_link,
12615 printable_section_name_from_index (filedata, section->sh_link, NULL));
12617 edefs = (Elf_External_Verdef *)
12618 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12619 _("version definition section"));
12620 if (!edefs)
12621 break;
12622 endbuf = (char *) edefs + section->sh_size;
12624 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12626 char * vstart;
12627 Elf_External_Verdef * edef;
12628 Elf_Internal_Verdef ent;
12629 Elf_External_Verdaux * eaux;
12630 Elf_Internal_Verdaux aux;
12631 size_t isum;
12632 int j;
12634 vstart = ((char *) edefs) + idx;
12635 if (vstart + sizeof (*edef) > endbuf)
12636 break;
12638 edef = (Elf_External_Verdef *) vstart;
12640 ent.vd_version = BYTE_GET (edef->vd_version);
12641 ent.vd_flags = BYTE_GET (edef->vd_flags);
12642 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12643 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12644 ent.vd_hash = BYTE_GET (edef->vd_hash);
12645 ent.vd_aux = BYTE_GET (edef->vd_aux);
12646 ent.vd_next = BYTE_GET (edef->vd_next);
12648 printf (_(" %#06zx: Rev: %d Flags: %s"),
12649 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12651 printf (_(" Index: %d Cnt: %d "),
12652 ent.vd_ndx, ent.vd_cnt);
12654 /* Check for overflow. */
12655 if (ent.vd_aux > (size_t) (endbuf - vstart))
12656 break;
12658 vstart += ent.vd_aux;
12660 if (vstart + sizeof (*eaux) > endbuf)
12661 break;
12662 eaux = (Elf_External_Verdaux *) vstart;
12664 aux.vda_name = BYTE_GET (eaux->vda_name);
12665 aux.vda_next = BYTE_GET (eaux->vda_next);
12667 if (valid_dynamic_name (filedata, aux.vda_name))
12668 printf (_("Name: %s\n"),
12669 get_dynamic_name (filedata, aux.vda_name));
12670 else
12671 printf (_("Name index: %ld\n"), aux.vda_name);
12673 isum = idx + ent.vd_aux;
12675 for (j = 1; j < ent.vd_cnt; j++)
12677 if (aux.vda_next < sizeof (*eaux)
12678 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12680 warn (_("Invalid vda_next field of %lx\n"),
12681 aux.vda_next);
12682 j = ent.vd_cnt;
12683 break;
12685 /* Check for overflow. */
12686 if (aux.vda_next > (size_t) (endbuf - vstart))
12687 break;
12689 isum += aux.vda_next;
12690 vstart += aux.vda_next;
12692 if (vstart + sizeof (*eaux) > endbuf)
12693 break;
12694 eaux = (Elf_External_Verdaux *) vstart;
12696 aux.vda_name = BYTE_GET (eaux->vda_name);
12697 aux.vda_next = BYTE_GET (eaux->vda_next);
12699 if (valid_dynamic_name (filedata, aux.vda_name))
12700 printf (_(" %#06zx: Parent %d: %s\n"),
12701 isum, j,
12702 get_dynamic_name (filedata, aux.vda_name));
12703 else
12704 printf (_(" %#06zx: Parent %d, name index: %ld\n"),
12705 isum, j, aux.vda_name);
12708 if (j < ent.vd_cnt)
12709 printf (_(" Version def aux past end of section\n"));
12711 /* PR 17531:
12712 file: id:000001,src:000172+005151,op:splice,rep:2. */
12713 if (ent.vd_next < sizeof (*edef)
12714 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12716 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12717 cnt = section->sh_info;
12718 break;
12720 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12721 break;
12723 idx += ent.vd_next;
12726 if (cnt < section->sh_info)
12727 printf (_(" Version definition past end of section\n"));
12729 free (edefs);
12731 break;
12733 case SHT_GNU_verneed:
12735 Elf_External_Verneed * eneed;
12736 size_t idx;
12737 size_t cnt;
12738 char * endbuf;
12740 found = true;
12742 if (filedata->is_separate)
12743 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12744 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12745 section->sh_info),
12746 filedata->file_name,
12747 printable_section_name (filedata, section),
12748 section->sh_info);
12749 else
12750 printf (ngettext ("\nVersion needs section '%s' "
12751 "contains %u entry:\n",
12752 "\nVersion needs section '%s' "
12753 "contains %u entries:\n",
12754 section->sh_info),
12755 printable_section_name (filedata, section),
12756 section->sh_info);
12758 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12759 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12760 section->sh_offset, section->sh_link,
12761 printable_section_name_from_index (filedata, section->sh_link, NULL));
12763 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12764 section->sh_offset, 1,
12765 section->sh_size,
12766 _("Version Needs section"));
12767 if (!eneed)
12768 break;
12769 endbuf = (char *) eneed + section->sh_size;
12771 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12773 Elf_External_Verneed * entry;
12774 Elf_Internal_Verneed ent;
12775 size_t isum;
12776 int j;
12777 char * vstart;
12779 vstart = ((char *) eneed) + idx;
12780 if (vstart + sizeof (*entry) > endbuf)
12781 break;
12783 entry = (Elf_External_Verneed *) vstart;
12785 ent.vn_version = BYTE_GET (entry->vn_version);
12786 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12787 ent.vn_file = BYTE_GET (entry->vn_file);
12788 ent.vn_aux = BYTE_GET (entry->vn_aux);
12789 ent.vn_next = BYTE_GET (entry->vn_next);
12791 printf (_(" %#06zx: Version: %d"), idx, ent.vn_version);
12793 if (valid_dynamic_name (filedata, ent.vn_file))
12794 printf (_(" File: %s"),
12795 get_dynamic_name (filedata, ent.vn_file));
12796 else
12797 printf (_(" File: %lx"), ent.vn_file);
12799 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12801 /* Check for overflow. */
12802 if (ent.vn_aux > (size_t) (endbuf - vstart))
12803 break;
12804 vstart += ent.vn_aux;
12806 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12808 Elf_External_Vernaux * eaux;
12809 Elf_Internal_Vernaux aux;
12811 if (vstart + sizeof (*eaux) > endbuf)
12812 break;
12813 eaux = (Elf_External_Vernaux *) vstart;
12815 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12816 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12817 aux.vna_other = BYTE_GET (eaux->vna_other);
12818 aux.vna_name = BYTE_GET (eaux->vna_name);
12819 aux.vna_next = BYTE_GET (eaux->vna_next);
12821 if (valid_dynamic_name (filedata, aux.vna_name))
12822 printf (_(" %#06zx: Name: %s"),
12823 isum, get_dynamic_name (filedata, aux.vna_name));
12824 else
12825 printf (_(" %#06zx: Name index: %lx"),
12826 isum, aux.vna_name);
12828 printf (_(" Flags: %s Version: %d\n"),
12829 get_ver_flags (aux.vna_flags), aux.vna_other);
12831 if (aux.vna_next < sizeof (*eaux)
12832 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12834 warn (_("Invalid vna_next field of %lx\n"),
12835 aux.vna_next);
12836 j = ent.vn_cnt;
12837 break;
12839 /* Check for overflow. */
12840 if (aux.vna_next > (size_t) (endbuf - vstart))
12841 break;
12842 isum += aux.vna_next;
12843 vstart += aux.vna_next;
12846 if (j < ent.vn_cnt)
12847 warn (_("Missing Version Needs auxiliary information\n"));
12849 if (ent.vn_next < sizeof (*entry)
12850 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12852 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12853 cnt = section->sh_info;
12854 break;
12856 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12857 break;
12858 idx += ent.vn_next;
12861 if (cnt < section->sh_info)
12862 warn (_("Missing Version Needs information\n"));
12864 free (eneed);
12866 break;
12868 case SHT_GNU_versym:
12870 Elf_Internal_Shdr * link_section;
12871 uint64_t total;
12872 unsigned int cnt;
12873 unsigned char * edata;
12874 unsigned short * data;
12875 char * strtab;
12876 Elf_Internal_Sym * symbols;
12877 Elf_Internal_Shdr * string_sec;
12878 uint64_t num_syms;
12879 uint64_t off;
12881 if (section->sh_link >= filedata->file_header.e_shnum)
12882 break;
12884 link_section = filedata->section_headers + section->sh_link;
12885 total = section->sh_size / sizeof (Elf_External_Versym);
12887 if (link_section->sh_link >= filedata->file_header.e_shnum)
12888 break;
12890 found = true;
12892 symbols = get_elf_symbols (filedata, link_section, & num_syms);
12893 if (symbols == NULL)
12894 break;
12896 string_sec = filedata->section_headers + link_section->sh_link;
12898 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12899 string_sec->sh_size,
12900 _("version string table"));
12901 if (!strtab)
12903 free (symbols);
12904 break;
12907 if (filedata->is_separate)
12908 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entry:\n",
12909 "\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entries:\n",
12910 total),
12911 filedata->file_name,
12912 printable_section_name (filedata, section),
12913 total);
12914 else
12915 printf (ngettext ("\nVersion symbols section '%s' "
12916 "contains %" PRIu64 " entry:\n",
12917 "\nVersion symbols section '%s' "
12918 "contains %" PRIu64 " entries:\n",
12919 total),
12920 printable_section_name (filedata, section),
12921 total);
12923 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12924 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12925 section->sh_offset, section->sh_link,
12926 printable_section_name (filedata, link_section));
12928 off = offset_from_vma (filedata,
12929 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12930 total * sizeof (short));
12931 edata = (unsigned char *) get_data (NULL, filedata, off,
12932 sizeof (short), total,
12933 _("version symbol data"));
12934 if (!edata)
12936 free (strtab);
12937 free (symbols);
12938 break;
12941 data = (short unsigned int *) cmalloc (total, sizeof (short));
12943 for (cnt = total; cnt --;)
12944 data[cnt] = byte_get (edata + cnt * sizeof (short),
12945 sizeof (short));
12947 free (edata);
12949 for (cnt = 0; cnt < total; cnt += 4)
12951 int j, nn;
12952 char *name;
12953 char *invalid = _("*invalid*");
12955 printf (" %03x:", cnt);
12957 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12958 switch (data[cnt + j])
12960 case 0:
12961 fputs (_(" 0 (*local*) "), stdout);
12962 break;
12964 case 1:
12965 fputs (_(" 1 (*global*) "), stdout);
12966 break;
12968 default:
12969 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12970 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12972 /* If this index value is greater than the size of the symbols
12973 array, break to avoid an out-of-bounds read. */
12974 if (cnt + j >= num_syms)
12976 warn (_("invalid index into symbol array\n"));
12977 break;
12980 name = NULL;
12981 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12983 Elf_Internal_Verneed ivn;
12984 uint64_t offset;
12986 offset = offset_from_vma
12987 (filedata,
12988 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12989 sizeof (Elf_External_Verneed));
12993 Elf_Internal_Vernaux ivna;
12994 Elf_External_Verneed evn;
12995 Elf_External_Vernaux evna;
12996 uint64_t a_off;
12998 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12999 _("version need")) == NULL)
13000 break;
13002 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13003 ivn.vn_next = BYTE_GET (evn.vn_next);
13005 a_off = offset + ivn.vn_aux;
13009 if (get_data (&evna, filedata, a_off, sizeof (evna),
13010 1, _("version need aux (2)")) == NULL)
13012 ivna.vna_next = 0;
13013 ivna.vna_other = 0;
13015 else
13017 ivna.vna_next = BYTE_GET (evna.vna_next);
13018 ivna.vna_other = BYTE_GET (evna.vna_other);
13021 a_off += ivna.vna_next;
13023 while (ivna.vna_other != data[cnt + j]
13024 && ivna.vna_next != 0);
13026 if (ivna.vna_other == data[cnt + j])
13028 ivna.vna_name = BYTE_GET (evna.vna_name);
13030 if (ivna.vna_name >= string_sec->sh_size)
13031 name = invalid;
13032 else
13033 name = strtab + ivna.vna_name;
13034 break;
13037 offset += ivn.vn_next;
13039 while (ivn.vn_next);
13042 if (data[cnt + j] != 0x8001
13043 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13045 Elf_Internal_Verdef ivd;
13046 Elf_External_Verdef evd;
13047 uint64_t offset;
13049 offset = offset_from_vma
13050 (filedata,
13051 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13052 sizeof evd);
13056 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
13057 _("version def")) == NULL)
13059 ivd.vd_next = 0;
13060 /* PR 17531: file: 046-1082287-0.004. */
13061 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
13062 break;
13064 else
13066 ivd.vd_next = BYTE_GET (evd.vd_next);
13067 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13070 offset += ivd.vd_next;
13072 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
13073 && ivd.vd_next != 0);
13075 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
13077 Elf_External_Verdaux evda;
13078 Elf_Internal_Verdaux ivda;
13080 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13082 if (get_data (&evda, filedata,
13083 offset - ivd.vd_next + ivd.vd_aux,
13084 sizeof (evda), 1,
13085 _("version def aux")) == NULL)
13086 break;
13088 ivda.vda_name = BYTE_GET (evda.vda_name);
13090 if (ivda.vda_name >= string_sec->sh_size)
13091 name = invalid;
13092 else if (name != NULL && name != invalid)
13093 name = _("*both*");
13094 else
13095 name = strtab + ivda.vda_name;
13098 if (name != NULL)
13099 nn += printf ("(%s%-*s",
13100 name,
13101 12 - (int) strlen (name),
13102 ")");
13104 if (nn < 18)
13105 printf ("%*c", 18 - nn, ' ');
13108 putchar ('\n');
13111 free (data);
13112 free (strtab);
13113 free (symbols);
13115 break;
13117 default:
13118 break;
13122 if (! found)
13124 if (filedata->is_separate)
13125 printf (_("\nNo version information found in linked file '%s'.\n"),
13126 filedata->file_name);
13127 else
13128 printf (_("\nNo version information found in this file.\n"));
13131 return true;
13134 static const char *
13135 get_symbol_binding (Filedata * filedata, unsigned int binding)
13137 static char buff[64];
13139 switch (binding)
13141 case STB_LOCAL: return "LOCAL";
13142 case STB_GLOBAL: return "GLOBAL";
13143 case STB_WEAK: return "WEAK";
13144 default:
13145 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
13146 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
13147 binding);
13148 else if (binding >= STB_LOOS && binding <= STB_HIOS)
13150 if (binding == STB_GNU_UNIQUE
13151 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
13152 return "UNIQUE";
13153 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
13155 else
13156 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
13157 return buff;
13161 static const char *
13162 get_symbol_type (Filedata * filedata, unsigned int type)
13164 static char buff[64];
13166 switch (type)
13168 case STT_NOTYPE: return "NOTYPE";
13169 case STT_OBJECT: return "OBJECT";
13170 case STT_FUNC: return "FUNC";
13171 case STT_SECTION: return "SECTION";
13172 case STT_FILE: return "FILE";
13173 case STT_COMMON: return "COMMON";
13174 case STT_TLS: return "TLS";
13175 case STT_RELC: return "RELC";
13176 case STT_SRELC: return "SRELC";
13177 default:
13178 if (type >= STT_LOPROC && type <= STT_HIPROC)
13180 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
13181 return "THUMB_FUNC";
13183 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
13184 return "REGISTER";
13186 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
13187 return "PARISC_MILLI";
13189 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
13191 else if (type >= STT_LOOS && type <= STT_HIOS)
13193 if (filedata->file_header.e_machine == EM_PARISC)
13195 if (type == STT_HP_OPAQUE)
13196 return "HP_OPAQUE";
13197 if (type == STT_HP_STUB)
13198 return "HP_STUB";
13201 if (type == STT_GNU_IFUNC
13202 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
13203 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
13204 return "IFUNC";
13206 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
13208 else
13209 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
13210 return buff;
13214 static const char *
13215 get_symbol_visibility (unsigned int visibility)
13217 switch (visibility)
13219 case STV_DEFAULT: return "DEFAULT";
13220 case STV_INTERNAL: return "INTERNAL";
13221 case STV_HIDDEN: return "HIDDEN";
13222 case STV_PROTECTED: return "PROTECTED";
13223 default:
13224 error (_("Unrecognized visibility value: %u\n"), visibility);
13225 return _("<unknown>");
13229 static const char *
13230 get_alpha_symbol_other (unsigned int other)
13232 switch (other)
13234 case STO_ALPHA_NOPV: return "NOPV";
13235 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
13236 default:
13237 error (_("Unrecognized alpha specific other value: %u\n"), other);
13238 return _("<unknown>");
13242 static const char *
13243 get_solaris_symbol_visibility (unsigned int visibility)
13245 switch (visibility)
13247 case 4: return "EXPORTED";
13248 case 5: return "SINGLETON";
13249 case 6: return "ELIMINATE";
13250 default: return get_symbol_visibility (visibility);
13254 static const char *
13255 get_aarch64_symbol_other (unsigned int other)
13257 static char buf[32];
13259 if (other & STO_AARCH64_VARIANT_PCS)
13261 other &= ~STO_AARCH64_VARIANT_PCS;
13262 if (other == 0)
13263 return "VARIANT_PCS";
13264 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
13265 return buf;
13267 return NULL;
13270 static const char *
13271 get_mips_symbol_other (unsigned int other)
13273 switch (other)
13275 case STO_OPTIONAL: return "OPTIONAL";
13276 case STO_MIPS_PLT: return "MIPS PLT";
13277 case STO_MIPS_PIC: return "MIPS PIC";
13278 case STO_MICROMIPS: return "MICROMIPS";
13279 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
13280 case STO_MIPS16: return "MIPS16";
13281 default: return NULL;
13285 static const char *
13286 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
13288 if (is_ia64_vms (filedata))
13290 static char res[32];
13292 res[0] = 0;
13294 /* Function types is for images and .STB files only. */
13295 switch (filedata->file_header.e_type)
13297 case ET_DYN:
13298 case ET_EXEC:
13299 switch (VMS_ST_FUNC_TYPE (other))
13301 case VMS_SFT_CODE_ADDR:
13302 strcat (res, " CA");
13303 break;
13304 case VMS_SFT_SYMV_IDX:
13305 strcat (res, " VEC");
13306 break;
13307 case VMS_SFT_FD:
13308 strcat (res, " FD");
13309 break;
13310 case VMS_SFT_RESERVE:
13311 strcat (res, " RSV");
13312 break;
13313 default:
13314 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
13315 VMS_ST_FUNC_TYPE (other));
13316 strcat (res, " <unknown>");
13317 break;
13319 break;
13320 default:
13321 break;
13323 switch (VMS_ST_LINKAGE (other))
13325 case VMS_STL_IGNORE:
13326 strcat (res, " IGN");
13327 break;
13328 case VMS_STL_RESERVE:
13329 strcat (res, " RSV");
13330 break;
13331 case VMS_STL_STD:
13332 strcat (res, " STD");
13333 break;
13334 case VMS_STL_LNK:
13335 strcat (res, " LNK");
13336 break;
13337 default:
13338 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
13339 VMS_ST_LINKAGE (other));
13340 strcat (res, " <unknown>");
13341 break;
13344 if (res[0] != 0)
13345 return res + 1;
13346 else
13347 return res;
13349 return NULL;
13352 static const char *
13353 get_ppc64_symbol_other (unsigned int other)
13355 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
13356 return NULL;
13358 other >>= STO_PPC64_LOCAL_BIT;
13359 if (other <= 6)
13361 static char buf[64];
13362 if (other >= 2)
13363 other = ppc64_decode_local_entry (other);
13364 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
13365 return buf;
13367 return NULL;
13370 static const char *
13371 get_riscv_symbol_other (unsigned int other)
13373 static char buf[32];
13374 buf[0] = 0;
13376 if (other & STO_RISCV_VARIANT_CC)
13378 strcat (buf, _(" VARIANT_CC"));
13379 other &= ~STO_RISCV_VARIANT_CC;
13382 if (other != 0)
13383 snprintf (buf, sizeof buf, " %x", other);
13386 if (buf[0] != 0)
13387 return buf + 1;
13388 else
13389 return buf;
13392 static const char *
13393 get_symbol_other (Filedata * filedata, unsigned int other)
13395 const char * result = NULL;
13396 static char buff [64];
13398 if (other == 0)
13399 return "";
13401 switch (filedata->file_header.e_machine)
13403 case EM_ALPHA:
13404 result = get_alpha_symbol_other (other);
13405 break;
13406 case EM_AARCH64:
13407 result = get_aarch64_symbol_other (other);
13408 break;
13409 case EM_MIPS:
13410 result = get_mips_symbol_other (other);
13411 break;
13412 case EM_IA_64:
13413 result = get_ia64_symbol_other (filedata, other);
13414 break;
13415 case EM_PPC64:
13416 result = get_ppc64_symbol_other (other);
13417 break;
13418 case EM_RISCV:
13419 result = get_riscv_symbol_other (other);
13420 break;
13421 default:
13422 result = NULL;
13423 break;
13426 if (result)
13427 return result;
13429 snprintf (buff, sizeof buff, _("<other>: %x"), other);
13430 return buff;
13433 static const char *
13434 get_symbol_version_string (Filedata *filedata,
13435 bool is_dynsym,
13436 const char *strtab,
13437 size_t strtab_size,
13438 unsigned int si,
13439 Elf_Internal_Sym *psym,
13440 enum versioned_symbol_info *sym_info,
13441 unsigned short *vna_other)
13443 unsigned char data[2];
13444 unsigned short vers_data;
13445 uint64_t offset;
13446 unsigned short max_vd_ndx;
13448 if (!is_dynsym
13449 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
13450 return NULL;
13452 offset = offset_from_vma (filedata,
13453 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
13454 sizeof data + si * sizeof (vers_data));
13456 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
13457 sizeof (data), 1, _("version data")) == NULL)
13458 return NULL;
13460 vers_data = byte_get (data, 2);
13462 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
13463 return NULL;
13465 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
13466 max_vd_ndx = 0;
13468 /* Usually we'd only see verdef for defined symbols, and verneed for
13469 undefined symbols. However, symbols defined by the linker in
13470 .dynbss for variables copied from a shared library in order to
13471 avoid text relocations are defined yet have verneed. We could
13472 use a heuristic to detect the special case, for example, check
13473 for verneed first on symbols defined in SHT_NOBITS sections, but
13474 it is simpler and more reliable to just look for both verdef and
13475 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
13477 if (psym->st_shndx != SHN_UNDEF
13478 && vers_data != 0x8001
13479 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13481 Elf_Internal_Verdef ivd;
13482 Elf_Internal_Verdaux ivda;
13483 Elf_External_Verdaux evda;
13484 uint64_t off;
13486 off = offset_from_vma (filedata,
13487 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13488 sizeof (Elf_External_Verdef));
13492 Elf_External_Verdef evd;
13494 if (get_data (&evd, filedata, off, sizeof (evd), 1,
13495 _("version def")) == NULL)
13497 ivd.vd_ndx = 0;
13498 ivd.vd_aux = 0;
13499 ivd.vd_next = 0;
13500 ivd.vd_flags = 0;
13502 else
13504 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13505 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13506 ivd.vd_next = BYTE_GET (evd.vd_next);
13507 ivd.vd_flags = BYTE_GET (evd.vd_flags);
13510 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13511 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13513 off += ivd.vd_next;
13515 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13517 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13519 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13520 return NULL;
13522 off -= ivd.vd_next;
13523 off += ivd.vd_aux;
13525 if (get_data (&evda, filedata, off, sizeof (evda), 1,
13526 _("version def aux")) != NULL)
13528 ivda.vda_name = BYTE_GET (evda.vda_name);
13530 if (psym->st_name != ivda.vda_name)
13531 return (ivda.vda_name < strtab_size
13532 ? strtab + ivda.vda_name : _("<corrupt>"));
13537 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13539 Elf_External_Verneed evn;
13540 Elf_Internal_Verneed ivn;
13541 Elf_Internal_Vernaux ivna;
13543 offset = offset_from_vma (filedata,
13544 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13545 sizeof evn);
13548 uint64_t vna_off;
13550 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13551 _("version need")) == NULL)
13553 ivna.vna_next = 0;
13554 ivna.vna_other = 0;
13555 ivna.vna_name = 0;
13556 break;
13559 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13560 ivn.vn_next = BYTE_GET (evn.vn_next);
13562 vna_off = offset + ivn.vn_aux;
13566 Elf_External_Vernaux evna;
13568 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13569 _("version need aux (3)")) == NULL)
13571 ivna.vna_next = 0;
13572 ivna.vna_other = 0;
13573 ivna.vna_name = 0;
13575 else
13577 ivna.vna_other = BYTE_GET (evna.vna_other);
13578 ivna.vna_next = BYTE_GET (evna.vna_next);
13579 ivna.vna_name = BYTE_GET (evna.vna_name);
13582 vna_off += ivna.vna_next;
13584 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13586 if (ivna.vna_other == vers_data)
13587 break;
13589 offset += ivn.vn_next;
13591 while (ivn.vn_next != 0);
13593 if (ivna.vna_other == vers_data)
13595 *sym_info = symbol_undefined;
13596 *vna_other = ivna.vna_other;
13597 return (ivna.vna_name < strtab_size
13598 ? strtab + ivna.vna_name : _("<corrupt>"));
13600 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13601 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13602 return _("<corrupt>");
13604 return NULL;
13607 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
13609 static unsigned int
13610 print_symbol_size (uint64_t vma, int base)
13612 switch (base)
13614 case 8:
13615 return print_vma (vma, OCTAL_5);
13617 case 10:
13618 return print_vma (vma, UNSIGNED_5);
13620 case 16:
13621 return print_vma (vma, PREFIX_HEX_5);
13623 case 0:
13624 default:
13625 return print_vma (vma, DEC_5);
13629 /* Print information on a single symbol. */
13631 static void
13632 print_symbol (Filedata * filedata,
13633 uint64_t symbol_index,
13634 Elf_Internal_Sym * symtab,
13635 Elf_Internal_Shdr * section,
13636 char * strtab,
13637 size_t strtab_size)
13639 const char *version_string;
13640 enum versioned_symbol_info sym_info;
13641 unsigned short vna_other;
13642 const char * sstr;
13643 Elf_Internal_Sym *psym = symtab + symbol_index;
13645 /* FIXME: We should have a table of field widths,
13646 rather than using hard coded constants. */
13647 printf ("%6" PRId64 ": ", symbol_index);
13648 print_vma (psym->st_value, LONG_HEX);
13649 putchar (' ');
13650 print_symbol_size (psym->st_size, sym_base);
13651 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13652 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13653 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13654 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13655 else
13657 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13659 printf (" %-7s", get_symbol_visibility (vis));
13661 /* Check to see if any other bits in the st_other field are set.
13662 FIXME: Displaying this information here disrupts the layout
13663 of the table being generated. */
13664 if (psym->st_other ^ vis)
13665 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13668 bool is_special;
13670 sstr = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
13672 /* Print the symbol's section index. If the index is special
13673 then print the index's name rather than its number. */
13674 if (is_special)
13676 int printed;
13678 /* Special case: If there are no section headers, and the printable
13679 name is "<section 0x...." then just display the section number
13680 as a decimal. This happens when objcopy --strip -section-headers
13681 is used. */
13682 if (filedata->file_header.e_shnum == 0 && startswith (sstr, "<section"))
13683 printed = printf (" %4d ", psym->st_shndx);
13684 else
13685 printed = printf (" %4s ", sstr);
13687 if (extra_sym_info && printed < 16)
13688 printf ("%*s", 16 - printed, "");
13690 else
13692 printf (" %4u ", psym->st_shndx);
13694 if (extra_sym_info)
13696 /* Display the section name referenced by the section index. */
13697 int printed = printf ("(%s) ", sstr);
13698 if (printed < 10)
13699 printf ("%*s", 10 - printed, "");
13703 /* Get the symbol's name. For section symbols without a
13704 specific name use the (already computed) section name. */
13705 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13706 && section_index_real (filedata, psym->st_shndx)
13707 && psym->st_name == 0)
13711 else
13713 bool is_valid;
13715 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13716 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13719 version_string
13720 = get_symbol_version_string (filedata,
13721 (section == NULL
13722 || section->sh_type == SHT_DYNSYM),
13723 strtab, strtab_size, symbol_index,
13724 psym, &sym_info, &vna_other);
13726 int len_avail = 21;
13727 if (! do_wide && version_string != NULL)
13729 char buffer[16];
13731 len_avail -= 1 + strlen (version_string);
13733 if (sym_info == symbol_undefined)
13734 len_avail -= sprintf (buffer," (%d)", vna_other);
13735 else if (sym_info != symbol_hidden)
13736 len_avail -= 1;
13739 print_symbol_name (len_avail, sstr);
13741 if (version_string)
13743 if (sym_info == symbol_undefined)
13744 printf ("@%s (%d)", version_string, vna_other);
13745 else
13746 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13747 version_string);
13750 putchar ('\n');
13752 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13753 && section != NULL
13754 && symbol_index >= section->sh_info
13755 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13756 && filedata->file_header.e_machine != EM_MIPS
13757 /* Solaris binaries have been found to violate this requirement as
13758 well. Not sure if this is a bug or an ABI requirement. */
13759 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13760 warn (_("local symbol %" PRIu64 " found at index >= %s's sh_info value of %u\n"),
13761 symbol_index, printable_section_name (filedata, section), section->sh_info);
13764 static const char *
13765 get_lto_kind (unsigned int kind)
13767 switch (kind)
13769 case 0: return "DEF";
13770 case 1: return "WEAKDEF";
13771 case 2: return "UNDEF";
13772 case 3: return "WEAKUNDEF";
13773 case 4: return "COMMON";
13774 default:
13775 break;
13778 static char buffer[30];
13779 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13780 sprintf (buffer, "<unknown: %u>", kind);
13781 return buffer;
13784 static const char *
13785 get_lto_visibility (unsigned int visibility)
13787 switch (visibility)
13789 case 0: return "DEFAULT";
13790 case 1: return "PROTECTED";
13791 case 2: return "INTERNAL";
13792 case 3: return "HIDDEN";
13793 default:
13794 break;
13797 static char buffer[30];
13798 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13799 sprintf (buffer, "<unknown: %u>", visibility);
13800 return buffer;
13803 static const char *
13804 get_lto_sym_type (unsigned int sym_type)
13806 switch (sym_type)
13808 case 0: return "UNKNOWN";
13809 case 1: return "FUNCTION";
13810 case 2: return "VARIABLE";
13811 default:
13812 break;
13815 static char buffer[30];
13816 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13817 sprintf (buffer, "<unknown: %u>", sym_type);
13818 return buffer;
13821 /* Display an LTO format symbol table.
13822 FIXME: The format of LTO symbol tables is not formalized.
13823 So this code could need changing in the future. */
13825 static bool
13826 display_lto_symtab (Filedata * filedata,
13827 Elf_Internal_Shdr * section)
13829 if (section->sh_size == 0)
13831 if (filedata->is_separate)
13832 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13833 printable_section_name (filedata, section),
13834 filedata->file_name);
13835 else
13836 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13837 printable_section_name (filedata, section));
13839 return true;
13842 if (section->sh_size > filedata->file_size)
13844 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
13845 printable_section_name (filedata, section),
13846 section->sh_size);
13847 return false;
13850 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13851 section->sh_size, 1, _("LTO symbols"));
13852 if (alloced_data == NULL)
13853 return false;
13855 /* Look for extended data for the symbol table. */
13856 Elf_Internal_Shdr * ext;
13857 void * ext_data_orig = NULL;
13858 char * ext_data = NULL;
13859 char * ext_data_end = NULL;
13860 char * ext_name = NULL;
13862 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13863 (section_name (filedata, section)
13864 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13865 && ext_name != NULL /* Paranoia. */
13866 && (ext = find_section (filedata, ext_name)) != NULL)
13868 if (ext->sh_size < 3)
13869 error (_("LTO Symbol extension table '%s' is empty!\n"),
13870 printable_section_name (filedata, ext));
13871 else
13873 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13874 ext->sh_size, 1,
13875 _("LTO ext symbol data"));
13876 if (ext_data != NULL)
13878 ext_data_end = ext_data + ext->sh_size;
13879 if (* ext_data++ != 1)
13880 error (_("Unexpected version number in symbol extension table\n"));
13885 const unsigned char * data = (const unsigned char *) alloced_data;
13886 const unsigned char * end = data + section->sh_size;
13888 if (filedata->is_separate)
13889 printf (_("\nIn linked file '%s': "), filedata->file_name);
13890 else
13891 printf ("\n");
13893 if (ext_data_orig != NULL)
13895 if (do_wide)
13896 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13897 printable_section_name (filedata, section),
13898 printable_section_name (filedata, ext));
13899 else
13901 printf (_("LTO Symbol table '%s'\n"),
13902 printable_section_name (filedata, section));
13903 printf (_(" and extension table '%s' contain:\n"),
13904 printable_section_name (filedata, ext));
13907 else
13908 printf (_("LTO Symbol table '%s' contains:\n"),
13909 printable_section_name (filedata, section));
13911 /* FIXME: Add a wide version. */
13912 if (ext_data_orig != NULL)
13913 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13914 else
13915 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13917 /* FIXME: We do not handle style prefixes. */
13919 while (data < end)
13921 const unsigned char * sym_name = data;
13922 data += strnlen ((const char *) sym_name, end - data) + 1;
13923 if (data >= end)
13924 goto fail;
13926 const unsigned char * comdat_key = data;
13927 data += strnlen ((const char *) comdat_key, end - data) + 1;
13928 if (data >= end)
13929 goto fail;
13931 if (data + 2 + 8 + 4 > end)
13932 goto fail;
13934 unsigned int kind = *data++;
13935 unsigned int visibility = *data++;
13937 uint64_t size = byte_get (data, 8);
13938 data += 8;
13940 uint64_t slot = byte_get (data, 4);
13941 data += 4;
13943 if (ext_data != NULL)
13945 if (ext_data < (ext_data_end - 1))
13947 unsigned int sym_type = * ext_data ++;
13948 unsigned int sec_kind = * ext_data ++;
13950 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
13951 * comdat_key == 0 ? "-" : (char *) comdat_key,
13952 get_lto_kind (kind),
13953 get_lto_visibility (visibility),
13954 size,
13955 slot,
13956 get_lto_sym_type (sym_type),
13957 sec_kind);
13958 print_symbol_name (6, (const char *) sym_name);
13960 else
13962 error (_("Ran out of LTO symbol extension data\n"));
13963 ext_data = NULL;
13964 /* FIXME: return FAIL result ? */
13967 else
13969 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
13970 * comdat_key == 0 ? "-" : (char *) comdat_key,
13971 get_lto_kind (kind),
13972 get_lto_visibility (visibility),
13973 size,
13974 slot);
13975 print_symbol_name (21, (const char *) sym_name);
13977 putchar ('\n');
13980 if (ext_data != NULL && ext_data < ext_data_end)
13982 error (_("Data remains in the LTO symbol extension table\n"));
13983 goto fail;
13986 free (alloced_data);
13987 free (ext_data_orig);
13988 free (ext_name);
13989 return true;
13991 fail:
13992 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13993 free (alloced_data);
13994 free (ext_data_orig);
13995 free (ext_name);
13996 return false;
13999 /* Display LTO symbol tables. */
14001 static bool
14002 process_lto_symbol_tables (Filedata * filedata)
14004 Elf_Internal_Shdr * section;
14005 unsigned int i;
14006 bool res = true;
14008 if (!do_lto_syms)
14009 return true;
14011 if (filedata->section_headers == NULL)
14012 return true;
14014 for (i = 0, section = filedata->section_headers;
14015 i < filedata->file_header.e_shnum;
14016 i++, section++)
14017 if (section_name_valid (filedata, section)
14018 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
14019 res &= display_lto_symtab (filedata, section);
14021 return res;
14024 static void
14025 print_symbol_table_heading (void)
14027 /* FIXME: We should store the size of each field in the display in a table and
14028 then use the values inside print_symbol(), instead of that function using
14029 hard coded constants. */
14030 if (is_32bit_elf)
14032 if (extra_sym_info)
14034 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
14035 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |---8--| |----13.....| |........... */
14036 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 (.text) get_sections */
14038 else if (do_wide)
14040 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14041 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
14042 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
14044 else
14046 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14047 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |------------29-------------| */
14048 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
14051 else
14053 if (extra_sym_info)
14055 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
14056 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-------14---| |..... */
14057 /* eg: 2: 0000000000000000 0 FUNC LOCAL DEFAULT 1 (.text) .very_long_function_name */
14060 else if (do_wide)
14062 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14063 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
14064 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_function_name */
14066 else
14068 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14069 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |--------21---------| */
14070 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_functi[...] */
14075 /* Dump the symbol table. */
14077 static bool
14078 process_symbol_table (Filedata * filedata)
14080 Elf_Internal_Shdr * section;
14082 if (!do_syms && !do_dyn_syms && !do_histogram)
14083 return true;
14085 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
14086 && do_syms
14087 && do_using_dynamic
14088 && filedata->dynamic_strings != NULL
14089 && filedata->dynamic_symbols != NULL)
14091 uint64_t si;
14093 if (filedata->is_separate)
14095 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table"
14096 " contains %" PRIu64 " entry:\n",
14097 "\nIn linked file '%s' the dynamic symbol table"
14098 " contains %" PRIu64 " entries:\n",
14099 filedata->num_dynamic_syms),
14100 filedata->file_name,
14101 filedata->num_dynamic_syms);
14103 else
14105 printf (ngettext ("\nSymbol table for image contains %" PRIu64
14106 " entry:\n",
14107 "\nSymbol table for image contains %" PRIu64
14108 " entries:\n",
14109 filedata->num_dynamic_syms),
14110 filedata->num_dynamic_syms);
14113 print_symbol_table_heading ();
14115 for (si = 0; si < filedata->num_dynamic_syms; si++)
14116 print_symbol (filedata, si, filedata->dynamic_symbols, NULL,
14117 filedata->dynamic_strings,
14118 filedata->dynamic_strings_length);
14120 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
14121 && filedata->section_headers != NULL)
14123 unsigned int i;
14125 for (i = 0, section = filedata->section_headers;
14126 i < filedata->file_header.e_shnum;
14127 i++, section++)
14129 char * strtab = NULL;
14130 uint64_t strtab_size = 0;
14131 Elf_Internal_Sym * symtab;
14132 uint64_t si, num_syms;
14134 if ((section->sh_type != SHT_SYMTAB
14135 && section->sh_type != SHT_DYNSYM)
14136 || (!do_syms
14137 && section->sh_type == SHT_SYMTAB))
14138 continue;
14140 if (section->sh_entsize == 0)
14142 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
14143 printable_section_name (filedata, section));
14144 continue;
14147 num_syms = section->sh_size / section->sh_entsize;
14149 if (filedata->is_separate)
14150 printf (ngettext ("\nIn linked file '%s' symbol section '%s'"
14151 " contains %" PRIu64 " entry:\n",
14152 "\nIn linked file '%s' symbol section '%s'"
14153 " contains %" PRIu64 " entries:\n",
14154 num_syms),
14155 filedata->file_name,
14156 printable_section_name (filedata, section),
14157 num_syms);
14158 else
14159 printf (ngettext ("\nSymbol table '%s' contains %" PRIu64
14160 " entry:\n",
14161 "\nSymbol table '%s' contains %" PRIu64
14162 " entries:\n",
14163 num_syms),
14164 printable_section_name (filedata, section),
14165 num_syms);
14167 print_symbol_table_heading ();
14169 symtab = get_elf_symbols (filedata, section, & num_syms);
14170 if (symtab == NULL)
14171 continue;
14173 if (section->sh_link == filedata->file_header.e_shstrndx)
14175 strtab = filedata->string_table;
14176 strtab_size = filedata->string_table_length;
14178 else if (section->sh_link < filedata->file_header.e_shnum)
14180 Elf_Internal_Shdr * string_sec;
14182 string_sec = filedata->section_headers + section->sh_link;
14184 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
14185 1, string_sec->sh_size,
14186 _("string table"));
14187 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
14190 for (si = 0; si < num_syms; si++)
14191 print_symbol (filedata, si, symtab, section,
14192 strtab, strtab_size);
14194 free (symtab);
14195 if (strtab != filedata->string_table)
14196 free (strtab);
14199 else if (do_syms)
14200 printf
14201 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
14203 if (do_histogram && filedata->buckets != NULL)
14205 uint64_t *lengths;
14206 uint64_t *counts;
14207 uint64_t hn;
14208 uint64_t si;
14209 uint64_t maxlength = 0;
14210 uint64_t nzero_counts = 0;
14211 uint64_t nsyms = 0;
14212 char *visited;
14214 printf (ngettext ("\nHistogram for bucket list length "
14215 "(total of %" PRIu64 " bucket):\n",
14216 "\nHistogram for bucket list length "
14217 "(total of %" PRIu64 " buckets):\n",
14218 filedata->nbuckets),
14219 filedata->nbuckets);
14221 lengths = calloc (filedata->nbuckets, sizeof (*lengths));
14222 if (lengths == NULL)
14224 error (_("Out of memory allocating space for histogram buckets\n"));
14225 goto err_out;
14227 visited = xcmalloc (filedata->nchains, 1);
14228 memset (visited, 0, filedata->nchains);
14230 printf (_(" Length Number %% of total Coverage\n"));
14231 for (hn = 0; hn < filedata->nbuckets; ++hn)
14233 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
14235 ++nsyms;
14236 if (maxlength < ++lengths[hn])
14237 ++maxlength;
14238 if (si >= filedata->nchains || visited[si])
14240 error (_("histogram chain is corrupt\n"));
14241 break;
14243 visited[si] = 1;
14246 free (visited);
14248 counts = calloc (maxlength + 1, sizeof (*counts));
14249 if (counts == NULL)
14251 free (lengths);
14252 error (_("Out of memory allocating space for histogram counts\n"));
14253 goto err_out;
14256 for (hn = 0; hn < filedata->nbuckets; ++hn)
14257 ++counts[lengths[hn]];
14259 if (filedata->nbuckets > 0)
14261 uint64_t i;
14262 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
14263 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
14264 for (i = 1; i <= maxlength; ++i)
14266 nzero_counts += counts[i] * i;
14267 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
14268 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
14269 (nzero_counts * 100.0) / nsyms);
14273 free (counts);
14274 free (lengths);
14277 free (filedata->buckets);
14278 filedata->buckets = NULL;
14279 filedata->nbuckets = 0;
14280 free (filedata->chains);
14281 filedata->chains = NULL;
14283 if (do_histogram && filedata->gnubuckets != NULL)
14285 uint64_t *lengths;
14286 uint64_t *counts;
14287 uint64_t hn;
14288 uint64_t maxlength = 0;
14289 uint64_t nzero_counts = 0;
14290 uint64_t nsyms = 0;
14292 printf (ngettext ("\nHistogram for `%s' bucket list length "
14293 "(total of %" PRIu64 " bucket):\n",
14294 "\nHistogram for `%s' bucket list length "
14295 "(total of %" PRIu64 " buckets):\n",
14296 filedata->ngnubuckets),
14297 GNU_HASH_SECTION_NAME (filedata),
14298 filedata->ngnubuckets);
14300 lengths = calloc (filedata->ngnubuckets, sizeof (*lengths));
14301 if (lengths == NULL)
14303 error (_("Out of memory allocating space for gnu histogram buckets\n"));
14304 goto err_out;
14307 printf (_(" Length Number %% of total Coverage\n"));
14309 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
14310 if (filedata->gnubuckets[hn] != 0)
14312 uint64_t off, length = 1;
14314 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
14315 /* PR 17531 file: 010-77222-0.004. */
14316 off < filedata->ngnuchains
14317 && (filedata->gnuchains[off] & 1) == 0;
14318 ++off)
14319 ++length;
14320 lengths[hn] = length;
14321 if (length > maxlength)
14322 maxlength = length;
14323 nsyms += length;
14326 counts = calloc (maxlength + 1, sizeof (*counts));
14327 if (counts == NULL)
14329 free (lengths);
14330 error (_("Out of memory allocating space for gnu histogram counts\n"));
14331 goto err_out;
14334 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
14335 ++counts[lengths[hn]];
14337 if (filedata->ngnubuckets > 0)
14339 uint64_t j;
14340 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
14341 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
14342 for (j = 1; j <= maxlength; ++j)
14344 nzero_counts += counts[j] * j;
14345 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
14346 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
14347 (nzero_counts * 100.0) / nsyms);
14351 free (counts);
14352 free (lengths);
14354 free (filedata->gnubuckets);
14355 filedata->gnubuckets = NULL;
14356 filedata->ngnubuckets = 0;
14357 free (filedata->gnuchains);
14358 filedata->gnuchains = NULL;
14359 filedata->ngnuchains = 0;
14360 free (filedata->mipsxlat);
14361 filedata->mipsxlat = NULL;
14362 return true;
14364 err_out:
14365 free (filedata->gnubuckets);
14366 filedata->gnubuckets = NULL;
14367 filedata->ngnubuckets = 0;
14368 free (filedata->gnuchains);
14369 filedata->gnuchains = NULL;
14370 filedata->ngnuchains = 0;
14371 free (filedata->mipsxlat);
14372 filedata->mipsxlat = NULL;
14373 free (filedata->buckets);
14374 filedata->buckets = NULL;
14375 filedata->nbuckets = 0;
14376 free (filedata->chains);
14377 filedata->chains = NULL;
14378 return false;
14381 static bool
14382 process_syminfo (Filedata * filedata)
14384 unsigned int i;
14386 if (filedata->dynamic_syminfo == NULL
14387 || !do_dynamic)
14388 /* No syminfo, this is ok. */
14389 return true;
14391 /* There better should be a dynamic symbol section. */
14392 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
14393 return false;
14395 if (filedata->is_separate)
14396 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entry:\n",
14397 "\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entries:\n",
14398 filedata->dynamic_syminfo_nent),
14399 filedata->file_name,
14400 filedata->dynamic_syminfo_offset,
14401 filedata->dynamic_syminfo_nent);
14402 else
14403 printf (ngettext ("\nDynamic info segment at offset %#" PRIx64
14404 " contains %d entry:\n",
14405 "\nDynamic info segment at offset %#" PRIx64
14406 " contains %d entries:\n",
14407 filedata->dynamic_syminfo_nent),
14408 filedata->dynamic_syminfo_offset,
14409 filedata->dynamic_syminfo_nent);
14411 printf (_(" Num: Name BoundTo Flags\n"));
14412 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
14414 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
14416 printf ("%4d: ", i);
14417 if (i >= filedata->num_dynamic_syms)
14418 printf (_("<corrupt index>"));
14419 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
14420 print_symbol_name (30, get_dynamic_name (filedata,
14421 filedata->dynamic_symbols[i].st_name));
14422 else
14423 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
14424 putchar (' ');
14426 switch (filedata->dynamic_syminfo[i].si_boundto)
14428 case SYMINFO_BT_SELF:
14429 fputs ("SELF ", stdout);
14430 break;
14431 case SYMINFO_BT_PARENT:
14432 fputs ("PARENT ", stdout);
14433 break;
14434 default:
14435 if (filedata->dynamic_syminfo[i].si_boundto > 0
14436 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
14437 && valid_dynamic_name (filedata,
14438 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
14440 print_symbol_name (10, get_dynamic_name (filedata,
14441 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
14442 putchar (' ' );
14444 else
14445 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
14446 break;
14449 if (flags & SYMINFO_FLG_DIRECT)
14450 printf (" DIRECT");
14451 if (flags & SYMINFO_FLG_PASSTHRU)
14452 printf (" PASSTHRU");
14453 if (flags & SYMINFO_FLG_COPY)
14454 printf (" COPY");
14455 if (flags & SYMINFO_FLG_LAZYLOAD)
14456 printf (" LAZYLOAD");
14458 puts ("");
14461 return true;
14464 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
14465 is contained by the region START .. END. The types of ADDR, START
14466 and END should all be the same. Note both ADDR + NELEM and END
14467 point to just beyond the end of the regions that are being tested. */
14468 #define IN_RANGE(START,END,ADDR,NELEM) \
14469 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
14471 /* Check to see if the given reloc needs to be handled in a target specific
14472 manner. If so then process the reloc and return TRUE otherwise return
14473 FALSE.
14475 If called with reloc == NULL, then this is a signal that reloc processing
14476 for the current section has finished, and any saved state should be
14477 discarded. */
14479 static bool
14480 target_specific_reloc_handling (Filedata *filedata,
14481 Elf_Internal_Rela *reloc,
14482 unsigned char *start,
14483 unsigned char *end,
14484 Elf_Internal_Sym *symtab,
14485 uint64_t num_syms)
14487 unsigned int reloc_type = 0;
14488 uint64_t sym_index = 0;
14490 if (reloc)
14492 reloc_type = get_reloc_type (filedata, reloc->r_info);
14493 sym_index = get_reloc_symindex (reloc->r_info);
14496 switch (filedata->file_header.e_machine)
14498 case EM_LOONGARCH:
14500 switch (reloc_type)
14502 /* For .uleb128 .LFE1-.LFB1, loongarch write 0 to object file
14503 at assembly time. */
14504 case 107: /* R_LARCH_ADD_ULEB128. */
14505 case 108: /* R_LARCH_SUB_ULEB128. */
14507 uint64_t value = 0;
14508 unsigned int reloc_size = 0;
14509 int leb_ret = 0;
14511 if (reloc->r_offset < (size_t) (end - start))
14512 value = read_leb128 (start + reloc->r_offset, end, false,
14513 &reloc_size, &leb_ret);
14514 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14515 error (_("LoongArch ULEB128 field at 0x%lx contains invalid "
14516 "ULEB128 value\n"),
14517 (long) reloc->r_offset);
14519 else if (sym_index >= num_syms)
14520 error (_("%s reloc contains invalid symbol index "
14521 "%" PRIu64 "\n"),
14522 (reloc_type == 107
14523 ? "R_LARCH_ADD_ULEB128"
14524 : "R_LARCH_SUB_ULEB128"),
14525 sym_index);
14526 else
14528 if (reloc_type == 107)
14529 value += reloc->r_addend + symtab[sym_index].st_value;
14530 else
14531 value -= reloc->r_addend + symtab[sym_index].st_value;
14533 /* Write uleb128 value to p. */
14534 bfd_byte *p = start + reloc->r_offset;
14537 bfd_byte c = value & 0x7f;
14538 value >>= 7;
14539 if (--reloc_size != 0)
14540 c |= 0x80;
14541 *p++ = c;
14543 while (reloc_size);
14546 return true;
14549 break;
14552 case EM_MSP430:
14553 case EM_MSP430_OLD:
14555 static Elf_Internal_Sym * saved_sym = NULL;
14557 if (reloc == NULL)
14559 saved_sym = NULL;
14560 return true;
14563 switch (reloc_type)
14565 case 10: /* R_MSP430_SYM_DIFF */
14566 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
14567 if (uses_msp430x_relocs (filedata))
14568 break;
14569 /* Fall through. */
14570 case 21: /* R_MSP430X_SYM_DIFF */
14571 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
14572 /* PR 21139. */
14573 if (sym_index >= num_syms)
14574 error (_("%s reloc contains invalid symbol index "
14575 "%" PRIu64 "\n"), "MSP430 SYM_DIFF", sym_index);
14576 else
14577 saved_sym = symtab + sym_index;
14578 return true;
14580 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14581 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
14582 goto handle_sym_diff;
14584 case 5: /* R_MSP430_16_BYTE */
14585 case 9: /* R_MSP430_8 */
14586 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14587 if (uses_msp430x_relocs (filedata))
14588 break;
14589 goto handle_sym_diff;
14591 case 2: /* R_MSP430_ABS16 */
14592 case 15: /* R_MSP430X_ABS16 */
14593 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14594 if (! uses_msp430x_relocs (filedata))
14595 break;
14596 goto handle_sym_diff;
14598 handle_sym_diff:
14599 if (saved_sym != NULL)
14601 uint64_t value;
14602 unsigned int reloc_size = 0;
14603 int leb_ret = 0;
14604 switch (reloc_type)
14606 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14607 reloc_size = 4;
14608 break;
14609 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14610 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14611 if (reloc->r_offset < (size_t) (end - start))
14612 read_leb128 (start + reloc->r_offset, end, false,
14613 &reloc_size, &leb_ret);
14614 break;
14615 default:
14616 reloc_size = 2;
14617 break;
14620 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14621 error (_("MSP430 ULEB128 field at %#" PRIx64
14622 " contains invalid ULEB128 value\n"),
14623 reloc->r_offset);
14624 else if (sym_index >= num_syms)
14625 error (_("%s reloc contains invalid symbol index "
14626 "%" PRIu64 "\n"), "MSP430", sym_index);
14627 else
14629 value = reloc->r_addend + (symtab[sym_index].st_value
14630 - saved_sym->st_value);
14632 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14633 byte_put (start + reloc->r_offset, value, reloc_size);
14634 else
14635 /* PR 21137 */
14636 error (_("MSP430 sym diff reloc contains invalid offset: "
14637 "%#" PRIx64 "\n"),
14638 reloc->r_offset);
14641 saved_sym = NULL;
14642 return true;
14644 break;
14646 default:
14647 if (saved_sym != NULL)
14648 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14649 break;
14651 break;
14654 case EM_MN10300:
14655 case EM_CYGNUS_MN10300:
14657 static Elf_Internal_Sym * saved_sym = NULL;
14659 if (reloc == NULL)
14661 saved_sym = NULL;
14662 return true;
14665 switch (reloc_type)
14667 case 34: /* R_MN10300_ALIGN */
14668 return true;
14669 case 33: /* R_MN10300_SYM_DIFF */
14670 if (sym_index >= num_syms)
14671 error (_("%s reloc contains invalid symbol index "
14672 "%" PRIu64 "\n"), "MN10300_SYM_DIFF", sym_index);
14673 else
14674 saved_sym = symtab + sym_index;
14675 return true;
14677 case 1: /* R_MN10300_32 */
14678 case 2: /* R_MN10300_16 */
14679 if (saved_sym != NULL)
14681 int reloc_size = reloc_type == 1 ? 4 : 2;
14682 uint64_t value;
14684 if (sym_index >= num_syms)
14685 error (_("%s reloc contains invalid symbol index "
14686 "%" PRIu64 "\n"), "MN10300", sym_index);
14687 else
14689 value = reloc->r_addend + (symtab[sym_index].st_value
14690 - saved_sym->st_value);
14692 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14693 byte_put (start + reloc->r_offset, value, reloc_size);
14694 else
14695 error (_("MN10300 sym diff reloc contains invalid offset:"
14696 " %#" PRIx64 "\n"),
14697 reloc->r_offset);
14700 saved_sym = NULL;
14701 return true;
14703 break;
14704 default:
14705 if (saved_sym != NULL)
14706 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
14707 break;
14709 break;
14712 case EM_RL78:
14714 static uint64_t saved_sym1 = 0;
14715 static uint64_t saved_sym2 = 0;
14716 static uint64_t value;
14718 if (reloc == NULL)
14720 saved_sym1 = saved_sym2 = 0;
14721 return true;
14724 switch (reloc_type)
14726 case 0x80: /* R_RL78_SYM. */
14727 saved_sym1 = saved_sym2;
14728 if (sym_index >= num_syms)
14729 error (_("%s reloc contains invalid symbol index "
14730 "%" PRIu64 "\n"), "RL78_SYM", sym_index);
14731 else
14733 saved_sym2 = symtab[sym_index].st_value;
14734 saved_sym2 += reloc->r_addend;
14736 return true;
14738 case 0x83: /* R_RL78_OPsub. */
14739 value = saved_sym1 - saved_sym2;
14740 saved_sym2 = saved_sym1 = 0;
14741 return true;
14742 break;
14744 case 0x41: /* R_RL78_ABS32. */
14745 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
14746 byte_put (start + reloc->r_offset, value, 4);
14747 else
14748 error (_("RL78 sym diff reloc contains invalid offset: "
14749 "%#" PRIx64 "\n"),
14750 reloc->r_offset);
14751 value = 0;
14752 return true;
14754 case 0x43: /* R_RL78_ABS16. */
14755 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
14756 byte_put (start + reloc->r_offset, value, 2);
14757 else
14758 error (_("RL78 sym diff reloc contains invalid offset: "
14759 "%#" PRIx64 "\n"),
14760 reloc->r_offset);
14761 value = 0;
14762 return true;
14764 default:
14765 break;
14767 break;
14771 return false;
14774 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14775 DWARF debug sections. This is a target specific test. Note - we do not
14776 go through the whole including-target-headers-multiple-times route, (as
14777 we have already done with <elf/h8.h>) because this would become very
14778 messy and even then this function would have to contain target specific
14779 information (the names of the relocs instead of their numeric values).
14780 FIXME: This is not the correct way to solve this problem. The proper way
14781 is to have target specific reloc sizing and typing functions created by
14782 the reloc-macros.h header, in the same way that it already creates the
14783 reloc naming functions. */
14785 static bool
14786 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14788 /* Please keep this table alpha-sorted for ease of visual lookup. */
14789 switch (filedata->file_header.e_machine)
14791 case EM_386:
14792 case EM_IAMCU:
14793 return reloc_type == 1; /* R_386_32. */
14794 case EM_68K:
14795 return reloc_type == 1; /* R_68K_32. */
14796 case EM_860:
14797 return reloc_type == 1; /* R_860_32. */
14798 case EM_960:
14799 return reloc_type == 2; /* R_960_32. */
14800 case EM_AARCH64:
14801 return (reloc_type == 258
14802 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
14803 case EM_BPF:
14804 return reloc_type == 11; /* R_BPF_DATA_32 */
14805 case EM_ADAPTEVA_EPIPHANY:
14806 return reloc_type == 3;
14807 case EM_ALPHA:
14808 return reloc_type == 1; /* R_ALPHA_REFLONG. */
14809 case EM_ARC:
14810 return reloc_type == 1; /* R_ARC_32. */
14811 case EM_ARC_COMPACT:
14812 case EM_ARC_COMPACT2:
14813 case EM_ARC_COMPACT3:
14814 case EM_ARC_COMPACT3_64:
14815 return reloc_type == 4; /* R_ARC_32. */
14816 case EM_ARM:
14817 return reloc_type == 2; /* R_ARM_ABS32 */
14818 case EM_AVR_OLD:
14819 case EM_AVR:
14820 return reloc_type == 1;
14821 case EM_BLACKFIN:
14822 return reloc_type == 0x12; /* R_byte4_data. */
14823 case EM_CRIS:
14824 return reloc_type == 3; /* R_CRIS_32. */
14825 case EM_CR16:
14826 return reloc_type == 3; /* R_CR16_NUM32. */
14827 case EM_CRX:
14828 return reloc_type == 15; /* R_CRX_NUM32. */
14829 case EM_CSKY:
14830 return reloc_type == 1; /* R_CKCORE_ADDR32. */
14831 case EM_CYGNUS_FRV:
14832 return reloc_type == 1;
14833 case EM_CYGNUS_D10V:
14834 case EM_D10V:
14835 return reloc_type == 6; /* R_D10V_32. */
14836 case EM_CYGNUS_D30V:
14837 case EM_D30V:
14838 return reloc_type == 12; /* R_D30V_32_NORMAL. */
14839 case EM_DLX:
14840 return reloc_type == 3; /* R_DLX_RELOC_32. */
14841 case EM_CYGNUS_FR30:
14842 case EM_FR30:
14843 return reloc_type == 3; /* R_FR30_32. */
14844 case EM_FT32:
14845 return reloc_type == 1; /* R_FT32_32. */
14846 case EM_H8S:
14847 case EM_H8_300:
14848 case EM_H8_300H:
14849 return reloc_type == 1; /* R_H8_DIR32. */
14850 case EM_IA_64:
14851 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14852 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14853 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14854 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
14855 case EM_IP2K_OLD:
14856 case EM_IP2K:
14857 return reloc_type == 2; /* R_IP2K_32. */
14858 case EM_IQ2000:
14859 return reloc_type == 2; /* R_IQ2000_32. */
14860 case EM_KVX:
14861 return reloc_type == 2; /* R_KVX_32. */
14862 case EM_LATTICEMICO32:
14863 return reloc_type == 3; /* R_LM32_32. */
14864 case EM_LOONGARCH:
14865 return reloc_type == 1; /* R_LARCH_32. */
14866 case EM_M32C_OLD:
14867 case EM_M32C:
14868 return reloc_type == 3; /* R_M32C_32. */
14869 case EM_M32R:
14870 return reloc_type == 34; /* R_M32R_32_RELA. */
14871 case EM_68HC11:
14872 case EM_68HC12:
14873 return reloc_type == 6; /* R_M68HC11_32. */
14874 case EM_S12Z:
14875 return reloc_type == 7 || /* R_S12Z_EXT32 */
14876 reloc_type == 6; /* R_S12Z_CW32. */
14877 case EM_MCORE:
14878 return reloc_type == 1; /* R_MCORE_ADDR32. */
14879 case EM_CYGNUS_MEP:
14880 return reloc_type == 4; /* R_MEP_32. */
14881 case EM_METAG:
14882 return reloc_type == 2; /* R_METAG_ADDR32. */
14883 case EM_MICROBLAZE:
14884 return reloc_type == 1; /* R_MICROBLAZE_32. */
14885 case EM_MIPS:
14886 return reloc_type == 2; /* R_MIPS_32. */
14887 case EM_MMIX:
14888 return reloc_type == 4; /* R_MMIX_32. */
14889 case EM_CYGNUS_MN10200:
14890 case EM_MN10200:
14891 return reloc_type == 1; /* R_MN10200_32. */
14892 case EM_CYGNUS_MN10300:
14893 case EM_MN10300:
14894 return reloc_type == 1; /* R_MN10300_32. */
14895 case EM_MOXIE:
14896 return reloc_type == 1; /* R_MOXIE_32. */
14897 case EM_MSP430_OLD:
14898 case EM_MSP430:
14899 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
14900 case EM_MT:
14901 return reloc_type == 2; /* R_MT_32. */
14902 case EM_NDS32:
14903 return reloc_type == 20; /* R_NDS32_32_RELA. */
14904 case EM_ALTERA_NIOS2:
14905 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
14906 case EM_NIOS32:
14907 return reloc_type == 1; /* R_NIOS_32. */
14908 case EM_OR1K:
14909 return reloc_type == 1; /* R_OR1K_32. */
14910 case EM_PARISC:
14911 return (reloc_type == 1 /* R_PARISC_DIR32. */
14912 || reloc_type == 2 /* R_PARISC_DIR21L. */
14913 || reloc_type == 41); /* R_PARISC_SECREL32. */
14914 case EM_PJ:
14915 case EM_PJ_OLD:
14916 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14917 case EM_PPC64:
14918 return reloc_type == 1; /* R_PPC64_ADDR32. */
14919 case EM_PPC:
14920 return reloc_type == 1; /* R_PPC_ADDR32. */
14921 case EM_TI_PRU:
14922 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
14923 case EM_RISCV:
14924 return reloc_type == 1; /* R_RISCV_32. */
14925 case EM_RL78:
14926 return reloc_type == 1; /* R_RL78_DIR32. */
14927 case EM_RX:
14928 return reloc_type == 1; /* R_RX_DIR32. */
14929 case EM_S370:
14930 return reloc_type == 1; /* R_I370_ADDR31. */
14931 case EM_S390_OLD:
14932 case EM_S390:
14933 return reloc_type == 4; /* R_S390_32. */
14934 case EM_SCORE:
14935 return reloc_type == 8; /* R_SCORE_ABS32. */
14936 case EM_SH:
14937 return reloc_type == 1; /* R_SH_DIR32. */
14938 case EM_SPARC32PLUS:
14939 case EM_SPARCV9:
14940 case EM_SPARC:
14941 return reloc_type == 3 /* R_SPARC_32. */
14942 || reloc_type == 23; /* R_SPARC_UA32. */
14943 case EM_SPU:
14944 return reloc_type == 6; /* R_SPU_ADDR32 */
14945 case EM_TI_C6000:
14946 return reloc_type == 1; /* R_C6000_ABS32. */
14947 case EM_TILEGX:
14948 return reloc_type == 2; /* R_TILEGX_32. */
14949 case EM_TILEPRO:
14950 return reloc_type == 1; /* R_TILEPRO_32. */
14951 case EM_CYGNUS_V850:
14952 case EM_V850:
14953 return reloc_type == 6; /* R_V850_ABS32. */
14954 case EM_V800:
14955 return reloc_type == 0x33; /* R_V810_WORD. */
14956 case EM_VAX:
14957 return reloc_type == 1; /* R_VAX_32. */
14958 case EM_VISIUM:
14959 return reloc_type == 3; /* R_VISIUM_32. */
14960 case EM_WEBASSEMBLY:
14961 return reloc_type == 1; /* R_WASM32_32. */
14962 case EM_X86_64:
14963 case EM_L1OM:
14964 case EM_K1OM:
14965 return reloc_type == 10; /* R_X86_64_32. */
14966 case EM_XGATE:
14967 return reloc_type == 4; /* R_XGATE_32. */
14968 case EM_XSTORMY16:
14969 return reloc_type == 1; /* R_XSTROMY16_32. */
14970 case EM_XTENSA_OLD:
14971 case EM_XTENSA:
14972 return reloc_type == 1; /* R_XTENSA_32. */
14973 case EM_Z80:
14974 return reloc_type == 6; /* R_Z80_32. */
14975 default:
14977 static unsigned int prev_warn = 0;
14979 /* Avoid repeating the same warning multiple times. */
14980 if (prev_warn != filedata->file_header.e_machine)
14981 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
14982 filedata->file_header.e_machine);
14983 prev_warn = filedata->file_header.e_machine;
14984 return false;
14989 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14990 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14992 static bool
14993 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14995 switch (filedata->file_header.e_machine)
14996 /* Please keep this table alpha-sorted for ease of visual lookup. */
14998 case EM_386:
14999 case EM_IAMCU:
15000 return reloc_type == 2; /* R_386_PC32. */
15001 case EM_68K:
15002 return reloc_type == 4; /* R_68K_PC32. */
15003 case EM_AARCH64:
15004 return reloc_type == 261; /* R_AARCH64_PREL32 */
15005 case EM_ADAPTEVA_EPIPHANY:
15006 return reloc_type == 6;
15007 case EM_ALPHA:
15008 return reloc_type == 10; /* R_ALPHA_SREL32. */
15009 case EM_ARC_COMPACT:
15010 case EM_ARC_COMPACT2:
15011 case EM_ARC_COMPACT3:
15012 case EM_ARC_COMPACT3_64:
15013 return reloc_type == 49; /* R_ARC_32_PCREL. */
15014 case EM_ARM:
15015 return reloc_type == 3; /* R_ARM_REL32 */
15016 case EM_AVR_OLD:
15017 case EM_AVR:
15018 return reloc_type == 36; /* R_AVR_32_PCREL. */
15019 case EM_LOONGARCH:
15020 return reloc_type == 99; /* R_LARCH_32_PCREL. */
15021 case EM_MICROBLAZE:
15022 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
15023 case EM_OR1K:
15024 return reloc_type == 9; /* R_OR1K_32_PCREL. */
15025 case EM_PARISC:
15026 return reloc_type == 9; /* R_PARISC_PCREL32. */
15027 case EM_PPC:
15028 return reloc_type == 26; /* R_PPC_REL32. */
15029 case EM_PPC64:
15030 return reloc_type == 26; /* R_PPC64_REL32. */
15031 case EM_RISCV:
15032 return reloc_type == 57; /* R_RISCV_32_PCREL. */
15033 case EM_S390_OLD:
15034 case EM_S390:
15035 return reloc_type == 5; /* R_390_PC32. */
15036 case EM_SH:
15037 return reloc_type == 2; /* R_SH_REL32. */
15038 case EM_SPARC32PLUS:
15039 case EM_SPARCV9:
15040 case EM_SPARC:
15041 return reloc_type == 6; /* R_SPARC_DISP32. */
15042 case EM_SPU:
15043 return reloc_type == 13; /* R_SPU_REL32. */
15044 case EM_TILEGX:
15045 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
15046 case EM_TILEPRO:
15047 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
15048 case EM_VISIUM:
15049 return reloc_type == 6; /* R_VISIUM_32_PCREL */
15050 case EM_X86_64:
15051 case EM_L1OM:
15052 case EM_K1OM:
15053 return reloc_type == 2; /* R_X86_64_PC32. */
15054 case EM_VAX:
15055 return reloc_type == 4; /* R_VAX_PCREL32. */
15056 case EM_XTENSA_OLD:
15057 case EM_XTENSA:
15058 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
15059 case EM_KVX:
15060 return reloc_type == 7; /* R_KVX_32_PCREL */
15061 default:
15062 /* Do not abort or issue an error message here. Not all targets use
15063 pc-relative 32-bit relocs in their DWARF debug information and we
15064 have already tested for target coverage in is_32bit_abs_reloc. A
15065 more helpful warning message will be generated by apply_relocations
15066 anyway, so just return. */
15067 return false;
15071 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15072 a 64-bit absolute RELA relocation used in DWARF debug sections. */
15074 static bool
15075 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15077 switch (filedata->file_header.e_machine)
15079 case EM_AARCH64:
15080 return reloc_type == 257; /* R_AARCH64_ABS64. */
15081 case EM_ARC_COMPACT3_64:
15082 return reloc_type == 5; /* R_ARC_64. */
15083 case EM_ALPHA:
15084 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
15085 case EM_IA_64:
15086 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
15087 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
15088 case EM_LOONGARCH:
15089 return reloc_type == 2; /* R_LARCH_64 */
15090 case EM_PARISC:
15091 return reloc_type == 80; /* R_PARISC_DIR64. */
15092 case EM_PPC64:
15093 return reloc_type == 38; /* R_PPC64_ADDR64. */
15094 case EM_RISCV:
15095 return reloc_type == 2; /* R_RISCV_64. */
15096 case EM_SPARC32PLUS:
15097 case EM_SPARCV9:
15098 case EM_SPARC:
15099 return reloc_type == 32 /* R_SPARC_64. */
15100 || reloc_type == 54; /* R_SPARC_UA64. */
15101 case EM_X86_64:
15102 case EM_L1OM:
15103 case EM_K1OM:
15104 return reloc_type == 1; /* R_X86_64_64. */
15105 case EM_S390_OLD:
15106 case EM_S390:
15107 return reloc_type == 22; /* R_S390_64. */
15108 case EM_TILEGX:
15109 return reloc_type == 1; /* R_TILEGX_64. */
15110 case EM_MIPS:
15111 return reloc_type == 18; /* R_MIPS_64. */
15112 case EM_KVX:
15113 return reloc_type == 3; /* R_KVX_64 */
15114 default:
15115 return false;
15119 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
15120 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
15122 static bool
15123 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
15125 switch (filedata->file_header.e_machine)
15127 case EM_AARCH64:
15128 return reloc_type == 260; /* R_AARCH64_PREL64. */
15129 case EM_ALPHA:
15130 return reloc_type == 11; /* R_ALPHA_SREL64. */
15131 case EM_IA_64:
15132 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
15133 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
15134 case EM_PARISC:
15135 return reloc_type == 72; /* R_PARISC_PCREL64. */
15136 case EM_PPC64:
15137 return reloc_type == 44; /* R_PPC64_REL64. */
15138 case EM_SPARC32PLUS:
15139 case EM_SPARCV9:
15140 case EM_SPARC:
15141 return reloc_type == 46; /* R_SPARC_DISP64. */
15142 case EM_X86_64:
15143 case EM_L1OM:
15144 case EM_K1OM:
15145 return reloc_type == 24; /* R_X86_64_PC64. */
15146 case EM_S390_OLD:
15147 case EM_S390:
15148 return reloc_type == 23; /* R_S390_PC64. */
15149 case EM_TILEGX:
15150 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
15151 default:
15152 return false;
15156 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15157 a 24-bit absolute RELA relocation used in DWARF debug sections. */
15159 static bool
15160 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15162 switch (filedata->file_header.e_machine)
15164 case EM_CYGNUS_MN10200:
15165 case EM_MN10200:
15166 return reloc_type == 4; /* R_MN10200_24. */
15167 case EM_FT32:
15168 return reloc_type == 5; /* R_FT32_20. */
15169 case EM_Z80:
15170 return reloc_type == 5; /* R_Z80_24. */
15171 default:
15172 return false;
15176 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15177 a 16-bit absolute RELA relocation used in DWARF debug sections. */
15179 static bool
15180 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15182 /* Please keep this table alpha-sorted for ease of visual lookup. */
15183 switch (filedata->file_header.e_machine)
15185 case EM_ARC:
15186 case EM_ARC_COMPACT:
15187 case EM_ARC_COMPACT2:
15188 case EM_ARC_COMPACT3:
15189 case EM_ARC_COMPACT3_64:
15190 return reloc_type == 2; /* R_ARC_16. */
15191 case EM_ADAPTEVA_EPIPHANY:
15192 return reloc_type == 5;
15193 case EM_AVR_OLD:
15194 case EM_AVR:
15195 return reloc_type == 4; /* R_AVR_16. */
15196 case EM_CYGNUS_D10V:
15197 case EM_D10V:
15198 return reloc_type == 3; /* R_D10V_16. */
15199 case EM_FT32:
15200 return reloc_type == 2; /* R_FT32_16. */
15201 case EM_H8S:
15202 case EM_H8_300:
15203 case EM_H8_300H:
15204 return reloc_type == R_H8_DIR16;
15205 case EM_IP2K_OLD:
15206 case EM_IP2K:
15207 return reloc_type == 1; /* R_IP2K_16. */
15208 case EM_M32C_OLD:
15209 case EM_M32C:
15210 return reloc_type == 1; /* R_M32C_16 */
15211 case EM_CYGNUS_MN10200:
15212 case EM_MN10200:
15213 return reloc_type == 2; /* R_MN10200_16. */
15214 case EM_CYGNUS_MN10300:
15215 case EM_MN10300:
15216 return reloc_type == 2; /* R_MN10300_16. */
15217 case EM_KVX:
15218 return reloc_type == 1; /* R_KVX_16 */
15219 case EM_MSP430:
15220 if (uses_msp430x_relocs (filedata))
15221 return reloc_type == 2; /* R_MSP430_ABS16. */
15222 /* Fall through. */
15223 case EM_MSP430_OLD:
15224 return reloc_type == 5; /* R_MSP430_16_BYTE. */
15225 case EM_NDS32:
15226 return reloc_type == 19; /* R_NDS32_16_RELA. */
15227 case EM_ALTERA_NIOS2:
15228 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
15229 case EM_NIOS32:
15230 return reloc_type == 9; /* R_NIOS_16. */
15231 case EM_OR1K:
15232 return reloc_type == 2; /* R_OR1K_16. */
15233 case EM_RISCV:
15234 return reloc_type == 55; /* R_RISCV_SET16. */
15235 case EM_TI_PRU:
15236 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
15237 case EM_TI_C6000:
15238 return reloc_type == 2; /* R_C6000_ABS16. */
15239 case EM_VISIUM:
15240 return reloc_type == 2; /* R_VISIUM_16. */
15241 case EM_XGATE:
15242 return reloc_type == 3; /* R_XGATE_16. */
15243 case EM_Z80:
15244 return reloc_type == 4; /* R_Z80_16. */
15245 default:
15246 return false;
15250 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15251 a 8-bit absolute RELA relocation used in DWARF debug sections. */
15253 static bool
15254 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15256 switch (filedata->file_header.e_machine)
15258 case EM_RISCV:
15259 return reloc_type == 54; /* R_RISCV_SET8. */
15260 case EM_Z80:
15261 return reloc_type == 1; /* R_Z80_8. */
15262 default:
15263 return false;
15267 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15268 a 6-bit absolute RELA relocation used in DWARF debug sections. */
15270 static bool
15271 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15273 switch (filedata->file_header.e_machine)
15275 case EM_RISCV:
15276 return reloc_type == 53; /* R_RISCV_SET6. */
15277 default:
15278 return false;
15282 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15283 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
15285 static bool
15286 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15288 /* Please keep this table alpha-sorted for ease of visual lookup. */
15289 switch (filedata->file_header.e_machine)
15291 case EM_LOONGARCH:
15292 return reloc_type == 50; /* R_LARCH_ADD32. */
15293 case EM_RISCV:
15294 return reloc_type == 35; /* R_RISCV_ADD32. */
15295 default:
15296 return false;
15300 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15301 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
15303 static bool
15304 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15306 /* Please keep this table alpha-sorted for ease of visual lookup. */
15307 switch (filedata->file_header.e_machine)
15309 case EM_LOONGARCH:
15310 return reloc_type == 55; /* R_LARCH_SUB32. */
15311 case EM_RISCV:
15312 return reloc_type == 39; /* R_RISCV_SUB32. */
15313 default:
15314 return false;
15318 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15319 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
15321 static bool
15322 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15324 /* Please keep this table alpha-sorted for ease of visual lookup. */
15325 switch (filedata->file_header.e_machine)
15327 case EM_LOONGARCH:
15328 return reloc_type == 51; /* R_LARCH_ADD64. */
15329 case EM_RISCV:
15330 return reloc_type == 36; /* R_RISCV_ADD64. */
15331 default:
15332 return false;
15336 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15337 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
15339 static bool
15340 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15342 /* Please keep this table alpha-sorted for ease of visual lookup. */
15343 switch (filedata->file_header.e_machine)
15345 case EM_LOONGARCH:
15346 return reloc_type == 56; /* R_LARCH_SUB64. */
15347 case EM_RISCV:
15348 return reloc_type == 40; /* R_RISCV_SUB64. */
15349 default:
15350 return false;
15354 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15355 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
15357 static bool
15358 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15360 /* Please keep this table alpha-sorted for ease of visual lookup. */
15361 switch (filedata->file_header.e_machine)
15363 case EM_LOONGARCH:
15364 return reloc_type == 48; /* R_LARCH_ADD16. */
15365 case EM_RISCV:
15366 return reloc_type == 34; /* R_RISCV_ADD16. */
15367 default:
15368 return false;
15372 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15373 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
15375 static bool
15376 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15378 /* Please keep this table alpha-sorted for ease of visual lookup. */
15379 switch (filedata->file_header.e_machine)
15381 case EM_LOONGARCH:
15382 return reloc_type == 53; /* R_LARCH_SUB16. */
15383 case EM_RISCV:
15384 return reloc_type == 38; /* R_RISCV_SUB16. */
15385 default:
15386 return false;
15390 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15391 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
15393 static bool
15394 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15396 /* Please keep this table alpha-sorted for ease of visual lookup. */
15397 switch (filedata->file_header.e_machine)
15399 case EM_LOONGARCH:
15400 return reloc_type == 47; /* R_LARCH_ADD8. */
15401 case EM_RISCV:
15402 return reloc_type == 33; /* R_RISCV_ADD8. */
15403 default:
15404 return false;
15408 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15409 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
15411 static bool
15412 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15414 /* Please keep this table alpha-sorted for ease of visual lookup. */
15415 switch (filedata->file_header.e_machine)
15417 case EM_LOONGARCH:
15418 return reloc_type == 52; /* R_LARCH_SUB8. */
15419 case EM_RISCV:
15420 return reloc_type == 37; /* R_RISCV_SUB8. */
15421 default:
15422 return false;
15426 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15427 a 6-bit inplace add RELA relocation used in DWARF debug sections. */
15429 static bool
15430 is_6bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15432 switch (filedata->file_header.e_machine)
15434 case EM_LOONGARCH:
15435 return reloc_type == 105; /* R_LARCH_ADD6. */
15436 default:
15437 return false;
15441 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15442 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
15444 static bool
15445 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15447 switch (filedata->file_header.e_machine)
15449 case EM_LOONGARCH:
15450 return reloc_type == 106; /* R_LARCH_SUB6. */
15451 case EM_RISCV:
15452 return reloc_type == 52; /* R_RISCV_SUB6. */
15453 default:
15454 return false;
15458 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
15459 relocation entries (possibly formerly used for SHT_GROUP sections). */
15461 static bool
15462 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
15464 switch (filedata->file_header.e_machine)
15466 case EM_386: /* R_386_NONE. */
15467 case EM_68K: /* R_68K_NONE. */
15468 case EM_ADAPTEVA_EPIPHANY:
15469 case EM_ALPHA: /* R_ALPHA_NONE. */
15470 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
15471 case EM_ARC: /* R_ARC_NONE. */
15472 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
15473 case EM_ARC_COMPACT: /* R_ARC_NONE. */
15474 case EM_ARC_COMPACT3: /* R_ARC_NONE. */
15475 case EM_ARC_COMPACT3_64: /* R_ARC_NONE. */
15476 case EM_ARM: /* R_ARM_NONE. */
15477 case EM_CRIS: /* R_CRIS_NONE. */
15478 case EM_FT32: /* R_FT32_NONE. */
15479 case EM_IA_64: /* R_IA64_NONE. */
15480 case EM_K1OM: /* R_X86_64_NONE. */
15481 case EM_KVX: /* R_KVX_NONE. */
15482 case EM_L1OM: /* R_X86_64_NONE. */
15483 case EM_M32R: /* R_M32R_NONE. */
15484 case EM_MIPS: /* R_MIPS_NONE. */
15485 case EM_MN10300: /* R_MN10300_NONE. */
15486 case EM_MOXIE: /* R_MOXIE_NONE. */
15487 case EM_NIOS32: /* R_NIOS_NONE. */
15488 case EM_OR1K: /* R_OR1K_NONE. */
15489 case EM_PARISC: /* R_PARISC_NONE. */
15490 case EM_PPC64: /* R_PPC64_NONE. */
15491 case EM_PPC: /* R_PPC_NONE. */
15492 case EM_RISCV: /* R_RISCV_NONE. */
15493 case EM_S390: /* R_390_NONE. */
15494 case EM_S390_OLD:
15495 case EM_SH: /* R_SH_NONE. */
15496 case EM_SPARC32PLUS:
15497 case EM_SPARC: /* R_SPARC_NONE. */
15498 case EM_SPARCV9:
15499 case EM_TILEGX: /* R_TILEGX_NONE. */
15500 case EM_TILEPRO: /* R_TILEPRO_NONE. */
15501 case EM_TI_C6000:/* R_C6000_NONE. */
15502 case EM_X86_64: /* R_X86_64_NONE. */
15503 case EM_Z80: /* R_Z80_NONE. */
15504 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
15505 return reloc_type == 0;
15507 case EM_AARCH64:
15508 return reloc_type == 0 || reloc_type == 256;
15509 case EM_AVR_OLD:
15510 case EM_AVR:
15511 return (reloc_type == 0 /* R_AVR_NONE. */
15512 || reloc_type == 30 /* R_AVR_DIFF8. */
15513 || reloc_type == 31 /* R_AVR_DIFF16. */
15514 || reloc_type == 32 /* R_AVR_DIFF32. */);
15515 case EM_METAG:
15516 return reloc_type == 3; /* R_METAG_NONE. */
15517 case EM_NDS32:
15518 return (reloc_type == 0 /* R_NDS32_NONE. */
15519 || reloc_type == 205 /* R_NDS32_DIFF8. */
15520 || reloc_type == 206 /* R_NDS32_DIFF16. */
15521 || reloc_type == 207 /* R_NDS32_DIFF32. */
15522 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
15523 case EM_TI_PRU:
15524 return (reloc_type == 0 /* R_PRU_NONE. */
15525 || reloc_type == 65 /* R_PRU_DIFF8. */
15526 || reloc_type == 66 /* R_PRU_DIFF16. */
15527 || reloc_type == 67 /* R_PRU_DIFF32. */);
15528 case EM_XTENSA_OLD:
15529 case EM_XTENSA:
15530 return (reloc_type == 0 /* R_XTENSA_NONE. */
15531 || reloc_type == 17 /* R_XTENSA_DIFF8. */
15532 || reloc_type == 18 /* R_XTENSA_DIFF16. */
15533 || reloc_type == 19 /* R_XTENSA_DIFF32. */
15534 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
15535 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
15536 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
15537 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
15538 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
15539 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
15541 return false;
15544 /* Returns TRUE if there is a relocation against
15545 section NAME at OFFSET bytes. */
15547 bool
15548 reloc_at (struct dwarf_section * dsec, uint64_t offset)
15550 Elf_Internal_Rela * relocs;
15551 Elf_Internal_Rela * rp;
15553 if (dsec == NULL || dsec->reloc_info == NULL)
15554 return false;
15556 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
15558 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
15559 if (rp->r_offset == offset)
15560 return true;
15562 return false;
15565 /* Apply relocations to a section.
15566 Returns TRUE upon success, FALSE otherwise.
15567 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
15568 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
15569 will be set to the number of relocs loaded.
15571 Note: So far support has been added only for those relocations
15572 which can be found in debug sections. FIXME: Add support for
15573 more relocations ? */
15575 static bool
15576 apply_relocations (Filedata *filedata,
15577 const Elf_Internal_Shdr *section,
15578 unsigned char *start,
15579 size_t size,
15580 void **relocs_return,
15581 uint64_t *num_relocs_return)
15583 Elf_Internal_Shdr * relsec;
15584 unsigned char * end = start + size;
15586 if (relocs_return != NULL)
15588 * (Elf_Internal_Rela **) relocs_return = NULL;
15589 * num_relocs_return = 0;
15592 if (filedata->file_header.e_type != ET_REL)
15593 /* No relocs to apply. */
15594 return true;
15596 /* Find the reloc section associated with the section. */
15597 for (relsec = filedata->section_headers;
15598 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15599 ++relsec)
15601 bool is_rela;
15602 uint64_t num_relocs;
15603 Elf_Internal_Rela * relocs;
15604 Elf_Internal_Rela * rp;
15605 Elf_Internal_Shdr * symsec;
15606 Elf_Internal_Sym * symtab;
15607 uint64_t num_syms;
15608 Elf_Internal_Sym * sym;
15610 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15611 || relsec->sh_info >= filedata->file_header.e_shnum
15612 || filedata->section_headers + relsec->sh_info != section
15613 || relsec->sh_size == 0
15614 || relsec->sh_link >= filedata->file_header.e_shnum)
15615 continue;
15617 symsec = filedata->section_headers + relsec->sh_link;
15618 if (symsec->sh_type != SHT_SYMTAB
15619 && symsec->sh_type != SHT_DYNSYM)
15620 return false;
15622 is_rela = relsec->sh_type == SHT_RELA;
15624 if (is_rela)
15626 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
15627 relsec->sh_size, & relocs, & num_relocs))
15628 return false;
15630 else
15632 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
15633 relsec->sh_size, & relocs, & num_relocs))
15634 return false;
15637 /* SH uses RELA but uses in place value instead of the addend field. */
15638 if (filedata->file_header.e_machine == EM_SH)
15639 is_rela = false;
15641 symtab = get_elf_symbols (filedata, symsec, & num_syms);
15643 for (rp = relocs; rp < relocs + num_relocs; ++rp)
15645 uint64_t addend;
15646 unsigned int reloc_type;
15647 unsigned int reloc_size;
15648 bool reloc_inplace = false;
15649 bool reloc_subtract = false;
15650 unsigned char *rloc;
15651 uint64_t sym_index;
15653 reloc_type = get_reloc_type (filedata, rp->r_info);
15655 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
15656 continue;
15657 else if (is_none_reloc (filedata, reloc_type))
15658 continue;
15659 else if (is_32bit_abs_reloc (filedata, reloc_type)
15660 || is_32bit_pcrel_reloc (filedata, reloc_type))
15661 reloc_size = 4;
15662 else if (is_64bit_abs_reloc (filedata, reloc_type)
15663 || is_64bit_pcrel_reloc (filedata, reloc_type))
15664 reloc_size = 8;
15665 else if (is_24bit_abs_reloc (filedata, reloc_type))
15666 reloc_size = 3;
15667 else if (is_16bit_abs_reloc (filedata, reloc_type))
15668 reloc_size = 2;
15669 else if (is_8bit_abs_reloc (filedata, reloc_type)
15670 || is_6bit_abs_reloc (filedata, reloc_type))
15671 reloc_size = 1;
15672 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15673 reloc_type))
15674 || is_32bit_inplace_add_reloc (filedata, reloc_type))
15676 reloc_size = 4;
15677 reloc_inplace = true;
15679 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15680 reloc_type))
15681 || is_64bit_inplace_add_reloc (filedata, reloc_type))
15683 reloc_size = 8;
15684 reloc_inplace = true;
15686 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15687 reloc_type))
15688 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15690 reloc_size = 2;
15691 reloc_inplace = true;
15693 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15694 reloc_type))
15695 || is_8bit_inplace_add_reloc (filedata, reloc_type))
15697 reloc_size = 1;
15698 reloc_inplace = true;
15700 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
15701 reloc_type))
15702 || is_6bit_inplace_add_reloc (filedata, reloc_type))
15704 reloc_size = 1;
15705 reloc_inplace = true;
15707 else
15709 static unsigned int prev_reloc = 0;
15711 if (reloc_type != prev_reloc)
15712 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
15713 reloc_type, printable_section_name (filedata, section));
15714 prev_reloc = reloc_type;
15715 continue;
15718 rloc = start + rp->r_offset;
15719 if (!IN_RANGE (start, end, rloc, reloc_size))
15721 warn (_("skipping invalid relocation offset %#" PRIx64
15722 " in section %s\n"),
15723 rp->r_offset,
15724 printable_section_name (filedata, section));
15725 continue;
15728 sym_index = get_reloc_symindex (rp->r_info);
15729 if (sym_index >= num_syms)
15731 warn (_("skipping invalid relocation symbol index %#" PRIx64
15732 " in section %s\n"),
15733 sym_index, printable_section_name (filedata, section));
15734 continue;
15736 sym = symtab + sym_index;
15738 /* If the reloc has a symbol associated with it,
15739 make sure that it is of an appropriate type.
15741 Relocations against symbols without type can happen.
15742 Gcc -feliminate-dwarf2-dups may generate symbols
15743 without type for debug info.
15745 Icc generates relocations against function symbols
15746 instead of local labels.
15748 Relocations against object symbols can happen, eg when
15749 referencing a global array. For an example of this see
15750 the _clz.o binary in libgcc.a. */
15751 if (sym != symtab
15752 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
15753 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
15755 warn (_("skipping unexpected symbol type %s in section %s relocation %tu\n"),
15756 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15757 printable_section_name (filedata, relsec),
15758 rp - relocs);
15759 continue;
15762 addend = 0;
15763 if (is_rela)
15764 addend += rp->r_addend;
15765 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15766 partial_inplace. */
15767 if (!is_rela
15768 || (filedata->file_header.e_machine == EM_XTENSA
15769 && reloc_type == 1)
15770 || ((filedata->file_header.e_machine == EM_PJ
15771 || filedata->file_header.e_machine == EM_PJ_OLD)
15772 && reloc_type == 1)
15773 || ((filedata->file_header.e_machine == EM_D30V
15774 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
15775 && reloc_type == 12)
15776 || reloc_inplace)
15778 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15779 addend += byte_get (rloc, reloc_size) & 0x3f;
15780 else
15781 addend += byte_get (rloc, reloc_size);
15784 if (is_32bit_pcrel_reloc (filedata, reloc_type)
15785 || is_64bit_pcrel_reloc (filedata, reloc_type))
15787 /* On HPPA, all pc-relative relocations are biased by 8. */
15788 if (filedata->file_header.e_machine == EM_PARISC)
15789 addend -= 8;
15790 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
15791 reloc_size);
15793 else if (is_6bit_abs_reloc (filedata, reloc_type)
15794 || is_6bit_inplace_sub_reloc (filedata, reloc_type)
15795 || is_6bit_inplace_add_reloc (filedata, reloc_type))
15797 if (reloc_subtract)
15798 addend -= sym->st_value;
15799 else
15800 addend += sym->st_value;
15801 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15802 byte_put (rloc, addend, reloc_size);
15804 else if (reloc_subtract)
15805 byte_put (rloc, addend - sym->st_value, reloc_size);
15806 else
15807 byte_put (rloc, addend + sym->st_value, reloc_size);
15810 free (symtab);
15811 /* Let the target specific reloc processing code know that
15812 we have finished with these relocs. */
15813 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
15815 if (relocs_return)
15817 * (Elf_Internal_Rela **) relocs_return = relocs;
15818 * num_relocs_return = num_relocs;
15820 else
15821 free (relocs);
15823 break;
15826 return true;
15829 #ifdef SUPPORT_DISASSEMBLY
15830 static bool
15831 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
15833 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
15835 /* FIXME: XXX -- to be done --- XXX */
15837 return true;
15839 #endif
15841 /* Reads in the contents of SECTION from FILE, returning a pointer
15842 to a malloc'ed buffer or NULL if something went wrong. */
15844 static char *
15845 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
15847 uint64_t num_bytes = section->sh_size;
15849 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15851 printf (_("Section '%s' has no data to dump.\n"),
15852 printable_section_name (filedata, section));
15853 return NULL;
15856 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
15857 _("section contents"));
15860 /* Uncompresses a section that was compressed using zlib/zstd, in place. */
15862 static bool
15863 uncompress_section_contents (bool is_zstd,
15864 unsigned char ** buffer,
15865 uint64_t uncompressed_size,
15866 uint64_t * size,
15867 uint64_t file_size)
15869 uint64_t compressed_size = *size;
15870 unsigned char *compressed_buffer = *buffer;
15871 unsigned char *uncompressed_buffer = NULL;
15872 z_stream strm;
15873 int rc;
15875 /* Similar to _bfd_section_size_insane() in the BFD library we expect an
15876 upper limit of ~10x compression. Any compression larger than that is
15877 thought to be due to fuzzing of the compression header. */
15878 if (uncompressed_size > file_size * 10)
15880 error (_("Uncompressed section size is suspiciously large: 0x%" PRIu64 "\n"),
15881 uncompressed_size);
15882 goto fail;
15885 uncompressed_buffer = xmalloc (uncompressed_size);
15887 if (is_zstd)
15889 #ifdef HAVE_ZSTD
15890 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
15891 compressed_buffer, compressed_size);
15892 if (ZSTD_isError (ret))
15893 goto fail;
15894 #endif
15896 else
15898 /* It is possible the section consists of several compressed
15899 buffers concatenated together, so we uncompress in a loop. */
15900 /* PR 18313: The state field in the z_stream structure is supposed
15901 to be invisible to the user (ie us), but some compilers will
15902 still complain about it being used without initialisation. So
15903 we first zero the entire z_stream structure and then set the fields
15904 that we need. */
15905 memset (&strm, 0, sizeof strm);
15906 strm.avail_in = compressed_size;
15907 strm.next_in = (Bytef *)compressed_buffer;
15908 strm.avail_out = uncompressed_size;
15910 rc = inflateInit (&strm);
15911 while (strm.avail_in > 0)
15913 if (rc != Z_OK)
15914 break;
15915 strm.next_out = ((Bytef *)uncompressed_buffer
15916 + (uncompressed_size - strm.avail_out));
15917 rc = inflate (&strm, Z_FINISH);
15918 if (rc != Z_STREAM_END)
15919 break;
15920 rc = inflateReset (&strm);
15922 if (inflateEnd (&strm) != Z_OK || rc != Z_OK || strm.avail_out != 0)
15923 goto fail;
15926 *buffer = uncompressed_buffer;
15927 *size = uncompressed_size;
15928 return true;
15930 fail:
15931 free (uncompressed_buffer);
15932 /* Indicate decompression failure. */
15933 *buffer = NULL;
15934 return false;
15937 static bool
15938 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15940 Elf_Internal_Shdr *relsec;
15941 uint64_t num_bytes;
15942 unsigned char *data;
15943 unsigned char *end;
15944 unsigned char *real_start;
15945 unsigned char *start;
15946 bool some_strings_shown;
15948 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15949 if (start == NULL)
15950 /* PR 21820: Do not fail if the section was empty. */
15951 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15953 num_bytes = section->sh_size;
15955 if (filedata->is_separate)
15956 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15957 printable_section_name (filedata, section),
15958 filedata->file_name);
15959 else
15960 printf (_("\nString dump of section '%s':\n"),
15961 printable_section_name (filedata, section));
15963 if (decompress_dumps)
15965 uint64_t new_size = num_bytes;
15966 uint64_t uncompressed_size = 0;
15967 bool is_zstd = false;
15969 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15971 Elf_Internal_Chdr chdr;
15972 unsigned int compression_header_size
15973 = get_compression_header (& chdr, (unsigned char *) start,
15974 num_bytes);
15975 if (compression_header_size == 0)
15976 /* An error message will have already been generated
15977 by get_compression_header. */
15978 goto error_out;
15980 if (chdr.ch_type == ch_compress_zlib)
15982 #ifdef HAVE_ZSTD
15983 else if (chdr.ch_type == ch_compress_zstd)
15984 is_zstd = true;
15985 #endif
15986 else
15988 warn (_("section '%s' has unsupported compress type: %d\n"),
15989 printable_section_name (filedata, section), chdr.ch_type);
15990 goto error_out;
15992 uncompressed_size = chdr.ch_size;
15993 start += compression_header_size;
15994 new_size -= compression_header_size;
15996 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15998 /* Read the zlib header. In this case, it should be "ZLIB"
15999 followed by the uncompressed section size, 8 bytes in
16000 big-endian order. */
16001 uncompressed_size = start[4]; uncompressed_size <<= 8;
16002 uncompressed_size += start[5]; uncompressed_size <<= 8;
16003 uncompressed_size += start[6]; uncompressed_size <<= 8;
16004 uncompressed_size += start[7]; uncompressed_size <<= 8;
16005 uncompressed_size += start[8]; uncompressed_size <<= 8;
16006 uncompressed_size += start[9]; uncompressed_size <<= 8;
16007 uncompressed_size += start[10]; uncompressed_size <<= 8;
16008 uncompressed_size += start[11];
16009 start += 12;
16010 new_size -= 12;
16013 if (uncompressed_size)
16015 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16016 &new_size, filedata->file_size))
16017 num_bytes = new_size;
16018 else
16020 error (_("Unable to decompress section %s\n"),
16021 printable_section_name (filedata, section));
16022 goto error_out;
16025 else
16026 start = real_start;
16029 /* If the section being dumped has relocations against it the user might
16030 be expecting these relocations to have been applied. Check for this
16031 case and issue a warning message in order to avoid confusion.
16032 FIXME: Maybe we ought to have an option that dumps a section with
16033 relocs applied ? */
16034 for (relsec = filedata->section_headers;
16035 relsec < filedata->section_headers + filedata->file_header.e_shnum;
16036 ++relsec)
16038 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
16039 || relsec->sh_info >= filedata->file_header.e_shnum
16040 || filedata->section_headers + relsec->sh_info != section
16041 || relsec->sh_size == 0
16042 || relsec->sh_link >= filedata->file_header.e_shnum)
16043 continue;
16045 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
16046 break;
16049 data = start;
16050 end = start + num_bytes;
16051 some_strings_shown = false;
16053 #ifdef HAVE_MBSTATE_T
16054 mbstate_t state;
16055 /* Initialise the multibyte conversion state. */
16056 memset (& state, 0, sizeof (state));
16057 #endif
16059 bool continuing = false;
16061 while (data < end)
16063 while (!ISPRINT (* data))
16064 if (++ data >= end)
16065 break;
16067 if (data < end)
16069 size_t maxlen = end - data;
16071 if (continuing)
16073 printf (" ");
16074 continuing = false;
16076 else
16078 printf (" [%6tx] ", data - start);
16081 if (maxlen > 0)
16083 char c = 0;
16085 while (maxlen)
16087 c = *data++;
16089 if (c == 0)
16090 break;
16092 /* PR 25543: Treat new-lines as string-ending characters. */
16093 if (c == '\n')
16095 printf ("\\n\n");
16096 if (*data != 0)
16097 continuing = true;
16098 break;
16101 /* Do not print control characters directly as they can affect terminal
16102 settings. Such characters usually appear in the names generated
16103 by the assembler for local labels. */
16104 if (ISCNTRL (c))
16106 printf ("^%c", c + 0x40);
16108 else if (ISPRINT (c))
16110 putchar (c);
16112 else
16114 size_t n;
16115 #ifdef HAVE_MBSTATE_T
16116 wchar_t w;
16117 #endif
16118 /* Let printf do the hard work of displaying multibyte characters. */
16119 printf ("%.1s", data - 1);
16120 #ifdef HAVE_MBSTATE_T
16121 /* Try to find out how many bytes made up the character that was
16122 just printed. Advance the symbol pointer past the bytes that
16123 were displayed. */
16124 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
16125 #else
16126 n = 1;
16127 #endif
16128 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
16129 data += (n - 1);
16133 if (c != '\n')
16134 putchar ('\n');
16136 else
16138 printf (_("<corrupt>\n"));
16139 data = end;
16141 some_strings_shown = true;
16145 if (! some_strings_shown)
16146 printf (_(" No strings found in this section."));
16148 free (real_start);
16150 putchar ('\n');
16151 return true;
16153 error_out:
16154 free (real_start);
16155 return false;
16158 static bool
16159 dump_section_as_bytes (Elf_Internal_Shdr *section,
16160 Filedata *filedata,
16161 bool relocate)
16163 Elf_Internal_Shdr *relsec;
16164 size_t bytes;
16165 uint64_t section_size;
16166 uint64_t addr;
16167 unsigned char *data;
16168 unsigned char *real_start;
16169 unsigned char *start;
16171 real_start = start = (unsigned char *) get_section_contents (section, filedata);
16172 if (start == NULL)
16173 /* PR 21820: Do not fail if the section was empty. */
16174 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
16176 section_size = section->sh_size;
16178 if (filedata->is_separate)
16179 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
16180 printable_section_name (filedata, section),
16181 filedata->file_name);
16182 else
16183 printf (_("\nHex dump of section '%s':\n"),
16184 printable_section_name (filedata, section));
16186 if (decompress_dumps)
16188 uint64_t new_size = section_size;
16189 uint64_t uncompressed_size = 0;
16190 bool is_zstd = false;
16192 if ((section->sh_flags & SHF_COMPRESSED) != 0)
16194 Elf_Internal_Chdr chdr;
16195 unsigned int compression_header_size
16196 = get_compression_header (& chdr, start, section_size);
16198 if (compression_header_size == 0)
16199 /* An error message will have already been generated
16200 by get_compression_header. */
16201 goto error_out;
16203 if (chdr.ch_type == ch_compress_zlib)
16205 #ifdef HAVE_ZSTD
16206 else if (chdr.ch_type == ch_compress_zstd)
16207 is_zstd = true;
16208 #endif
16209 else
16211 warn (_("section '%s' has unsupported compress type: %d\n"),
16212 printable_section_name (filedata, section), chdr.ch_type);
16213 goto error_out;
16215 uncompressed_size = chdr.ch_size;
16216 start += compression_header_size;
16217 new_size -= compression_header_size;
16219 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
16221 /* Read the zlib header. In this case, it should be "ZLIB"
16222 followed by the uncompressed section size, 8 bytes in
16223 big-endian order. */
16224 uncompressed_size = start[4]; uncompressed_size <<= 8;
16225 uncompressed_size += start[5]; uncompressed_size <<= 8;
16226 uncompressed_size += start[6]; uncompressed_size <<= 8;
16227 uncompressed_size += start[7]; uncompressed_size <<= 8;
16228 uncompressed_size += start[8]; uncompressed_size <<= 8;
16229 uncompressed_size += start[9]; uncompressed_size <<= 8;
16230 uncompressed_size += start[10]; uncompressed_size <<= 8;
16231 uncompressed_size += start[11];
16232 start += 12;
16233 new_size -= 12;
16236 if (uncompressed_size)
16238 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16239 &new_size, filedata->file_size))
16241 section_size = new_size;
16243 else
16245 error (_("Unable to decompress section %s\n"),
16246 printable_section_name (filedata, section));
16247 /* FIXME: Print the section anyway ? */
16248 goto error_out;
16251 else
16252 start = real_start;
16255 if (relocate)
16257 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
16258 goto error_out;
16260 else
16262 /* If the section being dumped has relocations against it the user might
16263 be expecting these relocations to have been applied. Check for this
16264 case and issue a warning message in order to avoid confusion.
16265 FIXME: Maybe we ought to have an option that dumps a section with
16266 relocs applied ? */
16267 for (relsec = filedata->section_headers;
16268 relsec < filedata->section_headers + filedata->file_header.e_shnum;
16269 ++relsec)
16271 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
16272 || relsec->sh_info >= filedata->file_header.e_shnum
16273 || filedata->section_headers + relsec->sh_info != section
16274 || relsec->sh_size == 0
16275 || relsec->sh_link >= filedata->file_header.e_shnum)
16276 continue;
16278 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
16279 break;
16283 addr = section->sh_addr;
16284 bytes = section_size;
16285 data = start;
16287 while (bytes)
16289 int j;
16290 int k;
16291 int lbytes;
16293 lbytes = (bytes > 16 ? 16 : bytes);
16295 printf (" 0x%8.8" PRIx64 " ", addr);
16297 for (j = 0; j < 16; j++)
16299 if (j < lbytes)
16300 printf ("%2.2x", data[j]);
16301 else
16302 printf (" ");
16304 if ((j & 3) == 3)
16305 printf (" ");
16308 for (j = 0; j < lbytes; j++)
16310 k = data[j];
16311 if (k >= ' ' && k < 0x7f)
16312 printf ("%c", k);
16313 else
16314 printf (".");
16317 putchar ('\n');
16319 data += lbytes;
16320 addr += lbytes;
16321 bytes -= lbytes;
16324 free (real_start);
16326 putchar ('\n');
16327 return true;
16329 error_out:
16330 free (real_start);
16331 return false;
16334 #ifdef ENABLE_LIBCTF
16335 static ctf_sect_t *
16336 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
16338 buf->cts_name = printable_section_name (filedata, shdr);
16339 buf->cts_size = shdr->sh_size;
16340 buf->cts_entsize = shdr->sh_entsize;
16342 return buf;
16345 /* Formatting callback function passed to ctf_dump. Returns either the pointer
16346 it is passed, or a pointer to newly-allocated storage, in which case
16347 dump_ctf() will free it when it no longer needs it. */
16349 static char *
16350 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
16351 char *s, void *arg)
16353 const char *blanks = arg;
16354 char *new_s;
16356 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
16357 return s;
16358 return new_s;
16361 /* Dump CTF errors/warnings. */
16362 static void
16363 dump_ctf_errs (ctf_dict_t *fp)
16365 ctf_next_t *it = NULL;
16366 char *errtext;
16367 int is_warning;
16368 int err;
16370 /* Dump accumulated errors and warnings. */
16371 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
16373 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
16374 errtext);
16375 free (errtext);
16377 if (err != ECTF_NEXT_END)
16378 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
16381 /* Dump one CTF archive member. */
16383 static void
16384 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
16385 size_t member)
16387 const char *things[] = {"Header", "Labels", "Data objects",
16388 "Function objects", "Variables", "Types", "Strings",
16389 ""};
16390 const char **thing;
16391 size_t i;
16393 /* Don't print out the name of the default-named archive member if it appears
16394 first in the list. The name .ctf appears everywhere, even for things that
16395 aren't really archives, so printing it out is liable to be confusing; also,
16396 the common case by far is for only one archive member to exist, and hiding
16397 it in that case seems worthwhile. */
16399 if (strcmp (name, ".ctf") != 0 || member != 0)
16400 printf (_("\nCTF archive member: %s:\n"), name);
16402 if (ctf_parent_name (ctf) != NULL)
16403 ctf_import (ctf, parent);
16405 for (i = 0, thing = things; *thing[0]; thing++, i++)
16407 ctf_dump_state_t *s = NULL;
16408 char *item;
16410 printf ("\n %s:\n", *thing);
16411 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
16412 (void *) " ")) != NULL)
16414 printf ("%s\n", item);
16415 free (item);
16418 if (ctf_errno (ctf))
16420 error (_("Iteration failed: %s, %s\n"), *thing,
16421 ctf_errmsg (ctf_errno (ctf)));
16422 break;
16426 dump_ctf_errs (ctf);
16429 static bool
16430 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
16432 Elf_Internal_Shdr * symtab_sec = NULL;
16433 Elf_Internal_Shdr * strtab_sec = NULL;
16434 void * data = NULL;
16435 void * symdata = NULL;
16436 void * strdata = NULL;
16437 ctf_sect_t ctfsect, symsect, strsect;
16438 ctf_sect_t * symsectp = NULL;
16439 ctf_sect_t * strsectp = NULL;
16440 ctf_archive_t * ctfa = NULL;
16441 ctf_dict_t * parent = NULL;
16442 ctf_dict_t * fp;
16444 ctf_next_t *i = NULL;
16445 const char *name;
16446 size_t member = 0;
16447 int err;
16448 bool ret = false;
16450 shdr_to_ctf_sect (&ctfsect, section, filedata);
16451 data = get_section_contents (section, filedata);
16452 ctfsect.cts_data = data;
16454 if (!dump_ctf_symtab_name)
16455 dump_ctf_symtab_name = strdup (".dynsym");
16457 if (!dump_ctf_strtab_name)
16458 dump_ctf_strtab_name = strdup (".dynstr");
16460 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
16462 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
16464 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
16465 goto fail;
16467 if ((symdata = (void *) get_data (NULL, filedata,
16468 symtab_sec->sh_offset, 1,
16469 symtab_sec->sh_size,
16470 _("symbols"))) == NULL)
16471 goto fail;
16472 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
16473 symsect.cts_data = symdata;
16476 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
16478 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
16480 error (_("No string table section named %s\n"),
16481 dump_ctf_strtab_name);
16482 goto fail;
16484 if ((strdata = (void *) get_data (NULL, filedata,
16485 strtab_sec->sh_offset, 1,
16486 strtab_sec->sh_size,
16487 _("strings"))) == NULL)
16488 goto fail;
16489 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
16490 strsect.cts_data = strdata;
16493 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
16494 libctf papers over the difference, so we can pretend it is always an
16495 archive. */
16497 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
16499 dump_ctf_errs (NULL);
16500 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
16501 goto fail;
16504 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
16505 != ELFDATA2MSB);
16507 /* Preload the parent dict, since it will need to be imported into every
16508 child in turn. */
16509 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
16511 dump_ctf_errs (NULL);
16512 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
16513 goto fail;
16516 ret = true;
16518 if (filedata->is_separate)
16519 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
16520 printable_section_name (filedata, section),
16521 filedata->file_name);
16522 else
16523 printf (_("\nDump of CTF section '%s':\n"),
16524 printable_section_name (filedata, section));
16526 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
16527 dump_ctf_archive_member (fp, name, parent, member++);
16528 if (err != ECTF_NEXT_END)
16530 dump_ctf_errs (NULL);
16531 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
16532 ret = false;
16535 fail:
16536 ctf_dict_close (parent);
16537 ctf_close (ctfa);
16538 free (data);
16539 free (symdata);
16540 free (strdata);
16541 return ret;
16543 #endif
16545 static bool
16546 dump_section_as_sframe (Elf_Internal_Shdr * section, Filedata * filedata)
16548 void * data = NULL;
16549 sframe_decoder_ctx *sfd_ctx = NULL;
16550 const char *print_name = printable_section_name (filedata, section);
16552 bool ret = true;
16553 size_t sf_size;
16554 int err = 0;
16556 if (strcmp (print_name, "") == 0)
16558 error (_("Section name must be provided \n"));
16559 ret = false;
16560 return ret;
16563 data = get_section_contents (section, filedata);
16564 sf_size = section->sh_size;
16565 /* Decode the contents of the section. */
16566 sfd_ctx = sframe_decode ((const char*)data, sf_size, &err);
16567 if (!sfd_ctx)
16569 ret = false;
16570 error (_("SFrame decode failure: %s\n"), sframe_errmsg (err));
16571 goto fail;
16574 printf (_("Contents of the SFrame section %s:"), print_name);
16575 /* Dump the contents as text. */
16576 dump_sframe (sfd_ctx, section->sh_addr);
16578 fail:
16579 free (data);
16580 return ret;
16583 static bool
16584 load_specific_debug_section (enum dwarf_section_display_enum debug,
16585 const Elf_Internal_Shdr * sec,
16586 void * data)
16588 struct dwarf_section * section = &debug_displays [debug].section;
16589 char buf [64];
16590 Filedata * filedata = (Filedata *) data;
16592 if (section->start != NULL)
16594 /* If it is already loaded, do nothing. */
16595 if (streq (section->filename, filedata->file_name))
16596 return true;
16597 free (section->start);
16600 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
16601 section->address = sec->sh_addr;
16602 section->filename = filedata->file_name;
16603 section->start = (unsigned char *) get_data (NULL, filedata,
16604 sec->sh_offset, 1,
16605 sec->sh_size, buf);
16606 if (section->start == NULL)
16607 section->size = 0;
16608 else
16610 unsigned char *start = section->start;
16611 uint64_t size = sec->sh_size;
16612 uint64_t uncompressed_size = 0;
16613 bool is_zstd = false;
16615 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
16617 Elf_Internal_Chdr chdr;
16618 unsigned int compression_header_size;
16620 if (size < (is_32bit_elf
16621 ? sizeof (Elf32_External_Chdr)
16622 : sizeof (Elf64_External_Chdr)))
16624 warn (_("compressed section %s is too small to contain a compression header\n"),
16625 section->name);
16626 return false;
16629 compression_header_size = get_compression_header (&chdr, start, size);
16630 if (compression_header_size == 0)
16631 /* An error message will have already been generated
16632 by get_compression_header. */
16633 return false;
16635 if (chdr.ch_type == ch_compress_zlib)
16637 #ifdef HAVE_ZSTD
16638 else if (chdr.ch_type == ch_compress_zstd)
16639 is_zstd = true;
16640 #endif
16641 else
16643 warn (_("section '%s' has unsupported compress type: %d\n"),
16644 section->name, chdr.ch_type);
16645 return false;
16647 uncompressed_size = chdr.ch_size;
16648 start += compression_header_size;
16649 size -= compression_header_size;
16651 else if (size > 12 && streq ((char *) start, "ZLIB"))
16653 /* Read the zlib header. In this case, it should be "ZLIB"
16654 followed by the uncompressed section size, 8 bytes in
16655 big-endian order. */
16656 uncompressed_size = start[4]; uncompressed_size <<= 8;
16657 uncompressed_size += start[5]; uncompressed_size <<= 8;
16658 uncompressed_size += start[6]; uncompressed_size <<= 8;
16659 uncompressed_size += start[7]; uncompressed_size <<= 8;
16660 uncompressed_size += start[8]; uncompressed_size <<= 8;
16661 uncompressed_size += start[9]; uncompressed_size <<= 8;
16662 uncompressed_size += start[10]; uncompressed_size <<= 8;
16663 uncompressed_size += start[11];
16664 start += 12;
16665 size -= 12;
16668 if (uncompressed_size)
16670 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16671 &size, filedata->file_size))
16673 /* Free the compressed buffer, update the section buffer
16674 and the section size if uncompress is successful. */
16675 free (section->start);
16676 section->start = start;
16678 else
16680 error (_("Unable to decompress section %s\n"),
16681 printable_section_name (filedata, sec));
16682 return false;
16686 section->size = size;
16689 if (section->start == NULL)
16690 return false;
16692 if (debug_displays [debug].relocate)
16694 if (! apply_relocations (filedata, sec, section->start, section->size,
16695 & section->reloc_info, & section->num_relocs))
16696 return false;
16698 else
16700 section->reloc_info = NULL;
16701 section->num_relocs = 0;
16704 return true;
16707 #if HAVE_LIBDEBUGINFOD
16708 /* Return a hex string representation of the build-id. */
16709 unsigned char *
16710 get_build_id (void * data)
16712 Filedata * filedata = (Filedata *) data;
16713 Elf_Internal_Shdr * shdr;
16714 size_t i;
16716 /* Iterate through notes to find note.gnu.build-id.
16717 FIXME: Only the first note in any note section is examined. */
16718 for (i = 0, shdr = filedata->section_headers;
16719 i < filedata->file_header.e_shnum && shdr != NULL;
16720 i++, shdr++)
16722 if (shdr->sh_type != SHT_NOTE)
16723 continue;
16725 char * next;
16726 char * end;
16727 size_t data_remaining;
16728 size_t min_notesz;
16729 Elf_External_Note * enote;
16730 Elf_Internal_Note inote;
16732 uint64_t offset = shdr->sh_offset;
16733 uint64_t align = shdr->sh_addralign;
16734 uint64_t length = shdr->sh_size;
16736 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
16737 if (enote == NULL)
16738 continue;
16740 if (align < 4)
16741 align = 4;
16742 else if (align != 4 && align != 8)
16744 free (enote);
16745 continue;
16748 end = (char *) enote + length;
16749 data_remaining = end - (char *) enote;
16751 if (!is_ia64_vms (filedata))
16753 min_notesz = offsetof (Elf_External_Note, name);
16754 if (data_remaining < min_notesz)
16756 warn (_("\
16757 malformed note encountered in section %s whilst scanning for build-id note\n"),
16758 printable_section_name (filedata, shdr));
16759 free (enote);
16760 continue;
16762 data_remaining -= min_notesz;
16764 inote.type = BYTE_GET (enote->type);
16765 inote.namesz = BYTE_GET (enote->namesz);
16766 inote.namedata = enote->name;
16767 inote.descsz = BYTE_GET (enote->descsz);
16768 inote.descdata = ((char *) enote
16769 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16770 inote.descpos = offset + (inote.descdata - (char *) enote);
16771 next = ((char *) enote
16772 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16774 else
16776 Elf64_External_VMS_Note *vms_enote;
16778 /* PR binutils/15191
16779 Make sure that there is enough data to read. */
16780 min_notesz = offsetof (Elf64_External_VMS_Note, name);
16781 if (data_remaining < min_notesz)
16783 warn (_("\
16784 malformed note encountered in section %s whilst scanning for build-id note\n"),
16785 printable_section_name (filedata, shdr));
16786 free (enote);
16787 continue;
16789 data_remaining -= min_notesz;
16791 vms_enote = (Elf64_External_VMS_Note *) enote;
16792 inote.type = BYTE_GET (vms_enote->type);
16793 inote.namesz = BYTE_GET (vms_enote->namesz);
16794 inote.namedata = vms_enote->name;
16795 inote.descsz = BYTE_GET (vms_enote->descsz);
16796 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16797 inote.descpos = offset + (inote.descdata - (char *) enote);
16798 next = inote.descdata + align_power (inote.descsz, 3);
16801 /* Skip malformed notes. */
16802 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16803 || (size_t) (inote.descdata - inote.namedata) > data_remaining
16804 || (size_t) (next - inote.descdata) < inote.descsz
16805 || ((size_t) (next - inote.descdata)
16806 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16808 warn (_("\
16809 malformed note encountered in section %s whilst scanning for build-id note\n"),
16810 printable_section_name (filedata, shdr));
16811 free (enote);
16812 continue;
16815 /* Check if this is the build-id note. If so then convert the build-id
16816 bytes to a hex string. */
16817 if (inote.namesz > 0
16818 && startswith (inote.namedata, "GNU")
16819 && inote.type == NT_GNU_BUILD_ID)
16821 size_t j;
16822 char * build_id;
16824 build_id = malloc (inote.descsz * 2 + 1);
16825 if (build_id == NULL)
16827 free (enote);
16828 return NULL;
16831 for (j = 0; j < inote.descsz; ++j)
16832 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16833 build_id[inote.descsz * 2] = '\0';
16834 free (enote);
16836 return (unsigned char *) build_id;
16838 free (enote);
16841 return NULL;
16843 #endif /* HAVE_LIBDEBUGINFOD */
16845 /* If this is not NULL, load_debug_section will only look for sections
16846 within the list of sections given here. */
16847 static unsigned int * section_subset = NULL;
16849 bool
16850 load_debug_section (enum dwarf_section_display_enum debug, void * data)
16852 struct dwarf_section * section = &debug_displays [debug].section;
16853 Elf_Internal_Shdr * sec;
16854 Filedata * filedata = (Filedata *) data;
16856 if (!dump_any_debugging)
16857 return false;
16859 /* Without section headers we cannot find any sections. */
16860 if (filedata->section_headers == NULL)
16861 return false;
16863 if (filedata->string_table == NULL
16864 && filedata->file_header.e_shstrndx != SHN_UNDEF
16865 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
16867 Elf_Internal_Shdr * strs;
16869 /* Read in the string table, so that we have section names to scan. */
16870 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16872 if (strs != NULL && strs->sh_size != 0)
16874 filedata->string_table
16875 = (char *) get_data (NULL, filedata, strs->sh_offset,
16876 1, strs->sh_size, _("string table"));
16878 filedata->string_table_length
16879 = filedata->string_table != NULL ? strs->sh_size : 0;
16883 /* Locate the debug section. */
16884 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
16885 if (sec != NULL)
16886 section->name = section->uncompressed_name;
16887 else
16889 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
16890 if (sec != NULL)
16891 section->name = section->compressed_name;
16893 if (sec == NULL)
16894 return false;
16896 /* If we're loading from a subset of sections, and we've loaded
16897 a section matching this name before, it's likely that it's a
16898 different one. */
16899 if (section_subset != NULL)
16900 free_debug_section (debug);
16902 return load_specific_debug_section (debug, sec, data);
16905 void
16906 free_debug_section (enum dwarf_section_display_enum debug)
16908 struct dwarf_section * section = &debug_displays [debug].section;
16910 if (section->start == NULL)
16911 return;
16913 free ((char *) section->start);
16914 section->start = NULL;
16915 section->address = 0;
16916 section->size = 0;
16918 free (section->reloc_info);
16919 section->reloc_info = NULL;
16920 section->num_relocs = 0;
16923 static bool
16924 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
16926 const char *name = (section_name_valid (filedata, section)
16927 ? section_name (filedata, section) : "");
16928 const char *print_name = printable_section_name (filedata, section);
16929 uint64_t length;
16930 bool result = true;
16931 int i;
16933 length = section->sh_size;
16934 if (length == 0)
16936 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
16937 return true;
16939 if (section->sh_type == SHT_NOBITS)
16941 /* There is no point in dumping the contents of a debugging section
16942 which has the NOBITS type - the bits in the file will be random.
16943 This can happen when a file containing a .eh_frame section is
16944 stripped with the --only-keep-debug command line option. */
16945 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16946 print_name);
16947 return false;
16950 if (startswith (name, ".gnu.linkonce.wi."))
16951 name = ".debug_info";
16953 /* See if we know how to display the contents of this section. */
16954 for (i = 0; i < max; i++)
16956 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16957 struct dwarf_section_display * display = debug_displays + i;
16958 struct dwarf_section * sec = & display->section;
16960 if (streq (sec->uncompressed_name, name)
16961 || (id == line && startswith (name, ".debug_line."))
16962 || streq (sec->compressed_name, name))
16964 bool secondary = (section != find_section (filedata, name));
16966 if (secondary)
16967 free_debug_section (id);
16969 if (i == line && startswith (name, ".debug_line."))
16970 sec->name = name;
16971 else if (streq (sec->uncompressed_name, name))
16972 sec->name = sec->uncompressed_name;
16973 else
16974 sec->name = sec->compressed_name;
16976 if (load_specific_debug_section (id, section, filedata))
16978 /* If this debug section is part of a CU/TU set in a .dwp file,
16979 restrict load_debug_section to the sections in that set. */
16980 section_subset = find_cu_tu_set (filedata, shndx);
16982 result &= display->display (sec, filedata);
16984 section_subset = NULL;
16986 if (secondary || (id != info && id != abbrev && id != debug_addr))
16987 free_debug_section (id);
16989 break;
16993 if (i == max)
16995 printf (_("Unrecognized debug section: %s\n"), print_name);
16996 result = false;
16999 return result;
17002 /* Set DUMP_SECTS for all sections where dumps were requested
17003 based on section name. */
17005 static void
17006 initialise_dumps_byname (Filedata * filedata)
17008 struct dump_list_entry * cur;
17010 for (cur = dump_sects_byname; cur; cur = cur->next)
17012 unsigned int i;
17013 bool any = false;
17015 for (i = 0; i < filedata->file_header.e_shnum; i++)
17016 if (section_name_valid (filedata, filedata->section_headers + i)
17017 && streq (section_name (filedata, filedata->section_headers + i),
17018 cur->name))
17020 request_dump_bynumber (&filedata->dump, i, cur->type);
17021 any = true;
17024 if (!any && !filedata->is_separate)
17025 warn (_("Section '%s' was not dumped because it does not exist\n"),
17026 cur->name);
17030 static bool
17031 process_section_contents (Filedata * filedata)
17033 Elf_Internal_Shdr * section;
17034 unsigned int i;
17035 bool res = true;
17037 if (! do_dump)
17038 return true;
17040 initialise_dumps_byname (filedata);
17042 for (i = 0, section = filedata->section_headers;
17043 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
17044 i++, section++)
17046 dump_type dump = filedata->dump.dump_sects[i];
17048 if (filedata->is_separate && ! process_links)
17049 dump &= DEBUG_DUMP;
17051 #ifdef SUPPORT_DISASSEMBLY
17052 if (dump & DISASS_DUMP)
17054 if (! disassemble_section (section, filedata))
17055 res = false;
17057 #endif
17058 if (dump & HEX_DUMP)
17060 if (! dump_section_as_bytes (section, filedata, false))
17061 res = false;
17064 if (dump & RELOC_DUMP)
17066 if (! dump_section_as_bytes (section, filedata, true))
17067 res = false;
17070 if (dump & STRING_DUMP)
17072 if (! dump_section_as_strings (section, filedata))
17073 res = false;
17076 if (dump & DEBUG_DUMP)
17078 if (! display_debug_section (i, section, filedata))
17079 res = false;
17082 #ifdef ENABLE_LIBCTF
17083 if (dump & CTF_DUMP)
17085 if (! dump_section_as_ctf (section, filedata))
17086 res = false;
17088 #endif
17089 if (dump & SFRAME_DUMP)
17091 if (! dump_section_as_sframe (section, filedata))
17092 res = false;
17096 if (! filedata->is_separate)
17098 /* Check to see if the user requested a
17099 dump of a section that does not exist. */
17100 for (; i < filedata->dump.num_dump_sects; i++)
17101 if (filedata->dump.dump_sects[i])
17103 warn (_("Section %d was not dumped because it does not exist!\n"), i);
17104 res = false;
17108 return res;
17111 static void
17112 process_mips_fpe_exception (int mask)
17114 if (mask)
17116 bool first = true;
17118 if (mask & OEX_FPU_INEX)
17119 fputs ("INEX", stdout), first = false;
17120 if (mask & OEX_FPU_UFLO)
17121 printf ("%sUFLO", first ? "" : "|"), first = false;
17122 if (mask & OEX_FPU_OFLO)
17123 printf ("%sOFLO", first ? "" : "|"), first = false;
17124 if (mask & OEX_FPU_DIV0)
17125 printf ("%sDIV0", first ? "" : "|"), first = false;
17126 if (mask & OEX_FPU_INVAL)
17127 printf ("%sINVAL", first ? "" : "|");
17129 else
17130 fputs ("0", stdout);
17133 /* Display's the value of TAG at location P. If TAG is
17134 greater than 0 it is assumed to be an unknown tag, and
17135 a message is printed to this effect. Otherwise it is
17136 assumed that a message has already been printed.
17138 If the bottom bit of TAG is set it assumed to have a
17139 string value, otherwise it is assumed to have an integer
17140 value.
17142 Returns an updated P pointing to the first unread byte
17143 beyond the end of TAG's value.
17145 Reads at or beyond END will not be made. */
17147 static unsigned char *
17148 display_tag_value (signed int tag,
17149 unsigned char * p,
17150 const unsigned char * const end)
17152 uint64_t val;
17154 if (tag > 0)
17155 printf (" Tag_unknown_%d: ", tag);
17157 if (p >= end)
17159 warn (_("<corrupt tag>\n"));
17161 else if (tag & 1)
17163 /* PR 17531 file: 027-19978-0.004. */
17164 size_t maxlen = (end - p) - 1;
17166 putchar ('"');
17167 if (maxlen > 0)
17169 print_symbol_name ((int) maxlen, (const char *) p);
17170 p += strnlen ((char *) p, maxlen) + 1;
17172 else
17174 printf (_("<corrupt string tag>"));
17175 p = (unsigned char *) end;
17177 printf ("\"\n");
17179 else
17181 READ_ULEB (val, p, end);
17182 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
17185 assert (p <= end);
17186 return p;
17189 /* ARC ABI attributes section. */
17191 static unsigned char *
17192 display_arc_attribute (unsigned char * p,
17193 const unsigned char * const end)
17195 unsigned int tag;
17196 unsigned int val;
17198 READ_ULEB (tag, p, end);
17200 switch (tag)
17202 case Tag_ARC_PCS_config:
17203 READ_ULEB (val, p, end);
17204 printf (" Tag_ARC_PCS_config: ");
17205 switch (val)
17207 case 0:
17208 printf (_("Absent/Non standard\n"));
17209 break;
17210 case 1:
17211 printf (_("Bare metal/mwdt\n"));
17212 break;
17213 case 2:
17214 printf (_("Bare metal/newlib\n"));
17215 break;
17216 case 3:
17217 printf (_("Linux/uclibc\n"));
17218 break;
17219 case 4:
17220 printf (_("Linux/glibc\n"));
17221 break;
17222 default:
17223 printf (_("Unknown\n"));
17224 break;
17226 break;
17228 case Tag_ARC_CPU_base:
17229 READ_ULEB (val, p, end);
17230 printf (" Tag_ARC_CPU_base: ");
17231 switch (val)
17233 default:
17234 case TAG_CPU_NONE:
17235 printf (_("Absent\n"));
17236 break;
17237 case TAG_CPU_ARC6xx:
17238 printf ("ARC6xx\n");
17239 break;
17240 case TAG_CPU_ARC7xx:
17241 printf ("ARC7xx\n");
17242 break;
17243 case TAG_CPU_ARCEM:
17244 printf ("ARCEM\n");
17245 break;
17246 case TAG_CPU_ARCHS:
17247 printf ("ARCHS\n");
17248 break;
17250 break;
17252 case Tag_ARC_CPU_variation:
17253 READ_ULEB (val, p, end);
17254 printf (" Tag_ARC_CPU_variation: ");
17255 switch (val)
17257 default:
17258 if (val > 0 && val < 16)
17259 printf ("Core%d\n", val);
17260 else
17261 printf ("Unknown\n");
17262 break;
17264 case 0:
17265 printf (_("Absent\n"));
17266 break;
17268 break;
17270 case Tag_ARC_CPU_name:
17271 printf (" Tag_ARC_CPU_name: ");
17272 p = display_tag_value (-1, p, end);
17273 break;
17275 case Tag_ARC_ABI_rf16:
17276 READ_ULEB (val, p, end);
17277 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
17278 break;
17280 case Tag_ARC_ABI_osver:
17281 READ_ULEB (val, p, end);
17282 printf (" Tag_ARC_ABI_osver: v%d\n", val);
17283 break;
17285 case Tag_ARC_ABI_pic:
17286 case Tag_ARC_ABI_sda:
17287 READ_ULEB (val, p, end);
17288 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
17289 : " Tag_ARC_ABI_pic: ");
17290 switch (val)
17292 case 0:
17293 printf (_("Absent\n"));
17294 break;
17295 case 1:
17296 printf ("MWDT\n");
17297 break;
17298 case 2:
17299 printf ("GNU\n");
17300 break;
17301 default:
17302 printf (_("Unknown\n"));
17303 break;
17305 break;
17307 case Tag_ARC_ABI_tls:
17308 READ_ULEB (val, p, end);
17309 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
17310 break;
17312 case Tag_ARC_ABI_enumsize:
17313 READ_ULEB (val, p, end);
17314 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
17315 _("smallest"));
17316 break;
17318 case Tag_ARC_ABI_exceptions:
17319 READ_ULEB (val, p, end);
17320 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
17321 : _("default"));
17322 break;
17324 case Tag_ARC_ABI_double_size:
17325 READ_ULEB (val, p, end);
17326 printf (" Tag_ARC_ABI_double_size: %d\n", val);
17327 break;
17329 case Tag_ARC_ISA_config:
17330 printf (" Tag_ARC_ISA_config: ");
17331 p = display_tag_value (-1, p, end);
17332 break;
17334 case Tag_ARC_ISA_apex:
17335 printf (" Tag_ARC_ISA_apex: ");
17336 p = display_tag_value (-1, p, end);
17337 break;
17339 case Tag_ARC_ISA_mpy_option:
17340 READ_ULEB (val, p, end);
17341 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
17342 break;
17344 case Tag_ARC_ATR_version:
17345 READ_ULEB (val, p, end);
17346 printf (" Tag_ARC_ATR_version: %d\n", val);
17347 break;
17349 default:
17350 return display_tag_value (tag & 1, p, end);
17353 return p;
17356 /* ARM EABI attributes section. */
17357 typedef struct
17359 unsigned int tag;
17360 const char * name;
17361 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
17362 unsigned int type;
17363 const char *const *table;
17364 } arm_attr_public_tag;
17366 static const char *const arm_attr_tag_CPU_arch[] =
17367 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
17368 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
17369 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
17370 "v8.1-M.mainline", "v9"};
17371 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
17372 static const char *const arm_attr_tag_THUMB_ISA_use[] =
17373 {"No", "Thumb-1", "Thumb-2", "Yes"};
17374 static const char *const arm_attr_tag_FP_arch[] =
17375 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
17376 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
17377 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
17378 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
17379 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
17380 "NEON for ARMv8.1"};
17381 static const char *const arm_attr_tag_PCS_config[] =
17382 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
17383 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
17384 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
17385 {"V6", "SB", "TLS", "Unused"};
17386 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
17387 {"Absolute", "PC-relative", "SB-relative", "None"};
17388 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
17389 {"Absolute", "PC-relative", "None"};
17390 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
17391 {"None", "direct", "GOT-indirect"};
17392 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
17393 {"None", "??? 1", "2", "??? 3", "4"};
17394 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
17395 static const char *const arm_attr_tag_ABI_FP_denormal[] =
17396 {"Unused", "Needed", "Sign only"};
17397 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
17398 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
17399 static const char *const arm_attr_tag_ABI_FP_number_model[] =
17400 {"Unused", "Finite", "RTABI", "IEEE 754"};
17401 static const char *const arm_attr_tag_ABI_enum_size[] =
17402 {"Unused", "small", "int", "forced to int"};
17403 static const char *const arm_attr_tag_ABI_HardFP_use[] =
17404 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
17405 static const char *const arm_attr_tag_ABI_VFP_args[] =
17406 {"AAPCS", "VFP registers", "custom", "compatible"};
17407 static const char *const arm_attr_tag_ABI_WMMX_args[] =
17408 {"AAPCS", "WMMX registers", "custom"};
17409 static const char *const arm_attr_tag_ABI_optimization_goals[] =
17410 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
17411 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
17412 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
17413 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
17414 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
17415 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
17416 static const char *const arm_attr_tag_FP_HP_extension[] =
17417 {"Not Allowed", "Allowed"};
17418 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
17419 {"None", "IEEE 754", "Alternative Format"};
17420 static const char *const arm_attr_tag_DSP_extension[] =
17421 {"Follow architecture", "Allowed"};
17422 static const char *const arm_attr_tag_MPextension_use[] =
17423 {"Not Allowed", "Allowed"};
17424 static const char *const arm_attr_tag_DIV_use[] =
17425 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
17426 "Allowed in v7-A with integer division extension"};
17427 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
17428 static const char *const arm_attr_tag_Virtualization_use[] =
17429 {"Not Allowed", "TrustZone", "Virtualization Extensions",
17430 "TrustZone and Virtualization Extensions"};
17431 static const char *const arm_attr_tag_MPextension_use_legacy[] =
17432 {"Not Allowed", "Allowed"};
17434 static const char *const arm_attr_tag_MVE_arch[] =
17435 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
17437 static const char * arm_attr_tag_PAC_extension[] =
17438 {"No PAC/AUT instructions",
17439 "PAC/AUT instructions permitted in the NOP space",
17440 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
17442 static const char * arm_attr_tag_BTI_extension[] =
17443 {"BTI instructions not permitted",
17444 "BTI instructions permitted in the NOP space",
17445 "BTI instructions permitted in the NOP and in the non-NOP space"};
17447 static const char * arm_attr_tag_BTI_use[] =
17448 {"Compiled without branch target enforcement",
17449 "Compiled with branch target enforcement"};
17451 static const char * arm_attr_tag_PACRET_use[] =
17452 {"Compiled without return address signing and authentication",
17453 "Compiled with return address signing and authentication"};
17455 #define LOOKUP(id, name) \
17456 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
17457 static arm_attr_public_tag arm_attr_public_tags[] =
17459 {4, "CPU_raw_name", 1, NULL},
17460 {5, "CPU_name", 1, NULL},
17461 LOOKUP(6, CPU_arch),
17462 {7, "CPU_arch_profile", 0, NULL},
17463 LOOKUP(8, ARM_ISA_use),
17464 LOOKUP(9, THUMB_ISA_use),
17465 LOOKUP(10, FP_arch),
17466 LOOKUP(11, WMMX_arch),
17467 LOOKUP(12, Advanced_SIMD_arch),
17468 LOOKUP(13, PCS_config),
17469 LOOKUP(14, ABI_PCS_R9_use),
17470 LOOKUP(15, ABI_PCS_RW_data),
17471 LOOKUP(16, ABI_PCS_RO_data),
17472 LOOKUP(17, ABI_PCS_GOT_use),
17473 LOOKUP(18, ABI_PCS_wchar_t),
17474 LOOKUP(19, ABI_FP_rounding),
17475 LOOKUP(20, ABI_FP_denormal),
17476 LOOKUP(21, ABI_FP_exceptions),
17477 LOOKUP(22, ABI_FP_user_exceptions),
17478 LOOKUP(23, ABI_FP_number_model),
17479 {24, "ABI_align_needed", 0, NULL},
17480 {25, "ABI_align_preserved", 0, NULL},
17481 LOOKUP(26, ABI_enum_size),
17482 LOOKUP(27, ABI_HardFP_use),
17483 LOOKUP(28, ABI_VFP_args),
17484 LOOKUP(29, ABI_WMMX_args),
17485 LOOKUP(30, ABI_optimization_goals),
17486 LOOKUP(31, ABI_FP_optimization_goals),
17487 {32, "compatibility", 0, NULL},
17488 LOOKUP(34, CPU_unaligned_access),
17489 LOOKUP(36, FP_HP_extension),
17490 LOOKUP(38, ABI_FP_16bit_format),
17491 LOOKUP(42, MPextension_use),
17492 LOOKUP(44, DIV_use),
17493 LOOKUP(46, DSP_extension),
17494 LOOKUP(48, MVE_arch),
17495 LOOKUP(50, PAC_extension),
17496 LOOKUP(52, BTI_extension),
17497 LOOKUP(74, BTI_use),
17498 LOOKUP(76, PACRET_use),
17499 {64, "nodefaults", 0, NULL},
17500 {65, "also_compatible_with", 0, NULL},
17501 LOOKUP(66, T2EE_use),
17502 {67, "conformance", 1, NULL},
17503 LOOKUP(68, Virtualization_use),
17504 LOOKUP(70, MPextension_use_legacy)
17506 #undef LOOKUP
17508 static unsigned char *
17509 display_arm_attribute (unsigned char * p,
17510 const unsigned char * const end)
17512 unsigned int tag;
17513 unsigned int val;
17514 arm_attr_public_tag * attr;
17515 unsigned i;
17516 unsigned int type;
17518 READ_ULEB (tag, p, end);
17519 attr = NULL;
17520 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
17522 if (arm_attr_public_tags[i].tag == tag)
17524 attr = &arm_attr_public_tags[i];
17525 break;
17529 if (attr)
17531 printf (" Tag_%s: ", attr->name);
17532 switch (attr->type)
17534 case 0:
17535 switch (tag)
17537 case 7: /* Tag_CPU_arch_profile. */
17538 READ_ULEB (val, p, end);
17539 switch (val)
17541 case 0: printf (_("None\n")); break;
17542 case 'A': printf (_("Application\n")); break;
17543 case 'R': printf (_("Realtime\n")); break;
17544 case 'M': printf (_("Microcontroller\n")); break;
17545 case 'S': printf (_("Application or Realtime\n")); break;
17546 default: printf ("??? (%d)\n", val); break;
17548 break;
17550 case 24: /* Tag_align_needed. */
17551 READ_ULEB (val, p, end);
17552 switch (val)
17554 case 0: printf (_("None\n")); break;
17555 case 1: printf (_("8-byte\n")); break;
17556 case 2: printf (_("4-byte\n")); break;
17557 case 3: printf ("??? 3\n"); break;
17558 default:
17559 if (val <= 12)
17560 printf (_("8-byte and up to %d-byte extended\n"),
17561 1 << val);
17562 else
17563 printf ("??? (%d)\n", val);
17564 break;
17566 break;
17568 case 25: /* Tag_align_preserved. */
17569 READ_ULEB (val, p, end);
17570 switch (val)
17572 case 0: printf (_("None\n")); break;
17573 case 1: printf (_("8-byte, except leaf SP\n")); break;
17574 case 2: printf (_("8-byte\n")); break;
17575 case 3: printf ("??? 3\n"); break;
17576 default:
17577 if (val <= 12)
17578 printf (_("8-byte and up to %d-byte extended\n"),
17579 1 << val);
17580 else
17581 printf ("??? (%d)\n", val);
17582 break;
17584 break;
17586 case 32: /* Tag_compatibility. */
17588 READ_ULEB (val, p, end);
17589 printf (_("flag = %d, vendor = "), val);
17590 if (p < end - 1)
17592 size_t maxlen = (end - p) - 1;
17594 print_symbol_name ((int) maxlen, (const char *) p);
17595 p += strnlen ((char *) p, maxlen) + 1;
17597 else
17599 printf (_("<corrupt>"));
17600 p = (unsigned char *) end;
17602 putchar ('\n');
17604 break;
17606 case 64: /* Tag_nodefaults. */
17607 /* PR 17531: file: 001-505008-0.01. */
17608 if (p < end)
17609 p++;
17610 printf (_("True\n"));
17611 break;
17613 case 65: /* Tag_also_compatible_with. */
17614 READ_ULEB (val, p, end);
17615 if (val == 6 /* Tag_CPU_arch. */)
17617 READ_ULEB (val, p, end);
17618 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
17619 printf ("??? (%d)\n", val);
17620 else
17621 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
17623 else
17624 printf ("???\n");
17625 while (p < end && *(p++) != '\0' /* NUL terminator. */)
17627 break;
17629 default:
17630 printf (_("<unknown: %d>\n"), tag);
17631 break;
17633 return p;
17635 case 1:
17636 return display_tag_value (-1, p, end);
17637 case 2:
17638 return display_tag_value (0, p, end);
17640 default:
17641 assert (attr->type & 0x80);
17642 READ_ULEB (val, p, end);
17643 type = attr->type & 0x7f;
17644 if (val >= type)
17645 printf ("??? (%d)\n", val);
17646 else
17647 printf ("%s\n", attr->table[val]);
17648 return p;
17652 return display_tag_value (tag, p, end);
17655 static unsigned char *
17656 display_gnu_attribute (unsigned char * p,
17657 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
17658 const unsigned char * const end)
17660 unsigned int tag;
17661 unsigned int val;
17663 READ_ULEB (tag, p, end);
17665 /* Tag_compatibility is the only generic GNU attribute defined at
17666 present. */
17667 if (tag == 32)
17669 READ_ULEB (val, p, end);
17671 printf (_("flag = %d, vendor = "), val);
17672 if (p == end)
17674 printf (_("<corrupt>\n"));
17675 warn (_("corrupt vendor attribute\n"));
17677 else
17679 if (p < end - 1)
17681 size_t maxlen = (end - p) - 1;
17683 print_symbol_name ((int) maxlen, (const char *) p);
17684 p += strnlen ((char *) p, maxlen) + 1;
17686 else
17688 printf (_("<corrupt>"));
17689 p = (unsigned char *) end;
17691 putchar ('\n');
17693 return p;
17696 if ((tag & 2) == 0 && display_proc_gnu_attribute)
17697 return display_proc_gnu_attribute (p, tag, end);
17699 return display_tag_value (tag, p, end);
17702 static unsigned char *
17703 display_m68k_gnu_attribute (unsigned char * p,
17704 unsigned int tag,
17705 const unsigned char * const end)
17707 unsigned int val;
17709 if (tag == Tag_GNU_M68K_ABI_FP)
17711 printf (" Tag_GNU_M68K_ABI_FP: ");
17712 if (p == end)
17714 printf (_("<corrupt>\n"));
17715 return p;
17717 READ_ULEB (val, p, end);
17719 if (val > 3)
17720 printf ("(%#x), ", val);
17722 switch (val & 3)
17724 case 0:
17725 printf (_("unspecified hard/soft float\n"));
17726 break;
17727 case 1:
17728 printf (_("hard float\n"));
17729 break;
17730 case 2:
17731 printf (_("soft float\n"));
17732 break;
17734 return p;
17737 return display_tag_value (tag & 1, p, end);
17740 static unsigned char *
17741 display_power_gnu_attribute (unsigned char * p,
17742 unsigned int tag,
17743 const unsigned char * const end)
17745 unsigned int val;
17747 if (tag == Tag_GNU_Power_ABI_FP)
17749 printf (" Tag_GNU_Power_ABI_FP: ");
17750 if (p == end)
17752 printf (_("<corrupt>\n"));
17753 return p;
17755 READ_ULEB (val, p, end);
17757 if (val > 15)
17758 printf ("(%#x), ", val);
17760 switch (val & 3)
17762 case 0:
17763 printf (_("unspecified hard/soft float, "));
17764 break;
17765 case 1:
17766 printf (_("hard float, "));
17767 break;
17768 case 2:
17769 printf (_("soft float, "));
17770 break;
17771 case 3:
17772 printf (_("single-precision hard float, "));
17773 break;
17776 switch (val & 0xC)
17778 case 0:
17779 printf (_("unspecified long double\n"));
17780 break;
17781 case 4:
17782 printf (_("128-bit IBM long double\n"));
17783 break;
17784 case 8:
17785 printf (_("64-bit long double\n"));
17786 break;
17787 case 12:
17788 printf (_("128-bit IEEE long double\n"));
17789 break;
17791 return p;
17794 if (tag == Tag_GNU_Power_ABI_Vector)
17796 printf (" Tag_GNU_Power_ABI_Vector: ");
17797 if (p == end)
17799 printf (_("<corrupt>\n"));
17800 return p;
17802 READ_ULEB (val, p, end);
17804 if (val > 3)
17805 printf ("(%#x), ", val);
17807 switch (val & 3)
17809 case 0:
17810 printf (_("unspecified\n"));
17811 break;
17812 case 1:
17813 printf (_("generic\n"));
17814 break;
17815 case 2:
17816 printf ("AltiVec\n");
17817 break;
17818 case 3:
17819 printf ("SPE\n");
17820 break;
17822 return p;
17825 if (tag == Tag_GNU_Power_ABI_Struct_Return)
17827 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
17828 if (p == end)
17830 printf (_("<corrupt>\n"));
17831 return p;
17833 READ_ULEB (val, p, end);
17835 if (val > 2)
17836 printf ("(%#x), ", val);
17838 switch (val & 3)
17840 case 0:
17841 printf (_("unspecified\n"));
17842 break;
17843 case 1:
17844 printf ("r3/r4\n");
17845 break;
17846 case 2:
17847 printf (_("memory\n"));
17848 break;
17849 case 3:
17850 printf ("???\n");
17851 break;
17853 return p;
17856 return display_tag_value (tag & 1, p, end);
17859 static unsigned char *
17860 display_s390_gnu_attribute (unsigned char * p,
17861 unsigned int tag,
17862 const unsigned char * const end)
17864 unsigned int val;
17866 if (tag == Tag_GNU_S390_ABI_Vector)
17868 printf (" Tag_GNU_S390_ABI_Vector: ");
17869 READ_ULEB (val, p, end);
17871 switch (val)
17873 case 0:
17874 printf (_("any\n"));
17875 break;
17876 case 1:
17877 printf (_("software\n"));
17878 break;
17879 case 2:
17880 printf (_("hardware\n"));
17881 break;
17882 default:
17883 printf ("??? (%d)\n", val);
17884 break;
17886 return p;
17889 return display_tag_value (tag & 1, p, end);
17892 static void
17893 display_sparc_hwcaps (unsigned int mask)
17895 if (mask)
17897 bool first = true;
17899 if (mask & ELF_SPARC_HWCAP_MUL32)
17900 fputs ("mul32", stdout), first = false;
17901 if (mask & ELF_SPARC_HWCAP_DIV32)
17902 printf ("%sdiv32", first ? "" : "|"), first = false;
17903 if (mask & ELF_SPARC_HWCAP_FSMULD)
17904 printf ("%sfsmuld", first ? "" : "|"), first = false;
17905 if (mask & ELF_SPARC_HWCAP_V8PLUS)
17906 printf ("%sv8plus", first ? "" : "|"), first = false;
17907 if (mask & ELF_SPARC_HWCAP_POPC)
17908 printf ("%spopc", first ? "" : "|"), first = false;
17909 if (mask & ELF_SPARC_HWCAP_VIS)
17910 printf ("%svis", first ? "" : "|"), first = false;
17911 if (mask & ELF_SPARC_HWCAP_VIS2)
17912 printf ("%svis2", first ? "" : "|"), first = false;
17913 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
17914 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
17915 if (mask & ELF_SPARC_HWCAP_FMAF)
17916 printf ("%sfmaf", first ? "" : "|"), first = false;
17917 if (mask & ELF_SPARC_HWCAP_VIS3)
17918 printf ("%svis3", first ? "" : "|"), first = false;
17919 if (mask & ELF_SPARC_HWCAP_HPC)
17920 printf ("%shpc", first ? "" : "|"), first = false;
17921 if (mask & ELF_SPARC_HWCAP_RANDOM)
17922 printf ("%srandom", first ? "" : "|"), first = false;
17923 if (mask & ELF_SPARC_HWCAP_TRANS)
17924 printf ("%strans", first ? "" : "|"), first = false;
17925 if (mask & ELF_SPARC_HWCAP_FJFMAU)
17926 printf ("%sfjfmau", first ? "" : "|"), first = false;
17927 if (mask & ELF_SPARC_HWCAP_IMA)
17928 printf ("%sima", first ? "" : "|"), first = false;
17929 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
17930 printf ("%scspare", first ? "" : "|"), first = false;
17932 else
17933 fputc ('0', stdout);
17934 fputc ('\n', stdout);
17937 static void
17938 display_sparc_hwcaps2 (unsigned int mask)
17940 if (mask)
17942 bool first = true;
17944 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
17945 fputs ("fjathplus", stdout), first = false;
17946 if (mask & ELF_SPARC_HWCAP2_VIS3B)
17947 printf ("%svis3b", first ? "" : "|"), first = false;
17948 if (mask & ELF_SPARC_HWCAP2_ADP)
17949 printf ("%sadp", first ? "" : "|"), first = false;
17950 if (mask & ELF_SPARC_HWCAP2_SPARC5)
17951 printf ("%ssparc5", first ? "" : "|"), first = false;
17952 if (mask & ELF_SPARC_HWCAP2_MWAIT)
17953 printf ("%smwait", first ? "" : "|"), first = false;
17954 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17955 printf ("%sxmpmul", first ? "" : "|"), first = false;
17956 if (mask & ELF_SPARC_HWCAP2_XMONT)
17957 printf ("%sxmont2", first ? "" : "|"), first = false;
17958 if (mask & ELF_SPARC_HWCAP2_NSEC)
17959 printf ("%snsec", first ? "" : "|"), first = false;
17960 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17961 printf ("%sfjathhpc", first ? "" : "|"), first = false;
17962 if (mask & ELF_SPARC_HWCAP2_FJDES)
17963 printf ("%sfjdes", first ? "" : "|"), first = false;
17964 if (mask & ELF_SPARC_HWCAP2_FJAES)
17965 printf ("%sfjaes", first ? "" : "|"), first = false;
17967 else
17968 fputc ('0', stdout);
17969 fputc ('\n', stdout);
17972 static unsigned char *
17973 display_sparc_gnu_attribute (unsigned char * p,
17974 unsigned int tag,
17975 const unsigned char * const end)
17977 unsigned int val;
17979 if (tag == Tag_GNU_Sparc_HWCAPS)
17981 READ_ULEB (val, p, end);
17982 printf (" Tag_GNU_Sparc_HWCAPS: ");
17983 display_sparc_hwcaps (val);
17984 return p;
17986 if (tag == Tag_GNU_Sparc_HWCAPS2)
17988 READ_ULEB (val, p, end);
17989 printf (" Tag_GNU_Sparc_HWCAPS2: ");
17990 display_sparc_hwcaps2 (val);
17991 return p;
17994 return display_tag_value (tag, p, end);
17997 static void
17998 print_mips_fp_abi_value (unsigned int val)
18000 switch (val)
18002 case Val_GNU_MIPS_ABI_FP_ANY:
18003 printf (_("Hard or soft float\n"));
18004 break;
18005 case Val_GNU_MIPS_ABI_FP_DOUBLE:
18006 printf (_("Hard float (double precision)\n"));
18007 break;
18008 case Val_GNU_MIPS_ABI_FP_SINGLE:
18009 printf (_("Hard float (single precision)\n"));
18010 break;
18011 case Val_GNU_MIPS_ABI_FP_SOFT:
18012 printf (_("Soft float\n"));
18013 break;
18014 case Val_GNU_MIPS_ABI_FP_OLD_64:
18015 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
18016 break;
18017 case Val_GNU_MIPS_ABI_FP_XX:
18018 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
18019 break;
18020 case Val_GNU_MIPS_ABI_FP_64:
18021 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
18022 break;
18023 case Val_GNU_MIPS_ABI_FP_64A:
18024 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
18025 break;
18026 case Val_GNU_MIPS_ABI_FP_NAN2008:
18027 printf (_("NaN 2008 compatibility\n"));
18028 break;
18029 default:
18030 printf ("??? (%d)\n", val);
18031 break;
18035 static unsigned char *
18036 display_mips_gnu_attribute (unsigned char * p,
18037 unsigned int tag,
18038 const unsigned char * const end)
18040 if (tag == Tag_GNU_MIPS_ABI_FP)
18042 unsigned int val;
18044 printf (" Tag_GNU_MIPS_ABI_FP: ");
18045 READ_ULEB (val, p, end);
18046 print_mips_fp_abi_value (val);
18047 return p;
18050 if (tag == Tag_GNU_MIPS_ABI_MSA)
18052 unsigned int val;
18054 printf (" Tag_GNU_MIPS_ABI_MSA: ");
18055 READ_ULEB (val, p, end);
18057 switch (val)
18059 case Val_GNU_MIPS_ABI_MSA_ANY:
18060 printf (_("Any MSA or not\n"));
18061 break;
18062 case Val_GNU_MIPS_ABI_MSA_128:
18063 printf (_("128-bit MSA\n"));
18064 break;
18065 default:
18066 printf ("??? (%d)\n", val);
18067 break;
18069 return p;
18072 return display_tag_value (tag & 1, p, end);
18075 static unsigned char *
18076 display_tic6x_attribute (unsigned char * p,
18077 const unsigned char * const end)
18079 unsigned int tag;
18080 unsigned int val;
18082 READ_ULEB (tag, p, end);
18084 switch (tag)
18086 case Tag_ISA:
18087 printf (" Tag_ISA: ");
18088 READ_ULEB (val, p, end);
18090 switch (val)
18092 case C6XABI_Tag_ISA_none:
18093 printf (_("None\n"));
18094 break;
18095 case C6XABI_Tag_ISA_C62X:
18096 printf ("C62x\n");
18097 break;
18098 case C6XABI_Tag_ISA_C67X:
18099 printf ("C67x\n");
18100 break;
18101 case C6XABI_Tag_ISA_C67XP:
18102 printf ("C67x+\n");
18103 break;
18104 case C6XABI_Tag_ISA_C64X:
18105 printf ("C64x\n");
18106 break;
18107 case C6XABI_Tag_ISA_C64XP:
18108 printf ("C64x+\n");
18109 break;
18110 case C6XABI_Tag_ISA_C674X:
18111 printf ("C674x\n");
18112 break;
18113 default:
18114 printf ("??? (%d)\n", val);
18115 break;
18117 return p;
18119 case Tag_ABI_wchar_t:
18120 printf (" Tag_ABI_wchar_t: ");
18121 READ_ULEB (val, p, end);
18122 switch (val)
18124 case 0:
18125 printf (_("Not used\n"));
18126 break;
18127 case 1:
18128 printf (_("2 bytes\n"));
18129 break;
18130 case 2:
18131 printf (_("4 bytes\n"));
18132 break;
18133 default:
18134 printf ("??? (%d)\n", val);
18135 break;
18137 return p;
18139 case Tag_ABI_stack_align_needed:
18140 printf (" Tag_ABI_stack_align_needed: ");
18141 READ_ULEB (val, p, end);
18142 switch (val)
18144 case 0:
18145 printf (_("8-byte\n"));
18146 break;
18147 case 1:
18148 printf (_("16-byte\n"));
18149 break;
18150 default:
18151 printf ("??? (%d)\n", val);
18152 break;
18154 return p;
18156 case Tag_ABI_stack_align_preserved:
18157 READ_ULEB (val, p, end);
18158 printf (" Tag_ABI_stack_align_preserved: ");
18159 switch (val)
18161 case 0:
18162 printf (_("8-byte\n"));
18163 break;
18164 case 1:
18165 printf (_("16-byte\n"));
18166 break;
18167 default:
18168 printf ("??? (%d)\n", val);
18169 break;
18171 return p;
18173 case Tag_ABI_DSBT:
18174 READ_ULEB (val, p, end);
18175 printf (" Tag_ABI_DSBT: ");
18176 switch (val)
18178 case 0:
18179 printf (_("DSBT addressing not used\n"));
18180 break;
18181 case 1:
18182 printf (_("DSBT addressing used\n"));
18183 break;
18184 default:
18185 printf ("??? (%d)\n", val);
18186 break;
18188 return p;
18190 case Tag_ABI_PID:
18191 READ_ULEB (val, p, end);
18192 printf (" Tag_ABI_PID: ");
18193 switch (val)
18195 case 0:
18196 printf (_("Data addressing position-dependent\n"));
18197 break;
18198 case 1:
18199 printf (_("Data addressing position-independent, GOT near DP\n"));
18200 break;
18201 case 2:
18202 printf (_("Data addressing position-independent, GOT far from DP\n"));
18203 break;
18204 default:
18205 printf ("??? (%d)\n", val);
18206 break;
18208 return p;
18210 case Tag_ABI_PIC:
18211 READ_ULEB (val, p, end);
18212 printf (" Tag_ABI_PIC: ");
18213 switch (val)
18215 case 0:
18216 printf (_("Code addressing position-dependent\n"));
18217 break;
18218 case 1:
18219 printf (_("Code addressing position-independent\n"));
18220 break;
18221 default:
18222 printf ("??? (%d)\n", val);
18223 break;
18225 return p;
18227 case Tag_ABI_array_object_alignment:
18228 READ_ULEB (val, p, end);
18229 printf (" Tag_ABI_array_object_alignment: ");
18230 switch (val)
18232 case 0:
18233 printf (_("8-byte\n"));
18234 break;
18235 case 1:
18236 printf (_("4-byte\n"));
18237 break;
18238 case 2:
18239 printf (_("16-byte\n"));
18240 break;
18241 default:
18242 printf ("??? (%d)\n", val);
18243 break;
18245 return p;
18247 case Tag_ABI_array_object_align_expected:
18248 READ_ULEB (val, p, end);
18249 printf (" Tag_ABI_array_object_align_expected: ");
18250 switch (val)
18252 case 0:
18253 printf (_("8-byte\n"));
18254 break;
18255 case 1:
18256 printf (_("4-byte\n"));
18257 break;
18258 case 2:
18259 printf (_("16-byte\n"));
18260 break;
18261 default:
18262 printf ("??? (%d)\n", val);
18263 break;
18265 return p;
18267 case Tag_ABI_compatibility:
18269 READ_ULEB (val, p, end);
18270 printf (" Tag_ABI_compatibility: ");
18271 printf (_("flag = %d, vendor = "), val);
18272 if (p < end - 1)
18274 size_t maxlen = (end - p) - 1;
18276 print_symbol_name ((int) maxlen, (const char *) p);
18277 p += strnlen ((char *) p, maxlen) + 1;
18279 else
18281 printf (_("<corrupt>"));
18282 p = (unsigned char *) end;
18284 putchar ('\n');
18285 return p;
18288 case Tag_ABI_conformance:
18290 printf (" Tag_ABI_conformance: \"");
18291 if (p < end - 1)
18293 size_t maxlen = (end - p) - 1;
18295 print_symbol_name ((int) maxlen, (const char *) p);
18296 p += strnlen ((char *) p, maxlen) + 1;
18298 else
18300 printf (_("<corrupt>"));
18301 p = (unsigned char *) end;
18303 printf ("\"\n");
18304 return p;
18308 return display_tag_value (tag, p, end);
18311 static void
18312 display_raw_attribute (unsigned char * p, unsigned char const * const end)
18314 uint64_t addr = 0;
18315 size_t bytes = end - p;
18317 assert (end >= p);
18318 while (bytes)
18320 int j;
18321 int k;
18322 int lbytes = (bytes > 16 ? 16 : bytes);
18324 printf (" 0x%8.8" PRIx64 " ", addr);
18326 for (j = 0; j < 16; j++)
18328 if (j < lbytes)
18329 printf ("%2.2x", p[j]);
18330 else
18331 printf (" ");
18333 if ((j & 3) == 3)
18334 printf (" ");
18337 for (j = 0; j < lbytes; j++)
18339 k = p[j];
18340 if (k >= ' ' && k < 0x7f)
18341 printf ("%c", k);
18342 else
18343 printf (".");
18346 putchar ('\n');
18348 p += lbytes;
18349 bytes -= lbytes;
18350 addr += lbytes;
18353 putchar ('\n');
18356 static unsigned char *
18357 display_msp430_attribute (unsigned char * p,
18358 const unsigned char * const end)
18360 uint64_t val;
18361 uint64_t tag;
18363 READ_ULEB (tag, p, end);
18365 switch (tag)
18367 case OFBA_MSPABI_Tag_ISA:
18368 printf (" Tag_ISA: ");
18369 READ_ULEB (val, p, end);
18370 switch (val)
18372 case 0: printf (_("None\n")); break;
18373 case 1: printf (_("MSP430\n")); break;
18374 case 2: printf (_("MSP430X\n")); break;
18375 default: printf ("??? (%" PRId64 ")\n", val); break;
18377 break;
18379 case OFBA_MSPABI_Tag_Code_Model:
18380 printf (" Tag_Code_Model: ");
18381 READ_ULEB (val, p, end);
18382 switch (val)
18384 case 0: printf (_("None\n")); break;
18385 case 1: printf (_("Small\n")); break;
18386 case 2: printf (_("Large\n")); break;
18387 default: printf ("??? (%" PRId64 ")\n", val); break;
18389 break;
18391 case OFBA_MSPABI_Tag_Data_Model:
18392 printf (" Tag_Data_Model: ");
18393 READ_ULEB (val, p, end);
18394 switch (val)
18396 case 0: printf (_("None\n")); break;
18397 case 1: printf (_("Small\n")); break;
18398 case 2: printf (_("Large\n")); break;
18399 case 3: printf (_("Restricted Large\n")); break;
18400 default: printf ("??? (%" PRId64 ")\n", val); break;
18402 break;
18404 default:
18405 printf (_(" <unknown tag %" PRId64 ">: "), tag);
18407 if (tag & 1)
18409 putchar ('"');
18410 if (p < end - 1)
18412 size_t maxlen = (end - p) - 1;
18414 print_symbol_name ((int) maxlen, (const char *) p);
18415 p += strnlen ((char *) p, maxlen) + 1;
18417 else
18419 printf (_("<corrupt>"));
18420 p = (unsigned char *) end;
18422 printf ("\"\n");
18424 else
18426 READ_ULEB (val, p, end);
18427 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
18429 break;
18432 assert (p <= end);
18433 return p;
18436 static unsigned char *
18437 display_msp430_gnu_attribute (unsigned char * p,
18438 unsigned int tag,
18439 const unsigned char * const end)
18441 if (tag == Tag_GNU_MSP430_Data_Region)
18443 uint64_t val;
18445 printf (" Tag_GNU_MSP430_Data_Region: ");
18446 READ_ULEB (val, p, end);
18448 switch (val)
18450 case Val_GNU_MSP430_Data_Region_Any:
18451 printf (_("Any Region\n"));
18452 break;
18453 case Val_GNU_MSP430_Data_Region_Lower:
18454 printf (_("Lower Region Only\n"));
18455 break;
18456 default:
18457 printf ("??? (%" PRIu64 ")\n", val);
18459 return p;
18461 return display_tag_value (tag & 1, p, end);
18464 struct riscv_attr_tag_t {
18465 const char *name;
18466 unsigned int tag;
18469 static struct riscv_attr_tag_t riscv_attr_tag[] =
18471 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
18472 T(arch),
18473 T(priv_spec),
18474 T(priv_spec_minor),
18475 T(priv_spec_revision),
18476 T(unaligned_access),
18477 T(stack_align),
18478 #undef T
18481 static unsigned char *
18482 display_riscv_attribute (unsigned char *p,
18483 const unsigned char * const end)
18485 uint64_t val;
18486 uint64_t tag;
18487 struct riscv_attr_tag_t *attr = NULL;
18488 unsigned i;
18490 READ_ULEB (tag, p, end);
18492 /* Find the name of attribute. */
18493 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
18495 if (riscv_attr_tag[i].tag == tag)
18497 attr = &riscv_attr_tag[i];
18498 break;
18502 if (attr)
18503 printf (" %s: ", attr->name);
18504 else
18505 return display_tag_value (tag, p, end);
18507 switch (tag)
18509 case Tag_RISCV_priv_spec:
18510 case Tag_RISCV_priv_spec_minor:
18511 case Tag_RISCV_priv_spec_revision:
18512 READ_ULEB (val, p, end);
18513 printf ("%" PRIu64 "\n", val);
18514 break;
18515 case Tag_RISCV_unaligned_access:
18516 READ_ULEB (val, p, end);
18517 switch (val)
18519 case 0:
18520 printf (_("No unaligned access\n"));
18521 break;
18522 case 1:
18523 printf (_("Unaligned access\n"));
18524 break;
18526 break;
18527 case Tag_RISCV_stack_align:
18528 READ_ULEB (val, p, end);
18529 printf (_("%" PRIu64 "-bytes\n"), val);
18530 break;
18531 case Tag_RISCV_arch:
18532 p = display_tag_value (-1, p, end);
18533 break;
18534 default:
18535 return display_tag_value (tag, p, end);
18538 return p;
18541 static unsigned char *
18542 display_csky_attribute (unsigned char * p,
18543 const unsigned char * const end)
18545 uint64_t tag;
18546 uint64_t val;
18547 READ_ULEB (tag, p, end);
18549 if (tag >= Tag_CSKY_MAX)
18551 return display_tag_value (-1, p, end);
18554 switch (tag)
18556 case Tag_CSKY_ARCH_NAME:
18557 printf (" Tag_CSKY_ARCH_NAME:\t\t");
18558 return display_tag_value (-1, p, end);
18559 case Tag_CSKY_CPU_NAME:
18560 printf (" Tag_CSKY_CPU_NAME:\t\t");
18561 return display_tag_value (-1, p, end);
18563 case Tag_CSKY_ISA_FLAGS:
18564 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
18565 return display_tag_value (0, p, end);
18566 case Tag_CSKY_ISA_EXT_FLAGS:
18567 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
18568 return display_tag_value (0, p, end);
18570 case Tag_CSKY_DSP_VERSION:
18571 printf (" Tag_CSKY_DSP_VERSION:\t\t");
18572 READ_ULEB (val, p, end);
18573 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
18574 printf ("DSP Extension\n");
18575 else if (val == VAL_CSKY_DSP_VERSION_2)
18576 printf ("DSP 2.0\n");
18577 break;
18579 case Tag_CSKY_VDSP_VERSION:
18580 printf (" Tag_CSKY_VDSP_VERSION:\t");
18581 READ_ULEB (val, p, end);
18582 printf ("VDSP Version %" PRId64 "\n", val);
18583 break;
18585 case Tag_CSKY_FPU_VERSION:
18586 printf (" Tag_CSKY_FPU_VERSION:\t\t");
18587 READ_ULEB (val, p, end);
18588 if (val == VAL_CSKY_FPU_VERSION_1)
18589 printf ("ABIV1 FPU Version 1\n");
18590 else if (val == VAL_CSKY_FPU_VERSION_2)
18591 printf ("FPU Version 2\n");
18592 break;
18594 case Tag_CSKY_FPU_ABI:
18595 printf (" Tag_CSKY_FPU_ABI:\t\t");
18596 READ_ULEB (val, p, end);
18597 if (val == VAL_CSKY_FPU_ABI_HARD)
18598 printf ("Hard\n");
18599 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
18600 printf ("SoftFP\n");
18601 else if (val == VAL_CSKY_FPU_ABI_SOFT)
18602 printf ("Soft\n");
18603 break;
18604 case Tag_CSKY_FPU_ROUNDING:
18605 READ_ULEB (val, p, end);
18606 if (val == 1)
18608 printf (" Tag_CSKY_FPU_ROUNDING:\t");
18609 printf ("Needed\n");
18611 break;
18612 case Tag_CSKY_FPU_DENORMAL:
18613 READ_ULEB (val, p, end);
18614 if (val == 1)
18616 printf (" Tag_CSKY_FPU_DENORMAL:\t");
18617 printf ("Needed\n");
18619 break;
18620 case Tag_CSKY_FPU_Exception:
18621 READ_ULEB (val, p, end);
18622 if (val == 1)
18624 printf (" Tag_CSKY_FPU_Exception:\t");
18625 printf ("Needed\n");
18627 break;
18628 case Tag_CSKY_FPU_NUMBER_MODULE:
18629 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
18630 return display_tag_value (-1, p, end);
18631 case Tag_CSKY_FPU_HARDFP:
18632 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
18633 READ_ULEB (val, p, end);
18634 if (val & VAL_CSKY_FPU_HARDFP_HALF)
18635 printf (" Half");
18636 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
18637 printf (" Single");
18638 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
18639 printf (" Double");
18640 printf ("\n");
18641 break;
18642 default:
18643 return display_tag_value (tag, p, end);
18645 return p;
18648 static bool
18649 process_attributes (Filedata * filedata,
18650 const char * public_name,
18651 unsigned int proc_type,
18652 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
18653 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
18655 Elf_Internal_Shdr * sect;
18656 unsigned i;
18657 bool res = true;
18659 /* Find the section header so that we get the size. */
18660 for (i = 0, sect = filedata->section_headers;
18661 i < filedata->file_header.e_shnum;
18662 i++, sect++)
18664 unsigned char * contents;
18665 unsigned char * p;
18667 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
18668 continue;
18670 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
18671 sect->sh_size, _("attributes"));
18672 if (contents == NULL)
18674 res = false;
18675 continue;
18678 p = contents;
18679 /* The first character is the version of the attributes.
18680 Currently only version 1, (aka 'A') is recognised here. */
18681 if (*p != 'A')
18683 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
18684 res = false;
18686 else
18688 uint64_t section_len;
18690 section_len = sect->sh_size - 1;
18691 p++;
18693 while (section_len > 0)
18695 uint64_t attr_len;
18696 unsigned int namelen;
18697 bool public_section;
18698 bool gnu_section;
18700 if (section_len <= 4)
18702 error (_("Tag section ends prematurely\n"));
18703 res = false;
18704 break;
18706 attr_len = byte_get (p, 4);
18707 p += 4;
18709 if (attr_len > section_len)
18711 error (_("Bad attribute length (%u > %u)\n"),
18712 (unsigned) attr_len, (unsigned) section_len);
18713 attr_len = section_len;
18714 res = false;
18716 /* PR 17531: file: 001-101425-0.004 */
18717 else if (attr_len < 5)
18719 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
18720 res = false;
18721 break;
18724 section_len -= attr_len;
18725 attr_len -= 4;
18727 namelen = strnlen ((char *) p, attr_len) + 1;
18728 if (namelen == 0 || namelen >= attr_len)
18730 error (_("Corrupt attribute section name\n"));
18731 res = false;
18732 break;
18735 printf (_("Attribute Section: "));
18736 print_symbol_name (INT_MAX, (const char *) p);
18737 putchar ('\n');
18739 if (public_name && streq ((char *) p, public_name))
18740 public_section = true;
18741 else
18742 public_section = false;
18744 if (streq ((char *) p, "gnu"))
18745 gnu_section = true;
18746 else
18747 gnu_section = false;
18749 p += namelen;
18750 attr_len -= namelen;
18752 while (attr_len > 0 && p < contents + sect->sh_size)
18754 int tag;
18755 unsigned int val;
18756 uint64_t size;
18757 unsigned char * end;
18759 /* PR binutils/17531: Safe handling of corrupt files. */
18760 if (attr_len < 6)
18762 error (_("Unused bytes at end of section\n"));
18763 res = false;
18764 section_len = 0;
18765 break;
18768 tag = *(p++);
18769 size = byte_get (p, 4);
18770 if (size > attr_len)
18772 error (_("Bad subsection length (%u > %u)\n"),
18773 (unsigned) size, (unsigned) attr_len);
18774 res = false;
18775 size = attr_len;
18777 /* PR binutils/17531: Safe handling of corrupt files. */
18778 if (size < 6)
18780 error (_("Bad subsection length (%u < 6)\n"),
18781 (unsigned) size);
18782 res = false;
18783 section_len = 0;
18784 break;
18787 attr_len -= size;
18788 end = p + size - 1;
18789 assert (end <= contents + sect->sh_size);
18790 p += 4;
18792 switch (tag)
18794 case 1:
18795 printf (_("File Attributes\n"));
18796 break;
18797 case 2:
18798 printf (_("Section Attributes:"));
18799 goto do_numlist;
18800 case 3:
18801 printf (_("Symbol Attributes:"));
18802 /* Fall through. */
18803 do_numlist:
18804 for (;;)
18806 READ_ULEB (val, p, end);
18807 if (val == 0)
18808 break;
18809 printf (" %d", val);
18811 printf ("\n");
18812 break;
18813 default:
18814 printf (_("Unknown tag: %d\n"), tag);
18815 public_section = false;
18816 break;
18819 if (public_section && display_pub_attribute != NULL)
18821 while (p < end)
18822 p = display_pub_attribute (p, end);
18823 assert (p == end);
18825 else if (gnu_section && display_proc_gnu_attribute != NULL)
18827 while (p < end)
18828 p = display_gnu_attribute (p,
18829 display_proc_gnu_attribute,
18830 end);
18831 assert (p == end);
18833 else if (p < end)
18835 printf (_(" Unknown attribute:\n"));
18836 display_raw_attribute (p, end);
18837 p = end;
18839 else
18840 attr_len = 0;
18845 free (contents);
18848 return res;
18851 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18852 Print the Address, Access and Initial fields of an entry at VMA ADDR
18853 and return the VMA of the next entry, or -1 if there was a problem.
18854 Does not read from DATA_END or beyond. */
18856 static uint64_t
18857 print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
18858 unsigned char * data_end)
18860 printf (" ");
18861 print_vma (addr, LONG_HEX);
18862 printf (" ");
18863 if (addr < pltgot + 0xfff0)
18864 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18865 else
18866 printf ("%10s", "");
18867 printf (" ");
18868 if (data == NULL)
18869 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18870 else
18872 uint64_t entry;
18873 unsigned char * from = data + addr - pltgot;
18875 if (from + (is_32bit_elf ? 4 : 8) > data_end)
18877 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18878 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
18879 return (uint64_t) -1;
18881 else
18883 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18884 print_vma (entry, LONG_HEX);
18887 return addr + (is_32bit_elf ? 4 : 8);
18890 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18891 PLTGOT. Print the Address and Initial fields of an entry at VMA
18892 ADDR and return the VMA of the next entry. */
18894 static uint64_t
18895 print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
18897 printf (" ");
18898 print_vma (addr, LONG_HEX);
18899 printf (" ");
18900 if (data == NULL)
18901 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18902 else
18904 uint64_t entry;
18906 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18907 print_vma (entry, LONG_HEX);
18909 return addr + (is_32bit_elf ? 4 : 8);
18912 static void
18913 print_mips_ases (unsigned int mask)
18915 if (mask & AFL_ASE_DSP)
18916 fputs ("\n\tDSP ASE", stdout);
18917 if (mask & AFL_ASE_DSPR2)
18918 fputs ("\n\tDSP R2 ASE", stdout);
18919 if (mask & AFL_ASE_DSPR3)
18920 fputs ("\n\tDSP R3 ASE", stdout);
18921 if (mask & AFL_ASE_EVA)
18922 fputs ("\n\tEnhanced VA Scheme", stdout);
18923 if (mask & AFL_ASE_MCU)
18924 fputs ("\n\tMCU (MicroController) ASE", stdout);
18925 if (mask & AFL_ASE_MDMX)
18926 fputs ("\n\tMDMX ASE", stdout);
18927 if (mask & AFL_ASE_MIPS3D)
18928 fputs ("\n\tMIPS-3D ASE", stdout);
18929 if (mask & AFL_ASE_MT)
18930 fputs ("\n\tMT ASE", stdout);
18931 if (mask & AFL_ASE_SMARTMIPS)
18932 fputs ("\n\tSmartMIPS ASE", stdout);
18933 if (mask & AFL_ASE_VIRT)
18934 fputs ("\n\tVZ ASE", stdout);
18935 if (mask & AFL_ASE_MSA)
18936 fputs ("\n\tMSA ASE", stdout);
18937 if (mask & AFL_ASE_MIPS16)
18938 fputs ("\n\tMIPS16 ASE", stdout);
18939 if (mask & AFL_ASE_MICROMIPS)
18940 fputs ("\n\tMICROMIPS ASE", stdout);
18941 if (mask & AFL_ASE_XPA)
18942 fputs ("\n\tXPA ASE", stdout);
18943 if (mask & AFL_ASE_MIPS16E2)
18944 fputs ("\n\tMIPS16e2 ASE", stdout);
18945 if (mask & AFL_ASE_CRC)
18946 fputs ("\n\tCRC ASE", stdout);
18947 if (mask & AFL_ASE_GINV)
18948 fputs ("\n\tGINV ASE", stdout);
18949 if (mask & AFL_ASE_LOONGSON_MMI)
18950 fputs ("\n\tLoongson MMI ASE", stdout);
18951 if (mask & AFL_ASE_LOONGSON_CAM)
18952 fputs ("\n\tLoongson CAM ASE", stdout);
18953 if (mask & AFL_ASE_LOONGSON_EXT)
18954 fputs ("\n\tLoongson EXT ASE", stdout);
18955 if (mask & AFL_ASE_LOONGSON_EXT2)
18956 fputs ("\n\tLoongson EXT2 ASE", stdout);
18957 if (mask == 0)
18958 fprintf (stdout, "\n\t%s", _("None"));
18959 else if ((mask & ~AFL_ASE_MASK) != 0)
18960 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18963 static void
18964 print_mips_isa_ext (unsigned int isa_ext)
18966 switch (isa_ext)
18968 case 0:
18969 fputs (_("None"), stdout);
18970 break;
18971 case AFL_EXT_XLR:
18972 fputs ("RMI XLR", stdout);
18973 break;
18974 case AFL_EXT_OCTEON3:
18975 fputs ("Cavium Networks Octeon3", stdout);
18976 break;
18977 case AFL_EXT_OCTEON2:
18978 fputs ("Cavium Networks Octeon2", stdout);
18979 break;
18980 case AFL_EXT_OCTEONP:
18981 fputs ("Cavium Networks OcteonP", stdout);
18982 break;
18983 case AFL_EXT_OCTEON:
18984 fputs ("Cavium Networks Octeon", stdout);
18985 break;
18986 case AFL_EXT_5900:
18987 fputs ("Toshiba R5900", stdout);
18988 break;
18989 case AFL_EXT_4650:
18990 fputs ("MIPS R4650", stdout);
18991 break;
18992 case AFL_EXT_4010:
18993 fputs ("LSI R4010", stdout);
18994 break;
18995 case AFL_EXT_4100:
18996 fputs ("NEC VR4100", stdout);
18997 break;
18998 case AFL_EXT_3900:
18999 fputs ("Toshiba R3900", stdout);
19000 break;
19001 case AFL_EXT_10000:
19002 fputs ("MIPS R10000", stdout);
19003 break;
19004 case AFL_EXT_SB1:
19005 fputs ("Broadcom SB-1", stdout);
19006 break;
19007 case AFL_EXT_4111:
19008 fputs ("NEC VR4111/VR4181", stdout);
19009 break;
19010 case AFL_EXT_4120:
19011 fputs ("NEC VR4120", stdout);
19012 break;
19013 case AFL_EXT_5400:
19014 fputs ("NEC VR5400", stdout);
19015 break;
19016 case AFL_EXT_5500:
19017 fputs ("NEC VR5500", stdout);
19018 break;
19019 case AFL_EXT_LOONGSON_2E:
19020 fputs ("ST Microelectronics Loongson 2E", stdout);
19021 break;
19022 case AFL_EXT_LOONGSON_2F:
19023 fputs ("ST Microelectronics Loongson 2F", stdout);
19024 break;
19025 case AFL_EXT_INTERAPTIV_MR2:
19026 fputs ("Imagination interAptiv MR2", stdout);
19027 break;
19028 default:
19029 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
19033 static signed int
19034 get_mips_reg_size (int reg_size)
19036 return (reg_size == AFL_REG_NONE) ? 0
19037 : (reg_size == AFL_REG_32) ? 32
19038 : (reg_size == AFL_REG_64) ? 64
19039 : (reg_size == AFL_REG_128) ? 128
19040 : -1;
19043 static bool
19044 process_mips_specific (Filedata * filedata)
19046 Elf_Internal_Dyn * entry;
19047 Elf_Internal_Shdr *sect = NULL;
19048 size_t liblist_offset = 0;
19049 size_t liblistno = 0;
19050 size_t conflictsno = 0;
19051 size_t options_offset = 0;
19052 size_t conflicts_offset = 0;
19053 size_t pltrelsz = 0;
19054 size_t pltrel = 0;
19055 uint64_t pltgot = 0;
19056 uint64_t mips_pltgot = 0;
19057 uint64_t jmprel = 0;
19058 uint64_t local_gotno = 0;
19059 uint64_t gotsym = 0;
19060 uint64_t symtabno = 0;
19061 bool res = true;
19063 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
19064 display_mips_gnu_attribute))
19065 res = false;
19067 sect = find_section (filedata, ".MIPS.abiflags");
19069 if (sect != NULL)
19071 Elf_External_ABIFlags_v0 *abiflags_ext;
19072 Elf_Internal_ABIFlags_v0 abiflags_in;
19074 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
19076 error (_("Corrupt MIPS ABI Flags section.\n"));
19077 res = false;
19079 else
19081 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
19082 sect->sh_size, _("MIPS ABI Flags section"));
19083 if (abiflags_ext)
19085 abiflags_in.version = BYTE_GET (abiflags_ext->version);
19086 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
19087 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
19088 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
19089 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
19090 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
19091 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
19092 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
19093 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
19094 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
19095 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
19097 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
19098 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
19099 if (abiflags_in.isa_rev > 1)
19100 printf ("r%d", abiflags_in.isa_rev);
19101 printf ("\nGPR size: %d",
19102 get_mips_reg_size (abiflags_in.gpr_size));
19103 printf ("\nCPR1 size: %d",
19104 get_mips_reg_size (abiflags_in.cpr1_size));
19105 printf ("\nCPR2 size: %d",
19106 get_mips_reg_size (abiflags_in.cpr2_size));
19107 fputs ("\nFP ABI: ", stdout);
19108 print_mips_fp_abi_value (abiflags_in.fp_abi);
19109 fputs ("ISA Extension: ", stdout);
19110 print_mips_isa_ext (abiflags_in.isa_ext);
19111 fputs ("\nASEs:", stdout);
19112 print_mips_ases (abiflags_in.ases);
19113 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
19114 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
19115 fputc ('\n', stdout);
19116 free (abiflags_ext);
19121 /* We have a lot of special sections. Thanks SGI! */
19122 if (filedata->dynamic_section == NULL)
19124 /* No dynamic information available. See if there is static GOT. */
19125 sect = find_section (filedata, ".got");
19126 if (sect != NULL)
19128 unsigned char *data_end;
19129 unsigned char *data;
19130 uint64_t ent, end;
19131 int addr_size;
19133 pltgot = sect->sh_addr;
19135 ent = pltgot;
19136 addr_size = (is_32bit_elf ? 4 : 8);
19137 end = pltgot + sect->sh_size;
19139 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
19140 end - pltgot, 1,
19141 _("Global Offset Table data"));
19142 /* PR 12855: Null data is handled gracefully throughout. */
19143 data_end = data + (end - pltgot);
19145 printf (_("\nStatic GOT:\n"));
19146 printf (_(" Canonical gp value: "));
19147 print_vma (ent + 0x7ff0, LONG_HEX);
19148 printf ("\n\n");
19150 /* In a dynamic binary GOT[0] is reserved for the dynamic
19151 loader to store the lazy resolver pointer, however in
19152 a static binary it may well have been omitted and GOT
19153 reduced to a table of addresses.
19154 PR 21344: Check for the entry being fully available
19155 before fetching it. */
19156 if (data
19157 && data + ent - pltgot + addr_size <= data_end
19158 && byte_get (data + ent - pltgot, addr_size) == 0)
19160 printf (_(" Reserved entries:\n"));
19161 printf (_(" %*s %10s %*s\n"),
19162 addr_size * 2, _("Address"), _("Access"),
19163 addr_size * 2, _("Value"));
19164 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19165 printf ("\n");
19166 if (ent == (uint64_t) -1)
19167 goto sgot_print_fail;
19169 /* Check for the MSB of GOT[1] being set, identifying a
19170 GNU object. This entry will be used by some runtime
19171 loaders, to store the module pointer. Otherwise this
19172 is an ordinary local entry.
19173 PR 21344: Check for the entry being fully available
19174 before fetching it. */
19175 if (data
19176 && data + ent - pltgot + addr_size <= data_end
19177 && (byte_get (data + ent - pltgot, addr_size)
19178 >> (addr_size * 8 - 1)) != 0)
19180 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19181 printf ("\n");
19182 if (ent == (uint64_t) -1)
19183 goto sgot_print_fail;
19185 printf ("\n");
19188 if (data != NULL && ent < end)
19190 printf (_(" Local entries:\n"));
19191 printf (" %*s %10s %*s\n",
19192 addr_size * 2, _("Address"), _("Access"),
19193 addr_size * 2, _("Value"));
19194 while (ent < end)
19196 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19197 printf ("\n");
19198 if (ent == (uint64_t) -1)
19199 goto sgot_print_fail;
19201 printf ("\n");
19204 sgot_print_fail:
19205 free (data);
19207 return res;
19210 for (entry = filedata->dynamic_section;
19211 /* PR 17531 file: 012-50589-0.004. */
19212 (entry < filedata->dynamic_section + filedata->dynamic_nent
19213 && entry->d_tag != DT_NULL);
19214 ++entry)
19215 switch (entry->d_tag)
19217 case DT_MIPS_LIBLIST:
19218 liblist_offset
19219 = offset_from_vma (filedata, entry->d_un.d_val,
19220 liblistno * sizeof (Elf32_External_Lib));
19221 break;
19222 case DT_MIPS_LIBLISTNO:
19223 liblistno = entry->d_un.d_val;
19224 break;
19225 case DT_MIPS_OPTIONS:
19226 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
19227 break;
19228 case DT_MIPS_CONFLICT:
19229 conflicts_offset
19230 = offset_from_vma (filedata, entry->d_un.d_val,
19231 conflictsno * sizeof (Elf32_External_Conflict));
19232 break;
19233 case DT_MIPS_CONFLICTNO:
19234 conflictsno = entry->d_un.d_val;
19235 break;
19236 case DT_PLTGOT:
19237 pltgot = entry->d_un.d_ptr;
19238 break;
19239 case DT_MIPS_LOCAL_GOTNO:
19240 local_gotno = entry->d_un.d_val;
19241 break;
19242 case DT_MIPS_GOTSYM:
19243 gotsym = entry->d_un.d_val;
19244 break;
19245 case DT_MIPS_SYMTABNO:
19246 symtabno = entry->d_un.d_val;
19247 break;
19248 case DT_MIPS_PLTGOT:
19249 mips_pltgot = entry->d_un.d_ptr;
19250 break;
19251 case DT_PLTREL:
19252 pltrel = entry->d_un.d_val;
19253 break;
19254 case DT_PLTRELSZ:
19255 pltrelsz = entry->d_un.d_val;
19256 break;
19257 case DT_JMPREL:
19258 jmprel = entry->d_un.d_ptr;
19259 break;
19260 default:
19261 break;
19264 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
19266 Elf32_External_Lib * elib;
19267 size_t cnt;
19269 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
19270 sizeof (Elf32_External_Lib),
19271 liblistno,
19272 _("liblist section data"));
19273 if (elib)
19275 printf (ngettext ("\nSection '.liblist' contains %zu entry:\n",
19276 "\nSection '.liblist' contains %zu entries:\n",
19277 liblistno),
19278 liblistno);
19279 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
19280 stdout);
19282 for (cnt = 0; cnt < liblistno; ++cnt)
19284 Elf32_Lib liblist;
19285 time_t atime;
19286 char timebuf[128];
19287 struct tm * tmp;
19289 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19290 atime = BYTE_GET (elib[cnt].l_time_stamp);
19291 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19292 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19293 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19295 tmp = gmtime (&atime);
19296 snprintf (timebuf, sizeof (timebuf),
19297 "%04u-%02u-%02uT%02u:%02u:%02u",
19298 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19299 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19301 printf ("%3zu: ", cnt);
19302 if (valid_dynamic_name (filedata, liblist.l_name))
19303 print_symbol_name (20, get_dynamic_name (filedata, liblist.l_name));
19304 else
19305 printf (_("<corrupt: %9ld>"), liblist.l_name);
19306 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
19307 liblist.l_version);
19309 if (liblist.l_flags == 0)
19310 puts (_(" NONE"));
19311 else
19313 static const struct
19315 const char * name;
19316 int bit;
19318 l_flags_vals[] =
19320 { " EXACT_MATCH", LL_EXACT_MATCH },
19321 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
19322 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
19323 { " EXPORTS", LL_EXPORTS },
19324 { " DELAY_LOAD", LL_DELAY_LOAD },
19325 { " DELTA", LL_DELTA }
19327 int flags = liblist.l_flags;
19328 size_t fcnt;
19330 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
19331 if ((flags & l_flags_vals[fcnt].bit) != 0)
19333 fputs (l_flags_vals[fcnt].name, stdout);
19334 flags ^= l_flags_vals[fcnt].bit;
19336 if (flags != 0)
19337 printf (" %#x", (unsigned int) flags);
19339 puts ("");
19343 free (elib);
19345 else
19346 res = false;
19349 if (options_offset != 0)
19351 Elf_External_Options * eopt;
19352 size_t offset;
19353 int cnt;
19355 /* Find the section header so that we get the size. */
19356 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
19357 /* PR 17533 file: 012-277276-0.004. */
19358 if (sect == NULL)
19360 error (_("No MIPS_OPTIONS header found\n"));
19361 return false;
19363 /* PR 24243 */
19364 if (sect->sh_size < sizeof (* eopt))
19366 error (_("The MIPS options section is too small.\n"));
19367 return false;
19370 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
19371 sect->sh_size, _("options"));
19372 if (eopt)
19374 Elf_Internal_Options option;
19376 offset = cnt = 0;
19377 while (offset <= sect->sh_size - sizeof (* eopt))
19379 Elf_External_Options * eoption;
19380 unsigned int optsize;
19382 eoption = (Elf_External_Options *) ((char *) eopt + offset);
19384 optsize = BYTE_GET (eoption->size);
19386 /* PR 17531: file: ffa0fa3b. */
19387 if (optsize < sizeof (* eopt)
19388 || optsize > sect->sh_size - offset)
19390 error (_("Invalid size (%u) for MIPS option\n"),
19391 optsize);
19392 free (eopt);
19393 return false;
19395 offset += optsize;
19396 ++cnt;
19399 printf (ngettext ("\nSection '%s' contains %d entry:\n",
19400 "\nSection '%s' contains %d entries:\n",
19401 cnt),
19402 printable_section_name (filedata, sect), cnt);
19404 offset = 0;
19405 while (cnt-- > 0)
19407 size_t len;
19408 Elf_External_Options * eoption;
19410 eoption = (Elf_External_Options *) ((char *) eopt + offset);
19412 option.kind = BYTE_GET (eoption->kind);
19413 option.size = BYTE_GET (eoption->size);
19414 option.section = BYTE_GET (eoption->section);
19415 option.info = BYTE_GET (eoption->info);
19417 switch (option.kind)
19419 case ODK_NULL:
19420 /* This shouldn't happen. */
19421 printf (" NULL %" PRId16 " %" PRIx32,
19422 option.section, option.info);
19423 break;
19425 case ODK_REGINFO:
19426 printf (" REGINFO ");
19427 if (filedata->file_header.e_machine == EM_MIPS)
19429 Elf32_External_RegInfo * ereg;
19430 Elf32_RegInfo reginfo;
19432 /* 32bit form. */
19433 if (option.size < (sizeof (Elf_External_Options)
19434 + sizeof (Elf32_External_RegInfo)))
19436 printf (_("<corrupt>\n"));
19437 error (_("Truncated MIPS REGINFO option\n"));
19438 cnt = 0;
19439 break;
19442 ereg = (Elf32_External_RegInfo *) (eoption + 1);
19444 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
19445 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
19446 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
19447 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
19448 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
19449 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
19451 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
19452 reginfo.ri_gprmask, reginfo.ri_gp_value);
19453 printf (" "
19454 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
19455 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
19456 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
19457 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
19459 else
19461 /* 64 bit form. */
19462 Elf64_External_RegInfo * ereg;
19463 Elf64_Internal_RegInfo reginfo;
19465 if (option.size < (sizeof (Elf_External_Options)
19466 + sizeof (Elf64_External_RegInfo)))
19468 printf (_("<corrupt>\n"));
19469 error (_("Truncated MIPS REGINFO option\n"));
19470 cnt = 0;
19471 break;
19474 ereg = (Elf64_External_RegInfo *) (eoption + 1);
19475 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
19476 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
19477 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
19478 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
19479 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
19480 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
19482 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
19483 reginfo.ri_gprmask, reginfo.ri_gp_value);
19484 printf (" "
19485 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
19486 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
19487 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
19488 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
19490 offset += option.size;
19491 continue;
19493 case ODK_EXCEPTIONS:
19494 fputs (" EXCEPTIONS fpe_min(", stdout);
19495 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
19496 fputs (") fpe_max(", stdout);
19497 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
19498 fputs (")", stdout);
19500 if (option.info & OEX_PAGE0)
19501 fputs (" PAGE0", stdout);
19502 if (option.info & OEX_SMM)
19503 fputs (" SMM", stdout);
19504 if (option.info & OEX_FPDBUG)
19505 fputs (" FPDBUG", stdout);
19506 if (option.info & OEX_DISMISS)
19507 fputs (" DISMISS", stdout);
19508 break;
19510 case ODK_PAD:
19511 fputs (" PAD ", stdout);
19512 if (option.info & OPAD_PREFIX)
19513 fputs (" PREFIX", stdout);
19514 if (option.info & OPAD_POSTFIX)
19515 fputs (" POSTFIX", stdout);
19516 if (option.info & OPAD_SYMBOL)
19517 fputs (" SYMBOL", stdout);
19518 break;
19520 case ODK_HWPATCH:
19521 fputs (" HWPATCH ", stdout);
19522 if (option.info & OHW_R4KEOP)
19523 fputs (" R4KEOP", stdout);
19524 if (option.info & OHW_R8KPFETCH)
19525 fputs (" R8KPFETCH", stdout);
19526 if (option.info & OHW_R5KEOP)
19527 fputs (" R5KEOP", stdout);
19528 if (option.info & OHW_R5KCVTL)
19529 fputs (" R5KCVTL", stdout);
19530 break;
19532 case ODK_FILL:
19533 fputs (" FILL ", stdout);
19534 /* XXX Print content of info word? */
19535 break;
19537 case ODK_TAGS:
19538 fputs (" TAGS ", stdout);
19539 /* XXX Print content of info word? */
19540 break;
19542 case ODK_HWAND:
19543 fputs (" HWAND ", stdout);
19544 if (option.info & OHWA0_R4KEOP_CHECKED)
19545 fputs (" R4KEOP_CHECKED", stdout);
19546 if (option.info & OHWA0_R4KEOP_CLEAN)
19547 fputs (" R4KEOP_CLEAN", stdout);
19548 break;
19550 case ODK_HWOR:
19551 fputs (" HWOR ", stdout);
19552 if (option.info & OHWA0_R4KEOP_CHECKED)
19553 fputs (" R4KEOP_CHECKED", stdout);
19554 if (option.info & OHWA0_R4KEOP_CLEAN)
19555 fputs (" R4KEOP_CLEAN", stdout);
19556 break;
19558 case ODK_GP_GROUP:
19559 printf (" GP_GROUP %#06x self-contained %#06x",
19560 option.info & OGP_GROUP,
19561 (option.info & OGP_SELF) >> 16);
19562 break;
19564 case ODK_IDENT:
19565 printf (" IDENT %#06x self-contained %#06x",
19566 option.info & OGP_GROUP,
19567 (option.info & OGP_SELF) >> 16);
19568 break;
19570 default:
19571 /* This shouldn't happen. */
19572 printf (" %3d ??? %" PRId16 " %" PRIx32,
19573 option.kind, option.section, option.info);
19574 break;
19577 len = sizeof (* eopt);
19578 while (len < option.size)
19580 unsigned char datum = *((unsigned char *) eoption + len);
19582 if (ISPRINT (datum))
19583 printf ("%c", datum);
19584 else
19585 printf ("\\%03o", datum);
19586 len ++;
19588 fputs ("\n", stdout);
19590 offset += option.size;
19592 free (eopt);
19594 else
19595 res = false;
19598 if (conflicts_offset != 0 && conflictsno != 0)
19600 Elf32_Conflict * iconf;
19601 size_t cnt;
19603 if (filedata->dynamic_symbols == NULL)
19605 error (_("conflict list found without a dynamic symbol table\n"));
19606 return false;
19609 /* PR 21345 - print a slightly more helpful error message
19610 if we are sure that the cmalloc will fail. */
19611 if (conflictsno > filedata->file_size / sizeof (* iconf))
19613 error (_("Overlarge number of conflicts detected: %zx\n"),
19614 conflictsno);
19615 return false;
19618 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
19619 if (iconf == NULL)
19621 error (_("Out of memory allocating space for dynamic conflicts\n"));
19622 return false;
19625 if (is_32bit_elf)
19627 Elf32_External_Conflict * econf32;
19629 econf32 = (Elf32_External_Conflict *)
19630 get_data (NULL, filedata, conflicts_offset,
19631 sizeof (*econf32), conflictsno, _("conflict"));
19632 if (!econf32)
19634 free (iconf);
19635 return false;
19638 for (cnt = 0; cnt < conflictsno; ++cnt)
19639 iconf[cnt] = BYTE_GET (econf32[cnt]);
19641 free (econf32);
19643 else
19645 Elf64_External_Conflict * econf64;
19647 econf64 = (Elf64_External_Conflict *)
19648 get_data (NULL, filedata, conflicts_offset,
19649 sizeof (*econf64), conflictsno, _("conflict"));
19650 if (!econf64)
19652 free (iconf);
19653 return false;
19656 for (cnt = 0; cnt < conflictsno; ++cnt)
19657 iconf[cnt] = BYTE_GET (econf64[cnt]);
19659 free (econf64);
19662 printf (ngettext ("\nSection '.conflict' contains %zu entry:\n",
19663 "\nSection '.conflict' contains %zu entries:\n",
19664 conflictsno),
19665 conflictsno);
19666 puts (_(" Num: Index Value Name"));
19668 for (cnt = 0; cnt < conflictsno; ++cnt)
19670 printf ("%5zu: %8lu ", cnt, iconf[cnt]);
19672 if (iconf[cnt] >= filedata->num_dynamic_syms)
19673 printf (_("<corrupt symbol index>"));
19674 else
19676 Elf_Internal_Sym * psym;
19678 psym = & filedata->dynamic_symbols[iconf[cnt]];
19679 print_vma (psym->st_value, FULL_HEX);
19680 putchar (' ');
19681 if (valid_dynamic_name (filedata, psym->st_name))
19682 print_symbol_name (25, get_dynamic_name (filedata, psym->st_name));
19683 else
19684 printf (_("<corrupt: %14ld>"), psym->st_name);
19686 putchar ('\n');
19689 free (iconf);
19692 if (pltgot != 0 && local_gotno != 0)
19694 uint64_t ent, local_end, global_end;
19695 size_t i, offset;
19696 unsigned char * data;
19697 unsigned char * data_end;
19698 int addr_size;
19700 ent = pltgot;
19701 addr_size = (is_32bit_elf ? 4 : 8);
19702 local_end = pltgot + local_gotno * addr_size;
19704 /* PR binutils/17533 file: 012-111227-0.004 */
19705 if (symtabno < gotsym)
19707 error (_("The GOT symbol offset (%" PRIu64
19708 ") is greater than the symbol table size (%" PRIu64 ")\n"),
19709 gotsym, symtabno);
19710 return false;
19713 global_end = local_end + (symtabno - gotsym) * addr_size;
19714 /* PR 17531: file: 54c91a34. */
19715 if (global_end < local_end)
19717 error (_("Too many GOT symbols: %" PRIu64 "\n"), symtabno);
19718 return false;
19721 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
19722 data = (unsigned char *) get_data (NULL, filedata, offset,
19723 global_end - pltgot, 1,
19724 _("Global Offset Table data"));
19725 /* PR 12855: Null data is handled gracefully throughout. */
19726 data_end = data + (global_end - pltgot);
19728 printf (_("\nPrimary GOT:\n"));
19729 printf (_(" Canonical gp value: "));
19730 print_vma (pltgot + 0x7ff0, LONG_HEX);
19731 printf ("\n\n");
19733 printf (_(" Reserved entries:\n"));
19734 printf (_(" %*s %10s %*s Purpose\n"),
19735 addr_size * 2, _("Address"), _("Access"),
19736 addr_size * 2, _("Initial"));
19737 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19738 printf (_(" Lazy resolver\n"));
19739 if (ent == (uint64_t) -1)
19740 goto got_print_fail;
19742 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
19743 This entry will be used by some runtime loaders, to store the
19744 module pointer. Otherwise this is an ordinary local entry.
19745 PR 21344: Check for the entry being fully available before
19746 fetching it. */
19747 if (data
19748 && data + ent - pltgot + addr_size <= data_end
19749 && (byte_get (data + ent - pltgot, addr_size)
19750 >> (addr_size * 8 - 1)) != 0)
19752 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19753 printf (_(" Module pointer (GNU extension)\n"));
19754 if (ent == (uint64_t) -1)
19755 goto got_print_fail;
19757 printf ("\n");
19759 if (data != NULL && ent < local_end)
19761 printf (_(" Local entries:\n"));
19762 printf (" %*s %10s %*s\n",
19763 addr_size * 2, _("Address"), _("Access"),
19764 addr_size * 2, _("Initial"));
19765 while (ent < local_end)
19767 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19768 printf ("\n");
19769 if (ent == (uint64_t) -1)
19770 goto got_print_fail;
19772 printf ("\n");
19775 if (data != NULL && gotsym < symtabno)
19777 int sym_width;
19779 printf (_(" Global entries:\n"));
19780 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
19781 addr_size * 2, _("Address"),
19782 _("Access"),
19783 addr_size * 2, _("Initial"),
19784 addr_size * 2, _("Sym.Val."),
19785 _("Type"),
19786 /* Note for translators: "Ndx" = abbreviated form of "Index". */
19787 _("Ndx"), _("Name"));
19789 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
19791 for (i = gotsym; i < symtabno; i++)
19793 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19794 printf (" ");
19796 if (filedata->dynamic_symbols == NULL)
19797 printf (_("<no dynamic symbols>"));
19798 else if (i < filedata->num_dynamic_syms)
19800 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
19802 print_vma (psym->st_value, LONG_HEX);
19803 printf (" %-7s ", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
19805 bool is_special;
19806 const char * s = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
19807 if (is_special)
19808 printf ("%3s ", s);
19809 else
19810 printf ("%3u ", psym->st_shndx);
19812 if (valid_dynamic_name (filedata, psym->st_name))
19813 print_symbol_name (sym_width,
19814 get_dynamic_name (filedata, psym->st_name));
19815 else
19816 printf (_("<corrupt: %14ld>"), psym->st_name);
19818 else
19819 printf (_("<symbol index %zu exceeds number of dynamic symbols>"),
19822 printf ("\n");
19823 if (ent == (uint64_t) -1)
19824 break;
19826 printf ("\n");
19829 got_print_fail:
19830 free (data);
19833 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19835 uint64_t ent, end;
19836 uint64_t offset, rel_offset;
19837 uint64_t count, i;
19838 unsigned char * data;
19839 int addr_size, sym_width;
19840 Elf_Internal_Rela * rels;
19842 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
19843 if (pltrel == DT_RELA)
19845 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19846 return false;
19848 else
19850 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19851 return false;
19854 ent = mips_pltgot;
19855 addr_size = (is_32bit_elf ? 4 : 8);
19856 end = mips_pltgot + (2 + count) * addr_size;
19858 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19859 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
19860 1, _("Procedure Linkage Table data"));
19861 if (data == NULL)
19863 free (rels);
19864 return false;
19867 printf ("\nPLT GOT:\n\n");
19868 printf (_(" Reserved entries:\n"));
19869 printf (_(" %*s %*s Purpose\n"),
19870 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
19871 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19872 printf (_(" PLT lazy resolver\n"));
19873 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19874 printf (_(" Module pointer\n"));
19875 printf ("\n");
19877 printf (_(" Entries:\n"));
19878 printf (" %*s %*s %*s %-7s %3s %s\n",
19879 addr_size * 2, _("Address"),
19880 addr_size * 2, _("Initial"),
19881 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
19882 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19883 for (i = 0; i < count; i++)
19885 uint64_t idx = get_reloc_symindex (rels[i].r_info);
19887 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19888 printf (" ");
19890 if (idx >= filedata->num_dynamic_syms)
19891 printf (_("<corrupt symbol index: %" PRIu64 ">"), idx);
19892 else
19894 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
19896 print_vma (psym->st_value, LONG_HEX);
19897 printf (" %-7s %3s ",
19898 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19899 printable_section_name_from_index (filedata, psym->st_shndx, NULL));
19900 if (valid_dynamic_name (filedata, psym->st_name))
19901 print_symbol_name (sym_width,
19902 get_dynamic_name (filedata, psym->st_name));
19903 else
19904 printf (_("<corrupt: %14ld>"), psym->st_name);
19906 printf ("\n");
19908 printf ("\n");
19910 free (data);
19911 free (rels);
19914 return res;
19917 static bool
19918 process_nds32_specific (Filedata * filedata)
19920 Elf_Internal_Shdr *sect = NULL;
19922 sect = find_section (filedata, ".nds32_e_flags");
19923 if (sect != NULL && sect->sh_size >= 4)
19925 unsigned char *buf;
19926 unsigned int flag;
19928 printf ("\nNDS32 elf flags section:\n");
19929 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19930 _("NDS32 elf flags section"));
19932 if (buf == NULL)
19933 return false;
19935 flag = byte_get (buf, 4);
19936 free (buf);
19937 switch (flag & 0x3)
19939 case 0:
19940 printf ("(VEC_SIZE):\tNo entry.\n");
19941 break;
19942 case 1:
19943 printf ("(VEC_SIZE):\t4 bytes\n");
19944 break;
19945 case 2:
19946 printf ("(VEC_SIZE):\t16 bytes\n");
19947 break;
19948 case 3:
19949 printf ("(VEC_SIZE):\treserved\n");
19950 break;
19954 return true;
19957 static bool
19958 process_gnu_liblist (Filedata * filedata)
19960 Elf_Internal_Shdr * section;
19961 Elf_Internal_Shdr * string_sec;
19962 Elf32_External_Lib * elib;
19963 char * strtab;
19964 size_t strtab_size;
19965 size_t cnt;
19966 uint64_t num_liblist;
19967 unsigned i;
19968 bool res = true;
19970 if (! do_arch)
19971 return true;
19973 for (i = 0, section = filedata->section_headers;
19974 i < filedata->file_header.e_shnum;
19975 i++, section++)
19977 switch (section->sh_type)
19979 case SHT_GNU_LIBLIST:
19980 if (section->sh_link >= filedata->file_header.e_shnum)
19981 break;
19983 elib = (Elf32_External_Lib *)
19984 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
19985 _("liblist section data"));
19987 if (elib == NULL)
19989 res = false;
19990 break;
19993 string_sec = filedata->section_headers + section->sh_link;
19994 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
19995 string_sec->sh_size,
19996 _("liblist string table"));
19997 if (strtab == NULL
19998 || section->sh_entsize != sizeof (Elf32_External_Lib))
20000 free (elib);
20001 free (strtab);
20002 res = false;
20003 break;
20005 strtab_size = string_sec->sh_size;
20007 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
20008 printf (ngettext ("\nLibrary list section '%s' contains %" PRIu64
20009 " entries:\n",
20010 "\nLibrary list section '%s' contains %" PRIu64
20011 " entries:\n",
20012 num_liblist),
20013 printable_section_name (filedata, section),
20014 num_liblist);
20016 puts (_(" Library Time Stamp Checksum Version Flags"));
20018 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
20019 ++cnt)
20021 Elf32_Lib liblist;
20022 time_t atime;
20023 char timebuf[128];
20024 struct tm * tmp;
20026 liblist.l_name = BYTE_GET (elib[cnt].l_name);
20027 atime = BYTE_GET (elib[cnt].l_time_stamp);
20028 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
20029 liblist.l_version = BYTE_GET (elib[cnt].l_version);
20030 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
20032 tmp = gmtime (&atime);
20033 snprintf (timebuf, sizeof (timebuf),
20034 "%04u-%02u-%02uT%02u:%02u:%02u",
20035 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
20036 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
20038 printf ("%3zu: ", cnt);
20039 if (do_wide)
20040 printf ("%-20s", liblist.l_name < strtab_size
20041 ? strtab + liblist.l_name : _("<corrupt>"));
20042 else
20043 printf ("%-20.20s", liblist.l_name < strtab_size
20044 ? strtab + liblist.l_name : _("<corrupt>"));
20045 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
20046 liblist.l_version, liblist.l_flags);
20049 free (elib);
20050 free (strtab);
20054 return res;
20057 static const char *
20058 get_note_type (Filedata * filedata, unsigned e_type)
20060 static char buff[64];
20062 if (filedata->file_header.e_type == ET_CORE)
20063 switch (e_type)
20065 case NT_AUXV:
20066 return _("NT_AUXV (auxiliary vector)");
20067 case NT_PRSTATUS:
20068 return _("NT_PRSTATUS (prstatus structure)");
20069 case NT_FPREGSET:
20070 return _("NT_FPREGSET (floating point registers)");
20071 case NT_PRPSINFO:
20072 return _("NT_PRPSINFO (prpsinfo structure)");
20073 case NT_TASKSTRUCT:
20074 return _("NT_TASKSTRUCT (task structure)");
20075 case NT_GDB_TDESC:
20076 return _("NT_GDB_TDESC (GDB XML target description)");
20077 case NT_PRXFPREG:
20078 return _("NT_PRXFPREG (user_xfpregs structure)");
20079 case NT_PPC_VMX:
20080 return _("NT_PPC_VMX (ppc Altivec registers)");
20081 case NT_PPC_VSX:
20082 return _("NT_PPC_VSX (ppc VSX registers)");
20083 case NT_PPC_TAR:
20084 return _("NT_PPC_TAR (ppc TAR register)");
20085 case NT_PPC_PPR:
20086 return _("NT_PPC_PPR (ppc PPR register)");
20087 case NT_PPC_DSCR:
20088 return _("NT_PPC_DSCR (ppc DSCR register)");
20089 case NT_PPC_EBB:
20090 return _("NT_PPC_EBB (ppc EBB registers)");
20091 case NT_PPC_PMU:
20092 return _("NT_PPC_PMU (ppc PMU registers)");
20093 case NT_PPC_TM_CGPR:
20094 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
20095 case NT_PPC_TM_CFPR:
20096 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
20097 case NT_PPC_TM_CVMX:
20098 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
20099 case NT_PPC_TM_CVSX:
20100 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
20101 case NT_PPC_TM_SPR:
20102 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
20103 case NT_PPC_TM_CTAR:
20104 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
20105 case NT_PPC_TM_CPPR:
20106 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
20107 case NT_PPC_TM_CDSCR:
20108 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
20109 case NT_386_TLS:
20110 return _("NT_386_TLS (x86 TLS information)");
20111 case NT_386_IOPERM:
20112 return _("NT_386_IOPERM (x86 I/O permissions)");
20113 case NT_X86_XSTATE:
20114 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
20115 case NT_X86_CET:
20116 return _("NT_X86_CET (x86 CET state)");
20117 case NT_S390_HIGH_GPRS:
20118 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
20119 case NT_S390_TIMER:
20120 return _("NT_S390_TIMER (s390 timer register)");
20121 case NT_S390_TODCMP:
20122 return _("NT_S390_TODCMP (s390 TOD comparator register)");
20123 case NT_S390_TODPREG:
20124 return _("NT_S390_TODPREG (s390 TOD programmable register)");
20125 case NT_S390_CTRS:
20126 return _("NT_S390_CTRS (s390 control registers)");
20127 case NT_S390_PREFIX:
20128 return _("NT_S390_PREFIX (s390 prefix register)");
20129 case NT_S390_LAST_BREAK:
20130 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
20131 case NT_S390_SYSTEM_CALL:
20132 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
20133 case NT_S390_TDB:
20134 return _("NT_S390_TDB (s390 transaction diagnostic block)");
20135 case NT_S390_VXRS_LOW:
20136 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
20137 case NT_S390_VXRS_HIGH:
20138 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
20139 case NT_S390_GS_CB:
20140 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
20141 case NT_S390_GS_BC:
20142 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
20143 case NT_ARM_VFP:
20144 return _("NT_ARM_VFP (arm VFP registers)");
20145 case NT_ARM_TLS:
20146 return _("NT_ARM_TLS (AArch TLS registers)");
20147 case NT_ARM_HW_BREAK:
20148 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
20149 case NT_ARM_HW_WATCH:
20150 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
20151 case NT_ARM_SYSTEM_CALL:
20152 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
20153 case NT_ARM_SVE:
20154 return _("NT_ARM_SVE (AArch SVE registers)");
20155 case NT_ARM_PAC_MASK:
20156 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
20157 case NT_ARM_PACA_KEYS:
20158 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
20159 case NT_ARM_PACG_KEYS:
20160 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
20161 case NT_ARM_TAGGED_ADDR_CTRL:
20162 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
20163 case NT_ARM_SSVE:
20164 return _("NT_ARM_SSVE (AArch64 streaming SVE registers)");
20165 case NT_ARM_ZA:
20166 return _("NT_ARM_ZA (AArch64 SME ZA register)");
20167 case NT_ARM_ZT:
20168 return _("NT_ARM_ZT (AArch64 SME2 ZT registers)");
20169 case NT_ARM_PAC_ENABLED_KEYS:
20170 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
20171 case NT_ARC_V2:
20172 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
20173 case NT_RISCV_CSR:
20174 return _("NT_RISCV_CSR (RISC-V control and status registers)");
20175 case NT_PSTATUS:
20176 return _("NT_PSTATUS (pstatus structure)");
20177 case NT_FPREGS:
20178 return _("NT_FPREGS (floating point registers)");
20179 case NT_PSINFO:
20180 return _("NT_PSINFO (psinfo structure)");
20181 case NT_LWPSTATUS:
20182 return _("NT_LWPSTATUS (lwpstatus_t structure)");
20183 case NT_LWPSINFO:
20184 return _("NT_LWPSINFO (lwpsinfo_t structure)");
20185 case NT_WIN32PSTATUS:
20186 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
20187 case NT_SIGINFO:
20188 return _("NT_SIGINFO (siginfo_t data)");
20189 case NT_FILE:
20190 return _("NT_FILE (mapped files)");
20191 default:
20192 break;
20194 else
20195 switch (e_type)
20197 case NT_VERSION:
20198 return _("NT_VERSION (version)");
20199 case NT_ARCH:
20200 return _("NT_ARCH (architecture)");
20201 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
20202 return _("OPEN");
20203 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
20204 return _("func");
20205 case NT_GO_BUILDID:
20206 return _("GO BUILDID");
20207 case FDO_PACKAGING_METADATA:
20208 return _("FDO_PACKAGING_METADATA");
20209 default:
20210 break;
20213 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20214 return buff;
20217 static bool
20218 print_core_note (Elf_Internal_Note *pnote)
20220 unsigned int addr_size = is_32bit_elf ? 4 : 8;
20221 uint64_t count, page_size;
20222 unsigned char *descdata, *filenames, *descend;
20224 if (pnote->type != NT_FILE)
20226 if (do_wide)
20227 printf ("\n");
20228 return true;
20231 if (!is_32bit_elf)
20233 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
20234 /* Still "successful". */
20235 return true;
20238 if (pnote->descsz < 2 * addr_size)
20240 error (_(" Malformed note - too short for header\n"));
20241 return false;
20244 descdata = (unsigned char *) pnote->descdata;
20245 descend = descdata + pnote->descsz;
20247 if (descdata[pnote->descsz - 1] != '\0')
20249 error (_(" Malformed note - does not end with \\0\n"));
20250 return false;
20253 count = byte_get (descdata, addr_size);
20254 descdata += addr_size;
20256 page_size = byte_get (descdata, addr_size);
20257 descdata += addr_size;
20259 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
20260 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
20262 error (_(" Malformed note - too short for supplied file count\n"));
20263 return false;
20266 printf (_(" Page size: "));
20267 print_vma (page_size, DEC);
20268 printf ("\n");
20270 printf (_(" %*s%*s%*s\n"),
20271 (int) (2 + 2 * addr_size), _("Start"),
20272 (int) (4 + 2 * addr_size), _("End"),
20273 (int) (4 + 2 * addr_size), _("Page Offset"));
20274 filenames = descdata + count * 3 * addr_size;
20275 while (count-- > 0)
20277 uint64_t start, end, file_ofs;
20279 if (filenames == descend)
20281 error (_(" Malformed note - filenames end too early\n"));
20282 return false;
20285 start = byte_get (descdata, addr_size);
20286 descdata += addr_size;
20287 end = byte_get (descdata, addr_size);
20288 descdata += addr_size;
20289 file_ofs = byte_get (descdata, addr_size);
20290 descdata += addr_size;
20292 printf (" ");
20293 print_vma (start, FULL_HEX);
20294 printf (" ");
20295 print_vma (end, FULL_HEX);
20296 printf (" ");
20297 print_vma (file_ofs, FULL_HEX);
20298 printf ("\n %s\n", filenames);
20300 filenames += 1 + strlen ((char *) filenames);
20303 return true;
20306 static const char *
20307 get_gnu_elf_note_type (unsigned e_type)
20309 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
20310 switch (e_type)
20312 case NT_GNU_ABI_TAG:
20313 return _("NT_GNU_ABI_TAG (ABI version tag)");
20314 case NT_GNU_HWCAP:
20315 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
20316 case NT_GNU_BUILD_ID:
20317 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
20318 case NT_GNU_GOLD_VERSION:
20319 return _("NT_GNU_GOLD_VERSION (gold version)");
20320 case NT_GNU_PROPERTY_TYPE_0:
20321 return _("NT_GNU_PROPERTY_TYPE_0");
20322 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
20323 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
20324 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
20325 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
20326 default:
20328 static char buff[64];
20330 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20331 return buff;
20336 static void
20337 decode_x86_compat_isa (unsigned int bitmask)
20339 while (bitmask)
20341 unsigned int bit = bitmask & (- bitmask);
20343 bitmask &= ~ bit;
20344 switch (bit)
20346 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
20347 printf ("i486");
20348 break;
20349 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
20350 printf ("586");
20351 break;
20352 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
20353 printf ("686");
20354 break;
20355 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
20356 printf ("SSE");
20357 break;
20358 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
20359 printf ("SSE2");
20360 break;
20361 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
20362 printf ("SSE3");
20363 break;
20364 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
20365 printf ("SSSE3");
20366 break;
20367 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
20368 printf ("SSE4_1");
20369 break;
20370 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
20371 printf ("SSE4_2");
20372 break;
20373 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
20374 printf ("AVX");
20375 break;
20376 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
20377 printf ("AVX2");
20378 break;
20379 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
20380 printf ("AVX512F");
20381 break;
20382 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
20383 printf ("AVX512CD");
20384 break;
20385 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
20386 printf ("AVX512ER");
20387 break;
20388 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
20389 printf ("AVX512PF");
20390 break;
20391 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
20392 printf ("AVX512VL");
20393 break;
20394 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
20395 printf ("AVX512DQ");
20396 break;
20397 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
20398 printf ("AVX512BW");
20399 break;
20400 default:
20401 printf (_("<unknown: %x>"), bit);
20402 break;
20404 if (bitmask)
20405 printf (", ");
20409 static void
20410 decode_x86_compat_2_isa (unsigned int bitmask)
20412 if (!bitmask)
20414 printf (_("<None>"));
20415 return;
20418 while (bitmask)
20420 unsigned int bit = bitmask & (- bitmask);
20422 bitmask &= ~ bit;
20423 switch (bit)
20425 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
20426 printf ("CMOV");
20427 break;
20428 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
20429 printf ("SSE");
20430 break;
20431 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
20432 printf ("SSE2");
20433 break;
20434 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
20435 printf ("SSE3");
20436 break;
20437 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
20438 printf ("SSSE3");
20439 break;
20440 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
20441 printf ("SSE4_1");
20442 break;
20443 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
20444 printf ("SSE4_2");
20445 break;
20446 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
20447 printf ("AVX");
20448 break;
20449 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
20450 printf ("AVX2");
20451 break;
20452 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
20453 printf ("FMA");
20454 break;
20455 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
20456 printf ("AVX512F");
20457 break;
20458 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
20459 printf ("AVX512CD");
20460 break;
20461 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
20462 printf ("AVX512ER");
20463 break;
20464 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
20465 printf ("AVX512PF");
20466 break;
20467 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
20468 printf ("AVX512VL");
20469 break;
20470 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
20471 printf ("AVX512DQ");
20472 break;
20473 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
20474 printf ("AVX512BW");
20475 break;
20476 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
20477 printf ("AVX512_4FMAPS");
20478 break;
20479 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
20480 printf ("AVX512_4VNNIW");
20481 break;
20482 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
20483 printf ("AVX512_BITALG");
20484 break;
20485 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
20486 printf ("AVX512_IFMA");
20487 break;
20488 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
20489 printf ("AVX512_VBMI");
20490 break;
20491 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
20492 printf ("AVX512_VBMI2");
20493 break;
20494 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
20495 printf ("AVX512_VNNI");
20496 break;
20497 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
20498 printf ("AVX512_BF16");
20499 break;
20500 default:
20501 printf (_("<unknown: %x>"), bit);
20502 break;
20504 if (bitmask)
20505 printf (", ");
20509 static const char *
20510 get_amdgpu_elf_note_type (unsigned int e_type)
20512 switch (e_type)
20514 case NT_AMDGPU_METADATA:
20515 return _("NT_AMDGPU_METADATA (code object metadata)");
20516 default:
20518 static char buf[64];
20519 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
20520 return buf;
20525 static void
20526 decode_x86_isa (unsigned int bitmask)
20528 while (bitmask)
20530 unsigned int bit = bitmask & (- bitmask);
20532 bitmask &= ~ bit;
20533 switch (bit)
20535 case GNU_PROPERTY_X86_ISA_1_BASELINE:
20536 printf ("x86-64-baseline");
20537 break;
20538 case GNU_PROPERTY_X86_ISA_1_V2:
20539 printf ("x86-64-v2");
20540 break;
20541 case GNU_PROPERTY_X86_ISA_1_V3:
20542 printf ("x86-64-v3");
20543 break;
20544 case GNU_PROPERTY_X86_ISA_1_V4:
20545 printf ("x86-64-v4");
20546 break;
20547 default:
20548 printf (_("<unknown: %x>"), bit);
20549 break;
20551 if (bitmask)
20552 printf (", ");
20556 static void
20557 decode_x86_feature_1 (unsigned int bitmask)
20559 if (!bitmask)
20561 printf (_("<None>"));
20562 return;
20565 while (bitmask)
20567 unsigned int bit = bitmask & (- bitmask);
20569 bitmask &= ~ bit;
20570 switch (bit)
20572 case GNU_PROPERTY_X86_FEATURE_1_IBT:
20573 printf ("IBT");
20574 break;
20575 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
20576 printf ("SHSTK");
20577 break;
20578 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
20579 printf ("LAM_U48");
20580 break;
20581 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
20582 printf ("LAM_U57");
20583 break;
20584 default:
20585 printf (_("<unknown: %x>"), bit);
20586 break;
20588 if (bitmask)
20589 printf (", ");
20593 static void
20594 decode_x86_feature_2 (unsigned int bitmask)
20596 if (!bitmask)
20598 printf (_("<None>"));
20599 return;
20602 while (bitmask)
20604 unsigned int bit = bitmask & (- bitmask);
20606 bitmask &= ~ bit;
20607 switch (bit)
20609 case GNU_PROPERTY_X86_FEATURE_2_X86:
20610 printf ("x86");
20611 break;
20612 case GNU_PROPERTY_X86_FEATURE_2_X87:
20613 printf ("x87");
20614 break;
20615 case GNU_PROPERTY_X86_FEATURE_2_MMX:
20616 printf ("MMX");
20617 break;
20618 case GNU_PROPERTY_X86_FEATURE_2_XMM:
20619 printf ("XMM");
20620 break;
20621 case GNU_PROPERTY_X86_FEATURE_2_YMM:
20622 printf ("YMM");
20623 break;
20624 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
20625 printf ("ZMM");
20626 break;
20627 case GNU_PROPERTY_X86_FEATURE_2_TMM:
20628 printf ("TMM");
20629 break;
20630 case GNU_PROPERTY_X86_FEATURE_2_MASK:
20631 printf ("MASK");
20632 break;
20633 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
20634 printf ("FXSR");
20635 break;
20636 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
20637 printf ("XSAVE");
20638 break;
20639 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
20640 printf ("XSAVEOPT");
20641 break;
20642 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
20643 printf ("XSAVEC");
20644 break;
20645 default:
20646 printf (_("<unknown: %x>"), bit);
20647 break;
20649 if (bitmask)
20650 printf (", ");
20654 static void
20655 decode_aarch64_feature_1_and (unsigned int bitmask)
20657 while (bitmask)
20659 unsigned int bit = bitmask & (- bitmask);
20661 bitmask &= ~ bit;
20662 switch (bit)
20664 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
20665 printf ("BTI");
20666 break;
20668 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
20669 printf ("PAC");
20670 break;
20672 default:
20673 printf (_("<unknown: %x>"), bit);
20674 break;
20676 if (bitmask)
20677 printf (", ");
20681 static void
20682 decode_1_needed (unsigned int bitmask)
20684 while (bitmask)
20686 unsigned int bit = bitmask & (- bitmask);
20688 bitmask &= ~ bit;
20689 switch (bit)
20691 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
20692 printf ("indirect external access");
20693 break;
20694 default:
20695 printf (_("<unknown: %x>"), bit);
20696 break;
20698 if (bitmask)
20699 printf (", ");
20703 static void
20704 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
20706 unsigned char * ptr = (unsigned char *) pnote->descdata;
20707 unsigned char * ptr_end = ptr + pnote->descsz;
20708 unsigned int size = is_32bit_elf ? 4 : 8;
20710 printf (_(" Properties: "));
20712 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
20714 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
20715 return;
20718 while (ptr < ptr_end)
20720 unsigned int j;
20721 unsigned int type;
20722 unsigned int datasz;
20724 if ((size_t) (ptr_end - ptr) < 8)
20726 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
20727 break;
20730 type = byte_get (ptr, 4);
20731 datasz = byte_get (ptr + 4, 4);
20733 ptr += 8;
20735 if (datasz > (size_t) (ptr_end - ptr))
20737 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
20738 type, datasz);
20739 break;
20742 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
20744 if (filedata->file_header.e_machine == EM_X86_64
20745 || filedata->file_header.e_machine == EM_IAMCU
20746 || filedata->file_header.e_machine == EM_386)
20748 unsigned int bitmask;
20750 if (datasz == 4)
20751 bitmask = byte_get (ptr, 4);
20752 else
20753 bitmask = 0;
20755 switch (type)
20757 case GNU_PROPERTY_X86_ISA_1_USED:
20758 if (datasz != 4)
20759 printf (_("x86 ISA used: <corrupt length: %#x> "),
20760 datasz);
20761 else
20763 printf ("x86 ISA used: ");
20764 decode_x86_isa (bitmask);
20766 goto next;
20768 case GNU_PROPERTY_X86_ISA_1_NEEDED:
20769 if (datasz != 4)
20770 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20771 datasz);
20772 else
20774 printf ("x86 ISA needed: ");
20775 decode_x86_isa (bitmask);
20777 goto next;
20779 case GNU_PROPERTY_X86_FEATURE_1_AND:
20780 if (datasz != 4)
20781 printf (_("x86 feature: <corrupt length: %#x> "),
20782 datasz);
20783 else
20785 printf ("x86 feature: ");
20786 decode_x86_feature_1 (bitmask);
20788 goto next;
20790 case GNU_PROPERTY_X86_FEATURE_2_USED:
20791 if (datasz != 4)
20792 printf (_("x86 feature used: <corrupt length: %#x> "),
20793 datasz);
20794 else
20796 printf ("x86 feature used: ");
20797 decode_x86_feature_2 (bitmask);
20799 goto next;
20801 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20802 if (datasz != 4)
20803 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20804 else
20806 printf ("x86 feature needed: ");
20807 decode_x86_feature_2 (bitmask);
20809 goto next;
20811 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20812 if (datasz != 4)
20813 printf (_("x86 ISA used: <corrupt length: %#x> "),
20814 datasz);
20815 else
20817 printf ("x86 ISA used: ");
20818 decode_x86_compat_isa (bitmask);
20820 goto next;
20822 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20823 if (datasz != 4)
20824 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20825 datasz);
20826 else
20828 printf ("x86 ISA needed: ");
20829 decode_x86_compat_isa (bitmask);
20831 goto next;
20833 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20834 if (datasz != 4)
20835 printf (_("x86 ISA used: <corrupt length: %#x> "),
20836 datasz);
20837 else
20839 printf ("x86 ISA used: ");
20840 decode_x86_compat_2_isa (bitmask);
20842 goto next;
20844 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20845 if (datasz != 4)
20846 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20847 datasz);
20848 else
20850 printf ("x86 ISA needed: ");
20851 decode_x86_compat_2_isa (bitmask);
20853 goto next;
20855 default:
20856 break;
20859 else if (filedata->file_header.e_machine == EM_AARCH64)
20861 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20863 printf ("AArch64 feature: ");
20864 if (datasz != 4)
20865 printf (_("<corrupt length: %#x> "), datasz);
20866 else
20867 decode_aarch64_feature_1_and (byte_get (ptr, 4));
20868 goto next;
20872 else
20874 switch (type)
20876 case GNU_PROPERTY_STACK_SIZE:
20877 printf (_("stack size: "));
20878 if (datasz != size)
20879 printf (_("<corrupt length: %#x> "), datasz);
20880 else
20881 printf ("%#" PRIx64, byte_get (ptr, size));
20882 goto next;
20884 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20885 printf ("no copy on protected ");
20886 if (datasz)
20887 printf (_("<corrupt length: %#x> "), datasz);
20888 goto next;
20890 default:
20891 if ((type >= GNU_PROPERTY_UINT32_AND_LO
20892 && type <= GNU_PROPERTY_UINT32_AND_HI)
20893 || (type >= GNU_PROPERTY_UINT32_OR_LO
20894 && type <= GNU_PROPERTY_UINT32_OR_HI))
20896 switch (type)
20898 case GNU_PROPERTY_1_NEEDED:
20899 if (datasz != 4)
20900 printf (_("1_needed: <corrupt length: %#x> "),
20901 datasz);
20902 else
20904 unsigned int bitmask = byte_get (ptr, 4);
20905 printf ("1_needed: ");
20906 decode_1_needed (bitmask);
20908 goto next;
20910 default:
20911 break;
20913 if (type <= GNU_PROPERTY_UINT32_AND_HI)
20914 printf (_("UINT32_AND (%#x): "), type);
20915 else
20916 printf (_("UINT32_OR (%#x): "), type);
20917 if (datasz != 4)
20918 printf (_("<corrupt length: %#x> "), datasz);
20919 else
20920 printf ("%#x", (unsigned int) byte_get (ptr, 4));
20921 goto next;
20923 break;
20927 if (type < GNU_PROPERTY_LOPROC)
20928 printf (_("<unknown type %#x data: "), type);
20929 else if (type < GNU_PROPERTY_LOUSER)
20930 printf (_("<processor-specific type %#x data: "), type);
20931 else
20932 printf (_("<application-specific type %#x data: "), type);
20933 for (j = 0; j < datasz; ++j)
20934 printf ("%02x ", ptr[j] & 0xff);
20935 printf (">");
20937 next:
20938 ptr += ((datasz + (size - 1)) & ~ (size - 1));
20939 if (ptr == ptr_end)
20940 break;
20942 if (do_wide)
20943 printf (", ");
20944 else
20945 printf ("\n\t");
20948 printf ("\n");
20951 static bool
20952 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
20954 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
20955 switch (pnote->type)
20957 case NT_GNU_BUILD_ID:
20959 size_t i;
20961 printf (_(" Build ID: "));
20962 for (i = 0; i < pnote->descsz; ++i)
20963 printf ("%02x", pnote->descdata[i] & 0xff);
20964 printf ("\n");
20966 break;
20968 case NT_GNU_ABI_TAG:
20970 unsigned int os, major, minor, subminor;
20971 const char *osname;
20973 /* PR 17531: file: 030-599401-0.004. */
20974 if (pnote->descsz < 16)
20976 printf (_(" <corrupt GNU_ABI_TAG>\n"));
20977 break;
20980 os = byte_get ((unsigned char *) pnote->descdata, 4);
20981 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20982 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20983 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20985 switch (os)
20987 case GNU_ABI_TAG_LINUX:
20988 osname = "Linux";
20989 break;
20990 case GNU_ABI_TAG_HURD:
20991 osname = "Hurd";
20992 break;
20993 case GNU_ABI_TAG_SOLARIS:
20994 osname = "Solaris";
20995 break;
20996 case GNU_ABI_TAG_FREEBSD:
20997 osname = "FreeBSD";
20998 break;
20999 case GNU_ABI_TAG_NETBSD:
21000 osname = "NetBSD";
21001 break;
21002 case GNU_ABI_TAG_SYLLABLE:
21003 osname = "Syllable";
21004 break;
21005 case GNU_ABI_TAG_NACL:
21006 osname = "NaCl";
21007 break;
21008 default:
21009 osname = "Unknown";
21010 break;
21013 printf (_(" OS: %s, ABI: %d.%d.%d\n"), osname,
21014 major, minor, subminor);
21016 break;
21018 case NT_GNU_GOLD_VERSION:
21020 size_t i;
21022 printf (_(" Version: "));
21023 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
21024 printf ("%c", pnote->descdata[i]);
21025 printf ("\n");
21027 break;
21029 case NT_GNU_HWCAP:
21031 unsigned int num_entries, mask;
21033 /* Hardware capabilities information. Word 0 is the number of entries.
21034 Word 1 is a bitmask of enabled entries. The rest of the descriptor
21035 is a series of entries, where each entry is a single byte followed
21036 by a nul terminated string. The byte gives the bit number to test
21037 if enabled in the bitmask. */
21038 printf (_(" Hardware Capabilities: "));
21039 if (pnote->descsz < 8)
21041 error (_("<corrupt GNU_HWCAP>\n"));
21042 return false;
21044 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
21045 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21046 printf (_("num entries: %d, enabled mask: %x\n"), num_entries, mask);
21047 /* FIXME: Add code to display the entries... */
21049 break;
21051 case NT_GNU_PROPERTY_TYPE_0:
21052 print_gnu_property_note (filedata, pnote);
21053 break;
21055 default:
21056 /* Handle unrecognised types. An error message should have already been
21057 created by get_gnu_elf_note_type(), so all that we need to do is to
21058 display the data. */
21060 size_t i;
21062 printf (_(" Description data: "));
21063 for (i = 0; i < pnote->descsz; ++i)
21064 printf ("%02x ", pnote->descdata[i] & 0xff);
21065 printf ("\n");
21067 break;
21070 return true;
21073 static const char *
21074 get_v850_elf_note_type (enum v850_notes n_type)
21076 static char buff[64];
21078 switch (n_type)
21080 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
21081 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
21082 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
21083 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
21084 case V850_NOTE_CACHE_INFO: return _("Use of cache");
21085 case V850_NOTE_MMU_INFO: return _("Use of MMU");
21086 default:
21087 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
21088 return buff;
21092 static bool
21093 print_v850_note (Elf_Internal_Note * pnote)
21095 unsigned int val;
21097 if (pnote->descsz != 4)
21098 return false;
21100 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
21102 if (val == 0)
21104 printf (_("not set\n"));
21105 return true;
21108 switch (pnote->type)
21110 case V850_NOTE_ALIGNMENT:
21111 switch (val)
21113 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
21114 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
21116 break;
21118 case V850_NOTE_DATA_SIZE:
21119 switch (val)
21121 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
21122 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
21124 break;
21126 case V850_NOTE_FPU_INFO:
21127 switch (val)
21129 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
21130 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
21132 break;
21134 case V850_NOTE_MMU_INFO:
21135 case V850_NOTE_CACHE_INFO:
21136 case V850_NOTE_SIMD_INFO:
21137 if (val == EF_RH850_SIMD)
21139 printf (_("yes\n"));
21140 return true;
21142 break;
21144 default:
21145 /* An 'unknown note type' message will already have been displayed. */
21146 break;
21149 printf (_("unknown value: %x\n"), val);
21150 return false;
21153 static bool
21154 process_netbsd_elf_note (Elf_Internal_Note * pnote)
21156 unsigned int version;
21158 switch (pnote->type)
21160 case NT_NETBSD_IDENT:
21161 if (pnote->descsz < 1)
21162 break;
21163 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
21164 if ((version / 10000) % 100)
21165 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
21166 version, version / 100000000, (version / 1000000) % 100,
21167 (version / 10000) % 100 > 26 ? "Z" : "",
21168 'A' + (version / 10000) % 26);
21169 else
21170 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
21171 version, version / 100000000, (version / 1000000) % 100,
21172 (version / 100) % 100);
21173 return true;
21175 case NT_NETBSD_MARCH:
21176 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
21177 pnote->descdata);
21178 return true;
21180 case NT_NETBSD_PAX:
21181 if (pnote->descsz < 1)
21182 break;
21183 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
21184 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
21185 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
21186 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
21187 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
21188 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
21189 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
21190 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
21191 return true;
21194 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
21195 pnote->descsz, pnote->type);
21196 return false;
21199 static const char *
21200 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21202 switch (e_type)
21204 case NT_FREEBSD_THRMISC:
21205 return _("NT_THRMISC (thrmisc structure)");
21206 case NT_FREEBSD_PROCSTAT_PROC:
21207 return _("NT_PROCSTAT_PROC (proc data)");
21208 case NT_FREEBSD_PROCSTAT_FILES:
21209 return _("NT_PROCSTAT_FILES (files data)");
21210 case NT_FREEBSD_PROCSTAT_VMMAP:
21211 return _("NT_PROCSTAT_VMMAP (vmmap data)");
21212 case NT_FREEBSD_PROCSTAT_GROUPS:
21213 return _("NT_PROCSTAT_GROUPS (groups data)");
21214 case NT_FREEBSD_PROCSTAT_UMASK:
21215 return _("NT_PROCSTAT_UMASK (umask data)");
21216 case NT_FREEBSD_PROCSTAT_RLIMIT:
21217 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
21218 case NT_FREEBSD_PROCSTAT_OSREL:
21219 return _("NT_PROCSTAT_OSREL (osreldate data)");
21220 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
21221 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
21222 case NT_FREEBSD_PROCSTAT_AUXV:
21223 return _("NT_PROCSTAT_AUXV (auxv data)");
21224 case NT_FREEBSD_PTLWPINFO:
21225 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
21226 case NT_FREEBSD_X86_SEGBASES:
21227 return _("NT_X86_SEGBASES (x86 segment base registers)");
21229 return get_note_type (filedata, e_type);
21232 static const char *
21233 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21235 static char buff[64];
21237 switch (e_type)
21239 case NT_NETBSDCORE_PROCINFO:
21240 /* NetBSD core "procinfo" structure. */
21241 return _("NetBSD procinfo structure");
21243 case NT_NETBSDCORE_AUXV:
21244 return _("NetBSD ELF auxiliary vector data");
21246 case NT_NETBSDCORE_LWPSTATUS:
21247 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
21249 default:
21250 /* As of Jan 2020 there are no other machine-independent notes
21251 defined for NetBSD core files. If the note type is less
21252 than the start of the machine-dependent note types, we don't
21253 understand it. */
21255 if (e_type < NT_NETBSDCORE_FIRSTMACH)
21257 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21258 return buff;
21260 break;
21263 switch (filedata->file_header.e_machine)
21265 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
21266 and PT_GETFPREGS == mach+2. */
21268 case EM_OLD_ALPHA:
21269 case EM_ALPHA:
21270 case EM_SPARC:
21271 case EM_SPARC32PLUS:
21272 case EM_SPARCV9:
21273 switch (e_type)
21275 case NT_NETBSDCORE_FIRSTMACH + 0:
21276 return _("PT_GETREGS (reg structure)");
21277 case NT_NETBSDCORE_FIRSTMACH + 2:
21278 return _("PT_GETFPREGS (fpreg structure)");
21279 default:
21280 break;
21282 break;
21284 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
21285 There's also old PT___GETREGS40 == mach + 1 for old reg
21286 structure which lacks GBR. */
21287 case EM_SH:
21288 switch (e_type)
21290 case NT_NETBSDCORE_FIRSTMACH + 1:
21291 return _("PT___GETREGS40 (old reg structure)");
21292 case NT_NETBSDCORE_FIRSTMACH + 3:
21293 return _("PT_GETREGS (reg structure)");
21294 case NT_NETBSDCORE_FIRSTMACH + 5:
21295 return _("PT_GETFPREGS (fpreg structure)");
21296 default:
21297 break;
21299 break;
21301 /* On all other arch's, PT_GETREGS == mach+1 and
21302 PT_GETFPREGS == mach+3. */
21303 default:
21304 switch (e_type)
21306 case NT_NETBSDCORE_FIRSTMACH + 1:
21307 return _("PT_GETREGS (reg structure)");
21308 case NT_NETBSDCORE_FIRSTMACH + 3:
21309 return _("PT_GETFPREGS (fpreg structure)");
21310 default:
21311 break;
21315 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
21316 e_type - NT_NETBSDCORE_FIRSTMACH);
21317 return buff;
21320 static const char *
21321 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21323 switch (e_type)
21325 case NT_OPENBSD_PROCINFO:
21326 return _("OpenBSD procinfo structure");
21327 case NT_OPENBSD_AUXV:
21328 return _("OpenBSD ELF auxiliary vector data");
21329 case NT_OPENBSD_REGS:
21330 return _("OpenBSD regular registers");
21331 case NT_OPENBSD_FPREGS:
21332 return _("OpenBSD floating point registers");
21333 case NT_OPENBSD_WCOOKIE:
21334 return _("OpenBSD window cookie");
21337 return get_note_type (filedata, e_type);
21340 static const char *
21341 get_qnx_elfcore_note_type (Filedata * filedata, unsigned e_type)
21343 switch (e_type)
21345 case QNT_DEBUG_FULLPATH:
21346 return _("QNX debug fullpath");
21347 case QNT_DEBUG_RELOC:
21348 return _("QNX debug relocation");
21349 case QNT_STACK:
21350 return _("QNX stack");
21351 case QNT_GENERATOR:
21352 return _("QNX generator");
21353 case QNT_DEFAULT_LIB:
21354 return _("QNX default library");
21355 case QNT_CORE_SYSINFO:
21356 return _("QNX core sysinfo");
21357 case QNT_CORE_INFO:
21358 return _("QNX core info");
21359 case QNT_CORE_STATUS:
21360 return _("QNX core status");
21361 case QNT_CORE_GREG:
21362 return _("QNX general registers");
21363 case QNT_CORE_FPREG:
21364 return _("QNX floating point registers");
21365 case QNT_LINK_MAP:
21366 return _("QNX link map");
21369 return get_note_type (filedata, e_type);
21372 static const char *
21373 get_stapsdt_note_type (unsigned e_type)
21375 static char buff[64];
21377 switch (e_type)
21379 case NT_STAPSDT:
21380 return _("NT_STAPSDT (SystemTap probe descriptors)");
21382 default:
21383 break;
21386 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21387 return buff;
21390 static bool
21391 print_stapsdt_note (Elf_Internal_Note *pnote)
21393 size_t len, maxlen;
21394 size_t addr_size = is_32bit_elf ? 4 : 8;
21395 char *data = pnote->descdata;
21396 char *data_end = pnote->descdata + pnote->descsz;
21397 uint64_t pc, base_addr, semaphore;
21398 char *provider, *probe, *arg_fmt;
21400 if (pnote->descsz < (addr_size * 3))
21401 goto stapdt_note_too_small;
21403 pc = byte_get ((unsigned char *) data, addr_size);
21404 data += addr_size;
21406 base_addr = byte_get ((unsigned char *) data, addr_size);
21407 data += addr_size;
21409 semaphore = byte_get ((unsigned char *) data, addr_size);
21410 data += addr_size;
21412 if (data >= data_end)
21413 goto stapdt_note_too_small;
21414 maxlen = data_end - data;
21415 len = strnlen (data, maxlen);
21416 if (len < maxlen)
21418 provider = data;
21419 data += len + 1;
21421 else
21422 goto stapdt_note_too_small;
21424 if (data >= data_end)
21425 goto stapdt_note_too_small;
21426 maxlen = data_end - data;
21427 len = strnlen (data, maxlen);
21428 if (len < maxlen)
21430 probe = data;
21431 data += len + 1;
21433 else
21434 goto stapdt_note_too_small;
21436 if (data >= data_end)
21437 goto stapdt_note_too_small;
21438 maxlen = data_end - data;
21439 len = strnlen (data, maxlen);
21440 if (len < maxlen)
21442 arg_fmt = data;
21443 data += len + 1;
21445 else
21446 goto stapdt_note_too_small;
21448 printf (_(" Provider: %s\n"), provider);
21449 printf (_(" Name: %s\n"), probe);
21450 printf (_(" Location: "));
21451 print_vma (pc, FULL_HEX);
21452 printf (_(", Base: "));
21453 print_vma (base_addr, FULL_HEX);
21454 printf (_(", Semaphore: "));
21455 print_vma (semaphore, FULL_HEX);
21456 printf ("\n");
21457 printf (_(" Arguments: %s\n"), arg_fmt);
21459 return data == data_end;
21461 stapdt_note_too_small:
21462 printf (_(" <corrupt - note is too small>\n"));
21463 error (_("corrupt stapdt note - the data size is too small\n"));
21464 return false;
21467 static bool
21468 print_fdo_note (Elf_Internal_Note * pnote)
21470 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
21472 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
21473 return true;
21475 return false;
21478 static const char *
21479 get_ia64_vms_note_type (unsigned e_type)
21481 static char buff[64];
21483 switch (e_type)
21485 case NT_VMS_MHD:
21486 return _("NT_VMS_MHD (module header)");
21487 case NT_VMS_LNM:
21488 return _("NT_VMS_LNM (language name)");
21489 case NT_VMS_SRC:
21490 return _("NT_VMS_SRC (source files)");
21491 case NT_VMS_TITLE:
21492 return "NT_VMS_TITLE";
21493 case NT_VMS_EIDC:
21494 return _("NT_VMS_EIDC (consistency check)");
21495 case NT_VMS_FPMODE:
21496 return _("NT_VMS_FPMODE (FP mode)");
21497 case NT_VMS_LINKTIME:
21498 return "NT_VMS_LINKTIME";
21499 case NT_VMS_IMGNAM:
21500 return _("NT_VMS_IMGNAM (image name)");
21501 case NT_VMS_IMGID:
21502 return _("NT_VMS_IMGID (image id)");
21503 case NT_VMS_LINKID:
21504 return _("NT_VMS_LINKID (link id)");
21505 case NT_VMS_IMGBID:
21506 return _("NT_VMS_IMGBID (build id)");
21507 case NT_VMS_GSTNAM:
21508 return _("NT_VMS_GSTNAM (sym table name)");
21509 case NT_VMS_ORIG_DYN:
21510 return "NT_VMS_ORIG_DYN";
21511 case NT_VMS_PATCHTIME:
21512 return "NT_VMS_PATCHTIME";
21513 default:
21514 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21515 return buff;
21519 static bool
21520 print_ia64_vms_note (Elf_Internal_Note * pnote)
21522 unsigned int maxlen = pnote->descsz;
21524 if (maxlen < 2 || maxlen != pnote->descsz)
21525 goto desc_size_fail;
21527 switch (pnote->type)
21529 case NT_VMS_MHD:
21530 if (maxlen <= 36)
21531 goto desc_size_fail;
21533 size_t l = strnlen (pnote->descdata + 34, maxlen - 34);
21535 printf (_(" Creation date : %.17s\n"), pnote->descdata);
21536 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
21537 if (l + 34 < maxlen)
21539 printf (_(" Module name : %s\n"), pnote->descdata + 34);
21540 if (l + 35 < maxlen)
21541 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
21542 else
21543 printf (_(" Module version : <missing>\n"));
21545 else
21547 printf (_(" Module name : <missing>\n"));
21548 printf (_(" Module version : <missing>\n"));
21550 break;
21552 case NT_VMS_LNM:
21553 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
21554 break;
21556 case NT_VMS_FPMODE:
21557 printf (_(" Floating Point mode: "));
21558 if (maxlen < 8)
21559 goto desc_size_fail;
21560 /* FIXME: Generate an error if descsz > 8 ? */
21562 printf ("0x%016" PRIx64 "\n",
21563 byte_get ((unsigned char *) pnote->descdata, 8));
21564 break;
21566 case NT_VMS_LINKTIME:
21567 printf (_(" Link time: "));
21568 if (maxlen < 8)
21569 goto desc_size_fail;
21570 /* FIXME: Generate an error if descsz > 8 ? */
21572 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
21573 printf ("\n");
21574 break;
21576 case NT_VMS_PATCHTIME:
21577 printf (_(" Patch time: "));
21578 if (maxlen < 8)
21579 goto desc_size_fail;
21580 /* FIXME: Generate an error if descsz > 8 ? */
21582 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
21583 printf ("\n");
21584 break;
21586 case NT_VMS_ORIG_DYN:
21587 if (maxlen < 34)
21588 goto desc_size_fail;
21590 printf (_(" Major id: %u, minor id: %u\n"),
21591 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
21592 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
21593 printf (_(" Last modified : "));
21594 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
21595 printf (_("\n Link flags : "));
21596 printf ("0x%016" PRIx64 "\n",
21597 byte_get ((unsigned char *) pnote->descdata + 16, 8));
21598 printf (_(" Header flags: 0x%08x\n"),
21599 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
21600 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
21601 break;
21603 case NT_VMS_IMGNAM:
21604 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
21605 break;
21607 case NT_VMS_GSTNAM:
21608 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
21609 break;
21611 case NT_VMS_IMGID:
21612 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
21613 break;
21615 case NT_VMS_LINKID:
21616 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
21617 break;
21619 default:
21620 return false;
21623 return true;
21625 desc_size_fail:
21626 printf (_(" <corrupt - data size is too small>\n"));
21627 error (_("corrupt IA64 note: data size is too small\n"));
21628 return false;
21631 struct build_attr_cache {
21632 Filedata *filedata;
21633 char *strtab;
21634 uint64_t strtablen;
21635 Elf_Internal_Sym *symtab;
21636 uint64_t nsyms;
21637 } ba_cache;
21639 /* Find the symbol associated with a build attribute that is attached
21640 to address OFFSET. If PNAME is non-NULL then store the name of
21641 the symbol (if found) in the provided pointer, Returns NULL if a
21642 symbol could not be found. */
21644 static Elf_Internal_Sym *
21645 get_symbol_for_build_attribute (Filedata *filedata,
21646 uint64_t offset,
21647 bool is_open_attr,
21648 const char **pname)
21650 Elf_Internal_Sym *saved_sym = NULL;
21651 Elf_Internal_Sym *sym;
21653 if (filedata->section_headers != NULL
21654 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
21656 Elf_Internal_Shdr * symsec;
21658 free (ba_cache.strtab);
21659 ba_cache.strtab = NULL;
21660 free (ba_cache.symtab);
21661 ba_cache.symtab = NULL;
21663 /* Load the symbol and string sections. */
21664 for (symsec = filedata->section_headers;
21665 symsec < filedata->section_headers + filedata->file_header.e_shnum;
21666 symsec ++)
21668 if (symsec->sh_type == SHT_SYMTAB
21669 && get_symtab (filedata, symsec,
21670 &ba_cache.symtab, &ba_cache.nsyms,
21671 &ba_cache.strtab, &ba_cache.strtablen))
21672 break;
21674 ba_cache.filedata = filedata;
21677 if (ba_cache.symtab == NULL)
21678 return NULL;
21680 /* Find a symbol whose value matches offset. */
21681 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
21682 if (sym->st_value == offset)
21684 if (sym->st_name >= ba_cache.strtablen)
21685 /* Huh ? This should not happen. */
21686 continue;
21688 if (ba_cache.strtab[sym->st_name] == 0)
21689 continue;
21691 /* The AArch64, ARM and RISC-V architectures define mapping symbols
21692 (eg $d, $x, $t) which we want to ignore. */
21693 if (ba_cache.strtab[sym->st_name] == '$'
21694 && ba_cache.strtab[sym->st_name + 1] != 0
21695 && ba_cache.strtab[sym->st_name + 2] == 0)
21696 continue;
21698 if (is_open_attr)
21700 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
21701 and FILE or OBJECT symbols over NOTYPE symbols. We skip
21702 FUNC symbols entirely. */
21703 switch (ELF_ST_TYPE (sym->st_info))
21705 case STT_OBJECT:
21706 case STT_FILE:
21707 saved_sym = sym;
21708 if (sym->st_size)
21710 /* If the symbol has a size associated
21711 with it then we can stop searching. */
21712 sym = ba_cache.symtab + ba_cache.nsyms;
21714 continue;
21716 case STT_FUNC:
21717 /* Ignore function symbols. */
21718 continue;
21720 default:
21721 break;
21724 switch (ELF_ST_BIND (sym->st_info))
21726 case STB_GLOBAL:
21727 if (saved_sym == NULL
21728 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
21729 saved_sym = sym;
21730 break;
21732 case STB_LOCAL:
21733 if (saved_sym == NULL)
21734 saved_sym = sym;
21735 break;
21737 default:
21738 break;
21741 else
21743 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
21744 continue;
21746 saved_sym = sym;
21747 break;
21751 if (saved_sym && pname)
21752 * pname = ba_cache.strtab + saved_sym->st_name;
21754 return saved_sym;
21757 /* Returns true iff addr1 and addr2 are in the same section. */
21759 static bool
21760 same_section (Filedata * filedata, uint64_t addr1, uint64_t addr2)
21762 Elf_Internal_Shdr * a1;
21763 Elf_Internal_Shdr * a2;
21765 a1 = find_section_by_address (filedata, addr1);
21766 a2 = find_section_by_address (filedata, addr2);
21768 return a1 == a2 && a1 != NULL;
21771 static bool
21772 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
21773 Filedata * filedata)
21775 static uint64_t global_offset = 0;
21776 static uint64_t global_end = 0;
21777 static uint64_t func_offset = 0;
21778 static uint64_t func_end = 0;
21780 Elf_Internal_Sym *sym;
21781 const char *name;
21782 uint64_t start;
21783 uint64_t end;
21784 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
21786 switch (pnote->descsz)
21788 case 0:
21789 /* A zero-length description means that the range of
21790 the previous note of the same type should be used. */
21791 if (is_open_attr)
21793 if (global_end > global_offset)
21794 printf (_(" Applies to region from %#" PRIx64
21795 " to %#" PRIx64 "\n"), global_offset, global_end);
21796 else
21797 printf (_(" Applies to region from %#" PRIx64
21798 "\n"), global_offset);
21800 else
21802 if (func_end > func_offset)
21803 printf (_(" Applies to region from %#" PRIx64
21804 " to %#" PRIx64 "\n"), func_offset, func_end);
21805 else
21806 printf (_(" Applies to region from %#" PRIx64
21807 "\n"), func_offset);
21809 return true;
21811 case 4:
21812 start = byte_get ((unsigned char *) pnote->descdata, 4);
21813 end = 0;
21814 break;
21816 case 8:
21817 start = byte_get ((unsigned char *) pnote->descdata, 4);
21818 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21819 break;
21821 case 16:
21822 start = byte_get ((unsigned char *) pnote->descdata, 8);
21823 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21824 break;
21826 default:
21827 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
21828 printf (_(" <invalid descsz>"));
21829 return false;
21832 name = NULL;
21833 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
21834 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21835 in order to avoid them being confused with the start address of the
21836 first function in the file... */
21837 if (sym == NULL && is_open_attr)
21838 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21839 & name);
21841 if (end == 0 && sym != NULL && sym->st_size > 0)
21842 end = start + sym->st_size;
21844 if (is_open_attr)
21846 /* FIXME: Need to properly allow for section alignment.
21847 16 is just the alignment used on x86_64. */
21848 if (global_end > 0
21849 && start > BFD_ALIGN (global_end, 16)
21850 /* Build notes are not guaranteed to be organised in order of
21851 increasing address, but we should find the all of the notes
21852 for one section in the same place. */
21853 && same_section (filedata, start, global_end))
21854 warn (_("Gap in build notes detected from %#" PRIx64
21855 " to %#" PRIx64 "\n"),
21856 global_end + 1, start - 1);
21858 printf (_(" Applies to region from %#" PRIx64), start);
21859 global_offset = start;
21861 if (end)
21863 printf (_(" to %#" PRIx64), end);
21864 global_end = end;
21867 else
21869 printf (_(" Applies to region from %#" PRIx64), start);
21870 func_offset = start;
21872 if (end)
21874 printf (_(" to %#" PRIx64), end);
21875 func_end = end;
21879 if (sym && name)
21880 printf (_(" (%s)"), name);
21882 printf ("\n");
21883 return true;
21886 static bool
21887 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21889 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21890 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21891 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
21892 char name_type;
21893 char name_attribute;
21894 const char * expected_types;
21895 const char * name = pnote->namedata;
21896 const char * text;
21897 signed int left;
21899 if (name == NULL || pnote->namesz < 2)
21901 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21902 print_symbol_name (-20, _(" <corrupt name>"));
21903 return false;
21906 if (do_wide)
21907 left = 28;
21908 else
21909 left = 20;
21911 /* Version 2 of the spec adds a "GA" prefix to the name field. */
21912 if (name[0] == 'G' && name[1] == 'A')
21914 if (pnote->namesz < 4)
21916 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21917 print_symbol_name (-20, _(" <corrupt name>"));
21918 return false;
21921 printf ("GA");
21922 name += 2;
21923 left -= 2;
21926 switch ((name_type = * name))
21928 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21929 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21930 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21931 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21932 printf ("%c", * name);
21933 left --;
21934 break;
21935 default:
21936 error (_("unrecognised attribute type in name field: %d\n"), name_type);
21937 print_symbol_name (-20, _("<unknown name type>"));
21938 return false;
21941 ++ name;
21942 text = NULL;
21944 switch ((name_attribute = * name))
21946 case GNU_BUILD_ATTRIBUTE_VERSION:
21947 text = _("<version>");
21948 expected_types = string_expected;
21949 ++ name;
21950 break;
21951 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21952 text = _("<stack prot>");
21953 expected_types = "!+*";
21954 ++ name;
21955 break;
21956 case GNU_BUILD_ATTRIBUTE_RELRO:
21957 text = _("<relro>");
21958 expected_types = bool_expected;
21959 ++ name;
21960 break;
21961 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21962 text = _("<stack size>");
21963 expected_types = number_expected;
21964 ++ name;
21965 break;
21966 case GNU_BUILD_ATTRIBUTE_TOOL:
21967 text = _("<tool>");
21968 expected_types = string_expected;
21969 ++ name;
21970 break;
21971 case GNU_BUILD_ATTRIBUTE_ABI:
21972 text = _("<ABI>");
21973 expected_types = "$*";
21974 ++ name;
21975 break;
21976 case GNU_BUILD_ATTRIBUTE_PIC:
21977 text = _("<PIC>");
21978 expected_types = number_expected;
21979 ++ name;
21980 break;
21981 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
21982 text = _("<short enum>");
21983 expected_types = bool_expected;
21984 ++ name;
21985 break;
21986 default:
21987 if (ISPRINT (* name))
21989 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
21991 if (len > left && ! do_wide)
21992 len = left;
21993 printf ("%.*s:", len, name);
21994 left -= len;
21995 name += len;
21997 else
21999 static char tmpbuf [128];
22001 error (_("unrecognised byte in name field: %d\n"), * name);
22002 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
22003 text = tmpbuf;
22004 name ++;
22006 expected_types = "*$!+";
22007 break;
22010 if (text)
22011 left -= printf ("%s", text);
22013 if (strchr (expected_types, name_type) == NULL)
22014 warn (_("attribute does not have an expected type (%c)\n"), name_type);
22016 if ((size_t) (name - pnote->namedata) > pnote->namesz)
22018 error (_("corrupt name field: namesz: %lu but parsing gets to %td\n"),
22019 pnote->namesz,
22020 name - pnote->namedata);
22021 return false;
22024 if (left < 1 && ! do_wide)
22025 return true;
22027 switch (name_type)
22029 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
22031 unsigned int bytes;
22032 uint64_t val = 0;
22033 unsigned int shift = 0;
22034 char *decoded = NULL;
22036 bytes = pnote->namesz - (name - pnote->namedata);
22037 if (bytes > 0)
22038 /* The -1 is because the name field is always 0 terminated, and we
22039 want to be able to ensure that the shift in the while loop below
22040 will not overflow. */
22041 -- bytes;
22043 if (bytes > sizeof (val))
22045 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
22046 bytes);
22047 bytes = sizeof (val);
22049 /* We do not bother to warn if bytes == 0 as this can
22050 happen with some early versions of the gcc plugin. */
22052 while (bytes --)
22054 uint64_t byte = *name++ & 0xff;
22056 val |= byte << shift;
22057 shift += 8;
22060 switch (name_attribute)
22062 case GNU_BUILD_ATTRIBUTE_PIC:
22063 switch (val)
22065 case 0: decoded = "static"; break;
22066 case 1: decoded = "pic"; break;
22067 case 2: decoded = "PIC"; break;
22068 case 3: decoded = "pie"; break;
22069 case 4: decoded = "PIE"; break;
22070 default: break;
22072 break;
22073 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
22074 switch (val)
22076 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
22077 case 0: decoded = "off"; break;
22078 case 1: decoded = "on"; break;
22079 case 2: decoded = "all"; break;
22080 case 3: decoded = "strong"; break;
22081 case 4: decoded = "explicit"; break;
22082 default: break;
22084 break;
22085 default:
22086 break;
22089 if (decoded != NULL)
22091 print_symbol_name (-left, decoded);
22092 left = 0;
22094 else if (val == 0)
22096 printf ("0x0");
22097 left -= 3;
22099 else
22101 if (do_wide)
22102 left -= printf ("0x%" PRIx64, val);
22103 else
22104 left -= printf ("0x%-.*" PRIx64, left, val);
22107 break;
22108 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
22109 left -= print_symbol_name (- left, name);
22110 break;
22111 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
22112 left -= print_symbol_name (- left, "true");
22113 break;
22114 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
22115 left -= print_symbol_name (- left, "false");
22116 break;
22119 if (do_wide && left > 0)
22120 printf ("%-*s", left, " ");
22122 return true;
22125 /* Print the contents of PNOTE as hex. */
22127 static void
22128 print_note_contents_hex (Elf_Internal_Note *pnote)
22130 if (pnote->descsz)
22132 size_t i;
22134 printf (_(" description data: "));
22135 for (i = 0; i < pnote->descsz; i++)
22136 printf ("%02x ", pnote->descdata[i] & 0xff);
22137 if (!do_wide)
22138 printf ("\n");
22141 if (do_wide)
22142 printf ("\n");
22145 #if defined HAVE_MSGPACK
22147 static void
22148 print_indents (int n)
22150 printf (" ");
22152 for (int i = 0; i < n; i++)
22153 printf (" ");
22156 /* Print OBJ in human-readable form. */
22158 static void
22159 dump_msgpack_obj (const msgpack_object *obj, int indent)
22161 switch (obj->type)
22163 case MSGPACK_OBJECT_NIL:
22164 printf ("(nil)");
22165 break;
22167 case MSGPACK_OBJECT_BOOLEAN:
22168 printf ("%s", obj->via.boolean ? "true" : "false");
22169 break;
22171 case MSGPACK_OBJECT_POSITIVE_INTEGER:
22172 printf ("%" PRIu64, obj->via.u64);
22173 break;
22175 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
22176 printf ("%" PRIi64, obj->via.i64);
22177 break;
22179 case MSGPACK_OBJECT_FLOAT32:
22180 case MSGPACK_OBJECT_FLOAT64:
22181 printf ("%f", obj->via.f64);
22182 break;
22184 case MSGPACK_OBJECT_STR:
22185 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
22186 break;
22188 case MSGPACK_OBJECT_ARRAY:
22190 const msgpack_object_array *array = &obj->via.array;
22192 printf ("[\n");
22193 ++indent;
22195 for (uint32_t i = 0; i < array->size; ++i)
22197 const msgpack_object *item = &array->ptr[i];
22199 print_indents (indent);
22200 dump_msgpack_obj (item, indent);
22201 printf (",\n");
22204 --indent;
22205 print_indents (indent);
22206 printf ("]");
22207 break;
22209 break;
22211 case MSGPACK_OBJECT_MAP:
22213 const msgpack_object_map *map = &obj->via.map;
22215 printf ("{\n");
22216 ++indent;
22218 for (uint32_t i = 0; i < map->size; ++i)
22220 const msgpack_object_kv *kv = &map->ptr[i];
22221 const msgpack_object *key = &kv->key;
22222 const msgpack_object *val = &kv->val;
22224 print_indents (indent);
22225 dump_msgpack_obj (key, indent);
22226 printf (": ");
22227 dump_msgpack_obj (val, indent);
22229 printf (",\n");
22232 --indent;
22233 print_indents (indent);
22234 printf ("}");
22236 break;
22239 case MSGPACK_OBJECT_BIN:
22240 printf ("(bin)");
22241 break;
22243 case MSGPACK_OBJECT_EXT:
22244 printf ("(ext)");
22245 break;
22249 static void
22250 dump_msgpack (const msgpack_unpacked *msg)
22252 print_indents (0);
22253 dump_msgpack_obj (&msg->data, 0);
22254 printf ("\n");
22257 #endif /* defined HAVE_MSGPACK */
22259 static bool
22260 print_amdgpu_note (Elf_Internal_Note *pnote)
22262 #if defined HAVE_MSGPACK
22263 /* If msgpack is available, decode and dump the note's content. */
22264 bool ret;
22265 msgpack_unpacked msg;
22266 msgpack_unpack_return msgpack_ret;
22268 assert (pnote->type == NT_AMDGPU_METADATA);
22270 msgpack_unpacked_init (&msg);
22271 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
22272 NULL);
22274 switch (msgpack_ret)
22276 case MSGPACK_UNPACK_SUCCESS:
22277 dump_msgpack (&msg);
22278 ret = true;
22279 break;
22281 default:
22282 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
22283 ret = false;
22284 break;
22287 msgpack_unpacked_destroy (&msg);
22288 return ret;
22289 #else
22290 /* msgpack is not available, dump contents as hex. */
22291 print_note_contents_hex (pnote);
22292 return true;
22293 #endif
22296 static bool
22297 print_qnx_note (Elf_Internal_Note *pnote)
22299 switch (pnote->type)
22301 case QNT_STACK:
22302 if (pnote->descsz != 12)
22303 goto desc_size_fail;
22305 printf (_(" Stack Size: 0x%" PRIx32 "\n"),
22306 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4));
22307 printf (_(" Stack allocated: %" PRIx32 "\n"),
22308 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
22309 printf (_(" Executable: %s\n"),
22310 ((unsigned) byte_get ((unsigned char *) pnote->descdata + 8, 1)) ? "no": "yes");
22311 break;
22313 default:
22314 print_note_contents_hex(pnote);
22316 return true;
22318 desc_size_fail:
22319 printf (_(" <corrupt - data size is too small>\n"));
22320 error (_("corrupt QNX note: data size is too small\n"));
22321 return false;
22325 /* Note that by the ELF standard, the name field is already null byte
22326 terminated, and namesz includes the terminating null byte.
22327 I.E. the value of namesz for the name "FSF" is 4.
22329 If the value of namesz is zero, there is no name present. */
22331 static bool
22332 process_note (Elf_Internal_Note * pnote,
22333 Filedata * filedata)
22335 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
22336 const char * nt;
22338 if (pnote->namesz == 0)
22339 /* If there is no note name, then use the default set of
22340 note type strings. */
22341 nt = get_note_type (filedata, pnote->type);
22343 else if (startswith (pnote->namedata, "GNU"))
22344 /* GNU-specific object file notes. */
22345 nt = get_gnu_elf_note_type (pnote->type);
22347 else if (startswith (pnote->namedata, "AMDGPU"))
22348 /* AMDGPU-specific object file notes. */
22349 nt = get_amdgpu_elf_note_type (pnote->type);
22351 else if (startswith (pnote->namedata, "FreeBSD"))
22352 /* FreeBSD-specific core file notes. */
22353 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
22355 else if (startswith (pnote->namedata, "NetBSD-CORE"))
22356 /* NetBSD-specific core file notes. */
22357 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
22359 else if (startswith (pnote->namedata, "NetBSD"))
22360 /* NetBSD-specific core file notes. */
22361 return process_netbsd_elf_note (pnote);
22363 else if (startswith (pnote->namedata, "PaX"))
22364 /* NetBSD-specific core file notes. */
22365 return process_netbsd_elf_note (pnote);
22367 else if (startswith (pnote->namedata, "OpenBSD"))
22368 /* OpenBSD-specific core file notes. */
22369 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
22371 else if (startswith (pnote->namedata, "QNX"))
22372 /* QNX-specific core file notes. */
22373 nt = get_qnx_elfcore_note_type (filedata, pnote->type);
22375 else if (startswith (pnote->namedata, "SPU/"))
22377 /* SPU-specific core file notes. */
22378 nt = pnote->namedata + 4;
22379 name = "SPU";
22382 else if (startswith (pnote->namedata, "IPF/VMS"))
22383 /* VMS/ia64-specific file notes. */
22384 nt = get_ia64_vms_note_type (pnote->type);
22386 else if (startswith (pnote->namedata, "stapsdt"))
22387 nt = get_stapsdt_note_type (pnote->type);
22389 else
22390 /* Don't recognize this note name; just use the default set of
22391 note type strings. */
22392 nt = get_note_type (filedata, pnote->type);
22394 printf (" ");
22396 if (((startswith (pnote->namedata, "GA")
22397 && strchr ("*$!+", pnote->namedata[2]) != NULL)
22398 || strchr ("*$!+", pnote->namedata[0]) != NULL)
22399 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
22400 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
22401 print_gnu_build_attribute_name (pnote);
22402 else
22403 print_symbol_name (-20, name);
22405 if (do_wide)
22406 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
22407 else
22408 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
22410 if (startswith (pnote->namedata, "IPF/VMS"))
22411 return print_ia64_vms_note (pnote);
22412 else if (startswith (pnote->namedata, "GNU"))
22413 return print_gnu_note (filedata, pnote);
22414 else if (startswith (pnote->namedata, "stapsdt"))
22415 return print_stapsdt_note (pnote);
22416 else if (startswith (pnote->namedata, "CORE"))
22417 return print_core_note (pnote);
22418 else if (startswith (pnote->namedata, "FDO"))
22419 return print_fdo_note (pnote);
22420 else if (((startswith (pnote->namedata, "GA")
22421 && strchr ("*$!+", pnote->namedata[2]) != NULL)
22422 || strchr ("*$!+", pnote->namedata[0]) != NULL)
22423 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
22424 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
22425 return print_gnu_build_attribute_description (pnote, filedata);
22426 else if (startswith (pnote->namedata, "AMDGPU")
22427 && pnote->type == NT_AMDGPU_METADATA)
22428 return print_amdgpu_note (pnote);
22429 else if (startswith (pnote->namedata, "QNX"))
22430 return print_qnx_note (pnote);
22432 print_note_contents_hex (pnote);
22433 return true;
22436 static bool
22437 process_notes_at (Filedata * filedata,
22438 Elf_Internal_Shdr * section,
22439 uint64_t offset,
22440 uint64_t length,
22441 uint64_t align)
22443 Elf_External_Note *pnotes;
22444 Elf_External_Note *external;
22445 char *end;
22446 bool res = true;
22448 if (length <= 0)
22449 return false;
22451 if (section)
22453 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
22454 if (pnotes)
22456 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
22458 free (pnotes);
22459 return false;
22463 else
22464 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
22465 _("notes"));
22467 if (pnotes == NULL)
22468 return false;
22470 external = pnotes;
22472 if (filedata->is_separate)
22473 printf (_("In linked file '%s': "), filedata->file_name);
22474 else
22475 printf ("\n");
22476 if (section)
22477 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
22478 else
22479 printf (_("Displaying notes found at file offset 0x%08" PRIx64
22480 " with length 0x%08" PRIx64 ":\n"),
22481 offset, length);
22483 /* NB: Some note sections may have alignment value of 0 or 1. gABI
22484 specifies that notes should be aligned to 4 bytes in 32-bit
22485 objects and to 8 bytes in 64-bit objects. As a Linux extension,
22486 we also support 4 byte alignment in 64-bit objects. If section
22487 alignment is less than 4, we treate alignment as 4 bytes. */
22488 if (align < 4)
22489 align = 4;
22490 else if (align != 4 && align != 8)
22492 warn (_("Corrupt note: alignment %" PRId64 ", expecting 4 or 8\n"),
22493 align);
22494 free (pnotes);
22495 return false;
22498 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
22500 end = (char *) pnotes + length;
22501 while ((char *) external < end)
22503 Elf_Internal_Note inote;
22504 size_t min_notesz;
22505 char * next;
22506 char * temp = NULL;
22507 size_t data_remaining = end - (char *) external;
22509 if (!is_ia64_vms (filedata))
22511 /* PR binutils/15191
22512 Make sure that there is enough data to read. */
22513 min_notesz = offsetof (Elf_External_Note, name);
22514 if (data_remaining < min_notesz)
22516 warn (ngettext ("Corrupt note: only %zd byte remains, "
22517 "not enough for a full note\n",
22518 "Corrupt note: only %zd bytes remain, "
22519 "not enough for a full note\n",
22520 data_remaining),
22521 data_remaining);
22522 break;
22524 data_remaining -= min_notesz;
22526 inote.type = BYTE_GET (external->type);
22527 inote.namesz = BYTE_GET (external->namesz);
22528 inote.namedata = external->name;
22529 inote.descsz = BYTE_GET (external->descsz);
22530 inote.descdata = ((char *) external
22531 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
22532 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22533 next = ((char *) external
22534 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
22536 else
22538 Elf64_External_VMS_Note *vms_external;
22540 /* PR binutils/15191
22541 Make sure that there is enough data to read. */
22542 min_notesz = offsetof (Elf64_External_VMS_Note, name);
22543 if (data_remaining < min_notesz)
22545 warn (ngettext ("Corrupt note: only %zd byte remains, "
22546 "not enough for a full note\n",
22547 "Corrupt note: only %zd bytes remain, "
22548 "not enough for a full note\n",
22549 data_remaining),
22550 data_remaining);
22551 break;
22553 data_remaining -= min_notesz;
22555 vms_external = (Elf64_External_VMS_Note *) external;
22556 inote.type = BYTE_GET (vms_external->type);
22557 inote.namesz = BYTE_GET (vms_external->namesz);
22558 inote.namedata = vms_external->name;
22559 inote.descsz = BYTE_GET (vms_external->descsz);
22560 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
22561 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22562 next = inote.descdata + align_power (inote.descsz, 3);
22565 /* PR 17531: file: 3443835e. */
22566 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
22567 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
22568 || (size_t) (inote.descdata - inote.namedata) > data_remaining
22569 || (size_t) (next - inote.descdata) < inote.descsz
22570 || ((size_t) (next - inote.descdata)
22571 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
22573 warn (_("note with invalid namesz and/or descsz found at offset %#tx\n"),
22574 (char *) external - (char *) pnotes);
22575 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx, alignment: %u\n"),
22576 inote.type, inote.namesz, inote.descsz, (int) align);
22577 break;
22580 external = (Elf_External_Note *) next;
22582 /* Verify that name is null terminated. It appears that at least
22583 one version of Linux (RedHat 6.0) generates corefiles that don't
22584 comply with the ELF spec by failing to include the null byte in
22585 namesz. */
22586 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
22588 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
22590 temp = (char *) malloc (inote.namesz + 1);
22591 if (temp == NULL)
22593 error (_("Out of memory allocating space for inote name\n"));
22594 res = false;
22595 break;
22598 memcpy (temp, inote.namedata, inote.namesz);
22599 inote.namedata = temp;
22601 inote.namedata[inote.namesz] = 0;
22604 if (! process_note (& inote, filedata))
22605 res = false;
22607 free (temp);
22608 temp = NULL;
22611 free (pnotes);
22613 return res;
22616 static bool
22617 process_corefile_note_segments (Filedata * filedata)
22619 Elf_Internal_Phdr *segment;
22620 unsigned int i;
22621 bool res = true;
22623 if (! get_program_headers (filedata))
22624 return true;
22626 for (i = 0, segment = filedata->program_headers;
22627 i < filedata->file_header.e_phnum;
22628 i++, segment++)
22630 if (segment->p_type == PT_NOTE)
22631 if (! process_notes_at (filedata, NULL, segment->p_offset,
22632 segment->p_filesz, segment->p_align))
22633 res = false;
22636 return res;
22639 static bool
22640 process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
22642 Elf_External_Note * pnotes;
22643 Elf_External_Note * external;
22644 char * end;
22645 bool res = true;
22647 if (length <= 0)
22648 return false;
22650 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
22651 _("v850 notes"));
22652 if (pnotes == NULL)
22653 return false;
22655 external = pnotes;
22656 end = (char*) pnotes + length;
22658 printf (_("\nDisplaying contents of Renesas V850 notes section at offset"
22659 " %#" PRIx64 " with length %#" PRIx64 ":\n"),
22660 offset, length);
22662 while ((char *) external + sizeof (Elf_External_Note) < end)
22664 Elf_External_Note * next;
22665 Elf_Internal_Note inote;
22667 inote.type = BYTE_GET (external->type);
22668 inote.namesz = BYTE_GET (external->namesz);
22669 inote.namedata = external->name;
22670 inote.descsz = BYTE_GET (external->descsz);
22671 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
22672 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22674 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
22676 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
22677 inote.descdata = inote.namedata;
22678 inote.namesz = 0;
22681 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
22683 if ( ((char *) next > end)
22684 || ((char *) next < (char *) pnotes))
22686 warn (_("corrupt descsz found in note at offset %#tx\n"),
22687 (char *) external - (char *) pnotes);
22688 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
22689 inote.type, inote.namesz, inote.descsz);
22690 break;
22693 external = next;
22695 /* Prevent out-of-bounds indexing. */
22696 if ( inote.namedata + inote.namesz > end
22697 || inote.namedata + inote.namesz < inote.namedata)
22699 warn (_("corrupt namesz found in note at offset %#zx\n"),
22700 (char *) external - (char *) pnotes);
22701 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
22702 inote.type, inote.namesz, inote.descsz);
22703 break;
22706 printf (" %s: ", get_v850_elf_note_type (inote.type));
22708 if (! print_v850_note (& inote))
22710 res = false;
22711 printf ("<corrupt sizes: namesz: %#lx, descsz: %#lx>\n",
22712 inote.namesz, inote.descsz);
22716 free (pnotes);
22718 return res;
22721 static bool
22722 process_note_sections (Filedata * filedata)
22724 Elf_Internal_Shdr *section;
22725 size_t i;
22726 unsigned int n = 0;
22727 bool res = true;
22729 for (i = 0, section = filedata->section_headers;
22730 i < filedata->file_header.e_shnum && section != NULL;
22731 i++, section++)
22733 if (section->sh_type == SHT_NOTE)
22735 if (! process_notes_at (filedata, section, section->sh_offset,
22736 section->sh_size, section->sh_addralign))
22737 res = false;
22738 n++;
22741 if (( filedata->file_header.e_machine == EM_V800
22742 || filedata->file_header.e_machine == EM_V850
22743 || filedata->file_header.e_machine == EM_CYGNUS_V850)
22744 && section->sh_type == SHT_RENESAS_INFO)
22746 if (! process_v850_notes (filedata, section->sh_offset,
22747 section->sh_size))
22748 res = false;
22749 n++;
22753 if (n == 0)
22754 /* Try processing NOTE segments instead. */
22755 return process_corefile_note_segments (filedata);
22757 return res;
22760 static bool
22761 process_notes (Filedata * filedata)
22763 /* If we have not been asked to display the notes then do nothing. */
22764 if (! do_notes)
22765 return true;
22767 if (filedata->file_header.e_type != ET_CORE)
22768 return process_note_sections (filedata);
22770 /* No program headers means no NOTE segment. */
22771 if (filedata->file_header.e_phnum > 0)
22772 return process_corefile_note_segments (filedata);
22774 if (filedata->is_separate)
22775 printf (_("No notes found in linked file '%s'.\n"),
22776 filedata->file_name);
22777 else
22778 printf (_("No notes found file.\n"));
22780 return true;
22783 static unsigned char *
22784 display_public_gnu_attributes (unsigned char * start,
22785 const unsigned char * const end)
22787 printf (_(" Unknown GNU attribute: %s\n"), start);
22789 start += strnlen ((char *) start, end - start);
22790 display_raw_attribute (start, end);
22792 return (unsigned char *) end;
22795 static unsigned char *
22796 display_generic_attribute (unsigned char * start,
22797 unsigned int tag,
22798 const unsigned char * const end)
22800 if (tag == 0)
22801 return (unsigned char *) end;
22803 return display_tag_value (tag, start, end);
22806 static bool
22807 process_arch_specific (Filedata * filedata)
22809 if (! do_arch)
22810 return true;
22812 switch (filedata->file_header.e_machine)
22814 case EM_ARC:
22815 case EM_ARC_COMPACT:
22816 case EM_ARC_COMPACT2:
22817 case EM_ARC_COMPACT3:
22818 case EM_ARC_COMPACT3_64:
22819 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
22820 display_arc_attribute,
22821 display_generic_attribute);
22822 case EM_ARM:
22823 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
22824 display_arm_attribute,
22825 display_generic_attribute);
22827 case EM_MIPS:
22828 case EM_MIPS_RS3_LE:
22829 return process_mips_specific (filedata);
22831 case EM_MSP430:
22832 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
22833 display_msp430_attribute,
22834 display_msp430_gnu_attribute);
22836 case EM_RISCV:
22837 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
22838 display_riscv_attribute,
22839 display_generic_attribute);
22841 case EM_NDS32:
22842 return process_nds32_specific (filedata);
22844 case EM_68K:
22845 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22846 display_m68k_gnu_attribute);
22848 case EM_PPC:
22849 case EM_PPC64:
22850 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22851 display_power_gnu_attribute);
22853 case EM_S390:
22854 case EM_S390_OLD:
22855 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22856 display_s390_gnu_attribute);
22858 case EM_SPARC:
22859 case EM_SPARC32PLUS:
22860 case EM_SPARCV9:
22861 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22862 display_sparc_gnu_attribute);
22864 case EM_TI_C6000:
22865 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
22866 display_tic6x_attribute,
22867 display_generic_attribute);
22869 case EM_CSKY:
22870 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22871 display_csky_attribute, NULL);
22873 default:
22874 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
22875 display_public_gnu_attributes,
22876 display_generic_attribute);
22880 static bool
22881 get_file_header (Filedata * filedata)
22883 /* Read in the identity array. */
22884 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
22885 return false;
22887 /* Determine how to read the rest of the header. */
22888 switch (filedata->file_header.e_ident[EI_DATA])
22890 default:
22891 case ELFDATANONE:
22892 case ELFDATA2LSB:
22893 byte_get = byte_get_little_endian;
22894 byte_put = byte_put_little_endian;
22895 break;
22896 case ELFDATA2MSB:
22897 byte_get = byte_get_big_endian;
22898 byte_put = byte_put_big_endian;
22899 break;
22902 /* For now we only support 32 bit and 64 bit ELF files. */
22903 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
22905 /* Read in the rest of the header. */
22906 if (is_32bit_elf)
22908 Elf32_External_Ehdr ehdr32;
22910 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
22911 return false;
22913 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
22914 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
22915 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
22916 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
22917 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
22918 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
22919 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
22920 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
22921 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22922 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
22923 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22924 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
22925 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
22927 else
22929 Elf64_External_Ehdr ehdr64;
22931 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
22932 return false;
22934 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
22935 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
22936 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
22937 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
22938 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
22939 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
22940 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
22941 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
22942 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22943 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
22944 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22945 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
22946 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
22949 return true;
22952 static void
22953 free_filedata (Filedata *filedata)
22955 free (filedata->program_interpreter);
22956 free (filedata->program_headers);
22957 free (filedata->section_headers);
22958 free (filedata->string_table);
22959 free (filedata->dump.dump_sects);
22960 free (filedata->dynamic_strings);
22961 free (filedata->dynamic_symbols);
22962 free (filedata->dynamic_syminfo);
22963 free (filedata->dynamic_section);
22965 while (filedata->symtab_shndx_list != NULL)
22967 elf_section_list *next = filedata->symtab_shndx_list->next;
22968 free (filedata->symtab_shndx_list);
22969 filedata->symtab_shndx_list = next;
22972 free (filedata->section_headers_groups);
22974 if (filedata->section_groups)
22976 size_t i;
22977 struct group_list * g;
22978 struct group_list * next;
22980 for (i = 0; i < filedata->group_count; i++)
22982 for (g = filedata->section_groups [i].root; g != NULL; g = next)
22984 next = g->next;
22985 free (g);
22989 free (filedata->section_groups);
22991 memset (&filedata->section_headers, 0,
22992 sizeof (Filedata) - offsetof (Filedata, section_headers));
22995 static void
22996 close_file (Filedata * filedata)
22998 if (filedata)
23000 if (filedata->handle)
23001 fclose (filedata->handle);
23002 free (filedata);
23006 void
23007 close_debug_file (void * data)
23009 free_filedata ((Filedata *) data);
23010 close_file ((Filedata *) data);
23013 static Filedata *
23014 open_file (const char * pathname, bool is_separate)
23016 struct stat statbuf;
23017 Filedata * filedata = NULL;
23019 if (stat (pathname, & statbuf) < 0
23020 || ! S_ISREG (statbuf.st_mode))
23021 goto fail;
23023 filedata = calloc (1, sizeof * filedata);
23024 if (filedata == NULL)
23025 goto fail;
23027 filedata->handle = fopen (pathname, "rb");
23028 if (filedata->handle == NULL)
23029 goto fail;
23031 filedata->file_size = statbuf.st_size;
23032 filedata->file_name = pathname;
23033 filedata->is_separate = is_separate;
23035 if (! get_file_header (filedata))
23036 goto fail;
23038 if (!get_section_headers (filedata, false))
23039 goto fail;
23041 return filedata;
23043 fail:
23044 if (filedata)
23046 if (filedata->handle)
23047 fclose (filedata->handle);
23048 free (filedata);
23050 return NULL;
23053 void *
23054 open_debug_file (const char * pathname)
23056 return open_file (pathname, true);
23059 static void
23060 initialise_dump_sects (Filedata * filedata)
23062 /* Initialise the dump_sects array from the cmdline_dump_sects array.
23063 Note we do this even if cmdline_dump_sects is empty because we
23064 must make sure that the dump_sets array is zeroed out before each
23065 object file is processed. */
23066 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
23067 memset (filedata->dump.dump_sects, 0,
23068 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
23070 if (cmdline.num_dump_sects > 0)
23072 if (filedata->dump.num_dump_sects == 0)
23073 /* A sneaky way of allocating the dump_sects array. */
23074 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
23076 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
23077 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
23078 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
23082 static bool
23083 might_need_separate_debug_info (Filedata * filedata)
23085 /* Debuginfo files do not need further separate file loading. */
23086 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
23087 return false;
23089 /* Since do_follow_links might be enabled by default, only treat it as an
23090 indication that separate files should be loaded if setting it was a
23091 deliberate user action. */
23092 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
23093 return true;
23095 if (process_links || do_syms || do_unwind
23096 || dump_any_debugging || do_dump || do_debugging)
23097 return true;
23099 return false;
23102 /* Process one ELF object file according to the command line options.
23103 This file may actually be stored in an archive. The file is
23104 positioned at the start of the ELF object. Returns TRUE if no
23105 problems were encountered, FALSE otherwise. */
23107 static bool
23108 process_object (Filedata * filedata)
23110 bool have_separate_files;
23111 unsigned int i;
23112 bool res;
23114 if (! get_file_header (filedata))
23116 error (_("%s: Failed to read file header\n"), filedata->file_name);
23117 return false;
23120 /* Initialise per file variables. */
23121 for (i = ARRAY_SIZE (filedata->version_info); i--;)
23122 filedata->version_info[i] = 0;
23124 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
23125 filedata->dynamic_info[i] = 0;
23126 filedata->dynamic_info_DT_GNU_HASH = 0;
23127 filedata->dynamic_info_DT_MIPS_XHASH = 0;
23129 /* Process the file. */
23130 if (show_name)
23131 printf (_("\nFile: %s\n"), filedata->file_name);
23133 initialise_dump_sects (filedata);
23135 /* There may be some extensions in the first section header. Don't
23136 bomb if we can't read it. */
23137 get_section_headers (filedata, true);
23139 if (! process_file_header (filedata))
23141 res = false;
23142 goto out;
23145 /* Throw away the single section header read above, so that we
23146 re-read the entire set. */
23147 free (filedata->section_headers);
23148 filedata->section_headers = NULL;
23150 if (! process_section_headers (filedata))
23152 /* Without loaded section headers we cannot process lots of things. */
23153 do_unwind = do_version = do_dump = do_arch = false;
23155 if (! do_using_dynamic)
23156 do_syms = do_dyn_syms = do_reloc = false;
23159 if (! process_section_groups (filedata))
23160 /* Without loaded section groups we cannot process unwind. */
23161 do_unwind = false;
23163 process_program_headers (filedata);
23165 res = process_dynamic_section (filedata);
23167 if (! process_relocs (filedata))
23168 res = false;
23170 if (! process_unwind (filedata))
23171 res = false;
23173 if (! process_symbol_table (filedata))
23174 res = false;
23176 if (! process_lto_symbol_tables (filedata))
23177 res = false;
23179 if (! process_syminfo (filedata))
23180 res = false;
23182 if (! process_version_sections (filedata))
23183 res = false;
23185 if (might_need_separate_debug_info (filedata))
23186 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
23187 else
23188 have_separate_files = false;
23190 if (! process_section_contents (filedata))
23191 res = false;
23193 if (have_separate_files)
23195 separate_info * d;
23197 for (d = first_separate_info; d != NULL; d = d->next)
23199 initialise_dump_sects (d->handle);
23201 if (process_links && ! process_file_header (d->handle))
23202 res = false;
23203 else if (! process_section_headers (d->handle))
23204 res = false;
23205 else if (! process_section_contents (d->handle))
23206 res = false;
23207 else if (process_links)
23209 if (! process_section_groups (d->handle))
23210 res = false;
23211 process_program_headers (d->handle);
23212 if (! process_dynamic_section (d->handle))
23213 res = false;
23214 if (! process_relocs (d->handle))
23215 res = false;
23216 if (! process_unwind (d->handle))
23217 res = false;
23218 if (! process_symbol_table (d->handle))
23219 res = false;
23220 if (! process_lto_symbol_tables (d->handle))
23221 res = false;
23222 if (! process_syminfo (d->handle))
23223 res = false;
23224 if (! process_version_sections (d->handle))
23225 res = false;
23226 if (! process_notes (d->handle))
23227 res = false;
23231 /* The file handles are closed by the call to free_debug_memory() below. */
23234 if (! process_notes (filedata))
23235 res = false;
23237 if (! process_gnu_liblist (filedata))
23238 res = false;
23240 if (! process_arch_specific (filedata))
23241 res = false;
23243 out:
23244 free_filedata (filedata);
23246 free_debug_memory ();
23248 return res;
23251 /* Process an ELF archive.
23252 On entry the file is positioned just after the ARMAG string.
23253 Returns TRUE upon success, FALSE otherwise. */
23255 static bool
23256 process_archive (Filedata * filedata, bool is_thin_archive)
23258 struct archive_info arch;
23259 struct archive_info nested_arch;
23260 size_t got;
23261 bool ret = true;
23263 show_name = true;
23265 /* The ARCH structure is used to hold information about this archive. */
23266 arch.file_name = NULL;
23267 arch.file = NULL;
23268 arch.index_array = NULL;
23269 arch.sym_table = NULL;
23270 arch.longnames = NULL;
23272 /* The NESTED_ARCH structure is used as a single-item cache of information
23273 about a nested archive (when members of a thin archive reside within
23274 another regular archive file). */
23275 nested_arch.file_name = NULL;
23276 nested_arch.file = NULL;
23277 nested_arch.index_array = NULL;
23278 nested_arch.sym_table = NULL;
23279 nested_arch.longnames = NULL;
23281 if (setup_archive (&arch, filedata->file_name, filedata->handle,
23282 filedata->file_size, is_thin_archive,
23283 do_archive_index) != 0)
23285 ret = false;
23286 goto out;
23289 if (do_archive_index)
23291 if (arch.sym_table == NULL)
23292 error (_("%s: unable to dump the index as none was found\n"),
23293 filedata->file_name);
23294 else
23296 uint64_t i, l;
23297 uint64_t current_pos;
23299 printf (_("Index of archive %s: (%" PRIu64 " entries,"
23300 " %#" PRIx64 " bytes in the symbol table)\n"),
23301 filedata->file_name, arch.index_num,
23302 arch.sym_size);
23304 current_pos = ftell (filedata->handle);
23306 for (i = l = 0; i < arch.index_num; i++)
23308 if (i == 0
23309 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
23311 char * member_name
23312 = get_archive_member_name_at (&arch, arch.index_array[i],
23313 &nested_arch);
23315 if (member_name != NULL)
23317 char * qualified_name
23318 = make_qualified_name (&arch, &nested_arch,
23319 member_name);
23321 if (qualified_name != NULL)
23323 printf (_("Contents of binary %s at offset "),
23324 qualified_name);
23325 (void) print_vma (arch.index_array[i], PREFIX_HEX);
23326 putchar ('\n');
23327 free (qualified_name);
23329 free (member_name);
23333 if (l >= arch.sym_size)
23335 error (_("%s: end of the symbol table reached "
23336 "before the end of the index\n"),
23337 filedata->file_name);
23338 ret = false;
23339 break;
23341 /* PR 17531: file: 0b6630b2. */
23342 printf ("\t%.*s\n",
23343 (int) (arch.sym_size - l), arch.sym_table + l);
23344 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
23347 if (arch.uses_64bit_indices)
23348 l = (l + 7) & ~ 7;
23349 else
23350 l += l & 1;
23352 if (l < arch.sym_size)
23354 error (ngettext ("%s: %" PRId64 " byte remains in the symbol table, "
23355 "but without corresponding entries in "
23356 "the index table\n",
23357 "%s: %" PRId64 " bytes remain in the symbol table, "
23358 "but without corresponding entries in "
23359 "the index table\n",
23360 arch.sym_size - l),
23361 filedata->file_name, arch.sym_size - l);
23362 ret = false;
23365 if (fseek64 (filedata->handle, current_pos, SEEK_SET) != 0)
23367 error (_("%s: failed to seek back to start of object files "
23368 "in the archive\n"),
23369 filedata->file_name);
23370 ret = false;
23371 goto out;
23375 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
23376 && !do_segments && !do_header && !do_dump && !do_version
23377 && !do_histogram && !do_debugging && !do_arch && !do_notes
23378 && !do_section_groups && !do_dyn_syms)
23380 ret = true; /* Archive index only. */
23381 goto out;
23385 while (1)
23387 char * name;
23388 size_t namelen;
23389 char * qualified_name;
23391 /* Read the next archive header. */
23392 if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
23394 error (_("%s: failed to seek to next archive header\n"),
23395 arch.file_name);
23396 ret = false;
23397 break;
23399 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
23400 if (got != sizeof arch.arhdr)
23402 if (got == 0)
23403 break;
23404 /* PR 24049 - we cannot use filedata->file_name as this will
23405 have already been freed. */
23406 error (_("%s: failed to read archive header\n"), arch.file_name);
23408 ret = false;
23409 break;
23411 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
23413 error (_("%s: did not find a valid archive header\n"),
23414 arch.file_name);
23415 ret = false;
23416 break;
23419 arch.next_arhdr_offset += sizeof arch.arhdr;
23421 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
23423 name = get_archive_member_name (&arch, &nested_arch);
23424 if (name == NULL)
23426 error (_("%s: bad archive file name\n"), arch.file_name);
23427 ret = false;
23428 break;
23430 namelen = strlen (name);
23432 qualified_name = make_qualified_name (&arch, &nested_arch, name);
23433 if (qualified_name == NULL)
23435 error (_("%s: bad archive file name\n"), arch.file_name);
23436 free (name);
23437 ret = false;
23438 break;
23441 if (is_thin_archive && arch.nested_member_origin == 0)
23443 /* This is a proxy for an external member of a thin archive. */
23444 Filedata * member_filedata;
23445 char * member_file_name = adjust_relative_path
23446 (filedata->file_name, name, namelen);
23448 free (name);
23449 if (member_file_name == NULL)
23451 free (qualified_name);
23452 ret = false;
23453 break;
23456 member_filedata = open_file (member_file_name, false);
23457 if (member_filedata == NULL)
23459 error (_("Input file '%s' is not readable.\n"), member_file_name);
23460 free (member_file_name);
23461 free (qualified_name);
23462 ret = false;
23463 break;
23466 filedata->archive_file_offset = arch.nested_member_origin;
23467 member_filedata->file_name = qualified_name;
23469 /* The call to process_object() expects the file to be at the beginning. */
23470 rewind (member_filedata->handle);
23472 if (! process_object (member_filedata))
23473 ret = false;
23475 close_file (member_filedata);
23476 free (member_file_name);
23478 else if (is_thin_archive)
23480 Filedata thin_filedata;
23482 memset (&thin_filedata, 0, sizeof (thin_filedata));
23484 /* PR 15140: Allow for corrupt thin archives. */
23485 if (nested_arch.file == NULL)
23487 error (_("%s: contains corrupt thin archive: %s\n"),
23488 qualified_name, name);
23489 free (qualified_name);
23490 free (name);
23491 ret = false;
23492 break;
23494 free (name);
23496 /* This is a proxy for a member of a nested archive. */
23497 filedata->archive_file_offset
23498 = arch.nested_member_origin + sizeof arch.arhdr;
23500 /* The nested archive file will have been opened and setup by
23501 get_archive_member_name. */
23502 if (fseek64 (nested_arch.file, filedata->archive_file_offset,
23503 SEEK_SET) != 0)
23505 error (_("%s: failed to seek to archive member.\n"),
23506 nested_arch.file_name);
23507 free (qualified_name);
23508 ret = false;
23509 break;
23512 thin_filedata.handle = nested_arch.file;
23513 thin_filedata.file_name = qualified_name;
23515 if (! process_object (& thin_filedata))
23516 ret = false;
23518 else
23520 free (name);
23521 filedata->archive_file_offset = arch.next_arhdr_offset;
23522 filedata->file_name = qualified_name;
23523 if (! process_object (filedata))
23524 ret = false;
23525 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
23526 /* Stop looping with "negative" archive_file_size. */
23527 if (arch.next_arhdr_offset < filedata->archive_file_size)
23528 arch.next_arhdr_offset = -1ul;
23531 free (qualified_name);
23534 out:
23535 if (nested_arch.file != NULL)
23536 fclose (nested_arch.file);
23537 release_archive (&nested_arch);
23538 release_archive (&arch);
23540 return ret;
23543 static bool
23544 process_file (char * file_name)
23546 Filedata * filedata = NULL;
23547 struct stat statbuf;
23548 char armag[SARMAG];
23549 bool ret = true;
23551 if (stat (file_name, &statbuf) < 0)
23553 if (errno == ENOENT)
23554 error (_("'%s': No such file\n"), file_name);
23555 else
23556 error (_("Could not locate '%s'. System error message: %s\n"),
23557 file_name, strerror (errno));
23558 return false;
23561 if (! S_ISREG (statbuf.st_mode))
23563 error (_("'%s' is not an ordinary file\n"), file_name);
23564 return false;
23567 filedata = calloc (1, sizeof * filedata);
23568 if (filedata == NULL)
23570 error (_("Out of memory allocating file data structure\n"));
23571 return false;
23574 filedata->file_name = file_name;
23575 filedata->handle = fopen (file_name, "rb");
23576 if (filedata->handle == NULL)
23578 error (_("Input file '%s' is not readable.\n"), file_name);
23579 free (filedata);
23580 return false;
23583 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
23585 error (_("%s: Failed to read file's magic number\n"), file_name);
23586 fclose (filedata->handle);
23587 free (filedata);
23588 return false;
23591 filedata->file_size = statbuf.st_size;
23592 filedata->is_separate = false;
23594 if (memcmp (armag, ARMAG, SARMAG) == 0)
23596 if (! process_archive (filedata, false))
23597 ret = false;
23599 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
23601 if ( ! process_archive (filedata, true))
23602 ret = false;
23604 else
23606 if (do_archive_index && !check_all)
23607 error (_("File %s is not an archive so its index cannot be displayed.\n"),
23608 file_name);
23610 rewind (filedata->handle);
23611 filedata->archive_file_size = filedata->archive_file_offset = 0;
23613 if (! process_object (filedata))
23614 ret = false;
23617 fclose (filedata->handle);
23618 free (filedata->section_headers);
23619 free (filedata->program_headers);
23620 free (filedata->string_table);
23621 free (filedata->dump.dump_sects);
23622 free (filedata);
23624 free (ba_cache.strtab);
23625 ba_cache.strtab = NULL;
23626 free (ba_cache.symtab);
23627 ba_cache.symtab = NULL;
23628 ba_cache.filedata = NULL;
23630 return ret;
23633 #ifdef SUPPORT_DISASSEMBLY
23634 /* Needed by the i386 disassembler. For extra credit, someone could
23635 fix this so that we insert symbolic addresses here, esp for GOT/PLT
23636 symbols. */
23638 void
23639 print_address (unsigned int addr, FILE * outfile)
23641 fprintf (outfile,"0x%8.8x", addr);
23644 /* Needed by the i386 disassembler. */
23646 void
23647 db_task_printsym (unsigned int addr)
23649 print_address (addr, stderr);
23651 #endif
23654 main (int argc, char ** argv)
23656 int err;
23658 #ifdef HAVE_LC_MESSAGES
23659 setlocale (LC_MESSAGES, "");
23660 #endif
23661 setlocale (LC_CTYPE, "");
23662 bindtextdomain (PACKAGE, LOCALEDIR);
23663 textdomain (PACKAGE);
23665 expandargv (&argc, &argv);
23667 parse_args (& cmdline, argc, argv);
23669 if (optind < (argc - 1))
23670 /* When displaying information for more than one file,
23671 prefix the information with the file name. */
23672 show_name = true;
23673 else if (optind >= argc)
23675 /* Ensure that the warning is always displayed. */
23676 do_checks = true;
23678 warn (_("Nothing to do.\n"));
23679 usage (stderr);
23682 err = false;
23683 while (optind < argc)
23684 if (! process_file (argv[optind++]))
23685 err = true;
23687 free (cmdline.dump_sects);
23689 free (dump_ctf_symtab_name);
23690 free (dump_ctf_strtab_name);
23691 free (dump_ctf_parent_name);
23693 return err ? EXIT_FAILURE : EXIT_SUCCESS;