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