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