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