OSDN Git Service

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