OSDN Git Service

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