Merge branch 'vendor/NCURSES'
[dragonfly.git] / contrib / binutils-2.25 / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2    Copyright (C) 1998-2014 Free Software Foundation, Inc.
3
4    Originally developed by Eric Youngdale <eric@andante.jic.com>
5    Modifications by Nick Clifton <nickc@redhat.com>
6
7    This file is part of GNU Binutils.
8
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.
13
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.
18
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.  */
23 \f
24 /* The difference between readelf and objdump:
25
26   Both programs are capable of displaying the contents of ELF format files,
27   so why does the binutils project have two file dumpers ?
28
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.
35
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.
38
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.  */
42 \f
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #ifdef HAVE_ZLIB_H
47 #include <zlib.h>
48 #endif
49 #ifdef HAVE_WCHAR_H
50 #include <wchar.h>
51 #endif
52
53 #if __GNUC__ >= 2
54 /* Define BFD64 here, even if our default architecture is 32 bit ELF
55    as this will allow us to read in and parse 64bit and 32bit ELF files.
56    Only do this if we believe that the compiler can support a 64 bit
57    data type.  For now we only rely on GCC being able to do this.  */
58 #define BFD64
59 #endif
60
61 #include "bfd.h"
62 #include "bucomm.h"
63 #include "elfcomm.h"
64 #include "dwarf.h"
65
66 #include "elf/common.h"
67 #include "elf/external.h"
68 #include "elf/internal.h"
69
70
71 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
72    we can obtain the H8 reloc numbers.  We need these for the
73    get_reloc_size() function.  We include h8.h again after defining
74    RELOC_MACROS_GEN_FUNC so that we get the naming function as well.  */
75
76 #include "elf/h8.h"
77 #undef _ELF_H8_H
78
79 /* Undo the effects of #including reloc-macros.h.  */
80
81 #undef START_RELOC_NUMBERS
82 #undef RELOC_NUMBER
83 #undef FAKE_RELOC
84 #undef EMPTY_RELOC
85 #undef END_RELOC_NUMBERS
86 #undef _RELOC_MACROS_H
87
88 /* The following headers use the elf/reloc-macros.h file to
89    automatically generate relocation recognition functions
90    such as elf_mips_reloc_type()  */
91
92 #define RELOC_MACROS_GEN_FUNC
93
94 #include "elf/aarch64.h"
95 #include "elf/alpha.h"
96 #include "elf/arc.h"
97 #include "elf/arm.h"
98 #include "elf/avr.h"
99 #include "elf/bfin.h"
100 #include "elf/cr16.h"
101 #include "elf/cris.h"
102 #include "elf/crx.h"
103 #include "elf/d10v.h"
104 #include "elf/d30v.h"
105 #include "elf/dlx.h"
106 #include "elf/epiphany.h"
107 #include "elf/fr30.h"
108 #include "elf/frv.h"
109 #include "elf/h8.h"
110 #include "elf/hppa.h"
111 #include "elf/i386.h"
112 #include "elf/i370.h"
113 #include "elf/i860.h"
114 #include "elf/i960.h"
115 #include "elf/ia64.h"
116 #include "elf/ip2k.h"
117 #include "elf/lm32.h"
118 #include "elf/iq2000.h"
119 #include "elf/m32c.h"
120 #include "elf/m32r.h"
121 #include "elf/m68k.h"
122 #include "elf/m68hc11.h"
123 #include "elf/mcore.h"
124 #include "elf/mep.h"
125 #include "elf/metag.h"
126 #include "elf/microblaze.h"
127 #include "elf/mips.h"
128 #include "elf/mmix.h"
129 #include "elf/mn10200.h"
130 #include "elf/mn10300.h"
131 #include "elf/moxie.h"
132 #include "elf/mt.h"
133 #include "elf/msp430.h"
134 #include "elf/nds32.h"
135 #include "elf/nios2.h"
136 #include "elf/or1k.h"
137 #include "elf/pj.h"
138 #include "elf/ppc.h"
139 #include "elf/ppc64.h"
140 #include "elf/rl78.h"
141 #include "elf/rx.h"
142 #include "elf/s390.h"
143 #include "elf/score.h"
144 #include "elf/sh.h"
145 #include "elf/sparc.h"
146 #include "elf/spu.h"
147 #include "elf/tic6x.h"
148 #include "elf/tilegx.h"
149 #include "elf/tilepro.h"
150 #include "elf/v850.h"
151 #include "elf/vax.h"
152 #include "elf/x86-64.h"
153 #include "elf/xc16x.h"
154 #include "elf/xgate.h"
155 #include "elf/xstormy16.h"
156 #include "elf/xtensa.h"
157
158 #include "getopt.h"
159 #include "libiberty.h"
160 #include "safe-ctype.h"
161 #include "filenames.h"
162
163 #ifndef offsetof
164 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
165 #endif
166
167 char * program_name = "readelf";
168 static unsigned long archive_file_offset;
169 static unsigned long archive_file_size;
170 static bfd_size_type current_file_size;
171 static unsigned long dynamic_addr;
172 static bfd_size_type dynamic_size;
173 static size_t dynamic_nent;
174 static char * dynamic_strings;
175 static unsigned long dynamic_strings_length;
176 static char * string_table;
177 static unsigned long string_table_length;
178 static unsigned long num_dynamic_syms;
179 static Elf_Internal_Sym * dynamic_symbols;
180 static Elf_Internal_Syminfo * dynamic_syminfo;
181 static unsigned long dynamic_syminfo_offset;
182 static unsigned int dynamic_syminfo_nent;
183 static char program_interpreter[PATH_MAX];
184 static bfd_vma dynamic_info[DT_ENCODING];
185 static bfd_vma dynamic_info_DT_GNU_HASH;
186 static bfd_vma version_info[16];
187 static Elf_Internal_Ehdr elf_header;
188 static Elf_Internal_Shdr * section_headers;
189 static Elf_Internal_Phdr * program_headers;
190 static Elf_Internal_Dyn *  dynamic_section;
191 static Elf_Internal_Shdr * symtab_shndx_hdr;
192 static int show_name;
193 static int do_dynamic;
194 static int do_syms;
195 static int do_dyn_syms;
196 static int do_reloc;
197 static int do_sections;
198 static int do_section_groups;
199 static int do_section_details;
200 static int do_segments;
201 static int do_unwind;
202 static int do_using_dynamic;
203 static int do_header;
204 static int do_dump;
205 static int do_version;
206 static int do_histogram;
207 static int do_debugging;
208 static int do_arch;
209 static int do_notes;
210 static int do_archive_index;
211 static int is_32bit_elf;
212
213 struct group_list
214 {
215   struct group_list * next;
216   unsigned int section_index;
217 };
218
219 struct group
220 {
221   struct group_list * root;
222   unsigned int group_index;
223 };
224
225 static size_t group_count;
226 static struct group * section_groups;
227 static struct group ** section_headers_groups;
228
229
230 /* Flag bits indicating particular types of dump.  */
231 #define HEX_DUMP        (1 << 0)        /* The -x command line switch.  */
232 #define DISASS_DUMP     (1 << 1)        /* The -i command line switch.  */
233 #define DEBUG_DUMP      (1 << 2)        /* The -w command line switch.  */
234 #define STRING_DUMP     (1 << 3)        /* The -p command line switch.  */
235 #define RELOC_DUMP      (1 << 4)        /* The -R command line switch.  */
236
237 typedef unsigned char dump_type;
238
239 /* A linked list of the section names for which dumps were requested.  */
240 struct dump_list_entry
241 {
242   char * name;
243   dump_type type;
244   struct dump_list_entry * next;
245 };
246 static struct dump_list_entry * dump_sects_byname;
247
248 /* A dynamic array of flags indicating for which sections a dump
249    has been requested via command line switches.  */
250 static dump_type *   cmdline_dump_sects = NULL;
251 static unsigned int  num_cmdline_dump_sects = 0;
252
253 /* A dynamic array of flags indicating for which sections a dump of
254    some kind has been requested.  It is reset on a per-object file
255    basis and then initialised from the cmdline_dump_sects array,
256    the results of interpreting the -w switch, and the
257    dump_sects_byname list.  */
258 static dump_type *   dump_sects = NULL;
259 static unsigned int  num_dump_sects = 0;
260
261
262 /* How to print a vma value.  */
263 typedef enum print_mode
264 {
265   HEX,
266   DEC,
267   DEC_5,
268   UNSIGNED,
269   PREFIX_HEX,
270   FULL_HEX,
271   LONG_HEX
272 }
273 print_mode;
274
275 #define UNKNOWN -1
276
277 #define SECTION_NAME(X)                                         \
278   ((X) == NULL ? _("<none>")                                    \
279    : string_table == NULL ? _("<no-name>")                      \
280    : ((X)->sh_name >= string_table_length ? _("<corrupt>")      \
281   : string_table + (X)->sh_name))
282
283 #define DT_VERSIONTAGIDX(tag)   (DT_VERNEEDNUM - (tag)) /* Reverse order!  */
284
285 #define GET_ELF_SYMBOLS(file, section, sym_count)                       \
286   (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count)      \
287    : get_64bit_elf_symbols (file, section, sym_count))
288
289 #define VALID_DYNAMIC_NAME(offset)      ((dynamic_strings != NULL) && (offset < dynamic_strings_length))
290 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
291    already been called and verified that the string exists.  */
292 #define GET_DYNAMIC_NAME(offset)        (dynamic_strings + offset)
293
294 #define REMOVE_ARCH_BITS(ADDR)                  \
295   do                                            \
296     {                                           \
297       if (elf_header.e_machine == EM_ARM)       \
298         (ADDR) &= ~1;                           \
299     }                                           \
300   while (0)
301 \f
302 /* Retrieve NMEMB structures, each SIZE bytes long from FILE starting at OFFSET +
303    the offset of the current archive member, if we are examining an archive.
304    Put the retrieved data into VAR, if it is not NULL.  Otherwise allocate a buffer
305    using malloc and fill that.  In either case return the pointer to the start of
306    the retrieved data or NULL if something went wrong.  If something does go wrong
307    and REASON is not NULL then emit an error message using REASON as part of the
308    context.  */
309
310 static void *
311 get_data (void * var, FILE * file, unsigned long offset, bfd_size_type size,
312           bfd_size_type nmemb, const char * reason)
313 {
314   void * mvar;
315   bfd_size_type amt = size * nmemb;
316
317   if (size == 0 || nmemb == 0)
318     return NULL;
319
320   /* If the size_t type is smaller than the bfd_size_type, eg because
321      you are building a 32-bit tool on a 64-bit host, then make sure
322      that when the sizes are cast to (size_t) no information is lost.  */
323   if (sizeof (size_t) < sizeof (bfd_size_type)
324       && (   (bfd_size_type) ((size_t) size) != size
325           || (bfd_size_type) ((size_t) nmemb) != nmemb))
326     {
327       if (reason)
328         error (_("Size truncation prevents reading 0x%llx elements of size 0x%llx for %s\n"),
329                (unsigned long long) nmemb, (unsigned long long) size, reason);
330       return NULL;
331     }
332
333   /* Check for size overflow.  */
334   if (amt < nmemb)
335     {
336       if (reason)
337         error (_("Size overflow prevents reading 0x%llx elements of size 0x%llx for %s\n"),
338                (unsigned long long) nmemb, (unsigned long long) size, reason);
339       return NULL;
340     }
341
342   /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
343      attempting to allocate memory when the read is bound to fail.  */
344   if (amt > current_file_size
345       || offset + archive_file_offset + amt > current_file_size)
346     {
347       if (reason)
348         error (_("Reading 0x%llx bytes extends past end of file for %s\n"),
349                (unsigned long long) amt, reason);
350       return NULL;
351     }
352
353   if (fseek (file, archive_file_offset + offset, SEEK_SET))
354     {
355       if (reason)
356         error (_("Unable to seek to 0x%lx for %s\n"),
357                (unsigned long) archive_file_offset + offset, reason);
358       return NULL;
359     }
360
361   mvar = var;
362   if (mvar == NULL)
363     {
364       /* Check for overflow.  */
365       if (nmemb < (~(bfd_size_type) 0 - 1) / size)
366         /* + 1 so that we can '\0' terminate invalid string table sections.  */
367         mvar = malloc ((size_t) amt + 1);
368
369       if (mvar == NULL)
370         {
371           if (reason)
372             error (_("Out of memory allocating 0x%llx bytes for %s\n"),
373                    (unsigned long long) amt, reason);
374           return NULL;
375         }
376
377       ((char *) mvar)[amt] = '\0';
378     }
379
380   if (fread (mvar, (size_t) size, (size_t) nmemb, file) != nmemb)
381     {
382       if (reason)
383         error (_("Unable to read in 0x%llx bytes of %s\n"),
384                (unsigned long long) amt, reason);
385       if (mvar != var)
386         free (mvar);
387       return NULL;
388     }
389
390   return mvar;
391 }
392
393 /* Print a VMA value.  */
394
395 static int
396 print_vma (bfd_vma vma, print_mode mode)
397 {
398   int nc = 0;
399
400   switch (mode)
401     {
402     case FULL_HEX:
403       nc = printf ("0x");
404       /* Drop through.  */
405
406     case LONG_HEX:
407 #ifdef BFD64
408       if (is_32bit_elf)
409         return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
410 #endif
411       printf_vma (vma);
412       return nc + 16;
413
414     case DEC_5:
415       if (vma <= 99999)
416         return printf ("%5" BFD_VMA_FMT "d", vma);
417       /* Drop through.  */
418
419     case PREFIX_HEX:
420       nc = printf ("0x");
421       /* Drop through.  */
422
423     case HEX:
424       return nc + printf ("%" BFD_VMA_FMT "x", vma);
425
426     case DEC:
427       return printf ("%" BFD_VMA_FMT "d", vma);
428
429     case UNSIGNED:
430       return printf ("%" BFD_VMA_FMT "u", vma);
431     }
432   return 0;
433 }
434
435 /* Display a symbol on stdout.  Handles the display of control characters and
436    multibye characters (assuming the host environment supports them).
437
438    Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
439
440    If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
441    padding as necessary.
442
443    Returns the number of emitted characters.  */
444
445 static unsigned int
446 print_symbol (int width, const char *symbol)
447 {
448   bfd_boolean extra_padding = FALSE;
449   int num_printed = 0;
450 #ifdef HAVE_MBSTATE_T
451   mbstate_t state;
452 #endif
453   int width_remaining;
454
455   if (width < 0)
456     {
457       /* Keep the width positive.  This also helps.  */
458       width = - width;
459       extra_padding = TRUE;
460     }
461   assert (width != 0);
462
463   if (do_wide)
464     /* Set the remaining width to a very large value.
465        This simplifies the code below.  */
466     width_remaining = INT_MAX;
467   else
468     width_remaining = width;
469
470 #ifdef HAVE_MBSTATE_T
471   /* Initialise the multibyte conversion state.  */
472   memset (& state, 0, sizeof (state));
473 #endif
474
475   while (width_remaining)
476     {
477       size_t  n;
478       const char c = *symbol++;
479
480       if (c == 0)
481         break;
482
483       /* Do not print control characters directly as they can affect terminal
484          settings.  Such characters usually appear in the names generated
485          by the assembler for local labels.  */
486       if (ISCNTRL (c))
487         {
488           if (width_remaining < 2)
489             break;
490
491           printf ("^%c", c + 0x40);
492           width_remaining -= 2;
493           num_printed += 2;
494         }
495       else if (ISPRINT (c))
496         {
497           putchar (c);
498           width_remaining --;
499           num_printed ++;
500         }
501       else
502         {
503 #ifdef HAVE_MBSTATE_T
504           wchar_t w;
505 #endif
506           /* Let printf do the hard work of displaying multibyte characters.  */
507           printf ("%.1s", symbol - 1);
508           width_remaining --;
509           num_printed ++;
510
511 #ifdef HAVE_MBSTATE_T
512           /* Try to find out how many bytes made up the character that was
513              just printed.  Advance the symbol pointer past the bytes that
514              were displayed.  */
515           n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
516 #else
517           n = 1;
518 #endif
519           if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
520             symbol += (n - 1);
521         }
522     }
523
524   if (extra_padding && num_printed < width)
525     {
526       /* Fill in the remaining spaces.  */
527       printf ("%-*s", width - num_printed, " ");
528       num_printed = width;
529     }
530
531   return num_printed;
532 }
533
534 /* Returns a pointer to a static buffer containing a  printable version of
535    the given section's name.  Like print_symbol, except that it does not try
536    to print multibyte characters, it just interprets them as hex values.  */
537
538 static const char *
539 printable_section_name (Elf_Internal_Shdr * sec)
540 {
541 #define MAX_PRINT_SEC_NAME_LEN 128
542   static char  sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
543   const char * name = SECTION_NAME (sec);
544   char *       buf = sec_name_buf;
545   char         c;
546   unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
547
548   while ((c = * name ++) != 0)
549     {
550       if (ISCNTRL (c))
551         {
552           if (remaining < 2)
553             break;
554
555           * buf ++ = '^';
556           * buf ++ = c + 0x40;
557           remaining -= 2;
558         }
559       else if (ISPRINT (c))
560         {
561           * buf ++ = c;
562           remaining -= 1;
563         }
564       else
565         {
566           static char hex[17] = "0123456789ABCDEF";
567
568           if (remaining < 4)
569             break;
570           * buf ++ = '<';
571           * buf ++ = hex[(c & 0xf0) >> 4];
572           * buf ++ = hex[c & 0x0f];
573           * buf ++ = '>';
574           remaining -= 4;
575         }
576
577       if (remaining == 0)
578         break;
579     }
580
581   * buf = 0;
582   return sec_name_buf;
583 }
584
585 static const char *
586 printable_section_name_from_index (unsigned long ndx)
587 {
588   if (ndx >= elf_header.e_shnum)
589     return _("<corrupt>");
590
591   return printable_section_name (section_headers + ndx);
592 }
593
594 /* Return a pointer to section NAME, or NULL if no such section exists.  */
595
596 static Elf_Internal_Shdr *
597 find_section (const char * name)
598 {
599   unsigned int i;
600
601   for (i = 0; i < elf_header.e_shnum; i++)
602     if (streq (SECTION_NAME (section_headers + i), name))
603       return section_headers + i;
604
605   return NULL;
606 }
607
608 /* Return a pointer to a section containing ADDR, or NULL if no such
609    section exists.  */
610
611 static Elf_Internal_Shdr *
612 find_section_by_address (bfd_vma addr)
613 {
614   unsigned int i;
615
616   for (i = 0; i < elf_header.e_shnum; i++)
617     {
618       Elf_Internal_Shdr *sec = section_headers + i;
619       if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
620         return sec;
621     }
622
623   return NULL;
624 }
625
626 static Elf_Internal_Shdr *
627 find_section_by_type (unsigned int type)
628 {
629   unsigned int i;
630
631   for (i = 0; i < elf_header.e_shnum; i++)
632     {
633       Elf_Internal_Shdr *sec = section_headers + i;
634       if (sec->sh_type == type)
635         return sec;
636     }
637
638   return NULL;
639 }
640
641 /* Return a pointer to section NAME, or NULL if no such section exists,
642    restricted to the list of sections given in SET.  */
643
644 static Elf_Internal_Shdr *
645 find_section_in_set (const char * name, unsigned int * set)
646 {
647   unsigned int i;
648
649   if (set != NULL)
650     {
651       while ((i = *set++) > 0)
652         if (streq (SECTION_NAME (section_headers + i), name))
653           return section_headers + i;
654     }
655
656   return find_section (name);
657 }
658
659 /* Read an unsigned LEB128 encoded value from p.  Set *PLEN to the number of
660    bytes read.  */
661
662 static inline unsigned long
663 read_uleb128 (unsigned char *data,
664               unsigned int *length_return,
665               const unsigned char * const end)
666 {
667   return read_leb128 (data, length_return, FALSE, end);
668 }
669
670 /* Return true if the current file is for IA-64 machine and OpenVMS ABI.
671    This OS has so many departures from the ELF standard that we test it at
672    many places.  */
673
674 static inline int
675 is_ia64_vms (void)
676 {
677   return elf_header.e_machine == EM_IA_64
678     && elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
679 }
680
681 /* Guess the relocation size commonly used by the specific machines.  */
682
683 static int
684 guess_is_rela (unsigned int e_machine)
685 {
686   switch (e_machine)
687     {
688       /* Targets that use REL relocations.  */
689     case EM_386:
690     case EM_486:
691     case EM_960:
692     case EM_ARM:
693     case EM_D10V:
694     case EM_CYGNUS_D10V:
695     case EM_DLX:
696     case EM_MIPS:
697     case EM_MIPS_RS3_LE:
698     case EM_CYGNUS_M32R:
699     case EM_SCORE:
700     case EM_XGATE:
701       return FALSE;
702
703       /* Targets that use RELA relocations.  */
704     case EM_68K:
705     case EM_860:
706     case EM_AARCH64:
707     case EM_ADAPTEVA_EPIPHANY:
708     case EM_ALPHA:
709     case EM_ALTERA_NIOS2:
710     case EM_AVR:
711     case EM_AVR_OLD:
712     case EM_BLACKFIN:
713     case EM_CR16:
714     case EM_CRIS:
715     case EM_CRX:
716     case EM_D30V:
717     case EM_CYGNUS_D30V:
718     case EM_FR30:
719     case EM_CYGNUS_FR30:
720     case EM_CYGNUS_FRV:
721     case EM_H8S:
722     case EM_H8_300:
723     case EM_H8_300H:
724     case EM_IA_64:
725     case EM_IP2K:
726     case EM_IP2K_OLD:
727     case EM_IQ2000:
728     case EM_LATTICEMICO32:
729     case EM_M32C_OLD:
730     case EM_M32C:
731     case EM_M32R:
732     case EM_MCORE:
733     case EM_CYGNUS_MEP:
734     case EM_METAG:
735     case EM_MMIX:
736     case EM_MN10200:
737     case EM_CYGNUS_MN10200:
738     case EM_MN10300:
739     case EM_CYGNUS_MN10300:
740     case EM_MOXIE:
741     case EM_MSP430:
742     case EM_MSP430_OLD:
743     case EM_MT:
744     case EM_NDS32:
745     case EM_NIOS32:
746     case EM_OR1K:
747     case EM_PPC64:
748     case EM_PPC:
749     case EM_RL78:
750     case EM_RX:
751     case EM_S390:
752     case EM_S390_OLD:
753     case EM_SH:
754     case EM_SPARC:
755     case EM_SPARC32PLUS:
756     case EM_SPARCV9:
757     case EM_SPU:
758     case EM_TI_C6000:
759     case EM_TILEGX:
760     case EM_TILEPRO:
761     case EM_V800:
762     case EM_V850:
763     case EM_CYGNUS_V850:
764     case EM_VAX:
765     case EM_X86_64:
766     case EM_L1OM:
767     case EM_K1OM:
768     case EM_XSTORMY16:
769     case EM_XTENSA:
770     case EM_XTENSA_OLD:
771     case EM_MICROBLAZE:
772     case EM_MICROBLAZE_OLD:
773       return TRUE;
774
775     case EM_68HC05:
776     case EM_68HC08:
777     case EM_68HC11:
778     case EM_68HC16:
779     case EM_FX66:
780     case EM_ME16:
781     case EM_MMA:
782     case EM_NCPU:
783     case EM_NDR1:
784     case EM_PCP:
785     case EM_ST100:
786     case EM_ST19:
787     case EM_ST7:
788     case EM_ST9PLUS:
789     case EM_STARCORE:
790     case EM_SVX:
791     case EM_TINYJ:
792     default:
793       warn (_("Don't know about relocations on this machine architecture\n"));
794       return FALSE;
795     }
796 }
797
798 static int
799 slurp_rela_relocs (FILE * file,
800                    unsigned long rel_offset,
801                    unsigned long rel_size,
802                    Elf_Internal_Rela ** relasp,
803                    unsigned long * nrelasp)
804 {
805   Elf_Internal_Rela * relas;
806   size_t nrelas;
807   unsigned int i;
808
809   if (is_32bit_elf)
810     {
811       Elf32_External_Rela * erelas;
812
813       erelas = (Elf32_External_Rela *) get_data (NULL, file, rel_offset, 1,
814                                                  rel_size, _("32-bit relocation data"));
815       if (!erelas)
816         return 0;
817
818       nrelas = rel_size / sizeof (Elf32_External_Rela);
819
820       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
821                                              sizeof (Elf_Internal_Rela));
822
823       if (relas == NULL)
824         {
825           free (erelas);
826           error (_("out of memory parsing relocs\n"));
827           return 0;
828         }
829
830       for (i = 0; i < nrelas; i++)
831         {
832           relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
833           relas[i].r_info   = BYTE_GET (erelas[i].r_info);
834           relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
835         }
836
837       free (erelas);
838     }
839   else
840     {
841       Elf64_External_Rela * erelas;
842
843       erelas = (Elf64_External_Rela *) get_data (NULL, file, rel_offset, 1,
844                                                  rel_size, _("64-bit relocation data"));
845       if (!erelas)
846         return 0;
847
848       nrelas = rel_size / sizeof (Elf64_External_Rela);
849
850       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
851                                              sizeof (Elf_Internal_Rela));
852
853       if (relas == NULL)
854         {
855           free (erelas);
856           error (_("out of memory parsing relocs\n"));
857           return 0;
858         }
859
860       for (i = 0; i < nrelas; i++)
861         {
862           relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
863           relas[i].r_info   = BYTE_GET (erelas[i].r_info);
864           relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
865
866           /* The #ifdef BFD64 below is to prevent a compile time
867              warning.  We know that if we do not have a 64 bit data
868              type that we will never execute this code anyway.  */
869 #ifdef BFD64
870           if (elf_header.e_machine == EM_MIPS
871               && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
872             {
873               /* In little-endian objects, r_info isn't really a
874                  64-bit little-endian value: it has a 32-bit
875                  little-endian symbol index followed by four
876                  individual byte fields.  Reorder INFO
877                  accordingly.  */
878               bfd_vma inf = relas[i].r_info;
879               inf = (((inf & 0xffffffff) << 32)
880                       | ((inf >> 56) & 0xff)
881                       | ((inf >> 40) & 0xff00)
882                       | ((inf >> 24) & 0xff0000)
883                       | ((inf >> 8) & 0xff000000));
884               relas[i].r_info = inf;
885             }
886 #endif /* BFD64 */
887         }
888
889       free (erelas);
890     }
891   *relasp = relas;
892   *nrelasp = nrelas;
893   return 1;
894 }
895
896 static int
897 slurp_rel_relocs (FILE * file,
898                   unsigned long rel_offset,
899                   unsigned long rel_size,
900                   Elf_Internal_Rela ** relsp,
901                   unsigned long * nrelsp)
902 {
903   Elf_Internal_Rela * rels;
904   size_t nrels;
905   unsigned int i;
906
907   if (is_32bit_elf)
908     {
909       Elf32_External_Rel * erels;
910
911       erels = (Elf32_External_Rel *) get_data (NULL, file, rel_offset, 1,
912                                                rel_size, _("32-bit relocation data"));
913       if (!erels)
914         return 0;
915
916       nrels = rel_size / sizeof (Elf32_External_Rel);
917
918       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
919
920       if (rels == NULL)
921         {
922           free (erels);
923           error (_("out of memory parsing relocs\n"));
924           return 0;
925         }
926
927       for (i = 0; i < nrels; i++)
928         {
929           rels[i].r_offset = BYTE_GET (erels[i].r_offset);
930           rels[i].r_info   = BYTE_GET (erels[i].r_info);
931           rels[i].r_addend = 0;
932         }
933
934       free (erels);
935     }
936   else
937     {
938       Elf64_External_Rel * erels;
939
940       erels = (Elf64_External_Rel *) get_data (NULL, file, rel_offset, 1,
941                                                rel_size, _("64-bit relocation data"));
942       if (!erels)
943         return 0;
944
945       nrels = rel_size / sizeof (Elf64_External_Rel);
946
947       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
948
949       if (rels == NULL)
950         {
951           free (erels);
952           error (_("out of memory parsing relocs\n"));
953           return 0;
954         }
955
956       for (i = 0; i < nrels; i++)
957         {
958           rels[i].r_offset = BYTE_GET (erels[i].r_offset);
959           rels[i].r_info   = BYTE_GET (erels[i].r_info);
960           rels[i].r_addend = 0;
961
962           /* The #ifdef BFD64 below is to prevent a compile time
963              warning.  We know that if we do not have a 64 bit data
964              type that we will never execute this code anyway.  */
965 #ifdef BFD64
966           if (elf_header.e_machine == EM_MIPS
967               && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
968             {
969               /* In little-endian objects, r_info isn't really a
970                  64-bit little-endian value: it has a 32-bit
971                  little-endian symbol index followed by four
972                  individual byte fields.  Reorder INFO
973                  accordingly.  */
974               bfd_vma inf = rels[i].r_info;
975               inf = (((inf & 0xffffffff) << 32)
976                      | ((inf >> 56) & 0xff)
977                      | ((inf >> 40) & 0xff00)
978                      | ((inf >> 24) & 0xff0000)
979                      | ((inf >> 8) & 0xff000000));
980               rels[i].r_info = inf;
981             }
982 #endif /* BFD64 */
983         }
984
985       free (erels);
986     }
987   *relsp = rels;
988   *nrelsp = nrels;
989   return 1;
990 }
991
992 /* Returns the reloc type extracted from the reloc info field.  */
993
994 static unsigned int
995 get_reloc_type (bfd_vma reloc_info)
996 {
997   if (is_32bit_elf)
998     return ELF32_R_TYPE (reloc_info);
999
1000   switch (elf_header.e_machine)
1001     {
1002     case EM_MIPS:
1003       /* Note: We assume that reloc_info has already been adjusted for us.  */
1004       return ELF64_MIPS_R_TYPE (reloc_info);
1005
1006     case EM_SPARCV9:
1007       return ELF64_R_TYPE_ID (reloc_info);
1008
1009     default:
1010       return ELF64_R_TYPE (reloc_info);
1011     }
1012 }
1013
1014 /* Return the symbol index extracted from the reloc info field.  */
1015
1016 static bfd_vma
1017 get_reloc_symindex (bfd_vma reloc_info)
1018 {
1019   return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1020 }
1021
1022 static inline bfd_boolean
1023 uses_msp430x_relocs (void)
1024 {
1025   return
1026     elf_header.e_machine == EM_MSP430 /* Paranoia.  */
1027     /* GCC uses osabi == ELFOSBI_STANDALONE.  */
1028     && (((elf_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1029         /* TI compiler uses ELFOSABI_NONE.  */
1030         || (elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1031 }
1032
1033 /* Display the contents of the relocation data found at the specified
1034    offset.  */
1035
1036 static void
1037 dump_relocations (FILE * file,
1038                   unsigned long rel_offset,
1039                   unsigned long rel_size,
1040                   Elf_Internal_Sym * symtab,
1041                   unsigned long nsyms,
1042                   char * strtab,
1043                   unsigned long strtablen,
1044                   int is_rela)
1045 {
1046   unsigned int i;
1047   Elf_Internal_Rela * rels;
1048
1049   if (is_rela == UNKNOWN)
1050     is_rela = guess_is_rela (elf_header.e_machine);
1051
1052   if (is_rela)
1053     {
1054       if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
1055         return;
1056     }
1057   else
1058     {
1059       if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
1060         return;
1061     }
1062
1063   if (is_32bit_elf)
1064     {
1065       if (is_rela)
1066         {
1067           if (do_wide)
1068             printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name + Addend\n"));
1069           else
1070             printf (_(" Offset     Info    Type            Sym.Value  Sym. Name + Addend\n"));
1071         }
1072       else
1073         {
1074           if (do_wide)
1075             printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name\n"));
1076           else
1077             printf (_(" Offset     Info    Type            Sym.Value  Sym. Name\n"));
1078         }
1079     }
1080   else
1081     {
1082       if (is_rela)
1083         {
1084           if (do_wide)
1085             printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend\n"));
1086           else
1087             printf (_("  Offset          Info           Type           Sym. Value    Sym. Name + Addend\n"));
1088         }
1089       else
1090         {
1091           if (do_wide)
1092             printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name\n"));
1093           else
1094             printf (_("  Offset          Info           Type           Sym. Value    Sym. Name\n"));
1095         }
1096     }
1097
1098   for (i = 0; i < rel_size; i++)
1099     {
1100       const char * rtype;
1101       bfd_vma offset;
1102       bfd_vma inf;
1103       bfd_vma symtab_index;
1104       bfd_vma type;
1105
1106       offset = rels[i].r_offset;
1107       inf    = rels[i].r_info;
1108
1109       type = get_reloc_type (inf);
1110       symtab_index = get_reloc_symindex  (inf);
1111
1112       if (is_32bit_elf)
1113         {
1114           printf ("%8.8lx  %8.8lx ",
1115                   (unsigned long) offset & 0xffffffff,
1116                   (unsigned long) inf & 0xffffffff);
1117         }
1118       else
1119         {
1120 #if BFD_HOST_64BIT_LONG
1121           printf (do_wide
1122                   ? "%16.16lx  %16.16lx "
1123                   : "%12.12lx  %12.12lx ",
1124                   offset, inf);
1125 #elif BFD_HOST_64BIT_LONG_LONG
1126 #ifndef __MSVCRT__
1127           printf (do_wide
1128                   ? "%16.16llx  %16.16llx "
1129                   : "%12.12llx  %12.12llx ",
1130                   offset, inf);
1131 #else
1132           printf (do_wide
1133                   ? "%16.16I64x  %16.16I64x "
1134                   : "%12.12I64x  %12.12I64x ",
1135                   offset, inf);
1136 #endif
1137 #else
1138           printf (do_wide
1139                   ? "%8.8lx%8.8lx  %8.8lx%8.8lx "
1140                   : "%4.4lx%8.8lx  %4.4lx%8.8lx ",
1141                   _bfd_int64_high (offset),
1142                   _bfd_int64_low (offset),
1143                   _bfd_int64_high (inf),
1144                   _bfd_int64_low (inf));
1145 #endif
1146         }
1147
1148       switch (elf_header.e_machine)
1149         {
1150         default:
1151           rtype = NULL;
1152           break;
1153
1154         case EM_AARCH64:
1155           rtype = elf_aarch64_reloc_type (type);
1156           break;
1157
1158         case EM_M32R:
1159         case EM_CYGNUS_M32R:
1160           rtype = elf_m32r_reloc_type (type);
1161           break;
1162
1163         case EM_386:
1164         case EM_486:
1165           rtype = elf_i386_reloc_type (type);
1166           break;
1167
1168         case EM_68HC11:
1169         case EM_68HC12:
1170           rtype = elf_m68hc11_reloc_type (type);
1171           break;
1172
1173         case EM_68K:
1174           rtype = elf_m68k_reloc_type (type);
1175           break;
1176
1177         case EM_960:
1178           rtype = elf_i960_reloc_type (type);
1179           break;
1180
1181         case EM_AVR:
1182         case EM_AVR_OLD:
1183           rtype = elf_avr_reloc_type (type);
1184           break;
1185
1186         case EM_OLD_SPARCV9:
1187         case EM_SPARC32PLUS:
1188         case EM_SPARCV9:
1189         case EM_SPARC:
1190           rtype = elf_sparc_reloc_type (type);
1191           break;
1192
1193         case EM_SPU:
1194           rtype = elf_spu_reloc_type (type);
1195           break;
1196
1197         case EM_V800:
1198           rtype = v800_reloc_type (type);
1199           break;
1200         case EM_V850:
1201         case EM_CYGNUS_V850:
1202           rtype = v850_reloc_type (type);
1203           break;
1204
1205         case EM_D10V:
1206         case EM_CYGNUS_D10V:
1207           rtype = elf_d10v_reloc_type (type);
1208           break;
1209
1210         case EM_D30V:
1211         case EM_CYGNUS_D30V:
1212           rtype = elf_d30v_reloc_type (type);
1213           break;
1214
1215         case EM_DLX:
1216           rtype = elf_dlx_reloc_type (type);
1217           break;
1218
1219         case EM_SH:
1220           rtype = elf_sh_reloc_type (type);
1221           break;
1222
1223         case EM_MN10300:
1224         case EM_CYGNUS_MN10300:
1225           rtype = elf_mn10300_reloc_type (type);
1226           break;
1227
1228         case EM_MN10200:
1229         case EM_CYGNUS_MN10200:
1230           rtype = elf_mn10200_reloc_type (type);
1231           break;
1232
1233         case EM_FR30:
1234         case EM_CYGNUS_FR30:
1235           rtype = elf_fr30_reloc_type (type);
1236           break;
1237
1238         case EM_CYGNUS_FRV:
1239           rtype = elf_frv_reloc_type (type);
1240           break;
1241
1242         case EM_MCORE:
1243           rtype = elf_mcore_reloc_type (type);
1244           break;
1245
1246         case EM_MMIX:
1247           rtype = elf_mmix_reloc_type (type);
1248           break;
1249
1250         case EM_MOXIE:
1251           rtype = elf_moxie_reloc_type (type);
1252           break;
1253
1254         case EM_MSP430:
1255           if (uses_msp430x_relocs ())
1256             {
1257               rtype = elf_msp430x_reloc_type (type);
1258               break;
1259             }
1260         case EM_MSP430_OLD:
1261           rtype = elf_msp430_reloc_type (type);
1262           break;
1263
1264         case EM_NDS32:
1265           rtype = elf_nds32_reloc_type (type);
1266           break;
1267
1268         case EM_PPC:
1269           rtype = elf_ppc_reloc_type (type);
1270           break;
1271
1272         case EM_PPC64:
1273           rtype = elf_ppc64_reloc_type (type);
1274           break;
1275
1276         case EM_MIPS:
1277         case EM_MIPS_RS3_LE:
1278           rtype = elf_mips_reloc_type (type);
1279           break;
1280
1281         case EM_ALPHA:
1282           rtype = elf_alpha_reloc_type (type);
1283           break;
1284
1285         case EM_ARM:
1286           rtype = elf_arm_reloc_type (type);
1287           break;
1288
1289         case EM_ARC:
1290           rtype = elf_arc_reloc_type (type);
1291           break;
1292
1293         case EM_PARISC:
1294           rtype = elf_hppa_reloc_type (type);
1295           break;
1296
1297         case EM_H8_300:
1298         case EM_H8_300H:
1299         case EM_H8S:
1300           rtype = elf_h8_reloc_type (type);
1301           break;
1302
1303         case EM_OR1K:
1304           rtype = elf_or1k_reloc_type (type);
1305           break;
1306
1307         case EM_PJ:
1308         case EM_PJ_OLD:
1309           rtype = elf_pj_reloc_type (type);
1310           break;
1311         case EM_IA_64:
1312           rtype = elf_ia64_reloc_type (type);
1313           break;
1314
1315         case EM_CRIS:
1316           rtype = elf_cris_reloc_type (type);
1317           break;
1318
1319         case EM_860:
1320           rtype = elf_i860_reloc_type (type);
1321           break;
1322
1323         case EM_X86_64:
1324         case EM_L1OM:
1325         case EM_K1OM:
1326           rtype = elf_x86_64_reloc_type (type);
1327           break;
1328
1329         case EM_S370:
1330           rtype = i370_reloc_type (type);
1331           break;
1332
1333         case EM_S390_OLD:
1334         case EM_S390:
1335           rtype = elf_s390_reloc_type (type);
1336           break;
1337
1338         case EM_SCORE:
1339           rtype = elf_score_reloc_type (type);
1340           break;
1341
1342         case EM_XSTORMY16:
1343           rtype = elf_xstormy16_reloc_type (type);
1344           break;
1345
1346         case EM_CRX:
1347           rtype = elf_crx_reloc_type (type);
1348           break;
1349
1350         case EM_VAX:
1351           rtype = elf_vax_reloc_type (type);
1352           break;
1353
1354         case EM_ADAPTEVA_EPIPHANY:
1355           rtype = elf_epiphany_reloc_type (type);
1356           break;
1357
1358         case EM_IP2K:
1359         case EM_IP2K_OLD:
1360           rtype = elf_ip2k_reloc_type (type);
1361           break;
1362
1363         case EM_IQ2000:
1364           rtype = elf_iq2000_reloc_type (type);
1365           break;
1366
1367         case EM_XTENSA_OLD:
1368         case EM_XTENSA:
1369           rtype = elf_xtensa_reloc_type (type);
1370           break;
1371
1372         case EM_LATTICEMICO32:
1373           rtype = elf_lm32_reloc_type (type);
1374           break;
1375
1376         case EM_M32C_OLD:
1377         case EM_M32C:
1378           rtype = elf_m32c_reloc_type (type);
1379           break;
1380
1381         case EM_MT:
1382           rtype = elf_mt_reloc_type (type);
1383           break;
1384
1385         case EM_BLACKFIN:
1386           rtype = elf_bfin_reloc_type (type);
1387           break;
1388
1389         case EM_CYGNUS_MEP:
1390           rtype = elf_mep_reloc_type (type);
1391           break;
1392
1393         case EM_CR16:
1394           rtype = elf_cr16_reloc_type (type);
1395           break;
1396
1397         case EM_MICROBLAZE:
1398         case EM_MICROBLAZE_OLD:
1399           rtype = elf_microblaze_reloc_type (type);
1400           break;
1401
1402         case EM_RL78:
1403           rtype = elf_rl78_reloc_type (type);
1404           break;
1405
1406         case EM_RX:
1407           rtype = elf_rx_reloc_type (type);
1408           break;
1409
1410         case EM_METAG:
1411           rtype = elf_metag_reloc_type (type);
1412           break;
1413
1414         case EM_XC16X:
1415         case EM_C166:
1416           rtype = elf_xc16x_reloc_type (type);
1417           break;
1418
1419         case EM_TI_C6000:
1420           rtype = elf_tic6x_reloc_type (type);
1421           break;
1422
1423         case EM_TILEGX:
1424           rtype = elf_tilegx_reloc_type (type);
1425           break;
1426
1427         case EM_TILEPRO:
1428           rtype = elf_tilepro_reloc_type (type);
1429           break;
1430
1431         case EM_XGATE:
1432           rtype = elf_xgate_reloc_type (type);
1433           break;
1434
1435         case EM_ALTERA_NIOS2:
1436           rtype = elf_nios2_reloc_type (type);
1437           break;
1438         }
1439
1440       if (rtype == NULL)
1441         printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1442       else
1443         printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1444
1445       if (elf_header.e_machine == EM_ALPHA
1446           && rtype != NULL
1447           && streq (rtype, "R_ALPHA_LITUSE")
1448           && is_rela)
1449         {
1450           switch (rels[i].r_addend)
1451             {
1452             case LITUSE_ALPHA_ADDR:   rtype = "ADDR";   break;
1453             case LITUSE_ALPHA_BASE:   rtype = "BASE";   break;
1454             case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1455             case LITUSE_ALPHA_JSR:    rtype = "JSR";    break;
1456             case LITUSE_ALPHA_TLSGD:  rtype = "TLSGD";  break;
1457             case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1458             case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1459             default: rtype = NULL;
1460             }
1461           if (rtype)
1462             printf (" (%s)", rtype);
1463           else
1464             {
1465               putchar (' ');
1466               printf (_("<unknown addend: %lx>"),
1467                       (unsigned long) rels[i].r_addend);
1468             }
1469         }
1470       else if (symtab_index)
1471         {
1472           if (symtab == NULL || symtab_index >= nsyms)
1473             printf (_(" bad symbol index: %08lx"), (unsigned long) symtab_index);
1474           else
1475             {
1476               Elf_Internal_Sym * psym;
1477
1478               psym = symtab + symtab_index;
1479
1480               printf (" ");
1481
1482               if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1483                 {
1484                   const char * name;
1485                   unsigned int len;
1486                   unsigned int width = is_32bit_elf ? 8 : 14;
1487
1488                   /* Relocations against GNU_IFUNC symbols do not use the value
1489                      of the symbol as the address to relocate against.  Instead
1490                      they invoke the function named by the symbol and use its
1491                      result as the address for relocation.
1492
1493                      To indicate this to the user, do not display the value of
1494                      the symbol in the "Symbols's Value" field.  Instead show
1495                      its name followed by () as a hint that the symbol is
1496                      invoked.  */
1497
1498                   if (strtab == NULL
1499                       || psym->st_name == 0
1500                       || psym->st_name >= strtablen)
1501                     name = "??";
1502                   else
1503                     name = strtab + psym->st_name;
1504
1505                   len = print_symbol (width, name);
1506                   printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1507                 }
1508               else
1509                 {
1510                   print_vma (psym->st_value, LONG_HEX);
1511
1512                   printf (is_32bit_elf ? "   " : " ");
1513                 }
1514
1515               if (psym->st_name == 0)
1516                 {
1517                   const char * sec_name = "<null>";
1518                   char name_buf[40];
1519
1520                   if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1521                     {
1522                       if (psym->st_shndx < elf_header.e_shnum)
1523                         sec_name = SECTION_NAME (section_headers + psym->st_shndx);
1524                       else if (psym->st_shndx == SHN_ABS)
1525                         sec_name = "ABS";
1526                       else if (psym->st_shndx == SHN_COMMON)
1527                         sec_name = "COMMON";
1528                       else if ((elf_header.e_machine == EM_MIPS
1529                                 && psym->st_shndx == SHN_MIPS_SCOMMON)
1530                                || (elf_header.e_machine == EM_TI_C6000
1531                                    && psym->st_shndx == SHN_TIC6X_SCOMMON))
1532                         sec_name = "SCOMMON";
1533                       else if (elf_header.e_machine == EM_MIPS
1534                                && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1535                         sec_name = "SUNDEF";
1536                       else if ((elf_header.e_machine == EM_X86_64
1537                                 || elf_header.e_machine == EM_L1OM
1538                                 || elf_header.e_machine == EM_K1OM)
1539                                && psym->st_shndx == SHN_X86_64_LCOMMON)
1540                         sec_name = "LARGE_COMMON";
1541                       else if (elf_header.e_machine == EM_IA_64
1542                                && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1543                                && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1544                         sec_name = "ANSI_COM";
1545                       else if (is_ia64_vms ()
1546                                && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1547                         sec_name = "VMS_SYMVEC";
1548                       else
1549                         {
1550                           sprintf (name_buf, "<section 0x%x>",
1551                                    (unsigned int) psym->st_shndx);
1552                           sec_name = name_buf;
1553                         }
1554                     }
1555                   print_symbol (22, sec_name);
1556                 }
1557               else if (strtab == NULL)
1558                 printf (_("<string table index: %3ld>"), psym->st_name);
1559               else if (psym->st_name >= strtablen)
1560                 printf (_("<corrupt string table index: %3ld>"), psym->st_name);
1561               else
1562                 print_symbol (22, strtab + psym->st_name);
1563
1564               if (is_rela)
1565                 {
1566                   bfd_signed_vma off = rels[i].r_addend;
1567
1568                   /* PR 17531: file: 2e63226f.  */
1569                   if (off == ((bfd_signed_vma) 1) << ((sizeof (bfd_signed_vma) * 8) - 1))
1570                     printf (" + %" BFD_VMA_FMT "x", off);
1571                   else if (off < 0)
1572                     printf (" - %" BFD_VMA_FMT "x", - off);
1573                   else
1574                     printf (" + %" BFD_VMA_FMT "x", off);
1575                 }
1576             }
1577         }
1578       else if (is_rela)
1579         {
1580           bfd_signed_vma off = rels[i].r_addend;
1581
1582           printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1583           /* PR 17531: file: 2e63226f.  */
1584           if (off == ((bfd_signed_vma) 1) << ((sizeof (bfd_signed_vma) * 8) - 1))
1585             printf ("%" BFD_VMA_FMT "x", off);
1586           else if (off < 0)
1587             printf ("-%" BFD_VMA_FMT "x", - off);
1588           else
1589             printf ("%" BFD_VMA_FMT "x", off);
1590         }
1591
1592       if (elf_header.e_machine == EM_SPARCV9
1593           && rtype != NULL
1594           && streq (rtype, "R_SPARC_OLO10"))
1595         printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1596
1597       putchar ('\n');
1598
1599 #ifdef BFD64
1600       if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1601         {
1602           bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1603           bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1604           const char * rtype2 = elf_mips_reloc_type (type2);
1605           const char * rtype3 = elf_mips_reloc_type (type3);
1606
1607           printf ("                    Type2: ");
1608
1609           if (rtype2 == NULL)
1610             printf (_("unrecognized: %-7lx"),
1611                     (unsigned long) type2 & 0xffffffff);
1612           else
1613             printf ("%-17.17s", rtype2);
1614
1615           printf ("\n                    Type3: ");
1616
1617           if (rtype3 == NULL)
1618             printf (_("unrecognized: %-7lx"),
1619                     (unsigned long) type3 & 0xffffffff);
1620           else
1621             printf ("%-17.17s", rtype3);
1622
1623           putchar ('\n');
1624         }
1625 #endif /* BFD64 */
1626     }
1627
1628   free (rels);
1629 }
1630
1631 static const char *
1632 get_mips_dynamic_type (unsigned long type)
1633 {
1634   switch (type)
1635     {
1636     case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1637     case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1638     case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1639     case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1640     case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1641     case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1642     case DT_MIPS_MSYM: return "MIPS_MSYM";
1643     case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1644     case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1645     case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1646     case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1647     case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1648     case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1649     case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1650     case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1651     case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1652     case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1653     case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1654     case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1655     case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1656     case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1657     case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1658     case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1659     case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1660     case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1661     case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1662     case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1663     case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1664     case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1665     case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1666     case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1667     case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1668     case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1669     case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1670     case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1671     case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1672     case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1673     case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1674     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1675     case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1676     case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1677     case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1678     case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1679     case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1680     case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1681     default:
1682       return NULL;
1683     }
1684 }
1685
1686 static const char *
1687 get_sparc64_dynamic_type (unsigned long type)
1688 {
1689   switch (type)
1690     {
1691     case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1692     default:
1693       return NULL;
1694     }
1695 }
1696
1697 static const char *
1698 get_ppc_dynamic_type (unsigned long type)
1699 {
1700   switch (type)
1701     {
1702     case DT_PPC_GOT:    return "PPC_GOT";
1703     case DT_PPC_OPT:    return "PPC_OPT";
1704     default:
1705       return NULL;
1706     }
1707 }
1708
1709 static const char *
1710 get_ppc64_dynamic_type (unsigned long type)
1711 {
1712   switch (type)
1713     {
1714     case DT_PPC64_GLINK:  return "PPC64_GLINK";
1715     case DT_PPC64_OPD:    return "PPC64_OPD";
1716     case DT_PPC64_OPDSZ:  return "PPC64_OPDSZ";
1717     case DT_PPC64_OPT:    return "PPC64_OPT";
1718     default:
1719       return NULL;
1720     }
1721 }
1722
1723 static const char *
1724 get_parisc_dynamic_type (unsigned long type)
1725 {
1726   switch (type)
1727     {
1728     case DT_HP_LOAD_MAP:        return "HP_LOAD_MAP";
1729     case DT_HP_DLD_FLAGS:       return "HP_DLD_FLAGS";
1730     case DT_HP_DLD_HOOK:        return "HP_DLD_HOOK";
1731     case DT_HP_UX10_INIT:       return "HP_UX10_INIT";
1732     case DT_HP_UX10_INITSZ:     return "HP_UX10_INITSZ";
1733     case DT_HP_PREINIT:         return "HP_PREINIT";
1734     case DT_HP_PREINITSZ:       return "HP_PREINITSZ";
1735     case DT_HP_NEEDED:          return "HP_NEEDED";
1736     case DT_HP_TIME_STAMP:      return "HP_TIME_STAMP";
1737     case DT_HP_CHECKSUM:        return "HP_CHECKSUM";
1738     case DT_HP_GST_SIZE:        return "HP_GST_SIZE";
1739     case DT_HP_GST_VERSION:     return "HP_GST_VERSION";
1740     case DT_HP_GST_HASHVAL:     return "HP_GST_HASHVAL";
1741     case DT_HP_EPLTREL:         return "HP_GST_EPLTREL";
1742     case DT_HP_EPLTRELSZ:       return "HP_GST_EPLTRELSZ";
1743     case DT_HP_FILTERED:        return "HP_FILTERED";
1744     case DT_HP_FILTER_TLS:      return "HP_FILTER_TLS";
1745     case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1746     case DT_HP_LAZYLOAD:        return "HP_LAZYLOAD";
1747     case DT_HP_BIND_NOW_COUNT:  return "HP_BIND_NOW_COUNT";
1748     case DT_PLT:                return "PLT";
1749     case DT_PLT_SIZE:           return "PLT_SIZE";
1750     case DT_DLT:                return "DLT";
1751     case DT_DLT_SIZE:           return "DLT_SIZE";
1752     default:
1753       return NULL;
1754     }
1755 }
1756
1757 static const char *
1758 get_ia64_dynamic_type (unsigned long type)
1759 {
1760   switch (type)
1761     {
1762     case DT_IA_64_PLT_RESERVE:         return "IA_64_PLT_RESERVE";
1763     case DT_IA_64_VMS_SUBTYPE:         return "VMS_SUBTYPE";
1764     case DT_IA_64_VMS_IMGIOCNT:        return "VMS_IMGIOCNT";
1765     case DT_IA_64_VMS_LNKFLAGS:        return "VMS_LNKFLAGS";
1766     case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1767     case DT_IA_64_VMS_IDENT:           return "VMS_IDENT";
1768     case DT_IA_64_VMS_NEEDED_IDENT:    return "VMS_NEEDED_IDENT";
1769     case DT_IA_64_VMS_IMG_RELA_CNT:    return "VMS_IMG_RELA_CNT";
1770     case DT_IA_64_VMS_SEG_RELA_CNT:    return "VMS_SEG_RELA_CNT";
1771     case DT_IA_64_VMS_FIXUP_RELA_CNT:  return "VMS_FIXUP_RELA_CNT";
1772     case DT_IA_64_VMS_FIXUP_NEEDED:    return "VMS_FIXUP_NEEDED";
1773     case DT_IA_64_VMS_SYMVEC_CNT:      return "VMS_SYMVEC_CNT";
1774     case DT_IA_64_VMS_XLATED:          return "VMS_XLATED";
1775     case DT_IA_64_VMS_STACKSIZE:       return "VMS_STACKSIZE";
1776     case DT_IA_64_VMS_UNWINDSZ:        return "VMS_UNWINDSZ";
1777     case DT_IA_64_VMS_UNWIND_CODSEG:   return "VMS_UNWIND_CODSEG";
1778     case DT_IA_64_VMS_UNWIND_INFOSEG:  return "VMS_UNWIND_INFOSEG";
1779     case DT_IA_64_VMS_LINKTIME:        return "VMS_LINKTIME";
1780     case DT_IA_64_VMS_SEG_NO:          return "VMS_SEG_NO";
1781     case DT_IA_64_VMS_SYMVEC_OFFSET:   return "VMS_SYMVEC_OFFSET";
1782     case DT_IA_64_VMS_SYMVEC_SEG:      return "VMS_SYMVEC_SEG";
1783     case DT_IA_64_VMS_UNWIND_OFFSET:   return "VMS_UNWIND_OFFSET";
1784     case DT_IA_64_VMS_UNWIND_SEG:      return "VMS_UNWIND_SEG";
1785     case DT_IA_64_VMS_STRTAB_OFFSET:   return "VMS_STRTAB_OFFSET";
1786     case DT_IA_64_VMS_SYSVER_OFFSET:   return "VMS_SYSVER_OFFSET";
1787     case DT_IA_64_VMS_IMG_RELA_OFF:    return "VMS_IMG_RELA_OFF";
1788     case DT_IA_64_VMS_SEG_RELA_OFF:    return "VMS_SEG_RELA_OFF";
1789     case DT_IA_64_VMS_FIXUP_RELA_OFF:  return "VMS_FIXUP_RELA_OFF";
1790     case DT_IA_64_VMS_PLTGOT_OFFSET:   return "VMS_PLTGOT_OFFSET";
1791     case DT_IA_64_VMS_PLTGOT_SEG:      return "VMS_PLTGOT_SEG";
1792     case DT_IA_64_VMS_FPMODE:          return "VMS_FPMODE";
1793     default:
1794       return NULL;
1795     }
1796 }
1797
1798 static const char *
1799 get_alpha_dynamic_type (unsigned long type)
1800 {
1801   switch (type)
1802     {
1803     case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1804     default:
1805       return NULL;
1806     }
1807 }
1808
1809 static const char *
1810 get_score_dynamic_type (unsigned long type)
1811 {
1812   switch (type)
1813     {
1814     case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
1815     case DT_SCORE_LOCAL_GOTNO:  return "SCORE_LOCAL_GOTNO";
1816     case DT_SCORE_SYMTABNO:     return "SCORE_SYMTABNO";
1817     case DT_SCORE_GOTSYM:       return "SCORE_GOTSYM";
1818     case DT_SCORE_UNREFEXTNO:   return "SCORE_UNREFEXTNO";
1819     case DT_SCORE_HIPAGENO:     return "SCORE_HIPAGENO";
1820     default:
1821       return NULL;
1822     }
1823 }
1824
1825 static const char *
1826 get_tic6x_dynamic_type (unsigned long type)
1827 {
1828   switch (type)
1829     {
1830     case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
1831     case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
1832     case DT_C6000_DSBT_BASE:   return "C6000_DSBT_BASE";
1833     case DT_C6000_DSBT_SIZE:   return "C6000_DSBT_SIZE";
1834     case DT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
1835     case DT_C6000_DSBT_INDEX:  return "C6000_DSBT_INDEX";
1836     default:
1837       return NULL;
1838     }
1839 }
1840
1841 static const char *
1842 get_nios2_dynamic_type (unsigned long type)
1843 {
1844   switch (type)
1845     {
1846     case DT_NIOS2_GP: return "NIOS2_GP";
1847     default:
1848       return NULL;
1849     }
1850 }
1851
1852 static const char *
1853 get_dynamic_type (unsigned long type)
1854 {
1855   static char buff[64];
1856
1857   switch (type)
1858     {
1859     case DT_NULL:       return "NULL";
1860     case DT_NEEDED:     return "NEEDED";
1861     case DT_PLTRELSZ:   return "PLTRELSZ";
1862     case DT_PLTGOT:     return "PLTGOT";
1863     case DT_HASH:       return "HASH";
1864     case DT_STRTAB:     return "STRTAB";
1865     case DT_SYMTAB:     return "SYMTAB";
1866     case DT_RELA:       return "RELA";
1867     case DT_RELASZ:     return "RELASZ";
1868     case DT_RELAENT:    return "RELAENT";
1869     case DT_STRSZ:      return "STRSZ";
1870     case DT_SYMENT:     return "SYMENT";
1871     case DT_INIT:       return "INIT";
1872     case DT_FINI:       return "FINI";
1873     case DT_SONAME:     return "SONAME";
1874     case DT_RPATH:      return "RPATH";
1875     case DT_SYMBOLIC:   return "SYMBOLIC";
1876     case DT_REL:        return "REL";
1877     case DT_RELSZ:      return "RELSZ";
1878     case DT_RELENT:     return "RELENT";
1879     case DT_PLTREL:     return "PLTREL";
1880     case DT_DEBUG:      return "DEBUG";
1881     case DT_TEXTREL:    return "TEXTREL";
1882     case DT_JMPREL:     return "JMPREL";
1883     case DT_BIND_NOW:   return "BIND_NOW";
1884     case DT_INIT_ARRAY: return "INIT_ARRAY";
1885     case DT_FINI_ARRAY: return "FINI_ARRAY";
1886     case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
1887     case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
1888     case DT_RUNPATH:    return "RUNPATH";
1889     case DT_FLAGS:      return "FLAGS";
1890
1891     case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
1892     case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
1893
1894     case DT_CHECKSUM:   return "CHECKSUM";
1895     case DT_PLTPADSZ:   return "PLTPADSZ";
1896     case DT_MOVEENT:    return "MOVEENT";
1897     case DT_MOVESZ:     return "MOVESZ";
1898     case DT_FEATURE:    return "FEATURE";
1899     case DT_POSFLAG_1:  return "POSFLAG_1";
1900     case DT_SYMINSZ:    return "SYMINSZ";
1901     case DT_SYMINENT:   return "SYMINENT"; /* aka VALRNGHI */
1902
1903     case DT_ADDRRNGLO:  return "ADDRRNGLO";
1904     case DT_CONFIG:     return "CONFIG";
1905     case DT_DEPAUDIT:   return "DEPAUDIT";
1906     case DT_AUDIT:      return "AUDIT";
1907     case DT_PLTPAD:     return "PLTPAD";
1908     case DT_MOVETAB:    return "MOVETAB";
1909     case DT_SYMINFO:    return "SYMINFO"; /* aka ADDRRNGHI */
1910
1911     case DT_VERSYM:     return "VERSYM";
1912
1913     case DT_TLSDESC_GOT: return "TLSDESC_GOT";
1914     case DT_TLSDESC_PLT: return "TLSDESC_PLT";
1915     case DT_RELACOUNT:  return "RELACOUNT";
1916     case DT_RELCOUNT:   return "RELCOUNT";
1917     case DT_FLAGS_1:    return "FLAGS_1";
1918     case DT_VERDEF:     return "VERDEF";
1919     case DT_VERDEFNUM:  return "VERDEFNUM";
1920     case DT_VERNEED:    return "VERNEED";
1921     case DT_VERNEEDNUM: return "VERNEEDNUM";
1922
1923     case DT_AUXILIARY:  return "AUXILIARY";
1924     case DT_USED:       return "USED";
1925     case DT_FILTER:     return "FILTER";
1926
1927     case DT_GNU_PRELINKED: return "GNU_PRELINKED";
1928     case DT_GNU_CONFLICT: return "GNU_CONFLICT";
1929     case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
1930     case DT_GNU_LIBLIST: return "GNU_LIBLIST";
1931     case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
1932     case DT_GNU_HASH:   return "GNU_HASH";
1933
1934     default:
1935       if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
1936         {
1937           const char * result;
1938
1939           switch (elf_header.e_machine)
1940             {
1941             case EM_MIPS:
1942             case EM_MIPS_RS3_LE:
1943               result = get_mips_dynamic_type (type);
1944               break;
1945             case EM_SPARCV9:
1946               result = get_sparc64_dynamic_type (type);
1947               break;
1948             case EM_PPC:
1949               result = get_ppc_dynamic_type (type);
1950               break;
1951             case EM_PPC64:
1952               result = get_ppc64_dynamic_type (type);
1953               break;
1954             case EM_IA_64:
1955               result = get_ia64_dynamic_type (type);
1956               break;
1957             case EM_ALPHA:
1958               result = get_alpha_dynamic_type (type);
1959               break;
1960             case EM_SCORE:
1961               result = get_score_dynamic_type (type);
1962               break;
1963             case EM_TI_C6000:
1964               result = get_tic6x_dynamic_type (type);
1965               break;
1966             case EM_ALTERA_NIOS2:
1967               result = get_nios2_dynamic_type (type);
1968               break;
1969             default:
1970               result = NULL;
1971               break;
1972             }
1973
1974           if (result != NULL)
1975             return result;
1976
1977           snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
1978         }
1979       else if (((type >= DT_LOOS) && (type <= DT_HIOS))
1980                || (elf_header.e_machine == EM_PARISC
1981                    && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
1982         {
1983           const char * result;
1984
1985           switch (elf_header.e_machine)
1986             {
1987             case EM_PARISC:
1988               result = get_parisc_dynamic_type (type);
1989               break;
1990             case EM_IA_64:
1991               result = get_ia64_dynamic_type (type);
1992               break;
1993             default:
1994               result = NULL;
1995               break;
1996             }
1997
1998           if (result != NULL)
1999             return result;
2000
2001           snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2002                     type);
2003         }
2004       else
2005         snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2006
2007       return buff;
2008     }
2009 }
2010
2011 static char *
2012 get_file_type (unsigned e_type)
2013 {
2014   static char buff[32];
2015
2016   switch (e_type)
2017     {
2018     case ET_NONE:       return _("NONE (None)");
2019     case ET_REL:        return _("REL (Relocatable file)");
2020     case ET_EXEC:       return _("EXEC (Executable file)");
2021     case ET_DYN:        return _("DYN (Shared object file)");
2022     case ET_CORE:       return _("CORE (Core file)");
2023
2024     default:
2025       if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2026         snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2027       else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2028         snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2029       else
2030         snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2031       return buff;
2032     }
2033 }
2034
2035 static char *
2036 get_machine_name (unsigned e_machine)
2037 {
2038   static char buff[64]; /* XXX */
2039
2040   switch (e_machine)
2041     {
2042     case EM_NONE:               return _("None");
2043     case EM_AARCH64:            return "AArch64";
2044     case EM_M32:                return "WE32100";
2045     case EM_SPARC:              return "Sparc";
2046     case EM_SPU:                return "SPU";
2047     case EM_386:                return "Intel 80386";
2048     case EM_68K:                return "MC68000";
2049     case EM_88K:                return "MC88000";
2050     case EM_486:                return "Intel 80486";
2051     case EM_860:                return "Intel 80860";
2052     case EM_MIPS:               return "MIPS R3000";
2053     case EM_S370:               return "IBM System/370";
2054     case EM_MIPS_RS3_LE:        return "MIPS R4000 big-endian";
2055     case EM_OLD_SPARCV9:        return "Sparc v9 (old)";
2056     case EM_PARISC:             return "HPPA";
2057     case EM_PPC_OLD:            return "Power PC (old)";
2058     case EM_SPARC32PLUS:        return "Sparc v8+" ;
2059     case EM_960:                return "Intel 90860";
2060     case EM_PPC:                return "PowerPC";
2061     case EM_PPC64:              return "PowerPC64";
2062     case EM_FR20:               return "Fujitsu FR20";
2063     case EM_RH32:               return "TRW RH32";
2064     case EM_MCORE:              return "MCORE";
2065     case EM_ARM:                return "ARM";
2066     case EM_OLD_ALPHA:          return "Digital Alpha (old)";
2067     case EM_SH:                 return "Renesas / SuperH SH";
2068     case EM_SPARCV9:            return "Sparc v9";
2069     case EM_TRICORE:            return "Siemens Tricore";
2070     case EM_ARC:                return "ARC";
2071     case EM_H8_300:             return "Renesas H8/300";
2072     case EM_H8_300H:            return "Renesas H8/300H";
2073     case EM_H8S:                return "Renesas H8S";
2074     case EM_H8_500:             return "Renesas H8/500";
2075     case EM_IA_64:              return "Intel IA-64";
2076     case EM_MIPS_X:             return "Stanford MIPS-X";
2077     case EM_COLDFIRE:           return "Motorola Coldfire";
2078     case EM_ALPHA:              return "Alpha";
2079     case EM_CYGNUS_D10V:
2080     case EM_D10V:               return "d10v";
2081     case EM_CYGNUS_D30V:
2082     case EM_D30V:               return "d30v";
2083     case EM_CYGNUS_M32R:
2084     case EM_M32R:               return "Renesas M32R (formerly Mitsubishi M32r)";
2085     case EM_CYGNUS_V850:
2086     case EM_V800:               return "Renesas V850 (using RH850 ABI)";
2087     case EM_V850:               return "Renesas V850";
2088     case EM_CYGNUS_MN10300:
2089     case EM_MN10300:            return "mn10300";
2090     case EM_CYGNUS_MN10200:
2091     case EM_MN10200:            return "mn10200";
2092     case EM_MOXIE:              return "Moxie";
2093     case EM_CYGNUS_FR30:
2094     case EM_FR30:               return "Fujitsu FR30";
2095     case EM_CYGNUS_FRV:         return "Fujitsu FR-V";
2096     case EM_PJ_OLD:
2097     case EM_PJ:                 return "picoJava";
2098     case EM_MMA:                return "Fujitsu Multimedia Accelerator";
2099     case EM_PCP:                return "Siemens PCP";
2100     case EM_NCPU:               return "Sony nCPU embedded RISC processor";
2101     case EM_NDR1:               return "Denso NDR1 microprocesspr";
2102     case EM_STARCORE:           return "Motorola Star*Core processor";
2103     case EM_ME16:               return "Toyota ME16 processor";
2104     case EM_ST100:              return "STMicroelectronics ST100 processor";
2105     case EM_TINYJ:              return "Advanced Logic Corp. TinyJ embedded processor";
2106     case EM_PDSP:               return "Sony DSP processor";
2107     case EM_PDP10:              return "Digital Equipment Corp. PDP-10";
2108     case EM_PDP11:              return "Digital Equipment Corp. PDP-11";
2109     case EM_FX66:               return "Siemens FX66 microcontroller";
2110     case EM_ST9PLUS:            return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2111     case EM_ST7:                return "STMicroelectronics ST7 8-bit microcontroller";
2112     case EM_68HC16:             return "Motorola MC68HC16 Microcontroller";
2113     case EM_68HC12:             return "Motorola MC68HC12 Microcontroller";
2114     case EM_68HC11:             return "Motorola MC68HC11 Microcontroller";
2115     case EM_68HC08:             return "Motorola MC68HC08 Microcontroller";
2116     case EM_68HC05:             return "Motorola MC68HC05 Microcontroller";
2117     case EM_SVX:                return "Silicon Graphics SVx";
2118     case EM_ST19:               return "STMicroelectronics ST19 8-bit microcontroller";
2119     case EM_VAX:                return "Digital VAX";
2120     case EM_AVR_OLD:
2121     case EM_AVR:                return "Atmel AVR 8-bit microcontroller";
2122     case EM_CRIS:               return "Axis Communications 32-bit embedded processor";
2123     case EM_JAVELIN:            return "Infineon Technologies 32-bit embedded cpu";
2124     case EM_FIREPATH:           return "Element 14 64-bit DSP processor";
2125     case EM_ZSP:                return "LSI Logic's 16-bit DSP processor";
2126     case EM_MMIX:               return "Donald Knuth's educational 64-bit processor";
2127     case EM_HUANY:              return "Harvard Universitys's machine-independent object format";
2128     case EM_PRISM:              return "Vitesse Prism";
2129     case EM_X86_64:             return "Advanced Micro Devices X86-64";
2130     case EM_L1OM:               return "Intel L1OM";
2131     case EM_K1OM:               return "Intel K1OM";
2132     case EM_S390_OLD:
2133     case EM_S390:               return "IBM S/390";
2134     case EM_SCORE:              return "SUNPLUS S+Core";
2135     case EM_XSTORMY16:          return "Sanyo XStormy16 CPU core";
2136     case EM_OR1K:               return "OpenRISC 1000";
2137     case EM_ARC_A5:             return "ARC International ARCompact processor";
2138     case EM_CRX:                return "National Semiconductor CRX microprocessor";
2139     case EM_ADAPTEVA_EPIPHANY:  return "Adapteva EPIPHANY";
2140     case EM_DLX:                return "OpenDLX";
2141     case EM_IP2K_OLD:
2142     case EM_IP2K:               return "Ubicom IP2xxx 8-bit microcontrollers";
2143     case EM_IQ2000:             return "Vitesse IQ2000";
2144     case EM_XTENSA_OLD:
2145     case EM_XTENSA:             return "Tensilica Xtensa Processor";
2146     case EM_VIDEOCORE:          return "Alphamosaic VideoCore processor";
2147     case EM_TMM_GPP:            return "Thompson Multimedia General Purpose Processor";
2148     case EM_NS32K:              return "National Semiconductor 32000 series";
2149     case EM_TPC:                return "Tenor Network TPC processor";
2150     case EM_ST200:              return "STMicroelectronics ST200 microcontroller";
2151     case EM_MAX:                return "MAX Processor";
2152     case EM_CR:                 return "National Semiconductor CompactRISC";
2153     case EM_F2MC16:             return "Fujitsu F2MC16";
2154     case EM_MSP430:             return "Texas Instruments msp430 microcontroller";
2155     case EM_LATTICEMICO32:      return "Lattice Mico32";
2156     case EM_M32C_OLD:
2157     case EM_M32C:               return "Renesas M32c";
2158     case EM_MT:                 return "Morpho Techologies MT processor";
2159     case EM_BLACKFIN:           return "Analog Devices Blackfin";
2160     case EM_SE_C33:             return "S1C33 Family of Seiko Epson processors";
2161     case EM_SEP:                return "Sharp embedded microprocessor";
2162     case EM_ARCA:               return "Arca RISC microprocessor";
2163     case EM_UNICORE:            return "Unicore";
2164     case EM_EXCESS:             return "eXcess 16/32/64-bit configurable embedded CPU";
2165     case EM_DXP:                return "Icera Semiconductor Inc. Deep Execution Processor";
2166     case EM_NIOS32:             return "Altera Nios";
2167     case EM_ALTERA_NIOS2:       return "Altera Nios II";
2168     case EM_C166:
2169     case EM_XC16X:              return "Infineon Technologies xc16x";
2170     case EM_M16C:               return "Renesas M16C series microprocessors";
2171     case EM_DSPIC30F:           return "Microchip Technology dsPIC30F Digital Signal Controller";
2172     case EM_CE:                 return "Freescale Communication Engine RISC core";
2173     case EM_TSK3000:            return "Altium TSK3000 core";
2174     case EM_RS08:               return "Freescale RS08 embedded processor";
2175     case EM_ECOG2:              return "Cyan Technology eCOG2 microprocessor";
2176     case EM_DSP24:              return "New Japan Radio (NJR) 24-bit DSP Processor";
2177     case EM_VIDEOCORE3:         return "Broadcom VideoCore III processor";
2178     case EM_SE_C17:             return "Seiko Epson C17 family";
2179     case EM_TI_C6000:           return "Texas Instruments TMS320C6000 DSP family";
2180     case EM_TI_C2000:           return "Texas Instruments TMS320C2000 DSP family";
2181     case EM_TI_C5500:           return "Texas Instruments TMS320C55x DSP family";
2182     case EM_MMDSP_PLUS:         return "STMicroelectronics 64bit VLIW Data Signal Processor";
2183     case EM_CYPRESS_M8C:        return "Cypress M8C microprocessor";
2184     case EM_R32C:               return "Renesas R32C series microprocessors";
2185     case EM_TRIMEDIA:           return "NXP Semiconductors TriMedia architecture family";
2186     case EM_QDSP6:              return "QUALCOMM DSP6 Processor";
2187     case EM_8051:               return "Intel 8051 and variants";
2188     case EM_STXP7X:             return "STMicroelectronics STxP7x family";
2189     case EM_NDS32:              return "Andes Technology compact code size embedded RISC processor family";
2190     case EM_ECOG1X:             return "Cyan Technology eCOG1X family";
2191     case EM_MAXQ30:             return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2192     case EM_XIMO16:             return "New Japan Radio (NJR) 16-bit DSP Processor";
2193     case EM_MANIK:              return "M2000 Reconfigurable RISC Microprocessor";
2194     case EM_CRAYNV2:            return "Cray Inc. NV2 vector architecture";
2195     case EM_CYGNUS_MEP:         return "Toshiba MeP Media Engine";
2196     case EM_CR16:
2197     case EM_MICROBLAZE:
2198     case EM_MICROBLAZE_OLD:     return "Xilinx MicroBlaze";
2199     case EM_RL78:               return "Renesas RL78";
2200     case EM_RX:                 return "Renesas RX";
2201     case EM_METAG:              return "Imagination Technologies Meta processor architecture";
2202     case EM_MCST_ELBRUS:        return "MCST Elbrus general purpose hardware architecture";
2203     case EM_ECOG16:             return "Cyan Technology eCOG16 family";
2204     case EM_ETPU:               return "Freescale Extended Time Processing Unit";
2205     case EM_SLE9X:              return "Infineon Technologies SLE9X core";
2206     case EM_AVR32:              return "Atmel Corporation 32-bit microprocessor family";
2207     case EM_STM8:               return "STMicroeletronics STM8 8-bit microcontroller";
2208     case EM_TILE64:             return "Tilera TILE64 multicore architecture family";
2209     case EM_TILEPRO:            return "Tilera TILEPro multicore architecture family";
2210     case EM_TILEGX:             return "Tilera TILE-Gx multicore architecture family";
2211     case EM_CUDA:               return "NVIDIA CUDA architecture";
2212     case EM_XGATE:              return "Motorola XGATE embedded processor";
2213     default:
2214       snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2215       return buff;
2216     }
2217 }
2218
2219 static void
2220 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2221 {
2222   unsigned eabi;
2223   int unknown = 0;
2224
2225   eabi = EF_ARM_EABI_VERSION (e_flags);
2226   e_flags &= ~ EF_ARM_EABIMASK;
2227
2228   /* Handle "generic" ARM flags.  */
2229   if (e_flags & EF_ARM_RELEXEC)
2230     {
2231       strcat (buf, ", relocatable executable");
2232       e_flags &= ~ EF_ARM_RELEXEC;
2233     }
2234
2235   if (e_flags & EF_ARM_HASENTRY)
2236     {
2237       strcat (buf, ", has entry point");
2238       e_flags &= ~ EF_ARM_HASENTRY;
2239     }
2240
2241   /* Now handle EABI specific flags.  */
2242   switch (eabi)
2243     {
2244     default:
2245       strcat (buf, ", <unrecognized EABI>");
2246       if (e_flags)
2247         unknown = 1;
2248       break;
2249
2250     case EF_ARM_EABI_VER1:
2251       strcat (buf, ", Version1 EABI");
2252       while (e_flags)
2253         {
2254           unsigned flag;
2255
2256           /* Process flags one bit at a time.  */
2257           flag = e_flags & - e_flags;
2258           e_flags &= ~ flag;
2259
2260           switch (flag)
2261             {
2262             case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2263               strcat (buf, ", sorted symbol tables");
2264               break;
2265
2266             default:
2267               unknown = 1;
2268               break;
2269             }
2270         }
2271       break;
2272
2273     case EF_ARM_EABI_VER2:
2274       strcat (buf, ", Version2 EABI");
2275       while (e_flags)
2276         {
2277           unsigned flag;
2278
2279           /* Process flags one bit at a time.  */
2280           flag = e_flags & - e_flags;
2281           e_flags &= ~ flag;
2282
2283           switch (flag)
2284             {
2285             case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2286               strcat (buf, ", sorted symbol tables");
2287               break;
2288
2289             case EF_ARM_DYNSYMSUSESEGIDX:
2290               strcat (buf, ", dynamic symbols use segment index");
2291               break;
2292
2293             case EF_ARM_MAPSYMSFIRST:
2294               strcat (buf, ", mapping symbols precede others");
2295               break;
2296
2297             default:
2298               unknown = 1;
2299               break;
2300             }
2301         }
2302       break;
2303
2304     case EF_ARM_EABI_VER3:
2305       strcat (buf, ", Version3 EABI");
2306       break;
2307
2308     case EF_ARM_EABI_VER4:
2309       strcat (buf, ", Version4 EABI");
2310       while (e_flags)
2311         {
2312           unsigned flag;
2313
2314           /* Process flags one bit at a time.  */
2315           flag = e_flags & - e_flags;
2316           e_flags &= ~ flag;
2317
2318           switch (flag)
2319             {
2320             case EF_ARM_BE8:
2321               strcat (buf, ", BE8");
2322               break;
2323
2324             case EF_ARM_LE8:
2325               strcat (buf, ", LE8");
2326               break;
2327
2328             default:
2329               unknown = 1;
2330               break;
2331             }
2332       break;
2333         }
2334       break;
2335
2336     case EF_ARM_EABI_VER5:
2337       strcat (buf, ", Version5 EABI");
2338       while (e_flags)
2339         {
2340           unsigned flag;
2341
2342           /* Process flags one bit at a time.  */
2343           flag = e_flags & - e_flags;
2344           e_flags &= ~ flag;
2345
2346           switch (flag)
2347             {
2348             case EF_ARM_BE8:
2349               strcat (buf, ", BE8");
2350               break;
2351
2352             case EF_ARM_LE8:
2353               strcat (buf, ", LE8");
2354               break;
2355
2356             case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT.  */
2357               strcat (buf, ", soft-float ABI");
2358               break;
2359
2360             case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT.  */
2361               strcat (buf, ", hard-float ABI");
2362               break;
2363
2364             default:
2365               unknown = 1;
2366               break;
2367             }
2368         }
2369       break;
2370
2371     case EF_ARM_EABI_UNKNOWN:
2372       strcat (buf, ", GNU EABI");
2373       while (e_flags)
2374         {
2375           unsigned flag;
2376
2377           /* Process flags one bit at a time.  */
2378           flag = e_flags & - e_flags;
2379           e_flags &= ~ flag;
2380
2381           switch (flag)
2382             {
2383             case EF_ARM_INTERWORK:
2384               strcat (buf, ", interworking enabled");
2385               break;
2386
2387             case EF_ARM_APCS_26:
2388               strcat (buf, ", uses APCS/26");
2389               break;
2390
2391             case EF_ARM_APCS_FLOAT:
2392               strcat (buf, ", uses APCS/float");
2393               break;
2394
2395             case EF_ARM_PIC:
2396               strcat (buf, ", position independent");
2397               break;
2398
2399             case EF_ARM_ALIGN8:
2400               strcat (buf, ", 8 bit structure alignment");
2401               break;
2402
2403             case EF_ARM_NEW_ABI:
2404               strcat (buf, ", uses new ABI");
2405               break;
2406
2407             case EF_ARM_OLD_ABI:
2408               strcat (buf, ", uses old ABI");
2409               break;
2410
2411             case EF_ARM_SOFT_FLOAT:
2412               strcat (buf, ", software FP");
2413               break;
2414
2415             case EF_ARM_VFP_FLOAT:
2416               strcat (buf, ", VFP");
2417               break;
2418
2419             case EF_ARM_MAVERICK_FLOAT:
2420               strcat (buf, ", Maverick FP");
2421               break;
2422
2423             default:
2424               unknown = 1;
2425               break;
2426             }
2427         }
2428     }
2429
2430   if (unknown)
2431     strcat (buf,_(", <unknown>"));
2432 }
2433
2434 static void
2435 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2436 {
2437   unsigned abi;
2438   unsigned arch;
2439   unsigned config;
2440   unsigned version;
2441   int has_fpu = 0;
2442   int r = 0;
2443
2444   static const char *ABI_STRINGS[] =
2445   {
2446     "ABI v0", /* use r5 as return register; only used in N1213HC */
2447     "ABI v1", /* use r0 as return register */
2448     "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2449     "ABI v2fp", /* for FPU */
2450     "AABI",
2451     "ABI2 FP+"
2452   };
2453   static const char *VER_STRINGS[] =
2454   {
2455     "Andes ELF V1.3 or older",
2456     "Andes ELF V1.3.1",
2457     "Andes ELF V1.4"
2458   };
2459   static const char *ARCH_STRINGS[] =
2460   {
2461     "",
2462     "Andes Star v1.0",
2463     "Andes Star v2.0",
2464     "Andes Star v3.0",
2465     "Andes Star v3.0m"
2466   };
2467
2468   abi = EF_NDS_ABI & e_flags;
2469   arch = EF_NDS_ARCH & e_flags;
2470   config = EF_NDS_INST & e_flags;
2471   version = EF_NDS32_ELF_VERSION & e_flags;
2472
2473   memset (buf, 0, size);
2474
2475   switch (abi)
2476     {
2477     case E_NDS_ABI_V0:
2478     case E_NDS_ABI_V1:
2479     case E_NDS_ABI_V2:
2480     case E_NDS_ABI_V2FP:
2481     case E_NDS_ABI_AABI:
2482     case E_NDS_ABI_V2FP_PLUS:
2483       /* In case there are holes in the array.  */
2484       r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
2485       break;
2486
2487     default:
2488       r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
2489       break;
2490     }
2491
2492   switch (version)
2493     {
2494     case E_NDS32_ELF_VER_1_2:
2495     case E_NDS32_ELF_VER_1_3:
2496     case E_NDS32_ELF_VER_1_4:
2497       r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
2498       break;
2499
2500     default:
2501       r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
2502       break;
2503     }
2504
2505   if (E_NDS_ABI_V0 == abi)
2506     {
2507       /* OLD ABI; only used in N1213HC, has performance extension 1.  */
2508       r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
2509       if (arch == E_NDS_ARCH_STAR_V1_0)
2510         r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
2511       return;
2512     }
2513
2514   switch (arch)
2515     {
2516     case E_NDS_ARCH_STAR_V1_0:
2517     case E_NDS_ARCH_STAR_V2_0:
2518     case E_NDS_ARCH_STAR_V3_0:
2519     case E_NDS_ARCH_STAR_V3_M:
2520       r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
2521       break;
2522
2523     default:
2524       r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
2525       /* ARCH version determines how the e_flags are interpreted.
2526          If it is unknown, we cannot proceed.  */
2527       return;
2528     }
2529
2530   /* Newer ABI; Now handle architecture specific flags.  */
2531   if (arch == E_NDS_ARCH_STAR_V1_0)
2532     {
2533       if (config & E_NDS32_HAS_MFUSR_PC_INST)
2534         r += snprintf (buf + r, size -r, ", MFUSR_PC");
2535
2536       if (!(config & E_NDS32_HAS_NO_MAC_INST))
2537         r += snprintf (buf + r, size -r, ", MAC");
2538
2539       if (config & E_NDS32_HAS_DIV_INST)
2540         r += snprintf (buf + r, size -r, ", DIV");
2541
2542       if (config & E_NDS32_HAS_16BIT_INST)
2543         r += snprintf (buf + r, size -r, ", 16b");
2544     }
2545   else
2546     {
2547       if (config & E_NDS32_HAS_MFUSR_PC_INST)
2548         {
2549           if (version <= E_NDS32_ELF_VER_1_3)
2550             r += snprintf (buf + r, size -r, ", [B8]");
2551           else
2552             r += snprintf (buf + r, size -r, ", EX9");
2553         }
2554
2555       if (config & E_NDS32_HAS_MAC_DX_INST)
2556         r += snprintf (buf + r, size -r, ", MAC_DX");
2557
2558       if (config & E_NDS32_HAS_DIV_DX_INST)
2559         r += snprintf (buf + r, size -r, ", DIV_DX");
2560
2561       if (config & E_NDS32_HAS_16BIT_INST)
2562         {
2563           if (version <= E_NDS32_ELF_VER_1_3)
2564             r += snprintf (buf + r, size -r, ", 16b");
2565           else
2566             r += snprintf (buf + r, size -r, ", IFC");
2567         }
2568     }
2569
2570   if (config & E_NDS32_HAS_EXT_INST)
2571     r += snprintf (buf + r, size -r, ", PERF1");
2572
2573   if (config & E_NDS32_HAS_EXT2_INST)
2574     r += snprintf (buf + r, size -r, ", PERF2");
2575
2576   if (config & E_NDS32_HAS_FPU_INST)
2577     {
2578       has_fpu = 1;
2579       r += snprintf (buf + r, size -r, ", FPU_SP");
2580     }
2581
2582   if (config & E_NDS32_HAS_FPU_DP_INST)
2583     {
2584       has_fpu = 1;
2585       r += snprintf (buf + r, size -r, ", FPU_DP");
2586     }
2587
2588   if (config & E_NDS32_HAS_FPU_MAC_INST)
2589     {
2590       has_fpu = 1;
2591       r += snprintf (buf + r, size -r, ", FPU_MAC");
2592     }
2593
2594   if (has_fpu)
2595     {
2596       switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
2597         {
2598         case E_NDS32_FPU_REG_8SP_4DP:
2599           r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
2600           break;
2601         case E_NDS32_FPU_REG_16SP_8DP:
2602           r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
2603           break;
2604         case E_NDS32_FPU_REG_32SP_16DP:
2605           r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
2606           break;
2607         case E_NDS32_FPU_REG_32SP_32DP:
2608           r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
2609           break;
2610         }
2611     }
2612
2613   if (config & E_NDS32_HAS_AUDIO_INST)
2614     r += snprintf (buf + r, size -r, ", AUDIO");
2615
2616   if (config & E_NDS32_HAS_STRING_INST)
2617     r += snprintf (buf + r, size -r, ", STR");
2618
2619   if (config & E_NDS32_HAS_REDUCED_REGS)
2620     r += snprintf (buf + r, size -r, ", 16REG");
2621
2622   if (config & E_NDS32_HAS_VIDEO_INST)
2623     {
2624       if (version <= E_NDS32_ELF_VER_1_3)
2625         r += snprintf (buf + r, size -r, ", VIDEO");
2626       else
2627         r += snprintf (buf + r, size -r, ", SATURATION");
2628     }
2629
2630   if (config & E_NDS32_HAS_ENCRIPT_INST)
2631     r += snprintf (buf + r, size -r, ", ENCRP");
2632
2633   if (config & E_NDS32_HAS_L2C_INST)
2634     r += snprintf (buf + r, size -r, ", L2C");
2635 }
2636
2637 static char *
2638 get_machine_flags (unsigned e_flags, unsigned e_machine)
2639 {
2640   static char buf[1024];
2641
2642   buf[0] = '\0';
2643
2644   if (e_flags)
2645     {
2646       switch (e_machine)
2647         {
2648         default:
2649           break;
2650
2651         case EM_ARM:
2652           decode_ARM_machine_flags (e_flags, buf);
2653           break;
2654
2655         case EM_BLACKFIN:
2656           if (e_flags & EF_BFIN_PIC)
2657             strcat (buf, ", PIC");
2658
2659           if (e_flags & EF_BFIN_FDPIC)
2660             strcat (buf, ", FDPIC");
2661
2662           if (e_flags & EF_BFIN_CODE_IN_L1)
2663             strcat (buf, ", code in L1");
2664
2665           if (e_flags & EF_BFIN_DATA_IN_L1)
2666             strcat (buf, ", data in L1");
2667
2668           break;
2669
2670         case EM_CYGNUS_FRV:
2671           switch (e_flags & EF_FRV_CPU_MASK)
2672             {
2673             case EF_FRV_CPU_GENERIC:
2674               break;
2675
2676             default:
2677               strcat (buf, ", fr???");
2678               break;
2679
2680             case EF_FRV_CPU_FR300:
2681               strcat (buf, ", fr300");
2682               break;
2683
2684             case EF_FRV_CPU_FR400:
2685               strcat (buf, ", fr400");
2686               break;
2687             case EF_FRV_CPU_FR405:
2688               strcat (buf, ", fr405");
2689               break;
2690
2691             case EF_FRV_CPU_FR450:
2692               strcat (buf, ", fr450");
2693               break;
2694
2695             case EF_FRV_CPU_FR500:
2696               strcat (buf, ", fr500");
2697               break;
2698             case EF_FRV_CPU_FR550:
2699               strcat (buf, ", fr550");
2700               break;
2701
2702             case EF_FRV_CPU_SIMPLE:
2703               strcat (buf, ", simple");
2704               break;
2705             case EF_FRV_CPU_TOMCAT:
2706               strcat (buf, ", tomcat");
2707               break;
2708             }
2709           break;
2710
2711         case EM_68K:
2712           if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
2713             strcat (buf, ", m68000");
2714           else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
2715             strcat (buf, ", cpu32");
2716           else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
2717             strcat (buf, ", fido_a");
2718           else
2719             {
2720               char const * isa = _("unknown");
2721               char const * mac = _("unknown mac");
2722               char const * additional = NULL;
2723
2724               switch (e_flags & EF_M68K_CF_ISA_MASK)
2725                 {
2726                 case EF_M68K_CF_ISA_A_NODIV:
2727                   isa = "A";
2728                   additional = ", nodiv";
2729                   break;
2730                 case EF_M68K_CF_ISA_A:
2731                   isa = "A";
2732                   break;
2733                 case EF_M68K_CF_ISA_A_PLUS:
2734                   isa = "A+";
2735                   break;
2736                 case EF_M68K_CF_ISA_B_NOUSP:
2737                   isa = "B";
2738                   additional = ", nousp";
2739                   break;
2740                 case EF_M68K_CF_ISA_B:
2741                   isa = "B";
2742                   break;
2743                 case EF_M68K_CF_ISA_C:
2744                   isa = "C";
2745                   break;
2746                 case EF_M68K_CF_ISA_C_NODIV:
2747                   isa = "C";
2748                   additional = ", nodiv";
2749                   break;
2750                 }
2751               strcat (buf, ", cf, isa ");
2752               strcat (buf, isa);
2753               if (additional)
2754                 strcat (buf, additional);
2755               if (e_flags & EF_M68K_CF_FLOAT)
2756                 strcat (buf, ", float");
2757               switch (e_flags & EF_M68K_CF_MAC_MASK)
2758                 {
2759                 case 0:
2760                   mac = NULL;
2761                   break;
2762                 case EF_M68K_CF_MAC:
2763                   mac = "mac";
2764                   break;
2765                 case EF_M68K_CF_EMAC:
2766                   mac = "emac";
2767                   break;
2768                 case EF_M68K_CF_EMAC_B:
2769                   mac = "emac_b";
2770                   break;
2771                 }
2772               if (mac)
2773                 {
2774                   strcat (buf, ", ");
2775                   strcat (buf, mac);
2776                 }
2777             }
2778           break;
2779
2780         case EM_PPC:
2781           if (e_flags & EF_PPC_EMB)
2782             strcat (buf, ", emb");
2783
2784           if (e_flags & EF_PPC_RELOCATABLE)
2785             strcat (buf, _(", relocatable"));
2786
2787           if (e_flags & EF_PPC_RELOCATABLE_LIB)
2788             strcat (buf, _(", relocatable-lib"));
2789           break;
2790
2791         case EM_PPC64:
2792           if (e_flags & EF_PPC64_ABI)
2793             {
2794               char abi[] = ", abiv0";
2795
2796               abi[6] += e_flags & EF_PPC64_ABI;
2797               strcat (buf, abi);
2798             }
2799           break;
2800
2801         case EM_V800:
2802           if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
2803             strcat (buf, ", RH850 ABI");
2804
2805           if (e_flags & EF_V800_850E3)
2806             strcat (buf, ", V3 architecture");
2807
2808           if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
2809             strcat (buf, ", FPU not used");
2810
2811           if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
2812             strcat (buf, ", regmode: COMMON");
2813
2814           if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
2815             strcat (buf, ", r4 not used");
2816
2817           if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
2818             strcat (buf, ", r30 not used");
2819
2820           if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
2821             strcat (buf, ", r5 not used");
2822
2823           if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
2824             strcat (buf, ", r2 not used");
2825
2826           for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
2827             {
2828               switch (e_flags & - e_flags)
2829                 {
2830                 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
2831                 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
2832                 case EF_RH850_SIMD: strcat (buf, ", SIMD"); break;
2833                 case EF_RH850_CACHE: strcat (buf, ", CACHE"); break;
2834                 case EF_RH850_MMU: strcat (buf, ", MMU"); break;
2835                 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
2836                 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
2837                 case EF_RH850_DATA_ALIGN8: strcat (buf, ", 8-byte alignment"); break;
2838                 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
2839                 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
2840                 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
2841                 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
2842                 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
2843                 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
2844                 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
2845                 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
2846                 default: break;
2847                 }
2848             }
2849           break;
2850
2851         case EM_V850:
2852         case EM_CYGNUS_V850:
2853           switch (e_flags & EF_V850_ARCH)
2854             {
2855             case E_V850E3V5_ARCH:
2856               strcat (buf, ", v850e3v5");
2857               break;
2858             case E_V850E2V3_ARCH:
2859               strcat (buf, ", v850e2v3");
2860               break;
2861             case E_V850E2_ARCH:
2862               strcat (buf, ", v850e2");
2863               break;
2864             case E_V850E1_ARCH:
2865               strcat (buf, ", v850e1");
2866               break;
2867             case E_V850E_ARCH:
2868               strcat (buf, ", v850e");
2869               break;
2870             case E_V850_ARCH:
2871               strcat (buf, ", v850");
2872               break;
2873             default:
2874               strcat (buf, _(", unknown v850 architecture variant"));
2875               break;
2876             }
2877           break;
2878
2879         case EM_M32R:
2880         case EM_CYGNUS_M32R:
2881           if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
2882             strcat (buf, ", m32r");
2883           break;
2884
2885         case EM_MIPS:
2886         case EM_MIPS_RS3_LE:
2887           if (e_flags & EF_MIPS_NOREORDER)
2888             strcat (buf, ", noreorder");
2889
2890           if (e_flags & EF_MIPS_PIC)
2891             strcat (buf, ", pic");
2892
2893           if (e_flags & EF_MIPS_CPIC)
2894             strcat (buf, ", cpic");
2895
2896           if (e_flags & EF_MIPS_UCODE)
2897             strcat (buf, ", ugen_reserved");
2898
2899           if (e_flags & EF_MIPS_ABI2)
2900             strcat (buf, ", abi2");
2901
2902           if (e_flags & EF_MIPS_OPTIONS_FIRST)
2903             strcat (buf, ", odk first");
2904
2905           if (e_flags & EF_MIPS_32BITMODE)
2906             strcat (buf, ", 32bitmode");
2907
2908           if (e_flags & EF_MIPS_NAN2008)
2909             strcat (buf, ", nan2008");
2910
2911           if (e_flags & EF_MIPS_FP64)
2912             strcat (buf, ", fp64");
2913
2914           switch ((e_flags & EF_MIPS_MACH))
2915             {
2916             case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
2917             case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
2918             case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
2919             case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
2920             case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
2921             case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
2922             case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
2923             case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
2924             case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
2925             case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
2926             case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
2927             case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
2928             case E_MIPS_MACH_LS3A: strcat (buf, ", loongson-3a"); break;
2929             case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
2930             case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
2931             case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
2932             case E_MIPS_MACH_XLR:  strcat (buf, ", xlr"); break;
2933             case 0:
2934             /* We simply ignore the field in this case to avoid confusion:
2935                MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
2936                extension.  */
2937               break;
2938             default: strcat (buf, _(", unknown CPU")); break;
2939             }
2940
2941           switch ((e_flags & EF_MIPS_ABI))
2942             {
2943             case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
2944             case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
2945             case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
2946             case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
2947             case 0:
2948             /* We simply ignore the field in this case to avoid confusion:
2949                MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
2950                This means it is likely to be an o32 file, but not for
2951                sure.  */
2952               break;
2953             default: strcat (buf, _(", unknown ABI")); break;
2954             }
2955
2956           if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
2957             strcat (buf, ", mdmx");
2958
2959           if (e_flags & EF_MIPS_ARCH_ASE_M16)
2960             strcat (buf, ", mips16");
2961
2962           if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
2963             strcat (buf, ", micromips");
2964
2965           switch ((e_flags & EF_MIPS_ARCH))
2966             {
2967             case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
2968             case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
2969             case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
2970             case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
2971             case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
2972             case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
2973             case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
2974             case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
2975             case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
2976             case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
2977             case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
2978             default: strcat (buf, _(", unknown ISA")); break;
2979             }
2980           break;
2981
2982         case EM_NDS32:
2983           decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
2984           break;
2985
2986         case EM_SH:
2987           switch ((e_flags & EF_SH_MACH_MASK))
2988             {
2989             case EF_SH1: strcat (buf, ", sh1"); break;
2990             case EF_SH2: strcat (buf, ", sh2"); break;
2991             case EF_SH3: strcat (buf, ", sh3"); break;
2992             case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
2993             case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
2994             case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
2995             case EF_SH3E: strcat (buf, ", sh3e"); break;
2996             case EF_SH4: strcat (buf, ", sh4"); break;
2997             case EF_SH5: strcat (buf, ", sh5"); break;
2998             case EF_SH2E: strcat (buf, ", sh2e"); break;
2999             case EF_SH4A: strcat (buf, ", sh4a"); break;
3000             case EF_SH2A: strcat (buf, ", sh2a"); break;
3001             case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3002             case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3003             case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3004             case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3005             case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3006             case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3007             case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3008             case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3009             case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3010             default: strcat (buf, _(", unknown ISA")); break;
3011             }
3012
3013           if (e_flags & EF_SH_PIC)
3014             strcat (buf, ", pic");
3015
3016           if (e_flags & EF_SH_FDPIC)
3017             strcat (buf, ", fdpic");
3018           break;
3019
3020         case EM_OR1K:
3021           if (e_flags & EF_OR1K_NODELAY)
3022             strcat (buf, ", no delay");
3023           break;
3024
3025         case EM_SPARCV9:
3026           if (e_flags & EF_SPARC_32PLUS)
3027             strcat (buf, ", v8+");
3028
3029           if (e_flags & EF_SPARC_SUN_US1)
3030             strcat (buf, ", ultrasparcI");
3031
3032           if (e_flags & EF_SPARC_SUN_US3)
3033             strcat (buf, ", ultrasparcIII");
3034
3035           if (e_flags & EF_SPARC_HAL_R1)
3036             strcat (buf, ", halr1");
3037
3038           if (e_flags & EF_SPARC_LEDATA)
3039             strcat (buf, ", ledata");
3040
3041           if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3042             strcat (buf, ", tso");
3043
3044           if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3045             strcat (buf, ", pso");
3046
3047           if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3048             strcat (buf, ", rmo");
3049           break;
3050
3051         case EM_PARISC:
3052           switch (e_flags & EF_PARISC_ARCH)
3053             {
3054             case EFA_PARISC_1_0:
3055               strcpy (buf, ", PA-RISC 1.0");
3056               break;
3057             case EFA_PARISC_1_1:
3058               strcpy (buf, ", PA-RISC 1.1");
3059               break;
3060             case EFA_PARISC_2_0:
3061               strcpy (buf, ", PA-RISC 2.0");
3062               break;
3063             default:
3064               break;
3065             }
3066           if (e_flags & EF_PARISC_TRAPNIL)
3067             strcat (buf, ", trapnil");
3068           if (e_flags & EF_PARISC_EXT)
3069             strcat (buf, ", ext");
3070           if (e_flags & EF_PARISC_LSB)
3071             strcat (buf, ", lsb");
3072           if (e_flags & EF_PARISC_WIDE)
3073             strcat (buf, ", wide");
3074           if (e_flags & EF_PARISC_NO_KABP)
3075             strcat (buf, ", no kabp");
3076           if (e_flags & EF_PARISC_LAZYSWAP)
3077             strcat (buf, ", lazyswap");
3078           break;
3079
3080         case EM_PJ:
3081         case EM_PJ_OLD:
3082           if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3083             strcat (buf, ", new calling convention");
3084
3085           if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3086             strcat (buf, ", gnu calling convention");
3087           break;
3088
3089         case EM_IA_64:
3090           if ((e_flags & EF_IA_64_ABI64))
3091             strcat (buf, ", 64-bit");
3092           else
3093             strcat (buf, ", 32-bit");
3094           if ((e_flags & EF_IA_64_REDUCEDFP))
3095             strcat (buf, ", reduced fp model");
3096           if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3097             strcat (buf, ", no function descriptors, constant gp");
3098           else if ((e_flags & EF_IA_64_CONS_GP))
3099             strcat (buf, ", constant gp");
3100           if ((e_flags & EF_IA_64_ABSOLUTE))
3101             strcat (buf, ", absolute");
3102           if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3103             {
3104               if ((e_flags & EF_IA_64_VMS_LINKAGES))
3105                 strcat (buf, ", vms_linkages");
3106               switch ((e_flags & EF_IA_64_VMS_COMCOD))
3107                 {
3108                 case EF_IA_64_VMS_COMCOD_SUCCESS:
3109                   break;
3110                 case EF_IA_64_VMS_COMCOD_WARNING:
3111                   strcat (buf, ", warning");
3112                   break;
3113                 case EF_IA_64_VMS_COMCOD_ERROR:
3114                   strcat (buf, ", error");
3115                   break;
3116                 case EF_IA_64_VMS_COMCOD_ABORT:
3117                   strcat (buf, ", abort");
3118                   break;
3119                 default:
3120                   warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3121                         e_flags & EF_IA_64_VMS_COMCOD);
3122                   strcat (buf, ", <unknown>");
3123                 }
3124             }
3125           break;
3126
3127         case EM_VAX:
3128           if ((e_flags & EF_VAX_NONPIC))
3129             strcat (buf, ", non-PIC");
3130           if ((e_flags & EF_VAX_DFLOAT))
3131             strcat (buf, ", D-Float");
3132           if ((e_flags & EF_VAX_GFLOAT))
3133             strcat (buf, ", G-Float");
3134           break;
3135
3136         case EM_RL78:
3137           if (e_flags & E_FLAG_RL78_G10)
3138             strcat (buf, ", G10");
3139           if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3140             strcat (buf, ", 64-bit doubles");
3141           break;
3142
3143         case EM_RX:
3144           if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3145             strcat (buf, ", 64-bit doubles");
3146           if (e_flags & E_FLAG_RX_DSP)
3147             strcat (buf, ", dsp");
3148           if (e_flags & E_FLAG_RX_PID)
3149             strcat (buf, ", pid");
3150           if (e_flags & E_FLAG_RX_ABI)
3151             strcat (buf, ", RX ABI");
3152           break;
3153
3154         case EM_S390:
3155           if (e_flags & EF_S390_HIGH_GPRS)
3156             strcat (buf, ", highgprs");
3157           break;
3158
3159         case EM_TI_C6000:
3160           if ((e_flags & EF_C6000_REL))
3161             strcat (buf, ", relocatable module");
3162           break;
3163
3164         case EM_MSP430:
3165           strcat (buf, _(": architecture variant: "));
3166           switch (e_flags & EF_MSP430_MACH)
3167             {
3168             case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3169             case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3170             case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3171             case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3172             case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3173             case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3174             case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3175             case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3176             case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3177             case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3178             case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3179             case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3180             case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3181             case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3182             case E_MSP430_MACH_MSP430X  : strcat (buf, "MSP430X"); break;
3183             default:
3184               strcat (buf, _(": unknown")); break;
3185             }
3186
3187           if (e_flags & ~ EF_MSP430_MACH)
3188             strcat (buf, _(": unknown extra flag bits also present"));
3189         }
3190     }
3191
3192   return buf;
3193 }
3194
3195 static const char *
3196 get_osabi_name (unsigned int osabi)
3197 {
3198   static char buff[32];
3199
3200   switch (osabi)
3201     {
3202     case ELFOSABI_NONE:         return "UNIX - System V";
3203     case ELFOSABI_HPUX:         return "UNIX - HP-UX";
3204     case ELFOSABI_NETBSD:       return "UNIX - NetBSD";
3205     case ELFOSABI_GNU:          return "UNIX - GNU";
3206     case ELFOSABI_SOLARIS:      return "UNIX - Solaris";
3207     case ELFOSABI_AIX:          return "UNIX - AIX";
3208     case ELFOSABI_IRIX:         return "UNIX - IRIX";
3209     case ELFOSABI_FREEBSD:      return "UNIX - FreeBSD";
3210     case ELFOSABI_TRU64:        return "UNIX - TRU64";
3211     case ELFOSABI_MODESTO:      return "Novell - Modesto";
3212     case ELFOSABI_OPENBSD:      return "UNIX - OpenBSD";
3213     case ELFOSABI_OPENVMS:      return "VMS - OpenVMS";
3214     case ELFOSABI_NSK:          return "HP - Non-Stop Kernel";
3215     case ELFOSABI_AROS:         return "AROS";
3216     case ELFOSABI_FENIXOS:      return "FenixOS";
3217     default:
3218       if (osabi >= 64)
3219         switch (elf_header.e_machine)
3220           {
3221           case EM_ARM:
3222             switch (osabi)
3223               {
3224               case ELFOSABI_ARM:        return "ARM";
3225               default:
3226                 break;
3227               }
3228             break;
3229
3230           case EM_MSP430:
3231           case EM_MSP430_OLD:
3232             switch (osabi)
3233               {
3234               case ELFOSABI_STANDALONE: return _("Standalone App");
3235               default:
3236                 break;
3237               }
3238             break;
3239
3240           case EM_TI_C6000:
3241             switch (osabi)
3242               {
3243               case ELFOSABI_C6000_ELFABI:       return _("Bare-metal C6000");
3244               case ELFOSABI_C6000_LINUX:        return "Linux C6000";
3245               default:
3246                 break;
3247               }
3248             break;
3249
3250           default:
3251             break;
3252           }
3253       snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3254       return buff;
3255     }
3256 }
3257
3258 static const char *
3259 get_aarch64_segment_type (unsigned long type)
3260 {
3261   switch (type)
3262     {
3263     case PT_AARCH64_ARCHEXT:
3264       return "AARCH64_ARCHEXT";
3265     default:
3266       break;
3267     }
3268
3269   return NULL;
3270 }
3271
3272 static const char *
3273 get_arm_segment_type (unsigned long type)
3274 {
3275   switch (type)
3276     {
3277     case PT_ARM_EXIDX:
3278       return "EXIDX";
3279     default:
3280       break;
3281     }
3282
3283   return NULL;
3284 }
3285
3286 static const char *
3287 get_mips_segment_type (unsigned long type)
3288 {
3289   switch (type)
3290     {
3291     case PT_MIPS_REGINFO:
3292       return "REGINFO";
3293     case PT_MIPS_RTPROC:
3294       return "RTPROC";
3295     case PT_MIPS_OPTIONS:
3296       return "OPTIONS";
3297     case PT_MIPS_ABIFLAGS:
3298       return "ABIFLAGS";
3299     default:
3300       break;
3301     }
3302
3303   return NULL;
3304 }
3305
3306 static const char *
3307 get_parisc_segment_type (unsigned long type)
3308 {
3309   switch (type)
3310     {
3311     case PT_HP_TLS:             return "HP_TLS";
3312     case PT_HP_CORE_NONE:       return "HP_CORE_NONE";
3313     case PT_HP_CORE_VERSION:    return "HP_CORE_VERSION";
3314     case PT_HP_CORE_KERNEL:     return "HP_CORE_KERNEL";
3315     case PT_HP_CORE_COMM:       return "HP_CORE_COMM";
3316     case PT_HP_CORE_PROC:       return "HP_CORE_PROC";
3317     case PT_HP_CORE_LOADABLE:   return "HP_CORE_LOADABLE";
3318     case PT_HP_CORE_STACK:      return "HP_CORE_STACK";
3319     case PT_HP_CORE_SHM:        return "HP_CORE_SHM";
3320     case PT_HP_CORE_MMF:        return "HP_CORE_MMF";
3321     case PT_HP_PARALLEL:        return "HP_PARALLEL";
3322     case PT_HP_FASTBIND:        return "HP_FASTBIND";
3323     case PT_HP_OPT_ANNOT:       return "HP_OPT_ANNOT";
3324     case PT_HP_HSL_ANNOT:       return "HP_HSL_ANNOT";
3325     case PT_HP_STACK:           return "HP_STACK";
3326     case PT_HP_CORE_UTSNAME:    return "HP_CORE_UTSNAME";
3327     case PT_PARISC_ARCHEXT:     return "PARISC_ARCHEXT";
3328     case PT_PARISC_UNWIND:      return "PARISC_UNWIND";
3329     case PT_PARISC_WEAKORDER:   return "PARISC_WEAKORDER";
3330     default:
3331       break;
3332     }
3333
3334   return NULL;
3335 }
3336
3337 static const char *
3338 get_ia64_segment_type (unsigned long type)
3339 {
3340   switch (type)
3341     {
3342     case PT_IA_64_ARCHEXT:      return "IA_64_ARCHEXT";
3343     case PT_IA_64_UNWIND:       return "IA_64_UNWIND";
3344     case PT_HP_TLS:             return "HP_TLS";
3345     case PT_IA_64_HP_OPT_ANOT:  return "HP_OPT_ANNOT";
3346     case PT_IA_64_HP_HSL_ANOT:  return "HP_HSL_ANNOT";
3347     case PT_IA_64_HP_STACK:     return "HP_STACK";
3348     default:
3349       break;
3350     }
3351
3352   return NULL;
3353 }
3354
3355 static const char *
3356 get_tic6x_segment_type (unsigned long type)
3357 {
3358   switch (type)
3359     {
3360     case PT_C6000_PHATTR:       return "C6000_PHATTR";
3361     default:
3362       break;
3363     }
3364
3365   return NULL;
3366 }
3367
3368 static const char *
3369 get_segment_type (unsigned long p_type)
3370 {
3371   static char buff[32];
3372
3373   switch (p_type)
3374     {
3375     case PT_NULL:       return "NULL";
3376     case PT_LOAD:       return "LOAD";
3377     case PT_DYNAMIC:    return "DYNAMIC";
3378     case PT_INTERP:     return "INTERP";
3379     case PT_NOTE:       return "NOTE";
3380     case PT_SHLIB:      return "SHLIB";
3381     case PT_PHDR:       return "PHDR";
3382     case PT_TLS:        return "TLS";
3383
3384     case PT_GNU_EH_FRAME:
3385                         return "GNU_EH_FRAME";
3386     case PT_GNU_STACK:  return "GNU_STACK";
3387     case PT_GNU_RELRO:  return "GNU_RELRO";
3388
3389     default:
3390       if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
3391         {
3392           const char * result;
3393
3394           switch (elf_header.e_machine)
3395             {
3396             case EM_AARCH64:
3397               result = get_aarch64_segment_type (p_type);
3398               break;
3399             case EM_ARM:
3400               result = get_arm_segment_type (p_type);
3401               break;
3402             case EM_MIPS:
3403             case EM_MIPS_RS3_LE:
3404               result = get_mips_segment_type (p_type);
3405               break;
3406             case EM_PARISC:
3407               result = get_parisc_segment_type (p_type);
3408               break;
3409             case EM_IA_64:
3410               result = get_ia64_segment_type (p_type);
3411               break;
3412             case EM_TI_C6000:
3413               result = get_tic6x_segment_type (p_type);
3414               break;
3415             default:
3416               result = NULL;
3417               break;
3418             }
3419
3420           if (result != NULL)
3421             return result;
3422
3423           sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
3424         }
3425       else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
3426         {
3427           const char * result;
3428
3429           switch (elf_header.e_machine)
3430             {
3431             case EM_PARISC:
3432               result = get_parisc_segment_type (p_type);
3433               break;
3434             case EM_IA_64:
3435               result = get_ia64_segment_type (p_type);
3436               break;
3437             default:
3438               result = NULL;
3439               break;
3440             }
3441
3442           if (result != NULL)
3443             return result;
3444
3445           sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
3446         }
3447       else
3448         snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
3449
3450       return buff;
3451     }
3452 }
3453
3454 static const char *
3455 get_mips_section_type_name (unsigned int sh_type)
3456 {
3457   switch (sh_type)
3458     {
3459     case SHT_MIPS_LIBLIST:       return "MIPS_LIBLIST";
3460     case SHT_MIPS_MSYM:          return "MIPS_MSYM";
3461     case SHT_MIPS_CONFLICT:      return "MIPS_CONFLICT";
3462     case SHT_MIPS_GPTAB:         return "MIPS_GPTAB";
3463     case SHT_MIPS_UCODE:         return "MIPS_UCODE";
3464     case SHT_MIPS_DEBUG:         return "MIPS_DEBUG";
3465     case SHT_MIPS_REGINFO:       return "MIPS_REGINFO";
3466     case SHT_MIPS_PACKAGE:       return "MIPS_PACKAGE";
3467     case SHT_MIPS_PACKSYM:       return "MIPS_PACKSYM";
3468     case SHT_MIPS_RELD:          return "MIPS_RELD";
3469     case SHT_MIPS_IFACE:         return "MIPS_IFACE";
3470     case SHT_MIPS_CONTENT:       return "MIPS_CONTENT";
3471     case SHT_MIPS_OPTIONS:       return "MIPS_OPTIONS";
3472     case SHT_MIPS_SHDR:          return "MIPS_SHDR";
3473     case SHT_MIPS_FDESC:         return "MIPS_FDESC";
3474     case SHT_MIPS_EXTSYM:        return "MIPS_EXTSYM";
3475     case SHT_MIPS_DENSE:         return "MIPS_DENSE";
3476     case SHT_MIPS_PDESC:         return "MIPS_PDESC";
3477     case SHT_MIPS_LOCSYM:        return "MIPS_LOCSYM";
3478     case SHT_MIPS_AUXSYM:        return "MIPS_AUXSYM";
3479     case SHT_MIPS_OPTSYM:        return "MIPS_OPTSYM";
3480     case SHT_MIPS_LOCSTR:        return "MIPS_LOCSTR";
3481     case SHT_MIPS_LINE:          return "MIPS_LINE";
3482     case SHT_MIPS_RFDESC:        return "MIPS_RFDESC";
3483     case SHT_MIPS_DELTASYM:      return "MIPS_DELTASYM";
3484     case SHT_MIPS_DELTAINST:     return "MIPS_DELTAINST";
3485     case SHT_MIPS_DELTACLASS:    return "MIPS_DELTACLASS";
3486     case SHT_MIPS_DWARF:         return "MIPS_DWARF";
3487     case SHT_MIPS_DELTADECL:     return "MIPS_DELTADECL";
3488     case SHT_MIPS_SYMBOL_LIB:    return "MIPS_SYMBOL_LIB";
3489     case SHT_MIPS_EVENTS:        return "MIPS_EVENTS";
3490     case SHT_MIPS_TRANSLATE:     return "MIPS_TRANSLATE";
3491     case SHT_MIPS_PIXIE:         return "MIPS_PIXIE";
3492     case SHT_MIPS_XLATE:         return "MIPS_XLATE";
3493     case SHT_MIPS_XLATE_DEBUG:   return "MIPS_XLATE_DEBUG";
3494     case SHT_MIPS_WHIRL:         return "MIPS_WHIRL";
3495     case SHT_MIPS_EH_REGION:     return "MIPS_EH_REGION";
3496     case SHT_MIPS_XLATE_OLD:     return "MIPS_XLATE_OLD";
3497     case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
3498     case SHT_MIPS_ABIFLAGS:      return "MIPS_ABIFLAGS";
3499     default:
3500       break;
3501     }
3502   return NULL;
3503 }
3504
3505 static const char *
3506 get_parisc_section_type_name (unsigned int sh_type)
3507 {
3508   switch (sh_type)
3509     {
3510     case SHT_PARISC_EXT:        return "PARISC_EXT";
3511     case SHT_PARISC_UNWIND:     return "PARISC_UNWIND";
3512     case SHT_PARISC_DOC:        return "PARISC_DOC";
3513     case SHT_PARISC_ANNOT:      return "PARISC_ANNOT";
3514     case SHT_PARISC_SYMEXTN:    return "PARISC_SYMEXTN";
3515     case SHT_PARISC_STUBS:      return "PARISC_STUBS";
3516     case SHT_PARISC_DLKM:       return "PARISC_DLKM";
3517     default:
3518       break;
3519     }
3520   return NULL;
3521 }
3522
3523 static const char *
3524 get_ia64_section_type_name (unsigned int sh_type)
3525 {
3526   /* If the top 8 bits are 0x78 the next 8 are the os/abi ID.  */
3527   if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
3528     return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
3529
3530   switch (sh_type)
3531     {
3532     case SHT_IA_64_EXT:                return "IA_64_EXT";
3533     case SHT_IA_64_UNWIND:             return "IA_64_UNWIND";
3534     case SHT_IA_64_PRIORITY_INIT:      return "IA_64_PRIORITY_INIT";
3535     case SHT_IA_64_VMS_TRACE:          return "VMS_TRACE";
3536     case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
3537     case SHT_IA_64_VMS_DEBUG:          return "VMS_DEBUG";
3538     case SHT_IA_64_VMS_DEBUG_STR:      return "VMS_DEBUG_STR";
3539     case SHT_IA_64_VMS_LINKAGES:       return "VMS_LINKAGES";
3540     case SHT_IA_64_VMS_SYMBOL_VECTOR:  return "VMS_SYMBOL_VECTOR";
3541     case SHT_IA_64_VMS_FIXUP:          return "VMS_FIXUP";
3542     default:
3543       break;
3544     }
3545   return NULL;
3546 }
3547
3548 static const char *
3549 get_x86_64_section_type_name (unsigned int sh_type)
3550 {
3551   switch (sh_type)
3552     {
3553     case SHT_X86_64_UNWIND:     return "X86_64_UNWIND";
3554     default:
3555       break;
3556     }
3557   return NULL;
3558 }
3559
3560 static const char *
3561 get_aarch64_section_type_name (unsigned int sh_type)
3562 {
3563   switch (sh_type)
3564     {
3565     case SHT_AARCH64_ATTRIBUTES:
3566       return "AARCH64_ATTRIBUTES";
3567     default:
3568       break;
3569     }
3570   return NULL;
3571 }
3572
3573 static const char *
3574 get_arm_section_type_name (unsigned int sh_type)
3575 {
3576   switch (sh_type)
3577     {
3578     case SHT_ARM_EXIDX:           return "ARM_EXIDX";
3579     case SHT_ARM_PREEMPTMAP:      return "ARM_PREEMPTMAP";
3580     case SHT_ARM_ATTRIBUTES:      return "ARM_ATTRIBUTES";
3581     case SHT_ARM_DEBUGOVERLAY:    return "ARM_DEBUGOVERLAY";
3582     case SHT_ARM_OVERLAYSECTION:  return "ARM_OVERLAYSECTION";
3583     default:
3584       break;
3585     }
3586   return NULL;
3587 }
3588
3589 static const char *
3590 get_tic6x_section_type_name (unsigned int sh_type)
3591 {
3592   switch (sh_type)
3593     {
3594     case SHT_C6000_UNWIND:
3595       return "C6000_UNWIND";
3596     case SHT_C6000_PREEMPTMAP:
3597       return "C6000_PREEMPTMAP";
3598     case SHT_C6000_ATTRIBUTES:
3599       return "C6000_ATTRIBUTES";
3600     case SHT_TI_ICODE:
3601       return "TI_ICODE";
3602     case SHT_TI_XREF:
3603       return "TI_XREF";
3604     case SHT_TI_HANDLER:
3605       return "TI_HANDLER";
3606     case SHT_TI_INITINFO:
3607       return "TI_INITINFO";
3608     case SHT_TI_PHATTRS:
3609       return "TI_PHATTRS";
3610     default:
3611       break;
3612     }
3613   return NULL;
3614 }
3615
3616 static const char *
3617 get_msp430x_section_type_name (unsigned int sh_type)
3618 {
3619   switch (sh_type)
3620     {
3621     case SHT_MSP430_SEC_FLAGS:   return "MSP430_SEC_FLAGS";
3622     case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
3623     case SHT_MSP430_ATTRIBUTES:  return "MSP430_ATTRIBUTES";
3624     default: return NULL;
3625     }
3626 }
3627
3628 static const char *
3629 get_section_type_name (unsigned int sh_type)
3630 {
3631   static char buff[32];
3632
3633   switch (sh_type)
3634     {
3635     case SHT_NULL:              return "NULL";
3636     case SHT_PROGBITS:          return "PROGBITS";
3637     case SHT_SYMTAB:            return "SYMTAB";
3638     case SHT_STRTAB:            return "STRTAB";
3639     case SHT_RELA:              return "RELA";
3640     case SHT_HASH:              return "HASH";
3641     case SHT_DYNAMIC:           return "DYNAMIC";
3642     case SHT_NOTE:              return "NOTE";
3643     case SHT_NOBITS:            return "NOBITS";
3644     case SHT_REL:               return "REL";
3645     case SHT_SHLIB:             return "SHLIB";
3646     case SHT_DYNSYM:            return "DYNSYM";
3647     case SHT_INIT_ARRAY:        return "INIT_ARRAY";
3648     case SHT_FINI_ARRAY:        return "FINI_ARRAY";
3649     case SHT_PREINIT_ARRAY:     return "PREINIT_ARRAY";
3650     case SHT_GNU_HASH:          return "GNU_HASH";
3651     case SHT_GROUP:             return "GROUP";
3652     case SHT_SYMTAB_SHNDX:      return "SYMTAB SECTION INDICIES";
3653     case SHT_GNU_verdef:        return "VERDEF";
3654     case SHT_GNU_verneed:       return "VERNEED";
3655     case SHT_GNU_versym:        return "VERSYM";
3656     case 0x6ffffff0:            return "VERSYM";
3657     case 0x6ffffffc:            return "VERDEF";
3658     case 0x7ffffffd:            return "AUXILIARY";
3659     case 0x7fffffff:            return "FILTER";
3660     case SHT_GNU_LIBLIST:       return "GNU_LIBLIST";
3661
3662     default:
3663       if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
3664         {
3665           const char * result;
3666
3667           switch (elf_header.e_machine)
3668             {
3669             case EM_MIPS:
3670             case EM_MIPS_RS3_LE:
3671               result = get_mips_section_type_name (sh_type);
3672               break;
3673             case EM_PARISC:
3674               result = get_parisc_section_type_name (sh_type);
3675               break;
3676             case EM_IA_64:
3677               result = get_ia64_section_type_name (sh_type);
3678               break;
3679             case EM_X86_64:
3680             case EM_L1OM:
3681             case EM_K1OM:
3682               result = get_x86_64_section_type_name (sh_type);
3683               break;
3684             case EM_AARCH64:
3685               result = get_aarch64_section_type_name (sh_type);
3686               break;
3687             case EM_ARM:
3688               result = get_arm_section_type_name (sh_type);
3689               break;
3690             case EM_TI_C6000:
3691               result = get_tic6x_section_type_name (sh_type);
3692               break;
3693             case EM_MSP430:
3694               result = get_msp430x_section_type_name (sh_type);
3695               break;
3696             default:
3697               result = NULL;
3698               break;
3699             }
3700
3701           if (result != NULL)
3702             return result;
3703
3704           sprintf (buff, "LOPROC+%x", sh_type - SHT_LOPROC);
3705         }
3706       else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
3707         {
3708           const char * result;
3709
3710           switch (elf_header.e_machine)
3711             {
3712             case EM_IA_64:
3713               result = get_ia64_section_type_name (sh_type);
3714               break;
3715             default:
3716               result = NULL;
3717               break;
3718             }
3719
3720           if (result != NULL)
3721             return result;
3722
3723           sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
3724         }
3725       else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
3726         sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
3727       else
3728         /* This message is probably going to be displayed in a 15
3729            character wide field, so put the hex value first.  */
3730         snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
3731
3732       return buff;
3733     }
3734 }
3735
3736 #define OPTION_DEBUG_DUMP       512
3737 #define OPTION_DYN_SYMS         513
3738 #define OPTION_DWARF_DEPTH      514
3739 #define OPTION_DWARF_START      515
3740 #define OPTION_DWARF_CHECK      516
3741
3742 static struct option options[] =
3743 {
3744   {"all",              no_argument, 0, 'a'},
3745   {"file-header",      no_argument, 0, 'h'},
3746   {"program-headers",  no_argument, 0, 'l'},
3747   {"headers",          no_argument, 0, 'e'},
3748   {"histogram",        no_argument, 0, 'I'},
3749   {"segments",         no_argument, 0, 'l'},
3750   {"sections",         no_argument, 0, 'S'},
3751   {"section-headers",  no_argument, 0, 'S'},
3752   {"section-groups",   no_argument, 0, 'g'},
3753   {"section-details",  no_argument, 0, 't'},
3754   {"full-section-name",no_argument, 0, 'N'},
3755   {"symbols",          no_argument, 0, 's'},
3756   {"syms",             no_argument, 0, 's'},
3757   {"dyn-syms",         no_argument, 0, OPTION_DYN_SYMS},
3758   {"relocs",           no_argument, 0, 'r'},
3759   {"notes",            no_argument, 0, 'n'},
3760   {"dynamic",          no_argument, 0, 'd'},
3761   {"arch-specific",    no_argument, 0, 'A'},
3762   {"version-info",     no_argument, 0, 'V'},
3763   {"use-dynamic",      no_argument, 0, 'D'},
3764   {"unwind",           no_argument, 0, 'u'},
3765   {"archive-index",    no_argument, 0, 'c'},
3766   {"hex-dump",         required_argument, 0, 'x'},
3767   {"relocated-dump",   required_argument, 0, 'R'},
3768   {"string-dump",      required_argument, 0, 'p'},
3769 #ifdef SUPPORT_DISASSEMBLY
3770   {"instruction-dump", required_argument, 0, 'i'},
3771 #endif
3772   {"debug-dump",       optional_argument, 0, OPTION_DEBUG_DUMP},
3773
3774   {"dwarf-depth",      required_argument, 0, OPTION_DWARF_DEPTH},
3775   {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
3776   {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
3777
3778   {"version",          no_argument, 0, 'v'},
3779   {"wide",             no_argument, 0, 'W'},
3780   {"help",             no_argument, 0, 'H'},
3781   {0,                  no_argument, 0, 0}
3782 };
3783
3784 static void
3785 usage (FILE * stream)
3786 {
3787   fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
3788   fprintf (stream, _(" Display information about the contents of ELF format files\n"));
3789   fprintf (stream, _(" Options are:\n\
3790   -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n\
3791   -h --file-header       Display the ELF file header\n\
3792   -l --program-headers   Display the program headers\n\
3793      --segments          An alias for --program-headers\n\
3794   -S --section-headers   Display the sections' header\n\
3795      --sections          An alias for --section-headers\n\
3796   -g --section-groups    Display the section groups\n\
3797   -t --section-details   Display the section details\n\
3798   -e --headers           Equivalent to: -h -l -S\n\
3799   -s --syms              Display the symbol table\n\
3800      --symbols           An alias for --syms\n\
3801   --dyn-syms             Display the dynamic symbol table\n\
3802   -n --notes             Display the core notes (if present)\n\
3803   -r --relocs            Display the relocations (if present)\n\
3804   -u --unwind            Display the unwind info (if present)\n\
3805   -d --dynamic           Display the dynamic section (if present)\n\
3806   -V --version-info      Display the version sections (if present)\n\
3807   -A --arch-specific     Display architecture specific information (if any)\n\
3808   -c --archive-index     Display the symbol/file index in an archive\n\
3809   -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
3810   -x --hex-dump=<number|name>\n\
3811                          Dump the contents of section <number|name> as bytes\n\
3812   -p --string-dump=<number|name>\n\
3813                          Dump the contents of section <number|name> as strings\n\
3814   -R --relocated-dump=<number|name>\n\
3815                          Dump the contents of section <number|name> as relocated bytes\n\
3816   -w[lLiaprmfFsoRt] or\n\
3817   --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
3818                =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
3819                =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
3820                =addr,=cu_index]\n\
3821                          Display the contents of DWARF2 debug sections\n"));
3822   fprintf (stream, _("\
3823   --dwarf-depth=N        Do not display DIEs at depth N or greater\n\
3824   --dwarf-start=N        Display DIEs starting with N, at the same depth\n\
3825                          or deeper\n"));
3826 #ifdef SUPPORT_DISASSEMBLY
3827   fprintf (stream, _("\
3828   -i --instruction-dump=<number|name>\n\
3829                          Disassemble the contents of section <number|name>\n"));
3830 #endif
3831   fprintf (stream, _("\
3832   -I --histogram         Display histogram of bucket list lengths\n\
3833   -W --wide              Allow output width to exceed 80 characters\n\
3834   @<file>                Read options from <file>\n\
3835   -H --help              Display this information\n\
3836   -v --version           Display the version number of readelf\n"));
3837
3838   if (REPORT_BUGS_TO[0] && stream == stdout)
3839     fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
3840
3841   exit (stream == stdout ? 0 : 1);
3842 }
3843
3844 /* Record the fact that the user wants the contents of section number
3845    SECTION to be displayed using the method(s) encoded as flags bits
3846    in TYPE.  Note, TYPE can be zero if we are creating the array for
3847    the first time.  */
3848
3849 static void
3850 request_dump_bynumber (unsigned int section, dump_type type)
3851 {
3852   if (section >= num_dump_sects)
3853     {
3854       dump_type * new_dump_sects;
3855
3856       new_dump_sects = (dump_type *) calloc (section + 1,
3857                                              sizeof (* dump_sects));
3858
3859       if (new_dump_sects == NULL)
3860         error (_("Out of memory allocating dump request table.\n"));
3861       else
3862         {
3863           /* Copy current flag settings.  */
3864           memcpy (new_dump_sects, dump_sects, num_dump_sects * sizeof (* dump_sects));
3865
3866           free (dump_sects);
3867
3868           dump_sects = new_dump_sects;
3869           num_dump_sects = section + 1;
3870         }
3871     }
3872
3873   if (dump_sects)
3874     dump_sects[section] |= type;
3875
3876   return;
3877 }
3878
3879 /* Request a dump by section name.  */
3880
3881 static void
3882 request_dump_byname (const char * section, dump_type type)
3883 {
3884   struct dump_list_entry * new_request;
3885
3886   new_request = (struct dump_list_entry *)
3887       malloc (sizeof (struct dump_list_entry));
3888   if (!new_request)
3889     error (_("Out of memory allocating dump request table.\n"));
3890
3891   new_request->name = strdup (section);
3892   if (!new_request->name)
3893     error (_("Out of memory allocating dump request table.\n"));
3894
3895   new_request->type = type;
3896
3897   new_request->next = dump_sects_byname;
3898   dump_sects_byname = new_request;
3899 }
3900
3901 static inline void
3902 request_dump (dump_type type)
3903 {
3904   int section;
3905   char * cp;
3906
3907   do_dump++;
3908   section = strtoul (optarg, & cp, 0);
3909
3910   if (! *cp && section >= 0)
3911     request_dump_bynumber (section, type);
3912   else
3913     request_dump_byname (optarg, type);
3914 }
3915
3916
3917 static void
3918 parse_args (int argc, char ** argv)
3919 {
3920   int c;
3921
3922   if (argc < 2)
3923     usage (stderr);
3924
3925   while ((c = getopt_long
3926           (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:", options, NULL)) != EOF)
3927     {
3928       switch (c)
3929         {
3930         case 0:
3931           /* Long options.  */
3932           break;
3933         case 'H':
3934           usage (stdout);
3935           break;
3936
3937         case 'a':
3938           do_syms++;
3939           do_reloc++;
3940           do_unwind++;
3941           do_dynamic++;
3942           do_header++;
3943           do_sections++;
3944           do_section_groups++;
3945           do_segments++;
3946           do_version++;
3947           do_histogram++;
3948           do_arch++;
3949           do_notes++;
3950           break;
3951         case 'g':
3952           do_section_groups++;
3953           break;
3954         case 't':
3955         case 'N':
3956           do_sections++;
3957           do_section_details++;
3958           break;
3959         case 'e':
3960           do_header++;
3961           do_sections++;
3962           do_segments++;
3963           break;
3964         case 'A':
3965           do_arch++;
3966           break;
3967         case 'D':
3968           do_using_dynamic++;
3969           break;
3970         case 'r':
3971           do_reloc++;
3972           break;
3973         case 'u':
3974           do_unwind++;
3975           break;
3976         case 'h':
3977           do_header++;
3978           break;
3979         case 'l':
3980           do_segments++;
3981           break;
3982         case 's':
3983           do_syms++;
3984           break;
3985         case 'S':
3986           do_sections++;
3987           break;
3988         case 'd':
3989           do_dynamic++;
3990           break;
3991         case 'I':
3992           do_histogram++;
3993           break;
3994         case 'n':
3995           do_notes++;
3996           break;
3997         case 'c':
3998           do_archive_index++;
3999           break;
4000         case 'x':
4001           request_dump (HEX_DUMP);
4002           break;
4003         case 'p':
4004           request_dump (STRING_DUMP);
4005           break;
4006         case 'R':
4007           request_dump (RELOC_DUMP);
4008           break;
4009         case 'w':
4010           do_dump++;
4011           if (optarg == 0)
4012             {
4013               do_debugging = 1;
4014               dwarf_select_sections_all ();
4015             }
4016           else
4017             {
4018               do_debugging = 0;
4019               dwarf_select_sections_by_letters (optarg);
4020             }
4021           break;
4022         case OPTION_DEBUG_DUMP:
4023           do_dump++;
4024           if (optarg == 0)
4025             do_debugging = 1;
4026           else
4027             {
4028               do_debugging = 0;
4029               dwarf_select_sections_by_names (optarg);
4030             }
4031           break;
4032         case OPTION_DWARF_DEPTH:
4033           {
4034             char *cp;
4035
4036             dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4037           }
4038           break;
4039         case OPTION_DWARF_START:
4040           {
4041             char *cp;
4042
4043             dwarf_start_die = strtoul (optarg, & cp, 0);
4044           }
4045           break;
4046         case OPTION_DWARF_CHECK:
4047           dwarf_check = 1;
4048           break;
4049         case OPTION_DYN_SYMS:
4050           do_dyn_syms++;
4051           break;
4052 #ifdef SUPPORT_DISASSEMBLY
4053         case 'i':
4054           request_dump (DISASS_DUMP);
4055           break;
4056 #endif
4057         case 'v':
4058           print_version (program_name);
4059           break;
4060         case 'V':
4061           do_version++;
4062           break;
4063         case 'W':
4064           do_wide++;
4065           break;
4066         default:
4067           /* xgettext:c-format */
4068           error (_("Invalid option '-%c'\n"), c);
4069           /* Drop through.  */
4070         case '?':
4071           usage (stderr);
4072         }
4073     }
4074
4075   if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
4076       && !do_segments && !do_header && !do_dump && !do_version
4077       && !do_histogram && !do_debugging && !do_arch && !do_notes
4078       && !do_section_groups && !do_archive_index
4079       && !do_dyn_syms)
4080     usage (stderr);
4081   else if (argc < 3)
4082     {
4083       warn (_("Nothing to do.\n"));
4084       usage (stderr);
4085     }
4086 }
4087
4088 static const char *
4089 get_elf_class (unsigned int elf_class)
4090 {
4091   static char buff[32];
4092
4093   switch (elf_class)
4094     {
4095     case ELFCLASSNONE: return _("none");
4096     case ELFCLASS32:   return "ELF32";
4097     case ELFCLASS64:   return "ELF64";
4098     default:
4099       snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
4100       return buff;
4101     }
4102 }
4103
4104 static const char *
4105 get_data_encoding (unsigned int encoding)
4106 {
4107   static char buff[32];
4108
4109   switch (encoding)
4110     {
4111     case ELFDATANONE: return _("none");
4112     case ELFDATA2LSB: return _("2's complement, little endian");
4113     case ELFDATA2MSB: return _("2's complement, big endian");
4114     default:
4115       snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
4116       return buff;
4117     }
4118 }
4119
4120 /* Decode the data held in 'elf_header'.  */
4121
4122 static int
4123 process_file_header (void)
4124 {
4125   if (   elf_header.e_ident[EI_MAG0] != ELFMAG0
4126       || elf_header.e_ident[EI_MAG1] != ELFMAG1
4127       || elf_header.e_ident[EI_MAG2] != ELFMAG2
4128       || elf_header.e_ident[EI_MAG3] != ELFMAG3)
4129     {
4130       error
4131         (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
4132       return 0;
4133     }
4134
4135   init_dwarf_regnames (elf_header.e_machine);
4136
4137   if (do_header)
4138     {
4139       int i;
4140
4141       printf (_("ELF Header:\n"));
4142       printf (_("  Magic:   "));
4143       for (i = 0; i < EI_NIDENT; i++)
4144         printf ("%2.2x ", elf_header.e_ident[i]);
4145       printf ("\n");
4146       printf (_("  Class:                             %s\n"),
4147               get_elf_class (elf_header.e_ident[EI_CLASS]));
4148       printf (_("  Data:                              %s\n"),
4149               get_data_encoding (elf_header.e_ident[EI_DATA]));
4150       printf (_("  Version:                           %d %s\n"),
4151               elf_header.e_ident[EI_VERSION],
4152               (elf_header.e_ident[EI_VERSION] == EV_CURRENT
4153                ? "(current)"
4154                : (elf_header.e_ident[EI_VERSION] != EV_NONE
4155                   ? _("<unknown: %lx>")
4156                   : "")));
4157       printf (_("  OS/ABI:                            %s\n"),
4158               get_osabi_name (elf_header.e_ident[EI_OSABI]));
4159       printf (_("  ABI Version:                       %d\n"),
4160               elf_header.e_ident[EI_ABIVERSION]);
4161       printf (_("  Type:                              %s\n"),
4162               get_file_type (elf_header.e_type));
4163       printf (_("  Machine:                           %s\n"),
4164               get_machine_name (elf_header.e_machine));
4165       printf (_("  Version:                           0x%lx\n"),
4166               (unsigned long) elf_header.e_version);
4167
4168       printf (_("  Entry point address:               "));
4169       print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
4170       printf (_("\n  Start of program headers:          "));
4171       print_vma ((bfd_vma) elf_header.e_phoff, DEC);
4172       printf (_(" (bytes into file)\n  Start of section headers:          "));
4173       print_vma ((bfd_vma) elf_header.e_shoff, DEC);
4174       printf (_(" (bytes into file)\n"));
4175
4176       printf (_("  Flags:                             0x%lx%s\n"),
4177               (unsigned long) elf_header.e_flags,
4178               get_machine_flags (elf_header.e_flags, elf_header.e_machine));
4179       printf (_("  Size of this header:               %ld (bytes)\n"),
4180               (long) elf_header.e_ehsize);
4181       printf (_("  Size of program headers:           %ld (bytes)\n"),
4182               (long) elf_header.e_phentsize);
4183       printf (_("  Number of program headers:         %ld"),
4184               (long) elf_header.e_phnum);
4185       if (section_headers != NULL
4186           && elf_header.e_phnum == PN_XNUM
4187           && section_headers[0].sh_info != 0)
4188         printf (" (%ld)", (long) section_headers[0].sh_info);
4189       putc ('\n', stdout);
4190       printf (_("  Size of section headers:           %ld (bytes)\n"),
4191               (long) elf_header.e_shentsize);
4192       printf (_("  Number of section headers:         %ld"),
4193               (long) elf_header.e_shnum);
4194       if (section_headers != NULL && elf_header.e_shnum == SHN_UNDEF)
4195         printf (" (%ld)", (long) section_headers[0].sh_size);
4196       putc ('\n', stdout);
4197       printf (_("  Section header string table index: %ld"),
4198               (long) elf_header.e_shstrndx);
4199       if (section_headers != NULL
4200           && elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
4201         printf (" (%u)", section_headers[0].sh_link);
4202       else if (elf_header.e_shstrndx != SHN_UNDEF
4203                && elf_header.e_shstrndx >= elf_header.e_shnum)
4204         printf (_(" <corrupt: out of range>"));
4205       putc ('\n', stdout);
4206     }
4207
4208   if (section_headers != NULL)
4209     {
4210       if (elf_header.e_phnum == PN_XNUM
4211           && section_headers[0].sh_info != 0)
4212         elf_header.e_phnum = section_headers[0].sh_info;
4213       if (elf_header.e_shnum == SHN_UNDEF)
4214         elf_header.e_shnum = section_headers[0].sh_size;
4215       if (elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
4216         elf_header.e_shstrndx = section_headers[0].sh_link;
4217       else if (elf_header.e_shstrndx >= elf_header.e_shnum)
4218         elf_header.e_shstrndx = SHN_UNDEF;
4219       free (section_headers);
4220       section_headers = NULL;
4221     }
4222
4223   return 1;
4224 }
4225
4226 static bfd_boolean
4227 get_32bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
4228 {
4229   Elf32_External_Phdr * phdrs;
4230   Elf32_External_Phdr * external;
4231   Elf_Internal_Phdr *   internal;
4232   unsigned int i;
4233   unsigned int size = elf_header.e_phentsize;
4234   unsigned int num  = elf_header.e_phnum;
4235
4236   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4237   if (size == 0 || num == 0)
4238     return FALSE;
4239   if (size < sizeof * phdrs)
4240     {
4241       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4242       return FALSE;
4243     }
4244   if (size > sizeof * phdrs)
4245     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4246
4247   phdrs = (Elf32_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
4248                                             size, num, _("program headers"));
4249   if (phdrs == NULL)
4250     return FALSE;
4251
4252   for (i = 0, internal = pheaders, external = phdrs;
4253        i < elf_header.e_phnum;
4254        i++, internal++, external++)
4255     {
4256       internal->p_type   = BYTE_GET (external->p_type);
4257       internal->p_offset = BYTE_GET (external->p_offset);
4258       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4259       internal->p_paddr  = BYTE_GET (external->p_paddr);
4260       internal->p_filesz = BYTE_GET (external->p_filesz);
4261       internal->p_memsz  = BYTE_GET (external->p_memsz);
4262       internal->p_flags  = BYTE_GET (external->p_flags);
4263       internal->p_align  = BYTE_GET (external->p_align);
4264     }
4265
4266   free (phdrs);
4267   return TRUE;
4268 }
4269
4270 static bfd_boolean
4271 get_64bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
4272 {
4273   Elf64_External_Phdr * phdrs;
4274   Elf64_External_Phdr * external;
4275   Elf_Internal_Phdr *   internal;
4276   unsigned int i;
4277   unsigned int size = elf_header.e_phentsize;
4278   unsigned int num  = elf_header.e_phnum;
4279
4280   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4281   if (size == 0 || num == 0)
4282     return FALSE;
4283   if (size < sizeof * phdrs)
4284     {
4285       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4286       return FALSE;
4287     }
4288   if (size > sizeof * phdrs)
4289     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4290
4291   phdrs = (Elf64_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
4292                                             size, num, _("program headers"));
4293   if (!phdrs)
4294     return FALSE;
4295
4296   for (i = 0, internal = pheaders, external = phdrs;
4297        i < elf_header.e_phnum;
4298        i++, internal++, external++)
4299     {
4300       internal->p_type   = BYTE_GET (external->p_type);
4301       internal->p_flags  = BYTE_GET (external->p_flags);
4302       internal->p_offset = BYTE_GET (external->p_offset);
4303       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4304       internal->p_paddr  = BYTE_GET (external->p_paddr);
4305       internal->p_filesz = BYTE_GET (external->p_filesz);
4306       internal->p_memsz  = BYTE_GET (external->p_memsz);
4307       internal->p_align  = BYTE_GET (external->p_align);
4308     }
4309
4310   free (phdrs);
4311   return TRUE;
4312 }
4313
4314 /* Returns 1 if the program headers were read into `program_headers'.  */
4315
4316 static int
4317 get_program_headers (FILE * file)
4318 {
4319   Elf_Internal_Phdr * phdrs;
4320
4321   /* Check cache of prior read.  */
4322   if (program_headers != NULL)
4323     return 1;
4324
4325   phdrs = (Elf_Internal_Phdr *) cmalloc (elf_header.e_phnum,
4326                                          sizeof (Elf_Internal_Phdr));
4327
4328   if (phdrs == NULL)
4329     {
4330       error (_("Out of memory reading %u program headers\n"),
4331              elf_header.e_phnum);
4332       return 0;
4333     }
4334
4335   if (is_32bit_elf
4336       ? get_32bit_program_headers (file, phdrs)
4337       : get_64bit_program_headers (file, phdrs))
4338     {
4339       program_headers = phdrs;
4340       return 1;
4341     }
4342
4343   free (phdrs);
4344   return 0;
4345 }
4346
4347 /* Returns 1 if the program headers were loaded.  */
4348
4349 static int
4350 process_program_headers (FILE * file)
4351 {
4352   Elf_Internal_Phdr * segment;
4353   unsigned int i;
4354
4355   if (elf_header.e_phnum == 0)
4356     {
4357       /* PR binutils/12467.  */
4358       if (elf_header.e_phoff != 0)
4359         warn (_("possibly corrupt ELF header - it has a non-zero program"
4360                 " header offset, but no program headers\n"));
4361       else if (do_segments)
4362         printf (_("\nThere are no program headers in this file.\n"));
4363       return 0;
4364     }
4365
4366   if (do_segments && !do_header)
4367     {
4368       printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
4369       printf (_("Entry point "));
4370       print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
4371       printf (_("\nThere are %d program headers, starting at offset "),
4372               elf_header.e_phnum);
4373       print_vma ((bfd_vma) elf_header.e_phoff, DEC);
4374       printf ("\n");
4375     }
4376
4377   if (! get_program_headers (file))
4378       return 0;
4379
4380   if (do_segments)
4381     {
4382       if (elf_header.e_phnum > 1)
4383         printf (_("\nProgram Headers:\n"));
4384       else
4385         printf (_("\nProgram Headers:\n"));
4386
4387       if (is_32bit_elf)
4388         printf
4389           (_("  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n"));
4390       else if (do_wide)
4391         printf
4392           (_("  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align\n"));
4393       else
4394         {
4395           printf
4396             (_("  Type           Offset             VirtAddr           PhysAddr\n"));
4397           printf
4398             (_("                 FileSiz            MemSiz              Flags  Align\n"));
4399         }
4400     }
4401
4402   dynamic_addr = 0;
4403   dynamic_size = 0;
4404
4405   for (i = 0, segment = program_headers;
4406        i < elf_header.e_phnum;
4407        i++, segment++)
4408     {
4409       if (do_segments)
4410         {
4411           printf ("  %-14.14s ", get_segment_type (segment->p_type));
4412
4413           if (is_32bit_elf)
4414             {
4415               printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
4416               printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
4417               printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
4418               printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
4419               printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
4420               printf ("%c%c%c ",
4421                       (segment->p_flags & PF_R ? 'R' : ' '),
4422                       (segment->p_flags & PF_W ? 'W' : ' '),
4423                       (segment->p_flags & PF_X ? 'E' : ' '));
4424               printf ("%#lx", (unsigned long) segment->p_align);
4425             }
4426           else if (do_wide)
4427             {
4428               if ((unsigned long) segment->p_offset == segment->p_offset)
4429                 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
4430               else
4431                 {
4432                   print_vma (segment->p_offset, FULL_HEX);
4433                   putchar (' ');
4434                 }
4435
4436               print_vma (segment->p_vaddr, FULL_HEX);
4437               putchar (' ');
4438               print_vma (segment->p_paddr, FULL_HEX);
4439               putchar (' ');
4440
4441               if ((unsigned long) segment->p_filesz == segment->p_filesz)
4442                 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
4443               else
4444                 {
4445                   print_vma (segment->p_filesz, FULL_HEX);
4446                   putchar (' ');
4447                 }
4448
4449               if ((unsigned long) segment->p_memsz == segment->p_memsz)
4450                 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
4451               else
4452                 {
4453                   print_vma (segment->p_memsz, FULL_HEX);
4454                 }
4455
4456               printf (" %c%c%c ",
4457                       (segment->p_flags & PF_R ? 'R' : ' '),
4458                       (segment->p_flags & PF_W ? 'W' : ' '),
4459                       (segment->p_flags & PF_X ? 'E' : ' '));
4460
4461               if ((unsigned long) segment->p_align == segment->p_align)
4462                 printf ("%#lx", (unsigned long) segment->p_align);
4463               else
4464                 {
4465                   print_vma (segment->p_align, PREFIX_HEX);
4466                 }
4467             }
4468           else
4469             {
4470               print_vma (segment->p_offset, FULL_HEX);
4471               putchar (' ');
4472               print_vma (segment->p_vaddr, FULL_HEX);
4473               putchar (' ');
4474               print_vma (segment->p_paddr, FULL_HEX);
4475               printf ("\n                 ");
4476               print_vma (segment->p_filesz, FULL_HEX);
4477               putchar (' ');
4478               print_vma (segment->p_memsz, FULL_HEX);
4479               printf ("  %c%c%c    ",
4480                       (segment->p_flags & PF_R ? 'R' : ' '),
4481                       (segment->p_flags & PF_W ? 'W' : ' '),
4482                       (segment->p_flags & PF_X ? 'E' : ' '));
4483               print_vma (segment->p_align, HEX);
4484             }
4485         }
4486
4487       if (do_segments)
4488         putc ('\n', stdout);
4489
4490       switch (segment->p_type)
4491         {
4492         case PT_DYNAMIC:
4493           if (dynamic_addr)
4494             error (_("more than one dynamic segment\n"));
4495
4496           /* By default, assume that the .dynamic section is the first
4497              section in the DYNAMIC segment.  */
4498           dynamic_addr = segment->p_offset;
4499           dynamic_size = segment->p_filesz;
4500           /* PR binutils/17512: Avoid corrupt dynamic section info in the segment.  */
4501           if (dynamic_addr + dynamic_size >= current_file_size)
4502             {
4503               error (_("the dynamic segment offset + size exceeds the size of the file\n"));
4504               dynamic_addr = dynamic_size = 0;
4505             }
4506
4507           /* Try to locate the .dynamic section. If there is
4508              a section header table, we can easily locate it.  */
4509           if (section_headers != NULL)
4510             {
4511               Elf_Internal_Shdr * sec;
4512
4513               sec = find_section (".dynamic");
4514               if (sec == NULL || sec->sh_size == 0)
4515                 {
4516                   /* A corresponding .dynamic section is expected, but on
4517                      IA-64/OpenVMS it is OK for it to be missing.  */
4518                   if (!is_ia64_vms ())
4519                     error (_("no .dynamic section in the dynamic segment\n"));
4520                   break;
4521                 }
4522
4523               if (sec->sh_type == SHT_NOBITS)
4524                 {
4525                   dynamic_size = 0;
4526                   break;
4527                 }
4528
4529               dynamic_addr = sec->sh_offset;
4530               dynamic_size = sec->sh_size;
4531
4532               if (dynamic_addr < segment->p_offset
4533                   || dynamic_addr > segment->p_offset + segment->p_filesz)
4534                 warn (_("the .dynamic section is not contained"
4535                         " within the dynamic segment\n"));
4536               else if (dynamic_addr > segment->p_offset)
4537                 warn (_("the .dynamic section is not the first section"
4538                         " in the dynamic segment.\n"));
4539             }
4540           break;
4541
4542         case PT_INTERP:
4543           if (fseek (file, archive_file_offset + (long) segment->p_offset,
4544                      SEEK_SET))
4545             error (_("Unable to find program interpreter name\n"));
4546           else
4547             {
4548               char fmt [32];
4549               int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
4550
4551               if (ret >= (int) sizeof (fmt) || ret < 0)
4552                 error (_("Internal error: failed to create format string to display program interpreter\n"));
4553
4554               program_interpreter[0] = 0;
4555               if (fscanf (file, fmt, program_interpreter) <= 0)
4556                 error (_("Unable to read program interpreter name\n"));
4557
4558               if (do_segments)
4559                 printf (_("      [Requesting program interpreter: %s]\n"),
4560                     program_interpreter);
4561             }
4562           break;
4563         }
4564     }
4565
4566   if (do_segments && section_headers != NULL && string_table != NULL)
4567     {
4568       printf (_("\n Section to Segment mapping:\n"));
4569       printf (_("  Segment Sections...\n"));
4570
4571       for (i = 0; i < elf_header.e_phnum; i++)
4572         {
4573           unsigned int j;
4574           Elf_Internal_Shdr * section;
4575
4576           segment = program_headers + i;
4577           section = section_headers + 1;
4578
4579           printf ("   %2.2d     ", i);
4580
4581           for (j = 1; j < elf_header.e_shnum; j++, section++)
4582             {
4583               if (!ELF_TBSS_SPECIAL (section, segment)
4584                   && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
4585                 printf ("%s ", printable_section_name (section));
4586             }
4587
4588           putc ('\n',stdout);
4589         }
4590     }
4591
4592   return 1;
4593 }
4594
4595
4596 /* Find the file offset corresponding to VMA by using the program headers.  */
4597
4598 static long
4599 offset_from_vma (FILE * file, bfd_vma vma, bfd_size_type size)
4600 {
4601   Elf_Internal_Phdr * seg;
4602
4603   if (! get_program_headers (file))
4604     {
4605       warn (_("Cannot interpret virtual addresses without program headers.\n"));
4606       return (long) vma;
4607     }
4608
4609   for (seg = program_headers;
4610        seg < program_headers + elf_header.e_phnum;
4611        ++seg)
4612     {
4613       if (seg->p_type != PT_LOAD)
4614         continue;
4615
4616       if (vma >= (seg->p_vaddr & -seg->p_align)
4617           && vma + size <= seg->p_vaddr + seg->p_filesz)
4618         return vma - seg->p_vaddr + seg->p_offset;
4619     }
4620
4621   warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
4622         (unsigned long) vma);
4623   return (long) vma;
4624 }
4625
4626
4627 /* Allocate memory and load the sections headers into the global pointer
4628    SECTION_HEADERS.  If PROBE is true, this is just a probe and we do not
4629    generate any error messages if the load fails.  */
4630
4631 static bfd_boolean
4632 get_32bit_section_headers (FILE * file, bfd_boolean probe)
4633 {
4634   Elf32_External_Shdr * shdrs;
4635   Elf_Internal_Shdr *   internal;
4636   unsigned int i;
4637   unsigned int size = elf_header.e_shentsize;
4638   unsigned int num = probe ? 1 : elf_header.e_shnum;
4639
4640   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4641   if (size == 0 || num == 0)
4642     return FALSE;
4643   if (size < sizeof * shdrs)
4644     {
4645       if (! probe)
4646         error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
4647       return FALSE;
4648     }
4649   if (!probe && size > sizeof * shdrs)
4650     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
4651
4652   shdrs = (Elf32_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
4653                                             size, num,
4654                                             probe ? NULL : _("section headers"));
4655   if (shdrs == NULL)
4656     return FALSE;
4657
4658   if (section_headers != NULL)
4659     free (section_headers);
4660   section_headers = (Elf_Internal_Shdr *) cmalloc (num,
4661                                                    sizeof (Elf_Internal_Shdr));
4662   if (section_headers == NULL)
4663     {
4664       if (!probe)
4665         error (_("Out of memory reading %u section headers\n"), num);
4666       return FALSE;
4667     }
4668
4669   for (i = 0, internal = section_headers;
4670        i < num;
4671        i++, internal++)
4672     {
4673       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
4674       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
4675       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
4676       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
4677       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
4678       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
4679       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
4680       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
4681       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
4682       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
4683     }
4684
4685   free (shdrs);
4686   return TRUE;
4687 }
4688
4689 static bfd_boolean
4690 get_64bit_section_headers (FILE * file, bfd_boolean probe)
4691 {
4692   Elf64_External_Shdr * shdrs;
4693   Elf_Internal_Shdr *   internal;
4694   unsigned int i;
4695   unsigned int size = elf_header.e_shentsize;
4696   unsigned int num = probe ? 1 : elf_header.e_shnum;
4697
4698   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4699   if (size == 0 || num == 0)
4700     return FALSE;
4701   if (size < sizeof * shdrs)
4702     {
4703       if (! probe)
4704         error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
4705       return FALSE;
4706     }
4707   if (! probe && size > sizeof * shdrs)
4708     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
4709
4710   shdrs = (Elf64_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
4711                                             size, num,
4712                                             probe ? NULL : _("section headers"));
4713   if (shdrs == NULL)
4714     return FALSE;
4715
4716   if (section_headers != NULL)
4717     free (section_headers);
4718   section_headers = (Elf_Internal_Shdr *) cmalloc (num,
4719                                                    sizeof (Elf_Internal_Shdr));
4720   if (section_headers == NULL)
4721     {
4722       if (! probe)
4723         error (_("Out of memory reading %u section headers\n"), num);
4724       return FALSE;
4725     }
4726
4727   for (i = 0, internal = section_headers;
4728        i < num;
4729        i++, internal++)
4730     {
4731       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
4732       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
4733       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
4734       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
4735       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
4736       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
4737       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
4738       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
4739       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
4740       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
4741     }
4742
4743   free (shdrs);
4744   return TRUE;
4745 }
4746
4747 static Elf_Internal_Sym *
4748 get_32bit_elf_symbols (FILE * file,
4749                        Elf_Internal_Shdr * section,
4750                        unsigned long * num_syms_return)
4751 {
4752   unsigned long number = 0;
4753   Elf32_External_Sym * esyms = NULL;
4754   Elf_External_Sym_Shndx * shndx = NULL;
4755   Elf_Internal_Sym * isyms = NULL;
4756   Elf_Internal_Sym * psym;
4757   unsigned int j;
4758
4759   if (section->sh_size == 0)
4760     {
4761       if (num_syms_return != NULL)
4762         * num_syms_return = 0;
4763       return NULL;
4764     }
4765
4766   /* Run some sanity checks first.  */
4767   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
4768     {
4769       error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
4770              printable_section_name (section), (unsigned long) section->sh_entsize);
4771       goto exit_point;
4772     }
4773
4774   if (section->sh_size > current_file_size)
4775     {
4776       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
4777              printable_section_name (section), (unsigned long) section->sh_size);
4778       goto exit_point;
4779     }
4780
4781   number = section->sh_size / section->sh_entsize;
4782
4783   if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
4784     {
4785       error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
4786              (unsigned long) section->sh_size,
4787              printable_section_name (section),
4788              (unsigned long) section->sh_entsize);
4789       goto exit_point;
4790     }
4791
4792   esyms = (Elf32_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
4793                                            section->sh_size, _("symbols"));
4794   if (esyms == NULL)
4795     goto exit_point;
4796
4797   shndx = NULL;
4798   if (symtab_shndx_hdr != NULL
4799       && (symtab_shndx_hdr->sh_link
4800           == (unsigned long) (section - section_headers)))
4801     {
4802       shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
4803                                                    symtab_shndx_hdr->sh_offset,
4804                                                    1, symtab_shndx_hdr->sh_size,
4805                                                    _("symbol table section indicies"));
4806       if (shndx == NULL)
4807         goto exit_point;
4808       /* PR17531: file: heap-buffer-overflow */
4809       else if (symtab_shndx_hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
4810         {
4811           error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
4812                  printable_section_name (symtab_shndx_hdr),
4813                  (unsigned long) symtab_shndx_hdr->sh_size,
4814                  (unsigned long) section->sh_size);
4815           goto exit_point;
4816         }
4817     }
4818
4819   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
4820
4821   if (isyms == NULL)
4822     {
4823       error (_("Out of memory reading %lu symbols\n"),
4824              (unsigned long) number);
4825       goto exit_point;
4826     }
4827
4828   for (j = 0, psym = isyms; j < number; j++, psym++)
4829     {
4830       psym->st_name  = BYTE_GET (esyms[j].st_name);
4831       psym->st_value = BYTE_GET (esyms[j].st_value);
4832       psym->st_size  = BYTE_GET (esyms[j].st_size);
4833       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
4834       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
4835         psym->st_shndx
4836           = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4837       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
4838         psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
4839       psym->st_info  = BYTE_GET (esyms[j].st_info);
4840       psym->st_other = BYTE_GET (esyms[j].st_other);
4841     }
4842
4843  exit_point:
4844   if (shndx != NULL)
4845     free (shndx);
4846   if (esyms != NULL)
4847     free (esyms);
4848
4849   if (num_syms_return != NULL)
4850     * num_syms_return = isyms == NULL ? 0 : number;
4851
4852   return isyms;
4853 }
4854
4855 static Elf_Internal_Sym *
4856 get_64bit_elf_symbols (FILE * file,
4857                        Elf_Internal_Shdr * section,
4858                        unsigned long * num_syms_return)
4859 {
4860   unsigned long number = 0;
4861   Elf64_External_Sym * esyms = NULL;
4862   Elf_External_Sym_Shndx * shndx = NULL;
4863   Elf_Internal_Sym * isyms = NULL;
4864   Elf_Internal_Sym * psym;
4865   unsigned int j;
4866
4867   if (section->sh_size == 0)
4868     {
4869       if (num_syms_return != NULL)
4870         * num_syms_return = 0;
4871       return NULL;
4872     }
4873
4874   /* Run some sanity checks first.  */
4875   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
4876     {
4877       error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
4878              printable_section_name (section),
4879              (unsigned long) section->sh_entsize);
4880       goto exit_point;
4881     }
4882
4883   if (section->sh_size > current_file_size)
4884     {
4885       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
4886              printable_section_name (section),
4887              (unsigned long) section->sh_size);
4888       goto exit_point;
4889     }
4890
4891   number = section->sh_size / section->sh_entsize;
4892
4893   if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
4894     {
4895       error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
4896              (unsigned long) section->sh_size,
4897              printable_section_name (section),
4898              (unsigned long) section->sh_entsize);
4899       goto exit_point;
4900     }
4901
4902   esyms = (Elf64_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
4903                                            section->sh_size, _("symbols"));
4904   if (!esyms)
4905     goto exit_point;
4906
4907   if (symtab_shndx_hdr != NULL
4908       && (symtab_shndx_hdr->sh_link
4909           == (unsigned long) (section - section_headers)))
4910     {
4911       shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
4912                                                    symtab_shndx_hdr->sh_offset,
4913                                                    1, symtab_shndx_hdr->sh_size,
4914                                                    _("symbol table section indicies"));
4915       if (shndx == NULL)
4916         goto exit_point;
4917       else if (symtab_shndx_hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
4918         {
4919           error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
4920                  printable_section_name (symtab_shndx_hdr),
4921                  (unsigned long) symtab_shndx_hdr->sh_size,
4922                  (unsigned long) section->sh_size);
4923           goto exit_point;
4924         }
4925     }
4926
4927   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
4928
4929   if (isyms == NULL)
4930     {
4931       error (_("Out of memory reading %lu symbols\n"),
4932              (unsigned long) number);
4933       goto exit_point;
4934     }
4935
4936   for (j = 0, psym = isyms; j < number; j++, psym++)
4937     {
4938       psym->st_name  = BYTE_GET (esyms[j].st_name);
4939       psym->st_info  = BYTE_GET (esyms[j].st_info);
4940       psym->st_other = BYTE_GET (esyms[j].st_other);
4941       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
4942
4943       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
4944         psym->st_shndx
4945           = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4946       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
4947         psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
4948
4949       psym->st_value = BYTE_GET (esyms[j].st_value);
4950       psym->st_size  = BYTE_GET (esyms[j].st_size);
4951     }
4952
4953  exit_point:
4954   if (shndx != NULL)
4955     free (shndx);
4956   if (esyms != NULL)
4957     free (esyms);
4958
4959   if (num_syms_return != NULL)
4960     * num_syms_return = isyms == NULL ? 0 : number;
4961
4962   return isyms;
4963 }
4964
4965 static const char *
4966 get_elf_section_flags (bfd_vma sh_flags)
4967 {
4968   static char buff[1024];
4969   char * p = buff;
4970   int field_size = is_32bit_elf ? 8 : 16;
4971   int sindex;
4972   int size = sizeof (buff) - (field_size + 4 + 1);
4973   bfd_vma os_flags = 0;
4974   bfd_vma proc_flags = 0;
4975   bfd_vma unknown_flags = 0;
4976   static const struct
4977     {
4978       const char * str;
4979       int len;
4980     }
4981   flags [] =
4982     {
4983       /*  0 */ { STRING_COMMA_LEN ("WRITE") },
4984       /*  1 */ { STRING_COMMA_LEN ("ALLOC") },
4985       /*  2 */ { STRING_COMMA_LEN ("EXEC") },
4986       /*  3 */ { STRING_COMMA_LEN ("MERGE") },
4987       /*  4 */ { STRING_COMMA_LEN ("STRINGS") },
4988       /*  5 */ { STRING_COMMA_LEN ("INFO LINK") },
4989       /*  6 */ { STRING_COMMA_LEN ("LINK ORDER") },
4990       /*  7 */ { STRING_COMMA_LEN ("OS NONCONF") },
4991       /*  8 */ { STRING_COMMA_LEN ("GROUP") },
4992       /*  9 */ { STRING_COMMA_LEN ("TLS") },
4993       /* IA-64 specific.  */
4994       /* 10 */ { STRING_COMMA_LEN ("SHORT") },
4995       /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
4996       /* IA-64 OpenVMS specific.  */
4997       /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
4998       /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
4999       /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
5000       /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
5001       /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
5002       /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
5003       /* Generic.  */
5004       /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
5005       /* SPARC specific.  */
5006       /* 19 */ { STRING_COMMA_LEN ("ORDERED") }
5007     };
5008
5009   if (do_section_details)
5010     {
5011       sprintf (buff, "[%*.*lx]: ",
5012                field_size, field_size, (unsigned long) sh_flags);
5013       p += field_size + 4;
5014     }
5015
5016   while (sh_flags)
5017     {
5018       bfd_vma flag;
5019
5020       flag = sh_flags & - sh_flags;
5021       sh_flags &= ~ flag;
5022
5023       if (do_section_details)
5024         {
5025           switch (flag)
5026             {
5027             case SHF_WRITE:             sindex = 0; break;
5028             case SHF_ALLOC:             sindex = 1; break;
5029             case SHF_EXECINSTR:         sindex = 2; break;
5030             case SHF_MERGE:             sindex = 3; break;
5031             case SHF_STRINGS:           sindex = 4; break;
5032             case SHF_INFO_LINK:         sindex = 5; break;
5033             case SHF_LINK_ORDER:        sindex = 6; break;
5034             case SHF_OS_NONCONFORMING:  sindex = 7; break;
5035             case SHF_GROUP:             sindex = 8; break;
5036             case SHF_TLS:               sindex = 9; break;
5037             case SHF_EXCLUDE:           sindex = 18; break;
5038
5039             default:
5040               sindex = -1;
5041               switch (elf_header.e_machine)
5042                 {
5043                 case EM_IA_64:
5044                   if (flag == SHF_IA_64_SHORT)
5045                     sindex = 10;
5046                   else if (flag == SHF_IA_64_NORECOV)
5047                     sindex = 11;
5048 #ifdef BFD64
5049                   else if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
5050                     switch (flag)
5051                       {
5052                       case SHF_IA_64_VMS_GLOBAL:      sindex = 12; break;
5053                       case SHF_IA_64_VMS_OVERLAID:    sindex = 13; break;
5054                       case SHF_IA_64_VMS_SHARED:      sindex = 14; break;
5055                       case SHF_IA_64_VMS_VECTOR:      sindex = 15; break;
5056                       case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
5057                       case SHF_IA_64_VMS_PROTECTED:   sindex = 17; break;
5058                       default:                        break;
5059                       }
5060 #endif
5061                   break;
5062
5063                 case EM_386:
5064                 case EM_486:
5065                 case EM_X86_64:
5066                 case EM_L1OM:
5067                 case EM_K1OM:
5068                 case EM_OLD_SPARCV9:
5069                 case EM_SPARC32PLUS:
5070                 case EM_SPARCV9:
5071                 case EM_SPARC:
5072                   if (flag == SHF_ORDERED)
5073                     sindex = 19;
5074                   break;
5075                 default:
5076                   break;
5077                 }
5078             }
5079
5080           if (sindex != -1)
5081             {
5082               if (p != buff + field_size + 4)
5083                 {
5084                   if (size < (10 + 2))
5085                     {
5086                       warn (_("Internal error: not enough buffer room for section flag info"));
5087                       return _("<unknown>");
5088                     }
5089                   size -= 2;
5090                   *p++ = ',';
5091                   *p++ = ' ';
5092                 }
5093
5094               size -= flags [sindex].len;
5095               p = stpcpy (p, flags [sindex].str);
5096             }
5097           else if (flag & SHF_MASKOS)
5098             os_flags |= flag;
5099           else if (flag & SHF_MASKPROC)
5100             proc_flags |= flag;
5101           else
5102             unknown_flags |= flag;
5103         }
5104       else
5105         {
5106           switch (flag)
5107             {
5108             case SHF_WRITE:             *p = 'W'; break;
5109             case SHF_ALLOC:             *p = 'A'; break;
5110             case SHF_EXECINSTR:         *p = 'X'; break;
5111             case SHF_MERGE:             *p = 'M'; break;
5112             case SHF_STRINGS:           *p = 'S'; break;
5113             case SHF_INFO_LINK:         *p = 'I'; break;
5114             case SHF_LINK_ORDER:        *p = 'L'; break;
5115             case SHF_OS_NONCONFORMING:  *p = 'O'; break;
5116             case SHF_GROUP:             *p = 'G'; break;
5117             case SHF_TLS:               *p = 'T'; break;
5118             case SHF_EXCLUDE:           *p = 'E'; break;
5119
5120             default:
5121               if ((elf_header.e_machine == EM_X86_64
5122                    || elf_header.e_machine == EM_L1OM
5123                    || elf_header.e_machine == EM_K1OM)
5124                   && flag == SHF_X86_64_LARGE)
5125                 *p = 'l';
5126               else if (flag & SHF_MASKOS)
5127                 {
5128                   *p = 'o';
5129                   sh_flags &= ~ SHF_MASKOS;
5130                 }
5131               else if (flag & SHF_MASKPROC)
5132                 {
5133                   *p = 'p';
5134                   sh_flags &= ~ SHF_MASKPROC;
5135                 }
5136               else
5137                 *p = 'x';
5138               break;
5139             }
5140           p++;
5141         }
5142     }
5143
5144   if (do_section_details)
5145     {
5146       if (os_flags)
5147         {
5148           size -= 5 + field_size;
5149           if (p != buff + field_size + 4)
5150             {
5151               if (size < (2 + 1))
5152                 {
5153                   warn (_("Internal error: not enough buffer room for section flag info"));
5154                   return _("<unknown>");
5155                 }
5156               size -= 2;
5157               *p++ = ',';
5158               *p++ = ' ';
5159             }
5160           sprintf (p, "OS (%*.*lx)", field_size, field_size,
5161                    (unsigned long) os_flags);
5162           p += 5 + field_size;
5163         }
5164       if (proc_flags)
5165         {
5166           size -= 7 + field_size;
5167           if (p != buff + field_size + 4)
5168             {
5169               if (size < (2 + 1))
5170                 {
5171                   warn (_("Internal error: not enough buffer room for section flag info"));
5172                   return _("<unknown>");
5173                 }
5174               size -= 2;
5175               *p++ = ',';
5176               *p++ = ' ';
5177             }
5178           sprintf (p, "PROC (%*.*lx)", field_size, field_size,
5179                    (unsigned long) proc_flags);
5180           p += 7 + field_size;
5181         }
5182       if (unknown_flags)
5183         {
5184           size -= 10 + field_size;
5185           if (p != buff + field_size + 4)
5186             {
5187               if (size < (2 + 1))
5188                 {
5189                   warn (_("Internal error: not enough buffer room for section flag info"));
5190                   return _("<unknown>");
5191                 }
5192               size -= 2;
5193               *p++ = ',';
5194               *p++ = ' ';
5195             }
5196           sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
5197                    (unsigned long) unknown_flags);
5198           p += 10 + field_size;
5199         }
5200     }
5201
5202   *p = '\0';
5203   return buff;
5204 }
5205
5206 static int
5207 process_section_headers (FILE * file)
5208 {
5209   Elf_Internal_Shdr * section;
5210   unsigned int i;
5211
5212   section_headers = NULL;
5213
5214   if (elf_header.e_shnum == 0)
5215     {
5216       /* PR binutils/12467.  */
5217       if (elf_header.e_shoff != 0)
5218         warn (_("possibly corrupt ELF file header - it has a non-zero"
5219                 " section header offset, but no section headers\n"));
5220       else if (do_sections)
5221         printf (_("\nThere are no sections in this file.\n"));
5222
5223       return 1;
5224     }
5225
5226   if (do_sections && !do_header)
5227     printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
5228             elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
5229
5230   if (is_32bit_elf)
5231     {
5232       if (! get_32bit_section_headers (file, FALSE))
5233         return 0;
5234     }
5235   else if (! get_64bit_section_headers (file, FALSE))
5236     return 0;
5237
5238   /* Read in the string table, so that we have names to display.  */
5239   if (elf_header.e_shstrndx != SHN_UNDEF
5240        && elf_header.e_shstrndx < elf_header.e_shnum)
5241     {
5242       section = section_headers + elf_header.e_shstrndx;
5243
5244       if (section->sh_size != 0)
5245         {
5246           string_table = (char *) get_data (NULL, file, section->sh_offset,
5247                                             1, section->sh_size,
5248                                             _("string table"));
5249
5250           string_table_length = string_table != NULL ? section->sh_size : 0;
5251         }
5252     }
5253
5254   /* Scan the sections for the dynamic symbol table
5255      and dynamic string table and debug sections.  */
5256   dynamic_symbols = NULL;
5257   dynamic_strings = NULL;
5258   dynamic_syminfo = NULL;
5259   symtab_shndx_hdr = NULL;
5260
5261   eh_addr_size = is_32bit_elf ? 4 : 8;
5262   switch (elf_header.e_machine)
5263     {
5264     case EM_MIPS:
5265     case EM_MIPS_RS3_LE:
5266       /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
5267          FDE addresses.  However, the ABI also has a semi-official ILP32
5268          variant for which the normal FDE address size rules apply.
5269
5270          GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
5271          section, where XX is the size of longs in bits.  Unfortunately,
5272          earlier compilers provided no way of distinguishing ILP32 objects
5273          from LP64 objects, so if there's any doubt, we should assume that
5274          the official LP64 form is being used.  */
5275       if ((elf_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
5276           && find_section (".gcc_compiled_long32") == NULL)
5277         eh_addr_size = 8;
5278       break;
5279
5280     case EM_H8_300:
5281     case EM_H8_300H:
5282       switch (elf_header.e_flags & EF_H8_MACH)
5283         {
5284         case E_H8_MACH_H8300:
5285         case E_H8_MACH_H8300HN:
5286         case E_H8_MACH_H8300SN:
5287         case E_H8_MACH_H8300SXN:
5288           eh_addr_size = 2;
5289           break;
5290         case E_H8_MACH_H8300H:
5291         case E_H8_MACH_H8300S:
5292         case E_H8_MACH_H8300SX:
5293           eh_addr_size = 4;
5294           break;
5295         }
5296       break;
5297
5298     case EM_M32C_OLD:
5299     case EM_M32C:
5300       switch (elf_header.e_flags & EF_M32C_CPU_MASK)
5301         {
5302         case EF_M32C_CPU_M16C:
5303           eh_addr_size = 2;
5304           break;
5305         }
5306       break;
5307     }
5308
5309 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64)                \
5310   do                                                                    \
5311     {                                                                   \
5312       bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64;  \
5313       if (section->sh_entsize != expected_entsize)                      \
5314         {                                                               \
5315           char buf[40];                                                 \
5316           sprintf_vma (buf, section->sh_entsize);                       \
5317           /* Note: coded this way so that there is a single string for  \
5318              translation.  */ \
5319           error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
5320           error (_("(Using the expected size of %u for the rest of this dump)\n"), \
5321                    (unsigned) expected_entsize);                        \
5322           section->sh_entsize = expected_entsize;                       \
5323         }                                                               \
5324     }                                                                   \
5325   while (0)
5326
5327 #define CHECK_ENTSIZE(section, i, type)                                 \
5328   CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type),         \
5329                         sizeof (Elf64_External_##type))
5330
5331   for (i = 0, section = section_headers;
5332        i < elf_header.e_shnum;
5333        i++, section++)
5334     {
5335       char * name = SECTION_NAME (section);
5336
5337       if (section->sh_type == SHT_DYNSYM)
5338         {
5339           if (dynamic_symbols != NULL)
5340             {
5341               error (_("File contains multiple dynamic symbol tables\n"));
5342               continue;
5343             }
5344
5345           CHECK_ENTSIZE (section, i, Sym);
5346           dynamic_symbols = GET_ELF_SYMBOLS (file, section, & num_dynamic_syms);
5347         }
5348       else if (section->sh_type == SHT_STRTAB
5349                && streq (name, ".dynstr"))
5350         {
5351           if (dynamic_strings != NULL)
5352             {
5353               error (_("File contains multiple dynamic string tables\n"));
5354               continue;
5355             }
5356
5357           dynamic_strings = (char *) get_data (NULL, file, section->sh_offset,
5358                                                1, section->sh_size,
5359                                                _("dynamic strings"));
5360           dynamic_strings_length = dynamic_strings == NULL ? 0 : section->sh_size;
5361         }
5362       else if (section->sh_type == SHT_SYMTAB_SHNDX)
5363         {
5364           if (symtab_shndx_hdr != NULL)
5365             {
5366               error (_("File contains multiple symtab shndx tables\n"));
5367               continue;
5368             }
5369           symtab_shndx_hdr = section;
5370         }
5371       else if (section->sh_type == SHT_SYMTAB)
5372         CHECK_ENTSIZE (section, i, Sym);
5373       else if (section->sh_type == SHT_GROUP)
5374         CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
5375       else if (section->sh_type == SHT_REL)
5376         CHECK_ENTSIZE (section, i, Rel);
5377       else if (section->sh_type == SHT_RELA)
5378         CHECK_ENTSIZE (section, i, Rela);
5379       else if ((do_debugging || do_debug_info || do_debug_abbrevs
5380                 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
5381                 || do_debug_aranges || do_debug_frames || do_debug_macinfo
5382                 || do_debug_str || do_debug_loc || do_debug_ranges
5383                 || do_debug_addr || do_debug_cu_index)
5384                && (const_strneq (name, ".debug_")
5385                    || const_strneq (name, ".zdebug_")))
5386         {
5387           if (name[1] == 'z')
5388             name += sizeof (".zdebug_") - 1;
5389           else
5390             name += sizeof (".debug_") - 1;
5391
5392           if (do_debugging
5393               || (do_debug_info     && const_strneq (name, "info"))
5394               || (do_debug_info     && const_strneq (name, "types"))
5395               || (do_debug_abbrevs  && const_strneq (name, "abbrev"))
5396               || (do_debug_lines    && strcmp (name, "line") == 0)
5397               || (do_debug_lines    && const_strneq (name, "line."))
5398               || (do_debug_pubnames && const_strneq (name, "pubnames"))
5399               || (do_debug_pubtypes && const_strneq (name, "pubtypes"))
5400               || (do_debug_pubnames && const_strneq (name, "gnu_pubnames"))
5401               || (do_debug_pubtypes && const_strneq (name, "gnu_pubtypes"))
5402               || (do_debug_aranges  && const_strneq (name, "aranges"))
5403               || (do_debug_ranges   && const_strneq (name, "ranges"))
5404               || (do_debug_frames   && const_strneq (name, "frame"))
5405               || (do_debug_macinfo  && const_strneq (name, "macinfo"))
5406               || (do_debug_macinfo  && const_strneq (name, "macro"))
5407               || (do_debug_str      && const_strneq (name, "str"))
5408               || (do_debug_loc      && const_strneq (name, "loc"))
5409               || (do_debug_addr     && const_strneq (name, "addr"))
5410               || (do_debug_cu_index && const_strneq (name, "cu_index"))
5411               || (do_debug_cu_index && const_strneq (name, "tu_index"))
5412               )
5413             request_dump_bynumber (i, DEBUG_DUMP);
5414         }
5415       /* Linkonce section to be combined with .debug_info at link time.  */
5416       else if ((do_debugging || do_debug_info)
5417                && const_strneq (name, ".gnu.linkonce.wi."))
5418         request_dump_bynumber (i, DEBUG_DUMP);
5419       else if (do_debug_frames && streq (name, ".eh_frame"))
5420         request_dump_bynumber (i, DEBUG_DUMP);
5421       else if (do_gdb_index && streq (name, ".gdb_index"))
5422         request_dump_bynumber (i, DEBUG_DUMP);
5423       /* Trace sections for Itanium VMS.  */
5424       else if ((do_debugging || do_trace_info || do_trace_abbrevs
5425                 || do_trace_aranges)
5426                && const_strneq (name, ".trace_"))
5427         {
5428           name += sizeof (".trace_") - 1;
5429
5430           if (do_debugging
5431               || (do_trace_info     && streq (name, "info"))
5432               || (do_trace_abbrevs  && streq (name, "abbrev"))
5433               || (do_trace_aranges  && streq (name, "aranges"))
5434               )
5435             request_dump_bynumber (i, DEBUG_DUMP);
5436         }
5437     }
5438
5439   if (! do_sections)
5440     return 1;
5441
5442   if (elf_header.e_shnum > 1)
5443     printf (_("\nSection Headers:\n"));
5444   else
5445     printf (_("\nSection Header:\n"));
5446
5447   if (is_32bit_elf)
5448     {
5449       if (do_section_details)
5450         {
5451           printf (_("  [Nr] Name\n"));
5452           printf (_("       Type            Addr     Off    Size   ES   Lk Inf Al\n"));
5453         }
5454       else
5455         printf
5456           (_("  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al\n"));
5457     }
5458   else if (do_wide)
5459     {
5460       if (do_section_details)
5461         {
5462           printf (_("  [Nr] Name\n"));
5463           printf (_("       Type            Address          Off    Size   ES   Lk Inf Al\n"));
5464         }
5465       else
5466         printf
5467           (_("  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al\n"));
5468     }
5469   else
5470     {
5471       if (do_section_details)
5472         {
5473           printf (_("  [Nr] Name\n"));
5474           printf (_("       Type              Address          Offset            Link\n"));
5475           printf (_("       Size              EntSize          Info              Align\n"));
5476         }
5477       else
5478         {
5479           printf (_("  [Nr] Name              Type             Address           Offset\n"));
5480           printf (_("       Size              EntSize          Flags  Link  Info  Align\n"));
5481         }
5482     }
5483
5484   if (do_section_details)
5485     printf (_("       Flags\n"));
5486
5487   for (i = 0, section = section_headers;
5488        i < elf_header.e_shnum;
5489        i++, section++)
5490     {
5491       printf ("  [%2u] ", i);
5492       if (do_section_details)
5493         printf ("%s\n      ", printable_section_name (section));
5494       else
5495         print_symbol (-17, SECTION_NAME (section));
5496
5497       printf (do_wide ? " %-15s " : " %-15.15s ",
5498               get_section_type_name (section->sh_type));
5499
5500       if (is_32bit_elf)
5501         {
5502           const char * link_too_big = NULL;
5503
5504           print_vma (section->sh_addr, LONG_HEX);
5505
5506           printf ( " %6.6lx %6.6lx %2.2lx",
5507                    (unsigned long) section->sh_offset,
5508                    (unsigned long) section->sh_size,
5509                    (unsigned long) section->sh_entsize);
5510
5511           if (do_section_details)
5512             fputs ("  ", stdout);
5513           else
5514             printf (" %3s ", get_elf_section_flags (section->sh_flags));
5515
5516           if (section->sh_link >= elf_header.e_shnum)
5517             {
5518               link_too_big = "";
5519               /* The sh_link value is out of range.  Normally this indicates
5520                  an error but it can have special values in Solaris binaries.  */
5521               switch (elf_header.e_machine)
5522                 {
5523                 case EM_386:
5524                 case EM_486:
5525                 case EM_X86_64:
5526                 case EM_L1OM:
5527                 case EM_K1OM:
5528                 case EM_OLD_SPARCV9:
5529                 case EM_SPARC32PLUS:
5530                 case EM_SPARCV9:
5531                 case EM_SPARC:
5532                   if (section->sh_link == (SHN_BEFORE & 0xffff))
5533                     link_too_big = "BEFORE";
5534                   else if (section->sh_link == (SHN_AFTER & 0xffff))
5535                     link_too_big = "AFTER";
5536                   break;
5537                 default:
5538                   break;
5539                 }
5540             }
5541
5542           if (do_section_details)
5543             {
5544               if (link_too_big != NULL && * link_too_big)
5545                 printf ("<%s> ", link_too_big);
5546               else
5547                 printf ("%2u ", section->sh_link);
5548               printf ("%3u %2lu\n", section->sh_info,
5549                       (unsigned long) section->sh_addralign);
5550             }
5551           else
5552             printf ("%2u %3u %2lu\n",
5553                     section->sh_link,
5554                     section->sh_info,
5555                     (unsigned long) section->sh_addralign);
5556
5557           if (link_too_big && ! * link_too_big)
5558             warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
5559                   i, section->sh_link);
5560         }
5561       else if (do_wide)
5562         {
5563           print_vma (section->sh_addr, LONG_HEX);
5564
5565           if ((long) section->sh_offset == section->sh_offset)
5566             printf (" %6.6lx", (unsigned long) section->sh_offset);
5567           else
5568             {
5569               putchar (' ');
5570               print_vma (section->sh_offset, LONG_HEX);
5571             }
5572
5573           if ((unsigned long) section->sh_size == section->sh_size)
5574             printf (" %6.6lx", (unsigned long) section->sh_size);
5575           else
5576             {
5577               putchar (' ');
5578               print_vma (section->sh_size, LONG_HEX);
5579             }
5580
5581           if ((unsigned long) section->sh_entsize == section->sh_entsize)
5582             printf (" %2.2lx", (unsigned long) section->sh_entsize);
5583           else
5584             {
5585               putchar (' ');
5586               print_vma (section->sh_entsize, LONG_HEX);
5587             }
5588
5589           if (do_section_details)
5590             fputs ("  ", stdout);
5591           else
5592             printf (" %3s ", get_elf_section_flags (section->sh_flags));
5593
5594           printf ("%2u %3u ", section->sh_link, section->sh_info);
5595
5596           if ((unsigned long) section->sh_addralign == section->sh_addralign)
5597             printf ("%2lu\n", (unsigned long) section->sh_addralign);
5598           else
5599             {
5600               print_vma (section->sh_addralign, DEC);
5601               putchar ('\n');
5602             }
5603         }
5604       else if (do_section_details)
5605         {
5606           printf ("       %-15.15s  ",
5607                   get_section_type_name (section->sh_type));
5608           print_vma (section->sh_addr, LONG_HEX);
5609           if ((long) section->sh_offset == section->sh_offset)
5610             printf ("  %16.16lx", (unsigned long) section->sh_offset);
5611           else
5612             {
5613               printf ("  ");
5614               print_vma (section->sh_offset, LONG_HEX);
5615             }
5616           printf ("  %u\n       ", section->sh_link);
5617           print_vma (section->sh_size, LONG_HEX);
5618           putchar (' ');
5619           print_vma (section->sh_entsize, LONG_HEX);
5620
5621           printf ("  %-16u  %lu\n",
5622                   section->sh_info,
5623                   (unsigned long) section->sh_addralign);
5624         }
5625       else
5626         {
5627           putchar (' ');
5628           print_vma (section->sh_addr, LONG_HEX);
5629           if ((long) section->sh_offset == section->sh_offset)
5630             printf ("  %8.8lx", (unsigned long) section->sh_offset);
5631           else
5632             {
5633               printf ("  ");
5634               print_vma (section->sh_offset, LONG_HEX);
5635             }
5636           printf ("\n       ");
5637           print_vma (section->sh_size, LONG_HEX);
5638           printf ("  ");
5639           print_vma (section->sh_entsize, LONG_HEX);
5640
5641           printf (" %3s ", get_elf_section_flags (section->sh_flags));
5642
5643           printf ("     %2u   %3u     %lu\n",
5644                   section->sh_link,
5645                   section->sh_info,
5646                   (unsigned long) section->sh_addralign);
5647         }
5648
5649       if (do_section_details)
5650         printf ("       %s\n", get_elf_section_flags (section->sh_flags));
5651     }
5652
5653   if (!do_section_details)
5654     {
5655       if (elf_header.e_machine == EM_X86_64
5656           || elf_header.e_machine == EM_L1OM
5657           || elf_header.e_machine == EM_K1OM)
5658         printf (_("Key to Flags:\n\
5659   W (write), A (alloc), X (execute), M (merge), S (strings), l (large)\n\
5660   I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\n\
5661   O (extra OS processing required) o (OS specific), p (processor specific)\n"));
5662       else
5663         printf (_("Key to Flags:\n\
5664   W (write), A (alloc), X (execute), M (merge), S (strings)\n\
5665   I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\n\
5666   O (extra OS processing required) o (OS specific), p (processor specific)\n"));
5667     }
5668
5669   return 1;
5670 }
5671
5672 static const char *
5673 get_group_flags (unsigned int flags)
5674 {
5675   static char buff[32];
5676   switch (flags)
5677     {
5678     case 0:
5679       return "";
5680
5681     case GRP_COMDAT:
5682       return "COMDAT ";
5683
5684    default:
5685       snprintf (buff, sizeof (buff), _("[<unknown>: 0x%x] "), flags);
5686       break;
5687     }
5688   return buff;
5689 }
5690
5691 static int
5692 process_section_groups (FILE * file)
5693 {
5694   Elf_Internal_Shdr * section;
5695   unsigned int i;
5696   struct group * group;
5697   Elf_Internal_Shdr * symtab_sec;
5698   Elf_Internal_Shdr * strtab_sec;
5699   Elf_Internal_Sym * symtab;
5700   unsigned long num_syms;
5701   char * strtab;
5702   size_t strtab_size;
5703
5704   /* Don't process section groups unless needed.  */
5705   if (!do_unwind && !do_section_groups)
5706     return 1;
5707
5708   if (elf_header.e_shnum == 0)
5709     {
5710       if (do_section_groups)
5711         printf (_("\nThere are no sections to group in this file.\n"));
5712
5713       return 1;
5714     }
5715
5716   if (section_headers == NULL)
5717     {
5718       error (_("Section headers are not available!\n"));
5719       /* PR 13622: This can happen with a corrupt ELF header.  */
5720       return 0;
5721     }
5722
5723   section_headers_groups = (struct group **) calloc (elf_header.e_shnum,
5724                                                      sizeof (struct group *));
5725
5726   if (section_headers_groups == NULL)
5727     {
5728       error (_("Out of memory reading %u section group headers\n"),
5729              elf_header.e_shnum);
5730       return 0;
5731     }
5732
5733   /* Scan the sections for the group section.  */
5734   group_count = 0;
5735   for (i = 0, section = section_headers;
5736        i < elf_header.e_shnum;
5737        i++, section++)
5738     if (section->sh_type == SHT_GROUP)
5739       group_count++;
5740
5741   if (group_count == 0)
5742     {
5743       if (do_section_groups)
5744         printf (_("\nThere are no section groups in this file.\n"));
5745
5746       return 1;
5747     }
5748
5749   section_groups = (struct group *) calloc (group_count, sizeof (struct group));
5750
5751   if (section_groups == NULL)
5752     {
5753       error (_("Out of memory reading %lu groups\n"),
5754              (unsigned long) group_count);
5755       return 0;
5756     }
5757
5758   symtab_sec = NULL;
5759   strtab_sec = NULL;
5760   symtab = NULL;
5761   num_syms = 0;
5762   strtab = NULL;
5763   strtab_size = 0;
5764   for (i = 0, section = section_headers, group = section_groups;
5765        i < elf_header.e_shnum;
5766        i++, section++)
5767     {
5768       if (section->sh_type == SHT_GROUP)
5769         {
5770           const char * name = printable_section_name (section);
5771           const char * group_name;
5772           unsigned char * start;
5773           unsigned char * indices;
5774           unsigned int entry, j, size;
5775           Elf_Internal_Shdr * sec;
5776           Elf_Internal_Sym * sym;
5777
5778           /* Get the symbol table.  */
5779           if (section->sh_link >= elf_header.e_shnum
5780               || ((sec = section_headers + section->sh_link)->sh_type
5781                   != SHT_SYMTAB))
5782             {
5783               error (_("Bad sh_link in group section `%s'\n"), name);
5784               continue;
5785             }
5786
5787           if (symtab_sec != sec)
5788             {
5789               symtab_sec = sec;
5790               if (symtab)
5791                 free (symtab);
5792               symtab = GET_ELF_SYMBOLS (file, symtab_sec, & num_syms);
5793             }
5794
5795           if (symtab == NULL)
5796             {
5797               error (_("Corrupt header in group section `%s'\n"), name);
5798               continue;
5799             }
5800
5801           if (section->sh_info >= num_syms)
5802             {
5803               error (_("Bad sh_info in group section `%s'\n"), name);
5804               continue;
5805             }
5806
5807           sym = symtab + section->sh_info;
5808
5809           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5810             {
5811               if (sym->st_shndx == 0
5812                   || sym->st_shndx >= elf_header.e_shnum)
5813                 {
5814                   error (_("Bad sh_info in group section `%s'\n"), name);
5815                   continue;
5816                 }
5817
5818               group_name = SECTION_NAME (section_headers + sym->st_shndx);
5819               strtab_sec = NULL;
5820               if (strtab)
5821                 free (strtab);
5822               strtab = NULL;
5823               strtab_size = 0;
5824             }
5825           else
5826             {
5827               /* Get the string table.  */
5828               if (symtab_sec->sh_link >= elf_header.e_shnum)
5829                 {
5830                   strtab_sec = NULL;
5831                   if (strtab)
5832                     free (strtab);
5833                   strtab = NULL;
5834                   strtab_size = 0;
5835                 }
5836               else if (strtab_sec
5837                        != (sec = section_headers + symtab_sec->sh_link))
5838                 {
5839                   strtab_sec = sec;
5840                   if (strtab)
5841                     free (strtab);
5842
5843                   strtab = (char *) get_data (NULL, file, strtab_sec->sh_offset,
5844                                               1, strtab_sec->sh_size,
5845                                               _("string table"));
5846                   strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
5847                 }
5848               group_name = sym->st_name < strtab_size
5849                 ? strtab + sym->st_name : _("<corrupt>");
5850             }
5851
5852           /* PR 17531: file: loop.  */
5853           if (section->sh_entsize > section->sh_size)
5854             {
5855               error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
5856                      printable_section_name (section),
5857                      (unsigned long) section->sh_entsize,
5858                      (unsigned long) section->sh_size);
5859               break;
5860             }
5861
5862           start = (unsigned char *) get_data (NULL, file, section->sh_offset,
5863                                               1, section->sh_size,
5864                                               _("section data"));
5865           if (start == NULL)
5866             continue;
5867
5868           indices = start;
5869           size = (section->sh_size / section->sh_entsize) - 1;
5870           entry = byte_get (indices, 4);
5871           indices += 4;
5872
5873           if (do_section_groups)
5874             {
5875               printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
5876                       get_group_flags (entry), i, name, group_name, size);
5877
5878               printf (_("   [Index]    Name\n"));
5879             }
5880
5881           group->group_index = i;
5882
5883           for (j = 0; j < size; j++)
5884             {
5885               struct group_list * g;
5886
5887               entry = byte_get (indices, 4);
5888               indices += 4;
5889
5890               if (entry >= elf_header.e_shnum)
5891                 {
5892                   error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
5893                          entry, i, elf_header.e_shnum - 1);
5894                   continue;
5895                 }
5896
5897               if (section_headers_groups [entry] != NULL)
5898                 {
5899                   if (entry)
5900                     {
5901                       error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
5902                              entry, i,
5903                              section_headers_groups [entry]->group_index);
5904                       continue;
5905                     }
5906                   else
5907                     {
5908                       /* Intel C/C++ compiler may put section 0 in a
5909                          section group. We just warn it the first time
5910                          and ignore it afterwards.  */
5911                       static int warned = 0;
5912                       if (!warned)
5913                         {
5914                           error (_("section 0 in group section [%5u]\n"),
5915                                  section_headers_groups [entry]->group_index);
5916                           warned++;
5917                         }
5918                     }
5919                 }
5920
5921               section_headers_groups [entry] = group;
5922
5923               if (do_section_groups)
5924                 {
5925                   sec = section_headers + entry;
5926                   printf ("   [%5u]   %s\n", entry, printable_section_name (sec));
5927                 }
5928
5929               g = (struct group_list *) xmalloc (sizeof (struct group_list));
5930               g->section_index = entry;
5931               g->next = group->root;
5932               group->root = g;
5933             }
5934
5935           if (start)
5936             free (start);
5937
5938           group++;
5939         }
5940     }
5941
5942   if (symtab)
5943     free (symtab);
5944   if (strtab)
5945     free (strtab);
5946   return 1;
5947 }
5948
5949 /* Data used to display dynamic fixups.  */
5950
5951 struct ia64_vms_dynfixup
5952 {
5953   bfd_vma needed_ident;         /* Library ident number.  */
5954   bfd_vma needed;               /* Index in the dstrtab of the library name.  */
5955   bfd_vma fixup_needed;         /* Index of the library.  */
5956   bfd_vma fixup_rela_cnt;       /* Number of fixups.  */
5957   bfd_vma fixup_rela_off;       /* Fixups offset in the dynamic segment.  */
5958 };
5959
5960 /* Data used to display dynamic relocations.  */
5961
5962 struct ia64_vms_dynimgrela
5963 {
5964   bfd_vma img_rela_cnt;         /* Number of relocations.  */
5965   bfd_vma img_rela_off;         /* Reloc offset in the dynamic segment.  */
5966 };
5967
5968 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
5969    library).  */
5970
5971 static void
5972 dump_ia64_vms_dynamic_fixups (FILE *file, struct ia64_vms_dynfixup *fixup,
5973                               const char *strtab, unsigned int strtab_sz)
5974 {
5975   Elf64_External_VMS_IMAGE_FIXUP *imfs;
5976   long i;
5977   const char *lib_name;
5978
5979   imfs = get_data (NULL, file, dynamic_addr + fixup->fixup_rela_off,
5980                    1, fixup->fixup_rela_cnt * sizeof (*imfs),
5981                    _("dynamic section image fixups"));
5982   if (!imfs)
5983     return;
5984
5985   if (fixup->needed < strtab_sz)
5986     lib_name = strtab + fixup->needed;
5987   else
5988     {
5989       warn ("corrupt library name index of 0x%lx found in dynamic entry",
5990             (unsigned long) fixup->needed);
5991       lib_name = "???";
5992     }
5993   printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
5994           (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
5995   printf
5996     (_("Seg Offset           Type                             SymVec DataType\n"));
5997
5998   for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
5999     {
6000       unsigned int type;
6001       const char *rtype;
6002
6003       printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
6004       printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
6005       type = BYTE_GET (imfs [i].type);
6006       rtype = elf_ia64_reloc_type (type);
6007       if (rtype == NULL)
6008         printf (" 0x%08x                       ", type);
6009       else
6010         printf (" %-32s ", rtype);
6011       printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
6012       printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
6013     }
6014
6015   free (imfs);
6016 }
6017
6018 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image).  */
6019
6020 static void
6021 dump_ia64_vms_dynamic_relocs (FILE *file, struct ia64_vms_dynimgrela *imgrela)
6022 {
6023   Elf64_External_VMS_IMAGE_RELA *imrs;
6024   long i;
6025
6026   imrs = get_data (NULL, file, dynamic_addr + imgrela->img_rela_off,
6027                    1, imgrela->img_rela_cnt * sizeof (*imrs),
6028                    _("dynamic section image relocations"));
6029   if (!imrs)
6030     return;
6031
6032   printf (_("\nImage relocs\n"));
6033   printf
6034     (_("Seg Offset   Type                            Addend            Seg Sym Off\n"));
6035
6036   for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
6037     {
6038       unsigned int type;
6039       const char *rtype;
6040
6041       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
6042       printf ("%08" BFD_VMA_FMT "x ",
6043               (bfd_vma) BYTE_GET (imrs [i].rela_offset));
6044       type = BYTE_GET (imrs [i].type);
6045       rtype = elf_ia64_reloc_type (type);
6046       if (rtype == NULL)
6047         printf ("0x%08x                      ", type);
6048       else
6049         printf ("%-31s ", rtype);
6050       print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
6051       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
6052       printf ("%08" BFD_VMA_FMT "x\n",
6053               (bfd_vma) BYTE_GET (imrs [i].sym_offset));
6054     }
6055
6056   free (imrs);
6057 }
6058
6059 /* Display IA-64 OpenVMS dynamic relocations and fixups.  */
6060
6061 static int
6062 process_ia64_vms_dynamic_relocs (FILE *file)
6063 {
6064   struct ia64_vms_dynfixup fixup;
6065   struct ia64_vms_dynimgrela imgrela;
6066   Elf_Internal_Dyn *entry;
6067   int res = 0;
6068   bfd_vma strtab_off = 0;
6069   bfd_vma strtab_sz = 0;
6070   char *strtab = NULL;
6071
6072   memset (&fixup, 0, sizeof (fixup));
6073   memset (&imgrela, 0, sizeof (imgrela));
6074
6075   /* Note: the order of the entries is specified by the OpenVMS specs.  */
6076   for (entry = dynamic_section;
6077        entry < dynamic_section + dynamic_nent;
6078        entry++)
6079     {
6080       switch (entry->d_tag)
6081         {
6082         case DT_IA_64_VMS_STRTAB_OFFSET:
6083           strtab_off = entry->d_un.d_val;
6084           break;
6085         case DT_STRSZ:
6086           strtab_sz = entry->d_un.d_val;
6087           if (strtab == NULL)
6088             strtab = get_data (NULL, file, dynamic_addr + strtab_off,
6089                                1, strtab_sz, _("dynamic string section"));
6090           break;
6091
6092         case DT_IA_64_VMS_NEEDED_IDENT:
6093           fixup.needed_ident = entry->d_un.d_val;
6094           break;
6095         case DT_NEEDED:
6096           fixup.needed = entry->d_un.d_val;
6097           break;
6098         case DT_IA_64_VMS_FIXUP_NEEDED:
6099           fixup.fixup_needed = entry->d_un.d_val;
6100           break;
6101         case DT_IA_64_VMS_FIXUP_RELA_CNT:
6102           fixup.fixup_rela_cnt = entry->d_un.d_val;
6103           break;
6104         case DT_IA_64_VMS_FIXUP_RELA_OFF:
6105           fixup.fixup_rela_off = entry->d_un.d_val;
6106           res++;
6107           dump_ia64_vms_dynamic_fixups (file, &fixup, strtab, strtab_sz);
6108           break;
6109
6110         case DT_IA_64_VMS_IMG_RELA_CNT:
6111           imgrela.img_rela_cnt = entry->d_un.d_val;
6112           break;
6113         case DT_IA_64_VMS_IMG_RELA_OFF:
6114           imgrela.img_rela_off = entry->d_un.d_val;
6115           res++;
6116           dump_ia64_vms_dynamic_relocs (file, &imgrela);
6117           break;
6118
6119         default:
6120           break;
6121         }
6122     }
6123
6124   if (strtab != NULL)
6125     free (strtab);
6126
6127   return res;
6128 }
6129
6130 static struct
6131 {
6132   const char * name;
6133   int reloc;
6134   int size;
6135   int rela;
6136 } dynamic_relocations [] =
6137 {
6138     { "REL", DT_REL, DT_RELSZ, FALSE },
6139     { "RELA", DT_RELA, DT_RELASZ, TRUE },
6140     { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
6141 };
6142
6143 /* Process the reloc section.  */
6144
6145 static int
6146 process_relocs (FILE * file)
6147 {
6148   unsigned long rel_size;
6149   unsigned long rel_offset;
6150
6151
6152   if (!do_reloc)
6153     return 1;
6154
6155   if (do_using_dynamic)
6156     {
6157       int is_rela;
6158       const char * name;
6159       int has_dynamic_reloc;
6160       unsigned int i;
6161
6162       has_dynamic_reloc = 0;
6163
6164       for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
6165         {
6166           is_rela = dynamic_relocations [i].rela;
6167           name = dynamic_relocations [i].name;
6168           rel_size = dynamic_info [dynamic_relocations [i].size];
6169           rel_offset = dynamic_info [dynamic_relocations [i].reloc];
6170
6171           has_dynamic_reloc |= rel_size;
6172
6173           if (is_rela == UNKNOWN)
6174             {
6175               if (dynamic_relocations [i].reloc == DT_JMPREL)
6176                 switch (dynamic_info[DT_PLTREL])
6177                   {
6178                   case DT_REL:
6179                     is_rela = FALSE;
6180                     break;
6181                   case DT_RELA:
6182                     is_rela = TRUE;
6183                     break;
6184                   }
6185             }
6186
6187           if (rel_size)
6188             {
6189               printf
6190                 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
6191                  name, rel_offset, rel_size);
6192
6193               dump_relocations (file,
6194                                 offset_from_vma (file, rel_offset, rel_size),
6195                                 rel_size,
6196                                 dynamic_symbols, num_dynamic_syms,
6197                                 dynamic_strings, dynamic_strings_length,
6198                                 is_rela);
6199             }
6200         }
6201
6202       if (is_ia64_vms ())
6203         has_dynamic_reloc |= process_ia64_vms_dynamic_relocs (file);
6204
6205       if (! has_dynamic_reloc)
6206         printf (_("\nThere are no dynamic relocations in this file.\n"));
6207     }
6208   else
6209     {
6210       Elf_Internal_Shdr * section;
6211       unsigned long i;
6212       int found = 0;
6213
6214       for (i = 0, section = section_headers;
6215            i < elf_header.e_shnum;
6216            i++, section++)
6217         {
6218           if (   section->sh_type != SHT_RELA
6219               && section->sh_type != SHT_REL)
6220             continue;
6221
6222           rel_offset = section->sh_offset;
6223           rel_size   = section->sh_size;
6224
6225           if (rel_size)
6226             {
6227               Elf_Internal_Shdr * strsec;
6228               int is_rela;
6229
6230               printf (_("\nRelocation section "));
6231
6232               if (string_table == NULL)
6233                 printf ("%d", section->sh_name);
6234               else
6235                 printf ("'%s'", printable_section_name (section));
6236
6237               printf (_(" at offset 0x%lx contains %lu entries:\n"),
6238                  rel_offset, (unsigned long) (rel_size / section->sh_entsize));
6239
6240               is_rela = section->sh_type == SHT_RELA;
6241
6242               if (section->sh_link != 0
6243                   && section->sh_link < elf_header.e_shnum)
6244                 {
6245                   Elf_Internal_Shdr * symsec;
6246                   Elf_Internal_Sym *  symtab;
6247                   unsigned long nsyms;
6248                   unsigned long strtablen = 0;
6249                   char * strtab = NULL;
6250
6251                   symsec = section_headers + section->sh_link;
6252                   if (symsec->sh_type != SHT_SYMTAB
6253                       && symsec->sh_type != SHT_DYNSYM)
6254                     continue;
6255
6256                   symtab = GET_ELF_SYMBOLS (file, symsec, & nsyms);
6257
6258                   if (symtab == NULL)
6259                     continue;
6260
6261                   if (symsec->sh_link != 0
6262                       && symsec->sh_link < elf_header.e_shnum)
6263                     {
6264                       strsec = section_headers + symsec->sh_link;
6265
6266                       strtab = (char *) get_data (NULL, file, strsec->sh_offset,
6267                                                   1, strsec->sh_size,
6268                                                   _("string table"));
6269                       strtablen = strtab == NULL ? 0 : strsec->sh_size;
6270                     }
6271
6272                   dump_relocations (file, rel_offset, rel_size,
6273                                     symtab, nsyms, strtab, strtablen,
6274                                     is_rela);
6275                   if (strtab)
6276                     free (strtab);
6277                   free (symtab);
6278                 }
6279               else
6280                 dump_relocations (file, rel_offset, rel_size,
6281                                   NULL, 0, NULL, 0, is_rela);
6282
6283               found = 1;
6284             }
6285         }
6286
6287       if (! found)
6288         printf (_("\nThere are no relocations in this file.\n"));
6289     }
6290
6291   return 1;
6292 }
6293
6294 /* Process the unwind section.  */
6295
6296 #include "unwind-ia64.h"
6297
6298 /* An absolute address consists of a section and an offset.  If the
6299    section is NULL, the offset itself is the address, otherwise, the
6300    address equals to LOAD_ADDRESS(section) + offset.  */
6301
6302 struct absaddr
6303   {
6304     unsigned short section;
6305     bfd_vma offset;
6306   };
6307
6308 #define ABSADDR(a) \
6309   ((a).section \
6310    ? section_headers [(a).section].sh_addr + (a).offset \
6311    : (a).offset)
6312
6313 struct ia64_unw_table_entry
6314   {
6315     struct absaddr start;
6316     struct absaddr end;
6317     struct absaddr info;
6318   };
6319
6320 struct ia64_unw_aux_info
6321   {
6322
6323     struct ia64_unw_table_entry *table; /* Unwind table.  */
6324     unsigned long table_len;    /* Length of unwind table.  */
6325     unsigned char * info;       /* Unwind info.  */
6326     unsigned long info_size;    /* Size of unwind info.  */
6327     bfd_vma info_addr;          /* starting address of unwind info.  */
6328     bfd_vma seg_base;           /* Starting address of segment.  */
6329     Elf_Internal_Sym * symtab;  /* The symbol table.  */
6330     unsigned long nsyms;        /* Number of symbols.  */
6331     char * strtab;              /* The string table.  */
6332     unsigned long strtab_size;  /* Size of string table.  */
6333   };
6334
6335 static void
6336 find_symbol_for_address (Elf_Internal_Sym * symtab,
6337                          unsigned long nsyms,
6338                          const char * strtab,
6339                          unsigned long strtab_size,
6340                          struct absaddr addr,
6341                          const char ** symname,
6342                          bfd_vma * offset)
6343 {
6344   bfd_vma dist = 0x100000;
6345   Elf_Internal_Sym * sym;
6346   Elf_Internal_Sym * best = NULL;
6347   unsigned long i;
6348
6349   REMOVE_ARCH_BITS (addr.offset);
6350
6351   for (i = 0, sym = symtab; i < nsyms; ++i, ++sym)
6352     {
6353       bfd_vma value = sym->st_value;
6354
6355       REMOVE_ARCH_BITS (value);
6356
6357       if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
6358           && sym->st_name != 0
6359           && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
6360           && addr.offset >= value
6361           && addr.offset - value < dist)
6362         {
6363           best = sym;
6364           dist = addr.offset - value;
6365           if (!dist)
6366             break;
6367         }
6368     }
6369
6370   if (best)
6371     {
6372       *symname = (best->st_name >= strtab_size
6373                   ? _("<corrupt>") : strtab + best->st_name);
6374       *offset = dist;
6375       return;
6376     }
6377
6378   *symname = NULL;
6379   *offset = addr.offset;
6380 }
6381
6382 static void
6383 dump_ia64_unwind (struct ia64_unw_aux_info * aux)
6384 {
6385   struct ia64_unw_table_entry * tp;
6386   int in_body;
6387
6388   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
6389     {
6390       bfd_vma stamp;
6391       bfd_vma offset;
6392       const unsigned char * dp;
6393       const unsigned char * head;
6394       const unsigned char * end;
6395       const char * procname;
6396
6397       find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
6398                                aux->strtab_size, tp->start, &procname, &offset);
6399
6400       fputs ("\n<", stdout);
6401
6402       if (procname)
6403         {
6404           fputs (procname, stdout);
6405
6406           if (offset)
6407             printf ("+%lx", (unsigned long) offset);
6408         }
6409
6410       fputs (">: [", stdout);
6411       print_vma (tp->start.offset, PREFIX_HEX);
6412       fputc ('-', stdout);
6413       print_vma (tp->end.offset, PREFIX_HEX);
6414       printf ("], info at +0x%lx\n",
6415               (unsigned long) (tp->info.offset - aux->seg_base));
6416
6417       /* PR 17531: file: 86232b32.  */
6418       if (aux->info == NULL)
6419         continue;
6420
6421       /* PR 17531: file: 0997b4d1.  */
6422       if ((ABSADDR (tp->info) - aux->info_addr) >= aux->info_size)
6423         {
6424           warn (_("Invalid offset %lx in table entry %ld\n"),
6425                 (long) tp->info.offset, (long) (tp - aux->table));
6426           continue;
6427         }
6428
6429       head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
6430       stamp = byte_get ((unsigned char *) head, sizeof (stamp));
6431
6432       printf ("  v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
6433               (unsigned) UNW_VER (stamp),
6434               (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
6435               UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
6436               UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
6437               (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
6438
6439       if (UNW_VER (stamp) != 1)
6440         {
6441           printf (_("\tUnknown version.\n"));
6442           continue;
6443         }
6444
6445       in_body = 0;
6446       end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
6447       /* PR 17531: file: 16ceda89.  */
6448       if (end > aux->info + aux->info_size)
6449         end = aux->info + aux->info_size;
6450       for (dp = head + 8; dp < end;)
6451         dp = unw_decode (dp, in_body, & in_body);
6452     }
6453 }
6454
6455 static int
6456 slurp_ia64_unwind_table (FILE * file,
6457                          struct ia64_unw_aux_info * aux,
6458                          Elf_Internal_Shdr * sec)
6459 {
6460   unsigned long size, nrelas, i;
6461   Elf_Internal_Phdr * seg;
6462   struct ia64_unw_table_entry * tep;
6463   Elf_Internal_Shdr * relsec;
6464   Elf_Internal_Rela * rela;
6465   Elf_Internal_Rela * rp;
6466   unsigned char * table;
6467   unsigned char * tp;
6468   Elf_Internal_Sym * sym;
6469   const char * relname;
6470
6471   aux->table_len = 0;
6472
6473   /* First, find the starting address of the segment that includes
6474      this section: */
6475
6476   if (elf_header.e_phnum)
6477     {
6478       if (! get_program_headers (file))
6479           return 0;
6480
6481       for (seg = program_headers;
6482            seg < program_headers + elf_header.e_phnum;
6483            ++seg)
6484         {
6485           if (seg->p_type != PT_LOAD)
6486             continue;
6487
6488           if (sec->sh_addr >= seg->p_vaddr
6489               && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
6490             {
6491               aux->seg_base = seg->p_vaddr;
6492               break;
6493             }
6494         }
6495     }
6496
6497   /* Second, build the unwind table from the contents of the unwind section:  */
6498   size = sec->sh_size;
6499   table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
6500                                       _("unwind table"));
6501   if (!table)
6502     return 0;
6503
6504   aux->table_len = size / (3 * eh_addr_size);
6505   aux->table = (struct ia64_unw_table_entry *)
6506     xcmalloc (aux->table_len, sizeof (aux->table[0]));
6507   tep = aux->table;
6508
6509   for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
6510     {
6511       tep->start.section = SHN_UNDEF;
6512       tep->end.section   = SHN_UNDEF;
6513       tep->info.section  = SHN_UNDEF;
6514       tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
6515       tep->end.offset   = byte_get (tp, eh_addr_size); tp += eh_addr_size;
6516       tep->info.offset  = byte_get (tp, eh_addr_size); tp += eh_addr_size;
6517       tep->start.offset += aux->seg_base;
6518       tep->end.offset   += aux->seg_base;
6519       tep->info.offset  += aux->seg_base;
6520     }
6521   free (table);
6522
6523   /* Third, apply any relocations to the unwind table:  */
6524   for (relsec = section_headers;
6525        relsec < section_headers + elf_header.e_shnum;
6526        ++relsec)
6527     {
6528       if (relsec->sh_type != SHT_RELA
6529           || relsec->sh_info >= elf_header.e_shnum
6530           || section_headers + relsec->sh_info != sec)
6531         continue;
6532
6533       if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
6534                               & rela, & nrelas))
6535         {
6536           free (aux->table);
6537           aux->table = NULL;
6538           aux->table_len = 0;
6539           return 0;
6540         }
6541
6542       for (rp = rela; rp < rela + nrelas; ++rp)
6543         {
6544           relname = elf_ia64_reloc_type (get_reloc_type (rp->r_info));
6545           sym = aux->symtab + get_reloc_symindex (rp->r_info);
6546
6547           /* PR 17531: file: 9fa67536.  */
6548           if (relname == NULL)
6549             {
6550               warn (_("Skipping unknown relocation type: %u\n"), get_reloc_type (rp->r_info));
6551               continue;
6552             }
6553
6554           if (! const_strneq (relname, "R_IA64_SEGREL"))
6555             {
6556               warn (_("Skipping unexpected relocation type: %s\n"), relname);
6557               continue;
6558             }
6559
6560           i = rp->r_offset / (3 * eh_addr_size);
6561
6562           /* PR 17531: file: 5bc8d9bf.  */
6563           if (i >= aux->table_len)
6564             {
6565               warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
6566               continue;
6567             }
6568
6569           switch (rp->r_offset / eh_addr_size % 3)
6570             {
6571             case 0:
6572               aux->table[i].start.section = sym->st_shndx;
6573               aux->table[i].start.offset  = rp->r_addend + sym->st_value;
6574               break;
6575             case 1:
6576               aux->table[i].end.section   = sym->st_shndx;
6577               aux->table[i].end.offset    = rp->r_addend + sym->st_value;
6578               break;
6579             case 2:
6580               aux->table[i].info.section  = sym->st_shndx;
6581               aux->table[i].info.offset   = rp->r_addend + sym->st_value;
6582               break;
6583             default:
6584               break;
6585             }
6586         }
6587
6588       free (rela);
6589     }
6590
6591   return 1;
6592 }
6593
6594 static void
6595 ia64_process_unwind (FILE * file)
6596 {
6597   Elf_Internal_Shdr * sec;
6598   Elf_Internal_Shdr * unwsec = NULL;
6599   Elf_Internal_Shdr * strsec;
6600   unsigned long i, unwcount = 0, unwstart = 0;
6601   struct ia64_unw_aux_info aux;
6602
6603   memset (& aux, 0, sizeof (aux));
6604
6605   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
6606     {
6607       if (sec->sh_type == SHT_SYMTAB
6608           && sec->sh_link < elf_header.e_shnum)
6609         {
6610           aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
6611
6612           strsec = section_headers + sec->sh_link;
6613           if (aux.strtab != NULL)
6614             {
6615               error (_("Multiple auxillary string tables encountered\n"));
6616               free (aux.strtab);
6617             }
6618           aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
6619                                           1, strsec->sh_size,
6620                                           _("string table"));
6621           aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
6622         }
6623       else if (sec->sh_type == SHT_IA_64_UNWIND)
6624         unwcount++;
6625     }
6626
6627   if (!unwcount)
6628     printf (_("\nThere are no unwind sections in this file.\n"));
6629
6630   while (unwcount-- > 0)
6631     {
6632       char * suffix;
6633       size_t len, len2;
6634
6635       for (i = unwstart, sec = section_headers + unwstart, unwsec = NULL;
6636            i < elf_header.e_shnum; ++i, ++sec)
6637         if (sec->sh_type == SHT_IA_64_UNWIND)
6638           {
6639             unwsec = sec;
6640             break;
6641           }
6642       /* We have already counted the number of SHT_IA64_UNWIND
6643          sections so the loop above should never fail.  */
6644       assert (unwsec != NULL);
6645
6646       unwstart = i + 1;
6647       len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
6648
6649       if ((unwsec->sh_flags & SHF_GROUP) != 0)
6650         {
6651           /* We need to find which section group it is in.  */
6652           struct group_list * g;
6653
6654           if (section_headers_groups == NULL
6655               || section_headers_groups [i] == NULL)
6656             i = elf_header.e_shnum;
6657           else
6658             {
6659               g = section_headers_groups [i]->root;
6660
6661               for (; g != NULL; g = g->next)
6662                 {
6663                   sec = section_headers + g->section_index;
6664
6665                   if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
6666                     break;
6667                 }
6668
6669               if (g == NULL)
6670                 i = elf_header.e_shnum;
6671             }
6672         }
6673       else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
6674         {
6675           /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO.  */
6676           len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
6677           suffix = SECTION_NAME (unwsec) + len;
6678           for (i = 0, sec = section_headers; i < elf_header.e_shnum;
6679                ++i, ++sec)
6680             if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
6681                 && streq (SECTION_NAME (sec) + len2, suffix))
6682               break;
6683         }
6684       else
6685         {
6686           /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
6687              .IA_64.unwind or BAR -> .IA_64.unwind_info.  */
6688           len = sizeof (ELF_STRING_ia64_unwind) - 1;
6689           len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
6690           suffix = "";
6691           if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
6692             suffix = SECTION_NAME (unwsec) + len;
6693           for (i = 0, sec = section_headers; i < elf_header.e_shnum;
6694                ++i, ++sec)
6695             if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
6696                 && streq (SECTION_NAME (sec) + len2, suffix))
6697               break;
6698         }
6699
6700       if (i == elf_header.e_shnum)
6701         {
6702           printf (_("\nCould not find unwind info section for "));
6703
6704           if (string_table == NULL)
6705             printf ("%d", unwsec->sh_name);
6706           else
6707             printf ("'%s'", printable_section_name (unwsec));
6708         }
6709       else
6710         {
6711           aux.info_addr = sec->sh_addr;
6712           aux.info = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1,
6713                                                  sec->sh_size,
6714                                                  _("unwind info"));
6715           aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
6716
6717           printf (_("\nUnwind section "));
6718
6719           if (string_table == NULL)
6720             printf ("%d", unwsec->sh_name);
6721           else
6722             printf ("'%s'", printable_section_name (unwsec));
6723
6724           printf (_(" at offset 0x%lx contains %lu entries:\n"),
6725                   (unsigned long) unwsec->sh_offset,
6726                   (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
6727
6728           if (slurp_ia64_unwind_table (file, & aux, unwsec)
6729               && aux.table_len > 0)
6730             dump_ia64_unwind (& aux);
6731
6732           if (aux.table)
6733             free ((char *) aux.table);
6734           if (aux.info)
6735             free ((char *) aux.info);
6736           aux.table = NULL;
6737           aux.info = NULL;
6738         }
6739     }
6740
6741   if (aux.symtab)
6742     free (aux.symtab);
6743   if (aux.strtab)
6744     free ((char *) aux.strtab);
6745 }
6746
6747 struct hppa_unw_table_entry
6748   {
6749     struct absaddr start;
6750     struct absaddr end;
6751     unsigned int Cannot_unwind:1;                       /* 0 */
6752     unsigned int Millicode:1;                   /* 1 */
6753     unsigned int Millicode_save_sr0:1;          /* 2 */
6754     unsigned int Region_description:2;          /* 3..4 */
6755     unsigned int reserved1:1;                   /* 5 */
6756     unsigned int Entry_SR:1;                    /* 6 */
6757     unsigned int Entry_FR:4;     /* number saved */     /* 7..10 */
6758     unsigned int Entry_GR:5;     /* number saved */     /* 11..15 */
6759     unsigned int Args_stored:1;                 /* 16 */
6760     unsigned int Variable_Frame:1;                      /* 17 */
6761     unsigned int Separate_Package_Body:1;               /* 18 */
6762     unsigned int Frame_Extension_Millicode:1;   /* 19 */
6763     unsigned int Stack_Overflow_Check:1;                /* 20 */
6764     unsigned int Two_Instruction_SP_Increment:1;        /* 21 */
6765     unsigned int Ada_Region:1;                  /* 22 */
6766     unsigned int cxx_info:1;                    /* 23 */
6767     unsigned int cxx_try_catch:1;                       /* 24 */
6768     unsigned int sched_entry_seq:1;                     /* 25 */
6769     unsigned int reserved2:1;                   /* 26 */
6770     unsigned int Save_SP:1;                             /* 27 */
6771     unsigned int Save_RP:1;                             /* 28 */
6772     unsigned int Save_MRP_in_frame:1;           /* 29 */
6773     unsigned int extn_ptr_defined:1;            /* 30 */
6774     unsigned int Cleanup_defined:1;                     /* 31 */
6775
6776     unsigned int MPE_XL_interrupt_marker:1;             /* 0 */
6777     unsigned int HP_UX_interrupt_marker:1;              /* 1 */
6778     unsigned int Large_frame:1;                 /* 2 */
6779     unsigned int Pseudo_SP_Set:1;                       /* 3 */
6780     unsigned int reserved4:1;                   /* 4 */
6781     unsigned int Total_frame_size:27;           /* 5..31 */
6782   };
6783
6784 struct hppa_unw_aux_info
6785   {
6786     struct hppa_unw_table_entry *table; /* Unwind table.  */
6787     unsigned long table_len;    /* Length of unwind table.  */
6788     bfd_vma seg_base;           /* Starting address of segment.  */
6789     Elf_Internal_Sym * symtab;  /* The symbol table.  */
6790     unsigned long nsyms;        /* Number of symbols.  */
6791     char * strtab;              /* The string table.  */
6792     unsigned long strtab_size;  /* Size of string table.  */
6793   };
6794
6795 static void
6796 dump_hppa_unwind (struct hppa_unw_aux_info * aux)
6797 {
6798   struct hppa_unw_table_entry * tp;
6799
6800   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
6801     {
6802       bfd_vma offset;
6803       const char * procname;
6804
6805       find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
6806                                aux->strtab_size, tp->start, &procname,
6807                                &offset);
6808
6809       fputs ("\n<", stdout);
6810
6811       if (procname)
6812         {
6813           fputs (procname, stdout);
6814
6815           if (offset)
6816             printf ("+%lx", (unsigned long) offset);
6817         }
6818
6819       fputs (">: [", stdout);
6820       print_vma (tp->start.offset, PREFIX_HEX);
6821       fputc ('-', stdout);
6822       print_vma (tp->end.offset, PREFIX_HEX);
6823       printf ("]\n\t");
6824
6825 #define PF(_m) if (tp->_m) printf (#_m " ");
6826 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
6827       PF(Cannot_unwind);
6828       PF(Millicode);
6829       PF(Millicode_save_sr0);
6830       /* PV(Region_description);  */
6831       PF(Entry_SR);
6832       PV(Entry_FR);
6833       PV(Entry_GR);
6834       PF(Args_stored);
6835       PF(Variable_Frame);
6836       PF(Separate_Package_Body);
6837       PF(Frame_Extension_Millicode);
6838       PF(Stack_Overflow_Check);
6839       PF(Two_Instruction_SP_Increment);
6840       PF(Ada_Region);
6841       PF(cxx_info);
6842       PF(cxx_try_catch);
6843       PF(sched_entry_seq);
6844       PF(Save_SP);
6845       PF(Save_RP);
6846       PF(Save_MRP_in_frame);
6847       PF(extn_ptr_defined);
6848       PF(Cleanup_defined);
6849       PF(MPE_XL_interrupt_marker);
6850       PF(HP_UX_interrupt_marker);
6851       PF(Large_frame);
6852       PF(Pseudo_SP_Set);
6853       PV(Total_frame_size);
6854 #undef PF
6855 #undef PV
6856     }
6857
6858   printf ("\n");
6859 }
6860
6861 static int
6862 slurp_hppa_unwind_table (FILE * file,
6863                          struct hppa_unw_aux_info * aux,
6864                          Elf_Internal_Shdr * sec)
6865 {
6866   unsigned long size, unw_ent_size, nentries, nrelas, i;
6867   Elf_Internal_Phdr * seg;
6868   struct hppa_unw_table_entry * tep;
6869   Elf_Internal_Shdr * relsec;
6870   Elf_Internal_Rela * rela;
6871   Elf_Internal_Rela * rp;
6872   unsigned char * table;
6873   unsigned char * tp;
6874   Elf_Internal_Sym * sym;
6875   const char * relname;
6876
6877   /* First, find the starting address of the segment that includes
6878      this section.  */
6879
6880   if (elf_header.e_phnum)
6881     {
6882       if (! get_program_headers (file))
6883         return 0;
6884
6885       for (seg = program_headers;
6886            seg < program_headers + elf_header.e_phnum;
6887            ++seg)
6888         {
6889           if (seg->p_type != PT_LOAD)
6890             continue;
6891
6892           if (sec->sh_addr >= seg->p_vaddr
6893               && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
6894             {
6895               aux->seg_base = seg->p_vaddr;
6896               break;
6897             }
6898         }
6899     }
6900
6901   /* Second, build the unwind table from the contents of the unwind
6902      section.  */
6903   size = sec->sh_size;
6904   table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
6905                                       _("unwind table"));
6906   if (!table)
6907     return 0;
6908
6909   unw_ent_size = 16;
6910   nentries = size / unw_ent_size;
6911   size = unw_ent_size * nentries;
6912
6913   tep = aux->table = (struct hppa_unw_table_entry *)
6914       xcmalloc (nentries, sizeof (aux->table[0]));
6915
6916   for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
6917     {
6918       unsigned int tmp1, tmp2;
6919
6920       tep->start.section = SHN_UNDEF;
6921       tep->end.section   = SHN_UNDEF;
6922
6923       tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
6924       tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
6925       tmp1 = byte_get ((unsigned char *) tp + 8, 4);
6926       tmp2 = byte_get ((unsigned char *) tp + 12, 4);
6927
6928       tep->start.offset += aux->seg_base;
6929       tep->end.offset   += aux->seg_base;
6930
6931       tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
6932       tep->Millicode = (tmp1 >> 30) & 0x1;
6933       tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
6934       tep->Region_description = (tmp1 >> 27) & 0x3;
6935       tep->reserved1 = (tmp1 >> 26) & 0x1;
6936       tep->Entry_SR = (tmp1 >> 25) & 0x1;
6937       tep->Entry_FR = (tmp1 >> 21) & 0xf;
6938       tep->Entry_GR = (tmp1 >> 16) & 0x1f;
6939       tep->Args_stored = (tmp1 >> 15) & 0x1;
6940       tep->Variable_Frame = (tmp1 >> 14) & 0x1;
6941       tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
6942       tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
6943       tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
6944       tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
6945       tep->Ada_Region = (tmp1 >> 9) & 0x1;
6946       tep->cxx_info = (tmp1 >> 8) & 0x1;
6947       tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
6948       tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
6949       tep->reserved2 = (tmp1 >> 5) & 0x1;
6950       tep->Save_SP = (tmp1 >> 4) & 0x1;
6951       tep->Save_RP = (tmp1 >> 3) & 0x1;
6952       tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
6953       tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
6954       tep->Cleanup_defined = tmp1 & 0x1;
6955
6956       tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
6957       tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
6958       tep->Large_frame = (tmp2 >> 29) & 0x1;
6959       tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
6960       tep->reserved4 = (tmp2 >> 27) & 0x1;
6961       tep->Total_frame_size = tmp2 & 0x7ffffff;
6962     }
6963   free (table);
6964
6965   /* Third, apply any relocations to the unwind table.  */
6966   for (relsec = section_headers;
6967        relsec < section_headers + elf_header.e_shnum;
6968        ++relsec)
6969     {
6970       if (relsec->sh_type != SHT_RELA
6971           || relsec->sh_info >= elf_header.e_shnum
6972           || section_headers + relsec->sh_info != sec)
6973         continue;
6974
6975       if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
6976                               & rela, & nrelas))
6977         return 0;
6978
6979       for (rp = rela; rp < rela + nrelas; ++rp)
6980         {
6981           relname = elf_hppa_reloc_type (get_reloc_type (rp->r_info));
6982           sym = aux->symtab + get_reloc_symindex (rp->r_info);
6983
6984           /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64.  */
6985           if (! const_strneq (relname, "R_PARISC_SEGREL"))
6986             {
6987               warn (_("Skipping unexpected relocation type %s\n"), relname);
6988               continue;
6989             }
6990
6991           i = rp->r_offset / unw_ent_size;
6992
6993           switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
6994             {
6995             case 0:
6996               aux->table[i].start.section = sym->st_shndx;
6997               aux->table[i].start.offset  = sym->st_value + rp->r_addend;
6998               break;
6999             case 1:
7000               aux->table[i].end.section   = sym->st_shndx;
7001               aux->table[i].end.offset    = sym->st_value + rp->r_addend;
7002               break;
7003             default:
7004               break;
7005             }
7006         }
7007
7008       free (rela);
7009     }
7010
7011   aux->table_len = nentries;
7012
7013   return 1;
7014 }
7015
7016 static void
7017 hppa_process_unwind (FILE * file)
7018 {
7019   struct hppa_unw_aux_info aux;
7020   Elf_Internal_Shdr * unwsec = NULL;
7021   Elf_Internal_Shdr * strsec;
7022   Elf_Internal_Shdr * sec;
7023   unsigned long i;
7024
7025   if (string_table == NULL)
7026     return;
7027
7028   memset (& aux, 0, sizeof (aux));
7029
7030   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7031     {
7032       if (sec->sh_type == SHT_SYMTAB
7033           && sec->sh_link < elf_header.e_shnum)
7034         {
7035           aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
7036
7037           strsec = section_headers + sec->sh_link;
7038           if (aux.strtab != NULL)
7039             {
7040               error (_("Multiple auxillary string tables encountered\n"));
7041               free (aux.strtab);
7042             }
7043           aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
7044                                           1, strsec->sh_size,
7045                                           _("string table"));
7046           aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
7047         }
7048       else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
7049         unwsec = sec;
7050     }
7051
7052   if (!unwsec)
7053     printf (_("\nThere are no unwind sections in this file.\n"));
7054
7055   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7056     {
7057       if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
7058         {
7059           printf (_("\nUnwind section '%s' at offset 0x%lx contains %lu entries:\n"),
7060                   printable_section_name (sec),
7061                   (unsigned long) sec->sh_offset,
7062                   (unsigned long) (sec->sh_size / (2 * eh_addr_size + 8)));
7063
7064           slurp_hppa_unwind_table (file, &aux, sec);
7065           if (aux.table_len > 0)
7066             dump_hppa_unwind (&aux);
7067
7068           if (aux.table)
7069             free ((char *) aux.table);
7070           aux.table = NULL;
7071         }
7072     }
7073
7074   if (aux.symtab)
7075     free (aux.symtab);
7076   if (aux.strtab)
7077     free ((char *) aux.strtab);
7078 }
7079
7080 struct arm_section
7081 {
7082   unsigned char *      data;            /* The unwind data.  */
7083   Elf_Internal_Shdr *  sec;             /* The cached unwind section header.  */
7084   Elf_Internal_Rela *  rela;            /* The cached relocations for this section.  */
7085   unsigned long        nrelas;          /* The number of relocations.  */
7086   unsigned int         rel_type;        /* REL or RELA ?  */
7087   Elf_Internal_Rela *  next_rela;       /* Cyclic pointer to the next reloc to process.  */
7088 };
7089
7090 struct arm_unw_aux_info
7091 {
7092   FILE *              file;             /* The file containing the unwind sections.  */
7093   Elf_Internal_Sym *  symtab;           /* The file's symbol table.  */
7094   unsigned long       nsyms;            /* Number of symbols.  */
7095   char *              strtab;           /* The file's string table.  */
7096   unsigned long       strtab_size;      /* Size of string table.  */
7097 };
7098
7099 static const char *
7100 arm_print_vma_and_name (struct arm_unw_aux_info *aux,
7101                         bfd_vma fn, struct absaddr addr)
7102 {
7103   const char *procname;
7104   bfd_vma sym_offset;
7105
7106   if (addr.section == SHN_UNDEF)
7107     addr.offset = fn;
7108
7109   find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
7110                            aux->strtab_size, addr, &procname,
7111                            &sym_offset);
7112
7113   print_vma (fn, PREFIX_HEX);
7114
7115   if (procname)
7116     {
7117       fputs (" <", stdout);
7118       fputs (procname, stdout);
7119
7120       if (sym_offset)
7121         printf ("+0x%lx", (unsigned long) sym_offset);
7122       fputc ('>', stdout);
7123     }
7124
7125   return procname;
7126 }
7127
7128 static void
7129 arm_free_section (struct arm_section *arm_sec)
7130 {
7131   if (arm_sec->data != NULL)
7132     free (arm_sec->data);
7133
7134   if (arm_sec->rela != NULL)
7135     free (arm_sec->rela);
7136 }
7137
7138 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
7139       cached section and install SEC instead.
7140    2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
7141       and return its valued in * WORDP, relocating if necessary.
7142    3) Update the NEXT_RELA field in ARM_SEC and store the section index and
7143       relocation's offset in ADDR.
7144    4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
7145       into the string table of the symbol associated with the reloc.  If no
7146       reloc was applied store -1 there.
7147    5) Return TRUE upon success, FALSE otherwise.  */
7148
7149 static bfd_boolean
7150 get_unwind_section_word (struct arm_unw_aux_info *  aux,
7151                          struct arm_section *       arm_sec,
7152                          Elf_Internal_Shdr *        sec,
7153                          bfd_vma                    word_offset,
7154                          unsigned int *             wordp,
7155                          struct absaddr *           addr,
7156                          bfd_vma *                  sym_name)
7157 {
7158   Elf_Internal_Rela *rp;
7159   Elf_Internal_Sym *sym;
7160   const char * relname;
7161   unsigned int word;
7162   bfd_boolean wrapped;
7163
7164   if (sec == NULL || arm_sec == NULL)
7165     return FALSE;
7166
7167   addr->section = SHN_UNDEF;
7168   addr->offset = 0;
7169
7170   if (sym_name != NULL)
7171     *sym_name = (bfd_vma) -1;
7172
7173   /* If necessary, update the section cache.  */
7174   if (sec != arm_sec->sec)
7175     {
7176       Elf_Internal_Shdr *relsec;
7177
7178       arm_free_section (arm_sec);
7179
7180       arm_sec->sec = sec;
7181       arm_sec->data = get_data (NULL, aux->file, sec->sh_offset, 1,
7182                                 sec->sh_size, _("unwind data"));
7183       arm_sec->rela = NULL;
7184       arm_sec->nrelas = 0;
7185
7186       for (relsec = section_headers;
7187            relsec < section_headers + elf_header.e_shnum;
7188            ++relsec)
7189         {
7190           if (relsec->sh_info >= elf_header.e_shnum
7191               || section_headers + relsec->sh_info != sec
7192               /* PR 15745: Check the section type as well.  */
7193               || (relsec->sh_type != SHT_REL
7194                   && relsec->sh_type != SHT_RELA))
7195             continue;
7196
7197           arm_sec->rel_type = relsec->sh_type;
7198           if (relsec->sh_type == SHT_REL)
7199             {
7200               if (!slurp_rel_relocs (aux->file, relsec->sh_offset,
7201                                      relsec->sh_size,
7202                                      & arm_sec->rela, & arm_sec->nrelas))
7203                 return FALSE;
7204             }
7205           else /* relsec->sh_type == SHT_RELA */
7206             {
7207               if (!slurp_rela_relocs (aux->file, relsec->sh_offset,
7208                                       relsec->sh_size,
7209                                       & arm_sec->rela, & arm_sec->nrelas))
7210                 return FALSE;
7211             }
7212           break;
7213         }
7214
7215       arm_sec->next_rela = arm_sec->rela;
7216     }
7217
7218   /* If there is no unwind data we can do nothing.  */
7219   if (arm_sec->data == NULL)
7220     return FALSE;
7221
7222   /* If the offset is invalid then fail.  */
7223   if (word_offset > sec->sh_size - 4)
7224     return FALSE;
7225
7226   /* Get the word at the required offset.  */
7227   word = byte_get (arm_sec->data + word_offset, 4);
7228
7229   /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128.  */
7230   if (arm_sec->rela == NULL)
7231     {
7232       * wordp = word;
7233       return TRUE;
7234     }
7235
7236   /* Look through the relocs to find the one that applies to the provided offset.  */
7237   wrapped = FALSE;
7238   for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
7239     {
7240       bfd_vma prelval, offset;
7241
7242       if (rp->r_offset > word_offset && !wrapped)
7243         {
7244           rp = arm_sec->rela;
7245           wrapped = TRUE;
7246         }
7247       if (rp->r_offset > word_offset)
7248         break;
7249
7250       if (rp->r_offset & 3)
7251         {
7252           warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
7253                 (unsigned long) rp->r_offset);
7254           continue;
7255         }
7256
7257       if (rp->r_offset < word_offset)
7258         continue;
7259
7260       /* PR 17531: file: 027-161405-0.004  */
7261       if (aux->symtab == NULL)
7262         continue;
7263
7264       if (arm_sec->rel_type == SHT_REL)
7265         {
7266           offset = word & 0x7fffffff;
7267           if (offset & 0x40000000)
7268             offset |= ~ (bfd_vma) 0x7fffffff;
7269         }
7270       else if (arm_sec->rel_type == SHT_RELA)
7271         offset = rp->r_addend;
7272       else
7273         {
7274           error (_("Unknown section relocation type %d encountered\n"),
7275                  arm_sec->rel_type);
7276           break;
7277         }
7278
7279       /* PR 17531 file: 027-1241568-0.004.  */
7280       if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
7281         {
7282           error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
7283                  (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
7284           break;
7285         }
7286
7287       sym = aux->symtab + ELF32_R_SYM (rp->r_info);
7288       offset += sym->st_value;
7289       prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
7290
7291       /* Check that we are processing the expected reloc type.  */
7292       if (elf_header.e_machine == EM_ARM)
7293         {
7294           relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
7295           if (relname == NULL)
7296             {
7297               warn (_("Skipping unknown ARM relocation type: %d\n"),
7298                     (int) ELF32_R_TYPE (rp->r_info));
7299               continue;
7300             }
7301
7302           if (streq (relname, "R_ARM_NONE"))
7303               continue;
7304
7305           if (! streq (relname, "R_ARM_PREL31"))
7306             {
7307               warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
7308               continue;
7309             }
7310         }
7311       else if (elf_header.e_machine == EM_TI_C6000)
7312         {
7313           relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
7314           if (relname == NULL)
7315             {
7316               warn (_("Skipping unknown C6000 relocation type: %d\n"),
7317                     (int) ELF32_R_TYPE (rp->r_info));
7318               continue;
7319             }
7320
7321           if (streq (relname, "R_C6000_NONE"))
7322             continue;
7323
7324           if (! streq (relname, "R_C6000_PREL31"))
7325             {
7326               warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
7327               continue;
7328             }
7329
7330           prelval >>= 1;
7331         }
7332       else
7333         {
7334           /* This function currently only supports ARM and TI unwinders.  */
7335           warn (_("Only TI and ARM unwinders are currently supported\n"));
7336           break;
7337         }
7338
7339       word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
7340       addr->section = sym->st_shndx;
7341       addr->offset = offset;
7342
7343       if (sym_name)
7344         * sym_name = sym->st_name;
7345       break;
7346     }
7347
7348   *wordp = word;
7349   arm_sec->next_rela = rp;
7350
7351   return TRUE;
7352 }
7353
7354 static const char *tic6x_unwind_regnames[16] =
7355 {
7356   "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
7357   "A14", "A13", "A12", "A11", "A10",
7358   "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
7359 };
7360
7361 static void
7362 decode_tic6x_unwind_regmask (unsigned int mask)
7363 {
7364   int i;
7365
7366   for (i = 12; mask; mask >>= 1, i--)
7367     {
7368       if (mask & 1)
7369         {
7370           fputs (tic6x_unwind_regnames[i], stdout);
7371           if (mask > 1)
7372             fputs (", ", stdout);
7373         }
7374     }
7375 }
7376
7377 #define ADVANCE                                                 \
7378   if (remaining == 0 && more_words)                             \
7379     {                                                           \
7380       data_offset += 4;                                         \
7381       if (! get_unwind_section_word (aux, data_arm_sec, data_sec,       \
7382                                      data_offset, & word, & addr, NULL))        \
7383         return;                                                 \
7384       remaining = 4;                                            \
7385       more_words--;                                             \
7386     }                                                           \
7387
7388 #define GET_OP(OP)                      \
7389   ADVANCE;                              \
7390   if (remaining)                        \
7391     {                                   \
7392       remaining--;                      \
7393       (OP) = word >> 24;                \
7394       word <<= 8;                       \
7395     }                                   \
7396   else                                  \
7397     {                                   \
7398       printf (_("[Truncated opcode]\n"));       \
7399       return;                           \
7400     }                                   \
7401   printf ("0x%02x ", OP)
7402
7403 static void
7404 decode_arm_unwind_bytecode (struct arm_unw_aux_info *aux,
7405                             unsigned int word, unsigned int remaining,
7406                             unsigned int more_words,
7407                             bfd_vma data_offset, Elf_Internal_Shdr *data_sec,
7408                             struct arm_section *data_arm_sec)
7409 {
7410   struct absaddr addr;
7411
7412   /* Decode the unwinding instructions.  */
7413   while (1)
7414     {
7415       unsigned int op, op2;
7416
7417       ADVANCE;
7418       if (remaining == 0)
7419         break;
7420       remaining--;
7421       op = word >> 24;
7422       word <<= 8;
7423
7424       printf ("  0x%02x ", op);
7425
7426       if ((op & 0xc0) == 0x00)
7427         {
7428           int offset = ((op & 0x3f) << 2) + 4;
7429
7430           printf ("     vsp = vsp + %d", offset);
7431         }
7432       else if ((op & 0xc0) == 0x40)
7433         {
7434           int offset = ((op & 0x3f) << 2) + 4;
7435
7436           printf ("     vsp = vsp - %d", offset);
7437         }
7438       else if ((op & 0xf0) == 0x80)
7439         {
7440           GET_OP (op2);
7441           if (op == 0x80 && op2 == 0)
7442             printf (_("Refuse to unwind"));
7443           else
7444             {
7445               unsigned int mask = ((op & 0x0f) << 8) | op2;
7446               int first = 1;
7447               int i;
7448
7449               printf ("pop {");
7450               for (i = 0; i < 12; i++)
7451                 if (mask & (1 << i))
7452                   {
7453                     if (first)
7454                       first = 0;
7455                     else
7456                       printf (", ");
7457                     printf ("r%d", 4 + i);
7458                   }
7459               printf ("}");
7460             }
7461         }
7462       else if ((op & 0xf0) == 0x90)
7463         {
7464           if (op == 0x9d || op == 0x9f)
7465             printf (_("     [Reserved]"));
7466           else
7467             printf ("     vsp = r%d", op & 0x0f);
7468         }
7469       else if ((op & 0xf0) == 0xa0)
7470         {
7471           int end = 4 + (op & 0x07);
7472           int first = 1;
7473           int i;
7474
7475           printf ("     pop {");
7476           for (i = 4; i <= end; i++)
7477             {
7478               if (first)
7479                 first = 0;
7480               else
7481                 printf (", ");
7482               printf ("r%d", i);
7483             }
7484           if (op & 0x08)
7485             {
7486               if (!first)
7487                 printf (", ");
7488               printf ("r14");
7489             }
7490           printf ("}");
7491         }
7492       else if (op == 0xb0)
7493         printf (_("     finish"));
7494       else if (op == 0xb1)
7495         {
7496           GET_OP (op2);
7497           if (op2 == 0 || (op2 & 0xf0) != 0)
7498             printf (_("[Spare]"));
7499           else
7500             {
7501               unsigned int mask = op2 & 0x0f;
7502               int first = 1;
7503               int i;
7504
7505               printf ("pop {");
7506               for (i = 0; i < 12; i++)
7507                 if (mask & (1 << i))
7508                   {
7509                     if (first)
7510                       first = 0;
7511                     else
7512                       printf (", ");
7513                     printf ("r%d", i);
7514                   }
7515               printf ("}");
7516             }
7517         }
7518       else if (op == 0xb2)
7519         {
7520           unsigned char buf[9];
7521           unsigned int i, len;
7522           unsigned long offset;
7523
7524           for (i = 0; i < sizeof (buf); i++)
7525             {
7526               GET_OP (buf[i]);
7527               if ((buf[i] & 0x80) == 0)
7528                 break;
7529             }
7530           if (i == sizeof (buf))
7531             printf (_("corrupt change to vsp"));
7532           else
7533             {
7534               offset = read_uleb128 (buf, &len, buf + i + 1);
7535               assert (len == i + 1);
7536               offset = offset * 4 + 0x204;
7537               printf ("vsp = vsp + %ld", offset);
7538             }
7539         }
7540       else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
7541         {
7542           unsigned int first, last;
7543
7544           GET_OP (op2);
7545           first = op2 >> 4;
7546           last = op2 & 0x0f;
7547           if (op == 0xc8)
7548             first = first + 16;
7549           printf ("pop {D%d", first);
7550           if (last)
7551             printf ("-D%d", first + last);
7552           printf ("}");
7553         }
7554       else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
7555         {
7556           unsigned int count = op & 0x07;
7557
7558           printf ("pop {D8");
7559           if (count)
7560             printf ("-D%d", 8 + count);
7561           printf ("}");
7562         }
7563       else if (op >= 0xc0 && op <= 0xc5)
7564         {
7565           unsigned int count = op & 0x07;
7566
7567           printf ("     pop {wR10");
7568           if (count)
7569             printf ("-wR%d", 10 + count);
7570           printf ("}");
7571         }
7572       else if (op == 0xc6)
7573         {
7574           unsigned int first, last;
7575
7576           GET_OP (op2);
7577           first = op2 >> 4;
7578           last = op2 & 0x0f;
7579           printf ("pop {wR%d", first);
7580           if (last)
7581             printf ("-wR%d", first + last);
7582           printf ("}");
7583         }
7584       else if (op == 0xc7)
7585         {
7586           GET_OP (op2);
7587           if (op2 == 0 || (op2 & 0xf0) != 0)
7588             printf (_("[Spare]"));
7589           else
7590             {
7591               unsigned int mask = op2 & 0x0f;
7592               int first = 1;
7593               int i;
7594
7595               printf ("pop {");
7596               for (i = 0; i < 4; i++)
7597                 if (mask & (1 << i))
7598                   {
7599                     if (first)
7600                       first = 0;
7601                     else
7602                       printf (", ");
7603                     printf ("wCGR%d", i);
7604                   }
7605               printf ("}");
7606             }
7607         }
7608       else
7609         printf (_("     [unsupported opcode]"));
7610       printf ("\n");
7611     }
7612 }
7613
7614 static void
7615 decode_tic6x_unwind_bytecode (struct arm_unw_aux_info *aux,
7616                             unsigned int word, unsigned int remaining,
7617                             unsigned int more_words,
7618                             bfd_vma data_offset, Elf_Internal_Shdr *data_sec,
7619                             struct arm_section *data_arm_sec)
7620 {
7621   struct absaddr addr;
7622
7623   /* Decode the unwinding instructions.  */
7624   while (1)
7625     {
7626       unsigned int op, op2;
7627
7628       ADVANCE;
7629       if (remaining == 0)
7630         break;
7631       remaining--;
7632       op = word >> 24;
7633       word <<= 8;
7634
7635       printf ("  0x%02x ", op);
7636
7637       if ((op & 0xc0) == 0x00)
7638         {
7639           int offset = ((op & 0x3f) << 3) + 8;
7640           printf ("     sp = sp + %d", offset);
7641         }
7642       else if ((op & 0xc0) == 0x80)
7643         {
7644           GET_OP (op2);
7645           if (op == 0x80 && op2 == 0)
7646             printf (_("Refuse to unwind"));
7647           else
7648             {
7649               unsigned int mask = ((op & 0x1f) << 8) | op2;
7650               if (op & 0x20)
7651                 printf ("pop compact {");
7652               else
7653                 printf ("pop {");
7654
7655               decode_tic6x_unwind_regmask (mask);
7656               printf("}");
7657             }
7658         }
7659       else if ((op & 0xf0) == 0xc0)
7660         {
7661           unsigned int reg;
7662           unsigned int nregs;
7663           unsigned int i;
7664           const char *name;
7665           struct
7666           {
7667               unsigned int offset;
7668               unsigned int reg;
7669           } regpos[16];
7670
7671           /* Scan entire instruction first so that GET_OP output is not
7672              interleaved with disassembly.  */
7673           nregs = 0;
7674           for (i = 0; nregs < (op & 0xf); i++)
7675             {
7676               GET_OP (op2);
7677               reg = op2 >> 4;
7678               if (reg != 0xf)
7679                 {
7680                   regpos[nregs].offset = i * 2;
7681                   regpos[nregs].reg = reg;
7682                   nregs++;
7683                 }
7684
7685               reg = op2 & 0xf;
7686               if (reg != 0xf)
7687                 {
7688                   regpos[nregs].offset = i * 2 + 1;
7689                   regpos[nregs].reg = reg;
7690                   nregs++;
7691                 }
7692             }
7693
7694           printf (_("pop frame {"));
7695           reg = nregs - 1;
7696           for (i = i * 2; i > 0; i--)
7697             {
7698               if (regpos[reg].offset == i - 1)
7699                 {
7700                   name = tic6x_unwind_regnames[regpos[reg].reg];
7701                   if (reg > 0)
7702                     reg--;
7703                 }
7704               else
7705                 name = _("[pad]");
7706
7707               fputs (name, stdout);
7708               if (i > 1)
7709                 printf (", ");
7710             }
7711
7712           printf ("}");
7713         }
7714       else if (op == 0xd0)
7715         printf ("     MOV FP, SP");
7716       else if (op == 0xd1)
7717         printf ("     __c6xabi_pop_rts");
7718       else if (op == 0xd2)
7719         {
7720           unsigned char buf[9];
7721           unsigned int i, len;
7722           unsigned long offset;
7723
7724           for (i = 0; i < sizeof (buf); i++)
7725             {
7726               GET_OP (buf[i]);
7727               if ((buf[i] & 0x80) == 0)
7728                 break;
7729             }
7730           /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2.  */
7731           if (i == sizeof (buf))
7732             {
7733               printf ("<corrupt sp adjust>\n");
7734               warn (_("Corrupt stack pointer adjustment detected\n"));
7735               return;
7736             }
7737
7738           offset = read_uleb128 (buf, &len, buf + i + 1);
7739           assert (len == i + 1);
7740           offset = offset * 8 + 0x408;
7741           printf (_("sp = sp + %ld"), offset);
7742         }
7743       else if ((op & 0xf0) == 0xe0)
7744         {
7745           if ((op & 0x0f) == 7)
7746             printf ("     RETURN");
7747           else
7748             printf ("     MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
7749         }
7750       else
7751         {
7752           printf (_("     [unsupported opcode]"));
7753         }
7754       putchar ('\n');
7755     }
7756 }
7757
7758 static bfd_vma
7759 arm_expand_prel31 (bfd_vma word, bfd_vma where)
7760 {
7761   bfd_vma offset;
7762
7763   offset = word & 0x7fffffff;
7764   if (offset & 0x40000000)
7765     offset |= ~ (bfd_vma) 0x7fffffff;
7766
7767   if (elf_header.e_machine == EM_TI_C6000)
7768     offset <<= 1;
7769
7770   return offset + where;
7771 }
7772
7773 static void
7774 decode_arm_unwind (struct arm_unw_aux_info *  aux,
7775                    unsigned int               word,
7776                    unsigned int               remaining,
7777                    bfd_vma                    data_offset,
7778                    Elf_Internal_Shdr *        data_sec,
7779                    struct arm_section *       data_arm_sec)
7780 {
7781   int per_index;
7782   unsigned int more_words = 0;
7783   struct absaddr addr;
7784   bfd_vma sym_name = (bfd_vma) -1;
7785
7786   if (remaining == 0)
7787     {
7788       /* Fetch the first word.
7789          Note - when decoding an object file the address extracted
7790          here will always be 0.  So we also pass in the sym_name
7791          parameter so that we can find the symbol associated with
7792          the personality routine.  */
7793       if (! get_unwind_section_word (aux, data_arm_sec, data_sec, data_offset,
7794                                      & word, & addr, & sym_name))
7795         return;
7796
7797       remaining = 4;
7798     }
7799
7800   if ((word & 0x80000000) == 0)
7801     {
7802       /* Expand prel31 for personality routine.  */
7803       bfd_vma fn;
7804       const char *procname;
7805
7806       fn = arm_expand_prel31 (word, data_sec->sh_addr + data_offset);
7807       printf (_("  Personality routine: "));
7808       if (fn == 0
7809           && addr.section == SHN_UNDEF && addr.offset == 0
7810           && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
7811         {
7812           procname = aux->strtab + sym_name;
7813           print_vma (fn, PREFIX_HEX);
7814           if (procname)
7815             {
7816               fputs (" <", stdout);
7817               fputs (procname, stdout);
7818               fputc ('>', stdout);
7819             }
7820         }
7821       else
7822         procname = arm_print_vma_and_name (aux, fn, addr);
7823       fputc ('\n', stdout);
7824
7825       /* The GCC personality routines use the standard compact
7826          encoding, starting with one byte giving the number of
7827          words.  */
7828       if (procname != NULL
7829           && (const_strneq (procname, "__gcc_personality_v0")
7830               || const_strneq (procname, "__gxx_personality_v0")
7831               || const_strneq (procname, "__gcj_personality_v0")
7832               || const_strneq (procname, "__gnu_objc_personality_v0")))
7833         {
7834           remaining = 0;
7835           more_words = 1;
7836           ADVANCE;
7837           if (!remaining)
7838             {
7839               printf (_("  [Truncated data]\n"));
7840               return;
7841             }
7842           more_words = word >> 24;
7843           word <<= 8;
7844           remaining--;
7845           per_index = -1;
7846         }
7847       else
7848         return;
7849     }
7850   else
7851     {
7852       /* ARM EHABI Section 6.3:
7853
7854          An exception-handling table entry for the compact model looks like:
7855
7856            31 30-28 27-24 23-0
7857            -- ----- ----- ----
7858             1   0   index Data for personalityRoutine[index]    */
7859
7860       if (elf_header.e_machine == EM_ARM
7861           && (word & 0x70000000))
7862         warn (_("Corrupt ARM compact model table entry: %x \n"), word);
7863
7864       per_index = (word >> 24) & 0x7f;
7865       printf (_("  Compact model index: %d\n"), per_index);
7866       if (per_index == 0)
7867         {
7868           more_words = 0;
7869           word <<= 8;
7870           remaining--;
7871         }
7872       else if (per_index < 3)
7873         {
7874           more_words = (word >> 16) & 0xff;
7875           word <<= 16;
7876           remaining -= 2;
7877         }
7878     }
7879
7880   switch (elf_header.e_machine)
7881     {
7882     case EM_ARM:
7883       if (per_index < 3)
7884         {
7885           decode_arm_unwind_bytecode (aux, word, remaining, more_words,
7886                                       data_offset, data_sec, data_arm_sec);
7887         }
7888       else
7889         {
7890           warn (_("Unknown ARM compact model index encountered\n"));
7891           printf (_("  [reserved]\n"));
7892         }
7893       break;
7894
7895     case EM_TI_C6000:
7896       if (per_index < 3)
7897         {
7898           decode_tic6x_unwind_bytecode (aux, word, remaining, more_words,
7899                                         data_offset, data_sec, data_arm_sec);
7900         }
7901       else if (per_index < 5)
7902         {
7903           if (((word >> 17) & 0x7f) == 0x7f)
7904             printf (_("  Restore stack from frame pointer\n"));
7905           else
7906             printf (_("  Stack increment %d\n"), (word >> 14) & 0x1fc);
7907           printf (_("  Registers restored: "));
7908           if (per_index == 4)
7909             printf (" (compact) ");
7910           decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
7911           putchar ('\n');
7912           printf (_("  Return register: %s\n"),
7913                   tic6x_unwind_regnames[word & 0xf]);
7914         }
7915       else
7916         printf (_("  [reserved (%d)]\n"), per_index);
7917       break;
7918
7919     default:
7920       error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
7921              elf_header.e_machine);
7922     }
7923
7924   /* Decode the descriptors.  Not implemented.  */
7925 }
7926
7927 static void
7928 dump_arm_unwind (struct arm_unw_aux_info *aux, Elf_Internal_Shdr *exidx_sec)
7929 {
7930   struct arm_section exidx_arm_sec, extab_arm_sec;
7931   unsigned int i, exidx_len;
7932
7933   memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
7934   memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
7935   exidx_len = exidx_sec->sh_size / 8;
7936
7937   for (i = 0; i < exidx_len; i++)
7938     {
7939       unsigned int exidx_fn, exidx_entry;
7940       struct absaddr fn_addr, entry_addr;
7941       bfd_vma fn;
7942
7943       fputc ('\n', stdout);
7944
7945       if (! get_unwind_section_word (aux, & exidx_arm_sec, exidx_sec,
7946                                      8 * i, & exidx_fn, & fn_addr, NULL)
7947           || ! get_unwind_section_word (aux, & exidx_arm_sec, exidx_sec,
7948                                         8 * i + 4, & exidx_entry, & entry_addr, NULL))
7949         {
7950           arm_free_section (& exidx_arm_sec);
7951           arm_free_section (& extab_arm_sec);
7952           return;
7953         }
7954
7955       /* ARM EHABI, Section 5:
7956          An index table entry consists of 2 words.
7957          The first word contains a prel31 offset to the start of a function, with bit 31 clear.  */
7958       if (exidx_fn & 0x80000000)
7959         warn (_("corrupt index table entry: %x\n"), exidx_fn);
7960
7961       fn = arm_expand_prel31 (exidx_fn, exidx_sec->sh_addr + 8 * i);
7962
7963       arm_print_vma_and_name (aux, fn, fn_addr);
7964       fputs (": ", stdout);
7965
7966       if (exidx_entry == 1)
7967         {
7968           print_vma (exidx_entry, PREFIX_HEX);
7969           fputs (" [cantunwind]\n", stdout);
7970         }
7971       else if (exidx_entry & 0x80000000)
7972         {
7973           print_vma (exidx_entry, PREFIX_HEX);
7974           fputc ('\n', stdout);
7975           decode_arm_unwind (aux, exidx_entry, 4, 0, NULL, NULL);
7976         }
7977       else
7978         {
7979           bfd_vma table, table_offset = 0;
7980           Elf_Internal_Shdr *table_sec;
7981
7982           fputs ("@", stdout);
7983           table = arm_expand_prel31 (exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
7984           print_vma (table, PREFIX_HEX);
7985           printf ("\n");
7986
7987           /* Locate the matching .ARM.extab.  */
7988           if (entry_addr.section != SHN_UNDEF
7989               && entry_addr.section < elf_header.e_shnum)
7990             {
7991               table_sec = section_headers + entry_addr.section;
7992               table_offset = entry_addr.offset;
7993             }
7994           else
7995             {
7996               table_sec = find_section_by_address (table);
7997               if (table_sec != NULL)
7998                 table_offset = table - table_sec->sh_addr;
7999             }
8000           if (table_sec == NULL)
8001             {
8002               warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
8003                     (unsigned long) table);
8004               continue;
8005             }
8006           decode_arm_unwind (aux, 0, 0, table_offset, table_sec,
8007                              &extab_arm_sec);
8008         }
8009     }
8010
8011   printf ("\n");
8012
8013   arm_free_section (&exidx_arm_sec);
8014   arm_free_section (&extab_arm_sec);
8015 }
8016
8017 /* Used for both ARM and C6X unwinding tables.  */
8018
8019 static void
8020 arm_process_unwind (FILE *file)
8021 {
8022   struct arm_unw_aux_info aux;
8023   Elf_Internal_Shdr *unwsec = NULL;
8024   Elf_Internal_Shdr *strsec;
8025   Elf_Internal_Shdr *sec;
8026   unsigned long i;
8027   unsigned int sec_type;
8028
8029   switch (elf_header.e_machine)
8030     {
8031     case EM_ARM:
8032       sec_type = SHT_ARM_EXIDX;
8033       break;
8034
8035     case EM_TI_C6000:
8036       sec_type = SHT_C6000_UNWIND;
8037       break;
8038
8039     default:
8040       error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
8041              elf_header.e_machine);
8042       return;
8043     }
8044
8045   if (string_table == NULL)
8046     return;
8047
8048   memset (& aux, 0, sizeof (aux));
8049   aux.file = file;
8050
8051   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
8052     {
8053       if (sec->sh_type == SHT_SYMTAB && sec->sh_link < elf_header.e_shnum)
8054         {
8055           aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
8056
8057           strsec = section_headers + sec->sh_link;
8058
8059           /* PR binutils/17531 file: 011-12666-0.004.  */
8060           if (aux.strtab != NULL)
8061             {
8062               error (_("Multiple string tables found in file.\n"));
8063               free (aux.strtab);
8064             }
8065           aux.strtab = get_data (NULL, file, strsec->sh_offset,
8066                                  1, strsec->sh_size, _("string table"));
8067           aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
8068         }
8069       else if (sec->sh_type == sec_type)
8070         unwsec = sec;
8071     }
8072
8073   if (unwsec == NULL)
8074     printf (_("\nThere are no unwind sections in this file.\n"));
8075   else
8076     for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
8077       {
8078         if (sec->sh_type == sec_type)
8079           {
8080             printf (_("\nUnwind table index '%s' at offset 0x%lx contains %lu entries:\n"),
8081                     printable_section_name (sec),
8082                     (unsigned long) sec->sh_offset,
8083                     (unsigned long) (sec->sh_size / (2 * eh_addr_size)));
8084
8085             dump_arm_unwind (&aux, sec);
8086           }
8087       }
8088
8089   if (aux.symtab)
8090     free (aux.symtab);
8091   if (aux.strtab)
8092     free ((char *) aux.strtab);
8093 }
8094
8095 static void
8096 process_unwind (FILE * file)
8097 {
8098   struct unwind_handler
8099   {
8100     int machtype;
8101     void (* handler)(FILE *);
8102   } handlers[] =
8103   {
8104     { EM_ARM, arm_process_unwind },
8105     { EM_IA_64, ia64_process_unwind },
8106     { EM_PARISC, hppa_process_unwind },
8107     { EM_TI_C6000, arm_process_unwind },
8108     { 0, 0 }
8109   };
8110   int i;
8111
8112   if (!do_unwind)
8113     return;
8114
8115   for (i = 0; handlers[i].handler != NULL; i++)
8116     if (elf_header.e_machine == handlers[i].machtype)
8117       {
8118         handlers[i].handler (file);
8119         return;
8120       }
8121
8122   printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
8123           get_machine_name (elf_header.e_machine));
8124 }
8125
8126 static void
8127 dynamic_section_mips_val (Elf_Internal_Dyn * entry)
8128 {
8129   switch (entry->d_tag)
8130     {
8131     case DT_MIPS_FLAGS:
8132       if (entry->d_un.d_val == 0)
8133         printf (_("NONE"));
8134       else
8135         {
8136           static const char * opts[] =
8137           {
8138             "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
8139             "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
8140             "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
8141             "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
8142             "RLD_ORDER_SAFE"
8143           };
8144           unsigned int cnt;
8145           int first = 1;
8146
8147           for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
8148             if (entry->d_un.d_val & (1 << cnt))
8149               {
8150                 printf ("%s%s", first ? "" : " ", opts[cnt]);
8151                 first = 0;
8152               }
8153         }
8154       break;
8155
8156     case DT_MIPS_IVERSION:
8157       if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
8158         printf (_("Interface Version: %s"), GET_DYNAMIC_NAME (entry->d_un.d_val));
8159       else
8160         {
8161           char buf[40];
8162           sprintf_vma (buf, entry->d_un.d_ptr);
8163           /* Note: coded this way so that there is a single string for translation.  */
8164           printf (_("<corrupt: %s>"), buf);
8165         }
8166       break;
8167
8168     case DT_MIPS_TIME_STAMP:
8169       {
8170         char timebuf[20];
8171         struct tm * tmp;
8172
8173         time_t atime = entry->d_un.d_val;
8174         tmp = gmtime (&atime);
8175         /* PR 17531: file: 6accc532.  */
8176         if (tmp == NULL)
8177           snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
8178         else
8179           snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
8180                     tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
8181                     tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
8182         printf (_("Time Stamp: %s"), timebuf);
8183       }
8184       break;
8185
8186     case DT_MIPS_RLD_VERSION:
8187     case DT_MIPS_LOCAL_GOTNO:
8188     case DT_MIPS_CONFLICTNO:
8189     case DT_MIPS_LIBLISTNO:
8190     case DT_MIPS_SYMTABNO:
8191     case DT_MIPS_UNREFEXTNO:
8192     case DT_MIPS_HIPAGENO:
8193     case DT_MIPS_DELTA_CLASS_NO:
8194     case DT_MIPS_DELTA_INSTANCE_NO:
8195     case DT_MIPS_DELTA_RELOC_NO:
8196     case DT_MIPS_DELTA_SYM_NO:
8197     case DT_MIPS_DELTA_CLASSSYM_NO:
8198     case DT_MIPS_COMPACT_SIZE:
8199       print_vma (entry->d_un.d_ptr, DEC);
8200       break;
8201
8202     default:
8203       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8204     }
8205     putchar ('\n');
8206 }
8207
8208 static void
8209 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
8210 {
8211   switch (entry->d_tag)
8212     {
8213     case DT_HP_DLD_FLAGS:
8214       {
8215         static struct
8216         {
8217           long int bit;
8218           const char * str;
8219         }
8220         flags[] =
8221         {
8222           { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
8223           { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
8224           { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
8225           { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
8226           { DT_HP_BIND_NOW, "HP_BIND_NOW" },
8227           { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
8228           { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
8229           { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
8230           { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
8231           { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
8232           { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
8233           { DT_HP_GST, "HP_GST" },
8234           { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
8235           { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
8236           { DT_HP_NODELETE, "HP_NODELETE" },
8237           { DT_HP_GROUP, "HP_GROUP" },
8238           { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
8239         };
8240         int first = 1;
8241         size_t cnt;
8242         bfd_vma val = entry->d_un.d_val;
8243
8244         for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
8245           if (val & flags[cnt].bit)
8246             {
8247               if (! first)
8248                 putchar (' ');
8249               fputs (flags[cnt].str, stdout);
8250               first = 0;
8251               val ^= flags[cnt].bit;
8252             }
8253
8254         if (val != 0 || first)
8255           {
8256             if (! first)
8257               putchar (' ');
8258             print_vma (val, HEX);
8259           }
8260       }
8261       break;
8262
8263     default:
8264       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8265       break;
8266     }
8267   putchar ('\n');
8268 }
8269
8270 #ifdef BFD64
8271
8272 /* VMS vs Unix time offset and factor.  */
8273
8274 #define VMS_EPOCH_OFFSET 35067168000000000LL
8275 #define VMS_GRANULARITY_FACTOR 10000000
8276
8277 /* Display a VMS time in a human readable format.  */
8278
8279 static void
8280 print_vms_time (bfd_int64_t vmstime)
8281 {
8282   struct tm *tm;
8283   time_t unxtime;
8284
8285   unxtime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
8286   tm = gmtime (&unxtime);
8287   printf ("%04u-%02u-%02uT%02u:%02u:%02u",
8288           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
8289           tm->tm_hour, tm->tm_min, tm->tm_sec);
8290 }
8291 #endif /* BFD64 */
8292
8293 static void
8294 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
8295 {
8296   switch (entry->d_tag)
8297     {
8298     case DT_IA_64_PLT_RESERVE:
8299       /* First 3 slots reserved.  */
8300       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8301       printf (" -- ");
8302       print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
8303       break;
8304
8305     case DT_IA_64_VMS_LINKTIME:
8306 #ifdef BFD64
8307       print_vms_time (entry->d_un.d_val);
8308 #endif
8309       break;
8310
8311     case DT_IA_64_VMS_LNKFLAGS:
8312       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8313       if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
8314         printf (" CALL_DEBUG");
8315       if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
8316         printf (" NOP0BUFS");
8317       if (entry->d_un.d_val & VMS_LF_P0IMAGE)
8318         printf (" P0IMAGE");
8319       if (entry->d_un.d_val & VMS_LF_MKTHREADS)
8320         printf (" MKTHREADS");
8321       if (entry->d_un.d_val & VMS_LF_UPCALLS)
8322         printf (" UPCALLS");
8323       if (entry->d_un.d_val & VMS_LF_IMGSTA)
8324         printf (" IMGSTA");
8325       if (entry->d_un.d_val & VMS_LF_INITIALIZE)
8326         printf (" INITIALIZE");
8327       if (entry->d_un.d_val & VMS_LF_MAIN)
8328         printf (" MAIN");
8329       if (entry->d_un.d_val & VMS_LF_EXE_INIT)
8330         printf (" EXE_INIT");
8331       if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
8332         printf (" TBK_IN_IMG");
8333       if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
8334         printf (" DBG_IN_IMG");
8335       if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
8336         printf (" TBK_IN_DSF");
8337       if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
8338         printf (" DBG_IN_DSF");
8339       if (entry->d_un.d_val & VMS_LF_SIGNATURES)
8340         printf (" SIGNATURES");
8341       if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
8342         printf (" REL_SEG_OFF");
8343       break;
8344
8345     default:
8346       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8347       break;
8348     }
8349   putchar ('\n');
8350 }
8351
8352 static int
8353 get_32bit_dynamic_section (FILE * file)
8354 {
8355   Elf32_External_Dyn * edyn;
8356   Elf32_External_Dyn * ext;
8357   Elf_Internal_Dyn * entry;
8358
8359   edyn = (Elf32_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
8360                                           dynamic_size, _("dynamic section"));
8361   if (!edyn)
8362     return 0;
8363
8364   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
8365      might not have the luxury of section headers.  Look for the DT_NULL
8366      terminator to determine the number of entries.  */
8367   for (ext = edyn, dynamic_nent = 0;
8368        (char *) (ext + 1) <= (char *) edyn + dynamic_size;
8369        ext++)
8370     {
8371       dynamic_nent++;
8372       if (BYTE_GET (ext->d_tag) == DT_NULL)
8373         break;
8374     }
8375
8376   dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
8377                                                   sizeof (* entry));
8378   if (dynamic_section == NULL)
8379     {
8380       error (_("Out of memory allocating space for %lu dynamic entries\n"),
8381              (unsigned long) dynamic_nent);
8382       free (edyn);
8383       return 0;
8384     }
8385
8386   for (ext = edyn, entry = dynamic_section;
8387        entry < dynamic_section + dynamic_nent;
8388        ext++, entry++)
8389     {
8390       entry->d_tag      = BYTE_GET (ext->d_tag);
8391       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
8392     }
8393
8394   free (edyn);
8395
8396   return 1;
8397 }
8398
8399 static int
8400 get_64bit_dynamic_section (FILE * file)
8401 {
8402   Elf64_External_Dyn * edyn;
8403   Elf64_External_Dyn * ext;
8404   Elf_Internal_Dyn * entry;
8405
8406   /* Read in the data.  */
8407   edyn = (Elf64_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
8408                                           dynamic_size, _("dynamic section"));
8409   if (!edyn)
8410     return 0;
8411
8412   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
8413      might not have the luxury of section headers.  Look for the DT_NULL
8414      terminator to determine the number of entries.  */
8415   for (ext = edyn, dynamic_nent = 0;
8416        /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer.  */
8417        (char *) (ext + 1) <= (char *) edyn + dynamic_size;
8418        ext++)
8419     {
8420       dynamic_nent++;
8421       if (BYTE_GET (ext->d_tag) == DT_NULL)
8422         break;
8423     }
8424
8425   dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
8426                                                   sizeof (* entry));
8427   if (dynamic_section == NULL)
8428     {
8429       error (_("Out of memory allocating space for %lu dynamic entries\n"),
8430              (unsigned long) dynamic_nent);
8431       free (edyn);
8432       return 0;
8433     }
8434
8435   /* Convert from external to internal formats.  */
8436   for (ext = edyn, entry = dynamic_section;
8437        entry < dynamic_section + dynamic_nent;
8438        ext++, entry++)
8439     {
8440       entry->d_tag      = BYTE_GET (ext->d_tag);
8441       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
8442     }
8443
8444   free (edyn);
8445
8446   return 1;
8447 }
8448
8449 static void
8450 print_dynamic_flags (bfd_vma flags)
8451 {
8452   int first = 1;
8453
8454   while (flags)
8455     {
8456       bfd_vma flag;
8457
8458       flag = flags & - flags;
8459       flags &= ~ flag;
8460
8461       if (first)
8462         first = 0;
8463       else
8464         putc (' ', stdout);
8465
8466       switch (flag)
8467         {
8468         case DF_ORIGIN:         fputs ("ORIGIN", stdout); break;
8469         case DF_SYMBOLIC:       fputs ("SYMBOLIC", stdout); break;
8470         case DF_TEXTREL:        fputs ("TEXTREL", stdout); break;
8471         case DF_BIND_NOW:       fputs ("BIND_NOW", stdout); break;
8472         case DF_STATIC_TLS:     fputs ("STATIC_TLS", stdout); break;
8473         default:                fputs (_("unknown"), stdout); break;
8474         }
8475     }
8476   puts ("");
8477 }
8478
8479 /* Parse and display the contents of the dynamic section.  */
8480
8481 static int
8482 process_dynamic_section (FILE * file)
8483 {
8484   Elf_Internal_Dyn * entry;
8485
8486   if (dynamic_size == 0)
8487     {
8488       if (do_dynamic)
8489         printf (_("\nThere is no dynamic section in this file.\n"));
8490
8491       return 1;
8492     }
8493
8494   if (is_32bit_elf)
8495     {
8496       if (! get_32bit_dynamic_section (file))
8497         return 0;
8498     }
8499   else if (! get_64bit_dynamic_section (file))
8500     return 0;
8501
8502   /* Find the appropriate symbol table.  */
8503   if (dynamic_symbols == NULL)
8504     {
8505       for (entry = dynamic_section;
8506            entry < dynamic_section + dynamic_nent;
8507            ++entry)
8508         {
8509           Elf_Internal_Shdr section;
8510
8511           if (entry->d_tag != DT_SYMTAB)
8512             continue;
8513
8514           dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
8515
8516           /* Since we do not know how big the symbol table is,
8517              we default to reading in the entire file (!) and
8518              processing that.  This is overkill, I know, but it
8519              should work.  */
8520           section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
8521
8522           if (archive_file_offset != 0)
8523             section.sh_size = archive_file_size - section.sh_offset;
8524           else
8525             {
8526               if (fseek (file, 0, SEEK_END))
8527                 error (_("Unable to seek to end of file!\n"));
8528
8529               section.sh_size = ftell (file) - section.sh_offset;
8530             }
8531
8532           if (is_32bit_elf)
8533             section.sh_entsize = sizeof (Elf32_External_Sym);
8534           else
8535             section.sh_entsize = sizeof (Elf64_External_Sym);
8536           section.sh_name = string_table_length;
8537
8538           dynamic_symbols = GET_ELF_SYMBOLS (file, &section, & num_dynamic_syms);
8539           if (num_dynamic_syms < 1)
8540             {
8541               error (_("Unable to determine the number of symbols to load\n"));
8542               continue;
8543             }
8544         }
8545     }
8546
8547   /* Similarly find a string table.  */
8548   if (dynamic_strings == NULL)
8549     {
8550       for (entry = dynamic_section;
8551            entry < dynamic_section + dynamic_nent;
8552            ++entry)
8553         {
8554           unsigned long offset;
8555           long str_tab_len;
8556
8557           if (entry->d_tag != DT_STRTAB)
8558             continue;
8559
8560           dynamic_info[DT_STRTAB] = entry->d_un.d_val;
8561
8562           /* Since we do not know how big the string table is,
8563              we default to reading in the entire file (!) and
8564              processing that.  This is overkill, I know, but it
8565              should work.  */
8566
8567           offset = offset_from_vma (file, entry->d_un.d_val, 0);
8568
8569           if (archive_file_offset != 0)
8570             str_tab_len = archive_file_size - offset;
8571           else
8572             {
8573               if (fseek (file, 0, SEEK_END))
8574                 error (_("Unable to seek to end of file\n"));
8575               str_tab_len = ftell (file) - offset;
8576             }
8577
8578           if (str_tab_len < 1)
8579             {
8580               error
8581                 (_("Unable to determine the length of the dynamic string table\n"));
8582               continue;
8583             }
8584
8585           dynamic_strings = (char *) get_data (NULL, file, offset, 1,
8586                                                str_tab_len,
8587                                                _("dynamic string table"));
8588           dynamic_strings_length = dynamic_strings == NULL ? 0 : str_tab_len;
8589           break;
8590         }
8591     }
8592
8593   /* And find the syminfo section if available.  */
8594   if (dynamic_syminfo == NULL)
8595     {
8596       unsigned long syminsz = 0;
8597
8598       for (entry = dynamic_section;
8599            entry < dynamic_section + dynamic_nent;
8600            ++entry)
8601         {
8602           if (entry->d_tag == DT_SYMINENT)
8603             {
8604               /* Note: these braces are necessary to avoid a syntax
8605                  error from the SunOS4 C compiler.  */
8606               /* PR binutils/17531: A corrupt file can trigger this test.
8607                  So do not use an assert, instead generate an error message.  */
8608               if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
8609                 error (_("Bad value (%d) for SYMINENT entry\n"),
8610                        (int) entry->d_un.d_val);
8611             }
8612           else if (entry->d_tag == DT_SYMINSZ)
8613             syminsz = entry->d_un.d_val;
8614           else if (entry->d_tag == DT_SYMINFO)
8615             dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
8616                                                       syminsz);
8617         }
8618
8619       if (dynamic_syminfo_offset != 0 && syminsz != 0)
8620         {
8621           Elf_External_Syminfo * extsyminfo;
8622           Elf_External_Syminfo * extsym;
8623           Elf_Internal_Syminfo * syminfo;
8624
8625           /* There is a syminfo section.  Read the data.  */
8626           extsyminfo = (Elf_External_Syminfo *)
8627               get_data (NULL, file, dynamic_syminfo_offset, 1, syminsz,
8628                         _("symbol information"));
8629           if (!extsyminfo)
8630             return 0;
8631
8632           dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
8633           if (dynamic_syminfo == NULL)
8634             {
8635               error (_("Out of memory allocating %lu byte for dynamic symbol info\n"),
8636                      (unsigned long) syminsz);
8637               return 0;
8638             }
8639
8640           dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
8641           for (syminfo = dynamic_syminfo, extsym = extsyminfo;
8642                syminfo < dynamic_syminfo + dynamic_syminfo_nent;
8643                ++syminfo, ++extsym)
8644             {
8645               syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
8646               syminfo->si_flags = BYTE_GET (extsym->si_flags);
8647             }
8648
8649           free (extsyminfo);
8650         }
8651     }
8652
8653   if (do_dynamic && dynamic_addr)
8654     printf (_("\nDynamic section at offset 0x%lx contains %lu entries:\n"),
8655             dynamic_addr, (unsigned long) dynamic_nent);
8656   if (do_dynamic)
8657     printf (_("  Tag        Type                         Name/Value\n"));
8658
8659   for (entry = dynamic_section;
8660        entry < dynamic_section + dynamic_nent;
8661        entry++)
8662     {
8663       if (do_dynamic)
8664         {
8665           const char * dtype;
8666
8667           putchar (' ');
8668           print_vma (entry->d_tag, FULL_HEX);
8669           dtype = get_dynamic_type (entry->d_tag);
8670           printf (" (%s)%*s", dtype,
8671                   ((is_32bit_elf ? 27 : 19)
8672                    - (int) strlen (dtype)),
8673                   " ");
8674         }
8675
8676       switch (entry->d_tag)
8677         {
8678         case DT_FLAGS:
8679           if (do_dynamic)
8680             print_dynamic_flags (entry->d_un.d_val);
8681           break;
8682
8683         case DT_AUXILIARY:
8684         case DT_FILTER:
8685         case DT_CONFIG:
8686         case DT_DEPAUDIT:
8687         case DT_AUDIT:
8688           if (do_dynamic)
8689             {
8690               switch (entry->d_tag)
8691                 {
8692                 case DT_AUXILIARY:
8693                   printf (_("Auxiliary library"));
8694                   break;
8695
8696                 case DT_FILTER:
8697                   printf (_("Filter library"));
8698                   break;
8699
8700                 case DT_CONFIG:
8701                   printf (_("Configuration file"));
8702                   break;
8703
8704                 case DT_DEPAUDIT:
8705                   printf (_("Dependency audit library"));
8706                   break;
8707
8708                 case DT_AUDIT:
8709                   printf (_("Audit library"));
8710                   break;
8711                 }
8712
8713               if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
8714                 printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
8715               else
8716                 {
8717                   printf (": ");
8718                   print_vma (entry->d_un.d_val, PREFIX_HEX);
8719                   putchar ('\n');
8720                 }
8721             }
8722           break;
8723
8724         case DT_FEATURE:
8725           if (do_dynamic)
8726             {
8727               printf (_("Flags:"));
8728
8729               if (entry->d_un.d_val == 0)
8730                 printf (_(" None\n"));
8731               else
8732                 {
8733                   unsigned long int val = entry->d_un.d_val;
8734
8735                   if (val & DTF_1_PARINIT)
8736                     {
8737                       printf (" PARINIT");
8738                       val ^= DTF_1_PARINIT;
8739                     }
8740                   if (val & DTF_1_CONFEXP)
8741                     {
8742                       printf (" CONFEXP");
8743                       val ^= DTF_1_CONFEXP;
8744                     }
8745                   if (val != 0)
8746                     printf (" %lx", val);
8747                   puts ("");
8748                 }
8749             }
8750           break;
8751
8752         case DT_POSFLAG_1:
8753           if (do_dynamic)
8754             {
8755               printf (_("Flags:"));
8756
8757               if (entry->d_un.d_val == 0)
8758                 printf (_(" None\n"));
8759               else
8760                 {
8761                   unsigned long int val = entry->d_un.d_val;
8762
8763                   if (val & DF_P1_LAZYLOAD)
8764                     {
8765                       printf (" LAZYLOAD");
8766                       val ^= DF_P1_LAZYLOAD;
8767                     }
8768                   if (val & DF_P1_GROUPPERM)
8769                     {
8770                       printf (" GROUPPERM");
8771                       val ^= DF_P1_GROUPPERM;
8772                     }
8773                   if (val != 0)
8774                     printf (" %lx", val);
8775                   puts ("");
8776                 }
8777             }
8778           break;
8779
8780         case DT_FLAGS_1:
8781           if (do_dynamic)
8782             {
8783               printf (_("Flags:"));
8784               if (entry->d_un.d_val == 0)
8785                 printf (_(" None\n"));
8786               else
8787                 {
8788                   unsigned long int val = entry->d_un.d_val;
8789
8790                   if (val & DF_1_NOW)
8791                     {
8792                       printf (" NOW");
8793                       val ^= DF_1_NOW;
8794                     }
8795                   if (val & DF_1_GLOBAL)
8796                     {
8797                       printf (" GLOBAL");
8798                       val ^= DF_1_GLOBAL;
8799                     }
8800                   if (val & DF_1_GROUP)
8801                     {
8802                       printf (" GROUP");
8803                       val ^= DF_1_GROUP;
8804                     }
8805                   if (val & DF_1_NODELETE)
8806                     {
8807                       printf (" NODELETE");
8808                       val ^= DF_1_NODELETE;
8809                     }
8810                   if (val & DF_1_LOADFLTR)
8811                     {
8812                       printf (" LOADFLTR");
8813                       val ^= DF_1_LOADFLTR;
8814                     }
8815                   if (val & DF_1_INITFIRST)
8816                     {
8817                       printf (" INITFIRST");
8818                       val ^= DF_1_INITFIRST;
8819                     }
8820                   if (val & DF_1_NOOPEN)
8821                     {
8822                       printf (" NOOPEN");
8823                       val ^= DF_1_NOOPEN;
8824                     }
8825                   if (val & DF_1_ORIGIN)
8826                     {
8827                       printf (" ORIGIN");
8828                       val ^= DF_1_ORIGIN;
8829                     }
8830                   if (val & DF_1_DIRECT)
8831                     {
8832                       printf (" DIRECT");
8833                       val ^= DF_1_DIRECT;
8834                     }
8835                   if (val & DF_1_TRANS)
8836                     {
8837                       printf (" TRANS");
8838                       val ^= DF_1_TRANS;
8839                     }
8840                   if (val & DF_1_INTERPOSE)
8841                     {
8842                       printf (" INTERPOSE");
8843                       val ^= DF_1_INTERPOSE;
8844                     }
8845                   if (val & DF_1_NODEFLIB)
8846                     {
8847                       printf (" NODEFLIB");
8848                       val ^= DF_1_NODEFLIB;
8849                     }
8850                   if (val & DF_1_NODUMP)
8851                     {
8852                       printf (" NODUMP");
8853                       val ^= DF_1_NODUMP;
8854                     }
8855                   if (val & DF_1_CONFALT)
8856                     {
8857                       printf (" CONFALT");
8858                       val ^= DF_1_CONFALT;
8859                     }
8860                   if (val & DF_1_ENDFILTEE)
8861                     {
8862                       printf (" ENDFILTEE");
8863                       val ^= DF_1_ENDFILTEE;
8864                     }
8865                   if (val & DF_1_DISPRELDNE)
8866                     {
8867                       printf (" DISPRELDNE");
8868                       val ^= DF_1_DISPRELDNE;
8869                     }
8870                   if (val & DF_1_DISPRELPND)
8871                     {
8872                       printf (" DISPRELPND");
8873                       val ^= DF_1_DISPRELPND;
8874                     }
8875                   if (val & DF_1_NODIRECT)
8876                     {
8877                       printf (" NODIRECT");
8878                       val ^= DF_1_NODIRECT;
8879                     }
8880                   if (val & DF_1_IGNMULDEF)
8881                     {
8882                       printf (" IGNMULDEF");
8883                       val ^= DF_1_IGNMULDEF;
8884                     }
8885                   if (val & DF_1_NOKSYMS)
8886                     {
8887                       printf (" NOKSYMS");
8888                       val ^= DF_1_NOKSYMS;
8889                     }
8890                   if (val & DF_1_NOHDR)
8891                     {
8892                       printf (" NOHDR");
8893                       val ^= DF_1_NOHDR;
8894                     }
8895                   if (val & DF_1_EDITED)
8896                     {
8897                       printf (" EDITED");
8898                       val ^= DF_1_EDITED;
8899                     }
8900                   if (val & DF_1_NORELOC)
8901                     {
8902                       printf (" NORELOC");
8903                       val ^= DF_1_NORELOC;
8904                     }
8905                   if (val & DF_1_SYMINTPOSE)
8906                     {
8907                       printf (" SYMINTPOSE");
8908                       val ^= DF_1_SYMINTPOSE;
8909                     }
8910                   if (val & DF_1_GLOBAUDIT)
8911                     {
8912                       printf (" GLOBAUDIT");
8913                       val ^= DF_1_GLOBAUDIT;
8914                     }
8915                   if (val & DF_1_SINGLETON)
8916                     {
8917                       printf (" SINGLETON");
8918                       val ^= DF_1_SINGLETON;
8919                     }
8920                   if (val != 0)
8921                     printf (" %lx", val);
8922                   puts ("");
8923                 }
8924             }
8925           break;
8926
8927         case DT_PLTREL:
8928           dynamic_info[entry->d_tag] = entry->d_un.d_val;
8929           if (do_dynamic)
8930             puts (get_dynamic_type (entry->d_un.d_val));
8931           break;
8932
8933         case DT_NULL    :
8934         case DT_NEEDED  :
8935         case DT_PLTGOT  :
8936         case DT_HASH    :
8937         case DT_STRTAB  :
8938         case DT_SYMTAB  :
8939         case DT_RELA    :
8940         case DT_INIT    :
8941         case DT_FINI    :
8942         case DT_SONAME  :
8943         case DT_RPATH   :
8944         case DT_SYMBOLIC:
8945         case DT_REL     :
8946         case DT_DEBUG   :
8947         case DT_TEXTREL :
8948         case DT_JMPREL  :
8949         case DT_RUNPATH :
8950           dynamic_info[entry->d_tag] = entry->d_un.d_val;
8951
8952           if (do_dynamic)
8953             {
8954               char * name;
8955
8956               if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
8957                 name = GET_DYNAMIC_NAME (entry->d_un.d_val);
8958               else
8959                 name = NULL;
8960
8961               if (name)
8962                 {
8963                   switch (entry->d_tag)
8964                     {
8965                     case DT_NEEDED:
8966                       printf (_("Shared library: [%s]"), name);
8967
8968                       if (streq (name, program_interpreter))
8969                         printf (_(" program interpreter"));
8970                       break;
8971
8972                     case DT_SONAME:
8973                       printf (_("Library soname: [%s]"), name);
8974                       break;
8975
8976                     case DT_RPATH:
8977                       printf (_("Library rpath: [%s]"), name);
8978                       break;
8979
8980                     case DT_RUNPATH:
8981                       printf (_("Library runpath: [%s]"), name);
8982                       break;
8983
8984                     default:
8985                       print_vma (entry->d_un.d_val, PREFIX_HEX);
8986                       break;
8987                     }
8988                 }
8989               else
8990                 print_vma (entry->d_un.d_val, PREFIX_HEX);
8991
8992               putchar ('\n');
8993             }
8994           break;
8995
8996         case DT_PLTRELSZ:
8997         case DT_RELASZ  :
8998         case DT_STRSZ   :
8999         case DT_RELSZ   :
9000         case DT_RELAENT :
9001         case DT_SYMENT  :
9002         case DT_RELENT  :
9003           dynamic_info[entry->d_tag] = entry->d_un.d_val;
9004         case DT_PLTPADSZ:
9005         case DT_MOVEENT :
9006         case DT_MOVESZ  :
9007         case DT_INIT_ARRAYSZ:
9008         case DT_FINI_ARRAYSZ:
9009         case DT_GNU_CONFLICTSZ:
9010         case DT_GNU_LIBLISTSZ:
9011           if (do_dynamic)
9012             {
9013               print_vma (entry->d_un.d_val, UNSIGNED);
9014               printf (_(" (bytes)\n"));
9015             }
9016           break;
9017
9018         case DT_VERDEFNUM:
9019         case DT_VERNEEDNUM:
9020         case DT_RELACOUNT:
9021         case DT_RELCOUNT:
9022           if (do_dynamic)
9023             {
9024               print_vma (entry->d_un.d_val, UNSIGNED);
9025               putchar ('\n');
9026             }
9027           break;
9028
9029         case DT_SYMINSZ:
9030         case DT_SYMINENT:
9031         case DT_SYMINFO:
9032         case DT_USED:
9033         case DT_INIT_ARRAY:
9034         case DT_FINI_ARRAY:
9035           if (do_dynamic)
9036             {
9037               if (entry->d_tag == DT_USED
9038                   && VALID_DYNAMIC_NAME (entry->d_un.d_val))
9039                 {
9040                   char * name = GET_DYNAMIC_NAME (entry->d_un.d_val);
9041
9042                   if (*name)
9043                     {
9044                       printf (_("Not needed object: [%s]\n"), name);
9045                       break;
9046                     }
9047                 }
9048
9049               print_vma (entry->d_un.d_val, PREFIX_HEX);
9050               putchar ('\n');
9051             }
9052           break;
9053
9054         case DT_BIND_NOW:
9055           /* The value of this entry is ignored.  */
9056           if (do_dynamic)
9057             putchar ('\n');
9058           break;
9059
9060         case DT_GNU_PRELINKED:
9061           if (do_dynamic)
9062             {
9063               struct tm * tmp;
9064               time_t atime = entry->d_un.d_val;
9065
9066               tmp = gmtime (&atime);
9067               /* PR 17533 file: 041-1244816-0.004.  */
9068               if (tmp == NULL)
9069                 printf (_("<corrupt time val: %lx"),
9070                         (unsigned long) atime);
9071               else
9072                 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
9073                         tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9074                         tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9075
9076             }
9077           break;
9078
9079         case DT_GNU_HASH:
9080           dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
9081           if (do_dynamic)
9082             {
9083               print_vma (entry->d_un.d_val, PREFIX_HEX);
9084               putchar ('\n');
9085             }
9086           break;
9087
9088         default:
9089           if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
9090             version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
9091               entry->d_un.d_val;
9092
9093           if (do_dynamic)
9094             {
9095               switch (elf_header.e_machine)
9096                 {
9097                 case EM_MIPS:
9098                 case EM_MIPS_RS3_LE:
9099                   dynamic_section_mips_val (entry);
9100                   break;
9101                 case EM_PARISC:
9102                   dynamic_section_parisc_val (entry);
9103                   break;
9104                 case EM_IA_64:
9105                   dynamic_section_ia64_val (entry);
9106                   break;
9107                 default:
9108                   print_vma (entry->d_un.d_val, PREFIX_HEX);
9109                   putchar ('\n');
9110                 }
9111             }
9112           break;
9113         }
9114     }
9115
9116   return 1;
9117 }
9118
9119 static char *
9120 get_ver_flags (unsigned int flags)
9121 {
9122   static char buff[32];
9123
9124   buff[0] = 0;
9125
9126   if (flags == 0)
9127     return _("none");
9128
9129   if (flags & VER_FLG_BASE)
9130     strcat (buff, "BASE ");
9131
9132   if (flags & VER_FLG_WEAK)
9133     {
9134       if (flags & VER_FLG_BASE)
9135         strcat (buff, "| ");
9136
9137       strcat (buff, "WEAK ");
9138     }
9139
9140   if (flags & VER_FLG_INFO)
9141     {
9142       if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
9143         strcat (buff, "| ");
9144
9145       strcat (buff, "INFO ");
9146     }
9147
9148   if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
9149     strcat (buff, _("| <unknown>"));
9150
9151   return buff;
9152 }
9153
9154 /* Display the contents of the version sections.  */
9155
9156 static int
9157 process_version_sections (FILE * file)
9158 {
9159   Elf_Internal_Shdr * section;
9160   unsigned i;
9161   int found = 0;
9162
9163   if (! do_version)
9164     return 1;
9165
9166   for (i = 0, section = section_headers;
9167        i < elf_header.e_shnum;
9168        i++, section++)
9169     {
9170       switch (section->sh_type)
9171         {
9172         case SHT_GNU_verdef:
9173           {
9174             Elf_External_Verdef * edefs;
9175             unsigned int idx;
9176             unsigned int cnt;
9177             char * endbuf;
9178
9179             found = 1;
9180
9181             printf (_("\nVersion definition section '%s' contains %u entries:\n"),
9182                     printable_section_name (section),
9183                     section->sh_info);
9184
9185             printf (_("  Addr: 0x"));
9186             printf_vma (section->sh_addr);
9187             printf (_("  Offset: %#08lx  Link: %u (%s)"),
9188                     (unsigned long) section->sh_offset, section->sh_link,
9189                     printable_section_name_from_index (section->sh_link));
9190
9191             edefs = (Elf_External_Verdef *)
9192                 get_data (NULL, file, section->sh_offset, 1,section->sh_size,
9193                           _("version definition section"));
9194             if (!edefs)
9195               break;
9196             endbuf = (char *) edefs + section->sh_size;
9197
9198             for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
9199               {
9200                 char * vstart;
9201                 Elf_External_Verdef * edef;
9202                 Elf_Internal_Verdef ent;
9203                 Elf_External_Verdaux * eaux;
9204                 Elf_Internal_Verdaux aux;
9205                 int j;
9206                 int isum;
9207
9208                 /* Check for very large indicies.  */
9209                 if (idx > (size_t) (endbuf - (char *) edefs))
9210                   break;
9211
9212                 vstart = ((char *) edefs) + idx;
9213                 if (vstart + sizeof (*edef) > endbuf)
9214                   break;
9215
9216                 edef = (Elf_External_Verdef *) vstart;
9217
9218                 ent.vd_version = BYTE_GET (edef->vd_version);
9219                 ent.vd_flags   = BYTE_GET (edef->vd_flags);
9220                 ent.vd_ndx     = BYTE_GET (edef->vd_ndx);
9221                 ent.vd_cnt     = BYTE_GET (edef->vd_cnt);
9222                 ent.vd_hash    = BYTE_GET (edef->vd_hash);
9223                 ent.vd_aux     = BYTE_GET (edef->vd_aux);
9224                 ent.vd_next    = BYTE_GET (edef->vd_next);
9225
9226                 printf (_("  %#06x: Rev: %d  Flags: %s"),
9227                         idx, ent.vd_version, get_ver_flags (ent.vd_flags));
9228
9229                 printf (_("  Index: %d  Cnt: %d  "),
9230                         ent.vd_ndx, ent.vd_cnt);
9231
9232                 /* Check for overflow.  */
9233                 if (ent.vd_aux > (size_t) (endbuf - vstart))
9234                   break;
9235
9236                 vstart += ent.vd_aux;
9237
9238                 eaux = (Elf_External_Verdaux *) vstart;
9239
9240                 aux.vda_name = BYTE_GET (eaux->vda_name);
9241                 aux.vda_next = BYTE_GET (eaux->vda_next);
9242
9243                 if (VALID_DYNAMIC_NAME (aux.vda_name))
9244                   printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
9245                 else
9246                   printf (_("Name index: %ld\n"), aux.vda_name);
9247
9248                 isum = idx + ent.vd_aux;
9249
9250                 for (j = 1; j < ent.vd_cnt; j++)
9251                   {
9252                     /* Check for overflow.  */
9253                     if (aux.vda_next > (size_t) (endbuf - vstart))
9254                       break;
9255
9256                     isum   += aux.vda_next;
9257                     vstart += aux.vda_next;
9258
9259                     eaux = (Elf_External_Verdaux *) vstart;
9260                     if (vstart + sizeof (*eaux) > endbuf)
9261                       break;
9262
9263                     aux.vda_name = BYTE_GET (eaux->vda_name);
9264                     aux.vda_next = BYTE_GET (eaux->vda_next);
9265
9266                     if (VALID_DYNAMIC_NAME (aux.vda_name))
9267                       printf (_("  %#06x: Parent %d: %s\n"),
9268                               isum, j, GET_DYNAMIC_NAME (aux.vda_name));
9269                     else
9270                       printf (_("  %#06x: Parent %d, name index: %ld\n"),
9271                               isum, j, aux.vda_name);
9272                   }
9273
9274                 if (j < ent.vd_cnt)
9275                   printf (_("  Version def aux past end of section\n"));
9276
9277                 /* PR 17531: file: id:000001,src:000172+005151,op:splice,rep:2.  */
9278                 if (idx + ent.vd_next <= idx)
9279                   break;
9280
9281                 idx += ent.vd_next;
9282               }
9283
9284             if (cnt < section->sh_info)
9285               printf (_("  Version definition past end of section\n"));
9286
9287             free (edefs);
9288           }
9289           break;
9290
9291         case SHT_GNU_verneed:
9292           {
9293             Elf_External_Verneed * eneed;
9294             unsigned int idx;
9295             unsigned int cnt;
9296             char * endbuf;
9297
9298             found = 1;
9299
9300             printf (_("\nVersion needs section '%s' contains %u entries:\n"),
9301                     printable_section_name (section), section->sh_info);
9302
9303             printf (_(" Addr: 0x"));
9304             printf_vma (section->sh_addr);
9305             printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
9306                     (unsigned long) section->sh_offset, section->sh_link,
9307                     printable_section_name_from_index (section->sh_link));
9308
9309             eneed = (Elf_External_Verneed *) get_data (NULL, file,
9310                                                        section->sh_offset, 1,
9311                                                        section->sh_size,
9312                                                        _("Version Needs section"));
9313             if (!eneed)
9314               break;
9315             endbuf = (char *) eneed + section->sh_size;
9316
9317             for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
9318               {
9319                 Elf_External_Verneed * entry;
9320                 Elf_Internal_Verneed ent;
9321                 int j;
9322                 int isum;
9323                 char * vstart;
9324
9325                 if (idx > (size_t) (endbuf - (char *) eneed))
9326                   break;
9327
9328                 vstart = ((char *) eneed) + idx;
9329                 if (vstart + sizeof (*entry) > endbuf)
9330                   break;
9331
9332                 entry = (Elf_External_Verneed *) vstart;
9333
9334                 ent.vn_version = BYTE_GET (entry->vn_version);
9335                 ent.vn_cnt     = BYTE_GET (entry->vn_cnt);
9336                 ent.vn_file    = BYTE_GET (entry->vn_file);
9337                 ent.vn_aux     = BYTE_GET (entry->vn_aux);
9338                 ent.vn_next    = BYTE_GET (entry->vn_next);
9339
9340                 printf (_("  %#06x: Version: %d"), idx, ent.vn_version);
9341
9342                 if (VALID_DYNAMIC_NAME (ent.vn_file))
9343                   printf (_("  File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
9344                 else
9345                   printf (_("  File: %lx"), ent.vn_file);
9346
9347                 printf (_("  Cnt: %d\n"), ent.vn_cnt);
9348
9349                 /* Check for overflow.  */
9350                 if (ent.vn_aux > (size_t) (endbuf - vstart))
9351                   break;
9352
9353                 vstart += ent.vn_aux;
9354
9355                 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
9356                   {
9357                     Elf_External_Vernaux * eaux;
9358                     Elf_Internal_Vernaux aux;
9359
9360                     if (vstart + sizeof (*eaux) > endbuf)
9361                       break;
9362                     eaux = (Elf_External_Vernaux *) vstart;
9363
9364                     aux.vna_hash  = BYTE_GET (eaux->vna_hash);
9365                     aux.vna_flags = BYTE_GET (eaux->vna_flags);
9366                     aux.vna_other = BYTE_GET (eaux->vna_other);
9367                     aux.vna_name  = BYTE_GET (eaux->vna_name);
9368                     aux.vna_next  = BYTE_GET (eaux->vna_next);
9369
9370                     if (VALID_DYNAMIC_NAME (aux.vna_name))
9371                       printf (_("  %#06x:   Name: %s"),
9372                               isum, GET_DYNAMIC_NAME (aux.vna_name));
9373                     else
9374                       printf (_("  %#06x:   Name index: %lx"),
9375                               isum, aux.vna_name);
9376
9377                     printf (_("  Flags: %s  Version: %d\n"),
9378                             get_ver_flags (aux.vna_flags), aux.vna_other);
9379
9380                     /* Check for overflow.  */
9381                     if (aux.vna_next > (size_t) (endbuf - vstart)
9382                         || (aux.vna_next == 0 && j < ent.vn_cnt - 1))
9383                       {
9384                         warn (_("Invalid vna_next field of %lx\n"),
9385                               aux.vna_next);
9386                         j = ent.vn_cnt;
9387                         break;
9388                       }
9389
9390                     isum   += aux.vna_next;
9391                     vstart += aux.vna_next;
9392                   }
9393
9394                 if (j < ent.vn_cnt)
9395                   warn (_("Missing Version Needs auxillary information\n"));
9396
9397                 if (ent.vn_next == 0 && cnt < section->sh_info - 1)
9398                   {
9399                     warn (_("Corrupt Version Needs structure - offset to next structure is zero with entries still left to be processed\n"));
9400                     cnt = section->sh_info;
9401                     break;
9402                   }
9403                 idx += ent.vn_next;
9404               }
9405
9406             if (cnt < section->sh_info)
9407               warn (_("Missing Version Needs information\n"));
9408
9409             free (eneed);
9410           }
9411           break;
9412
9413         case SHT_GNU_versym:
9414           {
9415             Elf_Internal_Shdr * link_section;
9416             size_t total;
9417             unsigned int cnt;
9418             unsigned char * edata;
9419             unsigned short * data;
9420             char * strtab;
9421             Elf_Internal_Sym * symbols;
9422             Elf_Internal_Shdr * string_sec;
9423             unsigned long num_syms;
9424             long off;
9425
9426             if (section->sh_link >= elf_header.e_shnum)
9427               break;
9428
9429             link_section = section_headers + section->sh_link;
9430             total = section->sh_size / sizeof (Elf_External_Versym);
9431
9432             if (link_section->sh_link >= elf_header.e_shnum)
9433               break;
9434
9435             found = 1;
9436
9437             symbols = GET_ELF_SYMBOLS (file, link_section, & num_syms);
9438             if (symbols == NULL)
9439               break;
9440
9441             string_sec = section_headers + link_section->sh_link;
9442
9443             strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
9444                                         string_sec->sh_size,
9445                                         _("version string table"));
9446             if (!strtab)
9447               {
9448                 free (symbols);
9449                 break;
9450               }
9451
9452             printf (_("\nVersion symbols section '%s' contains %lu entries:\n"),
9453                     printable_section_name (section), (unsigned long) total);
9454
9455             printf (_(" Addr: "));
9456             printf_vma (section->sh_addr);
9457             printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
9458                     (unsigned long) section->sh_offset, section->sh_link,
9459                     printable_section_name (link_section));
9460
9461             off = offset_from_vma (file,
9462                                    version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
9463                                    total * sizeof (short));
9464             edata = (unsigned char *) get_data (NULL, file, off, total,
9465                                                 sizeof (short),
9466                                                 _("version symbol data"));
9467             if (!edata)
9468               {
9469                 free (strtab);
9470                 free (symbols);
9471                 break;
9472               }
9473
9474             data = (short unsigned int *) cmalloc (total, sizeof (short));
9475
9476             for (cnt = total; cnt --;)
9477               data[cnt] = byte_get (edata + cnt * sizeof (short),
9478                                     sizeof (short));
9479
9480             free (edata);
9481
9482             for (cnt = 0; cnt < total; cnt += 4)
9483               {
9484                 int j, nn;
9485                 int check_def, check_need;
9486                 char * name;
9487
9488                 printf ("  %03x:", cnt);
9489
9490                 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
9491                   switch (data[cnt + j])
9492                     {
9493                     case 0:
9494                       fputs (_("   0 (*local*)    "), stdout);
9495                       break;
9496
9497                     case 1:
9498                       fputs (_("   1 (*global*)   "), stdout);
9499                       break;
9500
9501                     default:
9502                       nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
9503                                    data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
9504
9505                       /* If this index value is greater than the size of the symbols
9506                          array, break to avoid an out-of-bounds read.  */
9507                       if ((unsigned long)(cnt + j) >= num_syms)
9508                         {
9509                           warn (_("invalid index into symbol array\n"));
9510                           break;
9511                         }
9512
9513                       check_def = 1;
9514                       check_need = 1;
9515                       if (symbols[cnt + j].st_shndx >= elf_header.e_shnum
9516                           || section_headers[symbols[cnt + j].st_shndx].sh_type
9517                              != SHT_NOBITS)
9518                         {
9519                           if (symbols[cnt + j].st_shndx == SHN_UNDEF)
9520                             check_def = 0;
9521                           else
9522                             check_need = 0;
9523                         }
9524
9525                       if (check_need
9526                           && version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
9527                         {
9528                           Elf_Internal_Verneed ivn;
9529                           unsigned long offset;
9530
9531                           offset = offset_from_vma
9532                             (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
9533                              sizeof (Elf_External_Verneed));
9534
9535                           do
9536                             {
9537                               Elf_Internal_Vernaux ivna;
9538                               Elf_External_Verneed evn;
9539                               Elf_External_Vernaux evna;
9540                               unsigned long a_off;
9541
9542                               if (get_data (&evn, file, offset, sizeof (evn), 1,
9543                                             _("version need")) == NULL)
9544                                 break;
9545
9546                               ivn.vn_aux  = BYTE_GET (evn.vn_aux);
9547                               ivn.vn_next = BYTE_GET (evn.vn_next);
9548
9549                               a_off = offset + ivn.vn_aux;
9550
9551                               do
9552                                 {
9553                                   if (get_data (&evna, file, a_off, sizeof (evna),
9554                                                 1, _("version need aux (2)")) == NULL)
9555                                     {
9556                                       ivna.vna_next  = 0;
9557                                       ivna.vna_other = 0;
9558                                     }
9559                                   else
9560                                     {
9561                                       ivna.vna_next  = BYTE_GET (evna.vna_next);
9562                                       ivna.vna_other = BYTE_GET (evna.vna_other);
9563                                     }
9564
9565                                   a_off += ivna.vna_next;
9566                                 }
9567                               while (ivna.vna_other != data[cnt + j]
9568                                      && ivna.vna_next != 0);
9569
9570                               if (ivna.vna_other == data[cnt + j])
9571                                 {
9572                                   ivna.vna_name = BYTE_GET (evna.vna_name);
9573
9574                                   if (ivna.vna_name >= string_sec->sh_size)
9575                                     name = _("*invalid*");
9576                                   else
9577                                     name = strtab + ivna.vna_name;
9578                                   nn += printf ("(%s%-*s",
9579                                                 name,
9580                                                 12 - (int) strlen (name),
9581                                                 ")");
9582                                   check_def = 0;
9583                                   break;
9584                                 }
9585
9586                               offset += ivn.vn_next;
9587                             }
9588                           while (ivn.vn_next);
9589                         }
9590
9591                       if (check_def && data[cnt + j] != 0x8001
9592                           && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
9593                         {
9594                           Elf_Internal_Verdef ivd;
9595                           Elf_External_Verdef evd;
9596                           unsigned long offset;
9597
9598                           offset = offset_from_vma
9599                             (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
9600                              sizeof evd);
9601
9602                           do
9603                             {
9604                               if (get_data (&evd, file, offset, sizeof (evd), 1,
9605                                             _("version def")) == NULL)
9606                                 {
9607                                   ivd.vd_next = 0;
9608                                   /* PR 17531: file: 046-1082287-0.004.  */
9609                                   ivd.vd_ndx  = (data[cnt + j] & VERSYM_VERSION) + 1;
9610                                   break;
9611                                 }
9612                               else
9613                                 {
9614                                   ivd.vd_next = BYTE_GET (evd.vd_next);
9615                                   ivd.vd_ndx  = BYTE_GET (evd.vd_ndx);
9616                                 }
9617
9618                               offset += ivd.vd_next;
9619                             }
9620                           while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
9621                                  && ivd.vd_next != 0);
9622
9623                           if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
9624                             {
9625                               Elf_External_Verdaux evda;
9626                               Elf_Internal_Verdaux ivda;
9627
9628                               ivd.vd_aux = BYTE_GET (evd.vd_aux);
9629
9630                               if (get_data (&evda, file,
9631                                             offset - ivd.vd_next + ivd.vd_aux,
9632                                             sizeof (evda), 1,
9633                                             _("version def aux")) == NULL)
9634                                 break;
9635
9636                               ivda.vda_name = BYTE_GET (evda.vda_name);
9637
9638                               if (ivda.vda_name >= string_sec->sh_size)
9639                                 name = _("*invalid*");
9640                               else
9641                                 name = strtab + ivda.vda_name;
9642                               nn += printf ("(%s%-*s",
9643                                             name,
9644                                             12 - (int) strlen (name),
9645                                             ")");
9646                             }
9647                         }
9648
9649                       if (nn < 18)
9650                         printf ("%*c", 18 - nn, ' ');
9651                     }
9652
9653                 putchar ('\n');
9654               }
9655
9656             free (data);
9657             free (strtab);
9658             free (symbols);
9659           }
9660           break;
9661
9662         default:
9663           break;
9664         }
9665     }
9666
9667   if (! found)
9668     printf (_("\nNo version information found in this file.\n"));
9669
9670   return 1;
9671 }
9672
9673 static const char *
9674 get_symbol_binding (unsigned int binding)
9675 {
9676   static char buff[32];
9677
9678   switch (binding)
9679     {
9680     case STB_LOCAL:     return "LOCAL";
9681     case STB_GLOBAL:    return "GLOBAL";
9682     case STB_WEAK:      return "WEAK";
9683     default:
9684       if (binding >= STB_LOPROC && binding <= STB_HIPROC)
9685         snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
9686                   binding);
9687       else if (binding >= STB_LOOS && binding <= STB_HIOS)
9688         {
9689           if (binding == STB_GNU_UNIQUE
9690               && (elf_header.e_ident[EI_OSABI] == ELFOSABI_GNU
9691                   /* GNU is still using the default value 0.  */
9692                   || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
9693             return "UNIQUE";
9694           snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
9695         }
9696       else
9697         snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
9698       return buff;
9699     }
9700 }
9701
9702 static const char *
9703 get_symbol_type (unsigned int type)
9704 {
9705   static char buff[32];
9706
9707   switch (type)
9708     {
9709     case STT_NOTYPE:    return "NOTYPE";
9710     case STT_OBJECT:    return "OBJECT";
9711     case STT_FUNC:      return "FUNC";
9712     case STT_SECTION:   return "SECTION";
9713     case STT_FILE:      return "FILE";
9714     case STT_COMMON:    return "COMMON";
9715     case STT_TLS:       return "TLS";
9716     case STT_RELC:      return "RELC";
9717     case STT_SRELC:     return "SRELC";
9718     default:
9719       if (type >= STT_LOPROC && type <= STT_HIPROC)
9720         {
9721           if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
9722             return "THUMB_FUNC";
9723
9724           if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
9725             return "REGISTER";
9726
9727           if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
9728             return "PARISC_MILLI";
9729
9730           snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
9731         }
9732       else if (type >= STT_LOOS && type <= STT_HIOS)
9733         {
9734           if (elf_header.e_machine == EM_PARISC)
9735             {
9736               if (type == STT_HP_OPAQUE)
9737                 return "HP_OPAQUE";
9738               if (type == STT_HP_STUB)
9739                 return "HP_STUB";
9740             }
9741
9742           if (type == STT_GNU_IFUNC
9743               && (elf_header.e_ident[EI_OSABI] == ELFOSABI_GNU
9744                   || elf_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD
9745                   /* GNU is still using the default value 0.  */
9746                   || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
9747             return "IFUNC";
9748
9749           snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
9750         }
9751       else
9752         snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
9753       return buff;
9754     }
9755 }
9756
9757 static const char *
9758 get_symbol_visibility (unsigned int visibility)
9759 {
9760   switch (visibility)
9761     {
9762     case STV_DEFAULT:   return "DEFAULT";
9763     case STV_INTERNAL:  return "INTERNAL";
9764     case STV_HIDDEN:    return "HIDDEN";
9765     case STV_PROTECTED: return "PROTECTED";
9766     default:
9767       error (_("Unrecognized visibility value: %u"), visibility);
9768       return _("<unknown>");
9769     }
9770 }
9771
9772 static const char *
9773 get_mips_symbol_other (unsigned int other)
9774 {
9775   switch (other)
9776     {
9777     case STO_OPTIONAL:
9778       return "OPTIONAL";
9779     case STO_MIPS_PLT:
9780       return "MIPS PLT";
9781     case STO_MIPS_PIC:
9782       return "MIPS PIC";
9783     case STO_MICROMIPS:
9784       return "MICROMIPS";
9785     case STO_MICROMIPS | STO_MIPS_PIC:
9786       return "MICROMIPS, MIPS PIC";
9787     case STO_MIPS16:
9788       return "MIPS16";
9789     default:
9790       return NULL;
9791     }
9792 }
9793
9794 static const char *
9795 get_ia64_symbol_other (unsigned int other)
9796 {
9797   if (is_ia64_vms ())
9798     {
9799       static char res[32];
9800
9801       res[0] = 0;
9802
9803       /* Function types is for images and .STB files only.  */
9804       switch (elf_header.e_type)
9805         {
9806         case ET_DYN:
9807         case ET_EXEC:
9808           switch (VMS_ST_FUNC_TYPE (other))
9809             {
9810             case VMS_SFT_CODE_ADDR:
9811               strcat (res, " CA");
9812               break;
9813             case VMS_SFT_SYMV_IDX:
9814               strcat (res, " VEC");
9815               break;
9816             case VMS_SFT_FD:
9817               strcat (res, " FD");
9818               break;
9819             case VMS_SFT_RESERVE:
9820               strcat (res, " RSV");
9821               break;
9822             default:
9823               warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
9824                     VMS_ST_FUNC_TYPE (other));
9825               strcat (res, " <unknown>");
9826               break;
9827             }
9828           break;
9829         default:
9830           break;
9831         }
9832       switch (VMS_ST_LINKAGE (other))
9833         {
9834         case VMS_STL_IGNORE:
9835           strcat (res, " IGN");
9836           break;
9837         case VMS_STL_RESERVE:
9838           strcat (res, " RSV");
9839           break;
9840         case VMS_STL_STD:
9841           strcat (res, " STD");
9842           break;
9843         case VMS_STL_LNK:
9844           strcat (res, " LNK");
9845           break;
9846         default:
9847           warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
9848                 VMS_ST_LINKAGE (other));
9849           strcat (res, " <unknown>");
9850           break;
9851         }
9852
9853       if (res[0] != 0)
9854         return res + 1;
9855       else
9856         return res;
9857     }
9858   return NULL;
9859 }
9860
9861 static const char *
9862 get_ppc64_symbol_other (unsigned int other)
9863 {
9864   if (PPC64_LOCAL_ENTRY_OFFSET (other) != 0)
9865     {
9866       static char buf[32];
9867       snprintf (buf, sizeof buf, _("<localentry>: %d"),
9868                 PPC64_LOCAL_ENTRY_OFFSET (other));
9869       return buf;
9870     }
9871   return NULL;
9872 }
9873
9874 static const char *
9875 get_symbol_other (unsigned int other)
9876 {
9877   const char * result = NULL;
9878   static char buff [32];
9879
9880   if (other == 0)
9881     return "";
9882
9883   switch (elf_header.e_machine)
9884     {
9885     case EM_MIPS:
9886       result = get_mips_symbol_other (other);
9887       break;
9888     case EM_IA_64:
9889       result = get_ia64_symbol_other (other);
9890       break;
9891     case EM_PPC64:
9892       result = get_ppc64_symbol_other (other);
9893       break;
9894     default:
9895       break;
9896     }
9897
9898   if (result)
9899     return result;
9900
9901   snprintf (buff, sizeof buff, _("<other>: %x"), other);
9902   return buff;
9903 }
9904
9905 static const char *
9906 get_symbol_index_type (unsigned int type)
9907 {
9908   static char buff[32];
9909
9910   switch (type)
9911     {
9912     case SHN_UNDEF:     return "UND";
9913     case SHN_ABS:       return "ABS";
9914     case SHN_COMMON:    return "COM";
9915     default:
9916       if (type == SHN_IA_64_ANSI_COMMON
9917           && elf_header.e_machine == EM_IA_64
9918           && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
9919         return "ANSI_COM";
9920       else if ((elf_header.e_machine == EM_X86_64
9921                 || elf_header.e_machine == EM_L1OM
9922                 || elf_header.e_machine == EM_K1OM)
9923                && type == SHN_X86_64_LCOMMON)
9924         return "LARGE_COM";
9925       else if ((type == SHN_MIPS_SCOMMON
9926                 && elf_header.e_machine == EM_MIPS)
9927                || (type == SHN_TIC6X_SCOMMON
9928                    && elf_header.e_machine == EM_TI_C6000))
9929         return "SCOM";
9930       else if (type == SHN_MIPS_SUNDEFINED
9931                && elf_header.e_machine == EM_MIPS)
9932         return "SUND";
9933       else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
9934         sprintf (buff, "PRC[0x%04x]", type & 0xffff);
9935       else if (type >= SHN_LOOS && type <= SHN_HIOS)
9936         sprintf (buff, "OS [0x%04x]", type & 0xffff);
9937       else if (type >= SHN_LORESERVE)
9938         sprintf (buff, "RSV[0x%04x]", type & 0xffff);
9939       else if (type >= elf_header.e_shnum)
9940         sprintf (buff, _("bad section index[%3d]"), type);
9941       else
9942         sprintf (buff, "%3d", type);
9943       break;
9944     }
9945
9946   return buff;
9947 }
9948
9949 static bfd_vma *
9950 get_dynamic_data (FILE * file, bfd_size_type number, unsigned int ent_size)
9951 {
9952   unsigned char * e_data;
9953   bfd_vma * i_data;
9954
9955   /* If the size_t type is smaller than the bfd_size_type, eg because
9956      you are building a 32-bit tool on a 64-bit host, then make sure
9957      that when (number) is cast to (size_t) no information is lost.  */
9958   if (sizeof (size_t) < sizeof (bfd_size_type)
9959       && (bfd_size_type) ((size_t) number) != number)
9960     {
9961       error (_("Size truncation prevents reading %llu elements of size %u\n"),
9962              (unsigned long long) number, ent_size);
9963       return NULL;
9964     }
9965
9966   /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
9967      attempting to allocate memory when the read is bound to fail.  */
9968   if (ent_size * number > current_file_size)
9969     {
9970       error (_("Invalid number of dynamic entries: %llu\n"),
9971              (unsigned long long) number);
9972       return NULL;
9973     }
9974
9975   e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
9976   if (e_data == NULL)
9977     {
9978       error (_("Out of memory reading %llu dynamic entries\n"),
9979              (unsigned long long) number);
9980       return NULL;
9981     }
9982
9983   if (fread (e_data, ent_size, (size_t) number, file) != number)
9984     {
9985       error (_("Unable to read in %llu bytes of dynamic data\n"),
9986              (unsigned long long) (number * ent_size));
9987       free (e_data);
9988       return NULL;
9989     }
9990
9991   i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
9992   if (i_data == NULL)
9993     {
9994       error (_("Out of memory allocating space for %llu dynamic entries\n"),
9995              (unsigned long long) number);
9996       free (e_data);
9997       return NULL;
9998     }
9999
10000   while (number--)
10001     i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10002
10003   free (e_data);
10004
10005   return i_data;
10006 }
10007
10008 static void
10009 print_dynamic_symbol (bfd_vma si, unsigned long hn)
10010 {
10011   Elf_Internal_Sym * psym;
10012   int n;
10013
10014   n = print_vma (si, DEC_5);
10015   if (n < 5)
10016     fputs (&"     "[n], stdout);
10017   printf (" %3lu: ", hn);
10018
10019   if (dynamic_symbols == NULL || si >= num_dynamic_syms)
10020     {
10021       printf (_("<No info available for dynamic symbol number %lu>\n"),
10022               (unsigned long) si);
10023       return;
10024     }
10025
10026   psym = dynamic_symbols + si;
10027   print_vma (psym->st_value, LONG_HEX);
10028   putchar (' ');
10029   print_vma (psym->st_size, DEC_5);
10030
10031   printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
10032   printf (" %-6s",  get_symbol_binding (ELF_ST_BIND (psym->st_info)));
10033   printf (" %-7s",  get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
10034   /* Check to see if any other bits in the st_other field are set.
10035      Note - displaying this information disrupts the layout of the
10036      table being generated, but for the moment this case is very
10037      rare.  */
10038   if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
10039     printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
10040   printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
10041   if (VALID_DYNAMIC_NAME (psym->st_name))
10042     print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
10043   else
10044     printf (_(" <corrupt: %14ld>"), psym->st_name);
10045   putchar ('\n');
10046 }
10047
10048 /* Dump the symbol table.  */
10049 static int
10050 process_symbol_table (FILE * file)
10051 {
10052   Elf_Internal_Shdr * section;
10053   bfd_size_type nbuckets = 0;
10054   bfd_size_type nchains = 0;
10055   bfd_vma * buckets = NULL;
10056   bfd_vma * chains = NULL;
10057   bfd_vma ngnubuckets = 0;
10058   bfd_vma * gnubuckets = NULL;
10059   bfd_vma * gnuchains = NULL;
10060   bfd_vma gnusymidx = 0;
10061   bfd_size_type ngnuchains = 0;
10062
10063   if (!do_syms && !do_dyn_syms && !do_histogram)
10064     return 1;
10065
10066   if (dynamic_info[DT_HASH]
10067       && (do_histogram
10068           || (do_using_dynamic
10069               && !do_dyn_syms
10070               && dynamic_strings != NULL)))
10071     {
10072       unsigned char nb[8];
10073       unsigned char nc[8];
10074       unsigned int hash_ent_size = 4;
10075
10076       if ((elf_header.e_machine == EM_ALPHA
10077            || elf_header.e_machine == EM_S390
10078            || elf_header.e_machine == EM_S390_OLD)
10079           && elf_header.e_ident[EI_CLASS] == ELFCLASS64)
10080         hash_ent_size = 8;
10081
10082       if (fseek (file,
10083                  (archive_file_offset
10084                   + offset_from_vma (file, dynamic_info[DT_HASH],
10085                                      sizeof nb + sizeof nc)),
10086                  SEEK_SET))
10087         {
10088           error (_("Unable to seek to start of dynamic information\n"));
10089           goto no_hash;
10090         }
10091
10092       if (fread (nb, hash_ent_size, 1, file) != 1)
10093         {
10094           error (_("Failed to read in number of buckets\n"));
10095           goto no_hash;
10096         }
10097
10098       if (fread (nc, hash_ent_size, 1, file) != 1)
10099         {
10100           error (_("Failed to read in number of chains\n"));
10101           goto no_hash;
10102         }
10103
10104       nbuckets = byte_get (nb, hash_ent_size);
10105       nchains  = byte_get (nc, hash_ent_size);
10106
10107       buckets = get_dynamic_data (file, nbuckets, hash_ent_size);
10108       chains  = get_dynamic_data (file, nchains, hash_ent_size);
10109
10110     no_hash:
10111       if (buckets == NULL || chains == NULL)
10112         {
10113           if (do_using_dynamic)
10114             return 0;
10115           free (buckets);
10116           free (chains);
10117           buckets = NULL;
10118           chains = NULL;
10119           nbuckets = 0;
10120           nchains = 0;
10121         }
10122     }
10123
10124   if (dynamic_info_DT_GNU_HASH
10125       && (do_histogram
10126           || (do_using_dynamic
10127               && !do_dyn_syms
10128               && dynamic_strings != NULL)))
10129     {
10130       unsigned char nb[16];
10131       bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10132       bfd_vma buckets_vma;
10133
10134       if (fseek (file,
10135                  (archive_file_offset
10136                   + offset_from_vma (file, dynamic_info_DT_GNU_HASH,
10137                                      sizeof nb)),
10138                  SEEK_SET))
10139         {
10140           error (_("Unable to seek to start of dynamic information\n"));
10141           goto no_gnu_hash;
10142         }
10143
10144       if (fread (nb, 16, 1, file) != 1)
10145         {
10146           error (_("Failed to read in number of buckets\n"));
10147           goto no_gnu_hash;
10148         }
10149
10150       ngnubuckets = byte_get (nb, 4);
10151       gnusymidx = byte_get (nb + 4, 4);
10152       bitmaskwords = byte_get (nb + 8, 4);
10153       buckets_vma = dynamic_info_DT_GNU_HASH + 16;
10154       if (is_32bit_elf)
10155         buckets_vma += bitmaskwords * 4;
10156       else
10157         buckets_vma += bitmaskwords * 8;
10158
10159       if (fseek (file,
10160                  (archive_file_offset
10161                   + offset_from_vma (file, buckets_vma, 4)),
10162                  SEEK_SET))
10163         {
10164           error (_("Unable to seek to start of dynamic information\n"));
10165           goto no_gnu_hash;
10166         }
10167
10168       gnubuckets = get_dynamic_data (file, ngnubuckets, 4);
10169
10170       if (gnubuckets == NULL)
10171         goto no_gnu_hash;
10172
10173       for (i = 0; i < ngnubuckets; i++)
10174         if (gnubuckets[i] != 0)
10175           {
10176             if (gnubuckets[i] < gnusymidx)
10177               return 0;
10178
10179             if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
10180               maxchain = gnubuckets[i];
10181           }
10182
10183       if (maxchain == 0xffffffff)
10184         goto no_gnu_hash;
10185
10186       maxchain -= gnusymidx;
10187
10188       if (fseek (file,
10189                  (archive_file_offset
10190                   + offset_from_vma (file, buckets_vma
10191                                            + 4 * (ngnubuckets + maxchain), 4)),
10192                  SEEK_SET))
10193         {
10194           error (_("Unable to seek to start of dynamic information\n"));
10195           goto no_gnu_hash;
10196         }
10197
10198       do
10199         {
10200           if (fread (nb, 4, 1, file) != 1)
10201             {
10202               error (_("Failed to determine last chain length\n"));
10203               goto no_gnu_hash;
10204             }
10205
10206           if (maxchain + 1 == 0)
10207             goto no_gnu_hash;
10208
10209           ++maxchain;
10210         }
10211       while ((byte_get (nb, 4) & 1) == 0);
10212
10213       if (fseek (file,
10214                  (archive_file_offset
10215                   + offset_from_vma (file, buckets_vma + 4 * ngnubuckets, 4)),
10216                  SEEK_SET))
10217         {
10218           error (_("Unable to seek to start of dynamic information\n"));
10219           goto no_gnu_hash;
10220         }
10221
10222       gnuchains = get_dynamic_data (file, maxchain, 4);
10223       ngnuchains = maxchain;
10224
10225     no_gnu_hash:
10226       if (gnuchains == NULL)
10227         {
10228           free (gnubuckets);
10229           gnubuckets = NULL;
10230           ngnubuckets = 0;
10231           if (do_using_dynamic)
10232             return 0;
10233         }
10234     }
10235
10236   if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
10237       && do_syms
10238       && do_using_dynamic
10239       && dynamic_strings != NULL
10240       && dynamic_symbols != NULL)
10241     {
10242       unsigned long hn;
10243
10244       if (dynamic_info[DT_HASH])
10245         {
10246           bfd_vma si;
10247
10248           printf (_("\nSymbol table for image:\n"));
10249           if (is_32bit_elf)
10250             printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
10251           else
10252             printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
10253
10254           for (hn = 0; hn < nbuckets; hn++)
10255             {
10256               if (! buckets[hn])
10257                 continue;
10258
10259               for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
10260                 print_dynamic_symbol (si, hn);
10261             }
10262         }
10263
10264       if (dynamic_info_DT_GNU_HASH)
10265         {
10266           printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
10267           if (is_32bit_elf)
10268             printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
10269           else
10270             printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
10271
10272           for (hn = 0; hn < ngnubuckets; ++hn)
10273             if (gnubuckets[hn] != 0)
10274               {
10275                 bfd_vma si = gnubuckets[hn];
10276                 bfd_vma off = si - gnusymidx;
10277
10278                 do
10279                   {
10280                     print_dynamic_symbol (si, hn);
10281                     si++;
10282                   }
10283                 while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
10284               }
10285         }
10286     }
10287   else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
10288            && section_headers != NULL)
10289     {
10290       unsigned int i;
10291
10292       for (i = 0, section = section_headers;
10293            i < elf_header.e_shnum;
10294            i++, section++)
10295         {
10296           unsigned int si;
10297           char * strtab = NULL;
10298           unsigned long int strtab_size = 0;
10299           Elf_Internal_Sym * symtab;
10300           Elf_Internal_Sym * psym;
10301           unsigned long num_syms;
10302
10303           if ((section->sh_type != SHT_SYMTAB
10304                && section->sh_type != SHT_DYNSYM)
10305               || (!do_syms
10306                   && section->sh_type == SHT_SYMTAB))
10307             continue;
10308
10309           if (section->sh_entsize == 0)
10310             {
10311               printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
10312                       printable_section_name (section));
10313               continue;
10314             }
10315
10316           printf (_("\nSymbol table '%s' contains %lu entries:\n"),
10317                   printable_section_name (section),
10318                   (unsigned long) (section->sh_size / section->sh_entsize));
10319
10320           if (is_32bit_elf)
10321             printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
10322           else
10323             printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
10324
10325           symtab = GET_ELF_SYMBOLS (file, section, & num_syms);
10326           if (symtab == NULL)
10327             continue;
10328
10329           if (section->sh_link == elf_header.e_shstrndx)
10330             {
10331               strtab = string_table;
10332               strtab_size = string_table_length;
10333             }
10334           else if (section->sh_link < elf_header.e_shnum)
10335             {
10336               Elf_Internal_Shdr * string_sec;
10337
10338               string_sec = section_headers + section->sh_link;
10339
10340               strtab = (char *) get_data (NULL, file, string_sec->sh_offset,
10341                                           1, string_sec->sh_size,
10342                                           _("string table"));
10343               strtab_size = strtab != NULL ? string_sec->sh_size : 0;
10344             }
10345
10346           for (si = 0, psym = symtab; si < num_syms; si++, psym++)
10347             {
10348               printf ("%6d: ", si);
10349               print_vma (psym->st_value, LONG_HEX);
10350               putchar (' ');
10351               print_vma (psym->st_size, DEC_5);
10352               printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
10353               printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
10354               printf (" %-7s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
10355               /* Check to see if any other bits in the st_other field are set.
10356                  Note - displaying this information disrupts the layout of the
10357                  table being generated, but for the moment this case is very rare.  */
10358               if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
10359                 printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
10360               printf (" %4s ", get_symbol_index_type (psym->st_shndx));
10361               print_symbol (25, psym->st_name < strtab_size
10362                             ? strtab + psym->st_name : _("<corrupt>"));
10363
10364               if (section->sh_type == SHT_DYNSYM
10365                   && version_info[DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
10366                 {
10367                   unsigned char data[2];
10368                   unsigned short vers_data;
10369                   unsigned long offset;
10370                   int is_nobits;
10371                   int check_def;
10372
10373                   offset = offset_from_vma
10374                     (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
10375                      sizeof data + si * sizeof (vers_data));
10376
10377                   if (get_data (&data, file, offset + si * sizeof (vers_data),
10378                                 sizeof (data), 1, _("version data")) == NULL)
10379                     break;
10380
10381                   vers_data = byte_get (data, 2);
10382
10383                   is_nobits = (section_headers != NULL
10384                                && psym->st_shndx < elf_header.e_shnum
10385                                && section_headers[psym->st_shndx].sh_type
10386                                   == SHT_NOBITS);
10387
10388                   check_def = (psym->st_shndx != SHN_UNDEF);
10389
10390                   if ((vers_data & VERSYM_HIDDEN) || vers_data > 1)
10391                     {
10392                       if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
10393                           && (is_nobits || ! check_def))
10394                         {
10395                           Elf_External_Verneed evn;
10396                           Elf_Internal_Verneed ivn;
10397                           Elf_Internal_Vernaux ivna;
10398
10399                           /* We must test both.  */
10400                           offset = offset_from_vma
10401                             (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
10402                              sizeof evn);
10403
10404                           do
10405                             {
10406                               unsigned long vna_off;
10407
10408                               if (get_data (&evn, file, offset, sizeof (evn), 1,
10409                                             _("version need")) == NULL)
10410                                 {
10411                                   ivna.vna_next = 0;
10412                                   ivna.vna_other = 0;
10413                                   ivna.vna_name = 0;
10414                                   break;
10415                                 }
10416
10417                               ivn.vn_aux  = BYTE_GET (evn.vn_aux);
10418                               ivn.vn_next = BYTE_GET (evn.vn_next);
10419
10420                               vna_off = offset + ivn.vn_aux;
10421
10422                               do
10423                                 {
10424                                   Elf_External_Vernaux evna;
10425
10426                                   if (get_data (&evna, file, vna_off,
10427                                                 sizeof (evna), 1,
10428                                                 _("version need aux (3)")) == NULL)
10429                                     {
10430                                       ivna.vna_next = 0;
10431                                       ivna.vna_other = 0;
10432                                       ivna.vna_name = 0;
10433                                     }
10434                                   else
10435                                     {
10436                                       ivna.vna_other = BYTE_GET (evna.vna_other);
10437                                       ivna.vna_next  = BYTE_GET (evna.vna_next);
10438                                       ivna.vna_name  = BYTE_GET (evna.vna_name);
10439                                     }
10440
10441                                   vna_off += ivna.vna_next;
10442                                 }
10443                               while (ivna.vna_other != vers_data
10444                                      && ivna.vna_next != 0);
10445
10446                               if (ivna.vna_other == vers_data)
10447                                 break;
10448
10449                               offset += ivn.vn_next;
10450                             }
10451                           while (ivn.vn_next != 0);
10452
10453                           if (ivna.vna_other == vers_data)
10454                             {
10455                               printf ("@%s (%d)",
10456                                       ivna.vna_name < strtab_size
10457                                       ? strtab + ivna.vna_name : _("<corrupt>"),
10458                                       ivna.vna_other);
10459                               check_def = 0;
10460                             }
10461                           else if (! is_nobits)
10462                             error (_("bad dynamic symbol\n"));
10463                           else
10464                             check_def = 1;
10465                         }
10466
10467                       if (check_def)
10468                         {
10469                           if (vers_data != 0x8001
10470                               && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
10471                             {
10472                               Elf_Internal_Verdef ivd;
10473                               Elf_Internal_Verdaux ivda;
10474                               Elf_External_Verdaux evda;
10475                               unsigned long off;
10476
10477                               off = offset_from_vma
10478                                 (file,
10479                                  version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
10480                                  sizeof (Elf_External_Verdef));
10481
10482                               do
10483                                 {
10484                                   Elf_External_Verdef evd;
10485
10486                                   if (get_data (&evd, file, off, sizeof (evd),
10487                                                 1, _("version def")) == NULL)
10488                                     {
10489                                       ivd.vd_ndx = 0;
10490                                       ivd.vd_aux = 0;
10491                                       ivd.vd_next = 0;
10492                                     }
10493                                   else
10494                                     {
10495                                       ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
10496                                       ivd.vd_aux = BYTE_GET (evd.vd_aux);
10497                                       ivd.vd_next = BYTE_GET (evd.vd_next);
10498                                     }
10499
10500                                   off += ivd.vd_next;
10501                                 }
10502                               while (ivd.vd_ndx != (vers_data & VERSYM_VERSION)
10503                                      && ivd.vd_next != 0);
10504
10505                               off -= ivd.vd_next;
10506                               off += ivd.vd_aux;
10507
10508                               if (get_data (&evda, file, off, sizeof (evda),
10509                                             1, _("version def aux")) == NULL)
10510                                 break;
10511
10512                               ivda.vda_name = BYTE_GET (evda.vda_name);
10513
10514                               if (psym->st_name != ivda.vda_name)
10515                                 printf ((vers_data & VERSYM_HIDDEN)
10516                                         ? "@%s" : "@@%s",
10517                                         ivda.vda_name < strtab_size
10518                                         ? strtab + ivda.vda_name : _("<corrupt>"));
10519                             }
10520                         }
10521                     }
10522                 }
10523
10524               putchar ('\n');
10525             }
10526
10527           free (symtab);
10528           if (strtab != string_table)
10529             free (strtab);
10530         }
10531     }
10532   else if (do_syms)
10533     printf
10534       (_("\nDynamic symbol information is not available for displaying symbols.\n"));
10535
10536   if (do_histogram && buckets != NULL)
10537     {
10538       unsigned long * lengths;
10539       unsigned long * counts;
10540       unsigned long hn;
10541       bfd_vma si;
10542       unsigned long maxlength = 0;
10543       unsigned long nzero_counts = 0;
10544       unsigned long nsyms = 0;
10545       unsigned long chained;
10546
10547       printf (_("\nHistogram for bucket list length (total of %lu buckets):\n"),
10548               (unsigned long) nbuckets);
10549
10550       lengths = (unsigned long *) calloc (nbuckets, sizeof (*lengths));
10551       if (lengths == NULL)
10552         {
10553           error (_("Out of memory allocating space for histogram buckets\n"));
10554           return 0;
10555         }
10556
10557       printf (_(" Length  Number     %% of total  Coverage\n"));
10558       for (hn = 0; hn < nbuckets; ++hn)
10559         {
10560           for (si = buckets[hn], chained = 0;
10561                si > 0 && si < nchains && si < nbuckets && chained <= nchains;
10562                si = chains[si], ++chained)
10563             {
10564               ++nsyms;
10565               if (maxlength < ++lengths[hn])
10566                 ++maxlength;
10567             }
10568
10569           /* PR binutils/17531: A corrupt binary could contain broken
10570              histogram data.  Do not go into an infinite loop trying
10571              to process it.  */
10572           if (chained > nchains)
10573             {
10574               error (_("histogram chain is corrupt\n"));
10575               break;
10576             }
10577         }
10578
10579       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
10580       if (counts == NULL)
10581         {
10582           free (lengths);
10583           error (_("Out of memory allocating space for histogram counts\n"));
10584           return 0;
10585         }
10586
10587       for (hn = 0; hn < nbuckets; ++hn)
10588         ++counts[lengths[hn]];
10589
10590       if (nbuckets > 0)
10591         {
10592           unsigned long i;
10593           printf ("      0  %-10lu (%5.1f%%)\n",
10594                   counts[0], (counts[0] * 100.0) / nbuckets);
10595           for (i = 1; i <= maxlength; ++i)
10596             {
10597               nzero_counts += counts[i] * i;
10598               printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
10599                       i, counts[i], (counts[i] * 100.0) / nbuckets,
10600                       (nzero_counts * 100.0) / nsyms);
10601             }
10602         }
10603
10604       free (counts);
10605       free (lengths);
10606     }
10607
10608   if (buckets != NULL)
10609     {
10610       free (buckets);
10611       free (chains);
10612     }
10613
10614   if (do_histogram && gnubuckets != NULL)
10615     {
10616       unsigned long * lengths;
10617       unsigned long * counts;
10618       unsigned long hn;
10619       unsigned long maxlength = 0;
10620       unsigned long nzero_counts = 0;
10621       unsigned long nsyms = 0;
10622
10623       printf (_("\nHistogram for `.gnu.hash' bucket list length (total of %lu buckets):\n"),
10624               (unsigned long) ngnubuckets);
10625
10626       lengths = (unsigned long *) calloc (ngnubuckets, sizeof (*lengths));
10627       if (lengths == NULL)
10628         {
10629           error (_("Out of memory allocating space for gnu histogram buckets\n"));
10630           return 0;
10631         }
10632
10633       printf (_(" Length  Number     %% of total  Coverage\n"));
10634
10635       for (hn = 0; hn < ngnubuckets; ++hn)
10636         if (gnubuckets[hn] != 0)
10637           {
10638             bfd_vma off, length = 1;
10639
10640             for (off = gnubuckets[hn] - gnusymidx;
10641                  /* PR 17531 file: 010-77222-0.004.  */
10642                  off < ngnuchains && (gnuchains[off] & 1) == 0;
10643                  ++off)
10644               ++length;
10645             lengths[hn] = length;
10646             if (length > maxlength)
10647               maxlength = length;
10648             nsyms += length;
10649           }
10650
10651       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
10652       if (counts == NULL)
10653         {
10654           free (lengths);
10655           error (_("Out of memory allocating space for gnu histogram counts\n"));
10656           return 0;
10657         }
10658
10659       for (hn = 0; hn < ngnubuckets; ++hn)
10660         ++counts[lengths[hn]];
10661
10662       if (ngnubuckets > 0)
10663         {
10664           unsigned long j;
10665           printf ("      0  %-10lu (%5.1f%%)\n",
10666                   counts[0], (counts[0] * 100.0) / ngnubuckets);
10667           for (j = 1; j <= maxlength; ++j)
10668             {
10669               nzero_counts += counts[j] * j;
10670               printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
10671                       j, counts[j], (counts[j] * 100.0) / ngnubuckets,
10672                       (nzero_counts * 100.0) / nsyms);
10673             }
10674         }
10675
10676       free (counts);
10677       free (lengths);
10678       free (gnubuckets);
10679       free (gnuchains);
10680     }
10681
10682   return 1;
10683 }
10684
10685 static int
10686 process_syminfo (FILE * file ATTRIBUTE_UNUSED)
10687 {
10688   unsigned int i;
10689
10690   if (dynamic_syminfo == NULL
10691       || !do_dynamic)
10692     /* No syminfo, this is ok.  */
10693     return 1;
10694
10695   /* There better should be a dynamic symbol section.  */
10696   if (dynamic_symbols == NULL || dynamic_strings == NULL)
10697     return 0;
10698
10699   if (dynamic_addr)
10700     printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
10701             dynamic_syminfo_offset, dynamic_syminfo_nent);
10702
10703   printf (_(" Num: Name                           BoundTo     Flags\n"));
10704   for (i = 0; i < dynamic_syminfo_nent; ++i)
10705     {
10706       unsigned short int flags = dynamic_syminfo[i].si_flags;
10707
10708       printf ("%4d: ", i);
10709       if (i >= num_dynamic_syms)
10710         printf (_("<corrupt index>"));
10711       else if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
10712         print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
10713       else
10714         printf (_("<corrupt: %19ld>"), dynamic_symbols[i].st_name);
10715       putchar (' ');
10716
10717       switch (dynamic_syminfo[i].si_boundto)
10718         {
10719         case SYMINFO_BT_SELF:
10720           fputs ("SELF       ", stdout);
10721           break;
10722         case SYMINFO_BT_PARENT:
10723           fputs ("PARENT     ", stdout);
10724           break;
10725         default:
10726           if (dynamic_syminfo[i].si_boundto > 0
10727               && dynamic_syminfo[i].si_boundto < dynamic_nent
10728               && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
10729             {
10730               print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
10731               putchar (' ' );
10732             }
10733           else
10734             printf ("%-10d ", dynamic_syminfo[i].si_boundto);
10735           break;
10736         }
10737
10738       if (flags & SYMINFO_FLG_DIRECT)
10739         printf (" DIRECT");
10740       if (flags & SYMINFO_FLG_PASSTHRU)
10741         printf (" PASSTHRU");
10742       if (flags & SYMINFO_FLG_COPY)
10743         printf (" COPY");
10744       if (flags & SYMINFO_FLG_LAZYLOAD)
10745         printf (" LAZYLOAD");
10746
10747       puts ("");
10748     }
10749
10750   return 1;
10751 }
10752
10753 /* Check to see if the given reloc needs to be handled in a target specific
10754    manner.  If so then process the reloc and return TRUE otherwise return
10755    FALSE.  */
10756
10757 static bfd_boolean
10758 target_specific_reloc_handling (Elf_Internal_Rela * reloc,
10759                                 unsigned char *     start,
10760                                 Elf_Internal_Sym *  symtab)
10761 {
10762   unsigned int reloc_type = get_reloc_type (reloc->r_info);
10763
10764   switch (elf_header.e_machine)
10765     {
10766     case EM_MSP430:
10767     case EM_MSP430_OLD:
10768       {
10769         static Elf_Internal_Sym * saved_sym = NULL;
10770
10771         switch (reloc_type)
10772           {
10773           case 10: /* R_MSP430_SYM_DIFF */
10774             if (uses_msp430x_relocs ())
10775               break;
10776           case 21: /* R_MSP430X_SYM_DIFF */
10777             saved_sym = symtab + get_reloc_symindex (reloc->r_info);
10778             return TRUE;
10779
10780           case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
10781           case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
10782             goto handle_sym_diff;
10783
10784           case 5: /* R_MSP430_16_BYTE */
10785           case 9: /* R_MSP430_8 */
10786             if (uses_msp430x_relocs ())
10787               break;
10788             goto handle_sym_diff;
10789
10790           case 2: /* R_MSP430_ABS16 */
10791           case 15: /* R_MSP430X_ABS16 */
10792             if (! uses_msp430x_relocs ())
10793               break;
10794             goto handle_sym_diff;
10795
10796           handle_sym_diff:
10797             if (saved_sym != NULL)
10798               {
10799                 bfd_vma value;
10800
10801                 value = reloc->r_addend
10802                   + (symtab[get_reloc_symindex (reloc->r_info)].st_value
10803                      - saved_sym->st_value);
10804
10805                 byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
10806
10807                 saved_sym = NULL;
10808                 return TRUE;
10809               }
10810             break;
10811
10812           default:
10813             if (saved_sym != NULL)
10814               error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
10815             break;
10816           }
10817         break;
10818       }
10819
10820     case EM_MN10300:
10821     case EM_CYGNUS_MN10300:
10822       {
10823         static Elf_Internal_Sym * saved_sym = NULL;
10824
10825         switch (reloc_type)
10826           {
10827           case 34: /* R_MN10300_ALIGN */
10828             return TRUE;
10829           case 33: /* R_MN10300_SYM_DIFF */
10830             saved_sym = symtab + get_reloc_symindex (reloc->r_info);
10831             return TRUE;
10832           case 1: /* R_MN10300_32 */
10833           case 2: /* R_MN10300_16 */
10834             if (saved_sym != NULL)
10835               {
10836                 bfd_vma value;
10837
10838                 value = reloc->r_addend
10839                   + (symtab[get_reloc_symindex (reloc->r_info)].st_value
10840                      - saved_sym->st_value);
10841
10842                 byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
10843
10844                 saved_sym = NULL;
10845                 return TRUE;
10846               }
10847             break;
10848           default:
10849             if (saved_sym != NULL)
10850               error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
10851             break;
10852           }
10853         break;
10854       }
10855     }
10856
10857   return FALSE;
10858 }
10859
10860 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
10861    DWARF debug sections.  This is a target specific test.  Note - we do not
10862    go through the whole including-target-headers-multiple-times route, (as
10863    we have already done with <elf/h8.h>) because this would become very
10864    messy and even then this function would have to contain target specific
10865    information (the names of the relocs instead of their numeric values).
10866    FIXME: This is not the correct way to solve this problem.  The proper way
10867    is to have target specific reloc sizing and typing functions created by
10868    the reloc-macros.h header, in the same way that it already creates the
10869    reloc naming functions.  */
10870
10871 static bfd_boolean
10872 is_32bit_abs_reloc (unsigned int reloc_type)
10873 {
10874   switch (elf_header.e_machine)
10875     {
10876     case EM_386:
10877     case EM_486:
10878       return reloc_type == 1; /* R_386_32.  */
10879     case EM_68K:
10880       return reloc_type == 1; /* R_68K_32.  */
10881     case EM_860:
10882       return reloc_type == 1; /* R_860_32.  */
10883     case EM_960:
10884       return reloc_type == 2; /* R_960_32.  */
10885     case EM_AARCH64:
10886       return reloc_type == 258; /* R_AARCH64_ABS32 */
10887     case EM_ALPHA:
10888       return reloc_type == 1; /* R_ALPHA_REFLONG.  */
10889     case EM_ARC:
10890       return reloc_type == 1; /* R_ARC_32.  */
10891     case EM_ARM:
10892       return reloc_type == 2; /* R_ARM_ABS32 */
10893     case EM_AVR_OLD:
10894     case EM_AVR:
10895       return reloc_type == 1;
10896     case EM_ADAPTEVA_EPIPHANY:
10897       return reloc_type == 3;
10898     case EM_BLACKFIN:
10899       return reloc_type == 0x12; /* R_byte4_data.  */
10900     case EM_CRIS:
10901       return reloc_type == 3; /* R_CRIS_32.  */
10902     case EM_CR16:
10903       return reloc_type == 3; /* R_CR16_NUM32.  */
10904     case EM_CRX:
10905       return reloc_type == 15; /* R_CRX_NUM32.  */
10906     case EM_CYGNUS_FRV:
10907       return reloc_type == 1;
10908     case EM_CYGNUS_D10V:
10909     case EM_D10V:
10910       return reloc_type == 6; /* R_D10V_32.  */
10911     case EM_CYGNUS_D30V:
10912     case EM_D30V:
10913       return reloc_type == 12; /* R_D30V_32_NORMAL.  */
10914     case EM_DLX:
10915       return reloc_type == 3; /* R_DLX_RELOC_32.  */
10916     case EM_CYGNUS_FR30:
10917     case EM_FR30:
10918       return reloc_type == 3; /* R_FR30_32.  */
10919     case EM_H8S:
10920     case EM_H8_300:
10921     case EM_H8_300H:
10922       return reloc_type == 1; /* R_H8_DIR32.  */
10923     case EM_IA_64:
10924       return reloc_type == 0x65; /* R_IA64_SECREL32LSB.  */
10925     case EM_IP2K_OLD:
10926     case EM_IP2K:
10927       return reloc_type == 2; /* R_IP2K_32.  */
10928     case EM_IQ2000:
10929       return reloc_type == 2; /* R_IQ2000_32.  */
10930     case EM_LATTICEMICO32:
10931       return reloc_type == 3; /* R_LM32_32.  */
10932     case EM_M32C_OLD:
10933     case EM_M32C:
10934       return reloc_type == 3; /* R_M32C_32.  */
10935     case EM_M32R:
10936       return reloc_type == 34; /* R_M32R_32_RELA.  */
10937     case EM_MCORE:
10938       return reloc_type == 1; /* R_MCORE_ADDR32.  */
10939     case EM_CYGNUS_MEP:
10940       return reloc_type == 4; /* R_MEP_32.  */
10941     case EM_METAG:
10942       return reloc_type == 2; /* R_METAG_ADDR32.  */
10943     case EM_MICROBLAZE:
10944       return reloc_type == 1; /* R_MICROBLAZE_32.  */
10945     case EM_MIPS:
10946       return reloc_type == 2; /* R_MIPS_32.  */
10947     case EM_MMIX:
10948       return reloc_type == 4; /* R_MMIX_32.  */
10949     case EM_CYGNUS_MN10200:
10950     case EM_MN10200:
10951       return reloc_type == 1; /* R_MN10200_32.  */
10952     case EM_CYGNUS_MN10300:
10953     case EM_MN10300:
10954       return reloc_type == 1; /* R_MN10300_32.  */
10955     case EM_MOXIE:
10956       return reloc_type == 1; /* R_MOXIE_32.  */
10957     case EM_MSP430_OLD:
10958     case EM_MSP430:
10959       return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32.  */
10960     case EM_MT:
10961       return reloc_type == 2; /* R_MT_32.  */
10962     case EM_NDS32:
10963       return reloc_type == 20; /* R_NDS32_RELA.  */
10964     case EM_ALTERA_NIOS2:
10965       return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32.  */
10966     case EM_NIOS32:
10967       return reloc_type == 1; /* R_NIOS_32.  */
10968     case EM_OR1K:
10969       return reloc_type == 1; /* R_OR1K_32.  */
10970     case EM_PARISC:
10971       return (reloc_type == 1 /* R_PARISC_DIR32.  */
10972               || reloc_type == 41); /* R_PARISC_SECREL32.  */
10973     case EM_PJ:
10974     case EM_PJ_OLD:
10975       return reloc_type == 1; /* R_PJ_DATA_DIR32.  */
10976     case EM_PPC64:
10977       return reloc_type == 1; /* R_PPC64_ADDR32.  */
10978     case EM_PPC:
10979       return reloc_type == 1; /* R_PPC_ADDR32.  */
10980     case EM_RL78:
10981       return reloc_type == 1; /* R_RL78_DIR32.  */
10982     case EM_RX:
10983       return reloc_type == 1; /* R_RX_DIR32.  */
10984     case EM_S370:
10985       return reloc_type == 1; /* R_I370_ADDR31.  */
10986     case EM_S390_OLD:
10987     case EM_S390:
10988       return reloc_type == 4; /* R_S390_32.  */
10989     case EM_SCORE:
10990       return reloc_type == 8; /* R_SCORE_ABS32.  */
10991     case EM_SH:
10992       return reloc_type == 1; /* R_SH_DIR32.  */
10993     case EM_SPARC32PLUS:
10994     case EM_SPARCV9:
10995     case EM_SPARC:
10996       return reloc_type == 3 /* R_SPARC_32.  */
10997         || reloc_type == 23; /* R_SPARC_UA32.  */
10998     case EM_SPU:
10999       return reloc_type == 6; /* R_SPU_ADDR32 */
11000     case EM_TI_C6000:
11001       return reloc_type == 1; /* R_C6000_ABS32.  */
11002     case EM_TILEGX:
11003       return reloc_type == 2; /* R_TILEGX_32.  */
11004     case EM_TILEPRO:
11005       return reloc_type == 1; /* R_TILEPRO_32.  */
11006     case EM_CYGNUS_V850:
11007     case EM_V850:
11008       return reloc_type == 6; /* R_V850_ABS32.  */
11009     case EM_V800:
11010       return reloc_type == 0x33; /* R_V810_WORD.  */
11011     case EM_VAX:
11012       return reloc_type == 1; /* R_VAX_32.  */
11013     case EM_X86_64:
11014     case EM_L1OM:
11015     case EM_K1OM:
11016       return reloc_type == 10; /* R_X86_64_32.  */
11017     case EM_XC16X:
11018     case EM_C166:
11019       return reloc_type == 3; /* R_XC16C_ABS_32.  */
11020     case EM_XGATE:
11021       return reloc_type == 4; /* R_XGATE_32.  */
11022     case EM_XSTORMY16:
11023       return reloc_type == 1; /* R_XSTROMY16_32.  */
11024     case EM_XTENSA_OLD:
11025     case EM_XTENSA:
11026       return reloc_type == 1; /* R_XTENSA_32.  */
11027     default:
11028       error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
11029              elf_header.e_machine);
11030       return FALSE;
11031     }
11032 }
11033
11034 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11035    a 32-bit pc-relative RELA relocation used in DWARF debug sections.  */
11036
11037 static bfd_boolean
11038 is_32bit_pcrel_reloc (unsigned int reloc_type)
11039 {
11040   switch (elf_header.e_machine)
11041     {
11042     case EM_386:
11043     case EM_486:
11044       return reloc_type == 2;  /* R_386_PC32.  */
11045     case EM_68K:
11046       return reloc_type == 4;  /* R_68K_PC32.  */
11047     case EM_AARCH64:
11048       return reloc_type == 261; /* R_AARCH64_PREL32 */
11049     case EM_ADAPTEVA_EPIPHANY:
11050       return reloc_type == 6;
11051     case EM_ALPHA:
11052       return reloc_type == 10; /* R_ALPHA_SREL32.  */
11053     case EM_ARM:
11054       return reloc_type == 3;  /* R_ARM_REL32 */
11055     case EM_MICROBLAZE:
11056       return reloc_type == 2;  /* R_MICROBLAZE_32_PCREL.  */
11057     case EM_OR1K:
11058       return reloc_type == 9; /* R_OR1K_32_PCREL.  */
11059     case EM_PARISC:
11060       return reloc_type == 9;  /* R_PARISC_PCREL32.  */
11061     case EM_PPC:
11062       return reloc_type == 26; /* R_PPC_REL32.  */
11063     case EM_PPC64:
11064       return reloc_type == 26; /* R_PPC64_REL32.  */
11065     case EM_S390_OLD:
11066     case EM_S390:
11067       return reloc_type == 5;  /* R_390_PC32.  */
11068     case EM_SH:
11069       return reloc_type == 2;  /* R_SH_REL32.  */
11070     case EM_SPARC32PLUS:
11071     case EM_SPARCV9:
11072     case EM_SPARC:
11073       return reloc_type == 6;  /* R_SPARC_DISP32.  */
11074     case EM_SPU:
11075       return reloc_type == 13; /* R_SPU_REL32.  */
11076     case EM_TILEGX:
11077       return reloc_type == 6; /* R_TILEGX_32_PCREL.  */
11078     case EM_TILEPRO:
11079       return reloc_type == 4; /* R_TILEPRO_32_PCREL.  */
11080     case EM_X86_64:
11081     case EM_L1OM:
11082     case EM_K1OM:
11083       return reloc_type == 2;  /* R_X86_64_PC32.  */
11084     case EM_XTENSA_OLD:
11085     case EM_XTENSA:
11086       return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
11087     default:
11088       /* Do not abort or issue an error message here.  Not all targets use
11089          pc-relative 32-bit relocs in their DWARF debug information and we
11090          have already tested for target coverage in is_32bit_abs_reloc.  A
11091          more helpful warning message will be generated by apply_relocations
11092          anyway, so just return.  */
11093       return FALSE;
11094     }
11095 }
11096
11097 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11098    a 64-bit absolute RELA relocation used in DWARF debug sections.  */
11099
11100 static bfd_boolean
11101 is_64bit_abs_reloc (unsigned int reloc_type)
11102 {
11103   switch (elf_header.e_machine)
11104     {
11105     case EM_AARCH64:
11106       return reloc_type == 257; /* R_AARCH64_ABS64.  */
11107     case EM_ALPHA:
11108       return reloc_type == 2; /* R_ALPHA_REFQUAD.  */
11109     case EM_IA_64:
11110       return reloc_type == 0x27; /* R_IA64_DIR64LSB.  */
11111     case EM_PARISC:
11112       return reloc_type == 80; /* R_PARISC_DIR64.  */
11113     case EM_PPC64:
11114       return reloc_type == 38; /* R_PPC64_ADDR64.  */
11115     case EM_SPARC32PLUS:
11116     case EM_SPARCV9:
11117     case EM_SPARC:
11118       return reloc_type == 54; /* R_SPARC_UA64.  */
11119     case EM_X86_64:
11120     case EM_L1OM:
11121     case EM_K1OM:
11122       return reloc_type == 1; /* R_X86_64_64.  */
11123     case EM_S390_OLD:
11124     case EM_S390:
11125       return reloc_type == 22;  /* R_S390_64.  */
11126     case EM_TILEGX:
11127       return reloc_type == 1; /* R_TILEGX_64.  */
11128     case EM_MIPS:
11129       return reloc_type == 18;  /* R_MIPS_64.  */
11130     default:
11131       return FALSE;
11132     }
11133 }
11134
11135 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
11136    a 64-bit pc-relative RELA relocation used in DWARF debug sections.  */
11137
11138 static bfd_boolean
11139 is_64bit_pcrel_reloc (unsigned int reloc_type)
11140 {
11141   switch (elf_header.e_machine)
11142     {
11143     case EM_AARCH64:
11144       return reloc_type == 260; /* R_AARCH64_PREL64.  */
11145     case EM_ALPHA:
11146       return reloc_type == 11; /* R_ALPHA_SREL64.  */
11147     case EM_IA_64:
11148       return reloc_type == 0x4f; /* R_IA64_PCREL64LSB.  */
11149     case EM_PARISC:
11150       return reloc_type == 72; /* R_PARISC_PCREL64.  */
11151     case EM_PPC64:
11152       return reloc_type == 44; /* R_PPC64_REL64.  */
11153     case EM_SPARC32PLUS:
11154     case EM_SPARCV9:
11155     case EM_SPARC:
11156       return reloc_type == 46; /* R_SPARC_DISP64.  */
11157     case EM_X86_64:
11158     case EM_L1OM:
11159     case EM_K1OM:
11160       return reloc_type == 24; /* R_X86_64_PC64.  */
11161     case EM_S390_OLD:
11162     case EM_S390:
11163       return reloc_type == 23;  /* R_S390_PC64.  */
11164     case EM_TILEGX:
11165       return reloc_type == 5;  /* R_TILEGX_64_PCREL.  */
11166     default:
11167       return FALSE;
11168     }
11169 }
11170
11171 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11172    a 24-bit absolute RELA relocation used in DWARF debug sections.  */
11173
11174 static bfd_boolean
11175 is_24bit_abs_reloc (unsigned int reloc_type)
11176 {
11177   switch (elf_header.e_machine)
11178     {
11179     case EM_CYGNUS_MN10200:
11180     case EM_MN10200:
11181       return reloc_type == 4; /* R_MN10200_24.  */
11182     default:
11183       return FALSE;
11184     }
11185 }
11186
11187 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11188    a 16-bit absolute RELA relocation used in DWARF debug sections.  */
11189
11190 static bfd_boolean
11191 is_16bit_abs_reloc (unsigned int reloc_type)
11192 {
11193   switch (elf_header.e_machine)
11194     {
11195     case EM_AVR_OLD:
11196     case EM_AVR:
11197       return reloc_type == 4; /* R_AVR_16.  */
11198     case EM_ADAPTEVA_EPIPHANY:
11199       return reloc_type == 5;
11200     case EM_CYGNUS_D10V:
11201     case EM_D10V:
11202       return reloc_type == 3; /* R_D10V_16.  */
11203     case EM_H8S:
11204     case EM_H8_300:
11205     case EM_H8_300H:
11206       return reloc_type == R_H8_DIR16;
11207     case EM_IP2K_OLD:
11208     case EM_IP2K:
11209       return reloc_type == 1; /* R_IP2K_16.  */
11210     case EM_M32C_OLD:
11211     case EM_M32C:
11212       return reloc_type == 1; /* R_M32C_16 */
11213     case EM_MSP430:
11214       if (uses_msp430x_relocs ())
11215         return reloc_type == 2; /* R_MSP430_ABS16.  */
11216     case EM_MSP430_OLD:
11217       return reloc_type == 5; /* R_MSP430_16_BYTE.  */
11218     case EM_NDS32:
11219       return reloc_type == 19; /* R_NDS32_RELA.  */
11220     case EM_ALTERA_NIOS2:
11221       return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16.  */
11222     case EM_NIOS32:
11223       return reloc_type == 9; /* R_NIOS_16.  */
11224     case EM_OR1K:
11225       return reloc_type == 2; /* R_OR1K_16.  */
11226     case EM_TI_C6000:
11227       return reloc_type == 2; /* R_C6000_ABS16.  */
11228     case EM_XC16X:
11229     case EM_C166:
11230       return reloc_type == 2; /* R_XC16C_ABS_16.  */
11231     case EM_CYGNUS_MN10200:
11232     case EM_MN10200:
11233       return reloc_type == 2; /* R_MN10200_16.  */
11234     case EM_CYGNUS_MN10300:
11235     case EM_MN10300:
11236       return reloc_type == 2; /* R_MN10300_16.  */
11237     case EM_XGATE:
11238       return reloc_type == 3; /* R_XGATE_16.  */
11239     default:
11240       return FALSE;
11241     }
11242 }
11243
11244 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
11245    relocation entries (possibly formerly used for SHT_GROUP sections).  */
11246
11247 static bfd_boolean
11248 is_none_reloc (unsigned int reloc_type)
11249 {
11250   switch (elf_header.e_machine)
11251     {
11252     case EM_68K:     /* R_68K_NONE.  */
11253     case EM_386:     /* R_386_NONE.  */
11254     case EM_SPARC32PLUS:
11255     case EM_SPARCV9:
11256     case EM_SPARC:   /* R_SPARC_NONE.  */
11257     case EM_MIPS:    /* R_MIPS_NONE.  */
11258     case EM_PARISC:  /* R_PARISC_NONE.  */
11259     case EM_ALPHA:   /* R_ALPHA_NONE.  */
11260     case EM_ADAPTEVA_EPIPHANY:
11261     case EM_PPC:     /* R_PPC_NONE.  */
11262     case EM_PPC64:   /* R_PPC64_NONE.  */
11263     case EM_ARM:     /* R_ARM_NONE.  */
11264     case EM_IA_64:   /* R_IA64_NONE.  */
11265     case EM_SH:      /* R_SH_NONE.  */
11266     case EM_S390_OLD:
11267     case EM_S390:    /* R_390_NONE.  */
11268     case EM_CRIS:    /* R_CRIS_NONE.  */
11269     case EM_X86_64:  /* R_X86_64_NONE.  */
11270     case EM_L1OM:    /* R_X86_64_NONE.  */
11271     case EM_K1OM:    /* R_X86_64_NONE.  */
11272     case EM_MN10300: /* R_MN10300_NONE.  */
11273     case EM_MOXIE:   /* R_MOXIE_NONE.  */
11274     case EM_M32R:    /* R_M32R_NONE.  */
11275     case EM_TI_C6000:/* R_C6000_NONE.  */
11276     case EM_TILEGX:  /* R_TILEGX_NONE.  */
11277     case EM_TILEPRO: /* R_TILEPRO_NONE.  */
11278     case EM_XC16X:
11279     case EM_C166:    /* R_XC16X_NONE.  */
11280     case EM_ALTERA_NIOS2: /* R_NIOS2_NONE.  */
11281     case EM_NIOS32:  /* R_NIOS_NONE.  */
11282     case EM_OR1K:    /* R_OR1K_NONE. */
11283       return reloc_type == 0;
11284     case EM_AARCH64:
11285       return reloc_type == 0 || reloc_type == 256;
11286     case EM_NDS32:
11287       return (reloc_type == 0       /* R_XTENSA_NONE.  */
11288               || reloc_type == 204  /* R_NDS32_DIFF8.  */
11289               || reloc_type == 205  /* R_NDS32_DIFF16.  */
11290               || reloc_type == 206  /* R_NDS32_DIFF32.  */
11291               || reloc_type == 207  /* R_NDS32_ULEB128.  */);
11292     case EM_XTENSA_OLD:
11293     case EM_XTENSA:
11294       return (reloc_type == 0      /* R_XTENSA_NONE.  */
11295               || reloc_type == 17  /* R_XTENSA_DIFF8.  */
11296               || reloc_type == 18  /* R_XTENSA_DIFF16.  */
11297               || reloc_type == 19  /* R_XTENSA_DIFF32.  */);
11298     case EM_METAG:
11299       return reloc_type == 3; /* R_METAG_NONE.  */
11300     }
11301   return FALSE;
11302 }
11303
11304 /* Apply relocations to a section.
11305    Note: So far support has been added only for those relocations
11306    which can be found in debug sections.
11307    FIXME: Add support for more relocations ?  */
11308
11309 static void
11310 apply_relocations (void * file,
11311                    Elf_Internal_Shdr * section,
11312                    unsigned char * start)
11313 {
11314   Elf_Internal_Shdr * relsec;
11315   unsigned char * end = start + section->sh_size;
11316
11317   if (elf_header.e_type != ET_REL)
11318     return;
11319
11320   /* Find the reloc section associated with the section.  */
11321   for (relsec = section_headers;
11322        relsec < section_headers + elf_header.e_shnum;
11323        ++relsec)
11324     {
11325       bfd_boolean is_rela;
11326       unsigned long num_relocs;
11327       Elf_Internal_Rela * relocs;
11328       Elf_Internal_Rela * rp;
11329       Elf_Internal_Shdr * symsec;
11330       Elf_Internal_Sym * symtab;
11331       unsigned long num_syms;
11332       Elf_Internal_Sym * sym;
11333
11334       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
11335           || relsec->sh_info >= elf_header.e_shnum
11336           || section_headers + relsec->sh_info != section
11337           || relsec->sh_size == 0
11338           || relsec->sh_link >= elf_header.e_shnum)
11339         continue;
11340
11341       is_rela = relsec->sh_type == SHT_RELA;
11342
11343       if (is_rela)
11344         {
11345           if (!slurp_rela_relocs ((FILE *) file, relsec->sh_offset,
11346                                   relsec->sh_size, & relocs, & num_relocs))
11347             return;
11348         }
11349       else
11350         {
11351           if (!slurp_rel_relocs ((FILE *) file, relsec->sh_offset,
11352                                  relsec->sh_size, & relocs, & num_relocs))
11353             return;
11354         }
11355
11356       /* SH uses RELA but uses in place value instead of the addend field.  */
11357       if (elf_header.e_machine == EM_SH)
11358         is_rela = FALSE;
11359
11360       symsec = section_headers + relsec->sh_link;
11361       symtab = GET_ELF_SYMBOLS ((FILE *) file, symsec, & num_syms);
11362
11363       for (rp = relocs; rp < relocs + num_relocs; ++rp)
11364         {
11365           bfd_vma         addend;
11366           unsigned int    reloc_type;
11367           unsigned int    reloc_size;
11368           unsigned char * rloc;
11369           unsigned long   sym_index;
11370
11371           reloc_type = get_reloc_type (rp->r_info);
11372
11373           if (target_specific_reloc_handling (rp, start, symtab))
11374             continue;
11375           else if (is_none_reloc (reloc_type))
11376             continue;
11377           else if (is_32bit_abs_reloc (reloc_type)
11378                    || is_32bit_pcrel_reloc (reloc_type))
11379             reloc_size = 4;
11380           else if (is_64bit_abs_reloc (reloc_type)
11381                    || is_64bit_pcrel_reloc (reloc_type))
11382             reloc_size = 8;
11383           else if (is_24bit_abs_reloc (reloc_type))
11384             reloc_size = 3;
11385           else if (is_16bit_abs_reloc (reloc_type))
11386             reloc_size = 2;
11387           else
11388             {
11389               warn (_("unable to apply unsupported reloc type %d to section %s\n"),
11390                     reloc_type, printable_section_name (section));
11391               continue;
11392             }
11393
11394           rloc = start + rp->r_offset;
11395           if ((rloc + reloc_size) > end || (rloc < start))
11396             {
11397               warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
11398                     (unsigned long) rp->r_offset,
11399                     printable_section_name (section));
11400               continue;
11401             }
11402
11403           sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
11404           if (sym_index >= num_syms)
11405             {
11406               warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
11407                     sym_index, printable_section_name (section));
11408               continue;
11409             }
11410           sym = symtab + sym_index;
11411
11412           /* If the reloc has a symbol associated with it,
11413              make sure that it is of an appropriate type.
11414
11415              Relocations against symbols without type can happen.
11416              Gcc -feliminate-dwarf2-dups may generate symbols
11417              without type for debug info.
11418
11419              Icc generates relocations against function symbols
11420              instead of local labels.
11421
11422              Relocations against object symbols can happen, eg when
11423              referencing a global array.  For an example of this see
11424              the _clz.o binary in libgcc.a.  */
11425           if (sym != symtab
11426               && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
11427             {
11428               warn (_("skipping unexpected symbol type %s in %ld'th relocation in section %s\n"),
11429                     get_symbol_type (ELF_ST_TYPE (sym->st_info)),
11430                     (long int)(rp - relocs),
11431                     printable_section_name (relsec));
11432               continue;
11433             }
11434
11435           addend = 0;
11436           if (is_rela)
11437             addend += rp->r_addend;
11438           /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
11439              partial_inplace.  */
11440           if (!is_rela
11441               || (elf_header.e_machine == EM_XTENSA
11442                   && reloc_type == 1)
11443               || ((elf_header.e_machine == EM_PJ
11444                    || elf_header.e_machine == EM_PJ_OLD)
11445                   && reloc_type == 1)
11446               || ((elf_header.e_machine == EM_D30V
11447                    || elf_header.e_machine == EM_CYGNUS_D30V)
11448                   && reloc_type == 12))
11449             addend += byte_get (rloc, reloc_size);
11450
11451           if (is_32bit_pcrel_reloc (reloc_type)
11452               || is_64bit_pcrel_reloc (reloc_type))
11453             {
11454               /* On HPPA, all pc-relative relocations are biased by 8.  */
11455               if (elf_header.e_machine == EM_PARISC)
11456                 addend -= 8;
11457               byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
11458                         reloc_size);
11459             }
11460           else
11461             byte_put (rloc, addend + sym->st_value, reloc_size);
11462         }
11463
11464       free (symtab);
11465       free (relocs);
11466       break;
11467     }
11468 }
11469
11470 #ifdef SUPPORT_DISASSEMBLY
11471 static int
11472 disassemble_section (Elf_Internal_Shdr * section, FILE * file)
11473 {
11474   printf (_("\nAssembly dump of section %s\n"), printable_section_name (section));
11475
11476   /* FIXME: XXX -- to be done --- XXX */
11477
11478   return 1;
11479 }
11480 #endif
11481
11482 /* Reads in the contents of SECTION from FILE, returning a pointer
11483    to a malloc'ed buffer or NULL if something went wrong.  */
11484
11485 static char *
11486 get_section_contents (Elf_Internal_Shdr * section, FILE * file)
11487 {
11488   bfd_size_type num_bytes;
11489
11490   num_bytes = section->sh_size;
11491
11492   if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
11493     {
11494       printf (_("\nSection '%s' has no data to dump.\n"),
11495               printable_section_name (section));
11496       return NULL;
11497     }
11498
11499   return  (char *) get_data (NULL, file, section->sh_offset, 1, num_bytes,
11500                              _("section contents"));
11501 }
11502
11503
11504 static void
11505 dump_section_as_strings (Elf_Internal_Shdr * section, FILE * file)
11506 {
11507   Elf_Internal_Shdr * relsec;
11508   bfd_size_type num_bytes;
11509   char * data;
11510   char * end;
11511   char * start;
11512   bfd_boolean some_strings_shown;
11513
11514   start = get_section_contents (section, file);
11515   if (start == NULL)
11516     return;
11517
11518   printf (_("\nString dump of section '%s':\n"), printable_section_name (section));
11519
11520   /* If the section being dumped has relocations against it the user might
11521      be expecting these relocations to have been applied.  Check for this
11522      case and issue a warning message in order to avoid confusion.
11523      FIXME: Maybe we ought to have an option that dumps a section with
11524      relocs applied ?  */
11525   for (relsec = section_headers;
11526        relsec < section_headers + elf_header.e_shnum;
11527        ++relsec)
11528     {
11529       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
11530           || relsec->sh_info >= elf_header.e_shnum
11531           || section_headers + relsec->sh_info != section
11532           || relsec->sh_size == 0
11533           || relsec->sh_link >= elf_header.e_shnum)
11534         continue;
11535
11536       printf (_("  Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
11537       break;
11538     }
11539
11540   num_bytes = section->sh_size;
11541   data = start;
11542   end  = start + num_bytes;
11543   some_strings_shown = FALSE;
11544
11545   while (data < end)
11546     {
11547       while (!ISPRINT (* data))
11548         if (++ data >= end)
11549           break;
11550
11551       if (data < end)
11552         {
11553           size_t maxlen = end - data;
11554
11555 #ifndef __MSVCRT__
11556           /* PR 11128: Use two separate invocations in order to work
11557              around bugs in the Solaris 8 implementation of printf.  */
11558           printf ("  [%6tx]  ", data - start);
11559 #else
11560           printf ("  [%6Ix]  ", (size_t) (data - start));
11561 #endif
11562           if (maxlen > 0)
11563             {
11564               print_symbol ((int) maxlen, data);
11565               putchar ('\n');
11566               data += strnlen (data, maxlen);
11567             }
11568           else
11569             {
11570               printf (_("<corrupt>\n"));
11571               data = end;
11572             }
11573           some_strings_shown = TRUE;
11574         }
11575     }
11576
11577   if (! some_strings_shown)
11578     printf (_("  No strings found in this section."));
11579
11580   free (start);
11581
11582   putchar ('\n');
11583 }
11584
11585 static void
11586 dump_section_as_bytes (Elf_Internal_Shdr * section,
11587                        FILE * file,
11588                        bfd_boolean relocate)
11589 {
11590   Elf_Internal_Shdr * relsec;
11591   bfd_size_type bytes;
11592   bfd_vma addr;
11593   unsigned char * data;
11594   unsigned char * start;
11595
11596   start = (unsigned char *) get_section_contents (section, file);
11597   if (start == NULL)
11598     return;
11599
11600   printf (_("\nHex dump of section '%s':\n"), printable_section_name (section));
11601
11602   if (relocate)
11603     {
11604       apply_relocations (file, section, start);
11605     }
11606   else
11607     {
11608       /* If the section being dumped has relocations against it the user might
11609          be expecting these relocations to have been applied.  Check for this
11610          case and issue a warning message in order to avoid confusion.
11611          FIXME: Maybe we ought to have an option that dumps a section with
11612          relocs applied ?  */
11613       for (relsec = section_headers;
11614            relsec < section_headers + elf_header.e_shnum;
11615            ++relsec)
11616         {
11617           if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
11618               || relsec->sh_info >= elf_header.e_shnum
11619               || section_headers + relsec->sh_info != section
11620               || relsec->sh_size == 0
11621               || relsec->sh_link >= elf_header.e_shnum)
11622             continue;
11623
11624           printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
11625           break;
11626         }
11627     }
11628
11629   addr = section->sh_addr;
11630   bytes = section->sh_size;
11631   data = start;
11632
11633   while (bytes)
11634     {
11635       int j;
11636       int k;
11637       int lbytes;
11638
11639       lbytes = (bytes > 16 ? 16 : bytes);
11640
11641       printf ("  0x%8.8lx ", (unsigned long) addr);
11642
11643       for (j = 0; j < 16; j++)
11644         {
11645           if (j < lbytes)
11646             printf ("%2.2x", data[j]);
11647           else
11648             printf ("  ");
11649
11650           if ((j & 3) == 3)
11651             printf (" ");
11652         }
11653
11654       for (j = 0; j < lbytes; j++)
11655         {
11656           k = data[j];
11657           if (k >= ' ' && k < 0x7f)
11658             printf ("%c", k);
11659           else
11660             printf (".");
11661         }
11662
11663       putchar ('\n');
11664
11665       data  += lbytes;
11666       addr  += lbytes;
11667       bytes -= lbytes;
11668     }
11669
11670   free (start);
11671
11672   putchar ('\n');
11673 }
11674
11675 /* Uncompresses a section that was compressed using zlib, in place.  */
11676
11677 static int
11678 uncompress_section_contents (unsigned char **buffer ATTRIBUTE_UNUSED,
11679                              dwarf_size_type *size ATTRIBUTE_UNUSED)
11680 {
11681 #ifndef HAVE_ZLIB_H
11682   return FALSE;
11683 #else
11684   dwarf_size_type compressed_size = *size;
11685   unsigned char * compressed_buffer = *buffer;
11686   dwarf_size_type uncompressed_size;
11687   unsigned char * uncompressed_buffer;
11688   z_stream strm;
11689   int rc;
11690   dwarf_size_type header_size = 12;
11691
11692   /* Read the zlib header.  In this case, it should be "ZLIB" followed
11693      by the uncompressed section size, 8 bytes in big-endian order.  */
11694   if (compressed_size < header_size
11695       || ! streq ((char *) compressed_buffer, "ZLIB"))
11696     return 0;
11697
11698   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
11699   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
11700   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
11701   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
11702   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
11703   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
11704   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
11705   uncompressed_size += compressed_buffer[11];
11706
11707   /* It is possible the section consists of several compressed
11708      buffers concatenated together, so we uncompress in a loop.  */
11709   strm.zalloc = NULL;
11710   strm.zfree = NULL;
11711   strm.opaque = NULL;
11712   strm.avail_in = compressed_size - header_size;
11713   strm.next_in = (Bytef *) compressed_buffer + header_size;
11714   strm.avail_out = uncompressed_size;
11715   uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
11716
11717   rc = inflateInit (& strm);
11718   while (strm.avail_in > 0)
11719     {
11720       if (rc != Z_OK)
11721         goto fail;
11722       strm.next_out = ((Bytef *) uncompressed_buffer
11723                        + (uncompressed_size - strm.avail_out));
11724       rc = inflate (&strm, Z_FINISH);
11725       if (rc != Z_STREAM_END)
11726         goto fail;
11727       rc = inflateReset (& strm);
11728     }
11729   rc = inflateEnd (& strm);
11730   if (rc != Z_OK
11731       || strm.avail_out != 0)
11732     goto fail;
11733
11734   free (compressed_buffer);
11735   *buffer = uncompressed_buffer;
11736   *size = uncompressed_size;
11737   return 1;
11738
11739  fail:
11740   free (uncompressed_buffer);
11741   /* Indicate decompression failure.  */
11742   *buffer = NULL;
11743   return 0;
11744 #endif  /* HAVE_ZLIB_H */
11745 }
11746
11747 static int
11748 load_specific_debug_section (enum dwarf_section_display_enum debug,
11749                              Elf_Internal_Shdr * sec, void * file)
11750 {
11751   struct dwarf_section * section = &debug_displays [debug].section;
11752   char buf [64];
11753
11754   /* If it is already loaded, do nothing.  */
11755   if (section->start != NULL)
11756     return 1;
11757
11758   snprintf (buf, sizeof (buf), _("%s section data"), section->name);
11759   section->address = sec->sh_addr;
11760   section->start = (unsigned char *) get_data (NULL, (FILE *) file,
11761                                                sec->sh_offset, 1,
11762                                                sec->sh_size, buf);
11763   if (section->start == NULL)
11764     section->size = 0;
11765   else
11766     {
11767       section->size = sec->sh_size;
11768       if (uncompress_section_contents (&section->start, &section->size))
11769         sec->sh_size = section->size;
11770     }
11771
11772   if (section->start == NULL)
11773     return 0;
11774
11775   if (debug_displays [debug].relocate)
11776     apply_relocations ((FILE *) file, sec, section->start);
11777
11778   return 1;
11779 }
11780
11781 /* If this is not NULL, load_debug_section will only look for sections
11782    within the list of sections given here.  */
11783 unsigned int *section_subset = NULL;
11784
11785 int
11786 load_debug_section (enum dwarf_section_display_enum debug, void * file)
11787 {
11788   struct dwarf_section * section = &debug_displays [debug].section;
11789   Elf_Internal_Shdr * sec;
11790
11791   /* Locate the debug section.  */
11792   sec = find_section_in_set (section->uncompressed_name, section_subset);
11793   if (sec != NULL)
11794     section->name = section->uncompressed_name;
11795   else
11796     {
11797       sec = find_section_in_set (section->compressed_name, section_subset);
11798       if (sec != NULL)
11799         section->name = section->compressed_name;
11800     }
11801   if (sec == NULL)
11802     return 0;
11803
11804   /* If we're loading from a subset of sections, and we've loaded
11805      a section matching this name before, it's likely that it's a
11806      different one.  */
11807   if (section_subset != NULL)
11808     free_debug_section (debug);
11809
11810   return load_specific_debug_section (debug, sec, (FILE *) file);
11811 }
11812
11813 void
11814 free_debug_section (enum dwarf_section_display_enum debug)
11815 {
11816   struct dwarf_section * section = &debug_displays [debug].section;
11817
11818   if (section->start == NULL)
11819     return;
11820
11821   free ((char *) section->start);
11822   section->start = NULL;
11823   section->address = 0;
11824   section->size = 0;
11825 }
11826
11827 static int
11828 display_debug_section (int shndx, Elf_Internal_Shdr * section, FILE * file)
11829 {
11830   char * name = SECTION_NAME (section);
11831   const char * print_name = printable_section_name (section);
11832   bfd_size_type length;
11833   int result = 1;
11834   int i;
11835
11836   length = section->sh_size;
11837   if (length == 0)
11838     {
11839       printf (_("\nSection '%s' has no debugging data.\n"), print_name);
11840       return 0;
11841     }
11842   if (section->sh_type == SHT_NOBITS)
11843     {
11844       /* There is no point in dumping the contents of a debugging section
11845          which has the NOBITS type - the bits in the file will be random.
11846          This can happen when a file containing a .eh_frame section is
11847          stripped with the --only-keep-debug command line option.  */
11848       printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
11849               print_name);
11850       return 0;
11851     }
11852
11853   if (const_strneq (name, ".gnu.linkonce.wi."))
11854     name = ".debug_info";
11855
11856   /* See if we know how to display the contents of this section.  */
11857   for (i = 0; i < max; i++)
11858     if (streq (debug_displays[i].section.uncompressed_name, name)
11859         || (i == line && const_strneq (name, ".debug_line."))
11860         || streq (debug_displays[i].section.compressed_name, name))
11861       {
11862         struct dwarf_section * sec = &debug_displays [i].section;
11863         int secondary = (section != find_section (name));
11864
11865         if (secondary)
11866           free_debug_section ((enum dwarf_section_display_enum) i);
11867
11868         if (i == line && const_strneq (name, ".debug_line."))
11869           sec->name = name;
11870         else if (streq (sec->uncompressed_name, name))
11871           sec->name = sec->uncompressed_name;
11872         else
11873           sec->name = sec->compressed_name;
11874         if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
11875                                          section, file))
11876           {
11877             /* If this debug section is part of a CU/TU set in a .dwp file,
11878                restrict load_debug_section to the sections in that set.  */
11879             section_subset = find_cu_tu_set (file, shndx);
11880
11881             result &= debug_displays[i].display (sec, file);
11882
11883             section_subset = NULL;
11884
11885             if (secondary || (i != info && i != abbrev))
11886               free_debug_section ((enum dwarf_section_display_enum) i);
11887           }
11888
11889         break;
11890       }
11891
11892   if (i == max)
11893     {
11894       printf (_("Unrecognized debug section: %s\n"), print_name);
11895       result = 0;
11896     }
11897
11898   return result;
11899 }
11900
11901 /* Set DUMP_SECTS for all sections where dumps were requested
11902    based on section name.  */
11903
11904 static void
11905 initialise_dumps_byname (void)
11906 {
11907   struct dump_list_entry * cur;
11908
11909   for (cur = dump_sects_byname; cur; cur = cur->next)
11910     {
11911       unsigned int i;
11912       int any;
11913
11914       for (i = 0, any = 0; i < elf_header.e_shnum; i++)
11915         if (streq (SECTION_NAME (section_headers + i), cur->name))
11916           {
11917             request_dump_bynumber (i, cur->type);
11918             any = 1;
11919           }
11920
11921       if (!any)
11922         warn (_("Section '%s' was not dumped because it does not exist!\n"),
11923               cur->name);
11924     }
11925 }
11926
11927 static void
11928 process_section_contents (FILE * file)
11929 {
11930   Elf_Internal_Shdr * section;
11931   unsigned int i;
11932
11933   if (! do_dump)
11934     return;
11935
11936   initialise_dumps_byname ();
11937
11938   for (i = 0, section = section_headers;
11939        i < elf_header.e_shnum && i < num_dump_sects;
11940        i++, section++)
11941     {
11942 #ifdef SUPPORT_DISASSEMBLY
11943       if (dump_sects[i] & DISASS_DUMP)
11944         disassemble_section (section, file);
11945 #endif
11946       if (dump_sects[i] & HEX_DUMP)
11947         dump_section_as_bytes (section, file, FALSE);
11948
11949       if (dump_sects[i] & RELOC_DUMP)
11950         dump_section_as_bytes (section, file, TRUE);
11951
11952       if (dump_sects[i] & STRING_DUMP)
11953         dump_section_as_strings (section, file);
11954
11955       if (dump_sects[i] & DEBUG_DUMP)
11956         display_debug_section (i, section, file);
11957     }
11958
11959   /* Check to see if the user requested a
11960      dump of a section that does not exist.  */
11961   while (i++ < num_dump_sects)
11962     if (dump_sects[i])
11963       warn (_("Section %d was not dumped because it does not exist!\n"), i);
11964 }
11965
11966 static void
11967 process_mips_fpe_exception (int mask)
11968 {
11969   if (mask)
11970     {
11971       int first = 1;
11972       if (mask & OEX_FPU_INEX)
11973         fputs ("INEX", stdout), first = 0;
11974       if (mask & OEX_FPU_UFLO)
11975         printf ("%sUFLO", first ? "" : "|"), first = 0;
11976       if (mask & OEX_FPU_OFLO)
11977         printf ("%sOFLO", first ? "" : "|"), first = 0;
11978       if (mask & OEX_FPU_DIV0)
11979         printf ("%sDIV0", first ? "" : "|"), first = 0;
11980       if (mask & OEX_FPU_INVAL)
11981         printf ("%sINVAL", first ? "" : "|");
11982     }
11983   else
11984     fputs ("0", stdout);
11985 }
11986
11987 /* Display's the value of TAG at location P.  If TAG is
11988    greater than 0 it is assumed to be an unknown tag, and
11989    a message is printed to this effect.  Otherwise it is
11990    assumed that a message has already been printed.
11991
11992    If the bottom bit of TAG is set it assumed to have a
11993    string value, otherwise it is assumed to have an integer
11994    value.
11995
11996    Returns an updated P pointing to the first unread byte
11997    beyond the end of TAG's value.
11998
11999    Reads at or beyond END will not be made.  */
12000
12001 static unsigned char *
12002 display_tag_value (int tag,
12003                    unsigned char * p,
12004                    const unsigned char * const end)
12005 {
12006   unsigned long val;
12007
12008   if (tag > 0)
12009     printf ("  Tag_unknown_%d: ", tag);
12010
12011   if (p >= end)
12012     {
12013       warn (_("<corrupt tag>\n"));
12014     }
12015   else if (tag & 1)
12016     {
12017       /* PR 17531 file: 027-19978-0.004.  */
12018       size_t maxlen = (end - p) - 1;
12019
12020       putchar ('"');
12021       if (maxlen > 0)
12022         {
12023           print_symbol ((int) maxlen, (const char *) p);
12024           p += strnlen ((char *) p, maxlen) + 1;
12025         }
12026       else
12027         {
12028           printf (_("<corrupt string tag>"));
12029           p = (unsigned char *) end;
12030         }
12031       printf ("\"\n");
12032     }
12033   else
12034     {
12035       unsigned int len;
12036
12037       val = read_uleb128 (p, &len, end);
12038       p += len;
12039       printf ("%ld (0x%lx)\n", val, val);
12040     }
12041
12042   assert (p <= end);
12043   return p;
12044 }
12045
12046 /* ARM EABI attributes section.  */
12047 typedef struct
12048 {
12049   unsigned int tag;
12050   const char * name;
12051   /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup.  */
12052   unsigned int type;
12053   const char ** table;
12054 } arm_attr_public_tag;
12055
12056 static const char * arm_attr_tag_CPU_arch[] =
12057   {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
12058    "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8"};
12059 static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
12060 static const char * arm_attr_tag_THUMB_ISA_use[] =
12061   {"No", "Thumb-1", "Thumb-2"};
12062 static const char * arm_attr_tag_FP_arch[] =
12063   {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
12064    "FP for ARMv8"};
12065 static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
12066 static const char * arm_attr_tag_Advanced_SIMD_arch[] =
12067   {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8"};
12068 static const char * arm_attr_tag_PCS_config[] =
12069   {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
12070    "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
12071 static const char * arm_attr_tag_ABI_PCS_R9_use[] =
12072   {"V6", "SB", "TLS", "Unused"};
12073 static const char * arm_attr_tag_ABI_PCS_RW_data[] =
12074   {"Absolute", "PC-relative", "SB-relative", "None"};
12075 static const char * arm_attr_tag_ABI_PCS_RO_data[] =
12076   {"Absolute", "PC-relative", "None"};
12077 static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
12078   {"None", "direct", "GOT-indirect"};
12079 static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
12080   {"None", "??? 1", "2", "??? 3", "4"};
12081 static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
12082 static const char * arm_attr_tag_ABI_FP_denormal[] =
12083   {"Unused", "Needed", "Sign only"};
12084 static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
12085 static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
12086 static const char * arm_attr_tag_ABI_FP_number_model[] =
12087   {"Unused", "Finite", "RTABI", "IEEE 754"};
12088 static const char * arm_attr_tag_ABI_enum_size[] =
12089   {"Unused", "small", "int", "forced to int"};
12090 static const char * arm_attr_tag_ABI_HardFP_use[] =
12091   {"As Tag_FP_arch", "SP only", "DP only", "SP and DP"};
12092 static const char * arm_attr_tag_ABI_VFP_args[] =
12093   {"AAPCS", "VFP registers", "custom"};
12094 static const char * arm_attr_tag_ABI_WMMX_args[] =
12095   {"AAPCS", "WMMX registers", "custom"};
12096 static const char * arm_attr_tag_ABI_optimization_goals[] =
12097   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
12098     "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
12099 static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
12100   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
12101     "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
12102 static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
12103 static const char * arm_attr_tag_FP_HP_extension[] =
12104   {"Not Allowed", "Allowed"};
12105 static const char * arm_attr_tag_ABI_FP_16bit_format[] =
12106   {"None", "IEEE 754", "Alternative Format"};
12107 static const char * arm_attr_tag_MPextension_use[] =
12108   {"Not Allowed", "Allowed"};
12109 static const char * arm_attr_tag_DIV_use[] =
12110   {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
12111     "Allowed in v7-A with integer division extension"};
12112 static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
12113 static const char * arm_attr_tag_Virtualization_use[] =
12114   {"Not Allowed", "TrustZone", "Virtualization Extensions",
12115     "TrustZone and Virtualization Extensions"};
12116 static const char * arm_attr_tag_MPextension_use_legacy[] =
12117   {"Not Allowed", "Allowed"};
12118
12119 #define LOOKUP(id, name) \
12120   {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
12121 static arm_attr_public_tag arm_attr_public_tags[] =
12122 {
12123   {4, "CPU_raw_name", 1, NULL},
12124   {5, "CPU_name", 1, NULL},
12125   LOOKUP(6, CPU_arch),
12126   {7, "CPU_arch_profile", 0, NULL},
12127   LOOKUP(8, ARM_ISA_use),
12128   LOOKUP(9, THUMB_ISA_use),
12129   LOOKUP(10, FP_arch),
12130   LOOKUP(11, WMMX_arch),
12131   LOOKUP(12, Advanced_SIMD_arch),
12132   LOOKUP(13, PCS_config),
12133   LOOKUP(14, ABI_PCS_R9_use),
12134   LOOKUP(15, ABI_PCS_RW_data),
12135   LOOKUP(16, ABI_PCS_RO_data),
12136   LOOKUP(17, ABI_PCS_GOT_use),
12137   LOOKUP(18, ABI_PCS_wchar_t),
12138   LOOKUP(19, ABI_FP_rounding),
12139   LOOKUP(20, ABI_FP_denormal),
12140   LOOKUP(21, ABI_FP_exceptions),
12141   LOOKUP(22, ABI_FP_user_exceptions),
12142   LOOKUP(23, ABI_FP_number_model),
12143   {24, "ABI_align_needed", 0, NULL},
12144   {25, "ABI_align_preserved", 0, NULL},
12145   LOOKUP(26, ABI_enum_size),
12146   LOOKUP(27, ABI_HardFP_use),
12147   LOOKUP(28, ABI_VFP_args),
12148   LOOKUP(29, ABI_WMMX_args),
12149   LOOKUP(30, ABI_optimization_goals),
12150   LOOKUP(31, ABI_FP_optimization_goals),
12151   {32, "compatibility", 0, NULL},
12152   LOOKUP(34, CPU_unaligned_access),
12153   LOOKUP(36, FP_HP_extension),
12154   LOOKUP(38, ABI_FP_16bit_format),
12155   LOOKUP(42, MPextension_use),
12156   LOOKUP(44, DIV_use),
12157   {64, "nodefaults", 0, NULL},
12158   {65, "also_compatible_with", 0, NULL},
12159   LOOKUP(66, T2EE_use),
12160   {67, "conformance", 1, NULL},
12161   LOOKUP(68, Virtualization_use),
12162   LOOKUP(70, MPextension_use_legacy)
12163 };
12164 #undef LOOKUP
12165
12166 static unsigned char *
12167 display_arm_attribute (unsigned char * p,
12168                        const unsigned char * const end)
12169 {
12170   unsigned int tag;
12171   unsigned int len;
12172   unsigned int val;
12173   arm_attr_public_tag * attr;
12174   unsigned i;
12175   unsigned int type;
12176
12177   tag = read_uleb128 (p, &len, end);
12178   p += len;
12179   attr = NULL;
12180   for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
12181     {
12182       if (arm_attr_public_tags[i].tag == tag)
12183         {
12184           attr = &arm_attr_public_tags[i];
12185           break;
12186         }
12187     }
12188
12189   if (attr)
12190     {
12191       printf ("  Tag_%s: ", attr->name);
12192       switch (attr->type)
12193         {
12194         case 0:
12195           switch (tag)
12196             {
12197             case 7: /* Tag_CPU_arch_profile.  */
12198               val = read_uleb128 (p, &len, end);
12199               p += len;
12200               switch (val)
12201                 {
12202                 case 0: printf (_("None\n")); break;
12203                 case 'A': printf (_("Application\n")); break;
12204                 case 'R': printf (_("Realtime\n")); break;
12205                 case 'M': printf (_("Microcontroller\n")); break;
12206                 case 'S': printf (_("Application or Realtime\n")); break;
12207                 default: printf ("??? (%d)\n", val); break;
12208                 }
12209               break;
12210
12211             case 24: /* Tag_align_needed.  */
12212               val = read_uleb128 (p, &len, end);
12213               p += len;
12214               switch (val)
12215                 {
12216                 case 0: printf (_("None\n")); break;
12217                 case 1: printf (_("8-byte\n")); break;
12218                 case 2: printf (_("4-byte\n")); break;
12219                 case 3: printf ("??? 3\n"); break;
12220                 default:
12221                   if (val <= 12)
12222                     printf (_("8-byte and up to %d-byte extended\n"),
12223                             1 << val);
12224                   else
12225                     printf ("??? (%d)\n", val);
12226                   break;
12227                 }
12228               break;
12229
12230             case 25: /* Tag_align_preserved.  */
12231               val = read_uleb128 (p, &len, end);
12232               p += len;
12233               switch (val)
12234                 {
12235                 case 0: printf (_("None\n")); break;
12236                 case 1: printf (_("8-byte, except leaf SP\n")); break;
12237                 case 2: printf (_("8-byte\n")); break;
12238                 case 3: printf ("??? 3\n"); break;
12239                 default:
12240                   if (val <= 12)
12241                     printf (_("8-byte and up to %d-byte extended\n"),
12242                             1 << val);
12243                   else
12244                     printf ("??? (%d)\n", val);
12245                   break;
12246                 }
12247               break;
12248
12249             case 32: /* Tag_compatibility.  */
12250               {
12251                 val = read_uleb128 (p, &len, end);
12252                 p += len;
12253                 printf (_("flag = %d, vendor = "), val);
12254                 if (p < end - 1)
12255                   {
12256                     size_t maxlen = (end - p) - 1;
12257
12258                     print_symbol ((int) maxlen, (const char *) p);
12259                     p += strnlen ((char *) p, maxlen) + 1;
12260                   }
12261                 else
12262                   {
12263                     printf (_("<corrupt>"));
12264                     p = (unsigned char *) end;
12265                   }
12266                 putchar ('\n');
12267               }
12268               break;
12269
12270             case 64: /* Tag_nodefaults.  */
12271               /* PR 17531: file: 001-505008-0.01.  */
12272               if (p < end)
12273                 p++;
12274               printf (_("True\n"));
12275               break;
12276
12277             case 65: /* Tag_also_compatible_with.  */
12278               val = read_uleb128 (p, &len, end);
12279               p += len;
12280               if (val == 6 /* Tag_CPU_arch.  */)
12281                 {
12282                   val = read_uleb128 (p, &len, end);
12283                   p += len;
12284                   if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
12285                     printf ("??? (%d)\n", val);
12286                   else
12287                     printf ("%s\n", arm_attr_tag_CPU_arch[val]);
12288                 }
12289               else
12290                 printf ("???\n");
12291               while (p < end && *(p++) != '\0' /* NUL terminator.  */)
12292                 ;
12293               break;
12294
12295             default:
12296               printf (_("<unknown: %d>\n"), tag);
12297               break;
12298             }
12299           return p;
12300
12301         case 1:
12302           return display_tag_value (-1, p, end);
12303         case 2:
12304           return display_tag_value (0, p, end);
12305
12306         default:
12307           assert (attr->type & 0x80);
12308           val = read_uleb128 (p, &len, end);
12309           p += len;
12310           type = attr->type & 0x7f;
12311           if (val >= type)
12312             printf ("??? (%d)\n", val);
12313           else
12314             printf ("%s\n", attr->table[val]);
12315           return p;
12316         }
12317     }
12318
12319   return display_tag_value (tag, p, end);
12320 }
12321
12322 static unsigned char *
12323 display_gnu_attribute (unsigned char * p,
12324                        unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int, const unsigned char * const),
12325                        const unsigned char * const end)
12326 {
12327   int tag;
12328   unsigned int len;
12329   int val;
12330
12331   tag = read_uleb128 (p, &len, end);
12332   p += len;
12333
12334   /* Tag_compatibility is the only generic GNU attribute defined at
12335      present.  */
12336   if (tag == 32)
12337     {
12338       val = read_uleb128 (p, &len, end);
12339       p += len;
12340
12341       printf (_("flag = %d, vendor = "), val);
12342       if (p == end)
12343         {
12344           printf (_("<corrupt>\n"));
12345           warn (_("corrupt vendor attribute\n"));
12346         }
12347       else
12348         {
12349           if (p < end - 1)
12350             {
12351               size_t maxlen = (end - p) - 1;
12352
12353               print_symbol ((int) maxlen, (const char *) p);
12354               p += strnlen ((char *) p, maxlen) + 1;
12355             }
12356           else
12357             {
12358               printf (_("<corrupt>"));
12359               p = (unsigned char *) end;
12360             }
12361           putchar ('\n');
12362         }
12363       return p;
12364     }
12365
12366   if ((tag & 2) == 0 && display_proc_gnu_attribute)
12367     return display_proc_gnu_attribute (p, tag, end);
12368
12369   return display_tag_value (tag, p, end);
12370 }
12371
12372 static unsigned char *
12373 display_power_gnu_attribute (unsigned char * p,
12374                              int tag,
12375                              const unsigned char * const end)
12376 {
12377   unsigned int len;
12378   int val;
12379
12380   if (tag == Tag_GNU_Power_ABI_FP)
12381     {
12382       val = read_uleb128 (p, &len, end);
12383       p += len;
12384       printf ("  Tag_GNU_Power_ABI_FP: ");
12385
12386       switch (val)
12387         {
12388         case 0:
12389           printf (_("Hard or soft float\n"));
12390           break;
12391         case 1:
12392           printf (_("Hard float\n"));
12393           break;
12394         case 2:
12395           printf (_("Soft float\n"));
12396           break;
12397         case 3:
12398           printf (_("Single-precision hard float\n"));
12399           break;
12400         default:
12401           printf ("??? (%d)\n", val);
12402           break;
12403         }
12404       return p;
12405    }
12406
12407   if (tag == Tag_GNU_Power_ABI_Vector)
12408     {
12409       val = read_uleb128 (p, &len, end);
12410       p += len;
12411       printf ("  Tag_GNU_Power_ABI_Vector: ");
12412       switch (val)
12413         {
12414         case 0:
12415           printf (_("Any\n"));
12416           break;
12417         case 1:
12418           printf (_("Generic\n"));
12419           break;
12420         case 2:
12421           printf ("AltiVec\n");
12422           break;
12423         case 3:
12424           printf ("SPE\n");
12425           break;
12426         default:
12427           printf ("??? (%d)\n", val);
12428           break;
12429         }
12430       return p;
12431    }
12432
12433   if (tag == Tag_GNU_Power_ABI_Struct_Return)
12434     {
12435       if (p == end)
12436         {
12437           warn (_("corrupt Tag_GNU_Power_ABI_Struct_Return\n"));
12438           return p;
12439         }
12440
12441       val = read_uleb128 (p, &len, end);
12442       p += len;
12443       printf ("  Tag_GNU_Power_ABI_Struct_Return: ");
12444       switch (val)
12445        {
12446        case 0:
12447          printf (_("Any\n"));
12448          break;
12449        case 1:
12450          printf ("r3/r4\n");
12451          break;
12452        case 2:
12453          printf (_("Memory\n"));
12454          break;
12455        default:
12456          printf ("??? (%d)\n", val);
12457          break;
12458        }
12459       return p;
12460     }
12461
12462   return display_tag_value (tag & 1, p, end);
12463 }
12464
12465 static void
12466 display_sparc_hwcaps (int mask)
12467 {
12468   if (mask)
12469     {
12470       int first = 1;
12471
12472       if (mask & ELF_SPARC_HWCAP_MUL32)
12473         fputs ("mul32", stdout), first = 0;
12474       if (mask & ELF_SPARC_HWCAP_DIV32)
12475         printf ("%sdiv32", first ? "" : "|"), first = 0;
12476       if (mask & ELF_SPARC_HWCAP_FSMULD)
12477         printf ("%sfsmuld", first ? "" : "|"), first = 0;
12478       if (mask & ELF_SPARC_HWCAP_V8PLUS)
12479         printf ("%sv8plus", first ? "" : "|"), first = 0;
12480       if (mask & ELF_SPARC_HWCAP_POPC)
12481         printf ("%spopc", first ? "" : "|"), first = 0;
12482       if (mask & ELF_SPARC_HWCAP_VIS)
12483         printf ("%svis", first ? "" : "|"), first = 0;
12484       if (mask & ELF_SPARC_HWCAP_VIS2)
12485         printf ("%svis2", first ? "" : "|"), first = 0;
12486       if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
12487         printf ("%sASIBlkInit", first ? "" : "|"), first = 0;
12488       if (mask & ELF_SPARC_HWCAP_FMAF)
12489         printf ("%sfmaf", first ? "" : "|"), first = 0;
12490       if (mask & ELF_SPARC_HWCAP_VIS3)
12491         printf ("%svis3", first ? "" : "|"), first = 0;
12492       if (mask & ELF_SPARC_HWCAP_HPC)
12493         printf ("%shpc", first ? "" : "|"), first = 0;
12494       if (mask & ELF_SPARC_HWCAP_RANDOM)
12495         printf ("%srandom", first ? "" : "|"), first = 0;
12496       if (mask & ELF_SPARC_HWCAP_TRANS)
12497         printf ("%strans", first ? "" : "|"), first = 0;
12498       if (mask & ELF_SPARC_HWCAP_FJFMAU)
12499         printf ("%sfjfmau", first ? "" : "|"), first = 0;
12500       if (mask & ELF_SPARC_HWCAP_IMA)
12501         printf ("%sima", first ? "" : "|"), first = 0;
12502       if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
12503         printf ("%scspare", first ? "" : "|"), first = 0;
12504     }
12505   else
12506     fputc ('0', stdout);
12507   fputc ('\n', stdout);
12508 }
12509
12510 static void
12511 display_sparc_hwcaps2 (int mask)
12512 {
12513   if (mask)
12514     {
12515       int first = 1;
12516
12517       if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
12518         fputs ("fjathplus", stdout), first = 0;
12519       if (mask & ELF_SPARC_HWCAP2_VIS3B)
12520         printf ("%svis3b", first ? "" : "|"), first = 0;
12521       if (mask & ELF_SPARC_HWCAP2_ADP)
12522         printf ("%sadp", first ? "" : "|"), first = 0;
12523       if (mask & ELF_SPARC_HWCAP2_SPARC5)
12524         printf ("%ssparc5", first ? "" : "|"), first = 0;
12525       if (mask & ELF_SPARC_HWCAP2_MWAIT)
12526         printf ("%smwait", first ? "" : "|"), first = 0;
12527       if (mask & ELF_SPARC_HWCAP2_XMPMUL)
12528         printf ("%sxmpmul", first ? "" : "|"), first = 0;
12529       if (mask & ELF_SPARC_HWCAP2_XMONT)
12530         printf ("%sxmont2", first ? "" : "|"), first = 0;
12531       if (mask & ELF_SPARC_HWCAP2_NSEC)
12532         printf ("%snsec", first ? "" : "|"), first = 0;
12533       if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
12534         printf ("%sfjathhpc", first ? "" : "|"), first = 0;
12535       if (mask & ELF_SPARC_HWCAP2_FJDES)
12536         printf ("%sfjdes", first ? "" : "|"), first = 0;
12537       if (mask & ELF_SPARC_HWCAP2_FJAES)
12538         printf ("%sfjaes", first ? "" : "|"), first = 0;
12539     }
12540   else
12541     fputc ('0', stdout);
12542   fputc ('\n', stdout);
12543 }
12544
12545 static unsigned char *
12546 display_sparc_gnu_attribute (unsigned char * p,
12547                              int tag,
12548                              const unsigned char * const end)
12549 {
12550   unsigned int len;
12551   int val;
12552
12553   if (tag == Tag_GNU_Sparc_HWCAPS)
12554     {
12555       val = read_uleb128 (p, &len, end);
12556       p += len;
12557       printf ("  Tag_GNU_Sparc_HWCAPS: ");
12558       display_sparc_hwcaps (val);
12559       return p;
12560     }
12561   if (tag == Tag_GNU_Sparc_HWCAPS2)
12562     {
12563       val = read_uleb128 (p, &len, end);
12564       p += len;
12565       printf ("  Tag_GNU_Sparc_HWCAPS2: ");
12566       display_sparc_hwcaps2 (val);
12567       return p;
12568     }
12569
12570   return display_tag_value (tag, p, end);
12571 }
12572
12573 static void
12574 print_mips_fp_abi_value (int val)
12575 {
12576   switch (val)
12577     {
12578     case Val_GNU_MIPS_ABI_FP_ANY:
12579       printf (_("Hard or soft float\n"));
12580       break;
12581     case Val_GNU_MIPS_ABI_FP_DOUBLE:
12582       printf (_("Hard float (double precision)\n"));
12583       break;
12584     case Val_GNU_MIPS_ABI_FP_SINGLE:
12585       printf (_("Hard float (single precision)\n"));
12586       break;
12587     case Val_GNU_MIPS_ABI_FP_SOFT:
12588       printf (_("Soft float\n"));
12589       break;
12590     case Val_GNU_MIPS_ABI_FP_OLD_64:
12591       printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
12592       break;
12593     case Val_GNU_MIPS_ABI_FP_XX:
12594       printf (_("Hard float (32-bit CPU, Any FPU)\n"));
12595       break;
12596     case Val_GNU_MIPS_ABI_FP_64:
12597       printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
12598       break;
12599     case Val_GNU_MIPS_ABI_FP_64A:
12600       printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
12601       break;
12602     default:
12603       printf ("??? (%d)\n", val);
12604       break;
12605     }
12606 }
12607
12608 static unsigned char *
12609 display_mips_gnu_attribute (unsigned char * p,
12610                             int tag,
12611                             const unsigned char * const end)
12612 {
12613   if (tag == Tag_GNU_MIPS_ABI_FP)
12614     {
12615       unsigned int len;
12616       int val;
12617
12618       val = read_uleb128 (p, &len, end);
12619       p += len;
12620       printf ("  Tag_GNU_MIPS_ABI_FP: ");
12621
12622       print_mips_fp_abi_value (val);
12623
12624       return p;
12625    }
12626
12627   if (tag == Tag_GNU_MIPS_ABI_MSA)
12628     {
12629       unsigned int len;
12630       int val;
12631
12632       val = read_uleb128 (p, &len, end);
12633       p += len;
12634       printf ("  Tag_GNU_MIPS_ABI_MSA: ");
12635
12636       switch (val)
12637         {
12638         case Val_GNU_MIPS_ABI_MSA_ANY:
12639           printf (_("Any MSA or not\n"));
12640           break;
12641         case Val_GNU_MIPS_ABI_MSA_128:
12642           printf (_("128-bit MSA\n"));
12643           break;
12644         default:
12645           printf ("??? (%d)\n", val);
12646           break;
12647         }
12648       return p;
12649     }
12650
12651   return display_tag_value (tag & 1, p, end);
12652 }
12653
12654 static unsigned char *
12655 display_tic6x_attribute (unsigned char * p,
12656                          const unsigned char * const end)
12657 {
12658   int tag;
12659   unsigned int len;
12660   int val;
12661
12662   tag = read_uleb128 (p, &len, end);
12663   p += len;
12664
12665   switch (tag)
12666     {
12667     case Tag_ISA:
12668       val = read_uleb128 (p, &len, end);
12669       p += len;
12670       printf ("  Tag_ISA: ");
12671
12672       switch (val)
12673         {
12674         case C6XABI_Tag_ISA_none:
12675           printf (_("None\n"));
12676           break;
12677         case C6XABI_Tag_ISA_C62X:
12678           printf ("C62x\n");
12679           break;
12680         case C6XABI_Tag_ISA_C67X:
12681           printf ("C67x\n");
12682           break;
12683         case C6XABI_Tag_ISA_C67XP:
12684           printf ("C67x+\n");
12685           break;
12686         case C6XABI_Tag_ISA_C64X:
12687           printf ("C64x\n");
12688           break;
12689         case C6XABI_Tag_ISA_C64XP:
12690           printf ("C64x+\n");
12691           break;
12692         case C6XABI_Tag_ISA_C674X:
12693           printf ("C674x\n");
12694           break;
12695         default:
12696           printf ("??? (%d)\n", val);
12697           break;
12698         }
12699       return p;
12700
12701     case Tag_ABI_wchar_t:
12702       val = read_uleb128 (p, &len, end);
12703       p += len;
12704       printf ("  Tag_ABI_wchar_t: ");
12705       switch (val)
12706         {
12707         case 0:
12708           printf (_("Not used\n"));
12709           break;
12710         case 1:
12711           printf (_("2 bytes\n"));
12712           break;
12713         case 2:
12714           printf (_("4 bytes\n"));
12715           break;
12716         default:
12717           printf ("??? (%d)\n", val);
12718           break;
12719         }
12720       return p;
12721
12722     case Tag_ABI_stack_align_needed:
12723       val = read_uleb128 (p, &len, end);
12724       p += len;
12725       printf ("  Tag_ABI_stack_align_needed: ");
12726       switch (val)
12727         {
12728         case 0:
12729           printf (_("8-byte\n"));
12730           break;
12731         case 1:
12732           printf (_("16-byte\n"));
12733           break;
12734         default:
12735           printf ("??? (%d)\n", val);
12736           break;
12737         }
12738       return p;
12739
12740     case Tag_ABI_stack_align_preserved:
12741       val = read_uleb128 (p, &len, end);
12742       p += len;
12743       printf ("  Tag_ABI_stack_align_preserved: ");
12744       switch (val)
12745         {
12746         case 0:
12747           printf (_("8-byte\n"));
12748           break;
12749         case 1:
12750           printf (_("16-byte\n"));
12751           break;
12752         default:
12753           printf ("??? (%d)\n", val);
12754           break;
12755         }
12756       return p;
12757
12758     case Tag_ABI_DSBT:
12759       val = read_uleb128 (p, &len, end);
12760       p += len;
12761       printf ("  Tag_ABI_DSBT: ");
12762       switch (val)
12763         {
12764         case 0:
12765           printf (_("DSBT addressing not used\n"));
12766           break;
12767         case 1:
12768           printf (_("DSBT addressing used\n"));
12769           break;
12770         default:
12771           printf ("??? (%d)\n", val);
12772           break;
12773         }
12774       return p;
12775
12776     case Tag_ABI_PID:
12777       val = read_uleb128 (p, &len, end);
12778       p += len;
12779       printf ("  Tag_ABI_PID: ");
12780       switch (val)
12781         {
12782         case 0:
12783           printf (_("Data addressing position-dependent\n"));
12784           break;
12785         case 1:
12786           printf (_("Data addressing position-independent, GOT near DP\n"));
12787           break;
12788         case 2:
12789           printf (_("Data addressing position-independent, GOT far from DP\n"));
12790           break;
12791         default:
12792           printf ("??? (%d)\n", val);
12793           break;
12794         }
12795       return p;
12796
12797     case Tag_ABI_PIC:
12798       val = read_uleb128 (p, &len, end);
12799       p += len;
12800       printf ("  Tag_ABI_PIC: ");
12801       switch (val)
12802         {
12803         case 0:
12804           printf (_("Code addressing position-dependent\n"));
12805           break;
12806         case 1:
12807           printf (_("Code addressing position-independent\n"));
12808           break;
12809         default:
12810           printf ("??? (%d)\n", val);
12811           break;
12812         }
12813       return p;
12814
12815     case Tag_ABI_array_object_alignment:
12816       val = read_uleb128 (p, &len, end);
12817       p += len;
12818       printf ("  Tag_ABI_array_object_alignment: ");
12819       switch (val)
12820         {
12821         case 0:
12822           printf (_("8-byte\n"));
12823           break;
12824         case 1:
12825           printf (_("4-byte\n"));
12826           break;
12827         case 2:
12828           printf (_("16-byte\n"));
12829           break;
12830         default:
12831           printf ("??? (%d)\n", val);
12832           break;
12833         }
12834       return p;
12835
12836     case Tag_ABI_array_object_align_expected:
12837       val = read_uleb128 (p, &len, end);
12838       p += len;
12839       printf ("  Tag_ABI_array_object_align_expected: ");
12840       switch (val)
12841         {
12842         case 0:
12843           printf (_("8-byte\n"));
12844           break;
12845         case 1:
12846           printf (_("4-byte\n"));
12847           break;
12848         case 2:
12849           printf (_("16-byte\n"));
12850           break;
12851         default:
12852           printf ("??? (%d)\n", val);
12853           break;
12854         }
12855       return p;
12856
12857     case Tag_ABI_compatibility:
12858       {
12859         val = read_uleb128 (p, &len, end);
12860         p += len;
12861         printf ("  Tag_ABI_compatibility: ");
12862         printf (_("flag = %d, vendor = "), val);
12863         if (p < end - 1)
12864           {
12865             size_t maxlen = (end - p) - 1;
12866
12867             print_symbol ((int) maxlen, (const char *) p);
12868             p += strnlen ((char *) p, maxlen) + 1;
12869           }
12870         else
12871           {
12872             printf (_("<corrupt>"));
12873             p = (unsigned char *) end;
12874           }
12875         putchar ('\n');
12876         return p;
12877       }
12878
12879     case Tag_ABI_conformance:
12880       {
12881         printf ("  Tag_ABI_conformance: \"");
12882         if (p < end - 1)
12883           {
12884             size_t maxlen = (end - p) - 1;
12885
12886             print_symbol ((int) maxlen, (const char *) p);
12887             p += strnlen ((char *) p, maxlen) + 1;
12888           }
12889         else
12890           {
12891             printf (_("<corrupt>"));
12892             p = (unsigned char *) end;
12893           }
12894         printf ("\"\n");
12895         return p;
12896       }
12897     }
12898
12899   return display_tag_value (tag, p, end);
12900 }
12901
12902 static void
12903 display_raw_attribute (unsigned char * p, unsigned char * end)
12904 {
12905   unsigned long addr = 0;
12906   size_t bytes = end - p;
12907
12908   assert (end > p);
12909   while (bytes)
12910     {
12911       int j;
12912       int k;
12913       int lbytes = (bytes > 16 ? 16 : bytes);
12914
12915       printf ("  0x%8.8lx ", addr);
12916
12917       for (j = 0; j < 16; j++)
12918         {
12919           if (j < lbytes)
12920             printf ("%2.2x", p[j]);
12921           else
12922             printf ("  ");
12923
12924           if ((j & 3) == 3)
12925             printf (" ");
12926         }
12927
12928       for (j = 0; j < lbytes; j++)
12929         {
12930           k = p[j];
12931           if (k >= ' ' && k < 0x7f)
12932             printf ("%c", k);
12933           else
12934             printf (".");
12935         }
12936
12937       putchar ('\n');
12938
12939       p  += lbytes;
12940       bytes -= lbytes;
12941       addr += lbytes;
12942     }
12943
12944   putchar ('\n');
12945 }
12946
12947 static unsigned char *
12948 display_msp430x_attribute (unsigned char * p,
12949                            const unsigned char * const end)
12950 {
12951   unsigned int len;
12952   int val;
12953   int tag;
12954
12955   tag = read_uleb128 (p, & len, end);
12956   p += len;
12957
12958   switch (tag)
12959     {
12960     case OFBA_MSPABI_Tag_ISA:
12961       val = read_uleb128 (p, &len, end);
12962       p += len;
12963       printf ("  Tag_ISA: ");
12964       switch (val)
12965         {
12966         case 0: printf (_("None\n")); break;
12967         case 1: printf (_("MSP430\n")); break;
12968         case 2: printf (_("MSP430X\n")); break;
12969         default: printf ("??? (%d)\n", val); break;
12970         }
12971       break;
12972
12973     case OFBA_MSPABI_Tag_Code_Model:
12974       val = read_uleb128 (p, &len, end);
12975       p += len;
12976       printf ("  Tag_Code_Model: ");
12977       switch (val)
12978         {
12979         case 0: printf (_("None\n")); break;
12980         case 1: printf (_("Small\n")); break;
12981         case 2: printf (_("Large\n")); break;
12982         default: printf ("??? (%d)\n", val); break;
12983         }
12984       break;
12985
12986     case OFBA_MSPABI_Tag_Data_Model:
12987       val = read_uleb128 (p, &len, end);
12988       p += len;
12989       printf ("  Tag_Data_Model: ");
12990       switch (val)
12991         {
12992         case 0: printf (_("None\n")); break;
12993         case 1: printf (_("Small\n")); break;
12994         case 2: printf (_("Large\n")); break;
12995         case 3: printf (_("Restricted Large\n")); break;
12996         default: printf ("??? (%d)\n", val); break;
12997         }
12998       break;
12999
13000     default:
13001       printf (_("  <unknown tag %d>: "), tag);
13002
13003       if (tag & 1)
13004         {
13005           putchar ('"');
13006           if (p < end - 1)
13007             {
13008               size_t maxlen = (end - p) - 1;
13009
13010               print_symbol ((int) maxlen, (const char *) p);
13011               p += strnlen ((char *) p, maxlen) + 1;
13012             }
13013           else
13014             {
13015               printf (_("<corrupt>"));
13016               p = (unsigned char *) end;
13017             }
13018           printf ("\"\n");
13019         }
13020       else
13021         {
13022           val = read_uleb128 (p, &len, end);
13023           p += len;
13024           printf ("%d (0x%x)\n", val, val);
13025         }
13026       break;
13027    }
13028
13029   assert (p <= end);
13030   return p;
13031 }
13032
13033 static int
13034 process_attributes (FILE * file,
13035                     const char * public_name,
13036                     unsigned int proc_type,
13037                     unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
13038                     unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int, const unsigned char * const))
13039 {
13040   Elf_Internal_Shdr * sect;
13041   unsigned i;
13042
13043   /* Find the section header so that we get the size.  */
13044   for (i = 0, sect = section_headers;
13045        i < elf_header.e_shnum;
13046        i++, sect++)
13047     {
13048       unsigned char * contents;
13049       unsigned char * p;
13050
13051       if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
13052         continue;
13053
13054       contents = (unsigned char *) get_data (NULL, file, sect->sh_offset, 1,
13055                                              sect->sh_size, _("attributes"));
13056       if (contents == NULL)
13057         continue;
13058
13059       p = contents;
13060       if (*p == 'A')
13061         {
13062           bfd_vma section_len;
13063
13064           section_len = sect->sh_size - 1;
13065           p++;
13066
13067           while (section_len > 0)
13068             {
13069               bfd_vma attr_len;
13070               unsigned int namelen;
13071               bfd_boolean public_section;
13072               bfd_boolean gnu_section;
13073
13074               if (section_len <= 4)
13075                 {
13076                   error (_("Tag section ends prematurely\n"));
13077                   break;
13078                 }
13079               attr_len = byte_get (p, 4);
13080               p += 4;
13081
13082               if (attr_len > section_len)
13083                 {
13084                   error (_("Bad attribute length (%u > %u)\n"),
13085                           (unsigned) attr_len, (unsigned) section_len);
13086                   attr_len = section_len;
13087                 }
13088               /* PR 17531: file: 001-101425-0.004  */
13089               else if (attr_len < 5)
13090                 {
13091                   error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
13092                   break;
13093                 }
13094
13095               section_len -= attr_len;
13096               attr_len -= 4;
13097
13098               namelen = strnlen ((char *) p, attr_len) + 1;
13099               if (namelen == 0 || namelen >= attr_len)
13100                 {
13101                   error (_("Corrupt attribute section name\n"));
13102                   break;
13103                 }
13104
13105               printf (_("Attribute Section: "));
13106               print_symbol (INT_MAX, (const char *) p);
13107               putchar ('\n');
13108
13109               if (public_name && streq ((char *) p, public_name))
13110                 public_section = TRUE;
13111               else
13112                 public_section = FALSE;
13113
13114               if (streq ((char *) p, "gnu"))
13115                 gnu_section = TRUE;
13116               else
13117                 gnu_section = FALSE;
13118
13119               p += namelen;
13120               attr_len -= namelen;
13121
13122               while (attr_len > 0 && p < contents + sect->sh_size)
13123                 {
13124                   int tag;
13125                   int val;
13126                   bfd_vma size;
13127                   unsigned char * end;
13128
13129                   /* PR binutils/17531: Safe handling of corrupt files.  */
13130                   if (attr_len < 6)
13131                     {
13132                       error (_("Unused bytes at end of section\n"));
13133                       section_len = 0;
13134                       break;
13135                     }
13136
13137                   tag = *(p++);
13138                   size = byte_get (p, 4);
13139                   if (size > attr_len)
13140                     {
13141                       error (_("Bad subsection length (%u > %u)\n"),
13142                               (unsigned) size, (unsigned) attr_len);
13143                       size = attr_len;
13144                     }
13145                   /* PR binutils/17531: Safe handling of corrupt files.  */
13146                   if (size < 6)
13147                     {
13148                       error (_("Bad subsection length (%u < 6)\n"),
13149                               (unsigned) size);
13150                       section_len = 0;
13151                       break;
13152                     }
13153
13154                   attr_len -= size;
13155                   end = p + size - 1;
13156                   assert (end <= contents + sect->sh_size);
13157                   p += 4;
13158
13159                   switch (tag)
13160                     {
13161                     case 1:
13162                       printf (_("File Attributes\n"));
13163                       break;
13164                     case 2:
13165                       printf (_("Section Attributes:"));
13166                       goto do_numlist;
13167                     case 3:
13168                       printf (_("Symbol Attributes:"));
13169                     do_numlist:
13170                       for (;;)
13171                         {
13172                           unsigned int j;
13173
13174                           val = read_uleb128 (p, &j, end);
13175                           p += j;
13176                           if (val == 0)
13177                             break;
13178                           printf (" %d", val);
13179                         }
13180                       printf ("\n");
13181                       break;
13182                     default:
13183                       printf (_("Unknown tag: %d\n"), tag);
13184                       public_section = FALSE;
13185                       break;
13186                     }
13187
13188                   if (public_section && display_pub_attribute != NULL)
13189                     {
13190                       while (p < end)
13191                         p = display_pub_attribute (p, end);
13192                       assert (p <= end);
13193                     }
13194                   else if (gnu_section && display_proc_gnu_attribute != NULL)
13195                     {
13196                       while (p < end)
13197                         p = display_gnu_attribute (p,
13198                                                    display_proc_gnu_attribute,
13199                                                    end);
13200                       assert (p <= end);
13201                     }
13202                   else if (p < end)
13203                     {
13204                       printf (_("  Unknown attribute:\n"));
13205                       display_raw_attribute (p, end);
13206                       p = end;
13207                     }
13208                   else
13209                     attr_len = 0;
13210                 }
13211             }
13212         }
13213       else
13214         printf (_("Unknown format '%c' (%d)\n"), *p, *p);
13215
13216       free (contents);
13217     }
13218   return 1;
13219 }
13220
13221 static int
13222 process_arm_specific (FILE * file)
13223 {
13224   return process_attributes (file, "aeabi", SHT_ARM_ATTRIBUTES,
13225                              display_arm_attribute, NULL);
13226 }
13227
13228 static int
13229 process_power_specific (FILE * file)
13230 {
13231   return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
13232                              display_power_gnu_attribute);
13233 }
13234
13235 static int
13236 process_sparc_specific (FILE * file)
13237 {
13238   return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
13239                              display_sparc_gnu_attribute);
13240 }
13241
13242 static int
13243 process_tic6x_specific (FILE * file)
13244 {
13245   return process_attributes (file, "c6xabi", SHT_C6000_ATTRIBUTES,
13246                              display_tic6x_attribute, NULL);
13247 }
13248
13249 static int
13250 process_msp430x_specific (FILE * file)
13251 {
13252   return process_attributes (file, "mspabi", SHT_MSP430_ATTRIBUTES,
13253                              display_msp430x_attribute, NULL);
13254 }
13255
13256 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
13257    Print the Address, Access and Initial fields of an entry at VMA ADDR
13258    and return the VMA of the next entry, or -1 if there was a problem.
13259    Does not read from DATA_END or beyond.  */
13260
13261 static bfd_vma
13262 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
13263                       unsigned char * data_end)
13264 {
13265   printf ("  ");
13266   print_vma (addr, LONG_HEX);
13267   printf (" ");
13268   if (addr < pltgot + 0xfff0)
13269     printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
13270   else
13271     printf ("%10s", "");
13272   printf (" ");
13273   if (data == NULL)
13274     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
13275   else
13276     {
13277       bfd_vma entry;
13278       unsigned char * from = data + addr - pltgot;
13279
13280       if (from + (is_32bit_elf ? 4 : 8) > data_end)
13281         {
13282           warn (_("MIPS GOT entry extends beyond the end of available data\n"));
13283           printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
13284           return (bfd_vma) -1;
13285         }
13286       else
13287         {
13288           entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
13289           print_vma (entry, LONG_HEX);
13290         }
13291     }
13292   return addr + (is_32bit_elf ? 4 : 8);
13293 }
13294
13295 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
13296    PLTGOT.  Print the Address and Initial fields of an entry at VMA
13297    ADDR and return the VMA of the next entry.  */
13298
13299 static bfd_vma
13300 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
13301 {
13302   printf ("  ");
13303   print_vma (addr, LONG_HEX);
13304   printf (" ");
13305   if (data == NULL)
13306     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
13307   else
13308     {
13309       bfd_vma entry;
13310
13311       entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
13312       print_vma (entry, LONG_HEX);
13313     }
13314   return addr + (is_32bit_elf ? 4 : 8);
13315 }
13316
13317 static void
13318 print_mips_ases (unsigned int mask)
13319 {
13320   if (mask & AFL_ASE_DSP)
13321     fputs ("\n\tDSP ASE", stdout);
13322   if (mask & AFL_ASE_DSPR2)
13323     fputs ("\n\tDSP R2 ASE", stdout);
13324   if (mask & AFL_ASE_EVA)
13325     fputs ("\n\tEnhanced VA Scheme", stdout);
13326   if (mask & AFL_ASE_MCU)
13327     fputs ("\n\tMCU (MicroController) ASE", stdout);
13328   if (mask & AFL_ASE_MDMX)
13329     fputs ("\n\tMDMX ASE", stdout);
13330   if (mask & AFL_ASE_MIPS3D)
13331     fputs ("\n\tMIPS-3D ASE", stdout);
13332   if (mask & AFL_ASE_MT)
13333     fputs ("\n\tMT ASE", stdout);
13334   if (mask & AFL_ASE_SMARTMIPS)
13335     fputs ("\n\tSmartMIPS ASE", stdout);
13336   if (mask & AFL_ASE_VIRT)
13337     fputs ("\n\tVZ ASE", stdout);
13338   if (mask & AFL_ASE_MSA)
13339     fputs ("\n\tMSA ASE", stdout);
13340   if (mask & AFL_ASE_MIPS16)
13341     fputs ("\n\tMIPS16 ASE", stdout);
13342   if (mask & AFL_ASE_MICROMIPS)
13343     fputs ("\n\tMICROMIPS ASE", stdout);
13344   if (mask & AFL_ASE_XPA)
13345     fputs ("\n\tXPA ASE", stdout);
13346   if (mask == 0)
13347     fprintf (stdout, "\n\t%s", _("None"));
13348   else if ((mask & ~AFL_ASE_MASK) != 0)
13349     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
13350 }
13351
13352 static void
13353 print_mips_isa_ext (unsigned int isa_ext)
13354 {
13355   switch (isa_ext)
13356     {
13357     case 0:
13358       fputs (_("None"), stdout);
13359       break;
13360     case AFL_EXT_XLR:
13361       fputs ("RMI XLR", stdout);
13362       break;
13363     case AFL_EXT_OCTEON2:
13364       fputs ("Cavium Networks Octeon2", stdout);
13365       break;
13366     case AFL_EXT_OCTEONP:
13367       fputs ("Cavium Networks OcteonP", stdout);
13368       break;
13369     case AFL_EXT_LOONGSON_3A:
13370       fputs ("Loongson 3A", stdout);
13371       break;
13372     case AFL_EXT_OCTEON:
13373       fputs ("Cavium Networks Octeon", stdout);
13374       break;
13375     case AFL_EXT_5900:
13376       fputs ("Toshiba R5900", stdout);
13377       break;
13378     case AFL_EXT_4650:
13379       fputs ("MIPS R4650", stdout);
13380       break;
13381     case AFL_EXT_4010:
13382       fputs ("LSI R4010", stdout);
13383       break;
13384     case AFL_EXT_4100:
13385       fputs ("NEC VR4100", stdout);
13386       break;
13387     case AFL_EXT_3900:
13388       fputs ("Toshiba R3900", stdout);
13389       break;
13390     case AFL_EXT_10000:
13391       fputs ("MIPS R10000", stdout);
13392       break;
13393     case AFL_EXT_SB1:
13394       fputs ("Broadcom SB-1", stdout);
13395       break;
13396     case AFL_EXT_4111:
13397       fputs ("NEC VR4111/VR4181", stdout);
13398       break;
13399     case AFL_EXT_4120:
13400       fputs ("NEC VR4120", stdout);
13401       break;
13402     case AFL_EXT_5400:
13403       fputs ("NEC VR5400", stdout);
13404       break;
13405     case AFL_EXT_5500:
13406       fputs ("NEC VR5500", stdout);
13407       break;
13408     case AFL_EXT_LOONGSON_2E:
13409       fputs ("ST Microelectronics Loongson 2E", stdout);
13410       break;
13411     case AFL_EXT_LOONGSON_2F:
13412       fputs ("ST Microelectronics Loongson 2F", stdout);
13413       break;
13414     default:
13415       fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
13416     }
13417 }
13418
13419 static int
13420 get_mips_reg_size (int reg_size)
13421 {
13422   return (reg_size == AFL_REG_NONE) ? 0
13423          : (reg_size == AFL_REG_32) ? 32
13424          : (reg_size == AFL_REG_64) ? 64
13425          : (reg_size == AFL_REG_128) ? 128
13426          : -1;
13427 }
13428
13429 static int
13430 process_mips_specific (FILE * file)
13431 {
13432   Elf_Internal_Dyn * entry;
13433   Elf_Internal_Shdr *sect = NULL;
13434   size_t liblist_offset = 0;
13435   size_t liblistno = 0;
13436   size_t conflictsno = 0;
13437   size_t options_offset = 0;
13438   size_t conflicts_offset = 0;
13439   size_t pltrelsz = 0;
13440   size_t pltrel = 0;
13441   bfd_vma pltgot = 0;
13442   bfd_vma mips_pltgot = 0;
13443   bfd_vma jmprel = 0;
13444   bfd_vma local_gotno = 0;
13445   bfd_vma gotsym = 0;
13446   bfd_vma symtabno = 0;
13447
13448   process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
13449                       display_mips_gnu_attribute);
13450
13451   sect = find_section (".MIPS.abiflags");
13452
13453   if (sect != NULL)
13454     {
13455       Elf_External_ABIFlags_v0 *abiflags_ext;
13456       Elf_Internal_ABIFlags_v0 abiflags_in;
13457
13458       if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
13459         fputs ("\nCorrupt ABI Flags section.\n", stdout);
13460       else
13461         {
13462           abiflags_ext = get_data (NULL, file, sect->sh_offset, 1,
13463                                    sect->sh_size, _("MIPS ABI Flags section"));
13464           if (abiflags_ext)
13465             {
13466               abiflags_in.version = BYTE_GET (abiflags_ext->version);
13467               abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
13468               abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
13469               abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
13470               abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
13471               abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
13472               abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
13473               abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
13474               abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
13475               abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
13476               abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
13477
13478               printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
13479               printf ("\nISA: MIPS%d", abiflags_in.isa_level);
13480               if (abiflags_in.isa_rev > 1)
13481                 printf ("r%d", abiflags_in.isa_rev);
13482               printf ("\nGPR size: %d",
13483                       get_mips_reg_size (abiflags_in.gpr_size));
13484               printf ("\nCPR1 size: %d",
13485                       get_mips_reg_size (abiflags_in.cpr1_size));
13486               printf ("\nCPR2 size: %d",
13487                       get_mips_reg_size (abiflags_in.cpr2_size));
13488               fputs ("\nFP ABI: ", stdout);
13489               print_mips_fp_abi_value (abiflags_in.fp_abi);
13490               fputs ("ISA Extension: ", stdout);
13491               print_mips_isa_ext (abiflags_in.isa_ext);
13492               fputs ("\nASEs:", stdout);
13493               print_mips_ases (abiflags_in.ases);
13494               printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
13495               printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
13496               fputc ('\n', stdout);
13497               free (abiflags_ext);
13498             }
13499         }
13500     }
13501
13502   /* We have a lot of special sections.  Thanks SGI!  */
13503   if (dynamic_section == NULL)
13504     /* No information available.  */
13505     return 0;
13506
13507   for (entry = dynamic_section;
13508        /* PR 17531 file: 012-50589-0.004.  */
13509        entry < dynamic_section + dynamic_nent && entry->d_tag != DT_NULL;
13510        ++entry)
13511     switch (entry->d_tag)
13512       {
13513       case DT_MIPS_LIBLIST:
13514         liblist_offset
13515           = offset_from_vma (file, entry->d_un.d_val,
13516                              liblistno * sizeof (Elf32_External_Lib));
13517         break;
13518       case DT_MIPS_LIBLISTNO:
13519         liblistno = entry->d_un.d_val;
13520         break;
13521       case DT_MIPS_OPTIONS:
13522         options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
13523         break;
13524       case DT_MIPS_CONFLICT:
13525         conflicts_offset
13526           = offset_from_vma (file, entry->d_un.d_val,
13527                              conflictsno * sizeof (Elf32_External_Conflict));
13528         break;
13529       case DT_MIPS_CONFLICTNO:
13530         conflictsno = entry->d_un.d_val;
13531         break;
13532       case DT_PLTGOT:
13533         pltgot = entry->d_un.d_ptr;
13534         break;
13535       case DT_MIPS_LOCAL_GOTNO:
13536         local_gotno = entry->d_un.d_val;
13537         break;
13538       case DT_MIPS_GOTSYM:
13539         gotsym = entry->d_un.d_val;
13540         break;
13541       case DT_MIPS_SYMTABNO:
13542         symtabno = entry->d_un.d_val;
13543         break;
13544       case DT_MIPS_PLTGOT:
13545         mips_pltgot = entry->d_un.d_ptr;
13546         break;
13547       case DT_PLTREL:
13548         pltrel = entry->d_un.d_val;
13549         break;
13550       case DT_PLTRELSZ:
13551         pltrelsz = entry->d_un.d_val;
13552         break;
13553       case DT_JMPREL:
13554         jmprel = entry->d_un.d_ptr;
13555         break;
13556       default:
13557         break;
13558       }
13559
13560   if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
13561     {
13562       Elf32_External_Lib * elib;
13563       size_t cnt;
13564
13565       elib = (Elf32_External_Lib *) get_data (NULL, file, liblist_offset,
13566                                               liblistno,
13567                                               sizeof (Elf32_External_Lib),
13568                                               _("liblist section data"));
13569       if (elib)
13570         {
13571           printf (_("\nSection '.liblist' contains %lu entries:\n"),
13572                   (unsigned long) liblistno);
13573           fputs (_("     Library              Time Stamp          Checksum   Version Flags\n"),
13574                  stdout);
13575
13576           for (cnt = 0; cnt < liblistno; ++cnt)
13577             {
13578               Elf32_Lib liblist;
13579               time_t atime;
13580               char timebuf[20];
13581               struct tm * tmp;
13582
13583               liblist.l_name = BYTE_GET (elib[cnt].l_name);
13584               atime = BYTE_GET (elib[cnt].l_time_stamp);
13585               liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
13586               liblist.l_version = BYTE_GET (elib[cnt].l_version);
13587               liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
13588
13589               tmp = gmtime (&atime);
13590               snprintf (timebuf, sizeof (timebuf),
13591                         "%04u-%02u-%02uT%02u:%02u:%02u",
13592                         tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
13593                         tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
13594
13595               printf ("%3lu: ", (unsigned long) cnt);
13596               if (VALID_DYNAMIC_NAME (liblist.l_name))
13597                 print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
13598               else
13599                 printf (_("<corrupt: %9ld>"), liblist.l_name);
13600               printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
13601                       liblist.l_version);
13602
13603               if (liblist.l_flags == 0)
13604                 puts (_(" NONE"));
13605               else
13606                 {
13607                   static const struct
13608                   {
13609                     const char * name;
13610                     int bit;
13611                   }
13612                   l_flags_vals[] =
13613                   {
13614                     { " EXACT_MATCH", LL_EXACT_MATCH },
13615                     { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
13616                     { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
13617                     { " EXPORTS", LL_EXPORTS },
13618                     { " DELAY_LOAD", LL_DELAY_LOAD },
13619                     { " DELTA", LL_DELTA }
13620                   };
13621                   int flags = liblist.l_flags;
13622                   size_t fcnt;
13623
13624                   for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
13625                     if ((flags & l_flags_vals[fcnt].bit) != 0)
13626                       {
13627                         fputs (l_flags_vals[fcnt].name, stdout);
13628                         flags ^= l_flags_vals[fcnt].bit;
13629                       }
13630                   if (flags != 0)
13631                     printf (" %#x", (unsigned int) flags);
13632
13633                   puts ("");
13634                 }
13635             }
13636
13637           free (elib);
13638         }
13639     }
13640
13641   if (options_offset != 0)
13642     {
13643       Elf_External_Options * eopt;
13644       Elf_Internal_Options * iopt;
13645       Elf_Internal_Options * option;
13646       size_t offset;
13647       int cnt;
13648       sect = section_headers;
13649
13650       /* Find the section header so that we get the size.  */
13651       sect = find_section_by_type (SHT_MIPS_OPTIONS);
13652       /* PR 17533 file: 012-277276-0.004.  */
13653       if (sect == NULL)
13654         {
13655           error (_("No MIPS_OPTIONS header found\n"));
13656           return 0;
13657         }
13658
13659       eopt = (Elf_External_Options *) get_data (NULL, file, options_offset, 1,
13660                                                 sect->sh_size, _("options"));
13661       if (eopt)
13662         {
13663           iopt = (Elf_Internal_Options *)
13664               cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (* iopt));
13665           if (iopt == NULL)
13666             {
13667               error (_("Out of memory allocatinf space for MIPS options\n"));
13668               return 0;
13669             }
13670
13671           offset = cnt = 0;
13672           option = iopt;
13673
13674           while (offset <= sect->sh_size - sizeof (* eopt))
13675             {
13676               Elf_External_Options * eoption;
13677
13678               eoption = (Elf_External_Options *) ((char *) eopt + offset);
13679
13680               option->kind = BYTE_GET (eoption->kind);
13681               option->size = BYTE_GET (eoption->size);
13682               option->section = BYTE_GET (eoption->section);
13683               option->info = BYTE_GET (eoption->info);
13684
13685               /* PR 17531: file: ffa0fa3b.  */
13686               if (option->size < sizeof (* eopt)
13687                   || offset + option->size > sect->sh_size)
13688                 {
13689                   error (_("Invalid size (%u) for MIPS option\n"), option->size);
13690                   return 0;
13691                 }
13692               offset += option->size;
13693
13694               ++option;
13695               ++cnt;
13696             }
13697
13698           printf (_("\nSection '%s' contains %d entries:\n"),
13699                   printable_section_name (sect), cnt);
13700
13701           option = iopt;
13702           offset = 0;
13703
13704           while (cnt-- > 0)
13705             {
13706               size_t len;
13707
13708               switch (option->kind)
13709                 {
13710                 case ODK_NULL:
13711                   /* This shouldn't happen.  */
13712                   printf (" NULL       %d %lx", option->section, option->info);
13713                   break;
13714                 case ODK_REGINFO:
13715                   printf (" REGINFO    ");
13716                   if (elf_header.e_machine == EM_MIPS)
13717                     {
13718                       /* 32bit form.  */
13719                       Elf32_External_RegInfo * ereg;
13720                       Elf32_RegInfo reginfo;
13721
13722                       ereg = (Elf32_External_RegInfo *) (option + 1);
13723                       reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
13724                       reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
13725                       reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
13726                       reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
13727                       reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
13728                       reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
13729
13730                       printf ("GPR %08lx  GP 0x%lx\n",
13731                               reginfo.ri_gprmask,
13732                               (unsigned long) reginfo.ri_gp_value);
13733                       printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
13734                               reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
13735                               reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
13736                     }
13737                   else
13738                     {
13739                       /* 64 bit form.  */
13740                       Elf64_External_RegInfo * ereg;
13741                       Elf64_Internal_RegInfo reginfo;
13742
13743                       ereg = (Elf64_External_RegInfo *) (option + 1);
13744                       reginfo.ri_gprmask    = BYTE_GET (ereg->ri_gprmask);
13745                       reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
13746                       reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
13747                       reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
13748                       reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
13749                       reginfo.ri_gp_value   = BYTE_GET (ereg->ri_gp_value);
13750
13751                       printf ("GPR %08lx  GP 0x",
13752                               reginfo.ri_gprmask);
13753                       printf_vma (reginfo.ri_gp_value);
13754                       printf ("\n");
13755
13756                       printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
13757                               reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
13758                               reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
13759                     }
13760                   ++option;
13761                   continue;
13762                 case ODK_EXCEPTIONS:
13763                   fputs (" EXCEPTIONS fpe_min(", stdout);
13764                   process_mips_fpe_exception (option->info & OEX_FPU_MIN);
13765                   fputs (") fpe_max(", stdout);
13766                   process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
13767                   fputs (")", stdout);
13768
13769                   if (option->info & OEX_PAGE0)
13770                     fputs (" PAGE0", stdout);
13771                   if (option->info & OEX_SMM)
13772                     fputs (" SMM", stdout);
13773                   if (option->info & OEX_FPDBUG)
13774                     fputs (" FPDBUG", stdout);
13775                   if (option->info & OEX_DISMISS)
13776                     fputs (" DISMISS", stdout);
13777                   break;
13778                 case ODK_PAD:
13779                   fputs (" PAD       ", stdout);
13780                   if (option->info & OPAD_PREFIX)
13781                     fputs (" PREFIX", stdout);
13782                   if (option->info & OPAD_POSTFIX)
13783                     fputs (" POSTFIX", stdout);
13784                   if (option->info & OPAD_SYMBOL)
13785                     fputs (" SYMBOL", stdout);
13786                   break;
13787                 case ODK_HWPATCH:
13788                   fputs (" HWPATCH   ", stdout);
13789                   if (option->info & OHW_R4KEOP)
13790                     fputs (" R4KEOP", stdout);
13791                   if (option->info & OHW_R8KPFETCH)
13792                     fputs (" R8KPFETCH", stdout);
13793                   if (option->info & OHW_R5KEOP)
13794                     fputs (" R5KEOP", stdout);
13795                   if (option->info & OHW_R5KCVTL)
13796                     fputs (" R5KCVTL", stdout);
13797                   break;
13798                 case ODK_FILL:
13799                   fputs (" FILL       ", stdout);
13800                   /* XXX Print content of info word?  */
13801                   break;
13802                 case ODK_TAGS:
13803                   fputs (" TAGS       ", stdout);
13804                   /* XXX Print content of info word?  */
13805                   break;
13806                 case ODK_HWAND:
13807                   fputs (" HWAND     ", stdout);
13808                   if (option->info & OHWA0_R4KEOP_CHECKED)
13809                     fputs (" R4KEOP_CHECKED", stdout);
13810                   if (option->info & OHWA0_R4KEOP_CLEAN)
13811                     fputs (" R4KEOP_CLEAN", stdout);
13812                   break;
13813                 case ODK_HWOR:
13814                   fputs (" HWOR      ", stdout);
13815                   if (option->info & OHWA0_R4KEOP_CHECKED)
13816                     fputs (" R4KEOP_CHECKED", stdout);
13817                   if (option->info & OHWA0_R4KEOP_CLEAN)
13818                     fputs (" R4KEOP_CLEAN", stdout);
13819                   break;
13820                 case ODK_GP_GROUP:
13821                   printf (" GP_GROUP  %#06lx  self-contained %#06lx",
13822                           option->info & OGP_GROUP,
13823                           (option->info & OGP_SELF) >> 16);
13824                   break;
13825                 case ODK_IDENT:
13826                   printf (" IDENT     %#06lx  self-contained %#06lx",
13827                           option->info & OGP_GROUP,
13828                           (option->info & OGP_SELF) >> 16);
13829                   break;
13830                 default:
13831                   /* This shouldn't happen.  */
13832                   printf (" %3d ???     %d %lx",
13833                           option->kind, option->section, option->info);
13834                   break;
13835                 }
13836
13837               len = sizeof (* eopt);
13838               while (len < option->size)
13839                 {
13840                   char datum = * ((char *) eopt + offset + len);
13841
13842                   if (ISPRINT (datum))
13843                     printf ("%c", datum);
13844                   else
13845                     printf ("\\%03o", datum);
13846                   len ++;
13847                 }
13848
13849               fputs ("\n", stdout);
13850               offset += option->size;
13851               ++option;
13852             }
13853
13854           free (eopt);
13855         }
13856     }
13857
13858   if (conflicts_offset != 0 && conflictsno != 0)
13859     {
13860       Elf32_Conflict * iconf;
13861       size_t cnt;
13862
13863       if (dynamic_symbols == NULL)
13864         {
13865           error (_("conflict list found without a dynamic symbol table\n"));
13866           return 0;
13867         }
13868
13869       iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
13870       if (iconf == NULL)
13871         {
13872           error (_("Out of memory allocating space for dynamic conflicts\n"));
13873           return 0;
13874         }
13875
13876       if (is_32bit_elf)
13877         {
13878           Elf32_External_Conflict * econf32;
13879
13880           econf32 = (Elf32_External_Conflict *)
13881               get_data (NULL, file, conflicts_offset, conflictsno,
13882                         sizeof (* econf32), _("conflict"));
13883           if (!econf32)
13884             return 0;
13885
13886           for (cnt = 0; cnt < conflictsno; ++cnt)
13887             iconf[cnt] = BYTE_GET (econf32[cnt]);
13888
13889           free (econf32);
13890         }
13891       else
13892         {
13893           Elf64_External_Conflict * econf64;
13894
13895           econf64 = (Elf64_External_Conflict *)
13896               get_data (NULL, file, conflicts_offset, conflictsno,
13897                         sizeof (* econf64), _("conflict"));
13898           if (!econf64)
13899             return 0;
13900
13901           for (cnt = 0; cnt < conflictsno; ++cnt)
13902             iconf[cnt] = BYTE_GET (econf64[cnt]);
13903
13904           free (econf64);
13905         }
13906
13907       printf (_("\nSection '.conflict' contains %lu entries:\n"),
13908               (unsigned long) conflictsno);
13909       puts (_("  Num:    Index       Value  Name"));
13910
13911       for (cnt = 0; cnt < conflictsno; ++cnt)
13912         {
13913           printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
13914
13915           if (iconf[cnt] >= num_dynamic_syms)
13916             printf (_("<corrupt symbol index>"));
13917           else
13918             {
13919               Elf_Internal_Sym * psym;
13920
13921               psym = & dynamic_symbols[iconf[cnt]];
13922               print_vma (psym->st_value, FULL_HEX);
13923               putchar (' ');
13924               if (VALID_DYNAMIC_NAME (psym->st_name))
13925                 print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
13926               else
13927                 printf (_("<corrupt: %14ld>"), psym->st_name);
13928             }
13929           putchar ('\n');
13930         }
13931
13932       free (iconf);
13933     }
13934
13935   if (pltgot != 0 && local_gotno != 0)
13936     {
13937       bfd_vma ent, local_end, global_end;
13938       size_t i, offset;
13939       unsigned char * data;
13940       unsigned char * data_end;
13941       int addr_size;
13942
13943       ent = pltgot;
13944       addr_size = (is_32bit_elf ? 4 : 8);
13945       local_end = pltgot + local_gotno * addr_size;
13946
13947       /* PR binutils/17533 file: 012-111227-0.004  */
13948       if (symtabno < gotsym)
13949         {
13950           error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
13951                  (unsigned long) gotsym, (unsigned long) symtabno);
13952           return 0;
13953         }
13954
13955       global_end = local_end + (symtabno - gotsym) * addr_size;
13956       /* PR 17531: file: 54c91a34.  */
13957       if (global_end < local_end)
13958         {
13959           error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
13960           return 0;
13961         }
13962
13963       offset = offset_from_vma (file, pltgot, global_end - pltgot);
13964       data = (unsigned char *) get_data (NULL, file, offset,
13965                                          global_end - pltgot, 1,
13966                                          _("Global Offset Table data"));
13967       if (data == NULL)
13968         return 0;
13969       data_end = data + (global_end - pltgot);
13970
13971       printf (_("\nPrimary GOT:\n"));
13972       printf (_(" Canonical gp value: "));
13973       print_vma (pltgot + 0x7ff0, LONG_HEX);
13974       printf ("\n\n");
13975
13976       printf (_(" Reserved entries:\n"));
13977       printf (_("  %*s %10s %*s Purpose\n"),
13978               addr_size * 2, _("Address"), _("Access"),
13979               addr_size * 2, _("Initial"));
13980       ent = print_mips_got_entry (data, pltgot, ent, data_end);
13981       printf (_(" Lazy resolver\n"));
13982       if (ent == (bfd_vma) -1)
13983         goto got_print_fail;
13984       if (data
13985           && (byte_get (data + ent - pltgot, addr_size)
13986               >> (addr_size * 8 - 1)) != 0)
13987         {
13988           ent = print_mips_got_entry (data, pltgot, ent, data_end);
13989           printf (_(" Module pointer (GNU extension)\n"));
13990           if (ent == (bfd_vma) -1)
13991             goto got_print_fail;
13992         }
13993       printf ("\n");
13994
13995       if (ent < local_end)
13996         {
13997           printf (_(" Local entries:\n"));
13998           printf ("  %*s %10s %*s\n",
13999                   addr_size * 2, _("Address"), _("Access"),
14000                   addr_size * 2, _("Initial"));
14001           while (ent < local_end)
14002             {
14003               ent = print_mips_got_entry (data, pltgot, ent, data_end);
14004               printf ("\n");
14005               if (ent == (bfd_vma) -1)
14006                 goto got_print_fail;
14007             }
14008           printf ("\n");
14009         }
14010
14011       if (gotsym < symtabno)
14012         {
14013           int sym_width;
14014
14015           printf (_(" Global entries:\n"));
14016           printf ("  %*s %10s %*s %*s %-7s %3s %s\n",
14017                   addr_size * 2, _("Address"),
14018                   _("Access"),
14019                   addr_size * 2, _("Initial"),
14020                   addr_size * 2, _("Sym.Val."),
14021                   _("Type"),
14022                   /* Note for translators: "Ndx" = abbreviated form of "Index".  */
14023                   _("Ndx"), _("Name"));
14024
14025           sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
14026
14027           for (i = gotsym; i < symtabno; i++)
14028             {
14029               ent = print_mips_got_entry (data, pltgot, ent, data_end);
14030               printf (" ");
14031
14032               if (dynamic_symbols == NULL)
14033                 printf (_("<no dynamic symbols>"));
14034               else if (i < num_dynamic_syms)
14035                 {
14036                   Elf_Internal_Sym * psym = dynamic_symbols + i;
14037
14038                   print_vma (psym->st_value, LONG_HEX);
14039                   printf (" %-7s %3s ",
14040                           get_symbol_type (ELF_ST_TYPE (psym->st_info)),
14041                           get_symbol_index_type (psym->st_shndx));
14042
14043                   if (VALID_DYNAMIC_NAME (psym->st_name))
14044                     print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
14045                   else
14046                     printf (_("<corrupt: %14ld>"), psym->st_name);
14047                 }
14048               else
14049                 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
14050                         (unsigned long) i);
14051
14052               printf ("\n");
14053               if (ent == (bfd_vma) -1)
14054                 break;
14055             }
14056           printf ("\n");
14057         }
14058
14059     got_print_fail:
14060       if (data)
14061         free (data);
14062     }
14063
14064   if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
14065     {
14066       bfd_vma ent, end;
14067       size_t offset, rel_offset;
14068       unsigned long count, i;
14069       unsigned char * data;
14070       int addr_size, sym_width;
14071       Elf_Internal_Rela * rels;
14072
14073       rel_offset = offset_from_vma (file, jmprel, pltrelsz);
14074       if (pltrel == DT_RELA)
14075         {
14076           if (!slurp_rela_relocs (file, rel_offset, pltrelsz, &rels, &count))
14077             return 0;
14078         }
14079       else
14080         {
14081           if (!slurp_rel_relocs (file, rel_offset, pltrelsz, &rels, &count))
14082             return 0;
14083         }
14084
14085       ent = mips_pltgot;
14086       addr_size = (is_32bit_elf ? 4 : 8);
14087       end = mips_pltgot + (2 + count) * addr_size;
14088
14089       offset = offset_from_vma (file, mips_pltgot, end - mips_pltgot);
14090       data = (unsigned char *) get_data (NULL, file, offset, end - mips_pltgot,
14091                                          1, _("Procedure Linkage Table data"));
14092       if (data == NULL)
14093         return 0;
14094
14095       printf ("\nPLT GOT:\n\n");
14096       printf (_(" Reserved entries:\n"));
14097       printf (_("  %*s %*s Purpose\n"),
14098               addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
14099       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
14100       printf (_(" PLT lazy resolver\n"));
14101       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
14102       printf (_(" Module pointer\n"));
14103       printf ("\n");
14104
14105       printf (_(" Entries:\n"));
14106       printf ("  %*s %*s %*s %-7s %3s %s\n",
14107               addr_size * 2, _("Address"),
14108               addr_size * 2, _("Initial"),
14109               addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
14110       sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
14111       for (i = 0; i < count; i++)
14112         {
14113           unsigned long idx = get_reloc_symindex (rels[i].r_info);
14114
14115           ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
14116           printf (" ");
14117
14118           if (idx >= num_dynamic_syms)
14119             printf (_("<corrupt symbol index: %lu>"), idx);
14120           else
14121             {
14122               Elf_Internal_Sym * psym = dynamic_symbols + idx;
14123
14124               print_vma (psym->st_value, LONG_HEX);
14125               printf (" %-7s %3s ",
14126                       get_symbol_type (ELF_ST_TYPE (psym->st_info)),
14127                       get_symbol_index_type (psym->st_shndx));
14128               if (VALID_DYNAMIC_NAME (psym->st_name))
14129                 print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
14130               else
14131                 printf (_("<corrupt: %14ld>"), psym->st_name);
14132             }
14133           printf ("\n");
14134         }
14135       printf ("\n");
14136
14137       if (data)
14138         free (data);
14139       free (rels);
14140     }
14141
14142   return 1;
14143 }
14144
14145 static int
14146 process_nds32_specific (FILE * file)
14147 {
14148   Elf_Internal_Shdr *sect = NULL;
14149
14150   sect = find_section (".nds32_e_flags");
14151   if (sect != NULL)
14152     {
14153       unsigned int *flag;
14154
14155       printf ("\nNDS32 elf flags section:\n");
14156       flag = get_data (NULL, file, sect->sh_offset, 1,
14157                        sect->sh_size, _("NDS32 elf flags section"));
14158
14159       switch ((*flag) & 0x3)
14160         {
14161         case 0:
14162           printf ("(VEC_SIZE):\tNo entry.\n");
14163           break;
14164         case 1:
14165           printf ("(VEC_SIZE):\t4 bytes\n");
14166           break;
14167         case 2:
14168           printf ("(VEC_SIZE):\t16 bytes\n");
14169           break;
14170         case 3:
14171           printf ("(VEC_SIZE):\treserved\n");
14172           break;
14173         }
14174     }
14175
14176   return TRUE;
14177 }
14178
14179 static int
14180 process_gnu_liblist (FILE * file)
14181 {
14182   Elf_Internal_Shdr * section;
14183   Elf_Internal_Shdr * string_sec;
14184   Elf32_External_Lib * elib;
14185   char * strtab;
14186   size_t strtab_size;
14187   size_t cnt;
14188   unsigned i;
14189
14190   if (! do_arch)
14191     return 0;
14192
14193   for (i = 0, section = section_headers;
14194        i < elf_header.e_shnum;
14195        i++, section++)
14196     {
14197       switch (section->sh_type)
14198         {
14199         case SHT_GNU_LIBLIST:
14200           if (section->sh_link >= elf_header.e_shnum)
14201             break;
14202
14203           elib = (Elf32_External_Lib *)
14204               get_data (NULL, file, section->sh_offset, 1, section->sh_size,
14205                         _("liblist section data"));
14206
14207           if (elib == NULL)
14208             break;
14209           string_sec = section_headers + section->sh_link;
14210
14211           strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
14212                                       string_sec->sh_size,
14213                                       _("liblist string table"));
14214           if (strtab == NULL
14215               || section->sh_entsize != sizeof (Elf32_External_Lib))
14216             {
14217               free (elib);
14218               free (strtab);
14219               break;
14220             }
14221           strtab_size = string_sec->sh_size;
14222
14223           printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
14224                   printable_section_name (section),
14225                   (unsigned long) (section->sh_size / sizeof (Elf32_External_Lib)));
14226
14227           puts (_("     Library              Time Stamp          Checksum   Version Flags"));
14228
14229           for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
14230                ++cnt)
14231             {
14232               Elf32_Lib liblist;
14233               time_t atime;
14234               char timebuf[20];
14235               struct tm * tmp;
14236
14237               liblist.l_name = BYTE_GET (elib[cnt].l_name);
14238               atime = BYTE_GET (elib[cnt].l_time_stamp);
14239               liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
14240               liblist.l_version = BYTE_GET (elib[cnt].l_version);
14241               liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
14242
14243               tmp = gmtime (&atime);
14244               snprintf (timebuf, sizeof (timebuf),
14245                         "%04u-%02u-%02uT%02u:%02u:%02u",
14246                         tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
14247                         tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
14248
14249               printf ("%3lu: ", (unsigned long) cnt);
14250               if (do_wide)
14251                 printf ("%-20s", liblist.l_name < strtab_size
14252                         ? strtab + liblist.l_name : _("<corrupt>"));
14253               else
14254                 printf ("%-20.20s", liblist.l_name < strtab_size
14255                         ? strtab + liblist.l_name : _("<corrupt>"));
14256               printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
14257                       liblist.l_version, liblist.l_flags);
14258             }
14259
14260           free (elib);
14261           free (strtab);
14262         }
14263     }
14264
14265   return 1;
14266 }
14267
14268 static const char *
14269 get_note_type (unsigned e_type)
14270 {
14271   static char buff[64];
14272
14273   if (elf_header.e_type == ET_CORE)
14274     switch (e_type)
14275       {
14276       case NT_AUXV:
14277         return _("NT_AUXV (auxiliary vector)");
14278       case NT_PRSTATUS:
14279         return _("NT_PRSTATUS (prstatus structure)");
14280       case NT_FPREGSET:
14281         return _("NT_FPREGSET (floating point registers)");
14282       case NT_PRPSINFO:
14283         return _("NT_PRPSINFO (prpsinfo structure)");
14284       case NT_TASKSTRUCT:
14285         return _("NT_TASKSTRUCT (task structure)");
14286       case NT_PRXFPREG:
14287         return _("NT_PRXFPREG (user_xfpregs structure)");
14288       case NT_PPC_VMX:
14289         return _("NT_PPC_VMX (ppc Altivec registers)");
14290       case NT_PPC_VSX:
14291         return _("NT_PPC_VSX (ppc VSX registers)");
14292       case NT_386_TLS:
14293         return _("NT_386_TLS (x86 TLS information)");
14294       case NT_386_IOPERM:
14295         return _("NT_386_IOPERM (x86 I/O permissions)");
14296       case NT_X86_XSTATE:
14297         return _("NT_X86_XSTATE (x86 XSAVE extended state)");
14298       case NT_S390_HIGH_GPRS:
14299         return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
14300       case NT_S390_TIMER:
14301         return _("NT_S390_TIMER (s390 timer register)");
14302       case NT_S390_TODCMP:
14303         return _("NT_S390_TODCMP (s390 TOD comparator register)");
14304       case NT_S390_TODPREG:
14305         return _("NT_S390_TODPREG (s390 TOD programmable register)");
14306       case NT_S390_CTRS:
14307         return _("NT_S390_CTRS (s390 control registers)");
14308       case NT_S390_PREFIX:
14309         return _("NT_S390_PREFIX (s390 prefix register)");
14310       case NT_S390_LAST_BREAK:
14311         return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
14312       case NT_S390_SYSTEM_CALL:
14313         return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
14314       case NT_S390_TDB:
14315         return _("NT_S390_TDB (s390 transaction diagnostic block)");
14316       case NT_ARM_VFP:
14317         return _("NT_ARM_VFP (arm VFP registers)");
14318       case NT_ARM_TLS:
14319         return _("NT_ARM_TLS (AArch TLS registers)");
14320       case NT_ARM_HW_BREAK:
14321         return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
14322       case NT_ARM_HW_WATCH:
14323         return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
14324       case NT_PSTATUS:
14325         return _("NT_PSTATUS (pstatus structure)");
14326       case NT_FPREGS:
14327         return _("NT_FPREGS (floating point registers)");
14328       case NT_PSINFO:
14329         return _("NT_PSINFO (psinfo structure)");
14330       case NT_LWPSTATUS:
14331         return _("NT_LWPSTATUS (lwpstatus_t structure)");
14332       case NT_LWPSINFO:
14333         return _("NT_LWPSINFO (lwpsinfo_t structure)");
14334       case NT_WIN32PSTATUS:
14335         return _("NT_WIN32PSTATUS (win32_pstatus structure)");
14336       case NT_SIGINFO:
14337         return _("NT_SIGINFO (siginfo_t data)");
14338       case NT_FILE:
14339         return _("NT_FILE (mapped files)");
14340       default:
14341         break;
14342       }
14343   else
14344     switch (e_type)
14345       {
14346       case NT_VERSION:
14347         return _("NT_VERSION (version)");
14348       case NT_ARCH:
14349         return _("NT_ARCH (architecture)");
14350       default:
14351         break;
14352       }
14353
14354   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14355   return buff;
14356 }
14357
14358 static int
14359 print_core_note (Elf_Internal_Note *pnote)
14360 {
14361   unsigned int addr_size = is_32bit_elf ? 4 : 8;
14362   bfd_vma count, page_size;
14363   unsigned char *descdata, *filenames, *descend;
14364
14365   if (pnote->type != NT_FILE)
14366     return 1;
14367
14368 #ifndef BFD64
14369   if (!is_32bit_elf)
14370     {
14371       printf (_("    Cannot decode 64-bit note in 32-bit build\n"));
14372       /* Still "successful".  */
14373       return 1;
14374     }
14375 #endif
14376
14377   if (pnote->descsz < 2 * addr_size)
14378     {
14379       printf (_("    Malformed note - too short for header\n"));
14380       return 0;
14381     }
14382
14383   descdata = (unsigned char *) pnote->descdata;
14384   descend = descdata + pnote->descsz;
14385
14386   if (descdata[pnote->descsz - 1] != '\0')
14387     {
14388       printf (_("    Malformed note - does not end with \\0\n"));
14389       return 0;
14390     }
14391
14392   count = byte_get (descdata, addr_size);
14393   descdata += addr_size;
14394
14395   page_size = byte_get (descdata, addr_size);
14396   descdata += addr_size;
14397
14398   if (pnote->descsz < 2 * addr_size + count * 3 * addr_size)
14399     {
14400       printf (_("    Malformed note - too short for supplied file count\n"));
14401       return 0;
14402     }
14403
14404   printf (_("    Page size: "));
14405   print_vma (page_size, DEC);
14406   printf ("\n");
14407
14408   printf (_("    %*s%*s%*s\n"),
14409           (int) (2 + 2 * addr_size), _("Start"),
14410           (int) (4 + 2 * addr_size), _("End"),
14411           (int) (4 + 2 * addr_size), _("Page Offset"));
14412   filenames = descdata + count * 3 * addr_size;
14413   while (--count > 0)
14414     {
14415       bfd_vma start, end, file_ofs;
14416
14417       if (filenames == descend)
14418         {
14419           printf (_("    Malformed note - filenames end too early\n"));
14420           return 0;
14421         }
14422
14423       start = byte_get (descdata, addr_size);
14424       descdata += addr_size;
14425       end = byte_get (descdata, addr_size);
14426       descdata += addr_size;
14427       file_ofs = byte_get (descdata, addr_size);
14428       descdata += addr_size;
14429
14430       printf ("    ");
14431       print_vma (start, FULL_HEX);
14432       printf ("  ");
14433       print_vma (end, FULL_HEX);
14434       printf ("  ");
14435       print_vma (file_ofs, FULL_HEX);
14436       printf ("\n        %s\n", filenames);
14437
14438       filenames += 1 + strlen ((char *) filenames);
14439     }
14440
14441   return 1;
14442 }
14443
14444 static const char *
14445 get_gnu_elf_note_type (unsigned e_type)
14446 {
14447   static char buff[64];
14448
14449   switch (e_type)
14450     {
14451     case NT_GNU_ABI_TAG:
14452       return _("NT_GNU_ABI_TAG (ABI version tag)");
14453     case NT_GNU_HWCAP:
14454       return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
14455     case NT_GNU_BUILD_ID:
14456       return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
14457     case NT_GNU_GOLD_VERSION:
14458       return _("NT_GNU_GOLD_VERSION (gold version)");
14459     default:
14460       break;
14461     }
14462
14463   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14464   return buff;
14465 }
14466
14467 static int
14468 print_gnu_note (Elf_Internal_Note *pnote)
14469 {
14470   switch (pnote->type)
14471     {
14472     case NT_GNU_BUILD_ID:
14473       {
14474         unsigned long i;
14475
14476         printf (_("    Build ID: "));
14477         for (i = 0; i < pnote->descsz; ++i)
14478           printf ("%02x", pnote->descdata[i] & 0xff);
14479         printf ("\n");
14480       }
14481       break;
14482
14483     case NT_GNU_ABI_TAG:
14484       {
14485         unsigned long os, major, minor, subminor;
14486         const char *osname;
14487
14488         /* PR 17531: file: 030-599401-0.004.  */
14489         if (pnote->descsz < 16)
14490           {
14491             printf (_("    <corrupt GNU_ABI_TAG>\n"));
14492             break;
14493           }
14494
14495         os = byte_get ((unsigned char *) pnote->descdata, 4);
14496         major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
14497         minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
14498         subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
14499
14500         switch (os)
14501           {
14502           case GNU_ABI_TAG_LINUX:
14503             osname = "Linux";
14504             break;
14505           case GNU_ABI_TAG_HURD:
14506             osname = "Hurd";
14507             break;
14508           case GNU_ABI_TAG_SOLARIS:
14509             osname = "Solaris";
14510             break;
14511           case GNU_ABI_TAG_FREEBSD:
14512             osname = "FreeBSD";
14513             break;
14514           case GNU_ABI_TAG_NETBSD:
14515             osname = "NetBSD";
14516             break;
14517           default:
14518             osname = "Unknown";
14519             break;
14520           }
14521
14522         printf (_("    OS: %s, ABI: %ld.%ld.%ld\n"), osname,
14523                 major, minor, subminor);
14524       }
14525       break;
14526
14527     case NT_GNU_GOLD_VERSION:
14528       {
14529         unsigned long i;
14530
14531         printf (_("    Version: "));
14532         for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
14533           printf ("%c", pnote->descdata[i]);
14534         printf ("\n");
14535       }
14536       break;
14537     }
14538
14539   return 1;
14540 }
14541
14542 static const char *
14543 get_netbsd_elfcore_note_type (unsigned e_type)
14544 {
14545   static char buff[64];
14546
14547   if (e_type == NT_NETBSDCORE_PROCINFO)
14548     {
14549       /* NetBSD core "procinfo" structure.  */
14550       return _("NetBSD procinfo structure");
14551     }
14552
14553   /* As of Jan 2002 there are no other machine-independent notes
14554      defined for NetBSD core files.  If the note type is less
14555      than the start of the machine-dependent note types, we don't
14556      understand it.  */
14557
14558   if (e_type < NT_NETBSDCORE_FIRSTMACH)
14559     {
14560       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14561       return buff;
14562     }
14563
14564   switch (elf_header.e_machine)
14565     {
14566     /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
14567        and PT_GETFPREGS == mach+2.  */
14568
14569     case EM_OLD_ALPHA:
14570     case EM_ALPHA:
14571     case EM_SPARC:
14572     case EM_SPARC32PLUS:
14573     case EM_SPARCV9:
14574       switch (e_type)
14575         {
14576         case NT_NETBSDCORE_FIRSTMACH + 0:
14577           return _("PT_GETREGS (reg structure)");
14578         case NT_NETBSDCORE_FIRSTMACH + 2:
14579           return _("PT_GETFPREGS (fpreg structure)");
14580         default:
14581           break;
14582         }
14583       break;
14584
14585     /* On all other arch's, PT_GETREGS == mach+1 and
14586        PT_GETFPREGS == mach+3.  */
14587     default:
14588       switch (e_type)
14589         {
14590         case NT_NETBSDCORE_FIRSTMACH + 1:
14591           return _("PT_GETREGS (reg structure)");
14592         case NT_NETBSDCORE_FIRSTMACH + 3:
14593           return _("PT_GETFPREGS (fpreg structure)");
14594         default:
14595           break;
14596         }
14597     }
14598
14599   snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
14600             e_type - NT_NETBSDCORE_FIRSTMACH);
14601   return buff;
14602 }
14603
14604 static const char *
14605 get_stapsdt_note_type (unsigned e_type)
14606 {
14607   static char buff[64];
14608
14609   switch (e_type)
14610     {
14611     case NT_STAPSDT:
14612       return _("NT_STAPSDT (SystemTap probe descriptors)");
14613
14614     default:
14615       break;
14616     }
14617
14618   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14619   return buff;
14620 }
14621
14622 static int
14623 print_stapsdt_note (Elf_Internal_Note *pnote)
14624 {
14625   int addr_size = is_32bit_elf ? 4 : 8;
14626   char *data = pnote->descdata;
14627   char *data_end = pnote->descdata + pnote->descsz;
14628   bfd_vma pc, base_addr, semaphore;
14629   char *provider, *probe, *arg_fmt;
14630
14631   pc = byte_get ((unsigned char *) data, addr_size);
14632   data += addr_size;
14633   base_addr = byte_get ((unsigned char *) data, addr_size);
14634   data += addr_size;
14635   semaphore = byte_get ((unsigned char *) data, addr_size);
14636   data += addr_size;
14637
14638   provider = data;
14639   data += strlen (data) + 1;
14640   probe = data;
14641   data += strlen (data) + 1;
14642   arg_fmt = data;
14643   data += strlen (data) + 1;
14644
14645   printf (_("    Provider: %s\n"), provider);
14646   printf (_("    Name: %s\n"), probe);
14647   printf (_("    Location: "));
14648   print_vma (pc, FULL_HEX);
14649   printf (_(", Base: "));
14650   print_vma (base_addr, FULL_HEX);
14651   printf (_(", Semaphore: "));
14652   print_vma (semaphore, FULL_HEX);
14653   printf ("\n");
14654   printf (_("    Arguments: %s\n"), arg_fmt);
14655
14656   return data == data_end;
14657 }
14658
14659 static const char *
14660 get_ia64_vms_note_type (unsigned e_type)
14661 {
14662   static char buff[64];
14663
14664   switch (e_type)
14665     {
14666     case NT_VMS_MHD:
14667       return _("NT_VMS_MHD (module header)");
14668     case NT_VMS_LNM:
14669       return _("NT_VMS_LNM (language name)");
14670     case NT_VMS_SRC:
14671       return _("NT_VMS_SRC (source files)");
14672     case NT_VMS_TITLE:
14673       return "NT_VMS_TITLE";
14674     case NT_VMS_EIDC:
14675       return _("NT_VMS_EIDC (consistency check)");
14676     case NT_VMS_FPMODE:
14677       return _("NT_VMS_FPMODE (FP mode)");
14678     case NT_VMS_LINKTIME:
14679       return "NT_VMS_LINKTIME";
14680     case NT_VMS_IMGNAM:
14681       return _("NT_VMS_IMGNAM (image name)");
14682     case NT_VMS_IMGID:
14683       return _("NT_VMS_IMGID (image id)");
14684     case NT_VMS_LINKID:
14685       return _("NT_VMS_LINKID (link id)");
14686     case NT_VMS_IMGBID:
14687       return _("NT_VMS_IMGBID (build id)");
14688     case NT_VMS_GSTNAM:
14689       return _("NT_VMS_GSTNAM (sym table name)");
14690     case NT_VMS_ORIG_DYN:
14691       return "NT_VMS_ORIG_DYN";
14692     case NT_VMS_PATCHTIME:
14693       return "NT_VMS_PATCHTIME";
14694     default:
14695       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14696       return buff;
14697     }
14698 }
14699
14700 static int
14701 print_ia64_vms_note (Elf_Internal_Note * pnote)
14702 {
14703   switch (pnote->type)
14704     {
14705     case NT_VMS_MHD:
14706       if (pnote->descsz > 36)
14707         {
14708           size_t l = strlen (pnote->descdata + 34);
14709           printf (_("    Creation date  : %.17s\n"), pnote->descdata);
14710           printf (_("    Last patch date: %.17s\n"), pnote->descdata + 17);
14711           printf (_("    Module name    : %s\n"), pnote->descdata + 34);
14712           printf (_("    Module version : %s\n"), pnote->descdata + 34 + l + 1);
14713         }
14714       else
14715         printf (_("    Invalid size\n"));
14716       break;
14717     case NT_VMS_LNM:
14718       printf (_("   Language: %s\n"), pnote->descdata);
14719       break;
14720 #ifdef BFD64
14721     case NT_VMS_FPMODE:
14722       printf (_("   Floating Point mode: "));
14723       printf ("0x%016" BFD_VMA_FMT "x\n",
14724               (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
14725       break;
14726     case NT_VMS_LINKTIME:
14727       printf (_("   Link time: "));
14728       print_vms_time
14729         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
14730       printf ("\n");
14731       break;
14732     case NT_VMS_PATCHTIME:
14733       printf (_("   Patch time: "));
14734       print_vms_time
14735         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
14736       printf ("\n");
14737       break;
14738     case NT_VMS_ORIG_DYN:
14739       printf (_("   Major id: %u,  minor id: %u\n"),
14740               (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
14741               (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
14742       printf (_("   Last modified  : "));
14743       print_vms_time
14744         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
14745       printf (_("\n   Link flags  : "));
14746       printf ("0x%016" BFD_VMA_FMT "x\n",
14747               (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
14748       printf (_("   Header flags: 0x%08x\n"),
14749               (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
14750       printf (_("   Image id    : %s\n"), pnote->descdata + 32);
14751       break;
14752 #endif
14753     case NT_VMS_IMGNAM:
14754       printf (_("    Image name: %s\n"), pnote->descdata);
14755       break;
14756     case NT_VMS_GSTNAM:
14757       printf (_("    Global symbol table name: %s\n"), pnote->descdata);
14758       break;
14759     case NT_VMS_IMGID:
14760       printf (_("    Image id: %s\n"), pnote->descdata);
14761       break;
14762     case NT_VMS_LINKID:
14763       printf (_("    Linker id: %s\n"), pnote->descdata);
14764       break;
14765     default:
14766       break;
14767     }
14768   return 1;
14769 }
14770
14771 /* Note that by the ELF standard, the name field is already null byte
14772    terminated, and namesz includes the terminating null byte.
14773    I.E. the value of namesz for the name "FSF" is 4.
14774
14775    If the value of namesz is zero, there is no name present.  */
14776 static int
14777 process_note (Elf_Internal_Note * pnote)
14778 {
14779   const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
14780   const char * nt;
14781
14782   if (pnote->namesz == 0)
14783     /* If there is no note name, then use the default set of
14784        note type strings.  */
14785     nt = get_note_type (pnote->type);
14786
14787   else if (const_strneq (pnote->namedata, "GNU"))
14788     /* GNU-specific object file notes.  */
14789     nt = get_gnu_elf_note_type (pnote->type);
14790
14791   else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
14792     /* NetBSD-specific core file notes.  */
14793     nt = get_netbsd_elfcore_note_type (pnote->type);
14794
14795   else if (strneq (pnote->namedata, "SPU/", 4))
14796     {
14797       /* SPU-specific core file notes.  */
14798       nt = pnote->namedata + 4;
14799       name = "SPU";
14800     }
14801
14802   else if (const_strneq (pnote->namedata, "IPF/VMS"))
14803     /* VMS/ia64-specific file notes.  */
14804     nt = get_ia64_vms_note_type (pnote->type);
14805
14806   else if (const_strneq (pnote->namedata, "stapsdt"))
14807     nt = get_stapsdt_note_type (pnote->type);
14808
14809   else
14810     /* Don't recognize this note name; just use the default set of
14811        note type strings.  */
14812     nt = get_note_type (pnote->type);
14813
14814   printf ("  %-20s 0x%08lx\t%s\n", name, pnote->descsz, nt);
14815
14816   if (const_strneq (pnote->namedata, "IPF/VMS"))
14817     return print_ia64_vms_note (pnote);
14818   else if (const_strneq (pnote->namedata, "GNU"))
14819     return print_gnu_note (pnote);
14820   else if (const_strneq (pnote->namedata, "stapsdt"))
14821     return print_stapsdt_note (pnote);
14822   else if (const_strneq (pnote->namedata, "CORE"))
14823     return print_core_note (pnote);
14824   else
14825     return 1;
14826 }
14827
14828
14829 static int
14830 process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
14831 {
14832   Elf_External_Note * pnotes;
14833   Elf_External_Note * external;
14834   char * end;
14835   int res = 1;
14836
14837   if (length <= 0)
14838     return 0;
14839
14840   pnotes = (Elf_External_Note *) get_data (NULL, file, offset, 1, length,
14841                                            _("notes"));
14842   if (pnotes == NULL)
14843     return 0;
14844
14845   external = pnotes;
14846
14847   printf (_("\nDisplaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
14848           (unsigned long) offset, (unsigned long) length);
14849   printf (_("  %-20s %10s\tDescription\n"), _("Owner"), _("Data size"));
14850
14851   end = (char *) pnotes + length;
14852   while ((char *) external < end)
14853     {
14854       Elf_Internal_Note inote;
14855       size_t min_notesz;
14856       char *next;
14857       char * temp = NULL;
14858       size_t data_remaining = end - (char *) external;
14859
14860       if (!is_ia64_vms ())
14861         {
14862           /* PR binutils/15191
14863              Make sure that there is enough data to read.  */
14864           min_notesz = offsetof (Elf_External_Note, name);
14865           if (data_remaining < min_notesz)
14866             {
14867               warn (_("Corrupt note: only %d bytes remain, not enough for a full note\n"),
14868                     (int) data_remaining);
14869               break;
14870             }
14871           inote.type     = BYTE_GET (external->type);
14872           inote.namesz   = BYTE_GET (external->namesz);
14873           inote.namedata = external->name;
14874           inote.descsz   = BYTE_GET (external->descsz);
14875           inote.descdata = inote.namedata + align_power (inote.namesz, 2);
14876           /* PR 17531: file: 3443835e.  */
14877           if (inote.descdata < (char *) pnotes || inote.descdata > end)
14878             {
14879               warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
14880               inote.descdata = inote.namedata;
14881               inote.namesz   = 0;
14882             }
14883
14884           inote.descpos  = offset + (inote.descdata - (char *) pnotes);
14885           next = inote.descdata + align_power (inote.descsz, 2);
14886         }
14887       else
14888         {
14889           Elf64_External_VMS_Note *vms_external;
14890
14891           /* PR binutils/15191
14892              Make sure that there is enough data to read.  */
14893           min_notesz = offsetof (Elf64_External_VMS_Note, name);
14894           if (data_remaining < min_notesz)
14895             {
14896               warn (_("Corrupt note: only %d bytes remain, not enough for a full note\n"),
14897                     (int) data_remaining);
14898               break;
14899             }
14900
14901           vms_external = (Elf64_External_VMS_Note *) external;
14902           inote.type     = BYTE_GET (vms_external->type);
14903           inote.namesz   = BYTE_GET (vms_external->namesz);
14904           inote.namedata = vms_external->name;
14905           inote.descsz   = BYTE_GET (vms_external->descsz);
14906           inote.descdata = inote.namedata + align_power (inote.namesz, 3);
14907           inote.descpos  = offset + (inote.descdata - (char *) pnotes);
14908           next = inote.descdata + align_power (inote.descsz, 3);
14909         }
14910
14911       if (inote.descdata < (char *) external + min_notesz
14912           || next < (char *) external + min_notesz
14913           /* PR binutils/17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4.  */
14914           || inote.namedata + inote.namesz < inote.namedata
14915           || inote.descdata + inote.descsz < inote.descdata
14916           || data_remaining < (size_t)(next - (char *) external))
14917         {
14918           warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
14919                 (unsigned long) ((char *) external - (char *) pnotes));
14920           warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx\n"),
14921                 inote.type, inote.namesz, inote.descsz);
14922           break;
14923         }
14924
14925       external = (Elf_External_Note *) next;
14926
14927       /* Verify that name is null terminated.  It appears that at least
14928          one version of Linux (RedHat 6.0) generates corefiles that don't
14929          comply with the ELF spec by failing to include the null byte in
14930          namesz.  */
14931       if (inote.namedata[inote.namesz - 1] != '\0')
14932         {
14933           temp = (char *) malloc (inote.namesz + 1);
14934           if (temp == NULL)
14935             {
14936               error (_("Out of memory allocating space for inote name\n"));
14937               res = 0;
14938               break;
14939             }
14940
14941           strncpy (temp, inote.namedata, inote.namesz);
14942           temp[inote.namesz] = 0;
14943
14944           /* warn (_("'%s' NOTE name not properly null terminated\n"), temp);  */
14945           inote.namedata = temp;
14946         }
14947
14948       res &= process_note (& inote);
14949
14950       if (temp != NULL)
14951         {
14952           free (temp);
14953           temp = NULL;
14954         }
14955     }
14956
14957   free (pnotes);
14958
14959   return res;
14960 }
14961
14962 static int
14963 process_corefile_note_segments (FILE * file)
14964 {
14965   Elf_Internal_Phdr * segment;
14966   unsigned int i;
14967   int res = 1;
14968
14969   if (! get_program_headers (file))
14970       return 0;
14971
14972   for (i = 0, segment = program_headers;
14973        i < elf_header.e_phnum;
14974        i++, segment++)
14975     {
14976       if (segment->p_type == PT_NOTE)
14977         res &= process_corefile_note_segment (file,
14978                                               (bfd_vma) segment->p_offset,
14979                                               (bfd_vma) segment->p_filesz);
14980     }
14981
14982   return res;
14983 }
14984
14985 static int
14986 process_note_sections (FILE * file)
14987 {
14988   Elf_Internal_Shdr * section;
14989   unsigned long i;
14990   int n = 0;
14991   int res = 1;
14992
14993   for (i = 0, section = section_headers;
14994        i < elf_header.e_shnum && section != NULL;
14995        i++, section++)
14996     if (section->sh_type == SHT_NOTE)
14997       {
14998         res &= process_corefile_note_segment (file,
14999                                               (bfd_vma) section->sh_offset,
15000                                               (bfd_vma) section->sh_size);
15001         n++;
15002       }
15003
15004   if (n == 0)
15005     /* Try processing NOTE segments instead.  */
15006     return process_corefile_note_segments (file);
15007
15008   return res;
15009 }
15010
15011 static int
15012 process_notes (FILE * file)
15013 {
15014   /* If we have not been asked to display the notes then do nothing.  */
15015   if (! do_notes)
15016     return 1;
15017
15018   if (elf_header.e_type != ET_CORE)
15019     return process_note_sections (file);
15020
15021   /* No program headers means no NOTE segment.  */
15022   if (elf_header.e_phnum > 0)
15023     return process_corefile_note_segments (file);
15024
15025   printf (_("No note segments present in the core file.\n"));
15026   return 1;
15027 }
15028
15029 static int
15030 process_arch_specific (FILE * file)
15031 {
15032   if (! do_arch)
15033     return 1;
15034
15035   switch (elf_header.e_machine)
15036     {
15037     case EM_ARM:
15038       return process_arm_specific (file);
15039     case EM_MIPS:
15040     case EM_MIPS_RS3_LE:
15041       return process_mips_specific (file);
15042       break;
15043     case EM_NDS32:
15044       return process_nds32_specific (file);
15045       break;
15046     case EM_PPC:
15047       return process_power_specific (file);
15048       break;
15049     case EM_SPARC:
15050     case EM_SPARC32PLUS:
15051     case EM_SPARCV9:
15052       return process_sparc_specific (file);
15053       break;
15054     case EM_TI_C6000:
15055       return process_tic6x_specific (file);
15056       break;
15057     case EM_MSP430:
15058       return process_msp430x_specific (file);
15059     default:
15060       break;
15061     }
15062   return 1;
15063 }
15064
15065 static int
15066 get_file_header (FILE * file)
15067 {
15068   /* Read in the identity array.  */
15069   if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
15070     return 0;
15071
15072   /* Determine how to read the rest of the header.  */
15073   switch (elf_header.e_ident[EI_DATA])
15074     {
15075     default: /* fall through */
15076     case ELFDATANONE: /* fall through */
15077     case ELFDATA2LSB:
15078       byte_get = byte_get_little_endian;
15079       byte_put = byte_put_little_endian;
15080       break;
15081     case ELFDATA2MSB:
15082       byte_get = byte_get_big_endian;
15083       byte_put = byte_put_big_endian;
15084       break;
15085     }
15086
15087   /* For now we only support 32 bit and 64 bit ELF files.  */
15088   is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
15089
15090   /* Read in the rest of the header.  */
15091   if (is_32bit_elf)
15092     {
15093       Elf32_External_Ehdr ehdr32;
15094
15095       if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
15096         return 0;
15097
15098       elf_header.e_type      = BYTE_GET (ehdr32.e_type);
15099       elf_header.e_machine   = BYTE_GET (ehdr32.e_machine);
15100       elf_header.e_version   = BYTE_GET (ehdr32.e_version);
15101       elf_header.e_entry     = BYTE_GET (ehdr32.e_entry);
15102       elf_header.e_phoff     = BYTE_GET (ehdr32.e_phoff);
15103       elf_header.e_shoff     = BYTE_GET (ehdr32.e_shoff);
15104       elf_header.e_flags     = BYTE_GET (ehdr32.e_flags);
15105       elf_header.e_ehsize    = BYTE_GET (ehdr32.e_ehsize);
15106       elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
15107       elf_header.e_phnum     = BYTE_GET (ehdr32.e_phnum);
15108       elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
15109       elf_header.e_shnum     = BYTE_GET (ehdr32.e_shnum);
15110       elf_header.e_shstrndx  = BYTE_GET (ehdr32.e_shstrndx);
15111     }
15112   else
15113     {
15114       Elf64_External_Ehdr ehdr64;
15115
15116       /* If we have been compiled with sizeof (bfd_vma) == 4, then
15117          we will not be able to cope with the 64bit data found in
15118          64 ELF files.  Detect this now and abort before we start
15119          overwriting things.  */
15120       if (sizeof (bfd_vma) < 8)
15121         {
15122           error (_("This instance of readelf has been built without support for a\n\
15123 64 bit data type and so it cannot read 64 bit ELF files.\n"));
15124           return 0;
15125         }
15126
15127       if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
15128         return 0;
15129
15130       elf_header.e_type      = BYTE_GET (ehdr64.e_type);
15131       elf_header.e_machine   = BYTE_GET (ehdr64.e_machine);
15132       elf_header.e_version   = BYTE_GET (ehdr64.e_version);
15133       elf_header.e_entry     = BYTE_GET (ehdr64.e_entry);
15134       elf_header.e_phoff     = BYTE_GET (ehdr64.e_phoff);
15135       elf_header.e_shoff     = BYTE_GET (ehdr64.e_shoff);
15136       elf_header.e_flags     = BYTE_GET (ehdr64.e_flags);
15137       elf_header.e_ehsize    = BYTE_GET (ehdr64.e_ehsize);
15138       elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
15139       elf_header.e_phnum     = BYTE_GET (ehdr64.e_phnum);
15140       elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
15141       elf_header.e_shnum     = BYTE_GET (ehdr64.e_shnum);
15142       elf_header.e_shstrndx  = BYTE_GET (ehdr64.e_shstrndx);
15143     }
15144
15145   if (elf_header.e_shoff)
15146     {
15147       /* There may be some extensions in the first section header.  Don't
15148          bomb if we can't read it.  */
15149       if (is_32bit_elf)
15150         get_32bit_section_headers (file, TRUE);
15151       else
15152         get_64bit_section_headers (file, TRUE);
15153     }
15154
15155   return 1;
15156 }
15157
15158 /* Process one ELF object file according to the command line options.
15159    This file may actually be stored in an archive.  The file is
15160    positioned at the start of the ELF object.  */
15161
15162 static int
15163 process_object (char * file_name, FILE * file)
15164 {
15165   unsigned int i;
15166
15167   if (! get_file_header (file))
15168     {
15169       error (_("%s: Failed to read file header\n"), file_name);
15170       return 1;
15171     }
15172
15173   /* Initialise per file variables.  */
15174   for (i = ARRAY_SIZE (version_info); i--;)
15175     version_info[i] = 0;
15176
15177   for (i = ARRAY_SIZE (dynamic_info); i--;)
15178     dynamic_info[i] = 0;
15179   dynamic_info_DT_GNU_HASH = 0;
15180
15181   /* Process the file.  */
15182   if (show_name)
15183     printf (_("\nFile: %s\n"), file_name);
15184
15185   /* Initialise the dump_sects array from the cmdline_dump_sects array.
15186      Note we do this even if cmdline_dump_sects is empty because we
15187      must make sure that the dump_sets array is zeroed out before each
15188      object file is processed.  */
15189   if (num_dump_sects > num_cmdline_dump_sects)
15190     memset (dump_sects, 0, num_dump_sects * sizeof (* dump_sects));
15191
15192   if (num_cmdline_dump_sects > 0)
15193     {
15194       if (num_dump_sects == 0)
15195         /* A sneaky way of allocating the dump_sects array.  */
15196         request_dump_bynumber (num_cmdline_dump_sects, 0);
15197
15198       assert (num_dump_sects >= num_cmdline_dump_sects);
15199       memcpy (dump_sects, cmdline_dump_sects,
15200               num_cmdline_dump_sects * sizeof (* dump_sects));
15201     }
15202
15203   if (! process_file_header ())
15204     return 1;
15205
15206   if (! process_section_headers (file))
15207     {
15208       /* Without loaded section headers we cannot process lots of
15209          things.  */
15210       do_unwind = do_version = do_dump = do_arch = 0;
15211
15212       if (! do_using_dynamic)
15213         do_syms = do_dyn_syms = do_reloc = 0;
15214     }
15215
15216   if (! process_section_groups (file))
15217     {
15218       /* Without loaded section groups we cannot process unwind.  */
15219       do_unwind = 0;
15220     }
15221
15222   if (process_program_headers (file))
15223     process_dynamic_section (file);
15224
15225   process_relocs (file);
15226
15227   process_unwind (file);
15228
15229   process_symbol_table (file);
15230
15231   process_syminfo (file);
15232
15233   process_version_sections (file);
15234
15235   process_section_contents (file);
15236
15237   process_notes (file);
15238
15239   process_gnu_liblist (file);
15240
15241   process_arch_specific (file);
15242
15243   if (program_headers)
15244     {
15245       free (program_headers);
15246       program_headers = NULL;
15247     }
15248
15249   if (section_headers)
15250     {
15251       free (section_headers);
15252       section_headers = NULL;
15253     }
15254
15255   if (string_table)
15256     {
15257       free (string_table);
15258       string_table = NULL;
15259       string_table_length = 0;
15260     }
15261
15262   if (dynamic_strings)
15263     {
15264       free (dynamic_strings);
15265       dynamic_strings = NULL;
15266       dynamic_strings_length = 0;
15267     }
15268
15269   if (dynamic_symbols)
15270     {
15271       free (dynamic_symbols);
15272       dynamic_symbols = NULL;
15273       num_dynamic_syms = 0;
15274     }
15275
15276   if (dynamic_syminfo)
15277     {
15278       free (dynamic_syminfo);
15279       dynamic_syminfo = NULL;
15280     }
15281
15282   if (dynamic_section)
15283     {
15284       free (dynamic_section);
15285       dynamic_section = NULL;
15286     }
15287
15288   if (section_headers_groups)
15289     {
15290       free (section_headers_groups);
15291       section_headers_groups = NULL;
15292     }
15293
15294   if (section_groups)
15295     {
15296       struct group_list * g;
15297       struct group_list * next;
15298
15299       for (i = 0; i < group_count; i++)
15300         {
15301           for (g = section_groups [i].root; g != NULL; g = next)
15302             {
15303               next = g->next;
15304               free (g);
15305             }
15306         }
15307
15308       free (section_groups);
15309       section_groups = NULL;
15310     }
15311
15312   free_debug_memory ();
15313
15314   return 0;
15315 }
15316
15317 /* Process an ELF archive.
15318    On entry the file is positioned just after the ARMAG string.  */
15319
15320 static int
15321 process_archive (char * file_name, FILE * file, bfd_boolean is_thin_archive)
15322 {
15323   struct archive_info arch;
15324   struct archive_info nested_arch;
15325   size_t got;
15326   int ret;
15327
15328   show_name = 1;
15329
15330   /* The ARCH structure is used to hold information about this archive.  */
15331   arch.file_name = NULL;
15332   arch.file = NULL;
15333   arch.index_array = NULL;
15334   arch.sym_table = NULL;
15335   arch.longnames = NULL;
15336
15337   /* The NESTED_ARCH structure is used as a single-item cache of information
15338      about a nested archive (when members of a thin archive reside within
15339      another regular archive file).  */
15340   nested_arch.file_name = NULL;
15341   nested_arch.file = NULL;
15342   nested_arch.index_array = NULL;
15343   nested_arch.sym_table = NULL;
15344   nested_arch.longnames = NULL;
15345
15346   if (setup_archive (&arch, file_name, file, is_thin_archive, do_archive_index) != 0)
15347     {
15348       ret = 1;
15349       goto out;
15350     }
15351
15352   if (do_archive_index)
15353     {
15354       if (arch.sym_table == NULL)
15355         error (_("%s: unable to dump the index as none was found\n"), file_name);
15356       else
15357         {
15358           unsigned long i, l;
15359           unsigned long current_pos;
15360
15361           printf (_("Index of archive %s: (%lu entries, 0x%lx bytes in the symbol table)\n"),
15362                   file_name, (unsigned long) arch.index_num, arch.sym_size);
15363           current_pos = ftell (file);
15364
15365           for (i = l = 0; i < arch.index_num; i++)
15366             {
15367               if ((i == 0) || ((i > 0) && (arch.index_array[i] != arch.index_array[i - 1])))
15368                 {
15369                   char * member_name;
15370
15371                   member_name = get_archive_member_name_at (&arch, arch.index_array[i], &nested_arch);
15372
15373                   if (member_name != NULL)
15374                     {
15375                       char * qualified_name = make_qualified_name (&arch, &nested_arch, member_name);
15376
15377                       if (qualified_name != NULL)
15378                         {
15379                           printf (_("Contents of binary %s at offset "), qualified_name);
15380                           (void) print_vma (arch.index_array[i], PREFIX_HEX);
15381                           putchar ('\n');
15382                           free (qualified_name);
15383                         }
15384                     }
15385                 }
15386
15387               if (l >= arch.sym_size)
15388                 {
15389                   error (_("%s: end of the symbol table reached before the end of the index\n"),
15390                          file_name);
15391                   break;
15392                 }
15393               /* PR 17531: file: 0b6630b2.  */
15394               printf ("\t%.*s\n", (int) (arch.sym_size - l), arch.sym_table + l);
15395               l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
15396             }
15397
15398           if (arch.uses_64bit_indicies)
15399             l = (l + 7) & ~ 7;
15400           else
15401             l += l & 1;
15402
15403           if (l < arch.sym_size)
15404             error (_("%s: %ld bytes remain in the symbol table, but without corresponding entries in the index table\n"),
15405                    file_name, arch.sym_size - l);
15406
15407           if (fseek (file, current_pos, SEEK_SET) != 0)
15408             {
15409               error (_("%s: failed to seek back to start of object files in the archive\n"), file_name);
15410               ret = 1;
15411               goto out;
15412             }
15413         }
15414
15415       if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
15416           && !do_segments && !do_header && !do_dump && !do_version
15417           && !do_histogram && !do_debugging && !do_arch && !do_notes
15418           && !do_section_groups && !do_dyn_syms)
15419         {
15420           ret = 0; /* Archive index only.  */
15421           goto out;
15422         }
15423     }
15424
15425   ret = 0;
15426
15427   while (1)
15428     {
15429       char * name;
15430       size_t namelen;
15431       char * qualified_name;
15432
15433       /* Read the next archive header.  */
15434       if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
15435         {
15436           error (_("%s: failed to seek to next archive header\n"), file_name);
15437           return 1;
15438         }
15439       got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
15440       if (got != sizeof arch.arhdr)
15441         {
15442           if (got == 0)
15443             break;
15444           error (_("%s: failed to read archive header\n"), file_name);
15445           ret = 1;
15446           break;
15447         }
15448       if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
15449         {
15450           error (_("%s: did not find a valid archive header\n"), arch.file_name);
15451           ret = 1;
15452           break;
15453         }
15454
15455       arch.next_arhdr_offset += sizeof arch.arhdr;
15456
15457       archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
15458       if (archive_file_size & 01)
15459         ++archive_file_size;
15460
15461       name = get_archive_member_name (&arch, &nested_arch);
15462       if (name == NULL)
15463         {
15464           error (_("%s: bad archive file name\n"), file_name);
15465           ret = 1;
15466           break;
15467         }
15468       namelen = strlen (name);
15469
15470       qualified_name = make_qualified_name (&arch, &nested_arch, name);
15471       if (qualified_name == NULL)
15472         {
15473           error (_("%s: bad archive file name\n"), file_name);
15474           ret = 1;
15475           break;
15476         }
15477
15478       if (is_thin_archive && arch.nested_member_origin == 0)
15479         {
15480           /* This is a proxy for an external member of a thin archive.  */
15481           FILE * member_file;
15482           char * member_file_name = adjust_relative_path (file_name, name, namelen);
15483           if (member_file_name == NULL)
15484             {
15485               ret = 1;
15486               break;
15487             }
15488
15489           member_file = fopen (member_file_name, "rb");
15490           if (member_file == NULL)
15491             {
15492               error (_("Input file '%s' is not readable.\n"), member_file_name);
15493               free (member_file_name);
15494               ret = 1;
15495               break;
15496             }
15497
15498           archive_file_offset = arch.nested_member_origin;
15499
15500           ret |= process_object (qualified_name, member_file);
15501
15502           fclose (member_file);
15503           free (member_file_name);
15504         }
15505       else if (is_thin_archive)
15506         {
15507           /* PR 15140: Allow for corrupt thin archives.  */
15508           if (nested_arch.file == NULL)
15509             {
15510               error (_("%s: contains corrupt thin archive: %s\n"),
15511                      file_name, name);
15512               ret = 1;
15513               break;
15514             }
15515
15516           /* This is a proxy for a member of a nested archive.  */
15517           archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
15518
15519           /* The nested archive file will have been opened and setup by
15520              get_archive_member_name.  */
15521           if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
15522             {
15523               error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
15524               ret = 1;
15525               break;
15526             }
15527
15528           ret |= process_object (qualified_name, nested_arch.file);
15529         }
15530       else
15531         {
15532           archive_file_offset = arch.next_arhdr_offset;
15533           arch.next_arhdr_offset += archive_file_size;
15534
15535           ret |= process_object (qualified_name, file);
15536         }
15537
15538       if (dump_sects != NULL)
15539         {
15540           free (dump_sects);
15541           dump_sects = NULL;
15542           num_dump_sects = 0;
15543         }
15544
15545       free (qualified_name);
15546     }
15547
15548  out:
15549   if (nested_arch.file != NULL)
15550     fclose (nested_arch.file);
15551   release_archive (&nested_arch);
15552   release_archive (&arch);
15553
15554   return ret;
15555 }
15556
15557 static int
15558 process_file (char * file_name)
15559 {
15560   FILE * file;
15561   struct stat statbuf;
15562   char armag[SARMAG];
15563   int ret;
15564
15565   if (stat (file_name, &statbuf) < 0)
15566     {
15567       if (errno == ENOENT)
15568         error (_("'%s': No such file\n"), file_name);
15569       else
15570         error (_("Could not locate '%s'.  System error message: %s\n"),
15571                file_name, strerror (errno));
15572       return 1;
15573     }
15574
15575   if (! S_ISREG (statbuf.st_mode))
15576     {
15577       error (_("'%s' is not an ordinary file\n"), file_name);
15578       return 1;
15579     }
15580
15581   file = fopen (file_name, "rb");
15582   if (file == NULL)
15583     {
15584       error (_("Input file '%s' is not readable.\n"), file_name);
15585       return 1;
15586     }
15587
15588   if (fread (armag, SARMAG, 1, file) != 1)
15589     {
15590       error (_("%s: Failed to read file's magic number\n"), file_name);
15591       fclose (file);
15592       return 1;
15593     }
15594
15595   current_file_size = (bfd_size_type) statbuf.st_size;
15596
15597   if (memcmp (armag, ARMAG, SARMAG) == 0)
15598     ret = process_archive (file_name, file, FALSE);
15599   else if (memcmp (armag, ARMAGT, SARMAG) == 0)
15600     ret = process_archive (file_name, file, TRUE);
15601   else
15602     {
15603       if (do_archive_index)
15604         error (_("File %s is not an archive so its index cannot be displayed.\n"),
15605                file_name);
15606
15607       rewind (file);
15608       archive_file_size = archive_file_offset = 0;
15609       ret = process_object (file_name, file);
15610     }
15611
15612   fclose (file);
15613
15614   current_file_size = 0;
15615   return ret;
15616 }
15617
15618 #ifdef SUPPORT_DISASSEMBLY
15619 /* Needed by the i386 disassembler.  For extra credit, someone could
15620    fix this so that we insert symbolic addresses here, esp for GOT/PLT
15621    symbols.  */
15622
15623 void
15624 print_address (unsigned int addr, FILE * outfile)
15625 {
15626   fprintf (outfile,"0x%8.8x", addr);
15627 }
15628
15629 /* Needed by the i386 disassembler.  */
15630 void
15631 db_task_printsym (unsigned int addr)
15632 {
15633   print_address (addr, stderr);
15634 }
15635 #endif
15636
15637 int
15638 main (int argc, char ** argv)
15639 {
15640   int err;
15641
15642 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
15643   setlocale (LC_MESSAGES, "");
15644 #endif
15645 #if defined (HAVE_SETLOCALE)
15646   setlocale (LC_CTYPE, "");
15647 #endif
15648   bindtextdomain (PACKAGE, LOCALEDIR);
15649   textdomain (PACKAGE);
15650
15651   expandargv (&argc, &argv);
15652
15653   parse_args (argc, argv);
15654
15655   if (num_dump_sects > 0)
15656     {
15657       /* Make a copy of the dump_sects array.  */
15658       cmdline_dump_sects = (dump_type *)
15659           malloc (num_dump_sects * sizeof (* dump_sects));
15660       if (cmdline_dump_sects == NULL)
15661         error (_("Out of memory allocating dump request table.\n"));
15662       else
15663         {
15664           memcpy (cmdline_dump_sects, dump_sects,
15665                   num_dump_sects * sizeof (* dump_sects));
15666           num_cmdline_dump_sects = num_dump_sects;
15667         }
15668     }
15669
15670   if (optind < (argc - 1))
15671     show_name = 1;
15672
15673   err = 0;
15674   while (optind < argc)
15675     err |= process_file (argv[optind++]);
15676
15677   if (dump_sects != NULL)
15678     free (dump_sects);
15679   if (cmdline_dump_sects != NULL)
15680     free (cmdline_dump_sects);
15681
15682   return err;
15683 }