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