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