Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / sdbinutils / binutils / readelf.c
blobb45683cd57180642976bb6176472b43d6b2583cd
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
7 This file is part of GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
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 #include <wchar.h>
49 #if __GNUC__ >= 2
50 /* Define BFD64 here, even if our default architecture is 32 bit ELF
51 as this will allow us to read in and parse 64bit and 32bit ELF files.
52 Only do this if we believe that the compiler can support a 64 bit
53 data type. For now we only rely on GCC being able to do this. */
54 #define BFD64
55 #endif
57 #include "bfd.h"
58 #include "bucomm.h"
59 #include "elfcomm.h"
60 #include "demanguse.h"
61 #include "dwarf.h"
62 #include "ctf-api.h"
63 #include "demangle.h"
65 #include "elf/common.h"
66 #include "elf/external.h"
67 #include "elf/internal.h"
70 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
71 we can obtain the H8 reloc numbers. We need these for the
72 get_reloc_size() function. We include h8.h again after defining
73 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
75 #include "elf/h8.h"
76 #undef _ELF_H8_H
78 /* Undo the effects of #including reloc-macros.h. */
80 #undef START_RELOC_NUMBERS
81 #undef RELOC_NUMBER
82 #undef FAKE_RELOC
83 #undef EMPTY_RELOC
84 #undef END_RELOC_NUMBERS
85 #undef _RELOC_MACROS_H
87 /* The following headers use the elf/reloc-macros.h file to
88 automatically generate relocation recognition functions
89 such as elf_mips_reloc_type() */
91 #define RELOC_MACROS_GEN_FUNC
93 #include "elf/aarch64.h"
94 #include "elf/alpha.h"
95 #include "elf/arc.h"
96 #include "elf/arm.h"
97 #include "elf/avr.h"
98 #include "elf/bfin.h"
99 #include "elf/cr16.h"
100 #include "elf/cris.h"
101 #include "elf/crx.h"
102 #include "elf/csky.h"
103 #include "elf/d10v.h"
104 #include "elf/d30v.h"
105 #include "elf/dlx.h"
106 #include "elf/bpf.h"
107 #include "elf/epiphany.h"
108 #include "elf/fr30.h"
109 #include "elf/frv.h"
110 #include "elf/ft32.h"
111 #include "elf/h8.h"
112 #include "elf/hppa.h"
113 #include "elf/i386.h"
114 #include "elf/i370.h"
115 #include "elf/i860.h"
116 #include "elf/i960.h"
117 #include "elf/ia64.h"
118 #include "elf/ip2k.h"
119 #include "elf/lm32.h"
120 #include "elf/iq2000.h"
121 #include "elf/m32c.h"
122 #include "elf/m32r.h"
123 #include "elf/m68k.h"
124 #include "elf/m68hc11.h"
125 #include "elf/s12z.h"
126 #include "elf/mcore.h"
127 #include "elf/mep.h"
128 #include "elf/metag.h"
129 #include "elf/microblaze.h"
130 #include "elf/mips.h"
131 #include "elf/mmix.h"
132 #include "elf/mn10200.h"
133 #include "elf/mn10300.h"
134 #include "elf/moxie.h"
135 #include "elf/mt.h"
136 #include "elf/msp430.h"
137 #include "elf/nds32.h"
138 #include "elf/nfp.h"
139 #include "elf/nios2.h"
140 #include "elf/or1k.h"
141 #include "elf/pj.h"
142 #include "elf/ppc.h"
143 #include "elf/ppc64.h"
144 #include "elf/pru.h"
145 #include "elf/riscv.h"
146 #include "elf/rl78.h"
147 #include "elf/rx.h"
148 #include "elf/s390.h"
149 #include "elf/score.h"
150 #include "elf/sh.h"
151 #include "elf/sparc.h"
152 #include "elf/spu.h"
153 #include "elf/tic6x.h"
154 #include "elf/tilegx.h"
155 #include "elf/tilepro.h"
156 #include "elf/v850.h"
157 #include "elf/vax.h"
158 #include "elf/visium.h"
159 #include "elf/wasm32.h"
160 #include "elf/x86-64.h"
161 #include "elf/xc16x.h"
162 #include "elf/xgate.h"
163 #include "elf/xstormy16.h"
164 #include "elf/xtensa.h"
165 #include "elf/z80.h"
166 #include "elf/loongarch.h"
168 #include "getopt.h"
169 #include "libiberty.h"
170 #include "safe-ctype.h"
171 #include "filenames.h"
173 #ifndef offsetof
174 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
175 #endif
177 typedef struct elf_section_list
179 Elf_Internal_Shdr * hdr;
180 struct elf_section_list * next;
181 } elf_section_list;
183 /* Flag bits indicating particular types of dump. */
184 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
185 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
186 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
187 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
188 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
189 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
191 typedef unsigned char dump_type;
193 /* A linked list of the section names for which dumps were requested. */
194 struct dump_list_entry
196 char * name;
197 dump_type type;
198 struct dump_list_entry * next;
201 /* A dynamic array of flags indicating for which sections a dump
202 has been requested via command line switches. */
203 struct dump_data
205 dump_type * dump_sects;
206 unsigned int num_dump_sects;
209 static struct dump_data cmdline;
211 static struct dump_list_entry * dump_sects_byname;
213 char * program_name = "readelf";
215 static bool show_name = false;
216 static bool do_dynamic = false;
217 static bool do_syms = false;
218 static bool do_dyn_syms = false;
219 static bool do_lto_syms = false;
220 static bool do_reloc = false;
221 static bool do_sections = false;
222 static bool do_section_groups = false;
223 static bool do_section_details = false;
224 static bool do_segments = false;
225 static bool do_unwind = false;
226 static bool do_using_dynamic = false;
227 static bool do_header = false;
228 static bool do_dump = false;
229 static bool do_version = false;
230 static bool do_histogram = false;
231 static bool do_debugging = false;
232 static bool do_ctf = false;
233 static bool do_arch = false;
234 static bool do_notes = false;
235 static bool do_archive_index = false;
236 static bool check_all = false;
237 static bool is_32bit_elf = false;
238 static bool decompress_dumps = false;
239 static bool do_not_show_symbol_truncation = false;
240 static bool do_demangle = false; /* Pretty print C++ symbol names. */
241 static bool process_links = false;
242 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
243 static int sym_base = 0;
245 static char *dump_ctf_parent_name;
246 static char *dump_ctf_symtab_name;
247 static char *dump_ctf_strtab_name;
249 struct group_list
251 struct group_list * next;
252 unsigned int section_index;
255 struct group
257 struct group_list * root;
258 unsigned int group_index;
261 typedef struct filedata
263 const char * file_name;
264 bool is_separate;
265 FILE * handle;
266 bfd_size_type file_size;
267 Elf_Internal_Ehdr file_header;
268 unsigned long archive_file_offset;
269 unsigned long archive_file_size;
270 /* Everything below this point is cleared out by free_filedata. */
271 Elf_Internal_Shdr * section_headers;
272 Elf_Internal_Phdr * program_headers;
273 char * string_table;
274 unsigned long string_table_length;
275 unsigned long dynamic_addr;
276 bfd_size_type dynamic_size;
277 size_t dynamic_nent;
278 Elf_Internal_Dyn * dynamic_section;
279 Elf_Internal_Shdr * dynamic_strtab_section;
280 char * dynamic_strings;
281 unsigned long dynamic_strings_length;
282 Elf_Internal_Shdr * dynamic_symtab_section;
283 unsigned long num_dynamic_syms;
284 Elf_Internal_Sym * dynamic_symbols;
285 bfd_vma version_info[16];
286 unsigned int dynamic_syminfo_nent;
287 Elf_Internal_Syminfo * dynamic_syminfo;
288 unsigned long dynamic_syminfo_offset;
289 bfd_size_type nbuckets;
290 bfd_size_type nchains;
291 bfd_vma * buckets;
292 bfd_vma * chains;
293 bfd_size_type ngnubuckets;
294 bfd_size_type ngnuchains;
295 bfd_vma * gnubuckets;
296 bfd_vma * gnuchains;
297 bfd_vma * mipsxlat;
298 bfd_vma gnusymidx;
299 char * program_interpreter;
300 bfd_vma dynamic_info[DT_ENCODING];
301 bfd_vma dynamic_info_DT_GNU_HASH;
302 bfd_vma dynamic_info_DT_MIPS_XHASH;
303 elf_section_list * symtab_shndx_list;
304 size_t group_count;
305 struct group * section_groups;
306 struct group ** section_headers_groups;
307 /* A dynamic array of flags indicating for which sections a dump of
308 some kind has been requested. It is reset on a per-object file
309 basis and then initialised from the cmdline_dump_sects array,
310 the results of interpreting the -w switch, and the
311 dump_sects_byname list. */
312 struct dump_data dump;
313 } Filedata;
315 /* How to print a vma value. */
316 typedef enum print_mode
318 HEX,
319 HEX_5,
320 DEC,
321 DEC_5,
322 UNSIGNED,
323 UNSIGNED_5,
324 PREFIX_HEX,
325 PREFIX_HEX_5,
326 FULL_HEX,
327 LONG_HEX,
328 OCTAL,
329 OCTAL_5
331 print_mode;
333 typedef enum unicode_display_type
335 unicode_default = 0,
336 unicode_locale,
337 unicode_escape,
338 unicode_hex,
339 unicode_highlight,
340 unicode_invalid
341 } unicode_display_type;
343 static unicode_display_type unicode_display = unicode_default;
345 typedef enum
347 reltype_unknown,
348 reltype_rel,
349 reltype_rela,
350 reltype_relr
351 } relocation_type;
353 /* Versioned symbol info. */
354 enum versioned_symbol_info
356 symbol_undefined,
357 symbol_hidden,
358 symbol_public
361 static const char * get_symbol_version_string
362 (Filedata *, bool, const char *, unsigned long, unsigned,
363 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
365 #define UNKNOWN -1
367 static inline const char *
368 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
370 return filedata->string_table + hdr->sh_name;
373 static inline bool
374 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
376 return (hdr != NULL
377 && filedata->string_table != NULL
378 && hdr->sh_name < filedata->string_table_length);
381 static inline const char *
382 section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
384 if (hdr == NULL)
385 return _("<none>");
386 if (filedata->string_table == NULL)
387 return _("<no-strings>");
388 if (hdr->sh_name >= filedata->string_table_length)
389 return _("<corrupt>");
390 return section_name (filedata, hdr);
393 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
395 static inline bool
396 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
398 return strtab != NULL && offset < strtab_size;
401 static inline bool
402 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
404 return valid_symbol_name (filedata->dynamic_strings,
405 filedata->dynamic_strings_length, offset);
408 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
409 already been called and verified that the string exists. */
410 static inline const char *
411 get_dynamic_name (const Filedata *filedata, size_t offset)
413 return filedata->dynamic_strings + offset;
416 #define REMOVE_ARCH_BITS(ADDR) \
417 do \
419 if (filedata->file_header.e_machine == EM_ARM) \
420 (ADDR) &= ~1; \
422 while (0)
424 /* Get the correct GNU hash section name. */
425 #define GNU_HASH_SECTION_NAME(filedata) \
426 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
428 /* Print a BFD_VMA to an internal buffer, for use in error messages.
429 BFD_FMA_FMT can't be used in translated strings. */
431 static const char *
432 bfd_vmatoa (char *fmtch, bfd_vma value)
434 /* bfd_vmatoa is used more then once in a printf call for output.
435 Cycle through an array of buffers. */
436 static int buf_pos = 0;
437 static struct bfd_vmatoa_buf
439 char place[64];
440 } buf[4];
441 char *ret;
442 char fmt[32];
444 ret = buf[buf_pos++].place;
445 buf_pos %= ARRAY_SIZE (buf);
447 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
448 snprintf (ret, sizeof (buf[0].place), fmt, value);
449 return ret;
452 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
453 OFFSET + the offset of the current archive member, if we are examining an
454 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
455 allocate a buffer using malloc and fill that. In either case return the
456 pointer to the start of the retrieved data or NULL if something went wrong.
457 If something does go wrong and REASON is not NULL then emit an error
458 message using REASON as part of the context. */
460 static void *
461 get_data (void * var,
462 Filedata * filedata,
463 unsigned long offset,
464 bfd_size_type size,
465 bfd_size_type nmemb,
466 const char * reason)
468 void * mvar;
469 bfd_size_type amt = size * nmemb;
471 if (size == 0 || nmemb == 0)
472 return NULL;
474 /* If the size_t type is smaller than the bfd_size_type, eg because
475 you are building a 32-bit tool on a 64-bit host, then make sure
476 that when the sizes are cast to (size_t) no information is lost. */
477 if ((size_t) size != size
478 || (size_t) nmemb != nmemb
479 || (size_t) amt != amt)
481 if (reason)
482 error (_("Size truncation prevents reading %s"
483 " elements of size %s for %s\n"),
484 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
485 return NULL;
488 /* Check for size overflow. */
489 if (amt / size != nmemb || (size_t) amt + 1 == 0)
491 if (reason)
492 error (_("Size overflow prevents reading %s"
493 " elements of size %s for %s\n"),
494 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
495 return NULL;
498 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
499 attempting to allocate memory when the read is bound to fail. */
500 if (filedata->archive_file_offset > filedata->file_size
501 || offset > filedata->file_size - filedata->archive_file_offset
502 || amt > filedata->file_size - filedata->archive_file_offset - offset)
504 if (reason)
505 error (_("Reading %s bytes extends past end of file for %s\n"),
506 bfd_vmatoa ("u", amt), reason);
507 return NULL;
510 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
511 SEEK_SET))
513 if (reason)
514 error (_("Unable to seek to 0x%lx for %s\n"),
515 filedata->archive_file_offset + offset, reason);
516 return NULL;
519 mvar = var;
520 if (mvar == NULL)
522 /* + 1 so that we can '\0' terminate invalid string table sections. */
523 mvar = malloc ((size_t) amt + 1);
525 if (mvar == NULL)
527 if (reason)
528 error (_("Out of memory allocating %s bytes for %s\n"),
529 bfd_vmatoa ("u", amt), reason);
530 return NULL;
533 ((char *) mvar)[amt] = '\0';
536 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
538 if (reason)
539 error (_("Unable to read in %s bytes of %s\n"),
540 bfd_vmatoa ("u", amt), reason);
541 if (mvar != var)
542 free (mvar);
543 return NULL;
546 return mvar;
549 /* Print a VMA value in the MODE specified.
550 Returns the number of characters displayed. */
552 static unsigned int
553 print_vma (bfd_vma vma, print_mode mode)
555 unsigned int nc = 0;
557 switch (mode)
559 case FULL_HEX:
560 nc = printf ("0x");
561 /* Fall through. */
562 case LONG_HEX:
563 #ifdef BFD64
564 if (is_32bit_elf)
565 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
566 #endif
567 printf_vma (vma);
568 return nc + 16;
570 case DEC_5:
571 if (vma <= 99999)
572 return printf ("%5" BFD_VMA_FMT "d", vma);
573 /* Fall through. */
574 case PREFIX_HEX:
575 nc = printf ("0x");
576 /* Fall through. */
577 case HEX:
578 return nc + printf ("%" BFD_VMA_FMT "x", vma);
580 case PREFIX_HEX_5:
581 nc = printf ("0x");
582 /* Fall through. */
583 case HEX_5:
584 return nc + printf ("%05" BFD_VMA_FMT "x", vma);
586 case DEC:
587 return printf ("%" BFD_VMA_FMT "d", vma);
589 case UNSIGNED:
590 return printf ("%" BFD_VMA_FMT "u", vma);
592 case UNSIGNED_5:
593 return printf ("%5" BFD_VMA_FMT "u", vma);
595 case OCTAL:
596 return printf ("%" BFD_VMA_FMT "o", vma);
598 case OCTAL_5:
599 return printf ("%5" BFD_VMA_FMT "o", vma);
601 default:
602 /* FIXME: Report unrecognised mode ? */
603 return 0;
608 /* Display a symbol on stdout. Handles the display of control characters and
609 multibye characters (assuming the host environment supports them).
611 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
613 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
614 abs(WIDTH) - 5 characters followed by "[...]".
616 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
617 padding as necessary.
619 Returns the number of emitted characters. */
621 static unsigned int
622 print_symbol (signed int width, const char * symbol)
624 bool extra_padding = false;
625 bool do_dots = false;
626 signed int num_printed = 0;
627 #ifdef HAVE_MBSTATE_T
628 mbstate_t state;
629 #endif
630 unsigned int width_remaining;
631 const void * alloced_symbol = NULL;
633 if (width < 0)
635 /* Keep the width positive. This helps the code below. */
636 width = - width;
637 extra_padding = true;
639 else if (width == 0)
640 return 0;
642 if (do_wide)
643 /* Set the remaining width to a very large value.
644 This simplifies the code below. */
645 width_remaining = INT_MAX;
646 else
648 width_remaining = width;
649 if (! do_not_show_symbol_truncation
650 && (int) strlen (symbol) > width)
652 width_remaining -= 5;
653 if ((int) width_remaining < 0)
654 width_remaining = 0;
655 do_dots = true;
659 #ifdef HAVE_MBSTATE_T
660 /* Initialise the multibyte conversion state. */
661 memset (& state, 0, sizeof (state));
662 #endif
664 if (do_demangle && *symbol)
666 const char * res = cplus_demangle (symbol, demangle_flags);
668 if (res != NULL)
669 alloced_symbol = symbol = res;
672 while (width_remaining)
674 size_t n;
675 const char c = *symbol++;
677 if (c == 0)
678 break;
680 if (ISPRINT (c))
682 putchar (c);
683 width_remaining --;
684 num_printed ++;
686 else if (ISCNTRL (c))
688 /* Do not print control characters directly as they can affect terminal
689 settings. Such characters usually appear in the names generated
690 by the assembler for local labels. */
692 if (width_remaining < 2)
693 break;
695 printf ("^%c", c + 0x40);
696 width_remaining -= 2;
697 num_printed += 2;
699 else if (c == 0x7f)
701 if (width_remaining < 5)
702 break;
703 printf ("<DEL>");
704 width_remaining -= 5;
705 num_printed += 5;
707 else if (unicode_display != unicode_locale
708 && unicode_display != unicode_default)
710 /* Display unicode characters as something else. */
711 unsigned char bytes[4];
712 bool is_utf8;
713 unsigned int nbytes;
715 bytes[0] = c;
717 if (bytes[0] < 0xc0)
719 nbytes = 1;
720 is_utf8 = false;
722 else
724 bytes[1] = *symbol++;
726 if ((bytes[1] & 0xc0) != 0x80)
728 is_utf8 = false;
729 /* Do not consume this character. It may only
730 be the first byte in the sequence that was
731 corrupt. */
732 --symbol;
733 nbytes = 1;
735 else if ((bytes[0] & 0x20) == 0)
737 is_utf8 = true;
738 nbytes = 2;
740 else
742 bytes[2] = *symbol++;
744 if ((bytes[2] & 0xc0) != 0x80)
746 is_utf8 = false;
747 symbol -= 2;
748 nbytes = 1;
750 else if ((bytes[0] & 0x10) == 0)
752 is_utf8 = true;
753 nbytes = 3;
755 else
757 bytes[3] = *symbol++;
759 nbytes = 4;
761 if ((bytes[3] & 0xc0) != 0x80)
763 is_utf8 = false;
764 symbol -= 3;
765 nbytes = 1;
767 else
768 is_utf8 = true;
773 if (unicode_display == unicode_invalid)
774 is_utf8 = false;
776 if (unicode_display == unicode_hex || ! is_utf8)
778 unsigned int i;
780 if (width_remaining < (nbytes * 2) + 2)
781 break;
783 putchar (is_utf8 ? '<' : '{');
784 printf ("0x");
785 for (i = 0; i < nbytes; i++)
786 printf ("%02x", bytes[i]);
787 putchar (is_utf8 ? '>' : '}');
789 else
791 if (unicode_display == unicode_highlight && isatty (1))
792 printf ("\x1B[31;47m"); /* Red. */
794 switch (nbytes)
796 case 2:
797 if (width_remaining < 6)
798 break;
799 printf ("\\u%02x%02x",
800 (bytes[0] & 0x1c) >> 2,
801 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
802 break;
803 case 3:
804 if (width_remaining < 6)
805 break;
806 printf ("\\u%02x%02x",
807 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
808 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
809 break;
810 case 4:
811 if (width_remaining < 8)
812 break;
813 printf ("\\u%02x%02x%02x",
814 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
815 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
816 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
818 break;
819 default:
820 /* URG. */
821 break;
824 if (unicode_display == unicode_highlight && isatty (1))
825 printf ("\033[0m"); /* Default colour. */
828 if (bytes[nbytes - 1] == 0)
829 break;
831 else
833 #ifdef HAVE_MBSTATE_T
834 wchar_t w;
835 #endif
836 /* Let printf do the hard work of displaying multibyte characters. */
837 printf ("%.1s", symbol - 1);
838 width_remaining --;
839 num_printed ++;
841 #ifdef HAVE_MBSTATE_T
842 /* Try to find out how many bytes made up the character that was
843 just printed. Advance the symbol pointer past the bytes that
844 were displayed. */
845 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
846 #else
847 n = 1;
848 #endif
849 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
850 symbol += (n - 1);
854 if (do_dots)
855 num_printed += printf ("[...]");
857 if (extra_padding && num_printed < width)
859 /* Fill in the remaining spaces. */
860 printf ("%-*s", width - num_printed, " ");
861 num_printed = width;
864 free ((void *) alloced_symbol);
865 return num_printed;
868 /* Returns a pointer to a static buffer containing a printable version of
869 the given section's name. Like print_symbol, except that it does not try
870 to print multibyte characters, it just interprets them as hex values. */
872 static const char *
873 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
875 #define MAX_PRINT_SEC_NAME_LEN 256
876 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
877 const char * name = section_name_print (filedata, sec);
878 char * buf = sec_name_buf;
879 char c;
880 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
882 while ((c = * name ++) != 0)
884 if (ISCNTRL (c))
886 if (remaining < 2)
887 break;
889 * buf ++ = '^';
890 * buf ++ = c + 0x40;
891 remaining -= 2;
893 else if (ISPRINT (c))
895 * buf ++ = c;
896 remaining -= 1;
898 else
900 static char hex[17] = "0123456789ABCDEF";
902 if (remaining < 4)
903 break;
904 * buf ++ = '<';
905 * buf ++ = hex[(c & 0xf0) >> 4];
906 * buf ++ = hex[c & 0x0f];
907 * buf ++ = '>';
908 remaining -= 4;
911 if (remaining == 0)
912 break;
915 * buf = 0;
916 return sec_name_buf;
919 static const char *
920 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
922 if (ndx >= filedata->file_header.e_shnum)
923 return _("<corrupt>");
925 return printable_section_name (filedata, filedata->section_headers + ndx);
928 /* Return a pointer to section NAME, or NULL if no such section exists. */
930 static Elf_Internal_Shdr *
931 find_section (Filedata * filedata, const char * name)
933 unsigned int i;
935 if (filedata->section_headers == NULL)
936 return NULL;
938 for (i = 0; i < filedata->file_header.e_shnum; i++)
939 if (section_name_valid (filedata, filedata->section_headers + i)
940 && streq (section_name (filedata, filedata->section_headers + i),
941 name))
942 return filedata->section_headers + i;
944 return NULL;
947 /* Return a pointer to a section containing ADDR, or NULL if no such
948 section exists. */
950 static Elf_Internal_Shdr *
951 find_section_by_address (Filedata * filedata, bfd_vma addr)
953 unsigned int i;
955 if (filedata->section_headers == NULL)
956 return NULL;
958 for (i = 0; i < filedata->file_header.e_shnum; i++)
960 Elf_Internal_Shdr *sec = filedata->section_headers + i;
962 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
963 return sec;
966 return NULL;
969 static Elf_Internal_Shdr *
970 find_section_by_type (Filedata * filedata, unsigned int type)
972 unsigned int i;
974 if (filedata->section_headers == NULL)
975 return NULL;
977 for (i = 0; i < filedata->file_header.e_shnum; i++)
979 Elf_Internal_Shdr *sec = filedata->section_headers + i;
981 if (sec->sh_type == type)
982 return sec;
985 return NULL;
988 /* Return a pointer to section NAME, or NULL if no such section exists,
989 restricted to the list of sections given in SET. */
991 static Elf_Internal_Shdr *
992 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
994 unsigned int i;
996 if (filedata->section_headers == NULL)
997 return NULL;
999 if (set != NULL)
1001 while ((i = *set++) > 0)
1003 /* See PR 21156 for a reproducer. */
1004 if (i >= filedata->file_header.e_shnum)
1005 continue; /* FIXME: Should we issue an error message ? */
1007 if (section_name_valid (filedata, filedata->section_headers + i)
1008 && streq (section_name (filedata, filedata->section_headers + i),
1009 name))
1010 return filedata->section_headers + i;
1014 return find_section (filedata, name);
1017 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
1018 This OS has so many departures from the ELF standard that we test it at
1019 many places. */
1021 static inline bool
1022 is_ia64_vms (Filedata * filedata)
1024 return filedata->file_header.e_machine == EM_IA_64
1025 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
1028 /* Guess the relocation size commonly used by the specific machines. */
1030 static bool
1031 guess_is_rela (unsigned int e_machine)
1033 switch (e_machine)
1035 /* Targets that use REL relocations. */
1036 case EM_386:
1037 case EM_IAMCU:
1038 case EM_960:
1039 case EM_ARM:
1040 case EM_D10V:
1041 case EM_CYGNUS_D10V:
1042 case EM_DLX:
1043 case EM_MIPS:
1044 case EM_MIPS_RS3_LE:
1045 case EM_CYGNUS_M32R:
1046 case EM_SCORE:
1047 case EM_XGATE:
1048 case EM_NFP:
1049 case EM_BPF:
1050 return false;
1052 /* Targets that use RELA relocations. */
1053 case EM_68K:
1054 case EM_860:
1055 case EM_AARCH64:
1056 case EM_ADAPTEVA_EPIPHANY:
1057 case EM_ALPHA:
1058 case EM_ALTERA_NIOS2:
1059 case EM_ARC:
1060 case EM_ARC_COMPACT:
1061 case EM_ARC_COMPACT2:
1062 case EM_AVR:
1063 case EM_AVR_OLD:
1064 case EM_BLACKFIN:
1065 case EM_CR16:
1066 case EM_CRIS:
1067 case EM_CRX:
1068 case EM_CSKY:
1069 case EM_D30V:
1070 case EM_CYGNUS_D30V:
1071 case EM_FR30:
1072 case EM_FT32:
1073 case EM_CYGNUS_FR30:
1074 case EM_CYGNUS_FRV:
1075 case EM_H8S:
1076 case EM_H8_300:
1077 case EM_H8_300H:
1078 case EM_IA_64:
1079 case EM_IP2K:
1080 case EM_IP2K_OLD:
1081 case EM_IQ2000:
1082 case EM_LATTICEMICO32:
1083 case EM_M32C_OLD:
1084 case EM_M32C:
1085 case EM_M32R:
1086 case EM_MCORE:
1087 case EM_CYGNUS_MEP:
1088 case EM_METAG:
1089 case EM_MMIX:
1090 case EM_MN10200:
1091 case EM_CYGNUS_MN10200:
1092 case EM_MN10300:
1093 case EM_CYGNUS_MN10300:
1094 case EM_MOXIE:
1095 case EM_MSP430:
1096 case EM_MSP430_OLD:
1097 case EM_MT:
1098 case EM_NDS32:
1099 case EM_NIOS32:
1100 case EM_OR1K:
1101 case EM_PPC64:
1102 case EM_PPC:
1103 case EM_TI_PRU:
1104 case EM_RISCV:
1105 case EM_RL78:
1106 case EM_RX:
1107 case EM_S390:
1108 case EM_S390_OLD:
1109 case EM_SH:
1110 case EM_SPARC:
1111 case EM_SPARC32PLUS:
1112 case EM_SPARCV9:
1113 case EM_SPU:
1114 case EM_TI_C6000:
1115 case EM_TILEGX:
1116 case EM_TILEPRO:
1117 case EM_V800:
1118 case EM_V850:
1119 case EM_CYGNUS_V850:
1120 case EM_VAX:
1121 case EM_VISIUM:
1122 case EM_X86_64:
1123 case EM_L1OM:
1124 case EM_K1OM:
1125 case EM_XSTORMY16:
1126 case EM_XTENSA:
1127 case EM_XTENSA_OLD:
1128 case EM_MICROBLAZE:
1129 case EM_MICROBLAZE_OLD:
1130 case EM_WEBASSEMBLY:
1131 return true;
1133 case EM_68HC05:
1134 case EM_68HC08:
1135 case EM_68HC11:
1136 case EM_68HC16:
1137 case EM_FX66:
1138 case EM_ME16:
1139 case EM_MMA:
1140 case EM_NCPU:
1141 case EM_NDR1:
1142 case EM_PCP:
1143 case EM_ST100:
1144 case EM_ST19:
1145 case EM_ST7:
1146 case EM_ST9PLUS:
1147 case EM_STARCORE:
1148 case EM_SVX:
1149 case EM_TINYJ:
1150 default:
1151 warn (_("Don't know about relocations on this machine architecture\n"));
1152 return false;
1156 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1157 Returns TRUE upon success, FALSE otherwise. If successful then a
1158 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1159 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1160 responsibility to free the allocated buffer. */
1162 static bool
1163 slurp_rela_relocs (Filedata * filedata,
1164 unsigned long rel_offset,
1165 unsigned long rel_size,
1166 Elf_Internal_Rela ** relasp,
1167 unsigned long * nrelasp)
1169 Elf_Internal_Rela * relas;
1170 size_t nrelas;
1171 unsigned int i;
1173 if (is_32bit_elf)
1175 Elf32_External_Rela * erelas;
1177 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1178 rel_size, _("32-bit relocation data"));
1179 if (!erelas)
1180 return false;
1182 nrelas = rel_size / sizeof (Elf32_External_Rela);
1184 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1185 sizeof (Elf_Internal_Rela));
1187 if (relas == NULL)
1189 free (erelas);
1190 error (_("out of memory parsing relocs\n"));
1191 return false;
1194 for (i = 0; i < nrelas; i++)
1196 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1197 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1198 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1201 free (erelas);
1203 else
1205 Elf64_External_Rela * erelas;
1207 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1208 rel_size, _("64-bit relocation data"));
1209 if (!erelas)
1210 return false;
1212 nrelas = rel_size / sizeof (Elf64_External_Rela);
1214 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1215 sizeof (Elf_Internal_Rela));
1217 if (relas == NULL)
1219 free (erelas);
1220 error (_("out of memory parsing relocs\n"));
1221 return false;
1224 for (i = 0; i < nrelas; i++)
1226 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1227 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1228 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1230 /* The #ifdef BFD64 below is to prevent a compile time
1231 warning. We know that if we do not have a 64 bit data
1232 type that we will never execute this code anyway. */
1233 #ifdef BFD64
1234 if (filedata->file_header.e_machine == EM_MIPS
1235 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1237 /* In little-endian objects, r_info isn't really a
1238 64-bit little-endian value: it has a 32-bit
1239 little-endian symbol index followed by four
1240 individual byte fields. Reorder INFO
1241 accordingly. */
1242 bfd_vma inf = relas[i].r_info;
1243 inf = (((inf & 0xffffffff) << 32)
1244 | ((inf >> 56) & 0xff)
1245 | ((inf >> 40) & 0xff00)
1246 | ((inf >> 24) & 0xff0000)
1247 | ((inf >> 8) & 0xff000000));
1248 relas[i].r_info = inf;
1250 #endif /* BFD64 */
1253 free (erelas);
1256 *relasp = relas;
1257 *nrelasp = nrelas;
1258 return true;
1261 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1262 Returns TRUE upon success, FALSE otherwise. If successful then a
1263 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1264 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1265 responsibility to free the allocated buffer. */
1267 static bool
1268 slurp_rel_relocs (Filedata * filedata,
1269 unsigned long rel_offset,
1270 unsigned long rel_size,
1271 Elf_Internal_Rela ** relsp,
1272 unsigned long * nrelsp)
1274 Elf_Internal_Rela * rels;
1275 size_t nrels;
1276 unsigned int i;
1278 if (is_32bit_elf)
1280 Elf32_External_Rel * erels;
1282 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1283 rel_size, _("32-bit relocation data"));
1284 if (!erels)
1285 return false;
1287 nrels = rel_size / sizeof (Elf32_External_Rel);
1289 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1291 if (rels == NULL)
1293 free (erels);
1294 error (_("out of memory parsing relocs\n"));
1295 return false;
1298 for (i = 0; i < nrels; i++)
1300 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1301 rels[i].r_info = BYTE_GET (erels[i].r_info);
1302 rels[i].r_addend = 0;
1305 free (erels);
1307 else
1309 Elf64_External_Rel * erels;
1311 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1312 rel_size, _("64-bit relocation data"));
1313 if (!erels)
1314 return false;
1316 nrels = rel_size / sizeof (Elf64_External_Rel);
1318 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1320 if (rels == NULL)
1322 free (erels);
1323 error (_("out of memory parsing relocs\n"));
1324 return false;
1327 for (i = 0; i < nrels; i++)
1329 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1330 rels[i].r_info = BYTE_GET (erels[i].r_info);
1331 rels[i].r_addend = 0;
1333 /* The #ifdef BFD64 below is to prevent a compile time
1334 warning. We know that if we do not have a 64 bit data
1335 type that we will never execute this code anyway. */
1336 #ifdef BFD64
1337 if (filedata->file_header.e_machine == EM_MIPS
1338 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1340 /* In little-endian objects, r_info isn't really a
1341 64-bit little-endian value: it has a 32-bit
1342 little-endian symbol index followed by four
1343 individual byte fields. Reorder INFO
1344 accordingly. */
1345 bfd_vma inf = rels[i].r_info;
1346 inf = (((inf & 0xffffffff) << 32)
1347 | ((inf >> 56) & 0xff)
1348 | ((inf >> 40) & 0xff00)
1349 | ((inf >> 24) & 0xff0000)
1350 | ((inf >> 8) & 0xff000000));
1351 rels[i].r_info = inf;
1353 #endif /* BFD64 */
1356 free (erels);
1359 *relsp = rels;
1360 *nrelsp = nrels;
1361 return true;
1364 static bool
1365 slurp_relr_relocs (Filedata * filedata,
1366 unsigned long relr_offset,
1367 unsigned long relr_size,
1368 bfd_vma ** relrsp,
1369 unsigned long * nrelrsp)
1371 void *relrs;
1372 size_t size = 0, nentries, i;
1373 bfd_vma base = 0, addr, entry;
1375 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1376 _("RELR relocation data"));
1377 if (!relrs)
1378 return false;
1380 if (is_32bit_elf)
1381 nentries = relr_size / sizeof (Elf32_External_Relr);
1382 else
1383 nentries = relr_size / sizeof (Elf64_External_Relr);
1384 for (i = 0; i < nentries; i++)
1386 if (is_32bit_elf)
1387 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1388 else
1389 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1390 if ((entry & 1) == 0)
1391 size++;
1392 else
1393 while ((entry >>= 1) != 0)
1394 if ((entry & 1) == 1)
1395 size++;
1398 *relrsp = (bfd_vma *) xmalloc (size * sizeof (bfd_vma));
1399 if (*relrsp == NULL)
1401 free (relrs);
1402 error (_("out of memory parsing relocs\n"));
1403 return false;
1406 size = 0;
1407 for (i = 0; i < nentries; i++)
1409 const bfd_vma entry_bytes = is_32bit_elf ? 4 : 8;
1411 if (is_32bit_elf)
1412 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1413 else
1414 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1415 if ((entry & 1) == 0)
1417 (*relrsp)[size++] = entry;
1418 base = entry + entry_bytes;
1420 else
1422 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1423 if ((entry & 1) != 0)
1424 (*relrsp)[size++] = addr;
1425 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1429 *nrelrsp = size;
1430 free (relrs);
1431 return true;
1434 /* Returns the reloc type extracted from the reloc info field. */
1436 static unsigned int
1437 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1439 if (is_32bit_elf)
1440 return ELF32_R_TYPE (reloc_info);
1442 switch (filedata->file_header.e_machine)
1444 case EM_MIPS:
1445 /* Note: We assume that reloc_info has already been adjusted for us. */
1446 return ELF64_MIPS_R_TYPE (reloc_info);
1448 case EM_SPARCV9:
1449 return ELF64_R_TYPE_ID (reloc_info);
1451 default:
1452 return ELF64_R_TYPE (reloc_info);
1456 /* Return the symbol index extracted from the reloc info field. */
1458 static bfd_vma
1459 get_reloc_symindex (bfd_vma reloc_info)
1461 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1464 static inline bool
1465 uses_msp430x_relocs (Filedata * filedata)
1467 return
1468 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1469 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1470 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1471 /* TI compiler uses ELFOSABI_NONE. */
1472 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1475 /* Display the contents of the relocation data found at the specified
1476 offset. */
1478 static bool
1479 dump_relocations (Filedata * filedata,
1480 unsigned long rel_offset,
1481 unsigned long rel_size,
1482 Elf_Internal_Sym * symtab,
1483 unsigned long nsyms,
1484 char * strtab,
1485 unsigned long strtablen,
1486 relocation_type rel_type,
1487 bool is_dynsym)
1489 unsigned long i;
1490 Elf_Internal_Rela * rels;
1491 bool res = true;
1493 if (rel_type == reltype_unknown)
1494 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1496 if (rel_type == reltype_rela)
1498 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1499 return false;
1501 else if (rel_type == reltype_rel)
1503 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1504 return false;
1506 else if (rel_type == reltype_relr)
1508 bfd_vma * relrs;
1509 const char *format
1510 = is_32bit_elf ? "%08" BFD_VMA_FMT "x\n" : "%016" BFD_VMA_FMT "x\n";
1512 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1513 &rel_size))
1514 return false;
1516 printf (ngettext (" %lu offset\n", " %lu offsets\n", rel_size), rel_size);
1517 for (i = 0; i < rel_size; i++)
1518 printf (format, relrs[i]);
1519 free (relrs);
1520 return true;
1523 if (is_32bit_elf)
1525 if (rel_type == reltype_rela)
1527 if (do_wide)
1528 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1529 else
1530 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1532 else
1534 if (do_wide)
1535 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1536 else
1537 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1540 else
1542 if (rel_type == reltype_rela)
1544 if (do_wide)
1545 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1546 else
1547 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1549 else
1551 if (do_wide)
1552 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1553 else
1554 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1558 for (i = 0; i < rel_size; i++)
1560 const char * rtype;
1561 bfd_vma offset;
1562 bfd_vma inf;
1563 bfd_vma symtab_index;
1564 bfd_vma type;
1566 offset = rels[i].r_offset;
1567 inf = rels[i].r_info;
1569 type = get_reloc_type (filedata, inf);
1570 symtab_index = get_reloc_symindex (inf);
1572 if (is_32bit_elf)
1574 printf ("%8.8lx %8.8lx ",
1575 (unsigned long) offset & 0xffffffff,
1576 (unsigned long) inf & 0xffffffff);
1578 else
1580 printf (do_wide
1581 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1582 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
1583 offset, inf);
1586 switch (filedata->file_header.e_machine)
1588 default:
1589 rtype = NULL;
1590 break;
1592 case EM_AARCH64:
1593 rtype = elf_aarch64_reloc_type (type);
1594 break;
1596 case EM_M32R:
1597 case EM_CYGNUS_M32R:
1598 rtype = elf_m32r_reloc_type (type);
1599 break;
1601 case EM_386:
1602 case EM_IAMCU:
1603 rtype = elf_i386_reloc_type (type);
1604 break;
1606 case EM_68HC11:
1607 case EM_68HC12:
1608 rtype = elf_m68hc11_reloc_type (type);
1609 break;
1611 case EM_S12Z:
1612 rtype = elf_s12z_reloc_type (type);
1613 break;
1615 case EM_68K:
1616 rtype = elf_m68k_reloc_type (type);
1617 break;
1619 case EM_960:
1620 rtype = elf_i960_reloc_type (type);
1621 break;
1623 case EM_AVR:
1624 case EM_AVR_OLD:
1625 rtype = elf_avr_reloc_type (type);
1626 break;
1628 case EM_OLD_SPARCV9:
1629 case EM_SPARC32PLUS:
1630 case EM_SPARCV9:
1631 case EM_SPARC:
1632 rtype = elf_sparc_reloc_type (type);
1633 break;
1635 case EM_SPU:
1636 rtype = elf_spu_reloc_type (type);
1637 break;
1639 case EM_V800:
1640 rtype = v800_reloc_type (type);
1641 break;
1642 case EM_V850:
1643 case EM_CYGNUS_V850:
1644 rtype = v850_reloc_type (type);
1645 break;
1647 case EM_D10V:
1648 case EM_CYGNUS_D10V:
1649 rtype = elf_d10v_reloc_type (type);
1650 break;
1652 case EM_D30V:
1653 case EM_CYGNUS_D30V:
1654 rtype = elf_d30v_reloc_type (type);
1655 break;
1657 case EM_DLX:
1658 rtype = elf_dlx_reloc_type (type);
1659 break;
1661 case EM_SH:
1662 rtype = elf_sh_reloc_type (type);
1663 break;
1665 case EM_MN10300:
1666 case EM_CYGNUS_MN10300:
1667 rtype = elf_mn10300_reloc_type (type);
1668 break;
1670 case EM_MN10200:
1671 case EM_CYGNUS_MN10200:
1672 rtype = elf_mn10200_reloc_type (type);
1673 break;
1675 case EM_FR30:
1676 case EM_CYGNUS_FR30:
1677 rtype = elf_fr30_reloc_type (type);
1678 break;
1680 case EM_CYGNUS_FRV:
1681 rtype = elf_frv_reloc_type (type);
1682 break;
1684 case EM_CSKY:
1685 rtype = elf_csky_reloc_type (type);
1686 break;
1688 case EM_FT32:
1689 rtype = elf_ft32_reloc_type (type);
1690 break;
1692 case EM_MCORE:
1693 rtype = elf_mcore_reloc_type (type);
1694 break;
1696 case EM_MMIX:
1697 rtype = elf_mmix_reloc_type (type);
1698 break;
1700 case EM_MOXIE:
1701 rtype = elf_moxie_reloc_type (type);
1702 break;
1704 case EM_MSP430:
1705 if (uses_msp430x_relocs (filedata))
1707 rtype = elf_msp430x_reloc_type (type);
1708 break;
1710 /* Fall through. */
1711 case EM_MSP430_OLD:
1712 rtype = elf_msp430_reloc_type (type);
1713 break;
1715 case EM_NDS32:
1716 rtype = elf_nds32_reloc_type (type);
1717 break;
1719 case EM_PPC:
1720 rtype = elf_ppc_reloc_type (type);
1721 break;
1723 case EM_PPC64:
1724 rtype = elf_ppc64_reloc_type (type);
1725 break;
1727 case EM_MIPS:
1728 case EM_MIPS_RS3_LE:
1729 rtype = elf_mips_reloc_type (type);
1730 break;
1732 case EM_RISCV:
1733 rtype = elf_riscv_reloc_type (type);
1734 break;
1736 case EM_ALPHA:
1737 rtype = elf_alpha_reloc_type (type);
1738 break;
1740 case EM_ARM:
1741 rtype = elf_arm_reloc_type (type);
1742 break;
1744 case EM_ARC:
1745 case EM_ARC_COMPACT:
1746 case EM_ARC_COMPACT2:
1747 rtype = elf_arc_reloc_type (type);
1748 break;
1750 case EM_PARISC:
1751 rtype = elf_hppa_reloc_type (type);
1752 break;
1754 case EM_H8_300:
1755 case EM_H8_300H:
1756 case EM_H8S:
1757 rtype = elf_h8_reloc_type (type);
1758 break;
1760 case EM_OR1K:
1761 rtype = elf_or1k_reloc_type (type);
1762 break;
1764 case EM_PJ:
1765 case EM_PJ_OLD:
1766 rtype = elf_pj_reloc_type (type);
1767 break;
1768 case EM_IA_64:
1769 rtype = elf_ia64_reloc_type (type);
1770 break;
1772 case EM_CRIS:
1773 rtype = elf_cris_reloc_type (type);
1774 break;
1776 case EM_860:
1777 rtype = elf_i860_reloc_type (type);
1778 break;
1780 case EM_X86_64:
1781 case EM_L1OM:
1782 case EM_K1OM:
1783 rtype = elf_x86_64_reloc_type (type);
1784 break;
1786 case EM_S370:
1787 rtype = i370_reloc_type (type);
1788 break;
1790 case EM_S390_OLD:
1791 case EM_S390:
1792 rtype = elf_s390_reloc_type (type);
1793 break;
1795 case EM_SCORE:
1796 rtype = elf_score_reloc_type (type);
1797 break;
1799 case EM_XSTORMY16:
1800 rtype = elf_xstormy16_reloc_type (type);
1801 break;
1803 case EM_CRX:
1804 rtype = elf_crx_reloc_type (type);
1805 break;
1807 case EM_VAX:
1808 rtype = elf_vax_reloc_type (type);
1809 break;
1811 case EM_VISIUM:
1812 rtype = elf_visium_reloc_type (type);
1813 break;
1815 case EM_BPF:
1816 rtype = elf_bpf_reloc_type (type);
1817 break;
1819 case EM_ADAPTEVA_EPIPHANY:
1820 rtype = elf_epiphany_reloc_type (type);
1821 break;
1823 case EM_IP2K:
1824 case EM_IP2K_OLD:
1825 rtype = elf_ip2k_reloc_type (type);
1826 break;
1828 case EM_IQ2000:
1829 rtype = elf_iq2000_reloc_type (type);
1830 break;
1832 case EM_XTENSA_OLD:
1833 case EM_XTENSA:
1834 rtype = elf_xtensa_reloc_type (type);
1835 break;
1837 case EM_LATTICEMICO32:
1838 rtype = elf_lm32_reloc_type (type);
1839 break;
1841 case EM_M32C_OLD:
1842 case EM_M32C:
1843 rtype = elf_m32c_reloc_type (type);
1844 break;
1846 case EM_MT:
1847 rtype = elf_mt_reloc_type (type);
1848 break;
1850 case EM_BLACKFIN:
1851 rtype = elf_bfin_reloc_type (type);
1852 break;
1854 case EM_CYGNUS_MEP:
1855 rtype = elf_mep_reloc_type (type);
1856 break;
1858 case EM_CR16:
1859 rtype = elf_cr16_reloc_type (type);
1860 break;
1862 case EM_MICROBLAZE:
1863 case EM_MICROBLAZE_OLD:
1864 rtype = elf_microblaze_reloc_type (type);
1865 break;
1867 case EM_RL78:
1868 rtype = elf_rl78_reloc_type (type);
1869 break;
1871 case EM_RX:
1872 rtype = elf_rx_reloc_type (type);
1873 break;
1875 case EM_METAG:
1876 rtype = elf_metag_reloc_type (type);
1877 break;
1879 case EM_XC16X:
1880 case EM_C166:
1881 rtype = elf_xc16x_reloc_type (type);
1882 break;
1884 case EM_TI_C6000:
1885 rtype = elf_tic6x_reloc_type (type);
1886 break;
1888 case EM_TILEGX:
1889 rtype = elf_tilegx_reloc_type (type);
1890 break;
1892 case EM_TILEPRO:
1893 rtype = elf_tilepro_reloc_type (type);
1894 break;
1896 case EM_WEBASSEMBLY:
1897 rtype = elf_wasm32_reloc_type (type);
1898 break;
1900 case EM_XGATE:
1901 rtype = elf_xgate_reloc_type (type);
1902 break;
1904 case EM_ALTERA_NIOS2:
1905 rtype = elf_nios2_reloc_type (type);
1906 break;
1908 case EM_TI_PRU:
1909 rtype = elf_pru_reloc_type (type);
1910 break;
1912 case EM_NFP:
1913 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1914 rtype = elf_nfp3200_reloc_type (type);
1915 else
1916 rtype = elf_nfp_reloc_type (type);
1917 break;
1919 case EM_Z80:
1920 rtype = elf_z80_reloc_type (type);
1921 break;
1923 case EM_LOONGARCH:
1924 rtype = elf_loongarch_reloc_type (type);
1925 break;
1929 if (rtype == NULL)
1930 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1931 else
1932 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1934 if (filedata->file_header.e_machine == EM_ALPHA
1935 && rtype != NULL
1936 && streq (rtype, "R_ALPHA_LITUSE")
1937 && rel_type == reltype_rela)
1939 switch (rels[i].r_addend)
1941 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1942 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1943 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1944 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1945 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1946 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1947 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1948 default: rtype = NULL;
1951 if (rtype)
1952 printf (" (%s)", rtype);
1953 else
1955 putchar (' ');
1956 printf (_("<unknown addend: %lx>"),
1957 (unsigned long) rels[i].r_addend);
1958 res = false;
1961 else if (symtab_index)
1963 if (symtab == NULL || symtab_index >= nsyms)
1965 error (_(" bad symbol index: %08lx in reloc\n"),
1966 (unsigned long) symtab_index);
1967 res = false;
1969 else
1971 Elf_Internal_Sym * psym;
1972 const char * version_string;
1973 enum versioned_symbol_info sym_info;
1974 unsigned short vna_other;
1976 psym = symtab + symtab_index;
1978 version_string
1979 = get_symbol_version_string (filedata, is_dynsym,
1980 strtab, strtablen,
1981 symtab_index,
1982 psym,
1983 &sym_info,
1984 &vna_other);
1986 printf (" ");
1988 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1990 const char * name;
1991 unsigned int len;
1992 unsigned int width = is_32bit_elf ? 8 : 14;
1994 /* Relocations against GNU_IFUNC symbols do not use the value
1995 of the symbol as the address to relocate against. Instead
1996 they invoke the function named by the symbol and use its
1997 result as the address for relocation.
1999 To indicate this to the user, do not display the value of
2000 the symbol in the "Symbols's Value" field. Instead show
2001 its name followed by () as a hint that the symbol is
2002 invoked. */
2004 if (strtab == NULL
2005 || psym->st_name == 0
2006 || psym->st_name >= strtablen)
2007 name = "??";
2008 else
2009 name = strtab + psym->st_name;
2011 len = print_symbol (width, name);
2012 if (version_string)
2013 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2014 version_string);
2015 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
2017 else
2019 print_vma (psym->st_value, LONG_HEX);
2021 printf (is_32bit_elf ? " " : " ");
2024 if (psym->st_name == 0)
2026 const char * sec_name = "<null>";
2027 char name_buf[40];
2029 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2031 if (psym->st_shndx < filedata->file_header.e_shnum
2032 && filedata->section_headers != NULL)
2033 sec_name = section_name_print (filedata,
2034 filedata->section_headers
2035 + psym->st_shndx);
2036 else if (psym->st_shndx == SHN_ABS)
2037 sec_name = "ABS";
2038 else if (psym->st_shndx == SHN_COMMON)
2039 sec_name = "COMMON";
2040 else if ((filedata->file_header.e_machine == EM_MIPS
2041 && psym->st_shndx == SHN_MIPS_SCOMMON)
2042 || (filedata->file_header.e_machine == EM_TI_C6000
2043 && psym->st_shndx == SHN_TIC6X_SCOMMON))
2044 sec_name = "SCOMMON";
2045 else if (filedata->file_header.e_machine == EM_MIPS
2046 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
2047 sec_name = "SUNDEF";
2048 else if ((filedata->file_header.e_machine == EM_X86_64
2049 || filedata->file_header.e_machine == EM_L1OM
2050 || filedata->file_header.e_machine == EM_K1OM)
2051 && psym->st_shndx == SHN_X86_64_LCOMMON)
2052 sec_name = "LARGE_COMMON";
2053 else if (filedata->file_header.e_machine == EM_IA_64
2054 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
2055 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
2056 sec_name = "ANSI_COM";
2057 else if (is_ia64_vms (filedata)
2058 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
2059 sec_name = "VMS_SYMVEC";
2060 else
2062 sprintf (name_buf, "<section 0x%x>",
2063 (unsigned int) psym->st_shndx);
2064 sec_name = name_buf;
2067 print_symbol (22, sec_name);
2069 else if (strtab == NULL)
2070 printf (_("<string table index: %3ld>"), psym->st_name);
2071 else if (psym->st_name >= strtablen)
2073 error (_("<corrupt string table index: %3ld>\n"),
2074 psym->st_name);
2075 res = false;
2077 else
2079 print_symbol (22, strtab + psym->st_name);
2080 if (version_string)
2081 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2082 version_string);
2085 if (rel_type == reltype_rela)
2087 bfd_vma off = rels[i].r_addend;
2089 if ((bfd_signed_vma) off < 0)
2090 printf (" - %" BFD_VMA_FMT "x", - off);
2091 else
2092 printf (" + %" BFD_VMA_FMT "x", off);
2096 else if (rel_type == reltype_rela)
2098 bfd_vma off = rels[i].r_addend;
2100 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2101 if ((bfd_signed_vma) off < 0)
2102 printf ("-%" BFD_VMA_FMT "x", - off);
2103 else
2104 printf ("%" BFD_VMA_FMT "x", off);
2107 if (filedata->file_header.e_machine == EM_SPARCV9
2108 && rtype != NULL
2109 && streq (rtype, "R_SPARC_OLO10"))
2110 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
2112 putchar ('\n');
2114 #ifdef BFD64
2115 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2117 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
2118 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
2119 const char * rtype2 = elf_mips_reloc_type (type2);
2120 const char * rtype3 = elf_mips_reloc_type (type3);
2122 printf (" Type2: ");
2124 if (rtype2 == NULL)
2125 printf (_("unrecognized: %-7lx"),
2126 (unsigned long) type2 & 0xffffffff);
2127 else
2128 printf ("%-17.17s", rtype2);
2130 printf ("\n Type3: ");
2132 if (rtype3 == NULL)
2133 printf (_("unrecognized: %-7lx"),
2134 (unsigned long) type3 & 0xffffffff);
2135 else
2136 printf ("%-17.17s", rtype3);
2138 putchar ('\n');
2140 #endif /* BFD64 */
2143 free (rels);
2145 return res;
2148 static const char *
2149 get_aarch64_dynamic_type (unsigned long type)
2151 switch (type)
2153 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2154 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2155 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2156 default:
2157 return NULL;
2161 static const char *
2162 get_mips_dynamic_type (unsigned long type)
2164 switch (type)
2166 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2167 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2168 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2169 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2170 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2171 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2172 case DT_MIPS_MSYM: return "MIPS_MSYM";
2173 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2174 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2175 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2176 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2177 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2178 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2179 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2180 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2181 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2182 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2183 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2184 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2185 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2186 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2187 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2188 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2189 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2190 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2191 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2192 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2193 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2194 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2195 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2196 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2197 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2198 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2199 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2200 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2201 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2202 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2203 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2204 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2205 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2206 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2207 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2208 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2209 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2210 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2211 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2212 case DT_MIPS_XHASH: return "MIPS_XHASH";
2213 default:
2214 return NULL;
2218 static const char *
2219 get_sparc64_dynamic_type (unsigned long type)
2221 switch (type)
2223 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2224 default:
2225 return NULL;
2229 static const char *
2230 get_ppc_dynamic_type (unsigned long type)
2232 switch (type)
2234 case DT_PPC_GOT: return "PPC_GOT";
2235 case DT_PPC_OPT: return "PPC_OPT";
2236 default:
2237 return NULL;
2241 static const char *
2242 get_ppc64_dynamic_type (unsigned long type)
2244 switch (type)
2246 case DT_PPC64_GLINK: return "PPC64_GLINK";
2247 case DT_PPC64_OPD: return "PPC64_OPD";
2248 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2249 case DT_PPC64_OPT: return "PPC64_OPT";
2250 default:
2251 return NULL;
2255 static const char *
2256 get_parisc_dynamic_type (unsigned long type)
2258 switch (type)
2260 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2261 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2262 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2263 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2264 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2265 case DT_HP_PREINIT: return "HP_PREINIT";
2266 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2267 case DT_HP_NEEDED: return "HP_NEEDED";
2268 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2269 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2270 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2271 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2272 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2273 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2274 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2275 case DT_HP_FILTERED: return "HP_FILTERED";
2276 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2277 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2278 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2279 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2280 case DT_PLT: return "PLT";
2281 case DT_PLT_SIZE: return "PLT_SIZE";
2282 case DT_DLT: return "DLT";
2283 case DT_DLT_SIZE: return "DLT_SIZE";
2284 default:
2285 return NULL;
2289 static const char *
2290 get_ia64_dynamic_type (unsigned long type)
2292 switch (type)
2294 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2295 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2296 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2297 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2298 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2299 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2300 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2301 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2302 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2303 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2304 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2305 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2306 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2307 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2308 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2309 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2310 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2311 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2312 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2313 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2314 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2315 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2316 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2317 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2318 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2319 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2320 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2321 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2322 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2323 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2324 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2325 default:
2326 return NULL;
2330 static const char *
2331 get_solaris_section_type (unsigned long type)
2333 switch (type)
2335 case 0x6fffffee: return "SUNW_ancillary";
2336 case 0x6fffffef: return "SUNW_capchain";
2337 case 0x6ffffff0: return "SUNW_capinfo";
2338 case 0x6ffffff1: return "SUNW_symsort";
2339 case 0x6ffffff2: return "SUNW_tlssort";
2340 case 0x6ffffff3: return "SUNW_LDYNSYM";
2341 case 0x6ffffff4: return "SUNW_dof";
2342 case 0x6ffffff5: return "SUNW_cap";
2343 case 0x6ffffff6: return "SUNW_SIGNATURE";
2344 case 0x6ffffff7: return "SUNW_ANNOTATE";
2345 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2346 case 0x6ffffff9: return "SUNW_DEBUG";
2347 case 0x6ffffffa: return "SUNW_move";
2348 case 0x6ffffffb: return "SUNW_COMDAT";
2349 case 0x6ffffffc: return "SUNW_syminfo";
2350 case 0x6ffffffd: return "SUNW_verdef";
2351 case 0x6ffffffe: return "SUNW_verneed";
2352 case 0x6fffffff: return "SUNW_versym";
2353 case 0x70000000: return "SPARC_GOTDATA";
2354 default: return NULL;
2358 static const char *
2359 get_alpha_dynamic_type (unsigned long type)
2361 switch (type)
2363 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2364 default: return NULL;
2368 static const char *
2369 get_score_dynamic_type (unsigned long type)
2371 switch (type)
2373 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2374 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2375 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2376 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2377 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2378 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2379 default: return NULL;
2383 static const char *
2384 get_tic6x_dynamic_type (unsigned long type)
2386 switch (type)
2388 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2389 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2390 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2391 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2392 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2393 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2394 default: return NULL;
2398 static const char *
2399 get_nios2_dynamic_type (unsigned long type)
2401 switch (type)
2403 case DT_NIOS2_GP: return "NIOS2_GP";
2404 default: return NULL;
2408 static const char *
2409 get_solaris_dynamic_type (unsigned long type)
2411 switch (type)
2413 case 0x6000000d: return "SUNW_AUXILIARY";
2414 case 0x6000000e: return "SUNW_RTLDINF";
2415 case 0x6000000f: return "SUNW_FILTER";
2416 case 0x60000010: return "SUNW_CAP";
2417 case 0x60000011: return "SUNW_SYMTAB";
2418 case 0x60000012: return "SUNW_SYMSZ";
2419 case 0x60000013: return "SUNW_SORTENT";
2420 case 0x60000014: return "SUNW_SYMSORT";
2421 case 0x60000015: return "SUNW_SYMSORTSZ";
2422 case 0x60000016: return "SUNW_TLSSORT";
2423 case 0x60000017: return "SUNW_TLSSORTSZ";
2424 case 0x60000018: return "SUNW_CAPINFO";
2425 case 0x60000019: return "SUNW_STRPAD";
2426 case 0x6000001a: return "SUNW_CAPCHAIN";
2427 case 0x6000001b: return "SUNW_LDMACH";
2428 case 0x6000001d: return "SUNW_CAPCHAINENT";
2429 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2430 case 0x60000021: return "SUNW_PARENT";
2431 case 0x60000023: return "SUNW_ASLR";
2432 case 0x60000025: return "SUNW_RELAX";
2433 case 0x60000029: return "SUNW_NXHEAP";
2434 case 0x6000002b: return "SUNW_NXSTACK";
2436 case 0x70000001: return "SPARC_REGISTER";
2437 case 0x7ffffffd: return "AUXILIARY";
2438 case 0x7ffffffe: return "USED";
2439 case 0x7fffffff: return "FILTER";
2441 default: return NULL;
2445 static const char *
2446 get_riscv_dynamic_type (unsigned long type)
2448 switch (type)
2450 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2451 default:
2452 return NULL;
2456 static const char *
2457 get_dynamic_type (Filedata * filedata, unsigned long type)
2459 static char buff[64];
2461 switch (type)
2463 case DT_NULL: return "NULL";
2464 case DT_NEEDED: return "NEEDED";
2465 case DT_PLTRELSZ: return "PLTRELSZ";
2466 case DT_PLTGOT: return "PLTGOT";
2467 case DT_HASH: return "HASH";
2468 case DT_STRTAB: return "STRTAB";
2469 case DT_SYMTAB: return "SYMTAB";
2470 case DT_RELA: return "RELA";
2471 case DT_RELASZ: return "RELASZ";
2472 case DT_RELAENT: return "RELAENT";
2473 case DT_STRSZ: return "STRSZ";
2474 case DT_SYMENT: return "SYMENT";
2475 case DT_INIT: return "INIT";
2476 case DT_FINI: return "FINI";
2477 case DT_SONAME: return "SONAME";
2478 case DT_RPATH: return "RPATH";
2479 case DT_SYMBOLIC: return "SYMBOLIC";
2480 case DT_REL: return "REL";
2481 case DT_RELSZ: return "RELSZ";
2482 case DT_RELENT: return "RELENT";
2483 case DT_RELR: return "RELR";
2484 case DT_RELRSZ: return "RELRSZ";
2485 case DT_RELRENT: return "RELRENT";
2486 case DT_PLTREL: return "PLTREL";
2487 case DT_DEBUG: return "DEBUG";
2488 case DT_TEXTREL: return "TEXTREL";
2489 case DT_JMPREL: return "JMPREL";
2490 case DT_BIND_NOW: return "BIND_NOW";
2491 case DT_INIT_ARRAY: return "INIT_ARRAY";
2492 case DT_FINI_ARRAY: return "FINI_ARRAY";
2493 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2494 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2495 case DT_RUNPATH: return "RUNPATH";
2496 case DT_FLAGS: return "FLAGS";
2498 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2499 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2500 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2502 case DT_CHECKSUM: return "CHECKSUM";
2503 case DT_PLTPADSZ: return "PLTPADSZ";
2504 case DT_MOVEENT: return "MOVEENT";
2505 case DT_MOVESZ: return "MOVESZ";
2506 case DT_FEATURE: return "FEATURE";
2507 case DT_POSFLAG_1: return "POSFLAG_1";
2508 case DT_SYMINSZ: return "SYMINSZ";
2509 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2511 case DT_ADDRRNGLO: return "ADDRRNGLO";
2512 case DT_CONFIG: return "CONFIG";
2513 case DT_DEPAUDIT: return "DEPAUDIT";
2514 case DT_AUDIT: return "AUDIT";
2515 case DT_PLTPAD: return "PLTPAD";
2516 case DT_MOVETAB: return "MOVETAB";
2517 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2519 case DT_VERSYM: return "VERSYM";
2521 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2522 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2523 case DT_RELACOUNT: return "RELACOUNT";
2524 case DT_RELCOUNT: return "RELCOUNT";
2525 case DT_FLAGS_1: return "FLAGS_1";
2526 case DT_VERDEF: return "VERDEF";
2527 case DT_VERDEFNUM: return "VERDEFNUM";
2528 case DT_VERNEED: return "VERNEED";
2529 case DT_VERNEEDNUM: return "VERNEEDNUM";
2531 case DT_AUXILIARY: return "AUXILIARY";
2532 case DT_USED: return "USED";
2533 case DT_FILTER: return "FILTER";
2535 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2536 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2537 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2538 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2539 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2540 case DT_GNU_HASH: return "GNU_HASH";
2541 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2543 default:
2544 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2546 const char * result;
2548 switch (filedata->file_header.e_machine)
2550 case EM_AARCH64:
2551 result = get_aarch64_dynamic_type (type);
2552 break;
2553 case EM_MIPS:
2554 case EM_MIPS_RS3_LE:
2555 result = get_mips_dynamic_type (type);
2556 break;
2557 case EM_SPARCV9:
2558 result = get_sparc64_dynamic_type (type);
2559 break;
2560 case EM_PPC:
2561 result = get_ppc_dynamic_type (type);
2562 break;
2563 case EM_PPC64:
2564 result = get_ppc64_dynamic_type (type);
2565 break;
2566 case EM_IA_64:
2567 result = get_ia64_dynamic_type (type);
2568 break;
2569 case EM_ALPHA:
2570 result = get_alpha_dynamic_type (type);
2571 break;
2572 case EM_SCORE:
2573 result = get_score_dynamic_type (type);
2574 break;
2575 case EM_TI_C6000:
2576 result = get_tic6x_dynamic_type (type);
2577 break;
2578 case EM_ALTERA_NIOS2:
2579 result = get_nios2_dynamic_type (type);
2580 break;
2581 case EM_RISCV:
2582 result = get_riscv_dynamic_type (type);
2583 break;
2584 default:
2585 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2586 result = get_solaris_dynamic_type (type);
2587 else
2588 result = NULL;
2589 break;
2592 if (result != NULL)
2593 return result;
2595 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2597 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2598 || (filedata->file_header.e_machine == EM_PARISC
2599 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2601 const char * result;
2603 switch (filedata->file_header.e_machine)
2605 case EM_PARISC:
2606 result = get_parisc_dynamic_type (type);
2607 break;
2608 case EM_IA_64:
2609 result = get_ia64_dynamic_type (type);
2610 break;
2611 default:
2612 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2613 result = get_solaris_dynamic_type (type);
2614 else
2615 result = NULL;
2616 break;
2619 if (result != NULL)
2620 return result;
2622 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2623 type);
2625 else
2626 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2628 return buff;
2632 static bool get_program_headers (Filedata *);
2633 static bool get_dynamic_section (Filedata *);
2635 static void
2636 locate_dynamic_section (Filedata *filedata)
2638 unsigned long dynamic_addr = 0;
2639 bfd_size_type dynamic_size = 0;
2641 if (filedata->file_header.e_phnum != 0
2642 && get_program_headers (filedata))
2644 Elf_Internal_Phdr *segment;
2645 unsigned int i;
2647 for (i = 0, segment = filedata->program_headers;
2648 i < filedata->file_header.e_phnum;
2649 i++, segment++)
2651 if (segment->p_type == PT_DYNAMIC)
2653 dynamic_addr = segment->p_offset;
2654 dynamic_size = segment->p_filesz;
2656 if (filedata->section_headers != NULL)
2658 Elf_Internal_Shdr *sec;
2660 sec = find_section (filedata, ".dynamic");
2661 if (sec != NULL)
2663 if (sec->sh_size == 0
2664 || sec->sh_type == SHT_NOBITS)
2666 dynamic_addr = 0;
2667 dynamic_size = 0;
2669 else
2671 dynamic_addr = sec->sh_offset;
2672 dynamic_size = sec->sh_size;
2677 if (dynamic_addr > filedata->file_size
2678 || (dynamic_size > filedata->file_size - dynamic_addr))
2680 dynamic_addr = 0;
2681 dynamic_size = 0;
2683 break;
2687 filedata->dynamic_addr = dynamic_addr;
2688 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2691 static bool
2692 is_pie (Filedata *filedata)
2694 Elf_Internal_Dyn *entry;
2696 if (filedata->dynamic_size == 0)
2697 locate_dynamic_section (filedata);
2698 if (filedata->dynamic_size <= 1)
2699 return false;
2701 if (!get_dynamic_section (filedata))
2702 return false;
2704 for (entry = filedata->dynamic_section;
2705 entry < filedata->dynamic_section + filedata->dynamic_nent;
2706 entry++)
2708 if (entry->d_tag == DT_FLAGS_1)
2710 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2711 return true;
2712 break;
2715 return false;
2718 static char *
2719 get_file_type (Filedata *filedata)
2721 unsigned e_type = filedata->file_header.e_type;
2722 static char buff[64];
2724 switch (e_type)
2726 case ET_NONE: return _("NONE (None)");
2727 case ET_REL: return _("REL (Relocatable file)");
2728 case ET_EXEC: return _("EXEC (Executable file)");
2729 case ET_DYN:
2730 if (is_pie (filedata))
2731 return _("DYN (Position-Independent Executable file)");
2732 else
2733 return _("DYN (Shared object file)");
2734 case ET_CORE: return _("CORE (Core file)");
2736 default:
2737 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2738 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2739 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2740 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2741 else
2742 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2743 return buff;
2747 static char *
2748 get_machine_name (unsigned e_machine)
2750 static char buff[64]; /* XXX */
2752 switch (e_machine)
2754 /* Please keep this switch table sorted by increasing EM_ value. */
2755 /* 0 */
2756 case EM_NONE: return _("None");
2757 case EM_M32: return "WE32100";
2758 case EM_SPARC: return "Sparc";
2759 case EM_386: return "Intel 80386";
2760 case EM_68K: return "MC68000";
2761 case EM_88K: return "MC88000";
2762 case EM_IAMCU: return "Intel MCU";
2763 case EM_860: return "Intel 80860";
2764 case EM_MIPS: return "MIPS R3000";
2765 case EM_S370: return "IBM System/370";
2766 /* 10 */
2767 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2768 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2769 case EM_PARISC: return "HPPA";
2770 case EM_VPP550: return "Fujitsu VPP500";
2771 case EM_SPARC32PLUS: return "Sparc v8+" ;
2772 case EM_960: return "Intel 80960";
2773 case EM_PPC: return "PowerPC";
2774 /* 20 */
2775 case EM_PPC64: return "PowerPC64";
2776 case EM_S390_OLD:
2777 case EM_S390: return "IBM S/390";
2778 case EM_SPU: return "SPU";
2779 /* 30 */
2780 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2781 case EM_FR20: return "Fujitsu FR20";
2782 case EM_RH32: return "TRW RH32";
2783 case EM_MCORE: return "MCORE";
2784 /* 40 */
2785 case EM_ARM: return "ARM";
2786 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2787 case EM_SH: return "Renesas / SuperH SH";
2788 case EM_SPARCV9: return "Sparc v9";
2789 case EM_TRICORE: return "Siemens Tricore";
2790 case EM_ARC: return "ARC";
2791 case EM_H8_300: return "Renesas H8/300";
2792 case EM_H8_300H: return "Renesas H8/300H";
2793 case EM_H8S: return "Renesas H8S";
2794 case EM_H8_500: return "Renesas H8/500";
2795 /* 50 */
2796 case EM_IA_64: return "Intel IA-64";
2797 case EM_MIPS_X: return "Stanford MIPS-X";
2798 case EM_COLDFIRE: return "Motorola Coldfire";
2799 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2800 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2801 case EM_PCP: return "Siemens PCP";
2802 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2803 case EM_NDR1: return "Denso NDR1 microprocesspr";
2804 case EM_STARCORE: return "Motorola Star*Core processor";
2805 case EM_ME16: return "Toyota ME16 processor";
2806 /* 60 */
2807 case EM_ST100: return "STMicroelectronics ST100 processor";
2808 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2809 case EM_X86_64: return "Advanced Micro Devices X86-64";
2810 case EM_PDSP: return "Sony DSP processor";
2811 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2812 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2813 case EM_FX66: return "Siemens FX66 microcontroller";
2814 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2815 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2816 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2817 /* 70 */
2818 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2819 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2820 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2821 case EM_SVX: return "Silicon Graphics SVx";
2822 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2823 case EM_VAX: return "Digital VAX";
2824 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2825 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2826 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2827 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2828 /* 80 */
2829 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2830 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2831 case EM_PRISM: return "Vitesse Prism";
2832 case EM_AVR_OLD:
2833 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2834 case EM_CYGNUS_FR30:
2835 case EM_FR30: return "Fujitsu FR30";
2836 case EM_CYGNUS_D10V:
2837 case EM_D10V: return "d10v";
2838 case EM_CYGNUS_D30V:
2839 case EM_D30V: return "d30v";
2840 case EM_CYGNUS_V850:
2841 case EM_V850: return "Renesas V850";
2842 case EM_CYGNUS_M32R:
2843 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2844 case EM_CYGNUS_MN10300:
2845 case EM_MN10300: return "mn10300";
2846 /* 90 */
2847 case EM_CYGNUS_MN10200:
2848 case EM_MN10200: return "mn10200";
2849 case EM_PJ: return "picoJava";
2850 case EM_OR1K: return "OpenRISC 1000";
2851 case EM_ARC_COMPACT: return "ARCompact";
2852 case EM_XTENSA_OLD:
2853 case EM_XTENSA: return "Tensilica Xtensa Processor";
2854 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2855 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2856 case EM_NS32K: return "National Semiconductor 32000 series";
2857 case EM_TPC: return "Tenor Network TPC processor";
2858 case EM_SNP1K: return "Trebia SNP 1000 processor";
2859 /* 100 */
2860 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2861 case EM_IP2K_OLD:
2862 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2863 case EM_MAX: return "MAX Processor";
2864 case EM_CR: return "National Semiconductor CompactRISC";
2865 case EM_F2MC16: return "Fujitsu F2MC16";
2866 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2867 case EM_BLACKFIN: return "Analog Devices Blackfin";
2868 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2869 case EM_SEP: return "Sharp embedded microprocessor";
2870 case EM_ARCA: return "Arca RISC microprocessor";
2871 /* 110 */
2872 case EM_UNICORE: return "Unicore";
2873 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2874 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2875 case EM_ALTERA_NIOS2: return "Altera Nios II";
2876 case EM_CRX: return "National Semiconductor CRX microprocessor";
2877 case EM_XGATE: return "Motorola XGATE embedded processor";
2878 case EM_C166:
2879 case EM_XC16X: return "Infineon Technologies xc16x";
2880 case EM_M16C: return "Renesas M16C series microprocessors";
2881 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2882 case EM_CE: return "Freescale Communication Engine RISC core";
2883 /* 120 */
2884 case EM_M32C: return "Renesas M32c";
2885 /* 130 */
2886 case EM_TSK3000: return "Altium TSK3000 core";
2887 case EM_RS08: return "Freescale RS08 embedded processor";
2888 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2889 case EM_SCORE: return "SUNPLUS S+Core";
2890 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2891 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2892 case EM_LATTICEMICO32: return "Lattice Mico32";
2893 case EM_SE_C17: return "Seiko Epson C17 family";
2894 /* 140 */
2895 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2896 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2897 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2898 case EM_TI_PRU: return "TI PRU I/O processor";
2899 /* 160 */
2900 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2901 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2902 case EM_R32C: return "Renesas R32C series microprocessors";
2903 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2904 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2905 case EM_8051: return "Intel 8051 and variants";
2906 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2907 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2908 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2909 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2910 /* 170 */
2911 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2912 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2913 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2914 case EM_RX: return "Renesas RX";
2915 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2916 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2917 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2918 case EM_CR16:
2919 case EM_MICROBLAZE:
2920 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2921 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2922 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2923 /* 180 */
2924 case EM_L1OM: return "Intel L1OM";
2925 case EM_K1OM: return "Intel K1OM";
2926 case EM_INTEL182: return "Intel (reserved)";
2927 case EM_AARCH64: return "AArch64";
2928 case EM_ARM184: return "ARM (reserved)";
2929 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2930 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2931 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2932 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2933 /* 190 */
2934 case EM_CUDA: return "NVIDIA CUDA architecture";
2935 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2936 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2937 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2938 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2939 case EM_ARC_COMPACT2: return "ARCv2";
2940 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2941 case EM_RL78: return "Renesas RL78";
2942 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2943 case EM_78K0R: return "Renesas 78K0R";
2944 /* 200 */
2945 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2946 case EM_BA1: return "Beyond BA1 CPU architecture";
2947 case EM_BA2: return "Beyond BA2 CPU architecture";
2948 case EM_XCORE: return "XMOS xCORE processor family";
2949 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2950 case EM_INTELGT: return "Intel Graphics Technology";
2951 /* 210 */
2952 case EM_KM32: return "KM211 KM32 32-bit processor";
2953 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2954 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2955 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2956 case EM_KVARC: return "KM211 KVARC processor";
2957 case EM_CDP: return "Paneve CDP architecture family";
2958 case EM_COGE: return "Cognitive Smart Memory Processor";
2959 case EM_COOL: return "Bluechip Systems CoolEngine";
2960 case EM_NORC: return "Nanoradio Optimized RISC";
2961 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2962 /* 220 */
2963 case EM_Z80: return "Zilog Z80";
2964 case EM_VISIUM: return "CDS VISIUMcore processor";
2965 case EM_FT32: return "FTDI Chip FT32";
2966 case EM_MOXIE: return "Moxie";
2967 case EM_AMDGPU: return "AMD GPU";
2968 /* 230 (all reserved) */
2969 /* 240 */
2970 case EM_RISCV: return "RISC-V";
2971 case EM_LANAI: return "Lanai 32-bit processor";
2972 case EM_CEVA: return "CEVA Processor Architecture Family";
2973 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2974 case EM_BPF: return "Linux BPF";
2975 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2976 case EM_IMG1: return "Imagination Technologies";
2977 /* 250 */
2978 case EM_NFP: return "Netronome Flow Processor";
2979 case EM_VE: return "NEC Vector Engine";
2980 case EM_CSKY: return "C-SKY";
2981 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2982 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2983 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2984 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2985 case EM_65816: return "WDC 65816/65C816";
2986 case EM_LOONGARCH: return "LoongArch";
2987 case EM_KF32: return "ChipON KungFu32";
2989 /* Large numbers... */
2990 case EM_MT: return "Morpho Techologies MT processor";
2991 case EM_ALPHA: return "Alpha";
2992 case EM_WEBASSEMBLY: return "Web Assembly";
2993 case EM_DLX: return "OpenDLX";
2994 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2995 case EM_IQ2000: return "Vitesse IQ2000";
2996 case EM_M32C_OLD:
2997 case EM_NIOS32: return "Altera Nios";
2998 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2999 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
3000 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
3001 case EM_S12Z: return "Freescale S12Z";
3003 default:
3004 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
3005 return buff;
3009 static void
3010 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
3012 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
3013 other compilers don't specify an architecture type in the e_flags, and
3014 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3015 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3016 architectures.
3018 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3019 but also sets a specific architecture type in the e_flags field.
3021 However, when decoding the flags we don't worry if we see an
3022 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3023 ARCEM architecture type. */
3025 switch (e_flags & EF_ARC_MACH_MSK)
3027 /* We only expect these to occur for EM_ARC_COMPACT2. */
3028 case EF_ARC_CPU_ARCV2EM:
3029 strcat (buf, ", ARC EM");
3030 break;
3031 case EF_ARC_CPU_ARCV2HS:
3032 strcat (buf, ", ARC HS");
3033 break;
3035 /* We only expect these to occur for EM_ARC_COMPACT. */
3036 case E_ARC_MACH_ARC600:
3037 strcat (buf, ", ARC600");
3038 break;
3039 case E_ARC_MACH_ARC601:
3040 strcat (buf, ", ARC601");
3041 break;
3042 case E_ARC_MACH_ARC700:
3043 strcat (buf, ", ARC700");
3044 break;
3046 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3047 new ELF with new architecture being read by an old version of
3048 readelf, or (c) An ELF built with non-GNU compiler that does not
3049 set the architecture in the e_flags. */
3050 default:
3051 if (e_machine == EM_ARC_COMPACT)
3052 strcat (buf, ", Unknown ARCompact");
3053 else
3054 strcat (buf, ", Unknown ARC");
3055 break;
3058 switch (e_flags & EF_ARC_OSABI_MSK)
3060 case E_ARC_OSABI_ORIG:
3061 strcat (buf, ", (ABI:legacy)");
3062 break;
3063 case E_ARC_OSABI_V2:
3064 strcat (buf, ", (ABI:v2)");
3065 break;
3066 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3067 case E_ARC_OSABI_V3:
3068 strcat (buf, ", v3 no-legacy-syscalls ABI");
3069 break;
3070 case E_ARC_OSABI_V4:
3071 strcat (buf, ", v4 ABI");
3072 break;
3073 default:
3074 strcat (buf, ", unrecognised ARC OSABI flag");
3075 break;
3079 static void
3080 decode_ARM_machine_flags (unsigned e_flags, char buf[])
3082 unsigned eabi;
3083 bool unknown = false;
3085 eabi = EF_ARM_EABI_VERSION (e_flags);
3086 e_flags &= ~ EF_ARM_EABIMASK;
3088 /* Handle "generic" ARM flags. */
3089 if (e_flags & EF_ARM_RELEXEC)
3091 strcat (buf, ", relocatable executable");
3092 e_flags &= ~ EF_ARM_RELEXEC;
3095 if (e_flags & EF_ARM_PIC)
3097 strcat (buf, ", position independent");
3098 e_flags &= ~ EF_ARM_PIC;
3101 /* Now handle EABI specific flags. */
3102 switch (eabi)
3104 default:
3105 strcat (buf, ", <unrecognized EABI>");
3106 if (e_flags)
3107 unknown = true;
3108 break;
3110 case EF_ARM_EABI_VER1:
3111 strcat (buf, ", Version1 EABI");
3112 while (e_flags)
3114 unsigned flag;
3116 /* Process flags one bit at a time. */
3117 flag = e_flags & - e_flags;
3118 e_flags &= ~ flag;
3120 switch (flag)
3122 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3123 strcat (buf, ", sorted symbol tables");
3124 break;
3126 default:
3127 unknown = true;
3128 break;
3131 break;
3133 case EF_ARM_EABI_VER2:
3134 strcat (buf, ", Version2 EABI");
3135 while (e_flags)
3137 unsigned flag;
3139 /* Process flags one bit at a time. */
3140 flag = e_flags & - e_flags;
3141 e_flags &= ~ flag;
3143 switch (flag)
3145 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3146 strcat (buf, ", sorted symbol tables");
3147 break;
3149 case EF_ARM_DYNSYMSUSESEGIDX:
3150 strcat (buf, ", dynamic symbols use segment index");
3151 break;
3153 case EF_ARM_MAPSYMSFIRST:
3154 strcat (buf, ", mapping symbols precede others");
3155 break;
3157 default:
3158 unknown = true;
3159 break;
3162 break;
3164 case EF_ARM_EABI_VER3:
3165 strcat (buf, ", Version3 EABI");
3166 break;
3168 case EF_ARM_EABI_VER4:
3169 strcat (buf, ", Version4 EABI");
3170 while (e_flags)
3172 unsigned flag;
3174 /* Process flags one bit at a time. */
3175 flag = e_flags & - e_flags;
3176 e_flags &= ~ flag;
3178 switch (flag)
3180 case EF_ARM_BE8:
3181 strcat (buf, ", BE8");
3182 break;
3184 case EF_ARM_LE8:
3185 strcat (buf, ", LE8");
3186 break;
3188 default:
3189 unknown = true;
3190 break;
3193 break;
3195 case EF_ARM_EABI_VER5:
3196 strcat (buf, ", Version5 EABI");
3197 while (e_flags)
3199 unsigned flag;
3201 /* Process flags one bit at a time. */
3202 flag = e_flags & - e_flags;
3203 e_flags &= ~ flag;
3205 switch (flag)
3207 case EF_ARM_BE8:
3208 strcat (buf, ", BE8");
3209 break;
3211 case EF_ARM_LE8:
3212 strcat (buf, ", LE8");
3213 break;
3215 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3216 strcat (buf, ", soft-float ABI");
3217 break;
3219 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3220 strcat (buf, ", hard-float ABI");
3221 break;
3223 default:
3224 unknown = true;
3225 break;
3228 break;
3230 case EF_ARM_EABI_UNKNOWN:
3231 strcat (buf, ", GNU EABI");
3232 while (e_flags)
3234 unsigned flag;
3236 /* Process flags one bit at a time. */
3237 flag = e_flags & - e_flags;
3238 e_flags &= ~ flag;
3240 switch (flag)
3242 case EF_ARM_INTERWORK:
3243 strcat (buf, ", interworking enabled");
3244 break;
3246 case EF_ARM_APCS_26:
3247 strcat (buf, ", uses APCS/26");
3248 break;
3250 case EF_ARM_APCS_FLOAT:
3251 strcat (buf, ", uses APCS/float");
3252 break;
3254 case EF_ARM_PIC:
3255 strcat (buf, ", position independent");
3256 break;
3258 case EF_ARM_ALIGN8:
3259 strcat (buf, ", 8 bit structure alignment");
3260 break;
3262 case EF_ARM_NEW_ABI:
3263 strcat (buf, ", uses new ABI");
3264 break;
3266 case EF_ARM_OLD_ABI:
3267 strcat (buf, ", uses old ABI");
3268 break;
3270 case EF_ARM_SOFT_FLOAT:
3271 strcat (buf, ", software FP");
3272 break;
3274 case EF_ARM_VFP_FLOAT:
3275 strcat (buf, ", VFP");
3276 break;
3278 case EF_ARM_MAVERICK_FLOAT:
3279 strcat (buf, ", Maverick FP");
3280 break;
3282 default:
3283 unknown = true;
3284 break;
3289 if (unknown)
3290 strcat (buf,_(", <unknown>"));
3293 static void
3294 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3296 --size; /* Leave space for null terminator. */
3298 switch (e_flags & EF_AVR_MACH)
3300 case E_AVR_MACH_AVR1:
3301 strncat (buf, ", avr:1", size);
3302 break;
3303 case E_AVR_MACH_AVR2:
3304 strncat (buf, ", avr:2", size);
3305 break;
3306 case E_AVR_MACH_AVR25:
3307 strncat (buf, ", avr:25", size);
3308 break;
3309 case E_AVR_MACH_AVR3:
3310 strncat (buf, ", avr:3", size);
3311 break;
3312 case E_AVR_MACH_AVR31:
3313 strncat (buf, ", avr:31", size);
3314 break;
3315 case E_AVR_MACH_AVR35:
3316 strncat (buf, ", avr:35", size);
3317 break;
3318 case E_AVR_MACH_AVR4:
3319 strncat (buf, ", avr:4", size);
3320 break;
3321 case E_AVR_MACH_AVR5:
3322 strncat (buf, ", avr:5", size);
3323 break;
3324 case E_AVR_MACH_AVR51:
3325 strncat (buf, ", avr:51", size);
3326 break;
3327 case E_AVR_MACH_AVR6:
3328 strncat (buf, ", avr:6", size);
3329 break;
3330 case E_AVR_MACH_AVRTINY:
3331 strncat (buf, ", avr:100", size);
3332 break;
3333 case E_AVR_MACH_XMEGA1:
3334 strncat (buf, ", avr:101", size);
3335 break;
3336 case E_AVR_MACH_XMEGA2:
3337 strncat (buf, ", avr:102", size);
3338 break;
3339 case E_AVR_MACH_XMEGA3:
3340 strncat (buf, ", avr:103", size);
3341 break;
3342 case E_AVR_MACH_XMEGA4:
3343 strncat (buf, ", avr:104", size);
3344 break;
3345 case E_AVR_MACH_XMEGA5:
3346 strncat (buf, ", avr:105", size);
3347 break;
3348 case E_AVR_MACH_XMEGA6:
3349 strncat (buf, ", avr:106", size);
3350 break;
3351 case E_AVR_MACH_XMEGA7:
3352 strncat (buf, ", avr:107", size);
3353 break;
3354 default:
3355 strncat (buf, ", avr:<unknown>", size);
3356 break;
3359 size -= strlen (buf);
3360 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3361 strncat (buf, ", link-relax", size);
3364 static void
3365 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3367 unsigned abi;
3368 unsigned arch;
3369 unsigned config;
3370 unsigned version;
3371 bool has_fpu = false;
3372 unsigned int r = 0;
3374 static const char *ABI_STRINGS[] =
3376 "ABI v0", /* use r5 as return register; only used in N1213HC */
3377 "ABI v1", /* use r0 as return register */
3378 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3379 "ABI v2fp", /* for FPU */
3380 "AABI",
3381 "ABI2 FP+"
3383 static const char *VER_STRINGS[] =
3385 "Andes ELF V1.3 or older",
3386 "Andes ELF V1.3.1",
3387 "Andes ELF V1.4"
3389 static const char *ARCH_STRINGS[] =
3392 "Andes Star v1.0",
3393 "Andes Star v2.0",
3394 "Andes Star v3.0",
3395 "Andes Star v3.0m"
3398 abi = EF_NDS_ABI & e_flags;
3399 arch = EF_NDS_ARCH & e_flags;
3400 config = EF_NDS_INST & e_flags;
3401 version = EF_NDS32_ELF_VERSION & e_flags;
3403 memset (buf, 0, size);
3405 switch (abi)
3407 case E_NDS_ABI_V0:
3408 case E_NDS_ABI_V1:
3409 case E_NDS_ABI_V2:
3410 case E_NDS_ABI_V2FP:
3411 case E_NDS_ABI_AABI:
3412 case E_NDS_ABI_V2FP_PLUS:
3413 /* In case there are holes in the array. */
3414 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3415 break;
3417 default:
3418 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3419 break;
3422 switch (version)
3424 case E_NDS32_ELF_VER_1_2:
3425 case E_NDS32_ELF_VER_1_3:
3426 case E_NDS32_ELF_VER_1_4:
3427 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3428 break;
3430 default:
3431 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3432 break;
3435 if (E_NDS_ABI_V0 == abi)
3437 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3438 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3439 if (arch == E_NDS_ARCH_STAR_V1_0)
3440 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3441 return;
3444 switch (arch)
3446 case E_NDS_ARCH_STAR_V1_0:
3447 case E_NDS_ARCH_STAR_V2_0:
3448 case E_NDS_ARCH_STAR_V3_0:
3449 case E_NDS_ARCH_STAR_V3_M:
3450 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3451 break;
3453 default:
3454 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3455 /* ARCH version determines how the e_flags are interpreted.
3456 If it is unknown, we cannot proceed. */
3457 return;
3460 /* Newer ABI; Now handle architecture specific flags. */
3461 if (arch == E_NDS_ARCH_STAR_V1_0)
3463 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3464 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3466 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3467 r += snprintf (buf + r, size -r, ", MAC");
3469 if (config & E_NDS32_HAS_DIV_INST)
3470 r += snprintf (buf + r, size -r, ", DIV");
3472 if (config & E_NDS32_HAS_16BIT_INST)
3473 r += snprintf (buf + r, size -r, ", 16b");
3475 else
3477 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3479 if (version <= E_NDS32_ELF_VER_1_3)
3480 r += snprintf (buf + r, size -r, ", [B8]");
3481 else
3482 r += snprintf (buf + r, size -r, ", EX9");
3485 if (config & E_NDS32_HAS_MAC_DX_INST)
3486 r += snprintf (buf + r, size -r, ", MAC_DX");
3488 if (config & E_NDS32_HAS_DIV_DX_INST)
3489 r += snprintf (buf + r, size -r, ", DIV_DX");
3491 if (config & E_NDS32_HAS_16BIT_INST)
3493 if (version <= E_NDS32_ELF_VER_1_3)
3494 r += snprintf (buf + r, size -r, ", 16b");
3495 else
3496 r += snprintf (buf + r, size -r, ", IFC");
3500 if (config & E_NDS32_HAS_EXT_INST)
3501 r += snprintf (buf + r, size -r, ", PERF1");
3503 if (config & E_NDS32_HAS_EXT2_INST)
3504 r += snprintf (buf + r, size -r, ", PERF2");
3506 if (config & E_NDS32_HAS_FPU_INST)
3508 has_fpu = true;
3509 r += snprintf (buf + r, size -r, ", FPU_SP");
3512 if (config & E_NDS32_HAS_FPU_DP_INST)
3514 has_fpu = true;
3515 r += snprintf (buf + r, size -r, ", FPU_DP");
3518 if (config & E_NDS32_HAS_FPU_MAC_INST)
3520 has_fpu = true;
3521 r += snprintf (buf + r, size -r, ", FPU_MAC");
3524 if (has_fpu)
3526 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3528 case E_NDS32_FPU_REG_8SP_4DP:
3529 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3530 break;
3531 case E_NDS32_FPU_REG_16SP_8DP:
3532 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3533 break;
3534 case E_NDS32_FPU_REG_32SP_16DP:
3535 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3536 break;
3537 case E_NDS32_FPU_REG_32SP_32DP:
3538 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3539 break;
3543 if (config & E_NDS32_HAS_AUDIO_INST)
3544 r += snprintf (buf + r, size -r, ", AUDIO");
3546 if (config & E_NDS32_HAS_STRING_INST)
3547 r += snprintf (buf + r, size -r, ", STR");
3549 if (config & E_NDS32_HAS_REDUCED_REGS)
3550 r += snprintf (buf + r, size -r, ", 16REG");
3552 if (config & E_NDS32_HAS_VIDEO_INST)
3554 if (version <= E_NDS32_ELF_VER_1_3)
3555 r += snprintf (buf + r, size -r, ", VIDEO");
3556 else
3557 r += snprintf (buf + r, size -r, ", SATURATION");
3560 if (config & E_NDS32_HAS_ENCRIPT_INST)
3561 r += snprintf (buf + r, size -r, ", ENCRP");
3563 if (config & E_NDS32_HAS_L2C_INST)
3564 r += snprintf (buf + r, size -r, ", L2C");
3567 static char *
3568 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3570 static char buf[1024];
3572 buf[0] = '\0';
3574 if (e_flags)
3576 switch (e_machine)
3578 default:
3579 break;
3581 case EM_ARC_COMPACT2:
3582 case EM_ARC_COMPACT:
3583 decode_ARC_machine_flags (e_flags, e_machine, buf);
3584 break;
3586 case EM_ARM:
3587 decode_ARM_machine_flags (e_flags, buf);
3588 break;
3590 case EM_AVR:
3591 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3592 break;
3594 case EM_BLACKFIN:
3595 if (e_flags & EF_BFIN_PIC)
3596 strcat (buf, ", PIC");
3598 if (e_flags & EF_BFIN_FDPIC)
3599 strcat (buf, ", FDPIC");
3601 if (e_flags & EF_BFIN_CODE_IN_L1)
3602 strcat (buf, ", code in L1");
3604 if (e_flags & EF_BFIN_DATA_IN_L1)
3605 strcat (buf, ", data in L1");
3607 break;
3609 case EM_CYGNUS_FRV:
3610 switch (e_flags & EF_FRV_CPU_MASK)
3612 case EF_FRV_CPU_GENERIC:
3613 break;
3615 default:
3616 strcat (buf, ", fr???");
3617 break;
3619 case EF_FRV_CPU_FR300:
3620 strcat (buf, ", fr300");
3621 break;
3623 case EF_FRV_CPU_FR400:
3624 strcat (buf, ", fr400");
3625 break;
3626 case EF_FRV_CPU_FR405:
3627 strcat (buf, ", fr405");
3628 break;
3630 case EF_FRV_CPU_FR450:
3631 strcat (buf, ", fr450");
3632 break;
3634 case EF_FRV_CPU_FR500:
3635 strcat (buf, ", fr500");
3636 break;
3637 case EF_FRV_CPU_FR550:
3638 strcat (buf, ", fr550");
3639 break;
3641 case EF_FRV_CPU_SIMPLE:
3642 strcat (buf, ", simple");
3643 break;
3644 case EF_FRV_CPU_TOMCAT:
3645 strcat (buf, ", tomcat");
3646 break;
3648 break;
3650 case EM_68K:
3651 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3652 strcat (buf, ", m68000");
3653 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3654 strcat (buf, ", cpu32");
3655 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3656 strcat (buf, ", fido_a");
3657 else
3659 char const * isa = _("unknown");
3660 char const * mac = _("unknown mac");
3661 char const * additional = NULL;
3663 switch (e_flags & EF_M68K_CF_ISA_MASK)
3665 case EF_M68K_CF_ISA_A_NODIV:
3666 isa = "A";
3667 additional = ", nodiv";
3668 break;
3669 case EF_M68K_CF_ISA_A:
3670 isa = "A";
3671 break;
3672 case EF_M68K_CF_ISA_A_PLUS:
3673 isa = "A+";
3674 break;
3675 case EF_M68K_CF_ISA_B_NOUSP:
3676 isa = "B";
3677 additional = ", nousp";
3678 break;
3679 case EF_M68K_CF_ISA_B:
3680 isa = "B";
3681 break;
3682 case EF_M68K_CF_ISA_C:
3683 isa = "C";
3684 break;
3685 case EF_M68K_CF_ISA_C_NODIV:
3686 isa = "C";
3687 additional = ", nodiv";
3688 break;
3690 strcat (buf, ", cf, isa ");
3691 strcat (buf, isa);
3692 if (additional)
3693 strcat (buf, additional);
3694 if (e_flags & EF_M68K_CF_FLOAT)
3695 strcat (buf, ", float");
3696 switch (e_flags & EF_M68K_CF_MAC_MASK)
3698 case 0:
3699 mac = NULL;
3700 break;
3701 case EF_M68K_CF_MAC:
3702 mac = "mac";
3703 break;
3704 case EF_M68K_CF_EMAC:
3705 mac = "emac";
3706 break;
3707 case EF_M68K_CF_EMAC_B:
3708 mac = "emac_b";
3709 break;
3711 if (mac)
3713 strcat (buf, ", ");
3714 strcat (buf, mac);
3717 break;
3719 case EM_CYGNUS_MEP:
3720 switch (e_flags & EF_MEP_CPU_MASK)
3722 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3723 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3724 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3725 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3726 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3727 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3728 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3731 switch (e_flags & EF_MEP_COP_MASK)
3733 case EF_MEP_COP_NONE: break;
3734 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3735 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3736 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3737 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3738 default: strcat (buf, _("<unknown MeP copro type>")); break;
3741 if (e_flags & EF_MEP_LIBRARY)
3742 strcat (buf, ", Built for Library");
3744 if (e_flags & EF_MEP_INDEX_MASK)
3745 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3746 e_flags & EF_MEP_INDEX_MASK);
3748 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3749 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3750 e_flags & ~ EF_MEP_ALL_FLAGS);
3751 break;
3753 case EM_PPC:
3754 if (e_flags & EF_PPC_EMB)
3755 strcat (buf, ", emb");
3757 if (e_flags & EF_PPC_RELOCATABLE)
3758 strcat (buf, _(", relocatable"));
3760 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3761 strcat (buf, _(", relocatable-lib"));
3762 break;
3764 case EM_PPC64:
3765 if (e_flags & EF_PPC64_ABI)
3767 char abi[] = ", abiv0";
3769 abi[6] += e_flags & EF_PPC64_ABI;
3770 strcat (buf, abi);
3772 break;
3774 case EM_V800:
3775 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3776 strcat (buf, ", RH850 ABI");
3778 if (e_flags & EF_V800_850E3)
3779 strcat (buf, ", V3 architecture");
3781 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3782 strcat (buf, ", FPU not used");
3784 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3785 strcat (buf, ", regmode: COMMON");
3787 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3788 strcat (buf, ", r4 not used");
3790 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3791 strcat (buf, ", r30 not used");
3793 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3794 strcat (buf, ", r5 not used");
3796 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3797 strcat (buf, ", r2 not used");
3799 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3801 switch (e_flags & - e_flags)
3803 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3804 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3805 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3806 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3807 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3808 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3809 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3810 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3811 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3812 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3813 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3814 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3815 default: break;
3818 break;
3820 case EM_V850:
3821 case EM_CYGNUS_V850:
3822 switch (e_flags & EF_V850_ARCH)
3824 case E_V850E3V5_ARCH:
3825 strcat (buf, ", v850e3v5");
3826 break;
3827 case E_V850E2V3_ARCH:
3828 strcat (buf, ", v850e2v3");
3829 break;
3830 case E_V850E2_ARCH:
3831 strcat (buf, ", v850e2");
3832 break;
3833 case E_V850E1_ARCH:
3834 strcat (buf, ", v850e1");
3835 break;
3836 case E_V850E_ARCH:
3837 strcat (buf, ", v850e");
3838 break;
3839 case E_V850_ARCH:
3840 strcat (buf, ", v850");
3841 break;
3842 default:
3843 strcat (buf, _(", unknown v850 architecture variant"));
3844 break;
3846 break;
3848 case EM_M32R:
3849 case EM_CYGNUS_M32R:
3850 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3851 strcat (buf, ", m32r");
3852 break;
3854 case EM_MIPS:
3855 case EM_MIPS_RS3_LE:
3856 if (e_flags & EF_MIPS_NOREORDER)
3857 strcat (buf, ", noreorder");
3859 if (e_flags & EF_MIPS_PIC)
3860 strcat (buf, ", pic");
3862 if (e_flags & EF_MIPS_CPIC)
3863 strcat (buf, ", cpic");
3865 if (e_flags & EF_MIPS_UCODE)
3866 strcat (buf, ", ugen_reserved");
3868 if (e_flags & EF_MIPS_ABI2)
3869 strcat (buf, ", abi2");
3871 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3872 strcat (buf, ", odk first");
3874 if (e_flags & EF_MIPS_32BITMODE)
3875 strcat (buf, ", 32bitmode");
3877 if (e_flags & EF_MIPS_NAN2008)
3878 strcat (buf, ", nan2008");
3880 if (e_flags & EF_MIPS_FP64)
3881 strcat (buf, ", fp64");
3883 switch ((e_flags & EF_MIPS_MACH))
3885 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3886 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3887 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3888 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3889 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3890 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3891 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3892 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3893 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3894 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
3895 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3896 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3897 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3898 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
3899 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
3900 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
3901 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3902 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3903 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3904 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
3905 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
3906 case 0:
3907 /* We simply ignore the field in this case to avoid confusion:
3908 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3909 extension. */
3910 break;
3911 default: strcat (buf, _(", unknown CPU")); break;
3914 switch ((e_flags & EF_MIPS_ABI))
3916 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3917 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3918 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3919 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3920 case 0:
3921 /* We simply ignore the field in this case to avoid confusion:
3922 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3923 This means it is likely to be an o32 file, but not for
3924 sure. */
3925 break;
3926 default: strcat (buf, _(", unknown ABI")); break;
3929 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3930 strcat (buf, ", mdmx");
3932 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3933 strcat (buf, ", mips16");
3935 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3936 strcat (buf, ", micromips");
3938 switch ((e_flags & EF_MIPS_ARCH))
3940 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3941 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3942 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3943 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3944 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3945 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3946 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3947 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3948 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3949 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3950 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3951 default: strcat (buf, _(", unknown ISA")); break;
3953 break;
3955 case EM_NDS32:
3956 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3957 break;
3959 case EM_NFP:
3960 switch (EF_NFP_MACH (e_flags))
3962 case E_NFP_MACH_3200:
3963 strcat (buf, ", NFP-32xx");
3964 break;
3965 case E_NFP_MACH_6000:
3966 strcat (buf, ", NFP-6xxx");
3967 break;
3969 break;
3971 case EM_RISCV:
3972 if (e_flags & EF_RISCV_RVC)
3973 strcat (buf, ", RVC");
3975 if (e_flags & EF_RISCV_RVE)
3976 strcat (buf, ", RVE");
3978 switch (e_flags & EF_RISCV_FLOAT_ABI)
3980 case EF_RISCV_FLOAT_ABI_SOFT:
3981 strcat (buf, ", soft-float ABI");
3982 break;
3984 case EF_RISCV_FLOAT_ABI_SINGLE:
3985 strcat (buf, ", single-float ABI");
3986 break;
3988 case EF_RISCV_FLOAT_ABI_DOUBLE:
3989 strcat (buf, ", double-float ABI");
3990 break;
3992 case EF_RISCV_FLOAT_ABI_QUAD:
3993 strcat (buf, ", quad-float ABI");
3994 break;
3996 break;
3998 case EM_SH:
3999 switch ((e_flags & EF_SH_MACH_MASK))
4001 case EF_SH1: strcat (buf, ", sh1"); break;
4002 case EF_SH2: strcat (buf, ", sh2"); break;
4003 case EF_SH3: strcat (buf, ", sh3"); break;
4004 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
4005 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
4006 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
4007 case EF_SH3E: strcat (buf, ", sh3e"); break;
4008 case EF_SH4: strcat (buf, ", sh4"); break;
4009 case EF_SH5: strcat (buf, ", sh5"); break;
4010 case EF_SH2E: strcat (buf, ", sh2e"); break;
4011 case EF_SH4A: strcat (buf, ", sh4a"); break;
4012 case EF_SH2A: strcat (buf, ", sh2a"); break;
4013 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
4014 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
4015 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
4016 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
4017 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
4018 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
4019 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
4020 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
4021 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
4022 default: strcat (buf, _(", unknown ISA")); break;
4025 if (e_flags & EF_SH_PIC)
4026 strcat (buf, ", pic");
4028 if (e_flags & EF_SH_FDPIC)
4029 strcat (buf, ", fdpic");
4030 break;
4032 case EM_OR1K:
4033 if (e_flags & EF_OR1K_NODELAY)
4034 strcat (buf, ", no delay");
4035 break;
4037 case EM_SPARCV9:
4038 if (e_flags & EF_SPARC_32PLUS)
4039 strcat (buf, ", v8+");
4041 if (e_flags & EF_SPARC_SUN_US1)
4042 strcat (buf, ", ultrasparcI");
4044 if (e_flags & EF_SPARC_SUN_US3)
4045 strcat (buf, ", ultrasparcIII");
4047 if (e_flags & EF_SPARC_HAL_R1)
4048 strcat (buf, ", halr1");
4050 if (e_flags & EF_SPARC_LEDATA)
4051 strcat (buf, ", ledata");
4053 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4054 strcat (buf, ", tso");
4056 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4057 strcat (buf, ", pso");
4059 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4060 strcat (buf, ", rmo");
4061 break;
4063 case EM_PARISC:
4064 switch (e_flags & EF_PARISC_ARCH)
4066 case EFA_PARISC_1_0:
4067 strcpy (buf, ", PA-RISC 1.0");
4068 break;
4069 case EFA_PARISC_1_1:
4070 strcpy (buf, ", PA-RISC 1.1");
4071 break;
4072 case EFA_PARISC_2_0:
4073 strcpy (buf, ", PA-RISC 2.0");
4074 break;
4075 default:
4076 break;
4078 if (e_flags & EF_PARISC_TRAPNIL)
4079 strcat (buf, ", trapnil");
4080 if (e_flags & EF_PARISC_EXT)
4081 strcat (buf, ", ext");
4082 if (e_flags & EF_PARISC_LSB)
4083 strcat (buf, ", lsb");
4084 if (e_flags & EF_PARISC_WIDE)
4085 strcat (buf, ", wide");
4086 if (e_flags & EF_PARISC_NO_KABP)
4087 strcat (buf, ", no kabp");
4088 if (e_flags & EF_PARISC_LAZYSWAP)
4089 strcat (buf, ", lazyswap");
4090 break;
4092 case EM_PJ:
4093 case EM_PJ_OLD:
4094 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4095 strcat (buf, ", new calling convention");
4097 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4098 strcat (buf, ", gnu calling convention");
4099 break;
4101 case EM_IA_64:
4102 if ((e_flags & EF_IA_64_ABI64))
4103 strcat (buf, ", 64-bit");
4104 else
4105 strcat (buf, ", 32-bit");
4106 if ((e_flags & EF_IA_64_REDUCEDFP))
4107 strcat (buf, ", reduced fp model");
4108 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4109 strcat (buf, ", no function descriptors, constant gp");
4110 else if ((e_flags & EF_IA_64_CONS_GP))
4111 strcat (buf, ", constant gp");
4112 if ((e_flags & EF_IA_64_ABSOLUTE))
4113 strcat (buf, ", absolute");
4114 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4116 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4117 strcat (buf, ", vms_linkages");
4118 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4120 case EF_IA_64_VMS_COMCOD_SUCCESS:
4121 break;
4122 case EF_IA_64_VMS_COMCOD_WARNING:
4123 strcat (buf, ", warning");
4124 break;
4125 case EF_IA_64_VMS_COMCOD_ERROR:
4126 strcat (buf, ", error");
4127 break;
4128 case EF_IA_64_VMS_COMCOD_ABORT:
4129 strcat (buf, ", abort");
4130 break;
4131 default:
4132 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4133 e_flags & EF_IA_64_VMS_COMCOD);
4134 strcat (buf, ", <unknown>");
4137 break;
4139 case EM_VAX:
4140 if ((e_flags & EF_VAX_NONPIC))
4141 strcat (buf, ", non-PIC");
4142 if ((e_flags & EF_VAX_DFLOAT))
4143 strcat (buf, ", D-Float");
4144 if ((e_flags & EF_VAX_GFLOAT))
4145 strcat (buf, ", G-Float");
4146 break;
4148 case EM_VISIUM:
4149 if (e_flags & EF_VISIUM_ARCH_MCM)
4150 strcat (buf, ", mcm");
4151 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4152 strcat (buf, ", mcm24");
4153 if (e_flags & EF_VISIUM_ARCH_GR6)
4154 strcat (buf, ", gr6");
4155 break;
4157 case EM_RL78:
4158 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4160 case E_FLAG_RL78_ANY_CPU: break;
4161 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
4162 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
4163 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
4165 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4166 strcat (buf, ", 64-bit doubles");
4167 break;
4169 case EM_RX:
4170 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4171 strcat (buf, ", 64-bit doubles");
4172 if (e_flags & E_FLAG_RX_DSP)
4173 strcat (buf, ", dsp");
4174 if (e_flags & E_FLAG_RX_PID)
4175 strcat (buf, ", pid");
4176 if (e_flags & E_FLAG_RX_ABI)
4177 strcat (buf, ", RX ABI");
4178 if (e_flags & E_FLAG_RX_SINSNS_SET)
4179 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
4180 ? ", uses String instructions" : ", bans String instructions");
4181 if (e_flags & E_FLAG_RX_V2)
4182 strcat (buf, ", V2");
4183 if (e_flags & E_FLAG_RX_V3)
4184 strcat (buf, ", V3");
4185 break;
4187 case EM_S390:
4188 if (e_flags & EF_S390_HIGH_GPRS)
4189 strcat (buf, ", highgprs");
4190 break;
4192 case EM_TI_C6000:
4193 if ((e_flags & EF_C6000_REL))
4194 strcat (buf, ", relocatable module");
4195 break;
4197 case EM_MSP430:
4198 strcat (buf, _(": architecture variant: "));
4199 switch (e_flags & EF_MSP430_MACH)
4201 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
4202 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
4203 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
4204 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
4205 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
4206 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
4207 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
4208 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
4209 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
4210 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
4211 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
4212 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
4213 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
4214 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
4215 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
4216 default:
4217 strcat (buf, _(": unknown")); break;
4220 if (e_flags & ~ EF_MSP430_MACH)
4221 strcat (buf, _(": unknown extra flag bits also present"));
4222 break;
4224 case EM_Z80:
4225 switch (e_flags & EF_Z80_MACH_MSK)
4227 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
4228 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
4229 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
4230 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
4231 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
4232 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
4233 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
4234 default:
4235 strcat (buf, _(", unknown")); break;
4237 break;
4238 case EM_LOONGARCH:
4239 if (EF_LOONGARCH_IS_LP64 (e_flags))
4240 strcat (buf, ", LP64");
4241 else if (EF_LOONGARCH_IS_ILP32 (e_flags))
4242 strcat (buf, ", ILP32");
4244 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4245 strcat (buf, ", SOFT-FLOAT");
4246 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4247 strcat (buf, ", SINGLE-FLOAT");
4248 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4249 strcat (buf, ", DOUBLE-FLOAT");
4251 break;
4255 return buf;
4258 static const char *
4259 get_osabi_name (Filedata * filedata, unsigned int osabi)
4261 static char buff[32];
4263 switch (osabi)
4265 case ELFOSABI_NONE: return "UNIX - System V";
4266 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4267 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4268 case ELFOSABI_GNU: return "UNIX - GNU";
4269 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4270 case ELFOSABI_AIX: return "UNIX - AIX";
4271 case ELFOSABI_IRIX: return "UNIX - IRIX";
4272 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4273 case ELFOSABI_TRU64: return "UNIX - TRU64";
4274 case ELFOSABI_MODESTO: return "Novell - Modesto";
4275 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4276 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4277 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4278 case ELFOSABI_AROS: return "AROS";
4279 case ELFOSABI_FENIXOS: return "FenixOS";
4280 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4281 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4282 default:
4283 if (osabi >= 64)
4284 switch (filedata->file_header.e_machine)
4286 case EM_ARM:
4287 switch (osabi)
4289 case ELFOSABI_ARM: return "ARM";
4290 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4291 default:
4292 break;
4294 break;
4296 case EM_MSP430:
4297 case EM_MSP430_OLD:
4298 case EM_VISIUM:
4299 switch (osabi)
4301 case ELFOSABI_STANDALONE: return _("Standalone App");
4302 default:
4303 break;
4305 break;
4307 case EM_TI_C6000:
4308 switch (osabi)
4310 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4311 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4312 default:
4313 break;
4315 break;
4317 default:
4318 break;
4320 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4321 return buff;
4325 static const char *
4326 get_aarch64_segment_type (unsigned long type)
4328 switch (type)
4330 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4331 default: return NULL;
4335 static const char *
4336 get_arm_segment_type (unsigned long type)
4338 switch (type)
4340 case PT_ARM_EXIDX: return "EXIDX";
4341 default: return NULL;
4345 static const char *
4346 get_s390_segment_type (unsigned long type)
4348 switch (type)
4350 case PT_S390_PGSTE: return "S390_PGSTE";
4351 default: return NULL;
4355 static const char *
4356 get_mips_segment_type (unsigned long type)
4358 switch (type)
4360 case PT_MIPS_REGINFO: return "REGINFO";
4361 case PT_MIPS_RTPROC: return "RTPROC";
4362 case PT_MIPS_OPTIONS: return "OPTIONS";
4363 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4364 default: return NULL;
4368 static const char *
4369 get_parisc_segment_type (unsigned long type)
4371 switch (type)
4373 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4374 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4375 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4376 default: return NULL;
4380 static const char *
4381 get_ia64_segment_type (unsigned long type)
4383 switch (type)
4385 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4386 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4387 default: return NULL;
4391 static const char *
4392 get_tic6x_segment_type (unsigned long type)
4394 switch (type)
4396 case PT_C6000_PHATTR: return "C6000_PHATTR";
4397 default: return NULL;
4401 static const char *
4402 get_riscv_segment_type (unsigned long type)
4404 switch (type)
4406 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4407 default: return NULL;
4411 static const char *
4412 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4414 if (e_machine == EM_PARISC)
4415 switch (type)
4417 case PT_HP_TLS: return "HP_TLS";
4418 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4419 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4420 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4421 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4422 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4423 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4424 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4425 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4426 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4427 case PT_HP_PARALLEL: return "HP_PARALLEL";
4428 case PT_HP_FASTBIND: return "HP_FASTBIND";
4429 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4430 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4431 case PT_HP_STACK: return "HP_STACK";
4432 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4433 default: return NULL;
4436 if (e_machine == EM_IA_64)
4437 switch (type)
4439 case PT_HP_TLS: return "HP_TLS";
4440 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4441 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4442 case PT_IA_64_HP_STACK: return "HP_STACK";
4443 default: return NULL;
4446 return NULL;
4449 static const char *
4450 get_solaris_segment_type (unsigned long type)
4452 switch (type)
4454 case 0x6464e550: return "PT_SUNW_UNWIND";
4455 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4456 case 0x6ffffff7: return "PT_LOSUNW";
4457 case 0x6ffffffa: return "PT_SUNWBSS";
4458 case 0x6ffffffb: return "PT_SUNWSTACK";
4459 case 0x6ffffffc: return "PT_SUNWDTRACE";
4460 case 0x6ffffffd: return "PT_SUNWCAP";
4461 case 0x6fffffff: return "PT_HISUNW";
4462 default: return NULL;
4466 static const char *
4467 get_segment_type (Filedata * filedata, unsigned long p_type)
4469 static char buff[32];
4471 switch (p_type)
4473 case PT_NULL: return "NULL";
4474 case PT_LOAD: return "LOAD";
4475 case PT_DYNAMIC: return "DYNAMIC";
4476 case PT_INTERP: return "INTERP";
4477 case PT_NOTE: return "NOTE";
4478 case PT_SHLIB: return "SHLIB";
4479 case PT_PHDR: return "PHDR";
4480 case PT_TLS: return "TLS";
4481 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4482 case PT_GNU_STACK: return "GNU_STACK";
4483 case PT_GNU_RELRO: return "GNU_RELRO";
4484 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4486 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4487 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4488 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4490 default:
4491 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4493 const char * result;
4495 switch (filedata->file_header.e_machine)
4497 case EM_AARCH64:
4498 result = get_aarch64_segment_type (p_type);
4499 break;
4500 case EM_ARM:
4501 result = get_arm_segment_type (p_type);
4502 break;
4503 case EM_MIPS:
4504 case EM_MIPS_RS3_LE:
4505 result = get_mips_segment_type (p_type);
4506 break;
4507 case EM_PARISC:
4508 result = get_parisc_segment_type (p_type);
4509 break;
4510 case EM_IA_64:
4511 result = get_ia64_segment_type (p_type);
4512 break;
4513 case EM_TI_C6000:
4514 result = get_tic6x_segment_type (p_type);
4515 break;
4516 case EM_S390:
4517 case EM_S390_OLD:
4518 result = get_s390_segment_type (p_type);
4519 break;
4520 case EM_RISCV:
4521 result = get_riscv_segment_type (p_type);
4522 break;
4523 default:
4524 result = NULL;
4525 break;
4528 if (result != NULL)
4529 return result;
4531 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4533 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4535 const char * result = NULL;
4537 switch (filedata->file_header.e_ident[EI_OSABI])
4539 case ELFOSABI_GNU:
4540 case ELFOSABI_FREEBSD:
4541 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4543 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4544 result = buff;
4546 break;
4547 case ELFOSABI_HPUX:
4548 result = get_hpux_segment_type (p_type,
4549 filedata->file_header.e_machine);
4550 break;
4551 case ELFOSABI_SOLARIS:
4552 result = get_solaris_segment_type (p_type);
4553 break;
4554 default:
4555 break;
4557 if (result != NULL)
4558 return result;
4560 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4562 else
4563 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4565 return buff;
4569 static const char *
4570 get_arc_section_type_name (unsigned int sh_type)
4572 switch (sh_type)
4574 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4575 default:
4576 break;
4578 return NULL;
4581 static const char *
4582 get_mips_section_type_name (unsigned int sh_type)
4584 switch (sh_type)
4586 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4587 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4588 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4589 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4590 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4591 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4592 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4593 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4594 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4595 case SHT_MIPS_RELD: return "MIPS_RELD";
4596 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4597 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4598 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4599 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4600 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4601 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4602 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4603 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4604 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4605 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4606 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4607 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4608 case SHT_MIPS_LINE: return "MIPS_LINE";
4609 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4610 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4611 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4612 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4613 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4614 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4615 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4616 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4617 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4618 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4619 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4620 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4621 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4622 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4623 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4624 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4625 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4626 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4627 default:
4628 break;
4630 return NULL;
4633 static const char *
4634 get_parisc_section_type_name (unsigned int sh_type)
4636 switch (sh_type)
4638 case SHT_PARISC_EXT: return "PARISC_EXT";
4639 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4640 case SHT_PARISC_DOC: return "PARISC_DOC";
4641 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4642 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4643 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4644 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4645 default: return NULL;
4649 static const char *
4650 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4652 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4653 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4654 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4656 switch (sh_type)
4658 case SHT_IA_64_EXT: return "IA_64_EXT";
4659 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4660 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4661 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4662 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4663 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4664 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4665 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4666 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4667 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4668 default:
4669 break;
4671 return NULL;
4674 static const char *
4675 get_x86_64_section_type_name (unsigned int sh_type)
4677 switch (sh_type)
4679 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4680 default: return NULL;
4684 static const char *
4685 get_aarch64_section_type_name (unsigned int sh_type)
4687 switch (sh_type)
4689 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4690 default: return NULL;
4694 static const char *
4695 get_arm_section_type_name (unsigned int sh_type)
4697 switch (sh_type)
4699 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4700 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4701 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4702 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4703 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4704 default: return NULL;
4708 static const char *
4709 get_tic6x_section_type_name (unsigned int sh_type)
4711 switch (sh_type)
4713 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4714 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4715 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4716 case SHT_TI_ICODE: return "TI_ICODE";
4717 case SHT_TI_XREF: return "TI_XREF";
4718 case SHT_TI_HANDLER: return "TI_HANDLER";
4719 case SHT_TI_INITINFO: return "TI_INITINFO";
4720 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4721 default: return NULL;
4725 static const char *
4726 get_msp430_section_type_name (unsigned int sh_type)
4728 switch (sh_type)
4730 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4731 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4732 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4733 default: return NULL;
4737 static const char *
4738 get_nfp_section_type_name (unsigned int sh_type)
4740 switch (sh_type)
4742 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4743 case SHT_NFP_INITREG: return "NFP_INITREG";
4744 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4745 default: return NULL;
4749 static const char *
4750 get_v850_section_type_name (unsigned int sh_type)
4752 switch (sh_type)
4754 case SHT_V850_SCOMMON: return "V850 Small Common";
4755 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4756 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4757 case SHT_RENESAS_IOP: return "RENESAS IOP";
4758 case SHT_RENESAS_INFO: return "RENESAS INFO";
4759 default: return NULL;
4763 static const char *
4764 get_riscv_section_type_name (unsigned int sh_type)
4766 switch (sh_type)
4768 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4769 default: return NULL;
4773 static const char *
4774 get_csky_section_type_name (unsigned int sh_type)
4776 switch (sh_type)
4778 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4779 default: return NULL;
4783 static const char *
4784 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4786 static char buff[32];
4787 const char * result;
4789 switch (sh_type)
4791 case SHT_NULL: return "NULL";
4792 case SHT_PROGBITS: return "PROGBITS";
4793 case SHT_SYMTAB: return "SYMTAB";
4794 case SHT_STRTAB: return "STRTAB";
4795 case SHT_RELA: return "RELA";
4796 case SHT_RELR: return "RELR";
4797 case SHT_HASH: return "HASH";
4798 case SHT_DYNAMIC: return "DYNAMIC";
4799 case SHT_NOTE: return "NOTE";
4800 case SHT_NOBITS: return "NOBITS";
4801 case SHT_REL: return "REL";
4802 case SHT_SHLIB: return "SHLIB";
4803 case SHT_DYNSYM: return "DYNSYM";
4804 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4805 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4806 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4807 case SHT_GNU_HASH: return "GNU_HASH";
4808 case SHT_GROUP: return "GROUP";
4809 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4810 case SHT_GNU_verdef: return "VERDEF";
4811 case SHT_GNU_verneed: return "VERNEED";
4812 case SHT_GNU_versym: return "VERSYM";
4813 case 0x6ffffff0: return "VERSYM";
4814 case 0x6ffffffc: return "VERDEF";
4815 case 0x7ffffffd: return "AUXILIARY";
4816 case 0x7fffffff: return "FILTER";
4817 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4819 default:
4820 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4822 switch (filedata->file_header.e_machine)
4824 case EM_ARC:
4825 case EM_ARC_COMPACT:
4826 case EM_ARC_COMPACT2:
4827 result = get_arc_section_type_name (sh_type);
4828 break;
4829 case EM_MIPS:
4830 case EM_MIPS_RS3_LE:
4831 result = get_mips_section_type_name (sh_type);
4832 break;
4833 case EM_PARISC:
4834 result = get_parisc_section_type_name (sh_type);
4835 break;
4836 case EM_IA_64:
4837 result = get_ia64_section_type_name (filedata, sh_type);
4838 break;
4839 case EM_X86_64:
4840 case EM_L1OM:
4841 case EM_K1OM:
4842 result = get_x86_64_section_type_name (sh_type);
4843 break;
4844 case EM_AARCH64:
4845 result = get_aarch64_section_type_name (sh_type);
4846 break;
4847 case EM_ARM:
4848 result = get_arm_section_type_name (sh_type);
4849 break;
4850 case EM_TI_C6000:
4851 result = get_tic6x_section_type_name (sh_type);
4852 break;
4853 case EM_MSP430:
4854 result = get_msp430_section_type_name (sh_type);
4855 break;
4856 case EM_NFP:
4857 result = get_nfp_section_type_name (sh_type);
4858 break;
4859 case EM_V800:
4860 case EM_V850:
4861 case EM_CYGNUS_V850:
4862 result = get_v850_section_type_name (sh_type);
4863 break;
4864 case EM_RISCV:
4865 result = get_riscv_section_type_name (sh_type);
4866 break;
4867 case EM_CSKY:
4868 result = get_csky_section_type_name (sh_type);
4869 break;
4870 default:
4871 result = NULL;
4872 break;
4875 if (result != NULL)
4876 return result;
4878 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4880 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4882 switch (filedata->file_header.e_machine)
4884 case EM_IA_64:
4885 result = get_ia64_section_type_name (filedata, sh_type);
4886 break;
4887 default:
4888 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4889 result = get_solaris_section_type (sh_type);
4890 else
4892 switch (sh_type)
4894 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4895 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4896 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4897 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4898 default:
4899 result = NULL;
4900 break;
4903 break;
4906 if (result != NULL)
4907 return result;
4909 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4911 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4913 switch (filedata->file_header.e_machine)
4915 case EM_V800:
4916 case EM_V850:
4917 case EM_CYGNUS_V850:
4918 result = get_v850_section_type_name (sh_type);
4919 break;
4920 default:
4921 result = NULL;
4922 break;
4925 if (result != NULL)
4926 return result;
4928 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4930 else
4931 /* This message is probably going to be displayed in a 15
4932 character wide field, so put the hex value first. */
4933 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4935 return buff;
4939 enum long_option_values
4941 OPTION_DEBUG_DUMP = 512,
4942 OPTION_DYN_SYMS,
4943 OPTION_LTO_SYMS,
4944 OPTION_DWARF_DEPTH,
4945 OPTION_DWARF_START,
4946 OPTION_DWARF_CHECK,
4947 OPTION_CTF_DUMP,
4948 OPTION_CTF_PARENT,
4949 OPTION_CTF_SYMBOLS,
4950 OPTION_CTF_STRINGS,
4951 OPTION_WITH_SYMBOL_VERSIONS,
4952 OPTION_RECURSE_LIMIT,
4953 OPTION_NO_RECURSE_LIMIT,
4954 OPTION_NO_DEMANGLING,
4955 OPTION_SYM_BASE
4958 static struct option options[] =
4960 /* Note - This table is alpha-sorted on the 'val'
4961 field in order to make adding new options easier. */
4962 {"arch-specific", no_argument, 0, 'A'},
4963 {"all", no_argument, 0, 'a'},
4964 {"demangle", optional_argument, 0, 'C'},
4965 {"archive-index", no_argument, 0, 'c'},
4966 {"use-dynamic", no_argument, 0, 'D'},
4967 {"dynamic", no_argument, 0, 'd'},
4968 {"headers", no_argument, 0, 'e'},
4969 {"section-groups", no_argument, 0, 'g'},
4970 {"help", no_argument, 0, 'H'},
4971 {"file-header", no_argument, 0, 'h'},
4972 {"histogram", no_argument, 0, 'I'},
4973 {"lint", no_argument, 0, 'L'},
4974 {"enable-checks", no_argument, 0, 'L'},
4975 {"program-headers", no_argument, 0, 'l'},
4976 {"segments", no_argument, 0, 'l'},
4977 {"full-section-name",no_argument, 0, 'N'},
4978 {"notes", no_argument, 0, 'n'},
4979 {"process-links", no_argument, 0, 'P'},
4980 {"string-dump", required_argument, 0, 'p'},
4981 {"relocated-dump", required_argument, 0, 'R'},
4982 {"relocs", no_argument, 0, 'r'},
4983 {"section-headers", no_argument, 0, 'S'},
4984 {"sections", no_argument, 0, 'S'},
4985 {"symbols", no_argument, 0, 's'},
4986 {"syms", no_argument, 0, 's'},
4987 {"silent-truncation",no_argument, 0, 'T'},
4988 {"section-details", no_argument, 0, 't'},
4989 {"unicode", required_argument, NULL, 'U'},
4990 {"unwind", no_argument, 0, 'u'},
4991 {"version-info", no_argument, 0, 'V'},
4992 {"version", no_argument, 0, 'v'},
4993 {"wide", no_argument, 0, 'W'},
4994 {"hex-dump", required_argument, 0, 'x'},
4995 {"decompress", no_argument, 0, 'z'},
4997 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
4998 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
4999 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5000 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5001 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5002 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5003 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5004 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5005 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5006 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5007 #ifdef ENABLE_LIBCTF
5008 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5009 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5010 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5011 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5012 #endif
5013 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5015 {0, no_argument, 0, 0}
5018 static void
5019 usage (FILE * stream)
5021 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5022 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5023 fprintf (stream, _(" Options are:\n"));
5024 fprintf (stream, _("\
5025 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5026 fprintf (stream, _("\
5027 -h --file-header Display the ELF file header\n"));
5028 fprintf (stream, _("\
5029 -l --program-headers Display the program headers\n"));
5030 fprintf (stream, _("\
5031 --segments An alias for --program-headers\n"));
5032 fprintf (stream, _("\
5033 -S --section-headers Display the sections' header\n"));
5034 fprintf (stream, _("\
5035 --sections An alias for --section-headers\n"));
5036 fprintf (stream, _("\
5037 -g --section-groups Display the section groups\n"));
5038 fprintf (stream, _("\
5039 -t --section-details Display the section details\n"));
5040 fprintf (stream, _("\
5041 -e --headers Equivalent to: -h -l -S\n"));
5042 fprintf (stream, _("\
5043 -s --syms Display the symbol table\n"));
5044 fprintf (stream, _("\
5045 --symbols An alias for --syms\n"));
5046 fprintf (stream, _("\
5047 --dyn-syms Display the dynamic symbol table\n"));
5048 fprintf (stream, _("\
5049 --lto-syms Display LTO symbol tables\n"));
5050 fprintf (stream, _("\
5051 --sym-base=[0|8|10|16] \n\
5052 Force base for symbol sizes. The options are \n\
5053 mixed (the default), octal, decimal, hexadecimal.\n"));
5054 fprintf (stream, _("\
5055 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5056 display_demangler_styles (stream, _("\
5057 STYLE can be "));
5058 fprintf (stream, _("\
5059 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5060 fprintf (stream, _("\
5061 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5062 fprintf (stream, _("\
5063 --no-recurse-limit Disable a demangling recursion limit\n"));
5064 fprintf (stream, _("\
5065 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5066 Display unicode characters as determined by the current locale\n\
5067 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5068 escape sequences, or treat them as invalid and display as\n\
5069 \"{hex sequences}\"\n"));
5070 fprintf (stream, _("\
5071 -n --notes Display the core notes (if present)\n"));
5072 fprintf (stream, _("\
5073 -r --relocs Display the relocations (if present)\n"));
5074 fprintf (stream, _("\
5075 -u --unwind Display the unwind info (if present)\n"));
5076 fprintf (stream, _("\
5077 -d --dynamic Display the dynamic section (if present)\n"));
5078 fprintf (stream, _("\
5079 -V --version-info Display the version sections (if present)\n"));
5080 fprintf (stream, _("\
5081 -A --arch-specific Display architecture specific information (if any)\n"));
5082 fprintf (stream, _("\
5083 -c --archive-index Display the symbol/file index in an archive\n"));
5084 fprintf (stream, _("\
5085 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5086 fprintf (stream, _("\
5087 -L --lint|--enable-checks\n\
5088 Display warning messages for possible problems\n"));
5089 fprintf (stream, _("\
5090 -x --hex-dump=<number|name>\n\
5091 Dump the contents of section <number|name> as bytes\n"));
5092 fprintf (stream, _("\
5093 -p --string-dump=<number|name>\n\
5094 Dump the contents of section <number|name> as strings\n"));
5095 fprintf (stream, _("\
5096 -R --relocated-dump=<number|name>\n\
5097 Dump the relocated contents of section <number|name>\n"));
5098 fprintf (stream, _("\
5099 -z --decompress Decompress section before dumping it\n"));
5100 fprintf (stream, _("\
5101 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5102 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5103 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5104 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5105 U/=trace_info]\n\
5106 Display the contents of DWARF debug sections\n"));
5107 fprintf (stream, _("\
5108 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5109 debuginfo files\n"));
5110 fprintf (stream, _("\
5111 -P --process-links Display the contents of non-debug sections in separate\n\
5112 debuginfo files. (Implies -wK)\n"));
5113 #if DEFAULT_FOR_FOLLOW_LINKS
5114 fprintf (stream, _("\
5115 -wK --debug-dump=follow-links\n\
5116 Follow links to separate debug info files (default)\n"));
5117 fprintf (stream, _("\
5118 -wN --debug-dump=no-follow-links\n\
5119 Do not follow links to separate debug info files\n"));
5120 #else
5121 fprintf (stream, _("\
5122 -wK --debug-dump=follow-links\n\
5123 Follow links to separate debug info files\n"));
5124 fprintf (stream, _("\
5125 -wN --debug-dump=no-follow-links\n\
5126 Do not follow links to separate debug info files\n\
5127 (default)\n"));
5128 #endif
5129 fprintf (stream, _("\
5130 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5131 fprintf (stream, _("\
5132 --dwarf-start=N Display DIEs starting at offset N\n"));
5133 #ifdef ENABLE_LIBCTF
5134 fprintf (stream, _("\
5135 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5136 fprintf (stream, _("\
5137 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
5138 fprintf (stream, _("\
5139 --ctf-symbols=<number|name>\n\
5140 Use section <number|name> as the CTF external symtab\n"));
5141 fprintf (stream, _("\
5142 --ctf-strings=<number|name>\n\
5143 Use section <number|name> as the CTF external strtab\n"));
5144 #endif
5146 #ifdef SUPPORT_DISASSEMBLY
5147 fprintf (stream, _("\
5148 -i --instruction-dump=<number|name>\n\
5149 Disassemble the contents of section <number|name>\n"));
5150 #endif
5151 fprintf (stream, _("\
5152 -I --histogram Display histogram of bucket list lengths\n"));
5153 fprintf (stream, _("\
5154 -W --wide Allow output width to exceed 80 characters\n"));
5155 fprintf (stream, _("\
5156 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5157 fprintf (stream, _("\
5158 @<file> Read options from <file>\n"));
5159 fprintf (stream, _("\
5160 -H --help Display this information\n"));
5161 fprintf (stream, _("\
5162 -v --version Display the version number of readelf\n"));
5164 if (REPORT_BUGS_TO[0] && stream == stdout)
5165 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5167 exit (stream == stdout ? 0 : 1);
5170 /* Record the fact that the user wants the contents of section number
5171 SECTION to be displayed using the method(s) encoded as flags bits
5172 in TYPE. Note, TYPE can be zero if we are creating the array for
5173 the first time. */
5175 static void
5176 request_dump_bynumber (struct dump_data *dumpdata,
5177 unsigned int section, dump_type type)
5179 if (section >= dumpdata->num_dump_sects)
5181 dump_type * new_dump_sects;
5183 new_dump_sects = (dump_type *) calloc (section + 1,
5184 sizeof (* new_dump_sects));
5186 if (new_dump_sects == NULL)
5187 error (_("Out of memory allocating dump request table.\n"));
5188 else
5190 if (dumpdata->dump_sects)
5192 /* Copy current flag settings. */
5193 memcpy (new_dump_sects, dumpdata->dump_sects,
5194 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5196 free (dumpdata->dump_sects);
5199 dumpdata->dump_sects = new_dump_sects;
5200 dumpdata->num_dump_sects = section + 1;
5204 if (dumpdata->dump_sects)
5205 dumpdata->dump_sects[section] |= type;
5208 /* Request a dump by section name. */
5210 static void
5211 request_dump_byname (const char * section, dump_type type)
5213 struct dump_list_entry * new_request;
5215 new_request = (struct dump_list_entry *)
5216 malloc (sizeof (struct dump_list_entry));
5217 if (!new_request)
5218 error (_("Out of memory allocating dump request table.\n"));
5220 new_request->name = strdup (section);
5221 if (!new_request->name)
5222 error (_("Out of memory allocating dump request table.\n"));
5224 new_request->type = type;
5226 new_request->next = dump_sects_byname;
5227 dump_sects_byname = new_request;
5230 static inline void
5231 request_dump (struct dump_data *dumpdata, dump_type type)
5233 int section;
5234 char * cp;
5236 do_dump = true;
5237 section = strtoul (optarg, & cp, 0);
5239 if (! *cp && section >= 0)
5240 request_dump_bynumber (dumpdata, section, type);
5241 else
5242 request_dump_byname (optarg, type);
5245 static void
5246 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5248 int c;
5250 if (argc < 2)
5251 usage (stderr);
5253 while ((c = getopt_long
5254 (argc, argv, "ACDHILNPR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5256 switch (c)
5258 case 0:
5259 /* Long options. */
5260 break;
5261 case 'H':
5262 usage (stdout);
5263 break;
5265 case 'a':
5266 do_syms = true;
5267 do_reloc = true;
5268 do_unwind = true;
5269 do_dynamic = true;
5270 do_header = true;
5271 do_sections = true;
5272 do_section_groups = true;
5273 do_segments = true;
5274 do_version = true;
5275 do_histogram = true;
5276 do_arch = true;
5277 do_notes = true;
5278 break;
5280 case 'g':
5281 do_section_groups = true;
5282 break;
5283 case 't':
5284 case 'N':
5285 do_sections = true;
5286 do_section_details = true;
5287 break;
5288 case 'e':
5289 do_header = true;
5290 do_sections = true;
5291 do_segments = true;
5292 break;
5293 case 'A':
5294 do_arch = true;
5295 break;
5296 case 'D':
5297 do_using_dynamic = true;
5298 break;
5299 case 'r':
5300 do_reloc = true;
5301 break;
5302 case 'u':
5303 do_unwind = true;
5304 break;
5305 case 'h':
5306 do_header = true;
5307 break;
5308 case 'l':
5309 do_segments = true;
5310 break;
5311 case 's':
5312 do_syms = true;
5313 break;
5314 case 'S':
5315 do_sections = true;
5316 break;
5317 case 'd':
5318 do_dynamic = true;
5319 break;
5320 case 'I':
5321 do_histogram = true;
5322 break;
5323 case 'n':
5324 do_notes = true;
5325 break;
5326 case 'c':
5327 do_archive_index = true;
5328 break;
5329 case 'L':
5330 do_checks = true;
5331 break;
5332 case 'P':
5333 process_links = true;
5334 do_follow_links = true;
5335 break;
5336 case 'x':
5337 request_dump (dumpdata, HEX_DUMP);
5338 break;
5339 case 'p':
5340 request_dump (dumpdata, STRING_DUMP);
5341 break;
5342 case 'R':
5343 request_dump (dumpdata, RELOC_DUMP);
5344 break;
5345 case 'z':
5346 decompress_dumps = true;
5347 break;
5348 case 'w':
5349 do_dump = true;
5350 if (optarg == NULL)
5352 do_debugging = true;
5353 dwarf_select_sections_all ();
5355 else
5357 do_debugging = false;
5358 dwarf_select_sections_by_letters (optarg);
5360 break;
5361 case OPTION_DEBUG_DUMP:
5362 do_dump = true;
5363 if (optarg == NULL)
5365 do_debugging = true;
5366 dwarf_select_sections_all ();
5368 else
5370 do_debugging = false;
5371 dwarf_select_sections_by_names (optarg);
5373 break;
5374 case OPTION_DWARF_DEPTH:
5376 char *cp;
5378 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5380 break;
5381 case OPTION_DWARF_START:
5383 char *cp;
5385 dwarf_start_die = strtoul (optarg, & cp, 0);
5387 break;
5388 case OPTION_DWARF_CHECK:
5389 dwarf_check = true;
5390 break;
5391 case OPTION_CTF_DUMP:
5392 do_ctf = true;
5393 request_dump (dumpdata, CTF_DUMP);
5394 break;
5395 case OPTION_CTF_SYMBOLS:
5396 free (dump_ctf_symtab_name);
5397 dump_ctf_symtab_name = strdup (optarg);
5398 break;
5399 case OPTION_CTF_STRINGS:
5400 free (dump_ctf_strtab_name);
5401 dump_ctf_strtab_name = strdup (optarg);
5402 break;
5403 case OPTION_CTF_PARENT:
5404 free (dump_ctf_parent_name);
5405 dump_ctf_parent_name = strdup (optarg);
5406 break;
5407 case OPTION_DYN_SYMS:
5408 do_dyn_syms = true;
5409 break;
5410 case OPTION_LTO_SYMS:
5411 do_lto_syms = true;
5412 break;
5413 #ifdef SUPPORT_DISASSEMBLY
5414 case 'i':
5415 request_dump (dumpdata, DISASS_DUMP);
5416 break;
5417 #endif
5418 case 'v':
5419 print_version (program_name);
5420 break;
5421 case 'V':
5422 do_version = true;
5423 break;
5424 case 'W':
5425 do_wide = true;
5426 break;
5427 case 'T':
5428 do_not_show_symbol_truncation = true;
5429 break;
5430 case 'C':
5431 do_demangle = true;
5432 if (optarg != NULL)
5434 enum demangling_styles style;
5436 style = cplus_demangle_name_to_style (optarg);
5437 if (style == unknown_demangling)
5438 error (_("unknown demangling style `%s'"), optarg);
5440 cplus_demangle_set_style (style);
5442 break;
5443 case OPTION_NO_DEMANGLING:
5444 do_demangle = false;
5445 break;
5446 case OPTION_RECURSE_LIMIT:
5447 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5448 break;
5449 case OPTION_NO_RECURSE_LIMIT:
5450 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5451 break;
5452 case OPTION_WITH_SYMBOL_VERSIONS:
5453 /* Ignored for backward compatibility. */
5454 break;
5456 case 'U':
5457 if (optarg == NULL)
5458 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
5459 else if (streq (optarg, "default") || streq (optarg, "d"))
5460 unicode_display = unicode_default;
5461 else if (streq (optarg, "locale") || streq (optarg, "l"))
5462 unicode_display = unicode_locale;
5463 else if (streq (optarg, "escape") || streq (optarg, "e"))
5464 unicode_display = unicode_escape;
5465 else if (streq (optarg, "invalid") || streq (optarg, "i"))
5466 unicode_display = unicode_invalid;
5467 else if (streq (optarg, "hex") || streq (optarg, "x"))
5468 unicode_display = unicode_hex;
5469 else if (streq (optarg, "highlight") || streq (optarg, "h"))
5470 unicode_display = unicode_highlight;
5471 else
5472 error (_("invalid argument to -U/--unicode: %s"), optarg);
5473 break;
5475 case OPTION_SYM_BASE:
5476 sym_base = 0;
5477 if (optarg != NULL)
5479 sym_base = strtoul (optarg, NULL, 0);
5480 switch (sym_base)
5482 case 0:
5483 case 8:
5484 case 10:
5485 case 16:
5486 break;
5488 default:
5489 sym_base = 0;
5490 break;
5493 break;
5495 default:
5496 /* xgettext:c-format */
5497 error (_("Invalid option '-%c'\n"), c);
5498 /* Fall through. */
5499 case '?':
5500 usage (stderr);
5504 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5505 && !do_segments && !do_header && !do_dump && !do_version
5506 && !do_histogram && !do_debugging && !do_arch && !do_notes
5507 && !do_section_groups && !do_archive_index
5508 && !do_dyn_syms && !do_lto_syms)
5510 if (do_checks)
5512 check_all = true;
5513 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5514 do_segments = do_header = do_dump = do_version = true;
5515 do_histogram = do_debugging = do_arch = do_notes = true;
5516 do_section_groups = do_archive_index = do_dyn_syms = true;
5517 do_lto_syms = true;
5519 else
5520 usage (stderr);
5524 static const char *
5525 get_elf_class (unsigned int elf_class)
5527 static char buff[32];
5529 switch (elf_class)
5531 case ELFCLASSNONE: return _("none");
5532 case ELFCLASS32: return "ELF32";
5533 case ELFCLASS64: return "ELF64";
5534 default:
5535 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5536 return buff;
5540 static const char *
5541 get_data_encoding (unsigned int encoding)
5543 static char buff[32];
5545 switch (encoding)
5547 case ELFDATANONE: return _("none");
5548 case ELFDATA2LSB: return _("2's complement, little endian");
5549 case ELFDATA2MSB: return _("2's complement, big endian");
5550 default:
5551 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5552 return buff;
5556 /* Decode the data held in 'filedata->file_header'. */
5558 static bool
5559 process_file_header (Filedata * filedata)
5561 Elf_Internal_Ehdr * header = & filedata->file_header;
5563 if ( header->e_ident[EI_MAG0] != ELFMAG0
5564 || header->e_ident[EI_MAG1] != ELFMAG1
5565 || header->e_ident[EI_MAG2] != ELFMAG2
5566 || header->e_ident[EI_MAG3] != ELFMAG3)
5568 error
5569 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5570 return false;
5573 if (! filedata->is_separate)
5574 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5576 if (do_header)
5578 unsigned i;
5580 if (filedata->is_separate)
5581 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5582 else
5583 printf (_("ELF Header:\n"));
5584 printf (_(" Magic: "));
5585 for (i = 0; i < EI_NIDENT; i++)
5586 printf ("%2.2x ", header->e_ident[i]);
5587 printf ("\n");
5588 printf (_(" Class: %s\n"),
5589 get_elf_class (header->e_ident[EI_CLASS]));
5590 printf (_(" Data: %s\n"),
5591 get_data_encoding (header->e_ident[EI_DATA]));
5592 printf (_(" Version: %d%s\n"),
5593 header->e_ident[EI_VERSION],
5594 (header->e_ident[EI_VERSION] == EV_CURRENT
5595 ? _(" (current)")
5596 : (header->e_ident[EI_VERSION] != EV_NONE
5597 ? _(" <unknown>")
5598 : "")));
5599 printf (_(" OS/ABI: %s\n"),
5600 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5601 printf (_(" ABI Version: %d\n"),
5602 header->e_ident[EI_ABIVERSION]);
5603 printf (_(" Type: %s\n"),
5604 get_file_type (filedata));
5605 printf (_(" Machine: %s\n"),
5606 get_machine_name (header->e_machine));
5607 printf (_(" Version: 0x%lx\n"),
5608 header->e_version);
5610 printf (_(" Entry point address: "));
5611 print_vma (header->e_entry, PREFIX_HEX);
5612 printf (_("\n Start of program headers: "));
5613 print_vma (header->e_phoff, DEC);
5614 printf (_(" (bytes into file)\n Start of section headers: "));
5615 print_vma (header->e_shoff, DEC);
5616 printf (_(" (bytes into file)\n"));
5618 printf (_(" Flags: 0x%lx%s\n"),
5619 header->e_flags,
5620 get_machine_flags (filedata, header->e_flags, header->e_machine));
5621 printf (_(" Size of this header: %u (bytes)\n"),
5622 header->e_ehsize);
5623 printf (_(" Size of program headers: %u (bytes)\n"),
5624 header->e_phentsize);
5625 printf (_(" Number of program headers: %u"),
5626 header->e_phnum);
5627 if (filedata->section_headers != NULL
5628 && header->e_phnum == PN_XNUM
5629 && filedata->section_headers[0].sh_info != 0)
5631 header->e_phnum = filedata->section_headers[0].sh_info;
5632 printf (" (%u)", header->e_phnum);
5634 putc ('\n', stdout);
5635 printf (_(" Size of section headers: %u (bytes)\n"),
5636 header->e_shentsize);
5637 printf (_(" Number of section headers: %u"),
5638 header->e_shnum);
5639 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5641 header->e_shnum = filedata->section_headers[0].sh_size;
5642 printf (" (%u)", header->e_shnum);
5644 putc ('\n', stdout);
5645 printf (_(" Section header string table index: %u"),
5646 header->e_shstrndx);
5647 if (filedata->section_headers != NULL
5648 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5650 header->e_shstrndx = filedata->section_headers[0].sh_link;
5651 printf (" (%u)", header->e_shstrndx);
5653 if (header->e_shstrndx != SHN_UNDEF
5654 && header->e_shstrndx >= header->e_shnum)
5656 header->e_shstrndx = SHN_UNDEF;
5657 printf (_(" <corrupt: out of range>"));
5659 putc ('\n', stdout);
5662 if (filedata->section_headers != NULL)
5664 if (header->e_phnum == PN_XNUM
5665 && filedata->section_headers[0].sh_info != 0)
5666 header->e_phnum = filedata->section_headers[0].sh_info;
5667 if (header->e_shnum == SHN_UNDEF)
5668 header->e_shnum = filedata->section_headers[0].sh_size;
5669 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5670 header->e_shstrndx = filedata->section_headers[0].sh_link;
5671 if (header->e_shstrndx >= header->e_shnum)
5672 header->e_shstrndx = SHN_UNDEF;
5675 return true;
5678 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5679 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5681 static bool
5682 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5684 Elf32_External_Phdr * phdrs;
5685 Elf32_External_Phdr * external;
5686 Elf_Internal_Phdr * internal;
5687 unsigned int i;
5688 unsigned int size = filedata->file_header.e_phentsize;
5689 unsigned int num = filedata->file_header.e_phnum;
5691 /* PR binutils/17531: Cope with unexpected section header sizes. */
5692 if (size == 0 || num == 0)
5693 return false;
5694 if (size < sizeof * phdrs)
5696 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5697 return false;
5699 if (size > sizeof * phdrs)
5700 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5702 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5703 size, num, _("program headers"));
5704 if (phdrs == NULL)
5705 return false;
5707 for (i = 0, internal = pheaders, external = phdrs;
5708 i < filedata->file_header.e_phnum;
5709 i++, internal++, external++)
5711 internal->p_type = BYTE_GET (external->p_type);
5712 internal->p_offset = BYTE_GET (external->p_offset);
5713 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5714 internal->p_paddr = BYTE_GET (external->p_paddr);
5715 internal->p_filesz = BYTE_GET (external->p_filesz);
5716 internal->p_memsz = BYTE_GET (external->p_memsz);
5717 internal->p_flags = BYTE_GET (external->p_flags);
5718 internal->p_align = BYTE_GET (external->p_align);
5721 free (phdrs);
5722 return true;
5725 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5726 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5728 static bool
5729 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5731 Elf64_External_Phdr * phdrs;
5732 Elf64_External_Phdr * external;
5733 Elf_Internal_Phdr * internal;
5734 unsigned int i;
5735 unsigned int size = filedata->file_header.e_phentsize;
5736 unsigned int num = filedata->file_header.e_phnum;
5738 /* PR binutils/17531: Cope with unexpected section header sizes. */
5739 if (size == 0 || num == 0)
5740 return false;
5741 if (size < sizeof * phdrs)
5743 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5744 return false;
5746 if (size > sizeof * phdrs)
5747 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5749 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5750 size, num, _("program headers"));
5751 if (!phdrs)
5752 return false;
5754 for (i = 0, internal = pheaders, external = phdrs;
5755 i < filedata->file_header.e_phnum;
5756 i++, internal++, external++)
5758 internal->p_type = BYTE_GET (external->p_type);
5759 internal->p_flags = BYTE_GET (external->p_flags);
5760 internal->p_offset = BYTE_GET (external->p_offset);
5761 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5762 internal->p_paddr = BYTE_GET (external->p_paddr);
5763 internal->p_filesz = BYTE_GET (external->p_filesz);
5764 internal->p_memsz = BYTE_GET (external->p_memsz);
5765 internal->p_align = BYTE_GET (external->p_align);
5768 free (phdrs);
5769 return true;
5772 /* Returns TRUE if the program headers were read into `program_headers'. */
5774 static bool
5775 get_program_headers (Filedata * filedata)
5777 Elf_Internal_Phdr * phdrs;
5779 /* Check cache of prior read. */
5780 if (filedata->program_headers != NULL)
5781 return true;
5783 /* Be kind to memory checkers by looking for
5784 e_phnum values which we know must be invalid. */
5785 if (filedata->file_header.e_phnum
5786 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5787 >= filedata->file_size)
5789 error (_("Too many program headers - %#x - the file is not that big\n"),
5790 filedata->file_header.e_phnum);
5791 return false;
5794 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5795 sizeof (Elf_Internal_Phdr));
5796 if (phdrs == NULL)
5798 error (_("Out of memory reading %u program headers\n"),
5799 filedata->file_header.e_phnum);
5800 return false;
5803 if (is_32bit_elf
5804 ? get_32bit_program_headers (filedata, phdrs)
5805 : get_64bit_program_headers (filedata, phdrs))
5807 filedata->program_headers = phdrs;
5808 return true;
5811 free (phdrs);
5812 return false;
5815 /* Print program header info and locate dynamic section. */
5817 static void
5818 process_program_headers (Filedata * filedata)
5820 Elf_Internal_Phdr * segment;
5821 unsigned int i;
5822 Elf_Internal_Phdr * previous_load = NULL;
5824 if (filedata->file_header.e_phnum == 0)
5826 /* PR binutils/12467. */
5827 if (filedata->file_header.e_phoff != 0)
5828 warn (_("possibly corrupt ELF header - it has a non-zero program"
5829 " header offset, but no program headers\n"));
5830 else if (do_segments)
5832 if (filedata->is_separate)
5833 printf (_("\nThere are no program headers in linked file '%s'.\n"),
5834 filedata->file_name);
5835 else
5836 printf (_("\nThere are no program headers in this file.\n"));
5838 goto no_headers;
5841 if (do_segments && !do_header)
5843 if (filedata->is_separate)
5844 printf ("\nIn linked file '%s' the ELF file type is %s\n",
5845 filedata->file_name, get_file_type (filedata));
5846 else
5847 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
5848 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5849 printf (ngettext ("There is %d program header, starting at offset %s\n",
5850 "There are %d program headers, starting at offset %s\n",
5851 filedata->file_header.e_phnum),
5852 filedata->file_header.e_phnum,
5853 bfd_vmatoa ("u", filedata->file_header.e_phoff));
5856 if (! get_program_headers (filedata))
5857 goto no_headers;
5859 if (do_segments)
5861 if (filedata->file_header.e_phnum > 1)
5862 printf (_("\nProgram Headers:\n"));
5863 else
5864 printf (_("\nProgram Headers:\n"));
5866 if (is_32bit_elf)
5867 printf
5868 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5869 else if (do_wide)
5870 printf
5871 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5872 else
5874 printf
5875 (_(" Type Offset VirtAddr PhysAddr\n"));
5876 printf
5877 (_(" FileSiz MemSiz Flags Align\n"));
5881 unsigned long dynamic_addr = 0;
5882 bfd_size_type dynamic_size = 0;
5883 for (i = 0, segment = filedata->program_headers;
5884 i < filedata->file_header.e_phnum;
5885 i++, segment++)
5887 if (do_segments)
5889 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
5891 if (is_32bit_elf)
5893 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5894 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5895 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5896 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5897 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5898 printf ("%c%c%c ",
5899 (segment->p_flags & PF_R ? 'R' : ' '),
5900 (segment->p_flags & PF_W ? 'W' : ' '),
5901 (segment->p_flags & PF_X ? 'E' : ' '));
5902 printf ("%#lx", (unsigned long) segment->p_align);
5904 else if (do_wide)
5906 if ((unsigned long) segment->p_offset == segment->p_offset)
5907 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5908 else
5910 print_vma (segment->p_offset, FULL_HEX);
5911 putchar (' ');
5914 print_vma (segment->p_vaddr, FULL_HEX);
5915 putchar (' ');
5916 print_vma (segment->p_paddr, FULL_HEX);
5917 putchar (' ');
5919 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5920 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5921 else
5923 print_vma (segment->p_filesz, FULL_HEX);
5924 putchar (' ');
5927 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5928 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5929 else
5931 print_vma (segment->p_memsz, FULL_HEX);
5934 printf (" %c%c%c ",
5935 (segment->p_flags & PF_R ? 'R' : ' '),
5936 (segment->p_flags & PF_W ? 'W' : ' '),
5937 (segment->p_flags & PF_X ? 'E' : ' '));
5939 if ((unsigned long) segment->p_align == segment->p_align)
5940 printf ("%#lx", (unsigned long) segment->p_align);
5941 else
5943 print_vma (segment->p_align, PREFIX_HEX);
5946 else
5948 print_vma (segment->p_offset, FULL_HEX);
5949 putchar (' ');
5950 print_vma (segment->p_vaddr, FULL_HEX);
5951 putchar (' ');
5952 print_vma (segment->p_paddr, FULL_HEX);
5953 printf ("\n ");
5954 print_vma (segment->p_filesz, FULL_HEX);
5955 putchar (' ');
5956 print_vma (segment->p_memsz, FULL_HEX);
5957 printf (" %c%c%c ",
5958 (segment->p_flags & PF_R ? 'R' : ' '),
5959 (segment->p_flags & PF_W ? 'W' : ' '),
5960 (segment->p_flags & PF_X ? 'E' : ' '));
5961 print_vma (segment->p_align, PREFIX_HEX);
5964 putc ('\n', stdout);
5967 switch (segment->p_type)
5969 case PT_LOAD:
5970 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5971 required by the ELF standard, several programs, including the Linux
5972 kernel, make use of non-ordered segments. */
5973 if (previous_load
5974 && previous_load->p_vaddr > segment->p_vaddr)
5975 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5976 #endif
5977 if (segment->p_memsz < segment->p_filesz)
5978 error (_("the segment's file size is larger than its memory size\n"));
5979 previous_load = segment;
5980 break;
5982 case PT_PHDR:
5983 /* PR 20815 - Verify that the program header is loaded into memory. */
5984 if (i > 0 && previous_load != NULL)
5985 error (_("the PHDR segment must occur before any LOAD segment\n"));
5986 if (filedata->file_header.e_machine != EM_PARISC)
5988 unsigned int j;
5990 for (j = 1; j < filedata->file_header.e_phnum; j++)
5992 Elf_Internal_Phdr *load = filedata->program_headers + j;
5993 if (load->p_type == PT_LOAD
5994 && load->p_offset <= segment->p_offset
5995 && (load->p_offset + load->p_filesz
5996 >= segment->p_offset + segment->p_filesz)
5997 && load->p_vaddr <= segment->p_vaddr
5998 && (load->p_vaddr + load->p_filesz
5999 >= segment->p_vaddr + segment->p_filesz))
6000 break;
6002 if (j == filedata->file_header.e_phnum)
6003 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6005 break;
6007 case PT_DYNAMIC:
6008 if (dynamic_addr)
6009 error (_("more than one dynamic segment\n"));
6011 /* By default, assume that the .dynamic section is the first
6012 section in the DYNAMIC segment. */
6013 dynamic_addr = segment->p_offset;
6014 dynamic_size = segment->p_filesz;
6016 /* Try to locate the .dynamic section. If there is
6017 a section header table, we can easily locate it. */
6018 if (filedata->section_headers != NULL)
6020 Elf_Internal_Shdr * sec;
6022 sec = find_section (filedata, ".dynamic");
6023 if (sec == NULL || sec->sh_size == 0)
6025 /* A corresponding .dynamic section is expected, but on
6026 IA-64/OpenVMS it is OK for it to be missing. */
6027 if (!is_ia64_vms (filedata))
6028 error (_("no .dynamic section in the dynamic segment\n"));
6029 break;
6032 if (sec->sh_type == SHT_NOBITS)
6034 dynamic_addr = 0;
6035 dynamic_size = 0;
6036 break;
6039 dynamic_addr = sec->sh_offset;
6040 dynamic_size = sec->sh_size;
6042 /* The PT_DYNAMIC segment, which is used by the run-time
6043 loader, should exactly match the .dynamic section. */
6044 if (do_checks
6045 && (dynamic_addr != segment->p_offset
6046 || dynamic_size != segment->p_filesz))
6047 warn (_("\
6048 the .dynamic section is not the same as the dynamic segment\n"));
6051 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6052 segment. Check this after matching against the section headers
6053 so we don't warn on debuginfo file (which have NOBITS .dynamic
6054 sections). */
6055 if (dynamic_addr > filedata->file_size
6056 || (dynamic_size > filedata->file_size - dynamic_addr))
6058 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6059 dynamic_addr = 0;
6060 dynamic_size = 0;
6062 break;
6064 case PT_INTERP:
6065 if (segment->p_offset >= filedata->file_size
6066 || segment->p_filesz > filedata->file_size - segment->p_offset
6067 || segment->p_filesz - 1 >= (size_t) -2
6068 || fseek (filedata->handle,
6069 filedata->archive_file_offset + (long) segment->p_offset,
6070 SEEK_SET))
6071 error (_("Unable to find program interpreter name\n"));
6072 else
6074 size_t len = segment->p_filesz;
6075 free (filedata->program_interpreter);
6076 filedata->program_interpreter = xmalloc (len + 1);
6077 len = fread (filedata->program_interpreter, 1, len,
6078 filedata->handle);
6079 filedata->program_interpreter[len] = 0;
6081 if (do_segments)
6082 printf (_(" [Requesting program interpreter: %s]\n"),
6083 filedata->program_interpreter);
6085 break;
6089 if (do_segments
6090 && filedata->section_headers != NULL
6091 && filedata->string_table != NULL)
6093 printf (_("\n Section to Segment mapping:\n"));
6094 printf (_(" Segment Sections...\n"));
6096 for (i = 0; i < filedata->file_header.e_phnum; i++)
6098 unsigned int j;
6099 Elf_Internal_Shdr * section;
6101 segment = filedata->program_headers + i;
6102 section = filedata->section_headers + 1;
6104 printf (" %2.2d ", i);
6106 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6108 if (!ELF_TBSS_SPECIAL (section, segment)
6109 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6110 printf ("%s ", printable_section_name (filedata, section));
6113 putc ('\n',stdout);
6117 filedata->dynamic_addr = dynamic_addr;
6118 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6119 return;
6121 no_headers:
6122 filedata->dynamic_addr = 0;
6123 filedata->dynamic_size = 1;
6127 /* Find the file offset corresponding to VMA by using the program headers. */
6129 static long
6130 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
6132 Elf_Internal_Phdr * seg;
6134 if (! get_program_headers (filedata))
6136 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6137 return (long) vma;
6140 for (seg = filedata->program_headers;
6141 seg < filedata->program_headers + filedata->file_header.e_phnum;
6142 ++seg)
6144 if (seg->p_type != PT_LOAD)
6145 continue;
6147 if (vma >= (seg->p_vaddr & -seg->p_align)
6148 && vma + size <= seg->p_vaddr + seg->p_filesz)
6149 return vma - seg->p_vaddr + seg->p_offset;
6152 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
6153 (unsigned long) vma);
6154 return (long) vma;
6158 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6159 If PROBE is true, this is just a probe and we do not generate any error
6160 messages if the load fails. */
6162 static bool
6163 get_32bit_section_headers (Filedata * filedata, bool probe)
6165 Elf32_External_Shdr * shdrs;
6166 Elf_Internal_Shdr * internal;
6167 unsigned int i;
6168 unsigned int size = filedata->file_header.e_shentsize;
6169 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6171 /* PR binutils/17531: Cope with unexpected section header sizes. */
6172 if (size == 0 || num == 0)
6173 return false;
6174 if (size < sizeof * shdrs)
6176 if (! probe)
6177 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6178 return false;
6180 if (!probe && size > sizeof * shdrs)
6181 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6183 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6184 size, num,
6185 probe ? NULL : _("section headers"));
6186 if (shdrs == NULL)
6187 return false;
6189 filedata->section_headers = (Elf_Internal_Shdr *)
6190 cmalloc (num, sizeof (Elf_Internal_Shdr));
6191 if (filedata->section_headers == NULL)
6193 if (!probe)
6194 error (_("Out of memory reading %u section headers\n"), num);
6195 free (shdrs);
6196 return false;
6199 for (i = 0, internal = filedata->section_headers;
6200 i < num;
6201 i++, internal++)
6203 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6204 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6205 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6206 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6207 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6208 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6209 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6210 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6211 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6212 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6213 if (!probe && internal->sh_link > num)
6214 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6215 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6216 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6219 free (shdrs);
6220 return true;
6223 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6225 static bool
6226 get_64bit_section_headers (Filedata * filedata, bool probe)
6228 Elf64_External_Shdr * shdrs;
6229 Elf_Internal_Shdr * internal;
6230 unsigned int i;
6231 unsigned int size = filedata->file_header.e_shentsize;
6232 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6234 /* PR binutils/17531: Cope with unexpected section header sizes. */
6235 if (size == 0 || num == 0)
6236 return false;
6238 if (size < sizeof * shdrs)
6240 if (! probe)
6241 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6242 return false;
6245 if (! probe && size > sizeof * shdrs)
6246 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6248 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6249 filedata->file_header.e_shoff,
6250 size, num,
6251 probe ? NULL : _("section headers"));
6252 if (shdrs == NULL)
6253 return false;
6255 filedata->section_headers = (Elf_Internal_Shdr *)
6256 cmalloc (num, sizeof (Elf_Internal_Shdr));
6257 if (filedata->section_headers == NULL)
6259 if (! probe)
6260 error (_("Out of memory reading %u section headers\n"), num);
6261 free (shdrs);
6262 return false;
6265 for (i = 0, internal = filedata->section_headers;
6266 i < num;
6267 i++, internal++)
6269 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6270 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6271 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6272 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6273 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6274 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6275 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6276 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6277 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6278 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6279 if (!probe && internal->sh_link > num)
6280 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6281 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6282 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6285 free (shdrs);
6286 return true;
6289 static bool
6290 get_section_headers (Filedata *filedata, bool probe)
6292 if (filedata->section_headers != NULL)
6293 return true;
6295 if (is_32bit_elf)
6296 return get_32bit_section_headers (filedata, probe);
6297 else
6298 return get_64bit_section_headers (filedata, probe);
6301 static Elf_Internal_Sym *
6302 get_32bit_elf_symbols (Filedata * filedata,
6303 Elf_Internal_Shdr * section,
6304 unsigned long * num_syms_return)
6306 unsigned long number = 0;
6307 Elf32_External_Sym * esyms = NULL;
6308 Elf_External_Sym_Shndx * shndx = NULL;
6309 Elf_Internal_Sym * isyms = NULL;
6310 Elf_Internal_Sym * psym;
6311 unsigned int j;
6312 elf_section_list * entry;
6314 if (section->sh_size == 0)
6316 if (num_syms_return != NULL)
6317 * num_syms_return = 0;
6318 return NULL;
6321 /* Run some sanity checks first. */
6322 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6324 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6325 printable_section_name (filedata, section),
6326 (unsigned long) section->sh_entsize);
6327 goto exit_point;
6330 if (section->sh_size > filedata->file_size)
6332 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6333 printable_section_name (filedata, section),
6334 (unsigned long) section->sh_size);
6335 goto exit_point;
6338 number = section->sh_size / section->sh_entsize;
6340 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6342 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6343 (unsigned long) section->sh_size,
6344 printable_section_name (filedata, section),
6345 (unsigned long) section->sh_entsize);
6346 goto exit_point;
6349 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6350 section->sh_size, _("symbols"));
6351 if (esyms == NULL)
6352 goto exit_point;
6354 shndx = NULL;
6355 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6357 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6358 continue;
6360 if (shndx != NULL)
6362 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6363 free (shndx);
6366 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6367 entry->hdr->sh_offset,
6368 1, entry->hdr->sh_size,
6369 _("symbol table section indices"));
6370 if (shndx == NULL)
6371 goto exit_point;
6373 /* PR17531: file: heap-buffer-overflow */
6374 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6376 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6377 printable_section_name (filedata, entry->hdr),
6378 (unsigned long) entry->hdr->sh_size,
6379 (unsigned long) section->sh_size);
6380 goto exit_point;
6384 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6386 if (isyms == NULL)
6388 error (_("Out of memory reading %lu symbols\n"),
6389 (unsigned long) number);
6390 goto exit_point;
6393 for (j = 0, psym = isyms; j < number; j++, psym++)
6395 psym->st_name = BYTE_GET (esyms[j].st_name);
6396 psym->st_value = BYTE_GET (esyms[j].st_value);
6397 psym->st_size = BYTE_GET (esyms[j].st_size);
6398 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6399 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6400 psym->st_shndx
6401 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6402 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6403 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6404 psym->st_info = BYTE_GET (esyms[j].st_info);
6405 psym->st_other = BYTE_GET (esyms[j].st_other);
6408 exit_point:
6409 free (shndx);
6410 free (esyms);
6412 if (num_syms_return != NULL)
6413 * num_syms_return = isyms == NULL ? 0 : number;
6415 return isyms;
6418 static Elf_Internal_Sym *
6419 get_64bit_elf_symbols (Filedata * filedata,
6420 Elf_Internal_Shdr * section,
6421 unsigned long * num_syms_return)
6423 unsigned long number = 0;
6424 Elf64_External_Sym * esyms = NULL;
6425 Elf_External_Sym_Shndx * shndx = NULL;
6426 Elf_Internal_Sym * isyms = NULL;
6427 Elf_Internal_Sym * psym;
6428 unsigned int j;
6429 elf_section_list * entry;
6431 if (section->sh_size == 0)
6433 if (num_syms_return != NULL)
6434 * num_syms_return = 0;
6435 return NULL;
6438 /* Run some sanity checks first. */
6439 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6441 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6442 printable_section_name (filedata, section),
6443 (unsigned long) section->sh_entsize);
6444 goto exit_point;
6447 if (section->sh_size > filedata->file_size)
6449 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6450 printable_section_name (filedata, section),
6451 (unsigned long) section->sh_size);
6452 goto exit_point;
6455 number = section->sh_size / section->sh_entsize;
6457 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6459 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6460 (unsigned long) section->sh_size,
6461 printable_section_name (filedata, section),
6462 (unsigned long) section->sh_entsize);
6463 goto exit_point;
6466 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6467 section->sh_size, _("symbols"));
6468 if (!esyms)
6469 goto exit_point;
6471 shndx = NULL;
6472 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6474 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6475 continue;
6477 if (shndx != NULL)
6479 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6480 free (shndx);
6483 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6484 entry->hdr->sh_offset,
6485 1, entry->hdr->sh_size,
6486 _("symbol table section indices"));
6487 if (shndx == NULL)
6488 goto exit_point;
6490 /* PR17531: file: heap-buffer-overflow */
6491 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6493 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6494 printable_section_name (filedata, entry->hdr),
6495 (unsigned long) entry->hdr->sh_size,
6496 (unsigned long) section->sh_size);
6497 goto exit_point;
6501 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6503 if (isyms == NULL)
6505 error (_("Out of memory reading %lu symbols\n"),
6506 (unsigned long) number);
6507 goto exit_point;
6510 for (j = 0, psym = isyms; j < number; j++, psym++)
6512 psym->st_name = BYTE_GET (esyms[j].st_name);
6513 psym->st_info = BYTE_GET (esyms[j].st_info);
6514 psym->st_other = BYTE_GET (esyms[j].st_other);
6515 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6517 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6518 psym->st_shndx
6519 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6520 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6521 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6523 psym->st_value = BYTE_GET (esyms[j].st_value);
6524 psym->st_size = BYTE_GET (esyms[j].st_size);
6527 exit_point:
6528 free (shndx);
6529 free (esyms);
6531 if (num_syms_return != NULL)
6532 * num_syms_return = isyms == NULL ? 0 : number;
6534 return isyms;
6537 static Elf_Internal_Sym *
6538 get_elf_symbols (Filedata *filedata,
6539 Elf_Internal_Shdr *section,
6540 unsigned long *num_syms_return)
6542 if (is_32bit_elf)
6543 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6544 else
6545 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6548 static const char *
6549 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6551 static char buff[1024];
6552 char * p = buff;
6553 unsigned int field_size = is_32bit_elf ? 8 : 16;
6554 signed int sindex;
6555 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6556 bfd_vma os_flags = 0;
6557 bfd_vma proc_flags = 0;
6558 bfd_vma unknown_flags = 0;
6559 static const struct
6561 const char * str;
6562 unsigned int len;
6564 flags [] =
6566 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6567 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6568 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6569 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6570 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6571 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6572 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6573 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6574 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6575 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6576 /* IA-64 specific. */
6577 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6578 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6579 /* IA-64 OpenVMS specific. */
6580 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6581 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6582 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6583 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6584 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6585 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6586 /* Generic. */
6587 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6588 /* SPARC specific. */
6589 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6590 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6591 /* ARM specific. */
6592 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6593 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6594 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6595 /* GNU specific. */
6596 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6597 /* VLE specific. */
6598 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6599 /* GNU specific. */
6600 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6603 if (do_section_details)
6605 sprintf (buff, "[%*.*lx]: ",
6606 field_size, field_size, (unsigned long) sh_flags);
6607 p += field_size + 4;
6610 while (sh_flags)
6612 bfd_vma flag;
6614 flag = sh_flags & - sh_flags;
6615 sh_flags &= ~ flag;
6617 if (do_section_details)
6619 switch (flag)
6621 case SHF_WRITE: sindex = 0; break;
6622 case SHF_ALLOC: sindex = 1; break;
6623 case SHF_EXECINSTR: sindex = 2; break;
6624 case SHF_MERGE: sindex = 3; break;
6625 case SHF_STRINGS: sindex = 4; break;
6626 case SHF_INFO_LINK: sindex = 5; break;
6627 case SHF_LINK_ORDER: sindex = 6; break;
6628 case SHF_OS_NONCONFORMING: sindex = 7; break;
6629 case SHF_GROUP: sindex = 8; break;
6630 case SHF_TLS: sindex = 9; break;
6631 case SHF_EXCLUDE: sindex = 18; break;
6632 case SHF_COMPRESSED: sindex = 20; break;
6634 default:
6635 sindex = -1;
6636 switch (filedata->file_header.e_machine)
6638 case EM_IA_64:
6639 if (flag == SHF_IA_64_SHORT)
6640 sindex = 10;
6641 else if (flag == SHF_IA_64_NORECOV)
6642 sindex = 11;
6643 #ifdef BFD64
6644 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6645 switch (flag)
6647 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6648 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6649 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6650 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6651 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6652 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6653 default: break;
6655 #endif
6656 break;
6658 case EM_386:
6659 case EM_IAMCU:
6660 case EM_X86_64:
6661 case EM_L1OM:
6662 case EM_K1OM:
6663 case EM_OLD_SPARCV9:
6664 case EM_SPARC32PLUS:
6665 case EM_SPARCV9:
6666 case EM_SPARC:
6667 if (flag == SHF_ORDERED)
6668 sindex = 19;
6669 break;
6671 case EM_ARM:
6672 switch (flag)
6674 case SHF_ENTRYSECT: sindex = 21; break;
6675 case SHF_ARM_PURECODE: sindex = 22; break;
6676 case SHF_COMDEF: sindex = 23; break;
6677 default: break;
6679 break;
6680 case EM_PPC:
6681 if (flag == SHF_PPC_VLE)
6682 sindex = 25;
6683 break;
6684 default:
6685 break;
6688 switch (filedata->file_header.e_ident[EI_OSABI])
6690 case ELFOSABI_GNU:
6691 case ELFOSABI_FREEBSD:
6692 if (flag == SHF_GNU_RETAIN)
6693 sindex = 26;
6694 /* Fall through */
6695 case ELFOSABI_NONE:
6696 if (flag == SHF_GNU_MBIND)
6697 /* We should not recognize SHF_GNU_MBIND for
6698 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6699 not set the EI_OSABI header byte. */
6700 sindex = 24;
6701 break;
6702 default:
6703 break;
6705 break;
6708 if (sindex != -1)
6710 if (p != buff + field_size + 4)
6712 if (size < (10 + 2))
6714 warn (_("Internal error: not enough buffer room for section flag info"));
6715 return _("<unknown>");
6717 size -= 2;
6718 *p++ = ',';
6719 *p++ = ' ';
6722 size -= flags [sindex].len;
6723 p = stpcpy (p, flags [sindex].str);
6725 else if (flag & SHF_MASKOS)
6726 os_flags |= flag;
6727 else if (flag & SHF_MASKPROC)
6728 proc_flags |= flag;
6729 else
6730 unknown_flags |= flag;
6732 else
6734 switch (flag)
6736 case SHF_WRITE: *p = 'W'; break;
6737 case SHF_ALLOC: *p = 'A'; break;
6738 case SHF_EXECINSTR: *p = 'X'; break;
6739 case SHF_MERGE: *p = 'M'; break;
6740 case SHF_STRINGS: *p = 'S'; break;
6741 case SHF_INFO_LINK: *p = 'I'; break;
6742 case SHF_LINK_ORDER: *p = 'L'; break;
6743 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6744 case SHF_GROUP: *p = 'G'; break;
6745 case SHF_TLS: *p = 'T'; break;
6746 case SHF_EXCLUDE: *p = 'E'; break;
6747 case SHF_COMPRESSED: *p = 'C'; break;
6749 default:
6750 if ((filedata->file_header.e_machine == EM_X86_64
6751 || filedata->file_header.e_machine == EM_L1OM
6752 || filedata->file_header.e_machine == EM_K1OM)
6753 && flag == SHF_X86_64_LARGE)
6754 *p = 'l';
6755 else if (filedata->file_header.e_machine == EM_ARM
6756 && flag == SHF_ARM_PURECODE)
6757 *p = 'y';
6758 else if (filedata->file_header.e_machine == EM_PPC
6759 && flag == SHF_PPC_VLE)
6760 *p = 'v';
6761 else if (flag & SHF_MASKOS)
6763 switch (filedata->file_header.e_ident[EI_OSABI])
6765 case ELFOSABI_GNU:
6766 case ELFOSABI_FREEBSD:
6767 if (flag == SHF_GNU_RETAIN)
6769 *p = 'R';
6770 break;
6772 /* Fall through */
6773 case ELFOSABI_NONE:
6774 if (flag == SHF_GNU_MBIND)
6776 /* We should not recognize SHF_GNU_MBIND for
6777 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6778 not set the EI_OSABI header byte. */
6779 *p = 'D';
6780 break;
6782 /* Fall through */
6783 default:
6784 *p = 'o';
6785 sh_flags &= ~SHF_MASKOS;
6786 break;
6789 else if (flag & SHF_MASKPROC)
6791 *p = 'p';
6792 sh_flags &= ~ SHF_MASKPROC;
6794 else
6795 *p = 'x';
6796 break;
6798 p++;
6802 if (do_section_details)
6804 if (os_flags)
6806 size -= 5 + field_size;
6807 if (p != buff + field_size + 4)
6809 if (size < (2 + 1))
6811 warn (_("Internal error: not enough buffer room for section flag info"));
6812 return _("<unknown>");
6814 size -= 2;
6815 *p++ = ',';
6816 *p++ = ' ';
6818 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6819 (unsigned long) os_flags);
6820 p += 5 + field_size;
6822 if (proc_flags)
6824 size -= 7 + field_size;
6825 if (p != buff + field_size + 4)
6827 if (size < (2 + 1))
6829 warn (_("Internal error: not enough buffer room for section flag info"));
6830 return _("<unknown>");
6832 size -= 2;
6833 *p++ = ',';
6834 *p++ = ' ';
6836 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6837 (unsigned long) proc_flags);
6838 p += 7 + field_size;
6840 if (unknown_flags)
6842 size -= 10 + field_size;
6843 if (p != buff + field_size + 4)
6845 if (size < (2 + 1))
6847 warn (_("Internal error: not enough buffer room for section flag info"));
6848 return _("<unknown>");
6850 size -= 2;
6851 *p++ = ',';
6852 *p++ = ' ';
6854 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
6855 (unsigned long) unknown_flags);
6856 p += 10 + field_size;
6860 *p = '\0';
6861 return buff;
6864 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
6865 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
6867 if (is_32bit_elf)
6869 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
6871 if (size < sizeof (* echdr))
6873 error (_("Compressed section is too small even for a compression header\n"));
6874 return 0;
6877 chdr->ch_type = BYTE_GET (echdr->ch_type);
6878 chdr->ch_size = BYTE_GET (echdr->ch_size);
6879 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6880 return sizeof (*echdr);
6882 else
6884 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
6886 if (size < sizeof (* echdr))
6888 error (_("Compressed section is too small even for a compression header\n"));
6889 return 0;
6892 chdr->ch_type = BYTE_GET (echdr->ch_type);
6893 chdr->ch_size = BYTE_GET (echdr->ch_size);
6894 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6895 return sizeof (*echdr);
6899 static bool
6900 process_section_headers (Filedata * filedata)
6902 Elf_Internal_Shdr * section;
6903 unsigned int i;
6905 if (filedata->file_header.e_shnum == 0)
6907 /* PR binutils/12467. */
6908 if (filedata->file_header.e_shoff != 0)
6910 warn (_("possibly corrupt ELF file header - it has a non-zero"
6911 " section header offset, but no section headers\n"));
6912 return false;
6914 else if (do_sections)
6915 printf (_("\nThere are no sections in this file.\n"));
6917 return true;
6920 if (do_sections && !do_header)
6922 if (filedata->is_separate && process_links)
6923 printf (_("In linked file '%s': "), filedata->file_name);
6924 if (! filedata->is_separate || process_links)
6925 printf (ngettext ("There is %d section header, "
6926 "starting at offset 0x%lx:\n",
6927 "There are %d section headers, "
6928 "starting at offset 0x%lx:\n",
6929 filedata->file_header.e_shnum),
6930 filedata->file_header.e_shnum,
6931 (unsigned long) filedata->file_header.e_shoff);
6934 if (!get_section_headers (filedata, false))
6935 return false;
6937 /* Read in the string table, so that we have names to display. */
6938 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6939 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6941 section = filedata->section_headers + filedata->file_header.e_shstrndx;
6943 if (section->sh_size != 0)
6945 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6946 1, section->sh_size,
6947 _("string table"));
6949 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6953 /* Scan the sections for the dynamic symbol table
6954 and dynamic string table and debug sections. */
6955 eh_addr_size = is_32bit_elf ? 4 : 8;
6956 switch (filedata->file_header.e_machine)
6958 case EM_MIPS:
6959 case EM_MIPS_RS3_LE:
6960 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6961 FDE addresses. However, the ABI also has a semi-official ILP32
6962 variant for which the normal FDE address size rules apply.
6964 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6965 section, where XX is the size of longs in bits. Unfortunately,
6966 earlier compilers provided no way of distinguishing ILP32 objects
6967 from LP64 objects, so if there's any doubt, we should assume that
6968 the official LP64 form is being used. */
6969 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6970 && find_section (filedata, ".gcc_compiled_long32") == NULL)
6971 eh_addr_size = 8;
6972 break;
6974 case EM_H8_300:
6975 case EM_H8_300H:
6976 switch (filedata->file_header.e_flags & EF_H8_MACH)
6978 case E_H8_MACH_H8300:
6979 case E_H8_MACH_H8300HN:
6980 case E_H8_MACH_H8300SN:
6981 case E_H8_MACH_H8300SXN:
6982 eh_addr_size = 2;
6983 break;
6984 case E_H8_MACH_H8300H:
6985 case E_H8_MACH_H8300S:
6986 case E_H8_MACH_H8300SX:
6987 eh_addr_size = 4;
6988 break;
6990 break;
6992 case EM_M32C_OLD:
6993 case EM_M32C:
6994 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6996 case EF_M32C_CPU_M16C:
6997 eh_addr_size = 2;
6998 break;
7000 break;
7003 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7004 do \
7006 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
7007 if (section->sh_entsize != expected_entsize) \
7009 char buf[40]; \
7010 sprintf_vma (buf, section->sh_entsize); \
7011 /* Note: coded this way so that there is a single string for \
7012 translation. */ \
7013 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
7014 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
7015 (unsigned) expected_entsize); \
7016 section->sh_entsize = expected_entsize; \
7019 while (0)
7021 #define CHECK_ENTSIZE(section, i, type) \
7022 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7023 sizeof (Elf64_External_##type))
7025 for (i = 0, section = filedata->section_headers;
7026 i < filedata->file_header.e_shnum;
7027 i++, section++)
7029 const char *name = section_name_print (filedata, section);
7031 /* Run some sanity checks on the headers and
7032 possibly fill in some file data as well. */
7033 switch (section->sh_type)
7035 case SHT_DYNSYM:
7036 if (filedata->dynamic_symbols != NULL)
7038 error (_("File contains multiple dynamic symbol tables\n"));
7039 continue;
7042 CHECK_ENTSIZE (section, i, Sym);
7043 filedata->dynamic_symbols
7044 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7045 filedata->dynamic_symtab_section = section;
7046 break;
7048 case SHT_STRTAB:
7049 if (streq (name, ".dynstr"))
7051 if (filedata->dynamic_strings != NULL)
7053 error (_("File contains multiple dynamic string tables\n"));
7054 continue;
7057 filedata->dynamic_strings
7058 = (char *) get_data (NULL, filedata, section->sh_offset,
7059 1, section->sh_size, _("dynamic strings"));
7060 filedata->dynamic_strings_length
7061 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7062 filedata->dynamic_strtab_section = section;
7064 break;
7066 case SHT_SYMTAB_SHNDX:
7068 elf_section_list * entry = xmalloc (sizeof * entry);
7070 entry->hdr = section;
7071 entry->next = filedata->symtab_shndx_list;
7072 filedata->symtab_shndx_list = entry;
7074 break;
7076 case SHT_SYMTAB:
7077 CHECK_ENTSIZE (section, i, Sym);
7078 break;
7080 case SHT_GROUP:
7081 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7082 break;
7084 case SHT_REL:
7085 CHECK_ENTSIZE (section, i, Rel);
7086 if (do_checks && section->sh_size == 0)
7087 warn (_("Section '%s': zero-sized relocation section\n"), name);
7088 break;
7090 case SHT_RELA:
7091 CHECK_ENTSIZE (section, i, Rela);
7092 if (do_checks && section->sh_size == 0)
7093 warn (_("Section '%s': zero-sized relocation section\n"), name);
7094 break;
7096 case SHT_RELR:
7097 CHECK_ENTSIZE (section, i, Relr);
7098 break;
7100 case SHT_NOTE:
7101 case SHT_PROGBITS:
7102 /* Having a zero sized section is not illegal according to the
7103 ELF standard, but it might be an indication that something
7104 is wrong. So issue a warning if we are running in lint mode. */
7105 if (do_checks && section->sh_size == 0)
7106 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7107 break;
7109 default:
7110 break;
7113 if ((do_debugging || do_debug_info || do_debug_abbrevs
7114 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7115 || do_debug_aranges || do_debug_frames || do_debug_macinfo
7116 || do_debug_str || do_debug_str_offsets || do_debug_loc
7117 || do_debug_ranges
7118 || do_debug_addr || do_debug_cu_index || do_debug_links)
7119 && (startswith (name, ".debug_")
7120 || startswith (name, ".zdebug_")))
7122 if (name[1] == 'z')
7123 name += sizeof (".zdebug_") - 1;
7124 else
7125 name += sizeof (".debug_") - 1;
7127 if (do_debugging
7128 || (do_debug_info && startswith (name, "info"))
7129 || (do_debug_info && startswith (name, "types"))
7130 || (do_debug_abbrevs && startswith (name, "abbrev"))
7131 || (do_debug_lines && strcmp (name, "line") == 0)
7132 || (do_debug_lines && startswith (name, "line."))
7133 || (do_debug_pubnames && startswith (name, "pubnames"))
7134 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7135 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7136 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7137 || (do_debug_aranges && startswith (name, "aranges"))
7138 || (do_debug_ranges && startswith (name, "ranges"))
7139 || (do_debug_ranges && startswith (name, "rnglists"))
7140 || (do_debug_frames && startswith (name, "frame"))
7141 || (do_debug_macinfo && startswith (name, "macinfo"))
7142 || (do_debug_macinfo && startswith (name, "macro"))
7143 || (do_debug_str && startswith (name, "str"))
7144 || (do_debug_links && startswith (name, "sup"))
7145 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7146 || (do_debug_loc && startswith (name, "loc"))
7147 || (do_debug_loc && startswith (name, "loclists"))
7148 || (do_debug_addr && startswith (name, "addr"))
7149 || (do_debug_cu_index && startswith (name, "cu_index"))
7150 || (do_debug_cu_index && startswith (name, "tu_index"))
7152 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7154 /* Linkonce section to be combined with .debug_info at link time. */
7155 else if ((do_debugging || do_debug_info)
7156 && startswith (name, ".gnu.linkonce.wi."))
7157 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7158 else if (do_debug_frames && streq (name, ".eh_frame"))
7159 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7160 else if (do_gdb_index && (streq (name, ".gdb_index")
7161 || streq (name, ".debug_names")))
7162 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7163 /* Trace sections for Itanium VMS. */
7164 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7165 || do_trace_aranges)
7166 && startswith (name, ".trace_"))
7168 name += sizeof (".trace_") - 1;
7170 if (do_debugging
7171 || (do_trace_info && streq (name, "info"))
7172 || (do_trace_abbrevs && streq (name, "abbrev"))
7173 || (do_trace_aranges && streq (name, "aranges"))
7175 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7177 else if ((do_debugging || do_debug_links)
7178 && (startswith (name, ".gnu_debuglink")
7179 || startswith (name, ".gnu_debugaltlink")))
7180 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7183 if (! do_sections)
7184 return true;
7186 if (filedata->is_separate && ! process_links)
7187 return true;
7189 if (filedata->is_separate)
7190 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7191 else if (filedata->file_header.e_shnum > 1)
7192 printf (_("\nSection Headers:\n"));
7193 else
7194 printf (_("\nSection Header:\n"));
7196 if (is_32bit_elf)
7198 if (do_section_details)
7200 printf (_(" [Nr] Name\n"));
7201 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
7203 else
7204 printf
7205 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7207 else if (do_wide)
7209 if (do_section_details)
7211 printf (_(" [Nr] Name\n"));
7212 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
7214 else
7215 printf
7216 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7218 else
7220 if (do_section_details)
7222 printf (_(" [Nr] Name\n"));
7223 printf (_(" Type Address Offset Link\n"));
7224 printf (_(" Size EntSize Info Align\n"));
7226 else
7228 printf (_(" [Nr] Name Type Address Offset\n"));
7229 printf (_(" Size EntSize Flags Link Info Align\n"));
7233 if (do_section_details)
7234 printf (_(" Flags\n"));
7236 for (i = 0, section = filedata->section_headers;
7237 i < filedata->file_header.e_shnum;
7238 i++, section++)
7240 /* Run some sanity checks on the section header. */
7242 /* Check the sh_link field. */
7243 switch (section->sh_type)
7245 case SHT_REL:
7246 case SHT_RELA:
7247 if (section->sh_link == 0
7248 && (filedata->file_header.e_type == ET_EXEC
7249 || filedata->file_header.e_type == ET_DYN))
7250 /* A dynamic relocation section where all entries use a
7251 zero symbol index need not specify a symtab section. */
7252 break;
7253 /* Fall through. */
7254 case SHT_SYMTAB_SHNDX:
7255 case SHT_GROUP:
7256 case SHT_HASH:
7257 case SHT_GNU_HASH:
7258 case SHT_GNU_versym:
7259 if (section->sh_link == 0
7260 || section->sh_link >= filedata->file_header.e_shnum
7261 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7262 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7263 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7264 i, section->sh_link);
7265 break;
7267 case SHT_DYNAMIC:
7268 case SHT_SYMTAB:
7269 case SHT_DYNSYM:
7270 case SHT_GNU_verneed:
7271 case SHT_GNU_verdef:
7272 case SHT_GNU_LIBLIST:
7273 if (section->sh_link == 0
7274 || section->sh_link >= filedata->file_header.e_shnum
7275 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
7276 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7277 i, section->sh_link);
7278 break;
7280 case SHT_INIT_ARRAY:
7281 case SHT_FINI_ARRAY:
7282 case SHT_PREINIT_ARRAY:
7283 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7284 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7285 i, section->sh_link);
7286 break;
7288 default:
7289 /* FIXME: Add support for target specific section types. */
7290 #if 0 /* Currently we do not check other section types as there are too
7291 many special cases. Stab sections for example have a type
7292 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7293 section. */
7294 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7295 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7296 i, section->sh_link);
7297 #endif
7298 break;
7301 /* Check the sh_info field. */
7302 switch (section->sh_type)
7304 case SHT_REL:
7305 case SHT_RELA:
7306 if (section->sh_info == 0
7307 && (filedata->file_header.e_type == ET_EXEC
7308 || filedata->file_header.e_type == ET_DYN))
7309 /* Dynamic relocations apply to segments, so they do not
7310 need to specify the section they relocate. */
7311 break;
7312 if (section->sh_info == 0
7313 || section->sh_info >= filedata->file_header.e_shnum
7314 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7315 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7316 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7317 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7318 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7319 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7320 /* FIXME: Are other section types valid ? */
7321 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7322 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7323 i, section->sh_info);
7324 break;
7326 case SHT_DYNAMIC:
7327 case SHT_HASH:
7328 case SHT_SYMTAB_SHNDX:
7329 case SHT_INIT_ARRAY:
7330 case SHT_FINI_ARRAY:
7331 case SHT_PREINIT_ARRAY:
7332 if (section->sh_info != 0)
7333 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7334 i, section->sh_info);
7335 break;
7337 case SHT_GROUP:
7338 case SHT_SYMTAB:
7339 case SHT_DYNSYM:
7340 /* A symbol index - we assume that it is valid. */
7341 break;
7343 default:
7344 /* FIXME: Add support for target specific section types. */
7345 if (section->sh_type == SHT_NOBITS)
7346 /* NOBITS section headers with non-zero sh_info fields can be
7347 created when a binary is stripped of everything but its debug
7348 information. The stripped sections have their headers
7349 preserved but their types set to SHT_NOBITS. So do not check
7350 this type of section. */
7352 else if (section->sh_flags & SHF_INFO_LINK)
7354 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7355 warn (_("[%2u]: Expected link to another section in info field"), i);
7357 else if (section->sh_type < SHT_LOOS
7358 && (section->sh_flags & SHF_GNU_MBIND) == 0
7359 && section->sh_info != 0)
7360 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7361 i, section->sh_info);
7362 break;
7365 /* Check the sh_size field. */
7366 if (section->sh_size > filedata->file_size
7367 && section->sh_type != SHT_NOBITS
7368 && section->sh_type != SHT_NULL
7369 && section->sh_type < SHT_LOOS)
7370 warn (_("Size of section %u is larger than the entire file!\n"), i);
7372 printf (" [%2u] ", i);
7373 if (do_section_details)
7374 printf ("%s\n ", printable_section_name (filedata, section));
7375 else
7376 print_symbol (-17, section_name_print (filedata, section));
7378 printf (do_wide ? " %-15s " : " %-15.15s ",
7379 get_section_type_name (filedata, section->sh_type));
7381 if (is_32bit_elf)
7383 const char * link_too_big = NULL;
7385 print_vma (section->sh_addr, LONG_HEX);
7387 printf ( " %6.6lx %6.6lx %2.2lx",
7388 (unsigned long) section->sh_offset,
7389 (unsigned long) section->sh_size,
7390 (unsigned long) section->sh_entsize);
7392 if (do_section_details)
7393 fputs (" ", stdout);
7394 else
7395 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7397 if (section->sh_link >= filedata->file_header.e_shnum)
7399 link_too_big = "";
7400 /* The sh_link value is out of range. Normally this indicates
7401 an error but it can have special values in Solaris binaries. */
7402 switch (filedata->file_header.e_machine)
7404 case EM_386:
7405 case EM_IAMCU:
7406 case EM_X86_64:
7407 case EM_L1OM:
7408 case EM_K1OM:
7409 case EM_OLD_SPARCV9:
7410 case EM_SPARC32PLUS:
7411 case EM_SPARCV9:
7412 case EM_SPARC:
7413 if (section->sh_link == (SHN_BEFORE & 0xffff))
7414 link_too_big = "BEFORE";
7415 else if (section->sh_link == (SHN_AFTER & 0xffff))
7416 link_too_big = "AFTER";
7417 break;
7418 default:
7419 break;
7423 if (do_section_details)
7425 if (link_too_big != NULL && * link_too_big)
7426 printf ("<%s> ", link_too_big);
7427 else
7428 printf ("%2u ", section->sh_link);
7429 printf ("%3u %2lu\n", section->sh_info,
7430 (unsigned long) section->sh_addralign);
7432 else
7433 printf ("%2u %3u %2lu\n",
7434 section->sh_link,
7435 section->sh_info,
7436 (unsigned long) section->sh_addralign);
7438 if (link_too_big && ! * link_too_big)
7439 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7440 i, section->sh_link);
7442 else if (do_wide)
7444 print_vma (section->sh_addr, LONG_HEX);
7446 if ((long) section->sh_offset == section->sh_offset)
7447 printf (" %6.6lx", (unsigned long) section->sh_offset);
7448 else
7450 putchar (' ');
7451 print_vma (section->sh_offset, LONG_HEX);
7454 if ((unsigned long) section->sh_size == section->sh_size)
7455 printf (" %6.6lx", (unsigned long) section->sh_size);
7456 else
7458 putchar (' ');
7459 print_vma (section->sh_size, LONG_HEX);
7462 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7463 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7464 else
7466 putchar (' ');
7467 print_vma (section->sh_entsize, LONG_HEX);
7470 if (do_section_details)
7471 fputs (" ", stdout);
7472 else
7473 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7475 printf ("%2u %3u ", section->sh_link, section->sh_info);
7477 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7478 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7479 else
7481 print_vma (section->sh_addralign, DEC);
7482 putchar ('\n');
7485 else if (do_section_details)
7487 putchar (' ');
7488 print_vma (section->sh_addr, LONG_HEX);
7489 if ((long) section->sh_offset == section->sh_offset)
7490 printf (" %16.16lx", (unsigned long) section->sh_offset);
7491 else
7493 printf (" ");
7494 print_vma (section->sh_offset, LONG_HEX);
7496 printf (" %u\n ", section->sh_link);
7497 print_vma (section->sh_size, LONG_HEX);
7498 putchar (' ');
7499 print_vma (section->sh_entsize, LONG_HEX);
7501 printf (" %-16u %lu\n",
7502 section->sh_info,
7503 (unsigned long) section->sh_addralign);
7505 else
7507 putchar (' ');
7508 print_vma (section->sh_addr, LONG_HEX);
7509 if ((long) section->sh_offset == section->sh_offset)
7510 printf (" %8.8lx", (unsigned long) section->sh_offset);
7511 else
7513 printf (" ");
7514 print_vma (section->sh_offset, LONG_HEX);
7516 printf ("\n ");
7517 print_vma (section->sh_size, LONG_HEX);
7518 printf (" ");
7519 print_vma (section->sh_entsize, LONG_HEX);
7521 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7523 printf (" %2u %3u %lu\n",
7524 section->sh_link,
7525 section->sh_info,
7526 (unsigned long) section->sh_addralign);
7529 if (do_section_details)
7531 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7532 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7534 /* Minimum section size is 12 bytes for 32-bit compression
7535 header + 12 bytes for compressed data header. */
7536 unsigned char buf[24];
7538 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7539 if (get_data (&buf, filedata, section->sh_offset, 1,
7540 sizeof (buf), _("compression header")))
7542 Elf_Internal_Chdr chdr;
7544 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7545 printf (_(" [<corrupt>]\n"));
7546 else
7548 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7549 printf (" ZLIB, ");
7550 else
7551 printf (_(" [<unknown>: 0x%x], "),
7552 chdr.ch_type);
7553 print_vma (chdr.ch_size, LONG_HEX);
7554 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7561 if (!do_section_details)
7563 /* The ordering of the letters shown here matches the ordering of the
7564 corresponding SHF_xxx values, and hence the order in which these
7565 letters will be displayed to the user. */
7566 printf (_("Key to Flags:\n\
7567 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7568 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7569 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7570 switch (filedata->file_header.e_ident[EI_OSABI])
7572 case ELFOSABI_GNU:
7573 case ELFOSABI_FREEBSD:
7574 printf (_("R (retain), "));
7575 /* Fall through */
7576 case ELFOSABI_NONE:
7577 printf (_("D (mbind), "));
7578 break;
7579 default:
7580 break;
7582 if (filedata->file_header.e_machine == EM_X86_64
7583 || filedata->file_header.e_machine == EM_L1OM
7584 || filedata->file_header.e_machine == EM_K1OM)
7585 printf (_("l (large), "));
7586 else if (filedata->file_header.e_machine == EM_ARM)
7587 printf (_("y (purecode), "));
7588 else if (filedata->file_header.e_machine == EM_PPC)
7589 printf (_("v (VLE), "));
7590 printf ("p (processor specific)\n");
7593 return true;
7596 static bool
7597 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7598 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7599 char **strtab, unsigned long *strtablen)
7601 *strtab = NULL;
7602 *strtablen = 0;
7603 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7605 if (*symtab == NULL)
7606 return false;
7608 if (symsec->sh_link != 0)
7610 Elf_Internal_Shdr *strsec;
7612 if (symsec->sh_link >= filedata->file_header.e_shnum)
7614 error (_("Bad sh_link in symbol table section\n"));
7615 free (*symtab);
7616 *symtab = NULL;
7617 *nsyms = 0;
7618 return false;
7621 strsec = filedata->section_headers + symsec->sh_link;
7623 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7624 1, strsec->sh_size, _("string table"));
7625 if (*strtab == NULL)
7627 free (*symtab);
7628 *symtab = NULL;
7629 *nsyms = 0;
7630 return false;
7632 *strtablen = strsec->sh_size;
7634 return true;
7637 static const char *
7638 get_group_flags (unsigned int flags)
7640 static char buff[128];
7642 if (flags == 0)
7643 return "";
7644 else if (flags == GRP_COMDAT)
7645 return "COMDAT ";
7647 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7648 flags,
7649 flags & GRP_MASKOS ? _("<OS specific>") : "",
7650 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7651 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7652 ? _("<unknown>") : ""));
7654 return buff;
7657 static bool
7658 process_section_groups (Filedata * filedata)
7660 Elf_Internal_Shdr * section;
7661 unsigned int i;
7662 struct group * group;
7663 Elf_Internal_Shdr * symtab_sec;
7664 Elf_Internal_Shdr * strtab_sec;
7665 Elf_Internal_Sym * symtab;
7666 unsigned long num_syms;
7667 char * strtab;
7668 size_t strtab_size;
7670 /* Don't process section groups unless needed. */
7671 if (!do_unwind && !do_section_groups)
7672 return true;
7674 if (filedata->file_header.e_shnum == 0)
7676 if (do_section_groups)
7678 if (filedata->is_separate)
7679 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7680 filedata->file_name);
7681 else
7682 printf (_("\nThere are no section groups in this file.\n"));
7684 return true;
7687 if (filedata->section_headers == NULL)
7689 error (_("Section headers are not available!\n"));
7690 /* PR 13622: This can happen with a corrupt ELF header. */
7691 return false;
7694 filedata->section_headers_groups
7695 = (struct group **) calloc (filedata->file_header.e_shnum,
7696 sizeof (struct group *));
7698 if (filedata->section_headers_groups == NULL)
7700 error (_("Out of memory reading %u section group headers\n"),
7701 filedata->file_header.e_shnum);
7702 return false;
7705 /* Scan the sections for the group section. */
7706 filedata->group_count = 0;
7707 for (i = 0, section = filedata->section_headers;
7708 i < filedata->file_header.e_shnum;
7709 i++, section++)
7710 if (section->sh_type == SHT_GROUP)
7711 filedata->group_count++;
7713 if (filedata->group_count == 0)
7715 if (do_section_groups)
7717 if (filedata->is_separate)
7718 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7719 filedata->file_name);
7720 else
7721 printf (_("\nThere are no section groups in this file.\n"));
7724 return true;
7727 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7728 sizeof (struct group));
7730 if (filedata->section_groups == NULL)
7732 error (_("Out of memory reading %lu groups\n"),
7733 (unsigned long) filedata->group_count);
7734 return false;
7737 symtab_sec = NULL;
7738 strtab_sec = NULL;
7739 symtab = NULL;
7740 num_syms = 0;
7741 strtab = NULL;
7742 strtab_size = 0;
7744 if (filedata->is_separate)
7745 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7747 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7748 i < filedata->file_header.e_shnum;
7749 i++, section++)
7751 if (section->sh_type == SHT_GROUP)
7753 const char * name = printable_section_name (filedata, section);
7754 const char * group_name;
7755 unsigned char * start;
7756 unsigned char * indices;
7757 unsigned int entry, j, size;
7758 Elf_Internal_Shdr * sec;
7759 Elf_Internal_Sym * sym;
7761 /* Get the symbol table. */
7762 if (section->sh_link >= filedata->file_header.e_shnum
7763 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7764 != SHT_SYMTAB))
7766 error (_("Bad sh_link in group section `%s'\n"), name);
7767 continue;
7770 if (symtab_sec != sec)
7772 symtab_sec = sec;
7773 free (symtab);
7774 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7777 if (symtab == NULL)
7779 error (_("Corrupt header in group section `%s'\n"), name);
7780 continue;
7783 if (section->sh_info >= num_syms)
7785 error (_("Bad sh_info in group section `%s'\n"), name);
7786 continue;
7789 sym = symtab + section->sh_info;
7791 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7793 if (sym->st_shndx == 0
7794 || sym->st_shndx >= filedata->file_header.e_shnum)
7796 error (_("Bad sh_info in group section `%s'\n"), name);
7797 continue;
7800 group_name = section_name_print (filedata,
7801 filedata->section_headers
7802 + sym->st_shndx);
7803 strtab_sec = NULL;
7804 free (strtab);
7805 strtab = NULL;
7806 strtab_size = 0;
7808 else
7810 /* Get the string table. */
7811 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7813 strtab_sec = NULL;
7814 free (strtab);
7815 strtab = NULL;
7816 strtab_size = 0;
7818 else if (strtab_sec
7819 != (sec = filedata->section_headers + symtab_sec->sh_link))
7821 strtab_sec = sec;
7822 free (strtab);
7824 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
7825 1, strtab_sec->sh_size,
7826 _("string table"));
7827 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
7829 group_name = sym->st_name < strtab_size
7830 ? strtab + sym->st_name : _("<corrupt>");
7833 /* PR 17531: file: loop. */
7834 if (section->sh_entsize > section->sh_size)
7836 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
7837 printable_section_name (filedata, section),
7838 (unsigned long) section->sh_entsize,
7839 (unsigned long) section->sh_size);
7840 continue;
7843 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
7844 1, section->sh_size,
7845 _("section data"));
7846 if (start == NULL)
7847 continue;
7849 indices = start;
7850 size = (section->sh_size / section->sh_entsize) - 1;
7851 entry = byte_get (indices, 4);
7852 indices += 4;
7854 if (do_section_groups)
7856 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
7857 get_group_flags (entry), i, name, group_name, size);
7859 printf (_(" [Index] Name\n"));
7862 group->group_index = i;
7864 for (j = 0; j < size; j++)
7866 struct group_list * g;
7868 entry = byte_get (indices, 4);
7869 indices += 4;
7871 if (entry >= filedata->file_header.e_shnum)
7873 static unsigned num_group_errors = 0;
7875 if (num_group_errors ++ < 10)
7877 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
7878 entry, i, filedata->file_header.e_shnum - 1);
7879 if (num_group_errors == 10)
7880 warn (_("Further error messages about overlarge group section indices suppressed\n"));
7882 continue;
7885 if (filedata->section_headers_groups [entry] != NULL)
7887 if (entry)
7889 static unsigned num_errs = 0;
7891 if (num_errs ++ < 10)
7893 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7894 entry, i,
7895 filedata->section_headers_groups [entry]->group_index);
7896 if (num_errs == 10)
7897 warn (_("Further error messages about already contained group sections suppressed\n"));
7899 continue;
7901 else
7903 /* Intel C/C++ compiler may put section 0 in a
7904 section group. We just warn it the first time
7905 and ignore it afterwards. */
7906 static bool warned = false;
7907 if (!warned)
7909 error (_("section 0 in group section [%5u]\n"),
7910 filedata->section_headers_groups [entry]->group_index);
7911 warned = true;
7916 filedata->section_headers_groups [entry] = group;
7918 if (do_section_groups)
7920 sec = filedata->section_headers + entry;
7921 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
7924 g = (struct group_list *) xmalloc (sizeof (struct group_list));
7925 g->section_index = entry;
7926 g->next = group->root;
7927 group->root = g;
7930 free (start);
7932 group++;
7936 free (symtab);
7937 free (strtab);
7938 return true;
7941 /* Data used to display dynamic fixups. */
7943 struct ia64_vms_dynfixup
7945 bfd_vma needed_ident; /* Library ident number. */
7946 bfd_vma needed; /* Index in the dstrtab of the library name. */
7947 bfd_vma fixup_needed; /* Index of the library. */
7948 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7949 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7952 /* Data used to display dynamic relocations. */
7954 struct ia64_vms_dynimgrela
7956 bfd_vma img_rela_cnt; /* Number of relocations. */
7957 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7960 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7961 library). */
7963 static bool
7964 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7965 struct ia64_vms_dynfixup * fixup,
7966 const char * strtab,
7967 unsigned int strtab_sz)
7969 Elf64_External_VMS_IMAGE_FIXUP * imfs;
7970 long i;
7971 const char * lib_name;
7973 imfs = get_data (NULL, filedata,
7974 filedata->dynamic_addr + fixup->fixup_rela_off,
7975 sizeof (*imfs), fixup->fixup_rela_cnt,
7976 _("dynamic section image fixups"));
7977 if (!imfs)
7978 return false;
7980 if (fixup->needed < strtab_sz)
7981 lib_name = strtab + fixup->needed;
7982 else
7984 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7985 (unsigned long) fixup->needed);
7986 lib_name = "???";
7989 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7990 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7991 printf
7992 (_("Seg Offset Type SymVec DataType\n"));
7994 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7996 unsigned int type;
7997 const char *rtype;
7999 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8000 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
8001 type = BYTE_GET (imfs [i].type);
8002 rtype = elf_ia64_reloc_type (type);
8003 if (rtype == NULL)
8004 printf (" 0x%08x ", type);
8005 else
8006 printf (" %-32s ", rtype);
8007 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8008 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8011 free (imfs);
8012 return true;
8015 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8017 static bool
8018 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8020 Elf64_External_VMS_IMAGE_RELA *imrs;
8021 long i;
8023 imrs = get_data (NULL, filedata,
8024 filedata->dynamic_addr + imgrela->img_rela_off,
8025 sizeof (*imrs), imgrela->img_rela_cnt,
8026 _("dynamic section image relocations"));
8027 if (!imrs)
8028 return false;
8030 printf (_("\nImage relocs\n"));
8031 printf
8032 (_("Seg Offset Type Addend Seg Sym Off\n"));
8034 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
8036 unsigned int type;
8037 const char *rtype;
8039 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8040 printf ("%08" BFD_VMA_FMT "x ",
8041 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
8042 type = BYTE_GET (imrs [i].type);
8043 rtype = elf_ia64_reloc_type (type);
8044 if (rtype == NULL)
8045 printf ("0x%08x ", type);
8046 else
8047 printf ("%-31s ", rtype);
8048 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8049 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8050 printf ("%08" BFD_VMA_FMT "x\n",
8051 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
8054 free (imrs);
8055 return true;
8058 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
8060 static bool
8061 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8063 struct ia64_vms_dynfixup fixup;
8064 struct ia64_vms_dynimgrela imgrela;
8065 Elf_Internal_Dyn *entry;
8066 bfd_vma strtab_off = 0;
8067 bfd_vma strtab_sz = 0;
8068 char *strtab = NULL;
8069 bool res = true;
8071 memset (&fixup, 0, sizeof (fixup));
8072 memset (&imgrela, 0, sizeof (imgrela));
8074 /* Note: the order of the entries is specified by the OpenVMS specs. */
8075 for (entry = filedata->dynamic_section;
8076 entry < filedata->dynamic_section + filedata->dynamic_nent;
8077 entry++)
8079 switch (entry->d_tag)
8081 case DT_IA_64_VMS_STRTAB_OFFSET:
8082 strtab_off = entry->d_un.d_val;
8083 break;
8084 case DT_STRSZ:
8085 strtab_sz = entry->d_un.d_val;
8086 if (strtab == NULL)
8087 strtab = get_data (NULL, filedata,
8088 filedata->dynamic_addr + strtab_off,
8089 1, strtab_sz, _("dynamic string section"));
8090 if (strtab == NULL)
8091 strtab_sz = 0;
8092 break;
8094 case DT_IA_64_VMS_NEEDED_IDENT:
8095 fixup.needed_ident = entry->d_un.d_val;
8096 break;
8097 case DT_NEEDED:
8098 fixup.needed = entry->d_un.d_val;
8099 break;
8100 case DT_IA_64_VMS_FIXUP_NEEDED:
8101 fixup.fixup_needed = entry->d_un.d_val;
8102 break;
8103 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8104 fixup.fixup_rela_cnt = entry->d_un.d_val;
8105 break;
8106 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8107 fixup.fixup_rela_off = entry->d_un.d_val;
8108 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8109 res = false;
8110 break;
8111 case DT_IA_64_VMS_IMG_RELA_CNT:
8112 imgrela.img_rela_cnt = entry->d_un.d_val;
8113 break;
8114 case DT_IA_64_VMS_IMG_RELA_OFF:
8115 imgrela.img_rela_off = entry->d_un.d_val;
8116 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8117 res = false;
8118 break;
8120 default:
8121 break;
8125 free (strtab);
8127 return res;
8130 static struct
8132 const char * name;
8133 int reloc;
8134 int size;
8135 relocation_type rel_type;
8137 dynamic_relocations [] =
8139 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8140 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8141 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8142 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8145 /* Process the reloc section. */
8147 static bool
8148 process_relocs (Filedata * filedata)
8150 unsigned long rel_size;
8151 unsigned long rel_offset;
8153 if (!do_reloc)
8154 return true;
8156 if (do_using_dynamic)
8158 relocation_type rel_type;
8159 const char * name;
8160 bool has_dynamic_reloc;
8161 unsigned int i;
8163 has_dynamic_reloc = false;
8165 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8167 rel_type = dynamic_relocations [i].rel_type;
8168 name = dynamic_relocations [i].name;
8169 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8170 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8172 if (rel_size)
8173 has_dynamic_reloc = true;
8175 if (rel_type == reltype_unknown)
8177 if (dynamic_relocations [i].reloc == DT_JMPREL)
8178 switch (filedata->dynamic_info[DT_PLTREL])
8180 case DT_REL:
8181 rel_type = reltype_rel;
8182 break;
8183 case DT_RELA:
8184 rel_type = reltype_rela;
8185 break;
8189 if (rel_size)
8191 if (filedata->is_separate)
8192 printf
8193 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
8194 filedata->file_name, name, rel_offset, rel_size);
8195 else
8196 printf
8197 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
8198 name, rel_offset, rel_size);
8200 dump_relocations (filedata,
8201 offset_from_vma (filedata, rel_offset, rel_size),
8202 rel_size,
8203 filedata->dynamic_symbols,
8204 filedata->num_dynamic_syms,
8205 filedata->dynamic_strings,
8206 filedata->dynamic_strings_length,
8207 rel_type, true /* is_dynamic */);
8211 if (is_ia64_vms (filedata))
8212 if (process_ia64_vms_dynamic_relocs (filedata))
8213 has_dynamic_reloc = true;
8215 if (! has_dynamic_reloc)
8217 if (filedata->is_separate)
8218 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8219 filedata->file_name);
8220 else
8221 printf (_("\nThere are no dynamic relocations in this file.\n"));
8224 else
8226 Elf_Internal_Shdr * section;
8227 unsigned long i;
8228 bool found = false;
8230 for (i = 0, section = filedata->section_headers;
8231 i < filedata->file_header.e_shnum;
8232 i++, section++)
8234 if ( section->sh_type != SHT_RELA
8235 && section->sh_type != SHT_REL
8236 && section->sh_type != SHT_RELR)
8237 continue;
8239 rel_offset = section->sh_offset;
8240 rel_size = section->sh_size;
8242 if (rel_size)
8244 relocation_type rel_type;
8245 unsigned long num_rela;
8247 if (filedata->is_separate)
8248 printf (_("\nIn linked file '%s' relocation section "),
8249 filedata->file_name);
8250 else
8251 printf (_("\nRelocation section "));
8253 if (filedata->string_table == NULL)
8254 printf ("%d", section->sh_name);
8255 else
8256 printf ("'%s'", printable_section_name (filedata, section));
8258 num_rela = rel_size / section->sh_entsize;
8259 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
8260 " at offset 0x%lx contains %lu entries:\n",
8261 num_rela),
8262 rel_offset, num_rela);
8264 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8265 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8267 if (section->sh_link != 0
8268 && section->sh_link < filedata->file_header.e_shnum)
8270 Elf_Internal_Shdr * symsec;
8271 Elf_Internal_Sym * symtab;
8272 unsigned long nsyms;
8273 unsigned long strtablen = 0;
8274 char * strtab = NULL;
8276 symsec = filedata->section_headers + section->sh_link;
8277 if (symsec->sh_type != SHT_SYMTAB
8278 && symsec->sh_type != SHT_DYNSYM)
8279 continue;
8281 if (!get_symtab (filedata, symsec,
8282 &symtab, &nsyms, &strtab, &strtablen))
8283 continue;
8285 dump_relocations (filedata, rel_offset, rel_size,
8286 symtab, nsyms, strtab, strtablen,
8287 rel_type,
8288 symsec->sh_type == SHT_DYNSYM);
8289 free (strtab);
8290 free (symtab);
8292 else
8293 dump_relocations (filedata, rel_offset, rel_size,
8294 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
8296 found = true;
8300 if (! found)
8302 /* Users sometimes forget the -D option, so try to be helpful. */
8303 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8305 if (filedata->dynamic_info[dynamic_relocations [i].size])
8307 if (filedata->is_separate)
8308 printf (_("\nThere are no static relocations in linked file '%s'."),
8309 filedata->file_name);
8310 else
8311 printf (_("\nThere are no static relocations in this file."));
8312 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8314 break;
8317 if (i == ARRAY_SIZE (dynamic_relocations))
8319 if (filedata->is_separate)
8320 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8321 filedata->file_name);
8322 else
8323 printf (_("\nThere are no relocations in this file.\n"));
8328 return true;
8331 /* An absolute address consists of a section and an offset. If the
8332 section is NULL, the offset itself is the address, otherwise, the
8333 address equals to LOAD_ADDRESS(section) + offset. */
8335 struct absaddr
8337 unsigned short section;
8338 bfd_vma offset;
8341 /* Find the nearest symbol at or below ADDR. Returns the symbol
8342 name, if found, and the offset from the symbol to ADDR. */
8344 static void
8345 find_symbol_for_address (Filedata * filedata,
8346 Elf_Internal_Sym * symtab,
8347 unsigned long nsyms,
8348 const char * strtab,
8349 unsigned long strtab_size,
8350 struct absaddr addr,
8351 const char ** symname,
8352 bfd_vma * offset)
8354 bfd_vma dist = 0x100000;
8355 Elf_Internal_Sym * sym;
8356 Elf_Internal_Sym * beg;
8357 Elf_Internal_Sym * end;
8358 Elf_Internal_Sym * best = NULL;
8360 REMOVE_ARCH_BITS (addr.offset);
8361 beg = symtab;
8362 end = symtab + nsyms;
8364 while (beg < end)
8366 bfd_vma value;
8368 sym = beg + (end - beg) / 2;
8370 value = sym->st_value;
8371 REMOVE_ARCH_BITS (value);
8373 if (sym->st_name != 0
8374 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8375 && addr.offset >= value
8376 && addr.offset - value < dist)
8378 best = sym;
8379 dist = addr.offset - value;
8380 if (!dist)
8381 break;
8384 if (addr.offset < value)
8385 end = sym;
8386 else
8387 beg = sym + 1;
8390 if (best)
8392 *symname = (best->st_name >= strtab_size
8393 ? _("<corrupt>") : strtab + best->st_name);
8394 *offset = dist;
8395 return;
8398 *symname = NULL;
8399 *offset = addr.offset;
8402 static /* signed */ int
8403 symcmp (const void *p, const void *q)
8405 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8406 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8408 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8411 /* Process the unwind section. */
8413 #include "unwind-ia64.h"
8415 struct ia64_unw_table_entry
8417 struct absaddr start;
8418 struct absaddr end;
8419 struct absaddr info;
8422 struct ia64_unw_aux_info
8424 struct ia64_unw_table_entry * table; /* Unwind table. */
8425 unsigned long table_len; /* Length of unwind table. */
8426 unsigned char * info; /* Unwind info. */
8427 unsigned long info_size; /* Size of unwind info. */
8428 bfd_vma info_addr; /* Starting address of unwind info. */
8429 bfd_vma seg_base; /* Starting address of segment. */
8430 Elf_Internal_Sym * symtab; /* The symbol table. */
8431 unsigned long nsyms; /* Number of symbols. */
8432 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8433 unsigned long nfuns; /* Number of entries in funtab. */
8434 char * strtab; /* The string table. */
8435 unsigned long strtab_size; /* Size of string table. */
8438 static bool
8439 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8441 struct ia64_unw_table_entry * tp;
8442 unsigned long j, nfuns;
8443 int in_body;
8444 bool res = true;
8446 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8447 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8448 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8449 aux->funtab[nfuns++] = aux->symtab[j];
8450 aux->nfuns = nfuns;
8451 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8453 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8455 bfd_vma stamp;
8456 bfd_vma offset;
8457 const unsigned char * dp;
8458 const unsigned char * head;
8459 const unsigned char * end;
8460 const char * procname;
8462 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8463 aux->strtab_size, tp->start, &procname, &offset);
8465 fputs ("\n<", stdout);
8467 if (procname)
8469 fputs (procname, stdout);
8471 if (offset)
8472 printf ("+%lx", (unsigned long) offset);
8475 fputs (">: [", stdout);
8476 print_vma (tp->start.offset, PREFIX_HEX);
8477 fputc ('-', stdout);
8478 print_vma (tp->end.offset, PREFIX_HEX);
8479 printf ("], info at +0x%lx\n",
8480 (unsigned long) (tp->info.offset - aux->seg_base));
8482 /* PR 17531: file: 86232b32. */
8483 if (aux->info == NULL)
8484 continue;
8486 offset = tp->info.offset;
8487 if (tp->info.section)
8489 if (tp->info.section >= filedata->file_header.e_shnum)
8491 warn (_("Invalid section %u in table entry %ld\n"),
8492 tp->info.section, (long) (tp - aux->table));
8493 res = false;
8494 continue;
8496 offset += filedata->section_headers[tp->info.section].sh_addr;
8498 offset -= aux->info_addr;
8499 /* PR 17531: file: 0997b4d1. */
8500 if (offset >= aux->info_size
8501 || aux->info_size - offset < 8)
8503 warn (_("Invalid offset %lx in table entry %ld\n"),
8504 (long) tp->info.offset, (long) (tp - aux->table));
8505 res = false;
8506 continue;
8509 head = aux->info + offset;
8510 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8512 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8513 (unsigned) UNW_VER (stamp),
8514 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8515 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8516 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8517 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8519 if (UNW_VER (stamp) != 1)
8521 printf (_("\tUnknown version.\n"));
8522 continue;
8525 in_body = 0;
8526 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8527 /* PR 17531: file: 16ceda89. */
8528 if (end > aux->info + aux->info_size)
8529 end = aux->info + aux->info_size;
8530 for (dp = head + 8; dp < end;)
8531 dp = unw_decode (dp, in_body, & in_body, end);
8534 free (aux->funtab);
8536 return res;
8539 static bool
8540 slurp_ia64_unwind_table (Filedata * filedata,
8541 struct ia64_unw_aux_info * aux,
8542 Elf_Internal_Shdr * sec)
8544 unsigned long size, nrelas, i;
8545 Elf_Internal_Phdr * seg;
8546 struct ia64_unw_table_entry * tep;
8547 Elf_Internal_Shdr * relsec;
8548 Elf_Internal_Rela * rela;
8549 Elf_Internal_Rela * rp;
8550 unsigned char * table;
8551 unsigned char * tp;
8552 Elf_Internal_Sym * sym;
8553 const char * relname;
8555 aux->table_len = 0;
8557 /* First, find the starting address of the segment that includes
8558 this section: */
8560 if (filedata->file_header.e_phnum)
8562 if (! get_program_headers (filedata))
8563 return false;
8565 for (seg = filedata->program_headers;
8566 seg < filedata->program_headers + filedata->file_header.e_phnum;
8567 ++seg)
8569 if (seg->p_type != PT_LOAD)
8570 continue;
8572 if (sec->sh_addr >= seg->p_vaddr
8573 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8575 aux->seg_base = seg->p_vaddr;
8576 break;
8581 /* Second, build the unwind table from the contents of the unwind section: */
8582 size = sec->sh_size;
8583 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8584 _("unwind table"));
8585 if (!table)
8586 return false;
8588 aux->table_len = size / (3 * eh_addr_size);
8589 aux->table = (struct ia64_unw_table_entry *)
8590 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8591 tep = aux->table;
8593 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8595 tep->start.section = SHN_UNDEF;
8596 tep->end.section = SHN_UNDEF;
8597 tep->info.section = SHN_UNDEF;
8598 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8599 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8600 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8601 tep->start.offset += aux->seg_base;
8602 tep->end.offset += aux->seg_base;
8603 tep->info.offset += aux->seg_base;
8605 free (table);
8607 /* Third, apply any relocations to the unwind table: */
8608 for (relsec = filedata->section_headers;
8609 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8610 ++relsec)
8612 if (relsec->sh_type != SHT_RELA
8613 || relsec->sh_info >= filedata->file_header.e_shnum
8614 || filedata->section_headers + relsec->sh_info != sec)
8615 continue;
8617 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8618 & rela, & nrelas))
8620 free (aux->table);
8621 aux->table = NULL;
8622 aux->table_len = 0;
8623 return false;
8626 for (rp = rela; rp < rela + nrelas; ++rp)
8628 unsigned int sym_ndx;
8629 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8630 relname = elf_ia64_reloc_type (r_type);
8632 /* PR 17531: file: 9fa67536. */
8633 if (relname == NULL)
8635 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8636 continue;
8639 if (! startswith (relname, "R_IA64_SEGREL"))
8641 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8642 continue;
8645 i = rp->r_offset / (3 * eh_addr_size);
8647 /* PR 17531: file: 5bc8d9bf. */
8648 if (i >= aux->table_len)
8650 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8651 continue;
8654 sym_ndx = get_reloc_symindex (rp->r_info);
8655 if (sym_ndx >= aux->nsyms)
8657 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8658 sym_ndx);
8659 continue;
8661 sym = aux->symtab + sym_ndx;
8663 switch (rp->r_offset / eh_addr_size % 3)
8665 case 0:
8666 aux->table[i].start.section = sym->st_shndx;
8667 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8668 break;
8669 case 1:
8670 aux->table[i].end.section = sym->st_shndx;
8671 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8672 break;
8673 case 2:
8674 aux->table[i].info.section = sym->st_shndx;
8675 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8676 break;
8677 default:
8678 break;
8682 free (rela);
8685 return true;
8688 static bool
8689 ia64_process_unwind (Filedata * filedata)
8691 Elf_Internal_Shdr * sec;
8692 Elf_Internal_Shdr * unwsec = NULL;
8693 unsigned long i, unwcount = 0, unwstart = 0;
8694 struct ia64_unw_aux_info aux;
8695 bool res = true;
8697 memset (& aux, 0, sizeof (aux));
8699 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8701 if (sec->sh_type == SHT_SYMTAB)
8703 if (aux.symtab)
8705 error (_("Multiple symbol tables encountered\n"));
8706 free (aux.symtab);
8707 aux.symtab = NULL;
8708 free (aux.strtab);
8709 aux.strtab = NULL;
8711 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8712 &aux.strtab, &aux.strtab_size))
8713 return false;
8715 else if (sec->sh_type == SHT_IA_64_UNWIND)
8716 unwcount++;
8719 if (!unwcount)
8720 printf (_("\nThere are no unwind sections in this file.\n"));
8722 while (unwcount-- > 0)
8724 const char *suffix;
8725 size_t len, len2;
8727 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8728 i < filedata->file_header.e_shnum; ++i, ++sec)
8729 if (sec->sh_type == SHT_IA_64_UNWIND)
8731 unwsec = sec;
8732 break;
8734 /* We have already counted the number of SHT_IA64_UNWIND
8735 sections so the loop above should never fail. */
8736 assert (unwsec != NULL);
8738 unwstart = i + 1;
8739 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8741 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8743 /* We need to find which section group it is in. */
8744 struct group_list * g;
8746 if (filedata->section_headers_groups == NULL
8747 || filedata->section_headers_groups[i] == NULL)
8748 i = filedata->file_header.e_shnum;
8749 else
8751 g = filedata->section_headers_groups[i]->root;
8753 for (; g != NULL; g = g->next)
8755 sec = filedata->section_headers + g->section_index;
8757 if (section_name_valid (filedata, sec)
8758 && streq (section_name (filedata, sec),
8759 ELF_STRING_ia64_unwind_info))
8760 break;
8763 if (g == NULL)
8764 i = filedata->file_header.e_shnum;
8767 else if (section_name_valid (filedata, unwsec)
8768 && startswith (section_name (filedata, unwsec),
8769 ELF_STRING_ia64_unwind_once))
8771 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8772 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8773 suffix = section_name (filedata, unwsec) + len;
8774 for (i = 0, sec = filedata->section_headers;
8775 i < filedata->file_header.e_shnum;
8776 ++i, ++sec)
8777 if (section_name_valid (filedata, sec)
8778 && startswith (section_name (filedata, sec),
8779 ELF_STRING_ia64_unwind_info_once)
8780 && streq (section_name (filedata, sec) + len2, suffix))
8781 break;
8783 else
8785 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8786 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8787 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8788 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8789 suffix = "";
8790 if (section_name_valid (filedata, unwsec)
8791 && startswith (section_name (filedata, unwsec),
8792 ELF_STRING_ia64_unwind))
8793 suffix = section_name (filedata, unwsec) + len;
8794 for (i = 0, sec = filedata->section_headers;
8795 i < filedata->file_header.e_shnum;
8796 ++i, ++sec)
8797 if (section_name_valid (filedata, sec)
8798 && startswith (section_name (filedata, sec),
8799 ELF_STRING_ia64_unwind_info)
8800 && streq (section_name (filedata, sec) + len2, suffix))
8801 break;
8804 if (i == filedata->file_header.e_shnum)
8806 printf (_("\nCould not find unwind info section for "));
8808 if (filedata->string_table == NULL)
8809 printf ("%d", unwsec->sh_name);
8810 else
8811 printf ("'%s'", printable_section_name (filedata, unwsec));
8813 else
8815 aux.info_addr = sec->sh_addr;
8816 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
8817 sec->sh_size,
8818 _("unwind info"));
8819 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
8821 printf (_("\nUnwind section "));
8823 if (filedata->string_table == NULL)
8824 printf ("%d", unwsec->sh_name);
8825 else
8826 printf ("'%s'", printable_section_name (filedata, unwsec));
8828 printf (_(" at offset 0x%lx contains %lu entries:\n"),
8829 (unsigned long) unwsec->sh_offset,
8830 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
8832 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
8833 && aux.table_len > 0)
8834 dump_ia64_unwind (filedata, & aux);
8836 free ((char *) aux.table);
8837 free ((char *) aux.info);
8838 aux.table = NULL;
8839 aux.info = NULL;
8843 free (aux.symtab);
8844 free ((char *) aux.strtab);
8846 return res;
8849 struct hppa_unw_table_entry
8851 struct absaddr start;
8852 struct absaddr end;
8853 unsigned int Cannot_unwind:1; /* 0 */
8854 unsigned int Millicode:1; /* 1 */
8855 unsigned int Millicode_save_sr0:1; /* 2 */
8856 unsigned int Region_description:2; /* 3..4 */
8857 unsigned int reserved1:1; /* 5 */
8858 unsigned int Entry_SR:1; /* 6 */
8859 unsigned int Entry_FR:4; /* Number saved 7..10 */
8860 unsigned int Entry_GR:5; /* Number saved 11..15 */
8861 unsigned int Args_stored:1; /* 16 */
8862 unsigned int Variable_Frame:1; /* 17 */
8863 unsigned int Separate_Package_Body:1; /* 18 */
8864 unsigned int Frame_Extension_Millicode:1; /* 19 */
8865 unsigned int Stack_Overflow_Check:1; /* 20 */
8866 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
8867 unsigned int Ada_Region:1; /* 22 */
8868 unsigned int cxx_info:1; /* 23 */
8869 unsigned int cxx_try_catch:1; /* 24 */
8870 unsigned int sched_entry_seq:1; /* 25 */
8871 unsigned int reserved2:1; /* 26 */
8872 unsigned int Save_SP:1; /* 27 */
8873 unsigned int Save_RP:1; /* 28 */
8874 unsigned int Save_MRP_in_frame:1; /* 29 */
8875 unsigned int extn_ptr_defined:1; /* 30 */
8876 unsigned int Cleanup_defined:1; /* 31 */
8878 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
8879 unsigned int HP_UX_interrupt_marker:1; /* 1 */
8880 unsigned int Large_frame:1; /* 2 */
8881 unsigned int Pseudo_SP_Set:1; /* 3 */
8882 unsigned int reserved4:1; /* 4 */
8883 unsigned int Total_frame_size:27; /* 5..31 */
8886 struct hppa_unw_aux_info
8888 struct hppa_unw_table_entry * table; /* Unwind table. */
8889 unsigned long table_len; /* Length of unwind table. */
8890 bfd_vma seg_base; /* Starting address of segment. */
8891 Elf_Internal_Sym * symtab; /* The symbol table. */
8892 unsigned long nsyms; /* Number of symbols. */
8893 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8894 unsigned long nfuns; /* Number of entries in funtab. */
8895 char * strtab; /* The string table. */
8896 unsigned long strtab_size; /* Size of string table. */
8899 static bool
8900 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
8902 struct hppa_unw_table_entry * tp;
8903 unsigned long j, nfuns;
8904 bool res = true;
8906 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8907 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8908 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8909 aux->funtab[nfuns++] = aux->symtab[j];
8910 aux->nfuns = nfuns;
8911 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8913 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8915 bfd_vma offset;
8916 const char * procname;
8918 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8919 aux->strtab_size, tp->start, &procname,
8920 &offset);
8922 fputs ("\n<", stdout);
8924 if (procname)
8926 fputs (procname, stdout);
8928 if (offset)
8929 printf ("+%lx", (unsigned long) offset);
8932 fputs (">: [", stdout);
8933 print_vma (tp->start.offset, PREFIX_HEX);
8934 fputc ('-', stdout);
8935 print_vma (tp->end.offset, PREFIX_HEX);
8936 printf ("]\n\t");
8938 #define PF(_m) if (tp->_m) printf (#_m " ");
8939 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
8940 PF(Cannot_unwind);
8941 PF(Millicode);
8942 PF(Millicode_save_sr0);
8943 /* PV(Region_description); */
8944 PF(Entry_SR);
8945 PV(Entry_FR);
8946 PV(Entry_GR);
8947 PF(Args_stored);
8948 PF(Variable_Frame);
8949 PF(Separate_Package_Body);
8950 PF(Frame_Extension_Millicode);
8951 PF(Stack_Overflow_Check);
8952 PF(Two_Instruction_SP_Increment);
8953 PF(Ada_Region);
8954 PF(cxx_info);
8955 PF(cxx_try_catch);
8956 PF(sched_entry_seq);
8957 PF(Save_SP);
8958 PF(Save_RP);
8959 PF(Save_MRP_in_frame);
8960 PF(extn_ptr_defined);
8961 PF(Cleanup_defined);
8962 PF(MPE_XL_interrupt_marker);
8963 PF(HP_UX_interrupt_marker);
8964 PF(Large_frame);
8965 PF(Pseudo_SP_Set);
8966 PV(Total_frame_size);
8967 #undef PF
8968 #undef PV
8971 printf ("\n");
8973 free (aux->funtab);
8975 return res;
8978 static bool
8979 slurp_hppa_unwind_table (Filedata * filedata,
8980 struct hppa_unw_aux_info * aux,
8981 Elf_Internal_Shdr * sec)
8983 unsigned long size, unw_ent_size, nentries, nrelas, i;
8984 Elf_Internal_Phdr * seg;
8985 struct hppa_unw_table_entry * tep;
8986 Elf_Internal_Shdr * relsec;
8987 Elf_Internal_Rela * rela;
8988 Elf_Internal_Rela * rp;
8989 unsigned char * table;
8990 unsigned char * tp;
8991 Elf_Internal_Sym * sym;
8992 const char * relname;
8994 /* First, find the starting address of the segment that includes
8995 this section. */
8996 if (filedata->file_header.e_phnum)
8998 if (! get_program_headers (filedata))
8999 return false;
9001 for (seg = filedata->program_headers;
9002 seg < filedata->program_headers + filedata->file_header.e_phnum;
9003 ++seg)
9005 if (seg->p_type != PT_LOAD)
9006 continue;
9008 if (sec->sh_addr >= seg->p_vaddr
9009 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9011 aux->seg_base = seg->p_vaddr;
9012 break;
9017 /* Second, build the unwind table from the contents of the unwind
9018 section. */
9019 size = sec->sh_size;
9020 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9021 _("unwind table"));
9022 if (!table)
9023 return false;
9025 unw_ent_size = 16;
9026 nentries = size / unw_ent_size;
9027 size = unw_ent_size * nentries;
9029 aux->table_len = nentries;
9030 tep = aux->table = (struct hppa_unw_table_entry *)
9031 xcmalloc (nentries, sizeof (aux->table[0]));
9033 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9035 unsigned int tmp1, tmp2;
9037 tep->start.section = SHN_UNDEF;
9038 tep->end.section = SHN_UNDEF;
9040 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9041 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9042 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9043 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9045 tep->start.offset += aux->seg_base;
9046 tep->end.offset += aux->seg_base;
9048 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9049 tep->Millicode = (tmp1 >> 30) & 0x1;
9050 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9051 tep->Region_description = (tmp1 >> 27) & 0x3;
9052 tep->reserved1 = (tmp1 >> 26) & 0x1;
9053 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9054 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9055 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9056 tep->Args_stored = (tmp1 >> 15) & 0x1;
9057 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9058 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9059 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9060 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9061 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9062 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9063 tep->cxx_info = (tmp1 >> 8) & 0x1;
9064 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9065 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9066 tep->reserved2 = (tmp1 >> 5) & 0x1;
9067 tep->Save_SP = (tmp1 >> 4) & 0x1;
9068 tep->Save_RP = (tmp1 >> 3) & 0x1;
9069 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9070 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9071 tep->Cleanup_defined = tmp1 & 0x1;
9073 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9074 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9075 tep->Large_frame = (tmp2 >> 29) & 0x1;
9076 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9077 tep->reserved4 = (tmp2 >> 27) & 0x1;
9078 tep->Total_frame_size = tmp2 & 0x7ffffff;
9080 free (table);
9082 /* Third, apply any relocations to the unwind table. */
9083 for (relsec = filedata->section_headers;
9084 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9085 ++relsec)
9087 if (relsec->sh_type != SHT_RELA
9088 || relsec->sh_info >= filedata->file_header.e_shnum
9089 || filedata->section_headers + relsec->sh_info != sec)
9090 continue;
9092 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9093 & rela, & nrelas))
9094 return false;
9096 for (rp = rela; rp < rela + nrelas; ++rp)
9098 unsigned int sym_ndx;
9099 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9100 relname = elf_hppa_reloc_type (r_type);
9102 if (relname == NULL)
9104 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9105 continue;
9108 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
9109 if (! startswith (relname, "R_PARISC_SEGREL"))
9111 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9112 continue;
9115 i = rp->r_offset / unw_ent_size;
9116 if (i >= aux->table_len)
9118 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
9119 continue;
9122 sym_ndx = get_reloc_symindex (rp->r_info);
9123 if (sym_ndx >= aux->nsyms)
9125 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9126 sym_ndx);
9127 continue;
9129 sym = aux->symtab + sym_ndx;
9131 switch ((rp->r_offset % unw_ent_size) / 4)
9133 case 0:
9134 aux->table[i].start.section = sym->st_shndx;
9135 aux->table[i].start.offset = sym->st_value + rp->r_addend;
9136 break;
9137 case 1:
9138 aux->table[i].end.section = sym->st_shndx;
9139 aux->table[i].end.offset = sym->st_value + rp->r_addend;
9140 break;
9141 default:
9142 break;
9146 free (rela);
9149 return true;
9152 static bool
9153 hppa_process_unwind (Filedata * filedata)
9155 struct hppa_unw_aux_info aux;
9156 Elf_Internal_Shdr * unwsec = NULL;
9157 Elf_Internal_Shdr * sec;
9158 unsigned long i;
9159 bool res = true;
9161 if (filedata->string_table == NULL)
9162 return false;
9164 memset (& aux, 0, sizeof (aux));
9166 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9168 if (sec->sh_type == SHT_SYMTAB)
9170 if (aux.symtab)
9172 error (_("Multiple symbol tables encountered\n"));
9173 free (aux.symtab);
9174 aux.symtab = NULL;
9175 free (aux.strtab);
9176 aux.strtab = NULL;
9178 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9179 &aux.strtab, &aux.strtab_size))
9180 return false;
9182 else if (section_name_valid (filedata, sec)
9183 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9184 unwsec = sec;
9187 if (!unwsec)
9188 printf (_("\nThere are no unwind sections in this file.\n"));
9190 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9192 if (section_name_valid (filedata, sec)
9193 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9195 unsigned long num_unwind = sec->sh_size / 16;
9197 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9198 "contains %lu entry:\n",
9199 "\nUnwind section '%s' at offset 0x%lx "
9200 "contains %lu entries:\n",
9201 num_unwind),
9202 printable_section_name (filedata, sec),
9203 (unsigned long) sec->sh_offset,
9204 num_unwind);
9206 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9207 res = false;
9209 if (res && aux.table_len > 0)
9211 if (! dump_hppa_unwind (filedata, &aux))
9212 res = false;
9215 free ((char *) aux.table);
9216 aux.table = NULL;
9220 free (aux.symtab);
9221 free ((char *) aux.strtab);
9223 return res;
9226 struct arm_section
9228 unsigned char * data; /* The unwind data. */
9229 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9230 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9231 unsigned long nrelas; /* The number of relocations. */
9232 unsigned int rel_type; /* REL or RELA ? */
9233 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
9236 struct arm_unw_aux_info
9238 Filedata * filedata; /* The file containing the unwind sections. */
9239 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9240 unsigned long nsyms; /* Number of symbols. */
9241 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9242 unsigned long nfuns; /* Number of these symbols. */
9243 char * strtab; /* The file's string table. */
9244 unsigned long strtab_size; /* Size of string table. */
9247 static const char *
9248 arm_print_vma_and_name (Filedata * filedata,
9249 struct arm_unw_aux_info * aux,
9250 bfd_vma fn,
9251 struct absaddr addr)
9253 const char *procname;
9254 bfd_vma sym_offset;
9256 if (addr.section == SHN_UNDEF)
9257 addr.offset = fn;
9259 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9260 aux->strtab_size, addr, &procname,
9261 &sym_offset);
9263 print_vma (fn, PREFIX_HEX);
9265 if (procname)
9267 fputs (" <", stdout);
9268 fputs (procname, stdout);
9270 if (sym_offset)
9271 printf ("+0x%lx", (unsigned long) sym_offset);
9272 fputc ('>', stdout);
9275 return procname;
9278 static void
9279 arm_free_section (struct arm_section *arm_sec)
9281 free (arm_sec->data);
9282 free (arm_sec->rela);
9285 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9286 cached section and install SEC instead.
9287 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9288 and return its valued in * WORDP, relocating if necessary.
9289 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9290 relocation's offset in ADDR.
9291 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9292 into the string table of the symbol associated with the reloc. If no
9293 reloc was applied store -1 there.
9294 5) Return TRUE upon success, FALSE otherwise. */
9296 static bool
9297 get_unwind_section_word (Filedata * filedata,
9298 struct arm_unw_aux_info * aux,
9299 struct arm_section * arm_sec,
9300 Elf_Internal_Shdr * sec,
9301 bfd_vma word_offset,
9302 unsigned int * wordp,
9303 struct absaddr * addr,
9304 bfd_vma * sym_name)
9306 Elf_Internal_Rela *rp;
9307 Elf_Internal_Sym *sym;
9308 const char * relname;
9309 unsigned int word;
9310 bool wrapped;
9312 if (sec == NULL || arm_sec == NULL)
9313 return false;
9315 addr->section = SHN_UNDEF;
9316 addr->offset = 0;
9318 if (sym_name != NULL)
9319 *sym_name = (bfd_vma) -1;
9321 /* If necessary, update the section cache. */
9322 if (sec != arm_sec->sec)
9324 Elf_Internal_Shdr *relsec;
9326 arm_free_section (arm_sec);
9328 arm_sec->sec = sec;
9329 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9330 sec->sh_size, _("unwind data"));
9331 arm_sec->rela = NULL;
9332 arm_sec->nrelas = 0;
9334 for (relsec = filedata->section_headers;
9335 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9336 ++relsec)
9338 if (relsec->sh_info >= filedata->file_header.e_shnum
9339 || filedata->section_headers + relsec->sh_info != sec
9340 /* PR 15745: Check the section type as well. */
9341 || (relsec->sh_type != SHT_REL
9342 && relsec->sh_type != SHT_RELA))
9343 continue;
9345 arm_sec->rel_type = relsec->sh_type;
9346 if (relsec->sh_type == SHT_REL)
9348 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9349 relsec->sh_size,
9350 & arm_sec->rela, & arm_sec->nrelas))
9351 return false;
9353 else /* relsec->sh_type == SHT_RELA */
9355 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9356 relsec->sh_size,
9357 & arm_sec->rela, & arm_sec->nrelas))
9358 return false;
9360 break;
9363 arm_sec->next_rela = arm_sec->rela;
9366 /* If there is no unwind data we can do nothing. */
9367 if (arm_sec->data == NULL)
9368 return false;
9370 /* If the offset is invalid then fail. */
9371 if (/* PR 21343 *//* PR 18879 */
9372 sec->sh_size < 4
9373 || word_offset > (sec->sh_size - 4)
9374 || ((bfd_signed_vma) word_offset) < 0)
9375 return false;
9377 /* Get the word at the required offset. */
9378 word = byte_get (arm_sec->data + word_offset, 4);
9380 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9381 if (arm_sec->rela == NULL)
9383 * wordp = word;
9384 return true;
9387 /* Look through the relocs to find the one that applies to the provided offset. */
9388 wrapped = false;
9389 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9391 bfd_vma prelval, offset;
9393 if (rp->r_offset > word_offset && !wrapped)
9395 rp = arm_sec->rela;
9396 wrapped = true;
9398 if (rp->r_offset > word_offset)
9399 break;
9401 if (rp->r_offset & 3)
9403 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9404 (unsigned long) rp->r_offset);
9405 continue;
9408 if (rp->r_offset < word_offset)
9409 continue;
9411 /* PR 17531: file: 027-161405-0.004 */
9412 if (aux->symtab == NULL)
9413 continue;
9415 if (arm_sec->rel_type == SHT_REL)
9417 offset = word & 0x7fffffff;
9418 if (offset & 0x40000000)
9419 offset |= ~ (bfd_vma) 0x7fffffff;
9421 else if (arm_sec->rel_type == SHT_RELA)
9422 offset = rp->r_addend;
9423 else
9425 error (_("Unknown section relocation type %d encountered\n"),
9426 arm_sec->rel_type);
9427 break;
9430 /* PR 17531 file: 027-1241568-0.004. */
9431 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9433 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9434 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9435 break;
9438 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9439 offset += sym->st_value;
9440 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9442 /* Check that we are processing the expected reloc type. */
9443 if (filedata->file_header.e_machine == EM_ARM)
9445 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9446 if (relname == NULL)
9448 warn (_("Skipping unknown ARM relocation type: %d\n"),
9449 (int) ELF32_R_TYPE (rp->r_info));
9450 continue;
9453 if (streq (relname, "R_ARM_NONE"))
9454 continue;
9456 if (! streq (relname, "R_ARM_PREL31"))
9458 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9459 continue;
9462 else if (filedata->file_header.e_machine == EM_TI_C6000)
9464 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9465 if (relname == NULL)
9467 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9468 (int) ELF32_R_TYPE (rp->r_info));
9469 continue;
9472 if (streq (relname, "R_C6000_NONE"))
9473 continue;
9475 if (! streq (relname, "R_C6000_PREL31"))
9477 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9478 continue;
9481 prelval >>= 1;
9483 else
9485 /* This function currently only supports ARM and TI unwinders. */
9486 warn (_("Only TI and ARM unwinders are currently supported\n"));
9487 break;
9490 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
9491 addr->section = sym->st_shndx;
9492 addr->offset = offset;
9494 if (sym_name)
9495 * sym_name = sym->st_name;
9496 break;
9499 *wordp = word;
9500 arm_sec->next_rela = rp;
9502 return true;
9505 static const char *tic6x_unwind_regnames[16] =
9507 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9508 "A14", "A13", "A12", "A11", "A10",
9509 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9512 static void
9513 decode_tic6x_unwind_regmask (unsigned int mask)
9515 int i;
9517 for (i = 12; mask; mask >>= 1, i--)
9519 if (mask & 1)
9521 fputs (tic6x_unwind_regnames[i], stdout);
9522 if (mask > 1)
9523 fputs (", ", stdout);
9528 #define ADVANCE \
9529 if (remaining == 0 && more_words) \
9531 data_offset += 4; \
9532 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9533 data_offset, & word, & addr, NULL)) \
9534 return false; \
9535 remaining = 4; \
9536 more_words--; \
9539 #define GET_OP(OP) \
9540 ADVANCE; \
9541 if (remaining) \
9543 remaining--; \
9544 (OP) = word >> 24; \
9545 word <<= 8; \
9547 else \
9549 printf (_("[Truncated opcode]\n")); \
9550 return false; \
9552 printf ("0x%02x ", OP)
9554 static bool
9555 decode_arm_unwind_bytecode (Filedata * filedata,
9556 struct arm_unw_aux_info * aux,
9557 unsigned int word,
9558 unsigned int remaining,
9559 unsigned int more_words,
9560 bfd_vma data_offset,
9561 Elf_Internal_Shdr * data_sec,
9562 struct arm_section * data_arm_sec)
9564 struct absaddr addr;
9565 bool res = true;
9567 /* Decode the unwinding instructions. */
9568 while (1)
9570 unsigned int op, op2;
9572 ADVANCE;
9573 if (remaining == 0)
9574 break;
9575 remaining--;
9576 op = word >> 24;
9577 word <<= 8;
9579 printf (" 0x%02x ", op);
9581 if ((op & 0xc0) == 0x00)
9583 int offset = ((op & 0x3f) << 2) + 4;
9585 printf (" vsp = vsp + %d", offset);
9587 else if ((op & 0xc0) == 0x40)
9589 int offset = ((op & 0x3f) << 2) + 4;
9591 printf (" vsp = vsp - %d", offset);
9593 else if ((op & 0xf0) == 0x80)
9595 GET_OP (op2);
9596 if (op == 0x80 && op2 == 0)
9597 printf (_("Refuse to unwind"));
9598 else
9600 unsigned int mask = ((op & 0x0f) << 8) | op2;
9601 bool first = true;
9602 int i;
9604 printf ("pop {");
9605 for (i = 0; i < 12; i++)
9606 if (mask & (1 << i))
9608 if (first)
9609 first = false;
9610 else
9611 printf (", ");
9612 printf ("r%d", 4 + i);
9614 printf ("}");
9617 else if ((op & 0xf0) == 0x90)
9619 if (op == 0x9d || op == 0x9f)
9620 printf (_(" [Reserved]"));
9621 else
9622 printf (" vsp = r%d", op & 0x0f);
9624 else if ((op & 0xf0) == 0xa0)
9626 int end = 4 + (op & 0x07);
9627 bool first = true;
9628 int i;
9630 printf (" pop {");
9631 for (i = 4; i <= end; i++)
9633 if (first)
9634 first = false;
9635 else
9636 printf (", ");
9637 printf ("r%d", i);
9639 if (op & 0x08)
9641 if (!first)
9642 printf (", ");
9643 printf ("r14");
9645 printf ("}");
9647 else if (op == 0xb0)
9648 printf (_(" finish"));
9649 else if (op == 0xb1)
9651 GET_OP (op2);
9652 if (op2 == 0 || (op2 & 0xf0) != 0)
9653 printf (_("[Spare]"));
9654 else
9656 unsigned int mask = op2 & 0x0f;
9657 bool first = true;
9658 int i;
9660 printf ("pop {");
9661 for (i = 0; i < 12; i++)
9662 if (mask & (1 << i))
9664 if (first)
9665 first = false;
9666 else
9667 printf (", ");
9668 printf ("r%d", i);
9670 printf ("}");
9673 else if (op == 0xb2)
9675 unsigned char buf[9];
9676 unsigned int i, len;
9677 unsigned long offset;
9679 for (i = 0; i < sizeof (buf); i++)
9681 GET_OP (buf[i]);
9682 if ((buf[i] & 0x80) == 0)
9683 break;
9685 if (i == sizeof (buf))
9687 error (_("corrupt change to vsp\n"));
9688 res = false;
9690 else
9692 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9693 assert (len == i + 1);
9694 offset = offset * 4 + 0x204;
9695 printf ("vsp = vsp + %ld", offset);
9698 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9700 unsigned int first, last;
9702 GET_OP (op2);
9703 first = op2 >> 4;
9704 last = op2 & 0x0f;
9705 if (op == 0xc8)
9706 first = first + 16;
9707 printf ("pop {D%d", first);
9708 if (last)
9709 printf ("-D%d", first + last);
9710 printf ("}");
9712 else if (op == 0xb4)
9713 printf (_(" pop {ra_auth_code}"));
9714 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9716 unsigned int count = op & 0x07;
9718 printf ("pop {D8");
9719 if (count)
9720 printf ("-D%d", 8 + count);
9721 printf ("}");
9723 else if (op >= 0xc0 && op <= 0xc5)
9725 unsigned int count = op & 0x07;
9727 printf (" pop {wR10");
9728 if (count)
9729 printf ("-wR%d", 10 + count);
9730 printf ("}");
9732 else if (op == 0xc6)
9734 unsigned int first, last;
9736 GET_OP (op2);
9737 first = op2 >> 4;
9738 last = op2 & 0x0f;
9739 printf ("pop {wR%d", first);
9740 if (last)
9741 printf ("-wR%d", first + last);
9742 printf ("}");
9744 else if (op == 0xc7)
9746 GET_OP (op2);
9747 if (op2 == 0 || (op2 & 0xf0) != 0)
9748 printf (_("[Spare]"));
9749 else
9751 unsigned int mask = op2 & 0x0f;
9752 bool first = true;
9753 int i;
9755 printf ("pop {");
9756 for (i = 0; i < 4; i++)
9757 if (mask & (1 << i))
9759 if (first)
9760 first = false;
9761 else
9762 printf (", ");
9763 printf ("wCGR%d", i);
9765 printf ("}");
9768 else
9770 printf (_(" [unsupported opcode]"));
9771 res = false;
9774 printf ("\n");
9777 return res;
9780 static bool
9781 decode_tic6x_unwind_bytecode (Filedata * filedata,
9782 struct arm_unw_aux_info * aux,
9783 unsigned int word,
9784 unsigned int remaining,
9785 unsigned int more_words,
9786 bfd_vma data_offset,
9787 Elf_Internal_Shdr * data_sec,
9788 struct arm_section * data_arm_sec)
9790 struct absaddr addr;
9792 /* Decode the unwinding instructions. */
9793 while (1)
9795 unsigned int op, op2;
9797 ADVANCE;
9798 if (remaining == 0)
9799 break;
9800 remaining--;
9801 op = word >> 24;
9802 word <<= 8;
9804 printf (" 0x%02x ", op);
9806 if ((op & 0xc0) == 0x00)
9808 int offset = ((op & 0x3f) << 3) + 8;
9809 printf (" sp = sp + %d", offset);
9811 else if ((op & 0xc0) == 0x80)
9813 GET_OP (op2);
9814 if (op == 0x80 && op2 == 0)
9815 printf (_("Refuse to unwind"));
9816 else
9818 unsigned int mask = ((op & 0x1f) << 8) | op2;
9819 if (op & 0x20)
9820 printf ("pop compact {");
9821 else
9822 printf ("pop {");
9824 decode_tic6x_unwind_regmask (mask);
9825 printf("}");
9828 else if ((op & 0xf0) == 0xc0)
9830 unsigned int reg;
9831 unsigned int nregs;
9832 unsigned int i;
9833 const char *name;
9834 struct
9836 unsigned int offset;
9837 unsigned int reg;
9838 } regpos[16];
9840 /* Scan entire instruction first so that GET_OP output is not
9841 interleaved with disassembly. */
9842 nregs = 0;
9843 for (i = 0; nregs < (op & 0xf); i++)
9845 GET_OP (op2);
9846 reg = op2 >> 4;
9847 if (reg != 0xf)
9849 regpos[nregs].offset = i * 2;
9850 regpos[nregs].reg = reg;
9851 nregs++;
9854 reg = op2 & 0xf;
9855 if (reg != 0xf)
9857 regpos[nregs].offset = i * 2 + 1;
9858 regpos[nregs].reg = reg;
9859 nregs++;
9863 printf (_("pop frame {"));
9864 if (nregs == 0)
9866 printf (_("*corrupt* - no registers specified"));
9868 else
9870 reg = nregs - 1;
9871 for (i = i * 2; i > 0; i--)
9873 if (regpos[reg].offset == i - 1)
9875 name = tic6x_unwind_regnames[regpos[reg].reg];
9876 if (reg > 0)
9877 reg--;
9879 else
9880 name = _("[pad]");
9882 fputs (name, stdout);
9883 if (i > 1)
9884 printf (", ");
9888 printf ("}");
9890 else if (op == 0xd0)
9891 printf (" MOV FP, SP");
9892 else if (op == 0xd1)
9893 printf (" __c6xabi_pop_rts");
9894 else if (op == 0xd2)
9896 unsigned char buf[9];
9897 unsigned int i, len;
9898 unsigned long offset;
9900 for (i = 0; i < sizeof (buf); i++)
9902 GET_OP (buf[i]);
9903 if ((buf[i] & 0x80) == 0)
9904 break;
9906 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
9907 if (i == sizeof (buf))
9909 warn (_("Corrupt stack pointer adjustment detected\n"));
9910 return false;
9913 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9914 assert (len == i + 1);
9915 offset = offset * 8 + 0x408;
9916 printf (_("sp = sp + %ld"), offset);
9918 else if ((op & 0xf0) == 0xe0)
9920 if ((op & 0x0f) == 7)
9921 printf (" RETURN");
9922 else
9923 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9925 else
9927 printf (_(" [unsupported opcode]"));
9929 putchar ('\n');
9932 return true;
9935 static bfd_vma
9936 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
9938 bfd_vma offset;
9940 offset = word & 0x7fffffff;
9941 if (offset & 0x40000000)
9942 offset |= ~ (bfd_vma) 0x7fffffff;
9944 if (filedata->file_header.e_machine == EM_TI_C6000)
9945 offset <<= 1;
9947 return offset + where;
9950 static bool
9951 decode_arm_unwind (Filedata * filedata,
9952 struct arm_unw_aux_info * aux,
9953 unsigned int word,
9954 unsigned int remaining,
9955 bfd_vma data_offset,
9956 Elf_Internal_Shdr * data_sec,
9957 struct arm_section * data_arm_sec)
9959 int per_index;
9960 unsigned int more_words = 0;
9961 struct absaddr addr;
9962 bfd_vma sym_name = (bfd_vma) -1;
9963 bool res = true;
9965 if (remaining == 0)
9967 /* Fetch the first word.
9968 Note - when decoding an object file the address extracted
9969 here will always be 0. So we also pass in the sym_name
9970 parameter so that we can find the symbol associated with
9971 the personality routine. */
9972 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
9973 & word, & addr, & sym_name))
9974 return false;
9976 remaining = 4;
9978 else
9980 addr.section = SHN_UNDEF;
9981 addr.offset = 0;
9984 if ((word & 0x80000000) == 0)
9986 /* Expand prel31 for personality routine. */
9987 bfd_vma fn;
9988 const char *procname;
9990 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
9991 printf (_(" Personality routine: "));
9992 if (fn == 0
9993 && addr.section == SHN_UNDEF && addr.offset == 0
9994 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9996 procname = aux->strtab + sym_name;
9997 print_vma (fn, PREFIX_HEX);
9998 if (procname)
10000 fputs (" <", stdout);
10001 fputs (procname, stdout);
10002 fputc ('>', stdout);
10005 else
10006 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10007 fputc ('\n', stdout);
10009 /* The GCC personality routines use the standard compact
10010 encoding, starting with one byte giving the number of
10011 words. */
10012 if (procname != NULL
10013 && (startswith (procname, "__gcc_personality_v0")
10014 || startswith (procname, "__gxx_personality_v0")
10015 || startswith (procname, "__gcj_personality_v0")
10016 || startswith (procname, "__gnu_objc_personality_v0")))
10018 remaining = 0;
10019 more_words = 1;
10020 ADVANCE;
10021 if (!remaining)
10023 printf (_(" [Truncated data]\n"));
10024 return false;
10026 more_words = word >> 24;
10027 word <<= 8;
10028 remaining--;
10029 per_index = -1;
10031 else
10032 return true;
10034 else
10036 /* ARM EHABI Section 6.3:
10038 An exception-handling table entry for the compact model looks like:
10040 31 30-28 27-24 23-0
10041 -- ----- ----- ----
10042 1 0 index Data for personalityRoutine[index] */
10044 if (filedata->file_header.e_machine == EM_ARM
10045 && (word & 0x70000000))
10047 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10048 res = false;
10051 per_index = (word >> 24) & 0x7f;
10052 printf (_(" Compact model index: %d\n"), per_index);
10053 if (per_index == 0)
10055 more_words = 0;
10056 word <<= 8;
10057 remaining--;
10059 else if (per_index < 3)
10061 more_words = (word >> 16) & 0xff;
10062 word <<= 16;
10063 remaining -= 2;
10067 switch (filedata->file_header.e_machine)
10069 case EM_ARM:
10070 if (per_index < 3)
10072 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10073 data_offset, data_sec, data_arm_sec))
10074 res = false;
10076 else
10078 warn (_("Unknown ARM compact model index encountered\n"));
10079 printf (_(" [reserved]\n"));
10080 res = false;
10082 break;
10084 case EM_TI_C6000:
10085 if (per_index < 3)
10087 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10088 data_offset, data_sec, data_arm_sec))
10089 res = false;
10091 else if (per_index < 5)
10093 if (((word >> 17) & 0x7f) == 0x7f)
10094 printf (_(" Restore stack from frame pointer\n"));
10095 else
10096 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10097 printf (_(" Registers restored: "));
10098 if (per_index == 4)
10099 printf (" (compact) ");
10100 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10101 putchar ('\n');
10102 printf (_(" Return register: %s\n"),
10103 tic6x_unwind_regnames[word & 0xf]);
10105 else
10106 printf (_(" [reserved (%d)]\n"), per_index);
10107 break;
10109 default:
10110 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10111 filedata->file_header.e_machine);
10112 res = false;
10115 /* Decode the descriptors. Not implemented. */
10117 return res;
10120 static bool
10121 dump_arm_unwind (Filedata * filedata,
10122 struct arm_unw_aux_info * aux,
10123 Elf_Internal_Shdr * exidx_sec)
10125 struct arm_section exidx_arm_sec, extab_arm_sec;
10126 unsigned int i, exidx_len;
10127 unsigned long j, nfuns;
10128 bool res = true;
10130 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10131 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10132 exidx_len = exidx_sec->sh_size / 8;
10134 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10135 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10136 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10137 aux->funtab[nfuns++] = aux->symtab[j];
10138 aux->nfuns = nfuns;
10139 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10141 for (i = 0; i < exidx_len; i++)
10143 unsigned int exidx_fn, exidx_entry;
10144 struct absaddr fn_addr, entry_addr;
10145 bfd_vma fn;
10147 fputc ('\n', stdout);
10149 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10150 8 * i, & exidx_fn, & fn_addr, NULL)
10151 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10152 8 * i + 4, & exidx_entry, & entry_addr, NULL))
10154 free (aux->funtab);
10155 arm_free_section (& exidx_arm_sec);
10156 arm_free_section (& extab_arm_sec);
10157 return false;
10160 /* ARM EHABI, Section 5:
10161 An index table entry consists of 2 words.
10162 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10163 if (exidx_fn & 0x80000000)
10165 warn (_("corrupt index table entry: %x\n"), exidx_fn);
10166 res = false;
10169 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10171 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10172 fputs (": ", stdout);
10174 if (exidx_entry == 1)
10176 print_vma (exidx_entry, PREFIX_HEX);
10177 fputs (" [cantunwind]\n", stdout);
10179 else if (exidx_entry & 0x80000000)
10181 print_vma (exidx_entry, PREFIX_HEX);
10182 fputc ('\n', stdout);
10183 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10185 else
10187 bfd_vma table, table_offset = 0;
10188 Elf_Internal_Shdr *table_sec;
10190 fputs ("@", stdout);
10191 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10192 print_vma (table, PREFIX_HEX);
10193 printf ("\n");
10195 /* Locate the matching .ARM.extab. */
10196 if (entry_addr.section != SHN_UNDEF
10197 && entry_addr.section < filedata->file_header.e_shnum)
10199 table_sec = filedata->section_headers + entry_addr.section;
10200 table_offset = entry_addr.offset;
10201 /* PR 18879 */
10202 if (table_offset > table_sec->sh_size
10203 || ((bfd_signed_vma) table_offset) < 0)
10205 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
10206 (unsigned long) table_offset,
10207 printable_section_name (filedata, table_sec));
10208 res = false;
10209 continue;
10212 else
10214 table_sec = find_section_by_address (filedata, table);
10215 if (table_sec != NULL)
10216 table_offset = table - table_sec->sh_addr;
10219 if (table_sec == NULL)
10221 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
10222 (unsigned long) table);
10223 res = false;
10224 continue;
10227 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10228 &extab_arm_sec))
10229 res = false;
10233 printf ("\n");
10235 free (aux->funtab);
10236 arm_free_section (&exidx_arm_sec);
10237 arm_free_section (&extab_arm_sec);
10239 return res;
10242 /* Used for both ARM and C6X unwinding tables. */
10244 static bool
10245 arm_process_unwind (Filedata * filedata)
10247 struct arm_unw_aux_info aux;
10248 Elf_Internal_Shdr *unwsec = NULL;
10249 Elf_Internal_Shdr *sec;
10250 unsigned long i;
10251 unsigned int sec_type;
10252 bool res = true;
10254 switch (filedata->file_header.e_machine)
10256 case EM_ARM:
10257 sec_type = SHT_ARM_EXIDX;
10258 break;
10260 case EM_TI_C6000:
10261 sec_type = SHT_C6000_UNWIND;
10262 break;
10264 default:
10265 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
10266 filedata->file_header.e_machine);
10267 return false;
10270 if (filedata->string_table == NULL)
10271 return false;
10273 memset (& aux, 0, sizeof (aux));
10274 aux.filedata = filedata;
10276 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10278 if (sec->sh_type == SHT_SYMTAB)
10280 if (aux.symtab)
10282 error (_("Multiple symbol tables encountered\n"));
10283 free (aux.symtab);
10284 aux.symtab = NULL;
10285 free (aux.strtab);
10286 aux.strtab = NULL;
10288 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10289 &aux.strtab, &aux.strtab_size))
10290 return false;
10292 else if (sec->sh_type == sec_type)
10293 unwsec = sec;
10296 if (unwsec == NULL)
10297 printf (_("\nThere are no unwind sections in this file.\n"));
10298 else
10299 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10301 if (sec->sh_type == sec_type)
10303 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10304 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10305 "contains %lu entry:\n",
10306 "\nUnwind section '%s' at offset 0x%lx "
10307 "contains %lu entries:\n",
10308 num_unwind),
10309 printable_section_name (filedata, sec),
10310 (unsigned long) sec->sh_offset,
10311 num_unwind);
10313 if (! dump_arm_unwind (filedata, &aux, sec))
10314 res = false;
10318 free (aux.symtab);
10319 free ((char *) aux.strtab);
10321 return res;
10324 static bool
10325 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10327 printf (_("No processor specific unwind information to decode\n"));
10328 return true;
10331 static bool
10332 process_unwind (Filedata * filedata)
10334 struct unwind_handler
10336 unsigned int machtype;
10337 bool (* handler)(Filedata *);
10338 } handlers[] =
10340 { EM_ARM, arm_process_unwind },
10341 { EM_IA_64, ia64_process_unwind },
10342 { EM_PARISC, hppa_process_unwind },
10343 { EM_TI_C6000, arm_process_unwind },
10344 { EM_386, no_processor_specific_unwind },
10345 { EM_X86_64, no_processor_specific_unwind },
10346 { 0, NULL }
10348 int i;
10350 if (!do_unwind)
10351 return true;
10353 for (i = 0; handlers[i].handler != NULL; i++)
10354 if (filedata->file_header.e_machine == handlers[i].machtype)
10355 return handlers[i].handler (filedata);
10357 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10358 get_machine_name (filedata->file_header.e_machine));
10359 return true;
10362 static void
10363 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10365 switch (entry->d_tag)
10367 case DT_AARCH64_BTI_PLT:
10368 case DT_AARCH64_PAC_PLT:
10369 break;
10370 default:
10371 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10372 break;
10374 putchar ('\n');
10377 static void
10378 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10380 switch (entry->d_tag)
10382 case DT_MIPS_FLAGS:
10383 if (entry->d_un.d_val == 0)
10384 printf (_("NONE"));
10385 else
10387 static const char * opts[] =
10389 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10390 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10391 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10392 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10393 "RLD_ORDER_SAFE"
10395 unsigned int cnt;
10396 bool first = true;
10398 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10399 if (entry->d_un.d_val & (1 << cnt))
10401 printf ("%s%s", first ? "" : " ", opts[cnt]);
10402 first = false;
10405 break;
10407 case DT_MIPS_IVERSION:
10408 if (valid_dynamic_name (filedata, entry->d_un.d_val))
10409 printf (_("Interface Version: %s"),
10410 get_dynamic_name (filedata, entry->d_un.d_val));
10411 else
10413 char buf[40];
10414 sprintf_vma (buf, entry->d_un.d_ptr);
10415 /* Note: coded this way so that there is a single string for translation. */
10416 printf (_("<corrupt: %s>"), buf);
10418 break;
10420 case DT_MIPS_TIME_STAMP:
10422 char timebuf[128];
10423 struct tm * tmp;
10424 time_t atime = entry->d_un.d_val;
10426 tmp = gmtime (&atime);
10427 /* PR 17531: file: 6accc532. */
10428 if (tmp == NULL)
10429 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10430 else
10431 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10432 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10433 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10434 printf (_("Time Stamp: %s"), timebuf);
10436 break;
10438 case DT_MIPS_RLD_VERSION:
10439 case DT_MIPS_LOCAL_GOTNO:
10440 case DT_MIPS_CONFLICTNO:
10441 case DT_MIPS_LIBLISTNO:
10442 case DT_MIPS_SYMTABNO:
10443 case DT_MIPS_UNREFEXTNO:
10444 case DT_MIPS_HIPAGENO:
10445 case DT_MIPS_DELTA_CLASS_NO:
10446 case DT_MIPS_DELTA_INSTANCE_NO:
10447 case DT_MIPS_DELTA_RELOC_NO:
10448 case DT_MIPS_DELTA_SYM_NO:
10449 case DT_MIPS_DELTA_CLASSSYM_NO:
10450 case DT_MIPS_COMPACT_SIZE:
10451 print_vma (entry->d_un.d_val, DEC);
10452 break;
10454 case DT_MIPS_XHASH:
10455 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10456 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10457 /* Falls through. */
10459 default:
10460 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10462 putchar ('\n');
10465 static void
10466 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10468 switch (entry->d_tag)
10470 case DT_HP_DLD_FLAGS:
10472 static struct
10474 long int bit;
10475 const char * str;
10477 flags[] =
10479 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10480 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10481 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10482 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10483 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10484 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10485 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10486 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10487 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10488 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10489 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10490 { DT_HP_GST, "HP_GST" },
10491 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10492 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10493 { DT_HP_NODELETE, "HP_NODELETE" },
10494 { DT_HP_GROUP, "HP_GROUP" },
10495 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10497 bool first = true;
10498 size_t cnt;
10499 bfd_vma val = entry->d_un.d_val;
10501 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10502 if (val & flags[cnt].bit)
10504 if (! first)
10505 putchar (' ');
10506 fputs (flags[cnt].str, stdout);
10507 first = false;
10508 val ^= flags[cnt].bit;
10511 if (val != 0 || first)
10513 if (! first)
10514 putchar (' ');
10515 print_vma (val, HEX);
10518 break;
10520 default:
10521 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10522 break;
10524 putchar ('\n');
10527 #ifdef BFD64
10529 /* VMS vs Unix time offset and factor. */
10531 #define VMS_EPOCH_OFFSET 35067168000000000LL
10532 #define VMS_GRANULARITY_FACTOR 10000000
10533 #ifndef INT64_MIN
10534 #define INT64_MIN (-9223372036854775807LL - 1)
10535 #endif
10537 /* Display a VMS time in a human readable format. */
10539 static void
10540 print_vms_time (bfd_int64_t vmstime)
10542 struct tm *tm = NULL;
10543 time_t unxtime;
10545 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10547 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10548 unxtime = vmstime;
10549 if (unxtime == vmstime)
10550 tm = gmtime (&unxtime);
10552 if (tm != NULL)
10553 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10554 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10555 tm->tm_hour, tm->tm_min, tm->tm_sec);
10557 #endif /* BFD64 */
10559 static void
10560 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10562 switch (entry->d_tag)
10564 case DT_IA_64_PLT_RESERVE:
10565 /* First 3 slots reserved. */
10566 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10567 printf (" -- ");
10568 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10569 break;
10571 case DT_IA_64_VMS_LINKTIME:
10572 #ifdef BFD64
10573 print_vms_time (entry->d_un.d_val);
10574 #endif
10575 break;
10577 case DT_IA_64_VMS_LNKFLAGS:
10578 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10579 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10580 printf (" CALL_DEBUG");
10581 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10582 printf (" NOP0BUFS");
10583 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10584 printf (" P0IMAGE");
10585 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10586 printf (" MKTHREADS");
10587 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10588 printf (" UPCALLS");
10589 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10590 printf (" IMGSTA");
10591 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10592 printf (" INITIALIZE");
10593 if (entry->d_un.d_val & VMS_LF_MAIN)
10594 printf (" MAIN");
10595 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10596 printf (" EXE_INIT");
10597 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10598 printf (" TBK_IN_IMG");
10599 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10600 printf (" DBG_IN_IMG");
10601 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10602 printf (" TBK_IN_DSF");
10603 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10604 printf (" DBG_IN_DSF");
10605 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10606 printf (" SIGNATURES");
10607 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10608 printf (" REL_SEG_OFF");
10609 break;
10611 default:
10612 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10613 break;
10615 putchar ('\n');
10618 static bool
10619 get_32bit_dynamic_section (Filedata * filedata)
10621 Elf32_External_Dyn * edyn;
10622 Elf32_External_Dyn * ext;
10623 Elf_Internal_Dyn * entry;
10625 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10626 filedata->dynamic_addr, 1,
10627 filedata->dynamic_size,
10628 _("dynamic section"));
10629 if (!edyn)
10630 return false;
10632 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10633 might not have the luxury of section headers. Look for the DT_NULL
10634 terminator to determine the number of entries. */
10635 for (ext = edyn, filedata->dynamic_nent = 0;
10636 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10637 ext++)
10639 filedata->dynamic_nent++;
10640 if (BYTE_GET (ext->d_tag) == DT_NULL)
10641 break;
10644 filedata->dynamic_section
10645 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10646 if (filedata->dynamic_section == NULL)
10648 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10649 (unsigned long) filedata->dynamic_nent);
10650 free (edyn);
10651 return false;
10654 for (ext = edyn, entry = filedata->dynamic_section;
10655 entry < filedata->dynamic_section + filedata->dynamic_nent;
10656 ext++, entry++)
10658 entry->d_tag = BYTE_GET (ext->d_tag);
10659 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10662 free (edyn);
10664 return true;
10667 static bool
10668 get_64bit_dynamic_section (Filedata * filedata)
10670 Elf64_External_Dyn * edyn;
10671 Elf64_External_Dyn * ext;
10672 Elf_Internal_Dyn * entry;
10674 /* Read in the data. */
10675 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10676 filedata->dynamic_addr, 1,
10677 filedata->dynamic_size,
10678 _("dynamic section"));
10679 if (!edyn)
10680 return false;
10682 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10683 might not have the luxury of section headers. Look for the DT_NULL
10684 terminator to determine the number of entries. */
10685 for (ext = edyn, filedata->dynamic_nent = 0;
10686 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10687 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10688 ext++)
10690 filedata->dynamic_nent++;
10691 if (BYTE_GET (ext->d_tag) == DT_NULL)
10692 break;
10695 filedata->dynamic_section
10696 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10697 if (filedata->dynamic_section == NULL)
10699 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10700 (unsigned long) filedata->dynamic_nent);
10701 free (edyn);
10702 return false;
10705 /* Convert from external to internal formats. */
10706 for (ext = edyn, entry = filedata->dynamic_section;
10707 entry < filedata->dynamic_section + filedata->dynamic_nent;
10708 ext++, entry++)
10710 entry->d_tag = BYTE_GET (ext->d_tag);
10711 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10714 free (edyn);
10716 return true;
10719 static bool
10720 get_dynamic_section (Filedata *filedata)
10722 if (filedata->dynamic_section)
10723 return true;
10725 if (is_32bit_elf)
10726 return get_32bit_dynamic_section (filedata);
10727 else
10728 return get_64bit_dynamic_section (filedata);
10731 static void
10732 print_dynamic_flags (bfd_vma flags)
10734 bool first = true;
10736 while (flags)
10738 bfd_vma flag;
10740 flag = flags & - flags;
10741 flags &= ~ flag;
10743 if (first)
10744 first = false;
10745 else
10746 putc (' ', stdout);
10748 switch (flag)
10750 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10751 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10752 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10753 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10754 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10755 default: fputs (_("unknown"), stdout); break;
10758 puts ("");
10761 static bfd_vma *
10762 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10764 unsigned char * e_data;
10765 bfd_vma * i_data;
10767 /* If the size_t type is smaller than the bfd_size_type, eg because
10768 you are building a 32-bit tool on a 64-bit host, then make sure
10769 that when (number) is cast to (size_t) no information is lost. */
10770 if (sizeof (size_t) < sizeof (bfd_size_type)
10771 && (bfd_size_type) ((size_t) number) != number)
10773 error (_("Size truncation prevents reading %s elements of size %u\n"),
10774 bfd_vmatoa ("u", number), ent_size);
10775 return NULL;
10778 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10779 attempting to allocate memory when the read is bound to fail. */
10780 if (ent_size * number > filedata->file_size)
10782 error (_("Invalid number of dynamic entries: %s\n"),
10783 bfd_vmatoa ("u", number));
10784 return NULL;
10787 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10788 if (e_data == NULL)
10790 error (_("Out of memory reading %s dynamic entries\n"),
10791 bfd_vmatoa ("u", number));
10792 return NULL;
10795 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10797 error (_("Unable to read in %s bytes of dynamic data\n"),
10798 bfd_vmatoa ("u", number * ent_size));
10799 free (e_data);
10800 return NULL;
10803 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10804 if (i_data == NULL)
10806 error (_("Out of memory allocating space for %s dynamic entries\n"),
10807 bfd_vmatoa ("u", number));
10808 free (e_data);
10809 return NULL;
10812 while (number--)
10813 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10815 free (e_data);
10817 return i_data;
10820 static unsigned long
10821 get_num_dynamic_syms (Filedata * filedata)
10823 unsigned long num_of_syms = 0;
10825 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10826 return num_of_syms;
10828 if (filedata->dynamic_info[DT_HASH])
10830 unsigned char nb[8];
10831 unsigned char nc[8];
10832 unsigned int hash_ent_size = 4;
10834 if ((filedata->file_header.e_machine == EM_ALPHA
10835 || filedata->file_header.e_machine == EM_S390
10836 || filedata->file_header.e_machine == EM_S390_OLD)
10837 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10838 hash_ent_size = 8;
10840 if (fseek (filedata->handle,
10841 (filedata->archive_file_offset
10842 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10843 sizeof nb + sizeof nc)),
10844 SEEK_SET))
10846 error (_("Unable to seek to start of dynamic information\n"));
10847 goto no_hash;
10850 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10852 error (_("Failed to read in number of buckets\n"));
10853 goto no_hash;
10856 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10858 error (_("Failed to read in number of chains\n"));
10859 goto no_hash;
10862 filedata->nbuckets = byte_get (nb, hash_ent_size);
10863 filedata->nchains = byte_get (nc, hash_ent_size);
10865 if (filedata->nbuckets != 0 && filedata->nchains != 0)
10867 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10868 hash_ent_size);
10869 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
10870 hash_ent_size);
10872 if (filedata->buckets != NULL && filedata->chains != NULL)
10873 num_of_syms = filedata->nchains;
10875 no_hash:
10876 if (num_of_syms == 0)
10878 free (filedata->buckets);
10879 filedata->buckets = NULL;
10880 free (filedata->chains);
10881 filedata->chains = NULL;
10882 filedata->nbuckets = 0;
10886 if (filedata->dynamic_info_DT_GNU_HASH)
10888 unsigned char nb[16];
10889 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10890 bfd_vma buckets_vma;
10891 unsigned long hn;
10893 if (fseek (filedata->handle,
10894 (filedata->archive_file_offset
10895 + offset_from_vma (filedata,
10896 filedata->dynamic_info_DT_GNU_HASH,
10897 sizeof nb)),
10898 SEEK_SET))
10900 error (_("Unable to seek to start of dynamic information\n"));
10901 goto no_gnu_hash;
10904 if (fread (nb, 16, 1, filedata->handle) != 1)
10906 error (_("Failed to read in number of buckets\n"));
10907 goto no_gnu_hash;
10910 filedata->ngnubuckets = byte_get (nb, 4);
10911 filedata->gnusymidx = byte_get (nb + 4, 4);
10912 bitmaskwords = byte_get (nb + 8, 4);
10913 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10914 if (is_32bit_elf)
10915 buckets_vma += bitmaskwords * 4;
10916 else
10917 buckets_vma += bitmaskwords * 8;
10919 if (fseek (filedata->handle,
10920 (filedata->archive_file_offset
10921 + offset_from_vma (filedata, buckets_vma, 4)),
10922 SEEK_SET))
10924 error (_("Unable to seek to start of dynamic information\n"));
10925 goto no_gnu_hash;
10928 filedata->gnubuckets
10929 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10931 if (filedata->gnubuckets == NULL)
10932 goto no_gnu_hash;
10934 for (i = 0; i < filedata->ngnubuckets; i++)
10935 if (filedata->gnubuckets[i] != 0)
10937 if (filedata->gnubuckets[i] < filedata->gnusymidx)
10938 goto no_gnu_hash;
10940 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10941 maxchain = filedata->gnubuckets[i];
10944 if (maxchain == 0xffffffff)
10945 goto no_gnu_hash;
10947 maxchain -= filedata->gnusymidx;
10949 if (fseek (filedata->handle,
10950 (filedata->archive_file_offset
10951 + offset_from_vma (filedata,
10952 buckets_vma + 4 * (filedata->ngnubuckets
10953 + maxchain),
10954 4)),
10955 SEEK_SET))
10957 error (_("Unable to seek to start of dynamic information\n"));
10958 goto no_gnu_hash;
10963 if (fread (nb, 4, 1, filedata->handle) != 1)
10965 error (_("Failed to determine last chain length\n"));
10966 goto no_gnu_hash;
10969 if (maxchain + 1 == 0)
10970 goto no_gnu_hash;
10972 ++maxchain;
10974 while ((byte_get (nb, 4) & 1) == 0);
10976 if (fseek (filedata->handle,
10977 (filedata->archive_file_offset
10978 + offset_from_vma (filedata, (buckets_vma
10979 + 4 * filedata->ngnubuckets),
10980 4)),
10981 SEEK_SET))
10983 error (_("Unable to seek to start of dynamic information\n"));
10984 goto no_gnu_hash;
10987 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10988 filedata->ngnuchains = maxchain;
10990 if (filedata->gnuchains == NULL)
10991 goto no_gnu_hash;
10993 if (filedata->dynamic_info_DT_MIPS_XHASH)
10995 if (fseek (filedata->handle,
10996 (filedata->archive_file_offset
10997 + offset_from_vma (filedata, (buckets_vma
10998 + 4 * (filedata->ngnubuckets
10999 + maxchain)), 4)),
11000 SEEK_SET))
11002 error (_("Unable to seek to start of dynamic information\n"));
11003 goto no_gnu_hash;
11006 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11007 if (filedata->mipsxlat == NULL)
11008 goto no_gnu_hash;
11011 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11012 if (filedata->gnubuckets[hn] != 0)
11014 bfd_vma si = filedata->gnubuckets[hn];
11015 bfd_vma off = si - filedata->gnusymidx;
11019 if (filedata->dynamic_info_DT_MIPS_XHASH)
11021 if (off < filedata->ngnuchains
11022 && filedata->mipsxlat[off] >= num_of_syms)
11023 num_of_syms = filedata->mipsxlat[off] + 1;
11025 else
11027 if (si >= num_of_syms)
11028 num_of_syms = si + 1;
11030 si++;
11032 while (off < filedata->ngnuchains
11033 && (filedata->gnuchains[off++] & 1) == 0);
11036 if (num_of_syms == 0)
11038 no_gnu_hash:
11039 free (filedata->mipsxlat);
11040 filedata->mipsxlat = NULL;
11041 free (filedata->gnuchains);
11042 filedata->gnuchains = NULL;
11043 free (filedata->gnubuckets);
11044 filedata->gnubuckets = NULL;
11045 filedata->ngnubuckets = 0;
11046 filedata->ngnuchains = 0;
11050 return num_of_syms;
11053 /* Parse and display the contents of the dynamic section. */
11055 static bool
11056 process_dynamic_section (Filedata * filedata)
11058 Elf_Internal_Dyn * entry;
11060 if (filedata->dynamic_size <= 1)
11062 if (do_dynamic)
11064 if (filedata->is_separate)
11065 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11066 filedata->file_name);
11067 else
11068 printf (_("\nThere is no dynamic section in this file.\n"));
11071 return true;
11074 if (!get_dynamic_section (filedata))
11075 return false;
11077 /* Find the appropriate symbol table. */
11078 if (filedata->dynamic_symbols == NULL || do_histogram)
11080 unsigned long num_of_syms;
11082 for (entry = filedata->dynamic_section;
11083 entry < filedata->dynamic_section + filedata->dynamic_nent;
11084 ++entry)
11085 if (entry->d_tag == DT_SYMTAB)
11086 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11087 else if (entry->d_tag == DT_SYMENT)
11088 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11089 else if (entry->d_tag == DT_HASH)
11090 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11091 else if (entry->d_tag == DT_GNU_HASH)
11092 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11093 else if ((filedata->file_header.e_machine == EM_MIPS
11094 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11095 && entry->d_tag == DT_MIPS_XHASH)
11097 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11098 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11101 num_of_syms = get_num_dynamic_syms (filedata);
11103 if (num_of_syms != 0
11104 && filedata->dynamic_symbols == NULL
11105 && filedata->dynamic_info[DT_SYMTAB]
11106 && filedata->dynamic_info[DT_SYMENT])
11108 Elf_Internal_Phdr *seg;
11109 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
11111 if (! get_program_headers (filedata))
11113 error (_("Cannot interpret virtual addresses "
11114 "without program headers.\n"));
11115 return false;
11118 for (seg = filedata->program_headers;
11119 seg < filedata->program_headers + filedata->file_header.e_phnum;
11120 ++seg)
11122 if (seg->p_type != PT_LOAD)
11123 continue;
11125 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11127 /* See PR 21379 for a reproducer. */
11128 error (_("Invalid PT_LOAD entry\n"));
11129 return false;
11132 if (vma >= (seg->p_vaddr & -seg->p_align)
11133 && vma < seg->p_vaddr + seg->p_filesz)
11135 /* Since we do not know how big the symbol table is,
11136 we default to reading in up to the end of PT_LOAD
11137 segment and processing that. This is overkill, I
11138 know, but it should work. */
11139 Elf_Internal_Shdr section;
11140 section.sh_offset = (vma - seg->p_vaddr
11141 + seg->p_offset);
11142 section.sh_size = (num_of_syms
11143 * filedata->dynamic_info[DT_SYMENT]);
11144 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11146 if (do_checks
11147 && filedata->dynamic_symtab_section != NULL
11148 && ((filedata->dynamic_symtab_section->sh_offset
11149 != section.sh_offset)
11150 || (filedata->dynamic_symtab_section->sh_size
11151 != section.sh_size)
11152 || (filedata->dynamic_symtab_section->sh_entsize
11153 != section.sh_entsize)))
11154 warn (_("\
11155 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11157 section.sh_name = filedata->string_table_length;
11158 filedata->dynamic_symbols
11159 = get_elf_symbols (filedata, &section,
11160 &filedata->num_dynamic_syms);
11161 if (filedata->dynamic_symbols == NULL
11162 || filedata->num_dynamic_syms != num_of_syms)
11164 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11165 return false;
11167 break;
11173 /* Similarly find a string table. */
11174 if (filedata->dynamic_strings == NULL)
11175 for (entry = filedata->dynamic_section;
11176 entry < filedata->dynamic_section + filedata->dynamic_nent;
11177 ++entry)
11179 if (entry->d_tag == DT_STRTAB)
11180 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11182 if (entry->d_tag == DT_STRSZ)
11183 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11185 if (filedata->dynamic_info[DT_STRTAB]
11186 && filedata->dynamic_info[DT_STRSZ])
11188 unsigned long offset;
11189 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
11191 offset = offset_from_vma (filedata,
11192 filedata->dynamic_info[DT_STRTAB],
11193 str_tab_len);
11194 if (do_checks
11195 && filedata->dynamic_strtab_section
11196 && ((filedata->dynamic_strtab_section->sh_offset
11197 != (file_ptr) offset)
11198 || (filedata->dynamic_strtab_section->sh_size
11199 != str_tab_len)))
11200 warn (_("\
11201 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11203 filedata->dynamic_strings
11204 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11205 _("dynamic string table"));
11206 if (filedata->dynamic_strings == NULL)
11208 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11209 break;
11212 filedata->dynamic_strings_length = str_tab_len;
11213 break;
11217 /* And find the syminfo section if available. */
11218 if (filedata->dynamic_syminfo == NULL)
11220 unsigned long syminsz = 0;
11222 for (entry = filedata->dynamic_section;
11223 entry < filedata->dynamic_section + filedata->dynamic_nent;
11224 ++entry)
11226 if (entry->d_tag == DT_SYMINENT)
11228 /* Note: these braces are necessary to avoid a syntax
11229 error from the SunOS4 C compiler. */
11230 /* PR binutils/17531: A corrupt file can trigger this test.
11231 So do not use an assert, instead generate an error message. */
11232 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11233 error (_("Bad value (%d) for SYMINENT entry\n"),
11234 (int) entry->d_un.d_val);
11236 else if (entry->d_tag == DT_SYMINSZ)
11237 syminsz = entry->d_un.d_val;
11238 else if (entry->d_tag == DT_SYMINFO)
11239 filedata->dynamic_syminfo_offset
11240 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11243 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11245 Elf_External_Syminfo * extsyminfo;
11246 Elf_External_Syminfo * extsym;
11247 Elf_Internal_Syminfo * syminfo;
11249 /* There is a syminfo section. Read the data. */
11250 extsyminfo = (Elf_External_Syminfo *)
11251 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11252 1, syminsz, _("symbol information"));
11253 if (!extsyminfo)
11254 return false;
11256 if (filedata->dynamic_syminfo != NULL)
11258 error (_("Multiple dynamic symbol information sections found\n"));
11259 free (filedata->dynamic_syminfo);
11261 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11262 if (filedata->dynamic_syminfo == NULL)
11264 error (_("Out of memory allocating %lu bytes "
11265 "for dynamic symbol info\n"),
11266 (unsigned long) syminsz);
11267 return false;
11270 filedata->dynamic_syminfo_nent
11271 = syminsz / sizeof (Elf_External_Syminfo);
11272 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
11273 syminfo < (filedata->dynamic_syminfo
11274 + filedata->dynamic_syminfo_nent);
11275 ++syminfo, ++extsym)
11277 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11278 syminfo->si_flags = BYTE_GET (extsym->si_flags);
11281 free (extsyminfo);
11285 if (do_dynamic && filedata->dynamic_addr)
11287 if (filedata->is_separate)
11288 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11289 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11290 (unsigned long) filedata->dynamic_nent),
11291 filedata->file_name,
11292 filedata->dynamic_addr,
11293 (unsigned long) filedata->dynamic_nent);
11294 else
11295 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11296 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11297 (unsigned long) filedata->dynamic_nent),
11298 filedata->dynamic_addr,
11299 (unsigned long) filedata->dynamic_nent);
11301 if (do_dynamic)
11302 printf (_(" Tag Type Name/Value\n"));
11304 for (entry = filedata->dynamic_section;
11305 entry < filedata->dynamic_section + filedata->dynamic_nent;
11306 entry++)
11308 if (do_dynamic)
11310 const char * dtype;
11312 putchar (' ');
11313 print_vma (entry->d_tag, FULL_HEX);
11314 dtype = get_dynamic_type (filedata, entry->d_tag);
11315 printf (" (%s)%*s", dtype,
11316 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11319 switch (entry->d_tag)
11321 case DT_FLAGS:
11322 if (do_dynamic)
11323 print_dynamic_flags (entry->d_un.d_val);
11324 break;
11326 case DT_AUXILIARY:
11327 case DT_FILTER:
11328 case DT_CONFIG:
11329 case DT_DEPAUDIT:
11330 case DT_AUDIT:
11331 if (do_dynamic)
11333 switch (entry->d_tag)
11335 case DT_AUXILIARY:
11336 printf (_("Auxiliary library"));
11337 break;
11339 case DT_FILTER:
11340 printf (_("Filter library"));
11341 break;
11343 case DT_CONFIG:
11344 printf (_("Configuration file"));
11345 break;
11347 case DT_DEPAUDIT:
11348 printf (_("Dependency audit library"));
11349 break;
11351 case DT_AUDIT:
11352 printf (_("Audit library"));
11353 break;
11356 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11357 printf (": [%s]\n",
11358 get_dynamic_name (filedata, entry->d_un.d_val));
11359 else
11361 printf (": ");
11362 print_vma (entry->d_un.d_val, PREFIX_HEX);
11363 putchar ('\n');
11366 break;
11368 case DT_FEATURE:
11369 if (do_dynamic)
11371 printf (_("Flags:"));
11373 if (entry->d_un.d_val == 0)
11374 printf (_(" None\n"));
11375 else
11377 unsigned long int val = entry->d_un.d_val;
11379 if (val & DTF_1_PARINIT)
11381 printf (" PARINIT");
11382 val ^= DTF_1_PARINIT;
11384 if (val & DTF_1_CONFEXP)
11386 printf (" CONFEXP");
11387 val ^= DTF_1_CONFEXP;
11389 if (val != 0)
11390 printf (" %lx", val);
11391 puts ("");
11394 break;
11396 case DT_POSFLAG_1:
11397 if (do_dynamic)
11399 printf (_("Flags:"));
11401 if (entry->d_un.d_val == 0)
11402 printf (_(" None\n"));
11403 else
11405 unsigned long int val = entry->d_un.d_val;
11407 if (val & DF_P1_LAZYLOAD)
11409 printf (" LAZYLOAD");
11410 val ^= DF_P1_LAZYLOAD;
11412 if (val & DF_P1_GROUPPERM)
11414 printf (" GROUPPERM");
11415 val ^= DF_P1_GROUPPERM;
11417 if (val != 0)
11418 printf (" %lx", val);
11419 puts ("");
11422 break;
11424 case DT_FLAGS_1:
11425 if (do_dynamic)
11427 printf (_("Flags:"));
11428 if (entry->d_un.d_val == 0)
11429 printf (_(" None\n"));
11430 else
11432 unsigned long int val = entry->d_un.d_val;
11434 if (val & DF_1_NOW)
11436 printf (" NOW");
11437 val ^= DF_1_NOW;
11439 if (val & DF_1_GLOBAL)
11441 printf (" GLOBAL");
11442 val ^= DF_1_GLOBAL;
11444 if (val & DF_1_GROUP)
11446 printf (" GROUP");
11447 val ^= DF_1_GROUP;
11449 if (val & DF_1_NODELETE)
11451 printf (" NODELETE");
11452 val ^= DF_1_NODELETE;
11454 if (val & DF_1_LOADFLTR)
11456 printf (" LOADFLTR");
11457 val ^= DF_1_LOADFLTR;
11459 if (val & DF_1_INITFIRST)
11461 printf (" INITFIRST");
11462 val ^= DF_1_INITFIRST;
11464 if (val & DF_1_NOOPEN)
11466 printf (" NOOPEN");
11467 val ^= DF_1_NOOPEN;
11469 if (val & DF_1_ORIGIN)
11471 printf (" ORIGIN");
11472 val ^= DF_1_ORIGIN;
11474 if (val & DF_1_DIRECT)
11476 printf (" DIRECT");
11477 val ^= DF_1_DIRECT;
11479 if (val & DF_1_TRANS)
11481 printf (" TRANS");
11482 val ^= DF_1_TRANS;
11484 if (val & DF_1_INTERPOSE)
11486 printf (" INTERPOSE");
11487 val ^= DF_1_INTERPOSE;
11489 if (val & DF_1_NODEFLIB)
11491 printf (" NODEFLIB");
11492 val ^= DF_1_NODEFLIB;
11494 if (val & DF_1_NODUMP)
11496 printf (" NODUMP");
11497 val ^= DF_1_NODUMP;
11499 if (val & DF_1_CONFALT)
11501 printf (" CONFALT");
11502 val ^= DF_1_CONFALT;
11504 if (val & DF_1_ENDFILTEE)
11506 printf (" ENDFILTEE");
11507 val ^= DF_1_ENDFILTEE;
11509 if (val & DF_1_DISPRELDNE)
11511 printf (" DISPRELDNE");
11512 val ^= DF_1_DISPRELDNE;
11514 if (val & DF_1_DISPRELPND)
11516 printf (" DISPRELPND");
11517 val ^= DF_1_DISPRELPND;
11519 if (val & DF_1_NODIRECT)
11521 printf (" NODIRECT");
11522 val ^= DF_1_NODIRECT;
11524 if (val & DF_1_IGNMULDEF)
11526 printf (" IGNMULDEF");
11527 val ^= DF_1_IGNMULDEF;
11529 if (val & DF_1_NOKSYMS)
11531 printf (" NOKSYMS");
11532 val ^= DF_1_NOKSYMS;
11534 if (val & DF_1_NOHDR)
11536 printf (" NOHDR");
11537 val ^= DF_1_NOHDR;
11539 if (val & DF_1_EDITED)
11541 printf (" EDITED");
11542 val ^= DF_1_EDITED;
11544 if (val & DF_1_NORELOC)
11546 printf (" NORELOC");
11547 val ^= DF_1_NORELOC;
11549 if (val & DF_1_SYMINTPOSE)
11551 printf (" SYMINTPOSE");
11552 val ^= DF_1_SYMINTPOSE;
11554 if (val & DF_1_GLOBAUDIT)
11556 printf (" GLOBAUDIT");
11557 val ^= DF_1_GLOBAUDIT;
11559 if (val & DF_1_SINGLETON)
11561 printf (" SINGLETON");
11562 val ^= DF_1_SINGLETON;
11564 if (val & DF_1_STUB)
11566 printf (" STUB");
11567 val ^= DF_1_STUB;
11569 if (val & DF_1_PIE)
11571 printf (" PIE");
11572 val ^= DF_1_PIE;
11574 if (val & DF_1_KMOD)
11576 printf (" KMOD");
11577 val ^= DF_1_KMOD;
11579 if (val & DF_1_WEAKFILTER)
11581 printf (" WEAKFILTER");
11582 val ^= DF_1_WEAKFILTER;
11584 if (val & DF_1_NOCOMMON)
11586 printf (" NOCOMMON");
11587 val ^= DF_1_NOCOMMON;
11589 if (val != 0)
11590 printf (" %lx", val);
11591 puts ("");
11594 break;
11596 case DT_PLTREL:
11597 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11598 if (do_dynamic)
11599 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11600 break;
11602 case DT_NULL :
11603 case DT_NEEDED :
11604 case DT_PLTGOT :
11605 case DT_HASH :
11606 case DT_STRTAB :
11607 case DT_SYMTAB :
11608 case DT_RELA :
11609 case DT_INIT :
11610 case DT_FINI :
11611 case DT_SONAME :
11612 case DT_RPATH :
11613 case DT_SYMBOLIC:
11614 case DT_REL :
11615 case DT_RELR :
11616 case DT_DEBUG :
11617 case DT_TEXTREL :
11618 case DT_JMPREL :
11619 case DT_RUNPATH :
11620 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11622 if (do_dynamic)
11624 const char *name;
11626 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11627 name = get_dynamic_name (filedata, entry->d_un.d_val);
11628 else
11629 name = NULL;
11631 if (name)
11633 switch (entry->d_tag)
11635 case DT_NEEDED:
11636 printf (_("Shared library: [%s]"), name);
11638 if (filedata->program_interpreter
11639 && streq (name, filedata->program_interpreter))
11640 printf (_(" program interpreter"));
11641 break;
11643 case DT_SONAME:
11644 printf (_("Library soname: [%s]"), name);
11645 break;
11647 case DT_RPATH:
11648 printf (_("Library rpath: [%s]"), name);
11649 break;
11651 case DT_RUNPATH:
11652 printf (_("Library runpath: [%s]"), name);
11653 break;
11655 default:
11656 print_vma (entry->d_un.d_val, PREFIX_HEX);
11657 break;
11660 else
11661 print_vma (entry->d_un.d_val, PREFIX_HEX);
11663 putchar ('\n');
11665 break;
11667 case DT_PLTRELSZ:
11668 case DT_RELASZ :
11669 case DT_STRSZ :
11670 case DT_RELSZ :
11671 case DT_RELAENT :
11672 case DT_RELRENT :
11673 case DT_RELRSZ :
11674 case DT_SYMENT :
11675 case DT_RELENT :
11676 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11677 /* Fall through. */
11678 case DT_PLTPADSZ:
11679 case DT_MOVEENT :
11680 case DT_MOVESZ :
11681 case DT_PREINIT_ARRAYSZ:
11682 case DT_INIT_ARRAYSZ:
11683 case DT_FINI_ARRAYSZ:
11684 case DT_GNU_CONFLICTSZ:
11685 case DT_GNU_LIBLISTSZ:
11686 if (do_dynamic)
11688 print_vma (entry->d_un.d_val, UNSIGNED);
11689 printf (_(" (bytes)\n"));
11691 break;
11693 case DT_VERDEFNUM:
11694 case DT_VERNEEDNUM:
11695 case DT_RELACOUNT:
11696 case DT_RELCOUNT:
11697 if (do_dynamic)
11699 print_vma (entry->d_un.d_val, UNSIGNED);
11700 putchar ('\n');
11702 break;
11704 case DT_SYMINSZ:
11705 case DT_SYMINENT:
11706 case DT_SYMINFO:
11707 case DT_USED:
11708 case DT_INIT_ARRAY:
11709 case DT_FINI_ARRAY:
11710 if (do_dynamic)
11712 if (entry->d_tag == DT_USED
11713 && valid_dynamic_name (filedata, entry->d_un.d_val))
11715 const char *name
11716 = get_dynamic_name (filedata, entry->d_un.d_val);
11718 if (*name)
11720 printf (_("Not needed object: [%s]\n"), name);
11721 break;
11725 print_vma (entry->d_un.d_val, PREFIX_HEX);
11726 putchar ('\n');
11728 break;
11730 case DT_BIND_NOW:
11731 /* The value of this entry is ignored. */
11732 if (do_dynamic)
11733 putchar ('\n');
11734 break;
11736 case DT_GNU_PRELINKED:
11737 if (do_dynamic)
11739 struct tm * tmp;
11740 time_t atime = entry->d_un.d_val;
11742 tmp = gmtime (&atime);
11743 /* PR 17533 file: 041-1244816-0.004. */
11744 if (tmp == NULL)
11745 printf (_("<corrupt time val: %lx"),
11746 (unsigned long) atime);
11747 else
11748 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11749 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11750 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11753 break;
11755 case DT_GNU_HASH:
11756 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11757 if (do_dynamic)
11759 print_vma (entry->d_un.d_val, PREFIX_HEX);
11760 putchar ('\n');
11762 break;
11764 case DT_GNU_FLAGS_1:
11765 if (do_dynamic)
11767 printf (_("Flags:"));
11768 if (entry->d_un.d_val == 0)
11769 printf (_(" None\n"));
11770 else
11772 unsigned long int val = entry->d_un.d_val;
11774 if (val & DF_GNU_1_UNIQUE)
11776 printf (" UNIQUE");
11777 val ^= DF_GNU_1_UNIQUE;
11779 if (val != 0)
11780 printf (" %lx", val);
11781 puts ("");
11784 break;
11786 default:
11787 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11788 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11789 = entry->d_un.d_val;
11791 if (do_dynamic)
11793 switch (filedata->file_header.e_machine)
11795 case EM_AARCH64:
11796 dynamic_section_aarch64_val (entry);
11797 break;
11798 case EM_MIPS:
11799 case EM_MIPS_RS3_LE:
11800 dynamic_section_mips_val (filedata, entry);
11801 break;
11802 case EM_PARISC:
11803 dynamic_section_parisc_val (entry);
11804 break;
11805 case EM_IA_64:
11806 dynamic_section_ia64_val (entry);
11807 break;
11808 default:
11809 print_vma (entry->d_un.d_val, PREFIX_HEX);
11810 putchar ('\n');
11813 break;
11817 return true;
11820 static char *
11821 get_ver_flags (unsigned int flags)
11823 static char buff[128];
11825 buff[0] = 0;
11827 if (flags == 0)
11828 return _("none");
11830 if (flags & VER_FLG_BASE)
11831 strcat (buff, "BASE");
11833 if (flags & VER_FLG_WEAK)
11835 if (flags & VER_FLG_BASE)
11836 strcat (buff, " | ");
11838 strcat (buff, "WEAK");
11841 if (flags & VER_FLG_INFO)
11843 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
11844 strcat (buff, " | ");
11846 strcat (buff, "INFO");
11849 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11851 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11852 strcat (buff, " | ");
11854 strcat (buff, _("<unknown>"));
11857 return buff;
11860 /* Display the contents of the version sections. */
11862 static bool
11863 process_version_sections (Filedata * filedata)
11865 Elf_Internal_Shdr * section;
11866 unsigned i;
11867 bool found = false;
11869 if (! do_version)
11870 return true;
11872 for (i = 0, section = filedata->section_headers;
11873 i < filedata->file_header.e_shnum;
11874 i++, section++)
11876 switch (section->sh_type)
11878 case SHT_GNU_verdef:
11880 Elf_External_Verdef * edefs;
11881 unsigned long idx;
11882 unsigned long cnt;
11883 char * endbuf;
11885 found = true;
11887 if (filedata->is_separate)
11888 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
11889 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
11890 section->sh_info),
11891 filedata->file_name,
11892 printable_section_name (filedata, section),
11893 section->sh_info);
11894 else
11895 printf (ngettext ("\nVersion definition section '%s' "
11896 "contains %u entry:\n",
11897 "\nVersion definition section '%s' "
11898 "contains %u entries:\n",
11899 section->sh_info),
11900 printable_section_name (filedata, section),
11901 section->sh_info);
11903 printf (_(" Addr: 0x"));
11904 printf_vma (section->sh_addr);
11905 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11906 (unsigned long) section->sh_offset, section->sh_link,
11907 printable_section_name_from_index (filedata, section->sh_link));
11909 edefs = (Elf_External_Verdef *)
11910 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
11911 _("version definition section"));
11912 if (!edefs)
11913 break;
11914 endbuf = (char *) edefs + section->sh_size;
11916 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11918 char * vstart;
11919 Elf_External_Verdef * edef;
11920 Elf_Internal_Verdef ent;
11921 Elf_External_Verdaux * eaux;
11922 Elf_Internal_Verdaux aux;
11923 unsigned long isum;
11924 int j;
11926 vstart = ((char *) edefs) + idx;
11927 if (vstart + sizeof (*edef) > endbuf)
11928 break;
11930 edef = (Elf_External_Verdef *) vstart;
11932 ent.vd_version = BYTE_GET (edef->vd_version);
11933 ent.vd_flags = BYTE_GET (edef->vd_flags);
11934 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
11935 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
11936 ent.vd_hash = BYTE_GET (edef->vd_hash);
11937 ent.vd_aux = BYTE_GET (edef->vd_aux);
11938 ent.vd_next = BYTE_GET (edef->vd_next);
11940 printf (_(" %#06lx: Rev: %d Flags: %s"),
11941 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11943 printf (_(" Index: %d Cnt: %d "),
11944 ent.vd_ndx, ent.vd_cnt);
11946 /* Check for overflow. */
11947 if (ent.vd_aux > (size_t) (endbuf - vstart))
11948 break;
11950 vstart += ent.vd_aux;
11952 if (vstart + sizeof (*eaux) > endbuf)
11953 break;
11954 eaux = (Elf_External_Verdaux *) vstart;
11956 aux.vda_name = BYTE_GET (eaux->vda_name);
11957 aux.vda_next = BYTE_GET (eaux->vda_next);
11959 if (valid_dynamic_name (filedata, aux.vda_name))
11960 printf (_("Name: %s\n"),
11961 get_dynamic_name (filedata, aux.vda_name));
11962 else
11963 printf (_("Name index: %ld\n"), aux.vda_name);
11965 isum = idx + ent.vd_aux;
11967 for (j = 1; j < ent.vd_cnt; j++)
11969 if (aux.vda_next < sizeof (*eaux)
11970 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11972 warn (_("Invalid vda_next field of %lx\n"),
11973 aux.vda_next);
11974 j = ent.vd_cnt;
11975 break;
11977 /* Check for overflow. */
11978 if (aux.vda_next > (size_t) (endbuf - vstart))
11979 break;
11981 isum += aux.vda_next;
11982 vstart += aux.vda_next;
11984 if (vstart + sizeof (*eaux) > endbuf)
11985 break;
11986 eaux = (Elf_External_Verdaux *) vstart;
11988 aux.vda_name = BYTE_GET (eaux->vda_name);
11989 aux.vda_next = BYTE_GET (eaux->vda_next);
11991 if (valid_dynamic_name (filedata, aux.vda_name))
11992 printf (_(" %#06lx: Parent %d: %s\n"),
11993 isum, j,
11994 get_dynamic_name (filedata, aux.vda_name));
11995 else
11996 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
11997 isum, j, aux.vda_name);
12000 if (j < ent.vd_cnt)
12001 printf (_(" Version def aux past end of section\n"));
12003 /* PR 17531:
12004 file: id:000001,src:000172+005151,op:splice,rep:2. */
12005 if (ent.vd_next < sizeof (*edef)
12006 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12008 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12009 cnt = section->sh_info;
12010 break;
12012 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12013 break;
12015 idx += ent.vd_next;
12018 if (cnt < section->sh_info)
12019 printf (_(" Version definition past end of section\n"));
12021 free (edefs);
12023 break;
12025 case SHT_GNU_verneed:
12027 Elf_External_Verneed * eneed;
12028 unsigned long idx;
12029 unsigned long cnt;
12030 char * endbuf;
12032 found = true;
12034 if (filedata->is_separate)
12035 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12036 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12037 section->sh_info),
12038 filedata->file_name,
12039 printable_section_name (filedata, section),
12040 section->sh_info);
12041 else
12042 printf (ngettext ("\nVersion needs section '%s' "
12043 "contains %u entry:\n",
12044 "\nVersion needs section '%s' "
12045 "contains %u entries:\n",
12046 section->sh_info),
12047 printable_section_name (filedata, section),
12048 section->sh_info);
12050 printf (_(" Addr: 0x"));
12051 printf_vma (section->sh_addr);
12052 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12053 (unsigned long) section->sh_offset, section->sh_link,
12054 printable_section_name_from_index (filedata, section->sh_link));
12056 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12057 section->sh_offset, 1,
12058 section->sh_size,
12059 _("Version Needs section"));
12060 if (!eneed)
12061 break;
12062 endbuf = (char *) eneed + section->sh_size;
12064 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12066 Elf_External_Verneed * entry;
12067 Elf_Internal_Verneed ent;
12068 unsigned long isum;
12069 int j;
12070 char * vstart;
12072 vstart = ((char *) eneed) + idx;
12073 if (vstart + sizeof (*entry) > endbuf)
12074 break;
12076 entry = (Elf_External_Verneed *) vstart;
12078 ent.vn_version = BYTE_GET (entry->vn_version);
12079 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12080 ent.vn_file = BYTE_GET (entry->vn_file);
12081 ent.vn_aux = BYTE_GET (entry->vn_aux);
12082 ent.vn_next = BYTE_GET (entry->vn_next);
12084 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
12086 if (valid_dynamic_name (filedata, ent.vn_file))
12087 printf (_(" File: %s"),
12088 get_dynamic_name (filedata, ent.vn_file));
12089 else
12090 printf (_(" File: %lx"), ent.vn_file);
12092 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12094 /* Check for overflow. */
12095 if (ent.vn_aux > (size_t) (endbuf - vstart))
12096 break;
12097 vstart += ent.vn_aux;
12099 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12101 Elf_External_Vernaux * eaux;
12102 Elf_Internal_Vernaux aux;
12104 if (vstart + sizeof (*eaux) > endbuf)
12105 break;
12106 eaux = (Elf_External_Vernaux *) vstart;
12108 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12109 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12110 aux.vna_other = BYTE_GET (eaux->vna_other);
12111 aux.vna_name = BYTE_GET (eaux->vna_name);
12112 aux.vna_next = BYTE_GET (eaux->vna_next);
12114 if (valid_dynamic_name (filedata, aux.vna_name))
12115 printf (_(" %#06lx: Name: %s"),
12116 isum, get_dynamic_name (filedata, aux.vna_name));
12117 else
12118 printf (_(" %#06lx: Name index: %lx"),
12119 isum, aux.vna_name);
12121 printf (_(" Flags: %s Version: %d\n"),
12122 get_ver_flags (aux.vna_flags), aux.vna_other);
12124 if (aux.vna_next < sizeof (*eaux)
12125 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12127 warn (_("Invalid vna_next field of %lx\n"),
12128 aux.vna_next);
12129 j = ent.vn_cnt;
12130 break;
12132 /* Check for overflow. */
12133 if (aux.vna_next > (size_t) (endbuf - vstart))
12134 break;
12135 isum += aux.vna_next;
12136 vstart += aux.vna_next;
12139 if (j < ent.vn_cnt)
12140 warn (_("Missing Version Needs auxiliary information\n"));
12142 if (ent.vn_next < sizeof (*entry)
12143 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12145 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12146 cnt = section->sh_info;
12147 break;
12149 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12150 break;
12151 idx += ent.vn_next;
12154 if (cnt < section->sh_info)
12155 warn (_("Missing Version Needs information\n"));
12157 free (eneed);
12159 break;
12161 case SHT_GNU_versym:
12163 Elf_Internal_Shdr * link_section;
12164 size_t total;
12165 unsigned int cnt;
12166 unsigned char * edata;
12167 unsigned short * data;
12168 char * strtab;
12169 Elf_Internal_Sym * symbols;
12170 Elf_Internal_Shdr * string_sec;
12171 unsigned long num_syms;
12172 long off;
12174 if (section->sh_link >= filedata->file_header.e_shnum)
12175 break;
12177 link_section = filedata->section_headers + section->sh_link;
12178 total = section->sh_size / sizeof (Elf_External_Versym);
12180 if (link_section->sh_link >= filedata->file_header.e_shnum)
12181 break;
12183 found = true;
12185 symbols = get_elf_symbols (filedata, link_section, & num_syms);
12186 if (symbols == NULL)
12187 break;
12189 string_sec = filedata->section_headers + link_section->sh_link;
12191 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12192 string_sec->sh_size,
12193 _("version string table"));
12194 if (!strtab)
12196 free (symbols);
12197 break;
12200 if (filedata->is_separate)
12201 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
12202 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
12203 total),
12204 filedata->file_name,
12205 printable_section_name (filedata, section),
12206 (unsigned long) total);
12207 else
12208 printf (ngettext ("\nVersion symbols section '%s' "
12209 "contains %lu entry:\n",
12210 "\nVersion symbols section '%s' "
12211 "contains %lu entries:\n",
12212 total),
12213 printable_section_name (filedata, section),
12214 (unsigned long) total);
12216 printf (_(" Addr: 0x"));
12217 printf_vma (section->sh_addr);
12218 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12219 (unsigned long) section->sh_offset, section->sh_link,
12220 printable_section_name (filedata, link_section));
12222 off = offset_from_vma (filedata,
12223 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12224 total * sizeof (short));
12225 edata = (unsigned char *) get_data (NULL, filedata, off,
12226 sizeof (short), total,
12227 _("version symbol data"));
12228 if (!edata)
12230 free (strtab);
12231 free (symbols);
12232 break;
12235 data = (short unsigned int *) cmalloc (total, sizeof (short));
12237 for (cnt = total; cnt --;)
12238 data[cnt] = byte_get (edata + cnt * sizeof (short),
12239 sizeof (short));
12241 free (edata);
12243 for (cnt = 0; cnt < total; cnt += 4)
12245 int j, nn;
12246 char *name;
12247 char *invalid = _("*invalid*");
12249 printf (" %03x:", cnt);
12251 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12252 switch (data[cnt + j])
12254 case 0:
12255 fputs (_(" 0 (*local*) "), stdout);
12256 break;
12258 case 1:
12259 fputs (_(" 1 (*global*) "), stdout);
12260 break;
12262 default:
12263 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12264 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12266 /* If this index value is greater than the size of the symbols
12267 array, break to avoid an out-of-bounds read. */
12268 if ((unsigned long)(cnt + j) >= num_syms)
12270 warn (_("invalid index into symbol array\n"));
12271 break;
12274 name = NULL;
12275 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12277 Elf_Internal_Verneed ivn;
12278 unsigned long offset;
12280 offset = offset_from_vma
12281 (filedata,
12282 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12283 sizeof (Elf_External_Verneed));
12287 Elf_Internal_Vernaux ivna;
12288 Elf_External_Verneed evn;
12289 Elf_External_Vernaux evna;
12290 unsigned long a_off;
12292 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12293 _("version need")) == NULL)
12294 break;
12296 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12297 ivn.vn_next = BYTE_GET (evn.vn_next);
12299 a_off = offset + ivn.vn_aux;
12303 if (get_data (&evna, filedata, a_off, sizeof (evna),
12304 1, _("version need aux (2)")) == NULL)
12306 ivna.vna_next = 0;
12307 ivna.vna_other = 0;
12309 else
12311 ivna.vna_next = BYTE_GET (evna.vna_next);
12312 ivna.vna_other = BYTE_GET (evna.vna_other);
12315 a_off += ivna.vna_next;
12317 while (ivna.vna_other != data[cnt + j]
12318 && ivna.vna_next != 0);
12320 if (ivna.vna_other == data[cnt + j])
12322 ivna.vna_name = BYTE_GET (evna.vna_name);
12324 if (ivna.vna_name >= string_sec->sh_size)
12325 name = invalid;
12326 else
12327 name = strtab + ivna.vna_name;
12328 break;
12331 offset += ivn.vn_next;
12333 while (ivn.vn_next);
12336 if (data[cnt + j] != 0x8001
12337 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12339 Elf_Internal_Verdef ivd;
12340 Elf_External_Verdef evd;
12341 unsigned long offset;
12343 offset = offset_from_vma
12344 (filedata,
12345 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12346 sizeof evd);
12350 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12351 _("version def")) == NULL)
12353 ivd.vd_next = 0;
12354 /* PR 17531: file: 046-1082287-0.004. */
12355 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12356 break;
12358 else
12360 ivd.vd_next = BYTE_GET (evd.vd_next);
12361 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12364 offset += ivd.vd_next;
12366 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12367 && ivd.vd_next != 0);
12369 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12371 Elf_External_Verdaux evda;
12372 Elf_Internal_Verdaux ivda;
12374 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12376 if (get_data (&evda, filedata,
12377 offset - ivd.vd_next + ivd.vd_aux,
12378 sizeof (evda), 1,
12379 _("version def aux")) == NULL)
12380 break;
12382 ivda.vda_name = BYTE_GET (evda.vda_name);
12384 if (ivda.vda_name >= string_sec->sh_size)
12385 name = invalid;
12386 else if (name != NULL && name != invalid)
12387 name = _("*both*");
12388 else
12389 name = strtab + ivda.vda_name;
12392 if (name != NULL)
12393 nn += printf ("(%s%-*s",
12394 name,
12395 12 - (int) strlen (name),
12396 ")");
12398 if (nn < 18)
12399 printf ("%*c", 18 - nn, ' ');
12402 putchar ('\n');
12405 free (data);
12406 free (strtab);
12407 free (symbols);
12409 break;
12411 default:
12412 break;
12416 if (! found)
12418 if (filedata->is_separate)
12419 printf (_("\nNo version information found in linked file '%s'.\n"),
12420 filedata->file_name);
12421 else
12422 printf (_("\nNo version information found in this file.\n"));
12425 return true;
12428 static const char *
12429 get_symbol_binding (Filedata * filedata, unsigned int binding)
12431 static char buff[64];
12433 switch (binding)
12435 case STB_LOCAL: return "LOCAL";
12436 case STB_GLOBAL: return "GLOBAL";
12437 case STB_WEAK: return "WEAK";
12438 default:
12439 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12440 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12441 binding);
12442 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12444 if (binding == STB_GNU_UNIQUE
12445 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12446 return "UNIQUE";
12447 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12449 else
12450 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12451 return buff;
12455 static const char *
12456 get_symbol_type (Filedata * filedata, unsigned int type)
12458 static char buff[64];
12460 switch (type)
12462 case STT_NOTYPE: return "NOTYPE";
12463 case STT_OBJECT: return "OBJECT";
12464 case STT_FUNC: return "FUNC";
12465 case STT_SECTION: return "SECTION";
12466 case STT_FILE: return "FILE";
12467 case STT_COMMON: return "COMMON";
12468 case STT_TLS: return "TLS";
12469 case STT_RELC: return "RELC";
12470 case STT_SRELC: return "SRELC";
12471 default:
12472 if (type >= STT_LOPROC && type <= STT_HIPROC)
12474 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12475 return "THUMB_FUNC";
12477 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12478 return "REGISTER";
12480 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12481 return "PARISC_MILLI";
12483 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12485 else if (type >= STT_LOOS && type <= STT_HIOS)
12487 if (filedata->file_header.e_machine == EM_PARISC)
12489 if (type == STT_HP_OPAQUE)
12490 return "HP_OPAQUE";
12491 if (type == STT_HP_STUB)
12492 return "HP_STUB";
12495 if (type == STT_GNU_IFUNC
12496 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12497 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12498 return "IFUNC";
12500 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12502 else
12503 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12504 return buff;
12508 static const char *
12509 get_symbol_visibility (unsigned int visibility)
12511 switch (visibility)
12513 case STV_DEFAULT: return "DEFAULT";
12514 case STV_INTERNAL: return "INTERNAL";
12515 case STV_HIDDEN: return "HIDDEN";
12516 case STV_PROTECTED: return "PROTECTED";
12517 default:
12518 error (_("Unrecognized visibility value: %u\n"), visibility);
12519 return _("<unknown>");
12523 static const char *
12524 get_alpha_symbol_other (unsigned int other)
12526 switch (other)
12528 case STO_ALPHA_NOPV: return "NOPV";
12529 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12530 default:
12531 error (_("Unrecognized alpha specific other value: %u\n"), other);
12532 return _("<unknown>");
12536 static const char *
12537 get_solaris_symbol_visibility (unsigned int visibility)
12539 switch (visibility)
12541 case 4: return "EXPORTED";
12542 case 5: return "SINGLETON";
12543 case 6: return "ELIMINATE";
12544 default: return get_symbol_visibility (visibility);
12548 static const char *
12549 get_aarch64_symbol_other (unsigned int other)
12551 static char buf[32];
12553 if (other & STO_AARCH64_VARIANT_PCS)
12555 other &= ~STO_AARCH64_VARIANT_PCS;
12556 if (other == 0)
12557 return "VARIANT_PCS";
12558 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12559 return buf;
12561 return NULL;
12564 static const char *
12565 get_mips_symbol_other (unsigned int other)
12567 switch (other)
12569 case STO_OPTIONAL: return "OPTIONAL";
12570 case STO_MIPS_PLT: return "MIPS PLT";
12571 case STO_MIPS_PIC: return "MIPS PIC";
12572 case STO_MICROMIPS: return "MICROMIPS";
12573 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12574 case STO_MIPS16: return "MIPS16";
12575 default: return NULL;
12579 static const char *
12580 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12582 if (is_ia64_vms (filedata))
12584 static char res[32];
12586 res[0] = 0;
12588 /* Function types is for images and .STB files only. */
12589 switch (filedata->file_header.e_type)
12591 case ET_DYN:
12592 case ET_EXEC:
12593 switch (VMS_ST_FUNC_TYPE (other))
12595 case VMS_SFT_CODE_ADDR:
12596 strcat (res, " CA");
12597 break;
12598 case VMS_SFT_SYMV_IDX:
12599 strcat (res, " VEC");
12600 break;
12601 case VMS_SFT_FD:
12602 strcat (res, " FD");
12603 break;
12604 case VMS_SFT_RESERVE:
12605 strcat (res, " RSV");
12606 break;
12607 default:
12608 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12609 VMS_ST_FUNC_TYPE (other));
12610 strcat (res, " <unknown>");
12611 break;
12613 break;
12614 default:
12615 break;
12617 switch (VMS_ST_LINKAGE (other))
12619 case VMS_STL_IGNORE:
12620 strcat (res, " IGN");
12621 break;
12622 case VMS_STL_RESERVE:
12623 strcat (res, " RSV");
12624 break;
12625 case VMS_STL_STD:
12626 strcat (res, " STD");
12627 break;
12628 case VMS_STL_LNK:
12629 strcat (res, " LNK");
12630 break;
12631 default:
12632 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12633 VMS_ST_LINKAGE (other));
12634 strcat (res, " <unknown>");
12635 break;
12638 if (res[0] != 0)
12639 return res + 1;
12640 else
12641 return res;
12643 return NULL;
12646 static const char *
12647 get_ppc64_symbol_other (unsigned int other)
12649 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12650 return NULL;
12652 other >>= STO_PPC64_LOCAL_BIT;
12653 if (other <= 6)
12655 static char buf[64];
12656 if (other >= 2)
12657 other = ppc64_decode_local_entry (other);
12658 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12659 return buf;
12661 return NULL;
12664 static const char *
12665 get_riscv_symbol_other (unsigned int other)
12667 static char buf[32];
12668 buf[0] = 0;
12670 if (other & STO_RISCV_VARIANT_CC)
12672 strcat (buf, _(" VARIANT_CC"));
12673 other &= ~STO_RISCV_VARIANT_CC;
12676 if (other != 0)
12677 snprintf (buf, sizeof buf, " %x", other);
12680 if (buf[0] != 0)
12681 return buf + 1;
12682 else
12683 return buf;
12686 static const char *
12687 get_symbol_other (Filedata * filedata, unsigned int other)
12689 const char * result = NULL;
12690 static char buff [64];
12692 if (other == 0)
12693 return "";
12695 switch (filedata->file_header.e_machine)
12697 case EM_ALPHA:
12698 result = get_alpha_symbol_other (other);
12699 break;
12700 case EM_AARCH64:
12701 result = get_aarch64_symbol_other (other);
12702 break;
12703 case EM_MIPS:
12704 result = get_mips_symbol_other (other);
12705 break;
12706 case EM_IA_64:
12707 result = get_ia64_symbol_other (filedata, other);
12708 break;
12709 case EM_PPC64:
12710 result = get_ppc64_symbol_other (other);
12711 break;
12712 case EM_RISCV:
12713 result = get_riscv_symbol_other (other);
12714 break;
12715 default:
12716 result = NULL;
12717 break;
12720 if (result)
12721 return result;
12723 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12724 return buff;
12727 static const char *
12728 get_symbol_index_type (Filedata * filedata, unsigned int type)
12730 static char buff[32];
12732 switch (type)
12734 case SHN_UNDEF: return "UND";
12735 case SHN_ABS: return "ABS";
12736 case SHN_COMMON: return "COM";
12737 default:
12738 if (type == SHN_IA_64_ANSI_COMMON
12739 && filedata->file_header.e_machine == EM_IA_64
12740 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12741 return "ANSI_COM";
12742 else if ((filedata->file_header.e_machine == EM_X86_64
12743 || filedata->file_header.e_machine == EM_L1OM
12744 || filedata->file_header.e_machine == EM_K1OM)
12745 && type == SHN_X86_64_LCOMMON)
12746 return "LARGE_COM";
12747 else if ((type == SHN_MIPS_SCOMMON
12748 && filedata->file_header.e_machine == EM_MIPS)
12749 || (type == SHN_TIC6X_SCOMMON
12750 && filedata->file_header.e_machine == EM_TI_C6000))
12751 return "SCOM";
12752 else if (type == SHN_MIPS_SUNDEFINED
12753 && filedata->file_header.e_machine == EM_MIPS)
12754 return "SUND";
12755 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12756 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12757 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12758 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12759 else if (type >= SHN_LORESERVE)
12760 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12761 else if (filedata->file_header.e_shnum != 0
12762 && type >= filedata->file_header.e_shnum)
12763 sprintf (buff, _("bad section index[%3d]"), type);
12764 else
12765 sprintf (buff, "%3d", type);
12766 break;
12769 return buff;
12772 static const char *
12773 get_symbol_version_string (Filedata * filedata,
12774 bool is_dynsym,
12775 const char * strtab,
12776 unsigned long int strtab_size,
12777 unsigned int si,
12778 Elf_Internal_Sym * psym,
12779 enum versioned_symbol_info * sym_info,
12780 unsigned short * vna_other)
12782 unsigned char data[2];
12783 unsigned short vers_data;
12784 unsigned long offset;
12785 unsigned short max_vd_ndx;
12787 if (!is_dynsym
12788 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12789 return NULL;
12791 offset = offset_from_vma (filedata,
12792 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12793 sizeof data + si * sizeof (vers_data));
12795 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12796 sizeof (data), 1, _("version data")) == NULL)
12797 return NULL;
12799 vers_data = byte_get (data, 2);
12801 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12802 return NULL;
12804 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12805 max_vd_ndx = 0;
12807 /* Usually we'd only see verdef for defined symbols, and verneed for
12808 undefined symbols. However, symbols defined by the linker in
12809 .dynbss for variables copied from a shared library in order to
12810 avoid text relocations are defined yet have verneed. We could
12811 use a heuristic to detect the special case, for example, check
12812 for verneed first on symbols defined in SHT_NOBITS sections, but
12813 it is simpler and more reliable to just look for both verdef and
12814 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
12816 if (psym->st_shndx != SHN_UNDEF
12817 && vers_data != 0x8001
12818 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12820 Elf_Internal_Verdef ivd;
12821 Elf_Internal_Verdaux ivda;
12822 Elf_External_Verdaux evda;
12823 unsigned long off;
12825 off = offset_from_vma (filedata,
12826 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12827 sizeof (Elf_External_Verdef));
12831 Elf_External_Verdef evd;
12833 if (get_data (&evd, filedata, off, sizeof (evd), 1,
12834 _("version def")) == NULL)
12836 ivd.vd_ndx = 0;
12837 ivd.vd_aux = 0;
12838 ivd.vd_next = 0;
12839 ivd.vd_flags = 0;
12841 else
12843 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12844 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12845 ivd.vd_next = BYTE_GET (evd.vd_next);
12846 ivd.vd_flags = BYTE_GET (evd.vd_flags);
12849 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12850 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12852 off += ivd.vd_next;
12854 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
12856 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12858 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
12859 return NULL;
12861 off -= ivd.vd_next;
12862 off += ivd.vd_aux;
12864 if (get_data (&evda, filedata, off, sizeof (evda), 1,
12865 _("version def aux")) != NULL)
12867 ivda.vda_name = BYTE_GET (evda.vda_name);
12869 if (psym->st_name != ivda.vda_name)
12870 return (ivda.vda_name < strtab_size
12871 ? strtab + ivda.vda_name : _("<corrupt>"));
12876 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12878 Elf_External_Verneed evn;
12879 Elf_Internal_Verneed ivn;
12880 Elf_Internal_Vernaux ivna;
12882 offset = offset_from_vma (filedata,
12883 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12884 sizeof evn);
12887 unsigned long vna_off;
12889 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12890 _("version need")) == NULL)
12892 ivna.vna_next = 0;
12893 ivna.vna_other = 0;
12894 ivna.vna_name = 0;
12895 break;
12898 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12899 ivn.vn_next = BYTE_GET (evn.vn_next);
12901 vna_off = offset + ivn.vn_aux;
12905 Elf_External_Vernaux evna;
12907 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
12908 _("version need aux (3)")) == NULL)
12910 ivna.vna_next = 0;
12911 ivna.vna_other = 0;
12912 ivna.vna_name = 0;
12914 else
12916 ivna.vna_other = BYTE_GET (evna.vna_other);
12917 ivna.vna_next = BYTE_GET (evna.vna_next);
12918 ivna.vna_name = BYTE_GET (evna.vna_name);
12921 vna_off += ivna.vna_next;
12923 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
12925 if (ivna.vna_other == vers_data)
12926 break;
12928 offset += ivn.vn_next;
12930 while (ivn.vn_next != 0);
12932 if (ivna.vna_other == vers_data)
12934 *sym_info = symbol_undefined;
12935 *vna_other = ivna.vna_other;
12936 return (ivna.vna_name < strtab_size
12937 ? strtab + ivna.vna_name : _("<corrupt>"));
12939 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12940 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12941 return _("<corrupt>");
12943 return NULL;
12946 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
12948 static unsigned int
12949 print_dynamic_symbol_size (bfd_vma vma, int base)
12951 switch (base)
12953 case 8:
12954 return print_vma (vma, OCTAL_5);
12956 case 10:
12957 return print_vma (vma, UNSIGNED_5);
12959 case 16:
12960 return print_vma (vma, PREFIX_HEX_5);
12962 case 0:
12963 default:
12964 return print_vma (vma, DEC_5);
12968 static void
12969 print_dynamic_symbol (Filedata *filedata, unsigned long si,
12970 Elf_Internal_Sym *symtab,
12971 Elf_Internal_Shdr *section,
12972 char *strtab, size_t strtab_size)
12974 const char *version_string;
12975 enum versioned_symbol_info sym_info;
12976 unsigned short vna_other;
12977 bool is_valid;
12978 const char * sstr;
12979 Elf_Internal_Sym *psym = symtab + si;
12981 printf ("%6ld: ", si);
12982 print_vma (psym->st_value, LONG_HEX);
12983 putchar (' ');
12984 print_dynamic_symbol_size (psym->st_size, sym_base);
12985 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12986 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12987 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12988 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
12989 else
12991 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
12993 printf (" %-7s", get_symbol_visibility (vis));
12994 /* Check to see if any other bits in the st_other field are set.
12995 Note - displaying this information disrupts the layout of the
12996 table being generated, but for the moment this case is very rare. */
12997 if (psym->st_other ^ vis)
12998 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13000 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
13002 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13003 && psym->st_shndx < filedata->file_header.e_shnum
13004 && filedata->section_headers != NULL
13005 && psym->st_name == 0)
13007 is_valid
13008 = section_name_valid (filedata,
13009 filedata->section_headers + psym->st_shndx);
13010 sstr = is_valid ?
13011 section_name_print (filedata,
13012 filedata->section_headers + psym->st_shndx)
13013 : _("<corrupt>");
13015 else
13017 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13018 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13021 version_string
13022 = get_symbol_version_string (filedata,
13023 (section == NULL
13024 || section->sh_type == SHT_DYNSYM),
13025 strtab, strtab_size, si,
13026 psym, &sym_info, &vna_other);
13028 int len_avail = 21;
13029 if (! do_wide && version_string != NULL)
13031 char buffer[16];
13033 len_avail -= 1 + strlen (version_string);
13035 if (sym_info == symbol_undefined)
13036 len_avail -= sprintf (buffer," (%d)", vna_other);
13037 else if (sym_info != symbol_hidden)
13038 len_avail -= 1;
13041 print_symbol (len_avail, sstr);
13043 if (version_string)
13045 if (sym_info == symbol_undefined)
13046 printf ("@%s (%d)", version_string, vna_other);
13047 else
13048 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13049 version_string);
13052 putchar ('\n');
13054 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13055 && section != NULL
13056 && si >= section->sh_info
13057 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13058 && filedata->file_header.e_machine != EM_MIPS
13059 /* Solaris binaries have been found to violate this requirement as
13060 well. Not sure if this is a bug or an ABI requirement. */
13061 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13062 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
13063 si, printable_section_name (filedata, section), section->sh_info);
13066 static const char *
13067 get_lto_kind (unsigned int kind)
13069 switch (kind)
13071 case 0: return "DEF";
13072 case 1: return "WEAKDEF";
13073 case 2: return "UNDEF";
13074 case 3: return "WEAKUNDEF";
13075 case 4: return "COMMON";
13076 default:
13077 break;
13080 static char buffer[30];
13081 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13082 sprintf (buffer, "<unknown: %u>", kind);
13083 return buffer;
13086 static const char *
13087 get_lto_visibility (unsigned int visibility)
13089 switch (visibility)
13091 case 0: return "DEFAULT";
13092 case 1: return "PROTECTED";
13093 case 2: return "INTERNAL";
13094 case 3: return "HIDDEN";
13095 default:
13096 break;
13099 static char buffer[30];
13100 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13101 sprintf (buffer, "<unknown: %u>", visibility);
13102 return buffer;
13105 static const char *
13106 get_lto_sym_type (unsigned int sym_type)
13108 switch (sym_type)
13110 case 0: return "UNKNOWN";
13111 case 1: return "FUNCTION";
13112 case 2: return "VARIABLE";
13113 default:
13114 break;
13117 static char buffer[30];
13118 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13119 sprintf (buffer, "<unknown: %u>", sym_type);
13120 return buffer;
13123 /* Display an LTO format symbol table.
13124 FIXME: The format of LTO symbol tables is not formalized.
13125 So this code could need changing in the future. */
13127 static bool
13128 display_lto_symtab (Filedata * filedata,
13129 Elf_Internal_Shdr * section)
13131 if (section->sh_size == 0)
13133 if (filedata->is_separate)
13134 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13135 printable_section_name (filedata, section),
13136 filedata->file_name);
13137 else
13138 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13139 printable_section_name (filedata, section));
13141 return true;
13144 if (section->sh_size > filedata->file_size)
13146 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
13147 printable_section_name (filedata, section),
13148 (unsigned long) section->sh_size);
13149 return false;
13152 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13153 section->sh_size, 1, _("LTO symbols"));
13154 if (alloced_data == NULL)
13155 return false;
13157 /* Look for extended data for the symbol table. */
13158 Elf_Internal_Shdr * ext;
13159 void * ext_data_orig = NULL;
13160 char * ext_data = NULL;
13161 char * ext_data_end = NULL;
13162 char * ext_name = NULL;
13164 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13165 (section_name (filedata, section)
13166 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13167 && ext_name != NULL /* Paranoia. */
13168 && (ext = find_section (filedata, ext_name)) != NULL)
13170 if (ext->sh_size < 3)
13171 error (_("LTO Symbol extension table '%s' is empty!\n"),
13172 printable_section_name (filedata, ext));
13173 else
13175 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13176 ext->sh_size, 1,
13177 _("LTO ext symbol data"));
13178 if (ext_data != NULL)
13180 ext_data_end = ext_data + ext->sh_size;
13181 if (* ext_data++ != 1)
13182 error (_("Unexpected version number in symbol extension table\n"));
13187 const unsigned char * data = (const unsigned char *) alloced_data;
13188 const unsigned char * end = data + section->sh_size;
13190 if (filedata->is_separate)
13191 printf (_("\nIn linked file '%s': "), filedata->file_name);
13192 else
13193 printf ("\n");
13195 if (ext_data_orig != NULL)
13197 if (do_wide)
13198 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13199 printable_section_name (filedata, section),
13200 printable_section_name (filedata, ext));
13201 else
13203 printf (_("LTO Symbol table '%s'\n"),
13204 printable_section_name (filedata, section));
13205 printf (_(" and extension table '%s' contain:\n"),
13206 printable_section_name (filedata, ext));
13209 else
13210 printf (_("LTO Symbol table '%s' contains:\n"),
13211 printable_section_name (filedata, section));
13213 /* FIXME: Add a wide version. */
13214 if (ext_data_orig != NULL)
13215 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13216 else
13217 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13219 /* FIXME: We do not handle style prefixes. */
13221 while (data < end)
13223 const unsigned char * sym_name = data;
13224 data += strnlen ((const char *) sym_name, end - data) + 1;
13225 if (data >= end)
13226 goto fail;
13228 const unsigned char * comdat_key = data;
13229 data += strnlen ((const char *) comdat_key, end - data) + 1;
13230 if (data >= end)
13231 goto fail;
13233 if (data + 2 + 8 + 4 > end)
13234 goto fail;
13236 unsigned int kind = *data++;
13237 unsigned int visibility = *data++;
13239 elf_vma size = byte_get (data, 8);
13240 data += 8;
13242 elf_vma slot = byte_get (data, 4);
13243 data += 4;
13245 if (ext_data != NULL)
13247 if (ext_data < (ext_data_end - 1))
13249 unsigned int sym_type = * ext_data ++;
13250 unsigned int sec_kind = * ext_data ++;
13252 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
13253 * comdat_key == 0 ? "-" : (char *) comdat_key,
13254 get_lto_kind (kind),
13255 get_lto_visibility (visibility),
13256 (long) size,
13257 (long) slot,
13258 get_lto_sym_type (sym_type),
13259 (long) sec_kind);
13260 print_symbol (6, (const char *) sym_name);
13262 else
13264 error (_("Ran out of LTO symbol extension data\n"));
13265 ext_data = NULL;
13266 /* FIXME: return FAIL result ? */
13269 else
13271 printf (" %10s %10s %11s %08lx %08lx _",
13272 * comdat_key == 0 ? "-" : (char *) comdat_key,
13273 get_lto_kind (kind),
13274 get_lto_visibility (visibility),
13275 (long) size,
13276 (long) slot);
13277 print_symbol (21, (const char *) sym_name);
13279 putchar ('\n');
13282 if (ext_data != NULL && ext_data < ext_data_end)
13284 error (_("Data remains in the LTO symbol extension table\n"));
13285 goto fail;
13288 free (alloced_data);
13289 free (ext_data_orig);
13290 free (ext_name);
13291 return true;
13293 fail:
13294 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13295 free (alloced_data);
13296 free (ext_data_orig);
13297 free (ext_name);
13298 return false;
13301 /* Display LTO symbol tables. */
13303 static bool
13304 process_lto_symbol_tables (Filedata * filedata)
13306 Elf_Internal_Shdr * section;
13307 unsigned int i;
13308 bool res = true;
13310 if (!do_lto_syms)
13311 return true;
13313 if (filedata->section_headers == NULL)
13314 return true;
13316 for (i = 0, section = filedata->section_headers;
13317 i < filedata->file_header.e_shnum;
13318 i++, section++)
13319 if (section_name_valid (filedata, section)
13320 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13321 res &= display_lto_symtab (filedata, section);
13323 return res;
13326 /* Dump the symbol table. */
13328 static bool
13329 process_symbol_table (Filedata * filedata)
13331 Elf_Internal_Shdr * section;
13333 if (!do_syms && !do_dyn_syms && !do_histogram)
13334 return true;
13336 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13337 && do_syms
13338 && do_using_dynamic
13339 && filedata->dynamic_strings != NULL
13340 && filedata->dynamic_symbols != NULL)
13342 unsigned long si;
13344 if (filedata->is_separate)
13346 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13347 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13348 filedata->num_dynamic_syms),
13349 filedata->file_name,
13350 filedata->num_dynamic_syms);
13352 else
13354 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13355 "\nSymbol table for image contains %lu entries:\n",
13356 filedata->num_dynamic_syms),
13357 filedata->num_dynamic_syms);
13359 if (is_32bit_elf)
13360 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13361 else
13362 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13364 for (si = 0; si < filedata->num_dynamic_syms; si++)
13365 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13366 filedata->dynamic_strings,
13367 filedata->dynamic_strings_length);
13369 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13370 && filedata->section_headers != NULL)
13372 unsigned int i;
13374 for (i = 0, section = filedata->section_headers;
13375 i < filedata->file_header.e_shnum;
13376 i++, section++)
13378 char * strtab = NULL;
13379 unsigned long int strtab_size = 0;
13380 Elf_Internal_Sym * symtab;
13381 unsigned long si, num_syms;
13383 if ((section->sh_type != SHT_SYMTAB
13384 && section->sh_type != SHT_DYNSYM)
13385 || (!do_syms
13386 && section->sh_type == SHT_SYMTAB))
13387 continue;
13389 if (section->sh_entsize == 0)
13391 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13392 printable_section_name (filedata, section));
13393 continue;
13396 num_syms = section->sh_size / section->sh_entsize;
13398 if (filedata->is_separate)
13399 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13400 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13401 num_syms),
13402 filedata->file_name,
13403 printable_section_name (filedata, section),
13404 num_syms);
13405 else
13406 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13407 "\nSymbol table '%s' contains %lu entries:\n",
13408 num_syms),
13409 printable_section_name (filedata, section),
13410 num_syms);
13412 if (is_32bit_elf)
13413 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13414 else
13415 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13417 symtab = get_elf_symbols (filedata, section, & num_syms);
13418 if (symtab == NULL)
13419 continue;
13421 if (section->sh_link == filedata->file_header.e_shstrndx)
13423 strtab = filedata->string_table;
13424 strtab_size = filedata->string_table_length;
13426 else if (section->sh_link < filedata->file_header.e_shnum)
13428 Elf_Internal_Shdr * string_sec;
13430 string_sec = filedata->section_headers + section->sh_link;
13432 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13433 1, string_sec->sh_size,
13434 _("string table"));
13435 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13438 for (si = 0; si < num_syms; si++)
13439 print_dynamic_symbol (filedata, si, symtab, section,
13440 strtab, strtab_size);
13442 free (symtab);
13443 if (strtab != filedata->string_table)
13444 free (strtab);
13447 else if (do_syms)
13448 printf
13449 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13451 if (do_histogram && filedata->buckets != NULL)
13453 unsigned long * lengths;
13454 unsigned long * counts;
13455 unsigned long hn;
13456 bfd_vma si;
13457 unsigned long maxlength = 0;
13458 unsigned long nzero_counts = 0;
13459 unsigned long nsyms = 0;
13460 char *visited;
13462 printf (ngettext ("\nHistogram for bucket list length "
13463 "(total of %lu bucket):\n",
13464 "\nHistogram for bucket list length "
13465 "(total of %lu buckets):\n",
13466 (unsigned long) filedata->nbuckets),
13467 (unsigned long) filedata->nbuckets);
13469 lengths = (unsigned long *) calloc (filedata->nbuckets,
13470 sizeof (*lengths));
13471 if (lengths == NULL)
13473 error (_("Out of memory allocating space for histogram buckets\n"));
13474 goto err_out;
13476 visited = xcmalloc (filedata->nchains, 1);
13477 memset (visited, 0, filedata->nchains);
13479 printf (_(" Length Number %% of total Coverage\n"));
13480 for (hn = 0; hn < filedata->nbuckets; ++hn)
13482 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13484 ++nsyms;
13485 if (maxlength < ++lengths[hn])
13486 ++maxlength;
13487 if (si >= filedata->nchains || visited[si])
13489 error (_("histogram chain is corrupt\n"));
13490 break;
13492 visited[si] = 1;
13495 free (visited);
13497 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13498 if (counts == NULL)
13500 free (lengths);
13501 error (_("Out of memory allocating space for histogram counts\n"));
13502 goto err_out;
13505 for (hn = 0; hn < filedata->nbuckets; ++hn)
13506 ++counts[lengths[hn]];
13508 if (filedata->nbuckets > 0)
13510 unsigned long i;
13511 printf (" 0 %-10lu (%5.1f%%)\n",
13512 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13513 for (i = 1; i <= maxlength; ++i)
13515 nzero_counts += counts[i] * i;
13516 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13517 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13518 (nzero_counts * 100.0) / nsyms);
13522 free (counts);
13523 free (lengths);
13526 free (filedata->buckets);
13527 filedata->buckets = NULL;
13528 filedata->nbuckets = 0;
13529 free (filedata->chains);
13530 filedata->chains = NULL;
13532 if (do_histogram && filedata->gnubuckets != NULL)
13534 unsigned long * lengths;
13535 unsigned long * counts;
13536 unsigned long hn;
13537 unsigned long maxlength = 0;
13538 unsigned long nzero_counts = 0;
13539 unsigned long nsyms = 0;
13541 printf (ngettext ("\nHistogram for `%s' bucket list length "
13542 "(total of %lu bucket):\n",
13543 "\nHistogram for `%s' bucket list length "
13544 "(total of %lu buckets):\n",
13545 (unsigned long) filedata->ngnubuckets),
13546 GNU_HASH_SECTION_NAME (filedata),
13547 (unsigned long) filedata->ngnubuckets);
13549 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13550 sizeof (*lengths));
13551 if (lengths == NULL)
13553 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13554 goto err_out;
13557 printf (_(" Length Number %% of total Coverage\n"));
13559 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13560 if (filedata->gnubuckets[hn] != 0)
13562 bfd_vma off, length = 1;
13564 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13565 /* PR 17531 file: 010-77222-0.004. */
13566 off < filedata->ngnuchains
13567 && (filedata->gnuchains[off] & 1) == 0;
13568 ++off)
13569 ++length;
13570 lengths[hn] = length;
13571 if (length > maxlength)
13572 maxlength = length;
13573 nsyms += length;
13576 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13577 if (counts == NULL)
13579 free (lengths);
13580 error (_("Out of memory allocating space for gnu histogram counts\n"));
13581 goto err_out;
13584 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13585 ++counts[lengths[hn]];
13587 if (filedata->ngnubuckets > 0)
13589 unsigned long j;
13590 printf (" 0 %-10lu (%5.1f%%)\n",
13591 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13592 for (j = 1; j <= maxlength; ++j)
13594 nzero_counts += counts[j] * j;
13595 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13596 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13597 (nzero_counts * 100.0) / nsyms);
13601 free (counts);
13602 free (lengths);
13604 free (filedata->gnubuckets);
13605 filedata->gnubuckets = NULL;
13606 filedata->ngnubuckets = 0;
13607 free (filedata->gnuchains);
13608 filedata->gnuchains = NULL;
13609 filedata->ngnuchains = 0;
13610 free (filedata->mipsxlat);
13611 filedata->mipsxlat = NULL;
13612 return true;
13614 err_out:
13615 free (filedata->gnubuckets);
13616 filedata->gnubuckets = NULL;
13617 filedata->ngnubuckets = 0;
13618 free (filedata->gnuchains);
13619 filedata->gnuchains = NULL;
13620 filedata->ngnuchains = 0;
13621 free (filedata->mipsxlat);
13622 filedata->mipsxlat = NULL;
13623 free (filedata->buckets);
13624 filedata->buckets = NULL;
13625 filedata->nbuckets = 0;
13626 free (filedata->chains);
13627 filedata->chains = NULL;
13628 return false;
13631 static bool
13632 process_syminfo (Filedata * filedata)
13634 unsigned int i;
13636 if (filedata->dynamic_syminfo == NULL
13637 || !do_dynamic)
13638 /* No syminfo, this is ok. */
13639 return true;
13641 /* There better should be a dynamic symbol section. */
13642 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13643 return false;
13645 if (filedata->is_separate)
13646 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13647 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13648 filedata->dynamic_syminfo_nent),
13649 filedata->file_name,
13650 filedata->dynamic_syminfo_offset,
13651 filedata->dynamic_syminfo_nent);
13652 else
13653 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13654 "contains %d entry:\n",
13655 "\nDynamic info segment at offset 0x%lx "
13656 "contains %d entries:\n",
13657 filedata->dynamic_syminfo_nent),
13658 filedata->dynamic_syminfo_offset,
13659 filedata->dynamic_syminfo_nent);
13661 printf (_(" Num: Name BoundTo Flags\n"));
13662 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13664 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13666 printf ("%4d: ", i);
13667 if (i >= filedata->num_dynamic_syms)
13668 printf (_("<corrupt index>"));
13669 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13670 print_symbol (30, get_dynamic_name (filedata,
13671 filedata->dynamic_symbols[i].st_name));
13672 else
13673 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13674 putchar (' ');
13676 switch (filedata->dynamic_syminfo[i].si_boundto)
13678 case SYMINFO_BT_SELF:
13679 fputs ("SELF ", stdout);
13680 break;
13681 case SYMINFO_BT_PARENT:
13682 fputs ("PARENT ", stdout);
13683 break;
13684 default:
13685 if (filedata->dynamic_syminfo[i].si_boundto > 0
13686 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13687 && valid_dynamic_name (filedata,
13688 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13690 print_symbol (10, get_dynamic_name (filedata,
13691 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13692 putchar (' ' );
13694 else
13695 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13696 break;
13699 if (flags & SYMINFO_FLG_DIRECT)
13700 printf (" DIRECT");
13701 if (flags & SYMINFO_FLG_PASSTHRU)
13702 printf (" PASSTHRU");
13703 if (flags & SYMINFO_FLG_COPY)
13704 printf (" COPY");
13705 if (flags & SYMINFO_FLG_LAZYLOAD)
13706 printf (" LAZYLOAD");
13708 puts ("");
13711 return true;
13714 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13715 is contained by the region START .. END. The types of ADDR, START
13716 and END should all be the same. Note both ADDR + NELEM and END
13717 point to just beyond the end of the regions that are being tested. */
13718 #define IN_RANGE(START,END,ADDR,NELEM) \
13719 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13721 /* Check to see if the given reloc needs to be handled in a target specific
13722 manner. If so then process the reloc and return TRUE otherwise return
13723 FALSE.
13725 If called with reloc == NULL, then this is a signal that reloc processing
13726 for the current section has finished, and any saved state should be
13727 discarded. */
13729 static bool
13730 target_specific_reloc_handling (Filedata * filedata,
13731 Elf_Internal_Rela * reloc,
13732 unsigned char * start,
13733 unsigned char * end,
13734 Elf_Internal_Sym * symtab,
13735 unsigned long num_syms)
13737 unsigned int reloc_type = 0;
13738 unsigned long sym_index = 0;
13740 if (reloc)
13742 reloc_type = get_reloc_type (filedata, reloc->r_info);
13743 sym_index = get_reloc_symindex (reloc->r_info);
13746 switch (filedata->file_header.e_machine)
13748 case EM_MSP430:
13749 case EM_MSP430_OLD:
13751 static Elf_Internal_Sym * saved_sym = NULL;
13753 if (reloc == NULL)
13755 saved_sym = NULL;
13756 return true;
13759 switch (reloc_type)
13761 case 10: /* R_MSP430_SYM_DIFF */
13762 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13763 if (uses_msp430x_relocs (filedata))
13764 break;
13765 /* Fall through. */
13766 case 21: /* R_MSP430X_SYM_DIFF */
13767 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13768 /* PR 21139. */
13769 if (sym_index >= num_syms)
13770 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13771 sym_index);
13772 else
13773 saved_sym = symtab + sym_index;
13774 return true;
13776 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13777 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13778 goto handle_sym_diff;
13780 case 5: /* R_MSP430_16_BYTE */
13781 case 9: /* R_MSP430_8 */
13782 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13783 if (uses_msp430x_relocs (filedata))
13784 break;
13785 goto handle_sym_diff;
13787 case 2: /* R_MSP430_ABS16 */
13788 case 15: /* R_MSP430X_ABS16 */
13789 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13790 if (! uses_msp430x_relocs (filedata))
13791 break;
13792 goto handle_sym_diff;
13794 handle_sym_diff:
13795 if (saved_sym != NULL)
13797 bfd_vma value;
13798 unsigned int reloc_size = 0;
13799 int leb_ret = 0;
13800 switch (reloc_type)
13802 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13803 reloc_size = 4;
13804 break;
13805 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13806 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13807 if (reloc->r_offset < (size_t) (end - start))
13808 read_leb128 (start + reloc->r_offset, end, false,
13809 &reloc_size, &leb_ret);
13810 break;
13811 default:
13812 reloc_size = 2;
13813 break;
13816 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
13817 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13818 "ULEB128 value\n"),
13819 (long) reloc->r_offset);
13820 else if (sym_index >= num_syms)
13821 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13822 sym_index);
13823 else
13825 value = reloc->r_addend + (symtab[sym_index].st_value
13826 - saved_sym->st_value);
13828 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13829 byte_put (start + reloc->r_offset, value, reloc_size);
13830 else
13831 /* PR 21137 */
13832 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
13833 (long) reloc->r_offset);
13836 saved_sym = NULL;
13837 return true;
13839 break;
13841 default:
13842 if (saved_sym != NULL)
13843 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13844 break;
13846 break;
13849 case EM_MN10300:
13850 case EM_CYGNUS_MN10300:
13852 static Elf_Internal_Sym * saved_sym = NULL;
13854 if (reloc == NULL)
13856 saved_sym = NULL;
13857 return true;
13860 switch (reloc_type)
13862 case 34: /* R_MN10300_ALIGN */
13863 return true;
13864 case 33: /* R_MN10300_SYM_DIFF */
13865 if (sym_index >= num_syms)
13866 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
13867 sym_index);
13868 else
13869 saved_sym = symtab + sym_index;
13870 return true;
13872 case 1: /* R_MN10300_32 */
13873 case 2: /* R_MN10300_16 */
13874 if (saved_sym != NULL)
13876 int reloc_size = reloc_type == 1 ? 4 : 2;
13877 bfd_vma value;
13879 if (sym_index >= num_syms)
13880 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
13881 sym_index);
13882 else
13884 value = reloc->r_addend + (symtab[sym_index].st_value
13885 - saved_sym->st_value);
13887 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13888 byte_put (start + reloc->r_offset, value, reloc_size);
13889 else
13890 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
13891 (long) reloc->r_offset);
13894 saved_sym = NULL;
13895 return true;
13897 break;
13898 default:
13899 if (saved_sym != NULL)
13900 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
13901 break;
13903 break;
13906 case EM_RL78:
13908 static bfd_vma saved_sym1 = 0;
13909 static bfd_vma saved_sym2 = 0;
13910 static bfd_vma value;
13912 if (reloc == NULL)
13914 saved_sym1 = saved_sym2 = 0;
13915 return true;
13918 switch (reloc_type)
13920 case 0x80: /* R_RL78_SYM. */
13921 saved_sym1 = saved_sym2;
13922 if (sym_index >= num_syms)
13923 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
13924 sym_index);
13925 else
13927 saved_sym2 = symtab[sym_index].st_value;
13928 saved_sym2 += reloc->r_addend;
13930 return true;
13932 case 0x83: /* R_RL78_OPsub. */
13933 value = saved_sym1 - saved_sym2;
13934 saved_sym2 = saved_sym1 = 0;
13935 return true;
13936 break;
13938 case 0x41: /* R_RL78_ABS32. */
13939 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
13940 byte_put (start + reloc->r_offset, value, 4);
13941 else
13942 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13943 (long) reloc->r_offset);
13944 value = 0;
13945 return true;
13947 case 0x43: /* R_RL78_ABS16. */
13948 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
13949 byte_put (start + reloc->r_offset, value, 2);
13950 else
13951 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13952 (long) reloc->r_offset);
13953 value = 0;
13954 return true;
13956 default:
13957 break;
13959 break;
13963 return false;
13966 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
13967 DWARF debug sections. This is a target specific test. Note - we do not
13968 go through the whole including-target-headers-multiple-times route, (as
13969 we have already done with <elf/h8.h>) because this would become very
13970 messy and even then this function would have to contain target specific
13971 information (the names of the relocs instead of their numeric values).
13972 FIXME: This is not the correct way to solve this problem. The proper way
13973 is to have target specific reloc sizing and typing functions created by
13974 the reloc-macros.h header, in the same way that it already creates the
13975 reloc naming functions. */
13977 static bool
13978 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13980 /* Please keep this table alpha-sorted for ease of visual lookup. */
13981 switch (filedata->file_header.e_machine)
13983 case EM_386:
13984 case EM_IAMCU:
13985 return reloc_type == 1; /* R_386_32. */
13986 case EM_68K:
13987 return reloc_type == 1; /* R_68K_32. */
13988 case EM_860:
13989 return reloc_type == 1; /* R_860_32. */
13990 case EM_960:
13991 return reloc_type == 2; /* R_960_32. */
13992 case EM_AARCH64:
13993 return (reloc_type == 258
13994 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
13995 case EM_BPF:
13996 return reloc_type == 11; /* R_BPF_DATA_32 */
13997 case EM_ADAPTEVA_EPIPHANY:
13998 return reloc_type == 3;
13999 case EM_ALPHA:
14000 return reloc_type == 1; /* R_ALPHA_REFLONG. */
14001 case EM_ARC:
14002 return reloc_type == 1; /* R_ARC_32. */
14003 case EM_ARC_COMPACT:
14004 case EM_ARC_COMPACT2:
14005 return reloc_type == 4; /* R_ARC_32. */
14006 case EM_ARM:
14007 return reloc_type == 2; /* R_ARM_ABS32 */
14008 case EM_AVR_OLD:
14009 case EM_AVR:
14010 return reloc_type == 1;
14011 case EM_BLACKFIN:
14012 return reloc_type == 0x12; /* R_byte4_data. */
14013 case EM_CRIS:
14014 return reloc_type == 3; /* R_CRIS_32. */
14015 case EM_CR16:
14016 return reloc_type == 3; /* R_CR16_NUM32. */
14017 case EM_CRX:
14018 return reloc_type == 15; /* R_CRX_NUM32. */
14019 case EM_CSKY:
14020 return reloc_type == 1; /* R_CKCORE_ADDR32. */
14021 case EM_CYGNUS_FRV:
14022 return reloc_type == 1;
14023 case EM_CYGNUS_D10V:
14024 case EM_D10V:
14025 return reloc_type == 6; /* R_D10V_32. */
14026 case EM_CYGNUS_D30V:
14027 case EM_D30V:
14028 return reloc_type == 12; /* R_D30V_32_NORMAL. */
14029 case EM_DLX:
14030 return reloc_type == 3; /* R_DLX_RELOC_32. */
14031 case EM_CYGNUS_FR30:
14032 case EM_FR30:
14033 return reloc_type == 3; /* R_FR30_32. */
14034 case EM_FT32:
14035 return reloc_type == 1; /* R_FT32_32. */
14036 case EM_H8S:
14037 case EM_H8_300:
14038 case EM_H8_300H:
14039 return reloc_type == 1; /* R_H8_DIR32. */
14040 case EM_IA_64:
14041 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14042 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14043 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14044 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
14045 case EM_IP2K_OLD:
14046 case EM_IP2K:
14047 return reloc_type == 2; /* R_IP2K_32. */
14048 case EM_IQ2000:
14049 return reloc_type == 2; /* R_IQ2000_32. */
14050 case EM_LATTICEMICO32:
14051 return reloc_type == 3; /* R_LM32_32. */
14052 case EM_LOONGARCH:
14053 return reloc_type == 1; /* R_LARCH_32. */
14054 case EM_M32C_OLD:
14055 case EM_M32C:
14056 return reloc_type == 3; /* R_M32C_32. */
14057 case EM_M32R:
14058 return reloc_type == 34; /* R_M32R_32_RELA. */
14059 case EM_68HC11:
14060 case EM_68HC12:
14061 return reloc_type == 6; /* R_M68HC11_32. */
14062 case EM_S12Z:
14063 return reloc_type == 7 || /* R_S12Z_EXT32 */
14064 reloc_type == 6; /* R_S12Z_CW32. */
14065 case EM_MCORE:
14066 return reloc_type == 1; /* R_MCORE_ADDR32. */
14067 case EM_CYGNUS_MEP:
14068 return reloc_type == 4; /* R_MEP_32. */
14069 case EM_METAG:
14070 return reloc_type == 2; /* R_METAG_ADDR32. */
14071 case EM_MICROBLAZE:
14072 return reloc_type == 1; /* R_MICROBLAZE_32. */
14073 case EM_MIPS:
14074 return reloc_type == 2; /* R_MIPS_32. */
14075 case EM_MMIX:
14076 return reloc_type == 4; /* R_MMIX_32. */
14077 case EM_CYGNUS_MN10200:
14078 case EM_MN10200:
14079 return reloc_type == 1; /* R_MN10200_32. */
14080 case EM_CYGNUS_MN10300:
14081 case EM_MN10300:
14082 return reloc_type == 1; /* R_MN10300_32. */
14083 case EM_MOXIE:
14084 return reloc_type == 1; /* R_MOXIE_32. */
14085 case EM_MSP430_OLD:
14086 case EM_MSP430:
14087 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
14088 case EM_MT:
14089 return reloc_type == 2; /* R_MT_32. */
14090 case EM_NDS32:
14091 return reloc_type == 20; /* R_NDS32_RELA. */
14092 case EM_ALTERA_NIOS2:
14093 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
14094 case EM_NIOS32:
14095 return reloc_type == 1; /* R_NIOS_32. */
14096 case EM_OR1K:
14097 return reloc_type == 1; /* R_OR1K_32. */
14098 case EM_PARISC:
14099 return (reloc_type == 1 /* R_PARISC_DIR32. */
14100 || reloc_type == 2 /* R_PARISC_DIR21L. */
14101 || reloc_type == 41); /* R_PARISC_SECREL32. */
14102 case EM_PJ:
14103 case EM_PJ_OLD:
14104 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14105 case EM_PPC64:
14106 return reloc_type == 1; /* R_PPC64_ADDR32. */
14107 case EM_PPC:
14108 return reloc_type == 1; /* R_PPC_ADDR32. */
14109 case EM_TI_PRU:
14110 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
14111 case EM_RISCV:
14112 return reloc_type == 1; /* R_RISCV_32. */
14113 case EM_RL78:
14114 return reloc_type == 1; /* R_RL78_DIR32. */
14115 case EM_RX:
14116 return reloc_type == 1; /* R_RX_DIR32. */
14117 case EM_S370:
14118 return reloc_type == 1; /* R_I370_ADDR31. */
14119 case EM_S390_OLD:
14120 case EM_S390:
14121 return reloc_type == 4; /* R_S390_32. */
14122 case EM_SCORE:
14123 return reloc_type == 8; /* R_SCORE_ABS32. */
14124 case EM_SH:
14125 return reloc_type == 1; /* R_SH_DIR32. */
14126 case EM_SPARC32PLUS:
14127 case EM_SPARCV9:
14128 case EM_SPARC:
14129 return reloc_type == 3 /* R_SPARC_32. */
14130 || reloc_type == 23; /* R_SPARC_UA32. */
14131 case EM_SPU:
14132 return reloc_type == 6; /* R_SPU_ADDR32 */
14133 case EM_TI_C6000:
14134 return reloc_type == 1; /* R_C6000_ABS32. */
14135 case EM_TILEGX:
14136 return reloc_type == 2; /* R_TILEGX_32. */
14137 case EM_TILEPRO:
14138 return reloc_type == 1; /* R_TILEPRO_32. */
14139 case EM_CYGNUS_V850:
14140 case EM_V850:
14141 return reloc_type == 6; /* R_V850_ABS32. */
14142 case EM_V800:
14143 return reloc_type == 0x33; /* R_V810_WORD. */
14144 case EM_VAX:
14145 return reloc_type == 1; /* R_VAX_32. */
14146 case EM_VISIUM:
14147 return reloc_type == 3; /* R_VISIUM_32. */
14148 case EM_WEBASSEMBLY:
14149 return reloc_type == 1; /* R_WASM32_32. */
14150 case EM_X86_64:
14151 case EM_L1OM:
14152 case EM_K1OM:
14153 return reloc_type == 10; /* R_X86_64_32. */
14154 case EM_XC16X:
14155 case EM_C166:
14156 return reloc_type == 3; /* R_XC16C_ABS_32. */
14157 case EM_XGATE:
14158 return reloc_type == 4; /* R_XGATE_32. */
14159 case EM_XSTORMY16:
14160 return reloc_type == 1; /* R_XSTROMY16_32. */
14161 case EM_XTENSA_OLD:
14162 case EM_XTENSA:
14163 return reloc_type == 1; /* R_XTENSA_32. */
14164 case EM_Z80:
14165 return reloc_type == 6; /* R_Z80_32. */
14166 default:
14168 static unsigned int prev_warn = 0;
14170 /* Avoid repeating the same warning multiple times. */
14171 if (prev_warn != filedata->file_header.e_machine)
14172 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
14173 filedata->file_header.e_machine);
14174 prev_warn = filedata->file_header.e_machine;
14175 return false;
14180 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14181 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14183 static bool
14184 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14186 switch (filedata->file_header.e_machine)
14187 /* Please keep this table alpha-sorted for ease of visual lookup. */
14189 case EM_386:
14190 case EM_IAMCU:
14191 return reloc_type == 2; /* R_386_PC32. */
14192 case EM_68K:
14193 return reloc_type == 4; /* R_68K_PC32. */
14194 case EM_AARCH64:
14195 return reloc_type == 261; /* R_AARCH64_PREL32 */
14196 case EM_ADAPTEVA_EPIPHANY:
14197 return reloc_type == 6;
14198 case EM_ALPHA:
14199 return reloc_type == 10; /* R_ALPHA_SREL32. */
14200 case EM_ARC_COMPACT:
14201 case EM_ARC_COMPACT2:
14202 return reloc_type == 49; /* R_ARC_32_PCREL. */
14203 case EM_ARM:
14204 return reloc_type == 3; /* R_ARM_REL32 */
14205 case EM_AVR_OLD:
14206 case EM_AVR:
14207 return reloc_type == 36; /* R_AVR_32_PCREL. */
14208 case EM_MICROBLAZE:
14209 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
14210 case EM_OR1K:
14211 return reloc_type == 9; /* R_OR1K_32_PCREL. */
14212 case EM_PARISC:
14213 return reloc_type == 9; /* R_PARISC_PCREL32. */
14214 case EM_PPC:
14215 return reloc_type == 26; /* R_PPC_REL32. */
14216 case EM_PPC64:
14217 return reloc_type == 26; /* R_PPC64_REL32. */
14218 case EM_RISCV:
14219 return reloc_type == 57; /* R_RISCV_32_PCREL. */
14220 case EM_S390_OLD:
14221 case EM_S390:
14222 return reloc_type == 5; /* R_390_PC32. */
14223 case EM_SH:
14224 return reloc_type == 2; /* R_SH_REL32. */
14225 case EM_SPARC32PLUS:
14226 case EM_SPARCV9:
14227 case EM_SPARC:
14228 return reloc_type == 6; /* R_SPARC_DISP32. */
14229 case EM_SPU:
14230 return reloc_type == 13; /* R_SPU_REL32. */
14231 case EM_TILEGX:
14232 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
14233 case EM_TILEPRO:
14234 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
14235 case EM_VISIUM:
14236 return reloc_type == 6; /* R_VISIUM_32_PCREL */
14237 case EM_X86_64:
14238 case EM_L1OM:
14239 case EM_K1OM:
14240 return reloc_type == 2; /* R_X86_64_PC32. */
14241 case EM_VAX:
14242 return reloc_type == 4; /* R_VAX_PCREL32. */
14243 case EM_XTENSA_OLD:
14244 case EM_XTENSA:
14245 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
14246 default:
14247 /* Do not abort or issue an error message here. Not all targets use
14248 pc-relative 32-bit relocs in their DWARF debug information and we
14249 have already tested for target coverage in is_32bit_abs_reloc. A
14250 more helpful warning message will be generated by apply_relocations
14251 anyway, so just return. */
14252 return false;
14256 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14257 a 64-bit absolute RELA relocation used in DWARF debug sections. */
14259 static bool
14260 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14262 switch (filedata->file_header.e_machine)
14264 case EM_AARCH64:
14265 return reloc_type == 257; /* R_AARCH64_ABS64. */
14266 case EM_ALPHA:
14267 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
14268 case EM_IA_64:
14269 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
14270 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
14271 case EM_LOONGARCH:
14272 return reloc_type == 2; /* R_LARCH_64 */
14273 case EM_PARISC:
14274 return reloc_type == 80; /* R_PARISC_DIR64. */
14275 case EM_PPC64:
14276 return reloc_type == 38; /* R_PPC64_ADDR64. */
14277 case EM_RISCV:
14278 return reloc_type == 2; /* R_RISCV_64. */
14279 case EM_SPARC32PLUS:
14280 case EM_SPARCV9:
14281 case EM_SPARC:
14282 return reloc_type == 32 /* R_SPARC_64. */
14283 || reloc_type == 54; /* R_SPARC_UA64. */
14284 case EM_X86_64:
14285 case EM_L1OM:
14286 case EM_K1OM:
14287 return reloc_type == 1; /* R_X86_64_64. */
14288 case EM_S390_OLD:
14289 case EM_S390:
14290 return reloc_type == 22; /* R_S390_64. */
14291 case EM_TILEGX:
14292 return reloc_type == 1; /* R_TILEGX_64. */
14293 case EM_MIPS:
14294 return reloc_type == 18; /* R_MIPS_64. */
14295 default:
14296 return false;
14300 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
14301 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
14303 static bool
14304 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14306 switch (filedata->file_header.e_machine)
14308 case EM_AARCH64:
14309 return reloc_type == 260; /* R_AARCH64_PREL64. */
14310 case EM_ALPHA:
14311 return reloc_type == 11; /* R_ALPHA_SREL64. */
14312 case EM_IA_64:
14313 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
14314 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
14315 case EM_PARISC:
14316 return reloc_type == 72; /* R_PARISC_PCREL64. */
14317 case EM_PPC64:
14318 return reloc_type == 44; /* R_PPC64_REL64. */
14319 case EM_SPARC32PLUS:
14320 case EM_SPARCV9:
14321 case EM_SPARC:
14322 return reloc_type == 46; /* R_SPARC_DISP64. */
14323 case EM_X86_64:
14324 case EM_L1OM:
14325 case EM_K1OM:
14326 return reloc_type == 24; /* R_X86_64_PC64. */
14327 case EM_S390_OLD:
14328 case EM_S390:
14329 return reloc_type == 23; /* R_S390_PC64. */
14330 case EM_TILEGX:
14331 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
14332 default:
14333 return false;
14337 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14338 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14340 static bool
14341 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14343 switch (filedata->file_header.e_machine)
14345 case EM_CYGNUS_MN10200:
14346 case EM_MN10200:
14347 return reloc_type == 4; /* R_MN10200_24. */
14348 case EM_FT32:
14349 return reloc_type == 5; /* R_FT32_20. */
14350 case EM_Z80:
14351 return reloc_type == 5; /* R_Z80_24. */
14352 default:
14353 return false;
14357 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14358 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14360 static bool
14361 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14363 /* Please keep this table alpha-sorted for ease of visual lookup. */
14364 switch (filedata->file_header.e_machine)
14366 case EM_ARC:
14367 case EM_ARC_COMPACT:
14368 case EM_ARC_COMPACT2:
14369 return reloc_type == 2; /* R_ARC_16. */
14370 case EM_ADAPTEVA_EPIPHANY:
14371 return reloc_type == 5;
14372 case EM_AVR_OLD:
14373 case EM_AVR:
14374 return reloc_type == 4; /* R_AVR_16. */
14375 case EM_CYGNUS_D10V:
14376 case EM_D10V:
14377 return reloc_type == 3; /* R_D10V_16. */
14378 case EM_FT32:
14379 return reloc_type == 2; /* R_FT32_16. */
14380 case EM_H8S:
14381 case EM_H8_300:
14382 case EM_H8_300H:
14383 return reloc_type == R_H8_DIR16;
14384 case EM_IP2K_OLD:
14385 case EM_IP2K:
14386 return reloc_type == 1; /* R_IP2K_16. */
14387 case EM_M32C_OLD:
14388 case EM_M32C:
14389 return reloc_type == 1; /* R_M32C_16 */
14390 case EM_CYGNUS_MN10200:
14391 case EM_MN10200:
14392 return reloc_type == 2; /* R_MN10200_16. */
14393 case EM_CYGNUS_MN10300:
14394 case EM_MN10300:
14395 return reloc_type == 2; /* R_MN10300_16. */
14396 case EM_MSP430:
14397 if (uses_msp430x_relocs (filedata))
14398 return reloc_type == 2; /* R_MSP430_ABS16. */
14399 /* Fall through. */
14400 case EM_MSP430_OLD:
14401 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14402 case EM_NDS32:
14403 return reloc_type == 19; /* R_NDS32_RELA. */
14404 case EM_ALTERA_NIOS2:
14405 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14406 case EM_NIOS32:
14407 return reloc_type == 9; /* R_NIOS_16. */
14408 case EM_OR1K:
14409 return reloc_type == 2; /* R_OR1K_16. */
14410 case EM_RISCV:
14411 return reloc_type == 55; /* R_RISCV_SET16. */
14412 case EM_TI_PRU:
14413 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14414 case EM_TI_C6000:
14415 return reloc_type == 2; /* R_C6000_ABS16. */
14416 case EM_VISIUM:
14417 return reloc_type == 2; /* R_VISIUM_16. */
14418 case EM_XC16X:
14419 case EM_C166:
14420 return reloc_type == 2; /* R_XC16C_ABS_16. */
14421 case EM_XGATE:
14422 return reloc_type == 3; /* R_XGATE_16. */
14423 case EM_Z80:
14424 return reloc_type == 4; /* R_Z80_16. */
14425 default:
14426 return false;
14430 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14431 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14433 static bool
14434 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14436 switch (filedata->file_header.e_machine)
14438 case EM_RISCV:
14439 return reloc_type == 54; /* R_RISCV_SET8. */
14440 case EM_Z80:
14441 return reloc_type == 1; /* R_Z80_8. */
14442 default:
14443 return false;
14447 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14448 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14450 static bool
14451 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14453 switch (filedata->file_header.e_machine)
14455 case EM_RISCV:
14456 return reloc_type == 53; /* R_RISCV_SET6. */
14457 default:
14458 return false;
14462 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14463 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14465 static bool
14466 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14468 /* Please keep this table alpha-sorted for ease of visual lookup. */
14469 switch (filedata->file_header.e_machine)
14471 case EM_RISCV:
14472 return reloc_type == 35; /* R_RISCV_ADD32. */
14473 default:
14474 return false;
14478 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14479 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14481 static bool
14482 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14484 /* Please keep this table alpha-sorted for ease of visual lookup. */
14485 switch (filedata->file_header.e_machine)
14487 case EM_RISCV:
14488 return reloc_type == 39; /* R_RISCV_SUB32. */
14489 default:
14490 return false;
14494 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14495 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14497 static bool
14498 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14500 /* Please keep this table alpha-sorted for ease of visual lookup. */
14501 switch (filedata->file_header.e_machine)
14503 case EM_RISCV:
14504 return reloc_type == 36; /* R_RISCV_ADD64. */
14505 default:
14506 return false;
14510 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14511 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14513 static bool
14514 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14516 /* Please keep this table alpha-sorted for ease of visual lookup. */
14517 switch (filedata->file_header.e_machine)
14519 case EM_RISCV:
14520 return reloc_type == 40; /* R_RISCV_SUB64. */
14521 default:
14522 return false;
14526 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14527 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14529 static bool
14530 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14532 /* Please keep this table alpha-sorted for ease of visual lookup. */
14533 switch (filedata->file_header.e_machine)
14535 case EM_RISCV:
14536 return reloc_type == 34; /* R_RISCV_ADD16. */
14537 default:
14538 return false;
14542 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14543 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14545 static bool
14546 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14548 /* Please keep this table alpha-sorted for ease of visual lookup. */
14549 switch (filedata->file_header.e_machine)
14551 case EM_RISCV:
14552 return reloc_type == 38; /* R_RISCV_SUB16. */
14553 default:
14554 return false;
14558 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14559 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14561 static bool
14562 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14564 /* Please keep this table alpha-sorted for ease of visual lookup. */
14565 switch (filedata->file_header.e_machine)
14567 case EM_RISCV:
14568 return reloc_type == 33; /* R_RISCV_ADD8. */
14569 default:
14570 return false;
14574 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14575 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14577 static bool
14578 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14580 /* Please keep this table alpha-sorted for ease of visual lookup. */
14581 switch (filedata->file_header.e_machine)
14583 case EM_RISCV:
14584 return reloc_type == 37; /* R_RISCV_SUB8. */
14585 default:
14586 return false;
14590 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14591 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14593 static bool
14594 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14596 switch (filedata->file_header.e_machine)
14598 case EM_RISCV:
14599 return reloc_type == 52; /* R_RISCV_SUB6. */
14600 default:
14601 return false;
14605 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14606 relocation entries (possibly formerly used for SHT_GROUP sections). */
14608 static bool
14609 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14611 switch (filedata->file_header.e_machine)
14613 case EM_386: /* R_386_NONE. */
14614 case EM_68K: /* R_68K_NONE. */
14615 case EM_ADAPTEVA_EPIPHANY:
14616 case EM_ALPHA: /* R_ALPHA_NONE. */
14617 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14618 case EM_ARC: /* R_ARC_NONE. */
14619 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14620 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14621 case EM_ARM: /* R_ARM_NONE. */
14622 case EM_C166: /* R_XC16X_NONE. */
14623 case EM_CRIS: /* R_CRIS_NONE. */
14624 case EM_FT32: /* R_FT32_NONE. */
14625 case EM_IA_64: /* R_IA64_NONE. */
14626 case EM_K1OM: /* R_X86_64_NONE. */
14627 case EM_L1OM: /* R_X86_64_NONE. */
14628 case EM_M32R: /* R_M32R_NONE. */
14629 case EM_MIPS: /* R_MIPS_NONE. */
14630 case EM_MN10300: /* R_MN10300_NONE. */
14631 case EM_MOXIE: /* R_MOXIE_NONE. */
14632 case EM_NIOS32: /* R_NIOS_NONE. */
14633 case EM_OR1K: /* R_OR1K_NONE. */
14634 case EM_PARISC: /* R_PARISC_NONE. */
14635 case EM_PPC64: /* R_PPC64_NONE. */
14636 case EM_PPC: /* R_PPC_NONE. */
14637 case EM_RISCV: /* R_RISCV_NONE. */
14638 case EM_S390: /* R_390_NONE. */
14639 case EM_S390_OLD:
14640 case EM_SH: /* R_SH_NONE. */
14641 case EM_SPARC32PLUS:
14642 case EM_SPARC: /* R_SPARC_NONE. */
14643 case EM_SPARCV9:
14644 case EM_TILEGX: /* R_TILEGX_NONE. */
14645 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14646 case EM_TI_C6000:/* R_C6000_NONE. */
14647 case EM_X86_64: /* R_X86_64_NONE. */
14648 case EM_XC16X:
14649 case EM_Z80: /* R_Z80_NONE. */
14650 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14651 return reloc_type == 0;
14653 case EM_AARCH64:
14654 return reloc_type == 0 || reloc_type == 256;
14655 case EM_AVR_OLD:
14656 case EM_AVR:
14657 return (reloc_type == 0 /* R_AVR_NONE. */
14658 || reloc_type == 30 /* R_AVR_DIFF8. */
14659 || reloc_type == 31 /* R_AVR_DIFF16. */
14660 || reloc_type == 32 /* R_AVR_DIFF32. */);
14661 case EM_METAG:
14662 return reloc_type == 3; /* R_METAG_NONE. */
14663 case EM_NDS32:
14664 return (reloc_type == 0 /* R_XTENSA_NONE. */
14665 || reloc_type == 204 /* R_NDS32_DIFF8. */
14666 || reloc_type == 205 /* R_NDS32_DIFF16. */
14667 || reloc_type == 206 /* R_NDS32_DIFF32. */
14668 || reloc_type == 207 /* R_NDS32_ULEB128. */);
14669 case EM_TI_PRU:
14670 return (reloc_type == 0 /* R_PRU_NONE. */
14671 || reloc_type == 65 /* R_PRU_DIFF8. */
14672 || reloc_type == 66 /* R_PRU_DIFF16. */
14673 || reloc_type == 67 /* R_PRU_DIFF32. */);
14674 case EM_XTENSA_OLD:
14675 case EM_XTENSA:
14676 return (reloc_type == 0 /* R_XTENSA_NONE. */
14677 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14678 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14679 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14680 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14681 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14682 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14683 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14684 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14685 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14687 return false;
14690 /* Returns TRUE if there is a relocation against
14691 section NAME at OFFSET bytes. */
14693 bool
14694 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14696 Elf_Internal_Rela * relocs;
14697 Elf_Internal_Rela * rp;
14699 if (dsec == NULL || dsec->reloc_info == NULL)
14700 return false;
14702 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14704 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14705 if (rp->r_offset == offset)
14706 return true;
14708 return false;
14711 /* Apply relocations to a section.
14712 Returns TRUE upon success, FALSE otherwise.
14713 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14714 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14715 will be set to the number of relocs loaded.
14717 Note: So far support has been added only for those relocations
14718 which can be found in debug sections. FIXME: Add support for
14719 more relocations ? */
14721 static bool
14722 apply_relocations (Filedata * filedata,
14723 const Elf_Internal_Shdr * section,
14724 unsigned char * start,
14725 bfd_size_type size,
14726 void ** relocs_return,
14727 unsigned long * num_relocs_return)
14729 Elf_Internal_Shdr * relsec;
14730 unsigned char * end = start + size;
14732 if (relocs_return != NULL)
14734 * (Elf_Internal_Rela **) relocs_return = NULL;
14735 * num_relocs_return = 0;
14738 if (filedata->file_header.e_type != ET_REL)
14739 /* No relocs to apply. */
14740 return true;
14742 /* Find the reloc section associated with the section. */
14743 for (relsec = filedata->section_headers;
14744 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14745 ++relsec)
14747 bool is_rela;
14748 unsigned long num_relocs;
14749 Elf_Internal_Rela * relocs;
14750 Elf_Internal_Rela * rp;
14751 Elf_Internal_Shdr * symsec;
14752 Elf_Internal_Sym * symtab;
14753 unsigned long num_syms;
14754 Elf_Internal_Sym * sym;
14756 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14757 || relsec->sh_info >= filedata->file_header.e_shnum
14758 || filedata->section_headers + relsec->sh_info != section
14759 || relsec->sh_size == 0
14760 || relsec->sh_link >= filedata->file_header.e_shnum)
14761 continue;
14763 symsec = filedata->section_headers + relsec->sh_link;
14764 if (symsec->sh_type != SHT_SYMTAB
14765 && symsec->sh_type != SHT_DYNSYM)
14766 return false;
14768 is_rela = relsec->sh_type == SHT_RELA;
14770 if (is_rela)
14772 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14773 relsec->sh_size, & relocs, & num_relocs))
14774 return false;
14776 else
14778 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14779 relsec->sh_size, & relocs, & num_relocs))
14780 return false;
14783 /* SH uses RELA but uses in place value instead of the addend field. */
14784 if (filedata->file_header.e_machine == EM_SH)
14785 is_rela = false;
14787 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14789 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14791 bfd_vma addend;
14792 unsigned int reloc_type;
14793 unsigned int reloc_size;
14794 bool reloc_inplace = false;
14795 bool reloc_subtract = false;
14796 unsigned char *rloc;
14797 unsigned long sym_index;
14799 reloc_type = get_reloc_type (filedata, rp->r_info);
14801 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14802 continue;
14803 else if (is_none_reloc (filedata, reloc_type))
14804 continue;
14805 else if (is_32bit_abs_reloc (filedata, reloc_type)
14806 || is_32bit_pcrel_reloc (filedata, reloc_type))
14807 reloc_size = 4;
14808 else if (is_64bit_abs_reloc (filedata, reloc_type)
14809 || is_64bit_pcrel_reloc (filedata, reloc_type))
14810 reloc_size = 8;
14811 else if (is_24bit_abs_reloc (filedata, reloc_type))
14812 reloc_size = 3;
14813 else if (is_16bit_abs_reloc (filedata, reloc_type))
14814 reloc_size = 2;
14815 else if (is_8bit_abs_reloc (filedata, reloc_type)
14816 || is_6bit_abs_reloc (filedata, reloc_type))
14817 reloc_size = 1;
14818 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14819 reloc_type))
14820 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14822 reloc_size = 4;
14823 reloc_inplace = true;
14825 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14826 reloc_type))
14827 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14829 reloc_size = 8;
14830 reloc_inplace = true;
14832 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
14833 reloc_type))
14834 || is_16bit_inplace_add_reloc (filedata, reloc_type))
14836 reloc_size = 2;
14837 reloc_inplace = true;
14839 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
14840 reloc_type))
14841 || is_8bit_inplace_add_reloc (filedata, reloc_type))
14843 reloc_size = 1;
14844 reloc_inplace = true;
14846 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
14847 reloc_type)))
14849 reloc_size = 1;
14850 reloc_inplace = true;
14852 else
14854 static unsigned int prev_reloc = 0;
14856 if (reloc_type != prev_reloc)
14857 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
14858 reloc_type, printable_section_name (filedata, section));
14859 prev_reloc = reloc_type;
14860 continue;
14863 rloc = start + rp->r_offset;
14864 if (!IN_RANGE (start, end, rloc, reloc_size))
14866 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
14867 (unsigned long) rp->r_offset,
14868 printable_section_name (filedata, section));
14869 continue;
14872 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
14873 if (sym_index >= num_syms)
14875 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
14876 sym_index, printable_section_name (filedata, section));
14877 continue;
14879 sym = symtab + sym_index;
14881 /* If the reloc has a symbol associated with it,
14882 make sure that it is of an appropriate type.
14884 Relocations against symbols without type can happen.
14885 Gcc -feliminate-dwarf2-dups may generate symbols
14886 without type for debug info.
14888 Icc generates relocations against function symbols
14889 instead of local labels.
14891 Relocations against object symbols can happen, eg when
14892 referencing a global array. For an example of this see
14893 the _clz.o binary in libgcc.a. */
14894 if (sym != symtab
14895 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
14896 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
14898 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
14899 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
14900 printable_section_name (filedata, relsec),
14901 (long int)(rp - relocs));
14902 continue;
14905 addend = 0;
14906 if (is_rela)
14907 addend += rp->r_addend;
14908 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
14909 partial_inplace. */
14910 if (!is_rela
14911 || (filedata->file_header.e_machine == EM_XTENSA
14912 && reloc_type == 1)
14913 || ((filedata->file_header.e_machine == EM_PJ
14914 || filedata->file_header.e_machine == EM_PJ_OLD)
14915 && reloc_type == 1)
14916 || ((filedata->file_header.e_machine == EM_D30V
14917 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
14918 && reloc_type == 12)
14919 || reloc_inplace)
14921 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
14922 addend += byte_get (rloc, reloc_size) & 0x3f;
14923 else
14924 addend += byte_get (rloc, reloc_size);
14927 if (is_32bit_pcrel_reloc (filedata, reloc_type)
14928 || is_64bit_pcrel_reloc (filedata, reloc_type))
14930 /* On HPPA, all pc-relative relocations are biased by 8. */
14931 if (filedata->file_header.e_machine == EM_PARISC)
14932 addend -= 8;
14933 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
14934 reloc_size);
14936 else if (is_6bit_abs_reloc (filedata, reloc_type)
14937 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
14939 if (reloc_subtract)
14940 addend -= sym->st_value;
14941 else
14942 addend += sym->st_value;
14943 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
14944 byte_put (rloc, addend, reloc_size);
14946 else if (reloc_subtract)
14947 byte_put (rloc, addend - sym->st_value, reloc_size);
14948 else
14949 byte_put (rloc, addend + sym->st_value, reloc_size);
14952 free (symtab);
14953 /* Let the target specific reloc processing code know that
14954 we have finished with these relocs. */
14955 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
14957 if (relocs_return)
14959 * (Elf_Internal_Rela **) relocs_return = relocs;
14960 * num_relocs_return = num_relocs;
14962 else
14963 free (relocs);
14965 break;
14968 return true;
14971 #ifdef SUPPORT_DISASSEMBLY
14972 static bool
14973 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
14975 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
14977 /* FIXME: XXX -- to be done --- XXX */
14979 return true;
14981 #endif
14983 /* Reads in the contents of SECTION from FILE, returning a pointer
14984 to a malloc'ed buffer or NULL if something went wrong. */
14986 static char *
14987 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
14989 bfd_size_type num_bytes = section->sh_size;
14991 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
14993 printf (_("Section '%s' has no data to dump.\n"),
14994 printable_section_name (filedata, section));
14995 return NULL;
14998 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
14999 _("section contents"));
15002 /* Uncompresses a section that was compressed using zlib, in place. */
15004 static bool
15005 uncompress_section_contents (unsigned char ** buffer,
15006 dwarf_size_type uncompressed_size,
15007 dwarf_size_type * size)
15009 dwarf_size_type compressed_size = *size;
15010 unsigned char * compressed_buffer = *buffer;
15011 unsigned char * uncompressed_buffer;
15012 z_stream strm;
15013 int rc;
15015 /* It is possible the section consists of several compressed
15016 buffers concatenated together, so we uncompress in a loop. */
15017 /* PR 18313: The state field in the z_stream structure is supposed
15018 to be invisible to the user (ie us), but some compilers will
15019 still complain about it being used without initialisation. So
15020 we first zero the entire z_stream structure and then set the fields
15021 that we need. */
15022 memset (& strm, 0, sizeof strm);
15023 strm.avail_in = compressed_size;
15024 strm.next_in = (Bytef *) compressed_buffer;
15025 strm.avail_out = uncompressed_size;
15026 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
15028 rc = inflateInit (& strm);
15029 while (strm.avail_in > 0)
15031 if (rc != Z_OK)
15032 break;
15033 strm.next_out = ((Bytef *) uncompressed_buffer
15034 + (uncompressed_size - strm.avail_out));
15035 rc = inflate (&strm, Z_FINISH);
15036 if (rc != Z_STREAM_END)
15037 break;
15038 rc = inflateReset (& strm);
15040 if (inflateEnd (& strm) != Z_OK
15041 || rc != Z_OK
15042 || strm.avail_out != 0)
15043 goto fail;
15045 *buffer = uncompressed_buffer;
15046 *size = uncompressed_size;
15047 return true;
15049 fail:
15050 free (uncompressed_buffer);
15051 /* Indicate decompression failure. */
15052 *buffer = NULL;
15053 return false;
15056 static bool
15057 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15059 Elf_Internal_Shdr *relsec;
15060 bfd_size_type num_bytes;
15061 unsigned char *data;
15062 unsigned char *end;
15063 unsigned char *real_start;
15064 unsigned char *start;
15065 bool some_strings_shown;
15067 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15068 if (start == NULL)
15069 /* PR 21820: Do not fail if the section was empty. */
15070 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15072 num_bytes = section->sh_size;
15074 if (filedata->is_separate)
15075 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15076 printable_section_name (filedata, section),
15077 filedata->file_name);
15078 else
15079 printf (_("\nString dump of section '%s':\n"),
15080 printable_section_name (filedata, section));
15082 if (decompress_dumps)
15084 dwarf_size_type new_size = num_bytes;
15085 dwarf_size_type uncompressed_size = 0;
15087 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15089 Elf_Internal_Chdr chdr;
15090 unsigned int compression_header_size
15091 = get_compression_header (& chdr, (unsigned char *) start,
15092 num_bytes);
15093 if (compression_header_size == 0)
15094 /* An error message will have already been generated
15095 by get_compression_header. */
15096 goto error_out;
15098 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15100 warn (_("section '%s' has unsupported compress type: %d\n"),
15101 printable_section_name (filedata, section), chdr.ch_type);
15102 goto error_out;
15104 uncompressed_size = chdr.ch_size;
15105 start += compression_header_size;
15106 new_size -= compression_header_size;
15108 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15110 /* Read the zlib header. In this case, it should be "ZLIB"
15111 followed by the uncompressed section size, 8 bytes in
15112 big-endian order. */
15113 uncompressed_size = start[4]; uncompressed_size <<= 8;
15114 uncompressed_size += start[5]; uncompressed_size <<= 8;
15115 uncompressed_size += start[6]; uncompressed_size <<= 8;
15116 uncompressed_size += start[7]; uncompressed_size <<= 8;
15117 uncompressed_size += start[8]; uncompressed_size <<= 8;
15118 uncompressed_size += start[9]; uncompressed_size <<= 8;
15119 uncompressed_size += start[10]; uncompressed_size <<= 8;
15120 uncompressed_size += start[11];
15121 start += 12;
15122 new_size -= 12;
15125 if (uncompressed_size)
15127 if (uncompress_section_contents (& start,
15128 uncompressed_size, & new_size))
15129 num_bytes = new_size;
15130 else
15132 error (_("Unable to decompress section %s\n"),
15133 printable_section_name (filedata, section));
15134 goto error_out;
15137 else
15138 start = real_start;
15141 /* If the section being dumped has relocations against it the user might
15142 be expecting these relocations to have been applied. Check for this
15143 case and issue a warning message in order to avoid confusion.
15144 FIXME: Maybe we ought to have an option that dumps a section with
15145 relocs applied ? */
15146 for (relsec = filedata->section_headers;
15147 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15148 ++relsec)
15150 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15151 || relsec->sh_info >= filedata->file_header.e_shnum
15152 || filedata->section_headers + relsec->sh_info != section
15153 || relsec->sh_size == 0
15154 || relsec->sh_link >= filedata->file_header.e_shnum)
15155 continue;
15157 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15158 break;
15161 data = start;
15162 end = start + num_bytes;
15163 some_strings_shown = false;
15165 #ifdef HAVE_MBSTATE_T
15166 mbstate_t state;
15167 /* Initialise the multibyte conversion state. */
15168 memset (& state, 0, sizeof (state));
15169 #endif
15171 bool continuing = false;
15173 while (data < end)
15175 while (!ISPRINT (* data))
15176 if (++ data >= end)
15177 break;
15179 if (data < end)
15181 size_t maxlen = end - data;
15183 if (continuing)
15185 printf (" ");
15186 continuing = false;
15188 else
15190 printf (" [%6lx] ", (unsigned long) (data - start));
15193 if (maxlen > 0)
15195 char c = 0;
15197 while (maxlen)
15199 c = *data++;
15201 if (c == 0)
15202 break;
15204 /* PR 25543: Treat new-lines as string-ending characters. */
15205 if (c == '\n')
15207 printf ("\\n\n");
15208 if (*data != 0)
15209 continuing = true;
15210 break;
15213 /* Do not print control characters directly as they can affect terminal
15214 settings. Such characters usually appear in the names generated
15215 by the assembler for local labels. */
15216 if (ISCNTRL (c))
15218 printf ("^%c", c + 0x40);
15220 else if (ISPRINT (c))
15222 putchar (c);
15224 else
15226 size_t n;
15227 #ifdef HAVE_MBSTATE_T
15228 wchar_t w;
15229 #endif
15230 /* Let printf do the hard work of displaying multibyte characters. */
15231 printf ("%.1s", data - 1);
15232 #ifdef HAVE_MBSTATE_T
15233 /* Try to find out how many bytes made up the character that was
15234 just printed. Advance the symbol pointer past the bytes that
15235 were displayed. */
15236 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
15237 #else
15238 n = 1;
15239 #endif
15240 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
15241 data += (n - 1);
15245 if (c != '\n')
15246 putchar ('\n');
15248 else
15250 printf (_("<corrupt>\n"));
15251 data = end;
15253 some_strings_shown = true;
15257 if (! some_strings_shown)
15258 printf (_(" No strings found in this section."));
15260 free (real_start);
15262 putchar ('\n');
15263 return true;
15265 error_out:
15266 free (real_start);
15267 return false;
15270 static bool
15271 dump_section_as_bytes (Elf_Internal_Shdr *section,
15272 Filedata *filedata,
15273 bool relocate)
15275 Elf_Internal_Shdr * relsec;
15276 bfd_size_type bytes;
15277 bfd_size_type section_size;
15278 bfd_vma addr;
15279 unsigned char * data;
15280 unsigned char * real_start;
15281 unsigned char * start;
15283 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15284 if (start == NULL)
15285 /* PR 21820: Do not fail if the section was empty. */
15286 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15288 section_size = section->sh_size;
15290 if (filedata->is_separate)
15291 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
15292 printable_section_name (filedata, section),
15293 filedata->file_name);
15294 else
15295 printf (_("\nHex dump of section '%s':\n"),
15296 printable_section_name (filedata, section));
15298 if (decompress_dumps)
15300 dwarf_size_type new_size = section_size;
15301 dwarf_size_type uncompressed_size = 0;
15303 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15305 Elf_Internal_Chdr chdr;
15306 unsigned int compression_header_size
15307 = get_compression_header (& chdr, start, section_size);
15309 if (compression_header_size == 0)
15310 /* An error message will have already been generated
15311 by get_compression_header. */
15312 goto error_out;
15314 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15316 warn (_("section '%s' has unsupported compress type: %d\n"),
15317 printable_section_name (filedata, section), chdr.ch_type);
15318 goto error_out;
15320 uncompressed_size = chdr.ch_size;
15321 start += compression_header_size;
15322 new_size -= compression_header_size;
15324 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15326 /* Read the zlib header. In this case, it should be "ZLIB"
15327 followed by the uncompressed section size, 8 bytes in
15328 big-endian order. */
15329 uncompressed_size = start[4]; uncompressed_size <<= 8;
15330 uncompressed_size += start[5]; uncompressed_size <<= 8;
15331 uncompressed_size += start[6]; uncompressed_size <<= 8;
15332 uncompressed_size += start[7]; uncompressed_size <<= 8;
15333 uncompressed_size += start[8]; uncompressed_size <<= 8;
15334 uncompressed_size += start[9]; uncompressed_size <<= 8;
15335 uncompressed_size += start[10]; uncompressed_size <<= 8;
15336 uncompressed_size += start[11];
15337 start += 12;
15338 new_size -= 12;
15341 if (uncompressed_size)
15343 if (uncompress_section_contents (& start, uncompressed_size,
15344 & new_size))
15346 section_size = new_size;
15348 else
15350 error (_("Unable to decompress section %s\n"),
15351 printable_section_name (filedata, section));
15352 /* FIXME: Print the section anyway ? */
15353 goto error_out;
15356 else
15357 start = real_start;
15360 if (relocate)
15362 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15363 goto error_out;
15365 else
15367 /* If the section being dumped has relocations against it the user might
15368 be expecting these relocations to have been applied. Check for this
15369 case and issue a warning message in order to avoid confusion.
15370 FIXME: Maybe we ought to have an option that dumps a section with
15371 relocs applied ? */
15372 for (relsec = filedata->section_headers;
15373 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15374 ++relsec)
15376 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15377 || relsec->sh_info >= filedata->file_header.e_shnum
15378 || filedata->section_headers + relsec->sh_info != section
15379 || relsec->sh_size == 0
15380 || relsec->sh_link >= filedata->file_header.e_shnum)
15381 continue;
15383 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15384 break;
15388 addr = section->sh_addr;
15389 bytes = section_size;
15390 data = start;
15392 while (bytes)
15394 int j;
15395 int k;
15396 int lbytes;
15398 lbytes = (bytes > 16 ? 16 : bytes);
15400 printf (" 0x%8.8lx ", (unsigned long) addr);
15402 for (j = 0; j < 16; j++)
15404 if (j < lbytes)
15405 printf ("%2.2x", data[j]);
15406 else
15407 printf (" ");
15409 if ((j & 3) == 3)
15410 printf (" ");
15413 for (j = 0; j < lbytes; j++)
15415 k = data[j];
15416 if (k >= ' ' && k < 0x7f)
15417 printf ("%c", k);
15418 else
15419 printf (".");
15422 putchar ('\n');
15424 data += lbytes;
15425 addr += lbytes;
15426 bytes -= lbytes;
15429 free (real_start);
15431 putchar ('\n');
15432 return true;
15434 error_out:
15435 free (real_start);
15436 return false;
15439 #ifdef ENABLE_LIBCTF
15440 static ctf_sect_t *
15441 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15443 buf->cts_name = section_name_print (filedata, shdr);
15444 buf->cts_size = shdr->sh_size;
15445 buf->cts_entsize = shdr->sh_entsize;
15447 return buf;
15450 /* Formatting callback function passed to ctf_dump. Returns either the pointer
15451 it is passed, or a pointer to newly-allocated storage, in which case
15452 dump_ctf() will free it when it no longer needs it. */
15454 static char *
15455 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15456 char *s, void *arg)
15458 const char *blanks = arg;
15459 char *new_s;
15461 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15462 return s;
15463 return new_s;
15466 /* Dump CTF errors/warnings. */
15467 static void
15468 dump_ctf_errs (ctf_dict_t *fp)
15470 ctf_next_t *it = NULL;
15471 char *errtext;
15472 int is_warning;
15473 int err;
15475 /* Dump accumulated errors and warnings. */
15476 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15478 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15479 errtext);
15480 free (errtext);
15482 if (err != ECTF_NEXT_END)
15483 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15486 /* Dump one CTF archive member. */
15488 static void
15489 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15490 size_t member)
15492 const char *things[] = {"Header", "Labels", "Data objects",
15493 "Function objects", "Variables", "Types", "Strings",
15494 ""};
15495 const char **thing;
15496 size_t i;
15498 /* Don't print out the name of the default-named archive member if it appears
15499 first in the list. The name .ctf appears everywhere, even for things that
15500 aren't really archives, so printing it out is liable to be confusing; also,
15501 the common case by far is for only one archive member to exist, and hiding
15502 it in that case seems worthwhile. */
15504 if (strcmp (name, ".ctf") != 0 || member != 0)
15505 printf (_("\nCTF archive member: %s:\n"), name);
15507 if (ctf_parent_name (ctf) != NULL)
15508 ctf_import (ctf, parent);
15510 for (i = 0, thing = things; *thing[0]; thing++, i++)
15512 ctf_dump_state_t *s = NULL;
15513 char *item;
15515 printf ("\n %s:\n", *thing);
15516 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15517 (void *) " ")) != NULL)
15519 printf ("%s\n", item);
15520 free (item);
15523 if (ctf_errno (ctf))
15525 error (_("Iteration failed: %s, %s\n"), *thing,
15526 ctf_errmsg (ctf_errno (ctf)));
15527 break;
15531 dump_ctf_errs (ctf);
15534 static bool
15535 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15537 Elf_Internal_Shdr * symtab_sec = NULL;
15538 Elf_Internal_Shdr * strtab_sec = NULL;
15539 void * data = NULL;
15540 void * symdata = NULL;
15541 void * strdata = NULL;
15542 ctf_sect_t ctfsect, symsect, strsect;
15543 ctf_sect_t * symsectp = NULL;
15544 ctf_sect_t * strsectp = NULL;
15545 ctf_archive_t * ctfa = NULL;
15546 ctf_dict_t * parent = NULL;
15547 ctf_dict_t * fp;
15549 ctf_next_t *i = NULL;
15550 const char *name;
15551 size_t member = 0;
15552 int err;
15553 bool ret = false;
15555 shdr_to_ctf_sect (&ctfsect, section, filedata);
15556 data = get_section_contents (section, filedata);
15557 ctfsect.cts_data = data;
15559 if (!dump_ctf_symtab_name)
15560 dump_ctf_symtab_name = strdup (".dynsym");
15562 if (!dump_ctf_strtab_name)
15563 dump_ctf_strtab_name = strdup (".dynstr");
15565 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15567 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15569 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15570 goto fail;
15572 if ((symdata = (void *) get_data (NULL, filedata,
15573 symtab_sec->sh_offset, 1,
15574 symtab_sec->sh_size,
15575 _("symbols"))) == NULL)
15576 goto fail;
15577 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15578 symsect.cts_data = symdata;
15581 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15583 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15585 error (_("No string table section named %s\n"),
15586 dump_ctf_strtab_name);
15587 goto fail;
15589 if ((strdata = (void *) get_data (NULL, filedata,
15590 strtab_sec->sh_offset, 1,
15591 strtab_sec->sh_size,
15592 _("strings"))) == NULL)
15593 goto fail;
15594 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15595 strsect.cts_data = strdata;
15598 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15599 libctf papers over the difference, so we can pretend it is always an
15600 archive. */
15602 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15604 dump_ctf_errs (NULL);
15605 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15606 goto fail;
15609 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15610 != ELFDATA2MSB);
15612 /* Preload the parent dict, since it will need to be imported into every
15613 child in turn. */
15614 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15616 dump_ctf_errs (NULL);
15617 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15618 goto fail;
15621 ret = true;
15623 if (filedata->is_separate)
15624 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15625 printable_section_name (filedata, section),
15626 filedata->file_name);
15627 else
15628 printf (_("\nDump of CTF section '%s':\n"),
15629 printable_section_name (filedata, section));
15631 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15632 dump_ctf_archive_member (fp, name, parent, member++);
15633 if (err != ECTF_NEXT_END)
15635 dump_ctf_errs (NULL);
15636 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15637 ret = false;
15640 fail:
15641 ctf_dict_close (parent);
15642 ctf_close (ctfa);
15643 free (data);
15644 free (symdata);
15645 free (strdata);
15646 return ret;
15648 #endif
15650 static bool
15651 load_specific_debug_section (enum dwarf_section_display_enum debug,
15652 const Elf_Internal_Shdr * sec,
15653 void * data)
15655 struct dwarf_section * section = &debug_displays [debug].section;
15656 char buf [64];
15657 Filedata * filedata = (Filedata *) data;
15659 if (section->start != NULL)
15661 /* If it is already loaded, do nothing. */
15662 if (streq (section->filename, filedata->file_name))
15663 return true;
15664 free (section->start);
15667 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15668 section->address = sec->sh_addr;
15669 section->filename = filedata->file_name;
15670 section->start = (unsigned char *) get_data (NULL, filedata,
15671 sec->sh_offset, 1,
15672 sec->sh_size, buf);
15673 if (section->start == NULL)
15674 section->size = 0;
15675 else
15677 unsigned char *start = section->start;
15678 dwarf_size_type size = sec->sh_size;
15679 dwarf_size_type uncompressed_size = 0;
15681 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15683 Elf_Internal_Chdr chdr;
15684 unsigned int compression_header_size;
15686 if (size < (is_32bit_elf
15687 ? sizeof (Elf32_External_Chdr)
15688 : sizeof (Elf64_External_Chdr)))
15690 warn (_("compressed section %s is too small to contain a compression header\n"),
15691 section->name);
15692 return false;
15695 compression_header_size = get_compression_header (&chdr, start, size);
15696 if (compression_header_size == 0)
15697 /* An error message will have already been generated
15698 by get_compression_header. */
15699 return false;
15701 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15703 warn (_("section '%s' has unsupported compress type: %d\n"),
15704 section->name, chdr.ch_type);
15705 return false;
15707 uncompressed_size = chdr.ch_size;
15708 start += compression_header_size;
15709 size -= compression_header_size;
15711 else if (size > 12 && streq ((char *) start, "ZLIB"))
15713 /* Read the zlib header. In this case, it should be "ZLIB"
15714 followed by the uncompressed section size, 8 bytes in
15715 big-endian order. */
15716 uncompressed_size = start[4]; uncompressed_size <<= 8;
15717 uncompressed_size += start[5]; uncompressed_size <<= 8;
15718 uncompressed_size += start[6]; uncompressed_size <<= 8;
15719 uncompressed_size += start[7]; uncompressed_size <<= 8;
15720 uncompressed_size += start[8]; uncompressed_size <<= 8;
15721 uncompressed_size += start[9]; uncompressed_size <<= 8;
15722 uncompressed_size += start[10]; uncompressed_size <<= 8;
15723 uncompressed_size += start[11];
15724 start += 12;
15725 size -= 12;
15728 if (uncompressed_size)
15730 if (uncompress_section_contents (&start, uncompressed_size,
15731 &size))
15733 /* Free the compressed buffer, update the section buffer
15734 and the section size if uncompress is successful. */
15735 free (section->start);
15736 section->start = start;
15738 else
15740 error (_("Unable to decompress section %s\n"),
15741 printable_section_name (filedata, sec));
15742 return false;
15746 section->size = size;
15749 if (section->start == NULL)
15750 return false;
15752 if (debug_displays [debug].relocate)
15754 if (! apply_relocations (filedata, sec, section->start, section->size,
15755 & section->reloc_info, & section->num_relocs))
15756 return false;
15758 else
15760 section->reloc_info = NULL;
15761 section->num_relocs = 0;
15764 return true;
15767 #if HAVE_LIBDEBUGINFOD
15768 /* Return a hex string representation of the build-id. */
15769 unsigned char *
15770 get_build_id (void * data)
15772 Filedata * filedata = (Filedata *) data;
15773 Elf_Internal_Shdr * shdr;
15774 unsigned long i;
15776 /* Iterate through notes to find note.gnu.build-id.
15777 FIXME: Only the first note in any note section is examined. */
15778 for (i = 0, shdr = filedata->section_headers;
15779 i < filedata->file_header.e_shnum && shdr != NULL;
15780 i++, shdr++)
15782 if (shdr->sh_type != SHT_NOTE)
15783 continue;
15785 char * next;
15786 char * end;
15787 size_t data_remaining;
15788 size_t min_notesz;
15789 Elf_External_Note * enote;
15790 Elf_Internal_Note inote;
15792 bfd_vma offset = shdr->sh_offset;
15793 bfd_vma align = shdr->sh_addralign;
15794 bfd_vma length = shdr->sh_size;
15796 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15797 if (enote == NULL)
15798 continue;
15800 if (align < 4)
15801 align = 4;
15802 else if (align != 4 && align != 8)
15804 free (enote);
15805 continue;
15808 end = (char *) enote + length;
15809 data_remaining = end - (char *) enote;
15811 if (!is_ia64_vms (filedata))
15813 min_notesz = offsetof (Elf_External_Note, name);
15814 if (data_remaining < min_notesz)
15816 warn (_("\
15817 malformed note encountered in section %s whilst scanning for build-id note\n"),
15818 printable_section_name (filedata, shdr));
15819 free (enote);
15820 continue;
15822 data_remaining -= min_notesz;
15824 inote.type = BYTE_GET (enote->type);
15825 inote.namesz = BYTE_GET (enote->namesz);
15826 inote.namedata = enote->name;
15827 inote.descsz = BYTE_GET (enote->descsz);
15828 inote.descdata = ((char *) enote
15829 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15830 inote.descpos = offset + (inote.descdata - (char *) enote);
15831 next = ((char *) enote
15832 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15834 else
15836 Elf64_External_VMS_Note *vms_enote;
15838 /* PR binutils/15191
15839 Make sure that there is enough data to read. */
15840 min_notesz = offsetof (Elf64_External_VMS_Note, name);
15841 if (data_remaining < min_notesz)
15843 warn (_("\
15844 malformed note encountered in section %s whilst scanning for build-id note\n"),
15845 printable_section_name (filedata, shdr));
15846 free (enote);
15847 continue;
15849 data_remaining -= min_notesz;
15851 vms_enote = (Elf64_External_VMS_Note *) enote;
15852 inote.type = BYTE_GET (vms_enote->type);
15853 inote.namesz = BYTE_GET (vms_enote->namesz);
15854 inote.namedata = vms_enote->name;
15855 inote.descsz = BYTE_GET (vms_enote->descsz);
15856 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
15857 inote.descpos = offset + (inote.descdata - (char *) enote);
15858 next = inote.descdata + align_power (inote.descsz, 3);
15861 /* Skip malformed notes. */
15862 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
15863 || (size_t) (inote.descdata - inote.namedata) > data_remaining
15864 || (size_t) (next - inote.descdata) < inote.descsz
15865 || ((size_t) (next - inote.descdata)
15866 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
15868 warn (_("\
15869 malformed note encountered in section %s whilst scanning for build-id note\n"),
15870 printable_section_name (filedata, shdr));
15871 free (enote);
15872 continue;
15875 /* Check if this is the build-id note. If so then convert the build-id
15876 bytes to a hex string. */
15877 if (inote.namesz > 0
15878 && startswith (inote.namedata, "GNU")
15879 && inote.type == NT_GNU_BUILD_ID)
15881 unsigned long j;
15882 char * build_id;
15884 build_id = malloc (inote.descsz * 2 + 1);
15885 if (build_id == NULL)
15887 free (enote);
15888 return NULL;
15891 for (j = 0; j < inote.descsz; ++j)
15892 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
15893 build_id[inote.descsz * 2] = '\0';
15894 free (enote);
15896 return (unsigned char *) build_id;
15898 free (enote);
15901 return NULL;
15903 #endif /* HAVE_LIBDEBUGINFOD */
15905 /* If this is not NULL, load_debug_section will only look for sections
15906 within the list of sections given here. */
15907 static unsigned int * section_subset = NULL;
15909 bool
15910 load_debug_section (enum dwarf_section_display_enum debug, void * data)
15912 struct dwarf_section * section = &debug_displays [debug].section;
15913 Elf_Internal_Shdr * sec;
15914 Filedata * filedata = (Filedata *) data;
15916 /* Without section headers we cannot find any sections. */
15917 if (filedata->section_headers == NULL)
15918 return false;
15920 if (filedata->string_table == NULL
15921 && filedata->file_header.e_shstrndx != SHN_UNDEF
15922 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
15924 Elf_Internal_Shdr * strs;
15926 /* Read in the string table, so that we have section names to scan. */
15927 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
15929 if (strs != NULL && strs->sh_size != 0)
15931 filedata->string_table
15932 = (char *) get_data (NULL, filedata, strs->sh_offset,
15933 1, strs->sh_size, _("string table"));
15935 filedata->string_table_length
15936 = filedata->string_table != NULL ? strs->sh_size : 0;
15940 /* Locate the debug section. */
15941 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
15942 if (sec != NULL)
15943 section->name = section->uncompressed_name;
15944 else
15946 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
15947 if (sec != NULL)
15948 section->name = section->compressed_name;
15950 if (sec == NULL)
15951 return false;
15953 /* If we're loading from a subset of sections, and we've loaded
15954 a section matching this name before, it's likely that it's a
15955 different one. */
15956 if (section_subset != NULL)
15957 free_debug_section (debug);
15959 return load_specific_debug_section (debug, sec, data);
15962 void
15963 free_debug_section (enum dwarf_section_display_enum debug)
15965 struct dwarf_section * section = &debug_displays [debug].section;
15967 if (section->start == NULL)
15968 return;
15970 free ((char *) section->start);
15971 section->start = NULL;
15972 section->address = 0;
15973 section->size = 0;
15975 free (section->reloc_info);
15976 section->reloc_info = NULL;
15977 section->num_relocs = 0;
15980 static bool
15981 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
15983 const char *name = (section_name_valid (filedata, section)
15984 ? section_name (filedata, section) : "");
15985 const char *print_name = printable_section_name (filedata, section);
15986 bfd_size_type length;
15987 bool result = true;
15988 int i;
15990 length = section->sh_size;
15991 if (length == 0)
15993 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
15994 return true;
15996 if (section->sh_type == SHT_NOBITS)
15998 /* There is no point in dumping the contents of a debugging section
15999 which has the NOBITS type - the bits in the file will be random.
16000 This can happen when a file containing a .eh_frame section is
16001 stripped with the --only-keep-debug command line option. */
16002 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16003 print_name);
16004 return false;
16007 if (startswith (name, ".gnu.linkonce.wi."))
16008 name = ".debug_info";
16010 /* See if we know how to display the contents of this section. */
16011 for (i = 0; i < max; i++)
16013 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16014 struct dwarf_section_display * display = debug_displays + i;
16015 struct dwarf_section * sec = & display->section;
16017 if (streq (sec->uncompressed_name, name)
16018 || (id == line && startswith (name, ".debug_line."))
16019 || streq (sec->compressed_name, name))
16021 bool secondary = (section != find_section (filedata, name));
16023 if (secondary)
16024 free_debug_section (id);
16026 if (i == line && startswith (name, ".debug_line."))
16027 sec->name = name;
16028 else if (streq (sec->uncompressed_name, name))
16029 sec->name = sec->uncompressed_name;
16030 else
16031 sec->name = sec->compressed_name;
16033 if (load_specific_debug_section (id, section, filedata))
16035 /* If this debug section is part of a CU/TU set in a .dwp file,
16036 restrict load_debug_section to the sections in that set. */
16037 section_subset = find_cu_tu_set (filedata, shndx);
16039 result &= display->display (sec, filedata);
16041 section_subset = NULL;
16043 if (secondary || (id != info && id != abbrev && id != debug_addr))
16044 free_debug_section (id);
16046 break;
16050 if (i == max)
16052 printf (_("Unrecognized debug section: %s\n"), print_name);
16053 result = false;
16056 return result;
16059 /* Set DUMP_SECTS for all sections where dumps were requested
16060 based on section name. */
16062 static void
16063 initialise_dumps_byname (Filedata * filedata)
16065 struct dump_list_entry * cur;
16067 for (cur = dump_sects_byname; cur; cur = cur->next)
16069 unsigned int i;
16070 bool any = false;
16072 for (i = 0; i < filedata->file_header.e_shnum; i++)
16073 if (section_name_valid (filedata, filedata->section_headers + i)
16074 && streq (section_name (filedata, filedata->section_headers + i),
16075 cur->name))
16077 request_dump_bynumber (&filedata->dump, i, cur->type);
16078 any = true;
16081 if (!any && !filedata->is_separate)
16082 warn (_("Section '%s' was not dumped because it does not exist\n"),
16083 cur->name);
16087 static bool
16088 process_section_contents (Filedata * filedata)
16090 Elf_Internal_Shdr * section;
16091 unsigned int i;
16092 bool res = true;
16094 if (! do_dump)
16095 return true;
16097 initialise_dumps_byname (filedata);
16099 for (i = 0, section = filedata->section_headers;
16100 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
16101 i++, section++)
16103 dump_type dump = filedata->dump.dump_sects[i];
16105 if (filedata->is_separate && ! process_links)
16106 dump &= DEBUG_DUMP;
16108 #ifdef SUPPORT_DISASSEMBLY
16109 if (dump & DISASS_DUMP)
16111 if (! disassemble_section (section, filedata))
16112 res = false;
16114 #endif
16115 if (dump & HEX_DUMP)
16117 if (! dump_section_as_bytes (section, filedata, false))
16118 res = false;
16121 if (dump & RELOC_DUMP)
16123 if (! dump_section_as_bytes (section, filedata, true))
16124 res = false;
16127 if (dump & STRING_DUMP)
16129 if (! dump_section_as_strings (section, filedata))
16130 res = false;
16133 if (dump & DEBUG_DUMP)
16135 if (! display_debug_section (i, section, filedata))
16136 res = false;
16139 #ifdef ENABLE_LIBCTF
16140 if (dump & CTF_DUMP)
16142 if (! dump_section_as_ctf (section, filedata))
16143 res = false;
16145 #endif
16148 if (! filedata->is_separate)
16150 /* Check to see if the user requested a
16151 dump of a section that does not exist. */
16152 for (; i < filedata->dump.num_dump_sects; i++)
16153 if (filedata->dump.dump_sects[i])
16155 warn (_("Section %d was not dumped because it does not exist!\n"), i);
16156 res = false;
16160 return res;
16163 static void
16164 process_mips_fpe_exception (int mask)
16166 if (mask)
16168 bool first = true;
16170 if (mask & OEX_FPU_INEX)
16171 fputs ("INEX", stdout), first = false;
16172 if (mask & OEX_FPU_UFLO)
16173 printf ("%sUFLO", first ? "" : "|"), first = false;
16174 if (mask & OEX_FPU_OFLO)
16175 printf ("%sOFLO", first ? "" : "|"), first = false;
16176 if (mask & OEX_FPU_DIV0)
16177 printf ("%sDIV0", first ? "" : "|"), first = false;
16178 if (mask & OEX_FPU_INVAL)
16179 printf ("%sINVAL", first ? "" : "|");
16181 else
16182 fputs ("0", stdout);
16185 /* Display's the value of TAG at location P. If TAG is
16186 greater than 0 it is assumed to be an unknown tag, and
16187 a message is printed to this effect. Otherwise it is
16188 assumed that a message has already been printed.
16190 If the bottom bit of TAG is set it assumed to have a
16191 string value, otherwise it is assumed to have an integer
16192 value.
16194 Returns an updated P pointing to the first unread byte
16195 beyond the end of TAG's value.
16197 Reads at or beyond END will not be made. */
16199 static unsigned char *
16200 display_tag_value (signed int tag,
16201 unsigned char * p,
16202 const unsigned char * const end)
16204 unsigned long val;
16206 if (tag > 0)
16207 printf (" Tag_unknown_%d: ", tag);
16209 if (p >= end)
16211 warn (_("<corrupt tag>\n"));
16213 else if (tag & 1)
16215 /* PR 17531 file: 027-19978-0.004. */
16216 size_t maxlen = (end - p) - 1;
16218 putchar ('"');
16219 if (maxlen > 0)
16221 print_symbol ((int) maxlen, (const char *) p);
16222 p += strnlen ((char *) p, maxlen) + 1;
16224 else
16226 printf (_("<corrupt string tag>"));
16227 p = (unsigned char *) end;
16229 printf ("\"\n");
16231 else
16233 READ_ULEB (val, p, end);
16234 printf ("%ld (0x%lx)\n", val, val);
16237 assert (p <= end);
16238 return p;
16241 /* ARC ABI attributes section. */
16243 static unsigned char *
16244 display_arc_attribute (unsigned char * p,
16245 const unsigned char * const end)
16247 unsigned int tag;
16248 unsigned int val;
16250 READ_ULEB (tag, p, end);
16252 switch (tag)
16254 case Tag_ARC_PCS_config:
16255 READ_ULEB (val, p, end);
16256 printf (" Tag_ARC_PCS_config: ");
16257 switch (val)
16259 case 0:
16260 printf (_("Absent/Non standard\n"));
16261 break;
16262 case 1:
16263 printf (_("Bare metal/mwdt\n"));
16264 break;
16265 case 2:
16266 printf (_("Bare metal/newlib\n"));
16267 break;
16268 case 3:
16269 printf (_("Linux/uclibc\n"));
16270 break;
16271 case 4:
16272 printf (_("Linux/glibc\n"));
16273 break;
16274 default:
16275 printf (_("Unknown\n"));
16276 break;
16278 break;
16280 case Tag_ARC_CPU_base:
16281 READ_ULEB (val, p, end);
16282 printf (" Tag_ARC_CPU_base: ");
16283 switch (val)
16285 default:
16286 case TAG_CPU_NONE:
16287 printf (_("Absent\n"));
16288 break;
16289 case TAG_CPU_ARC6xx:
16290 printf ("ARC6xx\n");
16291 break;
16292 case TAG_CPU_ARC7xx:
16293 printf ("ARC7xx\n");
16294 break;
16295 case TAG_CPU_ARCEM:
16296 printf ("ARCEM\n");
16297 break;
16298 case TAG_CPU_ARCHS:
16299 printf ("ARCHS\n");
16300 break;
16302 break;
16304 case Tag_ARC_CPU_variation:
16305 READ_ULEB (val, p, end);
16306 printf (" Tag_ARC_CPU_variation: ");
16307 switch (val)
16309 default:
16310 if (val > 0 && val < 16)
16311 printf ("Core%d\n", val);
16312 else
16313 printf ("Unknown\n");
16314 break;
16316 case 0:
16317 printf (_("Absent\n"));
16318 break;
16320 break;
16322 case Tag_ARC_CPU_name:
16323 printf (" Tag_ARC_CPU_name: ");
16324 p = display_tag_value (-1, p, end);
16325 break;
16327 case Tag_ARC_ABI_rf16:
16328 READ_ULEB (val, p, end);
16329 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16330 break;
16332 case Tag_ARC_ABI_osver:
16333 READ_ULEB (val, p, end);
16334 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16335 break;
16337 case Tag_ARC_ABI_pic:
16338 case Tag_ARC_ABI_sda:
16339 READ_ULEB (val, p, end);
16340 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16341 : " Tag_ARC_ABI_pic: ");
16342 switch (val)
16344 case 0:
16345 printf (_("Absent\n"));
16346 break;
16347 case 1:
16348 printf ("MWDT\n");
16349 break;
16350 case 2:
16351 printf ("GNU\n");
16352 break;
16353 default:
16354 printf (_("Unknown\n"));
16355 break;
16357 break;
16359 case Tag_ARC_ABI_tls:
16360 READ_ULEB (val, p, end);
16361 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16362 break;
16364 case Tag_ARC_ABI_enumsize:
16365 READ_ULEB (val, p, end);
16366 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16367 _("smallest"));
16368 break;
16370 case Tag_ARC_ABI_exceptions:
16371 READ_ULEB (val, p, end);
16372 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16373 : _("default"));
16374 break;
16376 case Tag_ARC_ABI_double_size:
16377 READ_ULEB (val, p, end);
16378 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16379 break;
16381 case Tag_ARC_ISA_config:
16382 printf (" Tag_ARC_ISA_config: ");
16383 p = display_tag_value (-1, p, end);
16384 break;
16386 case Tag_ARC_ISA_apex:
16387 printf (" Tag_ARC_ISA_apex: ");
16388 p = display_tag_value (-1, p, end);
16389 break;
16391 case Tag_ARC_ISA_mpy_option:
16392 READ_ULEB (val, p, end);
16393 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16394 break;
16396 case Tag_ARC_ATR_version:
16397 READ_ULEB (val, p, end);
16398 printf (" Tag_ARC_ATR_version: %d\n", val);
16399 break;
16401 default:
16402 return display_tag_value (tag & 1, p, end);
16405 return p;
16408 /* ARM EABI attributes section. */
16409 typedef struct
16411 unsigned int tag;
16412 const char * name;
16413 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16414 unsigned int type;
16415 const char *const *table;
16416 } arm_attr_public_tag;
16418 static const char *const arm_attr_tag_CPU_arch[] =
16419 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16420 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16421 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16422 "v8.1-M.mainline", "v9"};
16423 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16424 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16425 {"No", "Thumb-1", "Thumb-2", "Yes"};
16426 static const char *const arm_attr_tag_FP_arch[] =
16427 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16428 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16429 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16430 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16431 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16432 "NEON for ARMv8.1"};
16433 static const char *const arm_attr_tag_PCS_config[] =
16434 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16435 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16436 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16437 {"V6", "SB", "TLS", "Unused"};
16438 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16439 {"Absolute", "PC-relative", "SB-relative", "None"};
16440 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16441 {"Absolute", "PC-relative", "None"};
16442 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16443 {"None", "direct", "GOT-indirect"};
16444 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16445 {"None", "??? 1", "2", "??? 3", "4"};
16446 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16447 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16448 {"Unused", "Needed", "Sign only"};
16449 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16450 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16451 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16452 {"Unused", "Finite", "RTABI", "IEEE 754"};
16453 static const char *const arm_attr_tag_ABI_enum_size[] =
16454 {"Unused", "small", "int", "forced to int"};
16455 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16456 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16457 static const char *const arm_attr_tag_ABI_VFP_args[] =
16458 {"AAPCS", "VFP registers", "custom", "compatible"};
16459 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16460 {"AAPCS", "WMMX registers", "custom"};
16461 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16462 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16463 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16464 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16465 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16466 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16467 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16468 static const char *const arm_attr_tag_FP_HP_extension[] =
16469 {"Not Allowed", "Allowed"};
16470 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16471 {"None", "IEEE 754", "Alternative Format"};
16472 static const char *const arm_attr_tag_DSP_extension[] =
16473 {"Follow architecture", "Allowed"};
16474 static const char *const arm_attr_tag_MPextension_use[] =
16475 {"Not Allowed", "Allowed"};
16476 static const char *const arm_attr_tag_DIV_use[] =
16477 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16478 "Allowed in v7-A with integer division extension"};
16479 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16480 static const char *const arm_attr_tag_Virtualization_use[] =
16481 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16482 "TrustZone and Virtualization Extensions"};
16483 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16484 {"Not Allowed", "Allowed"};
16486 static const char *const arm_attr_tag_MVE_arch[] =
16487 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16489 static const char * arm_attr_tag_PAC_extension[] =
16490 {"No PAC/AUT instructions",
16491 "PAC/AUT instructions permitted in the NOP space",
16492 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16494 static const char * arm_attr_tag_BTI_extension[] =
16495 {"BTI instructions not permitted",
16496 "BTI instructions permitted in the NOP space",
16497 "BTI instructions permitted in the NOP and in the non-NOP space"};
16499 static const char * arm_attr_tag_BTI_use[] =
16500 {"Compiled without branch target enforcement",
16501 "Compiled with branch target enforcement"};
16503 static const char * arm_attr_tag_PACRET_use[] =
16504 {"Compiled without return address signing and authentication",
16505 "Compiled with return address signing and authentication"};
16507 #define LOOKUP(id, name) \
16508 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16509 static arm_attr_public_tag arm_attr_public_tags[] =
16511 {4, "CPU_raw_name", 1, NULL},
16512 {5, "CPU_name", 1, NULL},
16513 LOOKUP(6, CPU_arch),
16514 {7, "CPU_arch_profile", 0, NULL},
16515 LOOKUP(8, ARM_ISA_use),
16516 LOOKUP(9, THUMB_ISA_use),
16517 LOOKUP(10, FP_arch),
16518 LOOKUP(11, WMMX_arch),
16519 LOOKUP(12, Advanced_SIMD_arch),
16520 LOOKUP(13, PCS_config),
16521 LOOKUP(14, ABI_PCS_R9_use),
16522 LOOKUP(15, ABI_PCS_RW_data),
16523 LOOKUP(16, ABI_PCS_RO_data),
16524 LOOKUP(17, ABI_PCS_GOT_use),
16525 LOOKUP(18, ABI_PCS_wchar_t),
16526 LOOKUP(19, ABI_FP_rounding),
16527 LOOKUP(20, ABI_FP_denormal),
16528 LOOKUP(21, ABI_FP_exceptions),
16529 LOOKUP(22, ABI_FP_user_exceptions),
16530 LOOKUP(23, ABI_FP_number_model),
16531 {24, "ABI_align_needed", 0, NULL},
16532 {25, "ABI_align_preserved", 0, NULL},
16533 LOOKUP(26, ABI_enum_size),
16534 LOOKUP(27, ABI_HardFP_use),
16535 LOOKUP(28, ABI_VFP_args),
16536 LOOKUP(29, ABI_WMMX_args),
16537 LOOKUP(30, ABI_optimization_goals),
16538 LOOKUP(31, ABI_FP_optimization_goals),
16539 {32, "compatibility", 0, NULL},
16540 LOOKUP(34, CPU_unaligned_access),
16541 LOOKUP(36, FP_HP_extension),
16542 LOOKUP(38, ABI_FP_16bit_format),
16543 LOOKUP(42, MPextension_use),
16544 LOOKUP(44, DIV_use),
16545 LOOKUP(46, DSP_extension),
16546 LOOKUP(48, MVE_arch),
16547 LOOKUP(50, PAC_extension),
16548 LOOKUP(52, BTI_extension),
16549 LOOKUP(74, BTI_use),
16550 LOOKUP(76, PACRET_use),
16551 {64, "nodefaults", 0, NULL},
16552 {65, "also_compatible_with", 0, NULL},
16553 LOOKUP(66, T2EE_use),
16554 {67, "conformance", 1, NULL},
16555 LOOKUP(68, Virtualization_use),
16556 LOOKUP(70, MPextension_use_legacy)
16558 #undef LOOKUP
16560 static unsigned char *
16561 display_arm_attribute (unsigned char * p,
16562 const unsigned char * const end)
16564 unsigned int tag;
16565 unsigned int val;
16566 arm_attr_public_tag * attr;
16567 unsigned i;
16568 unsigned int type;
16570 READ_ULEB (tag, p, end);
16571 attr = NULL;
16572 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16574 if (arm_attr_public_tags[i].tag == tag)
16576 attr = &arm_attr_public_tags[i];
16577 break;
16581 if (attr)
16583 printf (" Tag_%s: ", attr->name);
16584 switch (attr->type)
16586 case 0:
16587 switch (tag)
16589 case 7: /* Tag_CPU_arch_profile. */
16590 READ_ULEB (val, p, end);
16591 switch (val)
16593 case 0: printf (_("None\n")); break;
16594 case 'A': printf (_("Application\n")); break;
16595 case 'R': printf (_("Realtime\n")); break;
16596 case 'M': printf (_("Microcontroller\n")); break;
16597 case 'S': printf (_("Application or Realtime\n")); break;
16598 default: printf ("??? (%d)\n", val); break;
16600 break;
16602 case 24: /* Tag_align_needed. */
16603 READ_ULEB (val, p, end);
16604 switch (val)
16606 case 0: printf (_("None\n")); break;
16607 case 1: printf (_("8-byte\n")); break;
16608 case 2: printf (_("4-byte\n")); break;
16609 case 3: printf ("??? 3\n"); break;
16610 default:
16611 if (val <= 12)
16612 printf (_("8-byte and up to %d-byte extended\n"),
16613 1 << val);
16614 else
16615 printf ("??? (%d)\n", val);
16616 break;
16618 break;
16620 case 25: /* Tag_align_preserved. */
16621 READ_ULEB (val, p, end);
16622 switch (val)
16624 case 0: printf (_("None\n")); break;
16625 case 1: printf (_("8-byte, except leaf SP\n")); break;
16626 case 2: printf (_("8-byte\n")); break;
16627 case 3: printf ("??? 3\n"); break;
16628 default:
16629 if (val <= 12)
16630 printf (_("8-byte and up to %d-byte extended\n"),
16631 1 << val);
16632 else
16633 printf ("??? (%d)\n", val);
16634 break;
16636 break;
16638 case 32: /* Tag_compatibility. */
16640 READ_ULEB (val, p, end);
16641 printf (_("flag = %d, vendor = "), val);
16642 if (p < end - 1)
16644 size_t maxlen = (end - p) - 1;
16646 print_symbol ((int) maxlen, (const char *) p);
16647 p += strnlen ((char *) p, maxlen) + 1;
16649 else
16651 printf (_("<corrupt>"));
16652 p = (unsigned char *) end;
16654 putchar ('\n');
16656 break;
16658 case 64: /* Tag_nodefaults. */
16659 /* PR 17531: file: 001-505008-0.01. */
16660 if (p < end)
16661 p++;
16662 printf (_("True\n"));
16663 break;
16665 case 65: /* Tag_also_compatible_with. */
16666 READ_ULEB (val, p, end);
16667 if (val == 6 /* Tag_CPU_arch. */)
16669 READ_ULEB (val, p, end);
16670 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16671 printf ("??? (%d)\n", val);
16672 else
16673 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16675 else
16676 printf ("???\n");
16677 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16679 break;
16681 default:
16682 printf (_("<unknown: %d>\n"), tag);
16683 break;
16685 return p;
16687 case 1:
16688 return display_tag_value (-1, p, end);
16689 case 2:
16690 return display_tag_value (0, p, end);
16692 default:
16693 assert (attr->type & 0x80);
16694 READ_ULEB (val, p, end);
16695 type = attr->type & 0x7f;
16696 if (val >= type)
16697 printf ("??? (%d)\n", val);
16698 else
16699 printf ("%s\n", attr->table[val]);
16700 return p;
16704 return display_tag_value (tag, p, end);
16707 static unsigned char *
16708 display_gnu_attribute (unsigned char * p,
16709 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16710 const unsigned char * const end)
16712 unsigned int tag;
16713 unsigned int val;
16715 READ_ULEB (tag, p, end);
16717 /* Tag_compatibility is the only generic GNU attribute defined at
16718 present. */
16719 if (tag == 32)
16721 READ_ULEB (val, p, end);
16723 printf (_("flag = %d, vendor = "), val);
16724 if (p == end)
16726 printf (_("<corrupt>\n"));
16727 warn (_("corrupt vendor attribute\n"));
16729 else
16731 if (p < end - 1)
16733 size_t maxlen = (end - p) - 1;
16735 print_symbol ((int) maxlen, (const char *) p);
16736 p += strnlen ((char *) p, maxlen) + 1;
16738 else
16740 printf (_("<corrupt>"));
16741 p = (unsigned char *) end;
16743 putchar ('\n');
16745 return p;
16748 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16749 return display_proc_gnu_attribute (p, tag, end);
16751 return display_tag_value (tag, p, end);
16754 static unsigned char *
16755 display_m68k_gnu_attribute (unsigned char * p,
16756 unsigned int tag,
16757 const unsigned char * const end)
16759 unsigned int val;
16761 if (tag == Tag_GNU_M68K_ABI_FP)
16763 printf (" Tag_GNU_M68K_ABI_FP: ");
16764 if (p == end)
16766 printf (_("<corrupt>\n"));
16767 return p;
16769 READ_ULEB (val, p, end);
16771 if (val > 3)
16772 printf ("(%#x), ", val);
16774 switch (val & 3)
16776 case 0:
16777 printf (_("unspecified hard/soft float\n"));
16778 break;
16779 case 1:
16780 printf (_("hard float\n"));
16781 break;
16782 case 2:
16783 printf (_("soft float\n"));
16784 break;
16786 return p;
16789 return display_tag_value (tag & 1, p, end);
16792 static unsigned char *
16793 display_power_gnu_attribute (unsigned char * p,
16794 unsigned int tag,
16795 const unsigned char * const end)
16797 unsigned int val;
16799 if (tag == Tag_GNU_Power_ABI_FP)
16801 printf (" Tag_GNU_Power_ABI_FP: ");
16802 if (p == end)
16804 printf (_("<corrupt>\n"));
16805 return p;
16807 READ_ULEB (val, p, end);
16809 if (val > 15)
16810 printf ("(%#x), ", val);
16812 switch (val & 3)
16814 case 0:
16815 printf (_("unspecified hard/soft float, "));
16816 break;
16817 case 1:
16818 printf (_("hard float, "));
16819 break;
16820 case 2:
16821 printf (_("soft float, "));
16822 break;
16823 case 3:
16824 printf (_("single-precision hard float, "));
16825 break;
16828 switch (val & 0xC)
16830 case 0:
16831 printf (_("unspecified long double\n"));
16832 break;
16833 case 4:
16834 printf (_("128-bit IBM long double\n"));
16835 break;
16836 case 8:
16837 printf (_("64-bit long double\n"));
16838 break;
16839 case 12:
16840 printf (_("128-bit IEEE long double\n"));
16841 break;
16843 return p;
16846 if (tag == Tag_GNU_Power_ABI_Vector)
16848 printf (" Tag_GNU_Power_ABI_Vector: ");
16849 if (p == end)
16851 printf (_("<corrupt>\n"));
16852 return p;
16854 READ_ULEB (val, p, end);
16856 if (val > 3)
16857 printf ("(%#x), ", val);
16859 switch (val & 3)
16861 case 0:
16862 printf (_("unspecified\n"));
16863 break;
16864 case 1:
16865 printf (_("generic\n"));
16866 break;
16867 case 2:
16868 printf ("AltiVec\n");
16869 break;
16870 case 3:
16871 printf ("SPE\n");
16872 break;
16874 return p;
16877 if (tag == Tag_GNU_Power_ABI_Struct_Return)
16879 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
16880 if (p == end)
16882 printf (_("<corrupt>\n"));
16883 return p;
16885 READ_ULEB (val, p, end);
16887 if (val > 2)
16888 printf ("(%#x), ", val);
16890 switch (val & 3)
16892 case 0:
16893 printf (_("unspecified\n"));
16894 break;
16895 case 1:
16896 printf ("r3/r4\n");
16897 break;
16898 case 2:
16899 printf (_("memory\n"));
16900 break;
16901 case 3:
16902 printf ("???\n");
16903 break;
16905 return p;
16908 return display_tag_value (tag & 1, p, end);
16911 static unsigned char *
16912 display_s390_gnu_attribute (unsigned char * p,
16913 unsigned int tag,
16914 const unsigned char * const end)
16916 unsigned int val;
16918 if (tag == Tag_GNU_S390_ABI_Vector)
16920 printf (" Tag_GNU_S390_ABI_Vector: ");
16921 READ_ULEB (val, p, end);
16923 switch (val)
16925 case 0:
16926 printf (_("any\n"));
16927 break;
16928 case 1:
16929 printf (_("software\n"));
16930 break;
16931 case 2:
16932 printf (_("hardware\n"));
16933 break;
16934 default:
16935 printf ("??? (%d)\n", val);
16936 break;
16938 return p;
16941 return display_tag_value (tag & 1, p, end);
16944 static void
16945 display_sparc_hwcaps (unsigned int mask)
16947 if (mask)
16949 bool first = true;
16951 if (mask & ELF_SPARC_HWCAP_MUL32)
16952 fputs ("mul32", stdout), first = false;
16953 if (mask & ELF_SPARC_HWCAP_DIV32)
16954 printf ("%sdiv32", first ? "" : "|"), first = false;
16955 if (mask & ELF_SPARC_HWCAP_FSMULD)
16956 printf ("%sfsmuld", first ? "" : "|"), first = false;
16957 if (mask & ELF_SPARC_HWCAP_V8PLUS)
16958 printf ("%sv8plus", first ? "" : "|"), first = false;
16959 if (mask & ELF_SPARC_HWCAP_POPC)
16960 printf ("%spopc", first ? "" : "|"), first = false;
16961 if (mask & ELF_SPARC_HWCAP_VIS)
16962 printf ("%svis", first ? "" : "|"), first = false;
16963 if (mask & ELF_SPARC_HWCAP_VIS2)
16964 printf ("%svis2", first ? "" : "|"), first = false;
16965 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
16966 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
16967 if (mask & ELF_SPARC_HWCAP_FMAF)
16968 printf ("%sfmaf", first ? "" : "|"), first = false;
16969 if (mask & ELF_SPARC_HWCAP_VIS3)
16970 printf ("%svis3", first ? "" : "|"), first = false;
16971 if (mask & ELF_SPARC_HWCAP_HPC)
16972 printf ("%shpc", first ? "" : "|"), first = false;
16973 if (mask & ELF_SPARC_HWCAP_RANDOM)
16974 printf ("%srandom", first ? "" : "|"), first = false;
16975 if (mask & ELF_SPARC_HWCAP_TRANS)
16976 printf ("%strans", first ? "" : "|"), first = false;
16977 if (mask & ELF_SPARC_HWCAP_FJFMAU)
16978 printf ("%sfjfmau", first ? "" : "|"), first = false;
16979 if (mask & ELF_SPARC_HWCAP_IMA)
16980 printf ("%sima", first ? "" : "|"), first = false;
16981 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
16982 printf ("%scspare", first ? "" : "|"), first = false;
16984 else
16985 fputc ('0', stdout);
16986 fputc ('\n', stdout);
16989 static void
16990 display_sparc_hwcaps2 (unsigned int mask)
16992 if (mask)
16994 bool first = true;
16996 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
16997 fputs ("fjathplus", stdout), first = false;
16998 if (mask & ELF_SPARC_HWCAP2_VIS3B)
16999 printf ("%svis3b", first ? "" : "|"), first = false;
17000 if (mask & ELF_SPARC_HWCAP2_ADP)
17001 printf ("%sadp", first ? "" : "|"), first = false;
17002 if (mask & ELF_SPARC_HWCAP2_SPARC5)
17003 printf ("%ssparc5", first ? "" : "|"), first = false;
17004 if (mask & ELF_SPARC_HWCAP2_MWAIT)
17005 printf ("%smwait", first ? "" : "|"), first = false;
17006 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17007 printf ("%sxmpmul", first ? "" : "|"), first = false;
17008 if (mask & ELF_SPARC_HWCAP2_XMONT)
17009 printf ("%sxmont2", first ? "" : "|"), first = false;
17010 if (mask & ELF_SPARC_HWCAP2_NSEC)
17011 printf ("%snsec", first ? "" : "|"), first = false;
17012 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17013 printf ("%sfjathhpc", first ? "" : "|"), first = false;
17014 if (mask & ELF_SPARC_HWCAP2_FJDES)
17015 printf ("%sfjdes", first ? "" : "|"), first = false;
17016 if (mask & ELF_SPARC_HWCAP2_FJAES)
17017 printf ("%sfjaes", first ? "" : "|"), first = false;
17019 else
17020 fputc ('0', stdout);
17021 fputc ('\n', stdout);
17024 static unsigned char *
17025 display_sparc_gnu_attribute (unsigned char * p,
17026 unsigned int tag,
17027 const unsigned char * const end)
17029 unsigned int val;
17031 if (tag == Tag_GNU_Sparc_HWCAPS)
17033 READ_ULEB (val, p, end);
17034 printf (" Tag_GNU_Sparc_HWCAPS: ");
17035 display_sparc_hwcaps (val);
17036 return p;
17038 if (tag == Tag_GNU_Sparc_HWCAPS2)
17040 READ_ULEB (val, p, end);
17041 printf (" Tag_GNU_Sparc_HWCAPS2: ");
17042 display_sparc_hwcaps2 (val);
17043 return p;
17046 return display_tag_value (tag, p, end);
17049 static void
17050 print_mips_fp_abi_value (unsigned int val)
17052 switch (val)
17054 case Val_GNU_MIPS_ABI_FP_ANY:
17055 printf (_("Hard or soft float\n"));
17056 break;
17057 case Val_GNU_MIPS_ABI_FP_DOUBLE:
17058 printf (_("Hard float (double precision)\n"));
17059 break;
17060 case Val_GNU_MIPS_ABI_FP_SINGLE:
17061 printf (_("Hard float (single precision)\n"));
17062 break;
17063 case Val_GNU_MIPS_ABI_FP_SOFT:
17064 printf (_("Soft float\n"));
17065 break;
17066 case Val_GNU_MIPS_ABI_FP_OLD_64:
17067 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
17068 break;
17069 case Val_GNU_MIPS_ABI_FP_XX:
17070 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
17071 break;
17072 case Val_GNU_MIPS_ABI_FP_64:
17073 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
17074 break;
17075 case Val_GNU_MIPS_ABI_FP_64A:
17076 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
17077 break;
17078 case Val_GNU_MIPS_ABI_FP_NAN2008:
17079 printf (_("NaN 2008 compatibility\n"));
17080 break;
17081 default:
17082 printf ("??? (%d)\n", val);
17083 break;
17087 static unsigned char *
17088 display_mips_gnu_attribute (unsigned char * p,
17089 unsigned int tag,
17090 const unsigned char * const end)
17092 if (tag == Tag_GNU_MIPS_ABI_FP)
17094 unsigned int val;
17096 printf (" Tag_GNU_MIPS_ABI_FP: ");
17097 READ_ULEB (val, p, end);
17098 print_mips_fp_abi_value (val);
17099 return p;
17102 if (tag == Tag_GNU_MIPS_ABI_MSA)
17104 unsigned int val;
17106 printf (" Tag_GNU_MIPS_ABI_MSA: ");
17107 READ_ULEB (val, p, end);
17109 switch (val)
17111 case Val_GNU_MIPS_ABI_MSA_ANY:
17112 printf (_("Any MSA or not\n"));
17113 break;
17114 case Val_GNU_MIPS_ABI_MSA_128:
17115 printf (_("128-bit MSA\n"));
17116 break;
17117 default:
17118 printf ("??? (%d)\n", val);
17119 break;
17121 return p;
17124 return display_tag_value (tag & 1, p, end);
17127 static unsigned char *
17128 display_tic6x_attribute (unsigned char * p,
17129 const unsigned char * const end)
17131 unsigned int tag;
17132 unsigned int val;
17134 READ_ULEB (tag, p, end);
17136 switch (tag)
17138 case Tag_ISA:
17139 printf (" Tag_ISA: ");
17140 READ_ULEB (val, p, end);
17142 switch (val)
17144 case C6XABI_Tag_ISA_none:
17145 printf (_("None\n"));
17146 break;
17147 case C6XABI_Tag_ISA_C62X:
17148 printf ("C62x\n");
17149 break;
17150 case C6XABI_Tag_ISA_C67X:
17151 printf ("C67x\n");
17152 break;
17153 case C6XABI_Tag_ISA_C67XP:
17154 printf ("C67x+\n");
17155 break;
17156 case C6XABI_Tag_ISA_C64X:
17157 printf ("C64x\n");
17158 break;
17159 case C6XABI_Tag_ISA_C64XP:
17160 printf ("C64x+\n");
17161 break;
17162 case C6XABI_Tag_ISA_C674X:
17163 printf ("C674x\n");
17164 break;
17165 default:
17166 printf ("??? (%d)\n", val);
17167 break;
17169 return p;
17171 case Tag_ABI_wchar_t:
17172 printf (" Tag_ABI_wchar_t: ");
17173 READ_ULEB (val, p, end);
17174 switch (val)
17176 case 0:
17177 printf (_("Not used\n"));
17178 break;
17179 case 1:
17180 printf (_("2 bytes\n"));
17181 break;
17182 case 2:
17183 printf (_("4 bytes\n"));
17184 break;
17185 default:
17186 printf ("??? (%d)\n", val);
17187 break;
17189 return p;
17191 case Tag_ABI_stack_align_needed:
17192 printf (" Tag_ABI_stack_align_needed: ");
17193 READ_ULEB (val, p, end);
17194 switch (val)
17196 case 0:
17197 printf (_("8-byte\n"));
17198 break;
17199 case 1:
17200 printf (_("16-byte\n"));
17201 break;
17202 default:
17203 printf ("??? (%d)\n", val);
17204 break;
17206 return p;
17208 case Tag_ABI_stack_align_preserved:
17209 READ_ULEB (val, p, end);
17210 printf (" Tag_ABI_stack_align_preserved: ");
17211 switch (val)
17213 case 0:
17214 printf (_("8-byte\n"));
17215 break;
17216 case 1:
17217 printf (_("16-byte\n"));
17218 break;
17219 default:
17220 printf ("??? (%d)\n", val);
17221 break;
17223 return p;
17225 case Tag_ABI_DSBT:
17226 READ_ULEB (val, p, end);
17227 printf (" Tag_ABI_DSBT: ");
17228 switch (val)
17230 case 0:
17231 printf (_("DSBT addressing not used\n"));
17232 break;
17233 case 1:
17234 printf (_("DSBT addressing used\n"));
17235 break;
17236 default:
17237 printf ("??? (%d)\n", val);
17238 break;
17240 return p;
17242 case Tag_ABI_PID:
17243 READ_ULEB (val, p, end);
17244 printf (" Tag_ABI_PID: ");
17245 switch (val)
17247 case 0:
17248 printf (_("Data addressing position-dependent\n"));
17249 break;
17250 case 1:
17251 printf (_("Data addressing position-independent, GOT near DP\n"));
17252 break;
17253 case 2:
17254 printf (_("Data addressing position-independent, GOT far from DP\n"));
17255 break;
17256 default:
17257 printf ("??? (%d)\n", val);
17258 break;
17260 return p;
17262 case Tag_ABI_PIC:
17263 READ_ULEB (val, p, end);
17264 printf (" Tag_ABI_PIC: ");
17265 switch (val)
17267 case 0:
17268 printf (_("Code addressing position-dependent\n"));
17269 break;
17270 case 1:
17271 printf (_("Code addressing position-independent\n"));
17272 break;
17273 default:
17274 printf ("??? (%d)\n", val);
17275 break;
17277 return p;
17279 case Tag_ABI_array_object_alignment:
17280 READ_ULEB (val, p, end);
17281 printf (" Tag_ABI_array_object_alignment: ");
17282 switch (val)
17284 case 0:
17285 printf (_("8-byte\n"));
17286 break;
17287 case 1:
17288 printf (_("4-byte\n"));
17289 break;
17290 case 2:
17291 printf (_("16-byte\n"));
17292 break;
17293 default:
17294 printf ("??? (%d)\n", val);
17295 break;
17297 return p;
17299 case Tag_ABI_array_object_align_expected:
17300 READ_ULEB (val, p, end);
17301 printf (" Tag_ABI_array_object_align_expected: ");
17302 switch (val)
17304 case 0:
17305 printf (_("8-byte\n"));
17306 break;
17307 case 1:
17308 printf (_("4-byte\n"));
17309 break;
17310 case 2:
17311 printf (_("16-byte\n"));
17312 break;
17313 default:
17314 printf ("??? (%d)\n", val);
17315 break;
17317 return p;
17319 case Tag_ABI_compatibility:
17321 READ_ULEB (val, p, end);
17322 printf (" Tag_ABI_compatibility: ");
17323 printf (_("flag = %d, vendor = "), val);
17324 if (p < end - 1)
17326 size_t maxlen = (end - p) - 1;
17328 print_symbol ((int) maxlen, (const char *) p);
17329 p += strnlen ((char *) p, maxlen) + 1;
17331 else
17333 printf (_("<corrupt>"));
17334 p = (unsigned char *) end;
17336 putchar ('\n');
17337 return p;
17340 case Tag_ABI_conformance:
17342 printf (" Tag_ABI_conformance: \"");
17343 if (p < end - 1)
17345 size_t maxlen = (end - p) - 1;
17347 print_symbol ((int) maxlen, (const char *) p);
17348 p += strnlen ((char *) p, maxlen) + 1;
17350 else
17352 printf (_("<corrupt>"));
17353 p = (unsigned char *) end;
17355 printf ("\"\n");
17356 return p;
17360 return display_tag_value (tag, p, end);
17363 static void
17364 display_raw_attribute (unsigned char * p, unsigned char const * const end)
17366 unsigned long addr = 0;
17367 size_t bytes = end - p;
17369 assert (end >= p);
17370 while (bytes)
17372 int j;
17373 int k;
17374 int lbytes = (bytes > 16 ? 16 : bytes);
17376 printf (" 0x%8.8lx ", addr);
17378 for (j = 0; j < 16; j++)
17380 if (j < lbytes)
17381 printf ("%2.2x", p[j]);
17382 else
17383 printf (" ");
17385 if ((j & 3) == 3)
17386 printf (" ");
17389 for (j = 0; j < lbytes; j++)
17391 k = p[j];
17392 if (k >= ' ' && k < 0x7f)
17393 printf ("%c", k);
17394 else
17395 printf (".");
17398 putchar ('\n');
17400 p += lbytes;
17401 bytes -= lbytes;
17402 addr += lbytes;
17405 putchar ('\n');
17408 static unsigned char *
17409 display_msp430_attribute (unsigned char * p,
17410 const unsigned char * const end)
17412 unsigned int val;
17413 unsigned int tag;
17415 READ_ULEB (tag, p, end);
17417 switch (tag)
17419 case OFBA_MSPABI_Tag_ISA:
17420 printf (" Tag_ISA: ");
17421 READ_ULEB (val, p, end);
17422 switch (val)
17424 case 0: printf (_("None\n")); break;
17425 case 1: printf (_("MSP430\n")); break;
17426 case 2: printf (_("MSP430X\n")); break;
17427 default: printf ("??? (%d)\n", val); break;
17429 break;
17431 case OFBA_MSPABI_Tag_Code_Model:
17432 printf (" Tag_Code_Model: ");
17433 READ_ULEB (val, p, end);
17434 switch (val)
17436 case 0: printf (_("None\n")); break;
17437 case 1: printf (_("Small\n")); break;
17438 case 2: printf (_("Large\n")); break;
17439 default: printf ("??? (%d)\n", val); break;
17441 break;
17443 case OFBA_MSPABI_Tag_Data_Model:
17444 printf (" Tag_Data_Model: ");
17445 READ_ULEB (val, p, end);
17446 switch (val)
17448 case 0: printf (_("None\n")); break;
17449 case 1: printf (_("Small\n")); break;
17450 case 2: printf (_("Large\n")); break;
17451 case 3: printf (_("Restricted Large\n")); break;
17452 default: printf ("??? (%d)\n", val); break;
17454 break;
17456 default:
17457 printf (_(" <unknown tag %d>: "), tag);
17459 if (tag & 1)
17461 putchar ('"');
17462 if (p < end - 1)
17464 size_t maxlen = (end - p) - 1;
17466 print_symbol ((int) maxlen, (const char *) p);
17467 p += strnlen ((char *) p, maxlen) + 1;
17469 else
17471 printf (_("<corrupt>"));
17472 p = (unsigned char *) end;
17474 printf ("\"\n");
17476 else
17478 READ_ULEB (val, p, end);
17479 printf ("%d (0x%x)\n", val, val);
17481 break;
17484 assert (p <= end);
17485 return p;
17488 static unsigned char *
17489 display_msp430_gnu_attribute (unsigned char * p,
17490 unsigned int tag,
17491 const unsigned char * const end)
17493 if (tag == Tag_GNU_MSP430_Data_Region)
17495 unsigned int val;
17497 printf (" Tag_GNU_MSP430_Data_Region: ");
17498 READ_ULEB (val, p, end);
17500 switch (val)
17502 case Val_GNU_MSP430_Data_Region_Any:
17503 printf (_("Any Region\n"));
17504 break;
17505 case Val_GNU_MSP430_Data_Region_Lower:
17506 printf (_("Lower Region Only\n"));
17507 break;
17508 default:
17509 printf ("??? (%u)\n", val);
17511 return p;
17513 return display_tag_value (tag & 1, p, end);
17516 struct riscv_attr_tag_t {
17517 const char *name;
17518 unsigned int tag;
17521 static struct riscv_attr_tag_t riscv_attr_tag[] =
17523 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17524 T(arch),
17525 T(priv_spec),
17526 T(priv_spec_minor),
17527 T(priv_spec_revision),
17528 T(unaligned_access),
17529 T(stack_align),
17530 #undef T
17533 static unsigned char *
17534 display_riscv_attribute (unsigned char *p,
17535 const unsigned char * const end)
17537 unsigned int val;
17538 unsigned int tag;
17539 struct riscv_attr_tag_t *attr = NULL;
17540 unsigned i;
17542 READ_ULEB (tag, p, end);
17544 /* Find the name of attribute. */
17545 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17547 if (riscv_attr_tag[i].tag == tag)
17549 attr = &riscv_attr_tag[i];
17550 break;
17554 if (attr)
17555 printf (" %s: ", attr->name);
17556 else
17557 return display_tag_value (tag, p, end);
17559 switch (tag)
17561 case Tag_RISCV_priv_spec:
17562 case Tag_RISCV_priv_spec_minor:
17563 case Tag_RISCV_priv_spec_revision:
17564 READ_ULEB (val, p, end);
17565 printf (_("%u\n"), val);
17566 break;
17567 case Tag_RISCV_unaligned_access:
17568 READ_ULEB (val, p, end);
17569 switch (val)
17571 case 0:
17572 printf (_("No unaligned access\n"));
17573 break;
17574 case 1:
17575 printf (_("Unaligned access\n"));
17576 break;
17578 break;
17579 case Tag_RISCV_stack_align:
17580 READ_ULEB (val, p, end);
17581 printf (_("%u-bytes\n"), val);
17582 break;
17583 case Tag_RISCV_arch:
17584 p = display_tag_value (-1, p, end);
17585 break;
17586 default:
17587 return display_tag_value (tag, p, end);
17590 return p;
17593 static unsigned char *
17594 display_csky_attribute (unsigned char * p,
17595 const unsigned char * const end)
17597 unsigned int tag;
17598 unsigned int val;
17599 READ_ULEB (tag, p, end);
17601 if (tag >= Tag_CSKY_MAX)
17603 return display_tag_value (-1, p, end);
17606 switch (tag)
17608 case Tag_CSKY_ARCH_NAME:
17609 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17610 return display_tag_value (-1, p, end);
17611 case Tag_CSKY_CPU_NAME:
17612 printf (" Tag_CSKY_CPU_NAME:\t\t");
17613 return display_tag_value (-1, p, end);
17615 case Tag_CSKY_ISA_FLAGS:
17616 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17617 return display_tag_value (0, p, end);
17618 case Tag_CSKY_ISA_EXT_FLAGS:
17619 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17620 return display_tag_value (0, p, end);
17622 case Tag_CSKY_DSP_VERSION:
17623 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17624 READ_ULEB (val, p, end);
17625 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17626 printf ("DSP Extension\n");
17627 else if (val == VAL_CSKY_DSP_VERSION_2)
17628 printf ("DSP 2.0\n");
17629 break;
17631 case Tag_CSKY_VDSP_VERSION:
17632 printf (" Tag_CSKY_VDSP_VERSION:\t");
17633 READ_ULEB (val, p, end);
17634 printf ("VDSP Version %d\n", val);
17635 break;
17637 case Tag_CSKY_FPU_VERSION:
17638 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17639 READ_ULEB (val, p, end);
17640 if (val == VAL_CSKY_FPU_VERSION_1)
17641 printf ("ABIV1 FPU Version 1\n");
17642 else if (val == VAL_CSKY_FPU_VERSION_2)
17643 printf ("FPU Version 2\n");
17644 break;
17646 case Tag_CSKY_FPU_ABI:
17647 printf (" Tag_CSKY_FPU_ABI:\t\t");
17648 READ_ULEB (val, p, end);
17649 if (val == VAL_CSKY_FPU_ABI_HARD)
17650 printf ("Hard\n");
17651 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17652 printf ("SoftFP\n");
17653 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17654 printf ("Soft\n");
17655 break;
17656 case Tag_CSKY_FPU_ROUNDING:
17657 READ_ULEB (val, p, end);
17658 if (val == 1)
17660 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17661 printf ("Needed\n");
17663 break;
17664 case Tag_CSKY_FPU_DENORMAL:
17665 READ_ULEB (val, p, end);
17666 if (val == 1)
17668 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17669 printf ("Needed\n");
17671 break;
17672 case Tag_CSKY_FPU_Exception:
17673 READ_ULEB (val, p, end);
17674 if (val == 1)
17676 printf (" Tag_CSKY_FPU_Exception:\t");
17677 printf ("Needed\n");
17679 break;
17680 case Tag_CSKY_FPU_NUMBER_MODULE:
17681 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17682 return display_tag_value (-1, p, end);
17683 case Tag_CSKY_FPU_HARDFP:
17684 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17685 READ_ULEB (val, p, end);
17686 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17687 printf (" Half");
17688 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17689 printf (" Single");
17690 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17691 printf (" Double");
17692 printf ("\n");
17693 break;
17694 default:
17695 return display_tag_value (tag, p, end);
17697 return p;
17700 static bool
17701 process_attributes (Filedata * filedata,
17702 const char * public_name,
17703 unsigned int proc_type,
17704 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17705 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17707 Elf_Internal_Shdr * sect;
17708 unsigned i;
17709 bool res = true;
17711 /* Find the section header so that we get the size. */
17712 for (i = 0, sect = filedata->section_headers;
17713 i < filedata->file_header.e_shnum;
17714 i++, sect++)
17716 unsigned char * contents;
17717 unsigned char * p;
17719 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17720 continue;
17722 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17723 sect->sh_size, _("attributes"));
17724 if (contents == NULL)
17726 res = false;
17727 continue;
17730 p = contents;
17731 /* The first character is the version of the attributes.
17732 Currently only version 1, (aka 'A') is recognised here. */
17733 if (*p != 'A')
17735 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17736 res = false;
17738 else
17740 bfd_vma section_len;
17742 section_len = sect->sh_size - 1;
17743 p++;
17745 while (section_len > 0)
17747 bfd_vma attr_len;
17748 unsigned int namelen;
17749 bool public_section;
17750 bool gnu_section;
17752 if (section_len <= 4)
17754 error (_("Tag section ends prematurely\n"));
17755 res = false;
17756 break;
17758 attr_len = byte_get (p, 4);
17759 p += 4;
17761 if (attr_len > section_len)
17763 error (_("Bad attribute length (%u > %u)\n"),
17764 (unsigned) attr_len, (unsigned) section_len);
17765 attr_len = section_len;
17766 res = false;
17768 /* PR 17531: file: 001-101425-0.004 */
17769 else if (attr_len < 5)
17771 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17772 res = false;
17773 break;
17776 section_len -= attr_len;
17777 attr_len -= 4;
17779 namelen = strnlen ((char *) p, attr_len) + 1;
17780 if (namelen == 0 || namelen >= attr_len)
17782 error (_("Corrupt attribute section name\n"));
17783 res = false;
17784 break;
17787 printf (_("Attribute Section: "));
17788 print_symbol (INT_MAX, (const char *) p);
17789 putchar ('\n');
17791 if (public_name && streq ((char *) p, public_name))
17792 public_section = true;
17793 else
17794 public_section = false;
17796 if (streq ((char *) p, "gnu"))
17797 gnu_section = true;
17798 else
17799 gnu_section = false;
17801 p += namelen;
17802 attr_len -= namelen;
17804 while (attr_len > 0 && p < contents + sect->sh_size)
17806 int tag;
17807 unsigned int val;
17808 bfd_vma size;
17809 unsigned char * end;
17811 /* PR binutils/17531: Safe handling of corrupt files. */
17812 if (attr_len < 6)
17814 error (_("Unused bytes at end of section\n"));
17815 res = false;
17816 section_len = 0;
17817 break;
17820 tag = *(p++);
17821 size = byte_get (p, 4);
17822 if (size > attr_len)
17824 error (_("Bad subsection length (%u > %u)\n"),
17825 (unsigned) size, (unsigned) attr_len);
17826 res = false;
17827 size = attr_len;
17829 /* PR binutils/17531: Safe handling of corrupt files. */
17830 if (size < 6)
17832 error (_("Bad subsection length (%u < 6)\n"),
17833 (unsigned) size);
17834 res = false;
17835 section_len = 0;
17836 break;
17839 attr_len -= size;
17840 end = p + size - 1;
17841 assert (end <= contents + sect->sh_size);
17842 p += 4;
17844 switch (tag)
17846 case 1:
17847 printf (_("File Attributes\n"));
17848 break;
17849 case 2:
17850 printf (_("Section Attributes:"));
17851 goto do_numlist;
17852 case 3:
17853 printf (_("Symbol Attributes:"));
17854 /* Fall through. */
17855 do_numlist:
17856 for (;;)
17858 READ_ULEB (val, p, end);
17859 if (val == 0)
17860 break;
17861 printf (" %d", val);
17863 printf ("\n");
17864 break;
17865 default:
17866 printf (_("Unknown tag: %d\n"), tag);
17867 public_section = false;
17868 break;
17871 if (public_section && display_pub_attribute != NULL)
17873 while (p < end)
17874 p = display_pub_attribute (p, end);
17875 assert (p == end);
17877 else if (gnu_section && display_proc_gnu_attribute != NULL)
17879 while (p < end)
17880 p = display_gnu_attribute (p,
17881 display_proc_gnu_attribute,
17882 end);
17883 assert (p == end);
17885 else if (p < end)
17887 printf (_(" Unknown attribute:\n"));
17888 display_raw_attribute (p, end);
17889 p = end;
17891 else
17892 attr_len = 0;
17897 free (contents);
17900 return res;
17903 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
17904 Print the Address, Access and Initial fields of an entry at VMA ADDR
17905 and return the VMA of the next entry, or -1 if there was a problem.
17906 Does not read from DATA_END or beyond. */
17908 static bfd_vma
17909 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
17910 unsigned char * data_end)
17912 printf (" ");
17913 print_vma (addr, LONG_HEX);
17914 printf (" ");
17915 if (addr < pltgot + 0xfff0)
17916 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
17917 else
17918 printf ("%10s", "");
17919 printf (" ");
17920 if (data == NULL)
17921 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17922 else
17924 bfd_vma entry;
17925 unsigned char * from = data + addr - pltgot;
17927 if (from + (is_32bit_elf ? 4 : 8) > data_end)
17929 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
17930 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
17931 return (bfd_vma) -1;
17933 else
17935 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17936 print_vma (entry, LONG_HEX);
17939 return addr + (is_32bit_elf ? 4 : 8);
17942 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
17943 PLTGOT. Print the Address and Initial fields of an entry at VMA
17944 ADDR and return the VMA of the next entry. */
17946 static bfd_vma
17947 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
17949 printf (" ");
17950 print_vma (addr, LONG_HEX);
17951 printf (" ");
17952 if (data == NULL)
17953 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17954 else
17956 bfd_vma entry;
17958 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17959 print_vma (entry, LONG_HEX);
17961 return addr + (is_32bit_elf ? 4 : 8);
17964 static void
17965 print_mips_ases (unsigned int mask)
17967 if (mask & AFL_ASE_DSP)
17968 fputs ("\n\tDSP ASE", stdout);
17969 if (mask & AFL_ASE_DSPR2)
17970 fputs ("\n\tDSP R2 ASE", stdout);
17971 if (mask & AFL_ASE_DSPR3)
17972 fputs ("\n\tDSP R3 ASE", stdout);
17973 if (mask & AFL_ASE_EVA)
17974 fputs ("\n\tEnhanced VA Scheme", stdout);
17975 if (mask & AFL_ASE_MCU)
17976 fputs ("\n\tMCU (MicroController) ASE", stdout);
17977 if (mask & AFL_ASE_MDMX)
17978 fputs ("\n\tMDMX ASE", stdout);
17979 if (mask & AFL_ASE_MIPS3D)
17980 fputs ("\n\tMIPS-3D ASE", stdout);
17981 if (mask & AFL_ASE_MT)
17982 fputs ("\n\tMT ASE", stdout);
17983 if (mask & AFL_ASE_SMARTMIPS)
17984 fputs ("\n\tSmartMIPS ASE", stdout);
17985 if (mask & AFL_ASE_VIRT)
17986 fputs ("\n\tVZ ASE", stdout);
17987 if (mask & AFL_ASE_MSA)
17988 fputs ("\n\tMSA ASE", stdout);
17989 if (mask & AFL_ASE_MIPS16)
17990 fputs ("\n\tMIPS16 ASE", stdout);
17991 if (mask & AFL_ASE_MICROMIPS)
17992 fputs ("\n\tMICROMIPS ASE", stdout);
17993 if (mask & AFL_ASE_XPA)
17994 fputs ("\n\tXPA ASE", stdout);
17995 if (mask & AFL_ASE_MIPS16E2)
17996 fputs ("\n\tMIPS16e2 ASE", stdout);
17997 if (mask & AFL_ASE_CRC)
17998 fputs ("\n\tCRC ASE", stdout);
17999 if (mask & AFL_ASE_GINV)
18000 fputs ("\n\tGINV ASE", stdout);
18001 if (mask & AFL_ASE_LOONGSON_MMI)
18002 fputs ("\n\tLoongson MMI ASE", stdout);
18003 if (mask & AFL_ASE_LOONGSON_CAM)
18004 fputs ("\n\tLoongson CAM ASE", stdout);
18005 if (mask & AFL_ASE_LOONGSON_EXT)
18006 fputs ("\n\tLoongson EXT ASE", stdout);
18007 if (mask & AFL_ASE_LOONGSON_EXT2)
18008 fputs ("\n\tLoongson EXT2 ASE", stdout);
18009 if (mask == 0)
18010 fprintf (stdout, "\n\t%s", _("None"));
18011 else if ((mask & ~AFL_ASE_MASK) != 0)
18012 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18015 static void
18016 print_mips_isa_ext (unsigned int isa_ext)
18018 switch (isa_ext)
18020 case 0:
18021 fputs (_("None"), stdout);
18022 break;
18023 case AFL_EXT_XLR:
18024 fputs ("RMI XLR", stdout);
18025 break;
18026 case AFL_EXT_OCTEON3:
18027 fputs ("Cavium Networks Octeon3", stdout);
18028 break;
18029 case AFL_EXT_OCTEON2:
18030 fputs ("Cavium Networks Octeon2", stdout);
18031 break;
18032 case AFL_EXT_OCTEONP:
18033 fputs ("Cavium Networks OcteonP", stdout);
18034 break;
18035 case AFL_EXT_OCTEON:
18036 fputs ("Cavium Networks Octeon", stdout);
18037 break;
18038 case AFL_EXT_5900:
18039 fputs ("Toshiba R5900", stdout);
18040 break;
18041 case AFL_EXT_4650:
18042 fputs ("MIPS R4650", stdout);
18043 break;
18044 case AFL_EXT_4010:
18045 fputs ("LSI R4010", stdout);
18046 break;
18047 case AFL_EXT_4100:
18048 fputs ("NEC VR4100", stdout);
18049 break;
18050 case AFL_EXT_3900:
18051 fputs ("Toshiba R3900", stdout);
18052 break;
18053 case AFL_EXT_10000:
18054 fputs ("MIPS R10000", stdout);
18055 break;
18056 case AFL_EXT_SB1:
18057 fputs ("Broadcom SB-1", stdout);
18058 break;
18059 case AFL_EXT_4111:
18060 fputs ("NEC VR4111/VR4181", stdout);
18061 break;
18062 case AFL_EXT_4120:
18063 fputs ("NEC VR4120", stdout);
18064 break;
18065 case AFL_EXT_5400:
18066 fputs ("NEC VR5400", stdout);
18067 break;
18068 case AFL_EXT_5500:
18069 fputs ("NEC VR5500", stdout);
18070 break;
18071 case AFL_EXT_LOONGSON_2E:
18072 fputs ("ST Microelectronics Loongson 2E", stdout);
18073 break;
18074 case AFL_EXT_LOONGSON_2F:
18075 fputs ("ST Microelectronics Loongson 2F", stdout);
18076 break;
18077 case AFL_EXT_INTERAPTIV_MR2:
18078 fputs ("Imagination interAptiv MR2", stdout);
18079 break;
18080 default:
18081 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
18085 static signed int
18086 get_mips_reg_size (int reg_size)
18088 return (reg_size == AFL_REG_NONE) ? 0
18089 : (reg_size == AFL_REG_32) ? 32
18090 : (reg_size == AFL_REG_64) ? 64
18091 : (reg_size == AFL_REG_128) ? 128
18092 : -1;
18095 static bool
18096 process_mips_specific (Filedata * filedata)
18098 Elf_Internal_Dyn * entry;
18099 Elf_Internal_Shdr *sect = NULL;
18100 size_t liblist_offset = 0;
18101 size_t liblistno = 0;
18102 size_t conflictsno = 0;
18103 size_t options_offset = 0;
18104 size_t conflicts_offset = 0;
18105 size_t pltrelsz = 0;
18106 size_t pltrel = 0;
18107 bfd_vma pltgot = 0;
18108 bfd_vma mips_pltgot = 0;
18109 bfd_vma jmprel = 0;
18110 bfd_vma local_gotno = 0;
18111 bfd_vma gotsym = 0;
18112 bfd_vma symtabno = 0;
18113 bool res = true;
18115 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18116 display_mips_gnu_attribute))
18117 res = false;
18119 sect = find_section (filedata, ".MIPS.abiflags");
18121 if (sect != NULL)
18123 Elf_External_ABIFlags_v0 *abiflags_ext;
18124 Elf_Internal_ABIFlags_v0 abiflags_in;
18126 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
18128 error (_("Corrupt MIPS ABI Flags section.\n"));
18129 res = false;
18131 else
18133 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
18134 sect->sh_size, _("MIPS ABI Flags section"));
18135 if (abiflags_ext)
18137 abiflags_in.version = BYTE_GET (abiflags_ext->version);
18138 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
18139 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
18140 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
18141 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
18142 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
18143 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
18144 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
18145 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
18146 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
18147 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
18149 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
18150 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
18151 if (abiflags_in.isa_rev > 1)
18152 printf ("r%d", abiflags_in.isa_rev);
18153 printf ("\nGPR size: %d",
18154 get_mips_reg_size (abiflags_in.gpr_size));
18155 printf ("\nCPR1 size: %d",
18156 get_mips_reg_size (abiflags_in.cpr1_size));
18157 printf ("\nCPR2 size: %d",
18158 get_mips_reg_size (abiflags_in.cpr2_size));
18159 fputs ("\nFP ABI: ", stdout);
18160 print_mips_fp_abi_value (abiflags_in.fp_abi);
18161 fputs ("ISA Extension: ", stdout);
18162 print_mips_isa_ext (abiflags_in.isa_ext);
18163 fputs ("\nASEs:", stdout);
18164 print_mips_ases (abiflags_in.ases);
18165 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
18166 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
18167 fputc ('\n', stdout);
18168 free (abiflags_ext);
18173 /* We have a lot of special sections. Thanks SGI! */
18174 if (filedata->dynamic_section == NULL)
18176 /* No dynamic information available. See if there is static GOT. */
18177 sect = find_section (filedata, ".got");
18178 if (sect != NULL)
18180 unsigned char *data_end;
18181 unsigned char *data;
18182 bfd_vma ent, end;
18183 int addr_size;
18185 pltgot = sect->sh_addr;
18187 ent = pltgot;
18188 addr_size = (is_32bit_elf ? 4 : 8);
18189 end = pltgot + sect->sh_size;
18191 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
18192 end - pltgot, 1,
18193 _("Global Offset Table data"));
18194 /* PR 12855: Null data is handled gracefully throughout. */
18195 data_end = data + (end - pltgot);
18197 printf (_("\nStatic GOT:\n"));
18198 printf (_(" Canonical gp value: "));
18199 print_vma (ent + 0x7ff0, LONG_HEX);
18200 printf ("\n\n");
18202 /* In a dynamic binary GOT[0] is reserved for the dynamic
18203 loader to store the lazy resolver pointer, however in
18204 a static binary it may well have been omitted and GOT
18205 reduced to a table of addresses.
18206 PR 21344: Check for the entry being fully available
18207 before fetching it. */
18208 if (data
18209 && data + ent - pltgot + addr_size <= data_end
18210 && byte_get (data + ent - pltgot, addr_size) == 0)
18212 printf (_(" Reserved entries:\n"));
18213 printf (_(" %*s %10s %*s\n"),
18214 addr_size * 2, _("Address"), _("Access"),
18215 addr_size * 2, _("Value"));
18216 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18217 printf ("\n");
18218 if (ent == (bfd_vma) -1)
18219 goto sgot_print_fail;
18221 /* Check for the MSB of GOT[1] being set, identifying a
18222 GNU object. This entry will be used by some runtime
18223 loaders, to store the module pointer. Otherwise this
18224 is an ordinary local entry.
18225 PR 21344: Check for the entry being fully available
18226 before fetching it. */
18227 if (data
18228 && data + ent - pltgot + addr_size <= data_end
18229 && (byte_get (data + ent - pltgot, addr_size)
18230 >> (addr_size * 8 - 1)) != 0)
18232 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18233 printf ("\n");
18234 if (ent == (bfd_vma) -1)
18235 goto sgot_print_fail;
18237 printf ("\n");
18240 if (data != NULL && ent < end)
18242 printf (_(" Local entries:\n"));
18243 printf (" %*s %10s %*s\n",
18244 addr_size * 2, _("Address"), _("Access"),
18245 addr_size * 2, _("Value"));
18246 while (ent < end)
18248 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18249 printf ("\n");
18250 if (ent == (bfd_vma) -1)
18251 goto sgot_print_fail;
18253 printf ("\n");
18256 sgot_print_fail:
18257 free (data);
18259 return res;
18262 for (entry = filedata->dynamic_section;
18263 /* PR 17531 file: 012-50589-0.004. */
18264 (entry < filedata->dynamic_section + filedata->dynamic_nent
18265 && entry->d_tag != DT_NULL);
18266 ++entry)
18267 switch (entry->d_tag)
18269 case DT_MIPS_LIBLIST:
18270 liblist_offset
18271 = offset_from_vma (filedata, entry->d_un.d_val,
18272 liblistno * sizeof (Elf32_External_Lib));
18273 break;
18274 case DT_MIPS_LIBLISTNO:
18275 liblistno = entry->d_un.d_val;
18276 break;
18277 case DT_MIPS_OPTIONS:
18278 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
18279 break;
18280 case DT_MIPS_CONFLICT:
18281 conflicts_offset
18282 = offset_from_vma (filedata, entry->d_un.d_val,
18283 conflictsno * sizeof (Elf32_External_Conflict));
18284 break;
18285 case DT_MIPS_CONFLICTNO:
18286 conflictsno = entry->d_un.d_val;
18287 break;
18288 case DT_PLTGOT:
18289 pltgot = entry->d_un.d_ptr;
18290 break;
18291 case DT_MIPS_LOCAL_GOTNO:
18292 local_gotno = entry->d_un.d_val;
18293 break;
18294 case DT_MIPS_GOTSYM:
18295 gotsym = entry->d_un.d_val;
18296 break;
18297 case DT_MIPS_SYMTABNO:
18298 symtabno = entry->d_un.d_val;
18299 break;
18300 case DT_MIPS_PLTGOT:
18301 mips_pltgot = entry->d_un.d_ptr;
18302 break;
18303 case DT_PLTREL:
18304 pltrel = entry->d_un.d_val;
18305 break;
18306 case DT_PLTRELSZ:
18307 pltrelsz = entry->d_un.d_val;
18308 break;
18309 case DT_JMPREL:
18310 jmprel = entry->d_un.d_ptr;
18311 break;
18312 default:
18313 break;
18316 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18318 Elf32_External_Lib * elib;
18319 size_t cnt;
18321 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18322 sizeof (Elf32_External_Lib),
18323 liblistno,
18324 _("liblist section data"));
18325 if (elib)
18327 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18328 "\nSection '.liblist' contains %lu entries:\n",
18329 (unsigned long) liblistno),
18330 (unsigned long) liblistno);
18331 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
18332 stdout);
18334 for (cnt = 0; cnt < liblistno; ++cnt)
18336 Elf32_Lib liblist;
18337 time_t atime;
18338 char timebuf[128];
18339 struct tm * tmp;
18341 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18342 atime = BYTE_GET (elib[cnt].l_time_stamp);
18343 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18344 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18345 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18347 tmp = gmtime (&atime);
18348 snprintf (timebuf, sizeof (timebuf),
18349 "%04u-%02u-%02uT%02u:%02u:%02u",
18350 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18351 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18353 printf ("%3lu: ", (unsigned long) cnt);
18354 if (valid_dynamic_name (filedata, liblist.l_name))
18355 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18356 else
18357 printf (_("<corrupt: %9ld>"), liblist.l_name);
18358 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18359 liblist.l_version);
18361 if (liblist.l_flags == 0)
18362 puts (_(" NONE"));
18363 else
18365 static const struct
18367 const char * name;
18368 int bit;
18370 l_flags_vals[] =
18372 { " EXACT_MATCH", LL_EXACT_MATCH },
18373 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18374 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18375 { " EXPORTS", LL_EXPORTS },
18376 { " DELAY_LOAD", LL_DELAY_LOAD },
18377 { " DELTA", LL_DELTA }
18379 int flags = liblist.l_flags;
18380 size_t fcnt;
18382 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18383 if ((flags & l_flags_vals[fcnt].bit) != 0)
18385 fputs (l_flags_vals[fcnt].name, stdout);
18386 flags ^= l_flags_vals[fcnt].bit;
18388 if (flags != 0)
18389 printf (" %#x", (unsigned int) flags);
18391 puts ("");
18395 free (elib);
18397 else
18398 res = false;
18401 if (options_offset != 0)
18403 Elf_External_Options * eopt;
18404 size_t offset;
18405 int cnt;
18407 /* Find the section header so that we get the size. */
18408 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18409 /* PR 17533 file: 012-277276-0.004. */
18410 if (sect == NULL)
18412 error (_("No MIPS_OPTIONS header found\n"));
18413 return false;
18415 /* PR 24243 */
18416 if (sect->sh_size < sizeof (* eopt))
18418 error (_("The MIPS options section is too small.\n"));
18419 return false;
18422 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18423 sect->sh_size, _("options"));
18424 if (eopt)
18426 Elf_Internal_Options option;
18428 offset = cnt = 0;
18429 while (offset <= sect->sh_size - sizeof (* eopt))
18431 Elf_External_Options * eoption;
18432 unsigned int optsize;
18434 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18436 optsize = BYTE_GET (eoption->size);
18438 /* PR 17531: file: ffa0fa3b. */
18439 if (optsize < sizeof (* eopt)
18440 || optsize > sect->sh_size - offset)
18442 error (_("Invalid size (%u) for MIPS option\n"),
18443 optsize);
18444 free (eopt);
18445 return false;
18447 offset += optsize;
18448 ++cnt;
18451 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18452 "\nSection '%s' contains %d entries:\n",
18453 cnt),
18454 printable_section_name (filedata, sect), cnt);
18456 offset = 0;
18457 while (cnt-- > 0)
18459 size_t len;
18460 Elf_External_Options * eoption;
18462 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18464 option.kind = BYTE_GET (eoption->kind);
18465 option.size = BYTE_GET (eoption->size);
18466 option.section = BYTE_GET (eoption->section);
18467 option.info = BYTE_GET (eoption->info);
18469 switch (option.kind)
18471 case ODK_NULL:
18472 /* This shouldn't happen. */
18473 printf (" NULL %" PRId16 " %" PRIx32,
18474 option.section, option.info);
18475 break;
18477 case ODK_REGINFO:
18478 printf (" REGINFO ");
18479 if (filedata->file_header.e_machine == EM_MIPS)
18481 Elf32_External_RegInfo * ereg;
18482 Elf32_RegInfo reginfo;
18484 /* 32bit form. */
18485 if (option.size < (sizeof (Elf_External_Options)
18486 + sizeof (Elf32_External_RegInfo)))
18488 printf (_("<corrupt>\n"));
18489 error (_("Truncated MIPS REGINFO option\n"));
18490 cnt = 0;
18491 break;
18494 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18496 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18497 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18498 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18499 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18500 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18501 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18503 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18504 reginfo.ri_gprmask, reginfo.ri_gp_value);
18505 printf (" "
18506 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18507 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18508 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18509 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18511 else
18513 /* 64 bit form. */
18514 Elf64_External_RegInfo * ereg;
18515 Elf64_Internal_RegInfo reginfo;
18517 if (option.size < (sizeof (Elf_External_Options)
18518 + sizeof (Elf64_External_RegInfo)))
18520 printf (_("<corrupt>\n"));
18521 error (_("Truncated MIPS REGINFO option\n"));
18522 cnt = 0;
18523 break;
18526 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18527 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18528 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18529 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18530 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18531 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18532 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18534 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18535 reginfo.ri_gprmask, reginfo.ri_gp_value);
18536 printf (" "
18537 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18538 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18539 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18540 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18542 offset += option.size;
18543 continue;
18545 case ODK_EXCEPTIONS:
18546 fputs (" EXCEPTIONS fpe_min(", stdout);
18547 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18548 fputs (") fpe_max(", stdout);
18549 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18550 fputs (")", stdout);
18552 if (option.info & OEX_PAGE0)
18553 fputs (" PAGE0", stdout);
18554 if (option.info & OEX_SMM)
18555 fputs (" SMM", stdout);
18556 if (option.info & OEX_FPDBUG)
18557 fputs (" FPDBUG", stdout);
18558 if (option.info & OEX_DISMISS)
18559 fputs (" DISMISS", stdout);
18560 break;
18562 case ODK_PAD:
18563 fputs (" PAD ", stdout);
18564 if (option.info & OPAD_PREFIX)
18565 fputs (" PREFIX", stdout);
18566 if (option.info & OPAD_POSTFIX)
18567 fputs (" POSTFIX", stdout);
18568 if (option.info & OPAD_SYMBOL)
18569 fputs (" SYMBOL", stdout);
18570 break;
18572 case ODK_HWPATCH:
18573 fputs (" HWPATCH ", stdout);
18574 if (option.info & OHW_R4KEOP)
18575 fputs (" R4KEOP", stdout);
18576 if (option.info & OHW_R8KPFETCH)
18577 fputs (" R8KPFETCH", stdout);
18578 if (option.info & OHW_R5KEOP)
18579 fputs (" R5KEOP", stdout);
18580 if (option.info & OHW_R5KCVTL)
18581 fputs (" R5KCVTL", stdout);
18582 break;
18584 case ODK_FILL:
18585 fputs (" FILL ", stdout);
18586 /* XXX Print content of info word? */
18587 break;
18589 case ODK_TAGS:
18590 fputs (" TAGS ", stdout);
18591 /* XXX Print content of info word? */
18592 break;
18594 case ODK_HWAND:
18595 fputs (" HWAND ", stdout);
18596 if (option.info & OHWA0_R4KEOP_CHECKED)
18597 fputs (" R4KEOP_CHECKED", stdout);
18598 if (option.info & OHWA0_R4KEOP_CLEAN)
18599 fputs (" R4KEOP_CLEAN", stdout);
18600 break;
18602 case ODK_HWOR:
18603 fputs (" HWOR ", stdout);
18604 if (option.info & OHWA0_R4KEOP_CHECKED)
18605 fputs (" R4KEOP_CHECKED", stdout);
18606 if (option.info & OHWA0_R4KEOP_CLEAN)
18607 fputs (" R4KEOP_CLEAN", stdout);
18608 break;
18610 case ODK_GP_GROUP:
18611 printf (" GP_GROUP %#06x self-contained %#06x",
18612 option.info & OGP_GROUP,
18613 (option.info & OGP_SELF) >> 16);
18614 break;
18616 case ODK_IDENT:
18617 printf (" IDENT %#06x self-contained %#06x",
18618 option.info & OGP_GROUP,
18619 (option.info & OGP_SELF) >> 16);
18620 break;
18622 default:
18623 /* This shouldn't happen. */
18624 printf (" %3d ??? %" PRId16 " %" PRIx32,
18625 option.kind, option.section, option.info);
18626 break;
18629 len = sizeof (* eopt);
18630 while (len < option.size)
18632 unsigned char datum = *((unsigned char *) eoption + len);
18634 if (ISPRINT (datum))
18635 printf ("%c", datum);
18636 else
18637 printf ("\\%03o", datum);
18638 len ++;
18640 fputs ("\n", stdout);
18642 offset += option.size;
18644 free (eopt);
18646 else
18647 res = false;
18650 if (conflicts_offset != 0 && conflictsno != 0)
18652 Elf32_Conflict * iconf;
18653 size_t cnt;
18655 if (filedata->dynamic_symbols == NULL)
18657 error (_("conflict list found without a dynamic symbol table\n"));
18658 return false;
18661 /* PR 21345 - print a slightly more helpful error message
18662 if we are sure that the cmalloc will fail. */
18663 if (conflictsno > filedata->file_size / sizeof (* iconf))
18665 error (_("Overlarge number of conflicts detected: %lx\n"),
18666 (long) conflictsno);
18667 return false;
18670 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18671 if (iconf == NULL)
18673 error (_("Out of memory allocating space for dynamic conflicts\n"));
18674 return false;
18677 if (is_32bit_elf)
18679 Elf32_External_Conflict * econf32;
18681 econf32 = (Elf32_External_Conflict *)
18682 get_data (NULL, filedata, conflicts_offset,
18683 sizeof (*econf32), conflictsno, _("conflict"));
18684 if (!econf32)
18686 free (iconf);
18687 return false;
18690 for (cnt = 0; cnt < conflictsno; ++cnt)
18691 iconf[cnt] = BYTE_GET (econf32[cnt]);
18693 free (econf32);
18695 else
18697 Elf64_External_Conflict * econf64;
18699 econf64 = (Elf64_External_Conflict *)
18700 get_data (NULL, filedata, conflicts_offset,
18701 sizeof (*econf64), conflictsno, _("conflict"));
18702 if (!econf64)
18704 free (iconf);
18705 return false;
18708 for (cnt = 0; cnt < conflictsno; ++cnt)
18709 iconf[cnt] = BYTE_GET (econf64[cnt]);
18711 free (econf64);
18714 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18715 "\nSection '.conflict' contains %lu entries:\n",
18716 (unsigned long) conflictsno),
18717 (unsigned long) conflictsno);
18718 puts (_(" Num: Index Value Name"));
18720 for (cnt = 0; cnt < conflictsno; ++cnt)
18722 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18724 if (iconf[cnt] >= filedata->num_dynamic_syms)
18725 printf (_("<corrupt symbol index>"));
18726 else
18728 Elf_Internal_Sym * psym;
18730 psym = & filedata->dynamic_symbols[iconf[cnt]];
18731 print_vma (psym->st_value, FULL_HEX);
18732 putchar (' ');
18733 if (valid_dynamic_name (filedata, psym->st_name))
18734 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
18735 else
18736 printf (_("<corrupt: %14ld>"), psym->st_name);
18738 putchar ('\n');
18741 free (iconf);
18744 if (pltgot != 0 && local_gotno != 0)
18746 bfd_vma ent, local_end, global_end;
18747 size_t i, offset;
18748 unsigned char * data;
18749 unsigned char * data_end;
18750 int addr_size;
18752 ent = pltgot;
18753 addr_size = (is_32bit_elf ? 4 : 8);
18754 local_end = pltgot + local_gotno * addr_size;
18756 /* PR binutils/17533 file: 012-111227-0.004 */
18757 if (symtabno < gotsym)
18759 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18760 (unsigned long) gotsym, (unsigned long) symtabno);
18761 return false;
18764 global_end = local_end + (symtabno - gotsym) * addr_size;
18765 /* PR 17531: file: 54c91a34. */
18766 if (global_end < local_end)
18768 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18769 return false;
18772 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18773 data = (unsigned char *) get_data (NULL, filedata, offset,
18774 global_end - pltgot, 1,
18775 _("Global Offset Table data"));
18776 /* PR 12855: Null data is handled gracefully throughout. */
18777 data_end = data + (global_end - pltgot);
18779 printf (_("\nPrimary GOT:\n"));
18780 printf (_(" Canonical gp value: "));
18781 print_vma (pltgot + 0x7ff0, LONG_HEX);
18782 printf ("\n\n");
18784 printf (_(" Reserved entries:\n"));
18785 printf (_(" %*s %10s %*s Purpose\n"),
18786 addr_size * 2, _("Address"), _("Access"),
18787 addr_size * 2, _("Initial"));
18788 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18789 printf (_(" Lazy resolver\n"));
18790 if (ent == (bfd_vma) -1)
18791 goto got_print_fail;
18793 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18794 This entry will be used by some runtime loaders, to store the
18795 module pointer. Otherwise this is an ordinary local entry.
18796 PR 21344: Check for the entry being fully available before
18797 fetching it. */
18798 if (data
18799 && data + ent - pltgot + addr_size <= data_end
18800 && (byte_get (data + ent - pltgot, addr_size)
18801 >> (addr_size * 8 - 1)) != 0)
18803 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18804 printf (_(" Module pointer (GNU extension)\n"));
18805 if (ent == (bfd_vma) -1)
18806 goto got_print_fail;
18808 printf ("\n");
18810 if (data != NULL && ent < local_end)
18812 printf (_(" Local entries:\n"));
18813 printf (" %*s %10s %*s\n",
18814 addr_size * 2, _("Address"), _("Access"),
18815 addr_size * 2, _("Initial"));
18816 while (ent < local_end)
18818 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18819 printf ("\n");
18820 if (ent == (bfd_vma) -1)
18821 goto got_print_fail;
18823 printf ("\n");
18826 if (data != NULL && gotsym < symtabno)
18828 int sym_width;
18830 printf (_(" Global entries:\n"));
18831 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
18832 addr_size * 2, _("Address"),
18833 _("Access"),
18834 addr_size * 2, _("Initial"),
18835 addr_size * 2, _("Sym.Val."),
18836 _("Type"),
18837 /* Note for translators: "Ndx" = abbreviated form of "Index". */
18838 _("Ndx"), _("Name"));
18840 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
18842 for (i = gotsym; i < symtabno; i++)
18844 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18845 printf (" ");
18847 if (filedata->dynamic_symbols == NULL)
18848 printf (_("<no dynamic symbols>"));
18849 else if (i < filedata->num_dynamic_syms)
18851 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
18853 print_vma (psym->st_value, LONG_HEX);
18854 printf (" %-7s %3s ",
18855 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18856 get_symbol_index_type (filedata, psym->st_shndx));
18858 if (valid_dynamic_name (filedata, psym->st_name))
18859 print_symbol (sym_width,
18860 get_dynamic_name (filedata, psym->st_name));
18861 else
18862 printf (_("<corrupt: %14ld>"), psym->st_name);
18864 else
18865 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
18866 (unsigned long) i);
18868 printf ("\n");
18869 if (ent == (bfd_vma) -1)
18870 break;
18872 printf ("\n");
18875 got_print_fail:
18876 free (data);
18879 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
18881 bfd_vma ent, end;
18882 size_t offset, rel_offset;
18883 unsigned long count, i;
18884 unsigned char * data;
18885 int addr_size, sym_width;
18886 Elf_Internal_Rela * rels;
18888 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
18889 if (pltrel == DT_RELA)
18891 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18892 return false;
18894 else
18896 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18897 return false;
18900 ent = mips_pltgot;
18901 addr_size = (is_32bit_elf ? 4 : 8);
18902 end = mips_pltgot + (2 + count) * addr_size;
18904 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
18905 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
18906 1, _("Procedure Linkage Table data"));
18907 if (data == NULL)
18909 free (rels);
18910 return false;
18913 printf ("\nPLT GOT:\n\n");
18914 printf (_(" Reserved entries:\n"));
18915 printf (_(" %*s %*s Purpose\n"),
18916 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
18917 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18918 printf (_(" PLT lazy resolver\n"));
18919 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18920 printf (_(" Module pointer\n"));
18921 printf ("\n");
18923 printf (_(" Entries:\n"));
18924 printf (" %*s %*s %*s %-7s %3s %s\n",
18925 addr_size * 2, _("Address"),
18926 addr_size * 2, _("Initial"),
18927 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
18928 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
18929 for (i = 0; i < count; i++)
18931 unsigned long idx = get_reloc_symindex (rels[i].r_info);
18933 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18934 printf (" ");
18936 if (idx >= filedata->num_dynamic_syms)
18937 printf (_("<corrupt symbol index: %lu>"), idx);
18938 else
18940 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
18942 print_vma (psym->st_value, LONG_HEX);
18943 printf (" %-7s %3s ",
18944 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18945 get_symbol_index_type (filedata, psym->st_shndx));
18946 if (valid_dynamic_name (filedata, psym->st_name))
18947 print_symbol (sym_width,
18948 get_dynamic_name (filedata, psym->st_name));
18949 else
18950 printf (_("<corrupt: %14ld>"), psym->st_name);
18952 printf ("\n");
18954 printf ("\n");
18956 free (data);
18957 free (rels);
18960 return res;
18963 static bool
18964 process_nds32_specific (Filedata * filedata)
18966 Elf_Internal_Shdr *sect = NULL;
18968 sect = find_section (filedata, ".nds32_e_flags");
18969 if (sect != NULL && sect->sh_size >= 4)
18971 unsigned char *buf;
18972 unsigned int flag;
18974 printf ("\nNDS32 elf flags section:\n");
18975 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
18976 _("NDS32 elf flags section"));
18978 if (buf == NULL)
18979 return false;
18981 flag = byte_get (buf, 4);
18982 free (buf);
18983 switch (flag & 0x3)
18985 case 0:
18986 printf ("(VEC_SIZE):\tNo entry.\n");
18987 break;
18988 case 1:
18989 printf ("(VEC_SIZE):\t4 bytes\n");
18990 break;
18991 case 2:
18992 printf ("(VEC_SIZE):\t16 bytes\n");
18993 break;
18994 case 3:
18995 printf ("(VEC_SIZE):\treserved\n");
18996 break;
19000 return true;
19003 static bool
19004 process_gnu_liblist (Filedata * filedata)
19006 Elf_Internal_Shdr * section;
19007 Elf_Internal_Shdr * string_sec;
19008 Elf32_External_Lib * elib;
19009 char * strtab;
19010 size_t strtab_size;
19011 size_t cnt;
19012 unsigned long num_liblist;
19013 unsigned i;
19014 bool res = true;
19016 if (! do_arch)
19017 return true;
19019 for (i = 0, section = filedata->section_headers;
19020 i < filedata->file_header.e_shnum;
19021 i++, section++)
19023 switch (section->sh_type)
19025 case SHT_GNU_LIBLIST:
19026 if (section->sh_link >= filedata->file_header.e_shnum)
19027 break;
19029 elib = (Elf32_External_Lib *)
19030 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
19031 _("liblist section data"));
19033 if (elib == NULL)
19035 res = false;
19036 break;
19039 string_sec = filedata->section_headers + section->sh_link;
19040 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
19041 string_sec->sh_size,
19042 _("liblist string table"));
19043 if (strtab == NULL
19044 || section->sh_entsize != sizeof (Elf32_External_Lib))
19046 free (elib);
19047 free (strtab);
19048 res = false;
19049 break;
19051 strtab_size = string_sec->sh_size;
19053 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
19054 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
19055 "\nLibrary list section '%s' contains %lu entries:\n",
19056 num_liblist),
19057 printable_section_name (filedata, section),
19058 num_liblist);
19060 puts (_(" Library Time Stamp Checksum Version Flags"));
19062 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
19063 ++cnt)
19065 Elf32_Lib liblist;
19066 time_t atime;
19067 char timebuf[128];
19068 struct tm * tmp;
19070 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19071 atime = BYTE_GET (elib[cnt].l_time_stamp);
19072 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19073 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19074 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19076 tmp = gmtime (&atime);
19077 snprintf (timebuf, sizeof (timebuf),
19078 "%04u-%02u-%02uT%02u:%02u:%02u",
19079 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19080 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19082 printf ("%3lu: ", (unsigned long) cnt);
19083 if (do_wide)
19084 printf ("%-20s", liblist.l_name < strtab_size
19085 ? strtab + liblist.l_name : _("<corrupt>"));
19086 else
19087 printf ("%-20.20s", liblist.l_name < strtab_size
19088 ? strtab + liblist.l_name : _("<corrupt>"));
19089 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
19090 liblist.l_version, liblist.l_flags);
19093 free (elib);
19094 free (strtab);
19098 return res;
19101 static const char *
19102 get_note_type (Filedata * filedata, unsigned e_type)
19104 static char buff[64];
19106 if (filedata->file_header.e_type == ET_CORE)
19107 switch (e_type)
19109 case NT_AUXV:
19110 return _("NT_AUXV (auxiliary vector)");
19111 case NT_PRSTATUS:
19112 return _("NT_PRSTATUS (prstatus structure)");
19113 case NT_FPREGSET:
19114 return _("NT_FPREGSET (floating point registers)");
19115 case NT_PRPSINFO:
19116 return _("NT_PRPSINFO (prpsinfo structure)");
19117 case NT_TASKSTRUCT:
19118 return _("NT_TASKSTRUCT (task structure)");
19119 case NT_GDB_TDESC:
19120 return _("NT_GDB_TDESC (GDB XML target description)");
19121 case NT_PRXFPREG:
19122 return _("NT_PRXFPREG (user_xfpregs structure)");
19123 case NT_PPC_VMX:
19124 return _("NT_PPC_VMX (ppc Altivec registers)");
19125 case NT_PPC_VSX:
19126 return _("NT_PPC_VSX (ppc VSX registers)");
19127 case NT_PPC_TAR:
19128 return _("NT_PPC_TAR (ppc TAR register)");
19129 case NT_PPC_PPR:
19130 return _("NT_PPC_PPR (ppc PPR register)");
19131 case NT_PPC_DSCR:
19132 return _("NT_PPC_DSCR (ppc DSCR register)");
19133 case NT_PPC_EBB:
19134 return _("NT_PPC_EBB (ppc EBB registers)");
19135 case NT_PPC_PMU:
19136 return _("NT_PPC_PMU (ppc PMU registers)");
19137 case NT_PPC_TM_CGPR:
19138 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
19139 case NT_PPC_TM_CFPR:
19140 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
19141 case NT_PPC_TM_CVMX:
19142 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
19143 case NT_PPC_TM_CVSX:
19144 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
19145 case NT_PPC_TM_SPR:
19146 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
19147 case NT_PPC_TM_CTAR:
19148 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
19149 case NT_PPC_TM_CPPR:
19150 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
19151 case NT_PPC_TM_CDSCR:
19152 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
19153 case NT_386_TLS:
19154 return _("NT_386_TLS (x86 TLS information)");
19155 case NT_386_IOPERM:
19156 return _("NT_386_IOPERM (x86 I/O permissions)");
19157 case NT_X86_XSTATE:
19158 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
19159 case NT_X86_CET:
19160 return _("NT_X86_CET (x86 CET state)");
19161 case NT_S390_HIGH_GPRS:
19162 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
19163 case NT_S390_TIMER:
19164 return _("NT_S390_TIMER (s390 timer register)");
19165 case NT_S390_TODCMP:
19166 return _("NT_S390_TODCMP (s390 TOD comparator register)");
19167 case NT_S390_TODPREG:
19168 return _("NT_S390_TODPREG (s390 TOD programmable register)");
19169 case NT_S390_CTRS:
19170 return _("NT_S390_CTRS (s390 control registers)");
19171 case NT_S390_PREFIX:
19172 return _("NT_S390_PREFIX (s390 prefix register)");
19173 case NT_S390_LAST_BREAK:
19174 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
19175 case NT_S390_SYSTEM_CALL:
19176 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
19177 case NT_S390_TDB:
19178 return _("NT_S390_TDB (s390 transaction diagnostic block)");
19179 case NT_S390_VXRS_LOW:
19180 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
19181 case NT_S390_VXRS_HIGH:
19182 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
19183 case NT_S390_GS_CB:
19184 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
19185 case NT_S390_GS_BC:
19186 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
19187 case NT_ARM_VFP:
19188 return _("NT_ARM_VFP (arm VFP registers)");
19189 case NT_ARM_TLS:
19190 return _("NT_ARM_TLS (AArch TLS registers)");
19191 case NT_ARM_HW_BREAK:
19192 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
19193 case NT_ARM_HW_WATCH:
19194 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
19195 case NT_ARM_SVE:
19196 return _("NT_ARM_SVE (AArch SVE registers)");
19197 case NT_ARM_PAC_MASK:
19198 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
19199 case NT_ARM_PACA_KEYS:
19200 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
19201 case NT_ARM_PACG_KEYS:
19202 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
19203 case NT_ARM_TAGGED_ADDR_CTRL:
19204 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
19205 case NT_ARM_PAC_ENABLED_KEYS:
19206 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
19207 case NT_ARC_V2:
19208 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
19209 case NT_RISCV_CSR:
19210 return _("NT_RISCV_CSR (RISC-V control and status registers)");
19211 case NT_PSTATUS:
19212 return _("NT_PSTATUS (pstatus structure)");
19213 case NT_FPREGS:
19214 return _("NT_FPREGS (floating point registers)");
19215 case NT_PSINFO:
19216 return _("NT_PSINFO (psinfo structure)");
19217 case NT_LWPSTATUS:
19218 return _("NT_LWPSTATUS (lwpstatus_t structure)");
19219 case NT_LWPSINFO:
19220 return _("NT_LWPSINFO (lwpsinfo_t structure)");
19221 case NT_WIN32PSTATUS:
19222 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
19223 case NT_SIGINFO:
19224 return _("NT_SIGINFO (siginfo_t data)");
19225 case NT_FILE:
19226 return _("NT_FILE (mapped files)");
19227 default:
19228 break;
19230 else
19231 switch (e_type)
19233 case NT_VERSION:
19234 return _("NT_VERSION (version)");
19235 case NT_ARCH:
19236 return _("NT_ARCH (architecture)");
19237 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19238 return _("OPEN");
19239 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19240 return _("func");
19241 case NT_GO_BUILDID:
19242 return _("GO BUILDID");
19243 case FDO_PACKAGING_METADATA:
19244 return _("FDO_PACKAGING_METADATA");
19245 default:
19246 break;
19249 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19250 return buff;
19253 static bool
19254 print_core_note (Elf_Internal_Note *pnote)
19256 unsigned int addr_size = is_32bit_elf ? 4 : 8;
19257 bfd_vma count, page_size;
19258 unsigned char *descdata, *filenames, *descend;
19260 if (pnote->type != NT_FILE)
19262 if (do_wide)
19263 printf ("\n");
19264 return true;
19267 #ifndef BFD64
19268 if (!is_32bit_elf)
19270 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
19271 /* Still "successful". */
19272 return true;
19274 #endif
19276 if (pnote->descsz < 2 * addr_size)
19278 error (_(" Malformed note - too short for header\n"));
19279 return false;
19282 descdata = (unsigned char *) pnote->descdata;
19283 descend = descdata + pnote->descsz;
19285 if (descdata[pnote->descsz - 1] != '\0')
19287 error (_(" Malformed note - does not end with \\0\n"));
19288 return false;
19291 count = byte_get (descdata, addr_size);
19292 descdata += addr_size;
19294 page_size = byte_get (descdata, addr_size);
19295 descdata += addr_size;
19297 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
19298 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
19300 error (_(" Malformed note - too short for supplied file count\n"));
19301 return false;
19304 printf (_(" Page size: "));
19305 print_vma (page_size, DEC);
19306 printf ("\n");
19308 printf (_(" %*s%*s%*s\n"),
19309 (int) (2 + 2 * addr_size), _("Start"),
19310 (int) (4 + 2 * addr_size), _("End"),
19311 (int) (4 + 2 * addr_size), _("Page Offset"));
19312 filenames = descdata + count * 3 * addr_size;
19313 while (count-- > 0)
19315 bfd_vma start, end, file_ofs;
19317 if (filenames == descend)
19319 error (_(" Malformed note - filenames end too early\n"));
19320 return false;
19323 start = byte_get (descdata, addr_size);
19324 descdata += addr_size;
19325 end = byte_get (descdata, addr_size);
19326 descdata += addr_size;
19327 file_ofs = byte_get (descdata, addr_size);
19328 descdata += addr_size;
19330 printf (" ");
19331 print_vma (start, FULL_HEX);
19332 printf (" ");
19333 print_vma (end, FULL_HEX);
19334 printf (" ");
19335 print_vma (file_ofs, FULL_HEX);
19336 printf ("\n %s\n", filenames);
19338 filenames += 1 + strlen ((char *) filenames);
19341 return true;
19344 static const char *
19345 get_gnu_elf_note_type (unsigned e_type)
19347 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
19348 switch (e_type)
19350 case NT_GNU_ABI_TAG:
19351 return _("NT_GNU_ABI_TAG (ABI version tag)");
19352 case NT_GNU_HWCAP:
19353 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19354 case NT_GNU_BUILD_ID:
19355 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19356 case NT_GNU_GOLD_VERSION:
19357 return _("NT_GNU_GOLD_VERSION (gold version)");
19358 case NT_GNU_PROPERTY_TYPE_0:
19359 return _("NT_GNU_PROPERTY_TYPE_0");
19360 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19361 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19362 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19363 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19364 default:
19366 static char buff[64];
19368 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19369 return buff;
19374 static void
19375 decode_x86_compat_isa (unsigned int bitmask)
19377 while (bitmask)
19379 unsigned int bit = bitmask & (- bitmask);
19381 bitmask &= ~ bit;
19382 switch (bit)
19384 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19385 printf ("i486");
19386 break;
19387 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19388 printf ("586");
19389 break;
19390 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19391 printf ("686");
19392 break;
19393 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19394 printf ("SSE");
19395 break;
19396 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19397 printf ("SSE2");
19398 break;
19399 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19400 printf ("SSE3");
19401 break;
19402 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19403 printf ("SSSE3");
19404 break;
19405 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19406 printf ("SSE4_1");
19407 break;
19408 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19409 printf ("SSE4_2");
19410 break;
19411 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19412 printf ("AVX");
19413 break;
19414 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19415 printf ("AVX2");
19416 break;
19417 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19418 printf ("AVX512F");
19419 break;
19420 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19421 printf ("AVX512CD");
19422 break;
19423 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19424 printf ("AVX512ER");
19425 break;
19426 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19427 printf ("AVX512PF");
19428 break;
19429 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19430 printf ("AVX512VL");
19431 break;
19432 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19433 printf ("AVX512DQ");
19434 break;
19435 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19436 printf ("AVX512BW");
19437 break;
19438 default:
19439 printf (_("<unknown: %x>"), bit);
19440 break;
19442 if (bitmask)
19443 printf (", ");
19447 static void
19448 decode_x86_compat_2_isa (unsigned int bitmask)
19450 if (!bitmask)
19452 printf (_("<None>"));
19453 return;
19456 while (bitmask)
19458 unsigned int bit = bitmask & (- bitmask);
19460 bitmask &= ~ bit;
19461 switch (bit)
19463 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19464 printf ("CMOV");
19465 break;
19466 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19467 printf ("SSE");
19468 break;
19469 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19470 printf ("SSE2");
19471 break;
19472 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19473 printf ("SSE3");
19474 break;
19475 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19476 printf ("SSSE3");
19477 break;
19478 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19479 printf ("SSE4_1");
19480 break;
19481 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19482 printf ("SSE4_2");
19483 break;
19484 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19485 printf ("AVX");
19486 break;
19487 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19488 printf ("AVX2");
19489 break;
19490 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19491 printf ("FMA");
19492 break;
19493 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19494 printf ("AVX512F");
19495 break;
19496 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19497 printf ("AVX512CD");
19498 break;
19499 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19500 printf ("AVX512ER");
19501 break;
19502 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19503 printf ("AVX512PF");
19504 break;
19505 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19506 printf ("AVX512VL");
19507 break;
19508 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19509 printf ("AVX512DQ");
19510 break;
19511 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19512 printf ("AVX512BW");
19513 break;
19514 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19515 printf ("AVX512_4FMAPS");
19516 break;
19517 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19518 printf ("AVX512_4VNNIW");
19519 break;
19520 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19521 printf ("AVX512_BITALG");
19522 break;
19523 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19524 printf ("AVX512_IFMA");
19525 break;
19526 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19527 printf ("AVX512_VBMI");
19528 break;
19529 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19530 printf ("AVX512_VBMI2");
19531 break;
19532 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19533 printf ("AVX512_VNNI");
19534 break;
19535 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19536 printf ("AVX512_BF16");
19537 break;
19538 default:
19539 printf (_("<unknown: %x>"), bit);
19540 break;
19542 if (bitmask)
19543 printf (", ");
19547 static void
19548 decode_x86_isa (unsigned int bitmask)
19550 while (bitmask)
19552 unsigned int bit = bitmask & (- bitmask);
19554 bitmask &= ~ bit;
19555 switch (bit)
19557 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19558 printf ("x86-64-baseline");
19559 break;
19560 case GNU_PROPERTY_X86_ISA_1_V2:
19561 printf ("x86-64-v2");
19562 break;
19563 case GNU_PROPERTY_X86_ISA_1_V3:
19564 printf ("x86-64-v3");
19565 break;
19566 case GNU_PROPERTY_X86_ISA_1_V4:
19567 printf ("x86-64-v4");
19568 break;
19569 default:
19570 printf (_("<unknown: %x>"), bit);
19571 break;
19573 if (bitmask)
19574 printf (", ");
19578 static void
19579 decode_x86_feature_1 (unsigned int bitmask)
19581 if (!bitmask)
19583 printf (_("<None>"));
19584 return;
19587 while (bitmask)
19589 unsigned int bit = bitmask & (- bitmask);
19591 bitmask &= ~ bit;
19592 switch (bit)
19594 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19595 printf ("IBT");
19596 break;
19597 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19598 printf ("SHSTK");
19599 break;
19600 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19601 printf ("LAM_U48");
19602 break;
19603 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19604 printf ("LAM_U57");
19605 break;
19606 default:
19607 printf (_("<unknown: %x>"), bit);
19608 break;
19610 if (bitmask)
19611 printf (", ");
19615 static void
19616 decode_x86_feature_2 (unsigned int bitmask)
19618 if (!bitmask)
19620 printf (_("<None>"));
19621 return;
19624 while (bitmask)
19626 unsigned int bit = bitmask & (- bitmask);
19628 bitmask &= ~ bit;
19629 switch (bit)
19631 case GNU_PROPERTY_X86_FEATURE_2_X86:
19632 printf ("x86");
19633 break;
19634 case GNU_PROPERTY_X86_FEATURE_2_X87:
19635 printf ("x87");
19636 break;
19637 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19638 printf ("MMX");
19639 break;
19640 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19641 printf ("XMM");
19642 break;
19643 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19644 printf ("YMM");
19645 break;
19646 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19647 printf ("ZMM");
19648 break;
19649 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19650 printf ("TMM");
19651 break;
19652 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19653 printf ("MASK");
19654 break;
19655 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19656 printf ("FXSR");
19657 break;
19658 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19659 printf ("XSAVE");
19660 break;
19661 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19662 printf ("XSAVEOPT");
19663 break;
19664 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19665 printf ("XSAVEC");
19666 break;
19667 default:
19668 printf (_("<unknown: %x>"), bit);
19669 break;
19671 if (bitmask)
19672 printf (", ");
19676 static void
19677 decode_aarch64_feature_1_and (unsigned int bitmask)
19679 while (bitmask)
19681 unsigned int bit = bitmask & (- bitmask);
19683 bitmask &= ~ bit;
19684 switch (bit)
19686 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19687 printf ("BTI");
19688 break;
19690 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19691 printf ("PAC");
19692 break;
19694 default:
19695 printf (_("<unknown: %x>"), bit);
19696 break;
19698 if (bitmask)
19699 printf (", ");
19703 static void
19704 decode_1_needed (unsigned int bitmask)
19706 while (bitmask)
19708 unsigned int bit = bitmask & (- bitmask);
19710 bitmask &= ~ bit;
19711 switch (bit)
19713 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
19714 printf ("indirect external access");
19715 break;
19716 default:
19717 printf (_("<unknown: %x>"), bit);
19718 break;
19720 if (bitmask)
19721 printf (", ");
19725 static void
19726 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19728 unsigned char * ptr = (unsigned char *) pnote->descdata;
19729 unsigned char * ptr_end = ptr + pnote->descsz;
19730 unsigned int size = is_32bit_elf ? 4 : 8;
19732 printf (_(" Properties: "));
19734 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19736 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19737 return;
19740 while (ptr < ptr_end)
19742 unsigned int j;
19743 unsigned int type;
19744 unsigned int datasz;
19746 if ((size_t) (ptr_end - ptr) < 8)
19748 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19749 break;
19752 type = byte_get (ptr, 4);
19753 datasz = byte_get (ptr + 4, 4);
19755 ptr += 8;
19757 if (datasz > (size_t) (ptr_end - ptr))
19759 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19760 type, datasz);
19761 break;
19764 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19766 if (filedata->file_header.e_machine == EM_X86_64
19767 || filedata->file_header.e_machine == EM_IAMCU
19768 || filedata->file_header.e_machine == EM_386)
19770 unsigned int bitmask;
19772 if (datasz == 4)
19773 bitmask = byte_get (ptr, 4);
19774 else
19775 bitmask = 0;
19777 switch (type)
19779 case GNU_PROPERTY_X86_ISA_1_USED:
19780 if (datasz != 4)
19781 printf (_("x86 ISA used: <corrupt length: %#x> "),
19782 datasz);
19783 else
19785 printf ("x86 ISA used: ");
19786 decode_x86_isa (bitmask);
19788 goto next;
19790 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19791 if (datasz != 4)
19792 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19793 datasz);
19794 else
19796 printf ("x86 ISA needed: ");
19797 decode_x86_isa (bitmask);
19799 goto next;
19801 case GNU_PROPERTY_X86_FEATURE_1_AND:
19802 if (datasz != 4)
19803 printf (_("x86 feature: <corrupt length: %#x> "),
19804 datasz);
19805 else
19807 printf ("x86 feature: ");
19808 decode_x86_feature_1 (bitmask);
19810 goto next;
19812 case GNU_PROPERTY_X86_FEATURE_2_USED:
19813 if (datasz != 4)
19814 printf (_("x86 feature used: <corrupt length: %#x> "),
19815 datasz);
19816 else
19818 printf ("x86 feature used: ");
19819 decode_x86_feature_2 (bitmask);
19821 goto next;
19823 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
19824 if (datasz != 4)
19825 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
19826 else
19828 printf ("x86 feature needed: ");
19829 decode_x86_feature_2 (bitmask);
19831 goto next;
19833 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
19834 if (datasz != 4)
19835 printf (_("x86 ISA used: <corrupt length: %#x> "),
19836 datasz);
19837 else
19839 printf ("x86 ISA used: ");
19840 decode_x86_compat_isa (bitmask);
19842 goto next;
19844 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
19845 if (datasz != 4)
19846 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19847 datasz);
19848 else
19850 printf ("x86 ISA needed: ");
19851 decode_x86_compat_isa (bitmask);
19853 goto next;
19855 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
19856 if (datasz != 4)
19857 printf (_("x86 ISA used: <corrupt length: %#x> "),
19858 datasz);
19859 else
19861 printf ("x86 ISA used: ");
19862 decode_x86_compat_2_isa (bitmask);
19864 goto next;
19866 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
19867 if (datasz != 4)
19868 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19869 datasz);
19870 else
19872 printf ("x86 ISA needed: ");
19873 decode_x86_compat_2_isa (bitmask);
19875 goto next;
19877 default:
19878 break;
19881 else if (filedata->file_header.e_machine == EM_AARCH64)
19883 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
19885 printf ("AArch64 feature: ");
19886 if (datasz != 4)
19887 printf (_("<corrupt length: %#x> "), datasz);
19888 else
19889 decode_aarch64_feature_1_and (byte_get (ptr, 4));
19890 goto next;
19894 else
19896 switch (type)
19898 case GNU_PROPERTY_STACK_SIZE:
19899 printf (_("stack size: "));
19900 if (datasz != size)
19901 printf (_("<corrupt length: %#x> "), datasz);
19902 else
19903 printf ("%#lx", (unsigned long) byte_get (ptr, size));
19904 goto next;
19906 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
19907 printf ("no copy on protected ");
19908 if (datasz)
19909 printf (_("<corrupt length: %#x> "), datasz);
19910 goto next;
19912 default:
19913 if ((type >= GNU_PROPERTY_UINT32_AND_LO
19914 && type <= GNU_PROPERTY_UINT32_AND_HI)
19915 || (type >= GNU_PROPERTY_UINT32_OR_LO
19916 && type <= GNU_PROPERTY_UINT32_OR_HI))
19918 switch (type)
19920 case GNU_PROPERTY_1_NEEDED:
19921 if (datasz != 4)
19922 printf (_("1_needed: <corrupt length: %#x> "),
19923 datasz);
19924 else
19926 unsigned int bitmask = byte_get (ptr, 4);
19927 printf ("1_needed: ");
19928 decode_1_needed (bitmask);
19930 goto next;
19932 default:
19933 break;
19935 if (type <= GNU_PROPERTY_UINT32_AND_HI)
19936 printf (_("UINT32_AND (%#x): "), type);
19937 else
19938 printf (_("UINT32_OR (%#x): "), type);
19939 if (datasz != 4)
19940 printf (_("<corrupt length: %#x> "), datasz);
19941 else
19942 printf ("%#x", (unsigned int) byte_get (ptr, 4));
19943 goto next;
19945 break;
19949 if (type < GNU_PROPERTY_LOPROC)
19950 printf (_("<unknown type %#x data: "), type);
19951 else if (type < GNU_PROPERTY_LOUSER)
19952 printf (_("<processor-specific type %#x data: "), type);
19953 else
19954 printf (_("<application-specific type %#x data: "), type);
19955 for (j = 0; j < datasz; ++j)
19956 printf ("%02x ", ptr[j] & 0xff);
19957 printf (">");
19959 next:
19960 ptr += ((datasz + (size - 1)) & ~ (size - 1));
19961 if (ptr == ptr_end)
19962 break;
19964 if (do_wide)
19965 printf (", ");
19966 else
19967 printf ("\n\t");
19970 printf ("\n");
19973 static bool
19974 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
19976 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
19977 switch (pnote->type)
19979 case NT_GNU_BUILD_ID:
19981 unsigned long i;
19983 printf (_(" Build ID: "));
19984 for (i = 0; i < pnote->descsz; ++i)
19985 printf ("%02x", pnote->descdata[i] & 0xff);
19986 printf ("\n");
19988 break;
19990 case NT_GNU_ABI_TAG:
19992 unsigned long os, major, minor, subminor;
19993 const char *osname;
19995 /* PR 17531: file: 030-599401-0.004. */
19996 if (pnote->descsz < 16)
19998 printf (_(" <corrupt GNU_ABI_TAG>\n"));
19999 break;
20002 os = byte_get ((unsigned char *) pnote->descdata, 4);
20003 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20004 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20005 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20007 switch (os)
20009 case GNU_ABI_TAG_LINUX:
20010 osname = "Linux";
20011 break;
20012 case GNU_ABI_TAG_HURD:
20013 osname = "Hurd";
20014 break;
20015 case GNU_ABI_TAG_SOLARIS:
20016 osname = "Solaris";
20017 break;
20018 case GNU_ABI_TAG_FREEBSD:
20019 osname = "FreeBSD";
20020 break;
20021 case GNU_ABI_TAG_NETBSD:
20022 osname = "NetBSD";
20023 break;
20024 case GNU_ABI_TAG_SYLLABLE:
20025 osname = "Syllable";
20026 break;
20027 case GNU_ABI_TAG_NACL:
20028 osname = "NaCl";
20029 break;
20030 default:
20031 osname = "Unknown";
20032 break;
20035 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
20036 major, minor, subminor);
20038 break;
20040 case NT_GNU_GOLD_VERSION:
20042 unsigned long i;
20044 printf (_(" Version: "));
20045 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
20046 printf ("%c", pnote->descdata[i]);
20047 printf ("\n");
20049 break;
20051 case NT_GNU_HWCAP:
20053 unsigned long num_entries, mask;
20055 /* Hardware capabilities information. Word 0 is the number of entries.
20056 Word 1 is a bitmask of enabled entries. The rest of the descriptor
20057 is a series of entries, where each entry is a single byte followed
20058 by a nul terminated string. The byte gives the bit number to test
20059 if enabled in the bitmask. */
20060 printf (_(" Hardware Capabilities: "));
20061 if (pnote->descsz < 8)
20063 error (_("<corrupt GNU_HWCAP>\n"));
20064 return false;
20066 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
20067 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20068 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
20069 /* FIXME: Add code to display the entries... */
20071 break;
20073 case NT_GNU_PROPERTY_TYPE_0:
20074 print_gnu_property_note (filedata, pnote);
20075 break;
20077 default:
20078 /* Handle unrecognised types. An error message should have already been
20079 created by get_gnu_elf_note_type(), so all that we need to do is to
20080 display the data. */
20082 unsigned long i;
20084 printf (_(" Description data: "));
20085 for (i = 0; i < pnote->descsz; ++i)
20086 printf ("%02x ", pnote->descdata[i] & 0xff);
20087 printf ("\n");
20089 break;
20092 return true;
20095 static const char *
20096 get_v850_elf_note_type (enum v850_notes n_type)
20098 static char buff[64];
20100 switch (n_type)
20102 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
20103 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
20104 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
20105 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
20106 case V850_NOTE_CACHE_INFO: return _("Use of cache");
20107 case V850_NOTE_MMU_INFO: return _("Use of MMU");
20108 default:
20109 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
20110 return buff;
20114 static bool
20115 print_v850_note (Elf_Internal_Note * pnote)
20117 unsigned int val;
20119 if (pnote->descsz != 4)
20120 return false;
20122 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
20124 if (val == 0)
20126 printf (_("not set\n"));
20127 return true;
20130 switch (pnote->type)
20132 case V850_NOTE_ALIGNMENT:
20133 switch (val)
20135 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
20136 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
20138 break;
20140 case V850_NOTE_DATA_SIZE:
20141 switch (val)
20143 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
20144 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
20146 break;
20148 case V850_NOTE_FPU_INFO:
20149 switch (val)
20151 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
20152 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
20154 break;
20156 case V850_NOTE_MMU_INFO:
20157 case V850_NOTE_CACHE_INFO:
20158 case V850_NOTE_SIMD_INFO:
20159 if (val == EF_RH850_SIMD)
20161 printf (_("yes\n"));
20162 return true;
20164 break;
20166 default:
20167 /* An 'unknown note type' message will already have been displayed. */
20168 break;
20171 printf (_("unknown value: %x\n"), val);
20172 return false;
20175 static bool
20176 process_netbsd_elf_note (Elf_Internal_Note * pnote)
20178 unsigned int version;
20180 switch (pnote->type)
20182 case NT_NETBSD_IDENT:
20183 if (pnote->descsz < 1)
20184 break;
20185 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20186 if ((version / 10000) % 100)
20187 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
20188 version, version / 100000000, (version / 1000000) % 100,
20189 (version / 10000) % 100 > 26 ? "Z" : "",
20190 'A' + (version / 10000) % 26);
20191 else
20192 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
20193 version, version / 100000000, (version / 1000000) % 100,
20194 (version / 100) % 100);
20195 return true;
20197 case NT_NETBSD_MARCH:
20198 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
20199 pnote->descdata);
20200 return true;
20202 case NT_NETBSD_PAX:
20203 if (pnote->descsz < 1)
20204 break;
20205 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20206 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
20207 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
20208 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
20209 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
20210 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
20211 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
20212 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
20213 return true;
20216 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
20217 pnote->descsz, pnote->type);
20218 return false;
20221 static const char *
20222 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20224 switch (e_type)
20226 case NT_FREEBSD_THRMISC:
20227 return _("NT_THRMISC (thrmisc structure)");
20228 case NT_FREEBSD_PROCSTAT_PROC:
20229 return _("NT_PROCSTAT_PROC (proc data)");
20230 case NT_FREEBSD_PROCSTAT_FILES:
20231 return _("NT_PROCSTAT_FILES (files data)");
20232 case NT_FREEBSD_PROCSTAT_VMMAP:
20233 return _("NT_PROCSTAT_VMMAP (vmmap data)");
20234 case NT_FREEBSD_PROCSTAT_GROUPS:
20235 return _("NT_PROCSTAT_GROUPS (groups data)");
20236 case NT_FREEBSD_PROCSTAT_UMASK:
20237 return _("NT_PROCSTAT_UMASK (umask data)");
20238 case NT_FREEBSD_PROCSTAT_RLIMIT:
20239 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
20240 case NT_FREEBSD_PROCSTAT_OSREL:
20241 return _("NT_PROCSTAT_OSREL (osreldate data)");
20242 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
20243 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
20244 case NT_FREEBSD_PROCSTAT_AUXV:
20245 return _("NT_PROCSTAT_AUXV (auxv data)");
20246 case NT_FREEBSD_PTLWPINFO:
20247 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
20249 return get_note_type (filedata, e_type);
20252 static const char *
20253 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20255 static char buff[64];
20257 switch (e_type)
20259 case NT_NETBSDCORE_PROCINFO:
20260 /* NetBSD core "procinfo" structure. */
20261 return _("NetBSD procinfo structure");
20263 case NT_NETBSDCORE_AUXV:
20264 return _("NetBSD ELF auxiliary vector data");
20266 case NT_NETBSDCORE_LWPSTATUS:
20267 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
20269 default:
20270 /* As of Jan 2020 there are no other machine-independent notes
20271 defined for NetBSD core files. If the note type is less
20272 than the start of the machine-dependent note types, we don't
20273 understand it. */
20275 if (e_type < NT_NETBSDCORE_FIRSTMACH)
20277 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20278 return buff;
20280 break;
20283 switch (filedata->file_header.e_machine)
20285 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
20286 and PT_GETFPREGS == mach+2. */
20288 case EM_OLD_ALPHA:
20289 case EM_ALPHA:
20290 case EM_SPARC:
20291 case EM_SPARC32PLUS:
20292 case EM_SPARCV9:
20293 switch (e_type)
20295 case NT_NETBSDCORE_FIRSTMACH + 0:
20296 return _("PT_GETREGS (reg structure)");
20297 case NT_NETBSDCORE_FIRSTMACH + 2:
20298 return _("PT_GETFPREGS (fpreg structure)");
20299 default:
20300 break;
20302 break;
20304 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
20305 There's also old PT___GETREGS40 == mach + 1 for old reg
20306 structure which lacks GBR. */
20307 case EM_SH:
20308 switch (e_type)
20310 case NT_NETBSDCORE_FIRSTMACH + 1:
20311 return _("PT___GETREGS40 (old reg structure)");
20312 case NT_NETBSDCORE_FIRSTMACH + 3:
20313 return _("PT_GETREGS (reg structure)");
20314 case NT_NETBSDCORE_FIRSTMACH + 5:
20315 return _("PT_GETFPREGS (fpreg structure)");
20316 default:
20317 break;
20319 break;
20321 /* On all other arch's, PT_GETREGS == mach+1 and
20322 PT_GETFPREGS == mach+3. */
20323 default:
20324 switch (e_type)
20326 case NT_NETBSDCORE_FIRSTMACH + 1:
20327 return _("PT_GETREGS (reg structure)");
20328 case NT_NETBSDCORE_FIRSTMACH + 3:
20329 return _("PT_GETFPREGS (fpreg structure)");
20330 default:
20331 break;
20335 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20336 e_type - NT_NETBSDCORE_FIRSTMACH);
20337 return buff;
20340 static const char *
20341 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20343 switch (e_type)
20345 case NT_OPENBSD_PROCINFO:
20346 return _("OpenBSD procinfo structure");
20347 case NT_OPENBSD_AUXV:
20348 return _("OpenBSD ELF auxiliary vector data");
20349 case NT_OPENBSD_REGS:
20350 return _("OpenBSD regular registers");
20351 case NT_OPENBSD_FPREGS:
20352 return _("OpenBSD floating point registers");
20353 case NT_OPENBSD_WCOOKIE:
20354 return _("OpenBSD window cookie");
20357 return get_note_type (filedata, e_type);
20360 static const char *
20361 get_stapsdt_note_type (unsigned e_type)
20363 static char buff[64];
20365 switch (e_type)
20367 case NT_STAPSDT:
20368 return _("NT_STAPSDT (SystemTap probe descriptors)");
20370 default:
20371 break;
20374 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20375 return buff;
20378 static bool
20379 print_stapsdt_note (Elf_Internal_Note *pnote)
20381 size_t len, maxlen;
20382 unsigned long addr_size = is_32bit_elf ? 4 : 8;
20383 char *data = pnote->descdata;
20384 char *data_end = pnote->descdata + pnote->descsz;
20385 bfd_vma pc, base_addr, semaphore;
20386 char *provider, *probe, *arg_fmt;
20388 if (pnote->descsz < (addr_size * 3))
20389 goto stapdt_note_too_small;
20391 pc = byte_get ((unsigned char *) data, addr_size);
20392 data += addr_size;
20394 base_addr = byte_get ((unsigned char *) data, addr_size);
20395 data += addr_size;
20397 semaphore = byte_get ((unsigned char *) data, addr_size);
20398 data += addr_size;
20400 if (data >= data_end)
20401 goto stapdt_note_too_small;
20402 maxlen = data_end - data;
20403 len = strnlen (data, maxlen);
20404 if (len < maxlen)
20406 provider = data;
20407 data += len + 1;
20409 else
20410 goto stapdt_note_too_small;
20412 if (data >= data_end)
20413 goto stapdt_note_too_small;
20414 maxlen = data_end - data;
20415 len = strnlen (data, maxlen);
20416 if (len < maxlen)
20418 probe = data;
20419 data += len + 1;
20421 else
20422 goto stapdt_note_too_small;
20424 if (data >= data_end)
20425 goto stapdt_note_too_small;
20426 maxlen = data_end - data;
20427 len = strnlen (data, maxlen);
20428 if (len < maxlen)
20430 arg_fmt = data;
20431 data += len + 1;
20433 else
20434 goto stapdt_note_too_small;
20436 printf (_(" Provider: %s\n"), provider);
20437 printf (_(" Name: %s\n"), probe);
20438 printf (_(" Location: "));
20439 print_vma (pc, FULL_HEX);
20440 printf (_(", Base: "));
20441 print_vma (base_addr, FULL_HEX);
20442 printf (_(", Semaphore: "));
20443 print_vma (semaphore, FULL_HEX);
20444 printf ("\n");
20445 printf (_(" Arguments: %s\n"), arg_fmt);
20447 return data == data_end;
20449 stapdt_note_too_small:
20450 printf (_(" <corrupt - note is too small>\n"));
20451 error (_("corrupt stapdt note - the data size is too small\n"));
20452 return false;
20455 static bool
20456 print_fdo_note (Elf_Internal_Note * pnote)
20458 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
20460 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
20461 return true;
20463 return false;
20466 static const char *
20467 get_ia64_vms_note_type (unsigned e_type)
20469 static char buff[64];
20471 switch (e_type)
20473 case NT_VMS_MHD:
20474 return _("NT_VMS_MHD (module header)");
20475 case NT_VMS_LNM:
20476 return _("NT_VMS_LNM (language name)");
20477 case NT_VMS_SRC:
20478 return _("NT_VMS_SRC (source files)");
20479 case NT_VMS_TITLE:
20480 return "NT_VMS_TITLE";
20481 case NT_VMS_EIDC:
20482 return _("NT_VMS_EIDC (consistency check)");
20483 case NT_VMS_FPMODE:
20484 return _("NT_VMS_FPMODE (FP mode)");
20485 case NT_VMS_LINKTIME:
20486 return "NT_VMS_LINKTIME";
20487 case NT_VMS_IMGNAM:
20488 return _("NT_VMS_IMGNAM (image name)");
20489 case NT_VMS_IMGID:
20490 return _("NT_VMS_IMGID (image id)");
20491 case NT_VMS_LINKID:
20492 return _("NT_VMS_LINKID (link id)");
20493 case NT_VMS_IMGBID:
20494 return _("NT_VMS_IMGBID (build id)");
20495 case NT_VMS_GSTNAM:
20496 return _("NT_VMS_GSTNAM (sym table name)");
20497 case NT_VMS_ORIG_DYN:
20498 return "NT_VMS_ORIG_DYN";
20499 case NT_VMS_PATCHTIME:
20500 return "NT_VMS_PATCHTIME";
20501 default:
20502 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20503 return buff;
20507 static bool
20508 print_ia64_vms_note (Elf_Internal_Note * pnote)
20510 int maxlen = pnote->descsz;
20512 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20513 goto desc_size_fail;
20515 switch (pnote->type)
20517 case NT_VMS_MHD:
20518 if (maxlen <= 36)
20519 goto desc_size_fail;
20521 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20523 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20524 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20525 if (l + 34 < maxlen)
20527 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20528 if (l + 35 < maxlen)
20529 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20530 else
20531 printf (_(" Module version : <missing>\n"));
20533 else
20535 printf (_(" Module name : <missing>\n"));
20536 printf (_(" Module version : <missing>\n"));
20538 break;
20540 case NT_VMS_LNM:
20541 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20542 break;
20544 #ifdef BFD64
20545 case NT_VMS_FPMODE:
20546 printf (_(" Floating Point mode: "));
20547 if (maxlen < 8)
20548 goto desc_size_fail;
20549 /* FIXME: Generate an error if descsz > 8 ? */
20551 printf ("0x%016" BFD_VMA_FMT "x\n",
20552 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
20553 break;
20555 case NT_VMS_LINKTIME:
20556 printf (_(" Link time: "));
20557 if (maxlen < 8)
20558 goto desc_size_fail;
20559 /* FIXME: Generate an error if descsz > 8 ? */
20561 print_vms_time
20562 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20563 printf ("\n");
20564 break;
20566 case NT_VMS_PATCHTIME:
20567 printf (_(" Patch time: "));
20568 if (maxlen < 8)
20569 goto desc_size_fail;
20570 /* FIXME: Generate an error if descsz > 8 ? */
20572 print_vms_time
20573 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20574 printf ("\n");
20575 break;
20577 case NT_VMS_ORIG_DYN:
20578 if (maxlen < 34)
20579 goto desc_size_fail;
20581 printf (_(" Major id: %u, minor id: %u\n"),
20582 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
20583 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
20584 printf (_(" Last modified : "));
20585 print_vms_time
20586 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
20587 printf (_("\n Link flags : "));
20588 printf ("0x%016" BFD_VMA_FMT "x\n",
20589 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
20590 printf (_(" Header flags: 0x%08x\n"),
20591 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
20592 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20593 break;
20594 #endif
20596 case NT_VMS_IMGNAM:
20597 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20598 break;
20600 case NT_VMS_GSTNAM:
20601 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20602 break;
20604 case NT_VMS_IMGID:
20605 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20606 break;
20608 case NT_VMS_LINKID:
20609 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20610 break;
20612 default:
20613 return false;
20616 return true;
20618 desc_size_fail:
20619 printf (_(" <corrupt - data size is too small>\n"));
20620 error (_("corrupt IA64 note: data size is too small\n"));
20621 return false;
20624 struct build_attr_cache {
20625 Filedata *filedata;
20626 char *strtab;
20627 unsigned long strtablen;
20628 Elf_Internal_Sym *symtab;
20629 unsigned long nsyms;
20630 } ba_cache;
20632 /* Find the symbol associated with a build attribute that is attached
20633 to address OFFSET. If PNAME is non-NULL then store the name of
20634 the symbol (if found) in the provided pointer, Returns NULL if a
20635 symbol could not be found. */
20637 static Elf_Internal_Sym *
20638 get_symbol_for_build_attribute (Filedata *filedata,
20639 unsigned long offset,
20640 bool is_open_attr,
20641 const char **pname)
20643 Elf_Internal_Sym *saved_sym = NULL;
20644 Elf_Internal_Sym *sym;
20646 if (filedata->section_headers != NULL
20647 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20649 Elf_Internal_Shdr * symsec;
20651 free (ba_cache.strtab);
20652 ba_cache.strtab = NULL;
20653 free (ba_cache.symtab);
20654 ba_cache.symtab = NULL;
20656 /* Load the symbol and string sections. */
20657 for (symsec = filedata->section_headers;
20658 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20659 symsec ++)
20661 if (symsec->sh_type == SHT_SYMTAB
20662 && get_symtab (filedata, symsec,
20663 &ba_cache.symtab, &ba_cache.nsyms,
20664 &ba_cache.strtab, &ba_cache.strtablen))
20665 break;
20667 ba_cache.filedata = filedata;
20670 if (ba_cache.symtab == NULL)
20671 return NULL;
20673 /* Find a symbol whose value matches offset. */
20674 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20675 if (sym->st_value == offset)
20677 if (sym->st_name >= ba_cache.strtablen)
20678 /* Huh ? This should not happen. */
20679 continue;
20681 if (ba_cache.strtab[sym->st_name] == 0)
20682 continue;
20684 /* The AArch64, ARM and RISC-V architectures define mapping symbols
20685 (eg $d, $x, $t) which we want to ignore. */
20686 if (ba_cache.strtab[sym->st_name] == '$'
20687 && ba_cache.strtab[sym->st_name + 1] != 0
20688 && ba_cache.strtab[sym->st_name + 2] == 0)
20689 continue;
20691 if (is_open_attr)
20693 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20694 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20695 FUNC symbols entirely. */
20696 switch (ELF_ST_TYPE (sym->st_info))
20698 case STT_OBJECT:
20699 case STT_FILE:
20700 saved_sym = sym;
20701 if (sym->st_size)
20703 /* If the symbol has a size associated
20704 with it then we can stop searching. */
20705 sym = ba_cache.symtab + ba_cache.nsyms;
20707 continue;
20709 case STT_FUNC:
20710 /* Ignore function symbols. */
20711 continue;
20713 default:
20714 break;
20717 switch (ELF_ST_BIND (sym->st_info))
20719 case STB_GLOBAL:
20720 if (saved_sym == NULL
20721 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20722 saved_sym = sym;
20723 break;
20725 case STB_LOCAL:
20726 if (saved_sym == NULL)
20727 saved_sym = sym;
20728 break;
20730 default:
20731 break;
20734 else
20736 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20737 continue;
20739 saved_sym = sym;
20740 break;
20744 if (saved_sym && pname)
20745 * pname = ba_cache.strtab + saved_sym->st_name;
20747 return saved_sym;
20750 /* Returns true iff addr1 and addr2 are in the same section. */
20752 static bool
20753 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20755 Elf_Internal_Shdr * a1;
20756 Elf_Internal_Shdr * a2;
20758 a1 = find_section_by_address (filedata, addr1);
20759 a2 = find_section_by_address (filedata, addr2);
20761 return a1 == a2 && a1 != NULL;
20764 static bool
20765 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20766 Filedata * filedata)
20768 static unsigned long global_offset = 0;
20769 static unsigned long global_end = 0;
20770 static unsigned long func_offset = 0;
20771 static unsigned long func_end = 0;
20773 Elf_Internal_Sym *sym;
20774 const char *name;
20775 unsigned long start;
20776 unsigned long end;
20777 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20779 switch (pnote->descsz)
20781 case 0:
20782 /* A zero-length description means that the range of
20783 the previous note of the same type should be used. */
20784 if (is_open_attr)
20786 if (global_end > global_offset)
20787 printf (_(" Applies to region from %#lx to %#lx\n"),
20788 global_offset, global_end);
20789 else
20790 printf (_(" Applies to region from %#lx\n"), global_offset);
20792 else
20794 if (func_end > func_offset)
20795 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20796 else
20797 printf (_(" Applies to region from %#lx\n"), func_offset);
20799 return true;
20801 case 4:
20802 start = byte_get ((unsigned char *) pnote->descdata, 4);
20803 end = 0;
20804 break;
20806 case 8:
20807 start = byte_get ((unsigned char *) pnote->descdata, 4);
20808 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20809 break;
20811 case 16:
20812 start = byte_get ((unsigned char *) pnote->descdata, 8);
20813 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
20814 break;
20816 default:
20817 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
20818 printf (_(" <invalid descsz>"));
20819 return false;
20822 name = NULL;
20823 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
20824 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
20825 in order to avoid them being confused with the start address of the
20826 first function in the file... */
20827 if (sym == NULL && is_open_attr)
20828 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
20829 & name);
20831 if (end == 0 && sym != NULL && sym->st_size > 0)
20832 end = start + sym->st_size;
20834 if (is_open_attr)
20836 /* FIXME: Need to properly allow for section alignment.
20837 16 is just the alignment used on x86_64. */
20838 if (global_end > 0
20839 && start > BFD_ALIGN (global_end, 16)
20840 /* Build notes are not guaranteed to be organised in order of
20841 increasing address, but we should find the all of the notes
20842 for one section in the same place. */
20843 && same_section (filedata, start, global_end))
20844 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
20845 global_end + 1, start - 1);
20847 printf (_(" Applies to region from %#lx"), start);
20848 global_offset = start;
20850 if (end)
20852 printf (_(" to %#lx"), end);
20853 global_end = end;
20856 else
20858 printf (_(" Applies to region from %#lx"), start);
20859 func_offset = start;
20861 if (end)
20863 printf (_(" to %#lx"), end);
20864 func_end = end;
20868 if (sym && name)
20869 printf (_(" (%s)"), name);
20871 printf ("\n");
20872 return true;
20875 static bool
20876 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
20878 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
20879 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
20880 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
20881 char name_type;
20882 char name_attribute;
20883 const char * expected_types;
20884 const char * name = pnote->namedata;
20885 const char * text;
20886 signed int left;
20888 if (name == NULL || pnote->namesz < 2)
20890 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20891 print_symbol (-20, _(" <corrupt name>"));
20892 return false;
20895 if (do_wide)
20896 left = 28;
20897 else
20898 left = 20;
20900 /* Version 2 of the spec adds a "GA" prefix to the name field. */
20901 if (name[0] == 'G' && name[1] == 'A')
20903 if (pnote->namesz < 4)
20905 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20906 print_symbol (-20, _(" <corrupt name>"));
20907 return false;
20910 printf ("GA");
20911 name += 2;
20912 left -= 2;
20915 switch ((name_type = * name))
20917 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20918 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20919 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20920 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20921 printf ("%c", * name);
20922 left --;
20923 break;
20924 default:
20925 error (_("unrecognised attribute type in name field: %d\n"), name_type);
20926 print_symbol (-20, _("<unknown name type>"));
20927 return false;
20930 ++ name;
20931 text = NULL;
20933 switch ((name_attribute = * name))
20935 case GNU_BUILD_ATTRIBUTE_VERSION:
20936 text = _("<version>");
20937 expected_types = string_expected;
20938 ++ name;
20939 break;
20940 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20941 text = _("<stack prot>");
20942 expected_types = "!+*";
20943 ++ name;
20944 break;
20945 case GNU_BUILD_ATTRIBUTE_RELRO:
20946 text = _("<relro>");
20947 expected_types = bool_expected;
20948 ++ name;
20949 break;
20950 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
20951 text = _("<stack size>");
20952 expected_types = number_expected;
20953 ++ name;
20954 break;
20955 case GNU_BUILD_ATTRIBUTE_TOOL:
20956 text = _("<tool>");
20957 expected_types = string_expected;
20958 ++ name;
20959 break;
20960 case GNU_BUILD_ATTRIBUTE_ABI:
20961 text = _("<ABI>");
20962 expected_types = "$*";
20963 ++ name;
20964 break;
20965 case GNU_BUILD_ATTRIBUTE_PIC:
20966 text = _("<PIC>");
20967 expected_types = number_expected;
20968 ++ name;
20969 break;
20970 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
20971 text = _("<short enum>");
20972 expected_types = bool_expected;
20973 ++ name;
20974 break;
20975 default:
20976 if (ISPRINT (* name))
20978 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
20980 if (len > left && ! do_wide)
20981 len = left;
20982 printf ("%.*s:", len, name);
20983 left -= len;
20984 name += len;
20986 else
20988 static char tmpbuf [128];
20990 error (_("unrecognised byte in name field: %d\n"), * name);
20991 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
20992 text = tmpbuf;
20993 name ++;
20995 expected_types = "*$!+";
20996 break;
20999 if (text)
21000 left -= printf ("%s", text);
21002 if (strchr (expected_types, name_type) == NULL)
21003 warn (_("attribute does not have an expected type (%c)\n"), name_type);
21005 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
21007 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
21008 (unsigned long) pnote->namesz,
21009 (long) (name - pnote->namedata));
21010 return false;
21013 if (left < 1 && ! do_wide)
21014 return true;
21016 switch (name_type)
21018 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21020 unsigned int bytes;
21021 unsigned long long val = 0;
21022 unsigned int shift = 0;
21023 char * decoded = NULL;
21025 bytes = pnote->namesz - (name - pnote->namedata);
21026 if (bytes > 0)
21027 /* The -1 is because the name field is always 0 terminated, and we
21028 want to be able to ensure that the shift in the while loop below
21029 will not overflow. */
21030 -- bytes;
21032 if (bytes > sizeof (val))
21034 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
21035 bytes);
21036 bytes = sizeof (val);
21038 /* We do not bother to warn if bytes == 0 as this can
21039 happen with some early versions of the gcc plugin. */
21041 while (bytes --)
21043 unsigned long long byte = *name++ & 0xff;
21045 val |= byte << shift;
21046 shift += 8;
21049 switch (name_attribute)
21051 case GNU_BUILD_ATTRIBUTE_PIC:
21052 switch (val)
21054 case 0: decoded = "static"; break;
21055 case 1: decoded = "pic"; break;
21056 case 2: decoded = "PIC"; break;
21057 case 3: decoded = "pie"; break;
21058 case 4: decoded = "PIE"; break;
21059 default: break;
21061 break;
21062 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21063 switch (val)
21065 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
21066 case 0: decoded = "off"; break;
21067 case 1: decoded = "on"; break;
21068 case 2: decoded = "all"; break;
21069 case 3: decoded = "strong"; break;
21070 case 4: decoded = "explicit"; break;
21071 default: break;
21073 break;
21074 default:
21075 break;
21078 if (decoded != NULL)
21080 print_symbol (-left, decoded);
21081 left = 0;
21083 else if (val == 0)
21085 printf ("0x0");
21086 left -= 3;
21088 else
21090 if (do_wide)
21091 left -= printf ("0x%llx", val);
21092 else
21093 left -= printf ("0x%-.*llx", left, val);
21096 break;
21097 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21098 left -= print_symbol (- left, name);
21099 break;
21100 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21101 left -= print_symbol (- left, "true");
21102 break;
21103 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21104 left -= print_symbol (- left, "false");
21105 break;
21108 if (do_wide && left > 0)
21109 printf ("%-*s", left, " ");
21111 return true;
21114 /* Note that by the ELF standard, the name field is already null byte
21115 terminated, and namesz includes the terminating null byte.
21116 I.E. the value of namesz for the name "FSF" is 4.
21118 If the value of namesz is zero, there is no name present. */
21120 static bool
21121 process_note (Elf_Internal_Note * pnote,
21122 Filedata * filedata)
21124 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
21125 const char * nt;
21127 if (pnote->namesz == 0)
21128 /* If there is no note name, then use the default set of
21129 note type strings. */
21130 nt = get_note_type (filedata, pnote->type);
21132 else if (startswith (pnote->namedata, "GNU"))
21133 /* GNU-specific object file notes. */
21134 nt = get_gnu_elf_note_type (pnote->type);
21136 else if (startswith (pnote->namedata, "FreeBSD"))
21137 /* FreeBSD-specific core file notes. */
21138 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
21140 else if (startswith (pnote->namedata, "NetBSD-CORE"))
21141 /* NetBSD-specific core file notes. */
21142 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
21144 else if (startswith (pnote->namedata, "NetBSD"))
21145 /* NetBSD-specific core file notes. */
21146 return process_netbsd_elf_note (pnote);
21148 else if (startswith (pnote->namedata, "PaX"))
21149 /* NetBSD-specific core file notes. */
21150 return process_netbsd_elf_note (pnote);
21152 else if (startswith (pnote->namedata, "OpenBSD"))
21153 /* OpenBSD-specific core file notes. */
21154 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
21156 else if (startswith (pnote->namedata, "SPU/"))
21158 /* SPU-specific core file notes. */
21159 nt = pnote->namedata + 4;
21160 name = "SPU";
21163 else if (startswith (pnote->namedata, "IPF/VMS"))
21164 /* VMS/ia64-specific file notes. */
21165 nt = get_ia64_vms_note_type (pnote->type);
21167 else if (startswith (pnote->namedata, "stapsdt"))
21168 nt = get_stapsdt_note_type (pnote->type);
21170 else
21171 /* Don't recognize this note name; just use the default set of
21172 note type strings. */
21173 nt = get_note_type (filedata, pnote->type);
21175 printf (" ");
21177 if (((startswith (pnote->namedata, "GA")
21178 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21179 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21180 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21181 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21182 print_gnu_build_attribute_name (pnote);
21183 else
21184 print_symbol (-20, name);
21186 if (do_wide)
21187 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
21188 else
21189 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
21191 if (startswith (pnote->namedata, "IPF/VMS"))
21192 return print_ia64_vms_note (pnote);
21193 else if (startswith (pnote->namedata, "GNU"))
21194 return print_gnu_note (filedata, pnote);
21195 else if (startswith (pnote->namedata, "stapsdt"))
21196 return print_stapsdt_note (pnote);
21197 else if (startswith (pnote->namedata, "CORE"))
21198 return print_core_note (pnote);
21199 else if (startswith (pnote->namedata, "FDO"))
21200 return print_fdo_note (pnote);
21201 else if (((startswith (pnote->namedata, "GA")
21202 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21203 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21204 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21205 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21206 return print_gnu_build_attribute_description (pnote, filedata);
21208 if (pnote->descsz)
21210 unsigned long i;
21212 printf (_(" description data: "));
21213 for (i = 0; i < pnote->descsz; i++)
21214 printf ("%02x ", pnote->descdata[i] & 0xff);
21215 if (!do_wide)
21216 printf ("\n");
21219 if (do_wide)
21220 printf ("\n");
21222 return true;
21225 static bool
21226 process_notes_at (Filedata * filedata,
21227 Elf_Internal_Shdr * section,
21228 bfd_vma offset,
21229 bfd_vma length,
21230 bfd_vma align)
21232 Elf_External_Note *pnotes;
21233 Elf_External_Note *external;
21234 char *end;
21235 bool res = true;
21237 if (length <= 0)
21238 return false;
21240 if (section)
21242 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
21243 if (pnotes)
21245 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
21247 free (pnotes);
21248 return false;
21252 else
21253 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21254 _("notes"));
21256 if (pnotes == NULL)
21257 return false;
21259 external = pnotes;
21261 if (filedata->is_separate)
21262 printf (_("In linked file '%s': "), filedata->file_name);
21263 else
21264 printf ("\n");
21265 if (section)
21266 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
21267 else
21268 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
21269 (unsigned long) offset, (unsigned long) length);
21271 /* NB: Some note sections may have alignment value of 0 or 1. gABI
21272 specifies that notes should be aligned to 4 bytes in 32-bit
21273 objects and to 8 bytes in 64-bit objects. As a Linux extension,
21274 we also support 4 byte alignment in 64-bit objects. If section
21275 alignment is less than 4, we treate alignment as 4 bytes. */
21276 if (align < 4)
21277 align = 4;
21278 else if (align != 4 && align != 8)
21280 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
21281 (long) align);
21282 free (pnotes);
21283 return false;
21286 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
21288 end = (char *) pnotes + length;
21289 while ((char *) external < end)
21291 Elf_Internal_Note inote;
21292 size_t min_notesz;
21293 char * next;
21294 char * temp = NULL;
21295 size_t data_remaining = end - (char *) external;
21297 if (!is_ia64_vms (filedata))
21299 /* PR binutils/15191
21300 Make sure that there is enough data to read. */
21301 min_notesz = offsetof (Elf_External_Note, name);
21302 if (data_remaining < min_notesz)
21304 warn (ngettext ("Corrupt note: only %ld byte remains, "
21305 "not enough for a full note\n",
21306 "Corrupt note: only %ld bytes remain, "
21307 "not enough for a full note\n",
21308 data_remaining),
21309 (long) data_remaining);
21310 break;
21312 data_remaining -= min_notesz;
21314 inote.type = BYTE_GET (external->type);
21315 inote.namesz = BYTE_GET (external->namesz);
21316 inote.namedata = external->name;
21317 inote.descsz = BYTE_GET (external->descsz);
21318 inote.descdata = ((char *) external
21319 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
21320 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21321 next = ((char *) external
21322 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
21324 else
21326 Elf64_External_VMS_Note *vms_external;
21328 /* PR binutils/15191
21329 Make sure that there is enough data to read. */
21330 min_notesz = offsetof (Elf64_External_VMS_Note, name);
21331 if (data_remaining < min_notesz)
21333 warn (ngettext ("Corrupt note: only %ld byte remains, "
21334 "not enough for a full note\n",
21335 "Corrupt note: only %ld bytes remain, "
21336 "not enough for a full note\n",
21337 data_remaining),
21338 (long) data_remaining);
21339 break;
21341 data_remaining -= min_notesz;
21343 vms_external = (Elf64_External_VMS_Note *) external;
21344 inote.type = BYTE_GET (vms_external->type);
21345 inote.namesz = BYTE_GET (vms_external->namesz);
21346 inote.namedata = vms_external->name;
21347 inote.descsz = BYTE_GET (vms_external->descsz);
21348 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21349 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21350 next = inote.descdata + align_power (inote.descsz, 3);
21353 /* PR 17531: file: 3443835e. */
21354 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21355 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21356 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21357 || (size_t) (next - inote.descdata) < inote.descsz
21358 || ((size_t) (next - inote.descdata)
21359 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21361 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21362 (unsigned long) ((char *) external - (char *) pnotes));
21363 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21364 inote.type, inote.namesz, inote.descsz, (int) align);
21365 break;
21368 external = (Elf_External_Note *) next;
21370 /* Verify that name is null terminated. It appears that at least
21371 one version of Linux (RedHat 6.0) generates corefiles that don't
21372 comply with the ELF spec by failing to include the null byte in
21373 namesz. */
21374 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21376 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21378 temp = (char *) malloc (inote.namesz + 1);
21379 if (temp == NULL)
21381 error (_("Out of memory allocating space for inote name\n"));
21382 res = false;
21383 break;
21386 memcpy (temp, inote.namedata, inote.namesz);
21387 inote.namedata = temp;
21389 inote.namedata[inote.namesz] = 0;
21392 if (! process_note (& inote, filedata))
21393 res = false;
21395 free (temp);
21396 temp = NULL;
21399 free (pnotes);
21401 return res;
21404 static bool
21405 process_corefile_note_segments (Filedata * filedata)
21407 Elf_Internal_Phdr *segment;
21408 unsigned int i;
21409 bool res = true;
21411 if (! get_program_headers (filedata))
21412 return true;
21414 for (i = 0, segment = filedata->program_headers;
21415 i < filedata->file_header.e_phnum;
21416 i++, segment++)
21418 if (segment->p_type == PT_NOTE)
21419 if (! process_notes_at (filedata, NULL,
21420 (bfd_vma) segment->p_offset,
21421 (bfd_vma) segment->p_filesz,
21422 (bfd_vma) segment->p_align))
21423 res = false;
21426 return res;
21429 static bool
21430 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
21432 Elf_External_Note * pnotes;
21433 Elf_External_Note * external;
21434 char * end;
21435 bool res = true;
21437 if (length <= 0)
21438 return false;
21440 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21441 _("v850 notes"));
21442 if (pnotes == NULL)
21443 return false;
21445 external = pnotes;
21446 end = (char*) pnotes + length;
21448 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21449 (unsigned long) offset, (unsigned long) length);
21451 while ((char *) external + sizeof (Elf_External_Note) < end)
21453 Elf_External_Note * next;
21454 Elf_Internal_Note inote;
21456 inote.type = BYTE_GET (external->type);
21457 inote.namesz = BYTE_GET (external->namesz);
21458 inote.namedata = external->name;
21459 inote.descsz = BYTE_GET (external->descsz);
21460 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21461 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21463 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21465 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21466 inote.descdata = inote.namedata;
21467 inote.namesz = 0;
21470 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21472 if ( ((char *) next > end)
21473 || ((char *) next < (char *) pnotes))
21475 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21476 (unsigned long) ((char *) external - (char *) pnotes));
21477 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21478 inote.type, inote.namesz, inote.descsz);
21479 break;
21482 external = next;
21484 /* Prevent out-of-bounds indexing. */
21485 if ( inote.namedata + inote.namesz > end
21486 || inote.namedata + inote.namesz < inote.namedata)
21488 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21489 (unsigned long) ((char *) external - (char *) pnotes));
21490 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21491 inote.type, inote.namesz, inote.descsz);
21492 break;
21495 printf (" %s: ", get_v850_elf_note_type (inote.type));
21497 if (! print_v850_note (& inote))
21499 res = false;
21500 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21501 inote.namesz, inote.descsz);
21505 free (pnotes);
21507 return res;
21510 static bool
21511 process_note_sections (Filedata * filedata)
21513 Elf_Internal_Shdr *section;
21514 unsigned long i;
21515 unsigned int n = 0;
21516 bool res = true;
21518 for (i = 0, section = filedata->section_headers;
21519 i < filedata->file_header.e_shnum && section != NULL;
21520 i++, section++)
21522 if (section->sh_type == SHT_NOTE)
21524 if (! process_notes_at (filedata, section,
21525 (bfd_vma) section->sh_offset,
21526 (bfd_vma) section->sh_size,
21527 (bfd_vma) section->sh_addralign))
21528 res = false;
21529 n++;
21532 if (( filedata->file_header.e_machine == EM_V800
21533 || filedata->file_header.e_machine == EM_V850
21534 || filedata->file_header.e_machine == EM_CYGNUS_V850)
21535 && section->sh_type == SHT_RENESAS_INFO)
21537 if (! process_v850_notes (filedata,
21538 (bfd_vma) section->sh_offset,
21539 (bfd_vma) section->sh_size))
21540 res = false;
21541 n++;
21545 if (n == 0)
21546 /* Try processing NOTE segments instead. */
21547 return process_corefile_note_segments (filedata);
21549 return res;
21552 static bool
21553 process_notes (Filedata * filedata)
21555 /* If we have not been asked to display the notes then do nothing. */
21556 if (! do_notes)
21557 return true;
21559 if (filedata->file_header.e_type != ET_CORE)
21560 return process_note_sections (filedata);
21562 /* No program headers means no NOTE segment. */
21563 if (filedata->file_header.e_phnum > 0)
21564 return process_corefile_note_segments (filedata);
21566 if (filedata->is_separate)
21567 printf (_("No notes found in linked file '%s'.\n"),
21568 filedata->file_name);
21569 else
21570 printf (_("No notes found file.\n"));
21572 return true;
21575 static unsigned char *
21576 display_public_gnu_attributes (unsigned char * start,
21577 const unsigned char * const end)
21579 printf (_(" Unknown GNU attribute: %s\n"), start);
21581 start += strnlen ((char *) start, end - start);
21582 display_raw_attribute (start, end);
21584 return (unsigned char *) end;
21587 static unsigned char *
21588 display_generic_attribute (unsigned char * start,
21589 unsigned int tag,
21590 const unsigned char * const end)
21592 if (tag == 0)
21593 return (unsigned char *) end;
21595 return display_tag_value (tag, start, end);
21598 static bool
21599 process_arch_specific (Filedata * filedata)
21601 if (! do_arch)
21602 return true;
21604 switch (filedata->file_header.e_machine)
21606 case EM_ARC:
21607 case EM_ARC_COMPACT:
21608 case EM_ARC_COMPACT2:
21609 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
21610 display_arc_attribute,
21611 display_generic_attribute);
21612 case EM_ARM:
21613 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
21614 display_arm_attribute,
21615 display_generic_attribute);
21617 case EM_MIPS:
21618 case EM_MIPS_RS3_LE:
21619 return process_mips_specific (filedata);
21621 case EM_MSP430:
21622 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
21623 display_msp430_attribute,
21624 display_msp430_gnu_attribute);
21626 case EM_RISCV:
21627 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
21628 display_riscv_attribute,
21629 display_generic_attribute);
21631 case EM_NDS32:
21632 return process_nds32_specific (filedata);
21634 case EM_68K:
21635 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21636 display_m68k_gnu_attribute);
21638 case EM_PPC:
21639 case EM_PPC64:
21640 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21641 display_power_gnu_attribute);
21643 case EM_S390:
21644 case EM_S390_OLD:
21645 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21646 display_s390_gnu_attribute);
21648 case EM_SPARC:
21649 case EM_SPARC32PLUS:
21650 case EM_SPARCV9:
21651 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21652 display_sparc_gnu_attribute);
21654 case EM_TI_C6000:
21655 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
21656 display_tic6x_attribute,
21657 display_generic_attribute);
21659 case EM_CSKY:
21660 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
21661 display_csky_attribute, NULL);
21663 default:
21664 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
21665 display_public_gnu_attributes,
21666 display_generic_attribute);
21670 static bool
21671 get_file_header (Filedata * filedata)
21673 /* Read in the identity array. */
21674 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
21675 return false;
21677 /* Determine how to read the rest of the header. */
21678 switch (filedata->file_header.e_ident[EI_DATA])
21680 default:
21681 case ELFDATANONE:
21682 case ELFDATA2LSB:
21683 byte_get = byte_get_little_endian;
21684 byte_put = byte_put_little_endian;
21685 break;
21686 case ELFDATA2MSB:
21687 byte_get = byte_get_big_endian;
21688 byte_put = byte_put_big_endian;
21689 break;
21692 /* For now we only support 32 bit and 64 bit ELF files. */
21693 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
21695 /* Read in the rest of the header. */
21696 if (is_32bit_elf)
21698 Elf32_External_Ehdr ehdr32;
21700 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
21701 return false;
21703 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
21704 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
21705 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
21706 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
21707 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
21708 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
21709 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
21710 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
21711 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
21712 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
21713 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
21714 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
21715 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
21717 else
21719 Elf64_External_Ehdr ehdr64;
21721 /* If we have been compiled with sizeof (bfd_vma) == 4, then
21722 we will not be able to cope with the 64bit data found in
21723 64 ELF files. Detect this now and abort before we start
21724 overwriting things. */
21725 if (sizeof (bfd_vma) < 8)
21727 error (_("This instance of readelf has been built without support for a\n\
21728 64 bit data type and so it cannot read 64 bit ELF files.\n"));
21729 return false;
21732 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
21733 return false;
21735 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
21736 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
21737 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
21738 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
21739 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
21740 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
21741 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
21742 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
21743 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
21744 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
21745 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
21746 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
21747 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
21750 return true;
21753 static void
21754 free_filedata (Filedata *filedata)
21756 free (filedata->program_interpreter);
21757 free (filedata->program_headers);
21758 free (filedata->section_headers);
21759 free (filedata->string_table);
21760 free (filedata->dump.dump_sects);
21761 free (filedata->dynamic_strings);
21762 free (filedata->dynamic_symbols);
21763 free (filedata->dynamic_syminfo);
21764 free (filedata->dynamic_section);
21766 while (filedata->symtab_shndx_list != NULL)
21768 elf_section_list *next = filedata->symtab_shndx_list->next;
21769 free (filedata->symtab_shndx_list);
21770 filedata->symtab_shndx_list = next;
21773 free (filedata->section_headers_groups);
21775 if (filedata->section_groups)
21777 size_t i;
21778 struct group_list * g;
21779 struct group_list * next;
21781 for (i = 0; i < filedata->group_count; i++)
21783 for (g = filedata->section_groups [i].root; g != NULL; g = next)
21785 next = g->next;
21786 free (g);
21790 free (filedata->section_groups);
21792 memset (&filedata->section_headers, 0,
21793 sizeof (Filedata) - offsetof (Filedata, section_headers));
21796 static void
21797 close_file (Filedata * filedata)
21799 if (filedata)
21801 if (filedata->handle)
21802 fclose (filedata->handle);
21803 free (filedata);
21807 void
21808 close_debug_file (void * data)
21810 free_filedata ((Filedata *) data);
21811 close_file ((Filedata *) data);
21814 static Filedata *
21815 open_file (const char * pathname, bool is_separate)
21817 struct stat statbuf;
21818 Filedata * filedata = NULL;
21820 if (stat (pathname, & statbuf) < 0
21821 || ! S_ISREG (statbuf.st_mode))
21822 goto fail;
21824 filedata = calloc (1, sizeof * filedata);
21825 if (filedata == NULL)
21826 goto fail;
21828 filedata->handle = fopen (pathname, "rb");
21829 if (filedata->handle == NULL)
21830 goto fail;
21832 filedata->file_size = (bfd_size_type) statbuf.st_size;
21833 filedata->file_name = pathname;
21834 filedata->is_separate = is_separate;
21836 if (! get_file_header (filedata))
21837 goto fail;
21839 if (!get_section_headers (filedata, false))
21840 goto fail;
21842 return filedata;
21844 fail:
21845 if (filedata)
21847 if (filedata->handle)
21848 fclose (filedata->handle);
21849 free (filedata);
21851 return NULL;
21854 void *
21855 open_debug_file (const char * pathname)
21857 return open_file (pathname, true);
21860 static void
21861 initialise_dump_sects (Filedata * filedata)
21863 /* Initialise the dump_sects array from the cmdline_dump_sects array.
21864 Note we do this even if cmdline_dump_sects is empty because we
21865 must make sure that the dump_sets array is zeroed out before each
21866 object file is processed. */
21867 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
21868 memset (filedata->dump.dump_sects, 0,
21869 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21871 if (cmdline.num_dump_sects > 0)
21873 if (filedata->dump.num_dump_sects == 0)
21874 /* A sneaky way of allocating the dump_sects array. */
21875 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
21877 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
21878 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
21879 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21883 /* Process one ELF object file according to the command line options.
21884 This file may actually be stored in an archive. The file is
21885 positioned at the start of the ELF object. Returns TRUE if no
21886 problems were encountered, FALSE otherwise. */
21888 static bool
21889 process_object (Filedata * filedata)
21891 bool have_separate_files;
21892 unsigned int i;
21893 bool res;
21895 if (! get_file_header (filedata))
21897 error (_("%s: Failed to read file header\n"), filedata->file_name);
21898 return false;
21901 /* Initialise per file variables. */
21902 for (i = ARRAY_SIZE (filedata->version_info); i--;)
21903 filedata->version_info[i] = 0;
21905 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
21906 filedata->dynamic_info[i] = 0;
21907 filedata->dynamic_info_DT_GNU_HASH = 0;
21908 filedata->dynamic_info_DT_MIPS_XHASH = 0;
21910 /* Process the file. */
21911 if (show_name)
21912 printf (_("\nFile: %s\n"), filedata->file_name);
21914 initialise_dump_sects (filedata);
21916 /* There may be some extensions in the first section header. Don't
21917 bomb if we can't read it. */
21918 get_section_headers (filedata, true);
21920 if (! process_file_header (filedata))
21922 res = false;
21923 goto out;
21926 /* Throw away the single section header read above, so that we
21927 re-read the entire set. */
21928 free (filedata->section_headers);
21929 filedata->section_headers = NULL;
21931 if (! process_section_headers (filedata))
21933 /* Without loaded section headers we cannot process lots of things. */
21934 do_unwind = do_version = do_dump = do_arch = false;
21936 if (! do_using_dynamic)
21937 do_syms = do_dyn_syms = do_reloc = false;
21940 if (! process_section_groups (filedata))
21941 /* Without loaded section groups we cannot process unwind. */
21942 do_unwind = false;
21944 process_program_headers (filedata);
21946 res = process_dynamic_section (filedata);
21948 if (! process_relocs (filedata))
21949 res = false;
21951 if (! process_unwind (filedata))
21952 res = false;
21954 if (! process_symbol_table (filedata))
21955 res = false;
21957 if (! process_lto_symbol_tables (filedata))
21958 res = false;
21960 if (! process_syminfo (filedata))
21961 res = false;
21963 if (! process_version_sections (filedata))
21964 res = false;
21966 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
21967 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
21968 else
21969 have_separate_files = false;
21971 if (! process_section_contents (filedata))
21972 res = false;
21974 if (have_separate_files)
21976 separate_info * d;
21978 for (d = first_separate_info; d != NULL; d = d->next)
21980 initialise_dump_sects (d->handle);
21982 if (process_links && ! process_file_header (d->handle))
21983 res = false;
21984 else if (! process_section_headers (d->handle))
21985 res = false;
21986 else if (! process_section_contents (d->handle))
21987 res = false;
21988 else if (process_links)
21990 if (! process_section_groups (d->handle))
21991 res = false;
21992 process_program_headers (d->handle);
21993 if (! process_dynamic_section (d->handle))
21994 res = false;
21995 if (! process_relocs (d->handle))
21996 res = false;
21997 if (! process_unwind (d->handle))
21998 res = false;
21999 if (! process_symbol_table (d->handle))
22000 res = false;
22001 if (! process_lto_symbol_tables (d->handle))
22002 res = false;
22003 if (! process_syminfo (d->handle))
22004 res = false;
22005 if (! process_version_sections (d->handle))
22006 res = false;
22007 if (! process_notes (d->handle))
22008 res = false;
22012 /* The file handles are closed by the call to free_debug_memory() below. */
22015 if (! process_notes (filedata))
22016 res = false;
22018 if (! process_gnu_liblist (filedata))
22019 res = false;
22021 if (! process_arch_specific (filedata))
22022 res = false;
22024 out:
22025 free_filedata (filedata);
22027 free_debug_memory ();
22029 return res;
22032 /* Process an ELF archive.
22033 On entry the file is positioned just after the ARMAG string.
22034 Returns TRUE upon success, FALSE otherwise. */
22036 static bool
22037 process_archive (Filedata * filedata, bool is_thin_archive)
22039 struct archive_info arch;
22040 struct archive_info nested_arch;
22041 size_t got;
22042 bool ret = true;
22044 show_name = true;
22046 /* The ARCH structure is used to hold information about this archive. */
22047 arch.file_name = NULL;
22048 arch.file = NULL;
22049 arch.index_array = NULL;
22050 arch.sym_table = NULL;
22051 arch.longnames = NULL;
22053 /* The NESTED_ARCH structure is used as a single-item cache of information
22054 about a nested archive (when members of a thin archive reside within
22055 another regular archive file). */
22056 nested_arch.file_name = NULL;
22057 nested_arch.file = NULL;
22058 nested_arch.index_array = NULL;
22059 nested_arch.sym_table = NULL;
22060 nested_arch.longnames = NULL;
22062 if (setup_archive (&arch, filedata->file_name, filedata->handle,
22063 filedata->file_size, is_thin_archive,
22064 do_archive_index) != 0)
22066 ret = false;
22067 goto out;
22070 if (do_archive_index)
22072 if (arch.sym_table == NULL)
22073 error (_("%s: unable to dump the index as none was found\n"),
22074 filedata->file_name);
22075 else
22077 unsigned long i, l;
22078 unsigned long current_pos;
22080 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
22081 "in the symbol table)\n"),
22082 filedata->file_name, (unsigned long) arch.index_num,
22083 arch.sym_size);
22085 current_pos = ftell (filedata->handle);
22087 for (i = l = 0; i < arch.index_num; i++)
22089 if (i == 0
22090 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
22092 char * member_name
22093 = get_archive_member_name_at (&arch, arch.index_array[i],
22094 &nested_arch);
22096 if (member_name != NULL)
22098 char * qualified_name
22099 = make_qualified_name (&arch, &nested_arch,
22100 member_name);
22102 if (qualified_name != NULL)
22104 printf (_("Contents of binary %s at offset "),
22105 qualified_name);
22106 (void) print_vma (arch.index_array[i], PREFIX_HEX);
22107 putchar ('\n');
22108 free (qualified_name);
22110 free (member_name);
22114 if (l >= arch.sym_size)
22116 error (_("%s: end of the symbol table reached "
22117 "before the end of the index\n"),
22118 filedata->file_name);
22119 ret = false;
22120 break;
22122 /* PR 17531: file: 0b6630b2. */
22123 printf ("\t%.*s\n",
22124 (int) (arch.sym_size - l), arch.sym_table + l);
22125 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
22128 if (arch.uses_64bit_indices)
22129 l = (l + 7) & ~ 7;
22130 else
22131 l += l & 1;
22133 if (l < arch.sym_size)
22135 error (ngettext ("%s: %ld byte remains in the symbol table, "
22136 "but without corresponding entries in "
22137 "the index table\n",
22138 "%s: %ld bytes remain in the symbol table, "
22139 "but without corresponding entries in "
22140 "the index table\n",
22141 arch.sym_size - l),
22142 filedata->file_name, arch.sym_size - l);
22143 ret = false;
22146 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
22148 error (_("%s: failed to seek back to start of object files "
22149 "in the archive\n"),
22150 filedata->file_name);
22151 ret = false;
22152 goto out;
22156 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
22157 && !do_segments && !do_header && !do_dump && !do_version
22158 && !do_histogram && !do_debugging && !do_arch && !do_notes
22159 && !do_section_groups && !do_dyn_syms)
22161 ret = true; /* Archive index only. */
22162 goto out;
22166 while (1)
22168 char * name;
22169 size_t namelen;
22170 char * qualified_name;
22172 /* Read the next archive header. */
22173 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
22175 error (_("%s: failed to seek to next archive header\n"),
22176 arch.file_name);
22177 ret = false;
22178 break;
22180 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
22181 if (got != sizeof arch.arhdr)
22183 if (got == 0)
22184 break;
22185 /* PR 24049 - we cannot use filedata->file_name as this will
22186 have already been freed. */
22187 error (_("%s: failed to read archive header\n"), arch.file_name);
22189 ret = false;
22190 break;
22192 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
22194 error (_("%s: did not find a valid archive header\n"),
22195 arch.file_name);
22196 ret = false;
22197 break;
22200 arch.next_arhdr_offset += sizeof arch.arhdr;
22202 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
22204 name = get_archive_member_name (&arch, &nested_arch);
22205 if (name == NULL)
22207 error (_("%s: bad archive file name\n"), arch.file_name);
22208 ret = false;
22209 break;
22211 namelen = strlen (name);
22213 qualified_name = make_qualified_name (&arch, &nested_arch, name);
22214 if (qualified_name == NULL)
22216 error (_("%s: bad archive file name\n"), arch.file_name);
22217 free (name);
22218 ret = false;
22219 break;
22222 if (is_thin_archive && arch.nested_member_origin == 0)
22224 /* This is a proxy for an external member of a thin archive. */
22225 Filedata * member_filedata;
22226 char * member_file_name = adjust_relative_path
22227 (filedata->file_name, name, namelen);
22229 free (name);
22230 if (member_file_name == NULL)
22232 free (qualified_name);
22233 ret = false;
22234 break;
22237 member_filedata = open_file (member_file_name, false);
22238 if (member_filedata == NULL)
22240 error (_("Input file '%s' is not readable.\n"), member_file_name);
22241 free (member_file_name);
22242 free (qualified_name);
22243 ret = false;
22244 break;
22247 filedata->archive_file_offset = arch.nested_member_origin;
22248 member_filedata->file_name = qualified_name;
22250 /* The call to process_object() expects the file to be at the beginning. */
22251 rewind (member_filedata->handle);
22253 if (! process_object (member_filedata))
22254 ret = false;
22256 close_file (member_filedata);
22257 free (member_file_name);
22259 else if (is_thin_archive)
22261 Filedata thin_filedata;
22263 memset (&thin_filedata, 0, sizeof (thin_filedata));
22265 /* PR 15140: Allow for corrupt thin archives. */
22266 if (nested_arch.file == NULL)
22268 error (_("%s: contains corrupt thin archive: %s\n"),
22269 qualified_name, name);
22270 free (qualified_name);
22271 free (name);
22272 ret = false;
22273 break;
22275 free (name);
22277 /* This is a proxy for a member of a nested archive. */
22278 filedata->archive_file_offset
22279 = arch.nested_member_origin + sizeof arch.arhdr;
22281 /* The nested archive file will have been opened and setup by
22282 get_archive_member_name. */
22283 if (fseek (nested_arch.file, filedata->archive_file_offset,
22284 SEEK_SET) != 0)
22286 error (_("%s: failed to seek to archive member.\n"),
22287 nested_arch.file_name);
22288 free (qualified_name);
22289 ret = false;
22290 break;
22293 thin_filedata.handle = nested_arch.file;
22294 thin_filedata.file_name = qualified_name;
22296 if (! process_object (& thin_filedata))
22297 ret = false;
22299 else
22301 free (name);
22302 filedata->archive_file_offset = arch.next_arhdr_offset;
22303 filedata->file_name = qualified_name;
22304 if (! process_object (filedata))
22305 ret = false;
22306 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
22307 /* Stop looping with "negative" archive_file_size. */
22308 if (arch.next_arhdr_offset < filedata->archive_file_size)
22309 arch.next_arhdr_offset = -1ul;
22312 free (qualified_name);
22315 out:
22316 if (nested_arch.file != NULL)
22317 fclose (nested_arch.file);
22318 release_archive (&nested_arch);
22319 release_archive (&arch);
22321 return ret;
22324 static bool
22325 process_file (char * file_name)
22327 Filedata * filedata = NULL;
22328 struct stat statbuf;
22329 char armag[SARMAG];
22330 bool ret = true;
22332 if (stat (file_name, &statbuf) < 0)
22334 if (errno == ENOENT)
22335 error (_("'%s': No such file\n"), file_name);
22336 else
22337 error (_("Could not locate '%s'. System error message: %s\n"),
22338 file_name, strerror (errno));
22339 return false;
22342 if (! S_ISREG (statbuf.st_mode))
22344 error (_("'%s' is not an ordinary file\n"), file_name);
22345 return false;
22348 filedata = calloc (1, sizeof * filedata);
22349 if (filedata == NULL)
22351 error (_("Out of memory allocating file data structure\n"));
22352 return false;
22355 filedata->file_name = file_name;
22356 filedata->handle = fopen (file_name, "rb");
22357 if (filedata->handle == NULL)
22359 error (_("Input file '%s' is not readable.\n"), file_name);
22360 free (filedata);
22361 return false;
22364 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22366 error (_("%s: Failed to read file's magic number\n"), file_name);
22367 fclose (filedata->handle);
22368 free (filedata);
22369 return false;
22372 filedata->file_size = (bfd_size_type) statbuf.st_size;
22373 filedata->is_separate = false;
22375 if (memcmp (armag, ARMAG, SARMAG) == 0)
22377 if (! process_archive (filedata, false))
22378 ret = false;
22380 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22382 if ( ! process_archive (filedata, true))
22383 ret = false;
22385 else
22387 if (do_archive_index && !check_all)
22388 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22389 file_name);
22391 rewind (filedata->handle);
22392 filedata->archive_file_size = filedata->archive_file_offset = 0;
22394 if (! process_object (filedata))
22395 ret = false;
22398 fclose (filedata->handle);
22399 free (filedata->section_headers);
22400 free (filedata->program_headers);
22401 free (filedata->string_table);
22402 free (filedata->dump.dump_sects);
22403 free (filedata);
22405 free (ba_cache.strtab);
22406 ba_cache.strtab = NULL;
22407 free (ba_cache.symtab);
22408 ba_cache.symtab = NULL;
22409 ba_cache.filedata = NULL;
22411 return ret;
22414 #ifdef SUPPORT_DISASSEMBLY
22415 /* Needed by the i386 disassembler. For extra credit, someone could
22416 fix this so that we insert symbolic addresses here, esp for GOT/PLT
22417 symbols. */
22419 void
22420 print_address (unsigned int addr, FILE * outfile)
22422 fprintf (outfile,"0x%8.8x", addr);
22425 /* Needed by the i386 disassembler. */
22427 void
22428 db_task_printsym (unsigned int addr)
22430 print_address (addr, stderr);
22432 #endif
22435 main (int argc, char ** argv)
22437 int err;
22439 #ifdef HAVE_LC_MESSAGES
22440 setlocale (LC_MESSAGES, "");
22441 #endif
22442 setlocale (LC_CTYPE, "");
22443 bindtextdomain (PACKAGE, LOCALEDIR);
22444 textdomain (PACKAGE);
22446 expandargv (&argc, &argv);
22448 parse_args (& cmdline, argc, argv);
22450 if (optind < (argc - 1))
22451 /* When displaying information for more than one file,
22452 prefix the information with the file name. */
22453 show_name = true;
22454 else if (optind >= argc)
22456 /* Ensure that the warning is always displayed. */
22457 do_checks = true;
22459 warn (_("Nothing to do.\n"));
22460 usage (stderr);
22463 err = false;
22464 while (optind < argc)
22465 if (! process_file (argv[optind++]))
22466 err = true;
22468 free (cmdline.dump_sects);
22470 free (dump_ctf_symtab_name);
22471 free (dump_ctf_strtab_name);
22472 free (dump_ctf_parent_name);
22474 return err ? EXIT_FAILURE : EXIT_SUCCESS;