readelf.c revision 1.5
1/* readelf.c -- display contents of an ELF format file
2   Copyright (C) 1998-2016 Free Software Foundation, Inc.
3
4   Originally developed by Eric Youngdale <eric@andante.jic.com>
5   Modifications by Nick Clifton <nickc@redhat.com>
6
7   This file is part of GNU Binutils.
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 3 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22   02110-1301, USA.  */
23
24/* The difference between readelf and objdump:
25
26  Both programs are capable of displaying the contents of ELF format files,
27  so why does the binutils project have two file dumpers ?
28
29  The reason is that objdump sees an ELF file through a BFD filter of the
30  world; if BFD has a bug where, say, it disagrees about a machine constant
31  in e_flags, then the odds are good that it will remain internally
32  consistent.  The linker sees it the BFD way, objdump sees it the BFD way,
33  GAS sees it the BFD way.  There was need for a tool to go find out what
34  the file actually says.
35
36  This is why the readelf program does not link against the BFD library - it
37  exists as an independent program to help verify the correct working of BFD.
38
39  There is also the case that readelf can provide more information about an
40  ELF file than is provided by objdump.  In particular it can display DWARF
41  debugging information which (at the moment) objdump cannot.  */
42
43#include "sysdep.h"
44#include <assert.h>
45#include <time.h>
46#include <zlib.h>
47#ifdef HAVE_WCHAR_H
48#include <wchar.h>
49#endif
50
51#if __GNUC__ >= 2
52/* Define BFD64 here, even if our default architecture is 32 bit ELF
53   as this will allow us to read in and parse 64bit and 32bit ELF files.
54   Only do this if we believe that the compiler can support a 64 bit
55   data type.  For now we only rely on GCC being able to do this.  */
56#define BFD64
57#endif
58
59#include "bfd.h"
60#include "bucomm.h"
61#include "elfcomm.h"
62#include "dwarf.h"
63
64#include "elf/common.h"
65#include "elf/external.h"
66#include "elf/internal.h"
67
68
69/* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
70   we can obtain the H8 reloc numbers.  We need these for the
71   get_reloc_size() function.  We include h8.h again after defining
72   RELOC_MACROS_GEN_FUNC so that we get the naming function as well.  */
73
74#include "elf/h8.h"
75#undef _ELF_H8_H
76
77/* Undo the effects of #including reloc-macros.h.  */
78
79#undef START_RELOC_NUMBERS
80#undef RELOC_NUMBER
81#undef FAKE_RELOC
82#undef EMPTY_RELOC
83#undef END_RELOC_NUMBERS
84#undef _RELOC_MACROS_H
85
86/* The following headers use the elf/reloc-macros.h file to
87   automatically generate relocation recognition functions
88   such as elf_mips_reloc_type()  */
89
90#define RELOC_MACROS_GEN_FUNC
91
92#include "elf/aarch64.h"
93#include "elf/alpha.h"
94#include "elf/arc.h"
95#include "elf/arm.h"
96#include "elf/avr.h"
97#include "elf/bfin.h"
98#include "elf/cr16.h"
99#include "elf/cris.h"
100#include "elf/crx.h"
101#include "elf/d10v.h"
102#include "elf/d30v.h"
103#include "elf/dlx.h"
104#include "elf/epiphany.h"
105#include "elf/fr30.h"
106#include "elf/frv.h"
107#include "elf/ft32.h"
108#include "elf/h8.h"
109#include "elf/hppa.h"
110#include "elf/i386.h"
111#include "elf/i370.h"
112#include "elf/i860.h"
113#include "elf/i960.h"
114#include "elf/ia64.h"
115#include "elf/ip2k.h"
116#include "elf/lm32.h"
117#include "elf/iq2000.h"
118#include "elf/m32c.h"
119#include "elf/m32r.h"
120#include "elf/m68k.h"
121#include "elf/m68hc11.h"
122#include "elf/mcore.h"
123#include "elf/mep.h"
124#include "elf/metag.h"
125#include "elf/microblaze.h"
126#include "elf/mips.h"
127#include "elf/riscv.h"
128#include "elf/mmix.h"
129#include "elf/mn10200.h"
130#include "elf/mn10300.h"
131#include "elf/moxie.h"
132#include "elf/mt.h"
133#include "elf/msp430.h"
134#include "elf/nds32.h"
135#include "elf/nios2.h"
136#include "elf/or1k.h"
137#include "elf/pj.h"
138#include "elf/ppc.h"
139#include "elf/ppc64.h"
140#include "elf/rl78.h"
141#include "elf/rx.h"
142#include "elf/s390.h"
143#include "elf/score.h"
144#include "elf/sh.h"
145#include "elf/sparc.h"
146#include "elf/spu.h"
147#include "elf/tic6x.h"
148#include "elf/tilegx.h"
149#include "elf/tilepro.h"
150#include "elf/v850.h"
151#include "elf/vax.h"
152#include "elf/visium.h"
153#include "elf/x86-64.h"
154#include "elf/xc16x.h"
155#include "elf/xgate.h"
156#include "elf/xstormy16.h"
157#include "elf/xtensa.h"
158
159#include "getopt.h"
160#include "libiberty.h"
161#include "safe-ctype.h"
162#include "filenames.h"
163
164#ifndef offsetof
165#define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
166#endif
167
168typedef struct elf_section_list
169{
170  Elf_Internal_Shdr * hdr;
171  struct elf_section_list * next;
172} elf_section_list;
173
174char * program_name = "readelf";
175static unsigned long archive_file_offset;
176static unsigned long archive_file_size;
177static bfd_size_type current_file_size;
178static unsigned long dynamic_addr;
179static bfd_size_type dynamic_size;
180static size_t dynamic_nent;
181static char * dynamic_strings;
182static unsigned long dynamic_strings_length;
183static char * string_table;
184static unsigned long string_table_length;
185static unsigned long num_dynamic_syms;
186static Elf_Internal_Sym * dynamic_symbols;
187static Elf_Internal_Syminfo * dynamic_syminfo;
188static unsigned long dynamic_syminfo_offset;
189static unsigned int dynamic_syminfo_nent;
190static char program_interpreter[PATH_MAX];
191static bfd_vma dynamic_info[DT_ENCODING];
192static bfd_vma dynamic_info_DT_GNU_HASH;
193static bfd_vma version_info[16];
194static Elf_Internal_Ehdr elf_header;
195static Elf_Internal_Shdr * section_headers;
196static Elf_Internal_Phdr * program_headers;
197static Elf_Internal_Dyn *  dynamic_section;
198static elf_section_list * symtab_shndx_list;
199static int show_name;
200static int do_special_files;
201static int do_dynamic;
202static int do_syms;
203static int do_dyn_syms;
204static int do_reloc;
205static int do_sections;
206static int do_section_groups;
207static int do_section_details;
208static int do_segments;
209static int do_unwind;
210static int do_using_dynamic;
211static int do_header;
212static int do_dump;
213static int do_version;
214static int do_histogram;
215static int do_debugging;
216static int do_arch;
217static int do_notes;
218static int do_archive_index;
219static int is_32bit_elf;
220static int decompress_dumps;
221
222struct group_list
223{
224  struct group_list * next;
225  unsigned int section_index;
226};
227
228struct group
229{
230  struct group_list * root;
231  unsigned int group_index;
232};
233
234static size_t group_count;
235static struct group * section_groups;
236static struct group ** section_headers_groups;
237
238
239/* Flag bits indicating particular types of dump.  */
240#define HEX_DUMP	(1 << 0)	/* The -x command line switch.  */
241#define DISASS_DUMP	(1 << 1)	/* The -i command line switch.  */
242#define DEBUG_DUMP	(1 << 2)	/* The -w command line switch.  */
243#define STRING_DUMP     (1 << 3)	/* The -p command line switch.  */
244#define RELOC_DUMP      (1 << 4)	/* The -R command line switch.  */
245
246typedef unsigned char dump_type;
247
248/* A linked list of the section names for which dumps were requested.  */
249struct dump_list_entry
250{
251  char * name;
252  dump_type type;
253  struct dump_list_entry * next;
254};
255static struct dump_list_entry * dump_sects_byname;
256
257/* A dynamic array of flags indicating for which sections a dump
258   has been requested via command line switches.  */
259static dump_type *   cmdline_dump_sects = NULL;
260static unsigned int  num_cmdline_dump_sects = 0;
261
262/* A dynamic array of flags indicating for which sections a dump of
263   some kind has been requested.  It is reset on a per-object file
264   basis and then initialised from the cmdline_dump_sects array,
265   the results of interpreting the -w switch, and the
266   dump_sects_byname list.  */
267static dump_type *   dump_sects = NULL;
268static unsigned int  num_dump_sects = 0;
269
270
271/* How to print a vma value.  */
272typedef enum print_mode
273{
274  HEX,
275  DEC,
276  DEC_5,
277  UNSIGNED,
278  PREFIX_HEX,
279  FULL_HEX,
280  LONG_HEX
281}
282print_mode;
283
284/* Versioned symbol info.  */
285enum versioned_symbol_info
286{
287  symbol_undefined,
288  symbol_hidden,
289  symbol_public
290};
291
292static const char *get_symbol_version_string
293  (FILE *file, int is_dynsym, const char *strtab,
294   unsigned long int strtab_size, unsigned int si,
295   Elf_Internal_Sym *psym, enum versioned_symbol_info *sym_info,
296   unsigned short *vna_other);
297
298#define UNKNOWN -1
299
300#define SECTION_NAME(X)						\
301  ((X) == NULL ? _("<none>")					\
302   : string_table == NULL ? _("<no-name>")			\
303   : ((X)->sh_name >= string_table_length ? _("<corrupt>")	\
304  : string_table + (X)->sh_name))
305
306#define DT_VERSIONTAGIDX(tag)	(DT_VERNEEDNUM - (tag))	/* Reverse order!  */
307
308#define GET_ELF_SYMBOLS(file, section, sym_count)			\
309  (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count)	\
310   : get_64bit_elf_symbols (file, section, sym_count))
311
312#define VALID_DYNAMIC_NAME(offset)	((dynamic_strings != NULL) && (offset < dynamic_strings_length))
313/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
314   already been called and verified that the string exists.  */
315#define GET_DYNAMIC_NAME(offset)	(dynamic_strings + offset)
316
317#define REMOVE_ARCH_BITS(ADDR)			\
318  do						\
319    {						\
320      if (elf_header.e_machine == EM_ARM)	\
321	(ADDR) &= ~1;				\
322    }						\
323  while (0)
324
325/* Retrieve NMEMB structures, each SIZE bytes long from FILE starting at OFFSET +
326   the offset of the current archive member, if we are examining an archive.
327   Put the retrieved data into VAR, if it is not NULL.  Otherwise allocate a buffer
328   using malloc and fill that.  In either case return the pointer to the start of
329   the retrieved data or NULL if something went wrong.  If something does go wrong
330   and REASON is not NULL then emit an error message using REASON as part of the
331   context.  */
332
333static void *
334get_data (void * var, FILE * file, unsigned long offset, bfd_size_type size,
335	  bfd_size_type nmemb, const char * reason)
336{
337  void * mvar;
338  bfd_size_type amt = size * nmemb;
339
340  if (size == 0 || nmemb == 0)
341    return NULL;
342
343  /* If the size_t type is smaller than the bfd_size_type, eg because
344     you are building a 32-bit tool on a 64-bit host, then make sure
345     that when the sizes are cast to (size_t) no information is lost.  */
346  if (sizeof (size_t) < sizeof (bfd_size_type)
347      && (   (bfd_size_type) ((size_t) size) != size
348	  || (bfd_size_type) ((size_t) nmemb) != nmemb))
349    {
350      if (reason)
351	error (_("Size truncation prevents reading 0x%" BFD_VMA_FMT "x"
352		 " elements of size 0x%" BFD_VMA_FMT "x for %s\n"),
353	       nmemb, size, reason);
354      return NULL;
355    }
356
357  /* Check for size overflow.  */
358  if (amt < nmemb)
359    {
360      if (reason)
361	error (_("Size overflow prevents reading 0x%" BFD_VMA_FMT "x"
362		 " elements of size 0x%" BFD_VMA_FMT "x for %s\n"),
363	       nmemb, size, reason);
364      return NULL;
365    }
366
367  /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
368     attempting to allocate memory when the read is bound to fail.  */
369  if (amt > current_file_size
370      || offset + archive_file_offset + amt > current_file_size)
371    {
372      if (reason)
373	error (_("Reading 0x%" BFD_VMA_FMT "x"
374		 " bytes extends past end of file for %s\n"),
375	       amt, reason);
376      return NULL;
377    }
378
379  if (fseek (file, archive_file_offset + offset, SEEK_SET))
380    {
381      if (reason)
382	error (_("Unable to seek to 0x%lx for %s\n"),
383	       archive_file_offset + offset, reason);
384      return NULL;
385    }
386
387  mvar = var;
388  if (mvar == NULL)
389    {
390      /* Check for overflow.  */
391      if (nmemb < (~(bfd_size_type) 0 - 1) / size)
392	/* + 1 so that we can '\0' terminate invalid string table sections.  */
393	mvar = malloc ((size_t) amt + 1);
394
395      if (mvar == NULL)
396	{
397	  if (reason)
398	    error (_("Out of memory allocating 0x%" BFD_VMA_FMT "x"
399		     " bytes for %s\n"),
400		   amt, reason);
401	  return NULL;
402	}
403
404      ((char *) mvar)[amt] = '\0';
405    }
406
407  if (fread (mvar, (size_t) size, (size_t) nmemb, file) != nmemb)
408    {
409      if (reason)
410	error (_("Unable to read in 0x%" BFD_VMA_FMT "x bytes of %s\n"),
411	       amt, reason);
412      if (mvar != var)
413	free (mvar);
414      return NULL;
415    }
416
417  return mvar;
418}
419
420/* Print a VMA value.  */
421
422static int
423print_vma (bfd_vma vma, print_mode mode)
424{
425  int nc = 0;
426
427  switch (mode)
428    {
429    case FULL_HEX:
430      nc = printf ("0x");
431      /* Drop through.  */
432
433    case LONG_HEX:
434#ifdef BFD64
435      if (is_32bit_elf)
436	return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
437#endif
438      printf_vma (vma);
439      return nc + 16;
440
441    case DEC_5:
442      if (vma <= 99999)
443	return printf ("%5" BFD_VMA_FMT "d", vma);
444      /* Drop through.  */
445
446    case PREFIX_HEX:
447      nc = printf ("0x");
448      /* Drop through.  */
449
450    case HEX:
451      return nc + printf ("%" BFD_VMA_FMT "x", vma);
452
453    case DEC:
454      return printf ("%" BFD_VMA_FMT "d", vma);
455
456    case UNSIGNED:
457      return printf ("%" BFD_VMA_FMT "u", vma);
458    }
459  return 0;
460}
461
462/* Display a symbol on stdout.  Handles the display of control characters and
463   multibye characters (assuming the host environment supports them).
464
465   Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
466
467   If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
468   padding as necessary.
469
470   Returns the number of emitted characters.  */
471
472static unsigned int
473print_symbol (int width, const char *symbol)
474{
475  bfd_boolean extra_padding = FALSE;
476  int num_printed = 0;
477#ifdef HAVE_MBSTATE_T
478  mbstate_t state;
479#endif
480  int width_remaining;
481
482  if (width < 0)
483    {
484      /* Keep the width positive.  This also helps.  */
485      width = - width;
486      extra_padding = TRUE;
487    }
488  assert (width != 0);
489
490  if (do_wide)
491    /* Set the remaining width to a very large value.
492       This simplifies the code below.  */
493    width_remaining = INT_MAX;
494  else
495    width_remaining = width;
496
497#ifdef HAVE_MBSTATE_T
498  /* Initialise the multibyte conversion state.  */
499  memset (& state, 0, sizeof (state));
500#endif
501
502  while (width_remaining)
503    {
504      size_t  n;
505      const char c = *symbol++;
506
507      if (c == 0)
508	break;
509
510      /* Do not print control characters directly as they can affect terminal
511	 settings.  Such characters usually appear in the names generated
512	 by the assembler for local labels.  */
513      if (ISCNTRL (c))
514	{
515	  if (width_remaining < 2)
516	    break;
517
518	  printf ("^%c", c + 0x40);
519	  width_remaining -= 2;
520	  num_printed += 2;
521	}
522      else if (ISPRINT (c))
523	{
524	  putchar (c);
525	  width_remaining --;
526	  num_printed ++;
527	}
528      else
529	{
530#ifdef HAVE_MBSTATE_T
531	  wchar_t w;
532#endif
533	  /* Let printf do the hard work of displaying multibyte characters.  */
534	  printf ("%.1s", symbol - 1);
535	  width_remaining --;
536	  num_printed ++;
537
538#ifdef HAVE_MBSTATE_T
539	  /* Try to find out how many bytes made up the character that was
540	     just printed.  Advance the symbol pointer past the bytes that
541	     were displayed.  */
542	  n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
543#else
544	  n = 1;
545#endif
546	  if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
547	    symbol += (n - 1);
548	}
549    }
550
551  if (extra_padding && num_printed < width)
552    {
553      /* Fill in the remaining spaces.  */
554      printf ("%-*s", width - num_printed, " ");
555      num_printed = width;
556    }
557
558  return num_printed;
559}
560
561/* Returns a pointer to a static buffer containing a  printable version of
562   the given section's name.  Like print_symbol, except that it does not try
563   to print multibyte characters, it just interprets them as hex values.  */
564
565static const char *
566printable_section_name (const Elf_Internal_Shdr * sec)
567{
568#define MAX_PRINT_SEC_NAME_LEN 128
569  static char  sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
570  const char * name = SECTION_NAME (sec);
571  char *       buf = sec_name_buf;
572  char         c;
573  unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
574
575  while ((c = * name ++) != 0)
576    {
577      if (ISCNTRL (c))
578	{
579	  if (remaining < 2)
580	    break;
581
582	  * buf ++ = '^';
583	  * buf ++ = c + 0x40;
584	  remaining -= 2;
585	}
586      else if (ISPRINT (c))
587	{
588	  * buf ++ = c;
589	  remaining -= 1;
590	}
591      else
592	{
593	  static char hex[17] = "0123456789ABCDEF";
594
595	  if (remaining < 4)
596	    break;
597	  * buf ++ = '<';
598	  * buf ++ = hex[(c & 0xf0) >> 4];
599	  * buf ++ = hex[c & 0x0f];
600	  * buf ++ = '>';
601	  remaining -= 4;
602	}
603
604      if (remaining == 0)
605	break;
606    }
607
608  * buf = 0;
609  return sec_name_buf;
610}
611
612static const char *
613printable_section_name_from_index (unsigned long ndx)
614{
615  if (ndx >= elf_header.e_shnum)
616    return _("<corrupt>");
617
618  return printable_section_name (section_headers + ndx);
619}
620
621/* Return a pointer to section NAME, or NULL if no such section exists.  */
622
623static Elf_Internal_Shdr *
624find_section (const char * name)
625{
626  unsigned int i;
627
628  for (i = 0; i < elf_header.e_shnum; i++)
629    if (streq (SECTION_NAME (section_headers + i), name))
630      return section_headers + i;
631
632  return NULL;
633}
634
635/* Return a pointer to a section containing ADDR, or NULL if no such
636   section exists.  */
637
638static Elf_Internal_Shdr *
639find_section_by_address (bfd_vma addr)
640{
641  unsigned int i;
642
643  for (i = 0; i < elf_header.e_shnum; i++)
644    {
645      Elf_Internal_Shdr *sec = section_headers + i;
646      if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
647	return sec;
648    }
649
650  return NULL;
651}
652
653static Elf_Internal_Shdr *
654find_section_by_type (unsigned int type)
655{
656  unsigned int i;
657
658  for (i = 0; i < elf_header.e_shnum; i++)
659    {
660      Elf_Internal_Shdr *sec = section_headers + i;
661      if (sec->sh_type == type)
662	return sec;
663    }
664
665  return NULL;
666}
667
668/* Return a pointer to section NAME, or NULL if no such section exists,
669   restricted to the list of sections given in SET.  */
670
671static Elf_Internal_Shdr *
672find_section_in_set (const char * name, unsigned int * set)
673{
674  unsigned int i;
675
676  if (set != NULL)
677    {
678      while ((i = *set++) > 0)
679	if (streq (SECTION_NAME (section_headers + i), name))
680	  return section_headers + i;
681    }
682
683  return find_section (name);
684}
685
686/* Read an unsigned LEB128 encoded value from p.  Set *PLEN to the number of
687   bytes read.  */
688
689static inline unsigned long
690read_uleb128 (unsigned char *data,
691	      unsigned int *length_return,
692	      const unsigned char * const end)
693{
694  return read_leb128 (data, length_return, FALSE, end);
695}
696
697/* Return true if the current file is for IA-64 machine and OpenVMS ABI.
698   This OS has so many departures from the ELF standard that we test it at
699   many places.  */
700
701static inline int
702is_ia64_vms (void)
703{
704  return elf_header.e_machine == EM_IA_64
705    && elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
706}
707
708/* Guess the relocation size commonly used by the specific machines.  */
709
710static int
711guess_is_rela (unsigned int e_machine)
712{
713  switch (e_machine)
714    {
715      /* Targets that use REL relocations.  */
716    case EM_386:
717    case EM_IAMCU:
718    case EM_960:
719    case EM_ARM:
720    case EM_D10V:
721    case EM_CYGNUS_D10V:
722    case EM_DLX:
723    case EM_MIPS:
724    case EM_MIPS_RS3_LE:
725    case EM_CYGNUS_M32R:
726    case EM_SCORE:
727    case EM_XGATE:
728      return FALSE;
729
730      /* Targets that use RELA relocations.  */
731    case EM_68K:
732    case EM_860:
733    case EM_AARCH64:
734    case EM_ADAPTEVA_EPIPHANY:
735    case EM_ALPHA:
736    case EM_ALTERA_NIOS2:
737    case EM_ARC:
738    case EM_ARC_COMPACT:
739    case EM_ARC_COMPACT2:
740    case EM_AVR:
741    case EM_AVR_OLD:
742    case EM_BLACKFIN:
743    case EM_CR16:
744    case EM_CRIS:
745    case EM_CRX:
746    case EM_D30V:
747    case EM_CYGNUS_D30V:
748    case EM_FR30:
749    case EM_FT32:
750    case EM_CYGNUS_FR30:
751    case EM_CYGNUS_FRV:
752    case EM_H8S:
753    case EM_H8_300:
754    case EM_H8_300H:
755    case EM_IA_64:
756    case EM_IP2K:
757    case EM_IP2K_OLD:
758    case EM_IQ2000:
759    case EM_LATTICEMICO32:
760    case EM_M32C_OLD:
761    case EM_M32C:
762    case EM_M32R:
763    case EM_MCORE:
764    case EM_CYGNUS_MEP:
765    case EM_METAG:
766    case EM_MMIX:
767    case EM_MN10200:
768    case EM_CYGNUS_MN10200:
769    case EM_MN10300:
770    case EM_CYGNUS_MN10300:
771    case EM_MOXIE:
772    case EM_MSP430:
773    case EM_MSP430_OLD:
774    case EM_MT:
775    case EM_NDS32:
776    case EM_NIOS32:
777    case EM_OR1K:
778    case EM_PPC64:
779    case EM_PPC:
780    case EM_RISCV:
781    case EM_RL78:
782    case EM_RX:
783    case EM_S390:
784    case EM_S390_OLD:
785    case EM_SH:
786    case EM_SPARC:
787    case EM_SPARC32PLUS:
788    case EM_SPARCV9:
789    case EM_SPU:
790    case EM_TI_C6000:
791    case EM_TILEGX:
792    case EM_TILEPRO:
793    case EM_V800:
794    case EM_V850:
795    case EM_CYGNUS_V850:
796    case EM_VAX:
797    case EM_VISIUM:
798    case EM_X86_64:
799    case EM_L1OM:
800    case EM_K1OM:
801    case EM_XSTORMY16:
802    case EM_XTENSA:
803    case EM_XTENSA_OLD:
804    case EM_MICROBLAZE:
805    case EM_MICROBLAZE_OLD:
806      return TRUE;
807
808    case EM_68HC05:
809    case EM_68HC08:
810    case EM_68HC11:
811    case EM_68HC16:
812    case EM_FX66:
813    case EM_ME16:
814    case EM_MMA:
815    case EM_NCPU:
816    case EM_NDR1:
817    case EM_PCP:
818    case EM_ST100:
819    case EM_ST19:
820    case EM_ST7:
821    case EM_ST9PLUS:
822    case EM_STARCORE:
823    case EM_SVX:
824    case EM_TINYJ:
825    default:
826      warn (_("Don't know about relocations on this machine architecture\n"));
827      return FALSE;
828    }
829}
830
831static int
832slurp_rela_relocs (FILE * file,
833		   unsigned long rel_offset,
834		   unsigned long rel_size,
835		   Elf_Internal_Rela ** relasp,
836		   unsigned long * nrelasp)
837{
838  Elf_Internal_Rela * relas;
839  size_t nrelas;
840  unsigned int i;
841
842  if (is_32bit_elf)
843    {
844      Elf32_External_Rela * erelas;
845
846      erelas = (Elf32_External_Rela *) get_data (NULL, file, rel_offset, 1,
847                                                 rel_size, _("32-bit relocation data"));
848      if (!erelas)
849	return 0;
850
851      nrelas = rel_size / sizeof (Elf32_External_Rela);
852
853      relas = (Elf_Internal_Rela *) cmalloc (nrelas,
854                                             sizeof (Elf_Internal_Rela));
855
856      if (relas == NULL)
857	{
858	  free (erelas);
859	  error (_("out of memory parsing relocs\n"));
860	  return 0;
861	}
862
863      for (i = 0; i < nrelas; i++)
864	{
865	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
866	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
867	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
868	}
869
870      free (erelas);
871    }
872  else
873    {
874      Elf64_External_Rela * erelas;
875
876      erelas = (Elf64_External_Rela *) get_data (NULL, file, rel_offset, 1,
877                                                 rel_size, _("64-bit relocation data"));
878      if (!erelas)
879	return 0;
880
881      nrelas = rel_size / sizeof (Elf64_External_Rela);
882
883      relas = (Elf_Internal_Rela *) cmalloc (nrelas,
884                                             sizeof (Elf_Internal_Rela));
885
886      if (relas == NULL)
887	{
888	  free (erelas);
889	  error (_("out of memory parsing relocs\n"));
890	  return 0;
891	}
892
893      for (i = 0; i < nrelas; i++)
894	{
895	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
896	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
897	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
898
899	  /* The #ifdef BFD64 below is to prevent a compile time
900	     warning.  We know that if we do not have a 64 bit data
901	     type that we will never execute this code anyway.  */
902#ifdef BFD64
903	  if (elf_header.e_machine == EM_MIPS
904	      && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
905	    {
906	      /* In little-endian objects, r_info isn't really a
907		 64-bit little-endian value: it has a 32-bit
908		 little-endian symbol index followed by four
909		 individual byte fields.  Reorder INFO
910		 accordingly.  */
911	      bfd_vma inf = relas[i].r_info;
912	      inf = (((inf & 0xffffffff) << 32)
913		      | ((inf >> 56) & 0xff)
914		      | ((inf >> 40) & 0xff00)
915		      | ((inf >> 24) & 0xff0000)
916		      | ((inf >> 8) & 0xff000000));
917	      relas[i].r_info = inf;
918	    }
919#endif /* BFD64 */
920	}
921
922      free (erelas);
923    }
924  *relasp = relas;
925  *nrelasp = nrelas;
926  return 1;
927}
928
929static int
930slurp_rel_relocs (FILE * file,
931		  unsigned long rel_offset,
932		  unsigned long rel_size,
933		  Elf_Internal_Rela ** relsp,
934		  unsigned long * nrelsp)
935{
936  Elf_Internal_Rela * rels;
937  size_t nrels;
938  unsigned int i;
939
940  if (is_32bit_elf)
941    {
942      Elf32_External_Rel * erels;
943
944      erels = (Elf32_External_Rel *) get_data (NULL, file, rel_offset, 1,
945                                               rel_size, _("32-bit relocation data"));
946      if (!erels)
947	return 0;
948
949      nrels = rel_size / sizeof (Elf32_External_Rel);
950
951      rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
952
953      if (rels == NULL)
954	{
955	  free (erels);
956	  error (_("out of memory parsing relocs\n"));
957	  return 0;
958	}
959
960      for (i = 0; i < nrels; i++)
961	{
962	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
963	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
964	  rels[i].r_addend = 0;
965	}
966
967      free (erels);
968    }
969  else
970    {
971      Elf64_External_Rel * erels;
972
973      erels = (Elf64_External_Rel *) get_data (NULL, file, rel_offset, 1,
974                                               rel_size, _("64-bit relocation data"));
975      if (!erels)
976	return 0;
977
978      nrels = rel_size / sizeof (Elf64_External_Rel);
979
980      rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
981
982      if (rels == NULL)
983	{
984	  free (erels);
985	  error (_("out of memory parsing relocs\n"));
986	  return 0;
987	}
988
989      for (i = 0; i < nrels; i++)
990	{
991	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
992	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
993	  rels[i].r_addend = 0;
994
995	  /* The #ifdef BFD64 below is to prevent a compile time
996	     warning.  We know that if we do not have a 64 bit data
997	     type that we will never execute this code anyway.  */
998#ifdef BFD64
999	  if (elf_header.e_machine == EM_MIPS
1000	      && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
1001	    {
1002	      /* In little-endian objects, r_info isn't really a
1003		 64-bit little-endian value: it has a 32-bit
1004		 little-endian symbol index followed by four
1005		 individual byte fields.  Reorder INFO
1006		 accordingly.  */
1007	      bfd_vma inf = rels[i].r_info;
1008	      inf = (((inf & 0xffffffff) << 32)
1009		     | ((inf >> 56) & 0xff)
1010		     | ((inf >> 40) & 0xff00)
1011		     | ((inf >> 24) & 0xff0000)
1012		     | ((inf >> 8) & 0xff000000));
1013	      rels[i].r_info = inf;
1014	    }
1015#endif /* BFD64 */
1016	}
1017
1018      free (erels);
1019    }
1020  *relsp = rels;
1021  *nrelsp = nrels;
1022  return 1;
1023}
1024
1025/* Returns the reloc type extracted from the reloc info field.  */
1026
1027static unsigned int
1028get_reloc_type (bfd_vma reloc_info)
1029{
1030  if (is_32bit_elf)
1031    return ELF32_R_TYPE (reloc_info);
1032
1033  switch (elf_header.e_machine)
1034    {
1035    case EM_MIPS:
1036      /* Note: We assume that reloc_info has already been adjusted for us.  */
1037      return ELF64_MIPS_R_TYPE (reloc_info);
1038
1039    case EM_SPARCV9:
1040      return ELF64_R_TYPE_ID (reloc_info);
1041
1042    default:
1043      return ELF64_R_TYPE (reloc_info);
1044    }
1045}
1046
1047/* Return the symbol index extracted from the reloc info field.  */
1048
1049static bfd_vma
1050get_reloc_symindex (bfd_vma reloc_info)
1051{
1052  return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1053}
1054
1055static inline bfd_boolean
1056uses_msp430x_relocs (void)
1057{
1058  return
1059    elf_header.e_machine == EM_MSP430 /* Paranoia.  */
1060    /* GCC uses osabi == ELFOSBI_STANDALONE.  */
1061    && (((elf_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1062	/* TI compiler uses ELFOSABI_NONE.  */
1063	|| (elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1064}
1065
1066/* Display the contents of the relocation data found at the specified
1067   offset.  */
1068
1069static void
1070dump_relocations (FILE * file,
1071		  unsigned long rel_offset,
1072		  unsigned long rel_size,
1073		  Elf_Internal_Sym * symtab,
1074		  unsigned long nsyms,
1075		  char * strtab,
1076		  unsigned long strtablen,
1077		  int is_rela,
1078		  int is_dynsym)
1079{
1080  unsigned int i;
1081  Elf_Internal_Rela * rels;
1082
1083  if (is_rela == UNKNOWN)
1084    is_rela = guess_is_rela (elf_header.e_machine);
1085
1086  if (is_rela)
1087    {
1088      if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
1089	return;
1090    }
1091  else
1092    {
1093      if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
1094	return;
1095    }
1096
1097  if (is_32bit_elf)
1098    {
1099      if (is_rela)
1100	{
1101	  if (do_wide)
1102	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name + Addend\n"));
1103	  else
1104	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name + Addend\n"));
1105	}
1106      else
1107	{
1108	  if (do_wide)
1109	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name\n"));
1110	  else
1111	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name\n"));
1112	}
1113    }
1114  else
1115    {
1116      if (is_rela)
1117	{
1118	  if (do_wide)
1119	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend\n"));
1120	  else
1121	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name + Addend\n"));
1122	}
1123      else
1124	{
1125	  if (do_wide)
1126	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name\n"));
1127	  else
1128	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name\n"));
1129	}
1130    }
1131
1132  for (i = 0; i < rel_size; i++)
1133    {
1134      const char * rtype;
1135      bfd_vma offset;
1136      bfd_vma inf;
1137      bfd_vma symtab_index;
1138      bfd_vma type;
1139
1140      offset = rels[i].r_offset;
1141      inf    = rels[i].r_info;
1142
1143      type = get_reloc_type (inf);
1144      symtab_index = get_reloc_symindex  (inf);
1145
1146      if (is_32bit_elf)
1147	{
1148	  printf ("%8.8lx  %8.8lx ",
1149		  (unsigned long) offset & 0xffffffff,
1150		  (unsigned long) inf & 0xffffffff);
1151	}
1152      else
1153	{
1154#if BFD_HOST_64BIT_LONG
1155	  printf (do_wide
1156		  ? "%16.16lx  %16.16lx "
1157		  : "%12.12lx  %12.12lx ",
1158		  offset, inf);
1159#elif BFD_HOST_64BIT_LONG_LONG
1160#ifndef __MSVCRT__
1161	  printf (do_wide
1162		  ? "%16.16llx  %16.16llx "
1163		  : "%12.12llx  %12.12llx ",
1164		  offset, inf);
1165#else
1166	  printf (do_wide
1167		  ? "%16.16I64x  %16.16I64x "
1168		  : "%12.12I64x  %12.12I64x ",
1169		  offset, inf);
1170#endif
1171#else
1172	  printf (do_wide
1173		  ? "%8.8lx%8.8lx  %8.8lx%8.8lx "
1174		  : "%4.4lx%8.8lx  %4.4lx%8.8lx ",
1175		  _bfd_int64_high (offset),
1176		  _bfd_int64_low (offset),
1177		  _bfd_int64_high (inf),
1178		  _bfd_int64_low (inf));
1179#endif
1180	}
1181
1182      switch (elf_header.e_machine)
1183	{
1184	default:
1185	  rtype = NULL;
1186	  break;
1187
1188	case EM_AARCH64:
1189	  rtype = elf_aarch64_reloc_type (type);
1190	  break;
1191
1192	case EM_M32R:
1193	case EM_CYGNUS_M32R:
1194	  rtype = elf_m32r_reloc_type (type);
1195	  break;
1196
1197	case EM_386:
1198	case EM_IAMCU:
1199	  rtype = elf_i386_reloc_type (type);
1200	  break;
1201
1202	case EM_68HC11:
1203	case EM_68HC12:
1204	  rtype = elf_m68hc11_reloc_type (type);
1205	  break;
1206
1207	case EM_68K:
1208	  rtype = elf_m68k_reloc_type (type);
1209	  break;
1210
1211	case EM_960:
1212	  rtype = elf_i960_reloc_type (type);
1213	  break;
1214
1215	case EM_AVR:
1216	case EM_AVR_OLD:
1217	  rtype = elf_avr_reloc_type (type);
1218	  break;
1219
1220	case EM_OLD_SPARCV9:
1221	case EM_SPARC32PLUS:
1222	case EM_SPARCV9:
1223	case EM_SPARC:
1224	  rtype = elf_sparc_reloc_type (type);
1225	  break;
1226
1227	case EM_SPU:
1228	  rtype = elf_spu_reloc_type (type);
1229	  break;
1230
1231	case EM_V800:
1232	  rtype = v800_reloc_type (type);
1233	  break;
1234	case EM_V850:
1235	case EM_CYGNUS_V850:
1236	  rtype = v850_reloc_type (type);
1237	  break;
1238
1239	case EM_D10V:
1240	case EM_CYGNUS_D10V:
1241	  rtype = elf_d10v_reloc_type (type);
1242	  break;
1243
1244	case EM_D30V:
1245	case EM_CYGNUS_D30V:
1246	  rtype = elf_d30v_reloc_type (type);
1247	  break;
1248
1249	case EM_DLX:
1250	  rtype = elf_dlx_reloc_type (type);
1251	  break;
1252
1253	case EM_SH:
1254	  rtype = elf_sh_reloc_type (type);
1255	  break;
1256
1257	case EM_MN10300:
1258	case EM_CYGNUS_MN10300:
1259	  rtype = elf_mn10300_reloc_type (type);
1260	  break;
1261
1262	case EM_MN10200:
1263	case EM_CYGNUS_MN10200:
1264	  rtype = elf_mn10200_reloc_type (type);
1265	  break;
1266
1267	case EM_FR30:
1268	case EM_CYGNUS_FR30:
1269	  rtype = elf_fr30_reloc_type (type);
1270	  break;
1271
1272	case EM_CYGNUS_FRV:
1273	  rtype = elf_frv_reloc_type (type);
1274	  break;
1275
1276	case EM_FT32:
1277	  rtype = elf_ft32_reloc_type (type);
1278	  break;
1279
1280	case EM_MCORE:
1281	  rtype = elf_mcore_reloc_type (type);
1282	  break;
1283
1284	case EM_MMIX:
1285	  rtype = elf_mmix_reloc_type (type);
1286	  break;
1287
1288	case EM_MOXIE:
1289	  rtype = elf_moxie_reloc_type (type);
1290	  break;
1291
1292	case EM_MSP430:
1293	  if (uses_msp430x_relocs ())
1294	    {
1295	      rtype = elf_msp430x_reloc_type (type);
1296	      break;
1297	    }
1298	case EM_MSP430_OLD:
1299	  rtype = elf_msp430_reloc_type (type);
1300	  break;
1301
1302	case EM_NDS32:
1303	  rtype = elf_nds32_reloc_type (type);
1304	  break;
1305
1306	case EM_PPC:
1307	  rtype = elf_ppc_reloc_type (type);
1308	  break;
1309
1310	case EM_PPC64:
1311	  rtype = elf_ppc64_reloc_type (type);
1312	  break;
1313
1314	case EM_MIPS:
1315	case EM_MIPS_RS3_LE:
1316	  rtype = elf_mips_reloc_type (type);
1317	  break;
1318
1319	case EM_ALPHA:
1320	  rtype = elf_alpha_reloc_type (type);
1321	  break;
1322
1323	case EM_ARM:
1324	  rtype = elf_arm_reloc_type (type);
1325	  break;
1326
1327	case EM_ARC:
1328	case EM_ARC_COMPACT:
1329	case EM_ARC_COMPACT2:
1330	  rtype = elf_arc_reloc_type (type);
1331	  break;
1332
1333	case EM_PARISC:
1334	  rtype = elf_hppa_reloc_type (type);
1335	  break;
1336
1337	case EM_H8_300:
1338	case EM_H8_300H:
1339	case EM_H8S:
1340	  rtype = elf_h8_reloc_type (type);
1341	  break;
1342
1343	case EM_OR1K:
1344	  rtype = elf_or1k_reloc_type (type);
1345	  break;
1346
1347	case EM_PJ:
1348	case EM_PJ_OLD:
1349	  rtype = elf_pj_reloc_type (type);
1350	  break;
1351	case EM_IA_64:
1352	  rtype = elf_ia64_reloc_type (type);
1353	  break;
1354
1355	case EM_CRIS:
1356	  rtype = elf_cris_reloc_type (type);
1357	  break;
1358
1359	case EM_860:
1360	  rtype = elf_i860_reloc_type (type);
1361	  break;
1362
1363	case EM_X86_64:
1364	case EM_L1OM:
1365	case EM_K1OM:
1366	  rtype = elf_x86_64_reloc_type (type);
1367	  break;
1368
1369	case EM_S370:
1370	  rtype = i370_reloc_type (type);
1371	  break;
1372
1373	case EM_S390_OLD:
1374	case EM_S390:
1375	  rtype = elf_s390_reloc_type (type);
1376	  break;
1377
1378	case EM_SCORE:
1379	  rtype = elf_score_reloc_type (type);
1380	  break;
1381
1382	case EM_XSTORMY16:
1383	  rtype = elf_xstormy16_reloc_type (type);
1384	  break;
1385
1386	case EM_CRX:
1387	  rtype = elf_crx_reloc_type (type);
1388	  break;
1389
1390	case EM_VAX:
1391	  rtype = elf_vax_reloc_type (type);
1392	  break;
1393
1394	case EM_VISIUM:
1395	  rtype = elf_visium_reloc_type (type);
1396	  break;
1397
1398	case EM_ADAPTEVA_EPIPHANY:
1399	  rtype = elf_epiphany_reloc_type (type);
1400	  break;
1401
1402	case EM_IP2K:
1403	case EM_IP2K_OLD:
1404	  rtype = elf_ip2k_reloc_type (type);
1405	  break;
1406
1407	case EM_IQ2000:
1408	  rtype = elf_iq2000_reloc_type (type);
1409	  break;
1410
1411	case EM_XTENSA_OLD:
1412	case EM_XTENSA:
1413	  rtype = elf_xtensa_reloc_type (type);
1414	  break;
1415
1416	case EM_LATTICEMICO32:
1417	  rtype = elf_lm32_reloc_type (type);
1418	  break;
1419
1420	case EM_M32C_OLD:
1421	case EM_M32C:
1422	  rtype = elf_m32c_reloc_type (type);
1423	  break;
1424
1425	case EM_MT:
1426	  rtype = elf_mt_reloc_type (type);
1427	  break;
1428
1429	case EM_BLACKFIN:
1430	  rtype = elf_bfin_reloc_type (type);
1431	  break;
1432
1433	case EM_CYGNUS_MEP:
1434	  rtype = elf_mep_reloc_type (type);
1435	  break;
1436
1437	case EM_CR16:
1438	  rtype = elf_cr16_reloc_type (type);
1439	  break;
1440
1441	case EM_MICROBLAZE:
1442	case EM_MICROBLAZE_OLD:
1443	  rtype = elf_microblaze_reloc_type (type);
1444	  break;
1445
1446	case EM_RISCV:
1447	  rtype = elf_riscv_reloc_type (type);
1448	  break;
1449
1450	case EM_RL78:
1451	  rtype = elf_rl78_reloc_type (type);
1452	  break;
1453
1454	case EM_RX:
1455	  rtype = elf_rx_reloc_type (type);
1456	  break;
1457
1458	case EM_METAG:
1459	  rtype = elf_metag_reloc_type (type);
1460	  break;
1461
1462	case EM_XC16X:
1463	case EM_C166:
1464	  rtype = elf_xc16x_reloc_type (type);
1465	  break;
1466
1467	case EM_TI_C6000:
1468	  rtype = elf_tic6x_reloc_type (type);
1469	  break;
1470
1471	case EM_TILEGX:
1472	  rtype = elf_tilegx_reloc_type (type);
1473	  break;
1474
1475	case EM_TILEPRO:
1476	  rtype = elf_tilepro_reloc_type (type);
1477	  break;
1478
1479	case EM_XGATE:
1480	  rtype = elf_xgate_reloc_type (type);
1481	  break;
1482
1483	case EM_ALTERA_NIOS2:
1484	  rtype = elf_nios2_reloc_type (type);
1485	  break;
1486	}
1487
1488      if (rtype == NULL)
1489	printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1490      else
1491	printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1492
1493      if (elf_header.e_machine == EM_ALPHA
1494	  && rtype != NULL
1495	  && streq (rtype, "R_ALPHA_LITUSE")
1496	  && is_rela)
1497	{
1498	  switch (rels[i].r_addend)
1499	    {
1500	    case LITUSE_ALPHA_ADDR:   rtype = "ADDR";   break;
1501	    case LITUSE_ALPHA_BASE:   rtype = "BASE";   break;
1502	    case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1503	    case LITUSE_ALPHA_JSR:    rtype = "JSR";    break;
1504	    case LITUSE_ALPHA_TLSGD:  rtype = "TLSGD";  break;
1505	    case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1506	    case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1507	    default: rtype = NULL;
1508	    }
1509	  if (rtype)
1510	    printf (" (%s)", rtype);
1511	  else
1512	    {
1513	      putchar (' ');
1514	      printf (_("<unknown addend: %lx>"),
1515		      (unsigned long) rels[i].r_addend);
1516	    }
1517	}
1518      else if (symtab_index)
1519	{
1520	  if (symtab == NULL || symtab_index >= nsyms)
1521	    printf (_(" bad symbol index: %08lx"), (unsigned long) symtab_index);
1522	  else
1523	    {
1524	      Elf_Internal_Sym * psym;
1525	      const char * version_string;
1526	      enum versioned_symbol_info sym_info;
1527	      unsigned short vna_other;
1528
1529	      psym = symtab + symtab_index;
1530
1531	      version_string
1532		= get_symbol_version_string (file, is_dynsym,
1533					     strtab, strtablen,
1534					     symtab_index,
1535					     psym,
1536					     &sym_info,
1537					     &vna_other);
1538
1539	      printf (" ");
1540
1541	      if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1542		{
1543		  const char * name;
1544		  unsigned int len;
1545		  unsigned int width = is_32bit_elf ? 8 : 14;
1546
1547		  /* Relocations against GNU_IFUNC symbols do not use the value
1548		     of the symbol as the address to relocate against.  Instead
1549		     they invoke the function named by the symbol and use its
1550		     result as the address for relocation.
1551
1552		     To indicate this to the user, do not display the value of
1553		     the symbol in the "Symbols's Value" field.  Instead show
1554		     its name followed by () as a hint that the symbol is
1555		     invoked.  */
1556
1557		  if (strtab == NULL
1558		      || psym->st_name == 0
1559		      || psym->st_name >= strtablen)
1560		    name = "??";
1561		  else
1562		    name = strtab + psym->st_name;
1563
1564		  len = print_symbol (width, name);
1565		  if (version_string)
1566		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
1567			    version_string);
1568		  printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1569		}
1570	      else
1571		{
1572		  print_vma (psym->st_value, LONG_HEX);
1573
1574		  printf (is_32bit_elf ? "   " : " ");
1575		}
1576
1577	      if (psym->st_name == 0)
1578		{
1579		  const char * sec_name = "<null>";
1580		  char name_buf[40];
1581
1582		  if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1583		    {
1584		      if (psym->st_shndx < elf_header.e_shnum)
1585			sec_name = SECTION_NAME (section_headers + psym->st_shndx);
1586		      else if (psym->st_shndx == SHN_ABS)
1587			sec_name = "ABS";
1588		      else if (psym->st_shndx == SHN_COMMON)
1589			sec_name = "COMMON";
1590		      else if ((elf_header.e_machine == EM_MIPS
1591				&& psym->st_shndx == SHN_MIPS_SCOMMON)
1592			       || (elf_header.e_machine == EM_TI_C6000
1593				   && psym->st_shndx == SHN_TIC6X_SCOMMON))
1594			sec_name = "SCOMMON";
1595		      else if (elf_header.e_machine == EM_MIPS
1596			       && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1597			sec_name = "SUNDEF";
1598		      else if ((elf_header.e_machine == EM_X86_64
1599				|| elf_header.e_machine == EM_L1OM
1600				|| elf_header.e_machine == EM_K1OM)
1601			       && psym->st_shndx == SHN_X86_64_LCOMMON)
1602			sec_name = "LARGE_COMMON";
1603		      else if (elf_header.e_machine == EM_IA_64
1604			       && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1605			       && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1606			sec_name = "ANSI_COM";
1607		      else if (is_ia64_vms ()
1608			       && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1609			sec_name = "VMS_SYMVEC";
1610		      else
1611			{
1612			  sprintf (name_buf, "<section 0x%x>",
1613				   (unsigned int) psym->st_shndx);
1614			  sec_name = name_buf;
1615			}
1616		    }
1617		  print_symbol (22, sec_name);
1618		}
1619	      else if (strtab == NULL)
1620		printf (_("<string table index: %3ld>"), psym->st_name);
1621	      else if (psym->st_name >= strtablen)
1622		printf (_("<corrupt string table index: %3ld>"), psym->st_name);
1623	      else
1624		{
1625		  print_symbol (22, strtab + psym->st_name);
1626		  if (version_string)
1627		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
1628			    version_string);
1629		}
1630
1631	      if (is_rela)
1632		{
1633		  bfd_vma off = rels[i].r_addend;
1634
1635		  if ((bfd_signed_vma) off < 0)
1636		    printf (" - %" BFD_VMA_FMT "x", - off);
1637		  else
1638		    printf (" + %" BFD_VMA_FMT "x", off);
1639		}
1640	    }
1641	}
1642      else if (is_rela)
1643	{
1644	  bfd_vma off = rels[i].r_addend;
1645
1646	  printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1647	  if ((bfd_signed_vma) off < 0)
1648	    printf ("-%" BFD_VMA_FMT "x", - off);
1649	  else
1650	    printf ("%" BFD_VMA_FMT "x", off);
1651	}
1652
1653      if (elf_header.e_machine == EM_SPARCV9
1654	  && rtype != NULL
1655	  && streq (rtype, "R_SPARC_OLO10"))
1656	printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1657
1658      putchar ('\n');
1659
1660#ifdef BFD64
1661      if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1662	{
1663	  bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1664	  bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1665	  const char * rtype2 = elf_mips_reloc_type (type2);
1666	  const char * rtype3 = elf_mips_reloc_type (type3);
1667
1668	  printf ("                    Type2: ");
1669
1670	  if (rtype2 == NULL)
1671	    printf (_("unrecognized: %-7lx"),
1672		    (unsigned long) type2 & 0xffffffff);
1673	  else
1674	    printf ("%-17.17s", rtype2);
1675
1676	  printf ("\n                    Type3: ");
1677
1678	  if (rtype3 == NULL)
1679	    printf (_("unrecognized: %-7lx"),
1680		    (unsigned long) type3 & 0xffffffff);
1681	  else
1682	    printf ("%-17.17s", rtype3);
1683
1684	  putchar ('\n');
1685	}
1686#endif /* BFD64 */
1687    }
1688
1689  free (rels);
1690}
1691
1692static const char *
1693get_mips_dynamic_type (unsigned long type)
1694{
1695  switch (type)
1696    {
1697    case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1698    case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1699    case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1700    case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1701    case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1702    case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1703    case DT_MIPS_MSYM: return "MIPS_MSYM";
1704    case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1705    case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1706    case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1707    case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1708    case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1709    case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1710    case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1711    case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1712    case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1713    case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1714    case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1715    case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1716    case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1717    case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1718    case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1719    case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1720    case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1721    case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1722    case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1723    case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1724    case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1725    case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1726    case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1727    case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1728    case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1729    case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1730    case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1731    case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1732    case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1733    case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1734    case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1735    case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1736    case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1737    case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1738    case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1739    case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1740    case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1741    case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1742    case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1743    default:
1744      return NULL;
1745    }
1746}
1747
1748static const char *
1749get_sparc64_dynamic_type (unsigned long type)
1750{
1751  switch (type)
1752    {
1753    case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1754    default:
1755      return NULL;
1756    }
1757}
1758
1759static const char *
1760get_ppc_dynamic_type (unsigned long type)
1761{
1762  switch (type)
1763    {
1764    case DT_PPC_GOT:    return "PPC_GOT";
1765    case DT_PPC_OPT:    return "PPC_OPT";
1766    default:
1767      return NULL;
1768    }
1769}
1770
1771static const char *
1772get_ppc64_dynamic_type (unsigned long type)
1773{
1774  switch (type)
1775    {
1776    case DT_PPC64_GLINK:  return "PPC64_GLINK";
1777    case DT_PPC64_OPD:    return "PPC64_OPD";
1778    case DT_PPC64_OPDSZ:  return "PPC64_OPDSZ";
1779    case DT_PPC64_OPT:    return "PPC64_OPT";
1780    default:
1781      return NULL;
1782    }
1783}
1784
1785static const char *
1786get_parisc_dynamic_type (unsigned long type)
1787{
1788  switch (type)
1789    {
1790    case DT_HP_LOAD_MAP:	return "HP_LOAD_MAP";
1791    case DT_HP_DLD_FLAGS:	return "HP_DLD_FLAGS";
1792    case DT_HP_DLD_HOOK:	return "HP_DLD_HOOK";
1793    case DT_HP_UX10_INIT:	return "HP_UX10_INIT";
1794    case DT_HP_UX10_INITSZ:	return "HP_UX10_INITSZ";
1795    case DT_HP_PREINIT:		return "HP_PREINIT";
1796    case DT_HP_PREINITSZ:	return "HP_PREINITSZ";
1797    case DT_HP_NEEDED:		return "HP_NEEDED";
1798    case DT_HP_TIME_STAMP:	return "HP_TIME_STAMP";
1799    case DT_HP_CHECKSUM:	return "HP_CHECKSUM";
1800    case DT_HP_GST_SIZE:	return "HP_GST_SIZE";
1801    case DT_HP_GST_VERSION:	return "HP_GST_VERSION";
1802    case DT_HP_GST_HASHVAL:	return "HP_GST_HASHVAL";
1803    case DT_HP_EPLTREL:		return "HP_GST_EPLTREL";
1804    case DT_HP_EPLTRELSZ:	return "HP_GST_EPLTRELSZ";
1805    case DT_HP_FILTERED:	return "HP_FILTERED";
1806    case DT_HP_FILTER_TLS:	return "HP_FILTER_TLS";
1807    case DT_HP_COMPAT_FILTERED:	return "HP_COMPAT_FILTERED";
1808    case DT_HP_LAZYLOAD:	return "HP_LAZYLOAD";
1809    case DT_HP_BIND_NOW_COUNT:	return "HP_BIND_NOW_COUNT";
1810    case DT_PLT:		return "PLT";
1811    case DT_PLT_SIZE:		return "PLT_SIZE";
1812    case DT_DLT:		return "DLT";
1813    case DT_DLT_SIZE:		return "DLT_SIZE";
1814    default:
1815      return NULL;
1816    }
1817}
1818
1819static const char *
1820get_ia64_dynamic_type (unsigned long type)
1821{
1822  switch (type)
1823    {
1824    case DT_IA_64_PLT_RESERVE:         return "IA_64_PLT_RESERVE";
1825    case DT_IA_64_VMS_SUBTYPE:         return "VMS_SUBTYPE";
1826    case DT_IA_64_VMS_IMGIOCNT:        return "VMS_IMGIOCNT";
1827    case DT_IA_64_VMS_LNKFLAGS:        return "VMS_LNKFLAGS";
1828    case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1829    case DT_IA_64_VMS_IDENT:           return "VMS_IDENT";
1830    case DT_IA_64_VMS_NEEDED_IDENT:    return "VMS_NEEDED_IDENT";
1831    case DT_IA_64_VMS_IMG_RELA_CNT:    return "VMS_IMG_RELA_CNT";
1832    case DT_IA_64_VMS_SEG_RELA_CNT:    return "VMS_SEG_RELA_CNT";
1833    case DT_IA_64_VMS_FIXUP_RELA_CNT:  return "VMS_FIXUP_RELA_CNT";
1834    case DT_IA_64_VMS_FIXUP_NEEDED:    return "VMS_FIXUP_NEEDED";
1835    case DT_IA_64_VMS_SYMVEC_CNT:      return "VMS_SYMVEC_CNT";
1836    case DT_IA_64_VMS_XLATED:          return "VMS_XLATED";
1837    case DT_IA_64_VMS_STACKSIZE:       return "VMS_STACKSIZE";
1838    case DT_IA_64_VMS_UNWINDSZ:        return "VMS_UNWINDSZ";
1839    case DT_IA_64_VMS_UNWIND_CODSEG:   return "VMS_UNWIND_CODSEG";
1840    case DT_IA_64_VMS_UNWIND_INFOSEG:  return "VMS_UNWIND_INFOSEG";
1841    case DT_IA_64_VMS_LINKTIME:        return "VMS_LINKTIME";
1842    case DT_IA_64_VMS_SEG_NO:          return "VMS_SEG_NO";
1843    case DT_IA_64_VMS_SYMVEC_OFFSET:   return "VMS_SYMVEC_OFFSET";
1844    case DT_IA_64_VMS_SYMVEC_SEG:      return "VMS_SYMVEC_SEG";
1845    case DT_IA_64_VMS_UNWIND_OFFSET:   return "VMS_UNWIND_OFFSET";
1846    case DT_IA_64_VMS_UNWIND_SEG:      return "VMS_UNWIND_SEG";
1847    case DT_IA_64_VMS_STRTAB_OFFSET:   return "VMS_STRTAB_OFFSET";
1848    case DT_IA_64_VMS_SYSVER_OFFSET:   return "VMS_SYSVER_OFFSET";
1849    case DT_IA_64_VMS_IMG_RELA_OFF:    return "VMS_IMG_RELA_OFF";
1850    case DT_IA_64_VMS_SEG_RELA_OFF:    return "VMS_SEG_RELA_OFF";
1851    case DT_IA_64_VMS_FIXUP_RELA_OFF:  return "VMS_FIXUP_RELA_OFF";
1852    case DT_IA_64_VMS_PLTGOT_OFFSET:   return "VMS_PLTGOT_OFFSET";
1853    case DT_IA_64_VMS_PLTGOT_SEG:      return "VMS_PLTGOT_SEG";
1854    case DT_IA_64_VMS_FPMODE:          return "VMS_FPMODE";
1855    default:
1856      return NULL;
1857    }
1858}
1859
1860static const char *
1861get_solaris_section_type (unsigned long type)
1862{
1863  switch (type)
1864    {
1865    case 0x6fffffee: return "SUNW_ancillary";
1866    case 0x6fffffef: return "SUNW_capchain";
1867    case 0x6ffffff0: return "SUNW_capinfo";
1868    case 0x6ffffff1: return "SUNW_symsort";
1869    case 0x6ffffff2: return "SUNW_tlssort";
1870    case 0x6ffffff3: return "SUNW_LDYNSYM";
1871    case 0x6ffffff4: return "SUNW_dof";
1872    case 0x6ffffff5: return "SUNW_cap";
1873    case 0x6ffffff6: return "SUNW_SIGNATURE";
1874    case 0x6ffffff7: return "SUNW_ANNOTATE";
1875    case 0x6ffffff8: return "SUNW_DEBUGSTR";
1876    case 0x6ffffff9: return "SUNW_DEBUG";
1877    case 0x6ffffffa: return "SUNW_move";
1878    case 0x6ffffffb: return "SUNW_COMDAT";
1879    case 0x6ffffffc: return "SUNW_syminfo";
1880    case 0x6ffffffd: return "SUNW_verdef";
1881    case 0x6ffffffe: return "SUNW_verneed";
1882    case 0x6fffffff: return "SUNW_versym";
1883    case 0x70000000: return "SPARC_GOTDATA";
1884    default: return NULL;
1885    }
1886}
1887
1888static const char *
1889get_alpha_dynamic_type (unsigned long type)
1890{
1891  switch (type)
1892    {
1893    case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1894    default:
1895      return NULL;
1896    }
1897}
1898
1899static const char *
1900get_score_dynamic_type (unsigned long type)
1901{
1902  switch (type)
1903    {
1904    case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
1905    case DT_SCORE_LOCAL_GOTNO:  return "SCORE_LOCAL_GOTNO";
1906    case DT_SCORE_SYMTABNO:     return "SCORE_SYMTABNO";
1907    case DT_SCORE_GOTSYM:       return "SCORE_GOTSYM";
1908    case DT_SCORE_UNREFEXTNO:   return "SCORE_UNREFEXTNO";
1909    case DT_SCORE_HIPAGENO:     return "SCORE_HIPAGENO";
1910    default:
1911      return NULL;
1912    }
1913}
1914
1915static const char *
1916get_tic6x_dynamic_type (unsigned long type)
1917{
1918  switch (type)
1919    {
1920    case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
1921    case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
1922    case DT_C6000_DSBT_BASE:   return "C6000_DSBT_BASE";
1923    case DT_C6000_DSBT_SIZE:   return "C6000_DSBT_SIZE";
1924    case DT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
1925    case DT_C6000_DSBT_INDEX:  return "C6000_DSBT_INDEX";
1926    default:
1927      return NULL;
1928    }
1929}
1930
1931static const char *
1932get_nios2_dynamic_type (unsigned long type)
1933{
1934  switch (type)
1935    {
1936    case DT_NIOS2_GP: return "NIOS2_GP";
1937    default:
1938      return NULL;
1939    }
1940}
1941
1942static const char *
1943get_solaris_dynamic_type (unsigned long type)
1944{
1945  switch (type)
1946    {
1947    case 0x6000000d: return "SUNW_AUXILIARY";
1948    case 0x6000000e: return "SUNW_RTLDINF";
1949    case 0x6000000f: return "SUNW_FILTER";
1950    case 0x60000010: return "SUNW_CAP";
1951    case 0x60000011: return "SUNW_SYMTAB";
1952    case 0x60000012: return "SUNW_SYMSZ";
1953    case 0x60000013: return "SUNW_SORTENT";
1954    case 0x60000014: return "SUNW_SYMSORT";
1955    case 0x60000015: return "SUNW_SYMSORTSZ";
1956    case 0x60000016: return "SUNW_TLSSORT";
1957    case 0x60000017: return "SUNW_TLSSORTSZ";
1958    case 0x60000018: return "SUNW_CAPINFO";
1959    case 0x60000019: return "SUNW_STRPAD";
1960    case 0x6000001a: return "SUNW_CAPCHAIN";
1961    case 0x6000001b: return "SUNW_LDMACH";
1962    case 0x6000001d: return "SUNW_CAPCHAINENT";
1963    case 0x6000001f: return "SUNW_CAPCHAINSZ";
1964    case 0x60000021: return "SUNW_PARENT";
1965    case 0x60000023: return "SUNW_ASLR";
1966    case 0x60000025: return "SUNW_RELAX";
1967    case 0x60000029: return "SUNW_NXHEAP";
1968    case 0x6000002b: return "SUNW_NXSTACK";
1969
1970    case 0x70000001: return "SPARC_REGISTER";
1971    case 0x7ffffffd: return "AUXILIARY";
1972    case 0x7ffffffe: return "USED";
1973    case 0x7fffffff: return "FILTER";
1974
1975    default: return NULL;
1976    }
1977}
1978
1979static const char *
1980get_dynamic_type (unsigned long type)
1981{
1982  static char buff[64];
1983
1984  switch (type)
1985    {
1986    case DT_NULL:	return "NULL";
1987    case DT_NEEDED:	return "NEEDED";
1988    case DT_PLTRELSZ:	return "PLTRELSZ";
1989    case DT_PLTGOT:	return "PLTGOT";
1990    case DT_HASH:	return "HASH";
1991    case DT_STRTAB:	return "STRTAB";
1992    case DT_SYMTAB:	return "SYMTAB";
1993    case DT_RELA:	return "RELA";
1994    case DT_RELASZ:	return "RELASZ";
1995    case DT_RELAENT:	return "RELAENT";
1996    case DT_STRSZ:	return "STRSZ";
1997    case DT_SYMENT:	return "SYMENT";
1998    case DT_INIT:	return "INIT";
1999    case DT_FINI:	return "FINI";
2000    case DT_SONAME:	return "SONAME";
2001    case DT_RPATH:	return "RPATH";
2002    case DT_SYMBOLIC:	return "SYMBOLIC";
2003    case DT_REL:	return "REL";
2004    case DT_RELSZ:	return "RELSZ";
2005    case DT_RELENT:	return "RELENT";
2006    case DT_PLTREL:	return "PLTREL";
2007    case DT_DEBUG:	return "DEBUG";
2008    case DT_TEXTREL:	return "TEXTREL";
2009    case DT_JMPREL:	return "JMPREL";
2010    case DT_BIND_NOW:   return "BIND_NOW";
2011    case DT_INIT_ARRAY: return "INIT_ARRAY";
2012    case DT_FINI_ARRAY: return "FINI_ARRAY";
2013    case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2014    case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2015    case DT_RUNPATH:    return "RUNPATH";
2016    case DT_FLAGS:      return "FLAGS";
2017
2018    case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2019    case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2020
2021    case DT_CHECKSUM:	return "CHECKSUM";
2022    case DT_PLTPADSZ:	return "PLTPADSZ";
2023    case DT_MOVEENT:	return "MOVEENT";
2024    case DT_MOVESZ:	return "MOVESZ";
2025    case DT_FEATURE:	return "FEATURE";
2026    case DT_POSFLAG_1:	return "POSFLAG_1";
2027    case DT_SYMINSZ:	return "SYMINSZ";
2028    case DT_SYMINENT:	return "SYMINENT"; /* aka VALRNGHI */
2029
2030    case DT_ADDRRNGLO:  return "ADDRRNGLO";
2031    case DT_CONFIG:	return "CONFIG";
2032    case DT_DEPAUDIT:	return "DEPAUDIT";
2033    case DT_AUDIT:	return "AUDIT";
2034    case DT_PLTPAD:	return "PLTPAD";
2035    case DT_MOVETAB:	return "MOVETAB";
2036    case DT_SYMINFO:	return "SYMINFO"; /* aka ADDRRNGHI */
2037
2038    case DT_VERSYM:	return "VERSYM";
2039
2040    case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2041    case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2042    case DT_RELACOUNT:	return "RELACOUNT";
2043    case DT_RELCOUNT:	return "RELCOUNT";
2044    case DT_FLAGS_1:	return "FLAGS_1";
2045    case DT_VERDEF:	return "VERDEF";
2046    case DT_VERDEFNUM:	return "VERDEFNUM";
2047    case DT_VERNEED:	return "VERNEED";
2048    case DT_VERNEEDNUM:	return "VERNEEDNUM";
2049
2050    case DT_AUXILIARY:	return "AUXILIARY";
2051    case DT_USED:	return "USED";
2052    case DT_FILTER:	return "FILTER";
2053
2054    case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2055    case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2056    case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2057    case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2058    case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2059    case DT_GNU_HASH:	return "GNU_HASH";
2060
2061    default:
2062      if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2063	{
2064	  const char * result;
2065
2066	  switch (elf_header.e_machine)
2067	    {
2068	    case EM_MIPS:
2069	    case EM_MIPS_RS3_LE:
2070	      result = get_mips_dynamic_type (type);
2071	      break;
2072	    case EM_SPARCV9:
2073	      result = get_sparc64_dynamic_type (type);
2074	      break;
2075	    case EM_PPC:
2076	      result = get_ppc_dynamic_type (type);
2077	      break;
2078	    case EM_PPC64:
2079	      result = get_ppc64_dynamic_type (type);
2080	      break;
2081	    case EM_IA_64:
2082	      result = get_ia64_dynamic_type (type);
2083	      break;
2084	    case EM_ALPHA:
2085	      result = get_alpha_dynamic_type (type);
2086	      break;
2087	    case EM_SCORE:
2088	      result = get_score_dynamic_type (type);
2089	      break;
2090	    case EM_TI_C6000:
2091	      result = get_tic6x_dynamic_type (type);
2092	      break;
2093	    case EM_ALTERA_NIOS2:
2094	      result = get_nios2_dynamic_type (type);
2095	      break;
2096	    default:
2097	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2098		result = get_solaris_dynamic_type (type);
2099	      else
2100		result = NULL;
2101	      break;
2102	    }
2103
2104	  if (result != NULL)
2105	    return result;
2106
2107	  snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2108	}
2109      else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2110	       || (elf_header.e_machine == EM_PARISC
2111		   && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2112	{
2113	  const char * result;
2114
2115	  switch (elf_header.e_machine)
2116	    {
2117	    case EM_PARISC:
2118	      result = get_parisc_dynamic_type (type);
2119	      break;
2120	    case EM_IA_64:
2121	      result = get_ia64_dynamic_type (type);
2122	      break;
2123	    default:
2124	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2125		result = get_solaris_dynamic_type (type);
2126	      else
2127		result = NULL;
2128	      break;
2129	    }
2130
2131	  if (result != NULL)
2132	    return result;
2133
2134	  snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2135		    type);
2136	}
2137      else
2138	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2139
2140      return buff;
2141    }
2142}
2143
2144static char *
2145get_file_type (unsigned e_type)
2146{
2147  static char buff[32];
2148
2149  switch (e_type)
2150    {
2151    case ET_NONE:	return _("NONE (None)");
2152    case ET_REL:	return _("REL (Relocatable file)");
2153    case ET_EXEC:	return _("EXEC (Executable file)");
2154    case ET_DYN:	return _("DYN (Shared object file)");
2155    case ET_CORE:	return _("CORE (Core file)");
2156
2157    default:
2158      if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2159	snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2160      else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2161	snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2162      else
2163	snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2164      return buff;
2165    }
2166}
2167
2168static char *
2169get_machine_name (unsigned e_machine)
2170{
2171  static char buff[64]; /* XXX */
2172
2173  switch (e_machine)
2174    {
2175    case EM_NONE:		return _("None");
2176    case EM_AARCH64:		return "AArch64";
2177    case EM_M32:		return "WE32100";
2178    case EM_SPARC:		return "Sparc";
2179    case EM_SPU:		return "SPU";
2180    case EM_386:		return "Intel 80386";
2181    case EM_68K:		return "MC68000";
2182    case EM_88K:		return "MC88000";
2183    case EM_IAMCU:		return "Intel MCU";
2184    case EM_860:		return "Intel 80860";
2185    case EM_MIPS:		return "MIPS R3000";
2186    case EM_S370:		return "IBM System/370";
2187    case EM_MIPS_RS3_LE:	return "MIPS R4000 big-endian";
2188    case EM_OLD_SPARCV9:	return "Sparc v9 (old)";
2189    case EM_PARISC:		return "HPPA";
2190    case EM_PPC_OLD:		return "Power PC (old)";
2191    case EM_SPARC32PLUS:	return "Sparc v8+" ;
2192    case EM_960:		return "Intel 90860";
2193    case EM_PPC:		return "PowerPC";
2194    case EM_PPC64:		return "PowerPC64";
2195    case EM_FR20:		return "Fujitsu FR20";
2196    case EM_FT32:		return "FTDI FT32";
2197    case EM_RH32:		return "TRW RH32";
2198    case EM_MCORE:		return "MCORE";
2199    case EM_ARM:		return "ARM";
2200    case EM_OLD_ALPHA:		return "Digital Alpha (old)";
2201    case EM_SH:			return "Renesas / SuperH SH";
2202    case EM_SPARCV9:		return "Sparc v9";
2203    case EM_TRICORE:		return "Siemens Tricore";
2204    case EM_ARC:		return "ARC";
2205    case EM_ARC_COMPACT:	return "ARCompact";
2206    case EM_ARC_COMPACT2:	return "ARCv2";
2207    case EM_H8_300:		return "Renesas H8/300";
2208    case EM_H8_300H:		return "Renesas H8/300H";
2209    case EM_H8S:		return "Renesas H8S";
2210    case EM_H8_500:		return "Renesas H8/500";
2211    case EM_IA_64:		return "Intel IA-64";
2212    case EM_MIPS_X:		return "Stanford MIPS-X";
2213    case EM_COLDFIRE:		return "Motorola Coldfire";
2214    case EM_ALPHA:		return "Alpha";
2215    case EM_CYGNUS_D10V:
2216    case EM_D10V:		return "d10v";
2217    case EM_CYGNUS_D30V:
2218    case EM_D30V:		return "d30v";
2219    case EM_CYGNUS_M32R:
2220    case EM_M32R:		return "Renesas M32R (formerly Mitsubishi M32r)";
2221    case EM_CYGNUS_V850:
2222    case EM_V800:		return "Renesas V850 (using RH850 ABI)";
2223    case EM_V850:		return "Renesas V850";
2224    case EM_CYGNUS_MN10300:
2225    case EM_MN10300:		return "mn10300";
2226    case EM_CYGNUS_MN10200:
2227    case EM_MN10200:		return "mn10200";
2228    case EM_MOXIE:		return "Moxie";
2229    case EM_CYGNUS_FR30:
2230    case EM_FR30:		return "Fujitsu FR30";
2231    case EM_CYGNUS_FRV:		return "Fujitsu FR-V";
2232    case EM_PJ_OLD:
2233    case EM_PJ:			return "picoJava";
2234    case EM_MMA:		return "Fujitsu Multimedia Accelerator";
2235    case EM_PCP:		return "Siemens PCP";
2236    case EM_NCPU:		return "Sony nCPU embedded RISC processor";
2237    case EM_NDR1:		return "Denso NDR1 microprocesspr";
2238    case EM_STARCORE:		return "Motorola Star*Core processor";
2239    case EM_ME16:		return "Toyota ME16 processor";
2240    case EM_ST100:		return "STMicroelectronics ST100 processor";
2241    case EM_TINYJ:		return "Advanced Logic Corp. TinyJ embedded processor";
2242    case EM_PDSP:		return "Sony DSP processor";
2243    case EM_PDP10:		return "Digital Equipment Corp. PDP-10";
2244    case EM_PDP11:		return "Digital Equipment Corp. PDP-11";
2245    case EM_FX66:		return "Siemens FX66 microcontroller";
2246    case EM_ST9PLUS:		return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2247    case EM_ST7:		return "STMicroelectronics ST7 8-bit microcontroller";
2248    case EM_68HC16:		return "Motorola MC68HC16 Microcontroller";
2249    case EM_68HC12:		return "Motorola MC68HC12 Microcontroller";
2250    case EM_68HC11:		return "Motorola MC68HC11 Microcontroller";
2251    case EM_68HC08:		return "Motorola MC68HC08 Microcontroller";
2252    case EM_68HC05:		return "Motorola MC68HC05 Microcontroller";
2253    case EM_SVX:		return "Silicon Graphics SVx";
2254    case EM_ST19:		return "STMicroelectronics ST19 8-bit microcontroller";
2255    case EM_VAX:		return "Digital VAX";
2256    case EM_VISIUM:		return "CDS VISIUMcore processor";
2257    case EM_AVR_OLD:
2258    case EM_AVR:		return "Atmel AVR 8-bit microcontroller";
2259    case EM_CRIS:		return "Axis Communications 32-bit embedded processor";
2260    case EM_JAVELIN:		return "Infineon Technologies 32-bit embedded cpu";
2261    case EM_FIREPATH:		return "Element 14 64-bit DSP processor";
2262    case EM_ZSP:		return "LSI Logic's 16-bit DSP processor";
2263    case EM_MMIX:		return "Donald Knuth's educational 64-bit processor";
2264    case EM_HUANY:		return "Harvard Universitys's machine-independent object format";
2265    case EM_PRISM:		return "Vitesse Prism";
2266    case EM_X86_64:		return "Advanced Micro Devices X86-64";
2267    case EM_L1OM:		return "Intel L1OM";
2268    case EM_K1OM:		return "Intel K1OM";
2269    case EM_S390_OLD:
2270    case EM_S390:		return "IBM S/390";
2271    case EM_SCORE:		return "SUNPLUS S+Core";
2272    case EM_XSTORMY16:		return "Sanyo XStormy16 CPU core";
2273    case EM_OR1K:		return "OpenRISC 1000";
2274    case EM_CRX:		return "National Semiconductor CRX microprocessor";
2275    case EM_ADAPTEVA_EPIPHANY:	return "Adapteva EPIPHANY";
2276    case EM_DLX:		return "OpenDLX";
2277    case EM_IP2K_OLD:
2278    case EM_IP2K:		return "Ubicom IP2xxx 8-bit microcontrollers";
2279    case EM_IQ2000:       	return "Vitesse IQ2000";
2280    case EM_XTENSA_OLD:
2281    case EM_XTENSA:		return "Tensilica Xtensa Processor";
2282    case EM_VIDEOCORE:		return "Alphamosaic VideoCore processor";
2283    case EM_TMM_GPP:		return "Thompson Multimedia General Purpose Processor";
2284    case EM_NS32K:		return "National Semiconductor 32000 series";
2285    case EM_TPC:		return "Tenor Network TPC processor";
2286    case EM_ST200:		return "STMicroelectronics ST200 microcontroller";
2287    case EM_MAX:		return "MAX Processor";
2288    case EM_CR:			return "National Semiconductor CompactRISC";
2289    case EM_F2MC16:		return "Fujitsu F2MC16";
2290    case EM_MSP430:		return "Texas Instruments msp430 microcontroller";
2291    case EM_LATTICEMICO32:	return "Lattice Mico32";
2292    case EM_M32C_OLD:
2293    case EM_M32C:	        return "Renesas M32c";
2294    case EM_MT:                 return "Morpho Techologies MT processor";
2295    case EM_BLACKFIN:		return "Analog Devices Blackfin";
2296    case EM_SE_C33:		return "S1C33 Family of Seiko Epson processors";
2297    case EM_SEP:		return "Sharp embedded microprocessor";
2298    case EM_ARCA:		return "Arca RISC microprocessor";
2299    case EM_UNICORE:		return "Unicore";
2300    case EM_EXCESS:		return "eXcess 16/32/64-bit configurable embedded CPU";
2301    case EM_DXP:		return "Icera Semiconductor Inc. Deep Execution Processor";
2302    case EM_NIOS32:		return "Altera Nios";
2303    case EM_ALTERA_NIOS2:	return "Altera Nios II";
2304    case EM_C166:
2305    case EM_XC16X:		return "Infineon Technologies xc16x";
2306    case EM_M16C:		return "Renesas M16C series microprocessors";
2307    case EM_DSPIC30F:		return "Microchip Technology dsPIC30F Digital Signal Controller";
2308    case EM_CE:			return "Freescale Communication Engine RISC core";
2309    case EM_TSK3000:		return "Altium TSK3000 core";
2310    case EM_RS08:		return "Freescale RS08 embedded processor";
2311    case EM_ECOG2:		return "Cyan Technology eCOG2 microprocessor";
2312    case EM_DSP24:		return "New Japan Radio (NJR) 24-bit DSP Processor";
2313    case EM_VIDEOCORE3:		return "Broadcom VideoCore III processor";
2314    case EM_SE_C17:		return "Seiko Epson C17 family";
2315    case EM_TI_C6000:		return "Texas Instruments TMS320C6000 DSP family";
2316    case EM_TI_C2000:		return "Texas Instruments TMS320C2000 DSP family";
2317    case EM_TI_C5500:		return "Texas Instruments TMS320C55x DSP family";
2318    case EM_MMDSP_PLUS:		return "STMicroelectronics 64bit VLIW Data Signal Processor";
2319    case EM_CYPRESS_M8C:	return "Cypress M8C microprocessor";
2320    case EM_R32C:		return "Renesas R32C series microprocessors";
2321    case EM_TRIMEDIA:		return "NXP Semiconductors TriMedia architecture family";
2322    case EM_QDSP6:		return "QUALCOMM DSP6 Processor";
2323    case EM_8051:		return "Intel 8051 and variants";
2324    case EM_STXP7X:		return "STMicroelectronics STxP7x family";
2325    case EM_NDS32:		return "Andes Technology compact code size embedded RISC processor family";
2326    case EM_ECOG1X:		return "Cyan Technology eCOG1X family";
2327    case EM_MAXQ30:		return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2328    case EM_XIMO16:		return "New Japan Radio (NJR) 16-bit DSP Processor";
2329    case EM_MANIK:		return "M2000 Reconfigurable RISC Microprocessor";
2330    case EM_CRAYNV2:		return "Cray Inc. NV2 vector architecture";
2331    case EM_CYGNUS_MEP:         return "Toshiba MeP Media Engine";
2332    case EM_CR16:
2333    case EM_MICROBLAZE:
2334    case EM_MICROBLAZE_OLD:	return "Xilinx MicroBlaze";
2335    case EM_RISCV:		return "RISC-V";
2336    case EM_RL78:		return "Renesas RL78";
2337    case EM_RX:			return "Renesas RX";
2338    case EM_METAG:		return "Imagination Technologies Meta processor architecture";
2339    case EM_MCST_ELBRUS:	return "MCST Elbrus general purpose hardware architecture";
2340    case EM_ECOG16:		return "Cyan Technology eCOG16 family";
2341    case EM_ETPU:		return "Freescale Extended Time Processing Unit";
2342    case EM_SLE9X:		return "Infineon Technologies SLE9X core";
2343    case EM_AVR32:		return "Atmel Corporation 32-bit microprocessor family";
2344    case EM_STM8:		return "STMicroeletronics STM8 8-bit microcontroller";
2345    case EM_TILE64:		return "Tilera TILE64 multicore architecture family";
2346    case EM_TILEPRO:		return "Tilera TILEPro multicore architecture family";
2347    case EM_TILEGX:		return "Tilera TILE-Gx multicore architecture family";
2348    case EM_CUDA:		return "NVIDIA CUDA architecture";
2349    case EM_XGATE:		return "Motorola XGATE embedded processor";
2350    default:
2351      snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2352      return buff;
2353    }
2354}
2355
2356static void
2357decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2358{
2359  /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2.  Some
2360     other compilers don't a specific architecture type in the e_flags, and
2361     instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2362     architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2363     architectures.
2364
2365     Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2366     but also sets a specific architecture type in the e_flags field.
2367
2368     However, when decoding the flags we don't worry if we see an
2369     unexpected pairing, for example EM_ARC_COMPACT machine type, with
2370     ARCEM architecture type.  */
2371
2372  switch (e_flags & EF_ARC_MACH_MSK)
2373    {
2374      /* We only expect these to occur for EM_ARC_COMPACT2.  */
2375    case EF_ARC_CPU_ARCV2EM:
2376      strcat (buf, ", ARC EM");
2377      break;
2378    case EF_ARC_CPU_ARCV2HS:
2379      strcat (buf, ", ARC HS");
2380      break;
2381
2382      /* We only expect these to occur for EM_ARC_COMPACT.  */
2383    case E_ARC_MACH_ARC600:
2384      strcat (buf, ", ARC600");
2385      break;
2386    case E_ARC_MACH_ARC601:
2387      strcat (buf, ", ARC601");
2388      break;
2389    case E_ARC_MACH_ARC700:
2390      strcat (buf, ", ARC700");
2391      break;
2392
2393      /* The only times we should end up here are (a) A corrupt ELF, (b) A
2394         new ELF with new architecture being read by an old version of
2395         readelf, or (c) An ELF built with non-GNU compiler that does not
2396         set the architecture in the e_flags.  */
2397    default:
2398      if (e_machine == EM_ARC_COMPACT)
2399        strcat (buf, ", Unknown ARCompact");
2400      else
2401        strcat (buf, ", Unknown ARC");
2402      break;
2403    }
2404
2405  switch (e_flags & EF_ARC_OSABI_MSK)
2406    {
2407    case E_ARC_OSABI_ORIG:
2408      strcat (buf, ", (ABI:legacy)");
2409      break;
2410    case E_ARC_OSABI_V2:
2411      strcat (buf, ", (ABI:v2)");
2412      break;
2413      /* Only upstream 3.9+ kernels will support ARCv2 ISA.  */
2414    case E_ARC_OSABI_V3:
2415      strcat (buf, ", v3 no-legacy-syscalls ABI");
2416      break;
2417    default:
2418      strcat (buf, ", unrecognised ARC OSABI flag");
2419      break;
2420    }
2421}
2422
2423static void
2424decode_ARM_machine_flags (unsigned e_flags, char buf[])
2425{
2426  unsigned eabi;
2427  int unknown = 0;
2428
2429  eabi = EF_ARM_EABI_VERSION (e_flags);
2430  e_flags &= ~ EF_ARM_EABIMASK;
2431
2432  /* Handle "generic" ARM flags.  */
2433  if (e_flags & EF_ARM_RELEXEC)
2434    {
2435      strcat (buf, ", relocatable executable");
2436      e_flags &= ~ EF_ARM_RELEXEC;
2437    }
2438
2439  /* Now handle EABI specific flags.  */
2440  switch (eabi)
2441    {
2442    default:
2443      strcat (buf, ", <unrecognized EABI>");
2444      if (e_flags)
2445	unknown = 1;
2446      break;
2447
2448    case EF_ARM_EABI_VER1:
2449      strcat (buf, ", Version1 EABI");
2450      while (e_flags)
2451	{
2452	  unsigned flag;
2453
2454	  /* Process flags one bit at a time.  */
2455	  flag = e_flags & - e_flags;
2456	  e_flags &= ~ flag;
2457
2458	  switch (flag)
2459	    {
2460	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2461	      strcat (buf, ", sorted symbol tables");
2462	      break;
2463
2464	    default:
2465	      unknown = 1;
2466	      break;
2467	    }
2468	}
2469      break;
2470
2471    case EF_ARM_EABI_VER2:
2472      strcat (buf, ", Version2 EABI");
2473      while (e_flags)
2474	{
2475	  unsigned flag;
2476
2477	  /* Process flags one bit at a time.  */
2478	  flag = e_flags & - e_flags;
2479	  e_flags &= ~ flag;
2480
2481	  switch (flag)
2482	    {
2483	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2484	      strcat (buf, ", sorted symbol tables");
2485	      break;
2486
2487	    case EF_ARM_DYNSYMSUSESEGIDX:
2488	      strcat (buf, ", dynamic symbols use segment index");
2489	      break;
2490
2491	    case EF_ARM_MAPSYMSFIRST:
2492	      strcat (buf, ", mapping symbols precede others");
2493	      break;
2494
2495	    default:
2496	      unknown = 1;
2497	      break;
2498	    }
2499	}
2500      break;
2501
2502    case EF_ARM_EABI_VER3:
2503      strcat (buf, ", Version3 EABI");
2504      break;
2505
2506    case EF_ARM_EABI_VER4:
2507      strcat (buf, ", Version4 EABI");
2508      while (e_flags)
2509	{
2510	  unsigned flag;
2511
2512	  /* Process flags one bit at a time.  */
2513	  flag = e_flags & - e_flags;
2514	  e_flags &= ~ flag;
2515
2516	  switch (flag)
2517	    {
2518	    case EF_ARM_BE8:
2519	      strcat (buf, ", BE8");
2520	      break;
2521
2522	    case EF_ARM_LE8:
2523	      strcat (buf, ", LE8");
2524	      break;
2525
2526	    default:
2527	      unknown = 1;
2528	      break;
2529	    }
2530      break;
2531	}
2532      break;
2533
2534    case EF_ARM_EABI_VER5:
2535      strcat (buf, ", Version5 EABI");
2536      while (e_flags)
2537	{
2538	  unsigned flag;
2539
2540	  /* Process flags one bit at a time.  */
2541	  flag = e_flags & - e_flags;
2542	  e_flags &= ~ flag;
2543
2544	  switch (flag)
2545	    {
2546	    case EF_ARM_BE8:
2547	      strcat (buf, ", BE8");
2548	      break;
2549
2550	    case EF_ARM_LE8:
2551	      strcat (buf, ", LE8");
2552	      break;
2553
2554	    case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT.  */
2555	      strcat (buf, ", soft-float ABI");
2556	      break;
2557
2558	    case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT.  */
2559	      strcat (buf, ", hard-float ABI");
2560	      break;
2561
2562	    default:
2563	      unknown = 1;
2564	      break;
2565	    }
2566	}
2567      break;
2568
2569    case EF_ARM_EABI_UNKNOWN:
2570      strcat (buf, ", GNU EABI");
2571      while (e_flags)
2572	{
2573	  unsigned flag;
2574
2575	  /* Process flags one bit at a time.  */
2576	  flag = e_flags & - e_flags;
2577	  e_flags &= ~ flag;
2578
2579	  switch (flag)
2580	    {
2581	    case EF_ARM_INTERWORK:
2582	      strcat (buf, ", interworking enabled");
2583	      break;
2584
2585	    case EF_ARM_APCS_26:
2586	      strcat (buf, ", uses APCS/26");
2587	      break;
2588
2589	    case EF_ARM_APCS_FLOAT:
2590	      strcat (buf, ", uses APCS/float");
2591	      break;
2592
2593	    case EF_ARM_PIC:
2594	      strcat (buf, ", position independent");
2595	      break;
2596
2597	    case EF_ARM_ALIGN8:
2598	      strcat (buf, ", 8 bit structure alignment");
2599	      break;
2600
2601	    case EF_ARM_NEW_ABI:
2602	      strcat (buf, ", uses new ABI");
2603	      break;
2604
2605	    case EF_ARM_OLD_ABI:
2606	      strcat (buf, ", uses old ABI");
2607	      break;
2608
2609	    case EF_ARM_SOFT_FLOAT:
2610	      strcat (buf, ", software FP");
2611	      break;
2612
2613	    case EF_ARM_VFP_FLOAT:
2614	      strcat (buf, ", VFP");
2615	      break;
2616
2617	    case EF_ARM_MAVERICK_FLOAT:
2618	      strcat (buf, ", Maverick FP");
2619	      break;
2620
2621	    default:
2622	      unknown = 1;
2623	      break;
2624	    }
2625	}
2626    }
2627
2628  if (unknown)
2629    strcat (buf,_(", <unknown>"));
2630}
2631
2632static void
2633decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
2634{
2635  --size; /* Leave space for null terminator.  */
2636
2637  switch (e_flags & EF_AVR_MACH)
2638    {
2639    case E_AVR_MACH_AVR1:
2640      strncat (buf, ", avr:1", size);
2641      break;
2642    case E_AVR_MACH_AVR2:
2643      strncat (buf, ", avr:2", size);
2644      break;
2645    case E_AVR_MACH_AVR25:
2646      strncat (buf, ", avr:25", size);
2647      break;
2648    case E_AVR_MACH_AVR3:
2649      strncat (buf, ", avr:3", size);
2650      break;
2651    case E_AVR_MACH_AVR31:
2652      strncat (buf, ", avr:31", size);
2653      break;
2654    case E_AVR_MACH_AVR35:
2655      strncat (buf, ", avr:35", size);
2656      break;
2657    case E_AVR_MACH_AVR4:
2658      strncat (buf, ", avr:4", size);
2659      break;
2660    case E_AVR_MACH_AVR5:
2661      strncat (buf, ", avr:5", size);
2662      break;
2663    case E_AVR_MACH_AVR51:
2664      strncat (buf, ", avr:51", size);
2665      break;
2666    case E_AVR_MACH_AVR6:
2667      strncat (buf, ", avr:6", size);
2668      break;
2669    case E_AVR_MACH_AVRTINY:
2670      strncat (buf, ", avr:100", size);
2671      break;
2672    case E_AVR_MACH_XMEGA1:
2673      strncat (buf, ", avr:101", size);
2674      break;
2675    case E_AVR_MACH_XMEGA2:
2676      strncat (buf, ", avr:102", size);
2677      break;
2678    case E_AVR_MACH_XMEGA3:
2679      strncat (buf, ", avr:103", size);
2680      break;
2681    case E_AVR_MACH_XMEGA4:
2682      strncat (buf, ", avr:104", size);
2683      break;
2684    case E_AVR_MACH_XMEGA5:
2685      strncat (buf, ", avr:105", size);
2686      break;
2687    case E_AVR_MACH_XMEGA6:
2688      strncat (buf, ", avr:106", size);
2689      break;
2690    case E_AVR_MACH_XMEGA7:
2691      strncat (buf, ", avr:107", size);
2692      break;
2693    default:
2694      strncat (buf, ", avr:<unknown>", size);
2695      break;
2696    }
2697
2698  size -= strlen (buf);
2699  if (e_flags & EF_AVR_LINKRELAX_PREPARED)
2700    strncat (buf, ", link-relax", size);
2701}
2702
2703static void
2704decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2705{
2706  unsigned abi;
2707  unsigned arch;
2708  unsigned config;
2709  unsigned version;
2710  int has_fpu = 0;
2711  int r = 0;
2712
2713  static const char *ABI_STRINGS[] =
2714  {
2715    "ABI v0", /* use r5 as return register; only used in N1213HC */
2716    "ABI v1", /* use r0 as return register */
2717    "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2718    "ABI v2fp", /* for FPU */
2719    "AABI",
2720    "ABI2 FP+"
2721  };
2722  static const char *VER_STRINGS[] =
2723  {
2724    "Andes ELF V1.3 or older",
2725    "Andes ELF V1.3.1",
2726    "Andes ELF V1.4"
2727  };
2728  static const char *ARCH_STRINGS[] =
2729  {
2730    "",
2731    "Andes Star v1.0",
2732    "Andes Star v2.0",
2733    "Andes Star v3.0",
2734    "Andes Star v3.0m"
2735  };
2736
2737  abi = EF_NDS_ABI & e_flags;
2738  arch = EF_NDS_ARCH & e_flags;
2739  config = EF_NDS_INST & e_flags;
2740  version = EF_NDS32_ELF_VERSION & e_flags;
2741
2742  memset (buf, 0, size);
2743
2744  switch (abi)
2745    {
2746    case E_NDS_ABI_V0:
2747    case E_NDS_ABI_V1:
2748    case E_NDS_ABI_V2:
2749    case E_NDS_ABI_V2FP:
2750    case E_NDS_ABI_AABI:
2751    case E_NDS_ABI_V2FP_PLUS:
2752      /* In case there are holes in the array.  */
2753      r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
2754      break;
2755
2756    default:
2757      r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
2758      break;
2759    }
2760
2761  switch (version)
2762    {
2763    case E_NDS32_ELF_VER_1_2:
2764    case E_NDS32_ELF_VER_1_3:
2765    case E_NDS32_ELF_VER_1_4:
2766      r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
2767      break;
2768
2769    default:
2770      r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
2771      break;
2772    }
2773
2774  if (E_NDS_ABI_V0 == abi)
2775    {
2776      /* OLD ABI; only used in N1213HC, has performance extension 1.  */
2777      r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
2778      if (arch == E_NDS_ARCH_STAR_V1_0)
2779	r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
2780      return;
2781    }
2782
2783  switch (arch)
2784    {
2785    case E_NDS_ARCH_STAR_V1_0:
2786    case E_NDS_ARCH_STAR_V2_0:
2787    case E_NDS_ARCH_STAR_V3_0:
2788    case E_NDS_ARCH_STAR_V3_M:
2789      r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
2790      break;
2791
2792    default:
2793      r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
2794      /* ARCH version determines how the e_flags are interpreted.
2795	 If it is unknown, we cannot proceed.  */
2796      return;
2797    }
2798
2799  /* Newer ABI; Now handle architecture specific flags.  */
2800  if (arch == E_NDS_ARCH_STAR_V1_0)
2801    {
2802      if (config & E_NDS32_HAS_MFUSR_PC_INST)
2803	r += snprintf (buf + r, size -r, ", MFUSR_PC");
2804
2805      if (!(config & E_NDS32_HAS_NO_MAC_INST))
2806	r += snprintf (buf + r, size -r, ", MAC");
2807
2808      if (config & E_NDS32_HAS_DIV_INST)
2809	r += snprintf (buf + r, size -r, ", DIV");
2810
2811      if (config & E_NDS32_HAS_16BIT_INST)
2812	r += snprintf (buf + r, size -r, ", 16b");
2813    }
2814  else
2815    {
2816      if (config & E_NDS32_HAS_MFUSR_PC_INST)
2817	{
2818	  if (version <= E_NDS32_ELF_VER_1_3)
2819	    r += snprintf (buf + r, size -r, ", [B8]");
2820	  else
2821	    r += snprintf (buf + r, size -r, ", EX9");
2822	}
2823
2824      if (config & E_NDS32_HAS_MAC_DX_INST)
2825	r += snprintf (buf + r, size -r, ", MAC_DX");
2826
2827      if (config & E_NDS32_HAS_DIV_DX_INST)
2828	r += snprintf (buf + r, size -r, ", DIV_DX");
2829
2830      if (config & E_NDS32_HAS_16BIT_INST)
2831	{
2832	  if (version <= E_NDS32_ELF_VER_1_3)
2833	    r += snprintf (buf + r, size -r, ", 16b");
2834	  else
2835	    r += snprintf (buf + r, size -r, ", IFC");
2836	}
2837    }
2838
2839  if (config & E_NDS32_HAS_EXT_INST)
2840    r += snprintf (buf + r, size -r, ", PERF1");
2841
2842  if (config & E_NDS32_HAS_EXT2_INST)
2843    r += snprintf (buf + r, size -r, ", PERF2");
2844
2845  if (config & E_NDS32_HAS_FPU_INST)
2846    {
2847      has_fpu = 1;
2848      r += snprintf (buf + r, size -r, ", FPU_SP");
2849    }
2850
2851  if (config & E_NDS32_HAS_FPU_DP_INST)
2852    {
2853      has_fpu = 1;
2854      r += snprintf (buf + r, size -r, ", FPU_DP");
2855    }
2856
2857  if (config & E_NDS32_HAS_FPU_MAC_INST)
2858    {
2859      has_fpu = 1;
2860      r += snprintf (buf + r, size -r, ", FPU_MAC");
2861    }
2862
2863  if (has_fpu)
2864    {
2865      switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
2866	{
2867	case E_NDS32_FPU_REG_8SP_4DP:
2868	  r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
2869	  break;
2870	case E_NDS32_FPU_REG_16SP_8DP:
2871	  r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
2872	  break;
2873	case E_NDS32_FPU_REG_32SP_16DP:
2874	  r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
2875	  break;
2876	case E_NDS32_FPU_REG_32SP_32DP:
2877	  r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
2878	  break;
2879	}
2880    }
2881
2882  if (config & E_NDS32_HAS_AUDIO_INST)
2883    r += snprintf (buf + r, size -r, ", AUDIO");
2884
2885  if (config & E_NDS32_HAS_STRING_INST)
2886    r += snprintf (buf + r, size -r, ", STR");
2887
2888  if (config & E_NDS32_HAS_REDUCED_REGS)
2889    r += snprintf (buf + r, size -r, ", 16REG");
2890
2891  if (config & E_NDS32_HAS_VIDEO_INST)
2892    {
2893      if (version <= E_NDS32_ELF_VER_1_3)
2894	r += snprintf (buf + r, size -r, ", VIDEO");
2895      else
2896	r += snprintf (buf + r, size -r, ", SATURATION");
2897    }
2898
2899  if (config & E_NDS32_HAS_ENCRIPT_INST)
2900    r += snprintf (buf + r, size -r, ", ENCRP");
2901
2902  if (config & E_NDS32_HAS_L2C_INST)
2903    r += snprintf (buf + r, size -r, ", L2C");
2904}
2905
2906static char *
2907get_machine_flags (unsigned e_flags, unsigned e_machine)
2908{
2909  static char buf[1024];
2910
2911  buf[0] = '\0';
2912
2913  if (e_flags)
2914    {
2915      switch (e_machine)
2916	{
2917	default:
2918	  break;
2919
2920	case EM_ARC_COMPACT2:
2921	case EM_ARC_COMPACT:
2922          decode_ARC_machine_flags (e_flags, e_machine, buf);
2923          break;
2924
2925	case EM_ARM:
2926	  decode_ARM_machine_flags (e_flags, buf);
2927	  break;
2928
2929        case EM_AVR:
2930          decode_AVR_machine_flags (e_flags, buf, sizeof buf);
2931          break;
2932
2933	case EM_BLACKFIN:
2934	  if (e_flags & EF_BFIN_PIC)
2935	    strcat (buf, ", PIC");
2936
2937	  if (e_flags & EF_BFIN_FDPIC)
2938	    strcat (buf, ", FDPIC");
2939
2940	  if (e_flags & EF_BFIN_CODE_IN_L1)
2941	    strcat (buf, ", code in L1");
2942
2943	  if (e_flags & EF_BFIN_DATA_IN_L1)
2944	    strcat (buf, ", data in L1");
2945
2946	  break;
2947
2948	case EM_CYGNUS_FRV:
2949	  switch (e_flags & EF_FRV_CPU_MASK)
2950	    {
2951	    case EF_FRV_CPU_GENERIC:
2952	      break;
2953
2954	    default:
2955	      strcat (buf, ", fr???");
2956	      break;
2957
2958	    case EF_FRV_CPU_FR300:
2959	      strcat (buf, ", fr300");
2960	      break;
2961
2962	    case EF_FRV_CPU_FR400:
2963	      strcat (buf, ", fr400");
2964	      break;
2965	    case EF_FRV_CPU_FR405:
2966	      strcat (buf, ", fr405");
2967	      break;
2968
2969	    case EF_FRV_CPU_FR450:
2970	      strcat (buf, ", fr450");
2971	      break;
2972
2973	    case EF_FRV_CPU_FR500:
2974	      strcat (buf, ", fr500");
2975	      break;
2976	    case EF_FRV_CPU_FR550:
2977	      strcat (buf, ", fr550");
2978	      break;
2979
2980	    case EF_FRV_CPU_SIMPLE:
2981	      strcat (buf, ", simple");
2982	      break;
2983	    case EF_FRV_CPU_TOMCAT:
2984	      strcat (buf, ", tomcat");
2985	      break;
2986	    }
2987	  break;
2988
2989	case EM_68K:
2990	  if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
2991	    strcat (buf, ", m68000");
2992	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
2993	    strcat (buf, ", cpu32");
2994	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
2995	    strcat (buf, ", fido_a");
2996	  else
2997	    {
2998	      char const * isa = _("unknown");
2999	      char const * mac = _("unknown mac");
3000	      char const * additional = NULL;
3001
3002	      switch (e_flags & EF_M68K_CF_ISA_MASK)
3003		{
3004		case EF_M68K_CF_ISA_A_NODIV:
3005		  isa = "A";
3006		  additional = ", nodiv";
3007		  break;
3008		case EF_M68K_CF_ISA_A:
3009		  isa = "A";
3010		  break;
3011		case EF_M68K_CF_ISA_A_PLUS:
3012		  isa = "A+";
3013		  break;
3014		case EF_M68K_CF_ISA_B_NOUSP:
3015		  isa = "B";
3016		  additional = ", nousp";
3017		  break;
3018		case EF_M68K_CF_ISA_B:
3019		  isa = "B";
3020		  break;
3021		case EF_M68K_CF_ISA_C:
3022		  isa = "C";
3023		  break;
3024		case EF_M68K_CF_ISA_C_NODIV:
3025		  isa = "C";
3026		  additional = ", nodiv";
3027		  break;
3028		}
3029	      strcat (buf, ", cf, isa ");
3030	      strcat (buf, isa);
3031	      if (additional)
3032		strcat (buf, additional);
3033	      if (e_flags & EF_M68K_CF_FLOAT)
3034		strcat (buf, ", float");
3035	      switch (e_flags & EF_M68K_CF_MAC_MASK)
3036		{
3037		case 0:
3038		  mac = NULL;
3039		  break;
3040		case EF_M68K_CF_MAC:
3041		  mac = "mac";
3042		  break;
3043		case EF_M68K_CF_EMAC:
3044		  mac = "emac";
3045		  break;
3046		case EF_M68K_CF_EMAC_B:
3047		  mac = "emac_b";
3048		  break;
3049		}
3050	      if (mac)
3051		{
3052		  strcat (buf, ", ");
3053		  strcat (buf, mac);
3054		}
3055	    }
3056	  break;
3057
3058	case EM_CYGNUS_MEP:
3059	  switch (e_flags & EF_MEP_CPU_MASK)
3060	    {
3061	    case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3062	    case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3063	    case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3064	    case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3065	    case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3066	    case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3067	    default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3068	    }
3069
3070	  switch (e_flags & EF_MEP_COP_MASK)
3071	    {
3072	    case EF_MEP_COP_NONE: break;
3073	    case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3074	    case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3075	    case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3076	    case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3077	    default: strcat (buf, _("<unknown MeP copro type>")); break;
3078	    }
3079
3080	  if (e_flags & EF_MEP_LIBRARY)
3081	    strcat (buf, ", Built for Library");
3082
3083	  if (e_flags & EF_MEP_INDEX_MASK)
3084	    sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3085		     e_flags & EF_MEP_INDEX_MASK);
3086
3087	  if (e_flags & ~ EF_MEP_ALL_FLAGS)
3088	    sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3089		     e_flags & ~ EF_MEP_ALL_FLAGS);
3090	  break;
3091
3092	case EM_PPC:
3093	  if (e_flags & EF_PPC_EMB)
3094	    strcat (buf, ", emb");
3095
3096	  if (e_flags & EF_PPC_RELOCATABLE)
3097	    strcat (buf, _(", relocatable"));
3098
3099	  if (e_flags & EF_PPC_RELOCATABLE_LIB)
3100	    strcat (buf, _(", relocatable-lib"));
3101	  break;
3102
3103	case EM_PPC64:
3104	  if (e_flags & EF_PPC64_ABI)
3105	    {
3106	      char abi[] = ", abiv0";
3107
3108	      abi[6] += e_flags & EF_PPC64_ABI;
3109	      strcat (buf, abi);
3110	    }
3111	  break;
3112
3113	case EM_V800:
3114	  if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3115	    strcat (buf, ", RH850 ABI");
3116
3117	  if (e_flags & EF_V800_850E3)
3118	    strcat (buf, ", V3 architecture");
3119
3120	  if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3121	    strcat (buf, ", FPU not used");
3122
3123	  if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3124	    strcat (buf, ", regmode: COMMON");
3125
3126	  if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3127	    strcat (buf, ", r4 not used");
3128
3129	  if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3130	    strcat (buf, ", r30 not used");
3131
3132	  if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3133	    strcat (buf, ", r5 not used");
3134
3135	  if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3136	    strcat (buf, ", r2 not used");
3137
3138	  for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3139	    {
3140	      switch (e_flags & - e_flags)
3141		{
3142		case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3143		case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3144		case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3145		case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3146		case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3147		case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3148		case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3149		case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3150		case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3151		case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3152		case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3153		case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3154		default: break;
3155		}
3156	    }
3157	  break;
3158
3159	case EM_V850:
3160	case EM_CYGNUS_V850:
3161	  switch (e_flags & EF_V850_ARCH)
3162	    {
3163	    case E_V850E3V5_ARCH:
3164	      strcat (buf, ", v850e3v5");
3165	      break;
3166	    case E_V850E2V3_ARCH:
3167	      strcat (buf, ", v850e2v3");
3168	      break;
3169	    case E_V850E2_ARCH:
3170	      strcat (buf, ", v850e2");
3171	      break;
3172            case E_V850E1_ARCH:
3173              strcat (buf, ", v850e1");
3174	      break;
3175	    case E_V850E_ARCH:
3176	      strcat (buf, ", v850e");
3177	      break;
3178	    case E_V850_ARCH:
3179	      strcat (buf, ", v850");
3180	      break;
3181	    default:
3182	      strcat (buf, _(", unknown v850 architecture variant"));
3183	      break;
3184	    }
3185	  break;
3186
3187	case EM_M32R:
3188	case EM_CYGNUS_M32R:
3189	  if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3190	    strcat (buf, ", m32r");
3191	  break;
3192
3193	case EM_MIPS:
3194	case EM_MIPS_RS3_LE:
3195	  if (e_flags & EF_MIPS_NOREORDER)
3196	    strcat (buf, ", noreorder");
3197
3198	  if (e_flags & EF_MIPS_PIC)
3199	    strcat (buf, ", pic");
3200
3201	  if (e_flags & EF_MIPS_CPIC)
3202	    strcat (buf, ", cpic");
3203
3204	  if (e_flags & EF_MIPS_UCODE)
3205	    strcat (buf, ", ugen_reserved");
3206
3207	  if (e_flags & EF_MIPS_ABI2)
3208	    strcat (buf, ", abi2");
3209
3210	  if (e_flags & EF_MIPS_OPTIONS_FIRST)
3211	    strcat (buf, ", odk first");
3212
3213	  if (e_flags & EF_MIPS_32BITMODE)
3214	    strcat (buf, ", 32bitmode");
3215
3216	  if (e_flags & EF_MIPS_NAN2008)
3217	    strcat (buf, ", nan2008");
3218
3219	  if (e_flags & EF_MIPS_FP64)
3220	    strcat (buf, ", fp64");
3221
3222	  switch ((e_flags & EF_MIPS_MACH))
3223	    {
3224	    case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3225	    case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3226	    case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3227	    case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3228	    case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3229	    case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3230	    case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3231	    case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3232	    case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
3233	    case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3234  	    case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3235  	    case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3236  	    case E_MIPS_MACH_LS3A: strcat (buf, ", loongson-3a"); break;
3237	    case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3238	    case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3239	    case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3240	    case E_MIPS_MACH_XLR:  strcat (buf, ", xlr"); break;
3241	    case 0:
3242	    /* We simply ignore the field in this case to avoid confusion:
3243	       MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3244	       extension.  */
3245	      break;
3246	    default: strcat (buf, _(", unknown CPU")); break;
3247	    }
3248
3249	  switch ((e_flags & EF_MIPS_ABI))
3250	    {
3251	    case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3252	    case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3253	    case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3254	    case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3255	    case 0:
3256	    /* We simply ignore the field in this case to avoid confusion:
3257	       MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3258	       This means it is likely to be an o32 file, but not for
3259	       sure.  */
3260	      break;
3261	    default: strcat (buf, _(", unknown ABI")); break;
3262	    }
3263
3264	  if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3265	    strcat (buf, ", mdmx");
3266
3267	  if (e_flags & EF_MIPS_ARCH_ASE_M16)
3268	    strcat (buf, ", mips16");
3269
3270	  if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3271	    strcat (buf, ", micromips");
3272
3273	  switch ((e_flags & EF_MIPS_ARCH))
3274	    {
3275	    case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3276	    case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3277	    case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3278	    case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3279	    case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3280	    case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3281	    case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3282	    case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3283	    case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3284	    case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3285	    case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3286	    default: strcat (buf, _(", unknown ISA")); break;
3287	    }
3288	  break;
3289
3290	case EM_NDS32:
3291	  decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3292	  break;
3293
3294	case EM_RISCV:
3295	  {
3296	    unsigned int riscv_extension = EF_GET_RISCV_EXT(e_flags);
3297	    strcat (buf, ", ");
3298	    strcat (buf, riscv_elf_flag_to_name (riscv_extension));
3299	  }
3300	  break;
3301
3302	case EM_SH:
3303	  switch ((e_flags & EF_SH_MACH_MASK))
3304	    {
3305	    case EF_SH1: strcat (buf, ", sh1"); break;
3306	    case EF_SH2: strcat (buf, ", sh2"); break;
3307	    case EF_SH3: strcat (buf, ", sh3"); break;
3308	    case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3309	    case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3310	    case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3311	    case EF_SH3E: strcat (buf, ", sh3e"); break;
3312	    case EF_SH4: strcat (buf, ", sh4"); break;
3313	    case EF_SH5: strcat (buf, ", sh5"); break;
3314	    case EF_SH2E: strcat (buf, ", sh2e"); break;
3315	    case EF_SH4A: strcat (buf, ", sh4a"); break;
3316	    case EF_SH2A: strcat (buf, ", sh2a"); break;
3317	    case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3318	    case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3319	    case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3320	    case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3321	    case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3322	    case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3323	    case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3324	    case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3325	    case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3326	    default: strcat (buf, _(", unknown ISA")); break;
3327	    }
3328
3329	  if (e_flags & EF_SH_PIC)
3330	    strcat (buf, ", pic");
3331
3332	  if (e_flags & EF_SH_FDPIC)
3333	    strcat (buf, ", fdpic");
3334	  break;
3335
3336        case EM_OR1K:
3337          if (e_flags & EF_OR1K_NODELAY)
3338            strcat (buf, ", no delay");
3339          break;
3340
3341	case EM_SPARCV9:
3342	  if (e_flags & EF_SPARC_32PLUS)
3343	    strcat (buf, ", v8+");
3344
3345	  if (e_flags & EF_SPARC_SUN_US1)
3346	    strcat (buf, ", ultrasparcI");
3347
3348	  if (e_flags & EF_SPARC_SUN_US3)
3349	    strcat (buf, ", ultrasparcIII");
3350
3351	  if (e_flags & EF_SPARC_HAL_R1)
3352	    strcat (buf, ", halr1");
3353
3354	  if (e_flags & EF_SPARC_LEDATA)
3355	    strcat (buf, ", ledata");
3356
3357	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3358	    strcat (buf, ", tso");
3359
3360	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3361	    strcat (buf, ", pso");
3362
3363	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3364	    strcat (buf, ", rmo");
3365	  break;
3366
3367	case EM_PARISC:
3368	  switch (e_flags & EF_PARISC_ARCH)
3369	    {
3370	    case EFA_PARISC_1_0:
3371	      strcpy (buf, ", PA-RISC 1.0");
3372	      break;
3373	    case EFA_PARISC_1_1:
3374	      strcpy (buf, ", PA-RISC 1.1");
3375	      break;
3376	    case EFA_PARISC_2_0:
3377	      strcpy (buf, ", PA-RISC 2.0");
3378	      break;
3379	    default:
3380	      break;
3381	    }
3382	  if (e_flags & EF_PARISC_TRAPNIL)
3383	    strcat (buf, ", trapnil");
3384	  if (e_flags & EF_PARISC_EXT)
3385	    strcat (buf, ", ext");
3386	  if (e_flags & EF_PARISC_LSB)
3387	    strcat (buf, ", lsb");
3388	  if (e_flags & EF_PARISC_WIDE)
3389	    strcat (buf, ", wide");
3390	  if (e_flags & EF_PARISC_NO_KABP)
3391	    strcat (buf, ", no kabp");
3392	  if (e_flags & EF_PARISC_LAZYSWAP)
3393	    strcat (buf, ", lazyswap");
3394	  break;
3395
3396	case EM_PJ:
3397	case EM_PJ_OLD:
3398	  if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3399	    strcat (buf, ", new calling convention");
3400
3401	  if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3402	    strcat (buf, ", gnu calling convention");
3403	  break;
3404
3405	case EM_IA_64:
3406	  if ((e_flags & EF_IA_64_ABI64))
3407	    strcat (buf, ", 64-bit");
3408	  else
3409	    strcat (buf, ", 32-bit");
3410	  if ((e_flags & EF_IA_64_REDUCEDFP))
3411	    strcat (buf, ", reduced fp model");
3412	  if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3413	    strcat (buf, ", no function descriptors, constant gp");
3414	  else if ((e_flags & EF_IA_64_CONS_GP))
3415	    strcat (buf, ", constant gp");
3416	  if ((e_flags & EF_IA_64_ABSOLUTE))
3417	    strcat (buf, ", absolute");
3418          if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3419            {
3420              if ((e_flags & EF_IA_64_VMS_LINKAGES))
3421                strcat (buf, ", vms_linkages");
3422              switch ((e_flags & EF_IA_64_VMS_COMCOD))
3423                {
3424                case EF_IA_64_VMS_COMCOD_SUCCESS:
3425                  break;
3426                case EF_IA_64_VMS_COMCOD_WARNING:
3427                  strcat (buf, ", warning");
3428                  break;
3429                case EF_IA_64_VMS_COMCOD_ERROR:
3430                  strcat (buf, ", error");
3431                  break;
3432                case EF_IA_64_VMS_COMCOD_ABORT:
3433                  strcat (buf, ", abort");
3434                  break;
3435                default:
3436		  warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3437			e_flags & EF_IA_64_VMS_COMCOD);
3438		  strcat (buf, ", <unknown>");
3439                }
3440            }
3441	  break;
3442
3443	case EM_VAX:
3444	  if ((e_flags & EF_VAX_NONPIC))
3445	    strcat (buf, ", non-PIC");
3446	  if ((e_flags & EF_VAX_DFLOAT))
3447	    strcat (buf, ", D-Float");
3448	  if ((e_flags & EF_VAX_GFLOAT))
3449	    strcat (buf, ", G-Float");
3450	  break;
3451
3452        case EM_VISIUM:
3453	  if (e_flags & EF_VISIUM_ARCH_MCM)
3454	    strcat (buf, ", mcm");
3455	  else if (e_flags & EF_VISIUM_ARCH_MCM24)
3456	    strcat (buf, ", mcm24");
3457	  if (e_flags & EF_VISIUM_ARCH_GR6)
3458	    strcat (buf, ", gr6");
3459	  break;
3460
3461	case EM_RL78:
3462	  switch (e_flags & E_FLAG_RL78_CPU_MASK)
3463	    {
3464	    case E_FLAG_RL78_ANY_CPU: break;
3465	    case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3466	    case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3467	    case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3468	    }
3469	  if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3470	    strcat (buf, ", 64-bit doubles");
3471	  break;
3472
3473	case EM_RX:
3474	  if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3475	    strcat (buf, ", 64-bit doubles");
3476	  if (e_flags & E_FLAG_RX_DSP)
3477	    strcat (buf, ", dsp");
3478	  if (e_flags & E_FLAG_RX_PID)
3479	    strcat (buf, ", pid");
3480	  if (e_flags & E_FLAG_RX_ABI)
3481	    strcat (buf, ", RX ABI");
3482	  if (e_flags & E_FLAG_RX_SINSNS_SET)
3483	    strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3484		    ? ", uses String instructions" : ", bans String instructions");
3485	  if (e_flags & E_FLAG_RX_V2)
3486	    strcat (buf, ", V2");
3487	  break;
3488
3489	case EM_S390:
3490	  if (e_flags & EF_S390_HIGH_GPRS)
3491	    strcat (buf, ", highgprs");
3492	  break;
3493
3494	case EM_TI_C6000:
3495	  if ((e_flags & EF_C6000_REL))
3496	    strcat (buf, ", relocatable module");
3497	  break;
3498
3499	case EM_MSP430:
3500	  strcat (buf, _(": architecture variant: "));
3501	  switch (e_flags & EF_MSP430_MACH)
3502	    {
3503	    case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3504	    case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3505	    case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3506	    case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3507	    case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3508	    case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3509	    case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3510	    case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3511	    case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3512	    case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3513	    case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3514	    case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3515	    case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3516	    case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3517	    case E_MSP430_MACH_MSP430X  : strcat (buf, "MSP430X"); break;
3518	    default:
3519	      strcat (buf, _(": unknown")); break;
3520	    }
3521
3522	  if (e_flags & ~ EF_MSP430_MACH)
3523	    strcat (buf, _(": unknown extra flag bits also present"));
3524	}
3525    }
3526
3527  return buf;
3528}
3529
3530static const char *
3531get_osabi_name (unsigned int osabi)
3532{
3533  static char buff[32];
3534
3535  switch (osabi)
3536    {
3537    case ELFOSABI_NONE:		return "UNIX - System V";
3538    case ELFOSABI_HPUX:		return "UNIX - HP-UX";
3539    case ELFOSABI_NETBSD:	return "UNIX - NetBSD";
3540    case ELFOSABI_GNU:		return "UNIX - GNU";
3541    case ELFOSABI_SOLARIS:	return "UNIX - Solaris";
3542    case ELFOSABI_AIX:		return "UNIX - AIX";
3543    case ELFOSABI_IRIX:		return "UNIX - IRIX";
3544    case ELFOSABI_FREEBSD:	return "UNIX - FreeBSD";
3545    case ELFOSABI_TRU64:	return "UNIX - TRU64";
3546    case ELFOSABI_MODESTO:	return "Novell - Modesto";
3547    case ELFOSABI_OPENBSD:	return "UNIX - OpenBSD";
3548    case ELFOSABI_OPENVMS:	return "VMS - OpenVMS";
3549    case ELFOSABI_NSK:		return "HP - Non-Stop Kernel";
3550    case ELFOSABI_AROS:		return "AROS";
3551    case ELFOSABI_FENIXOS:	return "FenixOS";
3552    default:
3553      if (osabi >= 64)
3554	switch (elf_header.e_machine)
3555	  {
3556	  case EM_ARM:
3557	    switch (osabi)
3558	      {
3559	      case ELFOSABI_ARM:	return "ARM";
3560	      default:
3561		break;
3562	      }
3563	    break;
3564
3565	  case EM_MSP430:
3566	  case EM_MSP430_OLD:
3567	  case EM_VISIUM:
3568	    switch (osabi)
3569	      {
3570	      case ELFOSABI_STANDALONE:	return _("Standalone App");
3571	      default:
3572		break;
3573	      }
3574	    break;
3575
3576	  case EM_TI_C6000:
3577	    switch (osabi)
3578	      {
3579	      case ELFOSABI_C6000_ELFABI:	return _("Bare-metal C6000");
3580	      case ELFOSABI_C6000_LINUX:	return "Linux C6000";
3581	      default:
3582		break;
3583	      }
3584	    break;
3585
3586	  default:
3587	    break;
3588	  }
3589      snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3590      return buff;
3591    }
3592}
3593
3594static const char *
3595get_aarch64_segment_type (unsigned long type)
3596{
3597  switch (type)
3598    {
3599    case PT_AARCH64_ARCHEXT:
3600      return "AARCH64_ARCHEXT";
3601    default:
3602      break;
3603    }
3604
3605  return NULL;
3606}
3607
3608static const char *
3609get_arm_segment_type (unsigned long type)
3610{
3611  switch (type)
3612    {
3613    case PT_ARM_EXIDX:
3614      return "EXIDX";
3615    default:
3616      break;
3617    }
3618
3619  return NULL;
3620}
3621
3622static const char *
3623get_mips_segment_type (unsigned long type)
3624{
3625  switch (type)
3626    {
3627    case PT_MIPS_REGINFO:
3628      return "REGINFO";
3629    case PT_MIPS_RTPROC:
3630      return "RTPROC";
3631    case PT_MIPS_OPTIONS:
3632      return "OPTIONS";
3633    case PT_MIPS_ABIFLAGS:
3634      return "ABIFLAGS";
3635    default:
3636      break;
3637    }
3638
3639  return NULL;
3640}
3641
3642static const char *
3643get_parisc_segment_type (unsigned long type)
3644{
3645  switch (type)
3646    {
3647    case PT_HP_TLS:		return "HP_TLS";
3648    case PT_HP_CORE_NONE:	return "HP_CORE_NONE";
3649    case PT_HP_CORE_VERSION:	return "HP_CORE_VERSION";
3650    case PT_HP_CORE_KERNEL:	return "HP_CORE_KERNEL";
3651    case PT_HP_CORE_COMM:	return "HP_CORE_COMM";
3652    case PT_HP_CORE_PROC:	return "HP_CORE_PROC";
3653    case PT_HP_CORE_LOADABLE:	return "HP_CORE_LOADABLE";
3654    case PT_HP_CORE_STACK:	return "HP_CORE_STACK";
3655    case PT_HP_CORE_SHM:	return "HP_CORE_SHM";
3656    case PT_HP_CORE_MMF:	return "HP_CORE_MMF";
3657    case PT_HP_PARALLEL:	return "HP_PARALLEL";
3658    case PT_HP_FASTBIND:	return "HP_FASTBIND";
3659    case PT_HP_OPT_ANNOT:	return "HP_OPT_ANNOT";
3660    case PT_HP_HSL_ANNOT:	return "HP_HSL_ANNOT";
3661    case PT_HP_STACK:		return "HP_STACK";
3662    case PT_HP_CORE_UTSNAME:	return "HP_CORE_UTSNAME";
3663    case PT_PARISC_ARCHEXT:	return "PARISC_ARCHEXT";
3664    case PT_PARISC_UNWIND:	return "PARISC_UNWIND";
3665    case PT_PARISC_WEAKORDER:	return "PARISC_WEAKORDER";
3666    default:
3667      break;
3668    }
3669
3670  return NULL;
3671}
3672
3673static const char *
3674get_ia64_segment_type (unsigned long type)
3675{
3676  switch (type)
3677    {
3678    case PT_IA_64_ARCHEXT:	return "IA_64_ARCHEXT";
3679    case PT_IA_64_UNWIND:	return "IA_64_UNWIND";
3680    case PT_HP_TLS:		return "HP_TLS";
3681    case PT_IA_64_HP_OPT_ANOT:	return "HP_OPT_ANNOT";
3682    case PT_IA_64_HP_HSL_ANOT:	return "HP_HSL_ANNOT";
3683    case PT_IA_64_HP_STACK:	return "HP_STACK";
3684    default:
3685      break;
3686    }
3687
3688  return NULL;
3689}
3690
3691static const char *
3692get_tic6x_segment_type (unsigned long type)
3693{
3694  switch (type)
3695    {
3696    case PT_C6000_PHATTR:	return "C6000_PHATTR";
3697    default:
3698      break;
3699    }
3700
3701  return NULL;
3702}
3703
3704static const char *
3705get_solaris_segment_type (unsigned long type)
3706{
3707  switch (type)
3708    {
3709    case 0x6464e550: return "PT_SUNW_UNWIND";
3710    case 0x6474e550: return "PT_SUNW_EH_FRAME";
3711    case 0x6ffffff7: return "PT_LOSUNW";
3712    case 0x6ffffffa: return "PT_SUNWBSS";
3713    case 0x6ffffffb: return "PT_SUNWSTACK";
3714    case 0x6ffffffc: return "PT_SUNWDTRACE";
3715    case 0x6ffffffd: return "PT_SUNWCAP";
3716    case 0x6fffffff: return "PT_HISUNW";
3717    default: return NULL;
3718    }
3719}
3720
3721static const char *
3722get_segment_type (unsigned long p_type)
3723{
3724  static char buff[32];
3725
3726  switch (p_type)
3727    {
3728    case PT_NULL:	return "NULL";
3729    case PT_LOAD:	return "LOAD";
3730    case PT_DYNAMIC:	return "DYNAMIC";
3731    case PT_INTERP:	return "INTERP";
3732    case PT_NOTE:	return "NOTE";
3733    case PT_SHLIB:	return "SHLIB";
3734    case PT_PHDR:	return "PHDR";
3735    case PT_TLS:	return "TLS";
3736
3737    case PT_GNU_EH_FRAME:
3738			return "GNU_EH_FRAME";
3739    case PT_GNU_STACK:	return "GNU_STACK";
3740    case PT_GNU_RELRO:  return "GNU_RELRO";
3741
3742    default:
3743      if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
3744	{
3745	  const char * result;
3746
3747	  switch (elf_header.e_machine)
3748	    {
3749	    case EM_AARCH64:
3750	      result = get_aarch64_segment_type (p_type);
3751	      break;
3752	    case EM_ARM:
3753	      result = get_arm_segment_type (p_type);
3754	      break;
3755	    case EM_MIPS:
3756	    case EM_MIPS_RS3_LE:
3757	      result = get_mips_segment_type (p_type);
3758	      break;
3759	    case EM_PARISC:
3760	      result = get_parisc_segment_type (p_type);
3761	      break;
3762	    case EM_IA_64:
3763	      result = get_ia64_segment_type (p_type);
3764	      break;
3765	    case EM_TI_C6000:
3766	      result = get_tic6x_segment_type (p_type);
3767	      break;
3768	    default:
3769	      result = NULL;
3770	      break;
3771	    }
3772
3773	  if (result != NULL)
3774	    return result;
3775
3776	  sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
3777	}
3778      else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
3779	{
3780	  const char * result;
3781
3782	  switch (elf_header.e_machine)
3783	    {
3784	    case EM_PARISC:
3785	      result = get_parisc_segment_type (p_type);
3786	      break;
3787	    case EM_IA_64:
3788	      result = get_ia64_segment_type (p_type);
3789	      break;
3790	    default:
3791	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
3792		result = get_solaris_segment_type (p_type);
3793	      else
3794		result = NULL;
3795	      break;
3796	    }
3797
3798	  if (result != NULL)
3799	    return result;
3800
3801	  sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
3802	}
3803      else
3804	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
3805
3806      return buff;
3807    }
3808}
3809
3810static const char *
3811get_mips_section_type_name (unsigned int sh_type)
3812{
3813  switch (sh_type)
3814    {
3815    case SHT_MIPS_LIBLIST:	 return "MIPS_LIBLIST";
3816    case SHT_MIPS_MSYM:		 return "MIPS_MSYM";
3817    case SHT_MIPS_CONFLICT:	 return "MIPS_CONFLICT";
3818    case SHT_MIPS_GPTAB:	 return "MIPS_GPTAB";
3819    case SHT_MIPS_UCODE:	 return "MIPS_UCODE";
3820    case SHT_MIPS_DEBUG:	 return "MIPS_DEBUG";
3821    case SHT_MIPS_REGINFO:	 return "MIPS_REGINFO";
3822    case SHT_MIPS_PACKAGE:	 return "MIPS_PACKAGE";
3823    case SHT_MIPS_PACKSYM:	 return "MIPS_PACKSYM";
3824    case SHT_MIPS_RELD:		 return "MIPS_RELD";
3825    case SHT_MIPS_IFACE:	 return "MIPS_IFACE";
3826    case SHT_MIPS_CONTENT:	 return "MIPS_CONTENT";
3827    case SHT_MIPS_OPTIONS:	 return "MIPS_OPTIONS";
3828    case SHT_MIPS_SHDR:		 return "MIPS_SHDR";
3829    case SHT_MIPS_FDESC:	 return "MIPS_FDESC";
3830    case SHT_MIPS_EXTSYM:	 return "MIPS_EXTSYM";
3831    case SHT_MIPS_DENSE:	 return "MIPS_DENSE";
3832    case SHT_MIPS_PDESC:	 return "MIPS_PDESC";
3833    case SHT_MIPS_LOCSYM:	 return "MIPS_LOCSYM";
3834    case SHT_MIPS_AUXSYM:	 return "MIPS_AUXSYM";
3835    case SHT_MIPS_OPTSYM:	 return "MIPS_OPTSYM";
3836    case SHT_MIPS_LOCSTR:	 return "MIPS_LOCSTR";
3837    case SHT_MIPS_LINE:		 return "MIPS_LINE";
3838    case SHT_MIPS_RFDESC:	 return "MIPS_RFDESC";
3839    case SHT_MIPS_DELTASYM:	 return "MIPS_DELTASYM";
3840    case SHT_MIPS_DELTAINST:	 return "MIPS_DELTAINST";
3841    case SHT_MIPS_DELTACLASS:	 return "MIPS_DELTACLASS";
3842    case SHT_MIPS_DWARF:	 return "MIPS_DWARF";
3843    case SHT_MIPS_DELTADECL:	 return "MIPS_DELTADECL";
3844    case SHT_MIPS_SYMBOL_LIB:	 return "MIPS_SYMBOL_LIB";
3845    case SHT_MIPS_EVENTS:	 return "MIPS_EVENTS";
3846    case SHT_MIPS_TRANSLATE:	 return "MIPS_TRANSLATE";
3847    case SHT_MIPS_PIXIE:	 return "MIPS_PIXIE";
3848    case SHT_MIPS_XLATE:	 return "MIPS_XLATE";
3849    case SHT_MIPS_XLATE_DEBUG:	 return "MIPS_XLATE_DEBUG";
3850    case SHT_MIPS_WHIRL:	 return "MIPS_WHIRL";
3851    case SHT_MIPS_EH_REGION:	 return "MIPS_EH_REGION";
3852    case SHT_MIPS_XLATE_OLD:	 return "MIPS_XLATE_OLD";
3853    case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
3854    case SHT_MIPS_ABIFLAGS:	 return "MIPS_ABIFLAGS";
3855    default:
3856      break;
3857    }
3858  return NULL;
3859}
3860
3861static const char *
3862get_parisc_section_type_name (unsigned int sh_type)
3863{
3864  switch (sh_type)
3865    {
3866    case SHT_PARISC_EXT:	return "PARISC_EXT";
3867    case SHT_PARISC_UNWIND:	return "PARISC_UNWIND";
3868    case SHT_PARISC_DOC:	return "PARISC_DOC";
3869    case SHT_PARISC_ANNOT:	return "PARISC_ANNOT";
3870    case SHT_PARISC_SYMEXTN:	return "PARISC_SYMEXTN";
3871    case SHT_PARISC_STUBS:	return "PARISC_STUBS";
3872    case SHT_PARISC_DLKM:	return "PARISC_DLKM";
3873    default:
3874      break;
3875    }
3876  return NULL;
3877}
3878
3879static const char *
3880get_ia64_section_type_name (unsigned int sh_type)
3881{
3882  /* If the top 8 bits are 0x78 the next 8 are the os/abi ID.  */
3883  if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
3884    return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
3885
3886  switch (sh_type)
3887    {
3888    case SHT_IA_64_EXT:		       return "IA_64_EXT";
3889    case SHT_IA_64_UNWIND:	       return "IA_64_UNWIND";
3890    case SHT_IA_64_PRIORITY_INIT:      return "IA_64_PRIORITY_INIT";
3891    case SHT_IA_64_VMS_TRACE:          return "VMS_TRACE";
3892    case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
3893    case SHT_IA_64_VMS_DEBUG:          return "VMS_DEBUG";
3894    case SHT_IA_64_VMS_DEBUG_STR:      return "VMS_DEBUG_STR";
3895    case SHT_IA_64_VMS_LINKAGES:       return "VMS_LINKAGES";
3896    case SHT_IA_64_VMS_SYMBOL_VECTOR:  return "VMS_SYMBOL_VECTOR";
3897    case SHT_IA_64_VMS_FIXUP:          return "VMS_FIXUP";
3898    default:
3899      break;
3900    }
3901  return NULL;
3902}
3903
3904static const char *
3905get_x86_64_section_type_name (unsigned int sh_type)
3906{
3907  switch (sh_type)
3908    {
3909    case SHT_X86_64_UNWIND:	return "X86_64_UNWIND";
3910    default:
3911      break;
3912    }
3913  return NULL;
3914}
3915
3916static const char *
3917get_aarch64_section_type_name (unsigned int sh_type)
3918{
3919  switch (sh_type)
3920    {
3921    case SHT_AARCH64_ATTRIBUTES:
3922      return "AARCH64_ATTRIBUTES";
3923    default:
3924      break;
3925    }
3926  return NULL;
3927}
3928
3929static const char *
3930get_arm_section_type_name (unsigned int sh_type)
3931{
3932  switch (sh_type)
3933    {
3934    case SHT_ARM_EXIDX:           return "ARM_EXIDX";
3935    case SHT_ARM_PREEMPTMAP:      return "ARM_PREEMPTMAP";
3936    case SHT_ARM_ATTRIBUTES:      return "ARM_ATTRIBUTES";
3937    case SHT_ARM_DEBUGOVERLAY:    return "ARM_DEBUGOVERLAY";
3938    case SHT_ARM_OVERLAYSECTION:  return "ARM_OVERLAYSECTION";
3939    default:
3940      break;
3941    }
3942  return NULL;
3943}
3944
3945static const char *
3946get_tic6x_section_type_name (unsigned int sh_type)
3947{
3948  switch (sh_type)
3949    {
3950    case SHT_C6000_UNWIND:
3951      return "C6000_UNWIND";
3952    case SHT_C6000_PREEMPTMAP:
3953      return "C6000_PREEMPTMAP";
3954    case SHT_C6000_ATTRIBUTES:
3955      return "C6000_ATTRIBUTES";
3956    case SHT_TI_ICODE:
3957      return "TI_ICODE";
3958    case SHT_TI_XREF:
3959      return "TI_XREF";
3960    case SHT_TI_HANDLER:
3961      return "TI_HANDLER";
3962    case SHT_TI_INITINFO:
3963      return "TI_INITINFO";
3964    case SHT_TI_PHATTRS:
3965      return "TI_PHATTRS";
3966    default:
3967      break;
3968    }
3969  return NULL;
3970}
3971
3972static const char *
3973get_msp430x_section_type_name (unsigned int sh_type)
3974{
3975  switch (sh_type)
3976    {
3977    case SHT_MSP430_SEC_FLAGS:   return "MSP430_SEC_FLAGS";
3978    case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
3979    case SHT_MSP430_ATTRIBUTES:  return "MSP430_ATTRIBUTES";
3980    default: return NULL;
3981    }
3982}
3983
3984static const char *
3985get_v850_section_type_name (unsigned int sh_type)
3986{
3987  switch (sh_type)
3988    {
3989    case SHT_V850_SCOMMON: return "V850 Small Common";
3990    case SHT_V850_TCOMMON: return "V850 Tiny Common";
3991    case SHT_V850_ZCOMMON: return "V850 Zero Common";
3992    case SHT_RENESAS_IOP:  return "RENESAS IOP";
3993    case SHT_RENESAS_INFO: return "RENESAS INFO";
3994    default: return NULL;
3995    }
3996}
3997
3998static const char *
3999get_section_type_name (unsigned int sh_type)
4000{
4001  static char buff[32];
4002  const char * result;
4003
4004  switch (sh_type)
4005    {
4006    case SHT_NULL:		return "NULL";
4007    case SHT_PROGBITS:		return "PROGBITS";
4008    case SHT_SYMTAB:		return "SYMTAB";
4009    case SHT_STRTAB:		return "STRTAB";
4010    case SHT_RELA:		return "RELA";
4011    case SHT_HASH:		return "HASH";
4012    case SHT_DYNAMIC:		return "DYNAMIC";
4013    case SHT_NOTE:		return "NOTE";
4014    case SHT_NOBITS:		return "NOBITS";
4015    case SHT_REL:		return "REL";
4016    case SHT_SHLIB:		return "SHLIB";
4017    case SHT_DYNSYM:		return "DYNSYM";
4018    case SHT_INIT_ARRAY:	return "INIT_ARRAY";
4019    case SHT_FINI_ARRAY:	return "FINI_ARRAY";
4020    case SHT_PREINIT_ARRAY:	return "PREINIT_ARRAY";
4021    case SHT_GNU_HASH:		return "GNU_HASH";
4022    case SHT_GROUP:		return "GROUP";
4023    case SHT_SYMTAB_SHNDX:	return "SYMTAB SECTION INDICIES";
4024    case SHT_GNU_verdef:	return "VERDEF";
4025    case SHT_GNU_verneed:	return "VERNEED";
4026    case SHT_GNU_versym:	return "VERSYM";
4027    case 0x6ffffff0:		return "VERSYM";
4028    case 0x6ffffffc:		return "VERDEF";
4029    case 0x7ffffffd:		return "AUXILIARY";
4030    case 0x7fffffff:		return "FILTER";
4031    case SHT_GNU_LIBLIST:	return "GNU_LIBLIST";
4032
4033    default:
4034      if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4035	{
4036	  switch (elf_header.e_machine)
4037	    {
4038	    case EM_MIPS:
4039	    case EM_MIPS_RS3_LE:
4040	      result = get_mips_section_type_name (sh_type);
4041	      break;
4042	    case EM_PARISC:
4043	      result = get_parisc_section_type_name (sh_type);
4044	      break;
4045	    case EM_IA_64:
4046	      result = get_ia64_section_type_name (sh_type);
4047	      break;
4048	    case EM_X86_64:
4049	    case EM_L1OM:
4050	    case EM_K1OM:
4051	      result = get_x86_64_section_type_name (sh_type);
4052	      break;
4053	    case EM_AARCH64:
4054	      result = get_aarch64_section_type_name (sh_type);
4055	      break;
4056	    case EM_ARM:
4057	      result = get_arm_section_type_name (sh_type);
4058	      break;
4059	    case EM_TI_C6000:
4060	      result = get_tic6x_section_type_name (sh_type);
4061	      break;
4062	    case EM_MSP430:
4063	      result = get_msp430x_section_type_name (sh_type);
4064	      break;
4065	    case EM_V800:
4066	    case EM_V850:
4067	    case EM_CYGNUS_V850:
4068	      result = get_v850_section_type_name (sh_type);
4069	      break;
4070	    default:
4071	      result = NULL;
4072	      break;
4073	    }
4074
4075	  if (result != NULL)
4076	    return result;
4077
4078	  sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4079	}
4080      else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4081	{
4082	  switch (elf_header.e_machine)
4083	    {
4084	    case EM_IA_64:
4085	      result = get_ia64_section_type_name (sh_type);
4086	      break;
4087	    default:
4088	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4089		result = get_solaris_section_type (sh_type);
4090	      else
4091		result = NULL;
4092	      break;
4093	    }
4094
4095	  if (result != NULL)
4096	    return result;
4097
4098	  sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4099	}
4100      else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4101	{
4102	  switch (elf_header.e_machine)
4103	    {
4104	    case EM_V800:
4105	    case EM_V850:
4106	    case EM_CYGNUS_V850:
4107	      result = get_v850_section_type_name (sh_type);
4108	      break;
4109	    default:
4110	      result = NULL;
4111	      break;
4112	    }
4113
4114	  if (result != NULL)
4115	    return result;
4116
4117	  sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4118	}
4119      else
4120	/* This message is probably going to be displayed in a 15
4121	   character wide field, so put the hex value first.  */
4122	snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4123
4124      return buff;
4125    }
4126}
4127
4128#define OPTION_DEBUG_DUMP	512
4129#define OPTION_DYN_SYMS		513
4130#define OPTION_DWARF_DEPTH	514
4131#define OPTION_DWARF_START	515
4132#define OPTION_DWARF_CHECK	516
4133
4134static struct option options[] =
4135{
4136  {"all",	       no_argument, 0, 'a'},
4137  {"file-header",      no_argument, 0, 'h'},
4138  {"program-headers",  no_argument, 0, 'l'},
4139  {"headers",	       no_argument, 0, 'e'},
4140  {"histogram",	       no_argument, 0, 'I'},
4141  {"segments",	       no_argument, 0, 'l'},
4142  {"sections",	       no_argument, 0, 'S'},
4143  {"section-headers",  no_argument, 0, 'S'},
4144  {"section-groups",   no_argument, 0, 'g'},
4145  {"section-details",  no_argument, 0, 't'},
4146  {"full-section-name",no_argument, 0, 'N'},
4147  {"symbols",	       no_argument, 0, 's'},
4148  {"syms",	       no_argument, 0, 's'},
4149  {"dyn-syms",	       no_argument, 0, OPTION_DYN_SYMS},
4150  {"relocs",	       no_argument, 0, 'r'},
4151  {"notes",	       no_argument, 0, 'n'},
4152  {"dynamic",	       no_argument, 0, 'd'},
4153  {"special-files",    no_argument, 0, 'f'},
4154  {"arch-specific",    no_argument, 0, 'A'},
4155  {"version-info",     no_argument, 0, 'V'},
4156  {"use-dynamic",      no_argument, 0, 'D'},
4157  {"unwind",	       no_argument, 0, 'u'},
4158  {"archive-index",    no_argument, 0, 'c'},
4159  {"hex-dump",	       required_argument, 0, 'x'},
4160  {"relocated-dump",   required_argument, 0, 'R'},
4161  {"string-dump",      required_argument, 0, 'p'},
4162  {"decompress",       no_argument, 0, 'z'},
4163#ifdef SUPPORT_DISASSEMBLY
4164  {"instruction-dump", required_argument, 0, 'i'},
4165#endif
4166  {"debug-dump",       optional_argument, 0, OPTION_DEBUG_DUMP},
4167
4168  {"dwarf-depth",      required_argument, 0, OPTION_DWARF_DEPTH},
4169  {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
4170  {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
4171
4172  {"version",	       no_argument, 0, 'v'},
4173  {"wide",	       no_argument, 0, 'W'},
4174  {"help",	       no_argument, 0, 'H'},
4175  {0,		       no_argument, 0, 0}
4176};
4177
4178static void
4179usage (FILE * stream)
4180{
4181  fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4182  fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4183  fprintf (stream, _(" Options are:\n\
4184  -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n\
4185  -h --file-header       Display the ELF file header\n\
4186  -l --program-headers   Display the program headers\n\
4187     --segments          An alias for --program-headers\n\
4188  -S --section-headers   Display the sections' header\n\
4189     --sections          An alias for --section-headers\n\
4190  -g --section-groups    Display the section groups\n\
4191  -t --section-details   Display the section details\n\
4192  -e --headers           Equivalent to: -h -l -S\n\
4193  -s --syms              Display the symbol table\n\
4194     --symbols           An alias for --syms\n\
4195  --dyn-syms             Display the dynamic symbol table\n\
4196  -n --notes             Display the core notes (if present)\n\
4197  -r --relocs            Display the relocations (if present)\n\
4198  -u --unwind            Display the unwind info (if present)\n\
4199  -d --dynamic           Display the dynamic section (if present)\n\
4200  -f --special-files     Process non-plain files too\n\
4201  -V --version-info      Display the version sections (if present)\n\
4202  -A --arch-specific     Display architecture specific information (if any)\n\
4203  -c --archive-index     Display the symbol/file index in an archive\n\
4204  -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
4205  -x --hex-dump=<number|name>\n\
4206                         Dump the contents of section <number|name> as bytes\n\
4207  -p --string-dump=<number|name>\n\
4208                         Dump the contents of section <number|name> as strings\n\
4209  -R --relocated-dump=<number|name>\n\
4210                         Dump the contents of section <number|name> as relocated bytes\n\
4211  -z --decompress        Decompress section before dumping it\n\
4212  -w[lLiaprmfFsoRt] or\n\
4213  --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
4214               =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
4215               =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
4216               =addr,=cu_index]\n\
4217                         Display the contents of DWARF2 debug sections\n"));
4218  fprintf (stream, _("\
4219  --dwarf-depth=N        Do not display DIEs at depth N or greater\n\
4220  --dwarf-start=N        Display DIEs starting with N, at the same depth\n\
4221                         or deeper\n"));
4222#ifdef SUPPORT_DISASSEMBLY
4223  fprintf (stream, _("\
4224  -i --instruction-dump=<number|name>\n\
4225                         Disassemble the contents of section <number|name>\n"));
4226#endif
4227  fprintf (stream, _("\
4228  -I --histogram         Display histogram of bucket list lengths\n\
4229  -W --wide              Allow output width to exceed 80 characters\n\
4230  @<file>                Read options from <file>\n\
4231  -H --help              Display this information\n\
4232  -v --version           Display the version number of readelf\n"));
4233
4234  if (REPORT_BUGS_TO[0] && stream == stdout)
4235    fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4236
4237  exit (stream == stdout ? 0 : 1);
4238}
4239
4240/* Record the fact that the user wants the contents of section number
4241   SECTION to be displayed using the method(s) encoded as flags bits
4242   in TYPE.  Note, TYPE can be zero if we are creating the array for
4243   the first time.  */
4244
4245static void
4246request_dump_bynumber (unsigned int section, dump_type type)
4247{
4248  if (section >= num_dump_sects)
4249    {
4250      dump_type * new_dump_sects;
4251
4252      new_dump_sects = (dump_type *) calloc (section + 1,
4253                                             sizeof (* dump_sects));
4254
4255      if (new_dump_sects == NULL)
4256	error (_("Out of memory allocating dump request table.\n"));
4257      else
4258	{
4259	  /* Copy current flag settings.  */
4260	  memcpy (new_dump_sects, dump_sects, num_dump_sects * sizeof (* dump_sects));
4261
4262	  free (dump_sects);
4263
4264	  dump_sects = new_dump_sects;
4265	  num_dump_sects = section + 1;
4266	}
4267    }
4268
4269  if (dump_sects)
4270    dump_sects[section] |= type;
4271
4272  return;
4273}
4274
4275/* Request a dump by section name.  */
4276
4277static void
4278request_dump_byname (const char * section, dump_type type)
4279{
4280  struct dump_list_entry * new_request;
4281
4282  new_request = (struct dump_list_entry *)
4283      malloc (sizeof (struct dump_list_entry));
4284  if (!new_request)
4285    error (_("Out of memory allocating dump request table.\n"));
4286
4287  new_request->name = strdup (section);
4288  if (!new_request->name)
4289    error (_("Out of memory allocating dump request table.\n"));
4290
4291  new_request->type = type;
4292
4293  new_request->next = dump_sects_byname;
4294  dump_sects_byname = new_request;
4295}
4296
4297static inline void
4298request_dump (dump_type type)
4299{
4300  int section;
4301  char * cp;
4302
4303  do_dump++;
4304  section = strtoul (optarg, & cp, 0);
4305
4306  if (! *cp && section >= 0)
4307    request_dump_bynumber (section, type);
4308  else
4309    request_dump_byname (optarg, type);
4310}
4311
4312
4313static void
4314parse_args (int argc, char ** argv)
4315{
4316  int c;
4317
4318  if (argc < 2)
4319    usage (stderr);
4320
4321  while ((c = getopt_long
4322	  (argc, argv, "ADHINR:SVWacdefghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4323    {
4324      switch (c)
4325	{
4326	case 0:
4327	  /* Long options.  */
4328	  break;
4329	case 'H':
4330	  usage (stdout);
4331	  break;
4332
4333	case 'a':
4334	  do_syms++;
4335	  do_reloc++;
4336	  do_unwind++;
4337	  do_dynamic++;
4338	  do_header++;
4339	  do_sections++;
4340	  do_section_groups++;
4341	  do_segments++;
4342	  do_version++;
4343	  do_histogram++;
4344	  do_arch++;
4345	  do_notes++;
4346	  break;
4347	case 'g':
4348	  do_section_groups++;
4349	  break;
4350	case 't':
4351	case 'N':
4352	  do_sections++;
4353	  do_section_details++;
4354	  break;
4355	case 'e':
4356	  do_header++;
4357	  do_sections++;
4358	  do_segments++;
4359	  break;
4360	case 'A':
4361	  do_arch++;
4362	  break;
4363	case 'D':
4364	  do_using_dynamic++;
4365	  break;
4366	case 'r':
4367	  do_reloc++;
4368	  break;
4369	case 'u':
4370	  do_unwind++;
4371	  break;
4372	case 'f':
4373	  do_special_files++;
4374	  break;
4375	case 'h':
4376	  do_header++;
4377	  break;
4378	case 'l':
4379	  do_segments++;
4380	  break;
4381	case 's':
4382	  do_syms++;
4383	  break;
4384	case 'S':
4385	  do_sections++;
4386	  break;
4387	case 'd':
4388	  do_dynamic++;
4389	  break;
4390	case 'I':
4391	  do_histogram++;
4392	  break;
4393	case 'n':
4394	  do_notes++;
4395	  break;
4396	case 'c':
4397	  do_archive_index++;
4398	  break;
4399	case 'x':
4400	  request_dump (HEX_DUMP);
4401	  break;
4402	case 'p':
4403	  request_dump (STRING_DUMP);
4404	  break;
4405	case 'R':
4406	  request_dump (RELOC_DUMP);
4407	  break;
4408	case 'z':
4409	  decompress_dumps++;
4410	  break;
4411	case 'w':
4412	  do_dump++;
4413	  if (optarg == 0)
4414	    {
4415	      do_debugging = 1;
4416	      dwarf_select_sections_all ();
4417	    }
4418	  else
4419	    {
4420	      do_debugging = 0;
4421	      dwarf_select_sections_by_letters (optarg);
4422	    }
4423	  break;
4424	case OPTION_DEBUG_DUMP:
4425	  do_dump++;
4426	  if (optarg == 0)
4427	    do_debugging = 1;
4428	  else
4429	    {
4430	      do_debugging = 0;
4431	      dwarf_select_sections_by_names (optarg);
4432	    }
4433	  break;
4434	case OPTION_DWARF_DEPTH:
4435	  {
4436	    char *cp;
4437
4438	    dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4439	  }
4440	  break;
4441	case OPTION_DWARF_START:
4442	  {
4443	    char *cp;
4444
4445	    dwarf_start_die = strtoul (optarg, & cp, 0);
4446	  }
4447	  break;
4448	case OPTION_DWARF_CHECK:
4449	  dwarf_check = 1;
4450	  break;
4451	case OPTION_DYN_SYMS:
4452	  do_dyn_syms++;
4453	  break;
4454#ifdef SUPPORT_DISASSEMBLY
4455	case 'i':
4456	  request_dump (DISASS_DUMP);
4457	  break;
4458#endif
4459	case 'v':
4460	  print_version (program_name);
4461	  break;
4462	case 'V':
4463	  do_version++;
4464	  break;
4465	case 'W':
4466	  do_wide++;
4467	  break;
4468	default:
4469	  /* xgettext:c-format */
4470	  error (_("Invalid option '-%c'\n"), c);
4471	  /* Drop through.  */
4472	case '?':
4473	  usage (stderr);
4474	}
4475    }
4476
4477  if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
4478      && !do_segments && !do_header && !do_dump && !do_version
4479      && !do_histogram && !do_debugging && !do_arch && !do_notes
4480      && !do_section_groups && !do_archive_index
4481      && !do_dyn_syms)
4482    usage (stderr);
4483}
4484
4485static const char *
4486get_elf_class (unsigned int elf_class)
4487{
4488  static char buff[32];
4489
4490  switch (elf_class)
4491    {
4492    case ELFCLASSNONE: return _("none");
4493    case ELFCLASS32:   return "ELF32";
4494    case ELFCLASS64:   return "ELF64";
4495    default:
4496      snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
4497      return buff;
4498    }
4499}
4500
4501static const char *
4502get_data_encoding (unsigned int encoding)
4503{
4504  static char buff[32];
4505
4506  switch (encoding)
4507    {
4508    case ELFDATANONE: return _("none");
4509    case ELFDATA2LSB: return _("2's complement, little endian");
4510    case ELFDATA2MSB: return _("2's complement, big endian");
4511    default:
4512      snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
4513      return buff;
4514    }
4515}
4516
4517/* Decode the data held in 'elf_header'.  */
4518
4519static int
4520process_file_header (void)
4521{
4522  if (   elf_header.e_ident[EI_MAG0] != ELFMAG0
4523      || elf_header.e_ident[EI_MAG1] != ELFMAG1
4524      || elf_header.e_ident[EI_MAG2] != ELFMAG2
4525      || elf_header.e_ident[EI_MAG3] != ELFMAG3)
4526    {
4527      error
4528	(_("Not an ELF file - it has the wrong magic bytes at the start\n"));
4529      return 0;
4530    }
4531
4532  init_dwarf_regnames (elf_header.e_machine);
4533
4534  if (do_header)
4535    {
4536      int i;
4537
4538      printf (_("ELF Header:\n"));
4539      printf (_("  Magic:   "));
4540      for (i = 0; i < EI_NIDENT; i++)
4541	printf ("%2.2x ", elf_header.e_ident[i]);
4542      printf ("\n");
4543      printf (_("  Class:                             %s\n"),
4544	      get_elf_class (elf_header.e_ident[EI_CLASS]));
4545      printf (_("  Data:                              %s\n"),
4546	      get_data_encoding (elf_header.e_ident[EI_DATA]));
4547      printf (_("  Version:                           %d %s\n"),
4548	      elf_header.e_ident[EI_VERSION],
4549	      (elf_header.e_ident[EI_VERSION] == EV_CURRENT
4550	       ? "(current)"
4551	       : (elf_header.e_ident[EI_VERSION] != EV_NONE
4552		  ? _("<unknown: %lx>")
4553		  : "")));
4554      printf (_("  OS/ABI:                            %s\n"),
4555	      get_osabi_name (elf_header.e_ident[EI_OSABI]));
4556      printf (_("  ABI Version:                       %d\n"),
4557	      elf_header.e_ident[EI_ABIVERSION]);
4558      printf (_("  Type:                              %s\n"),
4559	      get_file_type (elf_header.e_type));
4560      printf (_("  Machine:                           %s\n"),
4561	      get_machine_name (elf_header.e_machine));
4562      printf (_("  Version:                           0x%lx\n"),
4563	      (unsigned long) elf_header.e_version);
4564
4565      printf (_("  Entry point address:               "));
4566      print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
4567      printf (_("\n  Start of program headers:          "));
4568      print_vma ((bfd_vma) elf_header.e_phoff, DEC);
4569      printf (_(" (bytes into file)\n  Start of section headers:          "));
4570      print_vma ((bfd_vma) elf_header.e_shoff, DEC);
4571      printf (_(" (bytes into file)\n"));
4572
4573      printf (_("  Flags:                             0x%lx%s\n"),
4574	      (unsigned long) elf_header.e_flags,
4575	      get_machine_flags (elf_header.e_flags, elf_header.e_machine));
4576      printf (_("  Size of this header:               %ld (bytes)\n"),
4577	      (long) elf_header.e_ehsize);
4578      printf (_("  Size of program headers:           %ld (bytes)\n"),
4579	      (long) elf_header.e_phentsize);
4580      printf (_("  Number of program headers:         %ld"),
4581	      (long) elf_header.e_phnum);
4582      if (section_headers != NULL
4583	  && elf_header.e_phnum == PN_XNUM
4584	  && section_headers[0].sh_info != 0)
4585	printf (" (%ld)", (long) section_headers[0].sh_info);
4586      putc ('\n', stdout);
4587      printf (_("  Size of section headers:           %ld (bytes)\n"),
4588	      (long) elf_header.e_shentsize);
4589      printf (_("  Number of section headers:         %ld"),
4590	      (long) elf_header.e_shnum);
4591      if (section_headers != NULL && elf_header.e_shnum == SHN_UNDEF)
4592	printf (" (%ld)", (long) section_headers[0].sh_size);
4593      putc ('\n', stdout);
4594      printf (_("  Section header string table index: %ld"),
4595	      (long) elf_header.e_shstrndx);
4596      if (section_headers != NULL
4597	  && elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
4598	printf (" (%u)", section_headers[0].sh_link);
4599      else if (elf_header.e_shstrndx != SHN_UNDEF
4600	       && elf_header.e_shstrndx >= elf_header.e_shnum)
4601	printf (_(" <corrupt: out of range>"));
4602      putc ('\n', stdout);
4603    }
4604
4605  if (section_headers != NULL)
4606    {
4607      if (elf_header.e_phnum == PN_XNUM
4608	  && section_headers[0].sh_info != 0)
4609	elf_header.e_phnum = section_headers[0].sh_info;
4610      if (elf_header.e_shnum == SHN_UNDEF)
4611	elf_header.e_shnum = section_headers[0].sh_size;
4612      if (elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
4613	elf_header.e_shstrndx = section_headers[0].sh_link;
4614      else if (elf_header.e_shstrndx >= elf_header.e_shnum)
4615	elf_header.e_shstrndx = SHN_UNDEF;
4616      free (section_headers);
4617      section_headers = NULL;
4618    }
4619
4620  return 1;
4621}
4622
4623static bfd_boolean
4624get_32bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
4625{
4626  Elf32_External_Phdr * phdrs;
4627  Elf32_External_Phdr * external;
4628  Elf_Internal_Phdr *   internal;
4629  unsigned int i;
4630  unsigned int size = elf_header.e_phentsize;
4631  unsigned int num  = elf_header.e_phnum;
4632
4633  /* PR binutils/17531: Cope with unexpected section header sizes.  */
4634  if (size == 0 || num == 0)
4635    return FALSE;
4636  if (size < sizeof * phdrs)
4637    {
4638      error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4639      return FALSE;
4640    }
4641  if (size > sizeof * phdrs)
4642    warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4643
4644  phdrs = (Elf32_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
4645                                            size, num, _("program headers"));
4646  if (phdrs == NULL)
4647    return FALSE;
4648
4649  for (i = 0, internal = pheaders, external = phdrs;
4650       i < elf_header.e_phnum;
4651       i++, internal++, external++)
4652    {
4653      internal->p_type   = BYTE_GET (external->p_type);
4654      internal->p_offset = BYTE_GET (external->p_offset);
4655      internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4656      internal->p_paddr  = BYTE_GET (external->p_paddr);
4657      internal->p_filesz = BYTE_GET (external->p_filesz);
4658      internal->p_memsz  = BYTE_GET (external->p_memsz);
4659      internal->p_flags  = BYTE_GET (external->p_flags);
4660      internal->p_align  = BYTE_GET (external->p_align);
4661    }
4662
4663  free (phdrs);
4664  return TRUE;
4665}
4666
4667static bfd_boolean
4668get_64bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
4669{
4670  Elf64_External_Phdr * phdrs;
4671  Elf64_External_Phdr * external;
4672  Elf_Internal_Phdr *   internal;
4673  unsigned int i;
4674  unsigned int size = elf_header.e_phentsize;
4675  unsigned int num  = elf_header.e_phnum;
4676
4677  /* PR binutils/17531: Cope with unexpected section header sizes.  */
4678  if (size == 0 || num == 0)
4679    return FALSE;
4680  if (size < sizeof * phdrs)
4681    {
4682      error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4683      return FALSE;
4684    }
4685  if (size > sizeof * phdrs)
4686    warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4687
4688  phdrs = (Elf64_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
4689                                            size, num, _("program headers"));
4690  if (!phdrs)
4691    return FALSE;
4692
4693  for (i = 0, internal = pheaders, external = phdrs;
4694       i < elf_header.e_phnum;
4695       i++, internal++, external++)
4696    {
4697      internal->p_type   = BYTE_GET (external->p_type);
4698      internal->p_flags  = BYTE_GET (external->p_flags);
4699      internal->p_offset = BYTE_GET (external->p_offset);
4700      internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4701      internal->p_paddr  = BYTE_GET (external->p_paddr);
4702      internal->p_filesz = BYTE_GET (external->p_filesz);
4703      internal->p_memsz  = BYTE_GET (external->p_memsz);
4704      internal->p_align  = BYTE_GET (external->p_align);
4705    }
4706
4707  free (phdrs);
4708  return TRUE;
4709}
4710
4711/* Returns 1 if the program headers were read into `program_headers'.  */
4712
4713static int
4714get_program_headers (FILE * file)
4715{
4716  Elf_Internal_Phdr * phdrs;
4717
4718  /* Check cache of prior read.  */
4719  if (program_headers != NULL)
4720    return 1;
4721
4722  phdrs = (Elf_Internal_Phdr *) cmalloc (elf_header.e_phnum,
4723                                         sizeof (Elf_Internal_Phdr));
4724
4725  if (phdrs == NULL)
4726    {
4727      error (_("Out of memory reading %u program headers\n"),
4728	     elf_header.e_phnum);
4729      return 0;
4730    }
4731
4732  if (is_32bit_elf
4733      ? get_32bit_program_headers (file, phdrs)
4734      : get_64bit_program_headers (file, phdrs))
4735    {
4736      program_headers = phdrs;
4737      return 1;
4738    }
4739
4740  free (phdrs);
4741  return 0;
4742}
4743
4744/* Returns 1 if the program headers were loaded.  */
4745
4746static int
4747process_program_headers (FILE * file)
4748{
4749  Elf_Internal_Phdr * segment;
4750  unsigned int i;
4751
4752  if (elf_header.e_phnum == 0)
4753    {
4754      /* PR binutils/12467.  */
4755      if (elf_header.e_phoff != 0)
4756	warn (_("possibly corrupt ELF header - it has a non-zero program"
4757		" header offset, but no program headers\n"));
4758      else if (do_segments)
4759	printf (_("\nThere are no program headers in this file.\n"));
4760      return 0;
4761    }
4762
4763  if (do_segments && !do_header)
4764    {
4765      printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
4766      printf (_("Entry point "));
4767      print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
4768      printf (_("\nThere are %d program headers, starting at offset "),
4769	      elf_header.e_phnum);
4770      print_vma ((bfd_vma) elf_header.e_phoff, DEC);
4771      printf ("\n");
4772    }
4773
4774  if (! get_program_headers (file))
4775      return 0;
4776
4777  if (do_segments)
4778    {
4779      if (elf_header.e_phnum > 1)
4780	printf (_("\nProgram Headers:\n"));
4781      else
4782	printf (_("\nProgram Header:\n"));
4783
4784      if (is_32bit_elf)
4785	printf
4786	  (_("  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n"));
4787      else if (do_wide)
4788	printf
4789	  (_("  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align\n"));
4790      else
4791	{
4792	  printf
4793	    (_("  Type           Offset             VirtAddr           PhysAddr\n"));
4794	  printf
4795	    (_("                 FileSiz            MemSiz              Flags  Align\n"));
4796	}
4797    }
4798
4799  dynamic_addr = 0;
4800  dynamic_size = 0;
4801
4802  for (i = 0, segment = program_headers;
4803       i < elf_header.e_phnum;
4804       i++, segment++)
4805    {
4806      if (do_segments)
4807	{
4808	  printf ("  %-14.14s ", get_segment_type (segment->p_type));
4809
4810	  if (is_32bit_elf)
4811	    {
4812	      printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
4813	      printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
4814	      printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
4815	      printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
4816	      printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
4817	      printf ("%c%c%c ",
4818		      (segment->p_flags & PF_R ? 'R' : ' '),
4819		      (segment->p_flags & PF_W ? 'W' : ' '),
4820		      (segment->p_flags & PF_X ? 'E' : ' '));
4821	      printf ("%#lx", (unsigned long) segment->p_align);
4822	    }
4823	  else if (do_wide)
4824	    {
4825	      if ((unsigned long) segment->p_offset == segment->p_offset)
4826		printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
4827	      else
4828		{
4829		  print_vma (segment->p_offset, FULL_HEX);
4830		  putchar (' ');
4831		}
4832
4833	      print_vma (segment->p_vaddr, FULL_HEX);
4834	      putchar (' ');
4835	      print_vma (segment->p_paddr, FULL_HEX);
4836	      putchar (' ');
4837
4838	      if ((unsigned long) segment->p_filesz == segment->p_filesz)
4839		printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
4840	      else
4841		{
4842		  print_vma (segment->p_filesz, FULL_HEX);
4843		  putchar (' ');
4844		}
4845
4846	      if ((unsigned long) segment->p_memsz == segment->p_memsz)
4847		printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
4848	      else
4849		{
4850		  print_vma (segment->p_memsz, FULL_HEX);
4851		}
4852
4853	      printf (" %c%c%c ",
4854		      (segment->p_flags & PF_R ? 'R' : ' '),
4855		      (segment->p_flags & PF_W ? 'W' : ' '),
4856		      (segment->p_flags & PF_X ? 'E' : ' '));
4857
4858	      if ((unsigned long) segment->p_align == segment->p_align)
4859		printf ("%#lx", (unsigned long) segment->p_align);
4860	      else
4861		{
4862		  print_vma (segment->p_align, PREFIX_HEX);
4863		}
4864	    }
4865	  else
4866	    {
4867	      print_vma (segment->p_offset, FULL_HEX);
4868	      putchar (' ');
4869	      print_vma (segment->p_vaddr, FULL_HEX);
4870	      putchar (' ');
4871	      print_vma (segment->p_paddr, FULL_HEX);
4872	      printf ("\n                 ");
4873	      print_vma (segment->p_filesz, FULL_HEX);
4874	      putchar (' ');
4875	      print_vma (segment->p_memsz, FULL_HEX);
4876	      printf ("  %c%c%c    ",
4877		      (segment->p_flags & PF_R ? 'R' : ' '),
4878		      (segment->p_flags & PF_W ? 'W' : ' '),
4879		      (segment->p_flags & PF_X ? 'E' : ' '));
4880	      print_vma (segment->p_align, HEX);
4881	    }
4882	}
4883
4884      if (do_segments)
4885	putc ('\n', stdout);
4886
4887      switch (segment->p_type)
4888	{
4889	case PT_DYNAMIC:
4890	  if (dynamic_addr)
4891	    error (_("more than one dynamic segment\n"));
4892
4893	  /* By default, assume that the .dynamic section is the first
4894	     section in the DYNAMIC segment.  */
4895	  dynamic_addr = segment->p_offset;
4896	  dynamic_size = segment->p_filesz;
4897	  /* PR binutils/17512: Avoid corrupt dynamic section info in the segment.  */
4898	  if (dynamic_addr + dynamic_size >= current_file_size)
4899	    {
4900	      error (_("the dynamic segment offset + size exceeds the size of the file\n"));
4901	      dynamic_addr = dynamic_size = 0;
4902	    }
4903
4904	  /* Try to locate the .dynamic section. If there is
4905	     a section header table, we can easily locate it.  */
4906	  if (section_headers != NULL)
4907	    {
4908	      Elf_Internal_Shdr * sec;
4909
4910	      sec = find_section (".dynamic");
4911	      if (sec == NULL || sec->sh_size == 0)
4912		{
4913                  /* A corresponding .dynamic section is expected, but on
4914                     IA-64/OpenVMS it is OK for it to be missing.  */
4915                  if (!is_ia64_vms ())
4916                    error (_("no .dynamic section in the dynamic segment\n"));
4917		  break;
4918		}
4919
4920	      if (sec->sh_type == SHT_NOBITS)
4921		{
4922		  dynamic_size = 0;
4923		  break;
4924		}
4925
4926	      dynamic_addr = sec->sh_offset;
4927	      dynamic_size = sec->sh_size;
4928
4929	      if (dynamic_addr < segment->p_offset
4930		  || dynamic_addr > segment->p_offset + segment->p_filesz)
4931		warn (_("the .dynamic section is not contained"
4932			" within the dynamic segment\n"));
4933	      else if (dynamic_addr > segment->p_offset)
4934		warn (_("the .dynamic section is not the first section"
4935			" in the dynamic segment.\n"));
4936	    }
4937	  break;
4938
4939	case PT_INTERP:
4940	  if (fseek (file, archive_file_offset + (long) segment->p_offset,
4941		     SEEK_SET))
4942	    error (_("Unable to find program interpreter name\n"));
4943	  else
4944	    {
4945	      char fmt [32];
4946	      int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
4947
4948	      if (ret >= (int) sizeof (fmt) || ret < 0)
4949		error (_("Internal error: failed to create format string to display program interpreter\n"));
4950
4951	      program_interpreter[0] = 0;
4952	      if (fscanf (file, fmt, program_interpreter) <= 0)
4953		error (_("Unable to read program interpreter name\n"));
4954
4955	      if (do_segments)
4956		printf (_("      [Requesting program interpreter: %s]\n"),
4957		    program_interpreter);
4958	    }
4959	  break;
4960	}
4961    }
4962
4963  if (do_segments && section_headers != NULL && string_table != NULL)
4964    {
4965      printf (_("\n Section to Segment mapping:\n"));
4966      printf (_("  Segment Sections...\n"));
4967
4968      for (i = 0; i < elf_header.e_phnum; i++)
4969	{
4970	  unsigned int j;
4971	  Elf_Internal_Shdr * section;
4972
4973	  segment = program_headers + i;
4974	  section = section_headers + 1;
4975
4976	  printf ("   %2.2d     ", i);
4977
4978	  for (j = 1; j < elf_header.e_shnum; j++, section++)
4979	    {
4980	      if (!ELF_TBSS_SPECIAL (section, segment)
4981		  && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
4982		printf ("%s ", printable_section_name (section));
4983	    }
4984
4985	  putc ('\n',stdout);
4986	}
4987    }
4988
4989  return 1;
4990}
4991
4992
4993/* Find the file offset corresponding to VMA by using the program headers.  */
4994
4995static long
4996offset_from_vma (FILE * file, bfd_vma vma, bfd_size_type size)
4997{
4998  Elf_Internal_Phdr * seg;
4999
5000  if (! get_program_headers (file))
5001    {
5002      warn (_("Cannot interpret virtual addresses without program headers.\n"));
5003      return (long) vma;
5004    }
5005
5006  for (seg = program_headers;
5007       seg < program_headers + elf_header.e_phnum;
5008       ++seg)
5009    {
5010      if (seg->p_type != PT_LOAD)
5011	continue;
5012
5013      if (vma >= (seg->p_vaddr & -seg->p_align)
5014	  && vma + size <= seg->p_vaddr + seg->p_filesz)
5015	return vma - seg->p_vaddr + seg->p_offset;
5016    }
5017
5018  warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5019	(unsigned long) vma);
5020  return (long) vma;
5021}
5022
5023
5024/* Allocate memory and load the sections headers into the global pointer
5025   SECTION_HEADERS.  If PROBE is true, this is just a probe and we do not
5026   generate any error messages if the load fails.  */
5027
5028static bfd_boolean
5029get_32bit_section_headers (FILE * file, bfd_boolean probe)
5030{
5031  Elf32_External_Shdr * shdrs;
5032  Elf_Internal_Shdr *   internal;
5033  unsigned int i;
5034  unsigned int size = elf_header.e_shentsize;
5035  unsigned int num = probe ? 1 : elf_header.e_shnum;
5036
5037  /* PR binutils/17531: Cope with unexpected section header sizes.  */
5038  if (size == 0 || num == 0)
5039    return FALSE;
5040  if (size < sizeof * shdrs)
5041    {
5042      if (! probe)
5043	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5044      return FALSE;
5045    }
5046  if (!probe && size > sizeof * shdrs)
5047    warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5048
5049  shdrs = (Elf32_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
5050                                            size, num,
5051					    probe ? NULL : _("section headers"));
5052  if (shdrs == NULL)
5053    return FALSE;
5054
5055  if (section_headers != NULL)
5056    free (section_headers);
5057  section_headers = (Elf_Internal_Shdr *) cmalloc (num,
5058                                                   sizeof (Elf_Internal_Shdr));
5059  if (section_headers == NULL)
5060    {
5061      if (!probe)
5062	error (_("Out of memory reading %u section headers\n"), num);
5063      return FALSE;
5064    }
5065
5066  for (i = 0, internal = section_headers;
5067       i < num;
5068       i++, internal++)
5069    {
5070      internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
5071      internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
5072      internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
5073      internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
5074      internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
5075      internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
5076      internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
5077      internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
5078      internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5079      internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
5080      if (!probe && internal->sh_link > num)
5081	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5082      if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5083	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5084    }
5085
5086  free (shdrs);
5087  return TRUE;
5088}
5089
5090static bfd_boolean
5091get_64bit_section_headers (FILE * file, bfd_boolean probe)
5092{
5093  Elf64_External_Shdr * shdrs;
5094  Elf_Internal_Shdr *   internal;
5095  unsigned int i;
5096  unsigned int size = elf_header.e_shentsize;
5097  unsigned int num = probe ? 1 : elf_header.e_shnum;
5098
5099  /* PR binutils/17531: Cope with unexpected section header sizes.  */
5100  if (size == 0 || num == 0)
5101    return FALSE;
5102  if (size < sizeof * shdrs)
5103    {
5104      if (! probe)
5105	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5106      return FALSE;
5107    }
5108  if (! probe && size > sizeof * shdrs)
5109    warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5110
5111  shdrs = (Elf64_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
5112                                            size, num,
5113					    probe ? NULL : _("section headers"));
5114  if (shdrs == NULL)
5115    return FALSE;
5116
5117  if (section_headers != NULL)
5118    free (section_headers);
5119  section_headers = (Elf_Internal_Shdr *) cmalloc (num,
5120                                                   sizeof (Elf_Internal_Shdr));
5121  if (section_headers == NULL)
5122    {
5123      if (! probe)
5124	error (_("Out of memory reading %u section headers\n"), num);
5125      return FALSE;
5126    }
5127
5128  for (i = 0, internal = section_headers;
5129       i < num;
5130       i++, internal++)
5131    {
5132      internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
5133      internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
5134      internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
5135      internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
5136      internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
5137      internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
5138      internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
5139      internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
5140      internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
5141      internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5142      if (!probe && internal->sh_link > num)
5143	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5144      if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5145	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5146    }
5147
5148  free (shdrs);
5149  return TRUE;
5150}
5151
5152static Elf_Internal_Sym *
5153get_32bit_elf_symbols (FILE * file,
5154		       Elf_Internal_Shdr * section,
5155		       unsigned long * num_syms_return)
5156{
5157  unsigned long number = 0;
5158  Elf32_External_Sym * esyms = NULL;
5159  Elf_External_Sym_Shndx * shndx = NULL;
5160  Elf_Internal_Sym * isyms = NULL;
5161  Elf_Internal_Sym * psym;
5162  unsigned int j;
5163
5164  if (section->sh_size == 0)
5165    {
5166      if (num_syms_return != NULL)
5167	* num_syms_return = 0;
5168      return NULL;
5169    }
5170
5171  /* Run some sanity checks first.  */
5172  if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5173    {
5174      error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5175	     printable_section_name (section), (unsigned long) section->sh_entsize);
5176      goto exit_point;
5177    }
5178
5179  if (section->sh_size > current_file_size)
5180    {
5181      error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5182	     printable_section_name (section), (unsigned long) section->sh_size);
5183      goto exit_point;
5184    }
5185
5186  number = section->sh_size / section->sh_entsize;
5187
5188  if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5189    {
5190      error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5191	     (unsigned long) section->sh_size,
5192	     printable_section_name (section),
5193	     (unsigned long) section->sh_entsize);
5194      goto exit_point;
5195    }
5196
5197  esyms = (Elf32_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
5198                                           section->sh_size, _("symbols"));
5199  if (esyms == NULL)
5200    goto exit_point;
5201
5202  {
5203    elf_section_list * entry;
5204
5205    shndx = NULL;
5206    for (entry = symtab_shndx_list; entry != NULL; entry = entry->next)
5207      if (entry->hdr->sh_link == (unsigned long) (section - section_headers))
5208	{
5209	  shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
5210						       entry->hdr->sh_offset,
5211						       1, entry->hdr->sh_size,
5212						       _("symbol table section indicies"));
5213	  if (shndx == NULL)
5214	    goto exit_point;
5215	  /* PR17531: file: heap-buffer-overflow */
5216	  else if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5217	    {
5218	      error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5219		     printable_section_name (entry->hdr),
5220		     (unsigned long) entry->hdr->sh_size,
5221		     (unsigned long) section->sh_size);
5222	      goto exit_point;
5223	    }
5224	}
5225  }
5226
5227  isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5228
5229  if (isyms == NULL)
5230    {
5231      error (_("Out of memory reading %lu symbols\n"),
5232	     (unsigned long) number);
5233      goto exit_point;
5234    }
5235
5236  for (j = 0, psym = isyms; j < number; j++, psym++)
5237    {
5238      psym->st_name  = BYTE_GET (esyms[j].st_name);
5239      psym->st_value = BYTE_GET (esyms[j].st_value);
5240      psym->st_size  = BYTE_GET (esyms[j].st_size);
5241      psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5242      if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5243	psym->st_shndx
5244	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5245      else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5246	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5247      psym->st_info  = BYTE_GET (esyms[j].st_info);
5248      psym->st_other = BYTE_GET (esyms[j].st_other);
5249    }
5250
5251 exit_point:
5252  if (shndx != NULL)
5253    free (shndx);
5254  if (esyms != NULL)
5255    free (esyms);
5256
5257  if (num_syms_return != NULL)
5258    * num_syms_return = isyms == NULL ? 0 : number;
5259
5260  return isyms;
5261}
5262
5263static Elf_Internal_Sym *
5264get_64bit_elf_symbols (FILE * file,
5265		       Elf_Internal_Shdr * section,
5266		       unsigned long * num_syms_return)
5267{
5268  unsigned long number = 0;
5269  Elf64_External_Sym * esyms = NULL;
5270  Elf_External_Sym_Shndx * shndx = NULL;
5271  Elf_Internal_Sym * isyms = NULL;
5272  Elf_Internal_Sym * psym;
5273  unsigned int j;
5274
5275  if (section->sh_size == 0)
5276    {
5277      if (num_syms_return != NULL)
5278	* num_syms_return = 0;
5279      return NULL;
5280    }
5281
5282  /* Run some sanity checks first.  */
5283  if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5284    {
5285      error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5286	     printable_section_name (section),
5287	     (unsigned long) section->sh_entsize);
5288      goto exit_point;
5289    }
5290
5291  if (section->sh_size > current_file_size)
5292    {
5293      error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5294	     printable_section_name (section),
5295	     (unsigned long) section->sh_size);
5296      goto exit_point;
5297    }
5298
5299  number = section->sh_size / section->sh_entsize;
5300
5301  if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
5302    {
5303      error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5304	     (unsigned long) section->sh_size,
5305	     printable_section_name (section),
5306	     (unsigned long) section->sh_entsize);
5307      goto exit_point;
5308    }
5309
5310  esyms = (Elf64_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
5311                                           section->sh_size, _("symbols"));
5312  if (!esyms)
5313    goto exit_point;
5314
5315  {
5316    elf_section_list * entry;
5317
5318    shndx = NULL;
5319    for (entry = symtab_shndx_list; entry != NULL; entry = entry->next)
5320      if (entry->hdr->sh_link == (unsigned long) (section - section_headers))
5321	{
5322	  shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
5323						       entry->hdr->sh_offset,
5324						       1, entry->hdr->sh_size,
5325						       _("symbol table section indicies"));
5326	  if (shndx == NULL)
5327	    goto exit_point;
5328	  /* PR17531: file: heap-buffer-overflow */
5329	  else if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5330	    {
5331	      error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5332		     printable_section_name (entry->hdr),
5333		     (unsigned long) entry->hdr->sh_size,
5334		     (unsigned long) section->sh_size);
5335	      goto exit_point;
5336	    }
5337	}
5338  }
5339
5340  isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5341
5342  if (isyms == NULL)
5343    {
5344      error (_("Out of memory reading %lu symbols\n"),
5345	     (unsigned long) number);
5346      goto exit_point;
5347    }
5348
5349  for (j = 0, psym = isyms; j < number; j++, psym++)
5350    {
5351      psym->st_name  = BYTE_GET (esyms[j].st_name);
5352      psym->st_info  = BYTE_GET (esyms[j].st_info);
5353      psym->st_other = BYTE_GET (esyms[j].st_other);
5354      psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5355
5356      if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5357	psym->st_shndx
5358	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5359      else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5360	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5361
5362      psym->st_value = BYTE_GET (esyms[j].st_value);
5363      psym->st_size  = BYTE_GET (esyms[j].st_size);
5364    }
5365
5366 exit_point:
5367  if (shndx != NULL)
5368    free (shndx);
5369  if (esyms != NULL)
5370    free (esyms);
5371
5372  if (num_syms_return != NULL)
5373    * num_syms_return = isyms == NULL ? 0 : number;
5374
5375  return isyms;
5376}
5377
5378static const char *
5379get_elf_section_flags (bfd_vma sh_flags)
5380{
5381  static char buff[1024];
5382  char * p = buff;
5383  int field_size = is_32bit_elf ? 8 : 16;
5384  int sindex;
5385  int size = sizeof (buff) - (field_size + 4 + 1);
5386  bfd_vma os_flags = 0;
5387  bfd_vma proc_flags = 0;
5388  bfd_vma unknown_flags = 0;
5389  static const struct
5390    {
5391      const char * str;
5392      int len;
5393    }
5394  flags [] =
5395    {
5396      /*  0 */ { STRING_COMMA_LEN ("WRITE") },
5397      /*  1 */ { STRING_COMMA_LEN ("ALLOC") },
5398      /*  2 */ { STRING_COMMA_LEN ("EXEC") },
5399      /*  3 */ { STRING_COMMA_LEN ("MERGE") },
5400      /*  4 */ { STRING_COMMA_LEN ("STRINGS") },
5401      /*  5 */ { STRING_COMMA_LEN ("INFO LINK") },
5402      /*  6 */ { STRING_COMMA_LEN ("LINK ORDER") },
5403      /*  7 */ { STRING_COMMA_LEN ("OS NONCONF") },
5404      /*  8 */ { STRING_COMMA_LEN ("GROUP") },
5405      /*  9 */ { STRING_COMMA_LEN ("TLS") },
5406      /* IA-64 specific.  */
5407      /* 10 */ { STRING_COMMA_LEN ("SHORT") },
5408      /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
5409      /* IA-64 OpenVMS specific.  */
5410      /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
5411      /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
5412      /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
5413      /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
5414      /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
5415      /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
5416      /* Generic.  */
5417      /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
5418      /* SPARC specific.  */
5419      /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
5420      /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
5421      /* ARM specific.  */
5422      /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
5423      /* 22 */ { STRING_COMMA_LEN ("ARM_NOREAD") },
5424      /* 23 */ { STRING_COMMA_LEN ("COMDEF") }
5425    };
5426
5427  if (do_section_details)
5428    {
5429      sprintf (buff, "[%*.*lx]: ",
5430	       field_size, field_size, (unsigned long) sh_flags);
5431      p += field_size + 4;
5432    }
5433
5434  while (sh_flags)
5435    {
5436      bfd_vma flag;
5437
5438      flag = sh_flags & - sh_flags;
5439      sh_flags &= ~ flag;
5440
5441      if (do_section_details)
5442	{
5443	  switch (flag)
5444	    {
5445	    case SHF_WRITE:		sindex = 0; break;
5446	    case SHF_ALLOC:		sindex = 1; break;
5447	    case SHF_EXECINSTR:		sindex = 2; break;
5448	    case SHF_MERGE:		sindex = 3; break;
5449	    case SHF_STRINGS:		sindex = 4; break;
5450	    case SHF_INFO_LINK:		sindex = 5; break;
5451	    case SHF_LINK_ORDER:	sindex = 6; break;
5452	    case SHF_OS_NONCONFORMING:	sindex = 7; break;
5453	    case SHF_GROUP:		sindex = 8; break;
5454	    case SHF_TLS:		sindex = 9; break;
5455	    case SHF_EXCLUDE:		sindex = 18; break;
5456	    case SHF_COMPRESSED:	sindex = 20; break;
5457
5458	    default:
5459	      sindex = -1;
5460	      switch (elf_header.e_machine)
5461		{
5462		case EM_IA_64:
5463		  if (flag == SHF_IA_64_SHORT)
5464		    sindex = 10;
5465		  else if (flag == SHF_IA_64_NORECOV)
5466		    sindex = 11;
5467#ifdef BFD64
5468		  else if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
5469		    switch (flag)
5470		      {
5471		      case SHF_IA_64_VMS_GLOBAL:      sindex = 12; break;
5472		      case SHF_IA_64_VMS_OVERLAID:    sindex = 13; break;
5473		      case SHF_IA_64_VMS_SHARED:      sindex = 14; break;
5474		      case SHF_IA_64_VMS_VECTOR:      sindex = 15; break;
5475		      case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
5476		      case SHF_IA_64_VMS_PROTECTED:   sindex = 17; break;
5477		      default:                        break;
5478		      }
5479#endif
5480		  break;
5481
5482		case EM_386:
5483		case EM_IAMCU:
5484		case EM_X86_64:
5485		case EM_L1OM:
5486		case EM_K1OM:
5487		case EM_OLD_SPARCV9:
5488		case EM_SPARC32PLUS:
5489		case EM_SPARCV9:
5490		case EM_SPARC:
5491		  if (flag == SHF_ORDERED)
5492		    sindex = 19;
5493		  break;
5494
5495		case EM_ARM:
5496		  switch (flag)
5497		    {
5498		    case SHF_ENTRYSECT: sindex = 21; break;
5499		    case SHF_ARM_NOREAD: sindex = 22; break;
5500		    case SHF_COMDEF: sindex = 23; break;
5501		    default: break;
5502		    }
5503		  break;
5504
5505		default:
5506		  break;
5507		}
5508	    }
5509
5510	  if (sindex != -1)
5511	    {
5512	      if (p != buff + field_size + 4)
5513		{
5514		  if (size < (10 + 2))
5515		    {
5516		      warn (_("Internal error: not enough buffer room for section flag info"));
5517		      return _("<unknown>");
5518		    }
5519		  size -= 2;
5520		  *p++ = ',';
5521		  *p++ = ' ';
5522		}
5523
5524	      size -= flags [sindex].len;
5525	      p = stpcpy (p, flags [sindex].str);
5526	    }
5527	  else if (flag & SHF_MASKOS)
5528	    os_flags |= flag;
5529	  else if (flag & SHF_MASKPROC)
5530	    proc_flags |= flag;
5531	  else
5532	    unknown_flags |= flag;
5533	}
5534      else
5535	{
5536	  switch (flag)
5537	    {
5538	    case SHF_WRITE:		*p = 'W'; break;
5539	    case SHF_ALLOC:		*p = 'A'; break;
5540	    case SHF_EXECINSTR:		*p = 'X'; break;
5541	    case SHF_MERGE:		*p = 'M'; break;
5542	    case SHF_STRINGS:		*p = 'S'; break;
5543	    case SHF_INFO_LINK:		*p = 'I'; break;
5544	    case SHF_LINK_ORDER:	*p = 'L'; break;
5545	    case SHF_OS_NONCONFORMING:	*p = 'O'; break;
5546	    case SHF_GROUP:		*p = 'G'; break;
5547	    case SHF_TLS:		*p = 'T'; break;
5548	    case SHF_EXCLUDE:		*p = 'E'; break;
5549	    case SHF_COMPRESSED:	*p = 'C'; break;
5550
5551	    default:
5552	      if ((elf_header.e_machine == EM_X86_64
5553		   || elf_header.e_machine == EM_L1OM
5554		   || elf_header.e_machine == EM_K1OM)
5555		  && flag == SHF_X86_64_LARGE)
5556		*p = 'l';
5557	      else if (elf_header.e_machine == EM_ARM
5558		       && flag == SHF_ARM_NOREAD)
5559		  *p = 'y';
5560	      else if (flag & SHF_MASKOS)
5561		{
5562		  *p = 'o';
5563		  sh_flags &= ~ SHF_MASKOS;
5564		}
5565	      else if (flag & SHF_MASKPROC)
5566		{
5567		  *p = 'p';
5568		  sh_flags &= ~ SHF_MASKPROC;
5569		}
5570	      else
5571		*p = 'x';
5572	      break;
5573	    }
5574	  p++;
5575	}
5576    }
5577
5578  if (do_section_details)
5579    {
5580      if (os_flags)
5581	{
5582	  size -= 5 + field_size;
5583	  if (p != buff + field_size + 4)
5584	    {
5585	      if (size < (2 + 1))
5586		{
5587		  warn (_("Internal error: not enough buffer room for section flag info"));
5588		  return _("<unknown>");
5589		}
5590	      size -= 2;
5591	      *p++ = ',';
5592	      *p++ = ' ';
5593	    }
5594	  sprintf (p, "OS (%*.*lx)", field_size, field_size,
5595		   (unsigned long) os_flags);
5596	  p += 5 + field_size;
5597	}
5598      if (proc_flags)
5599	{
5600	  size -= 7 + field_size;
5601	  if (p != buff + field_size + 4)
5602	    {
5603	      if (size < (2 + 1))
5604		{
5605		  warn (_("Internal error: not enough buffer room for section flag info"));
5606		  return _("<unknown>");
5607		}
5608	      size -= 2;
5609	      *p++ = ',';
5610	      *p++ = ' ';
5611	    }
5612	  sprintf (p, "PROC (%*.*lx)", field_size, field_size,
5613		   (unsigned long) proc_flags);
5614	  p += 7 + field_size;
5615	}
5616      if (unknown_flags)
5617	{
5618	  size -= 10 + field_size;
5619	  if (p != buff + field_size + 4)
5620	    {
5621	      if (size < (2 + 1))
5622		{
5623		  warn (_("Internal error: not enough buffer room for section flag info"));
5624		  return _("<unknown>");
5625		}
5626	      size -= 2;
5627	      *p++ = ',';
5628	      *p++ = ' ';
5629	    }
5630	  sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
5631		   (unsigned long) unknown_flags);
5632	  p += 10 + field_size;
5633	}
5634    }
5635
5636  *p = '\0';
5637  return buff;
5638}
5639
5640static unsigned int
5641get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf)
5642{
5643  if (is_32bit_elf)
5644    {
5645      Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
5646      chdr->ch_type = BYTE_GET (echdr->ch_type);
5647      chdr->ch_size = BYTE_GET (echdr->ch_size);
5648      chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
5649      return sizeof (*echdr);
5650    }
5651  else
5652    {
5653      Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
5654      chdr->ch_type = BYTE_GET (echdr->ch_type);
5655      chdr->ch_size = BYTE_GET (echdr->ch_size);
5656      chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
5657      return sizeof (*echdr);
5658    }
5659}
5660
5661static int
5662process_section_headers (FILE * file)
5663{
5664  Elf_Internal_Shdr * section;
5665  unsigned int i;
5666
5667  section_headers = NULL;
5668
5669  if (elf_header.e_shnum == 0)
5670    {
5671      /* PR binutils/12467.  */
5672      if (elf_header.e_shoff != 0)
5673	warn (_("possibly corrupt ELF file header - it has a non-zero"
5674		" section header offset, but no section headers\n"));
5675      else if (do_sections)
5676	printf (_("\nThere are no sections in this file.\n"));
5677
5678      return 1;
5679    }
5680
5681  if (do_sections && !do_header)
5682    printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
5683	    elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
5684
5685  if (is_32bit_elf)
5686    {
5687      if (! get_32bit_section_headers (file, FALSE))
5688	return 0;
5689    }
5690  else if (! get_64bit_section_headers (file, FALSE))
5691    return 0;
5692
5693  /* Read in the string table, so that we have names to display.  */
5694  if (elf_header.e_shstrndx != SHN_UNDEF
5695       && elf_header.e_shstrndx < elf_header.e_shnum)
5696    {
5697      section = section_headers + elf_header.e_shstrndx;
5698
5699      if (section->sh_size != 0)
5700	{
5701	  string_table = (char *) get_data (NULL, file, section->sh_offset,
5702                                            1, section->sh_size,
5703                                            _("string table"));
5704
5705	  string_table_length = string_table != NULL ? section->sh_size : 0;
5706	}
5707    }
5708
5709  /* Scan the sections for the dynamic symbol table
5710     and dynamic string table and debug sections.  */
5711  dynamic_symbols = NULL;
5712  dynamic_strings = NULL;
5713  dynamic_syminfo = NULL;
5714  symtab_shndx_list = NULL;
5715
5716  eh_addr_size = is_32bit_elf ? 4 : 8;
5717  switch (elf_header.e_machine)
5718    {
5719    case EM_MIPS:
5720    case EM_MIPS_RS3_LE:
5721      /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
5722	 FDE addresses.  However, the ABI also has a semi-official ILP32
5723	 variant for which the normal FDE address size rules apply.
5724
5725	 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
5726	 section, where XX is the size of longs in bits.  Unfortunately,
5727	 earlier compilers provided no way of distinguishing ILP32 objects
5728	 from LP64 objects, so if there's any doubt, we should assume that
5729	 the official LP64 form is being used.  */
5730      if ((elf_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
5731	  && find_section (".gcc_compiled_long32") == NULL)
5732	eh_addr_size = 8;
5733      break;
5734
5735    case EM_H8_300:
5736    case EM_H8_300H:
5737      switch (elf_header.e_flags & EF_H8_MACH)
5738	{
5739	case E_H8_MACH_H8300:
5740	case E_H8_MACH_H8300HN:
5741	case E_H8_MACH_H8300SN:
5742	case E_H8_MACH_H8300SXN:
5743	  eh_addr_size = 2;
5744	  break;
5745	case E_H8_MACH_H8300H:
5746	case E_H8_MACH_H8300S:
5747	case E_H8_MACH_H8300SX:
5748	  eh_addr_size = 4;
5749	  break;
5750	}
5751      break;
5752
5753    case EM_M32C_OLD:
5754    case EM_M32C:
5755      switch (elf_header.e_flags & EF_M32C_CPU_MASK)
5756	{
5757	case EF_M32C_CPU_M16C:
5758	  eh_addr_size = 2;
5759	  break;
5760	}
5761      break;
5762    }
5763
5764#define CHECK_ENTSIZE_VALUES(section, i, size32, size64)		\
5765  do									\
5766    {									\
5767      bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64;	\
5768      if (section->sh_entsize != expected_entsize)			\
5769	{								\
5770	  char buf[40];							\
5771	  sprintf_vma (buf, section->sh_entsize);			\
5772	  /* Note: coded this way so that there is a single string for  \
5773	     translation.  */ \
5774	  error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
5775	  error (_("(Using the expected size of %u for the rest of this dump)\n"), \
5776		   (unsigned) expected_entsize);			\
5777	  section->sh_entsize = expected_entsize;			\
5778	}								\
5779    }									\
5780  while (0)
5781
5782#define CHECK_ENTSIZE(section, i, type)					\
5783  CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type),	    \
5784			sizeof (Elf64_External_##type))
5785
5786  for (i = 0, section = section_headers;
5787       i < elf_header.e_shnum;
5788       i++, section++)
5789    {
5790      char * name = SECTION_NAME (section);
5791
5792      if (section->sh_type == SHT_DYNSYM)
5793	{
5794	  if (dynamic_symbols != NULL)
5795	    {
5796	      error (_("File contains multiple dynamic symbol tables\n"));
5797	      continue;
5798	    }
5799
5800	  CHECK_ENTSIZE (section, i, Sym);
5801	  dynamic_symbols = GET_ELF_SYMBOLS (file, section, & num_dynamic_syms);
5802	}
5803      else if (section->sh_type == SHT_STRTAB
5804	       && streq (name, ".dynstr"))
5805	{
5806	  if (dynamic_strings != NULL)
5807	    {
5808	      error (_("File contains multiple dynamic string tables\n"));
5809	      continue;
5810	    }
5811
5812	  dynamic_strings = (char *) get_data (NULL, file, section->sh_offset,
5813                                               1, section->sh_size,
5814                                               _("dynamic strings"));
5815	  dynamic_strings_length = dynamic_strings == NULL ? 0 : section->sh_size;
5816	}
5817      else if (section->sh_type == SHT_SYMTAB_SHNDX)
5818	{
5819	  elf_section_list * entry = xmalloc (sizeof * entry);
5820	  entry->hdr = section;
5821	  entry->next = symtab_shndx_list;
5822	  symtab_shndx_list = entry;
5823	}
5824      else if (section->sh_type == SHT_SYMTAB)
5825	CHECK_ENTSIZE (section, i, Sym);
5826      else if (section->sh_type == SHT_GROUP)
5827	CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
5828      else if (section->sh_type == SHT_REL)
5829	CHECK_ENTSIZE (section, i, Rel);
5830      else if (section->sh_type == SHT_RELA)
5831	CHECK_ENTSIZE (section, i, Rela);
5832      else if ((do_debugging || do_debug_info || do_debug_abbrevs
5833		|| do_debug_lines || do_debug_pubnames || do_debug_pubtypes
5834		|| do_debug_aranges || do_debug_frames || do_debug_macinfo
5835		|| do_debug_str || do_debug_loc || do_debug_ranges
5836		|| do_debug_addr || do_debug_cu_index)
5837	       && (const_strneq (name, ".debug_")
5838                   || const_strneq (name, ".zdebug_")))
5839	{
5840          if (name[1] == 'z')
5841            name += sizeof (".zdebug_") - 1;
5842          else
5843            name += sizeof (".debug_") - 1;
5844
5845	  if (do_debugging
5846	      || (do_debug_info     && const_strneq (name, "info"))
5847	      || (do_debug_info     && const_strneq (name, "types"))
5848	      || (do_debug_abbrevs  && const_strneq (name, "abbrev"))
5849	      || (do_debug_lines    && strcmp (name, "line") == 0)
5850	      || (do_debug_lines    && const_strneq (name, "line."))
5851	      || (do_debug_pubnames && const_strneq (name, "pubnames"))
5852	      || (do_debug_pubtypes && const_strneq (name, "pubtypes"))
5853	      || (do_debug_pubnames && const_strneq (name, "gnu_pubnames"))
5854	      || (do_debug_pubtypes && const_strneq (name, "gnu_pubtypes"))
5855	      || (do_debug_aranges  && const_strneq (name, "aranges"))
5856	      || (do_debug_ranges   && const_strneq (name, "ranges"))
5857	      || (do_debug_frames   && const_strneq (name, "frame"))
5858	      || (do_debug_macinfo  && const_strneq (name, "macinfo"))
5859	      || (do_debug_macinfo  && const_strneq (name, "macro"))
5860	      || (do_debug_str      && const_strneq (name, "str"))
5861	      || (do_debug_loc      && const_strneq (name, "loc"))
5862	      || (do_debug_addr     && const_strneq (name, "addr"))
5863	      || (do_debug_cu_index && const_strneq (name, "cu_index"))
5864	      || (do_debug_cu_index && const_strneq (name, "tu_index"))
5865	      )
5866	    request_dump_bynumber (i, DEBUG_DUMP);
5867	}
5868      /* Linkonce section to be combined with .debug_info at link time.  */
5869      else if ((do_debugging || do_debug_info)
5870	       && const_strneq (name, ".gnu.linkonce.wi."))
5871	request_dump_bynumber (i, DEBUG_DUMP);
5872      else if (do_debug_frames && streq (name, ".eh_frame"))
5873	request_dump_bynumber (i, DEBUG_DUMP);
5874      else if (do_gdb_index && streq (name, ".gdb_index"))
5875	request_dump_bynumber (i, DEBUG_DUMP);
5876      /* Trace sections for Itanium VMS.  */
5877      else if ((do_debugging || do_trace_info || do_trace_abbrevs
5878                || do_trace_aranges)
5879	       && const_strneq (name, ".trace_"))
5880	{
5881          name += sizeof (".trace_") - 1;
5882
5883	  if (do_debugging
5884	      || (do_trace_info     && streq (name, "info"))
5885	      || (do_trace_abbrevs  && streq (name, "abbrev"))
5886	      || (do_trace_aranges  && streq (name, "aranges"))
5887	      )
5888	    request_dump_bynumber (i, DEBUG_DUMP);
5889	}
5890    }
5891
5892  if (! do_sections)
5893    return 1;
5894
5895  if (elf_header.e_shnum > 1)
5896    printf (_("\nSection Headers:\n"));
5897  else
5898    printf (_("\nSection Header:\n"));
5899
5900  if (is_32bit_elf)
5901    {
5902      if (do_section_details)
5903	{
5904	  printf (_("  [Nr] Name\n"));
5905	  printf (_("       Type            Addr     Off    Size   ES   Lk Inf Al\n"));
5906	}
5907      else
5908	printf
5909	  (_("  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al\n"));
5910    }
5911  else if (do_wide)
5912    {
5913      if (do_section_details)
5914	{
5915	  printf (_("  [Nr] Name\n"));
5916	  printf (_("       Type            Address          Off    Size   ES   Lk Inf Al\n"));
5917	}
5918      else
5919	printf
5920	  (_("  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al\n"));
5921    }
5922  else
5923    {
5924      if (do_section_details)
5925	{
5926	  printf (_("  [Nr] Name\n"));
5927	  printf (_("       Type              Address          Offset            Link\n"));
5928	  printf (_("       Size              EntSize          Info              Align\n"));
5929	}
5930      else
5931	{
5932	  printf (_("  [Nr] Name              Type             Address           Offset\n"));
5933	  printf (_("       Size              EntSize          Flags  Link  Info  Align\n"));
5934	}
5935    }
5936
5937  if (do_section_details)
5938    printf (_("       Flags\n"));
5939
5940  for (i = 0, section = section_headers;
5941       i < elf_header.e_shnum;
5942       i++, section++)
5943    {
5944      printf ("  [%2u] ", i);
5945      if (do_section_details)
5946	printf ("%s\n      ", printable_section_name (section));
5947      else
5948	print_symbol (-17, SECTION_NAME (section));
5949
5950      printf (do_wide ? " %-15s " : " %-15.15s ",
5951	      get_section_type_name (section->sh_type));
5952
5953      if (is_32bit_elf)
5954	{
5955	  const char * link_too_big = NULL;
5956
5957	  print_vma (section->sh_addr, LONG_HEX);
5958
5959	  printf ( " %6.6lx %6.6lx %2.2lx",
5960		   (unsigned long) section->sh_offset,
5961		   (unsigned long) section->sh_size,
5962		   (unsigned long) section->sh_entsize);
5963
5964	  if (do_section_details)
5965	    fputs ("  ", stdout);
5966	  else
5967	    printf (" %3s ", get_elf_section_flags (section->sh_flags));
5968
5969	  if (section->sh_link >= elf_header.e_shnum)
5970	    {
5971	      link_too_big = "";
5972	      /* The sh_link value is out of range.  Normally this indicates
5973		 an error but it can have special values in Solaris binaries.  */
5974	      switch (elf_header.e_machine)
5975		{
5976		case EM_386:
5977		case EM_IAMCU:
5978		case EM_X86_64:
5979		case EM_L1OM:
5980		case EM_K1OM:
5981		case EM_OLD_SPARCV9:
5982		case EM_SPARC32PLUS:
5983		case EM_SPARCV9:
5984		case EM_SPARC:
5985		  if (section->sh_link == (SHN_BEFORE & 0xffff))
5986		    link_too_big = "BEFORE";
5987		  else if (section->sh_link == (SHN_AFTER & 0xffff))
5988		    link_too_big = "AFTER";
5989		  break;
5990		default:
5991		  break;
5992		}
5993	    }
5994
5995	  if (do_section_details)
5996	    {
5997	      if (link_too_big != NULL && * link_too_big)
5998		printf ("<%s> ", link_too_big);
5999	      else
6000		printf ("%2u ", section->sh_link);
6001	      printf ("%3u %2lu\n", section->sh_info,
6002		      (unsigned long) section->sh_addralign);
6003	    }
6004	  else
6005	    printf ("%2u %3u %2lu\n",
6006		    section->sh_link,
6007		    section->sh_info,
6008		    (unsigned long) section->sh_addralign);
6009
6010	  if (link_too_big && ! * link_too_big)
6011	    warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
6012		  i, section->sh_link);
6013	}
6014      else if (do_wide)
6015	{
6016	  print_vma (section->sh_addr, LONG_HEX);
6017
6018	  if ((long) section->sh_offset == section->sh_offset)
6019	    printf (" %6.6lx", (unsigned long) section->sh_offset);
6020	  else
6021	    {
6022	      putchar (' ');
6023	      print_vma (section->sh_offset, LONG_HEX);
6024	    }
6025
6026	  if ((unsigned long) section->sh_size == section->sh_size)
6027	    printf (" %6.6lx", (unsigned long) section->sh_size);
6028	  else
6029	    {
6030	      putchar (' ');
6031	      print_vma (section->sh_size, LONG_HEX);
6032	    }
6033
6034	  if ((unsigned long) section->sh_entsize == section->sh_entsize)
6035	    printf (" %2.2lx", (unsigned long) section->sh_entsize);
6036	  else
6037	    {
6038	      putchar (' ');
6039	      print_vma (section->sh_entsize, LONG_HEX);
6040	    }
6041
6042	  if (do_section_details)
6043	    fputs ("  ", stdout);
6044	  else
6045	    printf (" %3s ", get_elf_section_flags (section->sh_flags));
6046
6047	  printf ("%2u %3u ", section->sh_link, section->sh_info);
6048
6049	  if ((unsigned long) section->sh_addralign == section->sh_addralign)
6050	    printf ("%2lu\n", (unsigned long) section->sh_addralign);
6051	  else
6052	    {
6053	      print_vma (section->sh_addralign, DEC);
6054	      putchar ('\n');
6055	    }
6056	}
6057      else if (do_section_details)
6058	{
6059	  printf ("       %-15.15s  ",
6060		  get_section_type_name (section->sh_type));
6061	  print_vma (section->sh_addr, LONG_HEX);
6062	  if ((long) section->sh_offset == section->sh_offset)
6063	    printf ("  %16.16lx", (unsigned long) section->sh_offset);
6064	  else
6065	    {
6066	      printf ("  ");
6067	      print_vma (section->sh_offset, LONG_HEX);
6068	    }
6069	  printf ("  %u\n       ", section->sh_link);
6070	  print_vma (section->sh_size, LONG_HEX);
6071	  putchar (' ');
6072	  print_vma (section->sh_entsize, LONG_HEX);
6073
6074	  printf ("  %-16u  %lu\n",
6075		  section->sh_info,
6076		  (unsigned long) section->sh_addralign);
6077	}
6078      else
6079	{
6080	  putchar (' ');
6081	  print_vma (section->sh_addr, LONG_HEX);
6082	  if ((long) section->sh_offset == section->sh_offset)
6083	    printf ("  %8.8lx", (unsigned long) section->sh_offset);
6084	  else
6085	    {
6086	      printf ("  ");
6087	      print_vma (section->sh_offset, LONG_HEX);
6088	    }
6089	  printf ("\n       ");
6090	  print_vma (section->sh_size, LONG_HEX);
6091	  printf ("  ");
6092	  print_vma (section->sh_entsize, LONG_HEX);
6093
6094	  printf (" %3s ", get_elf_section_flags (section->sh_flags));
6095
6096	  printf ("     %2u   %3u     %lu\n",
6097		  section->sh_link,
6098		  section->sh_info,
6099		  (unsigned long) section->sh_addralign);
6100	}
6101
6102      if (do_section_details)
6103	{
6104	  printf ("       %s\n", get_elf_section_flags (section->sh_flags));
6105	  if ((section->sh_flags & SHF_COMPRESSED) != 0)
6106	    {
6107	      /* Minimum section size is 12 bytes for 32-bit compression
6108		 header + 12 bytes for compressed data header.  */
6109	      unsigned char buf[24];
6110	      assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
6111	      if (get_data (&buf, (FILE *) file, section->sh_offset, 1,
6112			    sizeof (buf), _("compression header")))
6113		{
6114		  Elf_Internal_Chdr chdr;
6115		  get_compression_header (&chdr, buf);
6116		  if (chdr.ch_type == ELFCOMPRESS_ZLIB)
6117		    printf ("       ZLIB, ");
6118		  else
6119		    printf (_("       [<unknown>: 0x%x], "),
6120			    chdr.ch_type);
6121		  print_vma (chdr.ch_size, LONG_HEX);
6122		  printf (", %lu\n", (unsigned long) chdr.ch_addralign);
6123		}
6124	    }
6125	}
6126    }
6127
6128  if (!do_section_details)
6129    {
6130      /* The ordering of the letters shown here matches the ordering of the
6131	 corresponding SHF_xxx values, and hence the order in which these
6132	 letters will be displayed to the user.  */
6133      printf (_("Key to Flags:\n\
6134  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
6135  L (link order), O (extra OS processing required), G (group), T (TLS),\n\
6136  C (compressed), x (unknown), o (OS specific), E (exclude),\n  "));
6137      if (elf_header.e_machine == EM_X86_64
6138	  || elf_header.e_machine == EM_L1OM
6139	  || elf_header.e_machine == EM_K1OM)
6140	printf (_("l (large), "));
6141      else if (elf_header.e_machine == EM_ARM)
6142	printf (_("y (noread), "));
6143      printf ("p (processor specific)\n");
6144    }
6145
6146  return 1;
6147}
6148
6149static const char *
6150get_group_flags (unsigned int flags)
6151{
6152  static char buff[32];
6153  switch (flags)
6154    {
6155    case 0:
6156      return "";
6157
6158    case GRP_COMDAT:
6159      return "COMDAT ";
6160
6161   default:
6162      snprintf (buff, sizeof (buff), _("[<unknown>: 0x%x] "), flags);
6163      break;
6164    }
6165  return buff;
6166}
6167
6168static int
6169process_section_groups (FILE * file)
6170{
6171  Elf_Internal_Shdr * section;
6172  unsigned int i;
6173  struct group * group;
6174  Elf_Internal_Shdr * symtab_sec;
6175  Elf_Internal_Shdr * strtab_sec;
6176  Elf_Internal_Sym * symtab;
6177  unsigned long num_syms;
6178  char * strtab;
6179  size_t strtab_size;
6180
6181  /* Don't process section groups unless needed.  */
6182  if (!do_unwind && !do_section_groups)
6183    return 1;
6184
6185  if (elf_header.e_shnum == 0)
6186    {
6187      if (do_section_groups)
6188	printf (_("\nThere are no sections to group in this file.\n"));
6189
6190      return 1;
6191    }
6192
6193  if (section_headers == NULL)
6194    {
6195      error (_("Section headers are not available!\n"));
6196      /* PR 13622: This can happen with a corrupt ELF header.  */
6197      return 0;
6198    }
6199
6200  section_headers_groups = (struct group **) calloc (elf_header.e_shnum,
6201                                                     sizeof (struct group *));
6202
6203  if (section_headers_groups == NULL)
6204    {
6205      error (_("Out of memory reading %u section group headers\n"),
6206	     elf_header.e_shnum);
6207      return 0;
6208    }
6209
6210  /* Scan the sections for the group section.  */
6211  group_count = 0;
6212  for (i = 0, section = section_headers;
6213       i < elf_header.e_shnum;
6214       i++, section++)
6215    if (section->sh_type == SHT_GROUP)
6216      group_count++;
6217
6218  if (group_count == 0)
6219    {
6220      if (do_section_groups)
6221	printf (_("\nThere are no section groups in this file.\n"));
6222
6223      return 1;
6224    }
6225
6226  section_groups = (struct group *) calloc (group_count, sizeof (struct group));
6227
6228  if (section_groups == NULL)
6229    {
6230      error (_("Out of memory reading %lu groups\n"),
6231	     (unsigned long) group_count);
6232      return 0;
6233    }
6234
6235  symtab_sec = NULL;
6236  strtab_sec = NULL;
6237  symtab = NULL;
6238  num_syms = 0;
6239  strtab = NULL;
6240  strtab_size = 0;
6241  for (i = 0, section = section_headers, group = section_groups;
6242       i < elf_header.e_shnum;
6243       i++, section++)
6244    {
6245      if (section->sh_type == SHT_GROUP)
6246	{
6247	  const char * name = printable_section_name (section);
6248	  const char * group_name;
6249	  unsigned char * start;
6250	  unsigned char * indices;
6251	  unsigned int entry, j, size;
6252	  Elf_Internal_Shdr * sec;
6253	  Elf_Internal_Sym * sym;
6254
6255	  /* Get the symbol table.  */
6256	  if (section->sh_link >= elf_header.e_shnum
6257	      || ((sec = section_headers + section->sh_link)->sh_type
6258		  != SHT_SYMTAB))
6259	    {
6260	      error (_("Bad sh_link in group section `%s'\n"), name);
6261	      continue;
6262	    }
6263
6264	  if (symtab_sec != sec)
6265	    {
6266	      symtab_sec = sec;
6267	      if (symtab)
6268		free (symtab);
6269	      symtab = GET_ELF_SYMBOLS (file, symtab_sec, & num_syms);
6270	    }
6271
6272	  if (symtab == NULL)
6273	    {
6274	      error (_("Corrupt header in group section `%s'\n"), name);
6275	      continue;
6276	    }
6277
6278	  if (section->sh_info >= num_syms)
6279	    {
6280	      error (_("Bad sh_info in group section `%s'\n"), name);
6281	      continue;
6282	    }
6283
6284	  sym = symtab + section->sh_info;
6285
6286	  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
6287	    {
6288	      if (sym->st_shndx == 0
6289		  || sym->st_shndx >= elf_header.e_shnum)
6290		{
6291		  error (_("Bad sh_info in group section `%s'\n"), name);
6292		  continue;
6293		}
6294
6295	      group_name = SECTION_NAME (section_headers + sym->st_shndx);
6296	      strtab_sec = NULL;
6297	      if (strtab)
6298		free (strtab);
6299	      strtab = NULL;
6300	      strtab_size = 0;
6301	    }
6302	  else
6303	    {
6304	      /* Get the string table.  */
6305	      if (symtab_sec->sh_link >= elf_header.e_shnum)
6306		{
6307		  strtab_sec = NULL;
6308		  if (strtab)
6309		    free (strtab);
6310		  strtab = NULL;
6311		  strtab_size = 0;
6312		}
6313	      else if (strtab_sec
6314		       != (sec = section_headers + symtab_sec->sh_link))
6315		{
6316		  strtab_sec = sec;
6317		  if (strtab)
6318		    free (strtab);
6319
6320		  strtab = (char *) get_data (NULL, file, strtab_sec->sh_offset,
6321					      1, strtab_sec->sh_size,
6322					      _("string table"));
6323		  strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
6324		}
6325	      group_name = sym->st_name < strtab_size
6326		? strtab + sym->st_name : _("<corrupt>");
6327	    }
6328
6329	  /* PR 17531: file: loop.  */
6330	  if (section->sh_entsize > section->sh_size)
6331	    {
6332	      error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
6333		     printable_section_name (section),
6334		     (unsigned long) section->sh_entsize,
6335		     (unsigned long) section->sh_size);
6336	      break;
6337	    }
6338
6339	  start = (unsigned char *) get_data (NULL, file, section->sh_offset,
6340                                              1, section->sh_size,
6341                                              _("section data"));
6342	  if (start == NULL)
6343	    continue;
6344
6345	  indices = start;
6346	  size = (section->sh_size / section->sh_entsize) - 1;
6347	  entry = byte_get (indices, 4);
6348	  indices += 4;
6349
6350	  if (do_section_groups)
6351	    {
6352	      printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
6353		      get_group_flags (entry), i, name, group_name, size);
6354
6355	      printf (_("   [Index]    Name\n"));
6356	    }
6357
6358	  group->group_index = i;
6359
6360	  for (j = 0; j < size; j++)
6361	    {
6362	      struct group_list * g;
6363
6364	      entry = byte_get (indices, 4);
6365	      indices += 4;
6366
6367	      if (entry >= elf_header.e_shnum)
6368		{
6369		  static unsigned num_group_errors = 0;
6370
6371		  if (num_group_errors ++ < 10)
6372		    {
6373		      error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
6374			     entry, i, elf_header.e_shnum - 1);
6375		      if (num_group_errors == 10)
6376			warn (_("Futher error messages about overlarge group section indicies suppressed\n"));
6377		    }
6378		  continue;
6379		}
6380
6381	      if (section_headers_groups [entry] != NULL)
6382		{
6383		  if (entry)
6384		    {
6385		      static unsigned num_errs = 0;
6386
6387		      if (num_errs ++ < 10)
6388			{
6389			  error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
6390				 entry, i,
6391				 section_headers_groups [entry]->group_index);
6392			  if (num_errs == 10)
6393			    warn (_("Further error messages about already contained group sections suppressed\n"));
6394			}
6395		      continue;
6396		    }
6397		  else
6398		    {
6399		      /* Intel C/C++ compiler may put section 0 in a
6400			 section group. We just warn it the first time
6401			 and ignore it afterwards.  */
6402		      static int warned = 0;
6403		      if (!warned)
6404			{
6405			  error (_("section 0 in group section [%5u]\n"),
6406				 section_headers_groups [entry]->group_index);
6407			  warned++;
6408			}
6409		    }
6410		}
6411
6412	      section_headers_groups [entry] = group;
6413
6414	      if (do_section_groups)
6415		{
6416		  sec = section_headers + entry;
6417		  printf ("   [%5u]   %s\n", entry, printable_section_name (sec));
6418		}
6419
6420	      g = (struct group_list *) xmalloc (sizeof (struct group_list));
6421	      g->section_index = entry;
6422	      g->next = group->root;
6423	      group->root = g;
6424	    }
6425
6426	  if (start)
6427	    free (start);
6428
6429	  group++;
6430	}
6431    }
6432
6433  if (symtab)
6434    free (symtab);
6435  if (strtab)
6436    free (strtab);
6437  return 1;
6438}
6439
6440/* Data used to display dynamic fixups.  */
6441
6442struct ia64_vms_dynfixup
6443{
6444  bfd_vma needed_ident;		/* Library ident number.  */
6445  bfd_vma needed;		/* Index in the dstrtab of the library name.  */
6446  bfd_vma fixup_needed;		/* Index of the library.  */
6447  bfd_vma fixup_rela_cnt;	/* Number of fixups.  */
6448  bfd_vma fixup_rela_off;	/* Fixups offset in the dynamic segment.  */
6449};
6450
6451/* Data used to display dynamic relocations.  */
6452
6453struct ia64_vms_dynimgrela
6454{
6455  bfd_vma img_rela_cnt;		/* Number of relocations.  */
6456  bfd_vma img_rela_off;		/* Reloc offset in the dynamic segment.  */
6457};
6458
6459/* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
6460   library).  */
6461
6462static void
6463dump_ia64_vms_dynamic_fixups (FILE *file, struct ia64_vms_dynfixup *fixup,
6464                              const char *strtab, unsigned int strtab_sz)
6465{
6466  Elf64_External_VMS_IMAGE_FIXUP *imfs;
6467  long i;
6468  const char *lib_name;
6469
6470  imfs = get_data (NULL, file, dynamic_addr + fixup->fixup_rela_off,
6471		   1, fixup->fixup_rela_cnt * sizeof (*imfs),
6472		   _("dynamic section image fixups"));
6473  if (!imfs)
6474    return;
6475
6476  if (fixup->needed < strtab_sz)
6477    lib_name = strtab + fixup->needed;
6478  else
6479    {
6480      warn ("corrupt library name index of 0x%lx found in dynamic entry",
6481            (unsigned long) fixup->needed);
6482      lib_name = "???";
6483    }
6484  printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
6485	  (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
6486  printf
6487    (_("Seg Offset           Type                             SymVec DataType\n"));
6488
6489  for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
6490    {
6491      unsigned int type;
6492      const char *rtype;
6493
6494      printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
6495      printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
6496      type = BYTE_GET (imfs [i].type);
6497      rtype = elf_ia64_reloc_type (type);
6498      if (rtype == NULL)
6499        printf (" 0x%08x                       ", type);
6500      else
6501        printf (" %-32s ", rtype);
6502      printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
6503      printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
6504    }
6505
6506  free (imfs);
6507}
6508
6509/* Display IA-64 OpenVMS dynamic relocations (used to relocate an image).  */
6510
6511static void
6512dump_ia64_vms_dynamic_relocs (FILE *file, struct ia64_vms_dynimgrela *imgrela)
6513{
6514  Elf64_External_VMS_IMAGE_RELA *imrs;
6515  long i;
6516
6517  imrs = get_data (NULL, file, dynamic_addr + imgrela->img_rela_off,
6518		   1, imgrela->img_rela_cnt * sizeof (*imrs),
6519		   _("dynamic section image relocations"));
6520  if (!imrs)
6521    return;
6522
6523  printf (_("\nImage relocs\n"));
6524  printf
6525    (_("Seg Offset   Type                            Addend            Seg Sym Off\n"));
6526
6527  for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
6528    {
6529      unsigned int type;
6530      const char *rtype;
6531
6532      printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
6533      printf ("%08" BFD_VMA_FMT "x ",
6534              (bfd_vma) BYTE_GET (imrs [i].rela_offset));
6535      type = BYTE_GET (imrs [i].type);
6536      rtype = elf_ia64_reloc_type (type);
6537      if (rtype == NULL)
6538        printf ("0x%08x                      ", type);
6539      else
6540        printf ("%-31s ", rtype);
6541      print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
6542      printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
6543      printf ("%08" BFD_VMA_FMT "x\n",
6544              (bfd_vma) BYTE_GET (imrs [i].sym_offset));
6545    }
6546
6547  free (imrs);
6548}
6549
6550/* Display IA-64 OpenVMS dynamic relocations and fixups.  */
6551
6552static int
6553process_ia64_vms_dynamic_relocs (FILE *file)
6554{
6555  struct ia64_vms_dynfixup fixup;
6556  struct ia64_vms_dynimgrela imgrela;
6557  Elf_Internal_Dyn *entry;
6558  int res = 0;
6559  bfd_vma strtab_off = 0;
6560  bfd_vma strtab_sz = 0;
6561  char *strtab = NULL;
6562
6563  memset (&fixup, 0, sizeof (fixup));
6564  memset (&imgrela, 0, sizeof (imgrela));
6565
6566  /* Note: the order of the entries is specified by the OpenVMS specs.  */
6567  for (entry = dynamic_section;
6568       entry < dynamic_section + dynamic_nent;
6569       entry++)
6570    {
6571      switch (entry->d_tag)
6572        {
6573        case DT_IA_64_VMS_STRTAB_OFFSET:
6574          strtab_off = entry->d_un.d_val;
6575          break;
6576        case DT_STRSZ:
6577          strtab_sz = entry->d_un.d_val;
6578          if (strtab == NULL)
6579            strtab = get_data (NULL, file, dynamic_addr + strtab_off,
6580                               1, strtab_sz, _("dynamic string section"));
6581          break;
6582
6583        case DT_IA_64_VMS_NEEDED_IDENT:
6584          fixup.needed_ident = entry->d_un.d_val;
6585          break;
6586        case DT_NEEDED:
6587          fixup.needed = entry->d_un.d_val;
6588          break;
6589        case DT_IA_64_VMS_FIXUP_NEEDED:
6590          fixup.fixup_needed = entry->d_un.d_val;
6591          break;
6592        case DT_IA_64_VMS_FIXUP_RELA_CNT:
6593          fixup.fixup_rela_cnt = entry->d_un.d_val;
6594          break;
6595        case DT_IA_64_VMS_FIXUP_RELA_OFF:
6596          fixup.fixup_rela_off = entry->d_un.d_val;
6597          res++;
6598          dump_ia64_vms_dynamic_fixups (file, &fixup, strtab, strtab_sz);
6599          break;
6600
6601        case DT_IA_64_VMS_IMG_RELA_CNT:
6602	  imgrela.img_rela_cnt = entry->d_un.d_val;
6603          break;
6604        case DT_IA_64_VMS_IMG_RELA_OFF:
6605	  imgrela.img_rela_off = entry->d_un.d_val;
6606          res++;
6607          dump_ia64_vms_dynamic_relocs (file, &imgrela);
6608          break;
6609
6610        default:
6611          break;
6612	}
6613    }
6614
6615  if (strtab != NULL)
6616    free (strtab);
6617
6618  return res;
6619}
6620
6621static struct
6622{
6623  const char * name;
6624  int reloc;
6625  int size;
6626  int rela;
6627} dynamic_relocations [] =
6628{
6629    { "REL", DT_REL, DT_RELSZ, FALSE },
6630    { "RELA", DT_RELA, DT_RELASZ, TRUE },
6631    { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
6632};
6633
6634/* Process the reloc section.  */
6635
6636static int
6637process_relocs (FILE * file)
6638{
6639  unsigned long rel_size;
6640  unsigned long rel_offset;
6641
6642
6643  if (!do_reloc)
6644    return 1;
6645
6646  if (do_using_dynamic)
6647    {
6648      int is_rela;
6649      const char * name;
6650      int has_dynamic_reloc;
6651      unsigned int i;
6652
6653      has_dynamic_reloc = 0;
6654
6655      for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
6656	{
6657	  is_rela = dynamic_relocations [i].rela;
6658	  name = dynamic_relocations [i].name;
6659	  rel_size = dynamic_info [dynamic_relocations [i].size];
6660	  rel_offset = dynamic_info [dynamic_relocations [i].reloc];
6661
6662	  has_dynamic_reloc |= rel_size;
6663
6664	  if (is_rela == UNKNOWN)
6665	    {
6666	      if (dynamic_relocations [i].reloc == DT_JMPREL)
6667		switch (dynamic_info[DT_PLTREL])
6668		  {
6669		  case DT_REL:
6670		    is_rela = FALSE;
6671		    break;
6672		  case DT_RELA:
6673		    is_rela = TRUE;
6674		    break;
6675		  }
6676	    }
6677
6678	  if (rel_size)
6679	    {
6680	      printf
6681		(_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
6682		 name, rel_offset, rel_size);
6683
6684	      dump_relocations (file,
6685				offset_from_vma (file, rel_offset, rel_size),
6686				rel_size,
6687				dynamic_symbols, num_dynamic_syms,
6688				dynamic_strings, dynamic_strings_length,
6689				is_rela, 1);
6690	    }
6691	}
6692
6693      if (is_ia64_vms ())
6694        has_dynamic_reloc |= process_ia64_vms_dynamic_relocs (file);
6695
6696      if (! has_dynamic_reloc)
6697	printf (_("\nThere are no dynamic relocations in this file.\n"));
6698    }
6699  else
6700    {
6701      Elf_Internal_Shdr * section;
6702      unsigned long i;
6703      int found = 0;
6704
6705      for (i = 0, section = section_headers;
6706	   i < elf_header.e_shnum;
6707	   i++, section++)
6708	{
6709	  if (   section->sh_type != SHT_RELA
6710	      && section->sh_type != SHT_REL)
6711	    continue;
6712
6713	  rel_offset = section->sh_offset;
6714	  rel_size   = section->sh_size;
6715
6716	  if (rel_size)
6717	    {
6718	      Elf_Internal_Shdr * strsec;
6719	      int is_rela;
6720
6721	      printf (_("\nRelocation section "));
6722
6723	      if (string_table == NULL)
6724		printf ("%d", section->sh_name);
6725	      else
6726		printf ("'%s'", printable_section_name (section));
6727
6728	      printf (_(" at offset 0x%lx contains %lu entries:\n"),
6729		 rel_offset, (unsigned long) (rel_size / section->sh_entsize));
6730
6731	      is_rela = section->sh_type == SHT_RELA;
6732
6733	      if (section->sh_link != 0
6734		  && section->sh_link < elf_header.e_shnum)
6735		{
6736		  Elf_Internal_Shdr * symsec;
6737		  Elf_Internal_Sym *  symtab;
6738		  unsigned long nsyms;
6739		  unsigned long strtablen = 0;
6740		  char * strtab = NULL;
6741
6742		  symsec = section_headers + section->sh_link;
6743		  if (symsec->sh_type != SHT_SYMTAB
6744		      && symsec->sh_type != SHT_DYNSYM)
6745                    continue;
6746
6747		  symtab = GET_ELF_SYMBOLS (file, symsec, & nsyms);
6748
6749		  if (symtab == NULL)
6750		    continue;
6751
6752		  if (symsec->sh_link != 0
6753		      && symsec->sh_link < elf_header.e_shnum)
6754		    {
6755		      strsec = section_headers + symsec->sh_link;
6756
6757		      strtab = (char *) get_data (NULL, file, strsec->sh_offset,
6758						  1, strsec->sh_size,
6759						  _("string table"));
6760		      strtablen = strtab == NULL ? 0 : strsec->sh_size;
6761		    }
6762
6763		  dump_relocations (file, rel_offset, rel_size,
6764				    symtab, nsyms, strtab, strtablen,
6765				    is_rela,
6766				    symsec->sh_type == SHT_DYNSYM);
6767		  if (strtab)
6768		    free (strtab);
6769		  free (symtab);
6770		}
6771	      else
6772		dump_relocations (file, rel_offset, rel_size,
6773				  NULL, 0, NULL, 0, is_rela, 0);
6774
6775	      found = 1;
6776	    }
6777	}
6778
6779      if (! found)
6780	printf (_("\nThere are no relocations in this file.\n"));
6781    }
6782
6783  return 1;
6784}
6785
6786/* An absolute address consists of a section and an offset.  If the
6787   section is NULL, the offset itself is the address, otherwise, the
6788   address equals to LOAD_ADDRESS(section) + offset.  */
6789
6790struct absaddr
6791{
6792  unsigned short section;
6793  bfd_vma offset;
6794};
6795
6796#define ABSADDR(a) \
6797  ((a).section \
6798   ? section_headers [(a).section].sh_addr + (a).offset \
6799   : (a).offset)
6800
6801/* Find the nearest symbol at or below ADDR.  Returns the symbol
6802   name, if found, and the offset from the symbol to ADDR.  */
6803
6804static void
6805find_symbol_for_address (Elf_Internal_Sym * symtab,
6806			 unsigned long      nsyms,
6807			 const char *       strtab,
6808			 unsigned long      strtab_size,
6809			 struct absaddr     addr,
6810			 const char **      symname,
6811			 bfd_vma *          offset)
6812{
6813  bfd_vma dist = 0x100000;
6814  Elf_Internal_Sym * sym;
6815  Elf_Internal_Sym * beg;
6816  Elf_Internal_Sym * end;
6817  Elf_Internal_Sym * best = NULL;
6818
6819  REMOVE_ARCH_BITS (addr.offset);
6820  beg = symtab;
6821  end = symtab + nsyms;
6822
6823  while (beg < end)
6824    {
6825      bfd_vma value;
6826
6827      sym = beg + (end - beg) / 2;
6828
6829      value = sym->st_value;
6830      REMOVE_ARCH_BITS (value);
6831
6832      if (sym->st_name != 0
6833	  && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
6834	  && addr.offset >= value
6835	  && addr.offset - value < dist)
6836	{
6837	  best = sym;
6838	  dist = addr.offset - value;
6839	  if (!dist)
6840	    break;
6841	}
6842
6843      if (addr.offset < value)
6844	end = sym;
6845      else
6846	beg = sym + 1;
6847    }
6848
6849  if (best)
6850    {
6851      *symname = (best->st_name >= strtab_size
6852		  ? _("<corrupt>") : strtab + best->st_name);
6853      *offset = dist;
6854      return;
6855    }
6856
6857  *symname = NULL;
6858  *offset = addr.offset;
6859}
6860
6861static int
6862symcmp (const void *p, const void *q)
6863{
6864  Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
6865  Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
6866
6867  return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
6868}
6869
6870/* Process the unwind section.  */
6871
6872#include "unwind-ia64.h"
6873
6874struct ia64_unw_table_entry
6875{
6876  struct absaddr start;
6877  struct absaddr end;
6878  struct absaddr info;
6879};
6880
6881struct ia64_unw_aux_info
6882{
6883  struct ia64_unw_table_entry *table;	/* Unwind table.  */
6884  unsigned long table_len;		/* Length of unwind table.  */
6885  unsigned char * info;			/* Unwind info.  */
6886  unsigned long info_size;		/* Size of unwind info.  */
6887  bfd_vma info_addr;			/* Starting address of unwind info.  */
6888  bfd_vma seg_base;			/* Starting address of segment.  */
6889  Elf_Internal_Sym * symtab;		/* The symbol table.  */
6890  unsigned long nsyms;			/* Number of symbols.  */
6891  Elf_Internal_Sym * funtab;		/* Sorted table of STT_FUNC symbols.  */
6892  unsigned long nfuns;			/* Number of entries in funtab.  */
6893  char * strtab;			/* The string table.  */
6894  unsigned long strtab_size;		/* Size of string table.  */
6895};
6896
6897static void
6898dump_ia64_unwind (struct ia64_unw_aux_info * aux)
6899{
6900  struct ia64_unw_table_entry * tp;
6901  unsigned long j, nfuns;
6902  int in_body;
6903
6904  aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
6905  for (nfuns = 0, j = 0; j < aux->nsyms; j++)
6906    if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
6907      aux->funtab[nfuns++] = aux->symtab[j];
6908  aux->nfuns = nfuns;
6909  qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
6910
6911  for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
6912    {
6913      bfd_vma stamp;
6914      bfd_vma offset;
6915      const unsigned char * dp;
6916      const unsigned char * head;
6917      const unsigned char * end;
6918      const char * procname;
6919
6920      find_symbol_for_address (aux->funtab, aux->nfuns, aux->strtab,
6921			       aux->strtab_size, tp->start, &procname, &offset);
6922
6923      fputs ("\n<", stdout);
6924
6925      if (procname)
6926	{
6927	  fputs (procname, stdout);
6928
6929	  if (offset)
6930	    printf ("+%lx", (unsigned long) offset);
6931	}
6932
6933      fputs (">: [", stdout);
6934      print_vma (tp->start.offset, PREFIX_HEX);
6935      fputc ('-', stdout);
6936      print_vma (tp->end.offset, PREFIX_HEX);
6937      printf ("], info at +0x%lx\n",
6938	      (unsigned long) (tp->info.offset - aux->seg_base));
6939
6940      /* PR 17531: file: 86232b32.  */
6941      if (aux->info == NULL)
6942	continue;
6943
6944      /* PR 17531: file: 0997b4d1.  */
6945      if ((ABSADDR (tp->info) - aux->info_addr) >= aux->info_size)
6946	{
6947	  warn (_("Invalid offset %lx in table entry %ld\n"),
6948		(long) tp->info.offset, (long) (tp - aux->table));
6949	  continue;
6950	}
6951
6952      head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
6953      stamp = byte_get ((unsigned char *) head, sizeof (stamp));
6954
6955      printf ("  v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
6956	      (unsigned) UNW_VER (stamp),
6957	      (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
6958	      UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
6959	      UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
6960	      (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
6961
6962      if (UNW_VER (stamp) != 1)
6963	{
6964	  printf (_("\tUnknown version.\n"));
6965	  continue;
6966	}
6967
6968      in_body = 0;
6969      end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
6970      /* PR 17531: file: 16ceda89.  */
6971      if (end > aux->info + aux->info_size)
6972	end = aux->info + aux->info_size;
6973      for (dp = head + 8; dp < end;)
6974	dp = unw_decode (dp, in_body, & in_body, end);
6975    }
6976
6977  free (aux->funtab);
6978}
6979
6980static bfd_boolean
6981slurp_ia64_unwind_table (FILE * file,
6982			 struct ia64_unw_aux_info * aux,
6983			 Elf_Internal_Shdr * sec)
6984{
6985  unsigned long size, nrelas, i;
6986  Elf_Internal_Phdr * seg;
6987  struct ia64_unw_table_entry * tep;
6988  Elf_Internal_Shdr * relsec;
6989  Elf_Internal_Rela * rela;
6990  Elf_Internal_Rela * rp;
6991  unsigned char * table;
6992  unsigned char * tp;
6993  Elf_Internal_Sym * sym;
6994  const char * relname;
6995
6996  aux->table_len = 0;
6997
6998  /* First, find the starting address of the segment that includes
6999     this section: */
7000
7001  if (elf_header.e_phnum)
7002    {
7003      if (! get_program_headers (file))
7004	  return FALSE;
7005
7006      for (seg = program_headers;
7007	   seg < program_headers + elf_header.e_phnum;
7008	   ++seg)
7009	{
7010	  if (seg->p_type != PT_LOAD)
7011	    continue;
7012
7013	  if (sec->sh_addr >= seg->p_vaddr
7014	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
7015	    {
7016	      aux->seg_base = seg->p_vaddr;
7017	      break;
7018	    }
7019	}
7020    }
7021
7022  /* Second, build the unwind table from the contents of the unwind section:  */
7023  size = sec->sh_size;
7024  table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
7025                                      _("unwind table"));
7026  if (!table)
7027    return FALSE;
7028
7029  aux->table_len = size / (3 * eh_addr_size);
7030  aux->table = (struct ia64_unw_table_entry *)
7031    xcmalloc (aux->table_len, sizeof (aux->table[0]));
7032  tep = aux->table;
7033
7034  for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
7035    {
7036      tep->start.section = SHN_UNDEF;
7037      tep->end.section   = SHN_UNDEF;
7038      tep->info.section  = SHN_UNDEF;
7039      tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7040      tep->end.offset   = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7041      tep->info.offset  = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7042      tep->start.offset += aux->seg_base;
7043      tep->end.offset   += aux->seg_base;
7044      tep->info.offset  += aux->seg_base;
7045    }
7046  free (table);
7047
7048  /* Third, apply any relocations to the unwind table:  */
7049  for (relsec = section_headers;
7050       relsec < section_headers + elf_header.e_shnum;
7051       ++relsec)
7052    {
7053      if (relsec->sh_type != SHT_RELA
7054	  || relsec->sh_info >= elf_header.e_shnum
7055	  || section_headers + relsec->sh_info != sec)
7056	continue;
7057
7058      if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
7059			      & rela, & nrelas))
7060	{
7061	  free (aux->table);
7062	  aux->table = NULL;
7063	  aux->table_len = 0;
7064	  return FALSE;
7065	}
7066
7067      for (rp = rela; rp < rela + nrelas; ++rp)
7068	{
7069	  relname = elf_ia64_reloc_type (get_reloc_type (rp->r_info));
7070	  sym = aux->symtab + get_reloc_symindex (rp->r_info);
7071
7072	  /* PR 17531: file: 9fa67536.  */
7073	  if (relname == NULL)
7074	    {
7075	      warn (_("Skipping unknown relocation type: %u\n"), get_reloc_type (rp->r_info));
7076	      continue;
7077	    }
7078
7079	  if (! const_strneq (relname, "R_IA64_SEGREL"))
7080	    {
7081	      warn (_("Skipping unexpected relocation type: %s\n"), relname);
7082	      continue;
7083	    }
7084
7085	  i = rp->r_offset / (3 * eh_addr_size);
7086
7087	  /* PR 17531: file: 5bc8d9bf.  */
7088	  if (i >= aux->table_len)
7089	    {
7090	      warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
7091	      continue;
7092	    }
7093
7094	  switch (rp->r_offset / eh_addr_size % 3)
7095	    {
7096	    case 0:
7097	      aux->table[i].start.section = sym->st_shndx;
7098	      aux->table[i].start.offset  = rp->r_addend + sym->st_value;
7099	      break;
7100	    case 1:
7101	      aux->table[i].end.section   = sym->st_shndx;
7102	      aux->table[i].end.offset    = rp->r_addend + sym->st_value;
7103	      break;
7104	    case 2:
7105	      aux->table[i].info.section  = sym->st_shndx;
7106	      aux->table[i].info.offset   = rp->r_addend + sym->st_value;
7107	      break;
7108	    default:
7109	      break;
7110	    }
7111	}
7112
7113      free (rela);
7114    }
7115
7116  return TRUE;
7117}
7118
7119static void
7120ia64_process_unwind (FILE * file)
7121{
7122  Elf_Internal_Shdr * sec;
7123  Elf_Internal_Shdr * unwsec = NULL;
7124  Elf_Internal_Shdr * strsec;
7125  unsigned long i, unwcount = 0, unwstart = 0;
7126  struct ia64_unw_aux_info aux;
7127
7128  memset (& aux, 0, sizeof (aux));
7129
7130  for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7131    {
7132      if (sec->sh_type == SHT_SYMTAB
7133	  && sec->sh_link < elf_header.e_shnum)
7134	{
7135	  aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
7136
7137	  strsec = section_headers + sec->sh_link;
7138	  if (aux.strtab != NULL)
7139	    {
7140	      error (_("Multiple auxillary string tables encountered\n"));
7141	      free (aux.strtab);
7142	    }
7143	  aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
7144                                          1, strsec->sh_size,
7145                                          _("string table"));
7146	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
7147	}
7148      else if (sec->sh_type == SHT_IA_64_UNWIND)
7149	unwcount++;
7150    }
7151
7152  if (!unwcount)
7153    printf (_("\nThere are no unwind sections in this file.\n"));
7154
7155  while (unwcount-- > 0)
7156    {
7157      char * suffix;
7158      size_t len, len2;
7159
7160      for (i = unwstart, sec = section_headers + unwstart, unwsec = NULL;
7161	   i < elf_header.e_shnum; ++i, ++sec)
7162	if (sec->sh_type == SHT_IA_64_UNWIND)
7163	  {
7164	    unwsec = sec;
7165	    break;
7166	  }
7167      /* We have already counted the number of SHT_IA64_UNWIND
7168	 sections so the loop above should never fail.  */
7169      assert (unwsec != NULL);
7170
7171      unwstart = i + 1;
7172      len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
7173
7174      if ((unwsec->sh_flags & SHF_GROUP) != 0)
7175	{
7176	  /* We need to find which section group it is in.  */
7177	  struct group_list * g;
7178
7179	  if (section_headers_groups == NULL
7180	      || section_headers_groups [i] == NULL)
7181	    i = elf_header.e_shnum;
7182	  else
7183	    {
7184	      g = section_headers_groups [i]->root;
7185
7186	      for (; g != NULL; g = g->next)
7187		{
7188		  sec = section_headers + g->section_index;
7189
7190		  if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
7191		    break;
7192		}
7193
7194	      if (g == NULL)
7195		i = elf_header.e_shnum;
7196	    }
7197	}
7198      else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
7199	{
7200	  /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO.  */
7201	  len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
7202	  suffix = SECTION_NAME (unwsec) + len;
7203	  for (i = 0, sec = section_headers; i < elf_header.e_shnum;
7204	       ++i, ++sec)
7205	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
7206		&& streq (SECTION_NAME (sec) + len2, suffix))
7207	      break;
7208	}
7209      else
7210	{
7211	  /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
7212	     .IA_64.unwind or BAR -> .IA_64.unwind_info.  */
7213	  len = sizeof (ELF_STRING_ia64_unwind) - 1;
7214	  len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
7215	  suffix = "";
7216	  if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
7217	    suffix = SECTION_NAME (unwsec) + len;
7218	  for (i = 0, sec = section_headers; i < elf_header.e_shnum;
7219	       ++i, ++sec)
7220	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
7221		&& streq (SECTION_NAME (sec) + len2, suffix))
7222	      break;
7223	}
7224
7225      if (i == elf_header.e_shnum)
7226	{
7227	  printf (_("\nCould not find unwind info section for "));
7228
7229	  if (string_table == NULL)
7230	    printf ("%d", unwsec->sh_name);
7231	  else
7232	    printf ("'%s'", printable_section_name (unwsec));
7233	}
7234      else
7235	{
7236	  aux.info_addr = sec->sh_addr;
7237	  aux.info = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1,
7238						 sec->sh_size,
7239						 _("unwind info"));
7240	  aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
7241
7242	  printf (_("\nUnwind section "));
7243
7244	  if (string_table == NULL)
7245	    printf ("%d", unwsec->sh_name);
7246	  else
7247	    printf ("'%s'", printable_section_name (unwsec));
7248
7249	  printf (_(" at offset 0x%lx contains %lu entries:\n"),
7250		  (unsigned long) unwsec->sh_offset,
7251		  (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
7252
7253	  if (slurp_ia64_unwind_table (file, & aux, unwsec)
7254	      && aux.table_len > 0)
7255	    dump_ia64_unwind (& aux);
7256
7257	  if (aux.table)
7258	    free ((char *) aux.table);
7259	  if (aux.info)
7260	    free ((char *) aux.info);
7261	  aux.table = NULL;
7262	  aux.info = NULL;
7263	}
7264    }
7265
7266  if (aux.symtab)
7267    free (aux.symtab);
7268  if (aux.strtab)
7269    free ((char *) aux.strtab);
7270}
7271
7272struct hppa_unw_table_entry
7273  {
7274    struct absaddr start;
7275    struct absaddr end;
7276    unsigned int Cannot_unwind:1;		/* 0 */
7277    unsigned int Millicode:1;			/* 1 */
7278    unsigned int Millicode_save_sr0:1;		/* 2 */
7279    unsigned int Region_description:2;		/* 3..4 */
7280    unsigned int reserved1:1;			/* 5 */
7281    unsigned int Entry_SR:1;			/* 6 */
7282    unsigned int Entry_FR:4;     /* number saved */	/* 7..10 */
7283    unsigned int Entry_GR:5;     /* number saved */	/* 11..15 */
7284    unsigned int Args_stored:1;			/* 16 */
7285    unsigned int Variable_Frame:1;		/* 17 */
7286    unsigned int Separate_Package_Body:1;	/* 18 */
7287    unsigned int Frame_Extension_Millicode:1;	/* 19 */
7288    unsigned int Stack_Overflow_Check:1;	/* 20 */
7289    unsigned int Two_Instruction_SP_Increment:1;/* 21 */
7290    unsigned int Ada_Region:1;			/* 22 */
7291    unsigned int cxx_info:1;			/* 23 */
7292    unsigned int cxx_try_catch:1;		/* 24 */
7293    unsigned int sched_entry_seq:1;		/* 25 */
7294    unsigned int reserved2:1;			/* 26 */
7295    unsigned int Save_SP:1;			/* 27 */
7296    unsigned int Save_RP:1;			/* 28 */
7297    unsigned int Save_MRP_in_frame:1;		/* 29 */
7298    unsigned int extn_ptr_defined:1;		/* 30 */
7299    unsigned int Cleanup_defined:1;		/* 31 */
7300
7301    unsigned int MPE_XL_interrupt_marker:1;	/* 0 */
7302    unsigned int HP_UX_interrupt_marker:1;	/* 1 */
7303    unsigned int Large_frame:1;			/* 2 */
7304    unsigned int Pseudo_SP_Set:1;		/* 3 */
7305    unsigned int reserved4:1;			/* 4 */
7306    unsigned int Total_frame_size:27;		/* 5..31 */
7307  };
7308
7309struct hppa_unw_aux_info
7310{
7311  struct hppa_unw_table_entry * table;	/* Unwind table.  */
7312  unsigned long table_len;		/* Length of unwind table.  */
7313  bfd_vma seg_base;			/* Starting address of segment.  */
7314  Elf_Internal_Sym * symtab;		/* The symbol table.  */
7315  unsigned long nsyms;			/* Number of symbols.  */
7316  Elf_Internal_Sym * funtab;		/* Sorted table of STT_FUNC symbols.  */
7317  unsigned long nfuns;			/* Number of entries in funtab.  */
7318  char * strtab;			/* The string table.  */
7319  unsigned long strtab_size;		/* Size of string table.  */
7320};
7321
7322static void
7323dump_hppa_unwind (struct hppa_unw_aux_info * aux)
7324{
7325  struct hppa_unw_table_entry * tp;
7326  unsigned long j, nfuns;
7327
7328  aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7329  for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7330    if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7331      aux->funtab[nfuns++] = aux->symtab[j];
7332  aux->nfuns = nfuns;
7333  qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7334
7335  for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7336    {
7337      bfd_vma offset;
7338      const char * procname;
7339
7340      find_symbol_for_address (aux->funtab, aux->nfuns, aux->strtab,
7341			       aux->strtab_size, tp->start, &procname,
7342			       &offset);
7343
7344      fputs ("\n<", stdout);
7345
7346      if (procname)
7347	{
7348	  fputs (procname, stdout);
7349
7350	  if (offset)
7351	    printf ("+%lx", (unsigned long) offset);
7352	}
7353
7354      fputs (">: [", stdout);
7355      print_vma (tp->start.offset, PREFIX_HEX);
7356      fputc ('-', stdout);
7357      print_vma (tp->end.offset, PREFIX_HEX);
7358      printf ("]\n\t");
7359
7360#define PF(_m) if (tp->_m) printf (#_m " ");
7361#define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
7362      PF(Cannot_unwind);
7363      PF(Millicode);
7364      PF(Millicode_save_sr0);
7365      /* PV(Region_description);  */
7366      PF(Entry_SR);
7367      PV(Entry_FR);
7368      PV(Entry_GR);
7369      PF(Args_stored);
7370      PF(Variable_Frame);
7371      PF(Separate_Package_Body);
7372      PF(Frame_Extension_Millicode);
7373      PF(Stack_Overflow_Check);
7374      PF(Two_Instruction_SP_Increment);
7375      PF(Ada_Region);
7376      PF(cxx_info);
7377      PF(cxx_try_catch);
7378      PF(sched_entry_seq);
7379      PF(Save_SP);
7380      PF(Save_RP);
7381      PF(Save_MRP_in_frame);
7382      PF(extn_ptr_defined);
7383      PF(Cleanup_defined);
7384      PF(MPE_XL_interrupt_marker);
7385      PF(HP_UX_interrupt_marker);
7386      PF(Large_frame);
7387      PF(Pseudo_SP_Set);
7388      PV(Total_frame_size);
7389#undef PF
7390#undef PV
7391    }
7392
7393  printf ("\n");
7394
7395  free (aux->funtab);
7396}
7397
7398static int
7399slurp_hppa_unwind_table (FILE * file,
7400			 struct hppa_unw_aux_info * aux,
7401			 Elf_Internal_Shdr * sec)
7402{
7403  unsigned long size, unw_ent_size, nentries, nrelas, i;
7404  Elf_Internal_Phdr * seg;
7405  struct hppa_unw_table_entry * tep;
7406  Elf_Internal_Shdr * relsec;
7407  Elf_Internal_Rela * rela;
7408  Elf_Internal_Rela * rp;
7409  unsigned char * table;
7410  unsigned char * tp;
7411  Elf_Internal_Sym * sym;
7412  const char * relname;
7413
7414  /* First, find the starting address of the segment that includes
7415     this section.  */
7416
7417  if (elf_header.e_phnum)
7418    {
7419      if (! get_program_headers (file))
7420	return 0;
7421
7422      for (seg = program_headers;
7423	   seg < program_headers + elf_header.e_phnum;
7424	   ++seg)
7425	{
7426	  if (seg->p_type != PT_LOAD)
7427	    continue;
7428
7429	  if (sec->sh_addr >= seg->p_vaddr
7430	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
7431	    {
7432	      aux->seg_base = seg->p_vaddr;
7433	      break;
7434	    }
7435	}
7436    }
7437
7438  /* Second, build the unwind table from the contents of the unwind
7439     section.  */
7440  size = sec->sh_size;
7441  table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
7442                                      _("unwind table"));
7443  if (!table)
7444    return 0;
7445
7446  unw_ent_size = 16;
7447  nentries = size / unw_ent_size;
7448  size = unw_ent_size * nentries;
7449
7450  tep = aux->table = (struct hppa_unw_table_entry *)
7451      xcmalloc (nentries, sizeof (aux->table[0]));
7452
7453  for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
7454    {
7455      unsigned int tmp1, tmp2;
7456
7457      tep->start.section = SHN_UNDEF;
7458      tep->end.section   = SHN_UNDEF;
7459
7460      tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
7461      tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
7462      tmp1 = byte_get ((unsigned char *) tp + 8, 4);
7463      tmp2 = byte_get ((unsigned char *) tp + 12, 4);
7464
7465      tep->start.offset += aux->seg_base;
7466      tep->end.offset   += aux->seg_base;
7467
7468      tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
7469      tep->Millicode = (tmp1 >> 30) & 0x1;
7470      tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
7471      tep->Region_description = (tmp1 >> 27) & 0x3;
7472      tep->reserved1 = (tmp1 >> 26) & 0x1;
7473      tep->Entry_SR = (tmp1 >> 25) & 0x1;
7474      tep->Entry_FR = (tmp1 >> 21) & 0xf;
7475      tep->Entry_GR = (tmp1 >> 16) & 0x1f;
7476      tep->Args_stored = (tmp1 >> 15) & 0x1;
7477      tep->Variable_Frame = (tmp1 >> 14) & 0x1;
7478      tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
7479      tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
7480      tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
7481      tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
7482      tep->Ada_Region = (tmp1 >> 9) & 0x1;
7483      tep->cxx_info = (tmp1 >> 8) & 0x1;
7484      tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
7485      tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
7486      tep->reserved2 = (tmp1 >> 5) & 0x1;
7487      tep->Save_SP = (tmp1 >> 4) & 0x1;
7488      tep->Save_RP = (tmp1 >> 3) & 0x1;
7489      tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
7490      tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
7491      tep->Cleanup_defined = tmp1 & 0x1;
7492
7493      tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
7494      tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
7495      tep->Large_frame = (tmp2 >> 29) & 0x1;
7496      tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
7497      tep->reserved4 = (tmp2 >> 27) & 0x1;
7498      tep->Total_frame_size = tmp2 & 0x7ffffff;
7499    }
7500  free (table);
7501
7502  /* Third, apply any relocations to the unwind table.  */
7503  for (relsec = section_headers;
7504       relsec < section_headers + elf_header.e_shnum;
7505       ++relsec)
7506    {
7507      if (relsec->sh_type != SHT_RELA
7508	  || relsec->sh_info >= elf_header.e_shnum
7509	  || section_headers + relsec->sh_info != sec)
7510	continue;
7511
7512      if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
7513			      & rela, & nrelas))
7514	return 0;
7515
7516      for (rp = rela; rp < rela + nrelas; ++rp)
7517	{
7518	  relname = elf_hppa_reloc_type (get_reloc_type (rp->r_info));
7519	  sym = aux->symtab + get_reloc_symindex (rp->r_info);
7520
7521	  /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64.  */
7522	  if (! const_strneq (relname, "R_PARISC_SEGREL"))
7523	    {
7524	      warn (_("Skipping unexpected relocation type %s\n"), relname);
7525	      continue;
7526	    }
7527
7528	  i = rp->r_offset / unw_ent_size;
7529
7530	  switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
7531	    {
7532	    case 0:
7533	      aux->table[i].start.section = sym->st_shndx;
7534	      aux->table[i].start.offset  = sym->st_value + rp->r_addend;
7535	      break;
7536	    case 1:
7537	      aux->table[i].end.section   = sym->st_shndx;
7538	      aux->table[i].end.offset    = sym->st_value + rp->r_addend;
7539	      break;
7540	    default:
7541	      break;
7542	    }
7543	}
7544
7545      free (rela);
7546    }
7547
7548  aux->table_len = nentries;
7549
7550  return 1;
7551}
7552
7553static void
7554hppa_process_unwind (FILE * file)
7555{
7556  struct hppa_unw_aux_info aux;
7557  Elf_Internal_Shdr * unwsec = NULL;
7558  Elf_Internal_Shdr * strsec;
7559  Elf_Internal_Shdr * sec;
7560  unsigned long i;
7561
7562  if (string_table == NULL)
7563    return;
7564
7565  memset (& aux, 0, sizeof (aux));
7566
7567  for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7568    {
7569      if (sec->sh_type == SHT_SYMTAB
7570	  && sec->sh_link < elf_header.e_shnum)
7571	{
7572	  aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
7573
7574	  strsec = section_headers + sec->sh_link;
7575	  if (aux.strtab != NULL)
7576	    {
7577	      error (_("Multiple auxillary string tables encountered\n"));
7578	      free (aux.strtab);
7579	    }
7580	  aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
7581                                          1, strsec->sh_size,
7582                                          _("string table"));
7583	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
7584	}
7585      else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
7586	unwsec = sec;
7587    }
7588
7589  if (!unwsec)
7590    printf (_("\nThere are no unwind sections in this file.\n"));
7591
7592  for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7593    {
7594      if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
7595	{
7596	  printf (_("\nUnwind section '%s' at offset 0x%lx contains %lu entries:\n"),
7597		  printable_section_name (sec),
7598		  (unsigned long) sec->sh_offset,
7599		  (unsigned long) (sec->sh_size / (2 * eh_addr_size + 8)));
7600
7601          slurp_hppa_unwind_table (file, &aux, sec);
7602	  if (aux.table_len > 0)
7603	    dump_hppa_unwind (&aux);
7604
7605	  if (aux.table)
7606	    free ((char *) aux.table);
7607	  aux.table = NULL;
7608	}
7609    }
7610
7611  if (aux.symtab)
7612    free (aux.symtab);
7613  if (aux.strtab)
7614    free ((char *) aux.strtab);
7615}
7616
7617struct arm_section
7618{
7619  unsigned char *      data;		/* The unwind data.  */
7620  Elf_Internal_Shdr *  sec;		/* The cached unwind section header.  */
7621  Elf_Internal_Rela *  rela;		/* The cached relocations for this section.  */
7622  unsigned long        nrelas;		/* The number of relocations.  */
7623  unsigned int         rel_type;	/* REL or RELA ?  */
7624  Elf_Internal_Rela *  next_rela;	/* Cyclic pointer to the next reloc to process.  */
7625};
7626
7627struct arm_unw_aux_info
7628{
7629  FILE *              file;		/* The file containing the unwind sections.  */
7630  Elf_Internal_Sym *  symtab;		/* The file's symbol table.  */
7631  unsigned long       nsyms;		/* Number of symbols.  */
7632  Elf_Internal_Sym *  funtab;		/* Sorted table of STT_FUNC symbols.  */
7633  unsigned long       nfuns;		/* Number of these symbols.  */
7634  char *              strtab;		/* The file's string table.  */
7635  unsigned long       strtab_size;	/* Size of string table.  */
7636};
7637
7638static const char *
7639arm_print_vma_and_name (struct arm_unw_aux_info *aux,
7640			bfd_vma fn, struct absaddr addr)
7641{
7642  const char *procname;
7643  bfd_vma sym_offset;
7644
7645  if (addr.section == SHN_UNDEF)
7646    addr.offset = fn;
7647
7648  find_symbol_for_address (aux->funtab, aux->nfuns, aux->strtab,
7649			   aux->strtab_size, addr, &procname,
7650			   &sym_offset);
7651
7652  print_vma (fn, PREFIX_HEX);
7653
7654  if (procname)
7655    {
7656      fputs (" <", stdout);
7657      fputs (procname, stdout);
7658
7659      if (sym_offset)
7660	printf ("+0x%lx", (unsigned long) sym_offset);
7661      fputc ('>', stdout);
7662    }
7663
7664  return procname;
7665}
7666
7667static void
7668arm_free_section (struct arm_section *arm_sec)
7669{
7670  if (arm_sec->data != NULL)
7671    free (arm_sec->data);
7672
7673  if (arm_sec->rela != NULL)
7674    free (arm_sec->rela);
7675}
7676
7677/* 1) If SEC does not match the one cached in ARM_SEC, then free the current
7678      cached section and install SEC instead.
7679   2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
7680      and return its valued in * WORDP, relocating if necessary.
7681   3) Update the NEXT_RELA field in ARM_SEC and store the section index and
7682      relocation's offset in ADDR.
7683   4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
7684      into the string table of the symbol associated with the reloc.  If no
7685      reloc was applied store -1 there.
7686   5) Return TRUE upon success, FALSE otherwise.  */
7687
7688static bfd_boolean
7689get_unwind_section_word (struct arm_unw_aux_info *  aux,
7690			 struct arm_section *       arm_sec,
7691			 Elf_Internal_Shdr *        sec,
7692			 bfd_vma 		    word_offset,
7693			 unsigned int *             wordp,
7694			 struct absaddr *           addr,
7695			 bfd_vma *		    sym_name)
7696{
7697  Elf_Internal_Rela *rp;
7698  Elf_Internal_Sym *sym;
7699  const char * relname;
7700  unsigned int word;
7701  bfd_boolean wrapped;
7702
7703  if (sec == NULL || arm_sec == NULL)
7704    return FALSE;
7705
7706  addr->section = SHN_UNDEF;
7707  addr->offset = 0;
7708
7709  if (sym_name != NULL)
7710    *sym_name = (bfd_vma) -1;
7711
7712  /* If necessary, update the section cache.  */
7713  if (sec != arm_sec->sec)
7714    {
7715      Elf_Internal_Shdr *relsec;
7716
7717      arm_free_section (arm_sec);
7718
7719      arm_sec->sec = sec;
7720      arm_sec->data = get_data (NULL, aux->file, sec->sh_offset, 1,
7721				sec->sh_size, _("unwind data"));
7722      arm_sec->rela = NULL;
7723      arm_sec->nrelas = 0;
7724
7725      for (relsec = section_headers;
7726	   relsec < section_headers + elf_header.e_shnum;
7727	   ++relsec)
7728	{
7729	  if (relsec->sh_info >= elf_header.e_shnum
7730	      || section_headers + relsec->sh_info != sec
7731	      /* PR 15745: Check the section type as well.  */
7732	      || (relsec->sh_type != SHT_REL
7733		  && relsec->sh_type != SHT_RELA))
7734	    continue;
7735
7736	  arm_sec->rel_type = relsec->sh_type;
7737	  if (relsec->sh_type == SHT_REL)
7738	    {
7739	      if (!slurp_rel_relocs (aux->file, relsec->sh_offset,
7740				     relsec->sh_size,
7741				     & arm_sec->rela, & arm_sec->nrelas))
7742		return FALSE;
7743	    }
7744	  else /* relsec->sh_type == SHT_RELA */
7745	    {
7746	      if (!slurp_rela_relocs (aux->file, relsec->sh_offset,
7747				      relsec->sh_size,
7748				      & arm_sec->rela, & arm_sec->nrelas))
7749		return FALSE;
7750	    }
7751	  break;
7752	}
7753
7754      arm_sec->next_rela = arm_sec->rela;
7755    }
7756
7757  /* If there is no unwind data we can do nothing.  */
7758  if (arm_sec->data == NULL)
7759    return FALSE;
7760
7761  /* If the offset is invalid then fail.  */
7762  if (word_offset > (sec->sh_size - 4)
7763      /* PR 18879 */
7764      || (sec->sh_size < 5 && word_offset >= sec->sh_size)
7765      || ((bfd_signed_vma) word_offset) < 0)
7766    return FALSE;
7767
7768  /* Get the word at the required offset.  */
7769  word = byte_get (arm_sec->data + word_offset, 4);
7770
7771  /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128.  */
7772  if (arm_sec->rela == NULL)
7773    {
7774      * wordp = word;
7775      return TRUE;
7776    }
7777
7778  /* Look through the relocs to find the one that applies to the provided offset.  */
7779  wrapped = FALSE;
7780  for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
7781    {
7782      bfd_vma prelval, offset;
7783
7784      if (rp->r_offset > word_offset && !wrapped)
7785	{
7786	  rp = arm_sec->rela;
7787	  wrapped = TRUE;
7788	}
7789      if (rp->r_offset > word_offset)
7790	break;
7791
7792      if (rp->r_offset & 3)
7793	{
7794	  warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
7795		(unsigned long) rp->r_offset);
7796	  continue;
7797	}
7798
7799      if (rp->r_offset < word_offset)
7800	continue;
7801
7802      /* PR 17531: file: 027-161405-0.004  */
7803      if (aux->symtab == NULL)
7804	continue;
7805
7806      if (arm_sec->rel_type == SHT_REL)
7807	{
7808	  offset = word & 0x7fffffff;
7809	  if (offset & 0x40000000)
7810	    offset |= ~ (bfd_vma) 0x7fffffff;
7811	}
7812      else if (arm_sec->rel_type == SHT_RELA)
7813	offset = rp->r_addend;
7814      else
7815	{
7816	  error (_("Unknown section relocation type %d encountered\n"),
7817		 arm_sec->rel_type);
7818	  break;
7819	}
7820
7821      /* PR 17531 file: 027-1241568-0.004.  */
7822      if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
7823	{
7824	  error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
7825		 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
7826	  break;
7827	}
7828
7829      sym = aux->symtab + ELF32_R_SYM (rp->r_info);
7830      offset += sym->st_value;
7831      prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
7832
7833      /* Check that we are processing the expected reloc type.  */
7834      if (elf_header.e_machine == EM_ARM)
7835	{
7836	  relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
7837	  if (relname == NULL)
7838	    {
7839	      warn (_("Skipping unknown ARM relocation type: %d\n"),
7840		    (int) ELF32_R_TYPE (rp->r_info));
7841	      continue;
7842	    }
7843
7844	  if (streq (relname, "R_ARM_NONE"))
7845	      continue;
7846
7847	  if (! streq (relname, "R_ARM_PREL31"))
7848	    {
7849	      warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
7850	      continue;
7851	    }
7852	}
7853      else if (elf_header.e_machine == EM_TI_C6000)
7854	{
7855	  relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
7856	  if (relname == NULL)
7857	    {
7858	      warn (_("Skipping unknown C6000 relocation type: %d\n"),
7859		    (int) ELF32_R_TYPE (rp->r_info));
7860	      continue;
7861	    }
7862
7863	  if (streq (relname, "R_C6000_NONE"))
7864	    continue;
7865
7866	  if (! streq (relname, "R_C6000_PREL31"))
7867	    {
7868	      warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
7869	      continue;
7870	    }
7871
7872	  prelval >>= 1;
7873	}
7874      else
7875	{
7876	  /* This function currently only supports ARM and TI unwinders.  */
7877	  warn (_("Only TI and ARM unwinders are currently supported\n"));
7878	  break;
7879	}
7880
7881      word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
7882      addr->section = sym->st_shndx;
7883      addr->offset = offset;
7884
7885      if (sym_name)
7886	* sym_name = sym->st_name;
7887      break;
7888    }
7889
7890  *wordp = word;
7891  arm_sec->next_rela = rp;
7892
7893  return TRUE;
7894}
7895
7896static const char *tic6x_unwind_regnames[16] =
7897{
7898  "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
7899  "A14", "A13", "A12", "A11", "A10",
7900  "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
7901};
7902
7903static void
7904decode_tic6x_unwind_regmask (unsigned int mask)
7905{
7906  int i;
7907
7908  for (i = 12; mask; mask >>= 1, i--)
7909    {
7910      if (mask & 1)
7911	{
7912	  fputs (tic6x_unwind_regnames[i], stdout);
7913	  if (mask > 1)
7914	    fputs (", ", stdout);
7915	}
7916    }
7917}
7918
7919#define ADVANCE							\
7920  if (remaining == 0 && more_words)				\
7921    {								\
7922      data_offset += 4;						\
7923      if (! get_unwind_section_word (aux, data_arm_sec, data_sec,	\
7924				     data_offset, & word, & addr, NULL))	\
7925	return;							\
7926      remaining = 4;						\
7927      more_words--;						\
7928    }								\
7929
7930#define GET_OP(OP)			\
7931  ADVANCE;				\
7932  if (remaining)			\
7933    {					\
7934      remaining--;			\
7935      (OP) = word >> 24;		\
7936      word <<= 8;			\
7937    }					\
7938  else					\
7939    {					\
7940      printf (_("[Truncated opcode]\n"));	\
7941      return;				\
7942    }					\
7943  printf ("0x%02x ", OP)
7944
7945static void
7946decode_arm_unwind_bytecode (struct arm_unw_aux_info *  aux,
7947			    unsigned int               word,
7948			    unsigned int               remaining,
7949			    unsigned int               more_words,
7950			    bfd_vma                    data_offset,
7951			    Elf_Internal_Shdr *        data_sec,
7952			    struct arm_section *       data_arm_sec)
7953{
7954  struct absaddr addr;
7955
7956  /* Decode the unwinding instructions.  */
7957  while (1)
7958    {
7959      unsigned int op, op2;
7960
7961      ADVANCE;
7962      if (remaining == 0)
7963	break;
7964      remaining--;
7965      op = word >> 24;
7966      word <<= 8;
7967
7968      printf ("  0x%02x ", op);
7969
7970      if ((op & 0xc0) == 0x00)
7971	{
7972	  int offset = ((op & 0x3f) << 2) + 4;
7973
7974	  printf ("     vsp = vsp + %d", offset);
7975	}
7976      else if ((op & 0xc0) == 0x40)
7977	{
7978	  int offset = ((op & 0x3f) << 2) + 4;
7979
7980	  printf ("     vsp = vsp - %d", offset);
7981	}
7982      else if ((op & 0xf0) == 0x80)
7983	{
7984	  GET_OP (op2);
7985	  if (op == 0x80 && op2 == 0)
7986	    printf (_("Refuse to unwind"));
7987	  else
7988	    {
7989	      unsigned int mask = ((op & 0x0f) << 8) | op2;
7990	      int first = 1;
7991	      int i;
7992
7993	      printf ("pop {");
7994	      for (i = 0; i < 12; i++)
7995		if (mask & (1 << i))
7996		  {
7997		    if (first)
7998		      first = 0;
7999		    else
8000		      printf (", ");
8001		    printf ("r%d", 4 + i);
8002		  }
8003	      printf ("}");
8004	    }
8005	}
8006      else if ((op & 0xf0) == 0x90)
8007	{
8008	  if (op == 0x9d || op == 0x9f)
8009	    printf (_("     [Reserved]"));
8010	  else
8011	    printf ("     vsp = r%d", op & 0x0f);
8012	}
8013      else if ((op & 0xf0) == 0xa0)
8014	{
8015	  int end = 4 + (op & 0x07);
8016	  int first = 1;
8017	  int i;
8018
8019	  printf ("     pop {");
8020	  for (i = 4; i <= end; i++)
8021	    {
8022	      if (first)
8023		first = 0;
8024	      else
8025		printf (", ");
8026	      printf ("r%d", i);
8027	    }
8028	  if (op & 0x08)
8029	    {
8030	      if (!first)
8031		printf (", ");
8032	      printf ("r14");
8033	    }
8034	  printf ("}");
8035	}
8036      else if (op == 0xb0)
8037	printf (_("     finish"));
8038      else if (op == 0xb1)
8039	{
8040	  GET_OP (op2);
8041	  if (op2 == 0 || (op2 & 0xf0) != 0)
8042	    printf (_("[Spare]"));
8043	  else
8044	    {
8045	      unsigned int mask = op2 & 0x0f;
8046	      int first = 1;
8047	      int i;
8048
8049	      printf ("pop {");
8050	      for (i = 0; i < 12; i++)
8051		if (mask & (1 << i))
8052		  {
8053		    if (first)
8054		      first = 0;
8055		    else
8056		      printf (", ");
8057		    printf ("r%d", i);
8058		  }
8059	      printf ("}");
8060	    }
8061	}
8062      else if (op == 0xb2)
8063	{
8064	  unsigned char buf[9];
8065	  unsigned int i, len;
8066	  unsigned long offset;
8067
8068	  for (i = 0; i < sizeof (buf); i++)
8069	    {
8070	      GET_OP (buf[i]);
8071	      if ((buf[i] & 0x80) == 0)
8072		break;
8073	    }
8074	  if (i == sizeof (buf))
8075	    printf (_("corrupt change to vsp"));
8076	  else
8077	    {
8078	      offset = read_uleb128 (buf, &len, buf + i + 1);
8079	      assert (len == i + 1);
8080	      offset = offset * 4 + 0x204;
8081	      printf ("vsp = vsp + %ld", offset);
8082	    }
8083	}
8084      else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
8085	{
8086	  unsigned int first, last;
8087
8088	  GET_OP (op2);
8089	  first = op2 >> 4;
8090	  last = op2 & 0x0f;
8091	  if (op == 0xc8)
8092	    first = first + 16;
8093	  printf ("pop {D%d", first);
8094	  if (last)
8095	    printf ("-D%d", first + last);
8096	  printf ("}");
8097	}
8098      else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
8099	{
8100	  unsigned int count = op & 0x07;
8101
8102	  printf ("pop {D8");
8103	  if (count)
8104	    printf ("-D%d", 8 + count);
8105	  printf ("}");
8106	}
8107      else if (op >= 0xc0 && op <= 0xc5)
8108	{
8109	  unsigned int count = op & 0x07;
8110
8111	  printf ("     pop {wR10");
8112	  if (count)
8113	    printf ("-wR%d", 10 + count);
8114	  printf ("}");
8115	}
8116      else if (op == 0xc6)
8117	{
8118	  unsigned int first, last;
8119
8120	  GET_OP (op2);
8121	  first = op2 >> 4;
8122	  last = op2 & 0x0f;
8123	  printf ("pop {wR%d", first);
8124	  if (last)
8125	    printf ("-wR%d", first + last);
8126	  printf ("}");
8127	}
8128      else if (op == 0xc7)
8129	{
8130	  GET_OP (op2);
8131	  if (op2 == 0 || (op2 & 0xf0) != 0)
8132	    printf (_("[Spare]"));
8133	  else
8134	    {
8135	      unsigned int mask = op2 & 0x0f;
8136	      int first = 1;
8137	      int i;
8138
8139	      printf ("pop {");
8140	      for (i = 0; i < 4; i++)
8141		if (mask & (1 << i))
8142		  {
8143		    if (first)
8144		      first = 0;
8145		    else
8146		      printf (", ");
8147		    printf ("wCGR%d", i);
8148		  }
8149	      printf ("}");
8150	    }
8151	}
8152      else
8153	printf (_("     [unsupported opcode]"));
8154      printf ("\n");
8155    }
8156}
8157
8158static void
8159decode_tic6x_unwind_bytecode (struct arm_unw_aux_info *  aux,
8160			      unsigned int               word,
8161			      unsigned int               remaining,
8162			      unsigned int               more_words,
8163			      bfd_vma                    data_offset,
8164			      Elf_Internal_Shdr *        data_sec,
8165			      struct arm_section *       data_arm_sec)
8166{
8167  struct absaddr addr;
8168
8169  /* Decode the unwinding instructions.  */
8170  while (1)
8171    {
8172      unsigned int op, op2;
8173
8174      ADVANCE;
8175      if (remaining == 0)
8176	break;
8177      remaining--;
8178      op = word >> 24;
8179      word <<= 8;
8180
8181      printf ("  0x%02x ", op);
8182
8183      if ((op & 0xc0) == 0x00)
8184	{
8185	  int offset = ((op & 0x3f) << 3) + 8;
8186	  printf ("     sp = sp + %d", offset);
8187	}
8188      else if ((op & 0xc0) == 0x80)
8189	{
8190	  GET_OP (op2);
8191	  if (op == 0x80 && op2 == 0)
8192	    printf (_("Refuse to unwind"));
8193	  else
8194	    {
8195	      unsigned int mask = ((op & 0x1f) << 8) | op2;
8196	      if (op & 0x20)
8197		printf ("pop compact {");
8198	      else
8199		printf ("pop {");
8200
8201	      decode_tic6x_unwind_regmask (mask);
8202	      printf("}");
8203	    }
8204	}
8205      else if ((op & 0xf0) == 0xc0)
8206	{
8207	  unsigned int reg;
8208	  unsigned int nregs;
8209	  unsigned int i;
8210	  const char *name;
8211	  struct
8212	  {
8213	      unsigned int offset;
8214	      unsigned int reg;
8215	  } regpos[16];
8216
8217	  /* Scan entire instruction first so that GET_OP output is not
8218	     interleaved with disassembly.  */
8219	  nregs = 0;
8220	  for (i = 0; nregs < (op & 0xf); i++)
8221	    {
8222	      GET_OP (op2);
8223	      reg = op2 >> 4;
8224	      if (reg != 0xf)
8225		{
8226		  regpos[nregs].offset = i * 2;
8227		  regpos[nregs].reg = reg;
8228		  nregs++;
8229		}
8230
8231	      reg = op2 & 0xf;
8232	      if (reg != 0xf)
8233		{
8234		  regpos[nregs].offset = i * 2 + 1;
8235		  regpos[nregs].reg = reg;
8236		  nregs++;
8237		}
8238	    }
8239
8240	  printf (_("pop frame {"));
8241	  reg = nregs - 1;
8242	  for (i = i * 2; i > 0; i--)
8243	    {
8244	      if (regpos[reg].offset == i - 1)
8245		{
8246		  name = tic6x_unwind_regnames[regpos[reg].reg];
8247		  if (reg > 0)
8248		    reg--;
8249		}
8250	      else
8251		name = _("[pad]");
8252
8253	      fputs (name, stdout);
8254	      if (i > 1)
8255		printf (", ");
8256	    }
8257
8258	  printf ("}");
8259	}
8260      else if (op == 0xd0)
8261	printf ("     MOV FP, SP");
8262      else if (op == 0xd1)
8263	printf ("     __c6xabi_pop_rts");
8264      else if (op == 0xd2)
8265	{
8266	  unsigned char buf[9];
8267	  unsigned int i, len;
8268	  unsigned long offset;
8269
8270	  for (i = 0; i < sizeof (buf); i++)
8271	    {
8272	      GET_OP (buf[i]);
8273	      if ((buf[i] & 0x80) == 0)
8274		break;
8275	    }
8276	  /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2.  */
8277	  if (i == sizeof (buf))
8278	    {
8279	      printf ("<corrupt sp adjust>\n");
8280	      warn (_("Corrupt stack pointer adjustment detected\n"));
8281	      return;
8282	    }
8283
8284	  offset = read_uleb128 (buf, &len, buf + i + 1);
8285	  assert (len == i + 1);
8286	  offset = offset * 8 + 0x408;
8287	  printf (_("sp = sp + %ld"), offset);
8288	}
8289      else if ((op & 0xf0) == 0xe0)
8290	{
8291	  if ((op & 0x0f) == 7)
8292	    printf ("     RETURN");
8293	  else
8294	    printf ("     MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
8295	}
8296      else
8297	{
8298	  printf (_("     [unsupported opcode]"));
8299	}
8300      putchar ('\n');
8301    }
8302}
8303
8304static bfd_vma
8305arm_expand_prel31 (bfd_vma word, bfd_vma where)
8306{
8307  bfd_vma offset;
8308
8309  offset = word & 0x7fffffff;
8310  if (offset & 0x40000000)
8311    offset |= ~ (bfd_vma) 0x7fffffff;
8312
8313  if (elf_header.e_machine == EM_TI_C6000)
8314    offset <<= 1;
8315
8316  return offset + where;
8317}
8318
8319static void
8320decode_arm_unwind (struct arm_unw_aux_info *  aux,
8321		   unsigned int               word,
8322		   unsigned int               remaining,
8323		   bfd_vma                    data_offset,
8324		   Elf_Internal_Shdr *        data_sec,
8325		   struct arm_section *       data_arm_sec)
8326{
8327  int per_index;
8328  unsigned int more_words = 0;
8329  struct absaddr addr;
8330  bfd_vma sym_name = (bfd_vma) -1;
8331
8332  if (remaining == 0)
8333    {
8334      /* Fetch the first word.
8335	 Note - when decoding an object file the address extracted
8336	 here will always be 0.  So we also pass in the sym_name
8337	 parameter so that we can find the symbol associated with
8338	 the personality routine.  */
8339      if (! get_unwind_section_word (aux, data_arm_sec, data_sec, data_offset,
8340				     & word, & addr, & sym_name))
8341	return;
8342
8343      remaining = 4;
8344    }
8345  else
8346    {
8347      addr.section = SHN_UNDEF;
8348      addr.offset = 0;
8349    }
8350
8351  if ((word & 0x80000000) == 0)
8352    {
8353      /* Expand prel31 for personality routine.  */
8354      bfd_vma fn;
8355      const char *procname;
8356
8357      fn = arm_expand_prel31 (word, data_sec->sh_addr + data_offset);
8358      printf (_("  Personality routine: "));
8359      if (fn == 0
8360	  && addr.section == SHN_UNDEF && addr.offset == 0
8361	  && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
8362	{
8363	  procname = aux->strtab + sym_name;
8364	  print_vma (fn, PREFIX_HEX);
8365	  if (procname)
8366	    {
8367	      fputs (" <", stdout);
8368	      fputs (procname, stdout);
8369	      fputc ('>', stdout);
8370	    }
8371	}
8372      else
8373	procname = arm_print_vma_and_name (aux, fn, addr);
8374      fputc ('\n', stdout);
8375
8376      /* The GCC personality routines use the standard compact
8377	 encoding, starting with one byte giving the number of
8378	 words.  */
8379      if (procname != NULL
8380	  && (const_strneq (procname, "__gcc_personality_v0")
8381	      || const_strneq (procname, "__gxx_personality_v0")
8382	      || const_strneq (procname, "__gcj_personality_v0")
8383	      || const_strneq (procname, "__gnu_objc_personality_v0")))
8384	{
8385	  remaining = 0;
8386	  more_words = 1;
8387	  ADVANCE;
8388	  if (!remaining)
8389	    {
8390	      printf (_("  [Truncated data]\n"));
8391	      return;
8392	    }
8393	  more_words = word >> 24;
8394	  word <<= 8;
8395	  remaining--;
8396	  per_index = -1;
8397	}
8398      else
8399	return;
8400    }
8401  else
8402    {
8403      /* ARM EHABI Section 6.3:
8404
8405	 An exception-handling table entry for the compact model looks like:
8406
8407           31 30-28 27-24 23-0
8408	   -- ----- ----- ----
8409            1   0   index Data for personalityRoutine[index]    */
8410
8411      if (elf_header.e_machine == EM_ARM
8412	  && (word & 0x70000000))
8413	warn (_("Corrupt ARM compact model table entry: %x \n"), word);
8414
8415      per_index = (word >> 24) & 0x7f;
8416      printf (_("  Compact model index: %d\n"), per_index);
8417      if (per_index == 0)
8418	{
8419	  more_words = 0;
8420	  word <<= 8;
8421	  remaining--;
8422	}
8423      else if (per_index < 3)
8424	{
8425	  more_words = (word >> 16) & 0xff;
8426	  word <<= 16;
8427	  remaining -= 2;
8428	}
8429    }
8430
8431  switch (elf_header.e_machine)
8432    {
8433    case EM_ARM:
8434      if (per_index < 3)
8435	{
8436	  decode_arm_unwind_bytecode (aux, word, remaining, more_words,
8437				      data_offset, data_sec, data_arm_sec);
8438	}
8439      else
8440	{
8441	  warn (_("Unknown ARM compact model index encountered\n"));
8442	  printf (_("  [reserved]\n"));
8443	}
8444      break;
8445
8446    case EM_TI_C6000:
8447      if (per_index < 3)
8448	{
8449	  decode_tic6x_unwind_bytecode (aux, word, remaining, more_words,
8450					data_offset, data_sec, data_arm_sec);
8451	}
8452      else if (per_index < 5)
8453	{
8454	  if (((word >> 17) & 0x7f) == 0x7f)
8455	    printf (_("  Restore stack from frame pointer\n"));
8456	  else
8457	    printf (_("  Stack increment %d\n"), (word >> 14) & 0x1fc);
8458	  printf (_("  Registers restored: "));
8459	  if (per_index == 4)
8460	    printf (" (compact) ");
8461	  decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
8462	  putchar ('\n');
8463	  printf (_("  Return register: %s\n"),
8464		  tic6x_unwind_regnames[word & 0xf]);
8465	}
8466      else
8467	printf (_("  [reserved (%d)]\n"), per_index);
8468      break;
8469
8470    default:
8471      error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
8472	     elf_header.e_machine);
8473    }
8474
8475  /* Decode the descriptors.  Not implemented.  */
8476}
8477
8478static void
8479dump_arm_unwind (struct arm_unw_aux_info *aux, Elf_Internal_Shdr *exidx_sec)
8480{
8481  struct arm_section exidx_arm_sec, extab_arm_sec;
8482  unsigned int i, exidx_len;
8483  unsigned long j, nfuns;
8484
8485  memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
8486  memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
8487  exidx_len = exidx_sec->sh_size / 8;
8488
8489  aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8490  for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8491    if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8492      aux->funtab[nfuns++] = aux->symtab[j];
8493  aux->nfuns = nfuns;
8494  qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8495
8496  for (i = 0; i < exidx_len; i++)
8497    {
8498      unsigned int exidx_fn, exidx_entry;
8499      struct absaddr fn_addr, entry_addr;
8500      bfd_vma fn;
8501
8502      fputc ('\n', stdout);
8503
8504      if (! get_unwind_section_word (aux, & exidx_arm_sec, exidx_sec,
8505				     8 * i, & exidx_fn, & fn_addr, NULL)
8506	  || ! get_unwind_section_word (aux, & exidx_arm_sec, exidx_sec,
8507					8 * i + 4, & exidx_entry, & entry_addr, NULL))
8508	{
8509	  free (aux->funtab);
8510	  arm_free_section (& exidx_arm_sec);
8511	  arm_free_section (& extab_arm_sec);
8512	  return;
8513	}
8514
8515      /* ARM EHABI, Section 5:
8516	 An index table entry consists of 2 words.
8517         The first word contains a prel31 offset to the start of a function, with bit 31 clear.  */
8518      if (exidx_fn & 0x80000000)
8519	warn (_("corrupt index table entry: %x\n"), exidx_fn);
8520
8521      fn = arm_expand_prel31 (exidx_fn, exidx_sec->sh_addr + 8 * i);
8522
8523      arm_print_vma_and_name (aux, fn, fn_addr);
8524      fputs (": ", stdout);
8525
8526      if (exidx_entry == 1)
8527	{
8528	  print_vma (exidx_entry, PREFIX_HEX);
8529	  fputs (" [cantunwind]\n", stdout);
8530	}
8531      else if (exidx_entry & 0x80000000)
8532	{
8533	  print_vma (exidx_entry, PREFIX_HEX);
8534	  fputc ('\n', stdout);
8535	  decode_arm_unwind (aux, exidx_entry, 4, 0, NULL, NULL);
8536	}
8537      else
8538	{
8539	  bfd_vma table, table_offset = 0;
8540	  Elf_Internal_Shdr *table_sec;
8541
8542	  fputs ("@", stdout);
8543	  table = arm_expand_prel31 (exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
8544	  print_vma (table, PREFIX_HEX);
8545	  printf ("\n");
8546
8547	  /* Locate the matching .ARM.extab.  */
8548	  if (entry_addr.section != SHN_UNDEF
8549	      && entry_addr.section < elf_header.e_shnum)
8550	    {
8551	      table_sec = section_headers + entry_addr.section;
8552	      table_offset = entry_addr.offset;
8553	      /* PR 18879 */
8554	      if (table_offset > table_sec->sh_size
8555		  || ((bfd_signed_vma) table_offset) < 0)
8556		{
8557		  warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
8558			(unsigned long) table_offset,
8559			printable_section_name (table_sec));
8560		  continue;
8561		}
8562	    }
8563	  else
8564	    {
8565	      table_sec = find_section_by_address (table);
8566	      if (table_sec != NULL)
8567		table_offset = table - table_sec->sh_addr;
8568	    }
8569	  if (table_sec == NULL)
8570	    {
8571	      warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
8572		    (unsigned long) table);
8573	      continue;
8574	    }
8575	  decode_arm_unwind (aux, 0, 0, table_offset, table_sec,
8576			     &extab_arm_sec);
8577	}
8578    }
8579
8580  printf ("\n");
8581
8582  free (aux->funtab);
8583  arm_free_section (&exidx_arm_sec);
8584  arm_free_section (&extab_arm_sec);
8585}
8586
8587/* Used for both ARM and C6X unwinding tables.  */
8588
8589static void
8590arm_process_unwind (FILE *file)
8591{
8592  struct arm_unw_aux_info aux;
8593  Elf_Internal_Shdr *unwsec = NULL;
8594  Elf_Internal_Shdr *strsec;
8595  Elf_Internal_Shdr *sec;
8596  unsigned long i;
8597  unsigned int sec_type;
8598
8599  switch (elf_header.e_machine)
8600    {
8601    case EM_ARM:
8602      sec_type = SHT_ARM_EXIDX;
8603      break;
8604
8605    case EM_TI_C6000:
8606      sec_type = SHT_C6000_UNWIND;
8607      break;
8608
8609    default:
8610      error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
8611	     elf_header.e_machine);
8612      return;
8613    }
8614
8615  if (string_table == NULL)
8616    return;
8617
8618  memset (& aux, 0, sizeof (aux));
8619  aux.file = file;
8620
8621  for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
8622    {
8623      if (sec->sh_type == SHT_SYMTAB && sec->sh_link < elf_header.e_shnum)
8624	{
8625	  aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
8626
8627	  strsec = section_headers + sec->sh_link;
8628
8629	  /* PR binutils/17531 file: 011-12666-0.004.  */
8630	  if (aux.strtab != NULL)
8631	    {
8632	      error (_("Multiple string tables found in file.\n"));
8633	      free (aux.strtab);
8634	    }
8635	  aux.strtab = get_data (NULL, file, strsec->sh_offset,
8636				 1, strsec->sh_size, _("string table"));
8637	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
8638	}
8639      else if (sec->sh_type == sec_type)
8640	unwsec = sec;
8641    }
8642
8643  if (unwsec == NULL)
8644    printf (_("\nThere are no unwind sections in this file.\n"));
8645  else
8646    for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
8647      {
8648	if (sec->sh_type == sec_type)
8649	  {
8650	    printf (_("\nUnwind table index '%s' at offset 0x%lx contains %lu entries:\n"),
8651		    printable_section_name (sec),
8652		    (unsigned long) sec->sh_offset,
8653		    (unsigned long) (sec->sh_size / (2 * eh_addr_size)));
8654
8655	    dump_arm_unwind (&aux, sec);
8656	  }
8657      }
8658
8659  if (aux.symtab)
8660    free (aux.symtab);
8661  if (aux.strtab)
8662    free ((char *) aux.strtab);
8663}
8664
8665static void
8666process_unwind (FILE * file)
8667{
8668  struct unwind_handler
8669  {
8670    int machtype;
8671    void (* handler)(FILE *);
8672  } handlers[] =
8673  {
8674    { EM_ARM, arm_process_unwind },
8675    { EM_IA_64, ia64_process_unwind },
8676    { EM_PARISC, hppa_process_unwind },
8677    { EM_TI_C6000, arm_process_unwind },
8678    { 0, 0 }
8679  };
8680  int i;
8681
8682  if (!do_unwind)
8683    return;
8684
8685  for (i = 0; handlers[i].handler != NULL; i++)
8686    if (elf_header.e_machine == handlers[i].machtype)
8687      {
8688	handlers[i].handler (file);
8689	return;
8690      }
8691
8692  printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
8693	  get_machine_name (elf_header.e_machine));
8694}
8695
8696static void
8697dynamic_section_mips_val (Elf_Internal_Dyn * entry)
8698{
8699  switch (entry->d_tag)
8700    {
8701    case DT_MIPS_FLAGS:
8702      if (entry->d_un.d_val == 0)
8703	printf (_("NONE"));
8704      else
8705	{
8706	  static const char * opts[] =
8707	  {
8708	    "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
8709	    "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
8710	    "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
8711	    "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
8712	    "RLD_ORDER_SAFE"
8713	  };
8714	  unsigned int cnt;
8715	  int first = 1;
8716
8717	  for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
8718	    if (entry->d_un.d_val & (1 << cnt))
8719	      {
8720		printf ("%s%s", first ? "" : " ", opts[cnt]);
8721		first = 0;
8722	      }
8723	}
8724      break;
8725
8726    case DT_MIPS_IVERSION:
8727      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
8728	printf (_("Interface Version: %s"), GET_DYNAMIC_NAME (entry->d_un.d_val));
8729      else
8730	{
8731	  char buf[40];
8732	  sprintf_vma (buf, entry->d_un.d_ptr);
8733	  /* Note: coded this way so that there is a single string for translation.  */
8734	  printf (_("<corrupt: %s>"), buf);
8735	}
8736      break;
8737
8738    case DT_MIPS_TIME_STAMP:
8739      {
8740	char timebuf[128];
8741	struct tm * tmp;
8742	time_t atime = entry->d_un.d_val;
8743
8744	tmp = gmtime (&atime);
8745	/* PR 17531: file: 6accc532.  */
8746	if (tmp == NULL)
8747	  snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
8748	else
8749	  snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
8750		    tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
8751		    tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
8752	printf (_("Time Stamp: %s"), timebuf);
8753      }
8754      break;
8755
8756    case DT_MIPS_RLD_VERSION:
8757    case DT_MIPS_LOCAL_GOTNO:
8758    case DT_MIPS_CONFLICTNO:
8759    case DT_MIPS_LIBLISTNO:
8760    case DT_MIPS_SYMTABNO:
8761    case DT_MIPS_UNREFEXTNO:
8762    case DT_MIPS_HIPAGENO:
8763    case DT_MIPS_DELTA_CLASS_NO:
8764    case DT_MIPS_DELTA_INSTANCE_NO:
8765    case DT_MIPS_DELTA_RELOC_NO:
8766    case DT_MIPS_DELTA_SYM_NO:
8767    case DT_MIPS_DELTA_CLASSSYM_NO:
8768    case DT_MIPS_COMPACT_SIZE:
8769      print_vma (entry->d_un.d_val, DEC);
8770      break;
8771
8772    default:
8773      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8774    }
8775    putchar ('\n');
8776}
8777
8778static void
8779dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
8780{
8781  switch (entry->d_tag)
8782    {
8783    case DT_HP_DLD_FLAGS:
8784      {
8785	static struct
8786	{
8787	  long int bit;
8788	  const char * str;
8789	}
8790	flags[] =
8791	{
8792	  { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
8793	  { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
8794	  { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
8795	  { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
8796	  { DT_HP_BIND_NOW, "HP_BIND_NOW" },
8797	  { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
8798	  { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
8799	  { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
8800	  { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
8801	  { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
8802	  { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
8803	  { DT_HP_GST, "HP_GST" },
8804	  { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
8805	  { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
8806	  { DT_HP_NODELETE, "HP_NODELETE" },
8807	  { DT_HP_GROUP, "HP_GROUP" },
8808	  { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
8809	};
8810	int first = 1;
8811	size_t cnt;
8812	bfd_vma val = entry->d_un.d_val;
8813
8814	for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
8815	  if (val & flags[cnt].bit)
8816	    {
8817	      if (! first)
8818		putchar (' ');
8819	      fputs (flags[cnt].str, stdout);
8820	      first = 0;
8821	      val ^= flags[cnt].bit;
8822	    }
8823
8824	if (val != 0 || first)
8825	  {
8826	    if (! first)
8827	      putchar (' ');
8828	    print_vma (val, HEX);
8829	  }
8830      }
8831      break;
8832
8833    default:
8834      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8835      break;
8836    }
8837  putchar ('\n');
8838}
8839
8840#ifdef BFD64
8841
8842/* VMS vs Unix time offset and factor.  */
8843
8844#define VMS_EPOCH_OFFSET 35067168000000000LL
8845#define VMS_GRANULARITY_FACTOR 10000000
8846
8847/* Display a VMS time in a human readable format.  */
8848
8849static void
8850print_vms_time (bfd_int64_t vmstime)
8851{
8852  struct tm *tm;
8853  time_t unxtime;
8854
8855  unxtime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
8856  tm = gmtime (&unxtime);
8857  printf ("%04u-%02u-%02uT%02u:%02u:%02u",
8858          tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
8859          tm->tm_hour, tm->tm_min, tm->tm_sec);
8860}
8861#endif /* BFD64 */
8862
8863static void
8864dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
8865{
8866  switch (entry->d_tag)
8867    {
8868    case DT_IA_64_PLT_RESERVE:
8869      /* First 3 slots reserved.  */
8870      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8871      printf (" -- ");
8872      print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
8873      break;
8874
8875    case DT_IA_64_VMS_LINKTIME:
8876#ifdef BFD64
8877      print_vms_time (entry->d_un.d_val);
8878#endif
8879      break;
8880
8881    case DT_IA_64_VMS_LNKFLAGS:
8882      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8883      if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
8884        printf (" CALL_DEBUG");
8885      if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
8886        printf (" NOP0BUFS");
8887      if (entry->d_un.d_val & VMS_LF_P0IMAGE)
8888        printf (" P0IMAGE");
8889      if (entry->d_un.d_val & VMS_LF_MKTHREADS)
8890        printf (" MKTHREADS");
8891      if (entry->d_un.d_val & VMS_LF_UPCALLS)
8892        printf (" UPCALLS");
8893      if (entry->d_un.d_val & VMS_LF_IMGSTA)
8894        printf (" IMGSTA");
8895      if (entry->d_un.d_val & VMS_LF_INITIALIZE)
8896        printf (" INITIALIZE");
8897      if (entry->d_un.d_val & VMS_LF_MAIN)
8898        printf (" MAIN");
8899      if (entry->d_un.d_val & VMS_LF_EXE_INIT)
8900        printf (" EXE_INIT");
8901      if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
8902        printf (" TBK_IN_IMG");
8903      if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
8904        printf (" DBG_IN_IMG");
8905      if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
8906        printf (" TBK_IN_DSF");
8907      if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
8908        printf (" DBG_IN_DSF");
8909      if (entry->d_un.d_val & VMS_LF_SIGNATURES)
8910        printf (" SIGNATURES");
8911      if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
8912        printf (" REL_SEG_OFF");
8913      break;
8914
8915    default:
8916      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8917      break;
8918    }
8919  putchar ('\n');
8920}
8921
8922static int
8923get_32bit_dynamic_section (FILE * file)
8924{
8925  Elf32_External_Dyn * edyn;
8926  Elf32_External_Dyn * ext;
8927  Elf_Internal_Dyn * entry;
8928
8929  edyn = (Elf32_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
8930                                          dynamic_size, _("dynamic section"));
8931  if (!edyn)
8932    return 0;
8933
8934  /* SGI's ELF has more than one section in the DYNAMIC segment, and we
8935     might not have the luxury of section headers.  Look for the DT_NULL
8936     terminator to determine the number of entries.  */
8937  for (ext = edyn, dynamic_nent = 0;
8938       (char *) (ext + 1) <= (char *) edyn + dynamic_size;
8939       ext++)
8940    {
8941      dynamic_nent++;
8942      if (BYTE_GET (ext->d_tag) == DT_NULL)
8943	break;
8944    }
8945
8946  dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
8947                                                  sizeof (* entry));
8948  if (dynamic_section == NULL)
8949    {
8950      error (_("Out of memory allocating space for %lu dynamic entries\n"),
8951	     (unsigned long) dynamic_nent);
8952      free (edyn);
8953      return 0;
8954    }
8955
8956  for (ext = edyn, entry = dynamic_section;
8957       entry < dynamic_section + dynamic_nent;
8958       ext++, entry++)
8959    {
8960      entry->d_tag      = BYTE_GET (ext->d_tag);
8961      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
8962    }
8963
8964  free (edyn);
8965
8966  return 1;
8967}
8968
8969static int
8970get_64bit_dynamic_section (FILE * file)
8971{
8972  Elf64_External_Dyn * edyn;
8973  Elf64_External_Dyn * ext;
8974  Elf_Internal_Dyn * entry;
8975
8976  /* Read in the data.  */
8977  edyn = (Elf64_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
8978                                          dynamic_size, _("dynamic section"));
8979  if (!edyn)
8980    return 0;
8981
8982  /* SGI's ELF has more than one section in the DYNAMIC segment, and we
8983     might not have the luxury of section headers.  Look for the DT_NULL
8984     terminator to determine the number of entries.  */
8985  for (ext = edyn, dynamic_nent = 0;
8986       /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer.  */
8987       (char *) (ext + 1) <= (char *) edyn + dynamic_size;
8988       ext++)
8989    {
8990      dynamic_nent++;
8991      if (BYTE_GET (ext->d_tag) == DT_NULL)
8992	break;
8993    }
8994
8995  dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
8996                                                  sizeof (* entry));
8997  if (dynamic_section == NULL)
8998    {
8999      error (_("Out of memory allocating space for %lu dynamic entries\n"),
9000	     (unsigned long) dynamic_nent);
9001      free (edyn);
9002      return 0;
9003    }
9004
9005  /* Convert from external to internal formats.  */
9006  for (ext = edyn, entry = dynamic_section;
9007       entry < dynamic_section + dynamic_nent;
9008       ext++, entry++)
9009    {
9010      entry->d_tag      = BYTE_GET (ext->d_tag);
9011      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
9012    }
9013
9014  free (edyn);
9015
9016  return 1;
9017}
9018
9019static void
9020print_dynamic_flags (bfd_vma flags)
9021{
9022  int first = 1;
9023
9024  while (flags)
9025    {
9026      bfd_vma flag;
9027
9028      flag = flags & - flags;
9029      flags &= ~ flag;
9030
9031      if (first)
9032	first = 0;
9033      else
9034	putc (' ', stdout);
9035
9036      switch (flag)
9037	{
9038	case DF_ORIGIN:		fputs ("ORIGIN", stdout); break;
9039	case DF_SYMBOLIC:	fputs ("SYMBOLIC", stdout); break;
9040	case DF_TEXTREL:	fputs ("TEXTREL", stdout); break;
9041	case DF_BIND_NOW:	fputs ("BIND_NOW", stdout); break;
9042	case DF_STATIC_TLS:	fputs ("STATIC_TLS", stdout); break;
9043	default:		fputs (_("unknown"), stdout); break;
9044	}
9045    }
9046  puts ("");
9047}
9048
9049/* Parse and display the contents of the dynamic section.  */
9050
9051static int
9052process_dynamic_section (FILE * file)
9053{
9054  Elf_Internal_Dyn * entry;
9055
9056  if (dynamic_size == 0)
9057    {
9058      if (do_dynamic)
9059	printf (_("\nThere is no dynamic section in this file.\n"));
9060
9061      return 1;
9062    }
9063
9064  if (is_32bit_elf)
9065    {
9066      if (! get_32bit_dynamic_section (file))
9067	return 0;
9068    }
9069  else if (! get_64bit_dynamic_section (file))
9070    return 0;
9071
9072  /* Find the appropriate symbol table.  */
9073  if (dynamic_symbols == NULL)
9074    {
9075      for (entry = dynamic_section;
9076	   entry < dynamic_section + dynamic_nent;
9077	   ++entry)
9078	{
9079	  Elf_Internal_Shdr section;
9080
9081	  if (entry->d_tag != DT_SYMTAB)
9082	    continue;
9083
9084	  dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
9085
9086	  /* Since we do not know how big the symbol table is,
9087	     we default to reading in the entire file (!) and
9088	     processing that.  This is overkill, I know, but it
9089	     should work.  */
9090	  section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
9091
9092	  if (archive_file_offset != 0)
9093	    section.sh_size = archive_file_size - section.sh_offset;
9094	  else
9095	    {
9096	      if (fseek (file, 0, SEEK_END))
9097		error (_("Unable to seek to end of file!\n"));
9098
9099	      section.sh_size = ftell (file) - section.sh_offset;
9100	    }
9101
9102	  if (is_32bit_elf)
9103	    section.sh_entsize = sizeof (Elf32_External_Sym);
9104	  else
9105	    section.sh_entsize = sizeof (Elf64_External_Sym);
9106	  section.sh_name = string_table_length;
9107
9108	  dynamic_symbols = GET_ELF_SYMBOLS (file, &section, & num_dynamic_syms);
9109	  if (num_dynamic_syms < 1)
9110	    {
9111	      error (_("Unable to determine the number of symbols to load\n"));
9112	      continue;
9113	    }
9114	}
9115    }
9116
9117  /* Similarly find a string table.  */
9118  if (dynamic_strings == NULL)
9119    {
9120      for (entry = dynamic_section;
9121	   entry < dynamic_section + dynamic_nent;
9122	   ++entry)
9123	{
9124	  unsigned long offset;
9125	  long str_tab_len;
9126
9127	  if (entry->d_tag != DT_STRTAB)
9128	    continue;
9129
9130	  dynamic_info[DT_STRTAB] = entry->d_un.d_val;
9131
9132	  /* Since we do not know how big the string table is,
9133	     we default to reading in the entire file (!) and
9134	     processing that.  This is overkill, I know, but it
9135	     should work.  */
9136
9137	  offset = offset_from_vma (file, entry->d_un.d_val, 0);
9138
9139	  if (archive_file_offset != 0)
9140	    str_tab_len = archive_file_size - offset;
9141	  else
9142	    {
9143	      if (fseek (file, 0, SEEK_END))
9144		error (_("Unable to seek to end of file\n"));
9145	      str_tab_len = ftell (file) - offset;
9146	    }
9147
9148	  if (str_tab_len < 1)
9149	    {
9150	      error
9151		(_("Unable to determine the length of the dynamic string table\n"));
9152	      continue;
9153	    }
9154
9155	  dynamic_strings = (char *) get_data (NULL, file, offset, 1,
9156                                               str_tab_len,
9157                                               _("dynamic string table"));
9158	  dynamic_strings_length = dynamic_strings == NULL ? 0 : str_tab_len;
9159	  break;
9160	}
9161    }
9162
9163  /* And find the syminfo section if available.  */
9164  if (dynamic_syminfo == NULL)
9165    {
9166      unsigned long syminsz = 0;
9167
9168      for (entry = dynamic_section;
9169	   entry < dynamic_section + dynamic_nent;
9170	   ++entry)
9171	{
9172	  if (entry->d_tag == DT_SYMINENT)
9173	    {
9174	      /* Note: these braces are necessary to avoid a syntax
9175		 error from the SunOS4 C compiler.  */
9176	      /* PR binutils/17531: A corrupt file can trigger this test.
9177		 So do not use an assert, instead generate an error message.  */
9178	      if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
9179		error (_("Bad value (%d) for SYMINENT entry\n"),
9180		       (int) entry->d_un.d_val);
9181	    }
9182	  else if (entry->d_tag == DT_SYMINSZ)
9183	    syminsz = entry->d_un.d_val;
9184	  else if (entry->d_tag == DT_SYMINFO)
9185	    dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
9186						      syminsz);
9187	}
9188
9189      if (dynamic_syminfo_offset != 0 && syminsz != 0)
9190	{
9191	  Elf_External_Syminfo * extsyminfo;
9192	  Elf_External_Syminfo * extsym;
9193	  Elf_Internal_Syminfo * syminfo;
9194
9195	  /* There is a syminfo section.  Read the data.  */
9196	  extsyminfo = (Elf_External_Syminfo *)
9197              get_data (NULL, file, dynamic_syminfo_offset, 1, syminsz,
9198                        _("symbol information"));
9199	  if (!extsyminfo)
9200	    return 0;
9201
9202	  dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
9203	  if (dynamic_syminfo == NULL)
9204	    {
9205	      error (_("Out of memory allocating %lu byte for dynamic symbol info\n"),
9206		     (unsigned long) syminsz);
9207	      return 0;
9208	    }
9209
9210	  dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
9211	  for (syminfo = dynamic_syminfo, extsym = extsyminfo;
9212	       syminfo < dynamic_syminfo + dynamic_syminfo_nent;
9213	       ++syminfo, ++extsym)
9214	    {
9215	      syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
9216	      syminfo->si_flags = BYTE_GET (extsym->si_flags);
9217	    }
9218
9219	  free (extsyminfo);
9220	}
9221    }
9222
9223  if (do_dynamic && dynamic_addr)
9224    printf (_("\nDynamic section at offset 0x%lx contains %lu entries:\n"),
9225	    dynamic_addr, (unsigned long) dynamic_nent);
9226  if (do_dynamic)
9227    printf (_("  Tag        Type                         Name/Value\n"));
9228
9229  for (entry = dynamic_section;
9230       entry < dynamic_section + dynamic_nent;
9231       entry++)
9232    {
9233      if (do_dynamic)
9234	{
9235	  const char * dtype;
9236
9237	  putchar (' ');
9238	  print_vma (entry->d_tag, FULL_HEX);
9239	  dtype = get_dynamic_type (entry->d_tag);
9240	  printf (" (%s)%*s", dtype,
9241		  ((is_32bit_elf ? 27 : 19)
9242		   - (int) strlen (dtype)),
9243		  " ");
9244	}
9245
9246      switch (entry->d_tag)
9247	{
9248	case DT_FLAGS:
9249	  if (do_dynamic)
9250	    print_dynamic_flags (entry->d_un.d_val);
9251	  break;
9252
9253	case DT_AUXILIARY:
9254	case DT_FILTER:
9255	case DT_CONFIG:
9256	case DT_DEPAUDIT:
9257	case DT_AUDIT:
9258	  if (do_dynamic)
9259	    {
9260	      switch (entry->d_tag)
9261		{
9262		case DT_AUXILIARY:
9263		  printf (_("Auxiliary library"));
9264		  break;
9265
9266		case DT_FILTER:
9267		  printf (_("Filter library"));
9268		  break;
9269
9270		case DT_CONFIG:
9271		  printf (_("Configuration file"));
9272		  break;
9273
9274		case DT_DEPAUDIT:
9275		  printf (_("Dependency audit library"));
9276		  break;
9277
9278		case DT_AUDIT:
9279		  printf (_("Audit library"));
9280		  break;
9281		}
9282
9283	      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
9284		printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
9285	      else
9286		{
9287		  printf (": ");
9288		  print_vma (entry->d_un.d_val, PREFIX_HEX);
9289		  putchar ('\n');
9290		}
9291	    }
9292	  break;
9293
9294	case DT_FEATURE:
9295	  if (do_dynamic)
9296	    {
9297	      printf (_("Flags:"));
9298
9299	      if (entry->d_un.d_val == 0)
9300		printf (_(" None\n"));
9301	      else
9302		{
9303		  unsigned long int val = entry->d_un.d_val;
9304
9305		  if (val & DTF_1_PARINIT)
9306		    {
9307		      printf (" PARINIT");
9308		      val ^= DTF_1_PARINIT;
9309		    }
9310		  if (val & DTF_1_CONFEXP)
9311		    {
9312		      printf (" CONFEXP");
9313		      val ^= DTF_1_CONFEXP;
9314		    }
9315		  if (val != 0)
9316		    printf (" %lx", val);
9317		  puts ("");
9318		}
9319	    }
9320	  break;
9321
9322	case DT_POSFLAG_1:
9323	  if (do_dynamic)
9324	    {
9325	      printf (_("Flags:"));
9326
9327	      if (entry->d_un.d_val == 0)
9328		printf (_(" None\n"));
9329	      else
9330		{
9331		  unsigned long int val = entry->d_un.d_val;
9332
9333		  if (val & DF_P1_LAZYLOAD)
9334		    {
9335		      printf (" LAZYLOAD");
9336		      val ^= DF_P1_LAZYLOAD;
9337		    }
9338		  if (val & DF_P1_GROUPPERM)
9339		    {
9340		      printf (" GROUPPERM");
9341		      val ^= DF_P1_GROUPPERM;
9342		    }
9343		  if (val != 0)
9344		    printf (" %lx", val);
9345		  puts ("");
9346		}
9347	    }
9348	  break;
9349
9350	case DT_FLAGS_1:
9351	  if (do_dynamic)
9352	    {
9353	      printf (_("Flags:"));
9354	      if (entry->d_un.d_val == 0)
9355		printf (_(" None\n"));
9356	      else
9357		{
9358		  unsigned long int val = entry->d_un.d_val;
9359
9360		  if (val & DF_1_NOW)
9361		    {
9362		      printf (" NOW");
9363		      val ^= DF_1_NOW;
9364		    }
9365		  if (val & DF_1_GLOBAL)
9366		    {
9367		      printf (" GLOBAL");
9368		      val ^= DF_1_GLOBAL;
9369		    }
9370		  if (val & DF_1_GROUP)
9371		    {
9372		      printf (" GROUP");
9373		      val ^= DF_1_GROUP;
9374		    }
9375		  if (val & DF_1_NODELETE)
9376		    {
9377		      printf (" NODELETE");
9378		      val ^= DF_1_NODELETE;
9379		    }
9380		  if (val & DF_1_LOADFLTR)
9381		    {
9382		      printf (" LOADFLTR");
9383		      val ^= DF_1_LOADFLTR;
9384		    }
9385		  if (val & DF_1_INITFIRST)
9386		    {
9387		      printf (" INITFIRST");
9388		      val ^= DF_1_INITFIRST;
9389		    }
9390		  if (val & DF_1_NOOPEN)
9391		    {
9392		      printf (" NOOPEN");
9393		      val ^= DF_1_NOOPEN;
9394		    }
9395		  if (val & DF_1_ORIGIN)
9396		    {
9397		      printf (" ORIGIN");
9398		      val ^= DF_1_ORIGIN;
9399		    }
9400		  if (val & DF_1_DIRECT)
9401		    {
9402		      printf (" DIRECT");
9403		      val ^= DF_1_DIRECT;
9404		    }
9405		  if (val & DF_1_TRANS)
9406		    {
9407		      printf (" TRANS");
9408		      val ^= DF_1_TRANS;
9409		    }
9410		  if (val & DF_1_INTERPOSE)
9411		    {
9412		      printf (" INTERPOSE");
9413		      val ^= DF_1_INTERPOSE;
9414		    }
9415		  if (val & DF_1_NODEFLIB)
9416		    {
9417		      printf (" NODEFLIB");
9418		      val ^= DF_1_NODEFLIB;
9419		    }
9420		  if (val & DF_1_NODUMP)
9421		    {
9422		      printf (" NODUMP");
9423		      val ^= DF_1_NODUMP;
9424		    }
9425		  if (val & DF_1_CONFALT)
9426		    {
9427		      printf (" CONFALT");
9428		      val ^= DF_1_CONFALT;
9429		    }
9430		  if (val & DF_1_ENDFILTEE)
9431		    {
9432		      printf (" ENDFILTEE");
9433		      val ^= DF_1_ENDFILTEE;
9434		    }
9435		  if (val & DF_1_DISPRELDNE)
9436		    {
9437		      printf (" DISPRELDNE");
9438		      val ^= DF_1_DISPRELDNE;
9439		    }
9440		  if (val & DF_1_DISPRELPND)
9441		    {
9442		      printf (" DISPRELPND");
9443		      val ^= DF_1_DISPRELPND;
9444		    }
9445		  if (val & DF_1_NODIRECT)
9446		    {
9447		      printf (" NODIRECT");
9448		      val ^= DF_1_NODIRECT;
9449		    }
9450		  if (val & DF_1_IGNMULDEF)
9451		    {
9452		      printf (" IGNMULDEF");
9453		      val ^= DF_1_IGNMULDEF;
9454		    }
9455		  if (val & DF_1_NOKSYMS)
9456		    {
9457		      printf (" NOKSYMS");
9458		      val ^= DF_1_NOKSYMS;
9459		    }
9460		  if (val & DF_1_NOHDR)
9461		    {
9462		      printf (" NOHDR");
9463		      val ^= DF_1_NOHDR;
9464		    }
9465		  if (val & DF_1_EDITED)
9466		    {
9467		      printf (" EDITED");
9468		      val ^= DF_1_EDITED;
9469		    }
9470		  if (val & DF_1_NORELOC)
9471		    {
9472		      printf (" NORELOC");
9473		      val ^= DF_1_NORELOC;
9474		    }
9475		  if (val & DF_1_SYMINTPOSE)
9476		    {
9477		      printf (" SYMINTPOSE");
9478		      val ^= DF_1_SYMINTPOSE;
9479		    }
9480		  if (val & DF_1_GLOBAUDIT)
9481		    {
9482		      printf (" GLOBAUDIT");
9483		      val ^= DF_1_GLOBAUDIT;
9484		    }
9485		  if (val & DF_1_SINGLETON)
9486		    {
9487		      printf (" SINGLETON");
9488		      val ^= DF_1_SINGLETON;
9489		    }
9490		  if (val & DF_1_STUB)
9491		    {
9492		      printf (" STUB");
9493		      val ^= DF_1_STUB;
9494		    }
9495		  if (val & DF_1_PIE)
9496		    {
9497		      printf (" PIE");
9498		      val ^= DF_1_PIE;
9499		    }
9500		  if (val != 0)
9501		    printf (" %lx", val);
9502		  puts ("");
9503		}
9504	    }
9505	  break;
9506
9507	case DT_PLTREL:
9508	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
9509	  if (do_dynamic)
9510	    puts (get_dynamic_type (entry->d_un.d_val));
9511	  break;
9512
9513	case DT_NULL	:
9514	case DT_NEEDED	:
9515	case DT_PLTGOT	:
9516	case DT_HASH	:
9517	case DT_STRTAB	:
9518	case DT_SYMTAB	:
9519	case DT_RELA	:
9520	case DT_INIT	:
9521	case DT_FINI	:
9522	case DT_SONAME	:
9523	case DT_RPATH	:
9524	case DT_SYMBOLIC:
9525	case DT_REL	:
9526	case DT_DEBUG	:
9527	case DT_TEXTREL	:
9528	case DT_JMPREL	:
9529	case DT_RUNPATH	:
9530	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
9531
9532	  if (do_dynamic)
9533	    {
9534	      char * name;
9535
9536	      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
9537		name = GET_DYNAMIC_NAME (entry->d_un.d_val);
9538	      else
9539		name = NULL;
9540
9541	      if (name)
9542		{
9543		  switch (entry->d_tag)
9544		    {
9545		    case DT_NEEDED:
9546		      printf (_("Shared library: [%s]"), name);
9547
9548		      if (streq (name, program_interpreter))
9549			printf (_(" program interpreter"));
9550		      break;
9551
9552		    case DT_SONAME:
9553		      printf (_("Library soname: [%s]"), name);
9554		      break;
9555
9556		    case DT_RPATH:
9557		      printf (_("Library rpath: [%s]"), name);
9558		      break;
9559
9560		    case DT_RUNPATH:
9561		      printf (_("Library runpath: [%s]"), name);
9562		      break;
9563
9564		    default:
9565		      print_vma (entry->d_un.d_val, PREFIX_HEX);
9566		      break;
9567		    }
9568		}
9569	      else
9570		print_vma (entry->d_un.d_val, PREFIX_HEX);
9571
9572	      putchar ('\n');
9573	    }
9574	  break;
9575
9576	case DT_PLTRELSZ:
9577	case DT_RELASZ	:
9578	case DT_STRSZ	:
9579	case DT_RELSZ	:
9580	case DT_RELAENT	:
9581	case DT_SYMENT	:
9582	case DT_RELENT	:
9583	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
9584	case DT_PLTPADSZ:
9585	case DT_MOVEENT	:
9586	case DT_MOVESZ	:
9587	case DT_INIT_ARRAYSZ:
9588	case DT_FINI_ARRAYSZ:
9589	case DT_GNU_CONFLICTSZ:
9590	case DT_GNU_LIBLISTSZ:
9591	  if (do_dynamic)
9592	    {
9593	      print_vma (entry->d_un.d_val, UNSIGNED);
9594	      printf (_(" (bytes)\n"));
9595	    }
9596	  break;
9597
9598	case DT_VERDEFNUM:
9599	case DT_VERNEEDNUM:
9600	case DT_RELACOUNT:
9601	case DT_RELCOUNT:
9602	  if (do_dynamic)
9603	    {
9604	      print_vma (entry->d_un.d_val, UNSIGNED);
9605	      putchar ('\n');
9606	    }
9607	  break;
9608
9609	case DT_SYMINSZ:
9610	case DT_SYMINENT:
9611	case DT_SYMINFO:
9612	case DT_USED:
9613	case DT_INIT_ARRAY:
9614	case DT_FINI_ARRAY:
9615	  if (do_dynamic)
9616	    {
9617	      if (entry->d_tag == DT_USED
9618		  && VALID_DYNAMIC_NAME (entry->d_un.d_val))
9619		{
9620		  char * name = GET_DYNAMIC_NAME (entry->d_un.d_val);
9621
9622		  if (*name)
9623		    {
9624		      printf (_("Not needed object: [%s]\n"), name);
9625		      break;
9626		    }
9627		}
9628
9629	      print_vma (entry->d_un.d_val, PREFIX_HEX);
9630	      putchar ('\n');
9631	    }
9632	  break;
9633
9634	case DT_BIND_NOW:
9635	  /* The value of this entry is ignored.  */
9636	  if (do_dynamic)
9637	    putchar ('\n');
9638	  break;
9639
9640	case DT_GNU_PRELINKED:
9641	  if (do_dynamic)
9642	    {
9643	      struct tm * tmp;
9644	      time_t atime = entry->d_un.d_val;
9645
9646	      tmp = gmtime (&atime);
9647	      /* PR 17533 file: 041-1244816-0.004.  */
9648	      if (tmp == NULL)
9649		printf (_("<corrupt time val: %lx"),
9650			(unsigned long) atime);
9651	      else
9652		printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
9653			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9654			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9655
9656	    }
9657	  break;
9658
9659	case DT_GNU_HASH:
9660	  dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
9661	  if (do_dynamic)
9662	    {
9663	      print_vma (entry->d_un.d_val, PREFIX_HEX);
9664	      putchar ('\n');
9665	    }
9666	  break;
9667
9668	default:
9669	  if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
9670	    version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
9671	      entry->d_un.d_val;
9672
9673	  if (do_dynamic)
9674	    {
9675	      switch (elf_header.e_machine)
9676		{
9677		case EM_MIPS:
9678		case EM_MIPS_RS3_LE:
9679		  dynamic_section_mips_val (entry);
9680		  break;
9681		case EM_PARISC:
9682		  dynamic_section_parisc_val (entry);
9683		  break;
9684		case EM_IA_64:
9685		  dynamic_section_ia64_val (entry);
9686		  break;
9687		default:
9688		  print_vma (entry->d_un.d_val, PREFIX_HEX);
9689		  putchar ('\n');
9690		}
9691	    }
9692	  break;
9693	}
9694    }
9695
9696  return 1;
9697}
9698
9699static char *
9700get_ver_flags (unsigned int flags)
9701{
9702  static char buff[32];
9703
9704  buff[0] = 0;
9705
9706  if (flags == 0)
9707    return _("none");
9708
9709  if (flags & VER_FLG_BASE)
9710    strcat (buff, "BASE ");
9711
9712  if (flags & VER_FLG_WEAK)
9713    {
9714      if (flags & VER_FLG_BASE)
9715	strcat (buff, "| ");
9716
9717      strcat (buff, "WEAK ");
9718    }
9719
9720  if (flags & VER_FLG_INFO)
9721    {
9722      if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
9723	strcat (buff, "| ");
9724
9725      strcat (buff, "INFO ");
9726    }
9727
9728  if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
9729    strcat (buff, _("| <unknown>"));
9730
9731  return buff;
9732}
9733
9734/* Display the contents of the version sections.  */
9735
9736static int
9737process_version_sections (FILE * file)
9738{
9739  Elf_Internal_Shdr * section;
9740  unsigned i;
9741  int found = 0;
9742
9743  if (! do_version)
9744    return 1;
9745
9746  for (i = 0, section = section_headers;
9747       i < elf_header.e_shnum;
9748       i++, section++)
9749    {
9750      switch (section->sh_type)
9751	{
9752	case SHT_GNU_verdef:
9753	  {
9754	    Elf_External_Verdef * edefs;
9755	    unsigned int idx;
9756	    unsigned int cnt;
9757	    char * endbuf;
9758
9759	    found = 1;
9760
9761	    printf (_("\nVersion definition section '%s' contains %u entries:\n"),
9762		    printable_section_name (section),
9763		    section->sh_info);
9764
9765	    printf (_("  Addr: 0x"));
9766	    printf_vma (section->sh_addr);
9767	    printf (_("  Offset: %#08lx  Link: %u (%s)"),
9768		    (unsigned long) section->sh_offset, section->sh_link,
9769		    printable_section_name_from_index (section->sh_link));
9770
9771	    edefs = (Elf_External_Verdef *)
9772                get_data (NULL, file, section->sh_offset, 1,section->sh_size,
9773                          _("version definition section"));
9774	    if (!edefs)
9775	      break;
9776	    endbuf = (char *) edefs + section->sh_size;
9777
9778	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
9779	      {
9780		char * vstart;
9781		Elf_External_Verdef * edef;
9782		Elf_Internal_Verdef ent;
9783		Elf_External_Verdaux * eaux;
9784		Elf_Internal_Verdaux aux;
9785		int j;
9786		int isum;
9787
9788		/* Check for very large indicies.  */
9789		if (idx > (size_t) (endbuf - (char *) edefs))
9790		  break;
9791
9792		vstart = ((char *) edefs) + idx;
9793		if (vstart + sizeof (*edef) > endbuf)
9794		  break;
9795
9796		edef = (Elf_External_Verdef *) vstart;
9797
9798		ent.vd_version = BYTE_GET (edef->vd_version);
9799		ent.vd_flags   = BYTE_GET (edef->vd_flags);
9800		ent.vd_ndx     = BYTE_GET (edef->vd_ndx);
9801		ent.vd_cnt     = BYTE_GET (edef->vd_cnt);
9802		ent.vd_hash    = BYTE_GET (edef->vd_hash);
9803		ent.vd_aux     = BYTE_GET (edef->vd_aux);
9804		ent.vd_next    = BYTE_GET (edef->vd_next);
9805
9806		printf (_("  %#06x: Rev: %d  Flags: %s"),
9807			idx, ent.vd_version, get_ver_flags (ent.vd_flags));
9808
9809		printf (_("  Index: %d  Cnt: %d  "),
9810			ent.vd_ndx, ent.vd_cnt);
9811
9812		/* Check for overflow.  */
9813		if (ent.vd_aux > (size_t) (endbuf - vstart))
9814		  break;
9815
9816		vstart += ent.vd_aux;
9817
9818		eaux = (Elf_External_Verdaux *) vstart;
9819
9820		aux.vda_name = BYTE_GET (eaux->vda_name);
9821		aux.vda_next = BYTE_GET (eaux->vda_next);
9822
9823		if (VALID_DYNAMIC_NAME (aux.vda_name))
9824		  printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
9825		else
9826		  printf (_("Name index: %ld\n"), aux.vda_name);
9827
9828		isum = idx + ent.vd_aux;
9829
9830		for (j = 1; j < ent.vd_cnt; j++)
9831		  {
9832		    /* Check for overflow.  */
9833		    if (aux.vda_next > (size_t) (endbuf - vstart))
9834		      break;
9835
9836		    isum   += aux.vda_next;
9837		    vstart += aux.vda_next;
9838
9839		    eaux = (Elf_External_Verdaux *) vstart;
9840		    if (vstart + sizeof (*eaux) > endbuf)
9841		      break;
9842
9843		    aux.vda_name = BYTE_GET (eaux->vda_name);
9844		    aux.vda_next = BYTE_GET (eaux->vda_next);
9845
9846		    if (VALID_DYNAMIC_NAME (aux.vda_name))
9847		      printf (_("  %#06x: Parent %d: %s\n"),
9848			      isum, j, GET_DYNAMIC_NAME (aux.vda_name));
9849		    else
9850		      printf (_("  %#06x: Parent %d, name index: %ld\n"),
9851			      isum, j, aux.vda_name);
9852		  }
9853
9854		if (j < ent.vd_cnt)
9855		  printf (_("  Version def aux past end of section\n"));
9856
9857		/* PR 17531: file: id:000001,src:000172+005151,op:splice,rep:2.  */
9858		if (idx + ent.vd_next <= idx)
9859		  break;
9860
9861		idx += ent.vd_next;
9862	      }
9863
9864	    if (cnt < section->sh_info)
9865	      printf (_("  Version definition past end of section\n"));
9866
9867	    free (edefs);
9868	  }
9869	  break;
9870
9871	case SHT_GNU_verneed:
9872	  {
9873	    Elf_External_Verneed * eneed;
9874	    unsigned int idx;
9875	    unsigned int cnt;
9876	    char * endbuf;
9877
9878	    found = 1;
9879
9880	    printf (_("\nVersion needs section '%s' contains %u entries:\n"),
9881		    printable_section_name (section), section->sh_info);
9882
9883	    printf (_(" Addr: 0x"));
9884	    printf_vma (section->sh_addr);
9885	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
9886		    (unsigned long) section->sh_offset, section->sh_link,
9887		    printable_section_name_from_index (section->sh_link));
9888
9889	    eneed = (Elf_External_Verneed *) get_data (NULL, file,
9890                                                       section->sh_offset, 1,
9891                                                       section->sh_size,
9892                                                       _("Version Needs section"));
9893	    if (!eneed)
9894	      break;
9895	    endbuf = (char *) eneed + section->sh_size;
9896
9897	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
9898	      {
9899		Elf_External_Verneed * entry;
9900		Elf_Internal_Verneed ent;
9901		int j;
9902		int isum;
9903		char * vstart;
9904
9905		if (idx > (size_t) (endbuf - (char *) eneed))
9906		  break;
9907
9908		vstart = ((char *) eneed) + idx;
9909		if (vstart + sizeof (*entry) > endbuf)
9910		  break;
9911
9912		entry = (Elf_External_Verneed *) vstart;
9913
9914		ent.vn_version = BYTE_GET (entry->vn_version);
9915		ent.vn_cnt     = BYTE_GET (entry->vn_cnt);
9916		ent.vn_file    = BYTE_GET (entry->vn_file);
9917		ent.vn_aux     = BYTE_GET (entry->vn_aux);
9918		ent.vn_next    = BYTE_GET (entry->vn_next);
9919
9920		printf (_("  %#06x: Version: %d"), idx, ent.vn_version);
9921
9922		if (VALID_DYNAMIC_NAME (ent.vn_file))
9923		  printf (_("  File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
9924		else
9925		  printf (_("  File: %lx"), ent.vn_file);
9926
9927		printf (_("  Cnt: %d\n"), ent.vn_cnt);
9928
9929		/* Check for overflow.  */
9930		if (ent.vn_aux > (size_t) (endbuf - vstart))
9931		  break;
9932		vstart += ent.vn_aux;
9933
9934		for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
9935		  {
9936		    Elf_External_Vernaux * eaux;
9937		    Elf_Internal_Vernaux aux;
9938
9939		    if (vstart + sizeof (*eaux) > endbuf)
9940		      break;
9941		    eaux = (Elf_External_Vernaux *) vstart;
9942
9943		    aux.vna_hash  = BYTE_GET (eaux->vna_hash);
9944		    aux.vna_flags = BYTE_GET (eaux->vna_flags);
9945		    aux.vna_other = BYTE_GET (eaux->vna_other);
9946		    aux.vna_name  = BYTE_GET (eaux->vna_name);
9947		    aux.vna_next  = BYTE_GET (eaux->vna_next);
9948
9949		    if (VALID_DYNAMIC_NAME (aux.vna_name))
9950		      printf (_("  %#06x:   Name: %s"),
9951			      isum, GET_DYNAMIC_NAME (aux.vna_name));
9952		    else
9953		      printf (_("  %#06x:   Name index: %lx"),
9954			      isum, aux.vna_name);
9955
9956		    printf (_("  Flags: %s  Version: %d\n"),
9957			    get_ver_flags (aux.vna_flags), aux.vna_other);
9958
9959		    /* Check for overflow.  */
9960		    if (aux.vna_next > (size_t) (endbuf - vstart)
9961			|| (aux.vna_next == 0 && j < ent.vn_cnt - 1))
9962		      {
9963			warn (_("Invalid vna_next field of %lx\n"),
9964			      aux.vna_next);
9965			j = ent.vn_cnt;
9966			break;
9967		      }
9968		    isum   += aux.vna_next;
9969		    vstart += aux.vna_next;
9970		  }
9971
9972		if (j < ent.vn_cnt)
9973		  warn (_("Missing Version Needs auxillary information\n"));
9974
9975		if (ent.vn_next == 0 && cnt < section->sh_info - 1)
9976		  {
9977		    warn (_("Corrupt Version Needs structure - offset to next structure is zero with entries still left to be processed\n"));
9978		    cnt = section->sh_info;
9979		    break;
9980		  }
9981		idx += ent.vn_next;
9982	      }
9983
9984	    if (cnt < section->sh_info)
9985	      warn (_("Missing Version Needs information\n"));
9986
9987	    free (eneed);
9988	  }
9989	  break;
9990
9991	case SHT_GNU_versym:
9992	  {
9993	    Elf_Internal_Shdr * link_section;
9994	    size_t total;
9995	    unsigned int cnt;
9996	    unsigned char * edata;
9997	    unsigned short * data;
9998	    char * strtab;
9999	    Elf_Internal_Sym * symbols;
10000	    Elf_Internal_Shdr * string_sec;
10001	    unsigned long num_syms;
10002	    long off;
10003
10004	    if (section->sh_link >= elf_header.e_shnum)
10005	      break;
10006
10007	    link_section = section_headers + section->sh_link;
10008	    total = section->sh_size / sizeof (Elf_External_Versym);
10009
10010	    if (link_section->sh_link >= elf_header.e_shnum)
10011	      break;
10012
10013	    found = 1;
10014
10015	    symbols = GET_ELF_SYMBOLS (file, link_section, & num_syms);
10016	    if (symbols == NULL)
10017	      break;
10018
10019	    string_sec = section_headers + link_section->sh_link;
10020
10021	    strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
10022                                        string_sec->sh_size,
10023                                        _("version string table"));
10024	    if (!strtab)
10025	      {
10026		free (symbols);
10027		break;
10028	      }
10029
10030	    printf (_("\nVersion symbols section '%s' contains %lu entries:\n"),
10031		    printable_section_name (section), (unsigned long) total);
10032
10033	    printf (_(" Addr: "));
10034	    printf_vma (section->sh_addr);
10035	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
10036		    (unsigned long) section->sh_offset, section->sh_link,
10037		    printable_section_name (link_section));
10038
10039	    off = offset_from_vma (file,
10040				   version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
10041				   total * sizeof (short));
10042	    edata = (unsigned char *) get_data (NULL, file, off, total,
10043                                                sizeof (short),
10044                                                _("version symbol data"));
10045	    if (!edata)
10046	      {
10047		free (strtab);
10048		free (symbols);
10049		break;
10050	      }
10051
10052	    data = (short unsigned int *) cmalloc (total, sizeof (short));
10053
10054	    for (cnt = total; cnt --;)
10055	      data[cnt] = byte_get (edata + cnt * sizeof (short),
10056				    sizeof (short));
10057
10058	    free (edata);
10059
10060	    for (cnt = 0; cnt < total; cnt += 4)
10061	      {
10062		int j, nn;
10063		char *name;
10064		char *invalid = _("*invalid*");
10065
10066		printf ("  %03x:", cnt);
10067
10068		for (j = 0; (j < 4) && (cnt + j) < total; ++j)
10069		  switch (data[cnt + j])
10070		    {
10071		    case 0:
10072		      fputs (_("   0 (*local*)    "), stdout);
10073		      break;
10074
10075		    case 1:
10076		      fputs (_("   1 (*global*)   "), stdout);
10077		      break;
10078
10079		    default:
10080		      nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
10081				   data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
10082
10083		      /* If this index value is greater than the size of the symbols
10084		         array, break to avoid an out-of-bounds read.  */
10085		      if ((unsigned long)(cnt + j) >= num_syms)
10086		        {
10087		          warn (_("invalid index into symbol array\n"));
10088		          break;
10089			}
10090
10091		      name = NULL;
10092		      if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
10093			{
10094			  Elf_Internal_Verneed ivn;
10095			  unsigned long offset;
10096
10097			  offset = offset_from_vma
10098			    (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
10099			     sizeof (Elf_External_Verneed));
10100
10101			  do
10102			    {
10103			      Elf_Internal_Vernaux ivna;
10104			      Elf_External_Verneed evn;
10105			      Elf_External_Vernaux evna;
10106			      unsigned long a_off;
10107
10108			      if (get_data (&evn, file, offset, sizeof (evn), 1,
10109					    _("version need")) == NULL)
10110				break;
10111
10112			      ivn.vn_aux  = BYTE_GET (evn.vn_aux);
10113			      ivn.vn_next = BYTE_GET (evn.vn_next);
10114
10115			      a_off = offset + ivn.vn_aux;
10116
10117			      do
10118				{
10119				  if (get_data (&evna, file, a_off, sizeof (evna),
10120						1, _("version need aux (2)")) == NULL)
10121				    {
10122				      ivna.vna_next  = 0;
10123				      ivna.vna_other = 0;
10124				    }
10125				  else
10126				    {
10127				      ivna.vna_next  = BYTE_GET (evna.vna_next);
10128				      ivna.vna_other = BYTE_GET (evna.vna_other);
10129				    }
10130
10131				  a_off += ivna.vna_next;
10132				}
10133			      while (ivna.vna_other != data[cnt + j]
10134				     && ivna.vna_next != 0);
10135
10136			      if (ivna.vna_other == data[cnt + j])
10137				{
10138				  ivna.vna_name = BYTE_GET (evna.vna_name);
10139
10140				  if (ivna.vna_name >= string_sec->sh_size)
10141				    name = invalid;
10142				  else
10143				    name = strtab + ivna.vna_name;
10144				  break;
10145				}
10146
10147			      offset += ivn.vn_next;
10148			    }
10149			  while (ivn.vn_next);
10150			}
10151
10152		      if (data[cnt + j] != 0x8001
10153			  && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
10154			{
10155			  Elf_Internal_Verdef ivd;
10156			  Elf_External_Verdef evd;
10157			  unsigned long offset;
10158
10159			  offset = offset_from_vma
10160			    (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
10161			     sizeof evd);
10162
10163			  do
10164			    {
10165			      if (get_data (&evd, file, offset, sizeof (evd), 1,
10166					    _("version def")) == NULL)
10167				{
10168				  ivd.vd_next = 0;
10169				  /* PR 17531: file: 046-1082287-0.004.  */
10170				  ivd.vd_ndx  = (data[cnt + j] & VERSYM_VERSION) + 1;
10171				  break;
10172				}
10173			      else
10174				{
10175				  ivd.vd_next = BYTE_GET (evd.vd_next);
10176				  ivd.vd_ndx  = BYTE_GET (evd.vd_ndx);
10177				}
10178
10179			      offset += ivd.vd_next;
10180			    }
10181			  while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
10182				 && ivd.vd_next != 0);
10183
10184			  if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
10185			    {
10186			      Elf_External_Verdaux evda;
10187			      Elf_Internal_Verdaux ivda;
10188
10189			      ivd.vd_aux = BYTE_GET (evd.vd_aux);
10190
10191			      if (get_data (&evda, file,
10192					    offset - ivd.vd_next + ivd.vd_aux,
10193					    sizeof (evda), 1,
10194					    _("version def aux")) == NULL)
10195				break;
10196
10197			      ivda.vda_name = BYTE_GET (evda.vda_name);
10198
10199			      if (ivda.vda_name >= string_sec->sh_size)
10200				name = invalid;
10201			      else if (name != NULL && name != invalid)
10202				name = _("*both*");
10203			      else
10204				name = strtab + ivda.vda_name;
10205			    }
10206			}
10207		      if (name != NULL)
10208			nn += printf ("(%s%-*s",
10209				      name,
10210				      12 - (int) strlen (name),
10211				      ")");
10212
10213		      if (nn < 18)
10214			printf ("%*c", 18 - nn, ' ');
10215		    }
10216
10217		putchar ('\n');
10218	      }
10219
10220	    free (data);
10221	    free (strtab);
10222	    free (symbols);
10223	  }
10224	  break;
10225
10226	default:
10227	  break;
10228	}
10229    }
10230
10231  if (! found)
10232    printf (_("\nNo version information found in this file.\n"));
10233
10234  return 1;
10235}
10236
10237static const char *
10238get_symbol_binding (unsigned int binding)
10239{
10240  static char buff[32];
10241
10242  switch (binding)
10243    {
10244    case STB_LOCAL:	return "LOCAL";
10245    case STB_GLOBAL:	return "GLOBAL";
10246    case STB_WEAK:	return "WEAK";
10247    default:
10248      if (binding >= STB_LOPROC && binding <= STB_HIPROC)
10249	snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
10250		  binding);
10251      else if (binding >= STB_LOOS && binding <= STB_HIOS)
10252	{
10253	  if (binding == STB_GNU_UNIQUE
10254	      && (elf_header.e_ident[EI_OSABI] == ELFOSABI_GNU
10255		  /* GNU is still using the default value 0.  */
10256		  || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
10257	    return "UNIQUE";
10258	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
10259	}
10260      else
10261	snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
10262      return buff;
10263    }
10264}
10265
10266static const char *
10267get_symbol_type (unsigned int type)
10268{
10269  static char buff[32];
10270
10271  switch (type)
10272    {
10273    case STT_NOTYPE:	return "NOTYPE";
10274    case STT_OBJECT:	return "OBJECT";
10275    case STT_FUNC:	return "FUNC";
10276    case STT_SECTION:	return "SECTION";
10277    case STT_FILE:	return "FILE";
10278    case STT_COMMON:	return "COMMON";
10279    case STT_TLS:	return "TLS";
10280    case STT_RELC:      return "RELC";
10281    case STT_SRELC:     return "SRELC";
10282    default:
10283      if (type >= STT_LOPROC && type <= STT_HIPROC)
10284	{
10285	  if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
10286	    return "THUMB_FUNC";
10287
10288	  if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
10289	    return "REGISTER";
10290
10291	  if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
10292	    return "PARISC_MILLI";
10293
10294	  snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
10295	}
10296      else if (type >= STT_LOOS && type <= STT_HIOS)
10297	{
10298	  if (elf_header.e_machine == EM_PARISC)
10299	    {
10300	      if (type == STT_HP_OPAQUE)
10301		return "HP_OPAQUE";
10302	      if (type == STT_HP_STUB)
10303		return "HP_STUB";
10304	    }
10305
10306	  if (type == STT_GNU_IFUNC
10307	      && (elf_header.e_ident[EI_OSABI] == ELFOSABI_GNU
10308		  || elf_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD
10309		  /* GNU is still using the default value 0.  */
10310		  || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
10311	    return "IFUNC";
10312
10313	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
10314	}
10315      else
10316	snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
10317      return buff;
10318    }
10319}
10320
10321static const char *
10322get_symbol_visibility (unsigned int visibility)
10323{
10324  switch (visibility)
10325    {
10326    case STV_DEFAULT:	return "DEFAULT";
10327    case STV_INTERNAL:	return "INTERNAL";
10328    case STV_HIDDEN:	return "HIDDEN";
10329    case STV_PROTECTED: return "PROTECTED";
10330    default:
10331      error (_("Unrecognized visibility value: %u"), visibility);
10332      return _("<unknown>");
10333    }
10334}
10335
10336static const char *
10337get_alpha_symbol_other (unsigned int other)
10338{
10339  switch (other)
10340    {
10341    case STO_ALPHA_NOPV:
10342      return "NOPV";
10343    case STO_ALPHA_STD_GPLOAD:
10344      return "STD GPLOAD";
10345    default:
10346      return NULL;
10347    }
10348}
10349
10350static const char *
10351get_solaris_symbol_visibility (unsigned int visibility)
10352{
10353  switch (visibility)
10354    {
10355    case 4: return "EXPORTED";
10356    case 5: return "SINGLETON";
10357    case 6: return "ELIMINATE";
10358    default: return get_symbol_visibility (visibility);
10359    }
10360}
10361
10362static const char *
10363get_mips_symbol_other (unsigned int other)
10364{
10365  switch (other)
10366    {
10367    case STO_OPTIONAL:
10368      return "OPTIONAL";
10369    case STO_MIPS_PLT:
10370      return "MIPS PLT";
10371    case STO_MIPS_PIC:
10372      return "MIPS PIC";
10373    case STO_MICROMIPS:
10374      return "MICROMIPS";
10375    case STO_MICROMIPS | STO_MIPS_PIC:
10376      return "MICROMIPS, MIPS PIC";
10377    case STO_MIPS16:
10378      return "MIPS16";
10379    default:
10380      return NULL;
10381    }
10382}
10383
10384static const char *
10385get_ia64_symbol_other (unsigned int other)
10386{
10387  if (is_ia64_vms ())
10388    {
10389      static char res[32];
10390
10391      res[0] = 0;
10392
10393      /* Function types is for images and .STB files only.  */
10394      switch (elf_header.e_type)
10395        {
10396        case ET_DYN:
10397        case ET_EXEC:
10398          switch (VMS_ST_FUNC_TYPE (other))
10399            {
10400            case VMS_SFT_CODE_ADDR:
10401              strcat (res, " CA");
10402              break;
10403            case VMS_SFT_SYMV_IDX:
10404              strcat (res, " VEC");
10405              break;
10406            case VMS_SFT_FD:
10407              strcat (res, " FD");
10408              break;
10409            case VMS_SFT_RESERVE:
10410              strcat (res, " RSV");
10411              break;
10412            default:
10413	      warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
10414		    VMS_ST_FUNC_TYPE (other));
10415	      strcat (res, " <unknown>");
10416	      break;
10417            }
10418          break;
10419        default:
10420          break;
10421        }
10422      switch (VMS_ST_LINKAGE (other))
10423        {
10424        case VMS_STL_IGNORE:
10425          strcat (res, " IGN");
10426          break;
10427        case VMS_STL_RESERVE:
10428          strcat (res, " RSV");
10429          break;
10430        case VMS_STL_STD:
10431          strcat (res, " STD");
10432          break;
10433        case VMS_STL_LNK:
10434          strcat (res, " LNK");
10435          break;
10436        default:
10437	  warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
10438		VMS_ST_LINKAGE (other));
10439	  strcat (res, " <unknown>");
10440	  break;
10441        }
10442
10443      if (res[0] != 0)
10444        return res + 1;
10445      else
10446        return res;
10447    }
10448  return NULL;
10449}
10450
10451static const char *
10452get_ppc64_symbol_other (unsigned int other)
10453{
10454  if (PPC64_LOCAL_ENTRY_OFFSET (other) != 0)
10455    {
10456      static char buf[32];
10457      snprintf (buf, sizeof buf, _("<localentry>: %d"),
10458		PPC64_LOCAL_ENTRY_OFFSET (other));
10459      return buf;
10460    }
10461  return NULL;
10462}
10463
10464static const char *
10465get_symbol_other (unsigned int other)
10466{
10467  const char * result = NULL;
10468  static char buff [32];
10469
10470  if (other == 0)
10471    return "";
10472
10473  switch (elf_header.e_machine)
10474    {
10475    case EM_ALPHA:
10476      result = get_alpha_symbol_other (other);
10477      break;
10478    case EM_MIPS:
10479      result = get_mips_symbol_other (other);
10480      break;
10481    case EM_IA_64:
10482      result = get_ia64_symbol_other (other);
10483      break;
10484    case EM_PPC64:
10485      result = get_ppc64_symbol_other (other);
10486      break;
10487    default:
10488      result = NULL;
10489      break;
10490    }
10491
10492  if (result)
10493    return result;
10494
10495  snprintf (buff, sizeof buff, _("<other>: %x"), other);
10496  return buff;
10497}
10498
10499static const char *
10500get_symbol_index_type (unsigned int type)
10501{
10502  static char buff[32];
10503
10504  switch (type)
10505    {
10506    case SHN_UNDEF:	return "UND";
10507    case SHN_ABS:	return "ABS";
10508    case SHN_COMMON:	return "COM";
10509    default:
10510      if (type == SHN_IA_64_ANSI_COMMON
10511	  && elf_header.e_machine == EM_IA_64
10512	  && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
10513	return "ANSI_COM";
10514      else if ((elf_header.e_machine == EM_X86_64
10515		|| elf_header.e_machine == EM_L1OM
10516		|| elf_header.e_machine == EM_K1OM)
10517	       && type == SHN_X86_64_LCOMMON)
10518	return "LARGE_COM";
10519      else if ((type == SHN_MIPS_SCOMMON
10520		&& elf_header.e_machine == EM_MIPS)
10521	       || (type == SHN_TIC6X_SCOMMON
10522		   && elf_header.e_machine == EM_TI_C6000))
10523	return "SCOM";
10524      else if (type == SHN_MIPS_SUNDEFINED
10525	       && elf_header.e_machine == EM_MIPS)
10526	return "SUND";
10527      else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
10528	sprintf (buff, "PRC[0x%04x]", type & 0xffff);
10529      else if (type >= SHN_LOOS && type <= SHN_HIOS)
10530	sprintf (buff, "OS [0x%04x]", type & 0xffff);
10531      else if (type >= SHN_LORESERVE)
10532	sprintf (buff, "RSV[0x%04x]", type & 0xffff);
10533      else if (type >= elf_header.e_shnum)
10534	sprintf (buff, _("bad section index[%3d]"), type);
10535      else
10536	sprintf (buff, "%3d", type);
10537      break;
10538    }
10539
10540  return buff;
10541}
10542
10543static bfd_vma *
10544get_dynamic_data (FILE * file, bfd_size_type number, unsigned int ent_size)
10545{
10546  unsigned char * e_data;
10547  bfd_vma * i_data;
10548
10549  /* If the size_t type is smaller than the bfd_size_type, eg because
10550     you are building a 32-bit tool on a 64-bit host, then make sure
10551     that when (number) is cast to (size_t) no information is lost.  */
10552  if (sizeof (size_t) < sizeof (bfd_size_type)
10553      && (bfd_size_type) ((size_t) number) != number)
10554    {
10555      error (_("Size truncation prevents reading %" BFD_VMA_FMT "u"
10556	       " elements of size %u\n"),
10557	     number, ent_size);
10558      return NULL;
10559    }
10560
10561  /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
10562     attempting to allocate memory when the read is bound to fail.  */
10563  if (ent_size * number > current_file_size)
10564    {
10565      error (_("Invalid number of dynamic entries: %" BFD_VMA_FMT "u\n"),
10566	     number);
10567      return NULL;
10568    }
10569
10570  e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10571  if (e_data == NULL)
10572    {
10573      error (_("Out of memory reading %" BFD_VMA_FMT "u dynamic entries\n"),
10574	     number);
10575      return NULL;
10576    }
10577
10578  if (fread (e_data, ent_size, (size_t) number, file) != number)
10579    {
10580      error (_("Unable to read in %" BFD_VMA_FMT "u bytes of dynamic data\n"),
10581	     number * ent_size);
10582      free (e_data);
10583      return NULL;
10584    }
10585
10586  i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10587  if (i_data == NULL)
10588    {
10589      error (_("Out of memory allocating space for %" BFD_VMA_FMT "u"
10590	       " dynamic entries\n"),
10591	     number);
10592      free (e_data);
10593      return NULL;
10594    }
10595
10596  while (number--)
10597    i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10598
10599  free (e_data);
10600
10601  return i_data;
10602}
10603
10604static void
10605print_dynamic_symbol (bfd_vma si, unsigned long hn)
10606{
10607  Elf_Internal_Sym * psym;
10608  int n;
10609
10610  n = print_vma (si, DEC_5);
10611  if (n < 5)
10612    fputs (&"     "[n], stdout);
10613  printf (" %3lu: ", hn);
10614
10615  if (dynamic_symbols == NULL || si >= num_dynamic_syms)
10616    {
10617      printf (_("<No info available for dynamic symbol number %lu>\n"),
10618	      (unsigned long) si);
10619      return;
10620    }
10621
10622  psym = dynamic_symbols + si;
10623  print_vma (psym->st_value, LONG_HEX);
10624  putchar (' ');
10625  print_vma (psym->st_size, DEC_5);
10626
10627  printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
10628  printf (" %-6s",  get_symbol_binding (ELF_ST_BIND (psym->st_info)));
10629
10630  if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
10631    printf (" %-7s",  get_solaris_symbol_visibility (psym->st_other));
10632  else
10633    {
10634      unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
10635
10636      printf (" %-7s",  get_symbol_visibility (vis));
10637      /* Check to see if any other bits in the st_other field are set.
10638	 Note - displaying this information disrupts the layout of the
10639	 table being generated, but for the moment this case is very
10640	 rare.  */
10641      if (psym->st_other ^ vis)
10642	printf (" [%s] ", get_symbol_other (psym->st_other ^ vis));
10643    }
10644
10645  printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
10646  if (VALID_DYNAMIC_NAME (psym->st_name))
10647    print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
10648  else
10649    printf (_(" <corrupt: %14ld>"), psym->st_name);
10650  putchar ('\n');
10651}
10652
10653static const char *
10654get_symbol_version_string (FILE *file, int is_dynsym,
10655			   const char *strtab,
10656			   unsigned long int strtab_size,
10657			   unsigned int si, Elf_Internal_Sym *psym,
10658			   enum versioned_symbol_info *sym_info,
10659			   unsigned short *vna_other)
10660{
10661  unsigned char data[2];
10662  unsigned short vers_data;
10663  unsigned long offset;
10664
10665  if (!is_dynsym
10666      || version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
10667    return NULL;
10668
10669  offset = offset_from_vma (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
10670			    sizeof data + si * sizeof (vers_data));
10671
10672  if (get_data (&data, file, offset + si * sizeof (vers_data),
10673		sizeof (data), 1, _("version data")) == NULL)
10674    return NULL;
10675
10676  vers_data = byte_get (data, 2);
10677
10678  if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data <= 1)
10679    return NULL;
10680
10681  /* Usually we'd only see verdef for defined symbols, and verneed for
10682     undefined symbols.  However, symbols defined by the linker in
10683     .dynbss for variables copied from a shared library in order to
10684     avoid text relocations are defined yet have verneed.  We could
10685     use a heuristic to detect the special case, for example, check
10686     for verneed first on symbols defined in SHT_NOBITS sections, but
10687     it is simpler and more reliable to just look for both verdef and
10688     verneed.  .dynbss might not be mapped to a SHT_NOBITS section.  */
10689
10690  if (psym->st_shndx != SHN_UNDEF
10691      && vers_data != 0x8001
10692      && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
10693    {
10694      Elf_Internal_Verdef ivd;
10695      Elf_Internal_Verdaux ivda;
10696      Elf_External_Verdaux evda;
10697      unsigned long off;
10698
10699      off = offset_from_vma (file,
10700			     version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
10701			     sizeof (Elf_External_Verdef));
10702
10703      do
10704	{
10705	  Elf_External_Verdef evd;
10706
10707	  if (get_data (&evd, file, off, sizeof (evd), 1,
10708			_("version def")) == NULL)
10709	    {
10710	      ivd.vd_ndx = 0;
10711	      ivd.vd_aux = 0;
10712	      ivd.vd_next = 0;
10713	    }
10714	  else
10715	    {
10716	      ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
10717	      ivd.vd_aux = BYTE_GET (evd.vd_aux);
10718	      ivd.vd_next = BYTE_GET (evd.vd_next);
10719	    }
10720
10721	  off += ivd.vd_next;
10722	}
10723      while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
10724
10725      if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
10726	{
10727	  off -= ivd.vd_next;
10728	  off += ivd.vd_aux;
10729
10730	  if (get_data (&evda, file, off, sizeof (evda), 1,
10731			_("version def aux")) != NULL)
10732	    {
10733	      ivda.vda_name = BYTE_GET (evda.vda_name);
10734
10735	      if (psym->st_name != ivda.vda_name)
10736		{
10737		  *sym_info = ((vers_data & VERSYM_HIDDEN) != 0
10738			       ? symbol_hidden : symbol_public);
10739		  return (ivda.vda_name < strtab_size
10740			  ? strtab + ivda.vda_name : _("<corrupt>"));
10741		}
10742	    }
10743	}
10744    }
10745
10746  if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
10747    {
10748      Elf_External_Verneed evn;
10749      Elf_Internal_Verneed ivn;
10750      Elf_Internal_Vernaux ivna;
10751
10752      offset = offset_from_vma (file,
10753				version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
10754				sizeof evn);
10755      do
10756	{
10757	  unsigned long vna_off;
10758
10759	  if (get_data (&evn, file, offset, sizeof (evn), 1,
10760			_("version need")) == NULL)
10761	    {
10762	      ivna.vna_next = 0;
10763	      ivna.vna_other = 0;
10764	      ivna.vna_name = 0;
10765	      break;
10766	    }
10767
10768	  ivn.vn_aux  = BYTE_GET (evn.vn_aux);
10769	  ivn.vn_next = BYTE_GET (evn.vn_next);
10770
10771	  vna_off = offset + ivn.vn_aux;
10772
10773	  do
10774	    {
10775	      Elf_External_Vernaux evna;
10776
10777	      if (get_data (&evna, file, vna_off, sizeof (evna), 1,
10778			    _("version need aux (3)")) == NULL)
10779		{
10780		  ivna.vna_next = 0;
10781		  ivna.vna_other = 0;
10782		  ivna.vna_name = 0;
10783		}
10784	      else
10785		{
10786		  ivna.vna_other = BYTE_GET (evna.vna_other);
10787		  ivna.vna_next  = BYTE_GET (evna.vna_next);
10788		  ivna.vna_name  = BYTE_GET (evna.vna_name);
10789		}
10790
10791	      vna_off += ivna.vna_next;
10792	    }
10793	  while (ivna.vna_other != vers_data && ivna.vna_next != 0);
10794
10795	  if (ivna.vna_other == vers_data)
10796	    break;
10797
10798	  offset += ivn.vn_next;
10799	}
10800      while (ivn.vn_next != 0);
10801
10802      if (ivna.vna_other == vers_data)
10803	{
10804	  *sym_info = symbol_undefined;
10805	  *vna_other = ivna.vna_other;
10806	  return (ivna.vna_name < strtab_size
10807		  ? strtab + ivna.vna_name : _("<corrupt>"));
10808	}
10809    }
10810  return NULL;
10811}
10812
10813/* Dump the symbol table.  */
10814static int
10815process_symbol_table (FILE * file)
10816{
10817  Elf_Internal_Shdr * section;
10818  bfd_size_type nbuckets = 0;
10819  bfd_size_type nchains = 0;
10820  bfd_vma * buckets = NULL;
10821  bfd_vma * chains = NULL;
10822  bfd_vma ngnubuckets = 0;
10823  bfd_vma * gnubuckets = NULL;
10824  bfd_vma * gnuchains = NULL;
10825  bfd_vma gnusymidx = 0;
10826  bfd_size_type ngnuchains = 0;
10827
10828  if (!do_syms && !do_dyn_syms && !do_histogram)
10829    return 1;
10830
10831  if (dynamic_info[DT_HASH]
10832      && (do_histogram
10833	  || (do_using_dynamic
10834	      && !do_dyn_syms
10835	      && dynamic_strings != NULL)))
10836    {
10837      unsigned char nb[8];
10838      unsigned char nc[8];
10839      unsigned int hash_ent_size = 4;
10840
10841      if ((elf_header.e_machine == EM_ALPHA
10842	   || elf_header.e_machine == EM_S390
10843	   || elf_header.e_machine == EM_S390_OLD)
10844	  && elf_header.e_ident[EI_CLASS] == ELFCLASS64)
10845	hash_ent_size = 8;
10846
10847      if (fseek (file,
10848		 (archive_file_offset
10849		  + offset_from_vma (file, dynamic_info[DT_HASH],
10850				     sizeof nb + sizeof nc)),
10851		 SEEK_SET))
10852	{
10853	  error (_("Unable to seek to start of dynamic information\n"));
10854	  goto no_hash;
10855	}
10856
10857      if (fread (nb, hash_ent_size, 1, file) != 1)
10858	{
10859	  error (_("Failed to read in number of buckets\n"));
10860	  goto no_hash;
10861	}
10862
10863      if (fread (nc, hash_ent_size, 1, file) != 1)
10864	{
10865	  error (_("Failed to read in number of chains\n"));
10866	  goto no_hash;
10867	}
10868
10869      nbuckets = byte_get (nb, hash_ent_size);
10870      nchains  = byte_get (nc, hash_ent_size);
10871
10872      buckets = get_dynamic_data (file, nbuckets, hash_ent_size);
10873      chains  = get_dynamic_data (file, nchains, hash_ent_size);
10874
10875    no_hash:
10876      if (buckets == NULL || chains == NULL)
10877	{
10878	  if (do_using_dynamic)
10879	    return 0;
10880	  free (buckets);
10881	  free (chains);
10882	  buckets = NULL;
10883	  chains = NULL;
10884	  nbuckets = 0;
10885	  nchains = 0;
10886	}
10887    }
10888
10889  if (dynamic_info_DT_GNU_HASH
10890      && (do_histogram
10891	  || (do_using_dynamic
10892	      && !do_dyn_syms
10893	      && dynamic_strings != NULL)))
10894    {
10895      unsigned char nb[16];
10896      bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10897      bfd_vma buckets_vma;
10898
10899      if (fseek (file,
10900		 (archive_file_offset
10901		  + offset_from_vma (file, dynamic_info_DT_GNU_HASH,
10902				     sizeof nb)),
10903		 SEEK_SET))
10904	{
10905	  error (_("Unable to seek to start of dynamic information\n"));
10906	  goto no_gnu_hash;
10907	}
10908
10909      if (fread (nb, 16, 1, file) != 1)
10910	{
10911	  error (_("Failed to read in number of buckets\n"));
10912	  goto no_gnu_hash;
10913	}
10914
10915      ngnubuckets = byte_get (nb, 4);
10916      gnusymidx = byte_get (nb + 4, 4);
10917      bitmaskwords = byte_get (nb + 8, 4);
10918      buckets_vma = dynamic_info_DT_GNU_HASH + 16;
10919      if (is_32bit_elf)
10920	buckets_vma += bitmaskwords * 4;
10921      else
10922	buckets_vma += bitmaskwords * 8;
10923
10924      if (fseek (file,
10925		 (archive_file_offset
10926		  + offset_from_vma (file, buckets_vma, 4)),
10927		 SEEK_SET))
10928	{
10929	  error (_("Unable to seek to start of dynamic information\n"));
10930	  goto no_gnu_hash;
10931	}
10932
10933      gnubuckets = get_dynamic_data (file, ngnubuckets, 4);
10934
10935      if (gnubuckets == NULL)
10936	goto no_gnu_hash;
10937
10938      for (i = 0; i < ngnubuckets; i++)
10939	if (gnubuckets[i] != 0)
10940	  {
10941	    if (gnubuckets[i] < gnusymidx)
10942	      return 0;
10943
10944	    if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
10945	      maxchain = gnubuckets[i];
10946	  }
10947
10948      if (maxchain == 0xffffffff)
10949	goto no_gnu_hash;
10950
10951      maxchain -= gnusymidx;
10952
10953      if (fseek (file,
10954		 (archive_file_offset
10955		  + offset_from_vma (file, buckets_vma
10956					   + 4 * (ngnubuckets + maxchain), 4)),
10957		 SEEK_SET))
10958	{
10959	  error (_("Unable to seek to start of dynamic information\n"));
10960	  goto no_gnu_hash;
10961	}
10962
10963      do
10964	{
10965	  if (fread (nb, 4, 1, file) != 1)
10966	    {
10967	      error (_("Failed to determine last chain length\n"));
10968	      goto no_gnu_hash;
10969	    }
10970
10971	  if (maxchain + 1 == 0)
10972	    goto no_gnu_hash;
10973
10974	  ++maxchain;
10975	}
10976      while ((byte_get (nb, 4) & 1) == 0);
10977
10978      if (fseek (file,
10979		 (archive_file_offset
10980		  + offset_from_vma (file, buckets_vma + 4 * ngnubuckets, 4)),
10981		 SEEK_SET))
10982	{
10983	  error (_("Unable to seek to start of dynamic information\n"));
10984	  goto no_gnu_hash;
10985	}
10986
10987      gnuchains = get_dynamic_data (file, maxchain, 4);
10988      ngnuchains = maxchain;
10989
10990    no_gnu_hash:
10991      if (gnuchains == NULL)
10992	{
10993	  free (gnubuckets);
10994	  gnubuckets = NULL;
10995	  ngnubuckets = 0;
10996	  if (do_using_dynamic)
10997	    return 0;
10998	}
10999    }
11000
11001  if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
11002      && do_syms
11003      && do_using_dynamic
11004      && dynamic_strings != NULL
11005      && dynamic_symbols != NULL)
11006    {
11007      unsigned long hn;
11008
11009      if (dynamic_info[DT_HASH])
11010	{
11011	  bfd_vma si;
11012
11013	  printf (_("\nSymbol table for image:\n"));
11014	  if (is_32bit_elf)
11015	    printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
11016	  else
11017	    printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
11018
11019	  for (hn = 0; hn < nbuckets; hn++)
11020	    {
11021	      if (! buckets[hn])
11022		continue;
11023
11024	      for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
11025		print_dynamic_symbol (si, hn);
11026	    }
11027	}
11028
11029      if (dynamic_info_DT_GNU_HASH)
11030	{
11031	  printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
11032	  if (is_32bit_elf)
11033	    printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
11034	  else
11035	    printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
11036
11037	  for (hn = 0; hn < ngnubuckets; ++hn)
11038	    if (gnubuckets[hn] != 0)
11039	      {
11040		bfd_vma si = gnubuckets[hn];
11041		bfd_vma off = si - gnusymidx;
11042
11043		do
11044		  {
11045		    print_dynamic_symbol (si, hn);
11046		    si++;
11047		  }
11048		while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
11049	      }
11050	}
11051    }
11052  else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
11053	   && section_headers != NULL)
11054    {
11055      unsigned int i;
11056
11057      for (i = 0, section = section_headers;
11058	   i < elf_header.e_shnum;
11059	   i++, section++)
11060	{
11061	  unsigned int si;
11062	  char * strtab = NULL;
11063	  unsigned long int strtab_size = 0;
11064	  Elf_Internal_Sym * symtab;
11065	  Elf_Internal_Sym * psym;
11066	  unsigned long num_syms;
11067
11068	  if ((section->sh_type != SHT_SYMTAB
11069	       && section->sh_type != SHT_DYNSYM)
11070	      || (!do_syms
11071		  && section->sh_type == SHT_SYMTAB))
11072	    continue;
11073
11074	  if (section->sh_entsize == 0)
11075	    {
11076	      printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
11077		      printable_section_name (section));
11078	      continue;
11079	    }
11080
11081	  printf (_("\nSymbol table '%s' contains %lu entries:\n"),
11082		  printable_section_name (section),
11083		  (unsigned long) (section->sh_size / section->sh_entsize));
11084
11085	  if (is_32bit_elf)
11086	    printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
11087	  else
11088	    printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
11089
11090	  symtab = GET_ELF_SYMBOLS (file, section, & num_syms);
11091	  if (symtab == NULL)
11092	    continue;
11093
11094	  if (section->sh_link == elf_header.e_shstrndx)
11095	    {
11096	      strtab = string_table;
11097	      strtab_size = string_table_length;
11098	    }
11099	  else if (section->sh_link < elf_header.e_shnum)
11100	    {
11101	      Elf_Internal_Shdr * string_sec;
11102
11103	      string_sec = section_headers + section->sh_link;
11104
11105	      strtab = (char *) get_data (NULL, file, string_sec->sh_offset,
11106                                          1, string_sec->sh_size,
11107                                          _("string table"));
11108	      strtab_size = strtab != NULL ? string_sec->sh_size : 0;
11109	    }
11110
11111	  for (si = 0, psym = symtab; si < num_syms; si++, psym++)
11112	    {
11113	      const char *version_string;
11114	      enum versioned_symbol_info sym_info;
11115	      unsigned short vna_other;
11116
11117	      printf ("%6d: ", si);
11118	      print_vma (psym->st_value, LONG_HEX);
11119	      putchar (' ');
11120	      print_vma (psym->st_size, DEC_5);
11121	      printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
11122	      printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
11123	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
11124		printf (" %-7s",  get_solaris_symbol_visibility (psym->st_other));
11125	      else
11126		{
11127		  unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
11128
11129		  printf (" %-7s", get_symbol_visibility (vis));
11130		  /* Check to see if any other bits in the st_other field are set.
11131		     Note - displaying this information disrupts the layout of the
11132		     table being generated, but for the moment this case is very rare.  */
11133		  if (psym->st_other ^ vis)
11134		    printf (" [%s] ", get_symbol_other (psym->st_other ^ vis));
11135		}
11136	      printf (" %4s ", get_symbol_index_type (psym->st_shndx));
11137	      print_symbol (25, psym->st_name < strtab_size
11138			    ? strtab + psym->st_name : _("<corrupt>"));
11139
11140	      version_string
11141		= get_symbol_version_string (file,
11142					     section->sh_type == SHT_DYNSYM,
11143					     strtab, strtab_size, si,
11144					     psym, &sym_info, &vna_other);
11145	      if (version_string)
11146		{
11147		  if (sym_info == symbol_undefined)
11148		    printf ("@%s (%d)", version_string, vna_other);
11149		  else
11150		    printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
11151			    version_string);
11152		}
11153
11154	      putchar ('\n');
11155	    }
11156
11157	  free (symtab);
11158	  if (strtab != string_table)
11159	    free (strtab);
11160	}
11161    }
11162  else if (do_syms)
11163    printf
11164      (_("\nDynamic symbol information is not available for displaying symbols.\n"));
11165
11166  if (do_histogram && buckets != NULL)
11167    {
11168      unsigned long * lengths;
11169      unsigned long * counts;
11170      unsigned long hn;
11171      bfd_vma si;
11172      unsigned long maxlength = 0;
11173      unsigned long nzero_counts = 0;
11174      unsigned long nsyms = 0;
11175      unsigned long chained;
11176
11177      printf (_("\nHistogram for bucket list length (total of %lu buckets):\n"),
11178	      (unsigned long) nbuckets);
11179
11180      lengths = (unsigned long *) calloc (nbuckets, sizeof (*lengths));
11181      if (lengths == NULL)
11182	{
11183	  error (_("Out of memory allocating space for histogram buckets\n"));
11184	  return 0;
11185	}
11186
11187      printf (_(" Length  Number     %% of total  Coverage\n"));
11188      for (hn = 0; hn < nbuckets; ++hn)
11189	{
11190	  for (si = buckets[hn], chained = 0;
11191	       si > 0 && si < nchains && si < nbuckets && chained <= nchains;
11192	       si = chains[si], ++chained)
11193	    {
11194	      ++nsyms;
11195	      if (maxlength < ++lengths[hn])
11196		++maxlength;
11197	    }
11198
11199	    /* PR binutils/17531: A corrupt binary could contain broken
11200	       histogram data.  Do not go into an infinite loop trying
11201	       to process it.  */
11202	    if (chained > nchains)
11203	      {
11204		error (_("histogram chain is corrupt\n"));
11205		break;
11206	      }
11207	}
11208
11209      counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
11210      if (counts == NULL)
11211	{
11212	  free (lengths);
11213	  error (_("Out of memory allocating space for histogram counts\n"));
11214	  return 0;
11215	}
11216
11217      for (hn = 0; hn < nbuckets; ++hn)
11218	++counts[lengths[hn]];
11219
11220      if (nbuckets > 0)
11221	{
11222	  unsigned long i;
11223	  printf ("      0  %-10lu (%5.1f%%)\n",
11224		  counts[0], (counts[0] * 100.0) / nbuckets);
11225	  for (i = 1; i <= maxlength; ++i)
11226	    {
11227	      nzero_counts += counts[i] * i;
11228	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
11229		      i, counts[i], (counts[i] * 100.0) / nbuckets,
11230		      (nzero_counts * 100.0) / nsyms);
11231	    }
11232	}
11233
11234      free (counts);
11235      free (lengths);
11236    }
11237
11238  if (buckets != NULL)
11239    {
11240      free (buckets);
11241      free (chains);
11242    }
11243
11244  if (do_histogram && gnubuckets != NULL)
11245    {
11246      unsigned long * lengths;
11247      unsigned long * counts;
11248      unsigned long hn;
11249      unsigned long maxlength = 0;
11250      unsigned long nzero_counts = 0;
11251      unsigned long nsyms = 0;
11252
11253      printf (_("\nHistogram for `.gnu.hash' bucket list length (total of %lu buckets):\n"),
11254	      (unsigned long) ngnubuckets);
11255
11256      lengths = (unsigned long *) calloc (ngnubuckets, sizeof (*lengths));
11257      if (lengths == NULL)
11258	{
11259	  error (_("Out of memory allocating space for gnu histogram buckets\n"));
11260	  return 0;
11261	}
11262
11263      printf (_(" Length  Number     %% of total  Coverage\n"));
11264
11265      for (hn = 0; hn < ngnubuckets; ++hn)
11266	if (gnubuckets[hn] != 0)
11267	  {
11268	    bfd_vma off, length = 1;
11269
11270	    for (off = gnubuckets[hn] - gnusymidx;
11271		 /* PR 17531 file: 010-77222-0.004.  */
11272		 off < ngnuchains && (gnuchains[off] & 1) == 0;
11273		 ++off)
11274	      ++length;
11275	    lengths[hn] = length;
11276	    if (length > maxlength)
11277	      maxlength = length;
11278	    nsyms += length;
11279	  }
11280
11281      counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
11282      if (counts == NULL)
11283	{
11284	  free (lengths);
11285	  error (_("Out of memory allocating space for gnu histogram counts\n"));
11286	  return 0;
11287	}
11288
11289      for (hn = 0; hn < ngnubuckets; ++hn)
11290	++counts[lengths[hn]];
11291
11292      if (ngnubuckets > 0)
11293	{
11294	  unsigned long j;
11295	  printf ("      0  %-10lu (%5.1f%%)\n",
11296		  counts[0], (counts[0] * 100.0) / ngnubuckets);
11297	  for (j = 1; j <= maxlength; ++j)
11298	    {
11299	      nzero_counts += counts[j] * j;
11300	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
11301		      j, counts[j], (counts[j] * 100.0) / ngnubuckets,
11302		      (nzero_counts * 100.0) / nsyms);
11303	    }
11304	}
11305
11306      free (counts);
11307      free (lengths);
11308      free (gnubuckets);
11309      free (gnuchains);
11310    }
11311
11312  return 1;
11313}
11314
11315static int
11316process_syminfo (FILE * file ATTRIBUTE_UNUSED)
11317{
11318  unsigned int i;
11319
11320  if (dynamic_syminfo == NULL
11321      || !do_dynamic)
11322    /* No syminfo, this is ok.  */
11323    return 1;
11324
11325  /* There better should be a dynamic symbol section.  */
11326  if (dynamic_symbols == NULL || dynamic_strings == NULL)
11327    return 0;
11328
11329  if (dynamic_addr)
11330    printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
11331	    dynamic_syminfo_offset, dynamic_syminfo_nent);
11332
11333  printf (_(" Num: Name                           BoundTo     Flags\n"));
11334  for (i = 0; i < dynamic_syminfo_nent; ++i)
11335    {
11336      unsigned short int flags = dynamic_syminfo[i].si_flags;
11337
11338      printf ("%4d: ", i);
11339      if (i >= num_dynamic_syms)
11340	printf (_("<corrupt index>"));
11341      else if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
11342	print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
11343      else
11344	printf (_("<corrupt: %19ld>"), dynamic_symbols[i].st_name);
11345      putchar (' ');
11346
11347      switch (dynamic_syminfo[i].si_boundto)
11348	{
11349	case SYMINFO_BT_SELF:
11350	  fputs ("SELF       ", stdout);
11351	  break;
11352	case SYMINFO_BT_PARENT:
11353	  fputs ("PARENT     ", stdout);
11354	  break;
11355	default:
11356	  if (dynamic_syminfo[i].si_boundto > 0
11357	      && dynamic_syminfo[i].si_boundto < dynamic_nent
11358	      && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
11359	    {
11360	      print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
11361	      putchar (' ' );
11362	    }
11363	  else
11364	    printf ("%-10d ", dynamic_syminfo[i].si_boundto);
11365	  break;
11366	}
11367
11368      if (flags & SYMINFO_FLG_DIRECT)
11369	printf (" DIRECT");
11370      if (flags & SYMINFO_FLG_PASSTHRU)
11371	printf (" PASSTHRU");
11372      if (flags & SYMINFO_FLG_COPY)
11373	printf (" COPY");
11374      if (flags & SYMINFO_FLG_LAZYLOAD)
11375	printf (" LAZYLOAD");
11376
11377      puts ("");
11378    }
11379
11380  return 1;
11381}
11382
11383/* Check to see if the given reloc needs to be handled in a target specific
11384   manner.  If so then process the reloc and return TRUE otherwise return
11385   FALSE.  */
11386
11387static bfd_boolean
11388target_specific_reloc_handling (Elf_Internal_Rela * reloc,
11389				unsigned char *     start,
11390				Elf_Internal_Sym *  symtab)
11391{
11392  unsigned int reloc_type = get_reloc_type (reloc->r_info);
11393
11394  switch (elf_header.e_machine)
11395    {
11396    case EM_MSP430:
11397    case EM_MSP430_OLD:
11398      {
11399	static Elf_Internal_Sym * saved_sym = NULL;
11400
11401	switch (reloc_type)
11402	  {
11403	  case 10: /* R_MSP430_SYM_DIFF */
11404	    if (uses_msp430x_relocs ())
11405	      break;
11406	  case 21: /* R_MSP430X_SYM_DIFF */
11407	    saved_sym = symtab + get_reloc_symindex (reloc->r_info);
11408	    return TRUE;
11409
11410	  case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
11411	  case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
11412	    goto handle_sym_diff;
11413
11414	  case 5: /* R_MSP430_16_BYTE */
11415	  case 9: /* R_MSP430_8 */
11416	    if (uses_msp430x_relocs ())
11417	      break;
11418	    goto handle_sym_diff;
11419
11420	  case 2: /* R_MSP430_ABS16 */
11421	  case 15: /* R_MSP430X_ABS16 */
11422	    if (! uses_msp430x_relocs ())
11423	      break;
11424	    goto handle_sym_diff;
11425
11426	  handle_sym_diff:
11427	    if (saved_sym != NULL)
11428	      {
11429		bfd_vma value;
11430
11431		value = reloc->r_addend
11432		  + (symtab[get_reloc_symindex (reloc->r_info)].st_value
11433		     - saved_sym->st_value);
11434
11435		byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
11436
11437		saved_sym = NULL;
11438		return TRUE;
11439	      }
11440	    break;
11441
11442	  default:
11443	    if (saved_sym != NULL)
11444	      error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
11445	    break;
11446	  }
11447	break;
11448      }
11449
11450    case EM_MN10300:
11451    case EM_CYGNUS_MN10300:
11452      {
11453	static Elf_Internal_Sym * saved_sym = NULL;
11454
11455	switch (reloc_type)
11456	  {
11457	  case 34: /* R_MN10300_ALIGN */
11458	    return TRUE;
11459	  case 33: /* R_MN10300_SYM_DIFF */
11460	    saved_sym = symtab + get_reloc_symindex (reloc->r_info);
11461	    return TRUE;
11462	  case 1: /* R_MN10300_32 */
11463	  case 2: /* R_MN10300_16 */
11464	    if (saved_sym != NULL)
11465	      {
11466		bfd_vma value;
11467
11468		value = reloc->r_addend
11469		  + (symtab[get_reloc_symindex (reloc->r_info)].st_value
11470		     - saved_sym->st_value);
11471
11472		byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
11473
11474		saved_sym = NULL;
11475		return TRUE;
11476	      }
11477	    break;
11478	  default:
11479	    if (saved_sym != NULL)
11480	      error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
11481	    break;
11482	  }
11483	break;
11484      }
11485
11486    case EM_RL78:
11487      {
11488	static bfd_vma saved_sym1 = 0;
11489	static bfd_vma saved_sym2 = 0;
11490	static bfd_vma value;
11491
11492	switch (reloc_type)
11493	  {
11494	  case 0x80: /* R_RL78_SYM.  */
11495	    saved_sym1 = saved_sym2;
11496	    saved_sym2 = symtab[get_reloc_symindex (reloc->r_info)].st_value;
11497	    saved_sym2 += reloc->r_addend;
11498	    return TRUE;
11499
11500	  case 0x83: /* R_RL78_OPsub.  */
11501	    value = saved_sym1 - saved_sym2;
11502	    saved_sym2 = saved_sym1 = 0;
11503	    return TRUE;
11504	    break;
11505
11506	  case 0x41: /* R_RL78_ABS32.  */
11507	    byte_put (start + reloc->r_offset, value, 4);
11508	    value = 0;
11509	    return TRUE;
11510
11511	  case 0x43: /* R_RL78_ABS16.  */
11512	    byte_put (start + reloc->r_offset, value, 2);
11513	    value = 0;
11514	    return TRUE;
11515
11516	  default:
11517	    break;
11518	  }
11519	break;
11520      }
11521    }
11522
11523  return FALSE;
11524}
11525
11526/* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
11527   DWARF debug sections.  This is a target specific test.  Note - we do not
11528   go through the whole including-target-headers-multiple-times route, (as
11529   we have already done with <elf/h8.h>) because this would become very
11530   messy and even then this function would have to contain target specific
11531   information (the names of the relocs instead of their numeric values).
11532   FIXME: This is not the correct way to solve this problem.  The proper way
11533   is to have target specific reloc sizing and typing functions created by
11534   the reloc-macros.h header, in the same way that it already creates the
11535   reloc naming functions.  */
11536
11537static bfd_boolean
11538is_32bit_abs_reloc (unsigned int reloc_type)
11539{
11540  /* Please keep this table alpha-sorted for ease of visual lookup.  */
11541  switch (elf_header.e_machine)
11542    {
11543    case EM_386:
11544    case EM_IAMCU:
11545      return reloc_type == 1; /* R_386_32.  */
11546    case EM_68K:
11547      return reloc_type == 1; /* R_68K_32.  */
11548    case EM_860:
11549      return reloc_type == 1; /* R_860_32.  */
11550    case EM_960:
11551      return reloc_type == 2; /* R_960_32.  */
11552    case EM_AARCH64:
11553      return reloc_type == 258; /* R_AARCH64_ABS32 */
11554    case EM_ADAPTEVA_EPIPHANY:
11555      return reloc_type == 3;
11556    case EM_ALPHA:
11557      return reloc_type == 1; /* R_ALPHA_REFLONG.  */
11558    case EM_ARC:
11559      return reloc_type == 1; /* R_ARC_32.  */
11560    case EM_ARC_COMPACT:
11561    case EM_ARC_COMPACT2:
11562      return reloc_type == 4; /* R_ARC_32.  */
11563    case EM_ARM:
11564      return reloc_type == 2; /* R_ARM_ABS32 */
11565    case EM_AVR_OLD:
11566    case EM_AVR:
11567      return reloc_type == 1;
11568    case EM_BLACKFIN:
11569      return reloc_type == 0x12; /* R_byte4_data.  */
11570    case EM_CRIS:
11571      return reloc_type == 3; /* R_CRIS_32.  */
11572    case EM_CR16:
11573      return reloc_type == 3; /* R_CR16_NUM32.  */
11574    case EM_CRX:
11575      return reloc_type == 15; /* R_CRX_NUM32.  */
11576    case EM_CYGNUS_FRV:
11577      return reloc_type == 1;
11578    case EM_CYGNUS_D10V:
11579    case EM_D10V:
11580      return reloc_type == 6; /* R_D10V_32.  */
11581    case EM_CYGNUS_D30V:
11582    case EM_D30V:
11583      return reloc_type == 12; /* R_D30V_32_NORMAL.  */
11584    case EM_DLX:
11585      return reloc_type == 3; /* R_DLX_RELOC_32.  */
11586    case EM_CYGNUS_FR30:
11587    case EM_FR30:
11588      return reloc_type == 3; /* R_FR30_32.  */
11589    case EM_FT32:
11590      return reloc_type == 1; /* R_FT32_32.  */
11591    case EM_H8S:
11592    case EM_H8_300:
11593    case EM_H8_300H:
11594      return reloc_type == 1; /* R_H8_DIR32.  */
11595    case EM_IA_64:
11596      return reloc_type == 0x65 /* R_IA64_SECREL32LSB.  */
11597	|| reloc_type == 0x25;  /* R_IA64_DIR32LSB.  */
11598    case EM_IP2K_OLD:
11599    case EM_IP2K:
11600      return reloc_type == 2; /* R_IP2K_32.  */
11601    case EM_IQ2000:
11602      return reloc_type == 2; /* R_IQ2000_32.  */
11603    case EM_LATTICEMICO32:
11604      return reloc_type == 3; /* R_LM32_32.  */
11605    case EM_M32C_OLD:
11606    case EM_M32C:
11607      return reloc_type == 3; /* R_M32C_32.  */
11608    case EM_M32R:
11609      return reloc_type == 34; /* R_M32R_32_RELA.  */
11610    case EM_68HC11:
11611    case EM_68HC12:
11612      return reloc_type == 6; /* R_M68HC11_32.  */
11613    case EM_MCORE:
11614      return reloc_type == 1; /* R_MCORE_ADDR32.  */
11615    case EM_CYGNUS_MEP:
11616      return reloc_type == 4; /* R_MEP_32.  */
11617    case EM_METAG:
11618      return reloc_type == 2; /* R_METAG_ADDR32.  */
11619    case EM_MICROBLAZE:
11620      return reloc_type == 1; /* R_MICROBLAZE_32.  */
11621    case EM_MIPS:
11622      return reloc_type == 2; /* R_MIPS_32.  */
11623    case EM_MMIX:
11624      return reloc_type == 4; /* R_MMIX_32.  */
11625    case EM_CYGNUS_MN10200:
11626    case EM_MN10200:
11627      return reloc_type == 1; /* R_MN10200_32.  */
11628    case EM_CYGNUS_MN10300:
11629    case EM_MN10300:
11630      return reloc_type == 1; /* R_MN10300_32.  */
11631    case EM_MOXIE:
11632      return reloc_type == 1; /* R_MOXIE_32.  */
11633    case EM_MSP430_OLD:
11634    case EM_MSP430:
11635      return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32.  */
11636    case EM_MT:
11637      return reloc_type == 2; /* R_MT_32.  */
11638    case EM_NDS32:
11639      return reloc_type == 20; /* R_NDS32_RELA.  */
11640    case EM_ALTERA_NIOS2:
11641      return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32.  */
11642    case EM_NIOS32:
11643      return reloc_type == 1; /* R_NIOS_32.  */
11644    case EM_OR1K:
11645      return reloc_type == 1; /* R_OR1K_32.  */
11646    case EM_PARISC:
11647      return (reloc_type == 1 /* R_PARISC_DIR32.  */
11648	      || reloc_type == 41); /* R_PARISC_SECREL32.  */
11649    case EM_PJ:
11650    case EM_PJ_OLD:
11651      return reloc_type == 1; /* R_PJ_DATA_DIR32.  */
11652    case EM_PPC64:
11653      return reloc_type == 1; /* R_PPC64_ADDR32.  */
11654    case EM_PPC:
11655      return reloc_type == 1; /* R_PPC_ADDR32.  */
11656    case EM_RISCV:
11657      return reloc_type == 1; /* R_RISCV_32.  */
11658    case EM_RL78:
11659      return reloc_type == 1; /* R_RL78_DIR32.  */
11660    case EM_RX:
11661      return reloc_type == 1; /* R_RX_DIR32.  */
11662    case EM_S370:
11663      return reloc_type == 1; /* R_I370_ADDR31.  */
11664    case EM_S390_OLD:
11665    case EM_S390:
11666      return reloc_type == 4; /* R_S390_32.  */
11667    case EM_SCORE:
11668      return reloc_type == 8; /* R_SCORE_ABS32.  */
11669    case EM_SH:
11670      return reloc_type == 1; /* R_SH_DIR32.  */
11671    case EM_SPARC32PLUS:
11672    case EM_SPARCV9:
11673    case EM_SPARC:
11674      return reloc_type == 3 /* R_SPARC_32.  */
11675	|| reloc_type == 23; /* R_SPARC_UA32.  */
11676    case EM_SPU:
11677      return reloc_type == 6; /* R_SPU_ADDR32 */
11678    case EM_TI_C6000:
11679      return reloc_type == 1; /* R_C6000_ABS32.  */
11680    case EM_TILEGX:
11681      return reloc_type == 2; /* R_TILEGX_32.  */
11682    case EM_TILEPRO:
11683      return reloc_type == 1; /* R_TILEPRO_32.  */
11684    case EM_CYGNUS_V850:
11685    case EM_V850:
11686      return reloc_type == 6; /* R_V850_ABS32.  */
11687    case EM_V800:
11688      return reloc_type == 0x33; /* R_V810_WORD.  */
11689    case EM_VAX:
11690      return reloc_type == 1; /* R_VAX_32.  */
11691    case EM_VISIUM:
11692      return reloc_type == 3;  /* R_VISIUM_32. */
11693    case EM_X86_64:
11694    case EM_L1OM:
11695    case EM_K1OM:
11696      return reloc_type == 10; /* R_X86_64_32.  */
11697    case EM_XC16X:
11698    case EM_C166:
11699      return reloc_type == 3; /* R_XC16C_ABS_32.  */
11700    case EM_XGATE:
11701      return reloc_type == 4; /* R_XGATE_32.  */
11702    case EM_XSTORMY16:
11703      return reloc_type == 1; /* R_XSTROMY16_32.  */
11704    case EM_XTENSA_OLD:
11705    case EM_XTENSA:
11706      return reloc_type == 1; /* R_XTENSA_32.  */
11707    default:
11708      {
11709	static unsigned int prev_warn = 0;
11710
11711	/* Avoid repeating the same warning multiple times.  */
11712	if (prev_warn != elf_header.e_machine)
11713	  error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
11714		 elf_header.e_machine);
11715	prev_warn = elf_header.e_machine;
11716	return FALSE;
11717      }
11718    }
11719}
11720
11721/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11722   a 32-bit pc-relative RELA relocation used in DWARF debug sections.  */
11723
11724static bfd_boolean
11725is_32bit_pcrel_reloc (unsigned int reloc_type)
11726{
11727  switch (elf_header.e_machine)
11728  /* Please keep this table alpha-sorted for ease of visual lookup.  */
11729    {
11730    case EM_386:
11731    case EM_IAMCU:
11732      return reloc_type == 2;  /* R_386_PC32.  */
11733    case EM_68K:
11734      return reloc_type == 4;  /* R_68K_PC32.  */
11735    case EM_AARCH64:
11736      return reloc_type == 261; /* R_AARCH64_PREL32 */
11737    case EM_ADAPTEVA_EPIPHANY:
11738      return reloc_type == 6;
11739    case EM_ALPHA:
11740      return reloc_type == 10; /* R_ALPHA_SREL32.  */
11741    case EM_ARC_COMPACT:
11742    case EM_ARC_COMPACT2:
11743      return reloc_type == 49; /* R_ARC_32_PCREL.  */
11744    case EM_ARM:
11745      return reloc_type == 3;  /* R_ARM_REL32 */
11746    case EM_AVR_OLD:
11747    case EM_AVR:
11748      return reloc_type == 36; /* R_AVR_32_PCREL.  */
11749    case EM_MICROBLAZE:
11750      return reloc_type == 2;  /* R_MICROBLAZE_32_PCREL.  */
11751    case EM_OR1K:
11752      return reloc_type == 9; /* R_OR1K_32_PCREL.  */
11753    case EM_PARISC:
11754      return reloc_type == 9;  /* R_PARISC_PCREL32.  */
11755    case EM_PPC:
11756      return reloc_type == 26; /* R_PPC_REL32.  */
11757    case EM_PPC64:
11758      return reloc_type == 26; /* R_PPC64_REL32.  */
11759    case EM_S390_OLD:
11760    case EM_S390:
11761      return reloc_type == 5;  /* R_390_PC32.  */
11762    case EM_SH:
11763      return reloc_type == 2;  /* R_SH_REL32.  */
11764    case EM_SPARC32PLUS:
11765    case EM_SPARCV9:
11766    case EM_SPARC:
11767      return reloc_type == 6;  /* R_SPARC_DISP32.  */
11768    case EM_SPU:
11769      return reloc_type == 13; /* R_SPU_REL32.  */
11770    case EM_TILEGX:
11771      return reloc_type == 6; /* R_TILEGX_32_PCREL.  */
11772    case EM_TILEPRO:
11773      return reloc_type == 4; /* R_TILEPRO_32_PCREL.  */
11774    case EM_VISIUM:
11775      return reloc_type == 6;  /* R_VISIUM_32_PCREL */
11776    case EM_X86_64:
11777    case EM_L1OM:
11778    case EM_K1OM:
11779      return reloc_type == 2;  /* R_X86_64_PC32.  */
11780    case EM_VAX:
11781      return reloc_type == 4;  /* R_VAX_PCREL32.  */
11782    case EM_XTENSA_OLD:
11783    case EM_XTENSA:
11784      return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
11785    default:
11786      /* Do not abort or issue an error message here.  Not all targets use
11787	 pc-relative 32-bit relocs in their DWARF debug information and we
11788	 have already tested for target coverage in is_32bit_abs_reloc.  A
11789	 more helpful warning message will be generated by apply_relocations
11790	 anyway, so just return.  */
11791      return FALSE;
11792    }
11793}
11794
11795/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11796   a 64-bit absolute RELA relocation used in DWARF debug sections.  */
11797
11798static bfd_boolean
11799is_64bit_abs_reloc (unsigned int reloc_type)
11800{
11801  switch (elf_header.e_machine)
11802    {
11803    case EM_AARCH64:
11804      return reloc_type == 257;	/* R_AARCH64_ABS64.  */
11805    case EM_ALPHA:
11806      return reloc_type == 2; /* R_ALPHA_REFQUAD.  */
11807    case EM_IA_64:
11808      return reloc_type == 0x27; /* R_IA64_DIR64LSB.  */
11809    case EM_PARISC:
11810      return reloc_type == 80; /* R_PARISC_DIR64.  */
11811    case EM_PPC64:
11812      return reloc_type == 38; /* R_PPC64_ADDR64.  */
11813    case EM_RISCV:
11814      return reloc_type == 2; /* R_RISCV_64.  */
11815    case EM_SPARC32PLUS:
11816    case EM_SPARCV9:
11817    case EM_SPARC:
11818      return reloc_type == 54; /* R_SPARC_UA64.  */
11819    case EM_X86_64:
11820    case EM_L1OM:
11821    case EM_K1OM:
11822      return reloc_type == 1; /* R_X86_64_64.  */
11823    case EM_S390_OLD:
11824    case EM_S390:
11825      return reloc_type == 22;	/* R_S390_64.  */
11826    case EM_TILEGX:
11827      return reloc_type == 1; /* R_TILEGX_64.  */
11828    case EM_MIPS:
11829      return reloc_type == 18;	/* R_MIPS_64.  */
11830    default:
11831      return FALSE;
11832    }
11833}
11834
11835/* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
11836   a 64-bit pc-relative RELA relocation used in DWARF debug sections.  */
11837
11838static bfd_boolean
11839is_64bit_pcrel_reloc (unsigned int reloc_type)
11840{
11841  switch (elf_header.e_machine)
11842    {
11843    case EM_AARCH64:
11844      return reloc_type == 260;	/* R_AARCH64_PREL64.  */
11845    case EM_ALPHA:
11846      return reloc_type == 11; /* R_ALPHA_SREL64.  */
11847    case EM_IA_64:
11848      return reloc_type == 0x4f; /* R_IA64_PCREL64LSB.  */
11849    case EM_PARISC:
11850      return reloc_type == 72; /* R_PARISC_PCREL64.  */
11851    case EM_PPC64:
11852      return reloc_type == 44; /* R_PPC64_REL64.  */
11853    case EM_SPARC32PLUS:
11854    case EM_SPARCV9:
11855    case EM_SPARC:
11856      return reloc_type == 46; /* R_SPARC_DISP64.  */
11857    case EM_X86_64:
11858    case EM_L1OM:
11859    case EM_K1OM:
11860      return reloc_type == 24; /* R_X86_64_PC64.  */
11861    case EM_S390_OLD:
11862    case EM_S390:
11863      return reloc_type == 23;	/* R_S390_PC64.  */
11864    case EM_TILEGX:
11865      return reloc_type == 5;  /* R_TILEGX_64_PCREL.  */
11866    default:
11867      return FALSE;
11868    }
11869}
11870
11871/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11872   a 24-bit absolute RELA relocation used in DWARF debug sections.  */
11873
11874static bfd_boolean
11875is_24bit_abs_reloc (unsigned int reloc_type)
11876{
11877  switch (elf_header.e_machine)
11878    {
11879    case EM_CYGNUS_MN10200:
11880    case EM_MN10200:
11881      return reloc_type == 4; /* R_MN10200_24.  */
11882    case EM_FT32:
11883      return reloc_type == 5; /* R_FT32_20.  */
11884    default:
11885      return FALSE;
11886    }
11887}
11888
11889/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11890   a 16-bit absolute RELA relocation used in DWARF debug sections.  */
11891
11892static bfd_boolean
11893is_16bit_abs_reloc (unsigned int reloc_type)
11894{
11895  /* Please keep this table alpha-sorted for ease of visual lookup.  */
11896  switch (elf_header.e_machine)
11897    {
11898    case EM_ARC:
11899    case EM_ARC_COMPACT:
11900    case EM_ARC_COMPACT2:
11901      return reloc_type == 2; /* R_ARC_16.  */
11902    case EM_ADAPTEVA_EPIPHANY:
11903      return reloc_type == 5;
11904    case EM_AVR_OLD:
11905    case EM_AVR:
11906      return reloc_type == 4; /* R_AVR_16.  */
11907    case EM_CYGNUS_D10V:
11908    case EM_D10V:
11909      return reloc_type == 3; /* R_D10V_16.  */
11910    case EM_H8S:
11911    case EM_H8_300:
11912    case EM_H8_300H:
11913      return reloc_type == R_H8_DIR16;
11914    case EM_IP2K_OLD:
11915    case EM_IP2K:
11916      return reloc_type == 1; /* R_IP2K_16.  */
11917    case EM_M32C_OLD:
11918    case EM_M32C:
11919      return reloc_type == 1; /* R_M32C_16 */
11920    case EM_CYGNUS_MN10200:
11921    case EM_MN10200:
11922      return reloc_type == 2; /* R_MN10200_16.  */
11923    case EM_CYGNUS_MN10300:
11924    case EM_MN10300:
11925      return reloc_type == 2; /* R_MN10300_16.  */
11926    case EM_MSP430:
11927      if (uses_msp430x_relocs ())
11928	return reloc_type == 2; /* R_MSP430_ABS16.  */
11929    case EM_MSP430_OLD:
11930      return reloc_type == 5; /* R_MSP430_16_BYTE.  */
11931    case EM_NDS32:
11932      return reloc_type == 19; /* R_NDS32_RELA.  */
11933    case EM_ALTERA_NIOS2:
11934      return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16.  */
11935    case EM_NIOS32:
11936      return reloc_type == 9; /* R_NIOS_16.  */
11937    case EM_OR1K:
11938      return reloc_type == 2; /* R_OR1K_16.  */
11939    case EM_TI_C6000:
11940      return reloc_type == 2; /* R_C6000_ABS16.  */
11941    case EM_VISIUM:
11942      return reloc_type == 2; /* R_VISIUM_16. */
11943    case EM_XC16X:
11944    case EM_C166:
11945      return reloc_type == 2; /* R_XC16C_ABS_16.  */
11946    case EM_XGATE:
11947      return reloc_type == 3; /* R_XGATE_16.  */
11948    default:
11949      return FALSE;
11950    }
11951}
11952
11953/* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
11954   relocation entries (possibly formerly used for SHT_GROUP sections).  */
11955
11956static bfd_boolean
11957is_none_reloc (unsigned int reloc_type)
11958{
11959  switch (elf_header.e_machine)
11960    {
11961    case EM_386:     /* R_386_NONE.  */
11962    case EM_68K:     /* R_68K_NONE.  */
11963    case EM_ADAPTEVA_EPIPHANY:
11964    case EM_ALPHA:   /* R_ALPHA_NONE.  */
11965    case EM_ALTERA_NIOS2: /* R_NIOS2_NONE.  */
11966    case EM_ARC:     /* R_ARC_NONE.  */
11967    case EM_ARC_COMPACT2: /* R_ARC_NONE.  */
11968    case EM_ARC_COMPACT: /* R_ARC_NONE.  */
11969    case EM_ARM:     /* R_ARM_NONE.  */
11970    case EM_C166:    /* R_XC16X_NONE.  */
11971    case EM_CRIS:    /* R_CRIS_NONE.  */
11972    case EM_FT32:    /* R_FT32_NONE.  */
11973    case EM_IA_64:   /* R_IA64_NONE.  */
11974    case EM_K1OM:    /* R_X86_64_NONE.  */
11975    case EM_L1OM:    /* R_X86_64_NONE.  */
11976    case EM_M32R:    /* R_M32R_NONE.  */
11977    case EM_MIPS:    /* R_MIPS_NONE.  */
11978    case EM_MN10300: /* R_MN10300_NONE.  */
11979    case EM_MOXIE:   /* R_MOXIE_NONE.  */
11980    case EM_NIOS32:  /* R_NIOS_NONE.  */
11981    case EM_OR1K:    /* R_OR1K_NONE. */
11982    case EM_PARISC:  /* R_PARISC_NONE.  */
11983    case EM_PPC64:   /* R_PPC64_NONE.  */
11984    case EM_PPC:     /* R_PPC_NONE.  */
11985    case EM_RISCV:   /* R_RISCV_NONE.  */
11986    case EM_S390:    /* R_390_NONE.  */
11987    case EM_S390_OLD:
11988    case EM_SH:      /* R_SH_NONE.  */
11989    case EM_SPARC32PLUS:
11990    case EM_SPARC:   /* R_SPARC_NONE.  */
11991    case EM_SPARCV9:
11992    case EM_TILEGX:  /* R_TILEGX_NONE.  */
11993    case EM_TILEPRO: /* R_TILEPRO_NONE.  */
11994    case EM_TI_C6000:/* R_C6000_NONE.  */
11995    case EM_X86_64:  /* R_X86_64_NONE.  */
11996    case EM_XC16X:
11997      return reloc_type == 0;
11998
11999    case EM_AARCH64:
12000      return reloc_type == 0 || reloc_type == 256;
12001    case EM_AVR_OLD:
12002    case EM_AVR:
12003      return (reloc_type == 0 /* R_AVR_NONE.  */
12004	      || reloc_type == 30 /* R_AVR_DIFF8.  */
12005	      || reloc_type == 31 /* R_AVR_DIFF16.  */
12006	      || reloc_type == 32 /* R_AVR_DIFF32.  */);
12007    case EM_METAG:
12008      return reloc_type == 3; /* R_METAG_NONE.  */
12009    case EM_NDS32:
12010      return (reloc_type == 0       /* R_XTENSA_NONE.  */
12011	      || reloc_type == 204  /* R_NDS32_DIFF8.  */
12012	      || reloc_type == 205  /* R_NDS32_DIFF16.  */
12013	      || reloc_type == 206  /* R_NDS32_DIFF32.  */
12014	      || reloc_type == 207  /* R_NDS32_ULEB128.  */);
12015    case EM_XTENSA_OLD:
12016    case EM_XTENSA:
12017      return (reloc_type == 0      /* R_XTENSA_NONE.  */
12018	      || reloc_type == 17  /* R_XTENSA_DIFF8.  */
12019	      || reloc_type == 18  /* R_XTENSA_DIFF16.  */
12020	      || reloc_type == 19  /* R_XTENSA_DIFF32.  */);
12021    }
12022  return FALSE;
12023}
12024
12025/* Returns TRUE if there is a relocation against
12026   section NAME at OFFSET bytes.  */
12027
12028bfd_boolean
12029reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
12030{
12031  Elf_Internal_Rela * relocs;
12032  Elf_Internal_Rela * rp;
12033
12034  if (dsec == NULL || dsec->reloc_info == NULL)
12035    return FALSE;
12036
12037  relocs = (Elf_Internal_Rela *) dsec->reloc_info;
12038
12039  for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
12040    if (rp->r_offset == offset)
12041      return TRUE;
12042
12043   return FALSE;
12044}
12045
12046/* Apply relocations to a section.
12047   Note: So far support has been added only for those relocations
12048   which can be found in debug sections.
12049   If RELOCS_RETURN is non-NULL then returns in it a pointer to the
12050   loaded relocs.  It is then the caller's responsibility to free them.
12051   FIXME: Add support for more relocations ?  */
12052
12053static void
12054apply_relocations (void *                     file,
12055		   const Elf_Internal_Shdr *  section,
12056		   unsigned char *            start,
12057		   bfd_size_type              size,
12058		   void **                     relocs_return,
12059		   unsigned long *            num_relocs_return)
12060{
12061  Elf_Internal_Shdr * relsec;
12062  unsigned char * end = start + size;
12063
12064  if (relocs_return != NULL)
12065    {
12066      * (Elf_Internal_Rela **) relocs_return = NULL;
12067      * num_relocs_return = 0;
12068    }
12069
12070  if (elf_header.e_type != ET_REL)
12071    return;
12072
12073  /* Find the reloc section associated with the section.  */
12074  for (relsec = section_headers;
12075       relsec < section_headers + elf_header.e_shnum;
12076       ++relsec)
12077    {
12078      bfd_boolean is_rela;
12079      unsigned long num_relocs;
12080      Elf_Internal_Rela * relocs;
12081      Elf_Internal_Rela * rp;
12082      Elf_Internal_Shdr * symsec;
12083      Elf_Internal_Sym * symtab;
12084      unsigned long num_syms;
12085      Elf_Internal_Sym * sym;
12086
12087      if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
12088	  || relsec->sh_info >= elf_header.e_shnum
12089	  || section_headers + relsec->sh_info != section
12090	  || relsec->sh_size == 0
12091	  || relsec->sh_link >= elf_header.e_shnum)
12092	continue;
12093
12094      is_rela = relsec->sh_type == SHT_RELA;
12095
12096      if (is_rela)
12097	{
12098	  if (!slurp_rela_relocs ((FILE *) file, relsec->sh_offset,
12099                                  relsec->sh_size, & relocs, & num_relocs))
12100	    return;
12101	}
12102      else
12103	{
12104	  if (!slurp_rel_relocs ((FILE *) file, relsec->sh_offset,
12105                                 relsec->sh_size, & relocs, & num_relocs))
12106	    return;
12107	}
12108
12109      /* SH uses RELA but uses in place value instead of the addend field.  */
12110      if (elf_header.e_machine == EM_SH)
12111	is_rela = FALSE;
12112
12113      symsec = section_headers + relsec->sh_link;
12114      symtab = GET_ELF_SYMBOLS ((FILE *) file, symsec, & num_syms);
12115
12116      for (rp = relocs; rp < relocs + num_relocs; ++rp)
12117	{
12118	  bfd_vma         addend;
12119	  unsigned int    reloc_type;
12120	  unsigned int    reloc_size;
12121	  unsigned char * rloc;
12122	  unsigned long   sym_index;
12123
12124	  reloc_type = get_reloc_type (rp->r_info);
12125
12126	  if (target_specific_reloc_handling (rp, start, symtab))
12127	    continue;
12128	  else if (is_none_reloc (reloc_type))
12129	    continue;
12130	  else if (is_32bit_abs_reloc (reloc_type)
12131		   || is_32bit_pcrel_reloc (reloc_type))
12132	    reloc_size = 4;
12133	  else if (is_64bit_abs_reloc (reloc_type)
12134		   || is_64bit_pcrel_reloc (reloc_type))
12135	    reloc_size = 8;
12136	  else if (is_24bit_abs_reloc (reloc_type))
12137	    reloc_size = 3;
12138	  else if (is_16bit_abs_reloc (reloc_type))
12139	    reloc_size = 2;
12140	  else
12141	    {
12142	      static unsigned int prev_reloc = 0;
12143	      if (reloc_type != prev_reloc)
12144		warn (_("unable to apply unsupported reloc type %d to section %s\n"),
12145		      reloc_type, printable_section_name (section));
12146	      prev_reloc = reloc_type;
12147	      continue;
12148	    }
12149
12150	  rloc = start + rp->r_offset;
12151	  if ((rloc + reloc_size) > end || (rloc < start))
12152	    {
12153	      warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
12154		    (unsigned long) rp->r_offset,
12155		    printable_section_name (section));
12156	      continue;
12157	    }
12158
12159	  sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
12160	  if (sym_index >= num_syms)
12161	    {
12162	      warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
12163		    sym_index, printable_section_name (section));
12164	      continue;
12165	    }
12166	  sym = symtab + sym_index;
12167
12168	  /* If the reloc has a symbol associated with it,
12169	     make sure that it is of an appropriate type.
12170
12171	     Relocations against symbols without type can happen.
12172	     Gcc -feliminate-dwarf2-dups may generate symbols
12173	     without type for debug info.
12174
12175	     Icc generates relocations against function symbols
12176	     instead of local labels.
12177
12178	     Relocations against object symbols can happen, eg when
12179	     referencing a global array.  For an example of this see
12180	     the _clz.o binary in libgcc.a.  */
12181	  if (sym != symtab
12182	      && ELF_ST_TYPE (sym->st_info) != STT_COMMON
12183	      && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
12184	    {
12185	      warn (_("skipping unexpected symbol type %s in %ld'th relocation in section %s\n"),
12186		    get_symbol_type (ELF_ST_TYPE (sym->st_info)),
12187		    (long int)(rp - relocs),
12188		    printable_section_name (relsec));
12189	      continue;
12190	    }
12191
12192	  addend = 0;
12193	  if (is_rela)
12194	    addend += rp->r_addend;
12195	  /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
12196	     partial_inplace.  */
12197	  if (!is_rela
12198	      || (elf_header.e_machine == EM_XTENSA
12199		  && reloc_type == 1)
12200	      || ((elf_header.e_machine == EM_PJ
12201		   || elf_header.e_machine == EM_PJ_OLD)
12202		  && reloc_type == 1)
12203	      || ((elf_header.e_machine == EM_D30V
12204		   || elf_header.e_machine == EM_CYGNUS_D30V)
12205		  && reloc_type == 12))
12206	    addend += byte_get (rloc, reloc_size);
12207
12208	  if (is_32bit_pcrel_reloc (reloc_type)
12209	      || is_64bit_pcrel_reloc (reloc_type))
12210	    {
12211	      /* On HPPA, all pc-relative relocations are biased by 8.  */
12212	      if (elf_header.e_machine == EM_PARISC)
12213		addend -= 8;
12214	      byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
12215		        reloc_size);
12216	    }
12217	  else
12218	    byte_put (rloc, addend + sym->st_value, reloc_size);
12219	}
12220
12221      free (symtab);
12222
12223      if (relocs_return)
12224	{
12225	  * (Elf_Internal_Rela **) relocs_return = relocs;
12226	  * num_relocs_return = num_relocs;
12227	}
12228      else
12229	free (relocs);
12230
12231      break;
12232    }
12233}
12234
12235#ifdef SUPPORT_DISASSEMBLY
12236static int
12237disassemble_section (Elf_Internal_Shdr * section, FILE * file)
12238{
12239  printf (_("\nAssembly dump of section %s\n"), printable_section_name (section));
12240
12241  /* FIXME: XXX -- to be done --- XXX */
12242
12243  return 1;
12244}
12245#endif
12246
12247/* Reads in the contents of SECTION from FILE, returning a pointer
12248   to a malloc'ed buffer or NULL if something went wrong.  */
12249
12250static char *
12251get_section_contents (Elf_Internal_Shdr * section, FILE * file)
12252{
12253  bfd_size_type num_bytes;
12254
12255  num_bytes = section->sh_size;
12256
12257  if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
12258    {
12259      printf (_("\nSection '%s' has no data to dump.\n"),
12260	      printable_section_name (section));
12261      return NULL;
12262    }
12263
12264  return  (char *) get_data (NULL, file, section->sh_offset, 1, num_bytes,
12265                             _("section contents"));
12266}
12267
12268/* Uncompresses a section that was compressed using zlib, in place.  */
12269
12270static bfd_boolean
12271uncompress_section_contents (unsigned char **buffer,
12272			     dwarf_size_type uncompressed_size,
12273			     dwarf_size_type *size)
12274{
12275  dwarf_size_type compressed_size = *size;
12276  unsigned char * compressed_buffer = *buffer;
12277  unsigned char * uncompressed_buffer;
12278  z_stream strm;
12279  int rc;
12280
12281  /* It is possible the section consists of several compressed
12282     buffers concatenated together, so we uncompress in a loop.  */
12283  /* PR 18313: The state field in the z_stream structure is supposed
12284     to be invisible to the user (ie us), but some compilers will
12285     still complain about it being used without initialisation.  So
12286     we first zero the entire z_stream structure and then set the fields
12287     that we need.  */
12288  memset (& strm, 0, sizeof strm);
12289  strm.avail_in = compressed_size;
12290  strm.next_in = (Bytef *) compressed_buffer;
12291  strm.avail_out = uncompressed_size;
12292  uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
12293
12294  rc = inflateInit (& strm);
12295  while (strm.avail_in > 0)
12296    {
12297      if (rc != Z_OK)
12298        goto fail;
12299      strm.next_out = ((Bytef *) uncompressed_buffer
12300                       + (uncompressed_size - strm.avail_out));
12301      rc = inflate (&strm, Z_FINISH);
12302      if (rc != Z_STREAM_END)
12303        goto fail;
12304      rc = inflateReset (& strm);
12305    }
12306  rc = inflateEnd (& strm);
12307  if (rc != Z_OK
12308      || strm.avail_out != 0)
12309    goto fail;
12310
12311  *buffer = uncompressed_buffer;
12312  *size = uncompressed_size;
12313  return TRUE;
12314
12315 fail:
12316  free (uncompressed_buffer);
12317  /* Indicate decompression failure.  */
12318  *buffer = NULL;
12319  return FALSE;
12320}
12321
12322static void
12323dump_section_as_strings (Elf_Internal_Shdr * section, FILE * file)
12324{
12325  Elf_Internal_Shdr *  relsec;
12326  bfd_size_type        num_bytes;
12327  unsigned char *      data;
12328  unsigned char *      end;
12329  unsigned char *      real_start;
12330  unsigned char *      start;
12331  bfd_boolean          some_strings_shown;
12332
12333  real_start = start = (unsigned char *) get_section_contents (section,
12334							       file);
12335  if (start == NULL)
12336    return;
12337  num_bytes = section->sh_size;
12338
12339  printf (_("\nString dump of section '%s':\n"), printable_section_name (section));
12340
12341  if (decompress_dumps)
12342    {
12343      dwarf_size_type new_size = num_bytes;
12344      dwarf_size_type uncompressed_size = 0;
12345
12346      if ((section->sh_flags & SHF_COMPRESSED) != 0)
12347	{
12348	  Elf_Internal_Chdr chdr;
12349	  unsigned int compression_header_size
12350	    = get_compression_header (& chdr, (unsigned char *) start);
12351
12352	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
12353	    {
12354	      warn (_("section '%s' has unsupported compress type: %d\n"),
12355		    printable_section_name (section), chdr.ch_type);
12356	      return;
12357	    }
12358	  else if (chdr.ch_addralign != section->sh_addralign)
12359	    {
12360	      warn (_("compressed section '%s' is corrupted\n"),
12361		    printable_section_name (section));
12362	      return;
12363	    }
12364	  uncompressed_size = chdr.ch_size;
12365	  start += compression_header_size;
12366	  new_size -= compression_header_size;
12367	}
12368      else if (new_size > 12 && streq ((char *) start, "ZLIB"))
12369	{
12370	  /* Read the zlib header.  In this case, it should be "ZLIB"
12371	     followed by the uncompressed section size, 8 bytes in
12372	     big-endian order.  */
12373	  uncompressed_size = start[4]; uncompressed_size <<= 8;
12374	  uncompressed_size += start[5]; uncompressed_size <<= 8;
12375	  uncompressed_size += start[6]; uncompressed_size <<= 8;
12376	  uncompressed_size += start[7]; uncompressed_size <<= 8;
12377	  uncompressed_size += start[8]; uncompressed_size <<= 8;
12378	  uncompressed_size += start[9]; uncompressed_size <<= 8;
12379	  uncompressed_size += start[10]; uncompressed_size <<= 8;
12380	  uncompressed_size += start[11];
12381	  start += 12;
12382	  new_size -= 12;
12383	}
12384
12385      if (uncompressed_size
12386	  && uncompress_section_contents (& start,
12387					  uncompressed_size, & new_size))
12388	num_bytes = new_size;
12389    }
12390
12391  /* If the section being dumped has relocations against it the user might
12392     be expecting these relocations to have been applied.  Check for this
12393     case and issue a warning message in order to avoid confusion.
12394     FIXME: Maybe we ought to have an option that dumps a section with
12395     relocs applied ?  */
12396  for (relsec = section_headers;
12397       relsec < section_headers + elf_header.e_shnum;
12398       ++relsec)
12399    {
12400      if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
12401	  || relsec->sh_info >= elf_header.e_shnum
12402	  || section_headers + relsec->sh_info != section
12403	  || relsec->sh_size == 0
12404	  || relsec->sh_link >= elf_header.e_shnum)
12405	continue;
12406
12407      printf (_("  Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
12408      break;
12409    }
12410
12411  data = start;
12412  end  = start + num_bytes;
12413  some_strings_shown = FALSE;
12414
12415  while (data < end)
12416    {
12417      while (!ISPRINT (* data))
12418	if (++ data >= end)
12419	  break;
12420
12421      if (data < end)
12422	{
12423	  size_t maxlen = end - data;
12424
12425#ifndef __MSVCRT__
12426	  /* PR 11128: Use two separate invocations in order to work
12427             around bugs in the Solaris 8 implementation of printf.  */
12428	  printf ("  [%6tx]  ", data - start);
12429#else
12430	  printf ("  [%6Ix]  ", (size_t) (data - start));
12431#endif
12432	  if (maxlen > 0)
12433	    {
12434	      print_symbol ((int) maxlen, (const char *) data);
12435	      putchar ('\n');
12436	      data += strnlen ((const char *) data, maxlen);
12437	    }
12438	  else
12439	    {
12440	      printf (_("<corrupt>\n"));
12441	      data = end;
12442	    }
12443	  some_strings_shown = TRUE;
12444	}
12445    }
12446
12447  if (! some_strings_shown)
12448    printf (_("  No strings found in this section."));
12449
12450  free (real_start);
12451
12452  putchar ('\n');
12453}
12454
12455static void
12456dump_section_as_bytes (Elf_Internal_Shdr * section,
12457		       FILE * file,
12458		       bfd_boolean relocate)
12459{
12460  Elf_Internal_Shdr * relsec;
12461  bfd_size_type       bytes;
12462  bfd_size_type       section_size;
12463  bfd_vma             addr;
12464  unsigned char *     data;
12465  unsigned char *     real_start;
12466  unsigned char *     start;
12467
12468  real_start = start = (unsigned char *) get_section_contents (section, file);
12469  if (start == NULL)
12470    return;
12471  section_size = section->sh_size;
12472
12473  printf (_("\nHex dump of section '%s':\n"), printable_section_name (section));
12474
12475  if (decompress_dumps)
12476    {
12477      dwarf_size_type new_size = section_size;
12478      dwarf_size_type uncompressed_size = 0;
12479
12480      if ((section->sh_flags & SHF_COMPRESSED) != 0)
12481	{
12482	  Elf_Internal_Chdr chdr;
12483	  unsigned int compression_header_size
12484	    = get_compression_header (& chdr, start);
12485
12486	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
12487	    {
12488	      warn (_("section '%s' has unsupported compress type: %d\n"),
12489		    printable_section_name (section), chdr.ch_type);
12490	      return;
12491	    }
12492	  else if (chdr.ch_addralign != section->sh_addralign)
12493	    {
12494	      warn (_("compressed section '%s' is corrupted\n"),
12495		    printable_section_name (section));
12496	      return;
12497	    }
12498	  uncompressed_size = chdr.ch_size;
12499	  start += compression_header_size;
12500	  new_size -= compression_header_size;
12501	}
12502      else if (new_size > 12 && streq ((char *) start, "ZLIB"))
12503	{
12504	  /* Read the zlib header.  In this case, it should be "ZLIB"
12505	     followed by the uncompressed section size, 8 bytes in
12506	     big-endian order.  */
12507	  uncompressed_size = start[4]; uncompressed_size <<= 8;
12508	  uncompressed_size += start[5]; uncompressed_size <<= 8;
12509	  uncompressed_size += start[6]; uncompressed_size <<= 8;
12510	  uncompressed_size += start[7]; uncompressed_size <<= 8;
12511	  uncompressed_size += start[8]; uncompressed_size <<= 8;
12512	  uncompressed_size += start[9]; uncompressed_size <<= 8;
12513	  uncompressed_size += start[10]; uncompressed_size <<= 8;
12514	  uncompressed_size += start[11];
12515	  start += 12;
12516	  new_size -= 12;
12517	}
12518
12519      if (uncompressed_size
12520	  && uncompress_section_contents (& start, uncompressed_size,
12521					  & new_size))
12522	section_size = new_size;
12523    }
12524
12525  if (relocate)
12526    {
12527      apply_relocations (file, section, start, section_size, NULL, NULL);
12528    }
12529  else
12530    {
12531      /* If the section being dumped has relocations against it the user might
12532	 be expecting these relocations to have been applied.  Check for this
12533	 case and issue a warning message in order to avoid confusion.
12534	 FIXME: Maybe we ought to have an option that dumps a section with
12535	 relocs applied ?  */
12536      for (relsec = section_headers;
12537	   relsec < section_headers + elf_header.e_shnum;
12538	   ++relsec)
12539	{
12540	  if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
12541	      || relsec->sh_info >= elf_header.e_shnum
12542	      || section_headers + relsec->sh_info != section
12543	      || relsec->sh_size == 0
12544	      || relsec->sh_link >= elf_header.e_shnum)
12545	    continue;
12546
12547	  printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
12548	  break;
12549	}
12550    }
12551
12552  addr = section->sh_addr;
12553  bytes = section_size;
12554  data = start;
12555
12556  while (bytes)
12557    {
12558      int j;
12559      int k;
12560      int lbytes;
12561
12562      lbytes = (bytes > 16 ? 16 : bytes);
12563
12564      printf ("  0x%8.8lx ", (unsigned long) addr);
12565
12566      for (j = 0; j < 16; j++)
12567	{
12568	  if (j < lbytes)
12569	    printf ("%2.2x", data[j]);
12570	  else
12571	    printf ("  ");
12572
12573	  if ((j & 3) == 3)
12574	    printf (" ");
12575	}
12576
12577      for (j = 0; j < lbytes; j++)
12578	{
12579	  k = data[j];
12580	  if (k >= ' ' && k < 0x7f)
12581	    printf ("%c", k);
12582	  else
12583	    printf (".");
12584	}
12585
12586      putchar ('\n');
12587
12588      data  += lbytes;
12589      addr  += lbytes;
12590      bytes -= lbytes;
12591    }
12592
12593  free (real_start);
12594
12595  putchar ('\n');
12596}
12597
12598static int
12599load_specific_debug_section (enum dwarf_section_display_enum debug,
12600			     const Elf_Internal_Shdr * sec, void * file)
12601{
12602  struct dwarf_section * section = &debug_displays [debug].section;
12603  char buf [64];
12604
12605  /* If it is already loaded, do nothing.  */
12606  if (section->start != NULL)
12607    return 1;
12608
12609  snprintf (buf, sizeof (buf), _("%s section data"), section->name);
12610  section->address = sec->sh_addr;
12611  section->user_data = NULL;
12612  section->start = (unsigned char *) get_data (NULL, (FILE *) file,
12613                                               sec->sh_offset, 1,
12614                                               sec->sh_size, buf);
12615  if (section->start == NULL)
12616    section->size = 0;
12617  else
12618    {
12619      unsigned char *start = section->start;
12620      dwarf_size_type size = sec->sh_size;
12621      dwarf_size_type uncompressed_size = 0;
12622
12623      if ((sec->sh_flags & SHF_COMPRESSED) != 0)
12624	{
12625	  Elf_Internal_Chdr chdr;
12626	  unsigned int compression_header_size
12627	    = get_compression_header (&chdr, start);
12628	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
12629	    {
12630	      warn (_("section '%s' has unsupported compress type: %d\n"),
12631		    section->name, chdr.ch_type);
12632	      return 0;
12633	    }
12634	  else if (chdr.ch_addralign != sec->sh_addralign)
12635	    {
12636	      warn (_("compressed section '%s' is corrupted\n"),
12637		    section->name);
12638	      return 0;
12639	    }
12640	  uncompressed_size = chdr.ch_size;
12641	  start += compression_header_size;
12642	  size -= compression_header_size;
12643	}
12644      else if (size > 12 && streq ((char *) start, "ZLIB"))
12645	{
12646	  /* Read the zlib header.  In this case, it should be "ZLIB"
12647	     followed by the uncompressed section size, 8 bytes in
12648	     big-endian order.  */
12649	  uncompressed_size = start[4]; uncompressed_size <<= 8;
12650	  uncompressed_size += start[5]; uncompressed_size <<= 8;
12651	  uncompressed_size += start[6]; uncompressed_size <<= 8;
12652	  uncompressed_size += start[7]; uncompressed_size <<= 8;
12653	  uncompressed_size += start[8]; uncompressed_size <<= 8;
12654	  uncompressed_size += start[9]; uncompressed_size <<= 8;
12655	  uncompressed_size += start[10]; uncompressed_size <<= 8;
12656	  uncompressed_size += start[11];
12657	  start += 12;
12658	  size -= 12;
12659	}
12660
12661      if (uncompressed_size
12662	  && uncompress_section_contents (&start, uncompressed_size,
12663					  &size))
12664	{
12665	  /* Free the compressed buffer, update the section buffer
12666	     and the section size if uncompress is successful.  */
12667	  free (section->start);
12668	  section->start = start;
12669	}
12670      section->size = size;
12671    }
12672
12673  if (section->start == NULL)
12674    return 0;
12675
12676  if (debug_displays [debug].relocate)
12677    apply_relocations ((FILE *) file, sec, section->start, section->size,
12678		       & section->reloc_info, & section->num_relocs);
12679  else
12680    {
12681      section->reloc_info = NULL;
12682      section->num_relocs = 0;
12683    }
12684
12685  return 1;
12686}
12687
12688/* If this is not NULL, load_debug_section will only look for sections
12689   within the list of sections given here.  */
12690unsigned int *section_subset = NULL;
12691
12692int
12693load_debug_section (enum dwarf_section_display_enum debug, void * file)
12694{
12695  struct dwarf_section * section = &debug_displays [debug].section;
12696  Elf_Internal_Shdr * sec;
12697
12698  /* Locate the debug section.  */
12699  sec = find_section_in_set (section->uncompressed_name, section_subset);
12700  if (sec != NULL)
12701    section->name = section->uncompressed_name;
12702  else
12703    {
12704      sec = find_section_in_set (section->compressed_name, section_subset);
12705      if (sec != NULL)
12706	section->name = section->compressed_name;
12707    }
12708  if (sec == NULL)
12709    return 0;
12710
12711  /* If we're loading from a subset of sections, and we've loaded
12712     a section matching this name before, it's likely that it's a
12713     different one.  */
12714  if (section_subset != NULL)
12715    free_debug_section (debug);
12716
12717  return load_specific_debug_section (debug, sec, (FILE *) file);
12718}
12719
12720void
12721free_debug_section (enum dwarf_section_display_enum debug)
12722{
12723  struct dwarf_section * section = &debug_displays [debug].section;
12724
12725  if (section->start == NULL)
12726    return;
12727
12728  free ((char *) section->start);
12729  section->start = NULL;
12730  section->address = 0;
12731  section->size = 0;
12732}
12733
12734static int
12735display_debug_section (int shndx, Elf_Internal_Shdr * section, FILE * file)
12736{
12737  char * name = SECTION_NAME (section);
12738  const char * print_name = printable_section_name (section);
12739  bfd_size_type length;
12740  int result = 1;
12741  int i;
12742
12743  length = section->sh_size;
12744  if (length == 0)
12745    {
12746      printf (_("\nSection '%s' has no debugging data.\n"), print_name);
12747      return 0;
12748    }
12749  if (section->sh_type == SHT_NOBITS)
12750    {
12751      /* There is no point in dumping the contents of a debugging section
12752	 which has the NOBITS type - the bits in the file will be random.
12753	 This can happen when a file containing a .eh_frame section is
12754	 stripped with the --only-keep-debug command line option.  */
12755      printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
12756	      print_name);
12757      return 0;
12758    }
12759
12760  if (const_strneq (name, ".gnu.linkonce.wi."))
12761    name = ".debug_info";
12762
12763  /* See if we know how to display the contents of this section.  */
12764  for (i = 0; i < max; i++)
12765    if (streq (debug_displays[i].section.uncompressed_name, name)
12766	|| (i == line && const_strneq (name, ".debug_line."))
12767        || streq (debug_displays[i].section.compressed_name, name))
12768      {
12769	struct dwarf_section * sec = &debug_displays [i].section;
12770	int secondary = (section != find_section (name));
12771
12772	if (secondary)
12773	  free_debug_section ((enum dwarf_section_display_enum) i);
12774
12775	if (i == line && const_strneq (name, ".debug_line."))
12776	  sec->name = name;
12777	else if (streq (sec->uncompressed_name, name))
12778	  sec->name = sec->uncompressed_name;
12779	else
12780	  sec->name = sec->compressed_name;
12781	if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
12782                                         section, file))
12783	  {
12784	    /* If this debug section is part of a CU/TU set in a .dwp file,
12785	       restrict load_debug_section to the sections in that set.  */
12786	    section_subset = find_cu_tu_set (file, shndx);
12787
12788	    result &= debug_displays[i].display (sec, file);
12789
12790	    section_subset = NULL;
12791
12792	    if (secondary || (i != info && i != abbrev))
12793	      free_debug_section ((enum dwarf_section_display_enum) i);
12794	  }
12795
12796	break;
12797      }
12798
12799  if (i == max)
12800    {
12801      printf (_("Unrecognized debug section: %s\n"), print_name);
12802      result = 0;
12803    }
12804
12805  return result;
12806}
12807
12808/* Set DUMP_SECTS for all sections where dumps were requested
12809   based on section name.  */
12810
12811static void
12812initialise_dumps_byname (void)
12813{
12814  struct dump_list_entry * cur;
12815
12816  for (cur = dump_sects_byname; cur; cur = cur->next)
12817    {
12818      unsigned int i;
12819      int any;
12820
12821      for (i = 0, any = 0; i < elf_header.e_shnum; i++)
12822	if (streq (SECTION_NAME (section_headers + i), cur->name))
12823	  {
12824	    request_dump_bynumber (i, cur->type);
12825	    any = 1;
12826	  }
12827
12828      if (!any)
12829	warn (_("Section '%s' was not dumped because it does not exist!\n"),
12830	      cur->name);
12831    }
12832}
12833
12834static void
12835process_section_contents (FILE * file)
12836{
12837  Elf_Internal_Shdr * section;
12838  unsigned int i;
12839
12840  if (! do_dump)
12841    return;
12842
12843  initialise_dumps_byname ();
12844
12845  for (i = 0, section = section_headers;
12846       i < elf_header.e_shnum && i < num_dump_sects;
12847       i++, section++)
12848    {
12849#ifdef SUPPORT_DISASSEMBLY
12850      if (dump_sects[i] & DISASS_DUMP)
12851	disassemble_section (section, file);
12852#endif
12853      if (dump_sects[i] & HEX_DUMP)
12854	dump_section_as_bytes (section, file, FALSE);
12855
12856      if (dump_sects[i] & RELOC_DUMP)
12857	dump_section_as_bytes (section, file, TRUE);
12858
12859      if (dump_sects[i] & STRING_DUMP)
12860	dump_section_as_strings (section, file);
12861
12862      if (dump_sects[i] & DEBUG_DUMP)
12863	display_debug_section (i, section, file);
12864    }
12865
12866  /* Check to see if the user requested a
12867     dump of a section that does not exist.  */
12868  while (i++ < num_dump_sects)
12869    if (dump_sects[i])
12870      warn (_("Section %d was not dumped because it does not exist!\n"), i);
12871}
12872
12873static void
12874process_mips_fpe_exception (int mask)
12875{
12876  if (mask)
12877    {
12878      int first = 1;
12879      if (mask & OEX_FPU_INEX)
12880	fputs ("INEX", stdout), first = 0;
12881      if (mask & OEX_FPU_UFLO)
12882	printf ("%sUFLO", first ? "" : "|"), first = 0;
12883      if (mask & OEX_FPU_OFLO)
12884	printf ("%sOFLO", first ? "" : "|"), first = 0;
12885      if (mask & OEX_FPU_DIV0)
12886	printf ("%sDIV0", first ? "" : "|"), first = 0;
12887      if (mask & OEX_FPU_INVAL)
12888	printf ("%sINVAL", first ? "" : "|");
12889    }
12890  else
12891    fputs ("0", stdout);
12892}
12893
12894/* Display's the value of TAG at location P.  If TAG is
12895   greater than 0 it is assumed to be an unknown tag, and
12896   a message is printed to this effect.  Otherwise it is
12897   assumed that a message has already been printed.
12898
12899   If the bottom bit of TAG is set it assumed to have a
12900   string value, otherwise it is assumed to have an integer
12901   value.
12902
12903   Returns an updated P pointing to the first unread byte
12904   beyond the end of TAG's value.
12905
12906   Reads at or beyond END will not be made.  */
12907
12908static unsigned char *
12909display_tag_value (int tag,
12910		   unsigned char * p,
12911		   const unsigned char * const end)
12912{
12913  unsigned long val;
12914
12915  if (tag > 0)
12916    printf ("  Tag_unknown_%d: ", tag);
12917
12918  if (p >= end)
12919    {
12920      warn (_("<corrupt tag>\n"));
12921    }
12922  else if (tag & 1)
12923    {
12924      /* PR 17531 file: 027-19978-0.004.  */
12925      size_t maxlen = (end - p) - 1;
12926
12927      putchar ('"');
12928      if (maxlen > 0)
12929	{
12930	  print_symbol ((int) maxlen, (const char *) p);
12931	  p += strnlen ((char *) p, maxlen) + 1;
12932	}
12933      else
12934	{
12935	  printf (_("<corrupt string tag>"));
12936	  p = (unsigned char *) end;
12937	}
12938      printf ("\"\n");
12939    }
12940  else
12941    {
12942      unsigned int len;
12943
12944      val = read_uleb128 (p, &len, end);
12945      p += len;
12946      printf ("%ld (0x%lx)\n", val, val);
12947    }
12948
12949  assert (p <= end);
12950  return p;
12951}
12952
12953/* ARM EABI attributes section.  */
12954typedef struct
12955{
12956  unsigned int tag;
12957  const char * name;
12958  /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup.  */
12959  unsigned int type;
12960  const char ** table;
12961} arm_attr_public_tag;
12962
12963static const char * arm_attr_tag_CPU_arch[] =
12964  {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
12965   "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "", "v8-M.baseline",
12966   "v8-M.mainline"};
12967static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
12968static const char * arm_attr_tag_THUMB_ISA_use[] =
12969  {"No", "Thumb-1", "Thumb-2", "Yes"};
12970static const char * arm_attr_tag_FP_arch[] =
12971  {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
12972   "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
12973static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
12974static const char * arm_attr_tag_Advanced_SIMD_arch[] =
12975  {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
12976   "NEON for ARMv8.1"};
12977static const char * arm_attr_tag_PCS_config[] =
12978  {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
12979   "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
12980static const char * arm_attr_tag_ABI_PCS_R9_use[] =
12981  {"V6", "SB", "TLS", "Unused"};
12982static const char * arm_attr_tag_ABI_PCS_RW_data[] =
12983  {"Absolute", "PC-relative", "SB-relative", "None"};
12984static const char * arm_attr_tag_ABI_PCS_RO_data[] =
12985  {"Absolute", "PC-relative", "None"};
12986static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
12987  {"None", "direct", "GOT-indirect"};
12988static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
12989  {"None", "??? 1", "2", "??? 3", "4"};
12990static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
12991static const char * arm_attr_tag_ABI_FP_denormal[] =
12992  {"Unused", "Needed", "Sign only"};
12993static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
12994static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
12995static const char * arm_attr_tag_ABI_FP_number_model[] =
12996  {"Unused", "Finite", "RTABI", "IEEE 754"};
12997static const char * arm_attr_tag_ABI_enum_size[] =
12998  {"Unused", "small", "int", "forced to int"};
12999static const char * arm_attr_tag_ABI_HardFP_use[] =
13000  {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
13001static const char * arm_attr_tag_ABI_VFP_args[] =
13002  {"AAPCS", "VFP registers", "custom", "compatible"};
13003static const char * arm_attr_tag_ABI_WMMX_args[] =
13004  {"AAPCS", "WMMX registers", "custom"};
13005static const char * arm_attr_tag_ABI_optimization_goals[] =
13006  {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
13007    "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
13008static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
13009  {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
13010    "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
13011static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
13012static const char * arm_attr_tag_FP_HP_extension[] =
13013  {"Not Allowed", "Allowed"};
13014static const char * arm_attr_tag_ABI_FP_16bit_format[] =
13015  {"None", "IEEE 754", "Alternative Format"};
13016static const char * arm_attr_tag_DSP_extension[] =
13017  {"Follow architecture", "Allowed"};
13018static const char * arm_attr_tag_MPextension_use[] =
13019  {"Not Allowed", "Allowed"};
13020static const char * arm_attr_tag_DIV_use[] =
13021  {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
13022    "Allowed in v7-A with integer division extension"};
13023static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
13024static const char * arm_attr_tag_Virtualization_use[] =
13025  {"Not Allowed", "TrustZone", "Virtualization Extensions",
13026    "TrustZone and Virtualization Extensions"};
13027static const char * arm_attr_tag_MPextension_use_legacy[] =
13028  {"Not Allowed", "Allowed"};
13029
13030#define LOOKUP(id, name) \
13031  {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
13032static arm_attr_public_tag arm_attr_public_tags[] =
13033{
13034  {4, "CPU_raw_name", 1, NULL},
13035  {5, "CPU_name", 1, NULL},
13036  LOOKUP(6, CPU_arch),
13037  {7, "CPU_arch_profile", 0, NULL},
13038  LOOKUP(8, ARM_ISA_use),
13039  LOOKUP(9, THUMB_ISA_use),
13040  LOOKUP(10, FP_arch),
13041  LOOKUP(11, WMMX_arch),
13042  LOOKUP(12, Advanced_SIMD_arch),
13043  LOOKUP(13, PCS_config),
13044  LOOKUP(14, ABI_PCS_R9_use),
13045  LOOKUP(15, ABI_PCS_RW_data),
13046  LOOKUP(16, ABI_PCS_RO_data),
13047  LOOKUP(17, ABI_PCS_GOT_use),
13048  LOOKUP(18, ABI_PCS_wchar_t),
13049  LOOKUP(19, ABI_FP_rounding),
13050  LOOKUP(20, ABI_FP_denormal),
13051  LOOKUP(21, ABI_FP_exceptions),
13052  LOOKUP(22, ABI_FP_user_exceptions),
13053  LOOKUP(23, ABI_FP_number_model),
13054  {24, "ABI_align_needed", 0, NULL},
13055  {25, "ABI_align_preserved", 0, NULL},
13056  LOOKUP(26, ABI_enum_size),
13057  LOOKUP(27, ABI_HardFP_use),
13058  LOOKUP(28, ABI_VFP_args),
13059  LOOKUP(29, ABI_WMMX_args),
13060  LOOKUP(30, ABI_optimization_goals),
13061  LOOKUP(31, ABI_FP_optimization_goals),
13062  {32, "compatibility", 0, NULL},
13063  LOOKUP(34, CPU_unaligned_access),
13064  LOOKUP(36, FP_HP_extension),
13065  LOOKUP(38, ABI_FP_16bit_format),
13066  LOOKUP(42, MPextension_use),
13067  LOOKUP(44, DIV_use),
13068  LOOKUP(46, DSP_extension),
13069  {64, "nodefaults", 0, NULL},
13070  {65, "also_compatible_with", 0, NULL},
13071  LOOKUP(66, T2EE_use),
13072  {67, "conformance", 1, NULL},
13073  LOOKUP(68, Virtualization_use),
13074  LOOKUP(70, MPextension_use_legacy)
13075};
13076#undef LOOKUP
13077
13078static unsigned char *
13079display_arm_attribute (unsigned char * p,
13080		       const unsigned char * const end)
13081{
13082  unsigned int tag;
13083  unsigned int len;
13084  unsigned int val;
13085  arm_attr_public_tag * attr;
13086  unsigned i;
13087  unsigned int type;
13088
13089  tag = read_uleb128 (p, &len, end);
13090  p += len;
13091  attr = NULL;
13092  for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
13093    {
13094      if (arm_attr_public_tags[i].tag == tag)
13095	{
13096	  attr = &arm_attr_public_tags[i];
13097	  break;
13098	}
13099    }
13100
13101  if (attr)
13102    {
13103      printf ("  Tag_%s: ", attr->name);
13104      switch (attr->type)
13105	{
13106	case 0:
13107	  switch (tag)
13108	    {
13109	    case 7: /* Tag_CPU_arch_profile.  */
13110	      val = read_uleb128 (p, &len, end);
13111	      p += len;
13112	      switch (val)
13113		{
13114		case 0: printf (_("None\n")); break;
13115		case 'A': printf (_("Application\n")); break;
13116		case 'R': printf (_("Realtime\n")); break;
13117		case 'M': printf (_("Microcontroller\n")); break;
13118		case 'S': printf (_("Application or Realtime\n")); break;
13119		default: printf ("??? (%d)\n", val); break;
13120		}
13121	      break;
13122
13123	    case 24: /* Tag_align_needed.  */
13124	      val = read_uleb128 (p, &len, end);
13125	      p += len;
13126	      switch (val)
13127		{
13128		case 0: printf (_("None\n")); break;
13129		case 1: printf (_("8-byte\n")); break;
13130		case 2: printf (_("4-byte\n")); break;
13131		case 3: printf ("??? 3\n"); break;
13132		default:
13133		  if (val <= 12)
13134		    printf (_("8-byte and up to %d-byte extended\n"),
13135			    1 << val);
13136		  else
13137		    printf ("??? (%d)\n", val);
13138		  break;
13139		}
13140	      break;
13141
13142	    case 25: /* Tag_align_preserved.  */
13143	      val = read_uleb128 (p, &len, end);
13144	      p += len;
13145	      switch (val)
13146		{
13147		case 0: printf (_("None\n")); break;
13148		case 1: printf (_("8-byte, except leaf SP\n")); break;
13149		case 2: printf (_("8-byte\n")); break;
13150		case 3: printf ("??? 3\n"); break;
13151		default:
13152		  if (val <= 12)
13153		    printf (_("8-byte and up to %d-byte extended\n"),
13154			    1 << val);
13155		  else
13156		    printf ("??? (%d)\n", val);
13157		  break;
13158		}
13159	      break;
13160
13161	    case 32: /* Tag_compatibility.  */
13162	      {
13163		val = read_uleb128 (p, &len, end);
13164		p += len;
13165		printf (_("flag = %d, vendor = "), val);
13166		if (p < end - 1)
13167		  {
13168		    size_t maxlen = (end - p) - 1;
13169
13170		    print_symbol ((int) maxlen, (const char *) p);
13171		    p += strnlen ((char *) p, maxlen) + 1;
13172		  }
13173		else
13174		  {
13175		    printf (_("<corrupt>"));
13176		    p = (unsigned char *) end;
13177		  }
13178		putchar ('\n');
13179	      }
13180	      break;
13181
13182	    case 64: /* Tag_nodefaults.  */
13183	      /* PR 17531: file: 001-505008-0.01.  */
13184	      if (p < end)
13185		p++;
13186	      printf (_("True\n"));
13187	      break;
13188
13189	    case 65: /* Tag_also_compatible_with.  */
13190	      val = read_uleb128 (p, &len, end);
13191	      p += len;
13192	      if (val == 6 /* Tag_CPU_arch.  */)
13193		{
13194		  val = read_uleb128 (p, &len, end);
13195		  p += len;
13196		  if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
13197		    printf ("??? (%d)\n", val);
13198		  else
13199		    printf ("%s\n", arm_attr_tag_CPU_arch[val]);
13200		}
13201	      else
13202		printf ("???\n");
13203	      while (p < end && *(p++) != '\0' /* NUL terminator.  */)
13204		;
13205	      break;
13206
13207	    default:
13208	      printf (_("<unknown: %d>\n"), tag);
13209	      break;
13210	    }
13211	  return p;
13212
13213	case 1:
13214	  return display_tag_value (-1, p, end);
13215	case 2:
13216	  return display_tag_value (0, p, end);
13217
13218	default:
13219	  assert (attr->type & 0x80);
13220	  val = read_uleb128 (p, &len, end);
13221	  p += len;
13222	  type = attr->type & 0x7f;
13223	  if (val >= type)
13224	    printf ("??? (%d)\n", val);
13225	  else
13226	    printf ("%s\n", attr->table[val]);
13227	  return p;
13228	}
13229    }
13230
13231  return display_tag_value (tag, p, end);
13232}
13233
13234static unsigned char *
13235display_gnu_attribute (unsigned char * p,
13236		       unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int, const unsigned char * const),
13237		       const unsigned char * const end)
13238{
13239  int tag;
13240  unsigned int len;
13241  int val;
13242
13243  tag = read_uleb128 (p, &len, end);
13244  p += len;
13245
13246  /* Tag_compatibility is the only generic GNU attribute defined at
13247     present.  */
13248  if (tag == 32)
13249    {
13250      val = read_uleb128 (p, &len, end);
13251      p += len;
13252
13253      printf (_("flag = %d, vendor = "), val);
13254      if (p == end)
13255	{
13256	  printf (_("<corrupt>\n"));
13257	  warn (_("corrupt vendor attribute\n"));
13258	}
13259      else
13260	{
13261	  if (p < end - 1)
13262	    {
13263	      size_t maxlen = (end - p) - 1;
13264
13265	      print_symbol ((int) maxlen, (const char *) p);
13266	      p += strnlen ((char *) p, maxlen) + 1;
13267	    }
13268	  else
13269	    {
13270	      printf (_("<corrupt>"));
13271	      p = (unsigned char *) end;
13272	    }
13273	  putchar ('\n');
13274	}
13275      return p;
13276    }
13277
13278  if ((tag & 2) == 0 && display_proc_gnu_attribute)
13279    return display_proc_gnu_attribute (p, tag, end);
13280
13281  return display_tag_value (tag, p, end);
13282}
13283
13284static unsigned char *
13285display_power_gnu_attribute (unsigned char * p,
13286			     int tag,
13287			     const unsigned char * const end)
13288{
13289  unsigned int len;
13290  int val;
13291
13292  if (tag == Tag_GNU_Power_ABI_FP)
13293    {
13294      val = read_uleb128 (p, &len, end);
13295      p += len;
13296      printf ("  Tag_GNU_Power_ABI_FP: ");
13297
13298      switch (val)
13299	{
13300	case 0:
13301	  printf (_("Hard or soft float\n"));
13302	  break;
13303	case 1:
13304	  printf (_("Hard float\n"));
13305	  break;
13306	case 2:
13307	  printf (_("Soft float\n"));
13308	  break;
13309	case 3:
13310	  printf (_("Single-precision hard float\n"));
13311	  break;
13312	default:
13313	  printf ("??? (%d)\n", val);
13314	  break;
13315	}
13316      return p;
13317   }
13318
13319  if (tag == Tag_GNU_Power_ABI_Vector)
13320    {
13321      val = read_uleb128 (p, &len, end);
13322      p += len;
13323      printf ("  Tag_GNU_Power_ABI_Vector: ");
13324      switch (val)
13325	{
13326	case 0:
13327	  printf (_("Any\n"));
13328	  break;
13329	case 1:
13330	  printf (_("Generic\n"));
13331	  break;
13332	case 2:
13333	  printf ("AltiVec\n");
13334	  break;
13335	case 3:
13336	  printf ("SPE\n");
13337	  break;
13338	default:
13339	  printf ("??? (%d)\n", val);
13340	  break;
13341	}
13342      return p;
13343   }
13344
13345  if (tag == Tag_GNU_Power_ABI_Struct_Return)
13346    {
13347      if (p == end)
13348	{
13349	  warn (_("corrupt Tag_GNU_Power_ABI_Struct_Return\n"));
13350	  return p;
13351	}
13352
13353      val = read_uleb128 (p, &len, end);
13354      p += len;
13355      printf ("  Tag_GNU_Power_ABI_Struct_Return: ");
13356      switch (val)
13357       {
13358       case 0:
13359         printf (_("Any\n"));
13360         break;
13361       case 1:
13362         printf ("r3/r4\n");
13363         break;
13364       case 2:
13365         printf (_("Memory\n"));
13366         break;
13367       default:
13368         printf ("??? (%d)\n", val);
13369         break;
13370       }
13371      return p;
13372    }
13373
13374  return display_tag_value (tag & 1, p, end);
13375}
13376
13377static unsigned char *
13378display_s390_gnu_attribute (unsigned char * p,
13379			    int tag,
13380			    const unsigned char * const end)
13381{
13382  unsigned int len;
13383  int val;
13384
13385  if (tag == Tag_GNU_S390_ABI_Vector)
13386    {
13387      val = read_uleb128 (p, &len, end);
13388      p += len;
13389      printf ("  Tag_GNU_S390_ABI_Vector: ");
13390
13391      switch (val)
13392	{
13393	case 0:
13394	  printf (_("any\n"));
13395	  break;
13396	case 1:
13397	  printf (_("software\n"));
13398	  break;
13399	case 2:
13400	  printf (_("hardware\n"));
13401	  break;
13402	default:
13403	  printf ("??? (%d)\n", val);
13404	  break;
13405	}
13406      return p;
13407   }
13408
13409  return display_tag_value (tag & 1, p, end);
13410}
13411
13412static void
13413display_sparc_hwcaps (int mask)
13414{
13415  if (mask)
13416    {
13417      int first = 1;
13418
13419      if (mask & ELF_SPARC_HWCAP_MUL32)
13420	fputs ("mul32", stdout), first = 0;
13421      if (mask & ELF_SPARC_HWCAP_DIV32)
13422	printf ("%sdiv32", first ? "" : "|"), first = 0;
13423      if (mask & ELF_SPARC_HWCAP_FSMULD)
13424	printf ("%sfsmuld", first ? "" : "|"), first = 0;
13425      if (mask & ELF_SPARC_HWCAP_V8PLUS)
13426	printf ("%sv8plus", first ? "" : "|"), first = 0;
13427      if (mask & ELF_SPARC_HWCAP_POPC)
13428	printf ("%spopc", first ? "" : "|"), first = 0;
13429      if (mask & ELF_SPARC_HWCAP_VIS)
13430	printf ("%svis", first ? "" : "|"), first = 0;
13431      if (mask & ELF_SPARC_HWCAP_VIS2)
13432	printf ("%svis2", first ? "" : "|"), first = 0;
13433      if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
13434	printf ("%sASIBlkInit", first ? "" : "|"), first = 0;
13435      if (mask & ELF_SPARC_HWCAP_FMAF)
13436	printf ("%sfmaf", first ? "" : "|"), first = 0;
13437      if (mask & ELF_SPARC_HWCAP_VIS3)
13438	printf ("%svis3", first ? "" : "|"), first = 0;
13439      if (mask & ELF_SPARC_HWCAP_HPC)
13440	printf ("%shpc", first ? "" : "|"), first = 0;
13441      if (mask & ELF_SPARC_HWCAP_RANDOM)
13442	printf ("%srandom", first ? "" : "|"), first = 0;
13443      if (mask & ELF_SPARC_HWCAP_TRANS)
13444	printf ("%strans", first ? "" : "|"), first = 0;
13445      if (mask & ELF_SPARC_HWCAP_FJFMAU)
13446	printf ("%sfjfmau", first ? "" : "|"), first = 0;
13447      if (mask & ELF_SPARC_HWCAP_IMA)
13448	printf ("%sima", first ? "" : "|"), first = 0;
13449      if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
13450	printf ("%scspare", first ? "" : "|"), first = 0;
13451    }
13452  else
13453    fputc ('0', stdout);
13454  fputc ('\n', stdout);
13455}
13456
13457static void
13458display_sparc_hwcaps2 (int mask)
13459{
13460  if (mask)
13461    {
13462      int first = 1;
13463
13464      if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
13465	fputs ("fjathplus", stdout), first = 0;
13466      if (mask & ELF_SPARC_HWCAP2_VIS3B)
13467	printf ("%svis3b", first ? "" : "|"), first = 0;
13468      if (mask & ELF_SPARC_HWCAP2_ADP)
13469	printf ("%sadp", first ? "" : "|"), first = 0;
13470      if (mask & ELF_SPARC_HWCAP2_SPARC5)
13471	printf ("%ssparc5", first ? "" : "|"), first = 0;
13472      if (mask & ELF_SPARC_HWCAP2_MWAIT)
13473	printf ("%smwait", first ? "" : "|"), first = 0;
13474      if (mask & ELF_SPARC_HWCAP2_XMPMUL)
13475	printf ("%sxmpmul", first ? "" : "|"), first = 0;
13476      if (mask & ELF_SPARC_HWCAP2_XMONT)
13477	printf ("%sxmont2", first ? "" : "|"), first = 0;
13478      if (mask & ELF_SPARC_HWCAP2_NSEC)
13479	printf ("%snsec", first ? "" : "|"), first = 0;
13480      if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
13481	printf ("%sfjathhpc", first ? "" : "|"), first = 0;
13482      if (mask & ELF_SPARC_HWCAP2_FJDES)
13483	printf ("%sfjdes", first ? "" : "|"), first = 0;
13484      if (mask & ELF_SPARC_HWCAP2_FJAES)
13485	printf ("%sfjaes", first ? "" : "|"), first = 0;
13486    }
13487  else
13488    fputc ('0', stdout);
13489  fputc ('\n', stdout);
13490}
13491
13492static unsigned char *
13493display_sparc_gnu_attribute (unsigned char * p,
13494			     int tag,
13495			     const unsigned char * const end)
13496{
13497  unsigned int len;
13498  int val;
13499
13500  if (tag == Tag_GNU_Sparc_HWCAPS)
13501    {
13502      val = read_uleb128 (p, &len, end);
13503      p += len;
13504      printf ("  Tag_GNU_Sparc_HWCAPS: ");
13505      display_sparc_hwcaps (val);
13506      return p;
13507    }
13508  if (tag == Tag_GNU_Sparc_HWCAPS2)
13509    {
13510      val = read_uleb128 (p, &len, end);
13511      p += len;
13512      printf ("  Tag_GNU_Sparc_HWCAPS2: ");
13513      display_sparc_hwcaps2 (val);
13514      return p;
13515    }
13516
13517  return display_tag_value (tag, p, end);
13518}
13519
13520static void
13521print_mips_fp_abi_value (int val)
13522{
13523  switch (val)
13524    {
13525    case Val_GNU_MIPS_ABI_FP_ANY:
13526      printf (_("Hard or soft float\n"));
13527      break;
13528    case Val_GNU_MIPS_ABI_FP_DOUBLE:
13529      printf (_("Hard float (double precision)\n"));
13530      break;
13531    case Val_GNU_MIPS_ABI_FP_SINGLE:
13532      printf (_("Hard float (single precision)\n"));
13533      break;
13534    case Val_GNU_MIPS_ABI_FP_SOFT:
13535      printf (_("Soft float\n"));
13536      break;
13537    case Val_GNU_MIPS_ABI_FP_OLD_64:
13538      printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
13539      break;
13540    case Val_GNU_MIPS_ABI_FP_XX:
13541      printf (_("Hard float (32-bit CPU, Any FPU)\n"));
13542      break;
13543    case Val_GNU_MIPS_ABI_FP_64:
13544      printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
13545      break;
13546    case Val_GNU_MIPS_ABI_FP_64A:
13547      printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
13548      break;
13549    case Val_GNU_MIPS_ABI_FP_NAN2008:
13550      printf (_("NaN 2008 compatibility\n"));
13551      break;
13552    default:
13553      printf ("??? (%d)\n", val);
13554      break;
13555    }
13556}
13557
13558static unsigned char *
13559display_mips_gnu_attribute (unsigned char * p,
13560			    int tag,
13561			    const unsigned char * const end)
13562{
13563  if (tag == Tag_GNU_MIPS_ABI_FP)
13564    {
13565      unsigned int len;
13566      int val;
13567
13568      val = read_uleb128 (p, &len, end);
13569      p += len;
13570      printf ("  Tag_GNU_MIPS_ABI_FP: ");
13571
13572      print_mips_fp_abi_value (val);
13573
13574      return p;
13575   }
13576
13577  if (tag == Tag_GNU_MIPS_ABI_MSA)
13578    {
13579      unsigned int len;
13580      int val;
13581
13582      val = read_uleb128 (p, &len, end);
13583      p += len;
13584      printf ("  Tag_GNU_MIPS_ABI_MSA: ");
13585
13586      switch (val)
13587	{
13588	case Val_GNU_MIPS_ABI_MSA_ANY:
13589	  printf (_("Any MSA or not\n"));
13590	  break;
13591	case Val_GNU_MIPS_ABI_MSA_128:
13592	  printf (_("128-bit MSA\n"));
13593	  break;
13594	default:
13595	  printf ("??? (%d)\n", val);
13596	  break;
13597	}
13598      return p;
13599    }
13600
13601  return display_tag_value (tag & 1, p, end);
13602}
13603
13604static unsigned char *
13605display_tic6x_attribute (unsigned char * p,
13606			 const unsigned char * const end)
13607{
13608  int tag;
13609  unsigned int len;
13610  int val;
13611
13612  tag = read_uleb128 (p, &len, end);
13613  p += len;
13614
13615  switch (tag)
13616    {
13617    case Tag_ISA:
13618      val = read_uleb128 (p, &len, end);
13619      p += len;
13620      printf ("  Tag_ISA: ");
13621
13622      switch (val)
13623	{
13624	case C6XABI_Tag_ISA_none:
13625	  printf (_("None\n"));
13626	  break;
13627	case C6XABI_Tag_ISA_C62X:
13628	  printf ("C62x\n");
13629	  break;
13630	case C6XABI_Tag_ISA_C67X:
13631	  printf ("C67x\n");
13632	  break;
13633	case C6XABI_Tag_ISA_C67XP:
13634	  printf ("C67x+\n");
13635	  break;
13636	case C6XABI_Tag_ISA_C64X:
13637	  printf ("C64x\n");
13638	  break;
13639	case C6XABI_Tag_ISA_C64XP:
13640	  printf ("C64x+\n");
13641	  break;
13642	case C6XABI_Tag_ISA_C674X:
13643	  printf ("C674x\n");
13644	  break;
13645	default:
13646	  printf ("??? (%d)\n", val);
13647	  break;
13648	}
13649      return p;
13650
13651    case Tag_ABI_wchar_t:
13652      val = read_uleb128 (p, &len, end);
13653      p += len;
13654      printf ("  Tag_ABI_wchar_t: ");
13655      switch (val)
13656	{
13657	case 0:
13658	  printf (_("Not used\n"));
13659	  break;
13660	case 1:
13661	  printf (_("2 bytes\n"));
13662	  break;
13663	case 2:
13664	  printf (_("4 bytes\n"));
13665	  break;
13666	default:
13667	  printf ("??? (%d)\n", val);
13668	  break;
13669	}
13670      return p;
13671
13672    case Tag_ABI_stack_align_needed:
13673      val = read_uleb128 (p, &len, end);
13674      p += len;
13675      printf ("  Tag_ABI_stack_align_needed: ");
13676      switch (val)
13677	{
13678	case 0:
13679	  printf (_("8-byte\n"));
13680	  break;
13681	case 1:
13682	  printf (_("16-byte\n"));
13683	  break;
13684	default:
13685	  printf ("??? (%d)\n", val);
13686	  break;
13687	}
13688      return p;
13689
13690    case Tag_ABI_stack_align_preserved:
13691      val = read_uleb128 (p, &len, end);
13692      p += len;
13693      printf ("  Tag_ABI_stack_align_preserved: ");
13694      switch (val)
13695	{
13696	case 0:
13697	  printf (_("8-byte\n"));
13698	  break;
13699	case 1:
13700	  printf (_("16-byte\n"));
13701	  break;
13702	default:
13703	  printf ("??? (%d)\n", val);
13704	  break;
13705	}
13706      return p;
13707
13708    case Tag_ABI_DSBT:
13709      val = read_uleb128 (p, &len, end);
13710      p += len;
13711      printf ("  Tag_ABI_DSBT: ");
13712      switch (val)
13713	{
13714	case 0:
13715	  printf (_("DSBT addressing not used\n"));
13716	  break;
13717	case 1:
13718	  printf (_("DSBT addressing used\n"));
13719	  break;
13720	default:
13721	  printf ("??? (%d)\n", val);
13722	  break;
13723	}
13724      return p;
13725
13726    case Tag_ABI_PID:
13727      val = read_uleb128 (p, &len, end);
13728      p += len;
13729      printf ("  Tag_ABI_PID: ");
13730      switch (val)
13731	{
13732	case 0:
13733	  printf (_("Data addressing position-dependent\n"));
13734	  break;
13735	case 1:
13736	  printf (_("Data addressing position-independent, GOT near DP\n"));
13737	  break;
13738	case 2:
13739	  printf (_("Data addressing position-independent, GOT far from DP\n"));
13740	  break;
13741	default:
13742	  printf ("??? (%d)\n", val);
13743	  break;
13744	}
13745      return p;
13746
13747    case Tag_ABI_PIC:
13748      val = read_uleb128 (p, &len, end);
13749      p += len;
13750      printf ("  Tag_ABI_PIC: ");
13751      switch (val)
13752	{
13753	case 0:
13754	  printf (_("Code addressing position-dependent\n"));
13755	  break;
13756	case 1:
13757	  printf (_("Code addressing position-independent\n"));
13758	  break;
13759	default:
13760	  printf ("??? (%d)\n", val);
13761	  break;
13762	}
13763      return p;
13764
13765    case Tag_ABI_array_object_alignment:
13766      val = read_uleb128 (p, &len, end);
13767      p += len;
13768      printf ("  Tag_ABI_array_object_alignment: ");
13769      switch (val)
13770	{
13771	case 0:
13772	  printf (_("8-byte\n"));
13773	  break;
13774	case 1:
13775	  printf (_("4-byte\n"));
13776	  break;
13777	case 2:
13778	  printf (_("16-byte\n"));
13779	  break;
13780	default:
13781	  printf ("??? (%d)\n", val);
13782	  break;
13783	}
13784      return p;
13785
13786    case Tag_ABI_array_object_align_expected:
13787      val = read_uleb128 (p, &len, end);
13788      p += len;
13789      printf ("  Tag_ABI_array_object_align_expected: ");
13790      switch (val)
13791	{
13792	case 0:
13793	  printf (_("8-byte\n"));
13794	  break;
13795	case 1:
13796	  printf (_("4-byte\n"));
13797	  break;
13798	case 2:
13799	  printf (_("16-byte\n"));
13800	  break;
13801	default:
13802	  printf ("??? (%d)\n", val);
13803	  break;
13804	}
13805      return p;
13806
13807    case Tag_ABI_compatibility:
13808      {
13809	val = read_uleb128 (p, &len, end);
13810	p += len;
13811	printf ("  Tag_ABI_compatibility: ");
13812	printf (_("flag = %d, vendor = "), val);
13813	if (p < end - 1)
13814	  {
13815	    size_t maxlen = (end - p) - 1;
13816
13817	    print_symbol ((int) maxlen, (const char *) p);
13818	    p += strnlen ((char *) p, maxlen) + 1;
13819	  }
13820	else
13821	  {
13822	    printf (_("<corrupt>"));
13823	    p = (unsigned char *) end;
13824	  }
13825	putchar ('\n');
13826	return p;
13827      }
13828
13829    case Tag_ABI_conformance:
13830      {
13831	printf ("  Tag_ABI_conformance: \"");
13832	if (p < end - 1)
13833	  {
13834	    size_t maxlen = (end - p) - 1;
13835
13836	    print_symbol ((int) maxlen, (const char *) p);
13837	    p += strnlen ((char *) p, maxlen) + 1;
13838	  }
13839	else
13840	  {
13841	    printf (_("<corrupt>"));
13842	    p = (unsigned char *) end;
13843	  }
13844	printf ("\"\n");
13845	return p;
13846      }
13847    }
13848
13849  return display_tag_value (tag, p, end);
13850}
13851
13852static void
13853display_raw_attribute (unsigned char * p, unsigned char * end)
13854{
13855  unsigned long addr = 0;
13856  size_t bytes = end - p;
13857
13858  assert (end > p);
13859  while (bytes)
13860    {
13861      int j;
13862      int k;
13863      int lbytes = (bytes > 16 ? 16 : bytes);
13864
13865      printf ("  0x%8.8lx ", addr);
13866
13867      for (j = 0; j < 16; j++)
13868	{
13869	  if (j < lbytes)
13870	    printf ("%2.2x", p[j]);
13871	  else
13872	    printf ("  ");
13873
13874	  if ((j & 3) == 3)
13875	    printf (" ");
13876	}
13877
13878      for (j = 0; j < lbytes; j++)
13879	{
13880	  k = p[j];
13881	  if (k >= ' ' && k < 0x7f)
13882	    printf ("%c", k);
13883	  else
13884	    printf (".");
13885	}
13886
13887      putchar ('\n');
13888
13889      p  += lbytes;
13890      bytes -= lbytes;
13891      addr += lbytes;
13892    }
13893
13894  putchar ('\n');
13895}
13896
13897static unsigned char *
13898display_msp430x_attribute (unsigned char * p,
13899			   const unsigned char * const end)
13900{
13901  unsigned int len;
13902  int val;
13903  int tag;
13904
13905  tag = read_uleb128 (p, & len, end);
13906  p += len;
13907
13908  switch (tag)
13909    {
13910    case OFBA_MSPABI_Tag_ISA:
13911      val = read_uleb128 (p, &len, end);
13912      p += len;
13913      printf ("  Tag_ISA: ");
13914      switch (val)
13915	{
13916	case 0: printf (_("None\n")); break;
13917	case 1: printf (_("MSP430\n")); break;
13918	case 2: printf (_("MSP430X\n")); break;
13919	default: printf ("??? (%d)\n", val); break;
13920	}
13921      break;
13922
13923    case OFBA_MSPABI_Tag_Code_Model:
13924      val = read_uleb128 (p, &len, end);
13925      p += len;
13926      printf ("  Tag_Code_Model: ");
13927      switch (val)
13928	{
13929	case 0: printf (_("None\n")); break;
13930	case 1: printf (_("Small\n")); break;
13931	case 2: printf (_("Large\n")); break;
13932	default: printf ("??? (%d)\n", val); break;
13933	}
13934      break;
13935
13936    case OFBA_MSPABI_Tag_Data_Model:
13937      val = read_uleb128 (p, &len, end);
13938      p += len;
13939      printf ("  Tag_Data_Model: ");
13940      switch (val)
13941	{
13942	case 0: printf (_("None\n")); break;
13943	case 1: printf (_("Small\n")); break;
13944	case 2: printf (_("Large\n")); break;
13945	case 3: printf (_("Restricted Large\n")); break;
13946	default: printf ("??? (%d)\n", val); break;
13947	}
13948      break;
13949
13950    default:
13951      printf (_("  <unknown tag %d>: "), tag);
13952
13953      if (tag & 1)
13954	{
13955	  putchar ('"');
13956	  if (p < end - 1)
13957	    {
13958	      size_t maxlen = (end - p) - 1;
13959
13960	      print_symbol ((int) maxlen, (const char *) p);
13961	      p += strnlen ((char *) p, maxlen) + 1;
13962	    }
13963	  else
13964	    {
13965	      printf (_("<corrupt>"));
13966	      p = (unsigned char *) end;
13967	    }
13968	  printf ("\"\n");
13969	}
13970      else
13971	{
13972	  val = read_uleb128 (p, &len, end);
13973	  p += len;
13974	  printf ("%d (0x%x)\n", val, val);
13975	}
13976      break;
13977   }
13978
13979  assert (p <= end);
13980  return p;
13981}
13982
13983static int
13984process_attributes (FILE * file,
13985		    const char * public_name,
13986		    unsigned int proc_type,
13987		    unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
13988		    unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int, const unsigned char * const))
13989{
13990  Elf_Internal_Shdr * sect;
13991  unsigned i;
13992
13993  /* Find the section header so that we get the size.  */
13994  for (i = 0, sect = section_headers;
13995       i < elf_header.e_shnum;
13996       i++, sect++)
13997    {
13998      unsigned char * contents;
13999      unsigned char * p;
14000
14001      if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
14002	continue;
14003
14004      contents = (unsigned char *) get_data (NULL, file, sect->sh_offset, 1,
14005                                             sect->sh_size, _("attributes"));
14006      if (contents == NULL)
14007	continue;
14008
14009      p = contents;
14010      if (*p == 'A')
14011	{
14012	  bfd_vma section_len;
14013
14014	  section_len = sect->sh_size - 1;
14015	  p++;
14016
14017	  while (section_len > 0)
14018	    {
14019	      bfd_vma attr_len;
14020	      unsigned int namelen;
14021	      bfd_boolean public_section;
14022	      bfd_boolean gnu_section;
14023
14024	      if (section_len <= 4)
14025		{
14026		  error (_("Tag section ends prematurely\n"));
14027		  break;
14028		}
14029	      attr_len = byte_get (p, 4);
14030	      p += 4;
14031
14032	      if (attr_len > section_len)
14033		{
14034		  error (_("Bad attribute length (%u > %u)\n"),
14035			  (unsigned) attr_len, (unsigned) section_len);
14036		  attr_len = section_len;
14037		}
14038	      /* PR 17531: file: 001-101425-0.004  */
14039	      else if (attr_len < 5)
14040		{
14041		  error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
14042		  break;
14043		}
14044
14045	      section_len -= attr_len;
14046	      attr_len -= 4;
14047
14048	      namelen = strnlen ((char *) p, attr_len) + 1;
14049	      if (namelen == 0 || namelen >= attr_len)
14050		{
14051		  error (_("Corrupt attribute section name\n"));
14052		  break;
14053		}
14054
14055	      printf (_("Attribute Section: "));
14056	      print_symbol (INT_MAX, (const char *) p);
14057	      putchar ('\n');
14058
14059	      if (public_name && streq ((char *) p, public_name))
14060		public_section = TRUE;
14061	      else
14062		public_section = FALSE;
14063
14064	      if (streq ((char *) p, "gnu"))
14065		gnu_section = TRUE;
14066	      else
14067		gnu_section = FALSE;
14068
14069	      p += namelen;
14070	      attr_len -= namelen;
14071
14072	      while (attr_len > 0 && p < contents + sect->sh_size)
14073		{
14074		  int tag;
14075		  int val;
14076		  bfd_vma size;
14077		  unsigned char * end;
14078
14079		  /* PR binutils/17531: Safe handling of corrupt files.  */
14080		  if (attr_len < 6)
14081		    {
14082		      error (_("Unused bytes at end of section\n"));
14083		      section_len = 0;
14084		      break;
14085		    }
14086
14087		  tag = *(p++);
14088		  size = byte_get (p, 4);
14089		  if (size > attr_len)
14090		    {
14091		      error (_("Bad subsection length (%u > %u)\n"),
14092			      (unsigned) size, (unsigned) attr_len);
14093		      size = attr_len;
14094		    }
14095		  /* PR binutils/17531: Safe handling of corrupt files.  */
14096		  if (size < 6)
14097		    {
14098		      error (_("Bad subsection length (%u < 6)\n"),
14099			      (unsigned) size);
14100		      section_len = 0;
14101		      break;
14102		    }
14103
14104		  attr_len -= size;
14105		  end = p + size - 1;
14106		  assert (end <= contents + sect->sh_size);
14107		  p += 4;
14108
14109		  switch (tag)
14110		    {
14111		    case 1:
14112		      printf (_("File Attributes\n"));
14113		      break;
14114		    case 2:
14115		      printf (_("Section Attributes:"));
14116		      goto do_numlist;
14117		    case 3:
14118		      printf (_("Symbol Attributes:"));
14119		    do_numlist:
14120		      for (;;)
14121			{
14122			  unsigned int j;
14123
14124			  val = read_uleb128 (p, &j, end);
14125			  p += j;
14126			  if (val == 0)
14127			    break;
14128			  printf (" %d", val);
14129			}
14130		      printf ("\n");
14131		      break;
14132		    default:
14133		      printf (_("Unknown tag: %d\n"), tag);
14134		      public_section = FALSE;
14135		      break;
14136		    }
14137
14138		  if (public_section && display_pub_attribute != NULL)
14139		    {
14140		      while (p < end)
14141			p = display_pub_attribute (p, end);
14142		      assert (p <= end);
14143		    }
14144		  else if (gnu_section && display_proc_gnu_attribute != NULL)
14145		    {
14146		      while (p < end)
14147			p = display_gnu_attribute (p,
14148						   display_proc_gnu_attribute,
14149						   end);
14150		      assert (p <= end);
14151		    }
14152		  else if (p < end)
14153		    {
14154		      printf (_("  Unknown attribute:\n"));
14155		      display_raw_attribute (p, end);
14156		      p = end;
14157		    }
14158		  else
14159		    attr_len = 0;
14160		}
14161	    }
14162	}
14163      else
14164	printf (_("Unknown format '%c' (%d)\n"), *p, *p);
14165
14166      free (contents);
14167    }
14168  return 1;
14169}
14170
14171static int
14172process_arm_specific (FILE * file)
14173{
14174  return process_attributes (file, "aeabi", SHT_ARM_ATTRIBUTES,
14175			     display_arm_attribute, NULL);
14176}
14177
14178static int
14179process_power_specific (FILE * file)
14180{
14181  return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
14182			     display_power_gnu_attribute);
14183}
14184
14185static int
14186process_s390_specific (FILE * file)
14187{
14188  return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
14189			     display_s390_gnu_attribute);
14190}
14191
14192static int
14193process_sparc_specific (FILE * file)
14194{
14195  return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
14196			     display_sparc_gnu_attribute);
14197}
14198
14199static int
14200process_tic6x_specific (FILE * file)
14201{
14202  return process_attributes (file, "c6xabi", SHT_C6000_ATTRIBUTES,
14203			     display_tic6x_attribute, NULL);
14204}
14205
14206static int
14207process_msp430x_specific (FILE * file)
14208{
14209  return process_attributes (file, "mspabi", SHT_MSP430_ATTRIBUTES,
14210			     display_msp430x_attribute, NULL);
14211}
14212
14213/* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
14214   Print the Address, Access and Initial fields of an entry at VMA ADDR
14215   and return the VMA of the next entry, or -1 if there was a problem.
14216   Does not read from DATA_END or beyond.  */
14217
14218static bfd_vma
14219print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
14220		      unsigned char * data_end)
14221{
14222  printf ("  ");
14223  print_vma (addr, LONG_HEX);
14224  printf (" ");
14225  if (addr < pltgot + 0xfff0)
14226    printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
14227  else
14228    printf ("%10s", "");
14229  printf (" ");
14230  if (data == NULL)
14231    printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
14232  else
14233    {
14234      bfd_vma entry;
14235      unsigned char * from = data + addr - pltgot;
14236
14237      if (from + (is_32bit_elf ? 4 : 8) > data_end)
14238	{
14239	  warn (_("MIPS GOT entry extends beyond the end of available data\n"));
14240	  printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
14241	  return (bfd_vma) -1;
14242	}
14243      else
14244	{
14245	  entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
14246	  print_vma (entry, LONG_HEX);
14247	}
14248    }
14249  return addr + (is_32bit_elf ? 4 : 8);
14250}
14251
14252/* DATA points to the contents of a MIPS PLT GOT that starts at VMA
14253   PLTGOT.  Print the Address and Initial fields of an entry at VMA
14254   ADDR and return the VMA of the next entry.  */
14255
14256static bfd_vma
14257print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
14258{
14259  printf ("  ");
14260  print_vma (addr, LONG_HEX);
14261  printf (" ");
14262  if (data == NULL)
14263    printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
14264  else
14265    {
14266      bfd_vma entry;
14267
14268      entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
14269      print_vma (entry, LONG_HEX);
14270    }
14271  return addr + (is_32bit_elf ? 4 : 8);
14272}
14273
14274static void
14275print_mips_ases (unsigned int mask)
14276{
14277  if (mask & AFL_ASE_DSP)
14278    fputs ("\n\tDSP ASE", stdout);
14279  if (mask & AFL_ASE_DSPR2)
14280    fputs ("\n\tDSP R2 ASE", stdout);
14281  if (mask & AFL_ASE_DSPR3)
14282    fputs ("\n\tDSP R3 ASE", stdout);
14283  if (mask & AFL_ASE_EVA)
14284    fputs ("\n\tEnhanced VA Scheme", stdout);
14285  if (mask & AFL_ASE_MCU)
14286    fputs ("\n\tMCU (MicroController) ASE", stdout);
14287  if (mask & AFL_ASE_MDMX)
14288    fputs ("\n\tMDMX ASE", stdout);
14289  if (mask & AFL_ASE_MIPS3D)
14290    fputs ("\n\tMIPS-3D ASE", stdout);
14291  if (mask & AFL_ASE_MT)
14292    fputs ("\n\tMT ASE", stdout);
14293  if (mask & AFL_ASE_SMARTMIPS)
14294    fputs ("\n\tSmartMIPS ASE", stdout);
14295  if (mask & AFL_ASE_VIRT)
14296    fputs ("\n\tVZ ASE", stdout);
14297  if (mask & AFL_ASE_MSA)
14298    fputs ("\n\tMSA ASE", stdout);
14299  if (mask & AFL_ASE_MIPS16)
14300    fputs ("\n\tMIPS16 ASE", stdout);
14301  if (mask & AFL_ASE_MICROMIPS)
14302    fputs ("\n\tMICROMIPS ASE", stdout);
14303  if (mask & AFL_ASE_XPA)
14304    fputs ("\n\tXPA ASE", stdout);
14305  if (mask == 0)
14306    fprintf (stdout, "\n\t%s", _("None"));
14307  else if ((mask & ~AFL_ASE_MASK) != 0)
14308    fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
14309}
14310
14311static void
14312print_mips_isa_ext (unsigned int isa_ext)
14313{
14314  switch (isa_ext)
14315    {
14316    case 0:
14317      fputs (_("None"), stdout);
14318      break;
14319    case AFL_EXT_XLR:
14320      fputs ("RMI XLR", stdout);
14321      break;
14322    case AFL_EXT_OCTEON3:
14323      fputs ("Cavium Networks Octeon3", stdout);
14324      break;
14325    case AFL_EXT_OCTEON2:
14326      fputs ("Cavium Networks Octeon2", stdout);
14327      break;
14328    case AFL_EXT_OCTEONP:
14329      fputs ("Cavium Networks OcteonP", stdout);
14330      break;
14331    case AFL_EXT_LOONGSON_3A:
14332      fputs ("Loongson 3A", stdout);
14333      break;
14334    case AFL_EXT_OCTEON:
14335      fputs ("Cavium Networks Octeon", stdout);
14336      break;
14337    case AFL_EXT_5900:
14338      fputs ("Toshiba R5900", stdout);
14339      break;
14340    case AFL_EXT_4650:
14341      fputs ("MIPS R4650", stdout);
14342      break;
14343    case AFL_EXT_4010:
14344      fputs ("LSI R4010", stdout);
14345      break;
14346    case AFL_EXT_4100:
14347      fputs ("NEC VR4100", stdout);
14348      break;
14349    case AFL_EXT_3900:
14350      fputs ("Toshiba R3900", stdout);
14351      break;
14352    case AFL_EXT_10000:
14353      fputs ("MIPS R10000", stdout);
14354      break;
14355    case AFL_EXT_SB1:
14356      fputs ("Broadcom SB-1", stdout);
14357      break;
14358    case AFL_EXT_4111:
14359      fputs ("NEC VR4111/VR4181", stdout);
14360      break;
14361    case AFL_EXT_4120:
14362      fputs ("NEC VR4120", stdout);
14363      break;
14364    case AFL_EXT_5400:
14365      fputs ("NEC VR5400", stdout);
14366      break;
14367    case AFL_EXT_5500:
14368      fputs ("NEC VR5500", stdout);
14369      break;
14370    case AFL_EXT_LOONGSON_2E:
14371      fputs ("ST Microelectronics Loongson 2E", stdout);
14372      break;
14373    case AFL_EXT_LOONGSON_2F:
14374      fputs ("ST Microelectronics Loongson 2F", stdout);
14375      break;
14376    default:
14377      fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
14378    }
14379}
14380
14381static int
14382get_mips_reg_size (int reg_size)
14383{
14384  return (reg_size == AFL_REG_NONE) ? 0
14385	 : (reg_size == AFL_REG_32) ? 32
14386	 : (reg_size == AFL_REG_64) ? 64
14387	 : (reg_size == AFL_REG_128) ? 128
14388	 : -1;
14389}
14390
14391static int
14392process_mips_specific (FILE * file)
14393{
14394  Elf_Internal_Dyn * entry;
14395  Elf_Internal_Shdr *sect = NULL;
14396  size_t liblist_offset = 0;
14397  size_t liblistno = 0;
14398  size_t conflictsno = 0;
14399  size_t options_offset = 0;
14400  size_t conflicts_offset = 0;
14401  size_t pltrelsz = 0;
14402  size_t pltrel = 0;
14403  bfd_vma pltgot = 0;
14404  bfd_vma mips_pltgot = 0;
14405  bfd_vma jmprel = 0;
14406  bfd_vma local_gotno = 0;
14407  bfd_vma gotsym = 0;
14408  bfd_vma symtabno = 0;
14409
14410  process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
14411		      display_mips_gnu_attribute);
14412
14413  sect = find_section (".MIPS.abiflags");
14414
14415  if (sect != NULL)
14416    {
14417      Elf_External_ABIFlags_v0 *abiflags_ext;
14418      Elf_Internal_ABIFlags_v0 abiflags_in;
14419
14420      if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
14421	fputs ("\nCorrupt ABI Flags section.\n", stdout);
14422      else
14423	{
14424	  abiflags_ext = get_data (NULL, file, sect->sh_offset, 1,
14425				   sect->sh_size, _("MIPS ABI Flags section"));
14426	  if (abiflags_ext)
14427	    {
14428	      abiflags_in.version = BYTE_GET (abiflags_ext->version);
14429	      abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
14430	      abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
14431	      abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
14432	      abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
14433	      abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
14434	      abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
14435	      abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
14436	      abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
14437	      abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
14438	      abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
14439
14440	      printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
14441	      printf ("\nISA: MIPS%d", abiflags_in.isa_level);
14442	      if (abiflags_in.isa_rev > 1)
14443		printf ("r%d", abiflags_in.isa_rev);
14444	      printf ("\nGPR size: %d",
14445		      get_mips_reg_size (abiflags_in.gpr_size));
14446	      printf ("\nCPR1 size: %d",
14447		      get_mips_reg_size (abiflags_in.cpr1_size));
14448	      printf ("\nCPR2 size: %d",
14449		      get_mips_reg_size (abiflags_in.cpr2_size));
14450	      fputs ("\nFP ABI: ", stdout);
14451	      print_mips_fp_abi_value (abiflags_in.fp_abi);
14452	      fputs ("ISA Extension: ", stdout);
14453	      print_mips_isa_ext (abiflags_in.isa_ext);
14454	      fputs ("\nASEs:", stdout);
14455	      print_mips_ases (abiflags_in.ases);
14456	      printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
14457	      printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
14458	      fputc ('\n', stdout);
14459	      free (abiflags_ext);
14460	    }
14461	}
14462    }
14463
14464  /* We have a lot of special sections.  Thanks SGI!  */
14465  if (dynamic_section == NULL)
14466    /* No information available.  */
14467    return 0;
14468
14469  for (entry = dynamic_section;
14470       /* PR 17531 file: 012-50589-0.004.  */
14471       entry < dynamic_section + dynamic_nent && entry->d_tag != DT_NULL;
14472       ++entry)
14473    switch (entry->d_tag)
14474      {
14475      case DT_MIPS_LIBLIST:
14476	liblist_offset
14477	  = offset_from_vma (file, entry->d_un.d_val,
14478			     liblistno * sizeof (Elf32_External_Lib));
14479	break;
14480      case DT_MIPS_LIBLISTNO:
14481	liblistno = entry->d_un.d_val;
14482	break;
14483      case DT_MIPS_OPTIONS:
14484	options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
14485	break;
14486      case DT_MIPS_CONFLICT:
14487	conflicts_offset
14488	  = offset_from_vma (file, entry->d_un.d_val,
14489			     conflictsno * sizeof (Elf32_External_Conflict));
14490	break;
14491      case DT_MIPS_CONFLICTNO:
14492	conflictsno = entry->d_un.d_val;
14493	break;
14494      case DT_PLTGOT:
14495	pltgot = entry->d_un.d_ptr;
14496	break;
14497      case DT_MIPS_LOCAL_GOTNO:
14498	local_gotno = entry->d_un.d_val;
14499	break;
14500      case DT_MIPS_GOTSYM:
14501	gotsym = entry->d_un.d_val;
14502	break;
14503      case DT_MIPS_SYMTABNO:
14504	symtabno = entry->d_un.d_val;
14505	break;
14506      case DT_MIPS_PLTGOT:
14507	mips_pltgot = entry->d_un.d_ptr;
14508	break;
14509      case DT_PLTREL:
14510	pltrel = entry->d_un.d_val;
14511	break;
14512      case DT_PLTRELSZ:
14513	pltrelsz = entry->d_un.d_val;
14514	break;
14515      case DT_JMPREL:
14516	jmprel = entry->d_un.d_ptr;
14517	break;
14518      default:
14519	break;
14520      }
14521
14522  if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
14523    {
14524      Elf32_External_Lib * elib;
14525      size_t cnt;
14526
14527      elib = (Elf32_External_Lib *) get_data (NULL, file, liblist_offset,
14528                                              liblistno,
14529                                              sizeof (Elf32_External_Lib),
14530                                              _("liblist section data"));
14531      if (elib)
14532	{
14533	  printf (_("\nSection '.liblist' contains %lu entries:\n"),
14534		  (unsigned long) liblistno);
14535	  fputs (_("     Library              Time Stamp          Checksum   Version Flags\n"),
14536		 stdout);
14537
14538	  for (cnt = 0; cnt < liblistno; ++cnt)
14539	    {
14540	      Elf32_Lib liblist;
14541	      time_t atime;
14542	      char timebuf[128];
14543	      struct tm * tmp;
14544
14545	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
14546	      atime = BYTE_GET (elib[cnt].l_time_stamp);
14547	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
14548	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
14549	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
14550
14551	      tmp = gmtime (&atime);
14552	      snprintf (timebuf, sizeof (timebuf),
14553			"%04u-%02u-%02uT%02u:%02u:%02u",
14554			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
14555			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
14556
14557	      printf ("%3lu: ", (unsigned long) cnt);
14558	      if (VALID_DYNAMIC_NAME (liblist.l_name))
14559		print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
14560	      else
14561		printf (_("<corrupt: %9ld>"), liblist.l_name);
14562	      printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
14563		      liblist.l_version);
14564
14565	      if (liblist.l_flags == 0)
14566		puts (_(" NONE"));
14567	      else
14568		{
14569		  static const struct
14570		  {
14571		    const char * name;
14572		    int bit;
14573		  }
14574		  l_flags_vals[] =
14575		  {
14576		    { " EXACT_MATCH", LL_EXACT_MATCH },
14577		    { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
14578		    { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
14579		    { " EXPORTS", LL_EXPORTS },
14580		    { " DELAY_LOAD", LL_DELAY_LOAD },
14581		    { " DELTA", LL_DELTA }
14582		  };
14583		  int flags = liblist.l_flags;
14584		  size_t fcnt;
14585
14586		  for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
14587		    if ((flags & l_flags_vals[fcnt].bit) != 0)
14588		      {
14589			fputs (l_flags_vals[fcnt].name, stdout);
14590			flags ^= l_flags_vals[fcnt].bit;
14591		      }
14592		  if (flags != 0)
14593		    printf (" %#x", (unsigned int) flags);
14594
14595		  puts ("");
14596		}
14597	    }
14598
14599	  free (elib);
14600	}
14601    }
14602
14603  if (options_offset != 0)
14604    {
14605      Elf_External_Options * eopt;
14606      Elf_Internal_Options * iopt;
14607      Elf_Internal_Options * option;
14608      size_t offset;
14609      int cnt;
14610      sect = section_headers;
14611
14612      /* Find the section header so that we get the size.  */
14613      sect = find_section_by_type (SHT_MIPS_OPTIONS);
14614      /* PR 17533 file: 012-277276-0.004.  */
14615      if (sect == NULL)
14616	{
14617	  error (_("No MIPS_OPTIONS header found\n"));
14618	  return 0;
14619	}
14620
14621      eopt = (Elf_External_Options *) get_data (NULL, file, options_offset, 1,
14622                                                sect->sh_size, _("options"));
14623      if (eopt)
14624	{
14625	  iopt = (Elf_Internal_Options *)
14626              cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (* iopt));
14627	  if (iopt == NULL)
14628	    {
14629	      error (_("Out of memory allocatinf space for MIPS options\n"));
14630	      return 0;
14631	    }
14632
14633	  offset = cnt = 0;
14634	  option = iopt;
14635
14636	  while (offset <= sect->sh_size - sizeof (* eopt))
14637	    {
14638	      Elf_External_Options * eoption;
14639
14640	      eoption = (Elf_External_Options *) ((char *) eopt + offset);
14641
14642	      option->kind = BYTE_GET (eoption->kind);
14643	      option->size = BYTE_GET (eoption->size);
14644	      option->section = BYTE_GET (eoption->section);
14645	      option->info = BYTE_GET (eoption->info);
14646
14647	      /* PR 17531: file: ffa0fa3b.  */
14648	      if (option->size < sizeof (* eopt)
14649		  || offset + option->size > sect->sh_size)
14650		{
14651		  error (_("Invalid size (%u) for MIPS option\n"), option->size);
14652		  return 0;
14653		}
14654	      offset += option->size;
14655
14656	      ++option;
14657	      ++cnt;
14658	    }
14659
14660	  printf (_("\nSection '%s' contains %d entries:\n"),
14661		  printable_section_name (sect), cnt);
14662
14663	  option = iopt;
14664	  offset = 0;
14665
14666	  while (cnt-- > 0)
14667	    {
14668	      size_t len;
14669
14670	      switch (option->kind)
14671		{
14672		case ODK_NULL:
14673		  /* This shouldn't happen.  */
14674		  printf (" NULL       %d %lx", option->section, option->info);
14675		  break;
14676		case ODK_REGINFO:
14677		  printf (" REGINFO    ");
14678		  if (elf_header.e_machine == EM_MIPS)
14679		    {
14680		      /* 32bit form.  */
14681		      Elf32_External_RegInfo * ereg;
14682		      Elf32_RegInfo reginfo;
14683
14684		      ereg = (Elf32_External_RegInfo *) (option + 1);
14685		      reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
14686		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
14687		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
14688		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
14689		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
14690		      reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
14691
14692		      printf ("GPR %08lx  GP 0x%lx\n",
14693			      reginfo.ri_gprmask,
14694			      (unsigned long) reginfo.ri_gp_value);
14695		      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
14696			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
14697			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
14698		    }
14699		  else
14700		    {
14701		      /* 64 bit form.  */
14702		      Elf64_External_RegInfo * ereg;
14703		      Elf64_Internal_RegInfo reginfo;
14704
14705		      ereg = (Elf64_External_RegInfo *) (option + 1);
14706		      reginfo.ri_gprmask    = BYTE_GET (ereg->ri_gprmask);
14707		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
14708		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
14709		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
14710		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
14711		      reginfo.ri_gp_value   = BYTE_GET (ereg->ri_gp_value);
14712
14713		      printf ("GPR %08lx  GP 0x",
14714			      reginfo.ri_gprmask);
14715		      printf_vma (reginfo.ri_gp_value);
14716		      printf ("\n");
14717
14718		      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
14719			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
14720			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
14721		    }
14722		  ++option;
14723		  continue;
14724		case ODK_EXCEPTIONS:
14725		  fputs (" EXCEPTIONS fpe_min(", stdout);
14726		  process_mips_fpe_exception (option->info & OEX_FPU_MIN);
14727		  fputs (") fpe_max(", stdout);
14728		  process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
14729		  fputs (")", stdout);
14730
14731		  if (option->info & OEX_PAGE0)
14732		    fputs (" PAGE0", stdout);
14733		  if (option->info & OEX_SMM)
14734		    fputs (" SMM", stdout);
14735		  if (option->info & OEX_FPDBUG)
14736		    fputs (" FPDBUG", stdout);
14737		  if (option->info & OEX_DISMISS)
14738		    fputs (" DISMISS", stdout);
14739		  break;
14740		case ODK_PAD:
14741		  fputs (" PAD       ", stdout);
14742		  if (option->info & OPAD_PREFIX)
14743		    fputs (" PREFIX", stdout);
14744		  if (option->info & OPAD_POSTFIX)
14745		    fputs (" POSTFIX", stdout);
14746		  if (option->info & OPAD_SYMBOL)
14747		    fputs (" SYMBOL", stdout);
14748		  break;
14749		case ODK_HWPATCH:
14750		  fputs (" HWPATCH   ", stdout);
14751		  if (option->info & OHW_R4KEOP)
14752		    fputs (" R4KEOP", stdout);
14753		  if (option->info & OHW_R8KPFETCH)
14754		    fputs (" R8KPFETCH", stdout);
14755		  if (option->info & OHW_R5KEOP)
14756		    fputs (" R5KEOP", stdout);
14757		  if (option->info & OHW_R5KCVTL)
14758		    fputs (" R5KCVTL", stdout);
14759		  break;
14760		case ODK_FILL:
14761		  fputs (" FILL       ", stdout);
14762		  /* XXX Print content of info word?  */
14763		  break;
14764		case ODK_TAGS:
14765		  fputs (" TAGS       ", stdout);
14766		  /* XXX Print content of info word?  */
14767		  break;
14768		case ODK_HWAND:
14769		  fputs (" HWAND     ", stdout);
14770		  if (option->info & OHWA0_R4KEOP_CHECKED)
14771		    fputs (" R4KEOP_CHECKED", stdout);
14772		  if (option->info & OHWA0_R4KEOP_CLEAN)
14773		    fputs (" R4KEOP_CLEAN", stdout);
14774		  break;
14775		case ODK_HWOR:
14776		  fputs (" HWOR      ", stdout);
14777		  if (option->info & OHWA0_R4KEOP_CHECKED)
14778		    fputs (" R4KEOP_CHECKED", stdout);
14779		  if (option->info & OHWA0_R4KEOP_CLEAN)
14780		    fputs (" R4KEOP_CLEAN", stdout);
14781		  break;
14782		case ODK_GP_GROUP:
14783		  printf (" GP_GROUP  %#06lx  self-contained %#06lx",
14784			  option->info & OGP_GROUP,
14785			  (option->info & OGP_SELF) >> 16);
14786		  break;
14787		case ODK_IDENT:
14788		  printf (" IDENT     %#06lx  self-contained %#06lx",
14789			  option->info & OGP_GROUP,
14790			  (option->info & OGP_SELF) >> 16);
14791		  break;
14792		default:
14793		  /* This shouldn't happen.  */
14794		  printf (" %3d ???     %d %lx",
14795			  option->kind, option->section, option->info);
14796		  break;
14797		}
14798
14799	      len = sizeof (* eopt);
14800	      while (len < option->size)
14801		{
14802		  unsigned char datum = * ((unsigned char *) eopt + offset + len);
14803
14804		  if (ISPRINT (datum))
14805		    printf ("%c", datum);
14806		  else
14807		    printf ("\\%03o", datum);
14808		  len ++;
14809		}
14810	      fputs ("\n", stdout);
14811
14812	      offset += option->size;
14813	      ++option;
14814	    }
14815
14816	  free (eopt);
14817	}
14818    }
14819
14820  if (conflicts_offset != 0 && conflictsno != 0)
14821    {
14822      Elf32_Conflict * iconf;
14823      size_t cnt;
14824
14825      if (dynamic_symbols == NULL)
14826	{
14827	  error (_("conflict list found without a dynamic symbol table\n"));
14828	  return 0;
14829	}
14830
14831      iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
14832      if (iconf == NULL)
14833	{
14834	  error (_("Out of memory allocating space for dynamic conflicts\n"));
14835	  return 0;
14836	}
14837
14838      if (is_32bit_elf)
14839	{
14840	  Elf32_External_Conflict * econf32;
14841
14842	  econf32 = (Elf32_External_Conflict *)
14843              get_data (NULL, file, conflicts_offset, conflictsno,
14844                        sizeof (* econf32), _("conflict"));
14845	  if (!econf32)
14846	    return 0;
14847
14848	  for (cnt = 0; cnt < conflictsno; ++cnt)
14849	    iconf[cnt] = BYTE_GET (econf32[cnt]);
14850
14851	  free (econf32);
14852	}
14853      else
14854	{
14855	  Elf64_External_Conflict * econf64;
14856
14857	  econf64 = (Elf64_External_Conflict *)
14858              get_data (NULL, file, conflicts_offset, conflictsno,
14859                        sizeof (* econf64), _("conflict"));
14860	  if (!econf64)
14861	    return 0;
14862
14863	  for (cnt = 0; cnt < conflictsno; ++cnt)
14864	    iconf[cnt] = BYTE_GET (econf64[cnt]);
14865
14866	  free (econf64);
14867	}
14868
14869      printf (_("\nSection '.conflict' contains %lu entries:\n"),
14870	      (unsigned long) conflictsno);
14871      puts (_("  Num:    Index       Value  Name"));
14872
14873      for (cnt = 0; cnt < conflictsno; ++cnt)
14874	{
14875	  printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
14876
14877	  if (iconf[cnt] >= num_dynamic_syms)
14878	    printf (_("<corrupt symbol index>"));
14879	  else
14880	    {
14881	      Elf_Internal_Sym * psym;
14882
14883	      psym = & dynamic_symbols[iconf[cnt]];
14884	      print_vma (psym->st_value, FULL_HEX);
14885	      putchar (' ');
14886	      if (VALID_DYNAMIC_NAME (psym->st_name))
14887		print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
14888	      else
14889		printf (_("<corrupt: %14ld>"), psym->st_name);
14890	    }
14891	  putchar ('\n');
14892	}
14893
14894      free (iconf);
14895    }
14896
14897  if (pltgot != 0 && local_gotno != 0)
14898    {
14899      bfd_vma ent, local_end, global_end;
14900      size_t i, offset;
14901      unsigned char * data;
14902      unsigned char * data_end;
14903      int addr_size;
14904
14905      ent = pltgot;
14906      addr_size = (is_32bit_elf ? 4 : 8);
14907      local_end = pltgot + local_gotno * addr_size;
14908
14909      /* PR binutils/17533 file: 012-111227-0.004  */
14910      if (symtabno < gotsym)
14911	{
14912	  error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
14913		 (unsigned long) gotsym, (unsigned long) symtabno);
14914	  return 0;
14915	}
14916
14917      global_end = local_end + (symtabno - gotsym) * addr_size;
14918      /* PR 17531: file: 54c91a34.  */
14919      if (global_end < local_end)
14920	{
14921	  error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
14922	  return 0;
14923	}
14924
14925      offset = offset_from_vma (file, pltgot, global_end - pltgot);
14926      data = (unsigned char *) get_data (NULL, file, offset,
14927                                         global_end - pltgot, 1,
14928					 _("Global Offset Table data"));
14929      if (data == NULL)
14930	return 0;
14931      data_end = data + (global_end - pltgot);
14932
14933      printf (_("\nPrimary GOT:\n"));
14934      printf (_(" Canonical gp value: "));
14935      print_vma (pltgot + 0x7ff0, LONG_HEX);
14936      printf ("\n\n");
14937
14938      printf (_(" Reserved entries:\n"));
14939      printf (_("  %*s %10s %*s Purpose\n"),
14940	      addr_size * 2, _("Address"), _("Access"),
14941	      addr_size * 2, _("Initial"));
14942      ent = print_mips_got_entry (data, pltgot, ent, data_end);
14943      printf (_(" Lazy resolver\n"));
14944      if (ent == (bfd_vma) -1)
14945	goto got_print_fail;
14946      if (data
14947	  && (byte_get (data + ent - pltgot, addr_size)
14948	      >> (addr_size * 8 - 1)) != 0)
14949	{
14950	  ent = print_mips_got_entry (data, pltgot, ent, data_end);
14951	  printf (_(" Module pointer (GNU extension)\n"));
14952	  if (ent == (bfd_vma) -1)
14953	    goto got_print_fail;
14954	}
14955      printf ("\n");
14956
14957      if (ent < local_end)
14958	{
14959	  printf (_(" Local entries:\n"));
14960	  printf ("  %*s %10s %*s\n",
14961		  addr_size * 2, _("Address"), _("Access"),
14962		  addr_size * 2, _("Initial"));
14963	  while (ent < local_end)
14964	    {
14965	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
14966	      printf ("\n");
14967	      if (ent == (bfd_vma) -1)
14968		goto got_print_fail;
14969	    }
14970	  printf ("\n");
14971	}
14972
14973      if (gotsym < symtabno)
14974	{
14975	  int sym_width;
14976
14977	  printf (_(" Global entries:\n"));
14978	  printf ("  %*s %10s %*s %*s %-7s %3s %s\n",
14979		  addr_size * 2, _("Address"),
14980		  _("Access"),
14981		  addr_size * 2, _("Initial"),
14982		  addr_size * 2, _("Sym.Val."),
14983		  _("Type"),
14984		  /* Note for translators: "Ndx" = abbreviated form of "Index".  */
14985		  _("Ndx"), _("Name"));
14986
14987	  sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
14988
14989	  for (i = gotsym; i < symtabno; i++)
14990	    {
14991	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
14992	      printf (" ");
14993
14994	      if (dynamic_symbols == NULL)
14995		printf (_("<no dynamic symbols>"));
14996	      else if (i < num_dynamic_syms)
14997		{
14998		  Elf_Internal_Sym * psym = dynamic_symbols + i;
14999
15000		  print_vma (psym->st_value, LONG_HEX);
15001		  printf (" %-7s %3s ",
15002			  get_symbol_type (ELF_ST_TYPE (psym->st_info)),
15003			  get_symbol_index_type (psym->st_shndx));
15004
15005		  if (VALID_DYNAMIC_NAME (psym->st_name))
15006		    print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
15007		  else
15008		    printf (_("<corrupt: %14ld>"), psym->st_name);
15009		}
15010	      else
15011		printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
15012			(unsigned long) i);
15013
15014	      printf ("\n");
15015	      if (ent == (bfd_vma) -1)
15016		break;
15017	    }
15018	  printf ("\n");
15019	}
15020
15021    got_print_fail:
15022      if (data)
15023	free (data);
15024    }
15025
15026  if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
15027    {
15028      bfd_vma ent, end;
15029      size_t offset, rel_offset;
15030      unsigned long count, i;
15031      unsigned char * data;
15032      int addr_size, sym_width;
15033      Elf_Internal_Rela * rels;
15034
15035      rel_offset = offset_from_vma (file, jmprel, pltrelsz);
15036      if (pltrel == DT_RELA)
15037	{
15038	  if (!slurp_rela_relocs (file, rel_offset, pltrelsz, &rels, &count))
15039	    return 0;
15040	}
15041      else
15042	{
15043	  if (!slurp_rel_relocs (file, rel_offset, pltrelsz, &rels, &count))
15044	    return 0;
15045	}
15046
15047      ent = mips_pltgot;
15048      addr_size = (is_32bit_elf ? 4 : 8);
15049      end = mips_pltgot + (2 + count) * addr_size;
15050
15051      offset = offset_from_vma (file, mips_pltgot, end - mips_pltgot);
15052      data = (unsigned char *) get_data (NULL, file, offset, end - mips_pltgot,
15053                                         1, _("Procedure Linkage Table data"));
15054      if (data == NULL)
15055	return 0;
15056
15057      printf ("\nPLT GOT:\n\n");
15058      printf (_(" Reserved entries:\n"));
15059      printf (_("  %*s %*s Purpose\n"),
15060	      addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
15061      ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
15062      printf (_(" PLT lazy resolver\n"));
15063      ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
15064      printf (_(" Module pointer\n"));
15065      printf ("\n");
15066
15067      printf (_(" Entries:\n"));
15068      printf ("  %*s %*s %*s %-7s %3s %s\n",
15069	      addr_size * 2, _("Address"),
15070	      addr_size * 2, _("Initial"),
15071	      addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
15072      sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
15073      for (i = 0; i < count; i++)
15074	{
15075	  unsigned long idx = get_reloc_symindex (rels[i].r_info);
15076
15077	  ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
15078	  printf (" ");
15079
15080	  if (idx >= num_dynamic_syms)
15081	    printf (_("<corrupt symbol index: %lu>"), idx);
15082	  else
15083	    {
15084	      Elf_Internal_Sym * psym = dynamic_symbols + idx;
15085
15086	      print_vma (psym->st_value, LONG_HEX);
15087	      printf (" %-7s %3s ",
15088		      get_symbol_type (ELF_ST_TYPE (psym->st_info)),
15089		      get_symbol_index_type (psym->st_shndx));
15090	      if (VALID_DYNAMIC_NAME (psym->st_name))
15091		print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
15092	      else
15093		printf (_("<corrupt: %14ld>"), psym->st_name);
15094	    }
15095	  printf ("\n");
15096	}
15097      printf ("\n");
15098
15099      if (data)
15100	free (data);
15101      free (rels);
15102    }
15103
15104  return 1;
15105}
15106
15107static int
15108process_nds32_specific (FILE * file)
15109{
15110  Elf_Internal_Shdr *sect = NULL;
15111
15112  sect = find_section (".nds32_e_flags");
15113  if (sect != NULL)
15114    {
15115      unsigned int *flag;
15116
15117      printf ("\nNDS32 elf flags section:\n");
15118      flag = get_data (NULL, file, sect->sh_offset, 1,
15119		       sect->sh_size, _("NDS32 elf flags section"));
15120
15121      switch ((*flag) & 0x3)
15122	{
15123	case 0:
15124	  printf ("(VEC_SIZE):\tNo entry.\n");
15125	  break;
15126	case 1:
15127	  printf ("(VEC_SIZE):\t4 bytes\n");
15128	  break;
15129	case 2:
15130	  printf ("(VEC_SIZE):\t16 bytes\n");
15131	  break;
15132	case 3:
15133	  printf ("(VEC_SIZE):\treserved\n");
15134	  break;
15135	}
15136    }
15137
15138  return TRUE;
15139}
15140
15141static int
15142process_gnu_liblist (FILE * file)
15143{
15144  Elf_Internal_Shdr * section;
15145  Elf_Internal_Shdr * string_sec;
15146  Elf32_External_Lib * elib;
15147  char * strtab;
15148  size_t strtab_size;
15149  size_t cnt;
15150  unsigned i;
15151
15152  if (! do_arch)
15153    return 0;
15154
15155  for (i = 0, section = section_headers;
15156       i < elf_header.e_shnum;
15157       i++, section++)
15158    {
15159      switch (section->sh_type)
15160	{
15161	case SHT_GNU_LIBLIST:
15162	  if (section->sh_link >= elf_header.e_shnum)
15163	    break;
15164
15165	  elib = (Elf32_External_Lib *)
15166              get_data (NULL, file, section->sh_offset, 1, section->sh_size,
15167                        _("liblist section data"));
15168
15169	  if (elib == NULL)
15170	    break;
15171	  string_sec = section_headers + section->sh_link;
15172
15173	  strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
15174                                      string_sec->sh_size,
15175                                      _("liblist string table"));
15176	  if (strtab == NULL
15177	      || section->sh_entsize != sizeof (Elf32_External_Lib))
15178	    {
15179	      free (elib);
15180	      free (strtab);
15181	      break;
15182	    }
15183	  strtab_size = string_sec->sh_size;
15184
15185	  printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
15186		  printable_section_name (section),
15187		  (unsigned long) (section->sh_size / sizeof (Elf32_External_Lib)));
15188
15189	  puts (_("     Library              Time Stamp          Checksum   Version Flags"));
15190
15191	  for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
15192	       ++cnt)
15193	    {
15194	      Elf32_Lib liblist;
15195	      time_t atime;
15196	      char timebuf[128];
15197	      struct tm * tmp;
15198
15199	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
15200	      atime = BYTE_GET (elib[cnt].l_time_stamp);
15201	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
15202	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
15203	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
15204
15205	      tmp = gmtime (&atime);
15206	      snprintf (timebuf, sizeof (timebuf),
15207			"%04u-%02u-%02uT%02u:%02u:%02u",
15208			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
15209			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
15210
15211	      printf ("%3lu: ", (unsigned long) cnt);
15212	      if (do_wide)
15213		printf ("%-20s", liblist.l_name < strtab_size
15214			? strtab + liblist.l_name : _("<corrupt>"));
15215	      else
15216		printf ("%-20.20s", liblist.l_name < strtab_size
15217			? strtab + liblist.l_name : _("<corrupt>"));
15218	      printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
15219		      liblist.l_version, liblist.l_flags);
15220	    }
15221
15222	  free (elib);
15223	  free (strtab);
15224	}
15225    }
15226
15227  return 1;
15228}
15229
15230static const char *
15231get_note_type (unsigned e_type)
15232{
15233  static char buff[64];
15234
15235  if (elf_header.e_type == ET_CORE)
15236    switch (e_type)
15237      {
15238      case NT_AUXV:
15239	return _("NT_AUXV (auxiliary vector)");
15240      case NT_PRSTATUS:
15241	return _("NT_PRSTATUS (prstatus structure)");
15242      case NT_FPREGSET:
15243	return _("NT_FPREGSET (floating point registers)");
15244      case NT_PRPSINFO:
15245	return _("NT_PRPSINFO (prpsinfo structure)");
15246      case NT_TASKSTRUCT:
15247	return _("NT_TASKSTRUCT (task structure)");
15248      case NT_PRXFPREG:
15249	return _("NT_PRXFPREG (user_xfpregs structure)");
15250      case NT_PPC_VMX:
15251	return _("NT_PPC_VMX (ppc Altivec registers)");
15252      case NT_PPC_VSX:
15253	return _("NT_PPC_VSX (ppc VSX registers)");
15254      case NT_386_TLS:
15255	return _("NT_386_TLS (x86 TLS information)");
15256      case NT_386_IOPERM:
15257	return _("NT_386_IOPERM (x86 I/O permissions)");
15258      case NT_X86_XSTATE:
15259	return _("NT_X86_XSTATE (x86 XSAVE extended state)");
15260      case NT_S390_HIGH_GPRS:
15261	return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
15262      case NT_S390_TIMER:
15263	return _("NT_S390_TIMER (s390 timer register)");
15264      case NT_S390_TODCMP:
15265	return _("NT_S390_TODCMP (s390 TOD comparator register)");
15266      case NT_S390_TODPREG:
15267	return _("NT_S390_TODPREG (s390 TOD programmable register)");
15268      case NT_S390_CTRS:
15269	return _("NT_S390_CTRS (s390 control registers)");
15270      case NT_S390_PREFIX:
15271	return _("NT_S390_PREFIX (s390 prefix register)");
15272      case NT_S390_LAST_BREAK:
15273	return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
15274      case NT_S390_SYSTEM_CALL:
15275	return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
15276      case NT_S390_TDB:
15277	return _("NT_S390_TDB (s390 transaction diagnostic block)");
15278      case NT_S390_VXRS_LOW:
15279	return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
15280      case NT_S390_VXRS_HIGH:
15281	return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
15282      case NT_ARM_VFP:
15283	return _("NT_ARM_VFP (arm VFP registers)");
15284      case NT_ARM_TLS:
15285	return _("NT_ARM_TLS (AArch TLS registers)");
15286      case NT_ARM_HW_BREAK:
15287	return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
15288      case NT_ARM_HW_WATCH:
15289	return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
15290      case NT_PSTATUS:
15291	return _("NT_PSTATUS (pstatus structure)");
15292      case NT_FPREGS:
15293	return _("NT_FPREGS (floating point registers)");
15294      case NT_PSINFO:
15295	return _("NT_PSINFO (psinfo structure)");
15296      case NT_LWPSTATUS:
15297	return _("NT_LWPSTATUS (lwpstatus_t structure)");
15298      case NT_LWPSINFO:
15299	return _("NT_LWPSINFO (lwpsinfo_t structure)");
15300      case NT_WIN32PSTATUS:
15301	return _("NT_WIN32PSTATUS (win32_pstatus structure)");
15302      case NT_SIGINFO:
15303	return _("NT_SIGINFO (siginfo_t data)");
15304      case NT_FILE:
15305	return _("NT_FILE (mapped files)");
15306      default:
15307	break;
15308      }
15309  else
15310    switch (e_type)
15311      {
15312      case NT_VERSION:
15313	return _("NT_VERSION (version)");
15314      case NT_ARCH:
15315	return _("NT_ARCH (architecture)");
15316      default:
15317	break;
15318      }
15319
15320  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15321  return buff;
15322}
15323
15324static int
15325print_core_note (Elf_Internal_Note *pnote)
15326{
15327  unsigned int addr_size = is_32bit_elf ? 4 : 8;
15328  bfd_vma count, page_size;
15329  unsigned char *descdata, *filenames, *descend;
15330
15331  if (pnote->type != NT_FILE)
15332    return 1;
15333
15334#ifndef BFD64
15335  if (!is_32bit_elf)
15336    {
15337      printf (_("    Cannot decode 64-bit note in 32-bit build\n"));
15338      /* Still "successful".  */
15339      return 1;
15340    }
15341#endif
15342
15343  if (pnote->descsz < 2 * addr_size)
15344    {
15345      printf (_("    Malformed note - too short for header\n"));
15346      return 0;
15347    }
15348
15349  descdata = (unsigned char *) pnote->descdata;
15350  descend = descdata + pnote->descsz;
15351
15352  if (descdata[pnote->descsz - 1] != '\0')
15353    {
15354      printf (_("    Malformed note - does not end with \\0\n"));
15355      return 0;
15356    }
15357
15358  count = byte_get (descdata, addr_size);
15359  descdata += addr_size;
15360
15361  page_size = byte_get (descdata, addr_size);
15362  descdata += addr_size;
15363
15364  if (pnote->descsz < 2 * addr_size + count * 3 * addr_size)
15365    {
15366      printf (_("    Malformed note - too short for supplied file count\n"));
15367      return 0;
15368    }
15369
15370  printf (_("    Page size: "));
15371  print_vma (page_size, DEC);
15372  printf ("\n");
15373
15374  printf (_("    %*s%*s%*s\n"),
15375	  (int) (2 + 2 * addr_size), _("Start"),
15376	  (int) (4 + 2 * addr_size), _("End"),
15377	  (int) (4 + 2 * addr_size), _("Page Offset"));
15378  filenames = descdata + count * 3 * addr_size;
15379  while (count-- > 0)
15380    {
15381      bfd_vma start, end, file_ofs;
15382
15383      if (filenames == descend)
15384	{
15385	  printf (_("    Malformed note - filenames end too early\n"));
15386	  return 0;
15387	}
15388
15389      start = byte_get (descdata, addr_size);
15390      descdata += addr_size;
15391      end = byte_get (descdata, addr_size);
15392      descdata += addr_size;
15393      file_ofs = byte_get (descdata, addr_size);
15394      descdata += addr_size;
15395
15396      printf ("    ");
15397      print_vma (start, FULL_HEX);
15398      printf ("  ");
15399      print_vma (end, FULL_HEX);
15400      printf ("  ");
15401      print_vma (file_ofs, FULL_HEX);
15402      printf ("\n        %s\n", filenames);
15403
15404      filenames += 1 + strlen ((char *) filenames);
15405    }
15406
15407  return 1;
15408}
15409
15410static const char *
15411get_gnu_elf_note_type (unsigned e_type)
15412{
15413  static char buff[64];
15414
15415  switch (e_type)
15416    {
15417    case NT_GNU_ABI_TAG:
15418      return _("NT_GNU_ABI_TAG (ABI version tag)");
15419    case NT_GNU_HWCAP:
15420      return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
15421    case NT_GNU_BUILD_ID:
15422      return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
15423    case NT_GNU_GOLD_VERSION:
15424      return _("NT_GNU_GOLD_VERSION (gold version)");
15425    default:
15426      break;
15427    }
15428
15429  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15430  return buff;
15431}
15432
15433static int
15434print_gnu_note (Elf_Internal_Note *pnote)
15435{
15436  switch (pnote->type)
15437    {
15438    case NT_GNU_BUILD_ID:
15439      {
15440	unsigned long i;
15441
15442	printf (_("    Build ID: "));
15443	for (i = 0; i < pnote->descsz; ++i)
15444	  printf ("%02x", pnote->descdata[i] & 0xff);
15445	printf ("\n");
15446      }
15447      break;
15448
15449    case NT_GNU_ABI_TAG:
15450      {
15451	unsigned long os, major, minor, subminor;
15452	const char *osname;
15453
15454	/* PR 17531: file: 030-599401-0.004.  */
15455	if (pnote->descsz < 16)
15456	  {
15457	    printf (_("    <corrupt GNU_ABI_TAG>\n"));
15458	    break;
15459	  }
15460
15461	os = byte_get ((unsigned char *) pnote->descdata, 4);
15462	major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
15463	minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
15464	subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
15465
15466	switch (os)
15467	  {
15468	  case GNU_ABI_TAG_LINUX:
15469	    osname = "Linux";
15470	    break;
15471	  case GNU_ABI_TAG_HURD:
15472	    osname = "Hurd";
15473	    break;
15474	  case GNU_ABI_TAG_SOLARIS:
15475	    osname = "Solaris";
15476	    break;
15477	  case GNU_ABI_TAG_FREEBSD:
15478	    osname = "FreeBSD";
15479	    break;
15480	  case GNU_ABI_TAG_NETBSD:
15481	    osname = "NetBSD";
15482	    break;
15483	  case GNU_ABI_TAG_SYLLABLE:
15484	    osname = "Syllable";
15485	    break;
15486	  case GNU_ABI_TAG_NACL:
15487	    osname = "NaCl";
15488	    break;
15489	  default:
15490	    osname = "Unknown";
15491	    break;
15492	  }
15493
15494	printf (_("    OS: %s, ABI: %ld.%ld.%ld\n"), osname,
15495		major, minor, subminor);
15496      }
15497      break;
15498
15499    case NT_GNU_GOLD_VERSION:
15500      {
15501	unsigned long i;
15502
15503	printf (_("    Version: "));
15504	for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
15505	  printf ("%c", pnote->descdata[i]);
15506	printf ("\n");
15507      }
15508      break;
15509    }
15510
15511  return 1;
15512}
15513
15514static const char *
15515get_v850_elf_note_type (enum v850_notes n_type)
15516{
15517  static char buff[64];
15518
15519  switch (n_type)
15520    {
15521    case V850_NOTE_ALIGNMENT:  return _("Alignment of 8-byte objects");
15522    case V850_NOTE_DATA_SIZE:  return _("Sizeof double and long double");
15523    case V850_NOTE_FPU_INFO:   return _("Type of FPU support needed");
15524    case V850_NOTE_SIMD_INFO:  return _("Use of SIMD instructions");
15525    case V850_NOTE_CACHE_INFO: return _("Use of cache");
15526    case V850_NOTE_MMU_INFO:   return _("Use of MMU");
15527    default:
15528      snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
15529      return buff;
15530    }
15531}
15532
15533static int
15534print_v850_note (Elf_Internal_Note * pnote)
15535{
15536  unsigned int val;
15537
15538  if (pnote->descsz != 4)
15539    return 0;
15540  val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
15541
15542  if (val == 0)
15543    {
15544      printf (_("not set\n"));
15545      return 1;
15546    }
15547
15548  switch (pnote->type)
15549    {
15550    case V850_NOTE_ALIGNMENT:
15551      switch (val)
15552	{
15553	case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return 1;
15554	case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return 1;
15555	}
15556      break;
15557
15558    case V850_NOTE_DATA_SIZE:
15559      switch (val)
15560	{
15561	case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return 1;
15562	case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return 1;
15563	}
15564      break;
15565
15566    case V850_NOTE_FPU_INFO:
15567      switch (val)
15568	{
15569	case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return 1;
15570	case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return 1;
15571	}
15572      break;
15573
15574    case V850_NOTE_MMU_INFO:
15575    case V850_NOTE_CACHE_INFO:
15576    case V850_NOTE_SIMD_INFO:
15577      if (val == EF_RH850_SIMD)
15578	{
15579	  printf (_("yes\n"));
15580	  return 1;
15581	}
15582      break;
15583
15584    default:
15585      /* An 'unknown note type' message will already have been displayed.  */
15586      break;
15587    }
15588
15589  printf (_("unknown value: %x\n"), val);
15590  return 0;
15591}
15592
15593static int
15594process_netbsd_elf_note (Elf_Internal_Note * pnote)
15595{
15596  unsigned int version;
15597
15598  switch (pnote->type)
15599    {
15600    case NT_NETBSD_IDENT:
15601      version = byte_get((unsigned char *)pnote->descdata, sizeof(version));
15602      if ((version / 10000) % 100)
15603        printf ("  NetBSD\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
15604		version, version / 100000000, (version / 1000000) % 100,
15605		(version / 10000) % 100 > 26 ? "Z" : "",
15606		'A' + (version / 10000) % 26);
15607      else
15608	printf ("  NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
15609	        version, version / 100000000, (version / 1000000) % 100,
15610		(version / 100) % 100);
15611      return 1;
15612    case NT_NETBSD_MARCH:
15613      printf ("  NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
15614	      pnote->descdata);
15615      return 1;
15616    case NT_NETBSD_PAX:
15617      version = byte_get((unsigned char *)pnote->descdata, sizeof(version));
15618      printf ("  NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
15619	      ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
15620	      ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
15621	      ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
15622	      ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
15623	      ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
15624	      ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
15625      return 1;
15626    default:
15627      break;
15628    }
15629
15630  printf ("  NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n", pnote->descsz,
15631	  pnote->type);
15632  return 1;
15633}
15634
15635static const char *
15636get_freebsd_elfcore_note_type (unsigned e_type)
15637{
15638  switch (e_type)
15639    {
15640    case NT_FREEBSD_THRMISC:
15641      return _("NT_THRMISC (thrmisc structure)");
15642    case NT_FREEBSD_PROCSTAT_PROC:
15643      return _("NT_PROCSTAT_PROC (proc data)");
15644    case NT_FREEBSD_PROCSTAT_FILES:
15645      return _("NT_PROCSTAT_FILES (files data)");
15646    case NT_FREEBSD_PROCSTAT_VMMAP:
15647      return _("NT_PROCSTAT_VMMAP (vmmap data)");
15648    case NT_FREEBSD_PROCSTAT_GROUPS:
15649      return _("NT_PROCSTAT_GROUPS (groups data)");
15650    case NT_FREEBSD_PROCSTAT_UMASK:
15651      return _("NT_PROCSTAT_UMASK (umask data)");
15652    case NT_FREEBSD_PROCSTAT_RLIMIT:
15653      return _("NT_PROCSTAT_RLIMIT (rlimit data)");
15654    case NT_FREEBSD_PROCSTAT_OSREL:
15655      return _("NT_PROCSTAT_OSREL (osreldate data)");
15656    case NT_FREEBSD_PROCSTAT_PSSTRINGS:
15657      return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
15658    case NT_FREEBSD_PROCSTAT_AUXV:
15659      return _("NT_PROCSTAT_AUXV (auxv data)");
15660    }
15661  return get_note_type (e_type);
15662}
15663
15664static const char *
15665get_netbsd_elfcore_note_type (unsigned e_type)
15666{
15667  static char buff[64];
15668
15669  switch (e_type)
15670    {
15671    case NT_NETBSDCORE_PROCINFO:
15672      /* NetBSD core "procinfo" structure.  */
15673      return _("NetBSD procinfo structure");
15674    case NT_NETBSDCORE_AUXV:
15675      return _("NetBSD ELF auxiliary vector data");
15676    default:
15677      break;
15678    }
15679
15680  /* As of Jan 2002 there are no other machine-independent notes
15681     defined for NetBSD core files.  If the note type is less
15682     than the start of the machine-dependent note types, we don't
15683     understand it.  */
15684
15685  if (e_type < NT_NETBSDCORE_FIRSTMACH)
15686    {
15687      snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15688      return buff;
15689    }
15690
15691  switch (elf_header.e_machine)
15692    {
15693    /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
15694       and PT_GETFPREGS == mach+2.  */
15695
15696    case EM_OLD_ALPHA:
15697    case EM_ALPHA:
15698    case EM_SPARC:
15699    case EM_SPARC32PLUS:
15700    case EM_SPARCV9:
15701      switch (e_type)
15702	{
15703	case NT_NETBSDCORE_FIRSTMACH + 0:
15704	  return _("PT_GETREGS (reg structure)");
15705	case NT_NETBSDCORE_FIRSTMACH + 2:
15706	  return _("PT_GETFPREGS (fpreg structure)");
15707	default:
15708	  break;
15709	}
15710      break;
15711
15712    /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
15713       There's also old PT___GETREGS40 == mach + 1 for old reg
15714       structure which lacks GBR.  */
15715    case EM_SH:
15716      switch (e_type)
15717	{
15718	case NT_NETBSDCORE_FIRSTMACH + 1:
15719	  return _("PT___GETREGS40 (old reg structure)");
15720	case NT_NETBSDCORE_FIRSTMACH + 3:
15721	  return _("PT_GETREGS (reg structure)");
15722	case NT_NETBSDCORE_FIRSTMACH + 5:
15723	  return _("PT_GETFPREGS (fpreg structure)");
15724	default:
15725	  break;
15726	}
15727      break;
15728
15729    /* On all other arch's, PT_GETREGS == mach+1 and
15730       PT_GETFPREGS == mach+3.  */
15731    default:
15732      switch (e_type)
15733	{
15734	case NT_NETBSDCORE_FIRSTMACH + 1:
15735	  return _("PT_GETREGS (reg structure)");
15736	case NT_NETBSDCORE_FIRSTMACH + 3:
15737	  return _("PT_GETFPREGS (fpreg structure)");
15738	default:
15739	  break;
15740	}
15741    }
15742
15743  snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
15744	    e_type - NT_NETBSDCORE_FIRSTMACH);
15745  return buff;
15746}
15747
15748static const char *
15749get_stapsdt_note_type (unsigned e_type)
15750{
15751  static char buff[64];
15752
15753  switch (e_type)
15754    {
15755    case NT_STAPSDT:
15756      return _("NT_STAPSDT (SystemTap probe descriptors)");
15757
15758    default:
15759      break;
15760    }
15761
15762  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15763  return buff;
15764}
15765
15766static int
15767print_stapsdt_note (Elf_Internal_Note *pnote)
15768{
15769  int addr_size = is_32bit_elf ? 4 : 8;
15770  char *data = pnote->descdata;
15771  char *data_end = pnote->descdata + pnote->descsz;
15772  bfd_vma pc, base_addr, semaphore;
15773  char *provider, *probe, *arg_fmt;
15774
15775  pc = byte_get ((unsigned char *) data, addr_size);
15776  data += addr_size;
15777  base_addr = byte_get ((unsigned char *) data, addr_size);
15778  data += addr_size;
15779  semaphore = byte_get ((unsigned char *) data, addr_size);
15780  data += addr_size;
15781
15782  provider = data;
15783  data += strlen (data) + 1;
15784  probe = data;
15785  data += strlen (data) + 1;
15786  arg_fmt = data;
15787  data += strlen (data) + 1;
15788
15789  printf (_("    Provider: %s\n"), provider);
15790  printf (_("    Name: %s\n"), probe);
15791  printf (_("    Location: "));
15792  print_vma (pc, FULL_HEX);
15793  printf (_(", Base: "));
15794  print_vma (base_addr, FULL_HEX);
15795  printf (_(", Semaphore: "));
15796  print_vma (semaphore, FULL_HEX);
15797  printf ("\n");
15798  printf (_("    Arguments: %s\n"), arg_fmt);
15799
15800  return data == data_end;
15801}
15802
15803static const char *
15804get_ia64_vms_note_type (unsigned e_type)
15805{
15806  static char buff[64];
15807
15808  switch (e_type)
15809    {
15810    case NT_VMS_MHD:
15811      return _("NT_VMS_MHD (module header)");
15812    case NT_VMS_LNM:
15813      return _("NT_VMS_LNM (language name)");
15814    case NT_VMS_SRC:
15815      return _("NT_VMS_SRC (source files)");
15816    case NT_VMS_TITLE:
15817      return "NT_VMS_TITLE";
15818    case NT_VMS_EIDC:
15819      return _("NT_VMS_EIDC (consistency check)");
15820    case NT_VMS_FPMODE:
15821      return _("NT_VMS_FPMODE (FP mode)");
15822    case NT_VMS_LINKTIME:
15823      return "NT_VMS_LINKTIME";
15824    case NT_VMS_IMGNAM:
15825      return _("NT_VMS_IMGNAM (image name)");
15826    case NT_VMS_IMGID:
15827      return _("NT_VMS_IMGID (image id)");
15828    case NT_VMS_LINKID:
15829      return _("NT_VMS_LINKID (link id)");
15830    case NT_VMS_IMGBID:
15831      return _("NT_VMS_IMGBID (build id)");
15832    case NT_VMS_GSTNAM:
15833      return _("NT_VMS_GSTNAM (sym table name)");
15834    case NT_VMS_ORIG_DYN:
15835      return "NT_VMS_ORIG_DYN";
15836    case NT_VMS_PATCHTIME:
15837      return "NT_VMS_PATCHTIME";
15838    default:
15839      snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15840      return buff;
15841    }
15842}
15843
15844static int
15845print_ia64_vms_note (Elf_Internal_Note * pnote)
15846{
15847  switch (pnote->type)
15848    {
15849    case NT_VMS_MHD:
15850      if (pnote->descsz > 36)
15851        {
15852          size_t l = strlen (pnote->descdata + 34);
15853          printf (_("    Creation date  : %.17s\n"), pnote->descdata);
15854          printf (_("    Last patch date: %.17s\n"), pnote->descdata + 17);
15855          printf (_("    Module name    : %s\n"), pnote->descdata + 34);
15856          printf (_("    Module version : %s\n"), pnote->descdata + 34 + l + 1);
15857        }
15858      else
15859        printf (_("    Invalid size\n"));
15860      break;
15861    case NT_VMS_LNM:
15862      printf (_("   Language: %s\n"), pnote->descdata);
15863      break;
15864#ifdef BFD64
15865    case NT_VMS_FPMODE:
15866      printf (_("   Floating Point mode: "));
15867      printf ("0x%016" BFD_VMA_FMT "x\n",
15868              (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
15869      break;
15870    case NT_VMS_LINKTIME:
15871      printf (_("   Link time: "));
15872      print_vms_time
15873        ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
15874      printf ("\n");
15875      break;
15876    case NT_VMS_PATCHTIME:
15877      printf (_("   Patch time: "));
15878      print_vms_time
15879        ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
15880      printf ("\n");
15881      break;
15882    case NT_VMS_ORIG_DYN:
15883      printf (_("   Major id: %u,  minor id: %u\n"),
15884              (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
15885              (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
15886      printf (_("   Last modified  : "));
15887      print_vms_time
15888        ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
15889      printf (_("\n   Link flags  : "));
15890      printf ("0x%016" BFD_VMA_FMT "x\n",
15891              (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
15892      printf (_("   Header flags: 0x%08x\n"),
15893              (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
15894      printf (_("   Image id    : %s\n"), pnote->descdata + 32);
15895      break;
15896#endif
15897    case NT_VMS_IMGNAM:
15898      printf (_("    Image name: %s\n"), pnote->descdata);
15899      break;
15900    case NT_VMS_GSTNAM:
15901      printf (_("    Global symbol table name: %s\n"), pnote->descdata);
15902      break;
15903    case NT_VMS_IMGID:
15904      printf (_("    Image id: %s\n"), pnote->descdata);
15905      break;
15906    case NT_VMS_LINKID:
15907      printf (_("    Linker id: %s\n"), pnote->descdata);
15908      break;
15909    default:
15910      break;
15911    }
15912  return 1;
15913}
15914
15915/* Note that by the ELF standard, the name field is already null byte
15916   terminated, and namesz includes the terminating null byte.
15917   I.E. the value of namesz for the name "FSF" is 4.
15918
15919   If the value of namesz is zero, there is no name present.  */
15920static int
15921process_note (Elf_Internal_Note * pnote)
15922{
15923  const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
15924  const char * nt;
15925
15926  if (pnote->namesz == 0)
15927    /* If there is no note name, then use the default set of
15928       note type strings.  */
15929    nt = get_note_type (pnote->type);
15930
15931  else if (const_strneq (pnote->namedata, "GNU"))
15932    /* GNU-specific object file notes.  */
15933    nt = get_gnu_elf_note_type (pnote->type);
15934
15935  else if (const_strneq (pnote->namedata, "FreeBSD"))
15936    /* FreeBSD-specific core file notes.  */
15937    nt = get_freebsd_elfcore_note_type (pnote->type);
15938
15939  else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
15940    /* NetBSD-specific core file notes.  */
15941    nt = get_netbsd_elfcore_note_type (pnote->type);
15942
15943  else if (const_strneq (pnote->namedata, "NetBSD"))
15944    /* NetBSD-specific core file notes.  */
15945    return process_netbsd_elf_note (pnote);
15946
15947  else if (const_strneq (pnote->namedata, "PaX"))
15948    /* NetBSD-specific core file notes.  */
15949    return process_netbsd_elf_note (pnote);
15950
15951  else if (strneq (pnote->namedata, "SPU/", 4))
15952    {
15953      /* SPU-specific core file notes.  */
15954      nt = pnote->namedata + 4;
15955      name = "SPU";
15956    }
15957
15958  else if (const_strneq (pnote->namedata, "IPF/VMS"))
15959    /* VMS/ia64-specific file notes.  */
15960    nt = get_ia64_vms_note_type (pnote->type);
15961
15962  else if (const_strneq (pnote->namedata, "stapsdt"))
15963    nt = get_stapsdt_note_type (pnote->type);
15964
15965  else
15966    /* Don't recognize this note name; just use the default set of
15967       note type strings.  */
15968    nt = get_note_type (pnote->type);
15969
15970  printf ("  %-20s 0x%08lx\t%s\n", name, pnote->descsz, nt);
15971
15972  if (const_strneq (pnote->namedata, "IPF/VMS"))
15973    return print_ia64_vms_note (pnote);
15974  else if (const_strneq (pnote->namedata, "GNU"))
15975    return print_gnu_note (pnote);
15976  else if (const_strneq (pnote->namedata, "stapsdt"))
15977    return print_stapsdt_note (pnote);
15978  else if (const_strneq (pnote->namedata, "CORE"))
15979    return print_core_note (pnote);
15980  else
15981    return 1;
15982}
15983
15984
15985static int
15986process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
15987{
15988  Elf_External_Note * pnotes;
15989  Elf_External_Note * external;
15990  char * end;
15991  int res = 1;
15992
15993  if (length <= 0)
15994    return 0;
15995
15996  pnotes = (Elf_External_Note *) get_data (NULL, file, offset, 1, length,
15997					   _("notes"));
15998  if (pnotes == NULL)
15999    return 0;
16000
16001  external = pnotes;
16002
16003  printf (_("\nDisplaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
16004	  (unsigned long) offset, (unsigned long) length);
16005  printf (_("  %-20s %10s\tDescription\n"), _("Owner"), _("Data size"));
16006
16007  end = (char *) pnotes + length;
16008  while ((char *) external < end)
16009    {
16010      Elf_Internal_Note inote;
16011      size_t min_notesz;
16012      char *next;
16013      char * temp = NULL;
16014      size_t data_remaining = end - (char *) external;
16015
16016      if (!is_ia64_vms ())
16017	{
16018	  /* PR binutils/15191
16019	     Make sure that there is enough data to read.  */
16020	  min_notesz = offsetof (Elf_External_Note, name);
16021	  if (data_remaining < min_notesz)
16022	    {
16023	      warn (_("Corrupt note: only %d bytes remain, not enough for a full note\n"),
16024		    (int) data_remaining);
16025	      break;
16026	    }
16027	  inote.type     = BYTE_GET (external->type);
16028	  inote.namesz   = BYTE_GET (external->namesz);
16029	  inote.namedata = external->name;
16030	  inote.descsz   = BYTE_GET (external->descsz);
16031	  inote.descdata = inote.namedata + align_power (inote.namesz, 2);
16032	  /* PR 17531: file: 3443835e.  */
16033	  if (inote.descdata < (char *) pnotes || inote.descdata > end)
16034	    {
16035	      warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
16036	      inote.descdata = inote.namedata;
16037	      inote.namesz   = 0;
16038	    }
16039
16040	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
16041	  next = inote.descdata + align_power (inote.descsz, 2);
16042	}
16043      else
16044	{
16045	  Elf64_External_VMS_Note *vms_external;
16046
16047	  /* PR binutils/15191
16048	     Make sure that there is enough data to read.  */
16049	  min_notesz = offsetof (Elf64_External_VMS_Note, name);
16050	  if (data_remaining < min_notesz)
16051	    {
16052	      warn (_("Corrupt note: only %d bytes remain, not enough for a full note\n"),
16053		    (int) data_remaining);
16054	      break;
16055	    }
16056
16057	  vms_external = (Elf64_External_VMS_Note *) external;
16058	  inote.type     = BYTE_GET (vms_external->type);
16059	  inote.namesz   = BYTE_GET (vms_external->namesz);
16060	  inote.namedata = vms_external->name;
16061	  inote.descsz   = BYTE_GET (vms_external->descsz);
16062	  inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16063	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
16064	  next = inote.descdata + align_power (inote.descsz, 3);
16065	}
16066
16067      if (inote.descdata < (char *) external + min_notesz
16068	  || next < (char *) external + min_notesz
16069	  /* PR binutils/17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4.  */
16070	  || inote.namedata + inote.namesz < inote.namedata
16071	  || inote.descdata + inote.descsz < inote.descdata
16072	  || data_remaining < (size_t)(next - (char *) external))
16073	{
16074	  warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
16075		(unsigned long) ((char *) external - (char *) pnotes));
16076	  warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx\n"),
16077		inote.type, inote.namesz, inote.descsz);
16078	  break;
16079	}
16080
16081      external = (Elf_External_Note *) next;
16082
16083      /* Verify that name is null terminated.  It appears that at least
16084	 one version of Linux (RedHat 6.0) generates corefiles that don't
16085	 comply with the ELF spec by failing to include the null byte in
16086	 namesz.  */
16087      if (inote.namedata[inote.namesz - 1] != '\0')
16088	{
16089	  temp = (char *) malloc (inote.namesz + 1);
16090	  if (temp == NULL)
16091	    {
16092	      error (_("Out of memory allocating space for inote name\n"));
16093	      res = 0;
16094	      break;
16095	    }
16096
16097	  strncpy (temp, inote.namedata, inote.namesz);
16098	  temp[inote.namesz] = 0;
16099
16100	  /* warn (_("'%s' NOTE name not properly null terminated\n"), temp);  */
16101	  inote.namedata = temp;
16102	}
16103
16104      res &= process_note (& inote);
16105
16106      if (temp != NULL)
16107	{
16108	  free (temp);
16109	  temp = NULL;
16110	}
16111    }
16112
16113  free (pnotes);
16114
16115  return res;
16116}
16117
16118static int
16119process_corefile_note_segments (FILE * file)
16120{
16121  Elf_Internal_Phdr * segment;
16122  unsigned int i;
16123  int res = 1;
16124
16125  if (! get_program_headers (file))
16126      return 0;
16127
16128  for (i = 0, segment = program_headers;
16129       i < elf_header.e_phnum;
16130       i++, segment++)
16131    {
16132      if (segment->p_type == PT_NOTE)
16133	res &= process_corefile_note_segment (file,
16134					      (bfd_vma) segment->p_offset,
16135					      (bfd_vma) segment->p_filesz);
16136    }
16137
16138  return res;
16139}
16140
16141static int
16142process_v850_notes (FILE * file, bfd_vma offset, bfd_vma length)
16143{
16144  Elf_External_Note * pnotes;
16145  Elf_External_Note * external;
16146  char * end;
16147  int res = 1;
16148
16149  if (length <= 0)
16150    return 0;
16151
16152  pnotes = (Elf_External_Note *) get_data (NULL, file, offset, 1, length,
16153                                           _("v850 notes"));
16154  if (pnotes == NULL)
16155    return 0;
16156
16157  external = pnotes;
16158  end = (char*) pnotes + length;
16159
16160  printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
16161	  (unsigned long) offset, (unsigned long) length);
16162
16163  while ((char *) external + sizeof (Elf_External_Note) < end)
16164    {
16165      Elf_External_Note * next;
16166      Elf_Internal_Note inote;
16167
16168      inote.type     = BYTE_GET (external->type);
16169      inote.namesz   = BYTE_GET (external->namesz);
16170      inote.namedata = external->name;
16171      inote.descsz   = BYTE_GET (external->descsz);
16172      inote.descdata = inote.namedata + align_power (inote.namesz, 2);
16173      inote.descpos  = offset + (inote.descdata - (char *) pnotes);
16174
16175      if (inote.descdata < (char *) pnotes || inote.descdata >= end)
16176	{
16177	  warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
16178	  inote.descdata = inote.namedata;
16179	  inote.namesz   = 0;
16180	}
16181
16182      next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
16183
16184      if (   ((char *) next > end)
16185	  || ((char *) next <  (char *) pnotes))
16186	{
16187	  warn (_("corrupt descsz found in note at offset 0x%lx\n"),
16188		(unsigned long) ((char *) external - (char *) pnotes));
16189	  warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
16190		inote.type, inote.namesz, inote.descsz);
16191	  break;
16192	}
16193
16194      external = next;
16195
16196      /* Prevent out-of-bounds indexing.  */
16197      if (   inote.namedata + inote.namesz > end
16198	  || inote.namedata + inote.namesz < inote.namedata)
16199        {
16200          warn (_("corrupt namesz found in note at offset 0x%lx\n"),
16201                (unsigned long) ((char *) external - (char *) pnotes));
16202          warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
16203                inote.type, inote.namesz, inote.descsz);
16204          break;
16205        }
16206
16207      printf ("  %s: ", get_v850_elf_note_type (inote.type));
16208
16209      if (! print_v850_note (& inote))
16210	{
16211	  res = 0;
16212	  printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
16213		  inote.namesz, inote.descsz);
16214	}
16215    }
16216
16217  free (pnotes);
16218
16219  return res;
16220}
16221
16222static int
16223process_note_sections (FILE * file)
16224{
16225  Elf_Internal_Shdr * section;
16226  unsigned long i;
16227  int n = 0;
16228  int res = 1;
16229
16230  for (i = 0, section = section_headers;
16231       i < elf_header.e_shnum && section != NULL;
16232       i++, section++)
16233    {
16234      if (section->sh_type == SHT_NOTE)
16235	{
16236	  res &= process_corefile_note_segment (file,
16237						(bfd_vma) section->sh_offset,
16238						(bfd_vma) section->sh_size);
16239	  n++;
16240	}
16241
16242      if ((   elf_header.e_machine == EM_V800
16243	   || elf_header.e_machine == EM_V850
16244	   || elf_header.e_machine == EM_CYGNUS_V850)
16245	  && section->sh_type == SHT_RENESAS_INFO)
16246	{
16247	  res &= process_v850_notes (file,
16248				     (bfd_vma) section->sh_offset,
16249				     (bfd_vma) section->sh_size);
16250	  n++;
16251	}
16252    }
16253
16254  if (n == 0)
16255    /* Try processing NOTE segments instead.  */
16256    return process_corefile_note_segments (file);
16257
16258  return res;
16259}
16260
16261static int
16262process_notes (FILE * file)
16263{
16264  /* If we have not been asked to display the notes then do nothing.  */
16265  if (! do_notes)
16266    return 1;
16267
16268  if (elf_header.e_type != ET_CORE)
16269    return process_note_sections (file);
16270
16271  /* No program headers means no NOTE segment.  */
16272  if (elf_header.e_phnum > 0)
16273    return process_corefile_note_segments (file);
16274
16275  printf (_("No note segments present in the core file.\n"));
16276  return 1;
16277}
16278
16279static int
16280process_arch_specific (FILE * file)
16281{
16282  if (! do_arch)
16283    return 1;
16284
16285  switch (elf_header.e_machine)
16286    {
16287    case EM_ARM:
16288      return process_arm_specific (file);
16289    case EM_MIPS:
16290    case EM_MIPS_RS3_LE:
16291      return process_mips_specific (file);
16292      break;
16293    case EM_NDS32:
16294      return process_nds32_specific (file);
16295      break;
16296    case EM_PPC:
16297      return process_power_specific (file);
16298      break;
16299    case EM_S390:
16300    case EM_S390_OLD:
16301      return process_s390_specific (file);
16302      break;
16303    case EM_SPARC:
16304    case EM_SPARC32PLUS:
16305    case EM_SPARCV9:
16306      return process_sparc_specific (file);
16307      break;
16308    case EM_TI_C6000:
16309      return process_tic6x_specific (file);
16310      break;
16311    case EM_MSP430:
16312      return process_msp430x_specific (file);
16313    default:
16314      break;
16315    }
16316  return 1;
16317}
16318
16319static int
16320get_file_header (FILE * file)
16321{
16322  /* Read in the identity array.  */
16323  if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
16324    return 0;
16325
16326  /* Determine how to read the rest of the header.  */
16327  switch (elf_header.e_ident[EI_DATA])
16328    {
16329    default: /* fall through */
16330    case ELFDATANONE: /* fall through */
16331    case ELFDATA2LSB:
16332      byte_get = byte_get_little_endian;
16333      byte_put = byte_put_little_endian;
16334      break;
16335    case ELFDATA2MSB:
16336      byte_get = byte_get_big_endian;
16337      byte_put = byte_put_big_endian;
16338      break;
16339    }
16340
16341  /* For now we only support 32 bit and 64 bit ELF files.  */
16342  is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
16343
16344  /* Read in the rest of the header.  */
16345  if (is_32bit_elf)
16346    {
16347      Elf32_External_Ehdr ehdr32;
16348
16349      if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
16350	return 0;
16351
16352      elf_header.e_type      = BYTE_GET (ehdr32.e_type);
16353      elf_header.e_machine   = BYTE_GET (ehdr32.e_machine);
16354      elf_header.e_version   = BYTE_GET (ehdr32.e_version);
16355      elf_header.e_entry     = BYTE_GET (ehdr32.e_entry);
16356      elf_header.e_phoff     = BYTE_GET (ehdr32.e_phoff);
16357      elf_header.e_shoff     = BYTE_GET (ehdr32.e_shoff);
16358      elf_header.e_flags     = BYTE_GET (ehdr32.e_flags);
16359      elf_header.e_ehsize    = BYTE_GET (ehdr32.e_ehsize);
16360      elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
16361      elf_header.e_phnum     = BYTE_GET (ehdr32.e_phnum);
16362      elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
16363      elf_header.e_shnum     = BYTE_GET (ehdr32.e_shnum);
16364      elf_header.e_shstrndx  = BYTE_GET (ehdr32.e_shstrndx);
16365    }
16366  else
16367    {
16368      Elf64_External_Ehdr ehdr64;
16369
16370      /* If we have been compiled with sizeof (bfd_vma) == 4, then
16371	 we will not be able to cope with the 64bit data found in
16372	 64 ELF files.  Detect this now and abort before we start
16373	 overwriting things.  */
16374      if (sizeof (bfd_vma) < 8)
16375	{
16376	  error (_("This instance of readelf has been built without support for a\n\
1637764 bit data type and so it cannot read 64 bit ELF files.\n"));
16378	  return 0;
16379	}
16380
16381      if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
16382	return 0;
16383
16384      elf_header.e_type      = BYTE_GET (ehdr64.e_type);
16385      elf_header.e_machine   = BYTE_GET (ehdr64.e_machine);
16386      elf_header.e_version   = BYTE_GET (ehdr64.e_version);
16387      elf_header.e_entry     = BYTE_GET (ehdr64.e_entry);
16388      elf_header.e_phoff     = BYTE_GET (ehdr64.e_phoff);
16389      elf_header.e_shoff     = BYTE_GET (ehdr64.e_shoff);
16390      elf_header.e_flags     = BYTE_GET (ehdr64.e_flags);
16391      elf_header.e_ehsize    = BYTE_GET (ehdr64.e_ehsize);
16392      elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
16393      elf_header.e_phnum     = BYTE_GET (ehdr64.e_phnum);
16394      elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
16395      elf_header.e_shnum     = BYTE_GET (ehdr64.e_shnum);
16396      elf_header.e_shstrndx  = BYTE_GET (ehdr64.e_shstrndx);
16397    }
16398
16399  if (elf_header.e_shoff)
16400    {
16401      /* There may be some extensions in the first section header.  Don't
16402	 bomb if we can't read it.  */
16403      if (is_32bit_elf)
16404	get_32bit_section_headers (file, TRUE);
16405      else
16406	get_64bit_section_headers (file, TRUE);
16407    }
16408
16409  return 1;
16410}
16411
16412/* Process one ELF object file according to the command line options.
16413   This file may actually be stored in an archive.  The file is
16414   positioned at the start of the ELF object.  */
16415
16416static int
16417process_object (char * file_name, FILE * file)
16418{
16419  unsigned int i;
16420
16421  if (! get_file_header (file))
16422    {
16423      error (_("%s: Failed to read file header\n"), file_name);
16424      return 1;
16425    }
16426
16427  /* Initialise per file variables.  */
16428  for (i = ARRAY_SIZE (version_info); i--;)
16429    version_info[i] = 0;
16430
16431  for (i = ARRAY_SIZE (dynamic_info); i--;)
16432    dynamic_info[i] = 0;
16433  dynamic_info_DT_GNU_HASH = 0;
16434
16435  /* Process the file.  */
16436  if (show_name)
16437    printf (_("\nFile: %s\n"), file_name);
16438
16439  /* Initialise the dump_sects array from the cmdline_dump_sects array.
16440     Note we do this even if cmdline_dump_sects is empty because we
16441     must make sure that the dump_sets array is zeroed out before each
16442     object file is processed.  */
16443  if (num_dump_sects > num_cmdline_dump_sects)
16444    memset (dump_sects, 0, num_dump_sects * sizeof (* dump_sects));
16445
16446  if (num_cmdline_dump_sects > 0)
16447    {
16448      if (num_dump_sects == 0)
16449	/* A sneaky way of allocating the dump_sects array.  */
16450	request_dump_bynumber (num_cmdline_dump_sects, 0);
16451
16452      assert (num_dump_sects >= num_cmdline_dump_sects);
16453      memcpy (dump_sects, cmdline_dump_sects,
16454	      num_cmdline_dump_sects * sizeof (* dump_sects));
16455    }
16456
16457  if (! process_file_header ())
16458    return 1;
16459
16460  if (! process_section_headers (file))
16461    {
16462      /* Without loaded section headers we cannot process lots of
16463	 things.  */
16464      do_unwind = do_version = do_dump = do_arch = 0;
16465
16466      if (! do_using_dynamic)
16467	do_syms = do_dyn_syms = do_reloc = 0;
16468    }
16469
16470  if (! process_section_groups (file))
16471    {
16472      /* Without loaded section groups we cannot process unwind.  */
16473      do_unwind = 0;
16474    }
16475
16476  if (process_program_headers (file))
16477    process_dynamic_section (file);
16478
16479  process_relocs (file);
16480
16481  process_unwind (file);
16482
16483  process_symbol_table (file);
16484
16485  process_syminfo (file);
16486
16487  process_version_sections (file);
16488
16489  process_section_contents (file);
16490
16491  process_notes (file);
16492
16493  process_gnu_liblist (file);
16494
16495  process_arch_specific (file);
16496
16497  if (program_headers)
16498    {
16499      free (program_headers);
16500      program_headers = NULL;
16501    }
16502
16503  if (section_headers)
16504    {
16505      free (section_headers);
16506      section_headers = NULL;
16507    }
16508
16509  if (string_table)
16510    {
16511      free (string_table);
16512      string_table = NULL;
16513      string_table_length = 0;
16514    }
16515
16516  if (dynamic_strings)
16517    {
16518      free (dynamic_strings);
16519      dynamic_strings = NULL;
16520      dynamic_strings_length = 0;
16521    }
16522
16523  if (dynamic_symbols)
16524    {
16525      free (dynamic_symbols);
16526      dynamic_symbols = NULL;
16527      num_dynamic_syms = 0;
16528    }
16529
16530  if (dynamic_syminfo)
16531    {
16532      free (dynamic_syminfo);
16533      dynamic_syminfo = NULL;
16534    }
16535
16536  if (dynamic_section)
16537    {
16538      free (dynamic_section);
16539      dynamic_section = NULL;
16540    }
16541
16542  if (section_headers_groups)
16543    {
16544      free (section_headers_groups);
16545      section_headers_groups = NULL;
16546    }
16547
16548  if (section_groups)
16549    {
16550      struct group_list * g;
16551      struct group_list * next;
16552
16553      for (i = 0; i < group_count; i++)
16554	{
16555	  for (g = section_groups [i].root; g != NULL; g = next)
16556	    {
16557	      next = g->next;
16558	      free (g);
16559	    }
16560	}
16561
16562      free (section_groups);
16563      section_groups = NULL;
16564    }
16565
16566  free_debug_memory ();
16567
16568  return 0;
16569}
16570
16571/* Process an ELF archive.
16572   On entry the file is positioned just after the ARMAG string.  */
16573
16574static int
16575process_archive (char * file_name, FILE * file, bfd_boolean is_thin_archive)
16576{
16577  struct archive_info arch;
16578  struct archive_info nested_arch;
16579  size_t got;
16580  int ret;
16581
16582  show_name = 1;
16583
16584  /* The ARCH structure is used to hold information about this archive.  */
16585  arch.file_name = NULL;
16586  arch.file = NULL;
16587  arch.index_array = NULL;
16588  arch.sym_table = NULL;
16589  arch.longnames = NULL;
16590
16591  /* The NESTED_ARCH structure is used as a single-item cache of information
16592     about a nested archive (when members of a thin archive reside within
16593     another regular archive file).  */
16594  nested_arch.file_name = NULL;
16595  nested_arch.file = NULL;
16596  nested_arch.index_array = NULL;
16597  nested_arch.sym_table = NULL;
16598  nested_arch.longnames = NULL;
16599
16600  if (setup_archive (&arch, file_name, file, is_thin_archive, do_archive_index) != 0)
16601    {
16602      ret = 1;
16603      goto out;
16604    }
16605
16606  if (do_archive_index)
16607    {
16608      if (arch.sym_table == NULL)
16609	error (_("%s: unable to dump the index as none was found\n"), file_name);
16610      else
16611	{
16612	  unsigned long i, l;
16613	  unsigned long current_pos;
16614
16615	  printf (_("Index of archive %s: (%lu entries, 0x%lx bytes in the symbol table)\n"),
16616		  file_name, (unsigned long) arch.index_num, arch.sym_size);
16617	  current_pos = ftell (file);
16618
16619	  for (i = l = 0; i < arch.index_num; i++)
16620	    {
16621	      if ((i == 0) || ((i > 0) && (arch.index_array[i] != arch.index_array[i - 1])))
16622	        {
16623	          char * member_name;
16624
16625		  member_name = get_archive_member_name_at (&arch, arch.index_array[i], &nested_arch);
16626
16627                  if (member_name != NULL)
16628                    {
16629	              char * qualified_name = make_qualified_name (&arch, &nested_arch, member_name);
16630
16631                      if (qualified_name != NULL)
16632                        {
16633		          printf (_("Contents of binary %s at offset "), qualified_name);
16634			  (void) print_vma (arch.index_array[i], PREFIX_HEX);
16635			  putchar ('\n');
16636		          free (qualified_name);
16637		        }
16638		    }
16639		}
16640
16641	      if (l >= arch.sym_size)
16642		{
16643		  error (_("%s: end of the symbol table reached before the end of the index\n"),
16644			 file_name);
16645		  break;
16646		}
16647	      /* PR 17531: file: 0b6630b2.  */
16648	      printf ("\t%.*s\n", (int) (arch.sym_size - l), arch.sym_table + l);
16649	      l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
16650	    }
16651
16652	  if (arch.uses_64bit_indicies)
16653	    l = (l + 7) & ~ 7;
16654	  else
16655	    l += l & 1;
16656
16657	  if (l < arch.sym_size)
16658	    error (_("%s: %ld bytes remain in the symbol table, but without corresponding entries in the index table\n"),
16659		   file_name, arch.sym_size - l);
16660
16661	  if (fseek (file, current_pos, SEEK_SET) != 0)
16662	    {
16663	      error (_("%s: failed to seek back to start of object files in the archive\n"), file_name);
16664	      ret = 1;
16665	      goto out;
16666	    }
16667	}
16668
16669      if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
16670	  && !do_segments && !do_header && !do_dump && !do_version
16671	  && !do_histogram && !do_debugging && !do_arch && !do_notes
16672	  && !do_section_groups && !do_dyn_syms)
16673	{
16674	  ret = 0; /* Archive index only.  */
16675	  goto out;
16676	}
16677    }
16678
16679  ret = 0;
16680
16681  while (1)
16682    {
16683      char * name;
16684      size_t namelen;
16685      char * qualified_name;
16686
16687      /* Read the next archive header.  */
16688      if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
16689        {
16690          error (_("%s: failed to seek to next archive header\n"), file_name);
16691          return 1;
16692        }
16693      got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
16694      if (got != sizeof arch.arhdr)
16695        {
16696          if (got == 0)
16697	    break;
16698          error (_("%s: failed to read archive header\n"), file_name);
16699          ret = 1;
16700          break;
16701        }
16702      if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
16703        {
16704          error (_("%s: did not find a valid archive header\n"), arch.file_name);
16705          ret = 1;
16706          break;
16707        }
16708
16709      arch.next_arhdr_offset += sizeof arch.arhdr;
16710
16711      archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
16712      if (archive_file_size & 01)
16713        ++archive_file_size;
16714
16715      name = get_archive_member_name (&arch, &nested_arch);
16716      if (name == NULL)
16717	{
16718	  error (_("%s: bad archive file name\n"), file_name);
16719	  ret = 1;
16720	  break;
16721	}
16722      namelen = strlen (name);
16723
16724      qualified_name = make_qualified_name (&arch, &nested_arch, name);
16725      if (qualified_name == NULL)
16726	{
16727	  error (_("%s: bad archive file name\n"), file_name);
16728	  ret = 1;
16729	  break;
16730	}
16731
16732      if (is_thin_archive && arch.nested_member_origin == 0)
16733        {
16734          /* This is a proxy for an external member of a thin archive.  */
16735          FILE * member_file;
16736          char * member_file_name = adjust_relative_path (file_name, name, namelen);
16737          if (member_file_name == NULL)
16738            {
16739              ret = 1;
16740              break;
16741            }
16742
16743          member_file = fopen (member_file_name, "rb");
16744          if (member_file == NULL)
16745            {
16746              error (_("Input file '%s' is not readable.\n"), member_file_name);
16747              free (member_file_name);
16748              ret = 1;
16749              break;
16750            }
16751
16752          archive_file_offset = arch.nested_member_origin;
16753
16754          ret |= process_object (qualified_name, member_file);
16755
16756          fclose (member_file);
16757          free (member_file_name);
16758        }
16759      else if (is_thin_archive)
16760        {
16761	  /* PR 15140: Allow for corrupt thin archives.  */
16762	  if (nested_arch.file == NULL)
16763	    {
16764	      error (_("%s: contains corrupt thin archive: %s\n"),
16765		     file_name, name);
16766	      ret = 1;
16767	      break;
16768	    }
16769
16770          /* This is a proxy for a member of a nested archive.  */
16771          archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
16772
16773          /* The nested archive file will have been opened and setup by
16774             get_archive_member_name.  */
16775          if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
16776            {
16777              error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
16778              ret = 1;
16779              break;
16780            }
16781
16782          ret |= process_object (qualified_name, nested_arch.file);
16783        }
16784      else
16785        {
16786          archive_file_offset = arch.next_arhdr_offset;
16787          arch.next_arhdr_offset += archive_file_size;
16788
16789          ret |= process_object (qualified_name, file);
16790        }
16791
16792      if (dump_sects != NULL)
16793	{
16794	  free (dump_sects);
16795	  dump_sects = NULL;
16796	  num_dump_sects = 0;
16797	}
16798
16799      free (qualified_name);
16800    }
16801
16802 out:
16803  if (nested_arch.file != NULL)
16804    fclose (nested_arch.file);
16805  release_archive (&nested_arch);
16806  release_archive (&arch);
16807
16808  return ret;
16809}
16810
16811static int
16812process_file (char * file_name)
16813{
16814  FILE * file;
16815  struct stat statbuf;
16816  char armag[SARMAG];
16817  int ret;
16818
16819  if (stat (file_name, &statbuf) < 0)
16820    {
16821      if (errno == ENOENT)
16822	error (_("'%s': No such file\n"), file_name);
16823      else
16824	error (_("Could not locate '%s'.  System error message: %s\n"),
16825	       file_name, strerror (errno));
16826      return 1;
16827    }
16828
16829  if (!do_special_files && ! S_ISREG (statbuf.st_mode))
16830    {
16831      error (_("'%s' is not an ordinary file\n"), file_name);
16832      return 1;
16833    }
16834
16835  file = fopen (file_name, "rb");
16836  if (file == NULL)
16837    {
16838      error (_("Input file '%s' is not readable.\n"), file_name);
16839      return 1;
16840    }
16841
16842  if (fread (armag, SARMAG, 1, file) != 1)
16843    {
16844      error (_("%s: Failed to read file's magic number\n"), file_name);
16845      fclose (file);
16846      return 1;
16847    }
16848
16849  current_file_size = (bfd_size_type) statbuf.st_size;
16850
16851  if (memcmp (armag, ARMAG, SARMAG) == 0)
16852    ret = process_archive (file_name, file, FALSE);
16853  else if (memcmp (armag, ARMAGT, SARMAG) == 0)
16854    ret = process_archive (file_name, file, TRUE);
16855  else
16856    {
16857      if (do_archive_index)
16858	error (_("File %s is not an archive so its index cannot be displayed.\n"),
16859	       file_name);
16860
16861      rewind (file);
16862      archive_file_size = archive_file_offset = 0;
16863      ret = process_object (file_name, file);
16864    }
16865
16866  fclose (file);
16867
16868  current_file_size = 0;
16869  return ret;
16870}
16871
16872#ifdef SUPPORT_DISASSEMBLY
16873/* Needed by the i386 disassembler.  For extra credit, someone could
16874   fix this so that we insert symbolic addresses here, esp for GOT/PLT
16875   symbols.  */
16876
16877void
16878print_address (unsigned int addr, FILE * outfile)
16879{
16880  fprintf (outfile,"0x%8.8x", addr);
16881}
16882
16883/* Needed by the i386 disassembler.  */
16884void
16885db_task_printsym (unsigned int addr)
16886{
16887  print_address (addr, stderr);
16888}
16889#endif
16890
16891int
16892main (int argc, char ** argv)
16893{
16894  int err;
16895
16896#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
16897  setlocale (LC_MESSAGES, "");
16898#endif
16899#if defined (HAVE_SETLOCALE)
16900  setlocale (LC_CTYPE, "");
16901#endif
16902  bindtextdomain (PACKAGE, LOCALEDIR);
16903  textdomain (PACKAGE);
16904
16905  expandargv (&argc, &argv);
16906
16907  parse_args (argc, argv);
16908
16909  if (num_dump_sects > 0)
16910    {
16911      /* Make a copy of the dump_sects array.  */
16912      cmdline_dump_sects = (dump_type *)
16913          malloc (num_dump_sects * sizeof (* dump_sects));
16914      if (cmdline_dump_sects == NULL)
16915	error (_("Out of memory allocating dump request table.\n"));
16916      else
16917	{
16918	  memcpy (cmdline_dump_sects, dump_sects,
16919		  num_dump_sects * sizeof (* dump_sects));
16920	  num_cmdline_dump_sects = num_dump_sects;
16921	}
16922    }
16923
16924  if (optind < (argc - 1))
16925    show_name = 1;
16926  else if (optind >= argc)
16927    {
16928      warn (_("Nothing to do.\n"));
16929      usage (stderr);
16930    }
16931
16932  err = 0;
16933  while (optind < argc)
16934    err |= process_file (argv[optind++]);
16935
16936  if (dump_sects != NULL)
16937    free (dump_sects);
16938  if (cmdline_dump_sects != NULL)
16939    free (cmdline_dump_sects);
16940
16941  return err;
16942}
16943