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