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