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