Add missing ChangeLog entries for my last commit.
[platform/upstream/binutils.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3    2008  Free Software Foundation, Inc.
4
5    Originally developed by Eric Youngdale <eric@andante.jic.com>
6    Modifications by Nick Clifton <nickc@redhat.com>
7
8    This file is part of GNU Binutils.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23    02110-1301, USA.  */
24 \f
25 /* The difference between readelf and objdump:
26
27   Both programs are capable of displaying the contents of ELF format files,
28   so why does the binutils project have two file dumpers ?
29
30   The reason is that objdump sees an ELF file through a BFD filter of the
31   world; if BFD has a bug where, say, it disagrees about a machine constant
32   in e_flags, then the odds are good that it will remain internally
33   consistent.  The linker sees it the BFD way, objdump sees it the BFD way,
34   GAS sees it the BFD way.  There was need for a tool to go find out what
35   the file actually says.
36
37   This is why the readelf program does not link against the BFD library - it
38   exists as an independent program to help verify the correct working of BFD.
39
40   There is also the case that readelf can provide more information about an
41   ELF file than is provided by objdump.  In particular it can display DWARF
42   debugging information which (at the moment) objdump cannot.  */
43 \f
44 #include "config.h"
45 #include "sysdep.h"
46 #include <assert.h>
47 #include <sys/stat.h>
48 #include <time.h>
49 #ifdef HAVE_ZLIB_H
50 #include <zlib.h>
51 #endif
52
53 /* For PATH_MAX.  */
54 #ifdef HAVE_LIMITS_H
55 #include <limits.h>
56 #endif
57
58 #ifndef PATH_MAX
59 /* For MAXPATHLEN.  */
60 # ifdef HAVE_SYS_PARAM_H
61 #  include <sys/param.h>
62 # endif
63 # ifndef PATH_MAX
64 #  ifdef MAXPATHLEN
65 #   define PATH_MAX MAXPATHLEN
66 #  else
67 #   define PATH_MAX 1024
68 #  endif
69 # endif
70 #endif
71
72 #if __GNUC__ >= 2
73 /* Define BFD64 here, even if our default architecture is 32 bit ELF
74    as this will allow us to read in and parse 64bit and 32bit ELF files.
75    Only do this if we believe that the compiler can support a 64 bit
76    data type.  For now we only rely on GCC being able to do this.  */
77 #define BFD64
78 #endif
79
80 #include "bfd.h"
81 #include "bucomm.h"
82 #include "dwarf.h"
83
84 #include "elf/common.h"
85 #include "elf/external.h"
86 #include "elf/internal.h"
87
88
89 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
90    we can obtain the H8 reloc numbers.  We need these for the
91    get_reloc_size() function.  We include h8.h again after defining
92    RELOC_MACROS_GEN_FUNC so that we get the naming function as well.  */
93
94 #include "elf/h8.h"
95 #undef _ELF_H8_H
96
97 /* Undo the effects of #including reloc-macros.h.  */
98
99 #undef START_RELOC_NUMBERS
100 #undef RELOC_NUMBER
101 #undef FAKE_RELOC
102 #undef EMPTY_RELOC
103 #undef END_RELOC_NUMBERS
104 #undef _RELOC_MACROS_H
105
106 /* The following headers use the elf/reloc-macros.h file to
107    automatically generate relocation recognition functions
108    such as elf_mips_reloc_type()  */
109
110 #define RELOC_MACROS_GEN_FUNC
111
112 #include "elf/alpha.h"
113 #include "elf/arc.h"
114 #include "elf/arm.h"
115 #include "elf/avr.h"
116 #include "elf/bfin.h"
117 #include "elf/cr16.h"
118 #include "elf/cris.h"
119 #include "elf/crx.h"
120 #include "elf/d10v.h"
121 #include "elf/d30v.h"
122 #include "elf/dlx.h"
123 #include "elf/fr30.h"
124 #include "elf/frv.h"
125 #include "elf/h8.h"
126 #include "elf/hppa.h"
127 #include "elf/i386.h"
128 #include "elf/i370.h"
129 #include "elf/i860.h"
130 #include "elf/i960.h"
131 #include "elf/ia64.h"
132 #include "elf/ip2k.h"
133 #include "elf/iq2000.h"
134 #include "elf/m32c.h"
135 #include "elf/m32r.h"
136 #include "elf/m68k.h"
137 #include "elf/m68hc11.h"
138 #include "elf/mcore.h"
139 #include "elf/mep.h"
140 #include "elf/mips.h"
141 #include "elf/mmix.h"
142 #include "elf/mn10200.h"
143 #include "elf/mn10300.h"
144 #include "elf/mt.h"
145 #include "elf/msp430.h"
146 #include "elf/or32.h"
147 #include "elf/pj.h"
148 #include "elf/ppc.h"
149 #include "elf/ppc64.h"
150 #include "elf/s390.h"
151 #include "elf/score.h"
152 #include "elf/sh.h"
153 #include "elf/sparc.h"
154 #include "elf/spu.h"
155 #include "elf/v850.h"
156 #include "elf/vax.h"
157 #include "elf/x86-64.h"
158 #include "elf/xstormy16.h"
159 #include "elf/xtensa.h"
160
161 #include "aout/ar.h"
162
163 #include "getopt.h"
164 #include "libiberty.h"
165 #include "safe-ctype.h"
166
167 char *program_name = "readelf";
168 int do_wide;
169 static long archive_file_offset;
170 static unsigned long archive_file_size;
171 static unsigned long dynamic_addr;
172 static bfd_size_type dynamic_size;
173 static unsigned int 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_JMPREL + 1];
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_reloc;
196 static int do_sections;
197 static int do_section_groups;
198 static int do_section_details;
199 static int do_segments;
200 static int do_unwind;
201 static int do_using_dynamic;
202 static int do_header;
203 static int do_dump;
204 static int do_version;
205 static int do_histogram;
206 static int do_debugging;
207 static int do_arch;
208 static int do_notes;
209 static int do_archive_index;
210 static int is_32bit_elf;
211
212 struct group_list
213 {
214   struct group_list *next;
215   unsigned int section_index;
216 };
217
218 struct group
219 {
220   struct group_list *root;
221   unsigned int group_index;
222 };
223
224 static size_t group_count;
225 static struct group *section_groups;
226 static struct group **section_headers_groups;
227
228
229 /* Flag bits indicating particular types of dump.  */
230 #define HEX_DUMP        (1 << 0)        /* The -x command line switch.  */
231 #define DISASS_DUMP     (1 << 1)        /* The -i command line switch.  */
232 #define DEBUG_DUMP      (1 << 2)        /* The -w command line switch.  */
233 #define STRING_DUMP     (1 << 3)        /* The -p command line switch.  */
234
235 typedef unsigned char dump_type;
236
237 /* A linked list of the section names for which dumps were requested.  */
238 struct dump_list_entry
239 {
240   char *name;
241   dump_type type;
242   struct dump_list_entry *next;
243 };
244 static struct dump_list_entry *dump_sects_byname;
245
246 /* A dynamic array of flags indicating for which sections a dump
247    has been requested via command line switches.  */
248 static dump_type *   cmdline_dump_sects = NULL;
249 static unsigned int  num_cmdline_dump_sects = 0;
250
251 /* A dynamic array of flags indicating for which sections a dump of
252    some kind has been requested.  It is reset on a per-object file
253    basis and then initialised from the cmdline_dump_sects array,
254    the results of interpreting the -w switch, and the
255    dump_sects_byname list.  */
256 static dump_type *   dump_sects = NULL;
257 static unsigned int  num_dump_sects = 0;
258
259
260 /* How to print a vma value.  */
261 typedef enum print_mode
262 {
263   HEX,
264   DEC,
265   DEC_5,
266   UNSIGNED,
267   PREFIX_HEX,
268   FULL_HEX,
269   LONG_HEX
270 }
271 print_mode;
272
273 static void (*byte_put) (unsigned char *, bfd_vma, int);
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 BYTE_GET(field) byte_get (field, sizeof (field))
286
287 #define GET_ELF_SYMBOLS(file, section)                  \
288   (is_32bit_elf ? get_32bit_elf_symbols (file, section) \
289    : get_64bit_elf_symbols (file, section))
290
291 #define VALID_DYNAMIC_NAME(offset)      ((dynamic_strings != NULL) && (offset < dynamic_strings_length))
292 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
293    already been called and verified that the string exists.  */
294 #define GET_DYNAMIC_NAME(offset)        (dynamic_strings + offset)
295
296 /* This is just a bit of syntatic sugar.  */
297 #define streq(a,b)        (strcmp ((a), (b)) == 0)
298 #define strneq(a,b,n)     (strncmp ((a), (b), (n)) == 0)
299 #define const_strneq(a,b) (strncmp ((a), (b), sizeof (b) - 1) == 0)
300 \f
301 static void *
302 get_data (void *var, FILE *file, long offset, size_t size, size_t nmemb,
303           const char *reason)
304 {
305   void *mvar;
306
307   if (size == 0 || nmemb == 0)
308     return NULL;
309
310   if (fseek (file, archive_file_offset + offset, SEEK_SET))
311     {
312       error (_("Unable to seek to 0x%lx for %s\n"),
313              (unsigned long) archive_file_offset + offset, reason);
314       return NULL;
315     }
316
317   mvar = var;
318   if (mvar == NULL)
319     {
320       /* Check for overflow.  */
321       if (nmemb < (~(size_t) 0 - 1) / size)
322         /* + 1 so that we can '\0' terminate invalid string table sections.  */
323         mvar = malloc (size * nmemb + 1);
324
325       if (mvar == NULL)
326         {
327           error (_("Out of memory allocating 0x%lx bytes for %s\n"),
328                  (unsigned long)(size * nmemb), reason);
329           return NULL;
330         }
331
332       ((char *) mvar)[size * nmemb] = '\0';
333     }
334
335   if (fread (mvar, size, nmemb, file) != nmemb)
336     {
337       error (_("Unable to read in 0x%lx bytes of %s\n"),
338              (unsigned long)(size * nmemb), reason);
339       if (mvar != var)
340         free (mvar);
341       return NULL;
342     }
343
344   return mvar;
345 }
346
347 static void
348 byte_put_little_endian (unsigned char *field, bfd_vma value, int size)
349 {
350   switch (size)
351     {
352     case 8:
353       field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
354       field[6] = ((value >> 24) >> 24) & 0xff;
355       field[5] = ((value >> 24) >> 16) & 0xff;
356       field[4] = ((value >> 24) >> 8) & 0xff;
357       /* Fall through.  */
358     case 4:
359       field[3] = (value >> 24) & 0xff;
360       field[2] = (value >> 16) & 0xff;
361       /* Fall through.  */
362     case 2:
363       field[1] = (value >> 8) & 0xff;
364       /* Fall through.  */
365     case 1:
366       field[0] = value & 0xff;
367       break;
368
369     default:
370       error (_("Unhandled data length: %d\n"), size);
371       abort ();
372     }
373 }
374
375 /* Print a VMA value.  */
376
377 static int
378 print_vma (bfd_vma vma, print_mode mode)
379 {
380   int nc = 0;
381
382   switch (mode)
383     {
384     case FULL_HEX:
385       nc = printf ("0x");
386       /* Drop through.  */
387
388     case LONG_HEX:
389 #ifdef BFD64
390       if (is_32bit_elf)
391         return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
392 #endif
393       printf_vma (vma);
394       return nc + 16;
395
396     case DEC_5:
397       if (vma <= 99999)
398         return printf ("%5" BFD_VMA_FMT "d", vma);
399       /* Drop through.  */
400
401     case PREFIX_HEX:
402       nc = printf ("0x");
403       /* Drop through.  */
404
405     case HEX:
406       return nc + printf ("%" BFD_VMA_FMT "x", vma);
407
408     case DEC:
409       return printf ("%" BFD_VMA_FMT "d", vma);
410
411     case UNSIGNED:
412       return printf ("%" BFD_VMA_FMT "u", vma);
413     }
414   return 0;
415 }
416
417 /* Display a symbol on stdout.  Handles the display of non-printing characters.
418
419    If DO_WIDE is not true then format the symbol to be at most WIDTH characters,
420    truncating as necessary.  If WIDTH is negative then format the string to be
421    exactly - WIDTH characters, truncating or padding as necessary.
422
423    Returns the number of emitted characters.  */
424
425 static unsigned int
426 print_symbol (int width, const char *symbol)
427 {
428   const char * c;
429   bfd_boolean extra_padding = FALSE;
430   unsigned int num_printed = 0;
431
432   if (do_wide)
433     {
434       /* Set the width to a very large value.  This simplifies the code below.  */
435       width = INT_MAX;
436     }
437   else if (width < 0)
438     {
439       /* Keep the width positive.  This also helps.  */
440       width = - width;
441       extra_padding = TRUE;
442     }
443
444   while (width)
445     {
446       int len;
447
448       c = symbol;
449
450       /* Look for non-printing symbols inside the symbol's name.
451          This test is triggered in particular by the names generated
452          by the assembler for local labels.  */
453       while (ISPRINT (* c))
454         c++;
455
456       len = c - symbol;
457
458       if (len)
459         {
460           if (len > width)
461             len = width;
462
463           printf ("%.*s", len, symbol);
464
465           width -= len;
466           num_printed += len;
467         }
468
469       if (* c == 0 || width == 0)
470         break;
471
472       /* Now display the non-printing character, if
473          there is room left in which to dipslay it.  */
474       if (*c < 32)
475         {
476           if (width < 2)
477             break;
478
479           printf ("^%c", *c + 0x40);
480
481           width -= 2;
482           num_printed += 2;
483         }
484       else
485         {
486           if (width < 6)
487             break;
488
489           printf ("<0x%.2x>", *c);
490
491           width -= 6;
492           num_printed += 6;
493         }
494
495       symbol = c + 1;
496     }
497
498   if (extra_padding && width > 0)
499     {
500       /* Fill in the remaining spaces.  */
501       printf ("%-*s", width, " ");
502       num_printed += 2;
503     }
504
505   return num_printed;
506 }
507
508 static void
509 byte_put_big_endian (unsigned char *field, bfd_vma value, int size)
510 {
511   switch (size)
512     {
513     case 8:
514       field[7] = value & 0xff;
515       field[6] = (value >> 8) & 0xff;
516       field[5] = (value >> 16) & 0xff;
517       field[4] = (value >> 24) & 0xff;
518       value >>= 16;
519       value >>= 16;
520       /* Fall through.  */
521     case 4:
522       field[3] = value & 0xff;
523       field[2] = (value >> 8) & 0xff;
524       value >>= 16;
525       /* Fall through.  */
526     case 2:
527       field[1] = value & 0xff;
528       value >>= 8;
529       /* Fall through.  */
530     case 1:
531       field[0] = value & 0xff;
532       break;
533
534     default:
535       error (_("Unhandled data length: %d\n"), size);
536       abort ();
537     }
538 }
539
540 /* Return a pointer to section NAME, or NULL if no such section exists.  */
541
542 static Elf_Internal_Shdr *
543 find_section (const char *name)
544 {
545   unsigned int i;
546
547   for (i = 0; i < elf_header.e_shnum; i++)
548     if (streq (SECTION_NAME (section_headers + i), name))
549       return section_headers + i;
550
551   return NULL;
552 }
553
554 /* Guess the relocation size commonly used by the specific machines.  */
555
556 static int
557 guess_is_rela (unsigned int e_machine)
558 {
559   switch (e_machine)
560     {
561       /* Targets that use REL relocations.  */
562     case EM_386:
563     case EM_486:
564     case EM_960:
565     case EM_ARM:
566     case EM_D10V:
567     case EM_CYGNUS_D10V:
568     case EM_DLX:
569     case EM_MIPS:
570     case EM_MIPS_RS3_LE:
571     case EM_CYGNUS_M32R:
572     case EM_OPENRISC:
573     case EM_OR32:
574     case EM_SCORE:
575       return FALSE;
576
577       /* Targets that use RELA relocations.  */
578     case EM_68K:
579     case EM_860:
580     case EM_ALPHA:
581     case EM_ALTERA_NIOS2:
582     case EM_AVR:
583     case EM_AVR_OLD:
584     case EM_BLACKFIN:
585     case EM_CR16:
586     case EM_CR16_OLD:
587     case EM_CRIS:
588     case EM_CRX:
589     case EM_D30V:
590     case EM_CYGNUS_D30V:
591     case EM_FR30:
592     case EM_CYGNUS_FR30:
593     case EM_CYGNUS_FRV:
594     case EM_H8S:
595     case EM_H8_300:
596     case EM_H8_300H:
597     case EM_IA_64:
598     case EM_IP2K:
599     case EM_IP2K_OLD:
600     case EM_IQ2000:
601     case EM_M32C_OLD:
602     case EM_M32C:
603     case EM_M32R:
604     case EM_MCORE:
605     case EM_CYGNUS_MEP:
606     case EM_MMIX:
607     case EM_MN10200:
608     case EM_CYGNUS_MN10200:
609     case EM_MN10300:
610     case EM_CYGNUS_MN10300:
611     case EM_MSP430:
612     case EM_MSP430_OLD:
613     case EM_MT:
614     case EM_NIOS32:
615     case EM_PPC64:
616     case EM_PPC:
617     case EM_S390:
618     case EM_S390_OLD:
619     case EM_SH:
620     case EM_SPARC:
621     case EM_SPARC32PLUS:
622     case EM_SPARCV9:
623     case EM_SPU:
624     case EM_V850:
625     case EM_CYGNUS_V850:
626     case EM_VAX:
627     case EM_X86_64:
628     case EM_XSTORMY16:
629     case EM_XTENSA:
630     case EM_XTENSA_OLD:
631       return TRUE;
632
633     case EM_68HC05:
634     case EM_68HC08:
635     case EM_68HC11:
636     case EM_68HC16:
637     case EM_FX66:
638     case EM_ME16:
639     case EM_MMA:
640     case EM_NCPU:
641     case EM_NDR1:
642     case EM_PCP:
643     case EM_ST100:
644     case EM_ST19:
645     case EM_ST7:
646     case EM_ST9PLUS:
647     case EM_STARCORE:
648     case EM_SVX:
649     case EM_TINYJ:
650     default:
651       warn (_("Don't know about relocations on this machine architecture\n"));
652       return FALSE;
653     }
654 }
655
656 static int
657 slurp_rela_relocs (FILE *file,
658                    unsigned long rel_offset,
659                    unsigned long rel_size,
660                    Elf_Internal_Rela **relasp,
661                    unsigned long *nrelasp)
662 {
663   Elf_Internal_Rela *relas;
664   unsigned long nrelas;
665   unsigned int i;
666
667   if (is_32bit_elf)
668     {
669       Elf32_External_Rela *erelas;
670
671       erelas = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
672       if (!erelas)
673         return 0;
674
675       nrelas = rel_size / sizeof (Elf32_External_Rela);
676
677       relas = cmalloc (nrelas, sizeof (Elf_Internal_Rela));
678
679       if (relas == NULL)
680         {
681           free (erelas);
682           error (_("out of memory parsing relocs\n"));
683           return 0;
684         }
685
686       for (i = 0; i < nrelas; i++)
687         {
688           relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
689           relas[i].r_info   = BYTE_GET (erelas[i].r_info);
690           relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
691         }
692
693       free (erelas);
694     }
695   else
696     {
697       Elf64_External_Rela *erelas;
698
699       erelas = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
700       if (!erelas)
701         return 0;
702
703       nrelas = rel_size / sizeof (Elf64_External_Rela);
704
705       relas = cmalloc (nrelas, sizeof (Elf_Internal_Rela));
706
707       if (relas == NULL)
708         {
709           free (erelas);
710           error (_("out of memory parsing relocs\n"));
711           return 0;
712         }
713
714       for (i = 0; i < nrelas; i++)
715         {
716           relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
717           relas[i].r_info   = BYTE_GET (erelas[i].r_info);
718           relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
719
720           /* The #ifdef BFD64 below is to prevent a compile time
721              warning.  We know that if we do not have a 64 bit data
722              type that we will never execute this code anyway.  */
723 #ifdef BFD64
724           if (elf_header.e_machine == EM_MIPS
725               && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
726             {
727               /* In little-endian objects, r_info isn't really a
728                  64-bit little-endian value: it has a 32-bit
729                  little-endian symbol index followed by four
730                  individual byte fields.  Reorder INFO
731                  accordingly.  */
732               bfd_vma info = relas[i].r_info;
733               info = (((info & 0xffffffff) << 32)
734                       | ((info >> 56) & 0xff)
735                       | ((info >> 40) & 0xff00)
736                       | ((info >> 24) & 0xff0000)
737                       | ((info >> 8) & 0xff000000));
738               relas[i].r_info = info;
739             }
740 #endif /* BFD64 */
741         }
742
743       free (erelas);
744     }
745   *relasp = relas;
746   *nrelasp = nrelas;
747   return 1;
748 }
749
750 static int
751 slurp_rel_relocs (FILE *file,
752                   unsigned long rel_offset,
753                   unsigned long rel_size,
754                   Elf_Internal_Rela **relsp,
755                   unsigned long *nrelsp)
756 {
757   Elf_Internal_Rela *rels;
758   unsigned long nrels;
759   unsigned int i;
760
761   if (is_32bit_elf)
762     {
763       Elf32_External_Rel *erels;
764
765       erels = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
766       if (!erels)
767         return 0;
768
769       nrels = rel_size / sizeof (Elf32_External_Rel);
770
771       rels = cmalloc (nrels, sizeof (Elf_Internal_Rela));
772
773       if (rels == NULL)
774         {
775           free (erels);
776           error (_("out of memory parsing relocs\n"));
777           return 0;
778         }
779
780       for (i = 0; i < nrels; i++)
781         {
782           rels[i].r_offset = BYTE_GET (erels[i].r_offset);
783           rels[i].r_info   = BYTE_GET (erels[i].r_info);
784           rels[i].r_addend = 0;
785         }
786
787       free (erels);
788     }
789   else
790     {
791       Elf64_External_Rel *erels;
792
793       erels = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
794       if (!erels)
795         return 0;
796
797       nrels = rel_size / sizeof (Elf64_External_Rel);
798
799       rels = cmalloc (nrels, sizeof (Elf_Internal_Rela));
800
801       if (rels == NULL)
802         {
803           free (erels);
804           error (_("out of memory parsing relocs\n"));
805           return 0;
806         }
807
808       for (i = 0; i < nrels; i++)
809         {
810           rels[i].r_offset = BYTE_GET (erels[i].r_offset);
811           rels[i].r_info   = BYTE_GET (erels[i].r_info);
812           rels[i].r_addend = 0;
813
814           /* The #ifdef BFD64 below is to prevent a compile time
815              warning.  We know that if we do not have a 64 bit data
816              type that we will never execute this code anyway.  */
817 #ifdef BFD64
818           if (elf_header.e_machine == EM_MIPS
819               && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
820             {
821               /* In little-endian objects, r_info isn't really a
822                  64-bit little-endian value: it has a 32-bit
823                  little-endian symbol index followed by four
824                  individual byte fields.  Reorder INFO
825                  accordingly.  */
826               bfd_vma info = rels[i].r_info;
827               info = (((info & 0xffffffff) << 32)
828                       | ((info >> 56) & 0xff)
829                       | ((info >> 40) & 0xff00)
830                       | ((info >> 24) & 0xff0000)
831                       | ((info >> 8) & 0xff000000));
832               rels[i].r_info = info;
833             }
834 #endif /* BFD64 */
835         }
836
837       free (erels);
838     }
839   *relsp = rels;
840   *nrelsp = nrels;
841   return 1;
842 }
843
844 /* Returns the reloc type extracted from the reloc info field.  */
845
846 static unsigned int
847 get_reloc_type (bfd_vma reloc_info)
848 {
849   if (is_32bit_elf)
850     return ELF32_R_TYPE (reloc_info);
851
852   switch (elf_header.e_machine)
853     {
854     case EM_MIPS:
855       /* Note: We assume that reloc_info has already been adjusted for us.  */
856       return ELF64_MIPS_R_TYPE (reloc_info);
857
858     case EM_SPARCV9:
859       return ELF64_R_TYPE_ID (reloc_info);
860
861     default:
862       return ELF64_R_TYPE (reloc_info);
863     }
864 }
865
866 /* Return the symbol index extracted from the reloc info field.  */
867
868 static bfd_vma
869 get_reloc_symindex (bfd_vma reloc_info)
870 {
871   return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
872 }
873
874 /* Display the contents of the relocation data found at the specified
875    offset.  */
876
877 static void
878 dump_relocations (FILE *file,
879                   unsigned long rel_offset,
880                   unsigned long rel_size,
881                   Elf_Internal_Sym *symtab,
882                   unsigned long nsyms,
883                   char *strtab,
884                   unsigned long strtablen,
885                   int is_rela)
886 {
887   unsigned int i;
888   Elf_Internal_Rela *rels;
889
890
891   if (is_rela == UNKNOWN)
892     is_rela = guess_is_rela (elf_header.e_machine);
893
894   if (is_rela)
895     {
896       if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
897         return;
898     }
899   else
900     {
901       if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
902         return;
903     }
904
905   if (is_32bit_elf)
906     {
907       if (is_rela)
908         {
909           if (do_wide)
910             printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name + Addend\n"));
911           else
912             printf (_(" Offset     Info    Type            Sym.Value  Sym. Name + Addend\n"));
913         }
914       else
915         {
916           if (do_wide)
917             printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name\n"));
918           else
919             printf (_(" Offset     Info    Type            Sym.Value  Sym. Name\n"));
920         }
921     }
922   else
923     {
924       if (is_rela)
925         {
926           if (do_wide)
927             printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend\n"));
928           else
929             printf (_("  Offset          Info           Type           Sym. Value    Sym. Name + Addend\n"));
930         }
931       else
932         {
933           if (do_wide)
934             printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name\n"));
935           else
936             printf (_("  Offset          Info           Type           Sym. Value    Sym. Name\n"));
937         }
938     }
939
940   for (i = 0; i < rel_size; i++)
941     {
942       const char *rtype;
943       bfd_vma offset;
944       bfd_vma info;
945       bfd_vma symtab_index;
946       bfd_vma type;
947
948       offset = rels[i].r_offset;
949       info   = rels[i].r_info;
950
951       type = get_reloc_type (info);
952       symtab_index = get_reloc_symindex  (info);
953
954       if (is_32bit_elf)
955         {
956           printf ("%8.8lx  %8.8lx ",
957                   (unsigned long) offset & 0xffffffff,
958                   (unsigned long) info & 0xffffffff);
959         }
960       else
961         {
962 #if BFD_HOST_64BIT_LONG
963           printf (do_wide
964                   ? "%16.16lx  %16.16lx "
965                   : "%12.12lx  %12.12lx ",
966                   offset, info);
967 #elif BFD_HOST_64BIT_LONG_LONG
968 #ifndef __MSVCRT__
969           printf (do_wide
970                   ? "%16.16llx  %16.16llx "
971                   : "%12.12llx  %12.12llx ",
972                   offset, info);
973 #else
974           printf (do_wide
975                   ? "%16.16I64x  %16.16I64x "
976                   : "%12.12I64x  %12.12I64x ",
977                   offset, info);
978 #endif
979 #else
980           printf (do_wide
981                   ? "%8.8lx%8.8lx  %8.8lx%8.8lx "
982                   : "%4.4lx%8.8lx  %4.4lx%8.8lx ",
983                   _bfd_int64_high (offset),
984                   _bfd_int64_low (offset),
985                   _bfd_int64_high (info),
986                   _bfd_int64_low (info));
987 #endif
988         }
989
990       switch (elf_header.e_machine)
991         {
992         default:
993           rtype = NULL;
994           break;
995
996         case EM_M32R:
997         case EM_CYGNUS_M32R:
998           rtype = elf_m32r_reloc_type (type);
999           break;
1000
1001         case EM_386:
1002         case EM_486:
1003           rtype = elf_i386_reloc_type (type);
1004           break;
1005
1006         case EM_68HC11:
1007         case EM_68HC12:
1008           rtype = elf_m68hc11_reloc_type (type);
1009           break;
1010
1011         case EM_68K:
1012           rtype = elf_m68k_reloc_type (type);
1013           break;
1014
1015         case EM_960:
1016           rtype = elf_i960_reloc_type (type);
1017           break;
1018
1019         case EM_AVR:
1020         case EM_AVR_OLD:
1021           rtype = elf_avr_reloc_type (type);
1022           break;
1023
1024         case EM_OLD_SPARCV9:
1025         case EM_SPARC32PLUS:
1026         case EM_SPARCV9:
1027         case EM_SPARC:
1028           rtype = elf_sparc_reloc_type (type);
1029           break;
1030
1031         case EM_SPU:
1032           rtype = elf_spu_reloc_type (type);
1033           break;
1034
1035         case EM_V850:
1036         case EM_CYGNUS_V850:
1037           rtype = v850_reloc_type (type);
1038           break;
1039
1040         case EM_D10V:
1041         case EM_CYGNUS_D10V:
1042           rtype = elf_d10v_reloc_type (type);
1043           break;
1044
1045         case EM_D30V:
1046         case EM_CYGNUS_D30V:
1047           rtype = elf_d30v_reloc_type (type);
1048           break;
1049
1050         case EM_DLX:
1051           rtype = elf_dlx_reloc_type (type);
1052           break;
1053
1054         case EM_SH:
1055           rtype = elf_sh_reloc_type (type);
1056           break;
1057
1058         case EM_MN10300:
1059         case EM_CYGNUS_MN10300:
1060           rtype = elf_mn10300_reloc_type (type);
1061           break;
1062
1063         case EM_MN10200:
1064         case EM_CYGNUS_MN10200:
1065           rtype = elf_mn10200_reloc_type (type);
1066           break;
1067
1068         case EM_FR30:
1069         case EM_CYGNUS_FR30:
1070           rtype = elf_fr30_reloc_type (type);
1071           break;
1072
1073         case EM_CYGNUS_FRV:
1074           rtype = elf_frv_reloc_type (type);
1075           break;
1076
1077         case EM_MCORE:
1078           rtype = elf_mcore_reloc_type (type);
1079           break;
1080
1081         case EM_MMIX:
1082           rtype = elf_mmix_reloc_type (type);
1083           break;
1084
1085         case EM_MSP430:
1086         case EM_MSP430_OLD:
1087           rtype = elf_msp430_reloc_type (type);
1088           break;
1089
1090         case EM_PPC:
1091           rtype = elf_ppc_reloc_type (type);
1092           break;
1093
1094         case EM_PPC64:
1095           rtype = elf_ppc64_reloc_type (type);
1096           break;
1097
1098         case EM_MIPS:
1099         case EM_MIPS_RS3_LE:
1100           rtype = elf_mips_reloc_type (type);
1101           break;
1102
1103         case EM_ALPHA:
1104           rtype = elf_alpha_reloc_type (type);
1105           break;
1106
1107         case EM_ARM:
1108           rtype = elf_arm_reloc_type (type);
1109           break;
1110
1111         case EM_ARC:
1112           rtype = elf_arc_reloc_type (type);
1113           break;
1114
1115         case EM_PARISC:
1116           rtype = elf_hppa_reloc_type (type);
1117           break;
1118
1119         case EM_H8_300:
1120         case EM_H8_300H:
1121         case EM_H8S:
1122           rtype = elf_h8_reloc_type (type);
1123           break;
1124
1125         case EM_OPENRISC:
1126         case EM_OR32:
1127           rtype = elf_or32_reloc_type (type);
1128           break;
1129
1130         case EM_PJ:
1131         case EM_PJ_OLD:
1132           rtype = elf_pj_reloc_type (type);
1133           break;
1134         case EM_IA_64:
1135           rtype = elf_ia64_reloc_type (type);
1136           break;
1137
1138         case EM_CRIS:
1139           rtype = elf_cris_reloc_type (type);
1140           break;
1141
1142         case EM_860:
1143           rtype = elf_i860_reloc_type (type);
1144           break;
1145
1146         case EM_X86_64:
1147           rtype = elf_x86_64_reloc_type (type);
1148           break;
1149
1150         case EM_S370:
1151           rtype = i370_reloc_type (type);
1152           break;
1153
1154         case EM_S390_OLD:
1155         case EM_S390:
1156           rtype = elf_s390_reloc_type (type);
1157           break;
1158
1159         case EM_SCORE:
1160           rtype = elf_score_reloc_type (type);
1161           break;
1162
1163         case EM_XSTORMY16:
1164           rtype = elf_xstormy16_reloc_type (type);
1165           break;
1166
1167         case EM_CRX:
1168           rtype = elf_crx_reloc_type (type);
1169           break;
1170
1171         case EM_VAX:
1172           rtype = elf_vax_reloc_type (type);
1173           break;
1174
1175         case EM_IP2K:
1176         case EM_IP2K_OLD:
1177           rtype = elf_ip2k_reloc_type (type);
1178           break;
1179
1180         case EM_IQ2000:
1181           rtype = elf_iq2000_reloc_type (type);
1182           break;
1183
1184         case EM_XTENSA_OLD:
1185         case EM_XTENSA:
1186           rtype = elf_xtensa_reloc_type (type);
1187           break;
1188
1189         case EM_M32C_OLD:
1190         case EM_M32C:
1191           rtype = elf_m32c_reloc_type (type);
1192           break;
1193
1194         case EM_MT:
1195           rtype = elf_mt_reloc_type (type);
1196           break;
1197
1198         case EM_BLACKFIN:
1199           rtype = elf_bfin_reloc_type (type);
1200           break;
1201
1202         case EM_CYGNUS_MEP:
1203           rtype = elf_mep_reloc_type (type);
1204           break;
1205
1206         case EM_CR16:
1207         case EM_CR16_OLD:
1208           rtype = elf_cr16_reloc_type (type);
1209           break;
1210         }
1211
1212       if (rtype == NULL)
1213         printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1214       else
1215         printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1216
1217       if (elf_header.e_machine == EM_ALPHA
1218           && rtype != NULL
1219           && streq (rtype, "R_ALPHA_LITUSE")
1220           && is_rela)
1221         {
1222           switch (rels[i].r_addend)
1223             {
1224             case LITUSE_ALPHA_ADDR:   rtype = "ADDR";   break;
1225             case LITUSE_ALPHA_BASE:   rtype = "BASE";   break;
1226             case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1227             case LITUSE_ALPHA_JSR:    rtype = "JSR";    break;
1228             case LITUSE_ALPHA_TLSGD:  rtype = "TLSGD";  break;
1229             case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1230             case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1231             default: rtype = NULL;
1232             }
1233           if (rtype)
1234             printf (" (%s)", rtype);
1235           else
1236             {
1237               putchar (' ');
1238               printf (_("<unknown addend: %lx>"),
1239                       (unsigned long) rels[i].r_addend);
1240             }
1241         }
1242       else if (symtab_index)
1243         {
1244           if (symtab == NULL || symtab_index >= nsyms)
1245             printf (" bad symbol index: %08lx", (unsigned long) symtab_index);
1246           else
1247             {
1248               Elf_Internal_Sym *psym;
1249
1250               psym = symtab + symtab_index;
1251
1252               printf (" ");
1253
1254               if (ELF_ST_TYPE (psym->st_info) == STT_IFUNC)
1255                 {
1256                   const char * name;
1257                   unsigned int len;
1258                   unsigned int width = is_32bit_elf ? 8 : 14;
1259
1260                   /* Relocations against IFUNC symbols do not use the value of
1261                      the symbol as the address to relocate against.  Instead
1262                      they invoke the function named by the symbol and use its
1263                      result as the address for relocation.
1264
1265                      To indicate this to the user, do not display the value of
1266                      the symbol in the "Symbols's Value" field.  Instead show
1267                      its name followed by () as a hint that the symbol is
1268                      invoked.  */
1269
1270                   if (strtab == NULL
1271                       || psym->st_name == 0
1272                       || psym->st_name >= strtablen)
1273                     name = "??";
1274                   else
1275                     name = strtab + psym->st_name;
1276
1277                   len = print_symbol (width, name);
1278                   printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1279                 }
1280               else
1281                 {
1282                   print_vma (psym->st_value, LONG_HEX);
1283
1284                   printf (is_32bit_elf ? "   " : " ");
1285                 }
1286
1287               if (psym->st_name == 0)
1288                 {
1289                   const char *sec_name = "<null>";
1290                   char name_buf[40];
1291
1292                   if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1293                     {
1294                       if (psym->st_shndx < elf_header.e_shnum)
1295                         sec_name
1296                           = SECTION_NAME (section_headers + psym->st_shndx);
1297                       else if (psym->st_shndx == SHN_ABS)
1298                         sec_name = "ABS";
1299                       else if (psym->st_shndx == SHN_COMMON)
1300                         sec_name = "COMMON";
1301                       else if (elf_header.e_machine == EM_MIPS
1302                                && psym->st_shndx == SHN_MIPS_SCOMMON)
1303                         sec_name = "SCOMMON";
1304                       else if (elf_header.e_machine == EM_MIPS
1305                                && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1306                         sec_name = "SUNDEF";
1307                       else if (elf_header.e_machine == EM_X86_64
1308                                && psym->st_shndx == SHN_X86_64_LCOMMON)
1309                         sec_name = "LARGE_COMMON";
1310                       else if (elf_header.e_machine == EM_IA_64
1311                                && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1312                                && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1313                         sec_name = "ANSI_COM";
1314                       else if (elf_header.e_machine == EM_IA_64
1315                                && (elf_header.e_ident[EI_OSABI]
1316                                    == ELFOSABI_OPENVMS)
1317                                && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1318                         sec_name = "VMS_SYMVEC";
1319                       else
1320                         {
1321                           sprintf (name_buf, "<section 0x%x>",
1322                                    (unsigned int) psym->st_shndx);
1323                           sec_name = name_buf;
1324                         }
1325                     }
1326                   print_symbol (22, sec_name);
1327                 }
1328               else if (strtab == NULL)
1329                 printf (_("<string table index: %3ld>"), psym->st_name);
1330               else if (psym->st_name >= strtablen)
1331                 printf (_("<corrupt string table index: %3ld>"), psym->st_name);
1332               else
1333                 print_symbol (22, strtab + psym->st_name);
1334
1335               if (is_rela)
1336                 {
1337                   long offset = (long) (bfd_signed_vma) rels[i].r_addend;
1338
1339                   if (offset < 0)
1340                     printf (" - %lx", - offset);
1341                   else
1342                     printf (" + %lx", offset);
1343                 }
1344             }
1345         }
1346       else if (is_rela)
1347         {
1348           printf ("%*c", is_32bit_elf ?
1349                   (do_wide ? 34 : 28) : (do_wide ? 26 : 20), ' ');
1350           print_vma (rels[i].r_addend, LONG_HEX);
1351         }
1352
1353       if (elf_header.e_machine == EM_SPARCV9
1354           && rtype != NULL
1355           && streq (rtype, "R_SPARC_OLO10"))
1356         printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (info));
1357
1358       putchar ('\n');
1359
1360 #ifdef BFD64
1361       if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1362         {
1363           bfd_vma type2 = ELF64_MIPS_R_TYPE2 (info);
1364           bfd_vma type3 = ELF64_MIPS_R_TYPE3 (info);
1365           const char *rtype2 = elf_mips_reloc_type (type2);
1366           const char *rtype3 = elf_mips_reloc_type (type3);
1367
1368           printf ("                    Type2: ");
1369
1370           if (rtype2 == NULL)
1371             printf (_("unrecognized: %-7lx"),
1372                     (unsigned long) type2 & 0xffffffff);
1373           else
1374             printf ("%-17.17s", rtype2);
1375
1376           printf ("\n                    Type3: ");
1377
1378           if (rtype3 == NULL)
1379             printf (_("unrecognized: %-7lx"),
1380                     (unsigned long) type3 & 0xffffffff);
1381           else
1382             printf ("%-17.17s", rtype3);
1383
1384           putchar ('\n');
1385         }
1386 #endif /* BFD64 */
1387     }
1388
1389   free (rels);
1390 }
1391
1392 static const char *
1393 get_mips_dynamic_type (unsigned long type)
1394 {
1395   switch (type)
1396     {
1397     case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1398     case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1399     case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1400     case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1401     case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1402     case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1403     case DT_MIPS_MSYM: return "MIPS_MSYM";
1404     case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1405     case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1406     case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1407     case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1408     case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1409     case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1410     case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1411     case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1412     case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1413     case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1414     case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1415     case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1416     case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1417     case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1418     case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1419     case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1420     case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1421     case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1422     case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1423     case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1424     case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1425     case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1426     case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1427     case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1428     case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1429     case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1430     case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1431     case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1432     case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1433     case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1434     case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1435     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1436     case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1437     case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1438     case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1439     case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1440     case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1441     case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1442     default:
1443       return NULL;
1444     }
1445 }
1446
1447 static const char *
1448 get_sparc64_dynamic_type (unsigned long type)
1449 {
1450   switch (type)
1451     {
1452     case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1453     default:
1454       return NULL;
1455     }
1456 }
1457
1458 static const char *
1459 get_ppc_dynamic_type (unsigned long type)
1460 {
1461   switch (type)
1462     {
1463     case DT_PPC_GOT: return "PPC_GOT";
1464     default:
1465       return NULL;
1466     }
1467 }
1468
1469 static const char *
1470 get_ppc64_dynamic_type (unsigned long type)
1471 {
1472   switch (type)
1473     {
1474     case DT_PPC64_GLINK: return "PPC64_GLINK";
1475     case DT_PPC64_OPD:   return "PPC64_OPD";
1476     case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1477     default:
1478       return NULL;
1479     }
1480 }
1481
1482 static const char *
1483 get_parisc_dynamic_type (unsigned long type)
1484 {
1485   switch (type)
1486     {
1487     case DT_HP_LOAD_MAP:        return "HP_LOAD_MAP";
1488     case DT_HP_DLD_FLAGS:       return "HP_DLD_FLAGS";
1489     case DT_HP_DLD_HOOK:        return "HP_DLD_HOOK";
1490     case DT_HP_UX10_INIT:       return "HP_UX10_INIT";
1491     case DT_HP_UX10_INITSZ:     return "HP_UX10_INITSZ";
1492     case DT_HP_PREINIT:         return "HP_PREINIT";
1493     case DT_HP_PREINITSZ:       return "HP_PREINITSZ";
1494     case DT_HP_NEEDED:          return "HP_NEEDED";
1495     case DT_HP_TIME_STAMP:      return "HP_TIME_STAMP";
1496     case DT_HP_CHECKSUM:        return "HP_CHECKSUM";
1497     case DT_HP_GST_SIZE:        return "HP_GST_SIZE";
1498     case DT_HP_GST_VERSION:     return "HP_GST_VERSION";
1499     case DT_HP_GST_HASHVAL:     return "HP_GST_HASHVAL";
1500     case DT_HP_EPLTREL:         return "HP_GST_EPLTREL";
1501     case DT_HP_EPLTRELSZ:       return "HP_GST_EPLTRELSZ";
1502     case DT_HP_FILTERED:        return "HP_FILTERED";
1503     case DT_HP_FILTER_TLS:      return "HP_FILTER_TLS";
1504     case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1505     case DT_HP_LAZYLOAD:        return "HP_LAZYLOAD";
1506     case DT_HP_BIND_NOW_COUNT:  return "HP_BIND_NOW_COUNT";
1507     case DT_PLT:                return "PLT";
1508     case DT_PLT_SIZE:           return "PLT_SIZE";
1509     case DT_DLT:                return "DLT";
1510     case DT_DLT_SIZE:           return "DLT_SIZE";
1511     default:
1512       return NULL;
1513     }
1514 }
1515
1516 static const char *
1517 get_ia64_dynamic_type (unsigned long type)
1518 {
1519   switch (type)
1520     {
1521     case DT_IA_64_PLT_RESERVE:         return "IA_64_PLT_RESERVE";
1522     case DT_IA_64_VMS_SUBTYPE:         return "VMS_SUBTYPE";
1523     case DT_IA_64_VMS_IMGIOCNT:        return "VMS_IMGIOCNT";
1524     case DT_IA_64_VMS_LNKFLAGS:        return "VMS_LNKFLAGS";
1525     case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1526     case DT_IA_64_VMS_IDENT:           return "VMS_IDENT";
1527     case DT_IA_64_VMS_NEEDED_IDENT:    return "VMS_NEEDED_IDENT";
1528     case DT_IA_64_VMS_IMG_RELA_CNT:    return "VMS_IMG_RELA_CNT";
1529     case DT_IA_64_VMS_SEG_RELA_CNT:    return "VMS_SEG_RELA_CNT";
1530     case DT_IA_64_VMS_FIXUP_RELA_CNT:  return "VMS_FIXUP_RELA_CNT";
1531     case DT_IA_64_VMS_FIXUP_NEEDED:    return "VMS_FIXUP_NEEDED";
1532     case DT_IA_64_VMS_SYMVEC_CNT:      return "VMS_SYMVEC_CNT";
1533     case DT_IA_64_VMS_XLATED:          return "VMS_XLATED";
1534     case DT_IA_64_VMS_STACKSIZE:       return "VMS_STACKSIZE";
1535     case DT_IA_64_VMS_UNWINDSZ:        return "VMS_UNWINDSZ";
1536     case DT_IA_64_VMS_UNWIND_CODSEG:   return "VMS_UNWIND_CODSEG";
1537     case DT_IA_64_VMS_UNWIND_INFOSEG:  return "VMS_UNWIND_INFOSEG";
1538     case DT_IA_64_VMS_LINKTIME:        return "VMS_LINKTIME";
1539     case DT_IA_64_VMS_SEG_NO:          return "VMS_SEG_NO";
1540     case DT_IA_64_VMS_SYMVEC_OFFSET:   return "VMS_SYMVEC_OFFSET";
1541     case DT_IA_64_VMS_SYMVEC_SEG:      return "VMS_SYMVEC_SEG";
1542     case DT_IA_64_VMS_UNWIND_OFFSET:   return "VMS_UNWIND_OFFSET";
1543     case DT_IA_64_VMS_UNWIND_SEG:      return "VMS_UNWIND_SEG";
1544     case DT_IA_64_VMS_STRTAB_OFFSET:   return "VMS_STRTAB_OFFSET";
1545     case DT_IA_64_VMS_SYSVER_OFFSET:   return "VMS_SYSVER_OFFSET";
1546     case DT_IA_64_VMS_IMG_RELA_OFF:    return "VMS_IMG_RELA_OFF";
1547     case DT_IA_64_VMS_SEG_RELA_OFF:    return "VMS_SEG_RELA_OFF";
1548     case DT_IA_64_VMS_FIXUP_RELA_OFF:  return "VMS_FIXUP_RELA_OFF";
1549     case DT_IA_64_VMS_PLTGOT_OFFSET:   return "VMS_PLTGOT_OFFSET";
1550     case DT_IA_64_VMS_PLTGOT_SEG:      return "VMS_PLTGOT_SEG";
1551     case DT_IA_64_VMS_FPMODE:          return "VMS_FPMODE";
1552     default:
1553       return NULL;
1554     }
1555 }
1556
1557 static const char *
1558 get_alpha_dynamic_type (unsigned long type)
1559 {
1560   switch (type)
1561     {
1562     case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1563     default:
1564       return NULL;
1565     }
1566 }
1567
1568 static const char *
1569 get_score_dynamic_type (unsigned long type)
1570 {
1571   switch (type)
1572     {
1573     case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
1574     case DT_SCORE_LOCAL_GOTNO:  return "SCORE_LOCAL_GOTNO";
1575     case DT_SCORE_SYMTABNO:     return "SCORE_SYMTABNO";
1576     case DT_SCORE_GOTSYM:       return "SCORE_GOTSYM";
1577     case DT_SCORE_UNREFEXTNO:   return "SCORE_UNREFEXTNO";
1578     case DT_SCORE_HIPAGENO:     return "SCORE_HIPAGENO";
1579     default:
1580       return NULL;
1581     }
1582 }
1583
1584
1585 static const char *
1586 get_dynamic_type (unsigned long type)
1587 {
1588   static char buff[64];
1589
1590   switch (type)
1591     {
1592     case DT_NULL:       return "NULL";
1593     case DT_NEEDED:     return "NEEDED";
1594     case DT_PLTRELSZ:   return "PLTRELSZ";
1595     case DT_PLTGOT:     return "PLTGOT";
1596     case DT_HASH:       return "HASH";
1597     case DT_STRTAB:     return "STRTAB";
1598     case DT_SYMTAB:     return "SYMTAB";
1599     case DT_RELA:       return "RELA";
1600     case DT_RELASZ:     return "RELASZ";
1601     case DT_RELAENT:    return "RELAENT";
1602     case DT_STRSZ:      return "STRSZ";
1603     case DT_SYMENT:     return "SYMENT";
1604     case DT_INIT:       return "INIT";
1605     case DT_FINI:       return "FINI";
1606     case DT_SONAME:     return "SONAME";
1607     case DT_RPATH:      return "RPATH";
1608     case DT_SYMBOLIC:   return "SYMBOLIC";
1609     case DT_REL:        return "REL";
1610     case DT_RELSZ:      return "RELSZ";
1611     case DT_RELENT:     return "RELENT";
1612     case DT_PLTREL:     return "PLTREL";
1613     case DT_DEBUG:      return "DEBUG";
1614     case DT_TEXTREL:    return "TEXTREL";
1615     case DT_JMPREL:     return "JMPREL";
1616     case DT_BIND_NOW:   return "BIND_NOW";
1617     case DT_INIT_ARRAY: return "INIT_ARRAY";
1618     case DT_FINI_ARRAY: return "FINI_ARRAY";
1619     case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
1620     case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
1621     case DT_RUNPATH:    return "RUNPATH";
1622     case DT_FLAGS:      return "FLAGS";
1623
1624     case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
1625     case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
1626
1627     case DT_CHECKSUM:   return "CHECKSUM";
1628     case DT_PLTPADSZ:   return "PLTPADSZ";
1629     case DT_MOVEENT:    return "MOVEENT";
1630     case DT_MOVESZ:     return "MOVESZ";
1631     case DT_FEATURE:    return "FEATURE";
1632     case DT_POSFLAG_1:  return "POSFLAG_1";
1633     case DT_SYMINSZ:    return "SYMINSZ";
1634     case DT_SYMINENT:   return "SYMINENT"; /* aka VALRNGHI */
1635
1636     case DT_ADDRRNGLO:  return "ADDRRNGLO";
1637     case DT_CONFIG:     return "CONFIG";
1638     case DT_DEPAUDIT:   return "DEPAUDIT";
1639     case DT_AUDIT:      return "AUDIT";
1640     case DT_PLTPAD:     return "PLTPAD";
1641     case DT_MOVETAB:    return "MOVETAB";
1642     case DT_SYMINFO:    return "SYMINFO"; /* aka ADDRRNGHI */
1643
1644     case DT_VERSYM:     return "VERSYM";
1645
1646     case DT_TLSDESC_GOT: return "TLSDESC_GOT";
1647     case DT_TLSDESC_PLT: return "TLSDESC_PLT";
1648     case DT_RELACOUNT:  return "RELACOUNT";
1649     case DT_RELCOUNT:   return "RELCOUNT";
1650     case DT_FLAGS_1:    return "FLAGS_1";
1651     case DT_VERDEF:     return "VERDEF";
1652     case DT_VERDEFNUM:  return "VERDEFNUM";
1653     case DT_VERNEED:    return "VERNEED";
1654     case DT_VERNEEDNUM: return "VERNEEDNUM";
1655
1656     case DT_AUXILIARY:  return "AUXILIARY";
1657     case DT_USED:       return "USED";
1658     case DT_FILTER:     return "FILTER";
1659
1660     case DT_GNU_PRELINKED: return "GNU_PRELINKED";
1661     case DT_GNU_CONFLICT: return "GNU_CONFLICT";
1662     case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
1663     case DT_GNU_LIBLIST: return "GNU_LIBLIST";
1664     case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
1665     case DT_GNU_HASH:   return "GNU_HASH";
1666
1667     default:
1668       if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
1669         {
1670           const char *result;
1671
1672           switch (elf_header.e_machine)
1673             {
1674             case EM_MIPS:
1675             case EM_MIPS_RS3_LE:
1676               result = get_mips_dynamic_type (type);
1677               break;
1678             case EM_SPARCV9:
1679               result = get_sparc64_dynamic_type (type);
1680               break;
1681             case EM_PPC:
1682               result = get_ppc_dynamic_type (type);
1683               break;
1684             case EM_PPC64:
1685               result = get_ppc64_dynamic_type (type);
1686               break;
1687             case EM_IA_64:
1688               result = get_ia64_dynamic_type (type);
1689               break;
1690             case EM_ALPHA:
1691               result = get_alpha_dynamic_type (type);
1692               break;
1693             case EM_SCORE:
1694               result = get_score_dynamic_type (type);
1695               break;
1696             default:
1697               result = NULL;
1698               break;
1699             }
1700
1701           if (result != NULL)
1702             return result;
1703
1704           snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
1705         }
1706       else if (((type >= DT_LOOS) && (type <= DT_HIOS))
1707                || (elf_header.e_machine == EM_PARISC
1708                    && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
1709         {
1710           const char *result;
1711
1712           switch (elf_header.e_machine)
1713             {
1714             case EM_PARISC:
1715               result = get_parisc_dynamic_type (type);
1716               break;
1717             case EM_IA_64:
1718               result = get_ia64_dynamic_type (type);
1719               break;
1720             default:
1721               result = NULL;
1722               break;
1723             }
1724
1725           if (result != NULL)
1726             return result;
1727
1728           snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
1729                     type);
1730         }
1731       else
1732         snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
1733
1734       return buff;
1735     }
1736 }
1737
1738 static char *
1739 get_file_type (unsigned e_type)
1740 {
1741   static char buff[32];
1742
1743   switch (e_type)
1744     {
1745     case ET_NONE:       return _("NONE (None)");
1746     case ET_REL:        return _("REL (Relocatable file)");
1747     case ET_EXEC:       return _("EXEC (Executable file)");
1748     case ET_DYN:        return _("DYN (Shared object file)");
1749     case ET_CORE:       return _("CORE (Core file)");
1750
1751     default:
1752       if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
1753         snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
1754       else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
1755         snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
1756       else
1757         snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
1758       return buff;
1759     }
1760 }
1761
1762 static char *
1763 get_machine_name (unsigned e_machine)
1764 {
1765   static char buff[64]; /* XXX */
1766
1767   switch (e_machine)
1768     {
1769     case EM_NONE:               return _("None");
1770     case EM_M32:                return "WE32100";
1771     case EM_SPARC:              return "Sparc";
1772     case EM_SPU:                return "SPU";
1773     case EM_386:                return "Intel 80386";
1774     case EM_68K:                return "MC68000";
1775     case EM_88K:                return "MC88000";
1776     case EM_486:                return "Intel 80486";
1777     case EM_860:                return "Intel 80860";
1778     case EM_MIPS:               return "MIPS R3000";
1779     case EM_S370:               return "IBM System/370";
1780     case EM_MIPS_RS3_LE:        return "MIPS R4000 big-endian";
1781     case EM_OLD_SPARCV9:        return "Sparc v9 (old)";
1782     case EM_PARISC:             return "HPPA";
1783     case EM_PPC_OLD:            return "Power PC (old)";
1784     case EM_SPARC32PLUS:        return "Sparc v8+" ;
1785     case EM_960:                return "Intel 90860";
1786     case EM_PPC:                return "PowerPC";
1787     case EM_PPC64:              return "PowerPC64";
1788     case EM_V800:               return "NEC V800";
1789     case EM_FR20:               return "Fujitsu FR20";
1790     case EM_RH32:               return "TRW RH32";
1791     case EM_MCORE:              return "MCORE";
1792     case EM_ARM:                return "ARM";
1793     case EM_OLD_ALPHA:          return "Digital Alpha (old)";
1794     case EM_SH:                 return "Renesas / SuperH SH";
1795     case EM_SPARCV9:            return "Sparc v9";
1796     case EM_TRICORE:            return "Siemens Tricore";
1797     case EM_ARC:                return "ARC";
1798     case EM_H8_300:             return "Renesas H8/300";
1799     case EM_H8_300H:            return "Renesas H8/300H";
1800     case EM_H8S:                return "Renesas H8S";
1801     case EM_H8_500:             return "Renesas H8/500";
1802     case EM_IA_64:              return "Intel IA-64";
1803     case EM_MIPS_X:             return "Stanford MIPS-X";
1804     case EM_COLDFIRE:           return "Motorola Coldfire";
1805     case EM_68HC12:             return "Motorola M68HC12";
1806     case EM_ALPHA:              return "Alpha";
1807     case EM_CYGNUS_D10V:
1808     case EM_D10V:               return "d10v";
1809     case EM_CYGNUS_D30V:
1810     case EM_D30V:               return "d30v";
1811     case EM_CYGNUS_M32R:
1812     case EM_M32R:               return "Renesas M32R (formerly Mitsubishi M32r)";
1813     case EM_CYGNUS_V850:
1814     case EM_V850:               return "NEC v850";
1815     case EM_CYGNUS_MN10300:
1816     case EM_MN10300:            return "mn10300";
1817     case EM_CYGNUS_MN10200:
1818     case EM_MN10200:            return "mn10200";
1819     case EM_CYGNUS_FR30:
1820     case EM_FR30:               return "Fujitsu FR30";
1821     case EM_CYGNUS_FRV:         return "Fujitsu FR-V";
1822     case EM_PJ_OLD:
1823     case EM_PJ:                 return "picoJava";
1824     case EM_MMA:                return "Fujitsu Multimedia Accelerator";
1825     case EM_PCP:                return "Siemens PCP";
1826     case EM_NCPU:               return "Sony nCPU embedded RISC processor";
1827     case EM_NDR1:               return "Denso NDR1 microprocesspr";
1828     case EM_STARCORE:           return "Motorola Star*Core processor";
1829     case EM_ME16:               return "Toyota ME16 processor";
1830     case EM_ST100:              return "STMicroelectronics ST100 processor";
1831     case EM_TINYJ:              return "Advanced Logic Corp. TinyJ embedded processor";
1832     case EM_FX66:               return "Siemens FX66 microcontroller";
1833     case EM_ST9PLUS:            return "STMicroelectronics ST9+ 8/16 bit microcontroller";
1834     case EM_ST7:                return "STMicroelectronics ST7 8-bit microcontroller";
1835     case EM_68HC16:             return "Motorola MC68HC16 Microcontroller";
1836     case EM_68HC11:             return "Motorola MC68HC11 Microcontroller";
1837     case EM_68HC08:             return "Motorola MC68HC08 Microcontroller";
1838     case EM_68HC05:             return "Motorola MC68HC05 Microcontroller";
1839     case EM_SVX:                return "Silicon Graphics SVx";
1840     case EM_ST19:               return "STMicroelectronics ST19 8-bit microcontroller";
1841     case EM_VAX:                return "Digital VAX";
1842     case EM_AVR_OLD:
1843     case EM_AVR:                return "Atmel AVR 8-bit microcontroller";
1844     case EM_CRIS:               return "Axis Communications 32-bit embedded processor";
1845     case EM_JAVELIN:            return "Infineon Technologies 32-bit embedded cpu";
1846     case EM_FIREPATH:           return "Element 14 64-bit DSP processor";
1847     case EM_ZSP:                return "LSI Logic's 16-bit DSP processor";
1848     case EM_MMIX:               return "Donald Knuth's educational 64-bit processor";
1849     case EM_HUANY:              return "Harvard Universitys's machine-independent object format";
1850     case EM_PRISM:              return "Vitesse Prism";
1851     case EM_X86_64:             return "Advanced Micro Devices X86-64";
1852     case EM_S390_OLD:
1853     case EM_S390:               return "IBM S/390";
1854     case EM_SCORE:              return "SUNPLUS S+Core";
1855     case EM_XSTORMY16:          return "Sanyo Xstormy16 CPU core";
1856     case EM_OPENRISC:
1857     case EM_OR32:               return "OpenRISC";
1858     case EM_CRX:                return "National Semiconductor CRX microprocessor";
1859     case EM_DLX:                return "OpenDLX";
1860     case EM_IP2K_OLD:
1861     case EM_IP2K:               return "Ubicom IP2xxx 8-bit microcontrollers";
1862     case EM_IQ2000:             return "Vitesse IQ2000";
1863     case EM_XTENSA_OLD:
1864     case EM_XTENSA:             return "Tensilica Xtensa Processor";
1865     case EM_M32C_OLD:
1866     case EM_M32C:               return "Renesas M32c";
1867     case EM_MT:                 return "Morpho Techologies MT processor";
1868     case EM_BLACKFIN:           return "Analog Devices Blackfin";
1869     case EM_NIOS32:             return "Altera Nios";
1870     case EM_ALTERA_NIOS2:       return "Altera Nios II";
1871     case EM_XC16X:              return "Infineon Technologies xc16x";
1872     case EM_CYGNUS_MEP:         return "Toshiba MeP Media Engine";
1873     case EM_CR16:
1874     case EM_CR16_OLD:           return "National Semiconductor's CR16";
1875     default:
1876       snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
1877       return buff;
1878     }
1879 }
1880
1881 static void
1882 decode_ARM_machine_flags (unsigned e_flags, char buf[])
1883 {
1884   unsigned eabi;
1885   int unknown = 0;
1886
1887   eabi = EF_ARM_EABI_VERSION (e_flags);
1888   e_flags &= ~ EF_ARM_EABIMASK;
1889
1890   /* Handle "generic" ARM flags.  */
1891   if (e_flags & EF_ARM_RELEXEC)
1892     {
1893       strcat (buf, ", relocatable executable");
1894       e_flags &= ~ EF_ARM_RELEXEC;
1895     }
1896
1897   if (e_flags & EF_ARM_HASENTRY)
1898     {
1899       strcat (buf, ", has entry point");
1900       e_flags &= ~ EF_ARM_HASENTRY;
1901     }
1902
1903   /* Now handle EABI specific flags.  */
1904   switch (eabi)
1905     {
1906     default:
1907       strcat (buf, ", <unrecognized EABI>");
1908       if (e_flags)
1909         unknown = 1;
1910       break;
1911
1912     case EF_ARM_EABI_VER1:
1913       strcat (buf, ", Version1 EABI");
1914       while (e_flags)
1915         {
1916           unsigned flag;
1917
1918           /* Process flags one bit at a time.  */
1919           flag = e_flags & - e_flags;
1920           e_flags &= ~ flag;
1921
1922           switch (flag)
1923             {
1924             case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
1925               strcat (buf, ", sorted symbol tables");
1926               break;
1927
1928             default:
1929               unknown = 1;
1930               break;
1931             }
1932         }
1933       break;
1934
1935     case EF_ARM_EABI_VER2:
1936       strcat (buf, ", Version2 EABI");
1937       while (e_flags)
1938         {
1939           unsigned flag;
1940
1941           /* Process flags one bit at a time.  */
1942           flag = e_flags & - e_flags;
1943           e_flags &= ~ flag;
1944
1945           switch (flag)
1946             {
1947             case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
1948               strcat (buf, ", sorted symbol tables");
1949               break;
1950
1951             case EF_ARM_DYNSYMSUSESEGIDX:
1952               strcat (buf, ", dynamic symbols use segment index");
1953               break;
1954
1955             case EF_ARM_MAPSYMSFIRST:
1956               strcat (buf, ", mapping symbols precede others");
1957               break;
1958
1959             default:
1960               unknown = 1;
1961               break;
1962             }
1963         }
1964       break;
1965
1966     case EF_ARM_EABI_VER3:
1967       strcat (buf, ", Version3 EABI");
1968       break;
1969
1970     case EF_ARM_EABI_VER4:
1971       strcat (buf, ", Version4 EABI");
1972       goto eabi;
1973
1974     case EF_ARM_EABI_VER5:
1975       strcat (buf, ", Version5 EABI");
1976     eabi:
1977       while (e_flags)
1978         {
1979           unsigned flag;
1980
1981           /* Process flags one bit at a time.  */
1982           flag = e_flags & - e_flags;
1983           e_flags &= ~ flag;
1984
1985           switch (flag)
1986             {
1987             case EF_ARM_BE8:
1988               strcat (buf, ", BE8");
1989               break;
1990
1991             case EF_ARM_LE8:
1992               strcat (buf, ", LE8");
1993               break;
1994
1995             default:
1996               unknown = 1;
1997               break;
1998             }
1999         }
2000       break;
2001
2002     case EF_ARM_EABI_UNKNOWN:
2003       strcat (buf, ", GNU EABI");
2004       while (e_flags)
2005         {
2006           unsigned flag;
2007
2008           /* Process flags one bit at a time.  */
2009           flag = e_flags & - e_flags;
2010           e_flags &= ~ flag;
2011
2012           switch (flag)
2013             {
2014             case EF_ARM_INTERWORK:
2015               strcat (buf, ", interworking enabled");
2016               break;
2017
2018             case EF_ARM_APCS_26:
2019               strcat (buf, ", uses APCS/26");
2020               break;
2021
2022             case EF_ARM_APCS_FLOAT:
2023               strcat (buf, ", uses APCS/float");
2024               break;
2025
2026             case EF_ARM_PIC:
2027               strcat (buf, ", position independent");
2028               break;
2029
2030             case EF_ARM_ALIGN8:
2031               strcat (buf, ", 8 bit structure alignment");
2032               break;
2033
2034             case EF_ARM_NEW_ABI:
2035               strcat (buf, ", uses new ABI");
2036               break;
2037
2038             case EF_ARM_OLD_ABI:
2039               strcat (buf, ", uses old ABI");
2040               break;
2041
2042             case EF_ARM_SOFT_FLOAT:
2043               strcat (buf, ", software FP");
2044               break;
2045
2046             case EF_ARM_VFP_FLOAT:
2047               strcat (buf, ", VFP");
2048               break;
2049
2050             case EF_ARM_MAVERICK_FLOAT:
2051               strcat (buf, ", Maverick FP");
2052               break;
2053
2054             default:
2055               unknown = 1;
2056               break;
2057             }
2058         }
2059     }
2060
2061   if (unknown)
2062     strcat (buf,", <unknown>");
2063 }
2064
2065 static char *
2066 get_machine_flags (unsigned e_flags, unsigned e_machine)
2067 {
2068   static char buf[1024];
2069
2070   buf[0] = '\0';
2071
2072   if (e_flags)
2073     {
2074       switch (e_machine)
2075         {
2076         default:
2077           break;
2078
2079         case EM_ARM:
2080           decode_ARM_machine_flags (e_flags, buf);
2081           break;
2082
2083         case EM_CYGNUS_FRV:
2084           switch (e_flags & EF_FRV_CPU_MASK)
2085             {
2086             case EF_FRV_CPU_GENERIC:
2087               break;
2088
2089             default:
2090               strcat (buf, ", fr???");
2091               break;
2092
2093             case EF_FRV_CPU_FR300:
2094               strcat (buf, ", fr300");
2095               break;
2096
2097             case EF_FRV_CPU_FR400:
2098               strcat (buf, ", fr400");
2099               break;
2100             case EF_FRV_CPU_FR405:
2101               strcat (buf, ", fr405");
2102               break;
2103
2104             case EF_FRV_CPU_FR450:
2105               strcat (buf, ", fr450");
2106               break;
2107
2108             case EF_FRV_CPU_FR500:
2109               strcat (buf, ", fr500");
2110               break;
2111             case EF_FRV_CPU_FR550:
2112               strcat (buf, ", fr550");
2113               break;
2114
2115             case EF_FRV_CPU_SIMPLE:
2116               strcat (buf, ", simple");
2117               break;
2118             case EF_FRV_CPU_TOMCAT:
2119               strcat (buf, ", tomcat");
2120               break;
2121             }
2122           break;
2123
2124         case EM_68K:
2125           if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
2126             strcat (buf, ", m68000");
2127           else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
2128             strcat (buf, ", cpu32");
2129           else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
2130             strcat (buf, ", fido_a");
2131           else
2132             {
2133               char const *isa = _("unknown");
2134               char const *mac = _("unknown mac");
2135               char const *additional = NULL;
2136
2137               switch (e_flags & EF_M68K_CF_ISA_MASK)
2138                 {
2139                 case EF_M68K_CF_ISA_A_NODIV:
2140                   isa = "A";
2141                   additional = ", nodiv";
2142                   break;
2143                 case EF_M68K_CF_ISA_A:
2144                   isa = "A";
2145                   break;
2146                 case EF_M68K_CF_ISA_A_PLUS:
2147                   isa = "A+";
2148                   break;
2149                 case EF_M68K_CF_ISA_B_NOUSP:
2150                   isa = "B";
2151                   additional = ", nousp";
2152                   break;
2153                 case EF_M68K_CF_ISA_B:
2154                   isa = "B";
2155                   break;
2156                 }
2157               strcat (buf, ", cf, isa ");
2158               strcat (buf, isa);
2159               if (additional)
2160                 strcat (buf, additional);
2161               if (e_flags & EF_M68K_CF_FLOAT)
2162                 strcat (buf, ", float");
2163               switch (e_flags & EF_M68K_CF_MAC_MASK)
2164                 {
2165                 case 0:
2166                   mac = NULL;
2167                   break;
2168                 case EF_M68K_CF_MAC:
2169                   mac = "mac";
2170                   break;
2171                 case EF_M68K_CF_EMAC:
2172                   mac = "emac";
2173                   break;
2174                 }
2175               if (mac)
2176                 {
2177                   strcat (buf, ", ");
2178                   strcat (buf, mac);
2179                 }
2180             }
2181           break;
2182
2183         case EM_PPC:
2184           if (e_flags & EF_PPC_EMB)
2185             strcat (buf, ", emb");
2186
2187           if (e_flags & EF_PPC_RELOCATABLE)
2188             strcat (buf, ", relocatable");
2189
2190           if (e_flags & EF_PPC_RELOCATABLE_LIB)
2191             strcat (buf, ", relocatable-lib");
2192           break;
2193
2194         case EM_V850:
2195         case EM_CYGNUS_V850:
2196           switch (e_flags & EF_V850_ARCH)
2197             {
2198             case E_V850E1_ARCH:
2199               strcat (buf, ", v850e1");
2200               break;
2201             case E_V850E_ARCH:
2202               strcat (buf, ", v850e");
2203               break;
2204             case E_V850_ARCH:
2205               strcat (buf, ", v850");
2206               break;
2207             default:
2208               strcat (buf, ", unknown v850 architecture variant");
2209               break;
2210             }
2211           break;
2212
2213         case EM_M32R:
2214         case EM_CYGNUS_M32R:
2215           if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
2216             strcat (buf, ", m32r");
2217           break;
2218
2219         case EM_MIPS:
2220         case EM_MIPS_RS3_LE:
2221           if (e_flags & EF_MIPS_NOREORDER)
2222             strcat (buf, ", noreorder");
2223
2224           if (e_flags & EF_MIPS_PIC)
2225             strcat (buf, ", pic");
2226
2227           if (e_flags & EF_MIPS_CPIC)
2228             strcat (buf, ", cpic");
2229
2230           if (e_flags & EF_MIPS_UCODE)
2231             strcat (buf, ", ugen_reserved");
2232
2233           if (e_flags & EF_MIPS_ABI2)
2234             strcat (buf, ", abi2");
2235
2236           if (e_flags & EF_MIPS_OPTIONS_FIRST)
2237             strcat (buf, ", odk first");
2238
2239           if (e_flags & EF_MIPS_32BITMODE)
2240             strcat (buf, ", 32bitmode");
2241
2242           switch ((e_flags & EF_MIPS_MACH))
2243             {
2244             case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
2245             case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
2246             case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
2247             case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
2248             case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
2249             case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
2250             case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
2251             case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
2252             case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
2253             case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
2254             case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
2255             case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
2256             case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
2257             case 0:
2258             /* We simply ignore the field in this case to avoid confusion:
2259                MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
2260                extension.  */
2261               break;
2262             default: strcat (buf, ", unknown CPU"); break;
2263             }
2264
2265           switch ((e_flags & EF_MIPS_ABI))
2266             {
2267             case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
2268             case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
2269             case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
2270             case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
2271             case 0:
2272             /* We simply ignore the field in this case to avoid confusion:
2273                MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
2274                This means it is likely to be an o32 file, but not for
2275                sure.  */
2276               break;
2277             default: strcat (buf, ", unknown ABI"); break;
2278             }
2279
2280           if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
2281             strcat (buf, ", mdmx");
2282
2283           if (e_flags & EF_MIPS_ARCH_ASE_M16)
2284             strcat (buf, ", mips16");
2285
2286           switch ((e_flags & EF_MIPS_ARCH))
2287             {
2288             case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
2289             case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
2290             case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
2291             case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
2292             case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
2293             case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
2294             case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
2295             case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
2296             case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
2297             default: strcat (buf, ", unknown ISA"); break;
2298             }
2299
2300           break;
2301
2302         case EM_SH:
2303           switch ((e_flags & EF_SH_MACH_MASK))
2304             {
2305             case EF_SH1: strcat (buf, ", sh1"); break;
2306             case EF_SH2: strcat (buf, ", sh2"); break;
2307             case EF_SH3: strcat (buf, ", sh3"); break;
2308             case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
2309             case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
2310             case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
2311             case EF_SH3E: strcat (buf, ", sh3e"); break;
2312             case EF_SH4: strcat (buf, ", sh4"); break;
2313             case EF_SH5: strcat (buf, ", sh5"); break;
2314             case EF_SH2E: strcat (buf, ", sh2e"); break;
2315             case EF_SH4A: strcat (buf, ", sh4a"); break;
2316             case EF_SH2A: strcat (buf, ", sh2a"); break;
2317             case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
2318             case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
2319             case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
2320             case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
2321             case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
2322             case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
2323             case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
2324             case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
2325             case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
2326             default: strcat (buf, ", unknown ISA"); break;
2327             }
2328
2329           break;
2330
2331         case EM_SPARCV9:
2332           if (e_flags & EF_SPARC_32PLUS)
2333             strcat (buf, ", v8+");
2334
2335           if (e_flags & EF_SPARC_SUN_US1)
2336             strcat (buf, ", ultrasparcI");
2337
2338           if (e_flags & EF_SPARC_SUN_US3)
2339             strcat (buf, ", ultrasparcIII");
2340
2341           if (e_flags & EF_SPARC_HAL_R1)
2342             strcat (buf, ", halr1");
2343
2344           if (e_flags & EF_SPARC_LEDATA)
2345             strcat (buf, ", ledata");
2346
2347           if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
2348             strcat (buf, ", tso");
2349
2350           if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
2351             strcat (buf, ", pso");
2352
2353           if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
2354             strcat (buf, ", rmo");
2355           break;
2356
2357         case EM_PARISC:
2358           switch (e_flags & EF_PARISC_ARCH)
2359             {
2360             case EFA_PARISC_1_0:
2361               strcpy (buf, ", PA-RISC 1.0");
2362               break;
2363             case EFA_PARISC_1_1:
2364               strcpy (buf, ", PA-RISC 1.1");
2365               break;
2366             case EFA_PARISC_2_0:
2367               strcpy (buf, ", PA-RISC 2.0");
2368               break;
2369             default:
2370               break;
2371             }
2372           if (e_flags & EF_PARISC_TRAPNIL)
2373             strcat (buf, ", trapnil");
2374           if (e_flags & EF_PARISC_EXT)
2375             strcat (buf, ", ext");
2376           if (e_flags & EF_PARISC_LSB)
2377             strcat (buf, ", lsb");
2378           if (e_flags & EF_PARISC_WIDE)
2379             strcat (buf, ", wide");
2380           if (e_flags & EF_PARISC_NO_KABP)
2381             strcat (buf, ", no kabp");
2382           if (e_flags & EF_PARISC_LAZYSWAP)
2383             strcat (buf, ", lazyswap");
2384           break;
2385
2386         case EM_PJ:
2387         case EM_PJ_OLD:
2388           if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
2389             strcat (buf, ", new calling convention");
2390
2391           if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
2392             strcat (buf, ", gnu calling convention");
2393           break;
2394
2395         case EM_IA_64:
2396           if ((e_flags & EF_IA_64_ABI64))
2397             strcat (buf, ", 64-bit");
2398           else
2399             strcat (buf, ", 32-bit");
2400           if ((e_flags & EF_IA_64_REDUCEDFP))
2401             strcat (buf, ", reduced fp model");
2402           if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
2403             strcat (buf, ", no function descriptors, constant gp");
2404           else if ((e_flags & EF_IA_64_CONS_GP))
2405             strcat (buf, ", constant gp");
2406           if ((e_flags & EF_IA_64_ABSOLUTE))
2407             strcat (buf, ", absolute");
2408           break;
2409
2410         case EM_VAX:
2411           if ((e_flags & EF_VAX_NONPIC))
2412             strcat (buf, ", non-PIC");
2413           if ((e_flags & EF_VAX_DFLOAT))
2414             strcat (buf, ", D-Float");
2415           if ((e_flags & EF_VAX_GFLOAT))
2416             strcat (buf, ", G-Float");
2417           break;
2418         }
2419     }
2420
2421   return buf;
2422 }
2423
2424 static const char *
2425 get_osabi_name (unsigned int osabi)
2426 {
2427   static char buff[32];
2428
2429   switch (osabi)
2430     {
2431     case ELFOSABI_NONE:         return "UNIX - System V";
2432     case ELFOSABI_HPUX:         return "UNIX - HP-UX";
2433     case ELFOSABI_NETBSD:       return "UNIX - NetBSD";
2434     case ELFOSABI_LINUX:        return "UNIX - Linux";
2435     case ELFOSABI_HURD:         return "GNU/Hurd";
2436     case ELFOSABI_SOLARIS:      return "UNIX - Solaris";
2437     case ELFOSABI_AIX:          return "UNIX - AIX";
2438     case ELFOSABI_IRIX:         return "UNIX - IRIX";
2439     case ELFOSABI_FREEBSD:      return "UNIX - FreeBSD";
2440     case ELFOSABI_TRU64:        return "UNIX - TRU64";
2441     case ELFOSABI_MODESTO:      return "Novell - Modesto";
2442     case ELFOSABI_OPENBSD:      return "UNIX - OpenBSD";
2443     case ELFOSABI_OPENVMS:      return "VMS - OpenVMS";
2444     case ELFOSABI_NSK:          return "HP - Non-Stop Kernel";
2445     case ELFOSABI_AROS:         return "AROS";
2446     case ELFOSABI_STANDALONE:   return _("Standalone App");
2447     case ELFOSABI_ARM:          return "ARM";
2448     default:
2449       snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
2450       return buff;
2451     }
2452 }
2453
2454 static const char *
2455 get_arm_segment_type (unsigned long type)
2456 {
2457   switch (type)
2458     {
2459     case PT_ARM_EXIDX:
2460       return "EXIDX";
2461     default:
2462       break;
2463     }
2464
2465   return NULL;
2466 }
2467
2468 static const char *
2469 get_mips_segment_type (unsigned long type)
2470 {
2471   switch (type)
2472     {
2473     case PT_MIPS_REGINFO:
2474       return "REGINFO";
2475     case PT_MIPS_RTPROC:
2476       return "RTPROC";
2477     case PT_MIPS_OPTIONS:
2478       return "OPTIONS";
2479     default:
2480       break;
2481     }
2482
2483   return NULL;
2484 }
2485
2486 static const char *
2487 get_parisc_segment_type (unsigned long type)
2488 {
2489   switch (type)
2490     {
2491     case PT_HP_TLS:             return "HP_TLS";
2492     case PT_HP_CORE_NONE:       return "HP_CORE_NONE";
2493     case PT_HP_CORE_VERSION:    return "HP_CORE_VERSION";
2494     case PT_HP_CORE_KERNEL:     return "HP_CORE_KERNEL";
2495     case PT_HP_CORE_COMM:       return "HP_CORE_COMM";
2496     case PT_HP_CORE_PROC:       return "HP_CORE_PROC";
2497     case PT_HP_CORE_LOADABLE:   return "HP_CORE_LOADABLE";
2498     case PT_HP_CORE_STACK:      return "HP_CORE_STACK";
2499     case PT_HP_CORE_SHM:        return "HP_CORE_SHM";
2500     case PT_HP_CORE_MMF:        return "HP_CORE_MMF";
2501     case PT_HP_PARALLEL:        return "HP_PARALLEL";
2502     case PT_HP_FASTBIND:        return "HP_FASTBIND";
2503     case PT_HP_OPT_ANNOT:       return "HP_OPT_ANNOT";
2504     case PT_HP_HSL_ANNOT:       return "HP_HSL_ANNOT";
2505     case PT_HP_STACK:           return "HP_STACK";
2506     case PT_HP_CORE_UTSNAME:    return "HP_CORE_UTSNAME";
2507     case PT_PARISC_ARCHEXT:     return "PARISC_ARCHEXT";
2508     case PT_PARISC_UNWIND:      return "PARISC_UNWIND";
2509     case PT_PARISC_WEAKORDER:   return "PARISC_WEAKORDER";
2510     default:
2511       break;
2512     }
2513
2514   return NULL;
2515 }
2516
2517 static const char *
2518 get_ia64_segment_type (unsigned long type)
2519 {
2520   switch (type)
2521     {
2522     case PT_IA_64_ARCHEXT:      return "IA_64_ARCHEXT";
2523     case PT_IA_64_UNWIND:       return "IA_64_UNWIND";
2524     case PT_HP_TLS:             return "HP_TLS";
2525     case PT_IA_64_HP_OPT_ANOT:  return "HP_OPT_ANNOT";
2526     case PT_IA_64_HP_HSL_ANOT:  return "HP_HSL_ANNOT";
2527     case PT_IA_64_HP_STACK:     return "HP_STACK";
2528     default:
2529       break;
2530     }
2531
2532   return NULL;
2533 }
2534
2535 static const char *
2536 get_segment_type (unsigned long p_type)
2537 {
2538   static char buff[32];
2539
2540   switch (p_type)
2541     {
2542     case PT_NULL:       return "NULL";
2543     case PT_LOAD:       return "LOAD";
2544     case PT_DYNAMIC:    return "DYNAMIC";
2545     case PT_INTERP:     return "INTERP";
2546     case PT_NOTE:       return "NOTE";
2547     case PT_SHLIB:      return "SHLIB";
2548     case PT_PHDR:       return "PHDR";
2549     case PT_TLS:        return "TLS";
2550
2551     case PT_GNU_EH_FRAME:
2552                         return "GNU_EH_FRAME";
2553     case PT_GNU_STACK:  return "GNU_STACK";
2554     case PT_GNU_RELRO:  return "GNU_RELRO";
2555
2556     default:
2557       if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
2558         {
2559           const char *result;
2560
2561           switch (elf_header.e_machine)
2562             {
2563             case EM_ARM:
2564               result = get_arm_segment_type (p_type);
2565               break;
2566             case EM_MIPS:
2567             case EM_MIPS_RS3_LE:
2568               result = get_mips_segment_type (p_type);
2569               break;
2570             case EM_PARISC:
2571               result = get_parisc_segment_type (p_type);
2572               break;
2573             case EM_IA_64:
2574               result = get_ia64_segment_type (p_type);
2575               break;
2576             default:
2577               result = NULL;
2578               break;
2579             }
2580
2581           if (result != NULL)
2582             return result;
2583
2584           sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
2585         }
2586       else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
2587         {
2588           const char *result;
2589
2590           switch (elf_header.e_machine)
2591             {
2592             case EM_PARISC:
2593               result = get_parisc_segment_type (p_type);
2594               break;
2595             case EM_IA_64:
2596               result = get_ia64_segment_type (p_type);
2597               break;
2598             default:
2599               result = NULL;
2600               break;
2601             }
2602
2603           if (result != NULL)
2604             return result;
2605
2606           sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
2607         }
2608       else
2609         snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
2610
2611       return buff;
2612     }
2613 }
2614
2615 static const char *
2616 get_mips_section_type_name (unsigned int sh_type)
2617 {
2618   switch (sh_type)
2619     {
2620     case SHT_MIPS_LIBLIST:       return "MIPS_LIBLIST";
2621     case SHT_MIPS_MSYM:          return "MIPS_MSYM";
2622     case SHT_MIPS_CONFLICT:      return "MIPS_CONFLICT";
2623     case SHT_MIPS_GPTAB:         return "MIPS_GPTAB";
2624     case SHT_MIPS_UCODE:         return "MIPS_UCODE";
2625     case SHT_MIPS_DEBUG:         return "MIPS_DEBUG";
2626     case SHT_MIPS_REGINFO:       return "MIPS_REGINFO";
2627     case SHT_MIPS_PACKAGE:       return "MIPS_PACKAGE";
2628     case SHT_MIPS_PACKSYM:       return "MIPS_PACKSYM";
2629     case SHT_MIPS_RELD:          return "MIPS_RELD";
2630     case SHT_MIPS_IFACE:         return "MIPS_IFACE";
2631     case SHT_MIPS_CONTENT:       return "MIPS_CONTENT";
2632     case SHT_MIPS_OPTIONS:       return "MIPS_OPTIONS";
2633     case SHT_MIPS_SHDR:          return "MIPS_SHDR";
2634     case SHT_MIPS_FDESC:         return "MIPS_FDESC";
2635     case SHT_MIPS_EXTSYM:        return "MIPS_EXTSYM";
2636     case SHT_MIPS_DENSE:         return "MIPS_DENSE";
2637     case SHT_MIPS_PDESC:         return "MIPS_PDESC";
2638     case SHT_MIPS_LOCSYM:        return "MIPS_LOCSYM";
2639     case SHT_MIPS_AUXSYM:        return "MIPS_AUXSYM";
2640     case SHT_MIPS_OPTSYM:        return "MIPS_OPTSYM";
2641     case SHT_MIPS_LOCSTR:        return "MIPS_LOCSTR";
2642     case SHT_MIPS_LINE:          return "MIPS_LINE";
2643     case SHT_MIPS_RFDESC:        return "MIPS_RFDESC";
2644     case SHT_MIPS_DELTASYM:      return "MIPS_DELTASYM";
2645     case SHT_MIPS_DELTAINST:     return "MIPS_DELTAINST";
2646     case SHT_MIPS_DELTACLASS:    return "MIPS_DELTACLASS";
2647     case SHT_MIPS_DWARF:         return "MIPS_DWARF";
2648     case SHT_MIPS_DELTADECL:     return "MIPS_DELTADECL";
2649     case SHT_MIPS_SYMBOL_LIB:    return "MIPS_SYMBOL_LIB";
2650     case SHT_MIPS_EVENTS:        return "MIPS_EVENTS";
2651     case SHT_MIPS_TRANSLATE:     return "MIPS_TRANSLATE";
2652     case SHT_MIPS_PIXIE:         return "MIPS_PIXIE";
2653     case SHT_MIPS_XLATE:         return "MIPS_XLATE";
2654     case SHT_MIPS_XLATE_DEBUG:   return "MIPS_XLATE_DEBUG";
2655     case SHT_MIPS_WHIRL:         return "MIPS_WHIRL";
2656     case SHT_MIPS_EH_REGION:     return "MIPS_EH_REGION";
2657     case SHT_MIPS_XLATE_OLD:     return "MIPS_XLATE_OLD";
2658     case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
2659     default:
2660       break;
2661     }
2662   return NULL;
2663 }
2664
2665 static const char *
2666 get_parisc_section_type_name (unsigned int sh_type)
2667 {
2668   switch (sh_type)
2669     {
2670     case SHT_PARISC_EXT:        return "PARISC_EXT";
2671     case SHT_PARISC_UNWIND:     return "PARISC_UNWIND";
2672     case SHT_PARISC_DOC:        return "PARISC_DOC";
2673     case SHT_PARISC_ANNOT:      return "PARISC_ANNOT";
2674     case SHT_PARISC_SYMEXTN:    return "PARISC_SYMEXTN";
2675     case SHT_PARISC_STUBS:      return "PARISC_STUBS";
2676     case SHT_PARISC_DLKM:       return "PARISC_DLKM";
2677     default:
2678       break;
2679     }
2680   return NULL;
2681 }
2682
2683 static const char *
2684 get_ia64_section_type_name (unsigned int sh_type)
2685 {
2686   /* If the top 8 bits are 0x78 the next 8 are the os/abi ID.  */
2687   if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
2688     return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
2689
2690   switch (sh_type)
2691     {
2692     case SHT_IA_64_EXT:                return "IA_64_EXT";
2693     case SHT_IA_64_UNWIND:             return "IA_64_UNWIND";
2694     case SHT_IA_64_PRIORITY_INIT:      return "IA_64_PRIORITY_INIT";
2695     case SHT_IA_64_VMS_TRACE:          return "VMS_TRACE";
2696     case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
2697     case SHT_IA_64_VMS_DEBUG:          return "VMS_DEBUG";
2698     case SHT_IA_64_VMS_DEBUG_STR:      return "VMS_DEBUG_STR";
2699     case SHT_IA_64_VMS_LINKAGES:       return "VMS_LINKAGES";
2700     case SHT_IA_64_VMS_SYMBOL_VECTOR:  return "VMS_SYMBOL_VECTOR";
2701     case SHT_IA_64_VMS_FIXUP:          return "VMS_FIXUP";
2702     default:
2703       break;
2704     }
2705   return NULL;
2706 }
2707
2708 static const char *
2709 get_x86_64_section_type_name (unsigned int sh_type)
2710 {
2711   switch (sh_type)
2712     {
2713     case SHT_X86_64_UNWIND:     return "X86_64_UNWIND";
2714     default:
2715       break;
2716     }
2717   return NULL;
2718 }
2719
2720 static const char *
2721 get_arm_section_type_name (unsigned int sh_type)
2722 {
2723   switch (sh_type)
2724     {
2725     case SHT_ARM_EXIDX:
2726       return "ARM_EXIDX";
2727     case SHT_ARM_PREEMPTMAP:
2728       return "ARM_PREEMPTMAP";
2729     case SHT_ARM_ATTRIBUTES:
2730       return "ARM_ATTRIBUTES";
2731     default:
2732       break;
2733     }
2734   return NULL;
2735 }
2736
2737 static const char *
2738 get_section_type_name (unsigned int sh_type)
2739 {
2740   static char buff[32];
2741
2742   switch (sh_type)
2743     {
2744     case SHT_NULL:              return "NULL";
2745     case SHT_PROGBITS:          return "PROGBITS";
2746     case SHT_SYMTAB:            return "SYMTAB";
2747     case SHT_STRTAB:            return "STRTAB";
2748     case SHT_RELA:              return "RELA";
2749     case SHT_HASH:              return "HASH";
2750     case SHT_DYNAMIC:           return "DYNAMIC";
2751     case SHT_NOTE:              return "NOTE";
2752     case SHT_NOBITS:            return "NOBITS";
2753     case SHT_REL:               return "REL";
2754     case SHT_SHLIB:             return "SHLIB";
2755     case SHT_DYNSYM:            return "DYNSYM";
2756     case SHT_INIT_ARRAY:        return "INIT_ARRAY";
2757     case SHT_FINI_ARRAY:        return "FINI_ARRAY";
2758     case SHT_PREINIT_ARRAY:     return "PREINIT_ARRAY";
2759     case SHT_GNU_HASH:          return "GNU_HASH";
2760     case SHT_GROUP:             return "GROUP";
2761     case SHT_SYMTAB_SHNDX:      return "SYMTAB SECTION INDICIES";
2762     case SHT_GNU_verdef:        return "VERDEF";
2763     case SHT_GNU_verneed:       return "VERNEED";
2764     case SHT_GNU_versym:        return "VERSYM";
2765     case 0x6ffffff0:            return "VERSYM";
2766     case 0x6ffffffc:            return "VERDEF";
2767     case 0x7ffffffd:            return "AUXILIARY";
2768     case 0x7fffffff:            return "FILTER";
2769     case SHT_GNU_LIBLIST:       return "GNU_LIBLIST";
2770
2771     default:
2772       if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
2773         {
2774           const char *result;
2775
2776           switch (elf_header.e_machine)
2777             {
2778             case EM_MIPS:
2779             case EM_MIPS_RS3_LE:
2780               result = get_mips_section_type_name (sh_type);
2781               break;
2782             case EM_PARISC:
2783               result = get_parisc_section_type_name (sh_type);
2784               break;
2785             case EM_IA_64:
2786               result = get_ia64_section_type_name (sh_type);
2787               break;
2788             case EM_X86_64:
2789               result = get_x86_64_section_type_name (sh_type);
2790               break;
2791             case EM_ARM:
2792               result = get_arm_section_type_name (sh_type);
2793               break;
2794             default:
2795               result = NULL;
2796               break;
2797             }
2798
2799           if (result != NULL)
2800             return result;
2801
2802           sprintf (buff, "LOPROC+%x", sh_type - SHT_LOPROC);
2803         }
2804       else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
2805         {
2806           const char *result;
2807
2808           switch (elf_header.e_machine)
2809             {
2810             case EM_IA_64:
2811               result = get_ia64_section_type_name (sh_type);
2812               break;
2813             default:
2814               result = NULL;
2815               break;
2816             }
2817
2818           if (result != NULL)
2819             return result;
2820
2821           sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
2822         }
2823       else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
2824         sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
2825       else
2826         snprintf (buff, sizeof (buff), _("<unknown>: %x"), sh_type);
2827
2828       return buff;
2829     }
2830 }
2831
2832 #define OPTION_DEBUG_DUMP       512
2833
2834 static struct option options[] =
2835 {
2836   {"all",              no_argument, 0, 'a'},
2837   {"file-header",      no_argument, 0, 'h'},
2838   {"program-headers",  no_argument, 0, 'l'},
2839   {"headers",          no_argument, 0, 'e'},
2840   {"histogram",        no_argument, 0, 'I'},
2841   {"segments",         no_argument, 0, 'l'},
2842   {"sections",         no_argument, 0, 'S'},
2843   {"section-headers",  no_argument, 0, 'S'},
2844   {"section-groups",   no_argument, 0, 'g'},
2845   {"section-details",  no_argument, 0, 't'},
2846   {"full-section-name",no_argument, 0, 'N'},
2847   {"symbols",          no_argument, 0, 's'},
2848   {"syms",             no_argument, 0, 's'},
2849   {"relocs",           no_argument, 0, 'r'},
2850   {"notes",            no_argument, 0, 'n'},
2851   {"dynamic",          no_argument, 0, 'd'},
2852   {"arch-specific",    no_argument, 0, 'A'},
2853   {"version-info",     no_argument, 0, 'V'},
2854   {"use-dynamic",      no_argument, 0, 'D'},
2855   {"unwind",           no_argument, 0, 'u'},
2856   {"archive-index",    no_argument, 0, 'c'},
2857   {"hex-dump",         required_argument, 0, 'x'},
2858   {"debug-dump",       optional_argument, 0, OPTION_DEBUG_DUMP},
2859   {"string-dump",      required_argument, 0, 'p'},
2860 #ifdef SUPPORT_DISASSEMBLY
2861   {"instruction-dump", required_argument, 0, 'i'},
2862 #endif
2863
2864   {"version",          no_argument, 0, 'v'},
2865   {"wide",             no_argument, 0, 'W'},
2866   {"help",             no_argument, 0, 'H'},
2867   {0,                  no_argument, 0, 0}
2868 };
2869
2870 static void
2871 usage (FILE *stream)
2872 {
2873   fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
2874   fprintf (stream, _(" Display information about the contents of ELF format files\n"));
2875   fprintf (stream, _(" Options are:\n\
2876   -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n\
2877   -h --file-header       Display the ELF file header\n\
2878   -l --program-headers   Display the program headers\n\
2879      --segments          An alias for --program-headers\n\
2880   -S --section-headers   Display the sections' header\n\
2881      --sections          An alias for --section-headers\n\
2882   -g --section-groups    Display the section groups\n\
2883   -t --section-details   Display the section details\n\
2884   -e --headers           Equivalent to: -h -l -S\n\
2885   -s --syms              Display the symbol table\n\
2886       --symbols          An alias for --syms\n\
2887   -n --notes             Display the core notes (if present)\n\
2888   -r --relocs            Display the relocations (if present)\n\
2889   -u --unwind            Display the unwind info (if present)\n\
2890   -d --dynamic           Display the dynamic section (if present)\n\
2891   -V --version-info      Display the version sections (if present)\n\
2892   -A --arch-specific     Display architecture specific information (if any).\n\
2893   -c --archive-index     Display the symbol/file index in an archive\n\
2894   -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
2895   -x --hex-dump=<number|name>\n\
2896                          Dump the contents of section <number|name> as bytes\n\
2897   -p --string-dump=<number|name>\n\
2898                          Dump the contents of section <number|name> as strings\n\
2899   -w[lLiaprmfFsoR] or\n\
2900   --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=str,=loc,=Ranges]\n\
2901                          Display the contents of DWARF2 debug sections\n"));
2902 #ifdef SUPPORT_DISASSEMBLY
2903   fprintf (stream, _("\
2904   -i --instruction-dump=<number|name>\n\
2905                          Disassemble the contents of section <number|name>\n"));
2906 #endif
2907   fprintf (stream, _("\
2908   -I --histogram         Display histogram of bucket list lengths\n\
2909   -W --wide              Allow output width to exceed 80 characters\n\
2910   @<file>                Read options from <file>\n\
2911   -H --help              Display this information\n\
2912   -v --version           Display the version number of readelf\n"));
2913
2914   if (REPORT_BUGS_TO[0] && stream == stdout)
2915     fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
2916
2917   exit (stream == stdout ? 0 : 1);
2918 }
2919
2920 /* Record the fact that the user wants the contents of section number
2921    SECTION to be displayed using the method(s) encoded as flags bits
2922    in TYPE.  Note, TYPE can be zero if we are creating the array for
2923    the first time.  */
2924
2925 static void
2926 request_dump_bynumber (unsigned int section, dump_type type)
2927 {
2928   if (section >= num_dump_sects)
2929     {
2930       dump_type *new_dump_sects;
2931
2932       new_dump_sects = calloc (section + 1, sizeof (* dump_sects));
2933
2934       if (new_dump_sects == NULL)
2935         error (_("Out of memory allocating dump request table.\n"));
2936       else
2937         {
2938           /* Copy current flag settings.  */
2939           memcpy (new_dump_sects, dump_sects, num_dump_sects * sizeof (* dump_sects));
2940
2941           free (dump_sects);
2942
2943           dump_sects = new_dump_sects;
2944           num_dump_sects = section + 1;
2945         }
2946     }
2947
2948   if (dump_sects)
2949     dump_sects[section] |= type;
2950
2951   return;
2952 }
2953
2954 /* Request a dump by section name.  */
2955
2956 static void
2957 request_dump_byname (const char *section, dump_type type)
2958 {
2959   struct dump_list_entry *new_request;
2960
2961   new_request = malloc (sizeof (struct dump_list_entry));
2962   if (!new_request)
2963     error (_("Out of memory allocating dump request table.\n"));
2964
2965   new_request->name = strdup (section);
2966   if (!new_request->name)
2967     error (_("Out of memory allocating dump request table.\n"));
2968
2969   new_request->type = type;
2970
2971   new_request->next = dump_sects_byname;
2972   dump_sects_byname = new_request;
2973 }
2974
2975 static void
2976 parse_args (int argc, char **argv)
2977 {
2978   int c;
2979
2980   if (argc < 2)
2981     usage (stderr);
2982
2983   while ((c = getopt_long
2984           (argc, argv, "ADHINSVWacdeghi:lnp:rstuvw::x:", options, NULL)) != EOF)
2985     {
2986       char *cp;
2987       int section;
2988
2989       switch (c)
2990         {
2991         case 0:
2992           /* Long options.  */
2993           break;
2994         case 'H':
2995           usage (stdout);
2996           break;
2997
2998         case 'a':
2999           do_syms++;
3000           do_reloc++;
3001           do_unwind++;
3002           do_dynamic++;
3003           do_header++;
3004           do_sections++;
3005           do_section_groups++;
3006           do_segments++;
3007           do_version++;
3008           do_histogram++;
3009           do_arch++;
3010           do_notes++;
3011           break;
3012         case 'g':
3013           do_section_groups++;
3014           break;
3015         case 't':
3016         case 'N':
3017           do_sections++;
3018           do_section_details++;
3019           break;
3020         case 'e':
3021           do_header++;
3022           do_sections++;
3023           do_segments++;
3024           break;
3025         case 'A':
3026           do_arch++;
3027           break;
3028         case 'D':
3029           do_using_dynamic++;
3030           break;
3031         case 'r':
3032           do_reloc++;
3033           break;
3034         case 'u':
3035           do_unwind++;
3036           break;
3037         case 'h':
3038           do_header++;
3039           break;
3040         case 'l':
3041           do_segments++;
3042           break;
3043         case 's':
3044           do_syms++;
3045           break;
3046         case 'S':
3047           do_sections++;
3048           break;
3049         case 'd':
3050           do_dynamic++;
3051           break;
3052         case 'I':
3053           do_histogram++;
3054           break;
3055         case 'n':
3056           do_notes++;
3057           break;
3058         case 'c':
3059           do_archive_index++;
3060           break;
3061         case 'x':
3062           do_dump++;
3063           section = strtoul (optarg, & cp, 0);
3064           if (! *cp && section >= 0)
3065             request_dump_bynumber (section, HEX_DUMP);
3066           else
3067             request_dump_byname (optarg, HEX_DUMP);
3068           break;
3069         case 'p':
3070           do_dump++;
3071           section = strtoul (optarg, & cp, 0);
3072           if (! *cp && section >= 0)
3073             request_dump_bynumber (section, STRING_DUMP);
3074           else
3075             request_dump_byname (optarg, STRING_DUMP);
3076           break;
3077         case 'w':
3078           do_dump++;
3079           if (optarg == 0)
3080             do_debugging = 1;
3081           else
3082             {
3083               unsigned int index = 0;
3084
3085               do_debugging = 0;
3086
3087               while (optarg[index])
3088                 switch (optarg[index++])
3089                   {
3090                   case 'i':
3091                     do_debug_info = 1;
3092                     break;
3093
3094                   case 'a':
3095                     do_debug_abbrevs = 1;
3096                     break;
3097
3098                   case 'l':
3099                     do_debug_lines = 1;
3100                     break;
3101
3102                   case 'L':
3103                     do_debug_lines_decoded = 1;
3104                     break;
3105
3106                   case 'p':
3107                     do_debug_pubnames = 1;
3108                     break;
3109
3110                   case 'r':
3111                     do_debug_aranges = 1;
3112                     break;
3113
3114                   case 'R':
3115                     do_debug_ranges = 1;
3116                     break;
3117
3118                   case 'F':
3119                     do_debug_frames_interp = 1;
3120                   case 'f':
3121                     do_debug_frames = 1;
3122                     break;
3123
3124                   case 'm':
3125                     do_debug_macinfo = 1;
3126                     break;
3127
3128                   case 's':
3129                     do_debug_str = 1;
3130                     break;
3131
3132                   case 'o':
3133                     do_debug_loc = 1;
3134                     break;
3135
3136                   default:
3137                     warn (_("Unrecognized debug option '%s'\n"), optarg);
3138                     break;
3139                   }
3140             }
3141           break;
3142         case OPTION_DEBUG_DUMP:
3143           do_dump++;
3144           if (optarg == 0)
3145             do_debugging = 1;
3146           else
3147             {
3148               typedef struct
3149               {
3150                 const char * option;
3151                 int *        variable;
3152               }
3153               debug_dump_long_opts;
3154
3155               debug_dump_long_opts opts_table [] =
3156                 {
3157                   /* Please keep this table alpha- sorted.  */
3158                   { "Ranges", & do_debug_ranges },
3159                   { "abbrev", & do_debug_abbrevs },
3160                   { "aranges", & do_debug_aranges },
3161                   { "frames", & do_debug_frames },
3162                   { "frames-interp", & do_debug_frames_interp },
3163                   { "info", & do_debug_info },
3164                   { "line", & do_debug_lines }, /* For backwards compatibility.  */
3165                   { "rawline", & do_debug_lines },
3166                   { "decodedline", & do_debug_lines_decoded },
3167                   { "loc",  & do_debug_loc },
3168                   { "macro", & do_debug_macinfo },
3169                   { "pubnames", & do_debug_pubnames },
3170                   /* This entry is for compatability
3171                      with earlier versions of readelf.  */
3172                   { "ranges", & do_debug_aranges },
3173                   { "str", & do_debug_str },
3174                   { NULL, NULL }
3175                 };
3176
3177               const char *p;
3178
3179               do_debugging = 0;
3180
3181               p = optarg;
3182               while (*p)
3183                 {
3184                   debug_dump_long_opts * entry;
3185
3186                   for (entry = opts_table; entry->option; entry++)
3187                     {
3188                       size_t len = strlen (entry->option);
3189
3190                       if (strneq (p, entry->option, len)
3191                           && (p[len] == ',' || p[len] == '\0'))
3192                         {
3193                           * entry->variable = 1;
3194
3195                           /* The --debug-dump=frames-interp option also
3196                              enables the --debug-dump=frames option.  */
3197                           if (do_debug_frames_interp)
3198                             do_debug_frames = 1;
3199
3200                           p += len;
3201                           break;
3202                         }
3203                     }
3204
3205                   if (entry->option == NULL)
3206                     {
3207                       warn (_("Unrecognized debug option '%s'\n"), p);
3208                       p = strchr (p, ',');
3209                       if (p == NULL)
3210                         break;
3211                     }
3212
3213                   if (*p == ',')
3214                     p++;
3215                 }
3216             }
3217           break;
3218 #ifdef SUPPORT_DISASSEMBLY
3219         case 'i':
3220           do_dump++;
3221           section = strtoul (optarg, & cp, 0);
3222           if (! *cp && section >= 0)
3223             request_dump_bynumber (section, DISASS_DUMP);
3224           else
3225             request_dump_byname (optarg, DISASS_DUMP);
3226 #endif
3227         case 'v':
3228           print_version (program_name);
3229           break;
3230         case 'V':
3231           do_version++;
3232           break;
3233         case 'W':
3234           do_wide++;
3235           break;
3236         default:
3237           /* xgettext:c-format */
3238           error (_("Invalid option '-%c'\n"), c);
3239           /* Drop through.  */
3240         case '?':
3241           usage (stderr);
3242         }
3243     }
3244
3245   if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
3246       && !do_segments && !do_header && !do_dump && !do_version
3247       && !do_histogram && !do_debugging && !do_arch && !do_notes
3248       && !do_section_groups && !do_archive_index)
3249     usage (stderr);
3250   else if (argc < 3)
3251     {
3252       warn (_("Nothing to do.\n"));
3253       usage (stderr);
3254     }
3255 }
3256
3257 static const char *
3258 get_elf_class (unsigned int elf_class)
3259 {
3260   static char buff[32];
3261
3262   switch (elf_class)
3263     {
3264     case ELFCLASSNONE: return _("none");
3265     case ELFCLASS32:   return "ELF32";
3266     case ELFCLASS64:   return "ELF64";
3267     default:
3268       snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
3269       return buff;
3270     }
3271 }
3272
3273 static const char *
3274 get_data_encoding (unsigned int encoding)
3275 {
3276   static char buff[32];
3277
3278   switch (encoding)
3279     {
3280     case ELFDATANONE: return _("none");
3281     case ELFDATA2LSB: return _("2's complement, little endian");
3282     case ELFDATA2MSB: return _("2's complement, big endian");
3283     default:
3284       snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
3285       return buff;
3286     }
3287 }
3288
3289 /* Decode the data held in 'elf_header'.  */
3290
3291 static int
3292 process_file_header (void)
3293 {
3294   if (   elf_header.e_ident[EI_MAG0] != ELFMAG0
3295       || elf_header.e_ident[EI_MAG1] != ELFMAG1
3296       || elf_header.e_ident[EI_MAG2] != ELFMAG2
3297       || elf_header.e_ident[EI_MAG3] != ELFMAG3)
3298     {
3299       error
3300         (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
3301       return 0;
3302     }
3303
3304   init_dwarf_regnames (elf_header.e_machine);
3305
3306   if (do_header)
3307     {
3308       int i;
3309
3310       printf (_("ELF Header:\n"));
3311       printf (_("  Magic:   "));
3312       for (i = 0; i < EI_NIDENT; i++)
3313         printf ("%2.2x ", elf_header.e_ident[i]);
3314       printf ("\n");
3315       printf (_("  Class:                             %s\n"),
3316               get_elf_class (elf_header.e_ident[EI_CLASS]));
3317       printf (_("  Data:                              %s\n"),
3318               get_data_encoding (elf_header.e_ident[EI_DATA]));
3319       printf (_("  Version:                           %d %s\n"),
3320               elf_header.e_ident[EI_VERSION],
3321               (elf_header.e_ident[EI_VERSION] == EV_CURRENT
3322                ? "(current)"
3323                : (elf_header.e_ident[EI_VERSION] != EV_NONE
3324                   ? "<unknown: %lx>"
3325                   : "")));
3326       printf (_("  OS/ABI:                            %s\n"),
3327               get_osabi_name (elf_header.e_ident[EI_OSABI]));
3328       printf (_("  ABI Version:                       %d\n"),
3329               elf_header.e_ident[EI_ABIVERSION]);
3330       printf (_("  Type:                              %s\n"),
3331               get_file_type (elf_header.e_type));
3332       printf (_("  Machine:                           %s\n"),
3333               get_machine_name (elf_header.e_machine));
3334       printf (_("  Version:                           0x%lx\n"),
3335               (unsigned long) elf_header.e_version);
3336
3337       printf (_("  Entry point address:               "));
3338       print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3339       printf (_("\n  Start of program headers:          "));
3340       print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3341       printf (_(" (bytes into file)\n  Start of section headers:          "));
3342       print_vma ((bfd_vma) elf_header.e_shoff, DEC);
3343       printf (_(" (bytes into file)\n"));
3344
3345       printf (_("  Flags:                             0x%lx%s\n"),
3346               (unsigned long) elf_header.e_flags,
3347               get_machine_flags (elf_header.e_flags, elf_header.e_machine));
3348       printf (_("  Size of this header:               %ld (bytes)\n"),
3349               (long) elf_header.e_ehsize);
3350       printf (_("  Size of program headers:           %ld (bytes)\n"),
3351               (long) elf_header.e_phentsize);
3352       printf (_("  Number of program headers:         %ld\n"),
3353               (long) elf_header.e_phnum);
3354       printf (_("  Size of section headers:           %ld (bytes)\n"),
3355               (long) elf_header.e_shentsize);
3356       printf (_("  Number of section headers:         %ld"),
3357               (long) elf_header.e_shnum);
3358       if (section_headers != NULL && elf_header.e_shnum == SHN_UNDEF)
3359         printf (" (%ld)", (long) section_headers[0].sh_size);
3360       putc ('\n', stdout);
3361       printf (_("  Section header string table index: %ld"),
3362               (long) elf_header.e_shstrndx);
3363       if (section_headers != NULL
3364           && elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
3365         printf (" (%u)", section_headers[0].sh_link);
3366       else if (elf_header.e_shstrndx >= elf_header.e_shnum)
3367         printf (" <corrupt: out of range>");
3368       putc ('\n', stdout);
3369     }
3370
3371   if (section_headers != NULL)
3372     {
3373       if (elf_header.e_shnum == SHN_UNDEF)
3374         elf_header.e_shnum = section_headers[0].sh_size;
3375       if (elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
3376         elf_header.e_shstrndx = section_headers[0].sh_link;
3377       else if (elf_header.e_shstrndx >= elf_header.e_shnum)
3378         elf_header.e_shstrndx = SHN_UNDEF;
3379       free (section_headers);
3380       section_headers = NULL;
3381     }
3382
3383   return 1;
3384 }
3385
3386
3387 static int
3388 get_32bit_program_headers (FILE *file, Elf_Internal_Phdr *program_headers)
3389 {
3390   Elf32_External_Phdr *phdrs;
3391   Elf32_External_Phdr *external;
3392   Elf_Internal_Phdr *internal;
3393   unsigned int i;
3394
3395   phdrs = get_data (NULL, file, elf_header.e_phoff,
3396                     elf_header.e_phentsize, elf_header.e_phnum,
3397                     _("program headers"));
3398   if (!phdrs)
3399     return 0;
3400
3401   for (i = 0, internal = program_headers, external = phdrs;
3402        i < elf_header.e_phnum;
3403        i++, internal++, external++)
3404     {
3405       internal->p_type   = BYTE_GET (external->p_type);
3406       internal->p_offset = BYTE_GET (external->p_offset);
3407       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
3408       internal->p_paddr  = BYTE_GET (external->p_paddr);
3409       internal->p_filesz = BYTE_GET (external->p_filesz);
3410       internal->p_memsz  = BYTE_GET (external->p_memsz);
3411       internal->p_flags  = BYTE_GET (external->p_flags);
3412       internal->p_align  = BYTE_GET (external->p_align);
3413     }
3414
3415   free (phdrs);
3416
3417   return 1;
3418 }
3419
3420 static int
3421 get_64bit_program_headers (FILE *file, Elf_Internal_Phdr *program_headers)
3422 {
3423   Elf64_External_Phdr *phdrs;
3424   Elf64_External_Phdr *external;
3425   Elf_Internal_Phdr *internal;
3426   unsigned int i;
3427
3428   phdrs = get_data (NULL, file, elf_header.e_phoff,
3429                     elf_header.e_phentsize, elf_header.e_phnum,
3430                     _("program headers"));
3431   if (!phdrs)
3432     return 0;
3433
3434   for (i = 0, internal = program_headers, external = phdrs;
3435        i < elf_header.e_phnum;
3436        i++, internal++, external++)
3437     {
3438       internal->p_type   = BYTE_GET (external->p_type);
3439       internal->p_flags  = BYTE_GET (external->p_flags);
3440       internal->p_offset = BYTE_GET (external->p_offset);
3441       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
3442       internal->p_paddr  = BYTE_GET (external->p_paddr);
3443       internal->p_filesz = BYTE_GET (external->p_filesz);
3444       internal->p_memsz  = BYTE_GET (external->p_memsz);
3445       internal->p_align  = BYTE_GET (external->p_align);
3446     }
3447
3448   free (phdrs);
3449
3450   return 1;
3451 }
3452
3453 /* Returns 1 if the program headers were read into `program_headers'.  */
3454
3455 static int
3456 get_program_headers (FILE *file)
3457 {
3458   Elf_Internal_Phdr *phdrs;
3459
3460   /* Check cache of prior read.  */
3461   if (program_headers != NULL)
3462     return 1;
3463
3464   phdrs = cmalloc (elf_header.e_phnum, sizeof (Elf_Internal_Phdr));
3465
3466   if (phdrs == NULL)
3467     {
3468       error (_("Out of memory\n"));
3469       return 0;
3470     }
3471
3472   if (is_32bit_elf
3473       ? get_32bit_program_headers (file, phdrs)
3474       : get_64bit_program_headers (file, phdrs))
3475     {
3476       program_headers = phdrs;
3477       return 1;
3478     }
3479
3480   free (phdrs);
3481   return 0;
3482 }
3483
3484 /* Returns 1 if the program headers were loaded.  */
3485
3486 static int
3487 process_program_headers (FILE *file)
3488 {
3489   Elf_Internal_Phdr *segment;
3490   unsigned int i;
3491
3492   if (elf_header.e_phnum == 0)
3493     {
3494       if (do_segments)
3495         printf (_("\nThere are no program headers in this file.\n"));
3496       return 0;
3497     }
3498
3499   if (do_segments && !do_header)
3500     {
3501       printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
3502       printf (_("Entry point "));
3503       print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3504       printf (_("\nThere are %d program headers, starting at offset "),
3505               elf_header.e_phnum);
3506       print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3507       printf ("\n");
3508     }
3509
3510   if (! get_program_headers (file))
3511       return 0;
3512
3513   if (do_segments)
3514     {
3515       if (elf_header.e_phnum > 1)
3516         printf (_("\nProgram Headers:\n"));
3517       else
3518         printf (_("\nProgram Headers:\n"));
3519
3520       if (is_32bit_elf)
3521         printf
3522           (_("  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n"));
3523       else if (do_wide)
3524         printf
3525           (_("  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align\n"));
3526       else
3527         {
3528           printf
3529             (_("  Type           Offset             VirtAddr           PhysAddr\n"));
3530           printf
3531             (_("                 FileSiz            MemSiz              Flags  Align\n"));
3532         }
3533     }
3534
3535   dynamic_addr = 0;
3536   dynamic_size = 0;
3537
3538   for (i = 0, segment = program_headers;
3539        i < elf_header.e_phnum;
3540        i++, segment++)
3541     {
3542       if (do_segments)
3543         {
3544           printf ("  %-14.14s ", get_segment_type (segment->p_type));
3545
3546           if (is_32bit_elf)
3547             {
3548               printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3549               printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
3550               printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
3551               printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
3552               printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
3553               printf ("%c%c%c ",
3554                       (segment->p_flags & PF_R ? 'R' : ' '),
3555                       (segment->p_flags & PF_W ? 'W' : ' '),
3556                       (segment->p_flags & PF_X ? 'E' : ' '));
3557               printf ("%#lx", (unsigned long) segment->p_align);
3558             }
3559           else if (do_wide)
3560             {
3561               if ((unsigned long) segment->p_offset == segment->p_offset)
3562                 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3563               else
3564                 {
3565                   print_vma (segment->p_offset, FULL_HEX);
3566                   putchar (' ');
3567                 }
3568
3569               print_vma (segment->p_vaddr, FULL_HEX);
3570               putchar (' ');
3571               print_vma (segment->p_paddr, FULL_HEX);
3572               putchar (' ');
3573
3574               if ((unsigned long) segment->p_filesz == segment->p_filesz)
3575                 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
3576               else
3577                 {
3578                   print_vma (segment->p_filesz, FULL_HEX);
3579                   putchar (' ');
3580                 }
3581
3582               if ((unsigned long) segment->p_memsz == segment->p_memsz)
3583                 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
3584               else
3585                 {
3586                   print_vma (segment->p_offset, FULL_HEX);
3587                 }
3588
3589               printf (" %c%c%c ",
3590                       (segment->p_flags & PF_R ? 'R' : ' '),
3591                       (segment->p_flags & PF_W ? 'W' : ' '),
3592                       (segment->p_flags & PF_X ? 'E' : ' '));
3593
3594               if ((unsigned long) segment->p_align == segment->p_align)
3595                 printf ("%#lx", (unsigned long) segment->p_align);
3596               else
3597                 {
3598                   print_vma (segment->p_align, PREFIX_HEX);
3599                 }
3600             }
3601           else
3602             {
3603               print_vma (segment->p_offset, FULL_HEX);
3604               putchar (' ');
3605               print_vma (segment->p_vaddr, FULL_HEX);
3606               putchar (' ');
3607               print_vma (segment->p_paddr, FULL_HEX);
3608               printf ("\n                 ");
3609               print_vma (segment->p_filesz, FULL_HEX);
3610               putchar (' ');
3611               print_vma (segment->p_memsz, FULL_HEX);
3612               printf ("  %c%c%c    ",
3613                       (segment->p_flags & PF_R ? 'R' : ' '),
3614                       (segment->p_flags & PF_W ? 'W' : ' '),
3615                       (segment->p_flags & PF_X ? 'E' : ' '));
3616               print_vma (segment->p_align, HEX);
3617             }
3618         }
3619
3620       switch (segment->p_type)
3621         {
3622         case PT_DYNAMIC:
3623           if (dynamic_addr)
3624             error (_("more than one dynamic segment\n"));
3625
3626           /* By default, assume that the .dynamic section is the first
3627              section in the DYNAMIC segment.  */
3628           dynamic_addr = segment->p_offset;
3629           dynamic_size = segment->p_filesz;
3630
3631           /* Try to locate the .dynamic section. If there is
3632              a section header table, we can easily locate it.  */
3633           if (section_headers != NULL)
3634             {
3635               Elf_Internal_Shdr *sec;
3636
3637               sec = find_section (".dynamic");
3638               if (sec == NULL || sec->sh_size == 0)
3639                 {
3640                   error (_("no .dynamic section in the dynamic segment\n"));
3641                   break;
3642                 }
3643
3644               if (sec->sh_type == SHT_NOBITS)
3645                 {
3646                   dynamic_size = 0;
3647                   break;
3648                 }
3649
3650               dynamic_addr = sec->sh_offset;
3651               dynamic_size = sec->sh_size;
3652
3653               if (dynamic_addr < segment->p_offset
3654                   || dynamic_addr > segment->p_offset + segment->p_filesz)
3655                 warn (_("the .dynamic section is not contained"
3656                         " within the dynamic segment\n"));
3657               else if (dynamic_addr > segment->p_offset)
3658                 warn (_("the .dynamic section is not the first section"
3659                         " in the dynamic segment.\n"));
3660             }
3661           break;
3662
3663         case PT_INTERP:
3664           if (fseek (file, archive_file_offset + (long) segment->p_offset,
3665                      SEEK_SET))
3666             error (_("Unable to find program interpreter name\n"));
3667           else
3668             {
3669               char fmt [32];
3670               int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX);
3671
3672               if (ret >= (int) sizeof (fmt) || ret < 0)
3673                 error (_("Internal error: failed to create format string to display program interpreter\n"));
3674
3675               program_interpreter[0] = 0;
3676               if (fscanf (file, fmt, program_interpreter) <= 0)
3677                 error (_("Unable to read program interpreter name\n"));
3678
3679               if (do_segments)
3680                 printf (_("\n      [Requesting program interpreter: %s]"),
3681                     program_interpreter);
3682             }
3683           break;
3684         }
3685
3686       if (do_segments)
3687         putc ('\n', stdout);
3688     }
3689
3690   if (do_segments && section_headers != NULL && string_table != NULL)
3691     {
3692       printf (_("\n Section to Segment mapping:\n"));
3693       printf (_("  Segment Sections...\n"));
3694
3695       for (i = 0; i < elf_header.e_phnum; i++)
3696         {
3697           unsigned int j;
3698           Elf_Internal_Shdr *section;
3699
3700           segment = program_headers + i;
3701           section = section_headers + 1;
3702
3703           printf ("   %2.2d     ", i);
3704
3705           for (j = 1; j < elf_header.e_shnum; j++, section++)
3706             {
3707               if (ELF_IS_SECTION_IN_SEGMENT_MEMORY(section, segment))
3708                 printf ("%s ", SECTION_NAME (section));
3709             }
3710
3711           putc ('\n',stdout);
3712         }
3713     }
3714
3715   return 1;
3716 }
3717
3718
3719 /* Find the file offset corresponding to VMA by using the program headers.  */
3720
3721 static long
3722 offset_from_vma (FILE *file, bfd_vma vma, bfd_size_type size)
3723 {
3724   Elf_Internal_Phdr *seg;
3725
3726   if (! get_program_headers (file))
3727     {
3728       warn (_("Cannot interpret virtual addresses without program headers.\n"));
3729       return (long) vma;
3730     }
3731
3732   for (seg = program_headers;
3733        seg < program_headers + elf_header.e_phnum;
3734        ++seg)
3735     {
3736       if (seg->p_type != PT_LOAD)
3737         continue;
3738
3739       if (vma >= (seg->p_vaddr & -seg->p_align)
3740           && vma + size <= seg->p_vaddr + seg->p_filesz)
3741         return vma - seg->p_vaddr + seg->p_offset;
3742     }
3743
3744   warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
3745         (unsigned long) vma);
3746   return (long) vma;
3747 }
3748
3749
3750 static int
3751 get_32bit_section_headers (FILE *file, unsigned int num)
3752 {
3753   Elf32_External_Shdr *shdrs;
3754   Elf_Internal_Shdr *internal;
3755   unsigned int i;
3756
3757   shdrs = get_data (NULL, file, elf_header.e_shoff,
3758                     elf_header.e_shentsize, num, _("section headers"));
3759   if (!shdrs)
3760     return 0;
3761
3762   section_headers = cmalloc (num, sizeof (Elf_Internal_Shdr));
3763
3764   if (section_headers == NULL)
3765     {
3766       error (_("Out of memory\n"));
3767       return 0;
3768     }
3769
3770   for (i = 0, internal = section_headers;
3771        i < num;
3772        i++, internal++)
3773     {
3774       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
3775       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
3776       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
3777       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
3778       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
3779       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
3780       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
3781       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
3782       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3783       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
3784     }
3785
3786   free (shdrs);
3787
3788   return 1;
3789 }
3790
3791 static int
3792 get_64bit_section_headers (FILE *file, unsigned int num)
3793 {
3794   Elf64_External_Shdr *shdrs;
3795   Elf_Internal_Shdr *internal;
3796   unsigned int i;
3797
3798   shdrs = get_data (NULL, file, elf_header.e_shoff,
3799                     elf_header.e_shentsize, num, _("section headers"));
3800   if (!shdrs)
3801     return 0;
3802
3803   section_headers = cmalloc (num, sizeof (Elf_Internal_Shdr));
3804
3805   if (section_headers == NULL)
3806     {
3807       error (_("Out of memory\n"));
3808       return 0;
3809     }
3810
3811   for (i = 0, internal = section_headers;
3812        i < num;
3813        i++, internal++)
3814     {
3815       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
3816       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
3817       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
3818       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
3819       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
3820       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
3821       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
3822       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
3823       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
3824       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3825     }
3826
3827   free (shdrs);
3828
3829   return 1;
3830 }
3831
3832 static Elf_Internal_Sym *
3833 get_32bit_elf_symbols (FILE *file, Elf_Internal_Shdr *section)
3834 {
3835   unsigned long number;
3836   Elf32_External_Sym *esyms;
3837   Elf_External_Sym_Shndx *shndx;
3838   Elf_Internal_Sym *isyms;
3839   Elf_Internal_Sym *psym;
3840   unsigned int j;
3841
3842   esyms = get_data (NULL, file, section->sh_offset, 1, section->sh_size,
3843                     _("symbols"));
3844   if (!esyms)
3845     return NULL;
3846
3847   shndx = NULL;
3848   if (symtab_shndx_hdr != NULL
3849       && (symtab_shndx_hdr->sh_link
3850           == (unsigned long) (section - section_headers)))
3851     {
3852       shndx = get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3853                         1, symtab_shndx_hdr->sh_size, _("symtab shndx"));
3854       if (!shndx)
3855         {
3856           free (esyms);
3857           return NULL;
3858         }
3859     }
3860
3861   number = section->sh_size / section->sh_entsize;
3862   isyms = cmalloc (number, sizeof (Elf_Internal_Sym));
3863
3864   if (isyms == NULL)
3865     {
3866       error (_("Out of memory\n"));
3867       if (shndx)
3868         free (shndx);
3869       free (esyms);
3870       return NULL;
3871     }
3872
3873   for (j = 0, psym = isyms;
3874        j < number;
3875        j++, psym++)
3876     {
3877       psym->st_name  = BYTE_GET (esyms[j].st_name);
3878       psym->st_value = BYTE_GET (esyms[j].st_value);
3879       psym->st_size  = BYTE_GET (esyms[j].st_size);
3880       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3881       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
3882         psym->st_shndx
3883           = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3884       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
3885         psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
3886       psym->st_info  = BYTE_GET (esyms[j].st_info);
3887       psym->st_other = BYTE_GET (esyms[j].st_other);
3888     }
3889
3890   if (shndx)
3891     free (shndx);
3892   free (esyms);
3893
3894   return isyms;
3895 }
3896
3897 static Elf_Internal_Sym *
3898 get_64bit_elf_symbols (FILE *file, Elf_Internal_Shdr *section)
3899 {
3900   unsigned long number;
3901   Elf64_External_Sym *esyms;
3902   Elf_External_Sym_Shndx *shndx;
3903   Elf_Internal_Sym *isyms;
3904   Elf_Internal_Sym *psym;
3905   unsigned int j;
3906
3907   esyms = get_data (NULL, file, section->sh_offset, 1, section->sh_size,
3908                     _("symbols"));
3909   if (!esyms)
3910     return NULL;
3911
3912   shndx = NULL;
3913   if (symtab_shndx_hdr != NULL
3914       && (symtab_shndx_hdr->sh_link
3915           == (unsigned long) (section - section_headers)))
3916     {
3917       shndx = get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3918                         1, symtab_shndx_hdr->sh_size, _("symtab shndx"));
3919       if (!shndx)
3920         {
3921           free (esyms);
3922           return NULL;
3923         }
3924     }
3925
3926   number = section->sh_size / section->sh_entsize;
3927   isyms = cmalloc (number, sizeof (Elf_Internal_Sym));
3928
3929   if (isyms == NULL)
3930     {
3931       error (_("Out of memory\n"));
3932       if (shndx)
3933         free (shndx);
3934       free (esyms);
3935       return NULL;
3936     }
3937
3938   for (j = 0, psym = isyms;
3939        j < number;
3940        j++, psym++)
3941     {
3942       psym->st_name  = BYTE_GET (esyms[j].st_name);
3943       psym->st_info  = BYTE_GET (esyms[j].st_info);
3944       psym->st_other = BYTE_GET (esyms[j].st_other);
3945       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3946       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
3947         psym->st_shndx
3948           = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3949       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
3950         psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
3951       psym->st_value = BYTE_GET (esyms[j].st_value);
3952       psym->st_size  = BYTE_GET (esyms[j].st_size);
3953     }
3954
3955   if (shndx)
3956     free (shndx);
3957   free (esyms);
3958
3959   return isyms;
3960 }
3961
3962 static const char *
3963 get_elf_section_flags (bfd_vma sh_flags)
3964 {
3965   static char buff[1024];
3966   char *p = buff;
3967   int field_size = is_32bit_elf ? 8 : 16;
3968   int index, size = sizeof (buff) - (field_size + 4 + 1);
3969   bfd_vma os_flags = 0;
3970   bfd_vma proc_flags = 0;
3971   bfd_vma unknown_flags = 0;
3972   static const struct
3973     {
3974       const char *str;
3975       int len;
3976     }
3977   flags [] =
3978     {
3979         { "WRITE", 5 },
3980         { "ALLOC", 5 },
3981         { "EXEC", 4 },
3982         { "MERGE", 5 },
3983         { "STRINGS", 7 },
3984         { "INFO LINK", 9 },
3985         { "LINK ORDER", 10 },
3986         { "OS NONCONF", 10 },
3987         { "GROUP", 5 },
3988         { "TLS", 3 },
3989         /* IA-64 specific.  */
3990         { "SHORT", 5 },
3991         { "NORECOV", 7 },
3992         /* IA-64 OpenVMS specific.  */
3993         { "VMS_GLOBAL", 10 },
3994         { "VMS_OVERLAID", 12 },
3995         { "VMS_SHARED", 10 },
3996         { "VMS_VECTOR", 10 },
3997         { "VMS_ALLOC_64BIT", 15 },
3998         { "VMS_PROTECTED", 13}
3999     };
4000
4001   if (do_section_details)
4002     {
4003       sprintf (buff, "[%*.*lx]: ",
4004                field_size, field_size, (unsigned long) sh_flags);
4005       p += field_size + 4;
4006     }
4007
4008   while (sh_flags)
4009     {
4010       bfd_vma flag;
4011
4012       flag = sh_flags & - sh_flags;
4013       sh_flags &= ~ flag;
4014
4015       if (do_section_details)
4016         {
4017           switch (flag)
4018             {
4019             case SHF_WRITE:             index = 0; break;
4020             case SHF_ALLOC:             index = 1; break;
4021             case SHF_EXECINSTR:         index = 2; break;
4022             case SHF_MERGE:             index = 3; break;
4023             case SHF_STRINGS:           index = 4; break;
4024             case SHF_INFO_LINK:         index = 5; break;
4025             case SHF_LINK_ORDER:        index = 6; break;
4026             case SHF_OS_NONCONFORMING:  index = 7; break;
4027             case SHF_GROUP:             index = 8; break;
4028             case SHF_TLS:               index = 9; break;
4029
4030             default:
4031               index = -1;
4032               if (elf_header.e_machine == EM_IA_64)
4033                 {
4034                   if (flag == SHF_IA_64_SHORT)
4035                     index = 10;
4036                   else if (flag == SHF_IA_64_NORECOV)
4037                     index = 11;
4038 #ifdef BFD64
4039                   else if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4040                     switch (flag)
4041                       {
4042                       case SHF_IA_64_VMS_GLOBAL:      index = 12; break;
4043                       case SHF_IA_64_VMS_OVERLAID:    index = 13; break;
4044                       case SHF_IA_64_VMS_SHARED:      index = 14; break;
4045                       case SHF_IA_64_VMS_VECTOR:      index = 15; break;
4046                       case SHF_IA_64_VMS_ALLOC_64BIT: index = 16; break;
4047                       case SHF_IA_64_VMS_PROTECTED:   index = 17; break;
4048                       default:                        break;
4049                       }
4050 #endif
4051                 }
4052               break;
4053             }
4054
4055           if (index != -1)
4056             {
4057               if (p != buff + field_size + 4)
4058                 {
4059                   if (size < (10 + 2))
4060                     abort ();
4061                   size -= 2;
4062                   *p++ = ',';
4063                   *p++ = ' ';
4064                 }
4065
4066               size -= flags [index].len;
4067               p = stpcpy (p, flags [index].str);
4068             }
4069           else if (flag & SHF_MASKOS)
4070             os_flags |= flag;
4071           else if (flag & SHF_MASKPROC)
4072             proc_flags |= flag;
4073           else
4074             unknown_flags |= flag;
4075         }
4076       else
4077         {
4078           switch (flag)
4079             {
4080             case SHF_WRITE:             *p = 'W'; break;
4081             case SHF_ALLOC:             *p = 'A'; break;
4082             case SHF_EXECINSTR:         *p = 'X'; break;
4083             case SHF_MERGE:             *p = 'M'; break;
4084             case SHF_STRINGS:           *p = 'S'; break;
4085             case SHF_INFO_LINK:         *p = 'I'; break;
4086             case SHF_LINK_ORDER:        *p = 'L'; break;
4087             case SHF_OS_NONCONFORMING:  *p = 'O'; break;
4088             case SHF_GROUP:             *p = 'G'; break;
4089             case SHF_TLS:               *p = 'T'; break;
4090
4091             default:
4092               if (elf_header.e_machine == EM_X86_64
4093                   && flag == SHF_X86_64_LARGE)
4094                 *p = 'l';
4095               else if (flag & SHF_MASKOS)
4096                 {
4097                   *p = 'o';
4098                   sh_flags &= ~ SHF_MASKOS;
4099                 }
4100               else if (flag & SHF_MASKPROC)
4101                 {
4102                   *p = 'p';
4103                   sh_flags &= ~ SHF_MASKPROC;
4104                 }
4105               else
4106                 *p = 'x';
4107               break;
4108             }
4109           p++;
4110         }
4111     }
4112
4113   if (do_section_details)
4114     {
4115       if (os_flags)
4116         {
4117           size -= 5 + field_size;
4118           if (p != buff + field_size + 4)
4119             {
4120               if (size < (2 + 1))
4121                 abort ();
4122               size -= 2;
4123               *p++ = ',';
4124               *p++ = ' ';
4125             }
4126           sprintf (p, "OS (%*.*lx)", field_size, field_size,
4127                    (unsigned long) os_flags);
4128           p += 5 + field_size;
4129         }
4130       if (proc_flags)
4131         {
4132           size -= 7 + field_size;
4133           if (p != buff + field_size + 4)
4134             {
4135               if (size < (2 + 1))
4136                 abort ();
4137               size -= 2;
4138               *p++ = ',';
4139               *p++ = ' ';
4140             }
4141           sprintf (p, "PROC (%*.*lx)", field_size, field_size,
4142                    (unsigned long) proc_flags);
4143           p += 7 + field_size;
4144         }
4145       if (unknown_flags)
4146         {
4147           size -= 10 + field_size;
4148           if (p != buff + field_size + 4)
4149             {
4150               if (size < (2 + 1))
4151                 abort ();
4152               size -= 2;
4153               *p++ = ',';
4154               *p++ = ' ';
4155             }
4156           sprintf (p, "UNKNOWN (%*.*lx)", field_size, field_size,
4157                    (unsigned long) unknown_flags);
4158           p += 10 + field_size;
4159         }
4160     }
4161
4162   *p = '\0';
4163   return buff;
4164 }
4165
4166 static int
4167 process_section_headers (FILE *file)
4168 {
4169   Elf_Internal_Shdr *section;
4170   unsigned int i;
4171
4172   section_headers = NULL;
4173
4174   if (elf_header.e_shnum == 0)
4175     {
4176       if (do_sections)
4177         printf (_("\nThere are no sections in this file.\n"));
4178
4179       return 1;
4180     }
4181
4182   if (do_sections && !do_header)
4183     printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
4184             elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
4185
4186   if (is_32bit_elf)
4187     {
4188       if (! get_32bit_section_headers (file, elf_header.e_shnum))
4189         return 0;
4190     }
4191   else if (! get_64bit_section_headers (file, elf_header.e_shnum))
4192     return 0;
4193
4194   /* Read in the string table, so that we have names to display.  */
4195   if (elf_header.e_shstrndx != SHN_UNDEF
4196        && elf_header.e_shstrndx < elf_header.e_shnum)
4197     {
4198       section = section_headers + elf_header.e_shstrndx;
4199
4200       if (section->sh_size != 0)
4201         {
4202           string_table = get_data (NULL, file, section->sh_offset,
4203                                    1, section->sh_size, _("string table"));
4204
4205           string_table_length = string_table != NULL ? section->sh_size : 0;
4206         }
4207     }
4208
4209   /* Scan the sections for the dynamic symbol table
4210      and dynamic string table and debug sections.  */
4211   dynamic_symbols = NULL;
4212   dynamic_strings = NULL;
4213   dynamic_syminfo = NULL;
4214   symtab_shndx_hdr = NULL;
4215
4216   eh_addr_size = is_32bit_elf ? 4 : 8;
4217   switch (elf_header.e_machine)
4218     {
4219     case EM_MIPS:
4220     case EM_MIPS_RS3_LE:
4221       /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
4222          FDE addresses.  However, the ABI also has a semi-official ILP32
4223          variant for which the normal FDE address size rules apply.
4224
4225          GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
4226          section, where XX is the size of longs in bits.  Unfortunately,
4227          earlier compilers provided no way of distinguishing ILP32 objects
4228          from LP64 objects, so if there's any doubt, we should assume that
4229          the official LP64 form is being used.  */
4230       if ((elf_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
4231           && find_section (".gcc_compiled_long32") == NULL)
4232         eh_addr_size = 8;
4233       break;
4234
4235     case EM_H8_300:
4236     case EM_H8_300H:
4237       switch (elf_header.e_flags & EF_H8_MACH)
4238         {
4239         case E_H8_MACH_H8300:
4240         case E_H8_MACH_H8300HN:
4241         case E_H8_MACH_H8300SN:
4242         case E_H8_MACH_H8300SXN:
4243           eh_addr_size = 2;
4244           break;
4245         case E_H8_MACH_H8300H:
4246         case E_H8_MACH_H8300S:
4247         case E_H8_MACH_H8300SX:
4248           eh_addr_size = 4;
4249           break;
4250         }
4251       break;
4252
4253     case EM_M32C_OLD:
4254     case EM_M32C:
4255       switch (elf_header.e_flags & EF_M32C_CPU_MASK)
4256         {
4257         case EF_M32C_CPU_M16C:
4258           eh_addr_size = 2;
4259           break;
4260         }
4261       break;
4262     }
4263
4264 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
4265   do                                                                        \
4266     {                                                                       \
4267       size_t expected_entsize                                               \
4268         = is_32bit_elf ? size32 : size64;                                   \
4269       if (section->sh_entsize != expected_entsize)                          \
4270         error (_("Section %d has invalid sh_entsize %lx (expected %lx)\n"), \
4271                i, (unsigned long int) section->sh_entsize,                  \
4272                (unsigned long int) expected_entsize);                       \
4273       section->sh_entsize = expected_entsize;                               \
4274     }                                                                       \
4275   while (0)
4276 #define CHECK_ENTSIZE(section, i, type) \
4277   CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type),         \
4278                         sizeof (Elf64_External_##type))
4279
4280   for (i = 0, section = section_headers;
4281        i < elf_header.e_shnum;
4282        i++, section++)
4283     {
4284       char *name = SECTION_NAME (section);
4285
4286       if (section->sh_type == SHT_DYNSYM)
4287         {
4288           if (dynamic_symbols != NULL)
4289             {
4290               error (_("File contains multiple dynamic symbol tables\n"));
4291               continue;
4292             }
4293
4294           CHECK_ENTSIZE (section, i, Sym);
4295           num_dynamic_syms = section->sh_size / section->sh_entsize;
4296           dynamic_symbols = GET_ELF_SYMBOLS (file, section);
4297         }
4298       else if (section->sh_type == SHT_STRTAB
4299                && streq (name, ".dynstr"))
4300         {
4301           if (dynamic_strings != NULL)
4302             {
4303               error (_("File contains multiple dynamic string tables\n"));
4304               continue;
4305             }
4306
4307           dynamic_strings = get_data (NULL, file, section->sh_offset,
4308                                       1, section->sh_size, _("dynamic strings"));
4309           dynamic_strings_length = section->sh_size;
4310         }
4311       else if (section->sh_type == SHT_SYMTAB_SHNDX)
4312         {
4313           if (symtab_shndx_hdr != NULL)
4314             {
4315               error (_("File contains multiple symtab shndx tables\n"));
4316               continue;
4317             }
4318           symtab_shndx_hdr = section;
4319         }
4320       else if (section->sh_type == SHT_SYMTAB)
4321         CHECK_ENTSIZE (section, i, Sym);
4322       else if (section->sh_type == SHT_GROUP)
4323         CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
4324       else if (section->sh_type == SHT_REL)
4325         CHECK_ENTSIZE (section, i, Rel);
4326       else if (section->sh_type == SHT_RELA)
4327         CHECK_ENTSIZE (section, i, Rela);
4328       else if ((do_debugging || do_debug_info || do_debug_abbrevs
4329                 || do_debug_lines || do_debug_lines_decoded || do_debug_pubnames
4330                 || do_debug_aranges || do_debug_frames || do_debug_macinfo
4331                 || do_debug_str || do_debug_loc || do_debug_ranges)
4332                && (const_strneq (name, ".debug_")
4333                    || const_strneq (name, ".zdebug_")))
4334         {
4335           if (name[1] == 'z')
4336             name += sizeof (".zdebug_") - 1;
4337           else
4338             name += sizeof (".debug_") - 1;
4339
4340           if (do_debugging
4341               || (do_debug_info     && streq (name, "info"))
4342               || (do_debug_abbrevs  && streq (name, "abbrev"))
4343               || ((do_debug_lines || do_debug_lines_decoded)
4344                   && streq (name, "line"))
4345               || (do_debug_pubnames && streq (name, "pubnames"))
4346               || (do_debug_aranges  && streq (name, "aranges"))
4347               || (do_debug_ranges   && streq (name, "ranges"))
4348               || (do_debug_frames   && streq (name, "frame"))
4349               || (do_debug_macinfo  && streq (name, "macinfo"))
4350               || (do_debug_str      && streq (name, "str"))
4351               || (do_debug_loc      && streq (name, "loc"))
4352               )
4353             request_dump_bynumber (i, DEBUG_DUMP);
4354         }
4355       /* Linkonce section to be combined with .debug_info at link time.  */
4356       else if ((do_debugging || do_debug_info)
4357                && const_strneq (name, ".gnu.linkonce.wi."))
4358         request_dump_bynumber (i, DEBUG_DUMP);
4359       else if (do_debug_frames && streq (name, ".eh_frame"))
4360         request_dump_bynumber (i, DEBUG_DUMP);
4361     }
4362
4363   if (! do_sections)
4364     return 1;
4365
4366   if (elf_header.e_shnum > 1)
4367     printf (_("\nSection Headers:\n"));
4368   else
4369     printf (_("\nSection Header:\n"));
4370
4371   if (is_32bit_elf)
4372     {
4373       if (do_section_details)
4374         {
4375           printf (_("  [Nr] Name\n"));
4376           printf (_("       Type            Addr     Off    Size   ES   Lk Inf Al\n"));
4377         }
4378       else
4379         printf
4380           (_("  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al\n"));
4381     }
4382   else if (do_wide)
4383     {
4384       if (do_section_details)
4385         {
4386           printf (_("  [Nr] Name\n"));
4387           printf (_("       Type            Address          Off    Size   ES   Lk Inf Al\n"));
4388         }
4389       else
4390         printf
4391           (_("  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al\n"));
4392     }
4393   else
4394     {
4395       if (do_section_details)
4396         {
4397           printf (_("  [Nr] Name\n"));
4398           printf (_("       Type              Address          Offset            Link\n"));
4399           printf (_("       Size              EntSize          Info              Align\n"));
4400         }
4401       else
4402         {
4403           printf (_("  [Nr] Name              Type             Address           Offset\n"));
4404           printf (_("       Size              EntSize          Flags  Link  Info  Align\n"));
4405         }
4406     }
4407
4408   if (do_section_details)
4409     printf (_("       Flags\n"));
4410
4411   for (i = 0, section = section_headers;
4412        i < elf_header.e_shnum;
4413        i++, section++)
4414     {
4415       if (do_section_details)
4416         {
4417           printf ("  [%2u] %s\n",
4418                   i,
4419                   SECTION_NAME (section));
4420           if (is_32bit_elf || do_wide)
4421             printf ("       %-15.15s ",
4422                     get_section_type_name (section->sh_type));
4423         }
4424       else
4425         printf ((do_wide ? "  [%2u] %-17s %-15s "
4426                          : "  [%2u] %-17.17s %-15.15s "),
4427                 i,
4428                 SECTION_NAME (section),
4429                 get_section_type_name (section->sh_type));
4430
4431       if (is_32bit_elf)
4432         {
4433           print_vma (section->sh_addr, LONG_HEX);
4434
4435           printf ( " %6.6lx %6.6lx %2.2lx",
4436                    (unsigned long) section->sh_offset,
4437                    (unsigned long) section->sh_size,
4438                    (unsigned long) section->sh_entsize);
4439
4440           if (do_section_details)
4441             fputs ("  ", stdout);
4442           else
4443             printf (" %3s ", get_elf_section_flags (section->sh_flags));
4444
4445           printf ("%2u %3u %2lu\n",
4446                   section->sh_link,
4447                   section->sh_info,
4448                   (unsigned long) section->sh_addralign);
4449         }
4450       else if (do_wide)
4451         {
4452           print_vma (section->sh_addr, LONG_HEX);
4453
4454           if ((long) section->sh_offset == section->sh_offset)
4455             printf (" %6.6lx", (unsigned long) section->sh_offset);
4456           else
4457             {
4458               putchar (' ');
4459               print_vma (section->sh_offset, LONG_HEX);
4460             }
4461
4462           if ((unsigned long) section->sh_size == section->sh_size)
4463             printf (" %6.6lx", (unsigned long) section->sh_size);
4464           else
4465             {
4466               putchar (' ');
4467               print_vma (section->sh_size, LONG_HEX);
4468             }
4469
4470           if ((unsigned long) section->sh_entsize == section->sh_entsize)
4471             printf (" %2.2lx", (unsigned long) section->sh_entsize);
4472           else
4473             {
4474               putchar (' ');
4475               print_vma (section->sh_entsize, LONG_HEX);
4476             }
4477
4478           if (do_section_details)
4479             fputs ("  ", stdout);
4480           else
4481             printf (" %3s ", get_elf_section_flags (section->sh_flags));
4482
4483           printf ("%2u %3u ", section->sh_link, section->sh_info);
4484
4485           if ((unsigned long) section->sh_addralign == section->sh_addralign)
4486             printf ("%2lu\n", (unsigned long) section->sh_addralign);
4487           else
4488             {
4489               print_vma (section->sh_addralign, DEC);
4490               putchar ('\n');
4491             }
4492         }
4493       else if (do_section_details)
4494         {
4495           printf ("       %-15.15s  ",
4496                   get_section_type_name (section->sh_type));
4497           print_vma (section->sh_addr, LONG_HEX);
4498           if ((long) section->sh_offset == section->sh_offset)
4499             printf ("  %16.16lx", (unsigned long) section->sh_offset);
4500           else
4501             {
4502               printf ("  ");
4503               print_vma (section->sh_offset, LONG_HEX);
4504             }
4505           printf ("  %u\n       ", section->sh_link);
4506           print_vma (section->sh_size, LONG_HEX);
4507           putchar (' ');
4508           print_vma (section->sh_entsize, LONG_HEX);
4509
4510           printf ("  %-16u  %lu\n",
4511                   section->sh_info,
4512                   (unsigned long) section->sh_addralign);
4513         }
4514       else
4515         {
4516           putchar (' ');
4517           print_vma (section->sh_addr, LONG_HEX);
4518           if ((long) section->sh_offset == section->sh_offset)
4519             printf ("  %8.8lx", (unsigned long) section->sh_offset);
4520           else
4521             {
4522               printf ("  ");
4523               print_vma (section->sh_offset, LONG_HEX);
4524             }
4525           printf ("\n       ");
4526           print_vma (section->sh_size, LONG_HEX);
4527           printf ("  ");
4528           print_vma (section->sh_entsize, LONG_HEX);
4529
4530           printf (" %3s ", get_elf_section_flags (section->sh_flags));
4531
4532           printf ("     %2u   %3u     %lu\n",
4533                   section->sh_link,
4534                   section->sh_info,
4535                   (unsigned long) section->sh_addralign);
4536         }
4537
4538       if (do_section_details)
4539         printf ("       %s\n", get_elf_section_flags (section->sh_flags));
4540     }
4541
4542   if (!do_section_details)
4543     printf (_("Key to Flags:\n\
4544   W (write), A (alloc), X (execute), M (merge), S (strings)\n\
4545   I (info), L (link order), G (group), x (unknown)\n\
4546   O (extra OS processing required) o (OS specific), p (processor specific)\n"));
4547
4548   return 1;
4549 }
4550
4551 static const char *
4552 get_group_flags (unsigned int flags)
4553 {
4554   static char buff[32];
4555   switch (flags)
4556     {
4557     case GRP_COMDAT:
4558       return "COMDAT";
4559
4560    default:
4561       snprintf (buff, sizeof (buff), _("[<unknown>: 0x%x]"), flags);
4562       break;
4563     }
4564   return buff;
4565 }
4566
4567 static int
4568 process_section_groups (FILE *file)
4569 {
4570   Elf_Internal_Shdr *section;
4571   unsigned int i;
4572   struct group *group;
4573   Elf_Internal_Shdr *symtab_sec, *strtab_sec;
4574   Elf_Internal_Sym *symtab;
4575   char *strtab;
4576   size_t strtab_size;
4577
4578   /* Don't process section groups unless needed.  */
4579   if (!do_unwind && !do_section_groups)
4580     return 1;
4581
4582   if (elf_header.e_shnum == 0)
4583     {
4584       if (do_section_groups)
4585         printf (_("\nThere are no sections in this file.\n"));
4586
4587       return 1;
4588     }
4589
4590   if (section_headers == NULL)
4591     {
4592       error (_("Section headers are not available!\n"));
4593       abort ();
4594     }
4595
4596   section_headers_groups = calloc (elf_header.e_shnum,
4597                                    sizeof (struct group *));
4598
4599   if (section_headers_groups == NULL)
4600     {
4601       error (_("Out of memory\n"));
4602       return 0;
4603     }
4604
4605   /* Scan the sections for the group section.  */
4606   group_count = 0;
4607   for (i = 0, section = section_headers;
4608        i < elf_header.e_shnum;
4609        i++, section++)
4610     if (section->sh_type == SHT_GROUP)
4611       group_count++;
4612
4613   if (group_count == 0)
4614     {
4615       if (do_section_groups)
4616         printf (_("\nThere are no section groups in this file.\n"));
4617
4618       return 1;
4619     }
4620
4621   section_groups = calloc (group_count, sizeof (struct group));
4622
4623   if (section_groups == NULL)
4624     {
4625       error (_("Out of memory\n"));
4626       return 0;
4627     }
4628
4629   symtab_sec = NULL;
4630   strtab_sec = NULL;
4631   symtab = NULL;
4632   strtab = NULL;
4633   strtab_size = 0;
4634   for (i = 0, section = section_headers, group = section_groups;
4635        i < elf_header.e_shnum;
4636        i++, section++)
4637     {
4638       if (section->sh_type == SHT_GROUP)
4639         {
4640           char *name = SECTION_NAME (section);
4641           char *group_name;
4642           unsigned char *start, *indices;
4643           unsigned int entry, j, size;
4644           Elf_Internal_Shdr *sec;
4645           Elf_Internal_Sym *sym;
4646
4647           /* Get the symbol table.  */
4648           if (section->sh_link >= elf_header.e_shnum
4649               || ((sec = section_headers + section->sh_link)->sh_type
4650                   != SHT_SYMTAB))
4651             {
4652               error (_("Bad sh_link in group section `%s'\n"), name);
4653               continue;
4654             }
4655
4656           if (symtab_sec != sec)
4657             {
4658               symtab_sec = sec;
4659               if (symtab)
4660                 free (symtab);
4661               symtab = GET_ELF_SYMBOLS (file, symtab_sec);
4662             }
4663
4664           sym = symtab + section->sh_info;
4665
4666           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4667             {
4668               if (sym->st_shndx == 0
4669                   || sym->st_shndx >= elf_header.e_shnum)
4670                 {
4671                   error (_("Bad sh_info in group section `%s'\n"), name);
4672                   continue;
4673                 }
4674
4675               group_name = SECTION_NAME (section_headers + sym->st_shndx);
4676               strtab_sec = NULL;
4677               if (strtab)
4678                 free (strtab);
4679               strtab = NULL;
4680               strtab_size = 0;
4681             }
4682           else
4683             {
4684               /* Get the string table.  */
4685               if (symtab_sec->sh_link >= elf_header.e_shnum)
4686                 {
4687                   strtab_sec = NULL;
4688                   if (strtab)
4689                     free (strtab);
4690                   strtab = NULL;
4691                   strtab_size = 0;
4692                 }
4693               else if (strtab_sec
4694                        != (sec = section_headers + symtab_sec->sh_link))
4695                 {
4696                   strtab_sec = sec;
4697                   if (strtab)
4698                     free (strtab);
4699                   strtab = get_data (NULL, file, strtab_sec->sh_offset,
4700                                      1, strtab_sec->sh_size,
4701                                      _("string table"));
4702                   strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
4703                 }
4704               group_name = sym->st_name < strtab_size
4705                            ? strtab + sym->st_name : "<corrupt>";
4706             }
4707
4708           start = get_data (NULL, file, section->sh_offset,
4709                             1, section->sh_size, _("section data"));
4710
4711           indices = start;
4712           size = (section->sh_size / section->sh_entsize) - 1;
4713           entry = byte_get (indices, 4);
4714           indices += 4;
4715
4716           if (do_section_groups)
4717             {
4718               printf ("\n%s group section [%5u] `%s' [%s] contains %u sections:\n",
4719                       get_group_flags (entry), i, name, group_name, size);
4720
4721               printf (_("   [Index]    Name\n"));
4722             }
4723
4724           group->group_index = i;
4725
4726           for (j = 0; j < size; j++)
4727             {
4728               struct group_list *g;
4729
4730               entry = byte_get (indices, 4);
4731               indices += 4;
4732
4733               if (entry >= elf_header.e_shnum)
4734                 {
4735                   error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
4736                          entry, i, elf_header.e_shnum - 1);
4737                   continue;
4738                 }
4739
4740               if (section_headers_groups [entry] != NULL)
4741                 {
4742                   if (entry)
4743                     {
4744                       error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
4745                              entry, i,
4746                              section_headers_groups [entry]->group_index);
4747                       continue;
4748                     }
4749                   else
4750                     {
4751                       /* Intel C/C++ compiler may put section 0 in a
4752                          section group. We just warn it the first time
4753                          and ignore it afterwards.  */
4754                       static int warned = 0;
4755                       if (!warned)
4756                         {
4757                           error (_("section 0 in group section [%5u]\n"),
4758                                  section_headers_groups [entry]->group_index);
4759                           warned++;
4760                         }
4761                     }
4762                 }
4763
4764               section_headers_groups [entry] = group;
4765
4766               if (do_section_groups)
4767                 {
4768                   sec = section_headers + entry;
4769                   printf ("   [%5u]   %s\n", entry, SECTION_NAME (sec));
4770                 }
4771
4772               g = xmalloc (sizeof (struct group_list));
4773               g->section_index = entry;
4774               g->next = group->root;
4775               group->root = g;
4776             }
4777
4778           if (start)
4779             free (start);
4780
4781           group++;
4782         }
4783     }
4784
4785   if (symtab)
4786     free (symtab);
4787   if (strtab)
4788     free (strtab);
4789   return 1;
4790 }
4791
4792 static struct
4793 {
4794   const char *name;
4795   int reloc;
4796   int size;
4797   int rela;
4798 } dynamic_relocations [] =
4799 {
4800     { "REL", DT_REL, DT_RELSZ, FALSE },
4801     { "RELA", DT_RELA, DT_RELASZ, TRUE },
4802     { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
4803 };
4804
4805 /* Process the reloc section.  */
4806
4807 static int
4808 process_relocs (FILE *file)
4809 {
4810   unsigned long rel_size;
4811   unsigned long rel_offset;
4812
4813
4814   if (!do_reloc)
4815     return 1;
4816
4817   if (do_using_dynamic)
4818     {
4819       int is_rela;
4820       const char *name;
4821       int has_dynamic_reloc;
4822       unsigned int i;
4823
4824       has_dynamic_reloc = 0;
4825
4826       for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
4827         {
4828           is_rela = dynamic_relocations [i].rela;
4829           name = dynamic_relocations [i].name;
4830           rel_size = dynamic_info [dynamic_relocations [i].size];
4831           rel_offset = dynamic_info [dynamic_relocations [i].reloc];
4832
4833           has_dynamic_reloc |= rel_size;
4834
4835           if (is_rela == UNKNOWN)
4836             {
4837               if (dynamic_relocations [i].reloc == DT_JMPREL)
4838                 switch (dynamic_info[DT_PLTREL])
4839                   {
4840                   case DT_REL:
4841                     is_rela = FALSE;
4842                     break;
4843                   case DT_RELA:
4844                     is_rela = TRUE;
4845                     break;
4846                   }
4847             }
4848
4849           if (rel_size)
4850             {
4851               printf
4852                 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
4853                  name, rel_offset, rel_size);
4854
4855               dump_relocations (file,
4856                                 offset_from_vma (file, rel_offset, rel_size),
4857                                 rel_size,
4858                                 dynamic_symbols, num_dynamic_syms,
4859                                 dynamic_strings, dynamic_strings_length, is_rela);
4860             }
4861         }
4862
4863       if (! has_dynamic_reloc)
4864         printf (_("\nThere are no dynamic relocations in this file.\n"));
4865     }
4866   else
4867     {
4868       Elf_Internal_Shdr *section;
4869       unsigned long i;
4870       int found = 0;
4871
4872       for (i = 0, section = section_headers;
4873            i < elf_header.e_shnum;
4874            i++, section++)
4875         {
4876           if (   section->sh_type != SHT_RELA
4877               && section->sh_type != SHT_REL)
4878             continue;
4879
4880           rel_offset = section->sh_offset;
4881           rel_size   = section->sh_size;
4882
4883           if (rel_size)
4884             {
4885               Elf_Internal_Shdr *strsec;
4886               int is_rela;
4887
4888               printf (_("\nRelocation section "));
4889
4890               if (string_table == NULL)
4891                 printf ("%d", section->sh_name);
4892               else
4893                 printf (_("'%s'"), SECTION_NAME (section));
4894
4895               printf (_(" at offset 0x%lx contains %lu entries:\n"),
4896                  rel_offset, (unsigned long) (rel_size / section->sh_entsize));
4897
4898               is_rela = section->sh_type == SHT_RELA;
4899
4900               if (section->sh_link != 0
4901                   && section->sh_link < elf_header.e_shnum)
4902                 {
4903                   Elf_Internal_Shdr *symsec;
4904                   Elf_Internal_Sym *symtab;
4905                   unsigned long nsyms;
4906                   unsigned long strtablen = 0;
4907                   char *strtab = NULL;
4908
4909                   symsec = section_headers + section->sh_link;
4910                   if (symsec->sh_type != SHT_SYMTAB
4911                       && symsec->sh_type != SHT_DYNSYM)
4912                     continue;
4913
4914                   nsyms = symsec->sh_size / symsec->sh_entsize;
4915                   symtab = GET_ELF_SYMBOLS (file, symsec);
4916
4917                   if (symtab == NULL)
4918                     continue;
4919
4920                   if (symsec->sh_link != 0
4921                       && symsec->sh_link < elf_header.e_shnum)
4922                     {
4923                       strsec = section_headers + symsec->sh_link;
4924
4925                       strtab = get_data (NULL, file, strsec->sh_offset,
4926                                          1, strsec->sh_size,
4927                                          _("string table"));
4928                       strtablen = strtab == NULL ? 0 : strsec->sh_size;
4929                     }
4930
4931                   dump_relocations (file, rel_offset, rel_size,
4932                                     symtab, nsyms, strtab, strtablen, is_rela);
4933                   if (strtab)
4934                     free (strtab);
4935                   free (symtab);
4936                 }
4937               else
4938                 dump_relocations (file, rel_offset, rel_size,
4939                                   NULL, 0, NULL, 0, is_rela);
4940
4941               found = 1;
4942             }
4943         }
4944
4945       if (! found)
4946         printf (_("\nThere are no relocations in this file.\n"));
4947     }
4948
4949   return 1;
4950 }
4951
4952 /* Process the unwind section.  */
4953
4954 #include "unwind-ia64.h"
4955
4956 /* An absolute address consists of a section and an offset.  If the
4957    section is NULL, the offset itself is the address, otherwise, the
4958    address equals to LOAD_ADDRESS(section) + offset.  */
4959
4960 struct absaddr
4961   {
4962     unsigned short section;
4963     bfd_vma offset;
4964   };
4965
4966 #define ABSADDR(a) \
4967   ((a).section \
4968    ? section_headers [(a).section].sh_addr + (a).offset \
4969    : (a).offset)
4970
4971 struct ia64_unw_aux_info
4972   {
4973     struct ia64_unw_table_entry
4974       {
4975         struct absaddr start;
4976         struct absaddr end;
4977         struct absaddr info;
4978       }
4979     *table;                     /* Unwind table.  */
4980     unsigned long table_len;    /* Length of unwind table.  */
4981     unsigned char *info;        /* Unwind info.  */
4982     unsigned long info_size;    /* Size of unwind info.  */
4983     bfd_vma info_addr;          /* starting address of unwind info.  */
4984     bfd_vma seg_base;           /* Starting address of segment.  */
4985     Elf_Internal_Sym *symtab;   /* The symbol table.  */
4986     unsigned long nsyms;        /* Number of symbols.  */
4987     char *strtab;               /* The string table.  */
4988     unsigned long strtab_size;  /* Size of string table.  */
4989   };
4990
4991 static void
4992 find_symbol_for_address (Elf_Internal_Sym *symtab,
4993                          unsigned long nsyms,
4994                          const char *strtab,
4995                          unsigned long strtab_size,
4996                          struct absaddr addr,
4997                          const char **symname,
4998                          bfd_vma *offset)
4999 {
5000   bfd_vma dist = 0x100000;
5001   Elf_Internal_Sym *sym, *best = NULL;
5002   unsigned long i;
5003
5004   for (i = 0, sym = symtab; i < nsyms; ++i, ++sym)
5005     {
5006       if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
5007           && sym->st_name != 0
5008           && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
5009           && addr.offset >= sym->st_value
5010           && addr.offset - sym->st_value < dist)
5011         {
5012           best = sym;
5013           dist = addr.offset - sym->st_value;
5014           if (!dist)
5015             break;
5016         }
5017     }
5018   if (best)
5019     {
5020       *symname = (best->st_name >= strtab_size
5021                   ? "<corrupt>" : strtab + best->st_name);
5022       *offset = dist;
5023       return;
5024     }
5025   *symname = NULL;
5026   *offset = addr.offset;
5027 }
5028
5029 static void
5030 dump_ia64_unwind (struct ia64_unw_aux_info *aux)
5031 {
5032   struct ia64_unw_table_entry *tp;
5033   int in_body;
5034
5035   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
5036     {
5037       bfd_vma stamp;
5038       bfd_vma offset;
5039       const unsigned char *dp;
5040       const unsigned char *head;
5041       const char *procname;
5042
5043       find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
5044                                aux->strtab_size, tp->start, &procname, &offset);
5045
5046       fputs ("\n<", stdout);
5047
5048       if (procname)
5049         {
5050           fputs (procname, stdout);
5051
5052           if (offset)
5053             printf ("+%lx", (unsigned long) offset);
5054         }
5055
5056       fputs (">: [", stdout);
5057       print_vma (tp->start.offset, PREFIX_HEX);
5058       fputc ('-', stdout);
5059       print_vma (tp->end.offset, PREFIX_HEX);
5060       printf ("], info at +0x%lx\n",
5061               (unsigned long) (tp->info.offset - aux->seg_base));
5062
5063       head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
5064       stamp = byte_get ((unsigned char *) head, sizeof (stamp));
5065
5066       printf ("  v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
5067               (unsigned) UNW_VER (stamp),
5068               (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
5069               UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
5070               UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
5071               (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
5072
5073       if (UNW_VER (stamp) != 1)
5074         {
5075           printf ("\tUnknown version.\n");
5076           continue;
5077         }
5078
5079       in_body = 0;
5080       for (dp = head + 8; dp < head + 8 + eh_addr_size * UNW_LENGTH (stamp);)
5081         dp = unw_decode (dp, in_body, & in_body);
5082     }
5083 }
5084
5085 static int
5086 slurp_ia64_unwind_table (FILE *file,
5087                          struct ia64_unw_aux_info *aux,
5088                          Elf_Internal_Shdr *sec)
5089 {
5090   unsigned long size, nrelas, i;
5091   Elf_Internal_Phdr *seg;
5092   struct ia64_unw_table_entry *tep;
5093   Elf_Internal_Shdr *relsec;
5094   Elf_Internal_Rela *rela, *rp;
5095   unsigned char *table, *tp;
5096   Elf_Internal_Sym *sym;
5097   const char *relname;
5098
5099   /* First, find the starting address of the segment that includes
5100      this section: */
5101
5102   if (elf_header.e_phnum)
5103     {
5104       if (! get_program_headers (file))
5105           return 0;
5106
5107       for (seg = program_headers;
5108            seg < program_headers + elf_header.e_phnum;
5109            ++seg)
5110         {
5111           if (seg->p_type != PT_LOAD)
5112             continue;
5113
5114           if (sec->sh_addr >= seg->p_vaddr
5115               && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
5116             {
5117               aux->seg_base = seg->p_vaddr;
5118               break;
5119             }
5120         }
5121     }
5122
5123   /* Second, build the unwind table from the contents of the unwind section:  */
5124   size = sec->sh_size;
5125   table = get_data (NULL, file, sec->sh_offset, 1, size, _("unwind table"));
5126   if (!table)
5127     return 0;
5128
5129   aux->table = xcmalloc (size / (3 * eh_addr_size), sizeof (aux->table[0]));
5130   tep = aux->table;
5131   for (tp = table; tp < table + size; tp += 3 * eh_addr_size, ++tep)
5132     {
5133       tep->start.section = SHN_UNDEF;
5134       tep->end.section   = SHN_UNDEF;
5135       tep->info.section  = SHN_UNDEF;
5136       if (is_32bit_elf)
5137         {
5138           tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
5139           tep->end.offset   = byte_get ((unsigned char *) tp + 4, 4);
5140           tep->info.offset  = byte_get ((unsigned char *) tp + 8, 4);
5141         }
5142       else
5143         {
5144           tep->start.offset = BYTE_GET ((unsigned char *) tp +  0);
5145           tep->end.offset   = BYTE_GET ((unsigned char *) tp +  8);
5146           tep->info.offset  = BYTE_GET ((unsigned char *) tp + 16);
5147         }
5148       tep->start.offset += aux->seg_base;
5149       tep->end.offset   += aux->seg_base;
5150       tep->info.offset  += aux->seg_base;
5151     }
5152   free (table);
5153
5154   /* Third, apply any relocations to the unwind table:  */
5155   for (relsec = section_headers;
5156        relsec < section_headers + elf_header.e_shnum;
5157        ++relsec)
5158     {
5159       if (relsec->sh_type != SHT_RELA
5160           || relsec->sh_info >= elf_header.e_shnum
5161           || section_headers + relsec->sh_info != sec)
5162         continue;
5163
5164       if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
5165                               & rela, & nrelas))
5166         return 0;
5167
5168       for (rp = rela; rp < rela + nrelas; ++rp)
5169         {
5170           relname = elf_ia64_reloc_type (get_reloc_type (rp->r_info));
5171           sym = aux->symtab + get_reloc_symindex (rp->r_info);
5172
5173           if (! const_strneq (relname, "R_IA64_SEGREL"))
5174             {
5175               warn (_("Skipping unexpected relocation type %s\n"), relname);
5176               continue;
5177             }
5178
5179           i = rp->r_offset / (3 * eh_addr_size);
5180
5181           switch (rp->r_offset/eh_addr_size % 3)
5182             {
5183             case 0:
5184               aux->table[i].start.section = sym->st_shndx;
5185               aux->table[i].start.offset += rp->r_addend + sym->st_value;
5186               break;
5187             case 1:
5188               aux->table[i].end.section   = sym->st_shndx;
5189               aux->table[i].end.offset   += rp->r_addend + sym->st_value;
5190               break;
5191             case 2:
5192               aux->table[i].info.section  = sym->st_shndx;
5193               aux->table[i].info.offset  += rp->r_addend + sym->st_value;
5194               break;
5195             default:
5196               break;
5197             }
5198         }
5199
5200       free (rela);
5201     }
5202
5203   aux->table_len = size / (3 * eh_addr_size);
5204   return 1;
5205 }
5206
5207 static int
5208 ia64_process_unwind (FILE *file)
5209 {
5210   Elf_Internal_Shdr *sec, *unwsec = NULL, *strsec;
5211   unsigned long i, unwcount = 0, unwstart = 0;
5212   struct ia64_unw_aux_info aux;
5213
5214   memset (& aux, 0, sizeof (aux));
5215
5216   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5217     {
5218       if (sec->sh_type == SHT_SYMTAB
5219           && sec->sh_link < elf_header.e_shnum)
5220         {
5221           aux.nsyms = sec->sh_size / sec->sh_entsize;
5222           aux.symtab = GET_ELF_SYMBOLS (file, sec);
5223
5224           strsec = section_headers + sec->sh_link;
5225           aux.strtab = get_data (NULL, file, strsec->sh_offset,
5226                                  1, strsec->sh_size, _("string table"));
5227           aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
5228         }
5229       else if (sec->sh_type == SHT_IA_64_UNWIND)
5230         unwcount++;
5231     }
5232
5233   if (!unwcount)
5234     printf (_("\nThere are no unwind sections in this file.\n"));
5235
5236   while (unwcount-- > 0)
5237     {
5238       char *suffix;
5239       size_t len, len2;
5240
5241       for (i = unwstart, sec = section_headers + unwstart;
5242            i < elf_header.e_shnum; ++i, ++sec)
5243         if (sec->sh_type == SHT_IA_64_UNWIND)
5244           {
5245             unwsec = sec;
5246             break;
5247           }
5248
5249       unwstart = i + 1;
5250       len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
5251
5252       if ((unwsec->sh_flags & SHF_GROUP) != 0)
5253         {
5254           /* We need to find which section group it is in.  */
5255           struct group_list *g = section_headers_groups [i]->root;
5256
5257           for (; g != NULL; g = g->next)
5258             {
5259               sec = section_headers + g->section_index;
5260
5261               if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
5262                 break;
5263             }
5264
5265           if (g == NULL)
5266             i = elf_header.e_shnum;
5267         }
5268       else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
5269         {
5270           /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO.  */
5271           len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
5272           suffix = SECTION_NAME (unwsec) + len;
5273           for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5274                ++i, ++sec)
5275             if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
5276                 && streq (SECTION_NAME (sec) + len2, suffix))
5277               break;
5278         }
5279       else
5280         {
5281           /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
5282              .IA_64.unwind or BAR -> .IA_64.unwind_info.  */
5283           len = sizeof (ELF_STRING_ia64_unwind) - 1;
5284           len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
5285           suffix = "";
5286           if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
5287             suffix = SECTION_NAME (unwsec) + len;
5288           for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5289                ++i, ++sec)
5290             if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
5291                 && streq (SECTION_NAME (sec) + len2, suffix))
5292               break;
5293         }
5294
5295       if (i == elf_header.e_shnum)
5296         {
5297           printf (_("\nCould not find unwind info section for "));
5298
5299           if (string_table == NULL)
5300             printf ("%d", unwsec->sh_name);
5301           else
5302             printf (_("'%s'"), SECTION_NAME (unwsec));
5303         }
5304       else
5305         {
5306           aux.info_size = sec->sh_size;
5307           aux.info_addr = sec->sh_addr;
5308           aux.info = get_data (NULL, file, sec->sh_offset, 1, aux.info_size,
5309                                _("unwind info"));
5310
5311           printf (_("\nUnwind section "));
5312
5313           if (string_table == NULL)
5314             printf ("%d", unwsec->sh_name);
5315           else
5316             printf (_("'%s'"), SECTION_NAME (unwsec));
5317
5318           printf (_(" at offset 0x%lx contains %lu entries:\n"),
5319                   (unsigned long) unwsec->sh_offset,
5320                   (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
5321
5322           (void) slurp_ia64_unwind_table (file, & aux, unwsec);
5323
5324           if (aux.table_len > 0)
5325             dump_ia64_unwind (& aux);
5326
5327           if (aux.table)
5328             free ((char *) aux.table);
5329           if (aux.info)
5330             free ((char *) aux.info);
5331           aux.table = NULL;
5332           aux.info = NULL;
5333         }
5334     }
5335
5336   if (aux.symtab)
5337     free (aux.symtab);
5338   if (aux.strtab)
5339     free ((char *) aux.strtab);
5340
5341   return 1;
5342 }
5343
5344 struct hppa_unw_aux_info
5345   {
5346     struct hppa_unw_table_entry
5347       {
5348         struct absaddr start;
5349         struct absaddr end;
5350         unsigned int Cannot_unwind:1;                   /* 0 */
5351         unsigned int Millicode:1;                       /* 1 */
5352         unsigned int Millicode_save_sr0:1;              /* 2 */
5353         unsigned int Region_description:2;              /* 3..4 */
5354         unsigned int reserved1:1;                       /* 5 */
5355         unsigned int Entry_SR:1;                        /* 6 */
5356         unsigned int Entry_FR:4;     /* number saved */ /* 7..10 */
5357         unsigned int Entry_GR:5;     /* number saved */ /* 11..15 */
5358         unsigned int Args_stored:1;                     /* 16 */
5359         unsigned int Variable_Frame:1;                  /* 17 */
5360         unsigned int Separate_Package_Body:1;           /* 18 */
5361         unsigned int Frame_Extension_Millicode:1;       /* 19 */
5362         unsigned int Stack_Overflow_Check:1;            /* 20 */
5363         unsigned int Two_Instruction_SP_Increment:1;    /* 21 */
5364         unsigned int Ada_Region:1;                      /* 22 */
5365         unsigned int cxx_info:1;                        /* 23 */
5366         unsigned int cxx_try_catch:1;                   /* 24 */
5367         unsigned int sched_entry_seq:1;                 /* 25 */
5368         unsigned int reserved2:1;                       /* 26 */
5369         unsigned int Save_SP:1;                         /* 27 */
5370         unsigned int Save_RP:1;                         /* 28 */
5371         unsigned int Save_MRP_in_frame:1;               /* 29 */
5372         unsigned int extn_ptr_defined:1;                /* 30 */
5373         unsigned int Cleanup_defined:1;                 /* 31 */
5374
5375         unsigned int MPE_XL_interrupt_marker:1;         /* 0 */
5376         unsigned int HP_UX_interrupt_marker:1;          /* 1 */
5377         unsigned int Large_frame:1;                     /* 2 */
5378         unsigned int Pseudo_SP_Set:1;                   /* 3 */
5379         unsigned int reserved4:1;                       /* 4 */
5380         unsigned int Total_frame_size:27;               /* 5..31 */
5381       }
5382     *table;                     /* Unwind table.  */
5383     unsigned long table_len;    /* Length of unwind table.  */
5384     bfd_vma seg_base;           /* Starting address of segment.  */
5385     Elf_Internal_Sym *symtab;   /* The symbol table.  */
5386     unsigned long nsyms;        /* Number of symbols.  */
5387     char *strtab;               /* The string table.  */
5388     unsigned long strtab_size;  /* Size of string table.  */
5389   };
5390
5391 static void
5392 dump_hppa_unwind (struct hppa_unw_aux_info *aux)
5393 {
5394   struct hppa_unw_table_entry *tp;
5395
5396   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
5397     {
5398       bfd_vma offset;
5399       const char *procname;
5400
5401       find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
5402                                aux->strtab_size, tp->start, &procname,
5403                                &offset);
5404
5405       fputs ("\n<", stdout);
5406
5407       if (procname)
5408         {
5409           fputs (procname, stdout);
5410
5411           if (offset)
5412             printf ("+%lx", (unsigned long) offset);
5413         }
5414
5415       fputs (">: [", stdout);
5416       print_vma (tp->start.offset, PREFIX_HEX);
5417       fputc ('-', stdout);
5418       print_vma (tp->end.offset, PREFIX_HEX);
5419       printf ("]\n\t");
5420
5421 #define PF(_m) if (tp->_m) printf (#_m " ");
5422 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
5423       PF(Cannot_unwind);
5424       PF(Millicode);
5425       PF(Millicode_save_sr0);
5426       /* PV(Region_description);  */
5427       PF(Entry_SR);
5428       PV(Entry_FR);
5429       PV(Entry_GR);
5430       PF(Args_stored);
5431       PF(Variable_Frame);
5432       PF(Separate_Package_Body);
5433       PF(Frame_Extension_Millicode);
5434       PF(Stack_Overflow_Check);
5435       PF(Two_Instruction_SP_Increment);
5436       PF(Ada_Region);
5437       PF(cxx_info);
5438       PF(cxx_try_catch);
5439       PF(sched_entry_seq);
5440       PF(Save_SP);
5441       PF(Save_RP);
5442       PF(Save_MRP_in_frame);
5443       PF(extn_ptr_defined);
5444       PF(Cleanup_defined);
5445       PF(MPE_XL_interrupt_marker);
5446       PF(HP_UX_interrupt_marker);
5447       PF(Large_frame);
5448       PF(Pseudo_SP_Set);
5449       PV(Total_frame_size);
5450 #undef PF
5451 #undef PV
5452     }
5453
5454   printf ("\n");
5455 }
5456
5457 static int
5458 slurp_hppa_unwind_table (FILE *file,
5459                          struct hppa_unw_aux_info *aux,
5460                          Elf_Internal_Shdr *sec)
5461 {
5462   unsigned long size, unw_ent_size, nentries, nrelas, i;
5463   Elf_Internal_Phdr *seg;
5464   struct hppa_unw_table_entry *tep;
5465   Elf_Internal_Shdr *relsec;
5466   Elf_Internal_Rela *rela, *rp;
5467   unsigned char *table, *tp;
5468   Elf_Internal_Sym *sym;
5469   const char *relname;
5470
5471   /* First, find the starting address of the segment that includes
5472      this section.  */
5473
5474   if (elf_header.e_phnum)
5475     {
5476       if (! get_program_headers (file))
5477         return 0;
5478
5479       for (seg = program_headers;
5480            seg < program_headers + elf_header.e_phnum;
5481            ++seg)
5482         {
5483           if (seg->p_type != PT_LOAD)
5484             continue;
5485
5486           if (sec->sh_addr >= seg->p_vaddr
5487               && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
5488             {
5489               aux->seg_base = seg->p_vaddr;
5490               break;
5491             }
5492         }
5493     }
5494
5495   /* Second, build the unwind table from the contents of the unwind
5496      section.  */
5497   size = sec->sh_size;
5498   table = get_data (NULL, file, sec->sh_offset, 1, size, _("unwind table"));
5499   if (!table)
5500     return 0;
5501
5502   unw_ent_size = 16;
5503   nentries = size / unw_ent_size;
5504   size = unw_ent_size * nentries;
5505
5506   tep = aux->table = xcmalloc (nentries, sizeof (aux->table[0]));
5507
5508   for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
5509     {
5510       unsigned int tmp1, tmp2;
5511
5512       tep->start.section = SHN_UNDEF;
5513       tep->end.section   = SHN_UNDEF;
5514
5515       tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
5516       tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
5517       tmp1 = byte_get ((unsigned char *) tp + 8, 4);
5518       tmp2 = byte_get ((unsigned char *) tp + 12, 4);
5519
5520       tep->start.offset += aux->seg_base;
5521       tep->end.offset   += aux->seg_base;
5522
5523       tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
5524       tep->Millicode = (tmp1 >> 30) & 0x1;
5525       tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
5526       tep->Region_description = (tmp1 >> 27) & 0x3;
5527       tep->reserved1 = (tmp1 >> 26) & 0x1;
5528       tep->Entry_SR = (tmp1 >> 25) & 0x1;
5529       tep->Entry_FR = (tmp1 >> 21) & 0xf;
5530       tep->Entry_GR = (tmp1 >> 16) & 0x1f;
5531       tep->Args_stored = (tmp1 >> 15) & 0x1;
5532       tep->Variable_Frame = (tmp1 >> 14) & 0x1;
5533       tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
5534       tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
5535       tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
5536       tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
5537       tep->Ada_Region = (tmp1 >> 9) & 0x1;
5538       tep->cxx_info = (tmp1 >> 8) & 0x1;
5539       tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
5540       tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
5541       tep->reserved2 = (tmp1 >> 5) & 0x1;
5542       tep->Save_SP = (tmp1 >> 4) & 0x1;
5543       tep->Save_RP = (tmp1 >> 3) & 0x1;
5544       tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
5545       tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
5546       tep->Cleanup_defined = tmp1 & 0x1;
5547
5548       tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
5549       tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
5550       tep->Large_frame = (tmp2 >> 29) & 0x1;
5551       tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
5552       tep->reserved4 = (tmp2 >> 27) & 0x1;
5553       tep->Total_frame_size = tmp2 & 0x7ffffff;
5554     }
5555   free (table);
5556
5557   /* Third, apply any relocations to the unwind table.  */
5558   for (relsec = section_headers;
5559        relsec < section_headers + elf_header.e_shnum;
5560        ++relsec)
5561     {
5562       if (relsec->sh_type != SHT_RELA
5563           || relsec->sh_info >= elf_header.e_shnum
5564           || section_headers + relsec->sh_info != sec)
5565         continue;
5566
5567       if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
5568                               & rela, & nrelas))
5569         return 0;
5570
5571       for (rp = rela; rp < rela + nrelas; ++rp)
5572         {
5573           relname = elf_hppa_reloc_type (get_reloc_type (rp->r_info));
5574           sym = aux->symtab + get_reloc_symindex (rp->r_info);
5575
5576           /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64.  */
5577           if (! const_strneq (relname, "R_PARISC_SEGREL"))
5578             {
5579               warn (_("Skipping unexpected relocation type %s\n"), relname);
5580               continue;
5581             }
5582
5583           i = rp->r_offset / unw_ent_size;
5584
5585           switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
5586             {
5587             case 0:
5588               aux->table[i].start.section = sym->st_shndx;
5589               aux->table[i].start.offset += sym->st_value + rp->r_addend;
5590               break;
5591             case 1:
5592               aux->table[i].end.section   = sym->st_shndx;
5593               aux->table[i].end.offset   += sym->st_value + rp->r_addend;
5594               break;
5595             default:
5596               break;
5597             }
5598         }
5599
5600       free (rela);
5601     }
5602
5603   aux->table_len = nentries;
5604
5605   return 1;
5606 }
5607
5608 static int
5609 hppa_process_unwind (FILE *file)
5610 {
5611   struct hppa_unw_aux_info aux;
5612   Elf_Internal_Shdr *unwsec = NULL;
5613   Elf_Internal_Shdr *strsec;
5614   Elf_Internal_Shdr *sec;
5615   unsigned long i;
5616
5617   memset (& aux, 0, sizeof (aux));
5618
5619   if (string_table == NULL)
5620     return 1;
5621
5622   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5623     {
5624       if (sec->sh_type == SHT_SYMTAB
5625           && sec->sh_link < elf_header.e_shnum)
5626         {
5627           aux.nsyms = sec->sh_size / sec->sh_entsize;
5628           aux.symtab = GET_ELF_SYMBOLS (file, sec);
5629
5630           strsec = section_headers + sec->sh_link;
5631           aux.strtab = get_data (NULL, file, strsec->sh_offset,
5632                                  1, strsec->sh_size, _("string table"));
5633           aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
5634         }
5635       else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
5636         unwsec = sec;
5637     }
5638
5639   if (!unwsec)
5640     printf (_("\nThere are no unwind sections in this file.\n"));
5641
5642   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5643     {
5644       if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
5645         {
5646           printf (_("\nUnwind section "));
5647           printf (_("'%s'"), SECTION_NAME (sec));
5648
5649           printf (_(" at offset 0x%lx contains %lu entries:\n"),
5650                   (unsigned long) sec->sh_offset,
5651                   (unsigned long) (sec->sh_size / (2 * eh_addr_size + 8)));
5652
5653           slurp_hppa_unwind_table (file, &aux, sec);
5654           if (aux.table_len > 0)
5655             dump_hppa_unwind (&aux);
5656
5657           if (aux.table)
5658             free ((char *) aux.table);
5659           aux.table = NULL;
5660         }
5661     }
5662
5663   if (aux.symtab)
5664     free (aux.symtab);
5665   if (aux.strtab)
5666     free ((char *) aux.strtab);
5667
5668   return 1;
5669 }
5670
5671 static int
5672 process_unwind (FILE *file)
5673 {
5674   struct unwind_handler {
5675     int machtype;
5676     int (*handler)(FILE *file);
5677   } handlers[] = {
5678     { EM_IA_64, ia64_process_unwind },
5679     { EM_PARISC, hppa_process_unwind },
5680     { 0, 0 }
5681   };
5682   int i;
5683
5684   if (!do_unwind)
5685     return 1;
5686
5687   for (i = 0; handlers[i].handler != NULL; i++)
5688     if (elf_header.e_machine == handlers[i].machtype)
5689       return handlers[i].handler (file);
5690
5691   printf (_("\nThere are no unwind sections in this file.\n"));
5692   return 1;
5693 }
5694
5695 static void
5696 dynamic_section_mips_val (Elf_Internal_Dyn *entry)
5697 {
5698   switch (entry->d_tag)
5699     {
5700     case DT_MIPS_FLAGS:
5701       if (entry->d_un.d_val == 0)
5702         printf ("NONE\n");
5703       else
5704         {
5705           static const char * opts[] =
5706           {
5707             "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
5708             "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
5709             "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
5710             "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
5711             "RLD_ORDER_SAFE"
5712           };
5713           unsigned int cnt;
5714           int first = 1;
5715           for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
5716             if (entry->d_un.d_val & (1 << cnt))
5717               {
5718                 printf ("%s%s", first ? "" : " ", opts[cnt]);
5719                 first = 0;
5720               }
5721           puts ("");
5722         }
5723       break;
5724
5725     case DT_MIPS_IVERSION:
5726       if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
5727         printf ("Interface Version: %s\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
5728       else
5729         printf ("<corrupt: %ld>\n", (long) entry->d_un.d_ptr);
5730       break;
5731
5732     case DT_MIPS_TIME_STAMP:
5733       {
5734         char timebuf[20];
5735         struct tm *tmp;
5736
5737         time_t time = entry->d_un.d_val;
5738         tmp = gmtime (&time);
5739         snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
5740                   tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
5741                   tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
5742         printf ("Time Stamp: %s\n", timebuf);
5743       }
5744       break;
5745
5746     case DT_MIPS_RLD_VERSION:
5747     case DT_MIPS_LOCAL_GOTNO:
5748     case DT_MIPS_CONFLICTNO:
5749     case DT_MIPS_LIBLISTNO:
5750     case DT_MIPS_SYMTABNO:
5751     case DT_MIPS_UNREFEXTNO:
5752     case DT_MIPS_HIPAGENO:
5753     case DT_MIPS_DELTA_CLASS_NO:
5754     case DT_MIPS_DELTA_INSTANCE_NO:
5755     case DT_MIPS_DELTA_RELOC_NO:
5756     case DT_MIPS_DELTA_SYM_NO:
5757     case DT_MIPS_DELTA_CLASSSYM_NO:
5758     case DT_MIPS_COMPACT_SIZE:
5759       printf ("%ld\n", (long) entry->d_un.d_ptr);
5760       break;
5761
5762     default:
5763       printf ("%#lx\n", (unsigned long) entry->d_un.d_ptr);
5764     }
5765 }
5766
5767
5768 static void
5769 dynamic_section_parisc_val (Elf_Internal_Dyn *entry)
5770 {
5771   switch (entry->d_tag)
5772     {
5773     case DT_HP_DLD_FLAGS:
5774       {
5775         static struct
5776         {
5777           long int bit;
5778           const char *str;
5779         }
5780         flags[] =
5781         {
5782           { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
5783           { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
5784           { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
5785           { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
5786           { DT_HP_BIND_NOW, "HP_BIND_NOW" },
5787           { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
5788           { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
5789           { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
5790           { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
5791           { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
5792           { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
5793           { DT_HP_GST, "HP_GST" },
5794           { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
5795           { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
5796           { DT_HP_NODELETE, "HP_NODELETE" },
5797           { DT_HP_GROUP, "HP_GROUP" },
5798           { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
5799         };
5800         int first = 1;
5801         size_t cnt;
5802         bfd_vma val = entry->d_un.d_val;
5803
5804         for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
5805           if (val & flags[cnt].bit)
5806             {
5807               if (! first)
5808                 putchar (' ');
5809               fputs (flags[cnt].str, stdout);
5810               first = 0;
5811               val ^= flags[cnt].bit;
5812             }
5813
5814         if (val != 0 || first)
5815           {
5816             if (! first)
5817               putchar (' ');
5818             print_vma (val, HEX);
5819           }
5820       }
5821       break;
5822
5823     default:
5824       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5825       break;
5826     }
5827   putchar ('\n');
5828 }
5829
5830 static void
5831 dynamic_section_ia64_val (Elf_Internal_Dyn *entry)
5832 {
5833   switch (entry->d_tag)
5834     {
5835     case DT_IA_64_PLT_RESERVE:
5836       /* First 3 slots reserved.  */
5837       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5838       printf (" -- ");
5839       print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
5840       break;
5841
5842     default:
5843       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5844       break;
5845     }
5846   putchar ('\n');
5847 }
5848
5849 static int
5850 get_32bit_dynamic_section (FILE *file)
5851 {
5852   Elf32_External_Dyn *edyn, *ext;
5853   Elf_Internal_Dyn *entry;
5854
5855   edyn = get_data (NULL, file, dynamic_addr, 1, dynamic_size,
5856                    _("dynamic section"));
5857   if (!edyn)
5858     return 0;
5859
5860 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
5861    might not have the luxury of section headers.  Look for the DT_NULL
5862    terminator to determine the number of entries.  */
5863   for (ext = edyn, dynamic_nent = 0;
5864        (char *) ext < (char *) edyn + dynamic_size;
5865        ext++)
5866     {
5867       dynamic_nent++;
5868       if (BYTE_GET (ext->d_tag) == DT_NULL)
5869         break;
5870     }
5871
5872   dynamic_section = cmalloc (dynamic_nent, sizeof (*entry));
5873   if (dynamic_section == NULL)
5874     {
5875       error (_("Out of memory\n"));
5876       free (edyn);
5877       return 0;
5878     }
5879
5880   for (ext = edyn, entry = dynamic_section;
5881        entry < dynamic_section + dynamic_nent;
5882        ext++, entry++)
5883     {
5884       entry->d_tag      = BYTE_GET (ext->d_tag);
5885       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
5886     }
5887
5888   free (edyn);
5889
5890   return 1;
5891 }
5892
5893 static int
5894 get_64bit_dynamic_section (FILE *file)
5895 {
5896   Elf64_External_Dyn *edyn, *ext;
5897   Elf_Internal_Dyn *entry;
5898
5899   edyn = get_data (NULL, file, dynamic_addr, 1, dynamic_size,
5900                    _("dynamic section"));
5901   if (!edyn)
5902     return 0;
5903
5904 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
5905    might not have the luxury of section headers.  Look for the DT_NULL
5906    terminator to determine the number of entries.  */
5907   for (ext = edyn, dynamic_nent = 0;
5908        (char *) ext < (char *) edyn + dynamic_size;
5909        ext++)
5910     {
5911       dynamic_nent++;
5912       if (BYTE_GET (ext->d_tag) == DT_NULL)
5913         break;
5914     }
5915
5916   dynamic_section = cmalloc (dynamic_nent, sizeof (*entry));
5917   if (dynamic_section == NULL)
5918     {
5919       error (_("Out of memory\n"));
5920       free (edyn);
5921       return 0;
5922     }
5923
5924   for (ext = edyn, entry = dynamic_section;
5925        entry < dynamic_section + dynamic_nent;
5926        ext++, entry++)
5927     {
5928       entry->d_tag      = BYTE_GET (ext->d_tag);
5929       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
5930     }
5931
5932   free (edyn);
5933
5934   return 1;
5935 }
5936
5937 static void
5938 print_dynamic_flags (bfd_vma flags)
5939 {
5940   int first = 1;
5941
5942   while (flags)
5943     {
5944       bfd_vma flag;
5945
5946       flag = flags & - flags;
5947       flags &= ~ flag;
5948
5949       if (first)
5950         first = 0;
5951       else
5952         putc (' ', stdout);
5953
5954       switch (flag)
5955         {
5956         case DF_ORIGIN:         fputs ("ORIGIN", stdout); break;
5957         case DF_SYMBOLIC:       fputs ("SYMBOLIC", stdout); break;
5958         case DF_TEXTREL:        fputs ("TEXTREL", stdout); break;
5959         case DF_BIND_NOW:       fputs ("BIND_NOW", stdout); break;
5960         case DF_STATIC_TLS:     fputs ("STATIC_TLS", stdout); break;
5961         default:                fputs ("unknown", stdout); break;
5962         }
5963     }
5964   puts ("");
5965 }
5966
5967 /* Parse and display the contents of the dynamic section.  */
5968
5969 static int
5970 process_dynamic_section (FILE *file)
5971 {
5972   Elf_Internal_Dyn *entry;
5973
5974   if (dynamic_size == 0)
5975     {
5976       if (do_dynamic)
5977         printf (_("\nThere is no dynamic section in this file.\n"));
5978
5979       return 1;
5980     }
5981
5982   if (is_32bit_elf)
5983     {
5984       if (! get_32bit_dynamic_section (file))
5985         return 0;
5986     }
5987   else if (! get_64bit_dynamic_section (file))
5988     return 0;
5989
5990   /* Find the appropriate symbol table.  */
5991   if (dynamic_symbols == NULL)
5992     {
5993       for (entry = dynamic_section;
5994            entry < dynamic_section + dynamic_nent;
5995            ++entry)
5996         {
5997           Elf_Internal_Shdr section;
5998
5999           if (entry->d_tag != DT_SYMTAB)
6000             continue;
6001
6002           dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
6003
6004           /* Since we do not know how big the symbol table is,
6005              we default to reading in the entire file (!) and
6006              processing that.  This is overkill, I know, but it
6007              should work.  */
6008           section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
6009
6010           if (archive_file_offset != 0)
6011             section.sh_size = archive_file_size - section.sh_offset;
6012           else
6013             {
6014               if (fseek (file, 0, SEEK_END))
6015                 error (_("Unable to seek to end of file!\n"));
6016
6017               section.sh_size = ftell (file) - section.sh_offset;
6018             }
6019
6020           if (is_32bit_elf)
6021             section.sh_entsize = sizeof (Elf32_External_Sym);
6022           else
6023             section.sh_entsize = sizeof (Elf64_External_Sym);
6024
6025           num_dynamic_syms = section.sh_size / section.sh_entsize;
6026           if (num_dynamic_syms < 1)
6027             {
6028               error (_("Unable to determine the number of symbols to load\n"));
6029               continue;
6030             }
6031
6032           dynamic_symbols = GET_ELF_SYMBOLS (file, &section);
6033         }
6034     }
6035
6036   /* Similarly find a string table.  */
6037   if (dynamic_strings == NULL)
6038     {
6039       for (entry = dynamic_section;
6040            entry < dynamic_section + dynamic_nent;
6041            ++entry)
6042         {
6043           unsigned long offset;
6044           long str_tab_len;
6045
6046           if (entry->d_tag != DT_STRTAB)
6047             continue;
6048
6049           dynamic_info[DT_STRTAB] = entry->d_un.d_val;
6050
6051           /* Since we do not know how big the string table is,
6052              we default to reading in the entire file (!) and
6053              processing that.  This is overkill, I know, but it
6054              should work.  */
6055
6056           offset = offset_from_vma (file, entry->d_un.d_val, 0);
6057
6058           if (archive_file_offset != 0)
6059             str_tab_len = archive_file_size - offset;
6060           else
6061             {
6062               if (fseek (file, 0, SEEK_END))
6063                 error (_("Unable to seek to end of file\n"));
6064               str_tab_len = ftell (file) - offset;
6065             }
6066
6067           if (str_tab_len < 1)
6068             {
6069               error
6070                 (_("Unable to determine the length of the dynamic string table\n"));
6071               continue;
6072             }
6073
6074           dynamic_strings = get_data (NULL, file, offset, 1, str_tab_len,
6075                                       _("dynamic string table"));
6076           dynamic_strings_length = str_tab_len;
6077           break;
6078         }
6079     }
6080
6081   /* And find the syminfo section if available.  */
6082   if (dynamic_syminfo == NULL)
6083     {
6084       unsigned long syminsz = 0;
6085
6086       for (entry = dynamic_section;
6087            entry < dynamic_section + dynamic_nent;
6088            ++entry)
6089         {
6090           if (entry->d_tag == DT_SYMINENT)
6091             {
6092               /* Note: these braces are necessary to avoid a syntax
6093                  error from the SunOS4 C compiler.  */
6094               assert (sizeof (Elf_External_Syminfo) == entry->d_un.d_val);
6095             }
6096           else if (entry->d_tag == DT_SYMINSZ)
6097             syminsz = entry->d_un.d_val;
6098           else if (entry->d_tag == DT_SYMINFO)
6099             dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
6100                                                       syminsz);
6101         }
6102
6103       if (dynamic_syminfo_offset != 0 && syminsz != 0)
6104         {
6105           Elf_External_Syminfo *extsyminfo, *extsym;
6106           Elf_Internal_Syminfo *syminfo;
6107
6108           /* There is a syminfo section.  Read the data.  */
6109           extsyminfo = get_data (NULL, file, dynamic_syminfo_offset, 1,
6110                                  syminsz, _("symbol information"));
6111           if (!extsyminfo)
6112             return 0;
6113
6114           dynamic_syminfo = malloc (syminsz);
6115           if (dynamic_syminfo == NULL)
6116             {
6117               error (_("Out of memory\n"));
6118               return 0;
6119             }
6120
6121           dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
6122           for (syminfo = dynamic_syminfo, extsym = extsyminfo;
6123                syminfo < dynamic_syminfo + dynamic_syminfo_nent;
6124                ++syminfo, ++extsym)
6125             {
6126               syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
6127               syminfo->si_flags = BYTE_GET (extsym->si_flags);
6128             }
6129
6130           free (extsyminfo);
6131         }
6132     }
6133
6134   if (do_dynamic && dynamic_addr)
6135     printf (_("\nDynamic section at offset 0x%lx contains %u entries:\n"),
6136             dynamic_addr, dynamic_nent);
6137   if (do_dynamic)
6138     printf (_("  Tag        Type                         Name/Value\n"));
6139
6140   for (entry = dynamic_section;
6141        entry < dynamic_section + dynamic_nent;
6142        entry++)
6143     {
6144       if (do_dynamic)
6145         {
6146           const char *dtype;
6147
6148           putchar (' ');
6149           print_vma (entry->d_tag, FULL_HEX);
6150           dtype = get_dynamic_type (entry->d_tag);
6151           printf (" (%s)%*s", dtype,
6152                   ((is_32bit_elf ? 27 : 19)
6153                    - (int) strlen (dtype)),
6154                   " ");
6155         }
6156
6157       switch (entry->d_tag)
6158         {
6159         case DT_FLAGS:
6160           if (do_dynamic)
6161             print_dynamic_flags (entry->d_un.d_val);
6162           break;
6163
6164         case DT_AUXILIARY:
6165         case DT_FILTER:
6166         case DT_CONFIG:
6167         case DT_DEPAUDIT:
6168         case DT_AUDIT:
6169           if (do_dynamic)
6170             {
6171               switch (entry->d_tag)
6172                 {
6173                 case DT_AUXILIARY:
6174                   printf (_("Auxiliary library"));
6175                   break;
6176
6177                 case DT_FILTER:
6178                   printf (_("Filter library"));
6179                   break;
6180
6181                 case DT_CONFIG:
6182                   printf (_("Configuration file"));
6183                   break;
6184
6185                 case DT_DEPAUDIT:
6186                   printf (_("Dependency audit library"));
6187                   break;
6188
6189                 case DT_AUDIT:
6190                   printf (_("Audit library"));
6191                   break;
6192                 }
6193
6194               if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
6195                 printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
6196               else
6197                 {
6198                   printf (": ");
6199                   print_vma (entry->d_un.d_val, PREFIX_HEX);
6200                   putchar ('\n');
6201                 }
6202             }
6203           break;
6204
6205         case DT_FEATURE:
6206           if (do_dynamic)
6207             {
6208               printf (_("Flags:"));
6209
6210               if (entry->d_un.d_val == 0)
6211                 printf (_(" None\n"));
6212               else
6213                 {
6214                   unsigned long int val = entry->d_un.d_val;
6215
6216                   if (val & DTF_1_PARINIT)
6217                     {
6218                       printf (" PARINIT");
6219                       val ^= DTF_1_PARINIT;
6220                     }
6221                   if (val & DTF_1_CONFEXP)
6222                     {
6223                       printf (" CONFEXP");
6224                       val ^= DTF_1_CONFEXP;
6225                     }
6226                   if (val != 0)
6227                     printf (" %lx", val);
6228                   puts ("");
6229                 }
6230             }
6231           break;
6232
6233         case DT_POSFLAG_1:
6234           if (do_dynamic)
6235             {
6236               printf (_("Flags:"));
6237
6238               if (entry->d_un.d_val == 0)
6239                 printf (_(" None\n"));
6240               else
6241                 {
6242                   unsigned long int val = entry->d_un.d_val;
6243
6244                   if (val & DF_P1_LAZYLOAD)
6245                     {
6246                       printf (" LAZYLOAD");
6247                       val ^= DF_P1_LAZYLOAD;
6248                     }
6249                   if (val & DF_P1_GROUPPERM)
6250                     {
6251                       printf (" GROUPPERM");
6252                       val ^= DF_P1_GROUPPERM;
6253                     }
6254                   if (val != 0)
6255                     printf (" %lx", val);
6256                   puts ("");
6257                 }
6258             }
6259           break;
6260
6261         case DT_FLAGS_1:
6262           if (do_dynamic)
6263             {
6264               printf (_("Flags:"));
6265               if (entry->d_un.d_val == 0)
6266                 printf (_(" None\n"));
6267               else
6268                 {
6269                   unsigned long int val = entry->d_un.d_val;
6270
6271                   if (val & DF_1_NOW)
6272                     {
6273                       printf (" NOW");
6274                       val ^= DF_1_NOW;
6275                     }
6276                   if (val & DF_1_GLOBAL)
6277                     {
6278                       printf (" GLOBAL");
6279                       val ^= DF_1_GLOBAL;
6280                     }
6281                   if (val & DF_1_GROUP)
6282                     {
6283                       printf (" GROUP");
6284                       val ^= DF_1_GROUP;
6285                     }
6286                   if (val & DF_1_NODELETE)
6287                     {
6288                       printf (" NODELETE");
6289                       val ^= DF_1_NODELETE;
6290                     }
6291                   if (val & DF_1_LOADFLTR)
6292                     {
6293                       printf (" LOADFLTR");
6294                       val ^= DF_1_LOADFLTR;
6295                     }
6296                   if (val & DF_1_INITFIRST)
6297                     {
6298                       printf (" INITFIRST");
6299                       val ^= DF_1_INITFIRST;
6300                     }
6301                   if (val & DF_1_NOOPEN)
6302                     {
6303                       printf (" NOOPEN");
6304                       val ^= DF_1_NOOPEN;
6305                     }
6306                   if (val & DF_1_ORIGIN)
6307                     {
6308                       printf (" ORIGIN");
6309                       val ^= DF_1_ORIGIN;
6310                     }
6311                   if (val & DF_1_DIRECT)
6312                     {
6313                       printf (" DIRECT");
6314                       val ^= DF_1_DIRECT;
6315                     }
6316                   if (val & DF_1_TRANS)
6317                     {
6318                       printf (" TRANS");
6319                       val ^= DF_1_TRANS;
6320                     }
6321                   if (val & DF_1_INTERPOSE)
6322                     {
6323                       printf (" INTERPOSE");
6324                       val ^= DF_1_INTERPOSE;
6325                     }
6326                   if (val & DF_1_NODEFLIB)
6327                     {
6328                       printf (" NODEFLIB");
6329                       val ^= DF_1_NODEFLIB;
6330                     }
6331                   if (val & DF_1_NODUMP)
6332                     {
6333                       printf (" NODUMP");
6334                       val ^= DF_1_NODUMP;
6335                     }
6336                   if (val & DF_1_CONLFAT)
6337                     {
6338                       printf (" CONLFAT");
6339                       val ^= DF_1_CONLFAT;
6340                     }
6341                   if (val != 0)
6342                     printf (" %lx", val);
6343                   puts ("");
6344                 }
6345             }
6346           break;
6347
6348         case DT_PLTREL:
6349           dynamic_info[entry->d_tag] = entry->d_un.d_val;
6350           if (do_dynamic)
6351             puts (get_dynamic_type (entry->d_un.d_val));
6352           break;
6353
6354         case DT_NULL    :
6355         case DT_NEEDED  :
6356         case DT_PLTGOT  :
6357         case DT_HASH    :
6358         case DT_STRTAB  :
6359         case DT_SYMTAB  :
6360         case DT_RELA    :
6361         case DT_INIT    :
6362         case DT_FINI    :
6363         case DT_SONAME  :
6364         case DT_RPATH   :
6365         case DT_SYMBOLIC:
6366         case DT_REL     :
6367         case DT_DEBUG   :
6368         case DT_TEXTREL :
6369         case DT_JMPREL  :
6370         case DT_RUNPATH :
6371           dynamic_info[entry->d_tag] = entry->d_un.d_val;
6372
6373           if (do_dynamic)
6374             {
6375               char *name;
6376
6377               if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
6378                 name = GET_DYNAMIC_NAME (entry->d_un.d_val);
6379               else
6380                 name = NULL;
6381
6382               if (name)
6383                 {
6384                   switch (entry->d_tag)
6385                     {
6386                     case DT_NEEDED:
6387                       printf (_("Shared library: [%s]"), name);
6388
6389                       if (streq (name, program_interpreter))
6390                         printf (_(" program interpreter"));
6391                       break;
6392
6393                     case DT_SONAME:
6394                       printf (_("Library soname: [%s]"), name);
6395                       break;
6396
6397                     case DT_RPATH:
6398                       printf (_("Library rpath: [%s]"), name);
6399                       break;
6400
6401                     case DT_RUNPATH:
6402                       printf (_("Library runpath: [%s]"), name);
6403                       break;
6404
6405                     default:
6406                       print_vma (entry->d_un.d_val, PREFIX_HEX);
6407                       break;
6408                     }
6409                 }
6410               else
6411                 print_vma (entry->d_un.d_val, PREFIX_HEX);
6412
6413               putchar ('\n');
6414             }
6415           break;
6416
6417         case DT_PLTRELSZ:
6418         case DT_RELASZ  :
6419         case DT_STRSZ   :
6420         case DT_RELSZ   :
6421         case DT_RELAENT :
6422         case DT_SYMENT  :
6423         case DT_RELENT  :
6424           dynamic_info[entry->d_tag] = entry->d_un.d_val;
6425         case DT_PLTPADSZ:
6426         case DT_MOVEENT :
6427         case DT_MOVESZ  :
6428         case DT_INIT_ARRAYSZ:
6429         case DT_FINI_ARRAYSZ:
6430         case DT_GNU_CONFLICTSZ:
6431         case DT_GNU_LIBLISTSZ:
6432           if (do_dynamic)
6433             {
6434               print_vma (entry->d_un.d_val, UNSIGNED);
6435               printf (" (bytes)\n");
6436             }
6437           break;
6438
6439         case DT_VERDEFNUM:
6440         case DT_VERNEEDNUM:
6441         case DT_RELACOUNT:
6442         case DT_RELCOUNT:
6443           if (do_dynamic)
6444             {
6445               print_vma (entry->d_un.d_val, UNSIGNED);
6446               putchar ('\n');
6447             }
6448           break;
6449
6450         case DT_SYMINSZ:
6451         case DT_SYMINENT:
6452         case DT_SYMINFO:
6453         case DT_USED:
6454         case DT_INIT_ARRAY:
6455         case DT_FINI_ARRAY:
6456           if (do_dynamic)
6457             {
6458               if (entry->d_tag == DT_USED
6459                   && VALID_DYNAMIC_NAME (entry->d_un.d_val))
6460                 {
6461                   char *name = GET_DYNAMIC_NAME (entry->d_un.d_val);
6462
6463                   if (*name)
6464                     {
6465                       printf (_("Not needed object: [%s]\n"), name);
6466                       break;
6467                     }
6468                 }
6469
6470               print_vma (entry->d_un.d_val, PREFIX_HEX);
6471               putchar ('\n');
6472             }
6473           break;
6474
6475         case DT_BIND_NOW:
6476           /* The value of this entry is ignored.  */
6477           if (do_dynamic)
6478             putchar ('\n');
6479           break;
6480
6481         case DT_GNU_PRELINKED:
6482           if (do_dynamic)
6483             {
6484               struct tm *tmp;
6485               time_t time = entry->d_un.d_val;
6486
6487               tmp = gmtime (&time);
6488               printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
6489                       tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
6490                       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
6491
6492             }
6493           break;
6494
6495         case DT_GNU_HASH:
6496           dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
6497           if (do_dynamic)
6498             {
6499               print_vma (entry->d_un.d_val, PREFIX_HEX);
6500               putchar ('\n');
6501             }
6502           break;
6503
6504         default:
6505           if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
6506             version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
6507               entry->d_un.d_val;
6508
6509           if (do_dynamic)
6510             {
6511               switch (elf_header.e_machine)
6512                 {
6513                 case EM_MIPS:
6514                 case EM_MIPS_RS3_LE:
6515                   dynamic_section_mips_val (entry);
6516                   break;
6517                 case EM_PARISC:
6518                   dynamic_section_parisc_val (entry);
6519                   break;
6520                 case EM_IA_64:
6521                   dynamic_section_ia64_val (entry);
6522                   break;
6523                 default:
6524                   print_vma (entry->d_un.d_val, PREFIX_HEX);
6525                   putchar ('\n');
6526                 }
6527             }
6528           break;
6529         }
6530     }
6531
6532   return 1;
6533 }
6534
6535 static char *
6536 get_ver_flags (unsigned int flags)
6537 {
6538   static char buff[32];
6539
6540   buff[0] = 0;
6541
6542   if (flags == 0)
6543     return _("none");
6544
6545   if (flags & VER_FLG_BASE)
6546     strcat (buff, "BASE ");
6547
6548   if (flags & VER_FLG_WEAK)
6549     {
6550       if (flags & VER_FLG_BASE)
6551         strcat (buff, "| ");
6552
6553       strcat (buff, "WEAK ");
6554     }
6555
6556   if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK))
6557     strcat (buff, "| <unknown>");
6558
6559   return buff;
6560 }
6561
6562 /* Display the contents of the version sections.  */
6563 static int
6564 process_version_sections (FILE *file)
6565 {
6566   Elf_Internal_Shdr *section;
6567   unsigned i;
6568   int found = 0;
6569
6570   if (! do_version)
6571     return 1;
6572
6573   for (i = 0, section = section_headers;
6574        i < elf_header.e_shnum;
6575        i++, section++)
6576     {
6577       switch (section->sh_type)
6578         {
6579         case SHT_GNU_verdef:
6580           {
6581             Elf_External_Verdef *edefs;
6582             unsigned int idx;
6583             unsigned int cnt;
6584             char *endbuf;
6585
6586             found = 1;
6587
6588             printf
6589               (_("\nVersion definition section '%s' contains %u entries:\n"),
6590                SECTION_NAME (section), section->sh_info);
6591
6592             printf (_("  Addr: 0x"));
6593             printf_vma (section->sh_addr);
6594             printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
6595                     (unsigned long) section->sh_offset, section->sh_link,
6596                     section->sh_link < elf_header.e_shnum
6597                     ? SECTION_NAME (section_headers + section->sh_link)
6598                     : "<corrupt>");
6599
6600             edefs = get_data (NULL, file, section->sh_offset, 1,
6601                               section->sh_size,
6602                               _("version definition section"));
6603             endbuf = (char *) edefs + section->sh_size;
6604             if (!edefs)
6605               break;
6606
6607             for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
6608               {
6609                 char *vstart;
6610                 Elf_External_Verdef *edef;
6611                 Elf_Internal_Verdef ent;
6612                 Elf_External_Verdaux *eaux;
6613                 Elf_Internal_Verdaux aux;
6614                 int j;
6615                 int isum;
6616
6617                 vstart = ((char *) edefs) + idx;
6618                 if (vstart + sizeof (*edef) > endbuf)
6619                   break;
6620
6621                 edef = (Elf_External_Verdef *) vstart;
6622
6623                 ent.vd_version = BYTE_GET (edef->vd_version);
6624                 ent.vd_flags   = BYTE_GET (edef->vd_flags);
6625                 ent.vd_ndx     = BYTE_GET (edef->vd_ndx);
6626                 ent.vd_cnt     = BYTE_GET (edef->vd_cnt);
6627                 ent.vd_hash    = BYTE_GET (edef->vd_hash);
6628                 ent.vd_aux     = BYTE_GET (edef->vd_aux);
6629                 ent.vd_next    = BYTE_GET (edef->vd_next);
6630
6631                 printf (_("  %#06x: Rev: %d  Flags: %s"),
6632                         idx, ent.vd_version, get_ver_flags (ent.vd_flags));
6633
6634                 printf (_("  Index: %d  Cnt: %d  "),
6635                         ent.vd_ndx, ent.vd_cnt);
6636
6637                 vstart += ent.vd_aux;
6638
6639                 eaux = (Elf_External_Verdaux *) vstart;
6640
6641                 aux.vda_name = BYTE_GET (eaux->vda_name);
6642                 aux.vda_next = BYTE_GET (eaux->vda_next);
6643
6644                 if (VALID_DYNAMIC_NAME (aux.vda_name))
6645                   printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
6646                 else
6647                   printf (_("Name index: %ld\n"), aux.vda_name);
6648
6649                 isum = idx + ent.vd_aux;
6650
6651                 for (j = 1; j < ent.vd_cnt; j++)
6652                   {
6653                     isum   += aux.vda_next;
6654                     vstart += aux.vda_next;
6655
6656                     eaux = (Elf_External_Verdaux *) vstart;
6657                     if (vstart + sizeof (*eaux) > endbuf)
6658                       break;
6659
6660                     aux.vda_name = BYTE_GET (eaux->vda_name);
6661                     aux.vda_next = BYTE_GET (eaux->vda_next);
6662
6663                     if (VALID_DYNAMIC_NAME (aux.vda_name))
6664                       printf (_("  %#06x: Parent %d: %s\n"),
6665                               isum, j, GET_DYNAMIC_NAME (aux.vda_name));
6666                     else
6667                       printf (_("  %#06x: Parent %d, name index: %ld\n"),
6668                               isum, j, aux.vda_name);
6669                   }
6670                 if (j < ent.vd_cnt)
6671                   printf (_("  Version def aux past end of section\n"));
6672
6673                 idx += ent.vd_next;
6674               }
6675             if (cnt < section->sh_info)
6676               printf (_("  Version definition past end of section\n"));
6677
6678             free (edefs);
6679           }
6680           break;
6681
6682         case SHT_GNU_verneed:
6683           {
6684             Elf_External_Verneed *eneed;
6685             unsigned int idx;
6686             unsigned int cnt;
6687             char *endbuf;
6688
6689             found = 1;
6690
6691             printf (_("\nVersion needs section '%s' contains %u entries:\n"),
6692                     SECTION_NAME (section), section->sh_info);
6693
6694             printf (_(" Addr: 0x"));
6695             printf_vma (section->sh_addr);
6696             printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
6697                     (unsigned long) section->sh_offset, section->sh_link,
6698                     section->sh_link < elf_header.e_shnum
6699                     ? SECTION_NAME (section_headers + section->sh_link)
6700                     : "<corrupt>");
6701
6702             eneed = get_data (NULL, file, section->sh_offset, 1,
6703                               section->sh_size,
6704                               _("version need section"));
6705             endbuf = (char *) eneed + section->sh_size;
6706             if (!eneed)
6707               break;
6708
6709             for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
6710               {
6711                 Elf_External_Verneed *entry;
6712                 Elf_Internal_Verneed ent;
6713                 int j;
6714                 int isum;
6715                 char *vstart;
6716
6717                 vstart = ((char *) eneed) + idx;
6718                 if (vstart + sizeof (*entry) > endbuf)
6719                   break;
6720
6721                 entry = (Elf_External_Verneed *) vstart;
6722
6723                 ent.vn_version = BYTE_GET (entry->vn_version);
6724                 ent.vn_cnt     = BYTE_GET (entry->vn_cnt);
6725                 ent.vn_file    = BYTE_GET (entry->vn_file);
6726                 ent.vn_aux     = BYTE_GET (entry->vn_aux);
6727                 ent.vn_next    = BYTE_GET (entry->vn_next);
6728
6729                 printf (_("  %#06x: Version: %d"), idx, ent.vn_version);
6730
6731                 if (VALID_DYNAMIC_NAME (ent.vn_file))
6732                   printf (_("  File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
6733                 else
6734                   printf (_("  File: %lx"), ent.vn_file);
6735
6736                 printf (_("  Cnt: %d\n"), ent.vn_cnt);
6737
6738                 vstart += ent.vn_aux;
6739
6740                 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
6741                   {
6742                     Elf_External_Vernaux *eaux;
6743                     Elf_Internal_Vernaux aux;
6744
6745                     if (vstart + sizeof (*eaux) > endbuf)
6746                       break;
6747                     eaux = (Elf_External_Vernaux *) vstart;
6748
6749                     aux.vna_hash  = BYTE_GET (eaux->vna_hash);
6750                     aux.vna_flags = BYTE_GET (eaux->vna_flags);
6751                     aux.vna_other = BYTE_GET (eaux->vna_other);
6752                     aux.vna_name  = BYTE_GET (eaux->vna_name);
6753                     aux.vna_next  = BYTE_GET (eaux->vna_next);
6754
6755                     if (VALID_DYNAMIC_NAME (aux.vna_name))
6756                       printf (_("  %#06x:   Name: %s"),
6757                               isum, GET_DYNAMIC_NAME (aux.vna_name));
6758                     else
6759                       printf (_("  %#06x:   Name index: %lx"),
6760                               isum, aux.vna_name);
6761
6762                     printf (_("  Flags: %s  Version: %d\n"),
6763                             get_ver_flags (aux.vna_flags), aux.vna_other);
6764
6765                     isum   += aux.vna_next;
6766                     vstart += aux.vna_next;
6767                   }
6768                 if (j < ent.vn_cnt)
6769                   printf (_("  Version need aux past end of section\n"));
6770
6771                 idx += ent.vn_next;
6772               }
6773             if (cnt < section->sh_info)
6774               printf (_("  Version need past end of section\n"));
6775
6776             free (eneed);
6777           }
6778           break;
6779
6780         case SHT_GNU_versym:
6781           {
6782             Elf_Internal_Shdr *link_section;
6783             int total;
6784             int cnt;
6785             unsigned char *edata;
6786             unsigned short *data;
6787             char *strtab;
6788             Elf_Internal_Sym *symbols;
6789             Elf_Internal_Shdr *string_sec;
6790             long off;
6791
6792             if (section->sh_link >= elf_header.e_shnum)
6793               break;
6794
6795             link_section = section_headers + section->sh_link;
6796             total = section->sh_size / sizeof (Elf_External_Versym);
6797
6798             if (link_section->sh_link >= elf_header.e_shnum)
6799               break;
6800
6801             found = 1;
6802
6803             symbols = GET_ELF_SYMBOLS (file, link_section);
6804
6805             string_sec = section_headers + link_section->sh_link;
6806
6807             strtab = get_data (NULL, file, string_sec->sh_offset, 1,
6808                                string_sec->sh_size, _("version string table"));
6809             if (!strtab)
6810               break;
6811
6812             printf (_("\nVersion symbols section '%s' contains %d entries:\n"),
6813                     SECTION_NAME (section), total);
6814
6815             printf (_(" Addr: "));
6816             printf_vma (section->sh_addr);
6817             printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
6818                     (unsigned long) section->sh_offset, section->sh_link,
6819                     SECTION_NAME (link_section));
6820
6821             off = offset_from_vma (file,
6822                                    version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
6823                                    total * sizeof (short));
6824             edata = get_data (NULL, file, off, total, sizeof (short),
6825                               _("version symbol data"));
6826             if (!edata)
6827               {
6828                 free (strtab);
6829                 break;
6830               }
6831
6832             data = cmalloc (total, sizeof (short));
6833
6834             for (cnt = total; cnt --;)
6835               data[cnt] = byte_get (edata + cnt * sizeof (short),
6836                                     sizeof (short));
6837
6838             free (edata);
6839
6840             for (cnt = 0; cnt < total; cnt += 4)
6841               {
6842                 int j, nn;
6843                 int check_def, check_need;
6844                 char *name;
6845
6846                 printf ("  %03x:", cnt);
6847
6848                 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
6849                   switch (data[cnt + j])
6850                     {
6851                     case 0:
6852                       fputs (_("   0 (*local*)    "), stdout);
6853                       break;
6854
6855                     case 1:
6856                       fputs (_("   1 (*global*)   "), stdout);
6857                       break;
6858
6859                     default:
6860                       nn = printf ("%4x%c", data[cnt + j] & 0x7fff,
6861                                    data[cnt + j] & 0x8000 ? 'h' : ' ');
6862
6863                       check_def = 1;
6864                       check_need = 1;
6865                       if (symbols[cnt + j].st_shndx >= elf_header.e_shnum
6866                           || section_headers[symbols[cnt + j].st_shndx].sh_type
6867                              != SHT_NOBITS)
6868                         {
6869                           if (symbols[cnt + j].st_shndx == SHN_UNDEF)
6870                             check_def = 0;
6871                           else
6872                             check_need = 0;
6873                         }
6874
6875                       if (check_need
6876                           && version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
6877                         {
6878                           Elf_Internal_Verneed ivn;
6879                           unsigned long offset;
6880
6881                           offset = offset_from_vma
6882                             (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
6883                              sizeof (Elf_External_Verneed));
6884
6885                           do
6886                             {
6887                               Elf_Internal_Vernaux ivna;
6888                               Elf_External_Verneed evn;
6889                               Elf_External_Vernaux evna;
6890                               unsigned long a_off;
6891
6892                               get_data (&evn, file, offset, sizeof (evn), 1,
6893                                         _("version need"));
6894
6895                               ivn.vn_aux  = BYTE_GET (evn.vn_aux);
6896                               ivn.vn_next = BYTE_GET (evn.vn_next);
6897
6898                               a_off = offset + ivn.vn_aux;
6899
6900                               do
6901                                 {
6902                                   get_data (&evna, file, a_off, sizeof (evna),
6903                                             1, _("version need aux (2)"));
6904
6905                                   ivna.vna_next  = BYTE_GET (evna.vna_next);
6906                                   ivna.vna_other = BYTE_GET (evna.vna_other);
6907
6908                                   a_off += ivna.vna_next;
6909                                 }
6910                               while (ivna.vna_other != data[cnt + j]
6911                                      && ivna.vna_next != 0);
6912
6913                               if (ivna.vna_other == data[cnt + j])
6914                                 {
6915                                   ivna.vna_name = BYTE_GET (evna.vna_name);
6916
6917                                   if (ivna.vna_name >= string_sec->sh_size)
6918                                     name = _("*invalid*");
6919                                   else
6920                                     name = strtab + ivna.vna_name;
6921                                   nn += printf ("(%s%-*s",
6922                                                 name,
6923                                                 12 - (int) strlen (name),
6924                                                 ")");
6925                                   check_def = 0;
6926                                   break;
6927                                 }
6928
6929                               offset += ivn.vn_next;
6930                             }
6931                           while (ivn.vn_next);
6932                         }
6933
6934                       if (check_def && data[cnt + j] != 0x8001
6935                           && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
6936                         {
6937                           Elf_Internal_Verdef ivd;
6938                           Elf_External_Verdef evd;
6939                           unsigned long offset;
6940
6941                           offset = offset_from_vma
6942                             (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
6943                              sizeof evd);
6944
6945                           do
6946                             {
6947                               get_data (&evd, file, offset, sizeof (evd), 1,
6948                                         _("version def"));
6949
6950                               ivd.vd_next = BYTE_GET (evd.vd_next);
6951                               ivd.vd_ndx  = BYTE_GET (evd.vd_ndx);
6952
6953                               offset += ivd.vd_next;
6954                             }
6955                           while (ivd.vd_ndx != (data[cnt + j] & 0x7fff)
6956                                  && ivd.vd_next != 0);
6957
6958                           if (ivd.vd_ndx == (data[cnt + j] & 0x7fff))
6959                             {
6960                               Elf_External_Verdaux evda;
6961                               Elf_Internal_Verdaux ivda;
6962
6963                               ivd.vd_aux = BYTE_GET (evd.vd_aux);
6964
6965                               get_data (&evda, file,
6966                                         offset - ivd.vd_next + ivd.vd_aux,
6967                                         sizeof (evda), 1,
6968                                         _("version def aux"));
6969
6970                               ivda.vda_name = BYTE_GET (evda.vda_name);
6971
6972                               if (ivda.vda_name >= string_sec->sh_size)
6973                                 name = _("*invalid*");
6974                               else
6975                                 name = strtab + ivda.vda_name;
6976                               nn += printf ("(%s%-*s",
6977                                             name,
6978                                             12 - (int) strlen (name),
6979                                             ")");
6980                             }
6981                         }
6982
6983                       if (nn < 18)
6984                         printf ("%*c", 18 - nn, ' ');
6985                     }
6986
6987                 putchar ('\n');
6988               }
6989
6990             free (data);
6991             free (strtab);
6992             free (symbols);
6993           }
6994           break;
6995
6996         default:
6997           break;
6998         }
6999     }
7000
7001   if (! found)
7002     printf (_("\nNo version information found in this file.\n"));
7003
7004   return 1;
7005 }
7006
7007 static const char *
7008 get_symbol_binding (unsigned int binding)
7009 {
7010   static char buff[32];
7011
7012   switch (binding)
7013     {
7014     case STB_LOCAL:     return "LOCAL";
7015     case STB_GLOBAL:    return "GLOBAL";
7016     case STB_WEAK:      return "WEAK";
7017     default:
7018       if (binding >= STB_LOPROC && binding <= STB_HIPROC)
7019         snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
7020                   binding);
7021       else if (binding >= STB_LOOS && binding <= STB_HIOS)
7022         snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
7023       else
7024         snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
7025       return buff;
7026     }
7027 }
7028
7029 static const char *
7030 get_symbol_type (unsigned int type)
7031 {
7032   static char buff[32];
7033
7034   switch (type)
7035     {
7036     case STT_NOTYPE:    return "NOTYPE";
7037     case STT_OBJECT:    return "OBJECT";
7038     case STT_FUNC:      return "FUNC";
7039     case STT_SECTION:   return "SECTION";
7040     case STT_FILE:      return "FILE";
7041     case STT_COMMON:    return "COMMON";
7042     case STT_TLS:       return "TLS";
7043     case STT_RELC:      return "RELC";
7044     case STT_SRELC:     return "SRELC";
7045     default:
7046       if (type >= STT_LOPROC && type <= STT_HIPROC)
7047         {
7048           if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
7049             return "THUMB_FUNC";
7050
7051           if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
7052             return "REGISTER";
7053
7054           if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
7055             return "PARISC_MILLI";
7056
7057           snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
7058         }
7059       else if (type >= STT_LOOS && type <= STT_HIOS)
7060         {
7061           if (elf_header.e_machine == EM_PARISC)
7062             {
7063               if (type == STT_HP_OPAQUE)
7064                 return "HP_OPAQUE";
7065               if (type == STT_HP_STUB)
7066                 return "HP_STUB";
7067             }
7068           else if (elf_header.e_ident[EI_OSABI] == ELFOSABI_LINUX
7069                    || elf_header.e_ident[EI_OSABI] == ELFOSABI_HURD
7070                    /* GNU/Linux is still using the default value 0.  */
7071                    || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE)
7072             {
7073               if (type == STT_IFUNC)
7074                 return "IFUNC";
7075             }
7076
7077           snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
7078         }
7079       else
7080         snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
7081       return buff;
7082     }
7083 }
7084
7085 static const char *
7086 get_symbol_visibility (unsigned int visibility)
7087 {
7088   switch (visibility)
7089     {
7090     case STV_DEFAULT:   return "DEFAULT";
7091     case STV_INTERNAL:  return "INTERNAL";
7092     case STV_HIDDEN:    return "HIDDEN";
7093     case STV_PROTECTED: return "PROTECTED";
7094     default: abort ();
7095     }
7096 }
7097
7098 static const char *
7099 get_mips_symbol_other (unsigned int other)
7100 {
7101   switch (other)
7102     {
7103     case STO_OPTIONAL:  return "OPTIONAL";
7104     case STO_MIPS16:    return "MIPS16";
7105     case STO_MIPS_PLT:  return "MIPS PLT";
7106     case STO_MIPS_PIC:  return "MIPS PIC";
7107     default:            return NULL;
7108     }
7109 }
7110
7111 static const char *
7112 get_symbol_other (unsigned int other)
7113 {
7114   const char * result = NULL;
7115   static char buff [32];
7116
7117   if (other == 0)
7118     return "";
7119
7120   switch (elf_header.e_machine)
7121     {
7122     case EM_MIPS:
7123       result = get_mips_symbol_other (other);
7124     default:
7125       break;
7126     }
7127
7128   if (result)
7129     return result;
7130
7131   snprintf (buff, sizeof buff, _("<other>: %x"), other);
7132   return buff;
7133 }
7134
7135 static const char *
7136 get_symbol_index_type (unsigned int type)
7137 {
7138   static char buff[32];
7139
7140   switch (type)
7141     {
7142     case SHN_UNDEF:     return "UND";
7143     case SHN_ABS:       return "ABS";
7144     case SHN_COMMON:    return "COM";
7145     default:
7146       if (type == SHN_IA_64_ANSI_COMMON
7147           && elf_header.e_machine == EM_IA_64
7148           && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
7149         return "ANSI_COM";
7150       else if (elf_header.e_machine == EM_X86_64
7151                && type == SHN_X86_64_LCOMMON)
7152         return "LARGE_COM";
7153       else if (type == SHN_MIPS_SCOMMON
7154                && elf_header.e_machine == EM_MIPS)
7155         return "SCOM";
7156       else if (type == SHN_MIPS_SUNDEFINED
7157                && elf_header.e_machine == EM_MIPS)
7158         return "SUND";
7159       else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
7160         sprintf (buff, "PRC[0x%04x]", type & 0xffff);
7161       else if (type >= SHN_LOOS && type <= SHN_HIOS)
7162         sprintf (buff, "OS [0x%04x]", type & 0xffff);
7163       else if (type >= SHN_LORESERVE)
7164         sprintf (buff, "RSV[0x%04x]", type & 0xffff);
7165       else
7166         sprintf (buff, "%3d", type);
7167       break;
7168     }
7169
7170   return buff;
7171 }
7172
7173 static bfd_vma *
7174 get_dynamic_data (FILE *file, unsigned int number, unsigned int ent_size)
7175 {
7176   unsigned char *e_data;
7177   bfd_vma *i_data;
7178
7179   e_data = cmalloc (number, ent_size);
7180
7181   if (e_data == NULL)
7182     {
7183       error (_("Out of memory\n"));
7184       return NULL;
7185     }
7186
7187   if (fread (e_data, ent_size, number, file) != number)
7188     {
7189       error (_("Unable to read in dynamic data\n"));
7190       return NULL;
7191     }
7192
7193   i_data = cmalloc (number, sizeof (*i_data));
7194
7195   if (i_data == NULL)
7196     {
7197       error (_("Out of memory\n"));
7198       free (e_data);
7199       return NULL;
7200     }
7201
7202   while (number--)
7203     i_data[number] = byte_get (e_data + number * ent_size, ent_size);
7204
7205   free (e_data);
7206
7207   return i_data;
7208 }
7209
7210 static void
7211 print_dynamic_symbol (bfd_vma si, unsigned long hn)
7212 {
7213   Elf_Internal_Sym *psym;
7214   int n;
7215
7216   psym = dynamic_symbols + si;
7217
7218   n = print_vma (si, DEC_5);
7219   if (n < 5)
7220     fputs ("     " + n, stdout);
7221   printf (" %3lu: ", hn);
7222   print_vma (psym->st_value, LONG_HEX);
7223   putchar (' ');
7224   print_vma (psym->st_size, DEC_5);
7225
7226   printf ("  %6s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
7227   printf (" %6s",  get_symbol_binding (ELF_ST_BIND (psym->st_info)));
7228   printf (" %3s",  get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
7229   /* Check to see if any other bits in the st_other field are set.
7230      Note - displaying this information disrupts the layout of the
7231      table being generated, but for the moment this case is very
7232      rare.  */
7233   if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
7234     printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
7235   printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
7236   if (VALID_DYNAMIC_NAME (psym->st_name))
7237     print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
7238   else
7239     printf (" <corrupt: %14ld>", psym->st_name);
7240   putchar ('\n');
7241 }
7242
7243 /* Dump the symbol table.  */
7244 static int
7245 process_symbol_table (FILE *file)
7246 {
7247   Elf_Internal_Shdr *section;
7248   bfd_vma nbuckets = 0;
7249   bfd_vma nchains = 0;
7250   bfd_vma *buckets = NULL;
7251   bfd_vma *chains = NULL;
7252   bfd_vma ngnubuckets = 0;
7253   bfd_vma *gnubuckets = NULL;
7254   bfd_vma *gnuchains = NULL;
7255   bfd_vma gnusymidx = 0;
7256
7257   if (! do_syms && !do_histogram)
7258     return 1;
7259
7260   if (dynamic_info[DT_HASH]
7261       && (do_histogram
7262           || (do_using_dynamic && dynamic_strings != NULL)))
7263     {
7264       unsigned char nb[8];
7265       unsigned char nc[8];
7266       int hash_ent_size = 4;
7267
7268       if ((elf_header.e_machine == EM_ALPHA
7269            || elf_header.e_machine == EM_S390
7270            || elf_header.e_machine == EM_S390_OLD)
7271           && elf_header.e_ident[EI_CLASS] == ELFCLASS64)
7272         hash_ent_size = 8;
7273
7274       if (fseek (file,
7275                  (archive_file_offset
7276                   + offset_from_vma (file, dynamic_info[DT_HASH],
7277                                      sizeof nb + sizeof nc)),
7278                  SEEK_SET))
7279         {
7280           error (_("Unable to seek to start of dynamic information\n"));
7281           return 0;
7282         }
7283
7284       if (fread (nb, hash_ent_size, 1, file) != 1)
7285         {
7286           error (_("Failed to read in number of buckets\n"));
7287           return 0;
7288         }
7289
7290       if (fread (nc, hash_ent_size, 1, file) != 1)
7291         {
7292           error (_("Failed to read in number of chains\n"));
7293           return 0;
7294         }
7295
7296       nbuckets = byte_get (nb, hash_ent_size);
7297       nchains  = byte_get (nc, hash_ent_size);
7298
7299       buckets = get_dynamic_data (file, nbuckets, hash_ent_size);
7300       chains  = get_dynamic_data (file, nchains, hash_ent_size);
7301
7302       if (buckets == NULL || chains == NULL)
7303         return 0;
7304     }
7305
7306   if (dynamic_info_DT_GNU_HASH
7307       && (do_histogram
7308           || (do_using_dynamic && dynamic_strings != NULL)))
7309     {
7310       unsigned char nb[16];
7311       bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
7312       bfd_vma buckets_vma;
7313
7314       if (fseek (file,
7315                  (archive_file_offset
7316                   + offset_from_vma (file, dynamic_info_DT_GNU_HASH,
7317                                      sizeof nb)),
7318                  SEEK_SET))
7319         {
7320           error (_("Unable to seek to start of dynamic information\n"));
7321           return 0;
7322         }
7323
7324       if (fread (nb, 16, 1, file) != 1)
7325         {
7326           error (_("Failed to read in number of buckets\n"));
7327           return 0;
7328         }
7329
7330       ngnubuckets = byte_get (nb, 4);
7331       gnusymidx = byte_get (nb + 4, 4);
7332       bitmaskwords = byte_get (nb + 8, 4);
7333       buckets_vma = dynamic_info_DT_GNU_HASH + 16;
7334       if (is_32bit_elf)
7335         buckets_vma += bitmaskwords * 4;
7336       else
7337         buckets_vma += bitmaskwords * 8;
7338
7339       if (fseek (file,
7340                  (archive_file_offset
7341                   + offset_from_vma (file, buckets_vma, 4)),
7342                  SEEK_SET))
7343         {
7344           error (_("Unable to seek to start of dynamic information\n"));
7345           return 0;
7346         }
7347
7348       gnubuckets = get_dynamic_data (file, ngnubuckets, 4);
7349
7350       if (gnubuckets == NULL)
7351         return 0;
7352
7353       for (i = 0; i < ngnubuckets; i++)
7354         if (gnubuckets[i] != 0)
7355           {
7356             if (gnubuckets[i] < gnusymidx)
7357               return 0;
7358
7359             if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
7360               maxchain = gnubuckets[i];
7361           }
7362
7363       if (maxchain == 0xffffffff)
7364         return 0;
7365
7366       maxchain -= gnusymidx;
7367
7368       if (fseek (file,
7369                  (archive_file_offset
7370                   + offset_from_vma (file, buckets_vma
7371                                            + 4 * (ngnubuckets + maxchain), 4)),
7372                  SEEK_SET))
7373         {
7374           error (_("Unable to seek to start of dynamic information\n"));
7375           return 0;
7376         }
7377
7378       do
7379         {
7380           if (fread (nb, 4, 1, file) != 1)
7381             {
7382               error (_("Failed to determine last chain length\n"));
7383               return 0;
7384             }
7385
7386           if (maxchain + 1 == 0)
7387             return 0;
7388
7389           ++maxchain;
7390         }
7391       while ((byte_get (nb, 4) & 1) == 0);
7392
7393       if (fseek (file,
7394                  (archive_file_offset
7395                   + offset_from_vma (file, buckets_vma + 4 * ngnubuckets, 4)),
7396                  SEEK_SET))
7397         {
7398           error (_("Unable to seek to start of dynamic information\n"));
7399           return 0;
7400         }
7401
7402       gnuchains = get_dynamic_data (file, maxchain, 4);
7403
7404       if (gnuchains == NULL)
7405         return 0;
7406     }
7407
7408   if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
7409       && do_syms
7410       && do_using_dynamic
7411       && dynamic_strings != NULL)
7412     {
7413       unsigned long hn;
7414
7415       if (dynamic_info[DT_HASH])
7416         {
7417           bfd_vma si;
7418
7419           printf (_("\nSymbol table for image:\n"));
7420           if (is_32bit_elf)
7421             printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
7422           else
7423             printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
7424
7425           for (hn = 0; hn < nbuckets; hn++)
7426             {
7427               if (! buckets[hn])
7428                 continue;
7429
7430               for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
7431                 print_dynamic_symbol (si, hn);
7432             }
7433         }
7434
7435       if (dynamic_info_DT_GNU_HASH)
7436         {
7437           printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
7438           if (is_32bit_elf)
7439             printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
7440           else
7441             printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
7442
7443           for (hn = 0; hn < ngnubuckets; ++hn)
7444             if (gnubuckets[hn] != 0)
7445               {
7446                 bfd_vma si = gnubuckets[hn];
7447                 bfd_vma off = si - gnusymidx;
7448
7449                 do
7450                   {
7451                     print_dynamic_symbol (si, hn);
7452                     si++;
7453                   }
7454                 while ((gnuchains[off++] & 1) == 0);
7455               }
7456         }
7457     }
7458   else if (do_syms && !do_using_dynamic)
7459     {
7460       unsigned int i;
7461
7462       for (i = 0, section = section_headers;
7463            i < elf_header.e_shnum;
7464            i++, section++)
7465         {
7466           unsigned int si;
7467           char *strtab = NULL;
7468           unsigned long int strtab_size = 0;
7469           Elf_Internal_Sym *symtab;
7470           Elf_Internal_Sym *psym;
7471
7472
7473           if (   section->sh_type != SHT_SYMTAB
7474               && section->sh_type != SHT_DYNSYM)
7475             continue;
7476
7477           printf (_("\nSymbol table '%s' contains %lu entries:\n"),
7478                   SECTION_NAME (section),
7479                   (unsigned long) (section->sh_size / section->sh_entsize));
7480           if (is_32bit_elf)
7481             printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
7482           else
7483             printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
7484
7485           symtab = GET_ELF_SYMBOLS (file, section);
7486           if (symtab == NULL)
7487             continue;
7488
7489           if (section->sh_link == elf_header.e_shstrndx)
7490             {
7491               strtab = string_table;
7492               strtab_size = string_table_length;
7493             }
7494           else if (section->sh_link < elf_header.e_shnum)
7495             {
7496               Elf_Internal_Shdr *string_sec;
7497
7498               string_sec = section_headers + section->sh_link;
7499
7500               strtab = get_data (NULL, file, string_sec->sh_offset,
7501                                  1, string_sec->sh_size, _("string table"));
7502               strtab_size = strtab != NULL ? string_sec->sh_size : 0;
7503             }
7504
7505           for (si = 0, psym = symtab;
7506                si < section->sh_size / section->sh_entsize;
7507                si++, psym++)
7508             {
7509               printf ("%6d: ", si);
7510               print_vma (psym->st_value, LONG_HEX);
7511               putchar (' ');
7512               print_vma (psym->st_size, DEC_5);
7513               printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
7514               printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
7515               printf (" %-3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
7516               /* Check to see if any other bits in the st_other field are set.
7517                  Note - displaying this information disrupts the layout of the
7518                  table being generated, but for the moment this case is very rare.  */
7519               if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
7520                 printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
7521               printf (" %4s ", get_symbol_index_type (psym->st_shndx));
7522               print_symbol (25, psym->st_name < strtab_size
7523                             ? strtab + psym->st_name : "<corrupt>");
7524
7525               if (section->sh_type == SHT_DYNSYM &&
7526                   version_info[DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
7527                 {
7528                   unsigned char data[2];
7529                   unsigned short vers_data;
7530                   unsigned long offset;
7531                   int is_nobits;
7532                   int check_def;
7533
7534                   offset = offset_from_vma
7535                     (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
7536                      sizeof data + si * sizeof (vers_data));
7537
7538                   get_data (&data, file, offset + si * sizeof (vers_data),
7539                             sizeof (data), 1, _("version data"));
7540
7541                   vers_data = byte_get (data, 2);
7542
7543                   is_nobits = (psym->st_shndx < elf_header.e_shnum
7544                                && section_headers[psym->st_shndx].sh_type
7545                                   == SHT_NOBITS);
7546
7547                   check_def = (psym->st_shndx != SHN_UNDEF);
7548
7549                   if ((vers_data & 0x8000) || vers_data > 1)
7550                     {
7551                       if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
7552                           && (is_nobits || ! check_def))
7553                         {
7554                           Elf_External_Verneed evn;
7555                           Elf_Internal_Verneed ivn;
7556                           Elf_Internal_Vernaux ivna;
7557
7558                           /* We must test both.  */
7559                           offset = offset_from_vma
7560                             (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
7561                              sizeof evn);
7562
7563                           do
7564                             {
7565                               unsigned long vna_off;
7566
7567                               get_data (&evn, file, offset, sizeof (evn), 1,
7568                                         _("version need"));
7569
7570                               ivn.vn_aux  = BYTE_GET (evn.vn_aux);
7571                               ivn.vn_next = BYTE_GET (evn.vn_next);
7572
7573                               vna_off = offset + ivn.vn_aux;
7574
7575                               do
7576                                 {
7577                                   Elf_External_Vernaux evna;
7578
7579                                   get_data (&evna, file, vna_off,
7580                                             sizeof (evna), 1,
7581                                             _("version need aux (3)"));
7582
7583                                   ivna.vna_other = BYTE_GET (evna.vna_other);
7584                                   ivna.vna_next  = BYTE_GET (evna.vna_next);
7585                                   ivna.vna_name  = BYTE_GET (evna.vna_name);
7586
7587                                   vna_off += ivna.vna_next;
7588                                 }
7589                               while (ivna.vna_other != vers_data
7590                                      && ivna.vna_next != 0);
7591
7592                               if (ivna.vna_other == vers_data)
7593                                 break;
7594
7595                               offset += ivn.vn_next;
7596                             }
7597                           while (ivn.vn_next != 0);
7598
7599                           if (ivna.vna_other == vers_data)
7600                             {
7601                               printf ("@%s (%d)",
7602                                       ivna.vna_name < strtab_size
7603                                       ? strtab + ivna.vna_name : "<corrupt>",
7604                                       ivna.vna_other);
7605                               check_def = 0;
7606                             }
7607                           else if (! is_nobits)
7608                             error (_("bad dynamic symbol\n"));
7609                           else
7610                             check_def = 1;
7611                         }
7612
7613                       if (check_def)
7614                         {
7615                           if (vers_data != 0x8001
7616                               && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
7617                             {
7618                               Elf_Internal_Verdef ivd;
7619                               Elf_Internal_Verdaux ivda;
7620                               Elf_External_Verdaux evda;
7621                               unsigned long offset;
7622
7623                               offset = offset_from_vma
7624                                 (file,
7625                                  version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
7626                                  sizeof (Elf_External_Verdef));
7627
7628                               do
7629                                 {
7630                                   Elf_External_Verdef evd;
7631
7632                                   get_data (&evd, file, offset, sizeof (evd),
7633                                             1, _("version def"));
7634
7635                                   ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
7636                                   ivd.vd_aux = BYTE_GET (evd.vd_aux);
7637                                   ivd.vd_next = BYTE_GET (evd.vd_next);
7638
7639                                   offset += ivd.vd_next;
7640                                 }
7641                               while (ivd.vd_ndx != (vers_data & 0x7fff)
7642                                      && ivd.vd_next != 0);
7643
7644                               offset -= ivd.vd_next;
7645                               offset += ivd.vd_aux;
7646
7647                               get_data (&evda, file, offset, sizeof (evda),
7648                                         1, _("version def aux"));
7649
7650                               ivda.vda_name = BYTE_GET (evda.vda_name);
7651
7652                               if (psym->st_name != ivda.vda_name)
7653                                 printf ((vers_data & 0x8000)
7654                                         ? "@%s" : "@@%s",
7655                                         ivda.vda_name < strtab_size
7656                                         ? strtab + ivda.vda_name : "<corrupt>");
7657                             }
7658                         }
7659                     }
7660                 }
7661
7662               putchar ('\n');
7663             }
7664
7665           free (symtab);
7666           if (strtab != string_table)
7667             free (strtab);
7668         }
7669     }
7670   else if (do_syms)
7671     printf
7672       (_("\nDynamic symbol information is not available for displaying symbols.\n"));
7673
7674   if (do_histogram && buckets != NULL)
7675     {
7676       unsigned long *lengths;
7677       unsigned long *counts;
7678       unsigned long hn;
7679       bfd_vma si;
7680       unsigned long maxlength = 0;
7681       unsigned long nzero_counts = 0;
7682       unsigned long nsyms = 0;
7683
7684       printf (_("\nHistogram for bucket list length (total of %lu buckets):\n"),
7685               (unsigned long) nbuckets);
7686       printf (_(" Length  Number     %% of total  Coverage\n"));
7687
7688       lengths = calloc (nbuckets, sizeof (*lengths));
7689       if (lengths == NULL)
7690         {
7691           error (_("Out of memory\n"));
7692           return 0;
7693         }
7694       for (hn = 0; hn < nbuckets; ++hn)
7695         {
7696           for (si = buckets[hn]; si > 0 && si < nchains; si = chains[si])
7697             {
7698               ++nsyms;
7699               if (maxlength < ++lengths[hn])
7700                 ++maxlength;
7701             }
7702         }
7703
7704       counts = calloc (maxlength + 1, sizeof (*counts));
7705       if (counts == NULL)
7706         {
7707           error (_("Out of memory\n"));
7708           return 0;
7709         }
7710
7711       for (hn = 0; hn < nbuckets; ++hn)
7712         ++counts[lengths[hn]];
7713
7714       if (nbuckets > 0)
7715         {
7716           unsigned long i;
7717           printf ("      0  %-10lu (%5.1f%%)\n",
7718                   counts[0], (counts[0] * 100.0) / nbuckets);
7719           for (i = 1; i <= maxlength; ++i)
7720             {
7721               nzero_counts += counts[i] * i;
7722               printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
7723                       i, counts[i], (counts[i] * 100.0) / nbuckets,
7724                       (nzero_counts * 100.0) / nsyms);
7725             }
7726         }
7727
7728       free (counts);
7729       free (lengths);
7730     }
7731
7732   if (buckets != NULL)
7733     {
7734       free (buckets);
7735       free (chains);
7736     }
7737
7738   if (do_histogram && dynamic_info_DT_GNU_HASH)
7739     {
7740       unsigned long *lengths;
7741       unsigned long *counts;
7742       unsigned long hn;
7743       unsigned long maxlength = 0;
7744       unsigned long nzero_counts = 0;
7745       unsigned long nsyms = 0;
7746
7747       lengths = calloc (ngnubuckets, sizeof (*lengths));
7748       if (lengths == NULL)
7749         {
7750           error (_("Out of memory\n"));
7751           return 0;
7752         }
7753
7754       printf (_("\nHistogram for `.gnu.hash' bucket list length (total of %lu buckets):\n"),
7755               (unsigned long) ngnubuckets);
7756       printf (_(" Length  Number     %% of total  Coverage\n"));
7757
7758       for (hn = 0; hn < ngnubuckets; ++hn)
7759         if (gnubuckets[hn] != 0)
7760           {
7761             bfd_vma off, length = 1;
7762
7763             for (off = gnubuckets[hn] - gnusymidx;
7764                  (gnuchains[off] & 1) == 0; ++off)
7765               ++length;
7766             lengths[hn] = length;
7767             if (length > maxlength)
7768               maxlength = length;
7769             nsyms += length;
7770           }
7771
7772       counts = calloc (maxlength + 1, sizeof (*counts));
7773       if (counts == NULL)
7774         {
7775           error (_("Out of memory\n"));
7776           return 0;
7777         }
7778
7779       for (hn = 0; hn < ngnubuckets; ++hn)
7780         ++counts[lengths[hn]];
7781
7782       if (ngnubuckets > 0)
7783         {
7784           unsigned long j;
7785           printf ("      0  %-10lu (%5.1f%%)\n",
7786                   counts[0], (counts[0] * 100.0) / ngnubuckets);
7787           for (j = 1; j <= maxlength; ++j)
7788             {
7789               nzero_counts += counts[j] * j;
7790               printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
7791                       j, counts[j], (counts[j] * 100.0) / ngnubuckets,
7792                       (nzero_counts * 100.0) / nsyms);
7793             }
7794         }
7795
7796       free (counts);
7797       free (lengths);
7798       free (gnubuckets);
7799       free (gnuchains);
7800     }
7801
7802   return 1;
7803 }
7804
7805 static int
7806 process_syminfo (FILE *file ATTRIBUTE_UNUSED)
7807 {
7808   unsigned int i;
7809
7810   if (dynamic_syminfo == NULL
7811       || !do_dynamic)
7812     /* No syminfo, this is ok.  */
7813     return 1;
7814
7815   /* There better should be a dynamic symbol section.  */
7816   if (dynamic_symbols == NULL || dynamic_strings == NULL)
7817     return 0;
7818
7819   if (dynamic_addr)
7820     printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
7821             dynamic_syminfo_offset, dynamic_syminfo_nent);
7822
7823   printf (_(" Num: Name                           BoundTo     Flags\n"));
7824   for (i = 0; i < dynamic_syminfo_nent; ++i)
7825     {
7826       unsigned short int flags = dynamic_syminfo[i].si_flags;
7827
7828       printf ("%4d: ", i);
7829       if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
7830         print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
7831       else
7832         printf ("<corrupt: %19ld>", dynamic_symbols[i].st_name);
7833       putchar (' ');
7834
7835       switch (dynamic_syminfo[i].si_boundto)
7836         {
7837         case SYMINFO_BT_SELF:
7838           fputs ("SELF       ", stdout);
7839           break;
7840         case SYMINFO_BT_PARENT:
7841           fputs ("PARENT     ", stdout);
7842           break;
7843         default:
7844           if (dynamic_syminfo[i].si_boundto > 0
7845               && dynamic_syminfo[i].si_boundto < dynamic_nent
7846               && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
7847             {
7848               print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
7849               putchar (' ' );
7850             }
7851           else
7852             printf ("%-10d ", dynamic_syminfo[i].si_boundto);
7853           break;
7854         }
7855
7856       if (flags & SYMINFO_FLG_DIRECT)
7857         printf (" DIRECT");
7858       if (flags & SYMINFO_FLG_PASSTHRU)
7859         printf (" PASSTHRU");
7860       if (flags & SYMINFO_FLG_COPY)
7861         printf (" COPY");
7862       if (flags & SYMINFO_FLG_LAZYLOAD)
7863         printf (" LAZYLOAD");
7864
7865       puts ("");
7866     }
7867
7868   return 1;
7869 }
7870
7871 #ifdef SUPPORT_DISASSEMBLY
7872 static int
7873 disassemble_section (Elf_Internal_Shdr *section, FILE *file)
7874 {
7875   printf (_("\nAssembly dump of section %s\n"),
7876           SECTION_NAME (section));
7877
7878   /* XXX -- to be done --- XXX */
7879
7880   return 1;
7881 }
7882 #endif
7883
7884 static int
7885 dump_section_as_strings (Elf_Internal_Shdr *section, FILE *file)
7886 {
7887   Elf_Internal_Shdr *relsec;
7888   bfd_size_type num_bytes;
7889   bfd_vma addr;
7890   char *data;
7891   char *end;
7892   char *start;
7893   char *name = SECTION_NAME (section);
7894   bfd_boolean some_strings_shown;
7895
7896   num_bytes = section->sh_size;
7897
7898   if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
7899     {
7900       printf (_("\nSection '%s' has no data to dump.\n"), name);
7901       return 0;
7902     }
7903
7904   addr = section->sh_addr;
7905
7906   start = get_data (NULL, file, section->sh_offset, 1, num_bytes,
7907                     _("section data"));
7908   if (!start)
7909     return 0;
7910
7911   printf (_("\nString dump of section '%s':\n"), name);
7912
7913   /* If the section being dumped has relocations against it the user might
7914      be expecting these relocations to have been applied.  Check for this
7915      case and issue a warning message in order to avoid confusion.
7916      FIXME: Maybe we ought to have an option that dumps a section with
7917      relocs applied ?  */
7918   for (relsec = section_headers;
7919        relsec < section_headers + elf_header.e_shnum;
7920        ++relsec)
7921     {
7922       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
7923           || relsec->sh_info >= elf_header.e_shnum
7924           || section_headers + relsec->sh_info != section
7925           || relsec->sh_size == 0
7926           || relsec->sh_link >= elf_header.e_shnum)
7927         continue;
7928
7929       printf (_("  Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
7930       break;
7931     }
7932
7933   data = start;
7934   end  = start + num_bytes;
7935   some_strings_shown = FALSE;
7936
7937   while (data < end)
7938     {
7939       while (!ISPRINT (* data))
7940         if (++ data >= end)
7941           break;
7942
7943       if (data < end)
7944         {
7945 #ifndef __MSVCRT__
7946           printf ("  [%6tx]  %s\n", data - start, data);
7947 #else
7948           printf ("  [%6Ix]  %s\n", (size_t) (data - start), data);
7949 #endif
7950           data += strlen (data);
7951           some_strings_shown = TRUE;
7952         }
7953     }
7954
7955   if (! some_strings_shown)
7956     printf (_("  No strings found in this section."));
7957
7958   free (start);
7959
7960   putchar ('\n');
7961   return 1;
7962 }
7963
7964
7965 static int
7966 dump_section_as_bytes (Elf_Internal_Shdr *section, FILE *file)
7967 {
7968   Elf_Internal_Shdr *relsec;
7969   bfd_size_type bytes;
7970   bfd_vma addr;
7971   unsigned char *data;
7972   unsigned char *start;
7973
7974   bytes = section->sh_size;
7975
7976   if (bytes == 0 || section->sh_type == SHT_NOBITS)
7977     {
7978       printf (_("\nSection '%s' has no data to dump.\n"),
7979               SECTION_NAME (section));
7980       return 0;
7981     }
7982   else
7983     printf (_("\nHex dump of section '%s':\n"), SECTION_NAME (section));
7984
7985   addr = section->sh_addr;
7986
7987   start = get_data (NULL, file, section->sh_offset, 1, bytes,
7988                     _("section data"));
7989   if (!start)
7990     return 0;
7991
7992   /* If the section being dumped has relocations against it the user might
7993      be expecting these relocations to have been applied.  Check for this
7994      case and issue a warning message in order to avoid confusion.
7995      FIXME: Maybe we ought to have an option that dumps a section with
7996      relocs applied ?  */
7997   for (relsec = section_headers;
7998        relsec < section_headers + elf_header.e_shnum;
7999        ++relsec)
8000     {
8001       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
8002           || relsec->sh_info >= elf_header.e_shnum
8003           || section_headers + relsec->sh_info != section
8004           || relsec->sh_size == 0
8005           || relsec->sh_link >= elf_header.e_shnum)
8006         continue;
8007
8008       printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
8009       break;
8010     }
8011
8012   data = start;
8013
8014   while (bytes)
8015     {
8016       int j;
8017       int k;
8018       int lbytes;
8019
8020       lbytes = (bytes > 16 ? 16 : bytes);
8021
8022       printf ("  0x%8.8lx ", (unsigned long) addr);
8023
8024       for (j = 0; j < 16; j++)
8025         {
8026           if (j < lbytes)
8027             printf ("%2.2x", data[j]);
8028           else
8029             printf ("  ");
8030
8031           if ((j & 3) == 3)
8032             printf (" ");
8033         }
8034
8035       for (j = 0; j < lbytes; j++)
8036         {
8037           k = data[j];
8038           if (k >= ' ' && k < 0x7f)
8039             printf ("%c", k);
8040           else
8041             printf (".");
8042         }
8043
8044       putchar ('\n');
8045
8046       data  += lbytes;
8047       addr  += lbytes;
8048       bytes -= lbytes;
8049     }
8050
8051   free (start);
8052
8053   putchar ('\n');
8054   return 1;
8055 }
8056
8057 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
8058    DWARF debug sections.  This is a target specific test.  Note - we do not
8059    go through the whole including-target-headers-multiple-times route, (as
8060    we have already done with <elf/h8.h>) because this would become very
8061    messy and even then this function would have to contain target specific
8062    information (the names of the relocs instead of their numeric values).
8063    FIXME: This is not the correct way to solve this problem.  The proper way
8064    is to have target specific reloc sizing and typing functions created by
8065    the reloc-macros.h header, in the same way that it already creates the
8066    reloc naming functions.  */
8067
8068 static bfd_boolean
8069 is_32bit_abs_reloc (unsigned int reloc_type)
8070 {
8071   switch (elf_header.e_machine)
8072     {
8073     case EM_386:
8074     case EM_486:
8075       return reloc_type == 1; /* R_386_32.  */
8076     case EM_68K:
8077       return reloc_type == 1; /* R_68K_32.  */
8078     case EM_860:
8079       return reloc_type == 1; /* R_860_32.  */
8080     case EM_ALPHA:
8081       return reloc_type == 1; /* XXX Is this right ?  */
8082     case EM_ARC:
8083       return reloc_type == 1; /* R_ARC_32.  */
8084     case EM_ARM:
8085       return reloc_type == 2; /* R_ARM_ABS32 */
8086     case EM_AVR_OLD:
8087     case EM_AVR:
8088       return reloc_type == 1;
8089     case EM_BLACKFIN:
8090       return reloc_type == 0x12; /* R_byte4_data.  */
8091     case EM_CRIS:
8092       return reloc_type == 3; /* R_CRIS_32.  */
8093     case EM_CR16:
8094     case EM_CR16_OLD:
8095       return reloc_type == 3; /* R_CR16_NUM32.  */
8096     case EM_CRX:
8097       return reloc_type == 15; /* R_CRX_NUM32.  */
8098     case EM_CYGNUS_FRV:
8099       return reloc_type == 1;
8100     case EM_CYGNUS_D10V:
8101     case EM_D10V:
8102       return reloc_type == 6; /* R_D10V_32.  */
8103     case EM_CYGNUS_D30V:
8104     case EM_D30V:
8105       return reloc_type == 12; /* R_D30V_32_NORMAL.  */
8106     case EM_DLX:
8107       return reloc_type == 3; /* R_DLX_RELOC_32.  */
8108     case EM_CYGNUS_FR30:
8109     case EM_FR30:
8110       return reloc_type == 3; /* R_FR30_32.  */
8111     case EM_H8S:
8112     case EM_H8_300:
8113     case EM_H8_300H:
8114       return reloc_type == 1; /* R_H8_DIR32.  */
8115     case EM_IA_64:
8116       return reloc_type == 0x65; /* R_IA64_SECREL32LSB.  */
8117     case EM_IP2K_OLD:
8118     case EM_IP2K:
8119       return reloc_type == 2; /* R_IP2K_32.  */
8120     case EM_IQ2000:
8121       return reloc_type == 2; /* R_IQ2000_32.  */
8122     case EM_M32C_OLD:
8123     case EM_M32C:
8124       return reloc_type == 3; /* R_M32C_32.  */
8125     case EM_M32R:
8126       return reloc_type == 34; /* R_M32R_32_RELA.  */
8127     case EM_MCORE:
8128       return reloc_type == 1; /* R_MCORE_ADDR32.  */
8129     case EM_CYGNUS_MEP:
8130       return reloc_type == 4; /* R_MEP_32.  */
8131     case EM_MIPS:
8132       return reloc_type == 2; /* R_MIPS_32.  */
8133     case EM_MMIX:
8134       return reloc_type == 4; /* R_MMIX_32.  */
8135     case EM_CYGNUS_MN10200:
8136     case EM_MN10200:
8137       return reloc_type == 1; /* R_MN10200_32.  */
8138     case EM_CYGNUS_MN10300:
8139     case EM_MN10300:
8140       return reloc_type == 1; /* R_MN10300_32.  */
8141     case EM_MSP430_OLD:
8142     case EM_MSP430:
8143       return reloc_type == 1; /* R_MSP43_32.  */
8144     case EM_MT:
8145       return reloc_type == 2; /* R_MT_32.  */
8146     case EM_ALTERA_NIOS2:
8147     case EM_NIOS32:
8148       return reloc_type == 1; /* R_NIOS_32.  */
8149     case EM_OPENRISC:
8150     case EM_OR32:
8151       return reloc_type == 1; /* R_OR32_32.  */
8152     case EM_PARISC:
8153       return reloc_type == 1; /* R_PARISC_DIR32.  */
8154     case EM_PJ:
8155     case EM_PJ_OLD:
8156       return reloc_type == 1; /* R_PJ_DATA_DIR32.  */
8157     case EM_PPC64:
8158       return reloc_type == 1; /* R_PPC64_ADDR32.  */
8159     case EM_PPC:
8160       return reloc_type == 1; /* R_PPC_ADDR32.  */
8161     case EM_S370:
8162       return reloc_type == 1; /* R_I370_ADDR31.  */
8163     case EM_S390_OLD:
8164     case EM_S390:
8165       return reloc_type == 4; /* R_S390_32.  */
8166     case EM_SCORE:
8167       return reloc_type == 8; /* R_SCORE_ABS32.  */
8168     case EM_SH:
8169       return reloc_type == 1; /* R_SH_DIR32.  */
8170     case EM_SPARC32PLUS:
8171     case EM_SPARCV9:
8172     case EM_SPARC:
8173       return reloc_type == 3 /* R_SPARC_32.  */
8174         || reloc_type == 23; /* R_SPARC_UA32.  */
8175     case EM_SPU:
8176       return reloc_type == 6; /* R_SPU_ADDR32 */
8177     case EM_CYGNUS_V850:
8178     case EM_V850:
8179       return reloc_type == 6; /* R_V850_ABS32.  */
8180     case EM_VAX:
8181       return reloc_type == 1; /* R_VAX_32.  */
8182     case EM_X86_64:
8183       return reloc_type == 10; /* R_X86_64_32.  */
8184     case EM_XSTORMY16:
8185       return reloc_type == 1; /* R_XSTROMY16_32.  */
8186     case EM_XTENSA_OLD:
8187     case EM_XTENSA:
8188       return reloc_type == 1; /* R_XTENSA_32.  */
8189
8190     default:
8191       error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
8192              elf_header.e_machine);
8193       abort ();
8194     }
8195 }
8196
8197 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
8198    a 32-bit pc-relative RELA relocation used in DWARF debug sections.  */
8199
8200 static bfd_boolean
8201 is_32bit_pcrel_reloc (unsigned int reloc_type)
8202 {
8203   switch (elf_header.e_machine)
8204     {
8205     case EM_386:
8206     case EM_486:
8207       return reloc_type == 2;  /* R_386_PC32.  */
8208     case EM_68K:
8209       return reloc_type == 4;  /* R_68K_PC32.  */
8210     case EM_ALPHA:
8211       return reloc_type == 10; /* R_ALPHA_SREL32.  */
8212     case EM_ARM:
8213       return reloc_type == 3;  /* R_ARM_REL32 */
8214     case EM_PARISC:
8215       return reloc_type == 9;  /* R_PARISC_PCREL32.  */
8216     case EM_PPC:
8217       return reloc_type == 26; /* R_PPC_REL32.  */
8218     case EM_PPC64:
8219       return reloc_type == 26; /* R_PPC64_REL32.  */
8220     case EM_S390_OLD:
8221     case EM_S390:
8222       return reloc_type == 5;  /* R_390_PC32.  */
8223     case EM_SH:
8224       return reloc_type == 2;  /* R_SH_REL32.  */
8225     case EM_SPARC32PLUS:
8226     case EM_SPARCV9:
8227     case EM_SPARC:
8228       return reloc_type == 6;  /* R_SPARC_DISP32.  */
8229     case EM_SPU:
8230       return reloc_type == 13; /* R_SPU_REL32.  */
8231     case EM_X86_64:
8232       return reloc_type == 2;  /* R_X86_64_PC32.  */
8233     case EM_XTENSA_OLD:
8234     case EM_XTENSA:
8235       return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
8236     default:
8237       /* Do not abort or issue an error message here.  Not all targets use
8238          pc-relative 32-bit relocs in their DWARF debug information and we
8239          have already tested for target coverage in is_32bit_abs_reloc.  A
8240          more helpful warning message will be generated by
8241          debug_apply_relocations anyway, so just return.  */
8242       return FALSE;
8243     }
8244 }
8245
8246 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
8247    a 64-bit absolute RELA relocation used in DWARF debug sections.  */
8248
8249 static bfd_boolean
8250 is_64bit_abs_reloc (unsigned int reloc_type)
8251 {
8252   switch (elf_header.e_machine)
8253     {
8254     case EM_ALPHA:
8255       return reloc_type == 2; /* R_ALPHA_REFQUAD.  */
8256     case EM_IA_64:
8257       return reloc_type == 0x27; /* R_IA64_DIR64LSB.  */
8258     case EM_PARISC:
8259       return reloc_type == 80; /* R_PARISC_DIR64.  */
8260     case EM_PPC64:
8261       return reloc_type == 38; /* R_PPC64_ADDR64.  */
8262     case EM_SPARC32PLUS:
8263     case EM_SPARCV9:
8264     case EM_SPARC:
8265       return reloc_type == 54; /* R_SPARC_UA64.  */
8266     case EM_X86_64:
8267       return reloc_type == 1; /* R_X86_64_64.  */
8268     case EM_S390_OLD:
8269     case EM_S390:
8270       return reloc_type == 22;  /* R_S390_64 */
8271     case EM_MIPS:
8272       return reloc_type == 18;  /* R_MIPS_64 */
8273     default:
8274       return FALSE;
8275     }
8276 }
8277
8278 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
8279    a 64-bit pc-relative RELA relocation used in DWARF debug sections.  */
8280
8281 static bfd_boolean
8282 is_64bit_pcrel_reloc (unsigned int reloc_type)
8283 {
8284   switch (elf_header.e_machine)
8285     {
8286     case EM_ALPHA:
8287       return reloc_type == 11; /* R_ALPHA_SREL64 */
8288     case EM_IA_64:
8289       return reloc_type == 0x4f; /* R_IA64_PCREL64LSB */
8290     case EM_PARISC:
8291       return reloc_type == 72; /* R_PARISC_PCREL64 */
8292     case EM_PPC64:
8293       return reloc_type == 44; /* R_PPC64_REL64 */
8294     case EM_SPARC32PLUS:
8295     case EM_SPARCV9:
8296     case EM_SPARC:
8297       return reloc_type == 46; /* R_SPARC_DISP64 */
8298     case EM_X86_64:
8299       return reloc_type == 24; /* R_X86_64_PC64 */
8300     case EM_S390_OLD:
8301     case EM_S390:
8302       return reloc_type == 23;  /* R_S390_PC64 */
8303     default:
8304       return FALSE;
8305     }
8306 }
8307
8308 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
8309    a 16-bit absolute RELA relocation used in DWARF debug sections.  */
8310
8311 static bfd_boolean
8312 is_16bit_abs_reloc (unsigned int reloc_type)
8313 {
8314   switch (elf_header.e_machine)
8315     {
8316     case EM_AVR_OLD:
8317     case EM_AVR:
8318       return reloc_type == 4; /* R_AVR_16.  */
8319     case EM_CYGNUS_D10V:
8320     case EM_D10V:
8321       return reloc_type == 3; /* R_D10V_16.  */
8322     case EM_H8S:
8323     case EM_H8_300:
8324     case EM_H8_300H:
8325       return reloc_type == R_H8_DIR16;
8326     case EM_IP2K_OLD:
8327     case EM_IP2K:
8328       return reloc_type == 1; /* R_IP2K_16.  */
8329     case EM_M32C_OLD:
8330     case EM_M32C:
8331       return reloc_type == 1; /* R_M32C_16 */
8332     case EM_MSP430_OLD:
8333     case EM_MSP430:
8334       return reloc_type == 5; /* R_MSP430_16_BYTE.  */
8335     case EM_ALTERA_NIOS2:
8336     case EM_NIOS32:
8337       return reloc_type == 9; /* R_NIOS_16.  */
8338     default:
8339       return FALSE;
8340     }
8341 }
8342
8343 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
8344    relocation entries (possibly formerly used for SHT_GROUP sections).  */
8345
8346 static bfd_boolean
8347 is_none_reloc (unsigned int reloc_type)
8348 {
8349   switch (elf_header.e_machine)
8350     {
8351     case EM_68K:     /* R_68K_NONE.  */
8352     case EM_386:     /* R_386_NONE.  */
8353     case EM_SPARC32PLUS:
8354     case EM_SPARCV9:
8355     case EM_SPARC:   /* R_SPARC_NONE.  */
8356     case EM_MIPS:    /* R_MIPS_NONE.  */
8357     case EM_PARISC:  /* R_PARISC_NONE.  */
8358     case EM_ALPHA:   /* R_ALPHA_NONE.  */
8359     case EM_PPC:     /* R_PPC_NONE.  */
8360     case EM_PPC64:   /* R_PPC64_NONE.  */
8361     case EM_ARM:     /* R_ARM_NONE.  */
8362     case EM_IA_64:   /* R_IA64_NONE.  */
8363     case EM_SH:      /* R_SH_NONE.  */
8364     case EM_S390_OLD:
8365     case EM_S390:    /* R_390_NONE.  */
8366     case EM_CRIS:    /* R_CRIS_NONE.  */
8367     case EM_X86_64:  /* R_X86_64_NONE.  */
8368     case EM_MN10300: /* R_MN10300_NONE.  */
8369     case EM_M32R:    /* R_M32R_NONE.  */
8370       return reloc_type == 0;
8371     }
8372   return FALSE;
8373 }
8374
8375 /* Uncompresses a section that was compressed using zlib, in place.
8376    This is a copy of bfd_uncompress_section_contents, in bfd/compress.c  */
8377
8378 static int
8379 uncompress_section_contents (unsigned char **buffer, dwarf_size_type *size)
8380 {
8381 #ifndef HAVE_ZLIB_H
8382   /* These are just to quiet gcc.  */
8383   buffer = 0;
8384   size = 0;
8385   return FALSE;
8386 #else
8387   dwarf_size_type compressed_size = *size;
8388   unsigned char * compressed_buffer = *buffer;
8389   dwarf_size_type uncompressed_size;
8390   unsigned char * uncompressed_buffer;
8391   z_stream strm;
8392   int rc;
8393   dwarf_size_type header_size = 12;
8394
8395   /* Read the zlib header.  In this case, it should be "ZLIB" followed
8396      by the uncompressed section size, 8 bytes in big-endian order.  */
8397   if (compressed_size < header_size
8398       || ! streq ((char *) compressed_buffer, "ZLIB"))
8399     return 0;
8400
8401   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
8402   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
8403   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
8404   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
8405   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
8406   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
8407   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
8408   uncompressed_size += compressed_buffer[11];
8409
8410   /* It is possible the section consists of several compressed
8411      buffers concatenated together, so we uncompress in a loop.  */
8412   strm.zalloc = NULL;
8413   strm.zfree = NULL;
8414   strm.opaque = NULL;
8415   strm.avail_in = compressed_size - header_size;
8416   strm.next_in = (Bytef *) compressed_buffer + header_size;
8417   strm.avail_out = uncompressed_size;
8418   uncompressed_buffer = xmalloc (uncompressed_size);
8419
8420   rc = inflateInit (& strm);
8421   while (strm.avail_in > 0)
8422     {
8423       if (rc != Z_OK)
8424         goto fail;
8425       strm.next_out = ((Bytef *) uncompressed_buffer
8426                        + (uncompressed_size - strm.avail_out));
8427       rc = inflate (&strm, Z_FINISH);
8428       if (rc != Z_STREAM_END)
8429         goto fail;
8430       rc = inflateReset (& strm);
8431     }
8432   rc = inflateEnd (& strm);
8433   if (rc != Z_OK
8434       || strm.avail_out != 0)
8435     goto fail;
8436
8437   free (compressed_buffer);
8438   *buffer = uncompressed_buffer;
8439   *size = uncompressed_size;
8440   return 1;
8441
8442  fail:
8443   free (uncompressed_buffer);
8444   return 0;
8445 #endif  /* HAVE_ZLIB_H */
8446 }
8447
8448 /* Apply relocations to a debug section.  */
8449
8450 static void
8451 debug_apply_relocations (void *file,
8452                          Elf_Internal_Shdr *section,
8453                          unsigned char *start)
8454 {
8455   Elf_Internal_Shdr *relsec;
8456   unsigned char *end = start + section->sh_size;
8457
8458   if (elf_header.e_type != ET_REL)
8459     return;
8460
8461   /* Find the reloc section associated with the debug section.  */
8462   for (relsec = section_headers;
8463        relsec < section_headers + elf_header.e_shnum;
8464        ++relsec)
8465     {
8466       bfd_boolean is_rela;
8467       unsigned long num_relocs;
8468       Elf_Internal_Rela *relocs, *rp;
8469       Elf_Internal_Shdr *symsec;
8470       Elf_Internal_Sym *symtab;
8471       Elf_Internal_Sym *sym;
8472
8473       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
8474           || relsec->sh_info >= elf_header.e_shnum
8475           || section_headers + relsec->sh_info != section
8476           || relsec->sh_size == 0
8477           || relsec->sh_link >= elf_header.e_shnum)
8478         continue;
8479
8480       is_rela = relsec->sh_type == SHT_RELA;
8481
8482       if (is_rela)
8483         {
8484           if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
8485                                   & relocs, & num_relocs))
8486             return;
8487         }
8488       else
8489         {
8490           if (!slurp_rel_relocs (file, relsec->sh_offset, relsec->sh_size,
8491                                  & relocs, & num_relocs))
8492             return;
8493         }
8494
8495       /* SH uses RELA but uses in place value instead of the addend field.  */
8496       if (elf_header.e_machine == EM_SH)
8497         is_rela = FALSE;
8498
8499       symsec = section_headers + relsec->sh_link;
8500       symtab = GET_ELF_SYMBOLS (file, symsec);
8501
8502       for (rp = relocs; rp < relocs + num_relocs; ++rp)
8503         {
8504           bfd_vma         addend;
8505           unsigned int    reloc_type;
8506           unsigned int    reloc_size;
8507           unsigned char * loc;
8508
8509           reloc_type = get_reloc_type (rp->r_info);
8510
8511           if (is_none_reloc (reloc_type))
8512             continue;
8513
8514           if (is_32bit_abs_reloc (reloc_type)
8515               || is_32bit_pcrel_reloc (reloc_type))
8516             reloc_size = 4;
8517           else if (is_64bit_abs_reloc (reloc_type)
8518                    || is_64bit_pcrel_reloc (reloc_type))
8519             reloc_size = 8;
8520           else if (is_16bit_abs_reloc (reloc_type))
8521             reloc_size = 2;
8522           else
8523             {
8524               warn (_("unable to apply unsupported reloc type %d to section %s\n"),
8525                     reloc_type, SECTION_NAME (section));
8526               continue;
8527             }
8528
8529           loc = start + rp->r_offset;
8530           if ((loc + reloc_size) > end)
8531             {
8532               warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
8533                     (unsigned long) rp->r_offset,
8534                     SECTION_NAME (section));
8535               continue;
8536             }
8537
8538           sym = symtab + get_reloc_symindex (rp->r_info);
8539
8540           /* If the reloc has a symbol associated with it,
8541              make sure that it is of an appropriate type.  */
8542           if (sym != symtab
8543               && ELF_ST_TYPE (sym->st_info) != STT_SECTION
8544               /* Relocations against symbols without type can happen.
8545                  Gcc -feliminate-dwarf2-dups may generate symbols
8546                  without type for debug info.  */
8547               && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
8548               /* Relocations against object symbols can happen,
8549                  eg when referencing a global array.  For an
8550                  example of this see the _clz.o binary in libgcc.a.  */
8551               && ELF_ST_TYPE (sym->st_info) != STT_OBJECT)
8552             {
8553               warn (_("skipping unexpected symbol type %s in %ld'th relocation in section %s\n"),
8554                     get_symbol_type (ELF_ST_TYPE (sym->st_info)),
8555                     (long int)(rp - relocs),
8556                     SECTION_NAME (relsec));
8557               continue;
8558             }
8559
8560           addend = is_rela ? rp->r_addend : byte_get (loc, reloc_size);
8561
8562           if (is_32bit_pcrel_reloc (reloc_type)
8563               || is_64bit_pcrel_reloc (reloc_type))
8564             {
8565               /* On HPPA, all pc-relative relocations are biased by 8.  */
8566               if (elf_header.e_machine == EM_PARISC)
8567                 addend -= 8;
8568               byte_put (loc, (addend + sym->st_value) - rp->r_offset,
8569                         reloc_size);
8570             }
8571           else
8572             byte_put (loc, addend + sym->st_value, reloc_size);
8573         }
8574
8575       free (symtab);
8576       free (relocs);
8577       break;
8578     }
8579 }
8580
8581 static int
8582 load_specific_debug_section (enum dwarf_section_display_enum debug,
8583                              Elf_Internal_Shdr *sec, void *file)
8584 {
8585   struct dwarf_section *section = &debug_displays [debug].section;
8586   char buf [64];
8587   int section_is_compressed;
8588
8589   /* If it is already loaded, do nothing.  */
8590   if (section->start != NULL)
8591     return 1;
8592
8593   section_is_compressed = section->name == section->compressed_name;
8594
8595   snprintf (buf, sizeof (buf), _("%s section data"), section->name);
8596   section->address = sec->sh_addr;
8597   section->size = sec->sh_size;
8598   section->start = get_data (NULL, file, sec->sh_offset, 1,
8599                              sec->sh_size, buf);
8600   if (section->start == NULL)
8601     return 0;
8602
8603   if (section_is_compressed)
8604     if (! uncompress_section_contents (&section->start, &section->size))
8605       return 0;
8606
8607   if (debug_displays [debug].relocate)
8608     debug_apply_relocations (file, sec, section->start);
8609
8610   return 1;
8611 }
8612
8613 int
8614 load_debug_section (enum dwarf_section_display_enum debug, void *file)
8615 {
8616   struct dwarf_section *section = &debug_displays [debug].section;
8617   Elf_Internal_Shdr *sec;
8618
8619   /* Locate the debug section.  */
8620   sec = find_section (section->uncompressed_name);
8621   if (sec != NULL)
8622     section->name = section->uncompressed_name;
8623   else
8624     {
8625       sec = find_section (section->compressed_name);
8626       if (sec != NULL)
8627         section->name = section->compressed_name;
8628     }
8629   if (sec == NULL)
8630     return 0;
8631
8632   return load_specific_debug_section (debug, sec, file);
8633 }
8634
8635 void
8636 free_debug_section (enum dwarf_section_display_enum debug)
8637 {
8638   struct dwarf_section *section = &debug_displays [debug].section;
8639
8640   if (section->start == NULL)
8641     return;
8642
8643   free ((char *) section->start);
8644   section->start = NULL;
8645   section->address = 0;
8646   section->size = 0;
8647 }
8648
8649 static int
8650 display_debug_section (Elf_Internal_Shdr *section, FILE *file)
8651 {
8652   char *name = SECTION_NAME (section);
8653   bfd_size_type length;
8654   int result = 1;
8655   enum dwarf_section_display_enum i;
8656
8657   length = section->sh_size;
8658   if (length == 0)
8659     {
8660       printf (_("\nSection '%s' has no debugging data.\n"), name);
8661       return 0;
8662     }
8663
8664   if (const_strneq (name, ".gnu.linkonce.wi."))
8665     name = ".debug_info";
8666
8667   /* See if we know how to display the contents of this section.  */
8668   for (i = 0; i < max; i++)
8669     if (streq (debug_displays[i].section.uncompressed_name, name)
8670         || streq (debug_displays[i].section.compressed_name, name))
8671       {
8672         struct dwarf_section *sec = &debug_displays [i].section;
8673         int secondary = (section != find_section (name));
8674
8675         if (secondary)
8676           free_debug_section (i);
8677
8678         if (streq (debug_displays[i].section.uncompressed_name, name))
8679           sec->name = sec->uncompressed_name;
8680         else
8681           sec->name = sec->compressed_name;
8682         if (load_specific_debug_section (i, section, file))
8683           {
8684             result &= debug_displays[i].display (sec, file);
8685
8686             if (secondary || (i != info && i != abbrev))
8687               free_debug_section (i);
8688           }
8689
8690         break;
8691       }
8692
8693   if (i == max)
8694     {
8695       printf (_("Unrecognized debug section: %s\n"), name);
8696       result = 0;
8697     }
8698
8699   return result;
8700 }
8701
8702 /* Set DUMP_SECTS for all sections where dumps were requested
8703    based on section name.  */
8704
8705 static void
8706 initialise_dumps_byname (void)
8707 {
8708   struct dump_list_entry *cur;
8709
8710   for (cur = dump_sects_byname; cur; cur = cur->next)
8711     {
8712       unsigned int i;
8713       int any;
8714
8715       for (i = 0, any = 0; i < elf_header.e_shnum; i++)
8716         if (streq (SECTION_NAME (section_headers + i), cur->name))
8717           {
8718             request_dump_bynumber (i, cur->type);
8719             any = 1;
8720           }
8721
8722       if (!any)
8723         warn (_("Section '%s' was not dumped because it does not exist!\n"),
8724               cur->name);
8725     }
8726 }
8727
8728 static void
8729 process_section_contents (FILE *file)
8730 {
8731   Elf_Internal_Shdr *section;
8732   unsigned int i;
8733
8734   if (! do_dump)
8735     return;
8736
8737   initialise_dumps_byname ();
8738
8739   for (i = 0, section = section_headers;
8740        i < elf_header.e_shnum && i < num_dump_sects;
8741        i++, section++)
8742     {
8743 #ifdef SUPPORT_DISASSEMBLY
8744       if (dump_sects[i] & DISASS_DUMP)
8745         disassemble_section (section, file);
8746 #endif
8747       if (dump_sects[i] & HEX_DUMP)
8748         dump_section_as_bytes (section, file);
8749
8750       if (dump_sects[i] & DEBUG_DUMP)
8751         display_debug_section (section, file);
8752
8753       if (dump_sects[i] & STRING_DUMP)
8754         dump_section_as_strings (section, file);
8755     }
8756
8757   /* Check to see if the user requested a
8758      dump of a section that does not exist.  */
8759   while (i++ < num_dump_sects)
8760     if (dump_sects[i])
8761       warn (_("Section %d was not dumped because it does not exist!\n"), i);
8762 }
8763
8764 static void
8765 process_mips_fpe_exception (int mask)
8766 {
8767   if (mask)
8768     {
8769       int first = 1;
8770       if (mask & OEX_FPU_INEX)
8771         fputs ("INEX", stdout), first = 0;
8772       if (mask & OEX_FPU_UFLO)
8773         printf ("%sUFLO", first ? "" : "|"), first = 0;
8774       if (mask & OEX_FPU_OFLO)
8775         printf ("%sOFLO", first ? "" : "|"), first = 0;
8776       if (mask & OEX_FPU_DIV0)
8777         printf ("%sDIV0", first ? "" : "|"), first = 0;
8778       if (mask & OEX_FPU_INVAL)
8779         printf ("%sINVAL", first ? "" : "|");
8780     }
8781   else
8782     fputs ("0", stdout);
8783 }
8784
8785 /* ARM EABI attributes section.  */
8786 typedef struct
8787 {
8788   int tag;
8789   const char *name;
8790   /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup.  */
8791   int type;
8792   const char **table;
8793 } arm_attr_public_tag;
8794
8795 static const char *arm_attr_tag_CPU_arch[] =
8796   {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
8797    "v6K", "v7", "v6-M", "v6S-M"};
8798 static const char *arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
8799 static const char *arm_attr_tag_THUMB_ISA_use[] =
8800   {"No", "Thumb-1", "Thumb-2"};
8801 static const char *arm_attr_tag_VFP_arch[] =
8802   {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16"};
8803 static const char *arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
8804 static const char *arm_attr_tag_Advanced_SIMD_arch[] = {"No", "NEONv1"};
8805 static const char *arm_attr_tag_PCS_config[] =
8806   {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
8807    "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
8808 static const char *arm_attr_tag_ABI_PCS_R9_use[] =
8809   {"V6", "SB", "TLS", "Unused"};
8810 static const char *arm_attr_tag_ABI_PCS_RW_data[] =
8811   {"Absolute", "PC-relative", "SB-relative", "None"};
8812 static const char *arm_attr_tag_ABI_PCS_RO_data[] =
8813   {"Absolute", "PC-relative", "None"};
8814 static const char *arm_attr_tag_ABI_PCS_GOT_use[] =
8815   {"None", "direct", "GOT-indirect"};
8816 static const char *arm_attr_tag_ABI_PCS_wchar_t[] =
8817   {"None", "??? 1", "2", "??? 3", "4"};
8818 static const char *arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
8819 static const char *arm_attr_tag_ABI_FP_denormal[] =
8820   {"Unused", "Needed", "Sign only"};
8821 static const char *arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
8822 static const char *arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
8823 static const char *arm_attr_tag_ABI_FP_number_model[] =
8824   {"Unused", "Finite", "RTABI", "IEEE 754"};
8825 static const char *arm_attr_tag_ABI_align8_needed[] = {"No", "Yes", "4-byte"};
8826 static const char *arm_attr_tag_ABI_align8_preserved[] =
8827   {"No", "Yes, except leaf SP", "Yes"};
8828 static const char *arm_attr_tag_ABI_enum_size[] =
8829   {"Unused", "small", "int", "forced to int"};
8830 static const char *arm_attr_tag_ABI_HardFP_use[] =
8831   {"As Tag_VFP_arch", "SP only", "DP only", "SP and DP"};
8832 static const char *arm_attr_tag_ABI_VFP_args[] =
8833   {"AAPCS", "VFP registers", "custom"};
8834 static const char *arm_attr_tag_ABI_WMMX_args[] =
8835   {"AAPCS", "WMMX registers", "custom"};
8836 static const char *arm_attr_tag_ABI_optimization_goals[] =
8837   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
8838     "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
8839 static const char *arm_attr_tag_ABI_FP_optimization_goals[] =
8840   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
8841     "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
8842 static const char *arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
8843 static const char *arm_attr_tag_VFP_HP_extension[] =
8844   {"Not Allowed", "Allowed"};
8845 static const char *arm_attr_tag_ABI_FP_16bit_format[] =
8846   {"None", "IEEE 754", "Alternative Format"};
8847 static const char *arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
8848 static const char *arm_attr_tag_Virtualization_use[] =
8849   {"Not Allowed", "Allowed"};
8850 static const char *arm_attr_tag_MPextension_use[] = {"Not Allowed", "Allowed"};
8851
8852 #define LOOKUP(id, name) \
8853   {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
8854 static arm_attr_public_tag arm_attr_public_tags[] =
8855 {
8856   {4, "CPU_raw_name", 1, NULL},
8857   {5, "CPU_name", 1, NULL},
8858   LOOKUP(6, CPU_arch),
8859   {7, "CPU_arch_profile", 0, NULL},
8860   LOOKUP(8, ARM_ISA_use),
8861   LOOKUP(9, THUMB_ISA_use),
8862   LOOKUP(10, VFP_arch),
8863   LOOKUP(11, WMMX_arch),
8864   LOOKUP(12, Advanced_SIMD_arch),
8865   LOOKUP(13, PCS_config),
8866   LOOKUP(14, ABI_PCS_R9_use),
8867   LOOKUP(15, ABI_PCS_RW_data),
8868   LOOKUP(16, ABI_PCS_RO_data),
8869   LOOKUP(17, ABI_PCS_GOT_use),
8870   LOOKUP(18, ABI_PCS_wchar_t),
8871   LOOKUP(19, ABI_FP_rounding),
8872   LOOKUP(20, ABI_FP_denormal),
8873   LOOKUP(21, ABI_FP_exceptions),
8874   LOOKUP(22, ABI_FP_user_exceptions),
8875   LOOKUP(23, ABI_FP_number_model),
8876   LOOKUP(24, ABI_align8_needed),
8877   LOOKUP(25, ABI_align8_preserved),
8878   LOOKUP(26, ABI_enum_size),
8879   LOOKUP(27, ABI_HardFP_use),
8880   LOOKUP(28, ABI_VFP_args),
8881   LOOKUP(29, ABI_WMMX_args),
8882   LOOKUP(30, ABI_optimization_goals),
8883   LOOKUP(31, ABI_FP_optimization_goals),
8884   {32, "compatibility", 0, NULL},
8885   LOOKUP(34, CPU_unaligned_access),
8886   LOOKUP(36, VFP_HP_extension),
8887   LOOKUP(38, ABI_FP_16bit_format),
8888   {64, "nodefaults", 0, NULL},
8889   {65, "also_compatible_with", 0, NULL},
8890   LOOKUP(66, T2EE_use),
8891   {67, "conformance", 1, NULL},
8892   LOOKUP(68, Virtualization_use),
8893   LOOKUP(70, MPextension_use)
8894 };
8895 #undef LOOKUP
8896
8897 /* Read an unsigned LEB128 encoded value from p.  Set *PLEN to the number of
8898    bytes read.  */
8899 static unsigned int
8900 read_uleb128 (unsigned char *p, unsigned int *plen)
8901 {
8902   unsigned char c;
8903   unsigned int val;
8904   int shift;
8905   int len;
8906
8907   val = 0;
8908   shift = 0;
8909   len = 0;
8910   do
8911     {
8912       c = *(p++);
8913       len++;
8914       val |= ((unsigned int)c & 0x7f) << shift;
8915       shift += 7;
8916     }
8917   while (c & 0x80);
8918
8919   *plen = len;
8920   return val;
8921 }
8922
8923 static unsigned char *
8924 display_arm_attribute (unsigned char *p)
8925 {
8926   int tag;
8927   unsigned int len;
8928   int val;
8929   arm_attr_public_tag *attr;
8930   unsigned i;
8931   int type;
8932
8933   tag = read_uleb128 (p, &len);
8934   p += len;
8935   attr = NULL;
8936   for (i = 0; i < ARRAY_SIZE(arm_attr_public_tags); i++)
8937     {
8938       if (arm_attr_public_tags[i].tag == tag)
8939         {
8940           attr = &arm_attr_public_tags[i];
8941           break;
8942         }
8943     }
8944
8945   if (attr)
8946     {
8947       printf ("  Tag_%s: ", attr->name);
8948       switch (attr->type)
8949         {
8950         case 0:
8951           switch (tag)
8952             {
8953             case 7: /* Tag_CPU_arch_profile.  */
8954               val = read_uleb128 (p, &len);
8955               p += len;
8956               switch (val)
8957                 {
8958                 case 0: printf ("None\n"); break;
8959                 case 'A': printf ("Application\n"); break;
8960                 case 'R': printf ("Realtime\n"); break;
8961                 case 'M': printf ("Microcontroller\n"); break;
8962                 default: printf ("??? (%d)\n", val); break;
8963                 }
8964               break;
8965
8966             case 32: /* Tag_compatibility.  */
8967               val = read_uleb128 (p, &len);
8968               p += len;
8969               printf ("flag = %d, vendor = %s\n", val, p);
8970               p += strlen((char *)p) + 1;
8971               break;
8972
8973             case 64: /* Tag_nodefaults.  */
8974               p++;
8975               printf ("True\n");
8976               break;
8977
8978             case 65: /* Tag_also_compatible_with.  */
8979               val = read_uleb128 (p, &len);
8980               p += len;
8981               if (val == 6 /* Tag_CPU_arch.  */)
8982                 {
8983                   val = read_uleb128 (p, &len);
8984                   p += len;
8985                   if ((unsigned int)val >= ARRAY_SIZE(arm_attr_tag_CPU_arch))
8986                     printf ("??? (%d)\n", val);
8987                   else
8988                     printf ("%s\n", arm_attr_tag_CPU_arch[val]);
8989                 }
8990               else
8991                 printf ("???\n");
8992               while (*(p++) != '\0' /* NUL terminator.  */);
8993               break;
8994
8995             default:
8996               abort();
8997             }
8998           return p;
8999
9000         case 1:
9001         case 2:
9002           type = attr->type;
9003           break;
9004
9005         default:
9006           assert (attr->type & 0x80);
9007           val = read_uleb128 (p, &len);
9008           p += len;
9009           type = attr->type & 0x7f;
9010           if (val >= type)
9011             printf ("??? (%d)\n", val);
9012           else
9013             printf ("%s\n", attr->table[val]);
9014           return p;
9015         }
9016     }
9017   else
9018     {
9019       if (tag & 1)
9020         type = 1; /* String.  */
9021       else
9022         type = 2; /* uleb128.  */
9023       printf ("  Tag_unknown_%d: ", tag);
9024     }
9025
9026   if (type == 1)
9027     {
9028       printf ("\"%s\"\n", p);
9029       p += strlen((char *)p) + 1;
9030     }
9031   else
9032     {
9033       val = read_uleb128 (p, &len);
9034       p += len;
9035       printf ("%d (0x%x)\n", val, val);
9036     }
9037
9038   return p;
9039 }
9040
9041 static unsigned char *
9042 display_gnu_attribute (unsigned char * p,
9043                        unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
9044 {
9045   int tag;
9046   unsigned int len;
9047   int val;
9048   int type;
9049
9050   tag = read_uleb128 (p, &len);
9051   p += len;
9052
9053   /* Tag_compatibility is the only generic GNU attribute defined at
9054      present.  */
9055   if (tag == 32)
9056     {
9057       val = read_uleb128 (p, &len);
9058       p += len;
9059       printf ("flag = %d, vendor = %s\n", val, p);
9060       p += strlen ((char *) p) + 1;
9061       return p;
9062     }
9063
9064   if ((tag & 2) == 0 && display_proc_gnu_attribute)
9065     return display_proc_gnu_attribute (p, tag);
9066
9067   if (tag & 1)
9068     type = 1; /* String.  */
9069   else
9070     type = 2; /* uleb128.  */
9071   printf ("  Tag_unknown_%d: ", tag);
9072
9073   if (type == 1)
9074     {
9075       printf ("\"%s\"\n", p);
9076       p += strlen ((char *) p) + 1;
9077     }
9078   else
9079     {
9080       val = read_uleb128 (p, &len);
9081       p += len;
9082       printf ("%d (0x%x)\n", val, val);
9083     }
9084
9085   return p;
9086 }
9087
9088 static unsigned char *
9089 display_power_gnu_attribute (unsigned char *p, int tag)
9090 {
9091   int type;
9092   unsigned int len;
9093   int val;
9094
9095   if (tag == Tag_GNU_Power_ABI_FP)
9096     {
9097       val = read_uleb128 (p, &len);
9098       p += len;
9099       printf ("  Tag_GNU_Power_ABI_FP: ");
9100
9101       switch (val)
9102         {
9103         case 0:
9104           printf ("Hard or soft float\n");
9105           break;
9106         case 1:
9107           printf ("Hard float\n");
9108           break;
9109         case 2:
9110           printf ("Soft float\n");
9111           break;
9112         case 3:
9113           printf ("Single-precision hard float\n");
9114           break;
9115         default:
9116           printf ("??? (%d)\n", val);
9117           break;
9118         }
9119       return p;
9120    }
9121
9122   if (tag == Tag_GNU_Power_ABI_Vector)
9123     {
9124       val = read_uleb128 (p, &len);
9125       p += len;
9126       printf ("  Tag_GNU_Power_ABI_Vector: ");
9127       switch (val)
9128         {
9129         case 0:
9130           printf ("Any\n");
9131           break;
9132         case 1:
9133           printf ("Generic\n");
9134           break;
9135         case 2:
9136           printf ("AltiVec\n");
9137           break;
9138         case 3:
9139           printf ("SPE\n");
9140           break;
9141         default:
9142           printf ("??? (%d)\n", val);
9143           break;
9144         }
9145       return p;
9146    }
9147
9148   if (tag == Tag_GNU_Power_ABI_Struct_Return)
9149     {
9150       val = read_uleb128 (p, &len);
9151       p += len;
9152       printf ("  Tag_GNU_Power_ABI_Struct_Return: ");
9153       switch (val)
9154        {
9155        case 0:
9156          printf ("Any\n");
9157          break;
9158        case 1:
9159          printf ("r3/r4\n");
9160          break;
9161        case 2:
9162          printf ("Memory\n");
9163          break;
9164        default:
9165          printf ("??? (%d)\n", val);
9166          break;
9167        }
9168       return p;
9169     }
9170
9171   if (tag & 1)
9172     type = 1; /* String.  */
9173   else
9174     type = 2; /* uleb128.  */
9175   printf ("  Tag_unknown_%d: ", tag);
9176
9177   if (type == 1)
9178     {
9179       printf ("\"%s\"\n", p);
9180       p += strlen ((char *) p) + 1;
9181     }
9182   else
9183     {
9184       val = read_uleb128 (p, &len);
9185       p += len;
9186       printf ("%d (0x%x)\n", val, val);
9187     }
9188
9189   return p;
9190 }
9191
9192 static unsigned char *
9193 display_mips_gnu_attribute (unsigned char *p, int tag)
9194 {
9195   int type;
9196   unsigned int len;
9197   int val;
9198
9199   if (tag == Tag_GNU_MIPS_ABI_FP)
9200     {
9201       val = read_uleb128 (p, &len);
9202       p += len;
9203       printf ("  Tag_GNU_MIPS_ABI_FP: ");
9204
9205       switch (val)
9206         {
9207         case 0:
9208           printf ("Hard or soft float\n");
9209           break;
9210         case 1:
9211           printf ("Hard float (-mdouble-float)\n");
9212           break;
9213         case 2:
9214           printf ("Hard float (-msingle-float)\n");
9215           break;
9216         case 3:
9217           printf ("Soft float\n");
9218           break;
9219         case 4:
9220           printf ("64-bit float (-mips32r2 -mfp64)\n");
9221           break;
9222         default:
9223           printf ("??? (%d)\n", val);
9224           break;
9225         }
9226       return p;
9227    }
9228
9229   if (tag & 1)
9230     type = 1; /* String.  */
9231   else
9232     type = 2; /* uleb128.  */
9233   printf ("  Tag_unknown_%d: ", tag);
9234
9235   if (type == 1)
9236     {
9237       printf ("\"%s\"\n", p);
9238       p += strlen ((char *) p) + 1;
9239     }
9240   else
9241     {
9242       val = read_uleb128 (p, &len);
9243       p += len;
9244       printf ("%d (0x%x)\n", val, val);
9245     }
9246
9247   return p;
9248 }
9249
9250 static int
9251 process_attributes (FILE * file,
9252                     const char * public_name,
9253                     unsigned int proc_type,
9254                     unsigned char * (* display_pub_attribute) (unsigned char *),
9255                     unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
9256 {
9257   Elf_Internal_Shdr *sect;
9258   unsigned char *contents;
9259   unsigned char *p;
9260   unsigned char *end;
9261   bfd_vma section_len;
9262   bfd_vma len;
9263   unsigned i;
9264
9265   /* Find the section header so that we get the size.  */
9266   for (i = 0, sect = section_headers;
9267        i < elf_header.e_shnum;
9268        i++, sect++)
9269     {
9270       if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
9271         continue;
9272
9273       contents = get_data (NULL, file, sect->sh_offset, 1, sect->sh_size,
9274                            _("attributes"));
9275       if (contents == NULL)
9276         continue;
9277
9278       p = contents;
9279       if (*p == 'A')
9280         {
9281           len = sect->sh_size - 1;
9282           p++;
9283
9284           while (len > 0)
9285             {
9286               int namelen;
9287               bfd_boolean public_section;
9288               bfd_boolean gnu_section;
9289
9290               section_len = byte_get (p, 4);
9291               p += 4;
9292
9293               if (section_len > len)
9294                 {
9295                   printf (_("ERROR: Bad section length (%d > %d)\n"),
9296                           (int) section_len, (int) len);
9297                   section_len = len;
9298                 }
9299
9300               len -= section_len;
9301               printf ("Attribute Section: %s\n", p);
9302
9303               if (public_name && streq ((char *) p, public_name))
9304                 public_section = TRUE;
9305               else
9306                 public_section = FALSE;
9307
9308               if (streq ((char *) p, "gnu"))
9309                 gnu_section = TRUE;
9310               else
9311                 gnu_section = FALSE;
9312
9313               namelen = strlen ((char *) p) + 1;
9314               p += namelen;
9315               section_len -= namelen + 4;
9316
9317               while (section_len > 0)
9318                 {
9319                   int tag = *(p++);
9320                   int val;
9321                   bfd_vma size;
9322
9323                   size = byte_get (p, 4);
9324                   if (size > section_len)
9325                     {
9326                       printf (_("ERROR: Bad subsection length (%d > %d)\n"),
9327                               (int) size, (int) section_len);
9328                       size = section_len;
9329                     }
9330
9331                   section_len -= size;
9332                   end = p + size - 1;
9333                   p += 4;
9334
9335                   switch (tag)
9336                     {
9337                     case 1:
9338                       printf ("File Attributes\n");
9339                       break;
9340                     case 2:
9341                       printf ("Section Attributes:");
9342                       goto do_numlist;
9343                     case 3:
9344                       printf ("Symbol Attributes:");
9345                     do_numlist:
9346                       for (;;)
9347                         {
9348                           unsigned int i;
9349
9350                           val = read_uleb128 (p, &i);
9351                           p += i;
9352                           if (val == 0)
9353                             break;
9354                           printf (" %d", val);
9355                         }
9356                       printf ("\n");
9357                       break;
9358                     default:
9359                       printf ("Unknown tag: %d\n", tag);
9360                       public_section = FALSE;
9361                       break;
9362                     }
9363
9364                   if (public_section)
9365                     {
9366                       while (p < end)
9367                         p = display_pub_attribute (p);
9368                     }
9369                   else if (gnu_section)
9370                     {
9371                       while (p < end)
9372                         p = display_gnu_attribute (p,
9373                                                    display_proc_gnu_attribute);
9374                     }
9375                   else
9376                     {
9377                       /* ??? Do something sensible, like dump hex.  */
9378                       printf ("  Unknown section contexts\n");
9379                       p = end;
9380                     }
9381                 }
9382             }
9383         }
9384       else
9385         printf (_("Unknown format '%c'\n"), *p);
9386
9387       free (contents);
9388     }
9389   return 1;
9390 }
9391
9392 static int
9393 process_arm_specific (FILE *file)
9394 {
9395   return process_attributes (file, "aeabi", SHT_ARM_ATTRIBUTES,
9396                              display_arm_attribute, NULL);
9397 }
9398
9399 static int
9400 process_power_specific (FILE *file)
9401 {
9402   return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
9403                              display_power_gnu_attribute);
9404 }
9405
9406 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
9407    Print the Address, Access and Initial fields of an entry at VMA ADDR
9408    and return the VMA of the next entry.  */
9409
9410 static bfd_vma
9411 print_mips_got_entry (unsigned char *data, bfd_vma pltgot, bfd_vma addr)
9412 {
9413   printf ("  ");
9414   print_vma (addr, LONG_HEX);
9415   printf (" ");
9416   if (addr < pltgot + 0xfff0)
9417     printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
9418   else
9419     printf ("%10s", "");
9420   printf (" ");
9421   if (data == NULL)
9422     printf ("%*s", is_32bit_elf ? 8 : 16, "<unknown>");
9423   else
9424     {
9425       bfd_vma entry;
9426
9427       entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
9428       print_vma (entry, LONG_HEX);
9429     }
9430   return addr + (is_32bit_elf ? 4 : 8);
9431 }
9432
9433 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
9434    PLTGOT.  Print the Address and Initial fields of an entry at VMA
9435    ADDR and return the VMA of the next entry.  */
9436
9437 static bfd_vma
9438 print_mips_pltgot_entry (unsigned char *data, bfd_vma pltgot, bfd_vma addr)
9439 {
9440   printf ("  ");
9441   print_vma (addr, LONG_HEX);
9442   printf (" ");
9443   if (data == NULL)
9444     printf ("%*s", is_32bit_elf ? 8 : 16, "<unknown>");
9445   else
9446     {
9447       bfd_vma entry;
9448
9449       entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
9450       print_vma (entry, LONG_HEX);
9451     }
9452   return addr + (is_32bit_elf ? 4 : 8);
9453 }
9454
9455 static int
9456 process_mips_specific (FILE *file)
9457 {
9458   Elf_Internal_Dyn *entry;
9459   size_t liblist_offset = 0;
9460   size_t liblistno = 0;
9461   size_t conflictsno = 0;
9462   size_t options_offset = 0;
9463   size_t conflicts_offset = 0;
9464   size_t pltrelsz = 0;
9465   size_t pltrel = 0;
9466   bfd_vma pltgot = 0;
9467   bfd_vma mips_pltgot = 0;
9468   bfd_vma jmprel = 0;
9469   bfd_vma local_gotno = 0;
9470   bfd_vma gotsym = 0;
9471   bfd_vma symtabno = 0;
9472
9473   process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
9474                       display_mips_gnu_attribute);
9475
9476   /* We have a lot of special sections.  Thanks SGI!  */
9477   if (dynamic_section == NULL)
9478     /* No information available.  */
9479     return 0;
9480
9481   for (entry = dynamic_section; entry->d_tag != DT_NULL; ++entry)
9482     switch (entry->d_tag)
9483       {
9484       case DT_MIPS_LIBLIST:
9485         liblist_offset
9486           = offset_from_vma (file, entry->d_un.d_val,
9487                              liblistno * sizeof (Elf32_External_Lib));
9488         break;
9489       case DT_MIPS_LIBLISTNO:
9490         liblistno = entry->d_un.d_val;
9491         break;
9492       case DT_MIPS_OPTIONS:
9493         options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
9494         break;
9495       case DT_MIPS_CONFLICT:
9496         conflicts_offset
9497           = offset_from_vma (file, entry->d_un.d_val,
9498                              conflictsno * sizeof (Elf32_External_Conflict));
9499         break;
9500       case DT_MIPS_CONFLICTNO:
9501         conflictsno = entry->d_un.d_val;
9502         break;
9503       case DT_PLTGOT:
9504         pltgot = entry->d_un.d_ptr;
9505         break;
9506       case DT_MIPS_LOCAL_GOTNO:
9507         local_gotno = entry->d_un.d_val;
9508         break;
9509       case DT_MIPS_GOTSYM:
9510         gotsym = entry->d_un.d_val;
9511         break;
9512       case DT_MIPS_SYMTABNO:
9513         symtabno = entry->d_un.d_val;
9514         break;
9515       case DT_MIPS_PLTGOT:
9516         mips_pltgot = entry->d_un.d_ptr;
9517         break;
9518       case DT_PLTREL:
9519         pltrel = entry->d_un.d_val;
9520         break;
9521       case DT_PLTRELSZ:
9522         pltrelsz = entry->d_un.d_val;
9523         break;
9524       case DT_JMPREL:
9525         jmprel = entry->d_un.d_ptr;
9526         break;
9527       default:
9528         break;
9529       }
9530
9531   if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
9532     {
9533       Elf32_External_Lib *elib;
9534       size_t cnt;
9535
9536       elib = get_data (NULL, file, liblist_offset,
9537                        liblistno, sizeof (Elf32_External_Lib),
9538                        _("liblist"));
9539       if (elib)
9540         {
9541           printf ("\nSection '.liblist' contains %lu entries:\n",
9542                   (unsigned long) liblistno);
9543           fputs ("     Library              Time Stamp          Checksum   Version Flags\n",
9544                  stdout);
9545
9546           for (cnt = 0; cnt < liblistno; ++cnt)
9547             {
9548               Elf32_Lib liblist;
9549               time_t time;
9550               char timebuf[20];
9551               struct tm *tmp;
9552
9553               liblist.l_name = BYTE_GET (elib[cnt].l_name);
9554               time = BYTE_GET (elib[cnt].l_time_stamp);
9555               liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
9556               liblist.l_version = BYTE_GET (elib[cnt].l_version);
9557               liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
9558
9559               tmp = gmtime (&time);
9560               snprintf (timebuf, sizeof (timebuf),
9561                         "%04u-%02u-%02uT%02u:%02u:%02u",
9562                         tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9563                         tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9564
9565               printf ("%3lu: ", (unsigned long) cnt);
9566               if (VALID_DYNAMIC_NAME (liblist.l_name))
9567                 print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
9568               else
9569                 printf ("<corrupt: %9ld>", liblist.l_name);
9570               printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
9571                       liblist.l_version);
9572
9573               if (liblist.l_flags == 0)
9574                 puts (" NONE");
9575               else
9576                 {
9577                   static const struct
9578                   {
9579                     const char *name;
9580                     int bit;
9581                   }
9582                   l_flags_vals[] =
9583                   {
9584                     { " EXACT_MATCH", LL_EXACT_MATCH },
9585                     { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
9586                     { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
9587                     { " EXPORTS", LL_EXPORTS },
9588                     { " DELAY_LOAD", LL_DELAY_LOAD },
9589                     { " DELTA", LL_DELTA }
9590                   };
9591                   int flags = liblist.l_flags;
9592                   size_t fcnt;
9593
9594                   for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
9595                     if ((flags & l_flags_vals[fcnt].bit) != 0)
9596                       {
9597                         fputs (l_flags_vals[fcnt].name, stdout);
9598                         flags ^= l_flags_vals[fcnt].bit;
9599                       }
9600                   if (flags != 0)
9601                     printf (" %#x", (unsigned int) flags);
9602
9603                   puts ("");
9604                 }
9605             }
9606
9607           free (elib);
9608         }
9609     }
9610
9611   if (options_offset != 0)
9612     {
9613       Elf_External_Options *eopt;
9614       Elf_Internal_Shdr *sect = section_headers;
9615       Elf_Internal_Options *iopt;
9616       Elf_Internal_Options *option;
9617       size_t offset;
9618       int cnt;
9619
9620       /* Find the section header so that we get the size.  */
9621       while (sect->sh_type != SHT_MIPS_OPTIONS)
9622         ++sect;
9623
9624       eopt = get_data (NULL, file, options_offset, 1, sect->sh_size,
9625                        _("options"));
9626       if (eopt)
9627         {
9628           iopt = cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (*iopt));
9629           if (iopt == NULL)
9630             {
9631               error (_("Out of memory\n"));
9632               return 0;
9633             }
9634
9635           offset = cnt = 0;
9636           option = iopt;
9637
9638           while (offset < sect->sh_size)
9639             {
9640               Elf_External_Options *eoption;
9641
9642               eoption = (Elf_External_Options *) ((char *) eopt + offset);
9643
9644               option->kind = BYTE_GET (eoption->kind);
9645               option->size = BYTE_GET (eoption->size);
9646               option->section = BYTE_GET (eoption->section);
9647               option->info = BYTE_GET (eoption->info);
9648
9649               offset += option->size;
9650
9651               ++option;
9652               ++cnt;
9653             }
9654
9655           printf (_("\nSection '%s' contains %d entries:\n"),
9656                   SECTION_NAME (sect), cnt);
9657
9658           option = iopt;
9659
9660           while (cnt-- > 0)
9661             {
9662               size_t len;
9663
9664               switch (option->kind)
9665                 {
9666                 case ODK_NULL:
9667                   /* This shouldn't happen.  */
9668                   printf (" NULL       %d %lx", option->section, option->info);
9669                   break;
9670                 case ODK_REGINFO:
9671                   printf (" REGINFO    ");
9672                   if (elf_header.e_machine == EM_MIPS)
9673                     {
9674                       /* 32bit form.  */
9675                       Elf32_External_RegInfo *ereg;
9676                       Elf32_RegInfo reginfo;
9677
9678                       ereg = (Elf32_External_RegInfo *) (option + 1);
9679                       reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
9680                       reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9681                       reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9682                       reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9683                       reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9684                       reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
9685
9686                       printf ("GPR %08lx  GP 0x%lx\n",
9687                               reginfo.ri_gprmask,
9688                               (unsigned long) reginfo.ri_gp_value);
9689                       printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
9690                               reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9691                               reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9692                     }
9693                   else
9694                     {
9695                       /* 64 bit form.  */
9696                       Elf64_External_RegInfo *ereg;
9697                       Elf64_Internal_RegInfo reginfo;
9698
9699                       ereg = (Elf64_External_RegInfo *) (option + 1);
9700                       reginfo.ri_gprmask    = BYTE_GET (ereg->ri_gprmask);
9701                       reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9702                       reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9703                       reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9704                       reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9705                       reginfo.ri_gp_value   = BYTE_GET (ereg->ri_gp_value);
9706
9707                       printf ("GPR %08lx  GP 0x",
9708                               reginfo.ri_gprmask);
9709                       printf_vma (reginfo.ri_gp_value);
9710                       printf ("\n");
9711
9712                       printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
9713                               reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9714                               reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9715                     }
9716                   ++option;
9717                   continue;
9718                 case ODK_EXCEPTIONS:
9719                   fputs (" EXCEPTIONS fpe_min(", stdout);
9720                   process_mips_fpe_exception (option->info & OEX_FPU_MIN);
9721                   fputs (") fpe_max(", stdout);
9722                   process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
9723                   fputs (")", stdout);
9724
9725                   if (option->info & OEX_PAGE0)
9726                     fputs (" PAGE0", stdout);
9727                   if (option->info & OEX_SMM)
9728                     fputs (" SMM", stdout);
9729                   if (option->info & OEX_FPDBUG)
9730                     fputs (" FPDBUG", stdout);
9731                   if (option->info & OEX_DISMISS)
9732                     fputs (" DISMISS", stdout);
9733                   break;
9734                 case ODK_PAD:
9735                   fputs (" PAD       ", stdout);
9736                   if (option->info & OPAD_PREFIX)
9737                     fputs (" PREFIX", stdout);
9738                   if (option->info & OPAD_POSTFIX)
9739                     fputs (" POSTFIX", stdout);
9740                   if (option->info & OPAD_SYMBOL)
9741                     fputs (" SYMBOL", stdout);
9742                   break;
9743                 case ODK_HWPATCH:
9744                   fputs (" HWPATCH   ", stdout);
9745                   if (option->info & OHW_R4KEOP)
9746                     fputs (" R4KEOP", stdout);
9747                   if (option->info & OHW_R8KPFETCH)
9748                     fputs (" R8KPFETCH", stdout);
9749                   if (option->info & OHW_R5KEOP)
9750                     fputs (" R5KEOP", stdout);
9751                   if (option->info & OHW_R5KCVTL)
9752                     fputs (" R5KCVTL", stdout);
9753                   break;
9754                 case ODK_FILL:
9755                   fputs (" FILL       ", stdout);
9756                   /* XXX Print content of info word?  */
9757                   break;
9758                 case ODK_TAGS:
9759                   fputs (" TAGS       ", stdout);
9760                   /* XXX Print content of info word?  */
9761                   break;
9762                 case ODK_HWAND:
9763                   fputs (" HWAND     ", stdout);
9764                   if (option->info & OHWA0_R4KEOP_CHECKED)
9765                     fputs (" R4KEOP_CHECKED", stdout);
9766                   if (option->info & OHWA0_R4KEOP_CLEAN)
9767                     fputs (" R4KEOP_CLEAN", stdout);
9768                   break;
9769                 case ODK_HWOR:
9770                   fputs (" HWOR      ", stdout);
9771                   if (option->info & OHWA0_R4KEOP_CHECKED)
9772                     fputs (" R4KEOP_CHECKED", stdout);
9773                   if (option->info & OHWA0_R4KEOP_CLEAN)
9774                     fputs (" R4KEOP_CLEAN", stdout);
9775                   break;
9776                 case ODK_GP_GROUP:
9777                   printf (" GP_GROUP  %#06lx  self-contained %#06lx",
9778                           option->info & OGP_GROUP,
9779                           (option->info & OGP_SELF) >> 16);
9780                   break;
9781                 case ODK_IDENT:
9782                   printf (" IDENT     %#06lx  self-contained %#06lx",
9783                           option->info & OGP_GROUP,
9784                           (option->info & OGP_SELF) >> 16);
9785                   break;
9786                 default:
9787                   /* This shouldn't happen.  */
9788                   printf (" %3d ???     %d %lx",
9789                           option->kind, option->section, option->info);
9790                   break;
9791                 }
9792
9793               len = sizeof (*eopt);
9794               while (len < option->size)
9795                 if (((char *) option)[len] >= ' '
9796                     && ((char *) option)[len] < 0x7f)
9797                   printf ("%c", ((char *) option)[len++]);
9798                 else
9799                   printf ("\\%03o", ((char *) option)[len++]);
9800
9801               fputs ("\n", stdout);
9802               ++option;
9803             }
9804
9805           free (eopt);
9806         }
9807     }
9808
9809   if (conflicts_offset != 0 && conflictsno != 0)
9810     {
9811       Elf32_Conflict *iconf;
9812       size_t cnt;
9813
9814       if (dynamic_symbols == NULL)
9815         {
9816           error (_("conflict list found without a dynamic symbol table\n"));
9817           return 0;
9818         }
9819
9820       iconf = cmalloc (conflictsno, sizeof (*iconf));
9821       if (iconf == NULL)
9822         {
9823           error (_("Out of memory\n"));
9824           return 0;
9825         }
9826
9827       if (is_32bit_elf)
9828         {
9829           Elf32_External_Conflict *econf32;
9830
9831           econf32 = get_data (NULL, file, conflicts_offset,
9832                               conflictsno, sizeof (*econf32), _("conflict"));
9833           if (!econf32)
9834             return 0;
9835
9836           for (cnt = 0; cnt < conflictsno; ++cnt)
9837             iconf[cnt] = BYTE_GET (econf32[cnt]);
9838
9839           free (econf32);
9840         }
9841       else
9842         {
9843           Elf64_External_Conflict *econf64;
9844
9845           econf64 = get_data (NULL, file, conflicts_offset,
9846                               conflictsno, sizeof (*econf64), _("conflict"));
9847           if (!econf64)
9848             return 0;
9849
9850           for (cnt = 0; cnt < conflictsno; ++cnt)
9851             iconf[cnt] = BYTE_GET (econf64[cnt]);
9852
9853           free (econf64);
9854         }
9855
9856       printf (_("\nSection '.conflict' contains %lu entries:\n"),
9857               (unsigned long) conflictsno);
9858       puts (_("  Num:    Index       Value  Name"));
9859
9860       for (cnt = 0; cnt < conflictsno; ++cnt)
9861         {
9862           Elf_Internal_Sym *psym = & dynamic_symbols[iconf[cnt]];
9863
9864           printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
9865           print_vma (psym->st_value, FULL_HEX);
9866           putchar (' ');
9867           if (VALID_DYNAMIC_NAME (psym->st_name))
9868             print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
9869           else
9870             printf ("<corrupt: %14ld>", psym->st_name);
9871           putchar ('\n');
9872         }
9873
9874       free (iconf);
9875     }
9876
9877   if (pltgot != 0 && local_gotno != 0)
9878     {
9879       bfd_vma entry, local_end, global_end;
9880       size_t i, offset;
9881       unsigned char *data;
9882       int addr_size;
9883
9884       entry = pltgot;
9885       addr_size = (is_32bit_elf ? 4 : 8);
9886       local_end = pltgot + local_gotno * addr_size;
9887       global_end = local_end + (symtabno - gotsym) * addr_size;
9888
9889       offset = offset_from_vma (file, pltgot, global_end - pltgot);
9890       data = get_data (NULL, file, offset, global_end - pltgot, 1, _("GOT"));
9891       printf (_("\nPrimary GOT:\n"));
9892       printf (_(" Canonical gp value: "));
9893       print_vma (pltgot + 0x7ff0, LONG_HEX);
9894       printf ("\n\n");
9895
9896       printf (_(" Reserved entries:\n"));
9897       printf (_("  %*s %10s %*s Purpose\n"),
9898               addr_size * 2, "Address", "Access",
9899               addr_size * 2, "Initial");
9900       entry = print_mips_got_entry (data, pltgot, entry);
9901       printf (" Lazy resolver\n");
9902       if (data
9903           && (byte_get (data + entry - pltgot, addr_size)
9904               >> (addr_size * 8 - 1)) != 0)
9905         {
9906           entry = print_mips_got_entry (data, pltgot, entry);
9907           printf (" Module pointer (GNU extension)\n");
9908         }
9909       printf ("\n");
9910
9911       if (entry < local_end)
9912         {
9913           printf (_(" Local entries:\n"));
9914           printf (_("  %*s %10s %*s\n"),
9915                   addr_size * 2, "Address", "Access",
9916                   addr_size * 2, "Initial");
9917           while (entry < local_end)
9918             {
9919               entry = print_mips_got_entry (data, pltgot, entry);
9920               printf ("\n");
9921             }
9922           printf ("\n");
9923         }
9924
9925       if (gotsym < symtabno)
9926         {
9927           int sym_width;
9928
9929           printf (_(" Global entries:\n"));
9930           printf (_("  %*s %10s %*s %*s %-7s %3s %s\n"),
9931                   addr_size * 2, "Address", "Access",
9932                   addr_size * 2, "Initial",
9933                   addr_size * 2, "Sym.Val.", "Type", "Ndx", "Name");
9934           sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
9935           for (i = gotsym; i < symtabno; i++)
9936             {
9937               Elf_Internal_Sym *psym;
9938
9939               psym = dynamic_symbols + i;
9940               entry = print_mips_got_entry (data, pltgot, entry);
9941               printf (" ");
9942               print_vma (psym->st_value, LONG_HEX);
9943               printf (" %-7s %3s ",
9944                       get_symbol_type (ELF_ST_TYPE (psym->st_info)),
9945                       get_symbol_index_type (psym->st_shndx));
9946               if (VALID_DYNAMIC_NAME (psym->st_name))
9947                 print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
9948               else
9949                 printf ("<corrupt: %14ld>", psym->st_name);
9950               printf ("\n");
9951             }
9952           printf ("\n");
9953         }
9954
9955       if (data)
9956         free (data);
9957     }
9958
9959   if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
9960     {
9961       bfd_vma entry, end;
9962       size_t offset, rel_offset;
9963       unsigned long count, i;
9964       unsigned char *data;
9965       int addr_size, sym_width;
9966       Elf_Internal_Rela *rels;
9967
9968       rel_offset = offset_from_vma (file, jmprel, pltrelsz);
9969       if (pltrel == DT_RELA)
9970         {
9971           if (!slurp_rela_relocs (file, rel_offset, pltrelsz, &rels, &count))
9972             return 0;
9973         }
9974       else
9975         {
9976           if (!slurp_rel_relocs (file, rel_offset, pltrelsz, &rels, &count))
9977             return 0;
9978         }
9979
9980       entry = mips_pltgot;
9981       addr_size = (is_32bit_elf ? 4 : 8);
9982       end = mips_pltgot + (2 + count) * addr_size;
9983
9984       offset = offset_from_vma (file, mips_pltgot, end - mips_pltgot);
9985       data = get_data (NULL, file, offset, end - mips_pltgot, 1, _("PLT GOT"));
9986       printf (_("\nPLT GOT:\n\n"));
9987       printf (_(" Reserved entries:\n"));
9988       printf (_("  %*s %*s Purpose\n"),
9989               addr_size * 2, "Address", addr_size * 2, "Initial");
9990       entry = print_mips_pltgot_entry (data, mips_pltgot, entry);
9991       printf (" PLT lazy resolver\n");
9992       entry = print_mips_pltgot_entry (data, mips_pltgot, entry);
9993       printf (" Module pointer\n");
9994       printf ("\n");
9995
9996       printf (_(" Entries:\n"));
9997       printf (_("  %*s %*s %*s %-7s %3s %s\n"),
9998               addr_size * 2, "Address",
9999               addr_size * 2, "Initial",
10000               addr_size * 2, "Sym.Val.", "Type", "Ndx", "Name");
10001       sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
10002       for (i = 0; i < count; i++)
10003         {
10004           Elf_Internal_Sym *psym;
10005
10006           psym = dynamic_symbols + get_reloc_symindex (rels[i].r_info);
10007           entry = print_mips_pltgot_entry (data, mips_pltgot, entry);
10008           printf (" ");
10009           print_vma (psym->st_value, LONG_HEX);
10010           printf (" %-7s %3s ",
10011                   get_symbol_type (ELF_ST_TYPE (psym->st_info)),
10012                   get_symbol_index_type (psym->st_shndx));
10013           if (VALID_DYNAMIC_NAME (psym->st_name))
10014             print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
10015           else
10016             printf ("<corrupt: %14ld>", psym->st_name);
10017           printf ("\n");
10018         }
10019       printf ("\n");
10020
10021       if (data)
10022         free (data);
10023       free (rels);
10024     }
10025
10026   return 1;
10027 }
10028
10029 static int
10030 process_gnu_liblist (FILE *file)
10031 {
10032   Elf_Internal_Shdr *section, *string_sec;
10033   Elf32_External_Lib *elib;
10034   char *strtab;
10035   size_t strtab_size;
10036   size_t cnt;
10037   unsigned i;
10038
10039   if (! do_arch)
10040     return 0;
10041
10042   for (i = 0, section = section_headers;
10043        i < elf_header.e_shnum;
10044        i++, section++)
10045     {
10046       switch (section->sh_type)
10047         {
10048         case SHT_GNU_LIBLIST:
10049           if (section->sh_link >= elf_header.e_shnum)
10050             break;
10051
10052           elib = get_data (NULL, file, section->sh_offset, 1, section->sh_size,
10053                            _("liblist"));
10054
10055           if (elib == NULL)
10056             break;
10057           string_sec = section_headers + section->sh_link;
10058
10059           strtab = get_data (NULL, file, string_sec->sh_offset, 1,
10060                              string_sec->sh_size, _("liblist string table"));
10061           strtab_size = string_sec->sh_size;
10062
10063           if (strtab == NULL
10064               || section->sh_entsize != sizeof (Elf32_External_Lib))
10065             {
10066               free (elib);
10067               break;
10068             }
10069
10070           printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
10071                   SECTION_NAME (section),
10072                   (unsigned long) (section->sh_size / sizeof (Elf32_External_Lib)));
10073
10074           puts ("     Library              Time Stamp          Checksum   Version Flags");
10075
10076           for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
10077                ++cnt)
10078             {
10079               Elf32_Lib liblist;
10080               time_t time;
10081               char timebuf[20];
10082               struct tm *tmp;
10083
10084               liblist.l_name = BYTE_GET (elib[cnt].l_name);
10085               time = BYTE_GET (elib[cnt].l_time_stamp);
10086               liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
10087               liblist.l_version = BYTE_GET (elib[cnt].l_version);
10088               liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
10089
10090               tmp = gmtime (&time);
10091               snprintf (timebuf, sizeof (timebuf),
10092                         "%04u-%02u-%02uT%02u:%02u:%02u",
10093                         tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10094                         tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10095
10096               printf ("%3lu: ", (unsigned long) cnt);
10097               if (do_wide)
10098                 printf ("%-20s", liblist.l_name < strtab_size
10099                                  ? strtab + liblist.l_name : "<corrupt>");
10100               else
10101                 printf ("%-20.20s", liblist.l_name < strtab_size
10102                                     ? strtab + liblist.l_name : "<corrupt>");
10103               printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
10104                       liblist.l_version, liblist.l_flags);
10105             }
10106
10107           free (elib);
10108         }
10109     }
10110
10111   return 1;
10112 }
10113
10114 static const char *
10115 get_note_type (unsigned e_type)
10116 {
10117   static char buff[64];
10118
10119   if (elf_header.e_type == ET_CORE)
10120     switch (e_type)
10121       {
10122       case NT_AUXV:
10123         return _("NT_AUXV (auxiliary vector)");
10124       case NT_PRSTATUS:
10125         return _("NT_PRSTATUS (prstatus structure)");
10126       case NT_FPREGSET:
10127         return _("NT_FPREGSET (floating point registers)");
10128       case NT_PRPSINFO:
10129         return _("NT_PRPSINFO (prpsinfo structure)");
10130       case NT_TASKSTRUCT:
10131         return _("NT_TASKSTRUCT (task structure)");
10132       case NT_PRXFPREG:
10133         return _("NT_PRXFPREG (user_xfpregs structure)");
10134       case NT_PPC_VMX:
10135         return _("NT_PPC_VMX (ppc Altivec registers)");
10136       case NT_PPC_VSX:
10137         return _("NT_PPC_VSX (ppc VSX registers)");
10138       case NT_PSTATUS:
10139         return _("NT_PSTATUS (pstatus structure)");
10140       case NT_FPREGS:
10141         return _("NT_FPREGS (floating point registers)");
10142       case NT_PSINFO:
10143         return _("NT_PSINFO (psinfo structure)");
10144       case NT_LWPSTATUS:
10145         return _("NT_LWPSTATUS (lwpstatus_t structure)");
10146       case NT_LWPSINFO:
10147         return _("NT_LWPSINFO (lwpsinfo_t structure)");
10148       case NT_WIN32PSTATUS:
10149         return _("NT_WIN32PSTATUS (win32_pstatus structure)");
10150       default:
10151         break;
10152       }
10153   else
10154     switch (e_type)
10155       {
10156       case NT_VERSION:
10157         return _("NT_VERSION (version)");
10158       case NT_ARCH:
10159         return _("NT_ARCH (architecture)");
10160       default:
10161         break;
10162       }
10163
10164   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
10165   return buff;
10166 }
10167
10168 static const char *
10169 get_gnu_elf_note_type (unsigned e_type)
10170 {
10171   static char buff[64];
10172
10173   switch (e_type)
10174     {
10175     case NT_GNU_ABI_TAG:
10176       return _("NT_GNU_ABI_TAG (ABI version tag)");
10177     case NT_GNU_HWCAP:
10178       return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
10179     case NT_GNU_BUILD_ID:
10180       return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
10181     case NT_GNU_GOLD_VERSION:
10182       return _("NT_GNU_GOLD_VERSION (gold version)");
10183     default:
10184       break;
10185     }
10186
10187   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
10188   return buff;
10189 }
10190
10191 static const char *
10192 get_netbsd_elfcore_note_type (unsigned e_type)
10193 {
10194   static char buff[64];
10195
10196   if (e_type == NT_NETBSDCORE_PROCINFO)
10197     {
10198       /* NetBSD core "procinfo" structure.  */
10199       return _("NetBSD procinfo structure");
10200     }
10201
10202   /* As of Jan 2002 there are no other machine-independent notes
10203      defined for NetBSD core files.  If the note type is less
10204      than the start of the machine-dependent note types, we don't
10205      understand it.  */
10206
10207   if (e_type < NT_NETBSDCORE_FIRSTMACH)
10208     {
10209       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
10210       return buff;
10211     }
10212
10213   switch (elf_header.e_machine)
10214     {
10215     /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
10216        and PT_GETFPREGS == mach+2.  */
10217
10218     case EM_OLD_ALPHA:
10219     case EM_ALPHA:
10220     case EM_SPARC:
10221     case EM_SPARC32PLUS:
10222     case EM_SPARCV9:
10223       switch (e_type)
10224         {
10225         case NT_NETBSDCORE_FIRSTMACH+0:
10226           return _("PT_GETREGS (reg structure)");
10227         case NT_NETBSDCORE_FIRSTMACH+2:
10228           return _("PT_GETFPREGS (fpreg structure)");
10229         default:
10230           break;
10231         }
10232       break;
10233
10234     /* On all other arch's, PT_GETREGS == mach+1 and
10235        PT_GETFPREGS == mach+3.  */
10236     default:
10237       switch (e_type)
10238         {
10239         case NT_NETBSDCORE_FIRSTMACH+1:
10240           return _("PT_GETREGS (reg structure)");
10241         case NT_NETBSDCORE_FIRSTMACH+3:
10242           return _("PT_GETFPREGS (fpreg structure)");
10243         default:
10244           break;
10245         }
10246     }
10247
10248   snprintf (buff, sizeof (buff), _("PT_FIRSTMACH+%d"),
10249             e_type - NT_NETBSDCORE_FIRSTMACH);
10250   return buff;
10251 }
10252
10253 /* Note that by the ELF standard, the name field is already null byte
10254    terminated, and namesz includes the terminating null byte.
10255    I.E. the value of namesz for the name "FSF" is 4.
10256
10257    If the value of namesz is zero, there is no name present.  */
10258 static int
10259 process_note (Elf_Internal_Note *pnote)
10260 {
10261   const char *name = pnote->namesz ? pnote->namedata : "(NONE)";
10262   const char *nt;
10263
10264   if (pnote->namesz == 0)
10265     /* If there is no note name, then use the default set of
10266        note type strings.  */
10267     nt = get_note_type (pnote->type);
10268
10269   else if (const_strneq (pnote->namedata, "GNU"))
10270     /* GNU-specific object file notes.  */
10271     nt = get_gnu_elf_note_type (pnote->type);
10272
10273   else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
10274     /* NetBSD-specific core file notes.  */
10275     nt = get_netbsd_elfcore_note_type (pnote->type);
10276
10277   else if (strneq (pnote->namedata, "SPU/", 4))
10278     {
10279       /* SPU-specific core file notes.  */
10280       nt = pnote->namedata + 4;
10281       name = "SPU";
10282     }
10283
10284   else
10285     /* Don't recognize this note name; just use the default set of
10286        note type strings.  */
10287       nt = get_note_type (pnote->type);
10288
10289   printf ("  %s\t\t0x%08lx\t%s\n", name, pnote->descsz, nt);
10290   return 1;
10291 }
10292
10293
10294 static int
10295 process_corefile_note_segment (FILE *file, bfd_vma offset, bfd_vma length)
10296 {
10297   Elf_External_Note *pnotes;
10298   Elf_External_Note *external;
10299   int res = 1;
10300
10301   if (length <= 0)
10302     return 0;
10303
10304   pnotes = get_data (NULL, file, offset, 1, length, _("notes"));
10305   if (!pnotes)
10306     return 0;
10307
10308   external = pnotes;
10309
10310   printf (_("\nNotes at offset 0x%08lx with length 0x%08lx:\n"),
10311           (unsigned long) offset, (unsigned long) length);
10312   printf (_("  Owner\t\tData size\tDescription\n"));
10313
10314   while (external < (Elf_External_Note *)((char *) pnotes + length))
10315     {
10316       Elf_External_Note *next;
10317       Elf_Internal_Note inote;
10318       char *temp = NULL;
10319
10320       inote.type     = BYTE_GET (external->type);
10321       inote.namesz   = BYTE_GET (external->namesz);
10322       inote.namedata = external->name;
10323       inote.descsz   = BYTE_GET (external->descsz);
10324       inote.descdata = inote.namedata + align_power (inote.namesz, 2);
10325       inote.descpos  = offset + (inote.descdata - (char *) pnotes);
10326
10327       next = (Elf_External_Note *)(inote.descdata + align_power (inote.descsz, 2));
10328
10329       if (((char *) next) > (((char *) pnotes) + length))
10330         {
10331           warn (_("corrupt note found at offset %lx into core notes\n"),
10332                 (unsigned long) ((char *) external - (char *) pnotes));
10333           warn (_(" type: %lx, namesize: %08lx, descsize: %08lx\n"),
10334                 inote.type, inote.namesz, inote.descsz);
10335           break;
10336         }
10337
10338       external = next;
10339
10340       /* Verify that name is null terminated.  It appears that at least
10341          one version of Linux (RedHat 6.0) generates corefiles that don't
10342          comply with the ELF spec by failing to include the null byte in
10343          namesz.  */
10344       if (inote.namedata[inote.namesz] != '\0')
10345         {
10346           temp = malloc (inote.namesz + 1);
10347
10348           if (temp == NULL)
10349             {
10350               error (_("Out of memory\n"));
10351               res = 0;
10352               break;
10353             }
10354
10355           strncpy (temp, inote.namedata, inote.namesz);
10356           temp[inote.namesz] = 0;
10357
10358           /* warn (_("'%s' NOTE name not properly null terminated\n"), temp);  */
10359           inote.namedata = temp;
10360         }
10361
10362       res &= process_note (& inote);
10363
10364       if (temp != NULL)
10365         {
10366           free (temp);
10367           temp = NULL;
10368         }
10369     }
10370
10371   free (pnotes);
10372
10373   return res;
10374 }
10375
10376 static int
10377 process_corefile_note_segments (FILE *file)
10378 {
10379   Elf_Internal_Phdr *segment;
10380   unsigned int i;
10381   int res = 1;
10382
10383   if (! get_program_headers (file))
10384       return 0;
10385
10386   for (i = 0, segment = program_headers;
10387        i < elf_header.e_phnum;
10388        i++, segment++)
10389     {
10390       if (segment->p_type == PT_NOTE)
10391         res &= process_corefile_note_segment (file,
10392                                               (bfd_vma) segment->p_offset,
10393                                               (bfd_vma) segment->p_filesz);
10394     }
10395
10396   return res;
10397 }
10398
10399 static int
10400 process_note_sections (FILE *file)
10401 {
10402   Elf_Internal_Shdr *section;
10403   unsigned long i;
10404   int res = 1;
10405
10406   for (i = 0, section = section_headers;
10407        i < elf_header.e_shnum;
10408        i++, section++)
10409     if (section->sh_type == SHT_NOTE)
10410       res &= process_corefile_note_segment (file,
10411                                             (bfd_vma) section->sh_offset,
10412                                             (bfd_vma) section->sh_size);
10413
10414   return res;
10415 }
10416
10417 static int
10418 process_notes (FILE *file)
10419 {
10420   /* If we have not been asked to display the notes then do nothing.  */
10421   if (! do_notes)
10422     return 1;
10423
10424   if (elf_header.e_type != ET_CORE)
10425     return process_note_sections (file);
10426
10427   /* No program headers means no NOTE segment.  */
10428   if (elf_header.e_phnum > 0)
10429     return process_corefile_note_segments (file);
10430
10431   printf (_("No note segments present in the core file.\n"));
10432   return 1;
10433 }
10434
10435 static int
10436 process_arch_specific (FILE *file)
10437 {
10438   if (! do_arch)
10439     return 1;
10440
10441   switch (elf_header.e_machine)
10442     {
10443     case EM_ARM:
10444       return process_arm_specific (file);
10445     case EM_MIPS:
10446     case EM_MIPS_RS3_LE:
10447       return process_mips_specific (file);
10448       break;
10449     case EM_PPC:
10450       return process_power_specific (file);
10451       break;
10452     default:
10453       break;
10454     }
10455   return 1;
10456 }
10457
10458 static int
10459 get_file_header (FILE *file)
10460 {
10461   /* Read in the identity array.  */
10462   if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
10463     return 0;
10464
10465   /* Determine how to read the rest of the header.  */
10466   switch (elf_header.e_ident[EI_DATA])
10467     {
10468     default: /* fall through */
10469     case ELFDATANONE: /* fall through */
10470     case ELFDATA2LSB:
10471       byte_get = byte_get_little_endian;
10472       byte_put = byte_put_little_endian;
10473       break;
10474     case ELFDATA2MSB:
10475       byte_get = byte_get_big_endian;
10476       byte_put = byte_put_big_endian;
10477       break;
10478     }
10479
10480   /* For now we only support 32 bit and 64 bit ELF files.  */
10481   is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
10482
10483   /* Read in the rest of the header.  */
10484   if (is_32bit_elf)
10485     {
10486       Elf32_External_Ehdr ehdr32;
10487
10488       if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
10489         return 0;
10490
10491       elf_header.e_type      = BYTE_GET (ehdr32.e_type);
10492       elf_header.e_machine   = BYTE_GET (ehdr32.e_machine);
10493       elf_header.e_version   = BYTE_GET (ehdr32.e_version);
10494       elf_header.e_entry     = BYTE_GET (ehdr32.e_entry);
10495       elf_header.e_phoff     = BYTE_GET (ehdr32.e_phoff);
10496       elf_header.e_shoff     = BYTE_GET (ehdr32.e_shoff);
10497       elf_header.e_flags     = BYTE_GET (ehdr32.e_flags);
10498       elf_header.e_ehsize    = BYTE_GET (ehdr32.e_ehsize);
10499       elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
10500       elf_header.e_phnum     = BYTE_GET (ehdr32.e_phnum);
10501       elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
10502       elf_header.e_shnum     = BYTE_GET (ehdr32.e_shnum);
10503       elf_header.e_shstrndx  = BYTE_GET (ehdr32.e_shstrndx);
10504     }
10505   else
10506     {
10507       Elf64_External_Ehdr ehdr64;
10508
10509       /* If we have been compiled with sizeof (bfd_vma) == 4, then
10510          we will not be able to cope with the 64bit data found in
10511          64 ELF files.  Detect this now and abort before we start
10512          overwriting things.  */
10513       if (sizeof (bfd_vma) < 8)
10514         {
10515           error (_("This instance of readelf has been built without support for a\n\
10516 64 bit data type and so it cannot read 64 bit ELF files.\n"));
10517           return 0;
10518         }
10519
10520       if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
10521         return 0;
10522
10523       elf_header.e_type      = BYTE_GET (ehdr64.e_type);
10524       elf_header.e_machine   = BYTE_GET (ehdr64.e_machine);
10525       elf_header.e_version   = BYTE_GET (ehdr64.e_version);
10526       elf_header.e_entry     = BYTE_GET (ehdr64.e_entry);
10527       elf_header.e_phoff     = BYTE_GET (ehdr64.e_phoff);
10528       elf_header.e_shoff     = BYTE_GET (ehdr64.e_shoff);
10529       elf_header.e_flags     = BYTE_GET (ehdr64.e_flags);
10530       elf_header.e_ehsize    = BYTE_GET (ehdr64.e_ehsize);
10531       elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
10532       elf_header.e_phnum     = BYTE_GET (ehdr64.e_phnum);
10533       elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
10534       elf_header.e_shnum     = BYTE_GET (ehdr64.e_shnum);
10535       elf_header.e_shstrndx  = BYTE_GET (ehdr64.e_shstrndx);
10536     }
10537
10538   if (elf_header.e_shoff)
10539     {
10540       /* There may be some extensions in the first section header.  Don't
10541          bomb if we can't read it.  */
10542       if (is_32bit_elf)
10543         get_32bit_section_headers (file, 1);
10544       else
10545         get_64bit_section_headers (file, 1);
10546     }
10547
10548   return 1;
10549 }
10550
10551 /* Process one ELF object file according to the command line options.
10552    This file may actually be stored in an archive.  The file is
10553    positioned at the start of the ELF object.  */
10554
10555 static int
10556 process_object (char *file_name, FILE *file)
10557 {
10558   unsigned int i;
10559
10560   if (! get_file_header (file))
10561     {
10562       error (_("%s: Failed to read file header\n"), file_name);
10563       return 1;
10564     }
10565
10566   /* Initialise per file variables.  */
10567   for (i = ARRAY_SIZE (version_info); i--;)
10568     version_info[i] = 0;
10569
10570   for (i = ARRAY_SIZE (dynamic_info); i--;)
10571     dynamic_info[i] = 0;
10572
10573   /* Process the file.  */
10574   if (show_name)
10575     printf (_("\nFile: %s\n"), file_name);
10576
10577   /* Initialise the dump_sects array from the cmdline_dump_sects array.
10578      Note we do this even if cmdline_dump_sects is empty because we
10579      must make sure that the dump_sets array is zeroed out before each
10580      object file is processed.  */
10581   if (num_dump_sects > num_cmdline_dump_sects)
10582     memset (dump_sects, 0, num_dump_sects * sizeof (* dump_sects));
10583
10584   if (num_cmdline_dump_sects > 0)
10585     {
10586       if (num_dump_sects == 0)
10587         /* A sneaky way of allocating the dump_sects array.  */
10588         request_dump_bynumber (num_cmdline_dump_sects, 0);
10589
10590       assert (num_dump_sects >= num_cmdline_dump_sects);
10591       memcpy (dump_sects, cmdline_dump_sects,
10592               num_cmdline_dump_sects * sizeof (* dump_sects));
10593     }
10594
10595   if (! process_file_header ())
10596     return 1;
10597
10598   if (! process_section_headers (file))
10599     {
10600       /* Without loaded section headers we cannot process lots of
10601          things.  */
10602       do_unwind = do_version = do_dump = do_arch = 0;
10603
10604       if (! do_using_dynamic)
10605         do_syms = do_reloc = 0;
10606     }
10607
10608   if (! process_section_groups (file))
10609     {
10610       /* Without loaded section groups we cannot process unwind.  */
10611       do_unwind = 0;
10612     }
10613
10614   if (process_program_headers (file))
10615     process_dynamic_section (file);
10616
10617   process_relocs (file);
10618
10619   process_unwind (file);
10620
10621   process_symbol_table (file);
10622
10623   process_syminfo (file);
10624
10625   process_version_sections (file);
10626
10627   process_section_contents (file);
10628
10629   process_notes (file);
10630
10631   process_gnu_liblist (file);
10632
10633   process_arch_specific (file);
10634
10635   if (program_headers)
10636     {
10637       free (program_headers);
10638       program_headers = NULL;
10639     }
10640
10641   if (section_headers)
10642     {
10643       free (section_headers);
10644       section_headers = NULL;
10645     }
10646
10647   if (string_table)
10648     {
10649       free (string_table);
10650       string_table = NULL;
10651       string_table_length = 0;
10652     }
10653
10654   if (dynamic_strings)
10655     {
10656       free (dynamic_strings);
10657       dynamic_strings = NULL;
10658       dynamic_strings_length = 0;
10659     }
10660
10661   if (dynamic_symbols)
10662     {
10663       free (dynamic_symbols);
10664       dynamic_symbols = NULL;
10665       num_dynamic_syms = 0;
10666     }
10667
10668   if (dynamic_syminfo)
10669     {
10670       free (dynamic_syminfo);
10671       dynamic_syminfo = NULL;
10672     }
10673
10674   if (section_headers_groups)
10675     {
10676       free (section_headers_groups);
10677       section_headers_groups = NULL;
10678     }
10679
10680   if (section_groups)
10681     {
10682       struct group_list *g, *next;
10683
10684       for (i = 0; i < group_count; i++)
10685         {
10686           for (g = section_groups [i].root; g != NULL; g = next)
10687             {
10688               next = g->next;
10689               free (g);
10690             }
10691         }
10692
10693       free (section_groups);
10694       section_groups = NULL;
10695     }
10696
10697   free_debug_memory ();
10698
10699   return 0;
10700 }
10701
10702 /* Process an ELF archive.
10703    On entry the file is positioned just after the ARMAG string.  */
10704
10705 static int
10706 process_archive (char *file_name, FILE *file)
10707 {
10708   struct ar_hdr arhdr;
10709   size_t got;
10710   unsigned long size;
10711   unsigned long index_num = 0;
10712   unsigned long *index_array = NULL;
10713   char *sym_table = NULL;
10714   unsigned long sym_size = 0;
10715   char *longnames = NULL;
10716   unsigned long longnames_size = 0;
10717   size_t file_name_size;
10718   int ret;
10719
10720   show_name = 1;
10721
10722   got = fread (&arhdr, 1, sizeof arhdr, file);
10723   if (got != sizeof arhdr)
10724     {
10725       if (got == 0)
10726         return 0;
10727
10728       error (_("%s: failed to read archive header\n"), file_name);
10729       return 1;
10730     }
10731
10732   /* See if this is the archive symbol table.  */
10733   if (const_strneq (arhdr.ar_name, "/               ")
10734       || const_strneq (arhdr.ar_name, "/SYM64/         "))
10735     {
10736       size = strtoul (arhdr.ar_size, NULL, 10);
10737       size = size + (size & 1);
10738
10739       if (do_archive_index)
10740         {
10741           unsigned long i;
10742           /* A buffer used to hold numbers read in from an archive index.
10743              These are always 4 bytes long and stored in big-endian format.  */
10744 #define SIZEOF_AR_INDEX_NUMBERS 4
10745           unsigned char integer_buffer[SIZEOF_AR_INDEX_NUMBERS];
10746           unsigned char * index_buffer;
10747
10748           /* Check the size of the archive index.  */
10749           if (size < SIZEOF_AR_INDEX_NUMBERS)
10750             {
10751               error (_("%s: the archive index is empty\n"), file_name);
10752               return 1;
10753             }
10754
10755           /* Read the numer of entries in the archive index.  */
10756           got = fread (integer_buffer, 1, sizeof integer_buffer, file);
10757           if (got != sizeof (integer_buffer))
10758             {
10759               error (_("%s: failed to read archive index\n"), file_name);
10760               return 1;
10761             }
10762           index_num = byte_get_big_endian (integer_buffer, sizeof integer_buffer);
10763           size -= SIZEOF_AR_INDEX_NUMBERS;
10764
10765           /* Read in the archive index.  */
10766           if (size < index_num * SIZEOF_AR_INDEX_NUMBERS)
10767             {
10768               error (_("%s: the archive index is supposed to have %ld entries, but the size in the header is too small\n"),
10769                      file_name, index_num);
10770               return 1;
10771             }
10772           index_buffer = malloc (index_num * SIZEOF_AR_INDEX_NUMBERS);
10773           if (index_buffer == NULL)
10774             {
10775               error (_("Out of memory whilst trying to read archive symbol index\n"));
10776               return 1;
10777             }
10778           got = fread (index_buffer, SIZEOF_AR_INDEX_NUMBERS, index_num, file);
10779           if (got != index_num)
10780             {
10781               free (index_buffer);
10782               error (_("%s: failed to read archive index\n"), file_name);
10783               ret = 1;
10784               goto out;
10785             }
10786           size -= index_num * SIZEOF_AR_INDEX_NUMBERS;
10787
10788           /* Convert the index numbers into the host's numeric format.  */
10789           index_array = malloc (index_num * sizeof (* index_array));
10790           if (index_array == NULL)
10791             {
10792               free (index_buffer);
10793               error (_("Out of memory whilst trying to convert the archive symbol index\n"));
10794               return 1;
10795             }
10796
10797           for (i = 0; i < index_num; i++)
10798             index_array[i] = byte_get_big_endian ((unsigned char *)(index_buffer + (i * SIZEOF_AR_INDEX_NUMBERS)),
10799                                                   SIZEOF_AR_INDEX_NUMBERS);
10800           free (index_buffer);
10801
10802           /* The remaining space in the header is taken up by the symbol table.  */
10803           if (size < 1)
10804             {
10805               error (_("%s: the archive has an index but no symbols\n"), file_name);
10806               ret = 1;
10807               goto out;
10808             }
10809           sym_table = malloc (size);
10810           sym_size = size;
10811           if (sym_table == NULL)
10812             {
10813               error (_("Out of memory whilst trying to read archive index symbol table\n"));
10814               ret = 1;
10815               goto out;
10816             }
10817           got = fread (sym_table, 1, size, file);
10818           if (got != size)
10819             {
10820               error (_("%s: failed to read archive index symbol table\n"), file_name);
10821               ret = 1;
10822               goto out;
10823             }
10824         }
10825       else
10826         {
10827           if (fseek (file, size, SEEK_CUR) != 0)
10828             {
10829               error (_("%s: failed to skip archive symbol table\n"), file_name);
10830               return 1;
10831             }
10832         }
10833
10834       got = fread (& arhdr, 1, sizeof arhdr, file);
10835       if (got != sizeof arhdr)
10836         {
10837           if (got == 0)
10838             {
10839               ret = 0;
10840               goto out;
10841             }
10842
10843           error (_("%s: failed to read archive header following archive index\n"), file_name);
10844           ret = 1;
10845           goto out;
10846         }
10847     }
10848   else if (do_archive_index)
10849     printf (_("%s has no archive index\n"), file_name);
10850
10851   if (const_strneq (arhdr.ar_name, "//              "))
10852     {
10853       /* This is the archive string table holding long member
10854          names.  */
10855
10856       longnames_size = strtoul (arhdr.ar_size, NULL, 10);
10857       longnames = malloc (longnames_size);
10858       if (longnames == NULL)
10859         {
10860           error (_("Out of memory reading long symbol names in archive\n"));
10861           ret = 1;
10862           goto out;
10863         }
10864
10865       if (fread (longnames, longnames_size, 1, file) != 1)
10866         {
10867           free (longnames);
10868           error (_("%s: failed to read long symbol name string table\n"), file_name);
10869           ret = 1;
10870           goto out;
10871         }
10872
10873       if ((longnames_size & 1) != 0)
10874         getc (file);
10875
10876       got = fread (& arhdr, 1, sizeof arhdr, file);
10877       if (got != sizeof arhdr)
10878         {
10879           if (got == 0)
10880             ret = 0;
10881           else
10882             {
10883               error (_("%s: failed to read archive header following long symbol names\n"), file_name);
10884               ret = 1;
10885             }
10886           goto out;
10887         }
10888     }
10889
10890   if (do_archive_index)
10891     {
10892       if (sym_table == NULL)
10893         error (_("%s: unable to dump the index as none was found\n"), file_name);
10894       else
10895         {
10896           unsigned int i, j, k, l;
10897           char elf_name[16];
10898           unsigned long current_pos;
10899
10900           printf (_("Index of archive %s: (%ld entries, 0x%lx bytes in the symbol table)\n"),
10901                   file_name, index_num, sym_size);
10902           current_pos = ftell (file);
10903
10904           for (i = l = 0; i < index_num; i++)
10905             {
10906               if ((i == 0) || ((i > 0) && (index_array[i] != index_array[i - 1])))
10907                 {
10908                   if (fseek (file, index_array[i], SEEK_SET) != 0)
10909                     {
10910                       error (_("%s: failed to seek to next file name\n"), file_name);
10911                       ret = 1;
10912                       goto out;
10913                     }
10914                   got = fread (elf_name, 1, 16, file);
10915                   if (got != 16)
10916                     {
10917                       error (_("%s: failed to read file name\n"), file_name);
10918                       ret = 1;
10919                       goto out;
10920                     }
10921
10922                   if (elf_name[0] == '/')
10923                     {
10924                       /* We have a long name.  */
10925                       k = j = strtoul (elf_name + 1, NULL, 10);
10926                       while ((j < longnames_size) && (longnames[j] != '/'))
10927                         j++;
10928                       longnames[j] = '\0';
10929                       printf (_("Binary %s contains:\n"), longnames + k);
10930                       longnames[j] = '/';
10931                     }
10932                   else
10933                     {
10934                       j = 0;
10935                       while ((elf_name[j] != '/') && (j < 16))
10936                         j++;
10937                       elf_name[j] = '\0';
10938                       printf(_("Binary %s contains:\n"), elf_name);
10939                     }
10940                 }
10941               if (l >= sym_size)
10942                 {
10943                   error (_("%s: end of the symbol table reached before the end of the index\n"),
10944                          file_name);
10945                   break;
10946                 }
10947               printf ("\t%s\n", sym_table + l);
10948               l += strlen (sym_table + l) + 1;
10949             }
10950
10951           if (l < sym_size)
10952             error (_("%s: symbols remain in the index symbol table, but without corresponding entries in the index table\n"),
10953                    file_name);
10954
10955           free (index_array);
10956           index_array = NULL;
10957           free (sym_table);
10958           sym_table = NULL;
10959           if (fseek (file, current_pos, SEEK_SET) != 0)
10960             {
10961               error (_("%s: failed to seek back to start of object files in the archive\n"), file_name);
10962               return 1;
10963             }
10964         }
10965
10966       if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
10967           && !do_segments && !do_header && !do_dump && !do_version
10968           && !do_histogram && !do_debugging && !do_arch && !do_notes
10969           && !do_section_groups)
10970         return 0; /* Archive index only.  */
10971     }
10972
10973   file_name_size = strlen (file_name);
10974   ret = 0;
10975
10976   while (1)
10977     {
10978       char *name;
10979       char *nameend;
10980       char *namealc;
10981
10982       if (arhdr.ar_name[0] == '/')
10983         {
10984           unsigned long off;
10985
10986           off = strtoul (arhdr.ar_name + 1, NULL, 10);
10987           if (off >= longnames_size)
10988             {
10989               error (_("%s: invalid archive string table offset %lu\n"), file_name, off);
10990               ret = 1;
10991               break;
10992             }
10993
10994           name = longnames + off;
10995           nameend = memchr (name, '/', longnames_size - off);
10996         }
10997       else
10998         {
10999           name = arhdr.ar_name;
11000           nameend = memchr (name, '/', 16);
11001         }
11002
11003       if (nameend == NULL)
11004         {
11005           error (_("%s: bad archive file name\n"), file_name);
11006           ret = 1;
11007           break;
11008         }
11009
11010       namealc = malloc (file_name_size + (nameend - name) + 3);
11011       if (namealc == NULL)
11012         {
11013           error (_("Out of memory\n"));
11014           ret = 1;
11015           break;
11016         }
11017
11018       memcpy (namealc, file_name, file_name_size);
11019       namealc[file_name_size] = '(';
11020       memcpy (namealc + file_name_size + 1, name, nameend - name);
11021       namealc[file_name_size + 1 + (nameend - name)] = ')';
11022       namealc[file_name_size + 2 + (nameend - name)] = '\0';
11023
11024       archive_file_offset = ftell (file);
11025       archive_file_size = strtoul (arhdr.ar_size, NULL, 10);
11026
11027       ret |= process_object (namealc, file);
11028
11029       free (namealc);
11030
11031       if (fseek (file,
11032                  (archive_file_offset
11033                   + archive_file_size
11034                   + (archive_file_size & 1)),
11035                  SEEK_SET) != 0)
11036         {
11037           error (_("%s: failed to seek to next archive header\n"), file_name);
11038           ret = 1;
11039           break;
11040         }
11041
11042       got = fread (&arhdr, 1, sizeof arhdr, file);
11043       if (got != sizeof arhdr)
11044         {
11045           if (got == 0)
11046             break;
11047
11048           error (_("%s: failed to read archive header\n"), file_name);
11049           ret = 1;
11050           break;
11051         }
11052     }
11053
11054  out:
11055   if (index_array != NULL)
11056     free (index_array);
11057   if (sym_table != NULL)
11058     free (sym_table);
11059   if (longnames != NULL)
11060     free (longnames);
11061
11062   return ret;
11063 }
11064
11065 static int
11066 process_file (char *file_name)
11067 {
11068   FILE *file;
11069   struct stat statbuf;
11070   char armag[SARMAG];
11071   int ret;
11072
11073   if (stat (file_name, &statbuf) < 0)
11074     {
11075       if (errno == ENOENT)
11076         error (_("'%s': No such file\n"), file_name);
11077       else
11078         error (_("Could not locate '%s'.  System error message: %s\n"),
11079                file_name, strerror (errno));
11080       return 1;
11081     }
11082
11083   if (! S_ISREG (statbuf.st_mode))
11084     {
11085       error (_("'%s' is not an ordinary file\n"), file_name);
11086       return 1;
11087     }
11088
11089   file = fopen (file_name, "rb");
11090   if (file == NULL)
11091     {
11092       error (_("Input file '%s' is not readable.\n"), file_name);
11093       return 1;
11094     }
11095
11096   if (fread (armag, SARMAG, 1, file) != 1)
11097     {
11098       error (_("%s: Failed to read file's magic number\n"), file_name);
11099       fclose (file);
11100       return 1;
11101     }
11102
11103   if (memcmp (armag, ARMAG, SARMAG) == 0)
11104     ret = process_archive (file_name, file);
11105   else
11106     {
11107       if (do_archive_index)
11108         error (_("File %s is not an archive so its index cannot be displayed.\n"),
11109                file_name);
11110
11111       rewind (file);
11112       archive_file_size = archive_file_offset = 0;
11113       ret = process_object (file_name, file);
11114     }
11115
11116   fclose (file);
11117
11118   return ret;
11119 }
11120
11121 #ifdef SUPPORT_DISASSEMBLY
11122 /* Needed by the i386 disassembler.  For extra credit, someone could
11123    fix this so that we insert symbolic addresses here, esp for GOT/PLT
11124    symbols.  */
11125
11126 void
11127 print_address (unsigned int addr, FILE *outfile)
11128 {
11129   fprintf (outfile,"0x%8.8x", addr);
11130 }
11131
11132 /* Needed by the i386 disassembler.  */
11133 void
11134 db_task_printsym (unsigned int addr)
11135 {
11136   print_address (addr, stderr);
11137 }
11138 #endif
11139
11140 int
11141 main (int argc, char **argv)
11142 {
11143   int err;
11144
11145 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
11146   setlocale (LC_MESSAGES, "");
11147 #endif
11148 #if defined (HAVE_SETLOCALE)
11149   setlocale (LC_CTYPE, "");
11150 #endif
11151   bindtextdomain (PACKAGE, LOCALEDIR);
11152   textdomain (PACKAGE);
11153
11154   expandargv (&argc, &argv);
11155
11156   parse_args (argc, argv);
11157
11158   if (num_dump_sects > 0)
11159     {
11160       /* Make a copy of the dump_sects array.  */
11161       cmdline_dump_sects = malloc (num_dump_sects * sizeof (* dump_sects));
11162       if (cmdline_dump_sects == NULL)
11163         error (_("Out of memory allocating dump request table.\n"));
11164       else
11165         {
11166           memcpy (cmdline_dump_sects, dump_sects,
11167                   num_dump_sects * sizeof (* dump_sects));
11168           num_cmdline_dump_sects = num_dump_sects;
11169         }
11170     }
11171
11172   if (optind < (argc - 1))
11173     show_name = 1;
11174
11175   err = 0;
11176   while (optind < argc)
11177     err |= process_file (argv[optind++]);
11178
11179   if (dump_sects != NULL)
11180     free (dump_sects);
11181   if (cmdline_dump_sects != NULL)
11182     free (cmdline_dump_sects);
11183
11184   return err;
11185 }