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