readelf.c revision 1.6
1/* readelf.c -- display contents of an ELF format file
2   Copyright (C) 1998-2018 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/s12z.h"
123#include "elf/mcore.h"
124#include "elf/mep.h"
125#include "elf/metag.h"
126#include "elf/microblaze.h"
127#include "elf/mips.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/nfp.h"
136#include "elf/nios2.h"
137#include "elf/or1k.h"
138#include "elf/pj.h"
139#include "elf/ppc.h"
140#include "elf/ppc64.h"
141#include "elf/pru.h"
142#include "elf/riscv.h"
143#include "elf/rl78.h"
144#include "elf/rx.h"
145#include "elf/s390.h"
146#include "elf/score.h"
147#include "elf/sh.h"
148#include "elf/sparc.h"
149#include "elf/spu.h"
150#include "elf/tic6x.h"
151#include "elf/tilegx.h"
152#include "elf/tilepro.h"
153#include "elf/v850.h"
154#include "elf/vax.h"
155#include "elf/visium.h"
156#include "elf/wasm32.h"
157#include "elf/x86-64.h"
158#include "elf/xc16x.h"
159#include "elf/xgate.h"
160#include "elf/xstormy16.h"
161#include "elf/xtensa.h"
162
163#include "getopt.h"
164#include "libiberty.h"
165#include "safe-ctype.h"
166#include "filenames.h"
167
168#ifndef offsetof
169#define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
170#endif
171
172typedef struct elf_section_list
173{
174  Elf_Internal_Shdr *        hdr;
175  struct elf_section_list *  next;
176} elf_section_list;
177
178/* Flag bits indicating particular types of dump.  */
179#define HEX_DUMP	(1 << 0)	/* The -x command line switch.  */
180#define DISASS_DUMP	(1 << 1)	/* The -i command line switch.  */
181#define DEBUG_DUMP	(1 << 2)	/* The -w command line switch.  */
182#define STRING_DUMP     (1 << 3)	/* The -p command line switch.  */
183#define RELOC_DUMP      (1 << 4)	/* The -R command line switch.  */
184
185typedef unsigned char dump_type;
186
187/* A linked list of the section names for which dumps were requested.  */
188struct dump_list_entry
189{
190  char *                    name;
191  dump_type                 type;
192  struct dump_list_entry *  next;
193};
194
195typedef struct filedata
196{
197  const char *         file_name;
198  FILE *               handle;
199  bfd_size_type        file_size;
200  Elf_Internal_Ehdr    file_header;
201  Elf_Internal_Shdr *  section_headers;
202  Elf_Internal_Phdr *  program_headers;
203  char *               string_table;
204  unsigned long        string_table_length;
205  /* A dynamic array of flags indicating for which sections a dump of
206     some kind has been requested.  It is reset on a per-object file
207     basis and then initialised from the cmdline_dump_sects array,
208     the results of interpreting the -w switch, and the
209     dump_sects_byname list.  */
210  dump_type *          dump_sects;
211  unsigned int         num_dump_sects;
212} Filedata;
213
214char * program_name = "readelf";
215
216static unsigned long archive_file_offset;
217static unsigned long archive_file_size;
218static unsigned long dynamic_addr;
219static bfd_size_type dynamic_size;
220static size_t dynamic_nent;
221static char * dynamic_strings;
222static unsigned long dynamic_strings_length;
223static unsigned long num_dynamic_syms;
224static Elf_Internal_Sym * dynamic_symbols;
225static Elf_Internal_Syminfo * dynamic_syminfo;
226static unsigned long dynamic_syminfo_offset;
227static unsigned int dynamic_syminfo_nent;
228static char program_interpreter[PATH_MAX];
229static bfd_vma dynamic_info[DT_ENCODING];
230static bfd_vma dynamic_info_DT_GNU_HASH;
231static bfd_vma version_info[16];
232static Elf_Internal_Dyn *  dynamic_section;
233static elf_section_list * symtab_shndx_list;
234static bfd_boolean show_name = FALSE;
235static bfd_boolean do_special_files = FALSE;
236static bfd_boolean do_dynamic = FALSE;
237static bfd_boolean do_syms = FALSE;
238static bfd_boolean do_dyn_syms = FALSE;
239static bfd_boolean do_reloc = FALSE;
240static bfd_boolean do_sections = FALSE;
241static bfd_boolean do_section_groups = FALSE;
242static bfd_boolean do_section_details = FALSE;
243static bfd_boolean do_segments = FALSE;
244static bfd_boolean do_unwind = FALSE;
245static bfd_boolean do_using_dynamic = FALSE;
246static bfd_boolean do_header = FALSE;
247static bfd_boolean do_dump = FALSE;
248static bfd_boolean do_version = FALSE;
249static bfd_boolean do_histogram = FALSE;
250static bfd_boolean do_debugging = FALSE;
251static bfd_boolean do_arch = FALSE;
252static bfd_boolean do_notes = FALSE;
253static bfd_boolean do_archive_index = FALSE;
254static bfd_boolean is_32bit_elf = FALSE;
255static bfd_boolean decompress_dumps = FALSE;
256
257struct group_list
258{
259  struct group_list *  next;
260  unsigned int         section_index;
261};
262
263struct group
264{
265  struct group_list *  root;
266  unsigned int         group_index;
267};
268
269static size_t           group_count;
270static struct group *   section_groups;
271static struct group **  section_headers_groups;
272
273/* A dynamic array of flags indicating for which sections a dump
274   has been requested via command line switches.  */
275static Filedata         cmdline;
276
277static struct dump_list_entry * dump_sects_byname;
278
279/* How to print a vma value.  */
280typedef enum print_mode
281{
282  HEX,
283  DEC,
284  DEC_5,
285  UNSIGNED,
286  PREFIX_HEX,
287  FULL_HEX,
288  LONG_HEX
289}
290print_mode;
291
292/* Versioned symbol info.  */
293enum versioned_symbol_info
294{
295  symbol_undefined,
296  symbol_hidden,
297  symbol_public
298};
299
300static const char * get_symbol_version_string
301  (Filedata *, bfd_boolean, const char *, unsigned long, unsigned,
302   Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
303
304#define UNKNOWN -1
305
306#define SECTION_NAME(X)						\
307  ((X) == NULL ? _("<none>")					\
308   : filedata->string_table == NULL ? _("<no-strings>")		\
309   : ((X)->sh_name >= filedata->string_table_length ? _("<corrupt>")	\
310  : filedata->string_table + (X)->sh_name))
311
312#define DT_VERSIONTAGIDX(tag)	(DT_VERNEEDNUM - (tag))	/* Reverse order!  */
313
314#define GET_ELF_SYMBOLS(file, section, sym_count)			\
315  (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count)	\
316   : get_64bit_elf_symbols (file, section, sym_count))
317
318#define VALID_DYNAMIC_NAME(offset)	((dynamic_strings != NULL) && (offset < dynamic_strings_length))
319/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
320   already been called and verified that the string exists.  */
321#define GET_DYNAMIC_NAME(offset)	(dynamic_strings + offset)
322
323#define REMOVE_ARCH_BITS(ADDR)			\
324  do						\
325    {						\
326      if (filedata->file_header.e_machine == EM_ARM)	\
327	(ADDR) &= ~1;				\
328    }						\
329  while (0)
330
331/* Print a BFD_VMA to an internal buffer, for use in error messages.
332   BFD_FMA_FMT can't be used in translated strings.  */
333
334static const char *
335bfd_vmatoa (char *fmtch, bfd_vma value)
336{
337  /* bfd_vmatoa is used more then once in a printf call for output.
338     Cycle through an array of buffers.  */
339  static int buf_pos = 0;
340  static struct bfd_vmatoa_buf
341  {
342    char place[64];
343  } buf[4];
344  char *ret;
345  char fmt[32];
346
347  ret = buf[buf_pos++].place;
348  buf_pos %= ARRAY_SIZE (buf);
349
350  sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
351  snprintf (ret, sizeof (buf[0].place), fmt, value);
352  return ret;
353}
354
355/* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
356   OFFSET + the offset of the current archive member, if we are examining an
357   archive.  Put the retrieved data into VAR, if it is not NULL.  Otherwise
358   allocate a buffer using malloc and fill that.  In either case return the
359   pointer to the start of the retrieved data or NULL if something went wrong.
360   If something does go wrong and REASON is not NULL then emit an error
361   message using REASON as part of the context.  */
362
363static void *
364get_data (void *         var,
365	  Filedata *     filedata,
366	  unsigned long  offset,
367	  bfd_size_type  size,
368	  bfd_size_type  nmemb,
369	  const char *   reason)
370{
371  void * mvar;
372  bfd_size_type amt = size * nmemb;
373
374  if (size == 0 || nmemb == 0)
375    return NULL;
376
377  /* If the size_t type is smaller than the bfd_size_type, eg because
378     you are building a 32-bit tool on a 64-bit host, then make sure
379     that when the sizes are cast to (size_t) no information is lost.  */
380  if (sizeof (size_t) < sizeof (bfd_size_type)
381      && (   (bfd_size_type) ((size_t) size) != size
382	  || (bfd_size_type) ((size_t) nmemb) != nmemb))
383    {
384      if (reason)
385	error (_("Size truncation prevents reading %s"
386		 " elements of size %s for %s\n"),
387	       bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
388      return NULL;
389    }
390
391  /* Check for size overflow.  */
392  if (amt < nmemb)
393    {
394      if (reason)
395	error (_("Size overflow prevents reading %s"
396		 " elements of size %s for %s\n"),
397	       bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
398      return NULL;
399    }
400
401  /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
402     attempting to allocate memory when the read is bound to fail.  */
403  if (amt > filedata->file_size
404      || offset + archive_file_offset + amt > filedata->file_size)
405    {
406      if (reason)
407	error (_("Reading %s bytes extends past end of file for %s\n"),
408	       bfd_vmatoa ("u", amt), reason);
409      return NULL;
410    }
411
412  if (fseek (filedata->handle, archive_file_offset + offset, SEEK_SET))
413    {
414      if (reason)
415	error (_("Unable to seek to 0x%lx for %s\n"),
416	       archive_file_offset + offset, reason);
417      return NULL;
418    }
419
420  mvar = var;
421  if (mvar == NULL)
422    {
423      /* Check for overflow.  */
424      if (nmemb < (~(bfd_size_type) 0 - 1) / size)
425	/* + 1 so that we can '\0' terminate invalid string table sections.  */
426	mvar = malloc ((size_t) amt + 1);
427
428      if (mvar == NULL)
429	{
430	  if (reason)
431	    error (_("Out of memory allocating %s bytes for %s\n"),
432		   bfd_vmatoa ("u", amt), reason);
433	  return NULL;
434	}
435
436      ((char *) mvar)[amt] = '\0';
437    }
438
439  if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
440    {
441      if (reason)
442	error (_("Unable to read in %s bytes of %s\n"),
443	       bfd_vmatoa ("u", amt), reason);
444      if (mvar != var)
445	free (mvar);
446      return NULL;
447    }
448
449  return mvar;
450}
451
452/* Print a VMA value in the MODE specified.
453   Returns the number of characters displayed.  */
454
455static unsigned int
456print_vma (bfd_vma vma, print_mode mode)
457{
458  unsigned int nc = 0;
459
460  switch (mode)
461    {
462    case FULL_HEX:
463      nc = printf ("0x");
464      /* Fall through.  */
465    case LONG_HEX:
466#ifdef BFD64
467      if (is_32bit_elf)
468	return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
469#endif
470      printf_vma (vma);
471      return nc + 16;
472
473    case DEC_5:
474      if (vma <= 99999)
475	return printf ("%5" BFD_VMA_FMT "d", vma);
476      /* Fall through.  */
477    case PREFIX_HEX:
478      nc = printf ("0x");
479      /* Fall through.  */
480    case HEX:
481      return nc + printf ("%" BFD_VMA_FMT "x", vma);
482
483    case DEC:
484      return printf ("%" BFD_VMA_FMT "d", vma);
485
486    case UNSIGNED:
487      return printf ("%" BFD_VMA_FMT "u", vma);
488
489    default:
490      /* FIXME: Report unrecognised mode ?  */
491      return 0;
492    }
493}
494
495/* Display a symbol on stdout.  Handles the display of control characters and
496   multibye characters (assuming the host environment supports them).
497
498   Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
499
500   If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
501   padding as necessary.
502
503   Returns the number of emitted characters.  */
504
505static unsigned int
506print_symbol (signed int width, const char *symbol)
507{
508  bfd_boolean extra_padding = FALSE;
509  signed int num_printed = 0;
510#ifdef HAVE_MBSTATE_T
511  mbstate_t state;
512#endif
513  unsigned int width_remaining;
514
515  if (width < 0)
516    {
517      /* Keep the width positive.  This helps the code below.  */
518      width = - width;
519      extra_padding = TRUE;
520    }
521  else if (width == 0)
522    return 0;
523
524  if (do_wide)
525    /* Set the remaining width to a very large value.
526       This simplifies the code below.  */
527    width_remaining = INT_MAX;
528  else
529    width_remaining = width;
530
531#ifdef HAVE_MBSTATE_T
532  /* Initialise the multibyte conversion state.  */
533  memset (& state, 0, sizeof (state));
534#endif
535
536  while (width_remaining)
537    {
538      size_t  n;
539      const char c = *symbol++;
540
541      if (c == 0)
542	break;
543
544      /* Do not print control characters directly as they can affect terminal
545	 settings.  Such characters usually appear in the names generated
546	 by the assembler for local labels.  */
547      if (ISCNTRL (c))
548	{
549	  if (width_remaining < 2)
550	    break;
551
552	  printf ("^%c", c + 0x40);
553	  width_remaining -= 2;
554	  num_printed += 2;
555	}
556      else if (ISPRINT (c))
557	{
558	  putchar (c);
559	  width_remaining --;
560	  num_printed ++;
561	}
562      else
563	{
564#ifdef HAVE_MBSTATE_T
565	  wchar_t w;
566#endif
567	  /* Let printf do the hard work of displaying multibyte characters.  */
568	  printf ("%.1s", symbol - 1);
569	  width_remaining --;
570	  num_printed ++;
571
572#ifdef HAVE_MBSTATE_T
573	  /* Try to find out how many bytes made up the character that was
574	     just printed.  Advance the symbol pointer past the bytes that
575	     were displayed.  */
576	  n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
577#else
578	  n = 1;
579#endif
580	  if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
581	    symbol += (n - 1);
582	}
583    }
584
585  if (extra_padding && num_printed < width)
586    {
587      /* Fill in the remaining spaces.  */
588      printf ("%-*s", width - num_printed, " ");
589      num_printed = width;
590    }
591
592  return num_printed;
593}
594
595/* Returns a pointer to a static buffer containing a printable version of
596   the given section's name.  Like print_symbol, except that it does not try
597   to print multibyte characters, it just interprets them as hex values.  */
598
599static const char *
600printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
601{
602#define MAX_PRINT_SEC_NAME_LEN 128
603  static char  sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
604  const char * name = SECTION_NAME (sec);
605  char *       buf = sec_name_buf;
606  char         c;
607  unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
608
609  while ((c = * name ++) != 0)
610    {
611      if (ISCNTRL (c))
612	{
613	  if (remaining < 2)
614	    break;
615
616	  * buf ++ = '^';
617	  * buf ++ = c + 0x40;
618	  remaining -= 2;
619	}
620      else if (ISPRINT (c))
621	{
622	  * buf ++ = c;
623	  remaining -= 1;
624	}
625      else
626	{
627	  static char hex[17] = "0123456789ABCDEF";
628
629	  if (remaining < 4)
630	    break;
631	  * buf ++ = '<';
632	  * buf ++ = hex[(c & 0xf0) >> 4];
633	  * buf ++ = hex[c & 0x0f];
634	  * buf ++ = '>';
635	  remaining -= 4;
636	}
637
638      if (remaining == 0)
639	break;
640    }
641
642  * buf = 0;
643  return sec_name_buf;
644}
645
646static const char *
647printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
648{
649  if (ndx >= filedata->file_header.e_shnum)
650    return _("<corrupt>");
651
652  return printable_section_name (filedata, filedata->section_headers + ndx);
653}
654
655/* Return a pointer to section NAME, or NULL if no such section exists.  */
656
657static Elf_Internal_Shdr *
658find_section (Filedata * filedata, const char * name)
659{
660  unsigned int i;
661
662  if (filedata->section_headers == NULL)
663    return NULL;
664
665  for (i = 0; i < filedata->file_header.e_shnum; i++)
666    if (streq (SECTION_NAME (filedata->section_headers + i), name))
667      return filedata->section_headers + i;
668
669  return NULL;
670}
671
672/* Return a pointer to a section containing ADDR, or NULL if no such
673   section exists.  */
674
675static Elf_Internal_Shdr *
676find_section_by_address (Filedata * filedata, bfd_vma addr)
677{
678  unsigned int i;
679
680  if (filedata->section_headers == NULL)
681    return NULL;
682
683  for (i = 0; i < filedata->file_header.e_shnum; i++)
684    {
685      Elf_Internal_Shdr *sec = filedata->section_headers + i;
686
687      if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
688	return sec;
689    }
690
691  return NULL;
692}
693
694static Elf_Internal_Shdr *
695find_section_by_type (Filedata * filedata, unsigned int type)
696{
697  unsigned int i;
698
699  if (filedata->section_headers == NULL)
700    return NULL;
701
702  for (i = 0; i < filedata->file_header.e_shnum; i++)
703    {
704      Elf_Internal_Shdr *sec = filedata->section_headers + i;
705
706      if (sec->sh_type == type)
707	return sec;
708    }
709
710  return NULL;
711}
712
713/* Return a pointer to section NAME, or NULL if no such section exists,
714   restricted to the list of sections given in SET.  */
715
716static Elf_Internal_Shdr *
717find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
718{
719  unsigned int i;
720
721  if (filedata->section_headers == NULL)
722    return NULL;
723
724  if (set != NULL)
725    {
726      while ((i = *set++) > 0)
727	{
728	  /* See PR 21156 for a reproducer.  */
729	  if (i >= filedata->file_header.e_shnum)
730	    continue; /* FIXME: Should we issue an error message ?  */
731
732	  if (streq (SECTION_NAME (filedata->section_headers + i), name))
733	    return filedata->section_headers + i;
734	}
735    }
736
737  return find_section (filedata, name);
738}
739
740/* Read an unsigned LEB128 encoded value from DATA.
741   Set *LENGTH_RETURN to the number of bytes read.  */
742
743static inline unsigned long
744read_uleb128 (unsigned char * data,
745	      unsigned int * length_return,
746	      const unsigned char * const end)
747{
748  return read_leb128 (data, length_return, FALSE, end);
749}
750
751/* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
752   This OS has so many departures from the ELF standard that we test it at
753   many places.  */
754
755static inline bfd_boolean
756is_ia64_vms (Filedata * filedata)
757{
758  return filedata->file_header.e_machine == EM_IA_64
759    && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
760}
761
762/* Guess the relocation size commonly used by the specific machines.  */
763
764static bfd_boolean
765guess_is_rela (unsigned int e_machine)
766{
767  switch (e_machine)
768    {
769      /* Targets that use REL relocations.  */
770    case EM_386:
771    case EM_IAMCU:
772    case EM_960:
773    case EM_ARM:
774    case EM_D10V:
775    case EM_CYGNUS_D10V:
776    case EM_DLX:
777    case EM_MIPS:
778    case EM_MIPS_RS3_LE:
779    case EM_CYGNUS_M32R:
780    case EM_SCORE:
781    case EM_XGATE:
782    case EM_NFP:
783      return FALSE;
784
785      /* Targets that use RELA relocations.  */
786    case EM_68K:
787    case EM_860:
788    case EM_AARCH64:
789    case EM_ADAPTEVA_EPIPHANY:
790    case EM_ALPHA:
791    case EM_ALTERA_NIOS2:
792    case EM_ARC:
793    case EM_ARC_COMPACT:
794    case EM_ARC_COMPACT2:
795    case EM_AVR:
796    case EM_AVR_OLD:
797    case EM_BLACKFIN:
798    case EM_CR16:
799    case EM_CRIS:
800    case EM_CRX:
801    case EM_D30V:
802    case EM_CYGNUS_D30V:
803    case EM_FR30:
804    case EM_FT32:
805    case EM_CYGNUS_FR30:
806    case EM_CYGNUS_FRV:
807    case EM_H8S:
808    case EM_H8_300:
809    case EM_H8_300H:
810    case EM_IA_64:
811    case EM_IP2K:
812    case EM_IP2K_OLD:
813    case EM_IQ2000:
814    case EM_LATTICEMICO32:
815    case EM_M32C_OLD:
816    case EM_M32C:
817    case EM_M32R:
818    case EM_MCORE:
819    case EM_CYGNUS_MEP:
820    case EM_METAG:
821    case EM_MMIX:
822    case EM_MN10200:
823    case EM_CYGNUS_MN10200:
824    case EM_MN10300:
825    case EM_CYGNUS_MN10300:
826    case EM_MOXIE:
827    case EM_MSP430:
828    case EM_MSP430_OLD:
829    case EM_MT:
830    case EM_NDS32:
831    case EM_NIOS32:
832    case EM_OR1K:
833    case EM_PPC64:
834    case EM_PPC:
835    case EM_TI_PRU:
836    case EM_RISCV:
837    case EM_RL78:
838    case EM_RX:
839    case EM_S390:
840    case EM_S390_OLD:
841    case EM_SH:
842    case EM_SPARC:
843    case EM_SPARC32PLUS:
844    case EM_SPARCV9:
845    case EM_SPU:
846    case EM_TI_C6000:
847    case EM_TILEGX:
848    case EM_TILEPRO:
849    case EM_V800:
850    case EM_V850:
851    case EM_CYGNUS_V850:
852    case EM_VAX:
853    case EM_VISIUM:
854    case EM_X86_64:
855    case EM_L1OM:
856    case EM_K1OM:
857    case EM_XSTORMY16:
858    case EM_XTENSA:
859    case EM_XTENSA_OLD:
860    case EM_MICROBLAZE:
861    case EM_MICROBLAZE_OLD:
862    case EM_WEBASSEMBLY:
863      return TRUE;
864
865    case EM_68HC05:
866    case EM_68HC08:
867    case EM_68HC11:
868    case EM_68HC16:
869    case EM_FX66:
870    case EM_ME16:
871    case EM_MMA:
872    case EM_NCPU:
873    case EM_NDR1:
874    case EM_PCP:
875    case EM_ST100:
876    case EM_ST19:
877    case EM_ST7:
878    case EM_ST9PLUS:
879    case EM_STARCORE:
880    case EM_SVX:
881    case EM_TINYJ:
882    default:
883      warn (_("Don't know about relocations on this machine architecture\n"));
884      return FALSE;
885    }
886}
887
888/* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
889   Returns TRUE upon success, FALSE otherwise.  If successful then a
890   pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
891   and the number of relocs loaded is placed in *NRELASP.  It is the caller's
892   responsibility to free the allocated buffer.  */
893
894static bfd_boolean
895slurp_rela_relocs (Filedata *            filedata,
896		   unsigned long         rel_offset,
897		   unsigned long         rel_size,
898		   Elf_Internal_Rela **  relasp,
899		   unsigned long *       nrelasp)
900{
901  Elf_Internal_Rela * relas;
902  size_t nrelas;
903  unsigned int i;
904
905  if (is_32bit_elf)
906    {
907      Elf32_External_Rela * erelas;
908
909      erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
910                                                 rel_size, _("32-bit relocation data"));
911      if (!erelas)
912	return FALSE;
913
914      nrelas = rel_size / sizeof (Elf32_External_Rela);
915
916      relas = (Elf_Internal_Rela *) cmalloc (nrelas,
917                                             sizeof (Elf_Internal_Rela));
918
919      if (relas == NULL)
920	{
921	  free (erelas);
922	  error (_("out of memory parsing relocs\n"));
923	  return FALSE;
924	}
925
926      for (i = 0; i < nrelas; i++)
927	{
928	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
929	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
930	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
931	}
932
933      free (erelas);
934    }
935  else
936    {
937      Elf64_External_Rela * erelas;
938
939      erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
940                                                 rel_size, _("64-bit relocation data"));
941      if (!erelas)
942	return FALSE;
943
944      nrelas = rel_size / sizeof (Elf64_External_Rela);
945
946      relas = (Elf_Internal_Rela *) cmalloc (nrelas,
947                                             sizeof (Elf_Internal_Rela));
948
949      if (relas == NULL)
950	{
951	  free (erelas);
952	  error (_("out of memory parsing relocs\n"));
953	  return FALSE;
954	}
955
956      for (i = 0; i < nrelas; i++)
957	{
958	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
959	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
960	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
961
962	  /* The #ifdef BFD64 below is to prevent a compile time
963	     warning.  We know that if we do not have a 64 bit data
964	     type that we will never execute this code anyway.  */
965#ifdef BFD64
966	  if (filedata->file_header.e_machine == EM_MIPS
967	      && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
968	    {
969	      /* In little-endian objects, r_info isn't really a
970		 64-bit little-endian value: it has a 32-bit
971		 little-endian symbol index followed by four
972		 individual byte fields.  Reorder INFO
973		 accordingly.  */
974	      bfd_vma inf = relas[i].r_info;
975	      inf = (((inf & 0xffffffff) << 32)
976		      | ((inf >> 56) & 0xff)
977		      | ((inf >> 40) & 0xff00)
978		      | ((inf >> 24) & 0xff0000)
979		      | ((inf >> 8) & 0xff000000));
980	      relas[i].r_info = inf;
981	    }
982#endif /* BFD64 */
983	}
984
985      free (erelas);
986    }
987
988  *relasp = relas;
989  *nrelasp = nrelas;
990  return TRUE;
991}
992
993/* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
994   Returns TRUE upon success, FALSE otherwise.  If successful then a
995   pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
996   and the number of relocs loaded is placed in *NRELSP.  It is the caller's
997   responsibility to free the allocated buffer.  */
998
999static bfd_boolean
1000slurp_rel_relocs (Filedata *            filedata,
1001		  unsigned long         rel_offset,
1002		  unsigned long         rel_size,
1003		  Elf_Internal_Rela **  relsp,
1004		  unsigned long *       nrelsp)
1005{
1006  Elf_Internal_Rela * rels;
1007  size_t nrels;
1008  unsigned int i;
1009
1010  if (is_32bit_elf)
1011    {
1012      Elf32_External_Rel * erels;
1013
1014      erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1015                                               rel_size, _("32-bit relocation data"));
1016      if (!erels)
1017	return FALSE;
1018
1019      nrels = rel_size / sizeof (Elf32_External_Rel);
1020
1021      rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1022
1023      if (rels == NULL)
1024	{
1025	  free (erels);
1026	  error (_("out of memory parsing relocs\n"));
1027	  return FALSE;
1028	}
1029
1030      for (i = 0; i < nrels; i++)
1031	{
1032	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1033	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
1034	  rels[i].r_addend = 0;
1035	}
1036
1037      free (erels);
1038    }
1039  else
1040    {
1041      Elf64_External_Rel * erels;
1042
1043      erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1044                                               rel_size, _("64-bit relocation data"));
1045      if (!erels)
1046	return FALSE;
1047
1048      nrels = rel_size / sizeof (Elf64_External_Rel);
1049
1050      rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1051
1052      if (rels == NULL)
1053	{
1054	  free (erels);
1055	  error (_("out of memory parsing relocs\n"));
1056	  return FALSE;
1057	}
1058
1059      for (i = 0; i < nrels; i++)
1060	{
1061	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1062	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
1063	  rels[i].r_addend = 0;
1064
1065	  /* The #ifdef BFD64 below is to prevent a compile time
1066	     warning.  We know that if we do not have a 64 bit data
1067	     type that we will never execute this code anyway.  */
1068#ifdef BFD64
1069	  if (filedata->file_header.e_machine == EM_MIPS
1070	      && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1071	    {
1072	      /* In little-endian objects, r_info isn't really a
1073		 64-bit little-endian value: it has a 32-bit
1074		 little-endian symbol index followed by four
1075		 individual byte fields.  Reorder INFO
1076		 accordingly.  */
1077	      bfd_vma inf = rels[i].r_info;
1078	      inf = (((inf & 0xffffffff) << 32)
1079		     | ((inf >> 56) & 0xff)
1080		     | ((inf >> 40) & 0xff00)
1081		     | ((inf >> 24) & 0xff0000)
1082		     | ((inf >> 8) & 0xff000000));
1083	      rels[i].r_info = inf;
1084	    }
1085#endif /* BFD64 */
1086	}
1087
1088      free (erels);
1089    }
1090
1091  *relsp = rels;
1092  *nrelsp = nrels;
1093  return TRUE;
1094}
1095
1096/* Returns the reloc type extracted from the reloc info field.  */
1097
1098static unsigned int
1099get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1100{
1101  if (is_32bit_elf)
1102    return ELF32_R_TYPE (reloc_info);
1103
1104  switch (filedata->file_header.e_machine)
1105    {
1106    case EM_MIPS:
1107      /* Note: We assume that reloc_info has already been adjusted for us.  */
1108      return ELF64_MIPS_R_TYPE (reloc_info);
1109
1110    case EM_SPARCV9:
1111      return ELF64_R_TYPE_ID (reloc_info);
1112
1113    default:
1114      return ELF64_R_TYPE (reloc_info);
1115    }
1116}
1117
1118/* Return the symbol index extracted from the reloc info field.  */
1119
1120static bfd_vma
1121get_reloc_symindex (bfd_vma reloc_info)
1122{
1123  return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1124}
1125
1126static inline bfd_boolean
1127uses_msp430x_relocs (Filedata * filedata)
1128{
1129  return
1130    filedata->file_header.e_machine == EM_MSP430 /* Paranoia.  */
1131    /* GCC uses osabi == ELFOSBI_STANDALONE.  */
1132    && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1133	/* TI compiler uses ELFOSABI_NONE.  */
1134	|| (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1135}
1136
1137/* Display the contents of the relocation data found at the specified
1138   offset.  */
1139
1140static bfd_boolean
1141dump_relocations (Filedata *          filedata,
1142		  unsigned long       rel_offset,
1143		  unsigned long       rel_size,
1144		  Elf_Internal_Sym *  symtab,
1145		  unsigned long       nsyms,
1146		  char *              strtab,
1147		  unsigned long       strtablen,
1148		  int                 is_rela,
1149		  bfd_boolean         is_dynsym)
1150{
1151  unsigned long i;
1152  Elf_Internal_Rela * rels;
1153  bfd_boolean res = TRUE;
1154
1155  if (is_rela == UNKNOWN)
1156    is_rela = guess_is_rela (filedata->file_header.e_machine);
1157
1158  if (is_rela)
1159    {
1160      if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1161	return FALSE;
1162    }
1163  else
1164    {
1165      if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1166	return FALSE;
1167    }
1168
1169  if (is_32bit_elf)
1170    {
1171      if (is_rela)
1172	{
1173	  if (do_wide)
1174	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name + Addend\n"));
1175	  else
1176	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name + Addend\n"));
1177	}
1178      else
1179	{
1180	  if (do_wide)
1181	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name\n"));
1182	  else
1183	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name\n"));
1184	}
1185    }
1186  else
1187    {
1188      if (is_rela)
1189	{
1190	  if (do_wide)
1191	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend\n"));
1192	  else
1193	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name + Addend\n"));
1194	}
1195      else
1196	{
1197	  if (do_wide)
1198	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name\n"));
1199	  else
1200	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name\n"));
1201	}
1202    }
1203
1204  for (i = 0; i < rel_size; i++)
1205    {
1206      const char * rtype;
1207      bfd_vma offset;
1208      bfd_vma inf;
1209      bfd_vma symtab_index;
1210      bfd_vma type;
1211
1212      offset = rels[i].r_offset;
1213      inf    = rels[i].r_info;
1214
1215      type = get_reloc_type (filedata, inf);
1216      symtab_index = get_reloc_symindex  (inf);
1217
1218      if (is_32bit_elf)
1219	{
1220	  printf ("%8.8lx  %8.8lx ",
1221		  (unsigned long) offset & 0xffffffff,
1222		  (unsigned long) inf & 0xffffffff);
1223	}
1224      else
1225	{
1226#if BFD_HOST_64BIT_LONG
1227	  printf (do_wide
1228		  ? "%16.16lx  %16.16lx "
1229		  : "%12.12lx  %12.12lx ",
1230		  offset, inf);
1231#elif BFD_HOST_64BIT_LONG_LONG
1232#ifndef __MSVCRT__
1233	  printf (do_wide
1234		  ? "%16.16llx  %16.16llx "
1235		  : "%12.12llx  %12.12llx ",
1236		  offset, inf);
1237#else
1238	  printf (do_wide
1239		  ? "%16.16I64x  %16.16I64x "
1240		  : "%12.12I64x  %12.12I64x ",
1241		  offset, inf);
1242#endif
1243#else
1244	  printf (do_wide
1245		  ? "%8.8lx%8.8lx  %8.8lx%8.8lx "
1246		  : "%4.4lx%8.8lx  %4.4lx%8.8lx ",
1247		  _bfd_int64_high (offset),
1248		  _bfd_int64_low (offset),
1249		  _bfd_int64_high (inf),
1250		  _bfd_int64_low (inf));
1251#endif
1252	}
1253
1254      switch (filedata->file_header.e_machine)
1255	{
1256	default:
1257	  rtype = NULL;
1258	  break;
1259
1260	case EM_AARCH64:
1261	  rtype = elf_aarch64_reloc_type (type);
1262	  break;
1263
1264	case EM_M32R:
1265	case EM_CYGNUS_M32R:
1266	  rtype = elf_m32r_reloc_type (type);
1267	  break;
1268
1269	case EM_386:
1270	case EM_IAMCU:
1271	  rtype = elf_i386_reloc_type (type);
1272	  break;
1273
1274	case EM_68HC11:
1275	case EM_68HC12:
1276	  rtype = elf_m68hc11_reloc_type (type);
1277	  break;
1278
1279	case EM_S12Z:
1280	  rtype = elf_s12z_reloc_type (type);
1281	  break;
1282
1283	case EM_68K:
1284	  rtype = elf_m68k_reloc_type (type);
1285	  break;
1286
1287	case EM_960:
1288	  rtype = elf_i960_reloc_type (type);
1289	  break;
1290
1291	case EM_AVR:
1292	case EM_AVR_OLD:
1293	  rtype = elf_avr_reloc_type (type);
1294	  break;
1295
1296	case EM_OLD_SPARCV9:
1297	case EM_SPARC32PLUS:
1298	case EM_SPARCV9:
1299	case EM_SPARC:
1300	  rtype = elf_sparc_reloc_type (type);
1301	  break;
1302
1303	case EM_SPU:
1304	  rtype = elf_spu_reloc_type (type);
1305	  break;
1306
1307	case EM_V800:
1308	  rtype = v800_reloc_type (type);
1309	  break;
1310	case EM_V850:
1311	case EM_CYGNUS_V850:
1312	  rtype = v850_reloc_type (type);
1313	  break;
1314
1315	case EM_D10V:
1316	case EM_CYGNUS_D10V:
1317	  rtype = elf_d10v_reloc_type (type);
1318	  break;
1319
1320	case EM_D30V:
1321	case EM_CYGNUS_D30V:
1322	  rtype = elf_d30v_reloc_type (type);
1323	  break;
1324
1325	case EM_DLX:
1326	  rtype = elf_dlx_reloc_type (type);
1327	  break;
1328
1329	case EM_SH:
1330	  rtype = elf_sh_reloc_type (type);
1331	  break;
1332
1333	case EM_MN10300:
1334	case EM_CYGNUS_MN10300:
1335	  rtype = elf_mn10300_reloc_type (type);
1336	  break;
1337
1338	case EM_MN10200:
1339	case EM_CYGNUS_MN10200:
1340	  rtype = elf_mn10200_reloc_type (type);
1341	  break;
1342
1343	case EM_FR30:
1344	case EM_CYGNUS_FR30:
1345	  rtype = elf_fr30_reloc_type (type);
1346	  break;
1347
1348	case EM_CYGNUS_FRV:
1349	  rtype = elf_frv_reloc_type (type);
1350	  break;
1351
1352	case EM_FT32:
1353	  rtype = elf_ft32_reloc_type (type);
1354	  break;
1355
1356	case EM_MCORE:
1357	  rtype = elf_mcore_reloc_type (type);
1358	  break;
1359
1360	case EM_MMIX:
1361	  rtype = elf_mmix_reloc_type (type);
1362	  break;
1363
1364	case EM_MOXIE:
1365	  rtype = elf_moxie_reloc_type (type);
1366	  break;
1367
1368	case EM_MSP430:
1369	  if (uses_msp430x_relocs (filedata))
1370	    {
1371	      rtype = elf_msp430x_reloc_type (type);
1372	      break;
1373	    }
1374	  /* Fall through.  */
1375	case EM_MSP430_OLD:
1376	  rtype = elf_msp430_reloc_type (type);
1377	  break;
1378
1379	case EM_NDS32:
1380	  rtype = elf_nds32_reloc_type (type);
1381	  break;
1382
1383	case EM_PPC:
1384	  rtype = elf_ppc_reloc_type (type);
1385	  break;
1386
1387	case EM_PPC64:
1388	  rtype = elf_ppc64_reloc_type (type);
1389	  break;
1390
1391	case EM_MIPS:
1392	case EM_MIPS_RS3_LE:
1393	  rtype = elf_mips_reloc_type (type);
1394	  break;
1395
1396	case EM_RISCV:
1397	  rtype = elf_riscv_reloc_type (type);
1398	  break;
1399
1400	case EM_ALPHA:
1401	  rtype = elf_alpha_reloc_type (type);
1402	  break;
1403
1404	case EM_ARM:
1405	  rtype = elf_arm_reloc_type (type);
1406	  break;
1407
1408	case EM_ARC:
1409	case EM_ARC_COMPACT:
1410	case EM_ARC_COMPACT2:
1411	  rtype = elf_arc_reloc_type (type);
1412	  break;
1413
1414	case EM_PARISC:
1415	  rtype = elf_hppa_reloc_type (type);
1416	  break;
1417
1418	case EM_H8_300:
1419	case EM_H8_300H:
1420	case EM_H8S:
1421	  rtype = elf_h8_reloc_type (type);
1422	  break;
1423
1424	case EM_OR1K:
1425	  rtype = elf_or1k_reloc_type (type);
1426	  break;
1427
1428	case EM_PJ:
1429	case EM_PJ_OLD:
1430	  rtype = elf_pj_reloc_type (type);
1431	  break;
1432	case EM_IA_64:
1433	  rtype = elf_ia64_reloc_type (type);
1434	  break;
1435
1436	case EM_CRIS:
1437	  rtype = elf_cris_reloc_type (type);
1438	  break;
1439
1440	case EM_860:
1441	  rtype = elf_i860_reloc_type (type);
1442	  break;
1443
1444	case EM_X86_64:
1445	case EM_L1OM:
1446	case EM_K1OM:
1447	  rtype = elf_x86_64_reloc_type (type);
1448	  break;
1449
1450	case EM_S370:
1451	  rtype = i370_reloc_type (type);
1452	  break;
1453
1454	case EM_S390_OLD:
1455	case EM_S390:
1456	  rtype = elf_s390_reloc_type (type);
1457	  break;
1458
1459	case EM_SCORE:
1460	  rtype = elf_score_reloc_type (type);
1461	  break;
1462
1463	case EM_XSTORMY16:
1464	  rtype = elf_xstormy16_reloc_type (type);
1465	  break;
1466
1467	case EM_CRX:
1468	  rtype = elf_crx_reloc_type (type);
1469	  break;
1470
1471	case EM_VAX:
1472	  rtype = elf_vax_reloc_type (type);
1473	  break;
1474
1475	case EM_VISIUM:
1476	  rtype = elf_visium_reloc_type (type);
1477	  break;
1478
1479	case EM_ADAPTEVA_EPIPHANY:
1480	  rtype = elf_epiphany_reloc_type (type);
1481	  break;
1482
1483	case EM_IP2K:
1484	case EM_IP2K_OLD:
1485	  rtype = elf_ip2k_reloc_type (type);
1486	  break;
1487
1488	case EM_IQ2000:
1489	  rtype = elf_iq2000_reloc_type (type);
1490	  break;
1491
1492	case EM_XTENSA_OLD:
1493	case EM_XTENSA:
1494	  rtype = elf_xtensa_reloc_type (type);
1495	  break;
1496
1497	case EM_LATTICEMICO32:
1498	  rtype = elf_lm32_reloc_type (type);
1499	  break;
1500
1501	case EM_M32C_OLD:
1502	case EM_M32C:
1503	  rtype = elf_m32c_reloc_type (type);
1504	  break;
1505
1506	case EM_MT:
1507	  rtype = elf_mt_reloc_type (type);
1508	  break;
1509
1510	case EM_BLACKFIN:
1511	  rtype = elf_bfin_reloc_type (type);
1512	  break;
1513
1514	case EM_CYGNUS_MEP:
1515	  rtype = elf_mep_reloc_type (type);
1516	  break;
1517
1518	case EM_CR16:
1519	  rtype = elf_cr16_reloc_type (type);
1520	  break;
1521
1522	case EM_MICROBLAZE:
1523	case EM_MICROBLAZE_OLD:
1524	  rtype = elf_microblaze_reloc_type (type);
1525	  break;
1526
1527	case EM_RL78:
1528	  rtype = elf_rl78_reloc_type (type);
1529	  break;
1530
1531	case EM_RX:
1532	  rtype = elf_rx_reloc_type (type);
1533	  break;
1534
1535	case EM_METAG:
1536	  rtype = elf_metag_reloc_type (type);
1537	  break;
1538
1539	case EM_XC16X:
1540	case EM_C166:
1541	  rtype = elf_xc16x_reloc_type (type);
1542	  break;
1543
1544	case EM_TI_C6000:
1545	  rtype = elf_tic6x_reloc_type (type);
1546	  break;
1547
1548	case EM_TILEGX:
1549	  rtype = elf_tilegx_reloc_type (type);
1550	  break;
1551
1552	case EM_TILEPRO:
1553	  rtype = elf_tilepro_reloc_type (type);
1554	  break;
1555
1556	case EM_WEBASSEMBLY:
1557	  rtype = elf_wasm32_reloc_type (type);
1558	  break;
1559
1560	case EM_XGATE:
1561	  rtype = elf_xgate_reloc_type (type);
1562	  break;
1563
1564	case EM_ALTERA_NIOS2:
1565	  rtype = elf_nios2_reloc_type (type);
1566	  break;
1567
1568	case EM_TI_PRU:
1569	  rtype = elf_pru_reloc_type (type);
1570	  break;
1571
1572	case EM_NFP:
1573	  if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1574	    rtype = elf_nfp3200_reloc_type (type);
1575	  else
1576	    rtype = elf_nfp_reloc_type (type);
1577	  break;
1578	}
1579
1580      if (rtype == NULL)
1581	printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1582      else
1583	printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1584
1585      if (filedata->file_header.e_machine == EM_ALPHA
1586	  && rtype != NULL
1587	  && streq (rtype, "R_ALPHA_LITUSE")
1588	  && is_rela)
1589	{
1590	  switch (rels[i].r_addend)
1591	    {
1592	    case LITUSE_ALPHA_ADDR:   rtype = "ADDR";   break;
1593	    case LITUSE_ALPHA_BASE:   rtype = "BASE";   break;
1594	    case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1595	    case LITUSE_ALPHA_JSR:    rtype = "JSR";    break;
1596	    case LITUSE_ALPHA_TLSGD:  rtype = "TLSGD";  break;
1597	    case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1598	    case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1599	    default: rtype = NULL;
1600	    }
1601
1602	  if (rtype)
1603	    printf (" (%s)", rtype);
1604	  else
1605	    {
1606	      putchar (' ');
1607	      printf (_("<unknown addend: %lx>"),
1608		      (unsigned long) rels[i].r_addend);
1609	      res = FALSE;
1610	    }
1611	}
1612      else if (symtab_index)
1613	{
1614	  if (symtab == NULL || symtab_index >= nsyms)
1615	    {
1616	      error (_(" bad symbol index: %08lx in reloc"), (unsigned long) symtab_index);
1617	      res = FALSE;
1618	    }
1619	  else
1620	    {
1621	      Elf_Internal_Sym * psym;
1622	      const char * version_string;
1623	      enum versioned_symbol_info sym_info;
1624	      unsigned short vna_other;
1625
1626	      psym = symtab + symtab_index;
1627
1628	      version_string
1629		= get_symbol_version_string (filedata, is_dynsym,
1630					     strtab, strtablen,
1631					     symtab_index,
1632					     psym,
1633					     &sym_info,
1634					     &vna_other);
1635
1636	      printf (" ");
1637
1638	      if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1639		{
1640		  const char * name;
1641		  unsigned int len;
1642		  unsigned int width = is_32bit_elf ? 8 : 14;
1643
1644		  /* Relocations against GNU_IFUNC symbols do not use the value
1645		     of the symbol as the address to relocate against.  Instead
1646		     they invoke the function named by the symbol and use its
1647		     result as the address for relocation.
1648
1649		     To indicate this to the user, do not display the value of
1650		     the symbol in the "Symbols's Value" field.  Instead show
1651		     its name followed by () as a hint that the symbol is
1652		     invoked.  */
1653
1654		  if (strtab == NULL
1655		      || psym->st_name == 0
1656		      || psym->st_name >= strtablen)
1657		    name = "??";
1658		  else
1659		    name = strtab + psym->st_name;
1660
1661		  len = print_symbol (width, name);
1662		  if (version_string)
1663		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
1664			    version_string);
1665		  printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1666		}
1667	      else
1668		{
1669		  print_vma (psym->st_value, LONG_HEX);
1670
1671		  printf (is_32bit_elf ? "   " : " ");
1672		}
1673
1674	      if (psym->st_name == 0)
1675		{
1676		  const char * sec_name = "<null>";
1677		  char name_buf[40];
1678
1679		  if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1680		    {
1681		      if (psym->st_shndx < filedata->file_header.e_shnum)
1682			sec_name = SECTION_NAME (filedata->section_headers + psym->st_shndx);
1683		      else if (psym->st_shndx == SHN_ABS)
1684			sec_name = "ABS";
1685		      else if (psym->st_shndx == SHN_COMMON)
1686			sec_name = "COMMON";
1687		      else if ((filedata->file_header.e_machine == EM_MIPS
1688				&& psym->st_shndx == SHN_MIPS_SCOMMON)
1689			       || (filedata->file_header.e_machine == EM_TI_C6000
1690				   && psym->st_shndx == SHN_TIC6X_SCOMMON))
1691			sec_name = "SCOMMON";
1692		      else if (filedata->file_header.e_machine == EM_MIPS
1693			       && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1694			sec_name = "SUNDEF";
1695		      else if ((filedata->file_header.e_machine == EM_X86_64
1696				|| filedata->file_header.e_machine == EM_L1OM
1697				|| filedata->file_header.e_machine == EM_K1OM)
1698			       && psym->st_shndx == SHN_X86_64_LCOMMON)
1699			sec_name = "LARGE_COMMON";
1700		      else if (filedata->file_header.e_machine == EM_IA_64
1701			       && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1702			       && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1703			sec_name = "ANSI_COM";
1704		      else if (is_ia64_vms (filedata)
1705			       && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1706			sec_name = "VMS_SYMVEC";
1707		      else
1708			{
1709			  sprintf (name_buf, "<section 0x%x>",
1710				   (unsigned int) psym->st_shndx);
1711			  sec_name = name_buf;
1712			}
1713		    }
1714		  print_symbol (22, sec_name);
1715		}
1716	      else if (strtab == NULL)
1717		printf (_("<string table index: %3ld>"), psym->st_name);
1718	      else if (psym->st_name >= strtablen)
1719		{
1720		  error (_("<corrupt string table index: %3ld>"), psym->st_name);
1721		  res = FALSE;
1722		}
1723	      else
1724		{
1725		  print_symbol (22, strtab + psym->st_name);
1726		  if (version_string)
1727		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
1728			    version_string);
1729		}
1730
1731	      if (is_rela)
1732		{
1733		  bfd_vma off = rels[i].r_addend;
1734
1735		  if ((bfd_signed_vma) off < 0)
1736		    printf (" - %" BFD_VMA_FMT "x", - off);
1737		  else
1738		    printf (" + %" BFD_VMA_FMT "x", off);
1739		}
1740	    }
1741	}
1742      else if (is_rela)
1743	{
1744	  bfd_vma off = rels[i].r_addend;
1745
1746	  printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1747	  if ((bfd_signed_vma) off < 0)
1748	    printf ("-%" BFD_VMA_FMT "x", - off);
1749	  else
1750	    printf ("%" BFD_VMA_FMT "x", off);
1751	}
1752
1753      if (filedata->file_header.e_machine == EM_SPARCV9
1754	  && rtype != NULL
1755	  && streq (rtype, "R_SPARC_OLO10"))
1756	printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1757
1758      putchar ('\n');
1759
1760#ifdef BFD64
1761      if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
1762	{
1763	  bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1764	  bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1765	  const char * rtype2 = elf_mips_reloc_type (type2);
1766	  const char * rtype3 = elf_mips_reloc_type (type3);
1767
1768	  printf ("                    Type2: ");
1769
1770	  if (rtype2 == NULL)
1771	    printf (_("unrecognized: %-7lx"),
1772		    (unsigned long) type2 & 0xffffffff);
1773	  else
1774	    printf ("%-17.17s", rtype2);
1775
1776	  printf ("\n                    Type3: ");
1777
1778	  if (rtype3 == NULL)
1779	    printf (_("unrecognized: %-7lx"),
1780		    (unsigned long) type3 & 0xffffffff);
1781	  else
1782	    printf ("%-17.17s", rtype3);
1783
1784	  putchar ('\n');
1785	}
1786#endif /* BFD64 */
1787    }
1788
1789  free (rels);
1790
1791  return res;
1792}
1793
1794static const char *
1795get_mips_dynamic_type (unsigned long type)
1796{
1797  switch (type)
1798    {
1799    case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1800    case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1801    case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1802    case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1803    case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1804    case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1805    case DT_MIPS_MSYM: return "MIPS_MSYM";
1806    case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1807    case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1808    case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1809    case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1810    case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1811    case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1812    case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1813    case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1814    case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1815    case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1816    case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1817    case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1818    case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1819    case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1820    case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1821    case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1822    case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1823    case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1824    case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1825    case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1826    case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1827    case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1828    case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1829    case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1830    case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1831    case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1832    case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1833    case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1834    case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1835    case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1836    case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1837    case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1838    case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1839    case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1840    case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1841    case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1842    case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1843    case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1844    case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1845    default:
1846      return NULL;
1847    }
1848}
1849
1850static const char *
1851get_sparc64_dynamic_type (unsigned long type)
1852{
1853  switch (type)
1854    {
1855    case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1856    default:
1857      return NULL;
1858    }
1859}
1860
1861static const char *
1862get_ppc_dynamic_type (unsigned long type)
1863{
1864  switch (type)
1865    {
1866    case DT_PPC_GOT:    return "PPC_GOT";
1867    case DT_PPC_OPT:    return "PPC_OPT";
1868    default:
1869      return NULL;
1870    }
1871}
1872
1873static const char *
1874get_ppc64_dynamic_type (unsigned long type)
1875{
1876  switch (type)
1877    {
1878    case DT_PPC64_GLINK:  return "PPC64_GLINK";
1879    case DT_PPC64_OPD:    return "PPC64_OPD";
1880    case DT_PPC64_OPDSZ:  return "PPC64_OPDSZ";
1881    case DT_PPC64_OPT:    return "PPC64_OPT";
1882    default:
1883      return NULL;
1884    }
1885}
1886
1887static const char *
1888get_parisc_dynamic_type (unsigned long type)
1889{
1890  switch (type)
1891    {
1892    case DT_HP_LOAD_MAP:	return "HP_LOAD_MAP";
1893    case DT_HP_DLD_FLAGS:	return "HP_DLD_FLAGS";
1894    case DT_HP_DLD_HOOK:	return "HP_DLD_HOOK";
1895    case DT_HP_UX10_INIT:	return "HP_UX10_INIT";
1896    case DT_HP_UX10_INITSZ:	return "HP_UX10_INITSZ";
1897    case DT_HP_PREINIT:		return "HP_PREINIT";
1898    case DT_HP_PREINITSZ:	return "HP_PREINITSZ";
1899    case DT_HP_NEEDED:		return "HP_NEEDED";
1900    case DT_HP_TIME_STAMP:	return "HP_TIME_STAMP";
1901    case DT_HP_CHECKSUM:	return "HP_CHECKSUM";
1902    case DT_HP_GST_SIZE:	return "HP_GST_SIZE";
1903    case DT_HP_GST_VERSION:	return "HP_GST_VERSION";
1904    case DT_HP_GST_HASHVAL:	return "HP_GST_HASHVAL";
1905    case DT_HP_EPLTREL:		return "HP_GST_EPLTREL";
1906    case DT_HP_EPLTRELSZ:	return "HP_GST_EPLTRELSZ";
1907    case DT_HP_FILTERED:	return "HP_FILTERED";
1908    case DT_HP_FILTER_TLS:	return "HP_FILTER_TLS";
1909    case DT_HP_COMPAT_FILTERED:	return "HP_COMPAT_FILTERED";
1910    case DT_HP_LAZYLOAD:	return "HP_LAZYLOAD";
1911    case DT_HP_BIND_NOW_COUNT:	return "HP_BIND_NOW_COUNT";
1912    case DT_PLT:		return "PLT";
1913    case DT_PLT_SIZE:		return "PLT_SIZE";
1914    case DT_DLT:		return "DLT";
1915    case DT_DLT_SIZE:		return "DLT_SIZE";
1916    default:
1917      return NULL;
1918    }
1919}
1920
1921static const char *
1922get_ia64_dynamic_type (unsigned long type)
1923{
1924  switch (type)
1925    {
1926    case DT_IA_64_PLT_RESERVE:         return "IA_64_PLT_RESERVE";
1927    case DT_IA_64_VMS_SUBTYPE:         return "VMS_SUBTYPE";
1928    case DT_IA_64_VMS_IMGIOCNT:        return "VMS_IMGIOCNT";
1929    case DT_IA_64_VMS_LNKFLAGS:        return "VMS_LNKFLAGS";
1930    case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1931    case DT_IA_64_VMS_IDENT:           return "VMS_IDENT";
1932    case DT_IA_64_VMS_NEEDED_IDENT:    return "VMS_NEEDED_IDENT";
1933    case DT_IA_64_VMS_IMG_RELA_CNT:    return "VMS_IMG_RELA_CNT";
1934    case DT_IA_64_VMS_SEG_RELA_CNT:    return "VMS_SEG_RELA_CNT";
1935    case DT_IA_64_VMS_FIXUP_RELA_CNT:  return "VMS_FIXUP_RELA_CNT";
1936    case DT_IA_64_VMS_FIXUP_NEEDED:    return "VMS_FIXUP_NEEDED";
1937    case DT_IA_64_VMS_SYMVEC_CNT:      return "VMS_SYMVEC_CNT";
1938    case DT_IA_64_VMS_XLATED:          return "VMS_XLATED";
1939    case DT_IA_64_VMS_STACKSIZE:       return "VMS_STACKSIZE";
1940    case DT_IA_64_VMS_UNWINDSZ:        return "VMS_UNWINDSZ";
1941    case DT_IA_64_VMS_UNWIND_CODSEG:   return "VMS_UNWIND_CODSEG";
1942    case DT_IA_64_VMS_UNWIND_INFOSEG:  return "VMS_UNWIND_INFOSEG";
1943    case DT_IA_64_VMS_LINKTIME:        return "VMS_LINKTIME";
1944    case DT_IA_64_VMS_SEG_NO:          return "VMS_SEG_NO";
1945    case DT_IA_64_VMS_SYMVEC_OFFSET:   return "VMS_SYMVEC_OFFSET";
1946    case DT_IA_64_VMS_SYMVEC_SEG:      return "VMS_SYMVEC_SEG";
1947    case DT_IA_64_VMS_UNWIND_OFFSET:   return "VMS_UNWIND_OFFSET";
1948    case DT_IA_64_VMS_UNWIND_SEG:      return "VMS_UNWIND_SEG";
1949    case DT_IA_64_VMS_STRTAB_OFFSET:   return "VMS_STRTAB_OFFSET";
1950    case DT_IA_64_VMS_SYSVER_OFFSET:   return "VMS_SYSVER_OFFSET";
1951    case DT_IA_64_VMS_IMG_RELA_OFF:    return "VMS_IMG_RELA_OFF";
1952    case DT_IA_64_VMS_SEG_RELA_OFF:    return "VMS_SEG_RELA_OFF";
1953    case DT_IA_64_VMS_FIXUP_RELA_OFF:  return "VMS_FIXUP_RELA_OFF";
1954    case DT_IA_64_VMS_PLTGOT_OFFSET:   return "VMS_PLTGOT_OFFSET";
1955    case DT_IA_64_VMS_PLTGOT_SEG:      return "VMS_PLTGOT_SEG";
1956    case DT_IA_64_VMS_FPMODE:          return "VMS_FPMODE";
1957    default:
1958      return NULL;
1959    }
1960}
1961
1962static const char *
1963get_solaris_section_type (unsigned long type)
1964{
1965  switch (type)
1966    {
1967    case 0x6fffffee: return "SUNW_ancillary";
1968    case 0x6fffffef: return "SUNW_capchain";
1969    case 0x6ffffff0: return "SUNW_capinfo";
1970    case 0x6ffffff1: return "SUNW_symsort";
1971    case 0x6ffffff2: return "SUNW_tlssort";
1972    case 0x6ffffff3: return "SUNW_LDYNSYM";
1973    case 0x6ffffff4: return "SUNW_dof";
1974    case 0x6ffffff5: return "SUNW_cap";
1975    case 0x6ffffff6: return "SUNW_SIGNATURE";
1976    case 0x6ffffff7: return "SUNW_ANNOTATE";
1977    case 0x6ffffff8: return "SUNW_DEBUGSTR";
1978    case 0x6ffffff9: return "SUNW_DEBUG";
1979    case 0x6ffffffa: return "SUNW_move";
1980    case 0x6ffffffb: return "SUNW_COMDAT";
1981    case 0x6ffffffc: return "SUNW_syminfo";
1982    case 0x6ffffffd: return "SUNW_verdef";
1983    case 0x6ffffffe: return "SUNW_verneed";
1984    case 0x6fffffff: return "SUNW_versym";
1985    case 0x70000000: return "SPARC_GOTDATA";
1986    default: return NULL;
1987    }
1988}
1989
1990static const char *
1991get_alpha_dynamic_type (unsigned long type)
1992{
1993  switch (type)
1994    {
1995    case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1996    default: return NULL;
1997    }
1998}
1999
2000static const char *
2001get_score_dynamic_type (unsigned long type)
2002{
2003  switch (type)
2004    {
2005    case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2006    case DT_SCORE_LOCAL_GOTNO:  return "SCORE_LOCAL_GOTNO";
2007    case DT_SCORE_SYMTABNO:     return "SCORE_SYMTABNO";
2008    case DT_SCORE_GOTSYM:       return "SCORE_GOTSYM";
2009    case DT_SCORE_UNREFEXTNO:   return "SCORE_UNREFEXTNO";
2010    case DT_SCORE_HIPAGENO:     return "SCORE_HIPAGENO";
2011    default:                    return NULL;
2012    }
2013}
2014
2015static const char *
2016get_tic6x_dynamic_type (unsigned long type)
2017{
2018  switch (type)
2019    {
2020    case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2021    case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2022    case DT_C6000_DSBT_BASE:   return "C6000_DSBT_BASE";
2023    case DT_C6000_DSBT_SIZE:   return "C6000_DSBT_SIZE";
2024    case DT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
2025    case DT_C6000_DSBT_INDEX:  return "C6000_DSBT_INDEX";
2026    default:                   return NULL;
2027    }
2028}
2029
2030static const char *
2031get_nios2_dynamic_type (unsigned long type)
2032{
2033  switch (type)
2034    {
2035    case DT_NIOS2_GP: return "NIOS2_GP";
2036    default:          return NULL;
2037    }
2038}
2039
2040static const char *
2041get_solaris_dynamic_type (unsigned long type)
2042{
2043  switch (type)
2044    {
2045    case 0x6000000d: return "SUNW_AUXILIARY";
2046    case 0x6000000e: return "SUNW_RTLDINF";
2047    case 0x6000000f: return "SUNW_FILTER";
2048    case 0x60000010: return "SUNW_CAP";
2049    case 0x60000011: return "SUNW_SYMTAB";
2050    case 0x60000012: return "SUNW_SYMSZ";
2051    case 0x60000013: return "SUNW_SORTENT";
2052    case 0x60000014: return "SUNW_SYMSORT";
2053    case 0x60000015: return "SUNW_SYMSORTSZ";
2054    case 0x60000016: return "SUNW_TLSSORT";
2055    case 0x60000017: return "SUNW_TLSSORTSZ";
2056    case 0x60000018: return "SUNW_CAPINFO";
2057    case 0x60000019: return "SUNW_STRPAD";
2058    case 0x6000001a: return "SUNW_CAPCHAIN";
2059    case 0x6000001b: return "SUNW_LDMACH";
2060    case 0x6000001d: return "SUNW_CAPCHAINENT";
2061    case 0x6000001f: return "SUNW_CAPCHAINSZ";
2062    case 0x60000021: return "SUNW_PARENT";
2063    case 0x60000023: return "SUNW_ASLR";
2064    case 0x60000025: return "SUNW_RELAX";
2065    case 0x60000029: return "SUNW_NXHEAP";
2066    case 0x6000002b: return "SUNW_NXSTACK";
2067
2068    case 0x70000001: return "SPARC_REGISTER";
2069    case 0x7ffffffd: return "AUXILIARY";
2070    case 0x7ffffffe: return "USED";
2071    case 0x7fffffff: return "FILTER";
2072
2073    default: return NULL;
2074    }
2075}
2076
2077static const char *
2078get_dynamic_type (Filedata * filedata, unsigned long type)
2079{
2080  static char buff[64];
2081
2082  switch (type)
2083    {
2084    case DT_NULL:	return "NULL";
2085    case DT_NEEDED:	return "NEEDED";
2086    case DT_PLTRELSZ:	return "PLTRELSZ";
2087    case DT_PLTGOT:	return "PLTGOT";
2088    case DT_HASH:	return "HASH";
2089    case DT_STRTAB:	return "STRTAB";
2090    case DT_SYMTAB:	return "SYMTAB";
2091    case DT_RELA:	return "RELA";
2092    case DT_RELASZ:	return "RELASZ";
2093    case DT_RELAENT:	return "RELAENT";
2094    case DT_STRSZ:	return "STRSZ";
2095    case DT_SYMENT:	return "SYMENT";
2096    case DT_INIT:	return "INIT";
2097    case DT_FINI:	return "FINI";
2098    case DT_SONAME:	return "SONAME";
2099    case DT_RPATH:	return "RPATH";
2100    case DT_SYMBOLIC:	return "SYMBOLIC";
2101    case DT_REL:	return "REL";
2102    case DT_RELSZ:	return "RELSZ";
2103    case DT_RELENT:	return "RELENT";
2104    case DT_PLTREL:	return "PLTREL";
2105    case DT_DEBUG:	return "DEBUG";
2106    case DT_TEXTREL:	return "TEXTREL";
2107    case DT_JMPREL:	return "JMPREL";
2108    case DT_BIND_NOW:   return "BIND_NOW";
2109    case DT_INIT_ARRAY: return "INIT_ARRAY";
2110    case DT_FINI_ARRAY: return "FINI_ARRAY";
2111    case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2112    case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2113    case DT_RUNPATH:    return "RUNPATH";
2114    case DT_FLAGS:      return "FLAGS";
2115
2116    case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2117    case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2118    case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2119
2120    case DT_CHECKSUM:	return "CHECKSUM";
2121    case DT_PLTPADSZ:	return "PLTPADSZ";
2122    case DT_MOVEENT:	return "MOVEENT";
2123    case DT_MOVESZ:	return "MOVESZ";
2124    case DT_FEATURE:	return "FEATURE";
2125    case DT_POSFLAG_1:	return "POSFLAG_1";
2126    case DT_SYMINSZ:	return "SYMINSZ";
2127    case DT_SYMINENT:	return "SYMINENT"; /* aka VALRNGHI */
2128
2129    case DT_ADDRRNGLO:  return "ADDRRNGLO";
2130    case DT_CONFIG:	return "CONFIG";
2131    case DT_DEPAUDIT:	return "DEPAUDIT";
2132    case DT_AUDIT:	return "AUDIT";
2133    case DT_PLTPAD:	return "PLTPAD";
2134    case DT_MOVETAB:	return "MOVETAB";
2135    case DT_SYMINFO:	return "SYMINFO"; /* aka ADDRRNGHI */
2136
2137    case DT_VERSYM:	return "VERSYM";
2138
2139    case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2140    case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2141    case DT_RELACOUNT:	return "RELACOUNT";
2142    case DT_RELCOUNT:	return "RELCOUNT";
2143    case DT_FLAGS_1:	return "FLAGS_1";
2144    case DT_VERDEF:	return "VERDEF";
2145    case DT_VERDEFNUM:	return "VERDEFNUM";
2146    case DT_VERNEED:	return "VERNEED";
2147    case DT_VERNEEDNUM:	return "VERNEEDNUM";
2148
2149    case DT_AUXILIARY:	return "AUXILIARY";
2150    case DT_USED:	return "USED";
2151    case DT_FILTER:	return "FILTER";
2152
2153    case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2154    case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2155    case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2156    case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2157    case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2158    case DT_GNU_HASH:	return "GNU_HASH";
2159
2160    default:
2161      if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2162	{
2163	  const char * result;
2164
2165	  switch (filedata->file_header.e_machine)
2166	    {
2167	    case EM_MIPS:
2168	    case EM_MIPS_RS3_LE:
2169	      result = get_mips_dynamic_type (type);
2170	      break;
2171	    case EM_SPARCV9:
2172	      result = get_sparc64_dynamic_type (type);
2173	      break;
2174	    case EM_PPC:
2175	      result = get_ppc_dynamic_type (type);
2176	      break;
2177	    case EM_PPC64:
2178	      result = get_ppc64_dynamic_type (type);
2179	      break;
2180	    case EM_IA_64:
2181	      result = get_ia64_dynamic_type (type);
2182	      break;
2183	    case EM_ALPHA:
2184	      result = get_alpha_dynamic_type (type);
2185	      break;
2186	    case EM_SCORE:
2187	      result = get_score_dynamic_type (type);
2188	      break;
2189	    case EM_TI_C6000:
2190	      result = get_tic6x_dynamic_type (type);
2191	      break;
2192	    case EM_ALTERA_NIOS2:
2193	      result = get_nios2_dynamic_type (type);
2194	      break;
2195	    default:
2196	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2197		result = get_solaris_dynamic_type (type);
2198	      else
2199		result = NULL;
2200	      break;
2201	    }
2202
2203	  if (result != NULL)
2204	    return result;
2205
2206	  snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2207	}
2208      else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2209	       || (filedata->file_header.e_machine == EM_PARISC
2210		   && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2211	{
2212	  const char * result;
2213
2214	  switch (filedata->file_header.e_machine)
2215	    {
2216	    case EM_PARISC:
2217	      result = get_parisc_dynamic_type (type);
2218	      break;
2219	    case EM_IA_64:
2220	      result = get_ia64_dynamic_type (type);
2221	      break;
2222	    default:
2223	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2224		result = get_solaris_dynamic_type (type);
2225	      else
2226		result = NULL;
2227	      break;
2228	    }
2229
2230	  if (result != NULL)
2231	    return result;
2232
2233	  snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2234		    type);
2235	}
2236      else
2237	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2238
2239      return buff;
2240    }
2241}
2242
2243static char *
2244get_file_type (unsigned e_type)
2245{
2246  static char buff[32];
2247
2248  switch (e_type)
2249    {
2250    case ET_NONE: return _("NONE (None)");
2251    case ET_REL:  return _("REL (Relocatable file)");
2252    case ET_EXEC: return _("EXEC (Executable file)");
2253    case ET_DYN:  return _("DYN (Shared object file)");
2254    case ET_CORE: return _("CORE (Core file)");
2255
2256    default:
2257      if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2258	snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2259      else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2260	snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2261      else
2262	snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2263      return buff;
2264    }
2265}
2266
2267static char *
2268get_machine_name (unsigned e_machine)
2269{
2270  static char buff[64]; /* XXX */
2271
2272  switch (e_machine)
2273    {
2274      /* Please keep this switch table sorted by increasing EM_ value.  */
2275      /* 0 */
2276    case EM_NONE:		return _("None");
2277    case EM_M32:		return "WE32100";
2278    case EM_SPARC:		return "Sparc";
2279    case EM_386:		return "Intel 80386";
2280    case EM_68K:		return "MC68000";
2281    case EM_88K:		return "MC88000";
2282    case EM_IAMCU:		return "Intel MCU";
2283    case EM_860:		return "Intel 80860";
2284    case EM_MIPS:		return "MIPS R3000";
2285    case EM_S370:		return "IBM System/370";
2286      /* 10 */
2287    case EM_MIPS_RS3_LE:	return "MIPS R4000 big-endian";
2288    case EM_OLD_SPARCV9:	return "Sparc v9 (old)";
2289    case EM_PARISC:		return "HPPA";
2290    case EM_VPP550:		return "Fujitsu VPP500";
2291    case EM_SPARC32PLUS:	return "Sparc v8+" ;
2292    case EM_960:		return "Intel 80960";
2293    case EM_PPC:		return "PowerPC";
2294      /* 20 */
2295    case EM_PPC64:		return "PowerPC64";
2296    case EM_S390_OLD:
2297    case EM_S390:		return "IBM S/390";
2298    case EM_SPU:		return "SPU";
2299      /* 30 */
2300    case EM_V800:		return "Renesas V850 (using RH850 ABI)";
2301    case EM_FR20:		return "Fujitsu FR20";
2302    case EM_RH32:		return "TRW RH32";
2303    case EM_MCORE:		return "MCORE";
2304      /* 40 */
2305    case EM_ARM:		return "ARM";
2306    case EM_OLD_ALPHA:		return "Digital Alpha (old)";
2307    case EM_SH:			return "Renesas / SuperH SH";
2308    case EM_SPARCV9:		return "Sparc v9";
2309    case EM_TRICORE:		return "Siemens Tricore";
2310    case EM_ARC:		return "ARC";
2311    case EM_H8_300:		return "Renesas H8/300";
2312    case EM_H8_300H:		return "Renesas H8/300H";
2313    case EM_H8S:		return "Renesas H8S";
2314    case EM_H8_500:		return "Renesas H8/500";
2315      /* 50 */
2316    case EM_IA_64:		return "Intel IA-64";
2317    case EM_MIPS_X:		return "Stanford MIPS-X";
2318    case EM_COLDFIRE:		return "Motorola Coldfire";
2319    case EM_68HC12:		return "Motorola MC68HC12 Microcontroller";
2320    case EM_MMA:		return "Fujitsu Multimedia Accelerator";
2321    case EM_PCP:		return "Siemens PCP";
2322    case EM_NCPU:		return "Sony nCPU embedded RISC processor";
2323    case EM_NDR1:		return "Denso NDR1 microprocesspr";
2324    case EM_STARCORE:		return "Motorola Star*Core processor";
2325    case EM_ME16:		return "Toyota ME16 processor";
2326      /* 60 */
2327    case EM_ST100:		return "STMicroelectronics ST100 processor";
2328    case EM_TINYJ:		return "Advanced Logic Corp. TinyJ embedded processor";
2329    case EM_X86_64:		return "Advanced Micro Devices X86-64";
2330    case EM_PDSP:		return "Sony DSP processor";
2331    case EM_PDP10:		return "Digital Equipment Corp. PDP-10";
2332    case EM_PDP11:		return "Digital Equipment Corp. PDP-11";
2333    case EM_FX66:		return "Siemens FX66 microcontroller";
2334    case EM_ST9PLUS:		return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2335    case EM_ST7:		return "STMicroelectronics ST7 8-bit microcontroller";
2336    case EM_68HC16:		return "Motorola MC68HC16 Microcontroller";
2337      /* 70 */
2338    case EM_68HC11:		return "Motorola MC68HC11 Microcontroller";
2339    case EM_68HC08:		return "Motorola MC68HC08 Microcontroller";
2340    case EM_68HC05:		return "Motorola MC68HC05 Microcontroller";
2341    case EM_SVX:		return "Silicon Graphics SVx";
2342    case EM_ST19:		return "STMicroelectronics ST19 8-bit microcontroller";
2343    case EM_VAX:		return "Digital VAX";
2344    case EM_CRIS:		return "Axis Communications 32-bit embedded processor";
2345    case EM_JAVELIN:		return "Infineon Technologies 32-bit embedded cpu";
2346    case EM_FIREPATH:		return "Element 14 64-bit DSP processor";
2347    case EM_ZSP:		return "LSI Logic's 16-bit DSP processor";
2348      /* 80 */
2349    case EM_MMIX:		return "Donald Knuth's educational 64-bit processor";
2350    case EM_HUANY:		return "Harvard Universitys's machine-independent object format";
2351    case EM_PRISM:		return "Vitesse Prism";
2352    case EM_AVR_OLD:
2353    case EM_AVR:		return "Atmel AVR 8-bit microcontroller";
2354    case EM_CYGNUS_FR30:
2355    case EM_FR30:		return "Fujitsu FR30";
2356    case EM_CYGNUS_D10V:
2357    case EM_D10V:		return "d10v";
2358    case EM_CYGNUS_D30V:
2359    case EM_D30V:		return "d30v";
2360    case EM_CYGNUS_V850:
2361    case EM_V850:		return "Renesas V850";
2362    case EM_CYGNUS_M32R:
2363    case EM_M32R:		return "Renesas M32R (formerly Mitsubishi M32r)";
2364    case EM_CYGNUS_MN10300:
2365    case EM_MN10300:		return "mn10300";
2366      /* 90 */
2367    case EM_CYGNUS_MN10200:
2368    case EM_MN10200:		return "mn10200";
2369    case EM_PJ:			return "picoJava";
2370    case EM_OR1K:		return "OpenRISC 1000";
2371    case EM_ARC_COMPACT:	return "ARCompact";
2372    case EM_XTENSA_OLD:
2373    case EM_XTENSA:		return "Tensilica Xtensa Processor";
2374    case EM_VIDEOCORE:		return "Alphamosaic VideoCore processor";
2375    case EM_TMM_GPP:		return "Thompson Multimedia General Purpose Processor";
2376    case EM_NS32K:		return "National Semiconductor 32000 series";
2377    case EM_TPC:		return "Tenor Network TPC processor";
2378    case EM_SNP1K:	        return "Trebia SNP 1000 processor";
2379      /* 100 */
2380    case EM_ST200:		return "STMicroelectronics ST200 microcontroller";
2381    case EM_IP2K_OLD:
2382    case EM_IP2K:		return "Ubicom IP2xxx 8-bit microcontrollers";
2383    case EM_MAX:		return "MAX Processor";
2384    case EM_CR:			return "National Semiconductor CompactRISC";
2385    case EM_F2MC16:		return "Fujitsu F2MC16";
2386    case EM_MSP430:		return "Texas Instruments msp430 microcontroller";
2387    case EM_BLACKFIN:		return "Analog Devices Blackfin";
2388    case EM_SE_C33:		return "S1C33 Family of Seiko Epson processors";
2389    case EM_SEP:		return "Sharp embedded microprocessor";
2390    case EM_ARCA:		return "Arca RISC microprocessor";
2391      /* 110 */
2392    case EM_UNICORE:		return "Unicore";
2393    case EM_EXCESS:		return "eXcess 16/32/64-bit configurable embedded CPU";
2394    case EM_DXP:		return "Icera Semiconductor Inc. Deep Execution Processor";
2395    case EM_ALTERA_NIOS2:	return "Altera Nios II";
2396    case EM_CRX:		return "National Semiconductor CRX microprocessor";
2397    case EM_XGATE:		return "Motorola XGATE embedded processor";
2398    case EM_C166:
2399    case EM_XC16X:		return "Infineon Technologies xc16x";
2400    case EM_M16C:		return "Renesas M16C series microprocessors";
2401    case EM_DSPIC30F:		return "Microchip Technology dsPIC30F Digital Signal Controller";
2402    case EM_CE:			return "Freescale Communication Engine RISC core";
2403      /* 120 */
2404    case EM_M32C:	        return "Renesas M32c";
2405      /* 130 */
2406    case EM_TSK3000:		return "Altium TSK3000 core";
2407    case EM_RS08:		return "Freescale RS08 embedded processor";
2408    case EM_ECOG2:		return "Cyan Technology eCOG2 microprocessor";
2409    case EM_SCORE:		return "SUNPLUS S+Core";
2410    case EM_DSP24:		return "New Japan Radio (NJR) 24-bit DSP Processor";
2411    case EM_VIDEOCORE3:		return "Broadcom VideoCore III processor";
2412    case EM_LATTICEMICO32:	return "Lattice Mico32";
2413    case EM_SE_C17:		return "Seiko Epson C17 family";
2414      /* 140 */
2415    case EM_TI_C6000:		return "Texas Instruments TMS320C6000 DSP family";
2416    case EM_TI_C2000:		return "Texas Instruments TMS320C2000 DSP family";
2417    case EM_TI_C5500:		return "Texas Instruments TMS320C55x DSP family";
2418    case EM_TI_PRU:		return "TI PRU I/O processor";
2419      /* 160 */
2420    case EM_MMDSP_PLUS:		return "STMicroelectronics 64bit VLIW Data Signal Processor";
2421    case EM_CYPRESS_M8C:	return "Cypress M8C microprocessor";
2422    case EM_R32C:		return "Renesas R32C series microprocessors";
2423    case EM_TRIMEDIA:		return "NXP Semiconductors TriMedia architecture family";
2424    case EM_QDSP6:		return "QUALCOMM DSP6 Processor";
2425    case EM_8051:		return "Intel 8051 and variants";
2426    case EM_STXP7X:		return "STMicroelectronics STxP7x family";
2427    case EM_NDS32:		return "Andes Technology compact code size embedded RISC processor family";
2428    case EM_ECOG1X:		return "Cyan Technology eCOG1X family";
2429    case EM_MAXQ30:		return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2430      /* 170 */
2431    case EM_XIMO16:		return "New Japan Radio (NJR) 16-bit DSP Processor";
2432    case EM_MANIK:		return "M2000 Reconfigurable RISC Microprocessor";
2433    case EM_CRAYNV2:		return "Cray Inc. NV2 vector architecture";
2434    case EM_RX:			return "Renesas RX";
2435    case EM_METAG:		return "Imagination Technologies Meta processor architecture";
2436    case EM_MCST_ELBRUS:	return "MCST Elbrus general purpose hardware architecture";
2437    case EM_ECOG16:		return "Cyan Technology eCOG16 family";
2438    case EM_CR16:
2439    case EM_MICROBLAZE:
2440    case EM_MICROBLAZE_OLD:	return "Xilinx MicroBlaze";
2441    case EM_ETPU:		return "Freescale Extended Time Processing Unit";
2442    case EM_SLE9X:		return "Infineon Technologies SLE9X core";
2443      /* 180 */
2444    case EM_L1OM:		return "Intel L1OM";
2445    case EM_K1OM:		return "Intel K1OM";
2446    case EM_INTEL182:		return "Intel (reserved)";
2447    case EM_AARCH64:		return "AArch64";
2448    case EM_ARM184:		return "ARM (reserved)";
2449    case EM_AVR32:		return "Atmel Corporation 32-bit microprocessor";
2450    case EM_STM8:		return "STMicroeletronics STM8 8-bit microcontroller";
2451    case EM_TILE64:		return "Tilera TILE64 multicore architecture family";
2452    case EM_TILEPRO:		return "Tilera TILEPro multicore architecture family";
2453      /* 190 */
2454    case EM_CUDA:		return "NVIDIA CUDA architecture";
2455    case EM_TILEGX:		return "Tilera TILE-Gx multicore architecture family";
2456    case EM_CLOUDSHIELD:	return "CloudShield architecture family";
2457    case EM_COREA_1ST:		return "KIPO-KAIST Core-A 1st generation processor family";
2458    case EM_COREA_2ND:		return "KIPO-KAIST Core-A 2nd generation processor family";
2459    case EM_ARC_COMPACT2:	return "ARCv2";
2460    case EM_OPEN8:		return "Open8 8-bit RISC soft processor core";
2461    case EM_RL78:		return "Renesas RL78";
2462    case EM_VIDEOCORE5:		return "Broadcom VideoCore V processor";
2463    case EM_78K0R:		return "Renesas 78K0R";
2464      /* 200 */
2465    case EM_56800EX:		return "Freescale 56800EX Digital Signal Controller (DSC)";
2466    case EM_BA1:		return "Beyond BA1 CPU architecture";
2467    case EM_BA2:		return "Beyond BA2 CPU architecture";
2468    case EM_XCORE:		return "XMOS xCORE processor family";
2469    case EM_MCHP_PIC:		return "Microchip 8-bit PIC(r) family";
2470      /* 210 */
2471    case EM_KM32:		return "KM211 KM32 32-bit processor";
2472    case EM_KMX32:		return "KM211 KMX32 32-bit processor";
2473    case EM_KMX16:		return "KM211 KMX16 16-bit processor";
2474    case EM_KMX8:		return "KM211 KMX8 8-bit processor";
2475    case EM_KVARC:		return "KM211 KVARC processor";
2476    case EM_CDP:		return "Paneve CDP architecture family";
2477    case EM_COGE:		return "Cognitive Smart Memory Processor";
2478    case EM_COOL:		return "Bluechip Systems CoolEngine";
2479    case EM_NORC:		return "Nanoradio Optimized RISC";
2480    case EM_CSR_KALIMBA:	return "CSR Kalimba architecture family";
2481      /* 220 */
2482    case EM_Z80:		return "Zilog Z80";
2483    case EM_VISIUM:		return "CDS VISIUMcore processor";
2484    case EM_FT32:               return "FTDI Chip FT32";
2485    case EM_MOXIE:              return "Moxie";
2486    case EM_AMDGPU: 	 	return "AMD GPU";
2487    case EM_RISCV: 	 	return "RISC-V";
2488    case EM_LANAI:		return "Lanai 32-bit processor";
2489    case EM_BPF:		return "Linux BPF";
2490    case EM_NFP:		return "Netronome Flow Processor";
2491
2492      /* Large numbers...  */
2493    case EM_MT:                 return "Morpho Techologies MT processor";
2494    case EM_ALPHA:		return "Alpha";
2495    case EM_WEBASSEMBLY:	return "Web Assembly";
2496    case EM_DLX:		return "OpenDLX";
2497    case EM_XSTORMY16:		return "Sanyo XStormy16 CPU core";
2498    case EM_IQ2000:       	return "Vitesse IQ2000";
2499    case EM_M32C_OLD:
2500    case EM_NIOS32:		return "Altera Nios";
2501    case EM_CYGNUS_MEP:         return "Toshiba MeP Media Engine";
2502    case EM_ADAPTEVA_EPIPHANY:	return "Adapteva EPIPHANY";
2503    case EM_CYGNUS_FRV:		return "Fujitsu FR-V";
2504    case EM_S12Z:               return "Freescale S12Z";
2505
2506    default:
2507      snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2508      return buff;
2509    }
2510}
2511
2512static void
2513decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2514{
2515  /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2.  Some
2516     other compilers don't a specific architecture type in the e_flags, and
2517     instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2518     architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2519     architectures.
2520
2521     Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2522     but also sets a specific architecture type in the e_flags field.
2523
2524     However, when decoding the flags we don't worry if we see an
2525     unexpected pairing, for example EM_ARC_COMPACT machine type, with
2526     ARCEM architecture type.  */
2527
2528  switch (e_flags & EF_ARC_MACH_MSK)
2529    {
2530      /* We only expect these to occur for EM_ARC_COMPACT2.  */
2531    case EF_ARC_CPU_ARCV2EM:
2532      strcat (buf, ", ARC EM");
2533      break;
2534    case EF_ARC_CPU_ARCV2HS:
2535      strcat (buf, ", ARC HS");
2536      break;
2537
2538      /* We only expect these to occur for EM_ARC_COMPACT.  */
2539    case E_ARC_MACH_ARC600:
2540      strcat (buf, ", ARC600");
2541      break;
2542    case E_ARC_MACH_ARC601:
2543      strcat (buf, ", ARC601");
2544      break;
2545    case E_ARC_MACH_ARC700:
2546      strcat (buf, ", ARC700");
2547      break;
2548
2549      /* The only times we should end up here are (a) A corrupt ELF, (b) A
2550         new ELF with new architecture being read by an old version of
2551         readelf, or (c) An ELF built with non-GNU compiler that does not
2552         set the architecture in the e_flags.  */
2553    default:
2554      if (e_machine == EM_ARC_COMPACT)
2555        strcat (buf, ", Unknown ARCompact");
2556      else
2557        strcat (buf, ", Unknown ARC");
2558      break;
2559    }
2560
2561  switch (e_flags & EF_ARC_OSABI_MSK)
2562    {
2563    case E_ARC_OSABI_ORIG:
2564      strcat (buf, ", (ABI:legacy)");
2565      break;
2566    case E_ARC_OSABI_V2:
2567      strcat (buf, ", (ABI:v2)");
2568      break;
2569      /* Only upstream 3.9+ kernels will support ARCv2 ISA.  */
2570    case E_ARC_OSABI_V3:
2571      strcat (buf, ", v3 no-legacy-syscalls ABI");
2572      break;
2573    case E_ARC_OSABI_V4:
2574      strcat (buf, ", v4 ABI");
2575      break;
2576    default:
2577      strcat (buf, ", unrecognised ARC OSABI flag");
2578      break;
2579    }
2580}
2581
2582static void
2583decode_ARM_machine_flags (unsigned e_flags, char buf[])
2584{
2585  unsigned eabi;
2586  bfd_boolean unknown = FALSE;
2587
2588  eabi = EF_ARM_EABI_VERSION (e_flags);
2589  e_flags &= ~ EF_ARM_EABIMASK;
2590
2591  /* Handle "generic" ARM flags.  */
2592  if (e_flags & EF_ARM_RELEXEC)
2593    {
2594      strcat (buf, ", relocatable executable");
2595      e_flags &= ~ EF_ARM_RELEXEC;
2596    }
2597
2598  if (e_flags & EF_ARM_PIC)
2599    {
2600      strcat (buf, ", position independent");
2601      e_flags &= ~ EF_ARM_PIC;
2602    }
2603
2604  /* Now handle EABI specific flags.  */
2605  switch (eabi)
2606    {
2607    default:
2608      strcat (buf, ", <unrecognized EABI>");
2609      if (e_flags)
2610	unknown = TRUE;
2611      break;
2612
2613    case EF_ARM_EABI_VER1:
2614      strcat (buf, ", Version1 EABI");
2615      while (e_flags)
2616	{
2617	  unsigned flag;
2618
2619	  /* Process flags one bit at a time.  */
2620	  flag = e_flags & - e_flags;
2621	  e_flags &= ~ flag;
2622
2623	  switch (flag)
2624	    {
2625	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2626	      strcat (buf, ", sorted symbol tables");
2627	      break;
2628
2629	    default:
2630	      unknown = TRUE;
2631	      break;
2632	    }
2633	}
2634      break;
2635
2636    case EF_ARM_EABI_VER2:
2637      strcat (buf, ", Version2 EABI");
2638      while (e_flags)
2639	{
2640	  unsigned flag;
2641
2642	  /* Process flags one bit at a time.  */
2643	  flag = e_flags & - e_flags;
2644	  e_flags &= ~ flag;
2645
2646	  switch (flag)
2647	    {
2648	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2649	      strcat (buf, ", sorted symbol tables");
2650	      break;
2651
2652	    case EF_ARM_DYNSYMSUSESEGIDX:
2653	      strcat (buf, ", dynamic symbols use segment index");
2654	      break;
2655
2656	    case EF_ARM_MAPSYMSFIRST:
2657	      strcat (buf, ", mapping symbols precede others");
2658	      break;
2659
2660	    default:
2661	      unknown = TRUE;
2662	      break;
2663	    }
2664	}
2665      break;
2666
2667    case EF_ARM_EABI_VER3:
2668      strcat (buf, ", Version3 EABI");
2669      break;
2670
2671    case EF_ARM_EABI_VER4:
2672      strcat (buf, ", Version4 EABI");
2673      while (e_flags)
2674	{
2675	  unsigned flag;
2676
2677	  /* Process flags one bit at a time.  */
2678	  flag = e_flags & - e_flags;
2679	  e_flags &= ~ flag;
2680
2681	  switch (flag)
2682	    {
2683	    case EF_ARM_BE8:
2684	      strcat (buf, ", BE8");
2685	      break;
2686
2687	    case EF_ARM_LE8:
2688	      strcat (buf, ", LE8");
2689	      break;
2690
2691	    default:
2692	      unknown = TRUE;
2693	      break;
2694	    }
2695	}
2696      break;
2697
2698    case EF_ARM_EABI_VER5:
2699      strcat (buf, ", Version5 EABI");
2700      while (e_flags)
2701	{
2702	  unsigned flag;
2703
2704	  /* Process flags one bit at a time.  */
2705	  flag = e_flags & - e_flags;
2706	  e_flags &= ~ flag;
2707
2708	  switch (flag)
2709	    {
2710	    case EF_ARM_BE8:
2711	      strcat (buf, ", BE8");
2712	      break;
2713
2714	    case EF_ARM_LE8:
2715	      strcat (buf, ", LE8");
2716	      break;
2717
2718	    case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT.  */
2719	      strcat (buf, ", soft-float ABI");
2720	      break;
2721
2722	    case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT.  */
2723	      strcat (buf, ", hard-float ABI");
2724	      break;
2725
2726	    default:
2727	      unknown = TRUE;
2728	      break;
2729	    }
2730	}
2731      break;
2732
2733    case EF_ARM_EABI_UNKNOWN:
2734      strcat (buf, ", GNU EABI");
2735      while (e_flags)
2736	{
2737	  unsigned flag;
2738
2739	  /* Process flags one bit at a time.  */
2740	  flag = e_flags & - e_flags;
2741	  e_flags &= ~ flag;
2742
2743	  switch (flag)
2744	    {
2745	    case EF_ARM_INTERWORK:
2746	      strcat (buf, ", interworking enabled");
2747	      break;
2748
2749	    case EF_ARM_APCS_26:
2750	      strcat (buf, ", uses APCS/26");
2751	      break;
2752
2753	    case EF_ARM_APCS_FLOAT:
2754	      strcat (buf, ", uses APCS/float");
2755	      break;
2756
2757	    case EF_ARM_PIC:
2758	      strcat (buf, ", position independent");
2759	      break;
2760
2761	    case EF_ARM_ALIGN8:
2762	      strcat (buf, ", 8 bit structure alignment");
2763	      break;
2764
2765	    case EF_ARM_NEW_ABI:
2766	      strcat (buf, ", uses new ABI");
2767	      break;
2768
2769	    case EF_ARM_OLD_ABI:
2770	      strcat (buf, ", uses old ABI");
2771	      break;
2772
2773	    case EF_ARM_SOFT_FLOAT:
2774	      strcat (buf, ", software FP");
2775	      break;
2776
2777	    case EF_ARM_VFP_FLOAT:
2778	      strcat (buf, ", VFP");
2779	      break;
2780
2781	    case EF_ARM_MAVERICK_FLOAT:
2782	      strcat (buf, ", Maverick FP");
2783	      break;
2784
2785	    default:
2786	      unknown = TRUE;
2787	      break;
2788	    }
2789	}
2790    }
2791
2792  if (unknown)
2793    strcat (buf,_(", <unknown>"));
2794}
2795
2796static void
2797decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
2798{
2799  --size; /* Leave space for null terminator.  */
2800
2801  switch (e_flags & EF_AVR_MACH)
2802    {
2803    case E_AVR_MACH_AVR1:
2804      strncat (buf, ", avr:1", size);
2805      break;
2806    case E_AVR_MACH_AVR2:
2807      strncat (buf, ", avr:2", size);
2808      break;
2809    case E_AVR_MACH_AVR25:
2810      strncat (buf, ", avr:25", size);
2811      break;
2812    case E_AVR_MACH_AVR3:
2813      strncat (buf, ", avr:3", size);
2814      break;
2815    case E_AVR_MACH_AVR31:
2816      strncat (buf, ", avr:31", size);
2817      break;
2818    case E_AVR_MACH_AVR35:
2819      strncat (buf, ", avr:35", size);
2820      break;
2821    case E_AVR_MACH_AVR4:
2822      strncat (buf, ", avr:4", size);
2823      break;
2824    case E_AVR_MACH_AVR5:
2825      strncat (buf, ", avr:5", size);
2826      break;
2827    case E_AVR_MACH_AVR51:
2828      strncat (buf, ", avr:51", size);
2829      break;
2830    case E_AVR_MACH_AVR6:
2831      strncat (buf, ", avr:6", size);
2832      break;
2833    case E_AVR_MACH_AVRTINY:
2834      strncat (buf, ", avr:100", size);
2835      break;
2836    case E_AVR_MACH_XMEGA1:
2837      strncat (buf, ", avr:101", size);
2838      break;
2839    case E_AVR_MACH_XMEGA2:
2840      strncat (buf, ", avr:102", size);
2841      break;
2842    case E_AVR_MACH_XMEGA3:
2843      strncat (buf, ", avr:103", size);
2844      break;
2845    case E_AVR_MACH_XMEGA4:
2846      strncat (buf, ", avr:104", size);
2847      break;
2848    case E_AVR_MACH_XMEGA5:
2849      strncat (buf, ", avr:105", size);
2850      break;
2851    case E_AVR_MACH_XMEGA6:
2852      strncat (buf, ", avr:106", size);
2853      break;
2854    case E_AVR_MACH_XMEGA7:
2855      strncat (buf, ", avr:107", size);
2856      break;
2857    default:
2858      strncat (buf, ", avr:<unknown>", size);
2859      break;
2860    }
2861
2862  size -= strlen (buf);
2863  if (e_flags & EF_AVR_LINKRELAX_PREPARED)
2864    strncat (buf, ", link-relax", size);
2865}
2866
2867static void
2868decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2869{
2870  unsigned abi;
2871  unsigned arch;
2872  unsigned config;
2873  unsigned version;
2874  bfd_boolean has_fpu = FALSE;
2875  unsigned int r = 0;
2876
2877  static const char *ABI_STRINGS[] =
2878  {
2879    "ABI v0", /* use r5 as return register; only used in N1213HC */
2880    "ABI v1", /* use r0 as return register */
2881    "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2882    "ABI v2fp", /* for FPU */
2883    "AABI",
2884    "ABI2 FP+"
2885  };
2886  static const char *VER_STRINGS[] =
2887  {
2888    "Andes ELF V1.3 or older",
2889    "Andes ELF V1.3.1",
2890    "Andes ELF V1.4"
2891  };
2892  static const char *ARCH_STRINGS[] =
2893  {
2894    "",
2895    "Andes Star v1.0",
2896    "Andes Star v2.0",
2897    "Andes Star v3.0",
2898    "Andes Star v3.0m"
2899  };
2900
2901  abi = EF_NDS_ABI & e_flags;
2902  arch = EF_NDS_ARCH & e_flags;
2903  config = EF_NDS_INST & e_flags;
2904  version = EF_NDS32_ELF_VERSION & e_flags;
2905
2906  memset (buf, 0, size);
2907
2908  switch (abi)
2909    {
2910    case E_NDS_ABI_V0:
2911    case E_NDS_ABI_V1:
2912    case E_NDS_ABI_V2:
2913    case E_NDS_ABI_V2FP:
2914    case E_NDS_ABI_AABI:
2915    case E_NDS_ABI_V2FP_PLUS:
2916      /* In case there are holes in the array.  */
2917      r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
2918      break;
2919
2920    default:
2921      r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
2922      break;
2923    }
2924
2925  switch (version)
2926    {
2927    case E_NDS32_ELF_VER_1_2:
2928    case E_NDS32_ELF_VER_1_3:
2929    case E_NDS32_ELF_VER_1_4:
2930      r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
2931      break;
2932
2933    default:
2934      r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
2935      break;
2936    }
2937
2938  if (E_NDS_ABI_V0 == abi)
2939    {
2940      /* OLD ABI; only used in N1213HC, has performance extension 1.  */
2941      r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
2942      if (arch == E_NDS_ARCH_STAR_V1_0)
2943	r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
2944      return;
2945    }
2946
2947  switch (arch)
2948    {
2949    case E_NDS_ARCH_STAR_V1_0:
2950    case E_NDS_ARCH_STAR_V2_0:
2951    case E_NDS_ARCH_STAR_V3_0:
2952    case E_NDS_ARCH_STAR_V3_M:
2953      r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
2954      break;
2955
2956    default:
2957      r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
2958      /* ARCH version determines how the e_flags are interpreted.
2959	 If it is unknown, we cannot proceed.  */
2960      return;
2961    }
2962
2963  /* Newer ABI; Now handle architecture specific flags.  */
2964  if (arch == E_NDS_ARCH_STAR_V1_0)
2965    {
2966      if (config & E_NDS32_HAS_MFUSR_PC_INST)
2967	r += snprintf (buf + r, size -r, ", MFUSR_PC");
2968
2969      if (!(config & E_NDS32_HAS_NO_MAC_INST))
2970	r += snprintf (buf + r, size -r, ", MAC");
2971
2972      if (config & E_NDS32_HAS_DIV_INST)
2973	r += snprintf (buf + r, size -r, ", DIV");
2974
2975      if (config & E_NDS32_HAS_16BIT_INST)
2976	r += snprintf (buf + r, size -r, ", 16b");
2977    }
2978  else
2979    {
2980      if (config & E_NDS32_HAS_MFUSR_PC_INST)
2981	{
2982	  if (version <= E_NDS32_ELF_VER_1_3)
2983	    r += snprintf (buf + r, size -r, ", [B8]");
2984	  else
2985	    r += snprintf (buf + r, size -r, ", EX9");
2986	}
2987
2988      if (config & E_NDS32_HAS_MAC_DX_INST)
2989	r += snprintf (buf + r, size -r, ", MAC_DX");
2990
2991      if (config & E_NDS32_HAS_DIV_DX_INST)
2992	r += snprintf (buf + r, size -r, ", DIV_DX");
2993
2994      if (config & E_NDS32_HAS_16BIT_INST)
2995	{
2996	  if (version <= E_NDS32_ELF_VER_1_3)
2997	    r += snprintf (buf + r, size -r, ", 16b");
2998	  else
2999	    r += snprintf (buf + r, size -r, ", IFC");
3000	}
3001    }
3002
3003  if (config & E_NDS32_HAS_EXT_INST)
3004    r += snprintf (buf + r, size -r, ", PERF1");
3005
3006  if (config & E_NDS32_HAS_EXT2_INST)
3007    r += snprintf (buf + r, size -r, ", PERF2");
3008
3009  if (config & E_NDS32_HAS_FPU_INST)
3010    {
3011      has_fpu = TRUE;
3012      r += snprintf (buf + r, size -r, ", FPU_SP");
3013    }
3014
3015  if (config & E_NDS32_HAS_FPU_DP_INST)
3016    {
3017      has_fpu = TRUE;
3018      r += snprintf (buf + r, size -r, ", FPU_DP");
3019    }
3020
3021  if (config & E_NDS32_HAS_FPU_MAC_INST)
3022    {
3023      has_fpu = TRUE;
3024      r += snprintf (buf + r, size -r, ", FPU_MAC");
3025    }
3026
3027  if (has_fpu)
3028    {
3029      switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3030	{
3031	case E_NDS32_FPU_REG_8SP_4DP:
3032	  r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3033	  break;
3034	case E_NDS32_FPU_REG_16SP_8DP:
3035	  r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3036	  break;
3037	case E_NDS32_FPU_REG_32SP_16DP:
3038	  r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3039	  break;
3040	case E_NDS32_FPU_REG_32SP_32DP:
3041	  r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3042	  break;
3043	}
3044    }
3045
3046  if (config & E_NDS32_HAS_AUDIO_INST)
3047    r += snprintf (buf + r, size -r, ", AUDIO");
3048
3049  if (config & E_NDS32_HAS_STRING_INST)
3050    r += snprintf (buf + r, size -r, ", STR");
3051
3052  if (config & E_NDS32_HAS_REDUCED_REGS)
3053    r += snprintf (buf + r, size -r, ", 16REG");
3054
3055  if (config & E_NDS32_HAS_VIDEO_INST)
3056    {
3057      if (version <= E_NDS32_ELF_VER_1_3)
3058	r += snprintf (buf + r, size -r, ", VIDEO");
3059      else
3060	r += snprintf (buf + r, size -r, ", SATURATION");
3061    }
3062
3063  if (config & E_NDS32_HAS_ENCRIPT_INST)
3064    r += snprintf (buf + r, size -r, ", ENCRP");
3065
3066  if (config & E_NDS32_HAS_L2C_INST)
3067    r += snprintf (buf + r, size -r, ", L2C");
3068}
3069
3070static char *
3071get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3072{
3073  static char buf[1024];
3074
3075  buf[0] = '\0';
3076
3077  if (e_flags)
3078    {
3079      switch (e_machine)
3080	{
3081	default:
3082	  break;
3083
3084	case EM_ARC_COMPACT2:
3085	case EM_ARC_COMPACT:
3086          decode_ARC_machine_flags (e_flags, e_machine, buf);
3087          break;
3088
3089	case EM_ARM:
3090	  decode_ARM_machine_flags (e_flags, buf);
3091	  break;
3092
3093        case EM_AVR:
3094          decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3095          break;
3096
3097	case EM_BLACKFIN:
3098	  if (e_flags & EF_BFIN_PIC)
3099	    strcat (buf, ", PIC");
3100
3101	  if (e_flags & EF_BFIN_FDPIC)
3102	    strcat (buf, ", FDPIC");
3103
3104	  if (e_flags & EF_BFIN_CODE_IN_L1)
3105	    strcat (buf, ", code in L1");
3106
3107	  if (e_flags & EF_BFIN_DATA_IN_L1)
3108	    strcat (buf, ", data in L1");
3109
3110	  break;
3111
3112	case EM_CYGNUS_FRV:
3113	  switch (e_flags & EF_FRV_CPU_MASK)
3114	    {
3115	    case EF_FRV_CPU_GENERIC:
3116	      break;
3117
3118	    default:
3119	      strcat (buf, ", fr???");
3120	      break;
3121
3122	    case EF_FRV_CPU_FR300:
3123	      strcat (buf, ", fr300");
3124	      break;
3125
3126	    case EF_FRV_CPU_FR400:
3127	      strcat (buf, ", fr400");
3128	      break;
3129	    case EF_FRV_CPU_FR405:
3130	      strcat (buf, ", fr405");
3131	      break;
3132
3133	    case EF_FRV_CPU_FR450:
3134	      strcat (buf, ", fr450");
3135	      break;
3136
3137	    case EF_FRV_CPU_FR500:
3138	      strcat (buf, ", fr500");
3139	      break;
3140	    case EF_FRV_CPU_FR550:
3141	      strcat (buf, ", fr550");
3142	      break;
3143
3144	    case EF_FRV_CPU_SIMPLE:
3145	      strcat (buf, ", simple");
3146	      break;
3147	    case EF_FRV_CPU_TOMCAT:
3148	      strcat (buf, ", tomcat");
3149	      break;
3150	    }
3151	  break;
3152
3153	case EM_68K:
3154	  if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3155	    strcat (buf, ", m68000");
3156	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3157	    strcat (buf, ", cpu32");
3158	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3159	    strcat (buf, ", fido_a");
3160	  else
3161	    {
3162	      char const * isa = _("unknown");
3163	      char const * mac = _("unknown mac");
3164	      char const * additional = NULL;
3165
3166	      switch (e_flags & EF_M68K_CF_ISA_MASK)
3167		{
3168		case EF_M68K_CF_ISA_A_NODIV:
3169		  isa = "A";
3170		  additional = ", nodiv";
3171		  break;
3172		case EF_M68K_CF_ISA_A:
3173		  isa = "A";
3174		  break;
3175		case EF_M68K_CF_ISA_A_PLUS:
3176		  isa = "A+";
3177		  break;
3178		case EF_M68K_CF_ISA_B_NOUSP:
3179		  isa = "B";
3180		  additional = ", nousp";
3181		  break;
3182		case EF_M68K_CF_ISA_B:
3183		  isa = "B";
3184		  break;
3185		case EF_M68K_CF_ISA_C:
3186		  isa = "C";
3187		  break;
3188		case EF_M68K_CF_ISA_C_NODIV:
3189		  isa = "C";
3190		  additional = ", nodiv";
3191		  break;
3192		}
3193	      strcat (buf, ", cf, isa ");
3194	      strcat (buf, isa);
3195	      if (additional)
3196		strcat (buf, additional);
3197	      if (e_flags & EF_M68K_CF_FLOAT)
3198		strcat (buf, ", float");
3199	      switch (e_flags & EF_M68K_CF_MAC_MASK)
3200		{
3201		case 0:
3202		  mac = NULL;
3203		  break;
3204		case EF_M68K_CF_MAC:
3205		  mac = "mac";
3206		  break;
3207		case EF_M68K_CF_EMAC:
3208		  mac = "emac";
3209		  break;
3210		case EF_M68K_CF_EMAC_B:
3211		  mac = "emac_b";
3212		  break;
3213		}
3214	      if (mac)
3215		{
3216		  strcat (buf, ", ");
3217		  strcat (buf, mac);
3218		}
3219	    }
3220	  break;
3221
3222	case EM_CYGNUS_MEP:
3223	  switch (e_flags & EF_MEP_CPU_MASK)
3224	    {
3225	    case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3226	    case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3227	    case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3228	    case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3229	    case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3230	    case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3231	    default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3232	    }
3233
3234	  switch (e_flags & EF_MEP_COP_MASK)
3235	    {
3236	    case EF_MEP_COP_NONE: break;
3237	    case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3238	    case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3239	    case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3240	    case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3241	    default: strcat (buf, _("<unknown MeP copro type>")); break;
3242	    }
3243
3244	  if (e_flags & EF_MEP_LIBRARY)
3245	    strcat (buf, ", Built for Library");
3246
3247	  if (e_flags & EF_MEP_INDEX_MASK)
3248	    sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3249		     e_flags & EF_MEP_INDEX_MASK);
3250
3251	  if (e_flags & ~ EF_MEP_ALL_FLAGS)
3252	    sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3253		     e_flags & ~ EF_MEP_ALL_FLAGS);
3254	  break;
3255
3256	case EM_PPC:
3257	  if (e_flags & EF_PPC_EMB)
3258	    strcat (buf, ", emb");
3259
3260	  if (e_flags & EF_PPC_RELOCATABLE)
3261	    strcat (buf, _(", relocatable"));
3262
3263	  if (e_flags & EF_PPC_RELOCATABLE_LIB)
3264	    strcat (buf, _(", relocatable-lib"));
3265	  break;
3266
3267	case EM_PPC64:
3268	  if (e_flags & EF_PPC64_ABI)
3269	    {
3270	      char abi[] = ", abiv0";
3271
3272	      abi[6] += e_flags & EF_PPC64_ABI;
3273	      strcat (buf, abi);
3274	    }
3275	  break;
3276
3277	case EM_V800:
3278	  if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3279	    strcat (buf, ", RH850 ABI");
3280
3281	  if (e_flags & EF_V800_850E3)
3282	    strcat (buf, ", V3 architecture");
3283
3284	  if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3285	    strcat (buf, ", FPU not used");
3286
3287	  if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3288	    strcat (buf, ", regmode: COMMON");
3289
3290	  if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3291	    strcat (buf, ", r4 not used");
3292
3293	  if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3294	    strcat (buf, ", r30 not used");
3295
3296	  if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3297	    strcat (buf, ", r5 not used");
3298
3299	  if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3300	    strcat (buf, ", r2 not used");
3301
3302	  for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3303	    {
3304	      switch (e_flags & - e_flags)
3305		{
3306		case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3307		case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3308		case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3309		case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3310		case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3311		case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3312		case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3313		case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3314		case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3315		case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3316		case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3317		case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3318		default: break;
3319		}
3320	    }
3321	  break;
3322
3323	case EM_V850:
3324	case EM_CYGNUS_V850:
3325	  switch (e_flags & EF_V850_ARCH)
3326	    {
3327	    case E_V850E3V5_ARCH:
3328	      strcat (buf, ", v850e3v5");
3329	      break;
3330	    case E_V850E2V3_ARCH:
3331	      strcat (buf, ", v850e2v3");
3332	      break;
3333	    case E_V850E2_ARCH:
3334	      strcat (buf, ", v850e2");
3335	      break;
3336            case E_V850E1_ARCH:
3337              strcat (buf, ", v850e1");
3338	      break;
3339	    case E_V850E_ARCH:
3340	      strcat (buf, ", v850e");
3341	      break;
3342	    case E_V850_ARCH:
3343	      strcat (buf, ", v850");
3344	      break;
3345	    default:
3346	      strcat (buf, _(", unknown v850 architecture variant"));
3347	      break;
3348	    }
3349	  break;
3350
3351	case EM_M32R:
3352	case EM_CYGNUS_M32R:
3353	  if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3354	    strcat (buf, ", m32r");
3355	  break;
3356
3357	case EM_MIPS:
3358	case EM_MIPS_RS3_LE:
3359	  if (e_flags & EF_MIPS_NOREORDER)
3360	    strcat (buf, ", noreorder");
3361
3362	  if (e_flags & EF_MIPS_PIC)
3363	    strcat (buf, ", pic");
3364
3365	  if (e_flags & EF_MIPS_CPIC)
3366	    strcat (buf, ", cpic");
3367
3368	  if (e_flags & EF_MIPS_UCODE)
3369	    strcat (buf, ", ugen_reserved");
3370
3371	  if (e_flags & EF_MIPS_ABI2)
3372	    strcat (buf, ", abi2");
3373
3374	  if (e_flags & EF_MIPS_OPTIONS_FIRST)
3375	    strcat (buf, ", odk first");
3376
3377	  if (e_flags & EF_MIPS_32BITMODE)
3378	    strcat (buf, ", 32bitmode");
3379
3380	  if (e_flags & EF_MIPS_NAN2008)
3381	    strcat (buf, ", nan2008");
3382
3383	  if (e_flags & EF_MIPS_FP64)
3384	    strcat (buf, ", fp64");
3385
3386	  switch ((e_flags & EF_MIPS_MACH))
3387	    {
3388	    case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3389	    case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3390	    case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3391	    case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3392	    case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3393	    case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3394	    case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3395	    case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3396	    case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3397	    case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
3398	    case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3399  	    case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3400  	    case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3401  	    case E_MIPS_MACH_LS3A: strcat (buf, ", loongson-3a"); break;
3402	    case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3403	    case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3404	    case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3405	    case E_MIPS_MACH_XLR:  strcat (buf, ", xlr"); break;
3406	    case E_MIPS_MACH_IAMR2:  strcat (buf, ", interaptiv-mr2"); break;
3407	    case 0:
3408	    /* We simply ignore the field in this case to avoid confusion:
3409	       MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3410	       extension.  */
3411	      break;
3412	    default: strcat (buf, _(", unknown CPU")); break;
3413	    }
3414
3415	  switch ((e_flags & EF_MIPS_ABI))
3416	    {
3417	    case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3418	    case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3419	    case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3420	    case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3421	    case 0:
3422	    /* We simply ignore the field in this case to avoid confusion:
3423	       MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3424	       This means it is likely to be an o32 file, but not for
3425	       sure.  */
3426	      break;
3427	    default: strcat (buf, _(", unknown ABI")); break;
3428	    }
3429
3430	  if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3431	    strcat (buf, ", mdmx");
3432
3433	  if (e_flags & EF_MIPS_ARCH_ASE_M16)
3434	    strcat (buf, ", mips16");
3435
3436	  if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3437	    strcat (buf, ", micromips");
3438
3439	  switch ((e_flags & EF_MIPS_ARCH))
3440	    {
3441	    case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3442	    case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3443	    case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3444	    case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3445	    case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3446	    case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3447	    case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3448	    case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3449	    case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3450	    case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3451	    case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3452	    default: strcat (buf, _(", unknown ISA")); break;
3453	    }
3454	  break;
3455
3456	case EM_NDS32:
3457	  decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3458	  break;
3459
3460	case EM_NFP:
3461	  switch (EF_NFP_MACH (e_flags))
3462	    {
3463	    case E_NFP_MACH_3200:
3464	      strcat (buf, ", NFP-32xx");
3465	      break;
3466	    case E_NFP_MACH_6000:
3467	      strcat (buf, ", NFP-6xxx");
3468	      break;
3469	    }
3470	  break;
3471
3472	case EM_RISCV:
3473	  if (e_flags & EF_RISCV_RVC)
3474	    strcat (buf, ", RVC");
3475
3476	  if (e_flags & EF_RISCV_RVE)
3477	    strcat (buf, ", RVE");
3478
3479	  switch (e_flags & EF_RISCV_FLOAT_ABI)
3480	    {
3481	    case EF_RISCV_FLOAT_ABI_SOFT:
3482	      strcat (buf, ", soft-float ABI");
3483	      break;
3484
3485	    case EF_RISCV_FLOAT_ABI_SINGLE:
3486	      strcat (buf, ", single-float ABI");
3487	      break;
3488
3489	    case EF_RISCV_FLOAT_ABI_DOUBLE:
3490	      strcat (buf, ", double-float ABI");
3491	      break;
3492
3493	    case EF_RISCV_FLOAT_ABI_QUAD:
3494	      strcat (buf, ", quad-float ABI");
3495	      break;
3496	    }
3497	  break;
3498
3499	case EM_SH:
3500	  switch ((e_flags & EF_SH_MACH_MASK))
3501	    {
3502	    case EF_SH1: strcat (buf, ", sh1"); break;
3503	    case EF_SH2: strcat (buf, ", sh2"); break;
3504	    case EF_SH3: strcat (buf, ", sh3"); break;
3505	    case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3506	    case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3507	    case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3508	    case EF_SH3E: strcat (buf, ", sh3e"); break;
3509	    case EF_SH4: strcat (buf, ", sh4"); break;
3510	    case EF_SH5: strcat (buf, ", sh5"); break;
3511	    case EF_SH2E: strcat (buf, ", sh2e"); break;
3512	    case EF_SH4A: strcat (buf, ", sh4a"); break;
3513	    case EF_SH2A: strcat (buf, ", sh2a"); break;
3514	    case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3515	    case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3516	    case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3517	    case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3518	    case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3519	    case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3520	    case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3521	    case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3522	    case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3523	    default: strcat (buf, _(", unknown ISA")); break;
3524	    }
3525
3526	  if (e_flags & EF_SH_PIC)
3527	    strcat (buf, ", pic");
3528
3529	  if (e_flags & EF_SH_FDPIC)
3530	    strcat (buf, ", fdpic");
3531	  break;
3532
3533        case EM_OR1K:
3534          if (e_flags & EF_OR1K_NODELAY)
3535            strcat (buf, ", no delay");
3536          break;
3537
3538	case EM_SPARCV9:
3539	  if (e_flags & EF_SPARC_32PLUS)
3540	    strcat (buf, ", v8+");
3541
3542	  if (e_flags & EF_SPARC_SUN_US1)
3543	    strcat (buf, ", ultrasparcI");
3544
3545	  if (e_flags & EF_SPARC_SUN_US3)
3546	    strcat (buf, ", ultrasparcIII");
3547
3548	  if (e_flags & EF_SPARC_HAL_R1)
3549	    strcat (buf, ", halr1");
3550
3551	  if (e_flags & EF_SPARC_LEDATA)
3552	    strcat (buf, ", ledata");
3553
3554	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3555	    strcat (buf, ", tso");
3556
3557	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3558	    strcat (buf, ", pso");
3559
3560	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3561	    strcat (buf, ", rmo");
3562	  break;
3563
3564	case EM_PARISC:
3565	  switch (e_flags & EF_PARISC_ARCH)
3566	    {
3567	    case EFA_PARISC_1_0:
3568	      strcpy (buf, ", PA-RISC 1.0");
3569	      break;
3570	    case EFA_PARISC_1_1:
3571	      strcpy (buf, ", PA-RISC 1.1");
3572	      break;
3573	    case EFA_PARISC_2_0:
3574	      strcpy (buf, ", PA-RISC 2.0");
3575	      break;
3576	    default:
3577	      break;
3578	    }
3579	  if (e_flags & EF_PARISC_TRAPNIL)
3580	    strcat (buf, ", trapnil");
3581	  if (e_flags & EF_PARISC_EXT)
3582	    strcat (buf, ", ext");
3583	  if (e_flags & EF_PARISC_LSB)
3584	    strcat (buf, ", lsb");
3585	  if (e_flags & EF_PARISC_WIDE)
3586	    strcat (buf, ", wide");
3587	  if (e_flags & EF_PARISC_NO_KABP)
3588	    strcat (buf, ", no kabp");
3589	  if (e_flags & EF_PARISC_LAZYSWAP)
3590	    strcat (buf, ", lazyswap");
3591	  break;
3592
3593	case EM_PJ:
3594	case EM_PJ_OLD:
3595	  if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3596	    strcat (buf, ", new calling convention");
3597
3598	  if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3599	    strcat (buf, ", gnu calling convention");
3600	  break;
3601
3602	case EM_IA_64:
3603	  if ((e_flags & EF_IA_64_ABI64))
3604	    strcat (buf, ", 64-bit");
3605	  else
3606	    strcat (buf, ", 32-bit");
3607	  if ((e_flags & EF_IA_64_REDUCEDFP))
3608	    strcat (buf, ", reduced fp model");
3609	  if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3610	    strcat (buf, ", no function descriptors, constant gp");
3611	  else if ((e_flags & EF_IA_64_CONS_GP))
3612	    strcat (buf, ", constant gp");
3613	  if ((e_flags & EF_IA_64_ABSOLUTE))
3614	    strcat (buf, ", absolute");
3615          if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3616            {
3617              if ((e_flags & EF_IA_64_VMS_LINKAGES))
3618                strcat (buf, ", vms_linkages");
3619              switch ((e_flags & EF_IA_64_VMS_COMCOD))
3620                {
3621                case EF_IA_64_VMS_COMCOD_SUCCESS:
3622                  break;
3623                case EF_IA_64_VMS_COMCOD_WARNING:
3624                  strcat (buf, ", warning");
3625                  break;
3626                case EF_IA_64_VMS_COMCOD_ERROR:
3627                  strcat (buf, ", error");
3628                  break;
3629                case EF_IA_64_VMS_COMCOD_ABORT:
3630                  strcat (buf, ", abort");
3631                  break;
3632                default:
3633		  warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3634			e_flags & EF_IA_64_VMS_COMCOD);
3635		  strcat (buf, ", <unknown>");
3636                }
3637            }
3638	  break;
3639
3640	case EM_VAX:
3641	  if ((e_flags & EF_VAX_NONPIC))
3642	    strcat (buf, ", non-PIC");
3643	  if ((e_flags & EF_VAX_DFLOAT))
3644	    strcat (buf, ", D-Float");
3645	  if ((e_flags & EF_VAX_GFLOAT))
3646	    strcat (buf, ", G-Float");
3647	  break;
3648
3649        case EM_VISIUM:
3650	  if (e_flags & EF_VISIUM_ARCH_MCM)
3651	    strcat (buf, ", mcm");
3652	  else if (e_flags & EF_VISIUM_ARCH_MCM24)
3653	    strcat (buf, ", mcm24");
3654	  if (e_flags & EF_VISIUM_ARCH_GR6)
3655	    strcat (buf, ", gr6");
3656	  break;
3657
3658	case EM_RL78:
3659	  switch (e_flags & E_FLAG_RL78_CPU_MASK)
3660	    {
3661	    case E_FLAG_RL78_ANY_CPU: break;
3662	    case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3663	    case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3664	    case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3665	    }
3666	  if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3667	    strcat (buf, ", 64-bit doubles");
3668	  break;
3669
3670	case EM_RX:
3671	  if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3672	    strcat (buf, ", 64-bit doubles");
3673	  if (e_flags & E_FLAG_RX_DSP)
3674	    strcat (buf, ", dsp");
3675	  if (e_flags & E_FLAG_RX_PID)
3676	    strcat (buf, ", pid");
3677	  if (e_flags & E_FLAG_RX_ABI)
3678	    strcat (buf, ", RX ABI");
3679	  if (e_flags & E_FLAG_RX_SINSNS_SET)
3680	    strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3681		    ? ", uses String instructions" : ", bans String instructions");
3682	  if (e_flags & E_FLAG_RX_V2)
3683	    strcat (buf, ", V2");
3684	  break;
3685
3686	case EM_S390:
3687	  if (e_flags & EF_S390_HIGH_GPRS)
3688	    strcat (buf, ", highgprs");
3689	  break;
3690
3691	case EM_TI_C6000:
3692	  if ((e_flags & EF_C6000_REL))
3693	    strcat (buf, ", relocatable module");
3694	  break;
3695
3696	case EM_MSP430:
3697	  strcat (buf, _(": architecture variant: "));
3698	  switch (e_flags & EF_MSP430_MACH)
3699	    {
3700	    case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3701	    case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3702	    case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3703	    case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3704	    case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3705	    case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3706	    case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3707	    case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3708	    case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3709	    case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3710	    case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3711	    case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3712	    case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3713	    case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3714	    case E_MSP430_MACH_MSP430X  : strcat (buf, "MSP430X"); break;
3715	    default:
3716	      strcat (buf, _(": unknown")); break;
3717	    }
3718
3719	  if (e_flags & ~ EF_MSP430_MACH)
3720	    strcat (buf, _(": unknown extra flag bits also present"));
3721	}
3722    }
3723
3724  return buf;
3725}
3726
3727static const char *
3728get_osabi_name (Filedata * filedata, unsigned int osabi)
3729{
3730  static char buff[32];
3731
3732  switch (osabi)
3733    {
3734    case ELFOSABI_NONE:		return "UNIX - System V";
3735    case ELFOSABI_HPUX:		return "UNIX - HP-UX";
3736    case ELFOSABI_NETBSD:	return "UNIX - NetBSD";
3737    case ELFOSABI_GNU:		return "UNIX - GNU";
3738    case ELFOSABI_SOLARIS:	return "UNIX - Solaris";
3739    case ELFOSABI_AIX:		return "UNIX - AIX";
3740    case ELFOSABI_IRIX:		return "UNIX - IRIX";
3741    case ELFOSABI_FREEBSD:	return "UNIX - FreeBSD";
3742    case ELFOSABI_TRU64:	return "UNIX - TRU64";
3743    case ELFOSABI_MODESTO:	return "Novell - Modesto";
3744    case ELFOSABI_OPENBSD:	return "UNIX - OpenBSD";
3745    case ELFOSABI_OPENVMS:	return "VMS - OpenVMS";
3746    case ELFOSABI_NSK:		return "HP - Non-Stop Kernel";
3747    case ELFOSABI_AROS:		return "AROS";
3748    case ELFOSABI_FENIXOS:	return "FenixOS";
3749    case ELFOSABI_CLOUDABI:	return "Nuxi CloudABI";
3750    case ELFOSABI_OPENVOS:	return "Stratus Technologies OpenVOS";
3751    default:
3752      if (osabi >= 64)
3753	switch (filedata->file_header.e_machine)
3754	  {
3755	  case EM_ARM:
3756	    switch (osabi)
3757	      {
3758	      case ELFOSABI_ARM:	return "ARM";
3759	      case ELFOSABI_ARM_FDPIC:	return "ARM FDPIC";
3760	      default:
3761		break;
3762	      }
3763	    break;
3764
3765	  case EM_MSP430:
3766	  case EM_MSP430_OLD:
3767	  case EM_VISIUM:
3768	    switch (osabi)
3769	      {
3770	      case ELFOSABI_STANDALONE:	return _("Standalone App");
3771	      default:
3772		break;
3773	      }
3774	    break;
3775
3776	  case EM_TI_C6000:
3777	    switch (osabi)
3778	      {
3779	      case ELFOSABI_C6000_ELFABI:	return _("Bare-metal C6000");
3780	      case ELFOSABI_C6000_LINUX:	return "Linux C6000";
3781	      default:
3782		break;
3783	      }
3784	    break;
3785
3786	  default:
3787	    break;
3788	  }
3789      snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3790      return buff;
3791    }
3792}
3793
3794static const char *
3795get_aarch64_segment_type (unsigned long type)
3796{
3797  switch (type)
3798    {
3799    case PT_AARCH64_ARCHEXT:  return "AARCH64_ARCHEXT";
3800    default:                  return NULL;
3801    }
3802}
3803
3804static const char *
3805get_arm_segment_type (unsigned long type)
3806{
3807  switch (type)
3808    {
3809    case PT_ARM_EXIDX: return "EXIDX";
3810    default:           return NULL;
3811    }
3812}
3813
3814static const char *
3815get_s390_segment_type (unsigned long type)
3816{
3817  switch (type)
3818    {
3819    case PT_S390_PGSTE: return "S390_PGSTE";
3820    default:            return NULL;
3821    }
3822}
3823
3824static const char *
3825get_mips_segment_type (unsigned long type)
3826{
3827  switch (type)
3828    {
3829    case PT_MIPS_REGINFO:   return "REGINFO";
3830    case PT_MIPS_RTPROC:    return "RTPROC";
3831    case PT_MIPS_OPTIONS:   return "OPTIONS";
3832    case PT_MIPS_ABIFLAGS:  return "ABIFLAGS";
3833    default:                return NULL;
3834    }
3835}
3836
3837static const char *
3838get_parisc_segment_type (unsigned long type)
3839{
3840  switch (type)
3841    {
3842    case PT_HP_TLS:		return "HP_TLS";
3843    case PT_HP_CORE_NONE:	return "HP_CORE_NONE";
3844    case PT_HP_CORE_VERSION:	return "HP_CORE_VERSION";
3845    case PT_HP_CORE_KERNEL:	return "HP_CORE_KERNEL";
3846    case PT_HP_CORE_COMM:	return "HP_CORE_COMM";
3847    case PT_HP_CORE_PROC:	return "HP_CORE_PROC";
3848    case PT_HP_CORE_LOADABLE:	return "HP_CORE_LOADABLE";
3849    case PT_HP_CORE_STACK:	return "HP_CORE_STACK";
3850    case PT_HP_CORE_SHM:	return "HP_CORE_SHM";
3851    case PT_HP_CORE_MMF:	return "HP_CORE_MMF";
3852    case PT_HP_PARALLEL:	return "HP_PARALLEL";
3853    case PT_HP_FASTBIND:	return "HP_FASTBIND";
3854    case PT_HP_OPT_ANNOT:	return "HP_OPT_ANNOT";
3855    case PT_HP_HSL_ANNOT:	return "HP_HSL_ANNOT";
3856    case PT_HP_STACK:		return "HP_STACK";
3857    case PT_HP_CORE_UTSNAME:	return "HP_CORE_UTSNAME";
3858    case PT_PARISC_ARCHEXT:	return "PARISC_ARCHEXT";
3859    case PT_PARISC_UNWIND:	return "PARISC_UNWIND";
3860    case PT_PARISC_WEAKORDER:	return "PARISC_WEAKORDER";
3861    default:                    return NULL;
3862    }
3863}
3864
3865static const char *
3866get_ia64_segment_type (unsigned long type)
3867{
3868  switch (type)
3869    {
3870    case PT_IA_64_ARCHEXT:	return "IA_64_ARCHEXT";
3871    case PT_IA_64_UNWIND:	return "IA_64_UNWIND";
3872    case PT_HP_TLS:		return "HP_TLS";
3873    case PT_IA_64_HP_OPT_ANOT:	return "HP_OPT_ANNOT";
3874    case PT_IA_64_HP_HSL_ANOT:	return "HP_HSL_ANNOT";
3875    case PT_IA_64_HP_STACK:	return "HP_STACK";
3876    default:                    return NULL;
3877    }
3878}
3879
3880static const char *
3881get_tic6x_segment_type (unsigned long type)
3882{
3883  switch (type)
3884    {
3885    case PT_C6000_PHATTR:  return "C6000_PHATTR";
3886    default:               return NULL;
3887    }
3888}
3889
3890static const char *
3891get_solaris_segment_type (unsigned long type)
3892{
3893  switch (type)
3894    {
3895    case 0x6464e550: return "PT_SUNW_UNWIND";
3896    case 0x6474e550: return "PT_SUNW_EH_FRAME";
3897    case 0x6ffffff7: return "PT_LOSUNW";
3898    case 0x6ffffffa: return "PT_SUNWBSS";
3899    case 0x6ffffffb: return "PT_SUNWSTACK";
3900    case 0x6ffffffc: return "PT_SUNWDTRACE";
3901    case 0x6ffffffd: return "PT_SUNWCAP";
3902    case 0x6fffffff: return "PT_HISUNW";
3903    default:         return NULL;
3904    }
3905}
3906
3907static const char *
3908get_segment_type (Filedata * filedata, unsigned long p_type)
3909{
3910  static char buff[32];
3911
3912  switch (p_type)
3913    {
3914    case PT_NULL:	return "NULL";
3915    case PT_LOAD:	return "LOAD";
3916    case PT_DYNAMIC:	return "DYNAMIC";
3917    case PT_INTERP:	return "INTERP";
3918    case PT_NOTE:	return "NOTE";
3919    case PT_SHLIB:	return "SHLIB";
3920    case PT_PHDR:	return "PHDR";
3921    case PT_TLS:	return "TLS";
3922    case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
3923    case PT_GNU_STACK:	return "GNU_STACK";
3924    case PT_GNU_RELRO:  return "GNU_RELRO";
3925
3926    default:
3927      if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
3928	{
3929	  sprintf (buff, "GNU_MBIND+%#lx",
3930		   p_type - PT_GNU_MBIND_LO);
3931	}
3932      else if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
3933	{
3934	  const char * result;
3935
3936	  switch (filedata->file_header.e_machine)
3937	    {
3938	    case EM_AARCH64:
3939	      result = get_aarch64_segment_type (p_type);
3940	      break;
3941	    case EM_ARM:
3942	      result = get_arm_segment_type (p_type);
3943	      break;
3944	    case EM_MIPS:
3945	    case EM_MIPS_RS3_LE:
3946	      result = get_mips_segment_type (p_type);
3947	      break;
3948	    case EM_PARISC:
3949	      result = get_parisc_segment_type (p_type);
3950	      break;
3951	    case EM_IA_64:
3952	      result = get_ia64_segment_type (p_type);
3953	      break;
3954	    case EM_TI_C6000:
3955	      result = get_tic6x_segment_type (p_type);
3956	      break;
3957	    case EM_S390:
3958	    case EM_S390_OLD:
3959	      result = get_s390_segment_type (p_type);
3960	      break;
3961	    default:
3962	      result = NULL;
3963	      break;
3964	    }
3965
3966	  if (result != NULL)
3967	    return result;
3968
3969	  sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
3970	}
3971      else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
3972	{
3973	  const char * result;
3974
3975	  switch (filedata->file_header.e_machine)
3976	    {
3977	    case EM_PARISC:
3978	      result = get_parisc_segment_type (p_type);
3979	      break;
3980	    case EM_IA_64:
3981	      result = get_ia64_segment_type (p_type);
3982	      break;
3983	    default:
3984	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
3985		result = get_solaris_segment_type (p_type);
3986	      else
3987		result = NULL;
3988	      break;
3989	    }
3990
3991	  if (result != NULL)
3992	    return result;
3993
3994	  sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
3995	}
3996      else
3997	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
3998
3999      return buff;
4000    }
4001}
4002
4003static const char *
4004get_arc_section_type_name (unsigned int sh_type)
4005{
4006  switch (sh_type)
4007    {
4008    case SHT_ARC_ATTRIBUTES:      return "ARC_ATTRIBUTES";
4009    default:
4010      break;
4011    }
4012  return NULL;
4013}
4014
4015static const char *
4016get_mips_section_type_name (unsigned int sh_type)
4017{
4018  switch (sh_type)
4019    {
4020    case SHT_MIPS_LIBLIST:	 return "MIPS_LIBLIST";
4021    case SHT_MIPS_MSYM:		 return "MIPS_MSYM";
4022    case SHT_MIPS_CONFLICT:	 return "MIPS_CONFLICT";
4023    case SHT_MIPS_GPTAB:	 return "MIPS_GPTAB";
4024    case SHT_MIPS_UCODE:	 return "MIPS_UCODE";
4025    case SHT_MIPS_DEBUG:	 return "MIPS_DEBUG";
4026    case SHT_MIPS_REGINFO:	 return "MIPS_REGINFO";
4027    case SHT_MIPS_PACKAGE:	 return "MIPS_PACKAGE";
4028    case SHT_MIPS_PACKSYM:	 return "MIPS_PACKSYM";
4029    case SHT_MIPS_RELD:		 return "MIPS_RELD";
4030    case SHT_MIPS_IFACE:	 return "MIPS_IFACE";
4031    case SHT_MIPS_CONTENT:	 return "MIPS_CONTENT";
4032    case SHT_MIPS_OPTIONS:	 return "MIPS_OPTIONS";
4033    case SHT_MIPS_SHDR:		 return "MIPS_SHDR";
4034    case SHT_MIPS_FDESC:	 return "MIPS_FDESC";
4035    case SHT_MIPS_EXTSYM:	 return "MIPS_EXTSYM";
4036    case SHT_MIPS_DENSE:	 return "MIPS_DENSE";
4037    case SHT_MIPS_PDESC:	 return "MIPS_PDESC";
4038    case SHT_MIPS_LOCSYM:	 return "MIPS_LOCSYM";
4039    case SHT_MIPS_AUXSYM:	 return "MIPS_AUXSYM";
4040    case SHT_MIPS_OPTSYM:	 return "MIPS_OPTSYM";
4041    case SHT_MIPS_LOCSTR:	 return "MIPS_LOCSTR";
4042    case SHT_MIPS_LINE:		 return "MIPS_LINE";
4043    case SHT_MIPS_RFDESC:	 return "MIPS_RFDESC";
4044    case SHT_MIPS_DELTASYM:	 return "MIPS_DELTASYM";
4045    case SHT_MIPS_DELTAINST:	 return "MIPS_DELTAINST";
4046    case SHT_MIPS_DELTACLASS:	 return "MIPS_DELTACLASS";
4047    case SHT_MIPS_DWARF:	 return "MIPS_DWARF";
4048    case SHT_MIPS_DELTADECL:	 return "MIPS_DELTADECL";
4049    case SHT_MIPS_SYMBOL_LIB:	 return "MIPS_SYMBOL_LIB";
4050    case SHT_MIPS_EVENTS:	 return "MIPS_EVENTS";
4051    case SHT_MIPS_TRANSLATE:	 return "MIPS_TRANSLATE";
4052    case SHT_MIPS_PIXIE:	 return "MIPS_PIXIE";
4053    case SHT_MIPS_XLATE:	 return "MIPS_XLATE";
4054    case SHT_MIPS_XLATE_DEBUG:	 return "MIPS_XLATE_DEBUG";
4055    case SHT_MIPS_WHIRL:	 return "MIPS_WHIRL";
4056    case SHT_MIPS_EH_REGION:	 return "MIPS_EH_REGION";
4057    case SHT_MIPS_XLATE_OLD:	 return "MIPS_XLATE_OLD";
4058    case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4059    case SHT_MIPS_ABIFLAGS:	 return "MIPS_ABIFLAGS";
4060    default:
4061      break;
4062    }
4063  return NULL;
4064}
4065
4066static const char *
4067get_parisc_section_type_name (unsigned int sh_type)
4068{
4069  switch (sh_type)
4070    {
4071    case SHT_PARISC_EXT:	return "PARISC_EXT";
4072    case SHT_PARISC_UNWIND:	return "PARISC_UNWIND";
4073    case SHT_PARISC_DOC:	return "PARISC_DOC";
4074    case SHT_PARISC_ANNOT:	return "PARISC_ANNOT";
4075    case SHT_PARISC_SYMEXTN:	return "PARISC_SYMEXTN";
4076    case SHT_PARISC_STUBS:	return "PARISC_STUBS";
4077    case SHT_PARISC_DLKM:	return "PARISC_DLKM";
4078    default:             	return NULL;
4079    }
4080}
4081
4082static const char *
4083get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4084{
4085  /* If the top 8 bits are 0x78 the next 8 are the os/abi ID.  */
4086  if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4087    return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4088
4089  switch (sh_type)
4090    {
4091    case SHT_IA_64_EXT:		       return "IA_64_EXT";
4092    case SHT_IA_64_UNWIND:	       return "IA_64_UNWIND";
4093    case SHT_IA_64_PRIORITY_INIT:      return "IA_64_PRIORITY_INIT";
4094    case SHT_IA_64_VMS_TRACE:          return "VMS_TRACE";
4095    case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4096    case SHT_IA_64_VMS_DEBUG:          return "VMS_DEBUG";
4097    case SHT_IA_64_VMS_DEBUG_STR:      return "VMS_DEBUG_STR";
4098    case SHT_IA_64_VMS_LINKAGES:       return "VMS_LINKAGES";
4099    case SHT_IA_64_VMS_SYMBOL_VECTOR:  return "VMS_SYMBOL_VECTOR";
4100    case SHT_IA_64_VMS_FIXUP:          return "VMS_FIXUP";
4101    default:
4102      break;
4103    }
4104  return NULL;
4105}
4106
4107static const char *
4108get_x86_64_section_type_name (unsigned int sh_type)
4109{
4110  switch (sh_type)
4111    {
4112    case SHT_X86_64_UNWIND:	return "X86_64_UNWIND";
4113    default:			return NULL;
4114    }
4115}
4116
4117static const char *
4118get_aarch64_section_type_name (unsigned int sh_type)
4119{
4120  switch (sh_type)
4121    {
4122    case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4123    default:			 return NULL;
4124    }
4125}
4126
4127static const char *
4128get_arm_section_type_name (unsigned int sh_type)
4129{
4130  switch (sh_type)
4131    {
4132    case SHT_ARM_EXIDX:           return "ARM_EXIDX";
4133    case SHT_ARM_PREEMPTMAP:      return "ARM_PREEMPTMAP";
4134    case SHT_ARM_ATTRIBUTES:      return "ARM_ATTRIBUTES";
4135    case SHT_ARM_DEBUGOVERLAY:    return "ARM_DEBUGOVERLAY";
4136    case SHT_ARM_OVERLAYSECTION:  return "ARM_OVERLAYSECTION";
4137    default:			  return NULL;
4138    }
4139}
4140
4141static const char *
4142get_tic6x_section_type_name (unsigned int sh_type)
4143{
4144  switch (sh_type)
4145    {
4146    case SHT_C6000_UNWIND:      return "C6000_UNWIND";
4147    case SHT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
4148    case SHT_C6000_ATTRIBUTES:  return "C6000_ATTRIBUTES";
4149    case SHT_TI_ICODE:          return "TI_ICODE";
4150    case SHT_TI_XREF:           return "TI_XREF";
4151    case SHT_TI_HANDLER:        return "TI_HANDLER";
4152    case SHT_TI_INITINFO:       return "TI_INITINFO";
4153    case SHT_TI_PHATTRS:        return "TI_PHATTRS";
4154    default:                    return NULL;
4155    }
4156}
4157
4158static const char *
4159get_msp430x_section_type_name (unsigned int sh_type)
4160{
4161  switch (sh_type)
4162    {
4163    case SHT_MSP430_SEC_FLAGS:    return "MSP430_SEC_FLAGS";
4164    case SHT_MSP430_SYM_ALIASES:  return "MSP430_SYM_ALIASES";
4165    case SHT_MSP430_ATTRIBUTES:   return "MSP430_ATTRIBUTES";
4166    default:                      return NULL;
4167    }
4168}
4169
4170static const char *
4171get_nfp_section_type_name (unsigned int sh_type)
4172{
4173  switch (sh_type)
4174    {
4175    case SHT_NFP_MECONFIG:	return "NFP_MECONFIG";
4176    case SHT_NFP_INITREG:	return "NFP_INITREG";
4177    case SHT_NFP_UDEBUG:	return "NFP_UDEBUG";
4178    default:			return NULL;
4179    }
4180}
4181
4182static const char *
4183get_v850_section_type_name (unsigned int sh_type)
4184{
4185  switch (sh_type)
4186    {
4187    case SHT_V850_SCOMMON:  return "V850 Small Common";
4188    case SHT_V850_TCOMMON:  return "V850 Tiny Common";
4189    case SHT_V850_ZCOMMON:  return "V850 Zero Common";
4190    case SHT_RENESAS_IOP:   return "RENESAS IOP";
4191    case SHT_RENESAS_INFO:  return "RENESAS INFO";
4192    default:                return NULL;
4193    }
4194}
4195
4196static const char *
4197get_section_type_name (Filedata * filedata, unsigned int sh_type)
4198{
4199  static char buff[32];
4200  const char * result;
4201
4202  switch (sh_type)
4203    {
4204    case SHT_NULL:		return "NULL";
4205    case SHT_PROGBITS:		return "PROGBITS";
4206    case SHT_SYMTAB:		return "SYMTAB";
4207    case SHT_STRTAB:		return "STRTAB";
4208    case SHT_RELA:		return "RELA";
4209    case SHT_HASH:		return "HASH";
4210    case SHT_DYNAMIC:		return "DYNAMIC";
4211    case SHT_NOTE:		return "NOTE";
4212    case SHT_NOBITS:		return "NOBITS";
4213    case SHT_REL:		return "REL";
4214    case SHT_SHLIB:		return "SHLIB";
4215    case SHT_DYNSYM:		return "DYNSYM";
4216    case SHT_INIT_ARRAY:	return "INIT_ARRAY";
4217    case SHT_FINI_ARRAY:	return "FINI_ARRAY";
4218    case SHT_PREINIT_ARRAY:	return "PREINIT_ARRAY";
4219    case SHT_GNU_HASH:		return "GNU_HASH";
4220    case SHT_GROUP:		return "GROUP";
4221    case SHT_SYMTAB_SHNDX:	return "SYMTAB SECTION INDICIES";
4222    case SHT_GNU_verdef:	return "VERDEF";
4223    case SHT_GNU_verneed:	return "VERNEED";
4224    case SHT_GNU_versym:	return "VERSYM";
4225    case 0x6ffffff0:		return "VERSYM";
4226    case 0x6ffffffc:		return "VERDEF";
4227    case 0x7ffffffd:		return "AUXILIARY";
4228    case 0x7fffffff:		return "FILTER";
4229    case SHT_GNU_LIBLIST:	return "GNU_LIBLIST";
4230
4231    default:
4232      if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4233	{
4234	  switch (filedata->file_header.e_machine)
4235	    {
4236	    case EM_ARC:
4237	    case EM_ARC_COMPACT:
4238	    case EM_ARC_COMPACT2:
4239	      result = get_arc_section_type_name (sh_type);
4240	      break;
4241	    case EM_MIPS:
4242	    case EM_MIPS_RS3_LE:
4243	      result = get_mips_section_type_name (sh_type);
4244	      break;
4245	    case EM_PARISC:
4246	      result = get_parisc_section_type_name (sh_type);
4247	      break;
4248	    case EM_IA_64:
4249	      result = get_ia64_section_type_name (filedata, sh_type);
4250	      break;
4251	    case EM_X86_64:
4252	    case EM_L1OM:
4253	    case EM_K1OM:
4254	      result = get_x86_64_section_type_name (sh_type);
4255	      break;
4256	    case EM_AARCH64:
4257	      result = get_aarch64_section_type_name (sh_type);
4258	      break;
4259	    case EM_ARM:
4260	      result = get_arm_section_type_name (sh_type);
4261	      break;
4262	    case EM_TI_C6000:
4263	      result = get_tic6x_section_type_name (sh_type);
4264	      break;
4265	    case EM_MSP430:
4266	      result = get_msp430x_section_type_name (sh_type);
4267	      break;
4268	    case EM_NFP:
4269	      result = get_nfp_section_type_name (sh_type);
4270	      break;
4271	    case EM_V800:
4272	    case EM_V850:
4273	    case EM_CYGNUS_V850:
4274	      result = get_v850_section_type_name (sh_type);
4275	      break;
4276	    default:
4277	      result = NULL;
4278	      break;
4279	    }
4280
4281	  if (result != NULL)
4282	    return result;
4283
4284	  sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4285	}
4286      else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4287	{
4288	  switch (filedata->file_header.e_machine)
4289	    {
4290	    case EM_IA_64:
4291	      result = get_ia64_section_type_name (filedata, sh_type);
4292	      break;
4293	    default:
4294	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4295		result = get_solaris_section_type (sh_type);
4296	      else
4297		{
4298		  switch (sh_type)
4299		    {
4300		    case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4301		    case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4302		    case SHT_GNU_HASH: result = "GNU_HASH"; break;
4303		    case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4304		    default:
4305		      result = NULL;
4306		      break;
4307		    }
4308		}
4309	      break;
4310	    }
4311
4312	  if (result != NULL)
4313	    return result;
4314
4315	  sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4316	}
4317      else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4318	{
4319	  switch (filedata->file_header.e_machine)
4320	    {
4321	    case EM_V800:
4322	    case EM_V850:
4323	    case EM_CYGNUS_V850:
4324	      result = get_v850_section_type_name (sh_type);
4325	      break;
4326	    default:
4327	      result = NULL;
4328	      break;
4329	    }
4330
4331	  if (result != NULL)
4332	    return result;
4333
4334	  sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4335	}
4336      else
4337	/* This message is probably going to be displayed in a 15
4338	   character wide field, so put the hex value first.  */
4339	snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4340
4341      return buff;
4342    }
4343}
4344
4345#define OPTION_DEBUG_DUMP	512
4346#define OPTION_DYN_SYMS		513
4347#define OPTION_DWARF_DEPTH	514
4348#define OPTION_DWARF_START	515
4349#define OPTION_DWARF_CHECK	516
4350
4351static struct option options[] =
4352{
4353  {"all",	       no_argument, 0, 'a'},
4354  {"file-header",      no_argument, 0, 'h'},
4355  {"program-headers",  no_argument, 0, 'l'},
4356  {"headers",	       no_argument, 0, 'e'},
4357  {"histogram",	       no_argument, 0, 'I'},
4358  {"segments",	       no_argument, 0, 'l'},
4359  {"sections",	       no_argument, 0, 'S'},
4360  {"section-headers",  no_argument, 0, 'S'},
4361  {"section-groups",   no_argument, 0, 'g'},
4362  {"section-details",  no_argument, 0, 't'},
4363  {"full-section-name",no_argument, 0, 'N'},
4364  {"symbols",	       no_argument, 0, 's'},
4365  {"syms",	       no_argument, 0, 's'},
4366  {"dyn-syms",	       no_argument, 0, OPTION_DYN_SYMS},
4367  {"relocs",	       no_argument, 0, 'r'},
4368  {"notes",	       no_argument, 0, 'n'},
4369  {"dynamic",	       no_argument, 0, 'd'},
4370  {"special-files",    no_argument, 0, 'f'},
4371  {"arch-specific",    no_argument, 0, 'A'},
4372  {"version-info",     no_argument, 0, 'V'},
4373  {"use-dynamic",      no_argument, 0, 'D'},
4374  {"unwind",	       no_argument, 0, 'u'},
4375  {"archive-index",    no_argument, 0, 'c'},
4376  {"hex-dump",	       required_argument, 0, 'x'},
4377  {"relocated-dump",   required_argument, 0, 'R'},
4378  {"string-dump",      required_argument, 0, 'p'},
4379  {"decompress",       no_argument, 0, 'z'},
4380#ifdef SUPPORT_DISASSEMBLY
4381  {"instruction-dump", required_argument, 0, 'i'},
4382#endif
4383  {"debug-dump",       optional_argument, 0, OPTION_DEBUG_DUMP},
4384
4385  {"dwarf-depth",      required_argument, 0, OPTION_DWARF_DEPTH},
4386  {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
4387  {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
4388
4389  {"version",	       no_argument, 0, 'v'},
4390  {"wide",	       no_argument, 0, 'W'},
4391  {"help",	       no_argument, 0, 'H'},
4392  {0,		       no_argument, 0, 0}
4393};
4394
4395static void
4396usage (FILE * stream)
4397{
4398  fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4399  fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4400  fprintf (stream, _(" Options are:\n\
4401  -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n\
4402  -h --file-header       Display the ELF file header\n\
4403  -l --program-headers   Display the program headers\n\
4404     --segments          An alias for --program-headers\n\
4405  -S --section-headers   Display the sections' header\n\
4406     --sections          An alias for --section-headers\n\
4407  -g --section-groups    Display the section groups\n\
4408  -t --section-details   Display the section details\n\
4409  -e --headers           Equivalent to: -h -l -S\n\
4410  -s --syms              Display the symbol table\n\
4411     --symbols           An alias for --syms\n\
4412  --dyn-syms             Display the dynamic symbol table\n\
4413  -n --notes             Display the core notes (if present)\n\
4414  -r --relocs            Display the relocations (if present)\n\
4415  -u --unwind            Display the unwind info (if present)\n\
4416  -d --dynamic           Display the dynamic section (if present)\n\
4417  -f --special-files     Process non-plain files too\n\
4418  -V --version-info      Display the version sections (if present)\n\
4419  -A --arch-specific     Display architecture specific information (if any)\n\
4420  -c --archive-index     Display the symbol/file index in an archive\n\
4421  -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
4422  -x --hex-dump=<number|name>\n\
4423                         Dump the contents of section <number|name> as bytes\n\
4424  -p --string-dump=<number|name>\n\
4425                         Dump the contents of section <number|name> as strings\n\
4426  -R --relocated-dump=<number|name>\n\
4427                         Dump the contents of section <number|name> as relocated bytes\n\
4428  -z --decompress        Decompress section before dumping it\n\
4429  -w[lLiaprmfFsoRtUuTgAckK] or\n\
4430  --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
4431               =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
4432               =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
4433               =addr,=cu_index,=links,=follow-links]\n\
4434                         Display the contents of DWARF debug sections\n"));
4435  fprintf (stream, _("\
4436  --dwarf-depth=N        Do not display DIEs at depth N or greater\n\
4437  --dwarf-start=N        Display DIEs starting with N, at the same depth\n\
4438                         or deeper\n"));
4439#ifdef SUPPORT_DISASSEMBLY
4440  fprintf (stream, _("\
4441  -i --instruction-dump=<number|name>\n\
4442                         Disassemble the contents of section <number|name>\n"));
4443#endif
4444  fprintf (stream, _("\
4445  -I --histogram         Display histogram of bucket list lengths\n\
4446  -W --wide              Allow output width to exceed 80 characters\n\
4447  @<file>                Read options from <file>\n\
4448  -H --help              Display this information\n\
4449  -v --version           Display the version number of readelf\n"));
4450
4451  if (REPORT_BUGS_TO[0] && stream == stdout)
4452    fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4453
4454  exit (stream == stdout ? 0 : 1);
4455}
4456
4457/* Record the fact that the user wants the contents of section number
4458   SECTION to be displayed using the method(s) encoded as flags bits
4459   in TYPE.  Note, TYPE can be zero if we are creating the array for
4460   the first time.  */
4461
4462static void
4463request_dump_bynumber (Filedata * filedata, unsigned int section, dump_type type)
4464{
4465  if (section >= filedata->num_dump_sects)
4466    {
4467      dump_type * new_dump_sects;
4468
4469      new_dump_sects = (dump_type *) calloc (section + 1,
4470                                             sizeof (* new_dump_sects));
4471
4472      if (new_dump_sects == NULL)
4473	error (_("Out of memory allocating dump request table.\n"));
4474      else
4475	{
4476	  if (filedata->dump_sects)
4477	    {
4478	      /* Copy current flag settings.  */
4479	      memcpy (new_dump_sects, filedata->dump_sects,
4480		      filedata->num_dump_sects * sizeof (* new_dump_sects));
4481
4482	      free (filedata->dump_sects);
4483	    }
4484
4485	  filedata->dump_sects = new_dump_sects;
4486	  filedata->num_dump_sects = section + 1;
4487	}
4488    }
4489
4490  if (filedata->dump_sects)
4491    filedata->dump_sects[section] |= type;
4492}
4493
4494/* Request a dump by section name.  */
4495
4496static void
4497request_dump_byname (const char * section, dump_type type)
4498{
4499  struct dump_list_entry * new_request;
4500
4501  new_request = (struct dump_list_entry *)
4502      malloc (sizeof (struct dump_list_entry));
4503  if (!new_request)
4504    error (_("Out of memory allocating dump request table.\n"));
4505
4506  new_request->name = strdup (section);
4507  if (!new_request->name)
4508    error (_("Out of memory allocating dump request table.\n"));
4509
4510  new_request->type = type;
4511
4512  new_request->next = dump_sects_byname;
4513  dump_sects_byname = new_request;
4514}
4515
4516static inline void
4517request_dump (Filedata * filedata, dump_type type)
4518{
4519  int section;
4520  char * cp;
4521
4522  do_dump++;
4523  section = strtoul (optarg, & cp, 0);
4524
4525  if (! *cp && section >= 0)
4526    request_dump_bynumber (filedata, section, type);
4527  else
4528    request_dump_byname (optarg, type);
4529}
4530
4531static void
4532parse_args (Filedata * filedata, int argc, char ** argv)
4533{
4534  int c;
4535
4536  if (argc < 2)
4537    usage (stderr);
4538
4539  while ((c = getopt_long
4540	  (argc, argv, "ADHINR:SVWacdefghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4541    {
4542      switch (c)
4543	{
4544	case 0:
4545	  /* Long options.  */
4546	  break;
4547	case 'H':
4548	  usage (stdout);
4549	  break;
4550
4551	case 'a':
4552	  do_syms = TRUE;
4553	  do_reloc = TRUE;
4554	  do_unwind = TRUE;
4555	  do_dynamic = TRUE;
4556	  do_header = TRUE;
4557	  do_sections = TRUE;
4558	  do_section_groups = TRUE;
4559	  do_segments = TRUE;
4560	  do_version = TRUE;
4561	  do_histogram = TRUE;
4562	  do_arch = TRUE;
4563	  do_notes = TRUE;
4564	  break;
4565	case 'g':
4566	  do_section_groups = TRUE;
4567	  break;
4568	case 't':
4569	case 'N':
4570	  do_sections = TRUE;
4571	  do_section_details = TRUE;
4572	  break;
4573	case 'e':
4574	  do_header = TRUE;
4575	  do_sections = TRUE;
4576	  do_segments = TRUE;
4577	  break;
4578	case 'A':
4579	  do_arch = TRUE;
4580	  break;
4581	case 'D':
4582	  do_using_dynamic = TRUE;
4583	  break;
4584	case 'r':
4585	  do_reloc = TRUE;
4586	  break;
4587	case 'u':
4588	  do_unwind = TRUE;
4589	  break;
4590	case 'f':
4591	  do_special_files++;
4592	  break;
4593	case 'h':
4594	  do_header = TRUE;
4595	  break;
4596	case 'l':
4597	  do_segments = TRUE;
4598	  break;
4599	case 's':
4600	  do_syms = TRUE;
4601	  break;
4602	case 'S':
4603	  do_sections = TRUE;
4604	  break;
4605	case 'd':
4606	  do_dynamic = TRUE;
4607	  break;
4608	case 'I':
4609	  do_histogram = TRUE;
4610	  break;
4611	case 'n':
4612	  do_notes = TRUE;
4613	  break;
4614	case 'c':
4615	  do_archive_index = TRUE;
4616	  break;
4617	case 'x':
4618	  request_dump (filedata, HEX_DUMP);
4619	  break;
4620	case 'p':
4621	  request_dump (filedata, STRING_DUMP);
4622	  break;
4623	case 'R':
4624	  request_dump (filedata, RELOC_DUMP);
4625	  break;
4626	case 'z':
4627	  decompress_dumps = TRUE;
4628	  break;
4629	case 'w':
4630	  do_dump = TRUE;
4631	  if (optarg == 0)
4632	    {
4633	      do_debugging = TRUE;
4634	      dwarf_select_sections_all ();
4635	    }
4636	  else
4637	    {
4638	      do_debugging = FALSE;
4639	      dwarf_select_sections_by_letters (optarg);
4640	    }
4641	  break;
4642	case OPTION_DEBUG_DUMP:
4643	  do_dump = TRUE;
4644	  if (optarg == 0)
4645	    do_debugging = TRUE;
4646	  else
4647	    {
4648	      do_debugging = FALSE;
4649	      dwarf_select_sections_by_names (optarg);
4650	    }
4651	  break;
4652	case OPTION_DWARF_DEPTH:
4653	  {
4654	    char *cp;
4655
4656	    dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4657	  }
4658	  break;
4659	case OPTION_DWARF_START:
4660	  {
4661	    char *cp;
4662
4663	    dwarf_start_die = strtoul (optarg, & cp, 0);
4664	  }
4665	  break;
4666	case OPTION_DWARF_CHECK:
4667	  dwarf_check = TRUE;
4668	  break;
4669	case OPTION_DYN_SYMS:
4670	  do_dyn_syms = TRUE;
4671	  break;
4672#ifdef SUPPORT_DISASSEMBLY
4673	case 'i':
4674	  request_dump (filedata, DISASS_DUMP);
4675	  break;
4676#endif
4677	case 'v':
4678	  print_version (program_name);
4679	  break;
4680	case 'V':
4681	  do_version = TRUE;
4682	  break;
4683	case 'W':
4684	  do_wide = TRUE;
4685	  break;
4686	default:
4687	  /* xgettext:c-format */
4688	  error (_("Invalid option '-%c'\n"), c);
4689	  /* Fall through.  */
4690	case '?':
4691	  usage (stderr);
4692	}
4693    }
4694
4695  if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
4696      && !do_segments && !do_header && !do_dump && !do_version
4697      && !do_histogram && !do_debugging && !do_arch && !do_notes
4698      && !do_section_groups && !do_archive_index
4699      && !do_dyn_syms)
4700    usage (stderr);
4701}
4702
4703static const char *
4704get_elf_class (unsigned int elf_class)
4705{
4706  static char buff[32];
4707
4708  switch (elf_class)
4709    {
4710    case ELFCLASSNONE: return _("none");
4711    case ELFCLASS32:   return "ELF32";
4712    case ELFCLASS64:   return "ELF64";
4713    default:
4714      snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
4715      return buff;
4716    }
4717}
4718
4719static const char *
4720get_data_encoding (unsigned int encoding)
4721{
4722  static char buff[32];
4723
4724  switch (encoding)
4725    {
4726    case ELFDATANONE: return _("none");
4727    case ELFDATA2LSB: return _("2's complement, little endian");
4728    case ELFDATA2MSB: return _("2's complement, big endian");
4729    default:
4730      snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
4731      return buff;
4732    }
4733}
4734
4735/* Decode the data held in 'filedata->file_header'.  */
4736
4737static bfd_boolean
4738process_file_header (Filedata * filedata)
4739{
4740  Elf_Internal_Ehdr * header = & filedata->file_header;
4741
4742  if (   header->e_ident[EI_MAG0] != ELFMAG0
4743      || header->e_ident[EI_MAG1] != ELFMAG1
4744      || header->e_ident[EI_MAG2] != ELFMAG2
4745      || header->e_ident[EI_MAG3] != ELFMAG3)
4746    {
4747      error
4748	(_("Not an ELF file - it has the wrong magic bytes at the start\n"));
4749      return FALSE;
4750    }
4751
4752  init_dwarf_regnames (header->e_machine);
4753
4754  if (do_header)
4755    {
4756      unsigned i;
4757
4758      printf (_("ELF Header:\n"));
4759      printf (_("  Magic:   "));
4760      for (i = 0; i < EI_NIDENT; i++)
4761	printf ("%2.2x ", header->e_ident[i]);
4762      printf ("\n");
4763      printf (_("  Class:                             %s\n"),
4764	      get_elf_class (header->e_ident[EI_CLASS]));
4765      printf (_("  Data:                              %s\n"),
4766	      get_data_encoding (header->e_ident[EI_DATA]));
4767      printf (_("  Version:                           %d %s\n"),
4768	      header->e_ident[EI_VERSION],
4769	      (header->e_ident[EI_VERSION] == EV_CURRENT
4770	       ? "(current)"
4771	       : (header->e_ident[EI_VERSION] != EV_NONE
4772		  ? _("<unknown: %lx>")
4773		  : "")));
4774      printf (_("  OS/ABI:                            %s\n"),
4775	      get_osabi_name (filedata, header->e_ident[EI_OSABI]));
4776      printf (_("  ABI Version:                       %d\n"),
4777	      header->e_ident[EI_ABIVERSION]);
4778      printf (_("  Type:                              %s\n"),
4779	      get_file_type (header->e_type));
4780      printf (_("  Machine:                           %s\n"),
4781	      get_machine_name (header->e_machine));
4782      printf (_("  Version:                           0x%lx\n"),
4783	      (unsigned long) header->e_version);
4784
4785      printf (_("  Entry point address:               "));
4786      print_vma ((bfd_vma) header->e_entry, PREFIX_HEX);
4787      printf (_("\n  Start of program headers:          "));
4788      print_vma ((bfd_vma) header->e_phoff, DEC);
4789      printf (_(" (bytes into file)\n  Start of section headers:          "));
4790      print_vma ((bfd_vma) header->e_shoff, DEC);
4791      printf (_(" (bytes into file)\n"));
4792
4793      printf (_("  Flags:                             0x%lx%s\n"),
4794	      (unsigned long) header->e_flags,
4795	      get_machine_flags (filedata, header->e_flags, header->e_machine));
4796      printf (_("  Size of this header:               %ld (bytes)\n"),
4797	      (long) header->e_ehsize);
4798      printf (_("  Size of program headers:           %ld (bytes)\n"),
4799	      (long) header->e_phentsize);
4800      printf (_("  Number of program headers:         %ld"),
4801	      (long) header->e_phnum);
4802      if (filedata->section_headers != NULL
4803	  && header->e_phnum == PN_XNUM
4804	  && filedata->section_headers[0].sh_info != 0)
4805	printf (" (%ld)", (long) filedata->section_headers[0].sh_info);
4806      putc ('\n', stdout);
4807      printf (_("  Size of section headers:           %ld (bytes)\n"),
4808	      (long) header->e_shentsize);
4809      printf (_("  Number of section headers:         %ld"),
4810	      (long) header->e_shnum);
4811      if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
4812	printf (" (%ld)", (long) filedata->section_headers[0].sh_size);
4813      putc ('\n', stdout);
4814      printf (_("  Section header string table index: %ld"),
4815	      (long) header->e_shstrndx);
4816      if (filedata->section_headers != NULL
4817	  && header->e_shstrndx == (SHN_XINDEX & 0xffff))
4818	printf (" (%u)", filedata->section_headers[0].sh_link);
4819      else if (header->e_shstrndx != SHN_UNDEF
4820	       && header->e_shstrndx >= header->e_shnum)
4821	printf (_(" <corrupt: out of range>"));
4822      putc ('\n', stdout);
4823    }
4824
4825  if (filedata->section_headers != NULL)
4826    {
4827      if (header->e_phnum == PN_XNUM
4828	  && filedata->section_headers[0].sh_info != 0)
4829	header->e_phnum = filedata->section_headers[0].sh_info;
4830      if (header->e_shnum == SHN_UNDEF)
4831	header->e_shnum = filedata->section_headers[0].sh_size;
4832      if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
4833	header->e_shstrndx = filedata->section_headers[0].sh_link;
4834      if (header->e_shstrndx >= header->e_shnum)
4835	header->e_shstrndx = SHN_UNDEF;
4836      free (filedata->section_headers);
4837      filedata->section_headers = NULL;
4838    }
4839
4840  return TRUE;
4841}
4842
4843/* Read in the program headers from FILEDATA and store them in PHEADERS.
4844   Returns TRUE upon success, FALSE otherwise.  Loads 32-bit headers.  */
4845
4846static bfd_boolean
4847get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
4848{
4849  Elf32_External_Phdr * phdrs;
4850  Elf32_External_Phdr * external;
4851  Elf_Internal_Phdr *   internal;
4852  unsigned int i;
4853  unsigned int size = filedata->file_header.e_phentsize;
4854  unsigned int num  = filedata->file_header.e_phnum;
4855
4856  /* PR binutils/17531: Cope with unexpected section header sizes.  */
4857  if (size == 0 || num == 0)
4858    return FALSE;
4859  if (size < sizeof * phdrs)
4860    {
4861      error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4862      return FALSE;
4863    }
4864  if (size > sizeof * phdrs)
4865    warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4866
4867  phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
4868                                            size, num, _("program headers"));
4869  if (phdrs == NULL)
4870    return FALSE;
4871
4872  for (i = 0, internal = pheaders, external = phdrs;
4873       i < filedata->file_header.e_phnum;
4874       i++, internal++, external++)
4875    {
4876      internal->p_type   = BYTE_GET (external->p_type);
4877      internal->p_offset = BYTE_GET (external->p_offset);
4878      internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4879      internal->p_paddr  = BYTE_GET (external->p_paddr);
4880      internal->p_filesz = BYTE_GET (external->p_filesz);
4881      internal->p_memsz  = BYTE_GET (external->p_memsz);
4882      internal->p_flags  = BYTE_GET (external->p_flags);
4883      internal->p_align  = BYTE_GET (external->p_align);
4884    }
4885
4886  free (phdrs);
4887  return TRUE;
4888}
4889
4890/* Read in the program headers from FILEDATA and store them in PHEADERS.
4891   Returns TRUE upon success, FALSE otherwise.  Loads 64-bit headers.  */
4892
4893static bfd_boolean
4894get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
4895{
4896  Elf64_External_Phdr * phdrs;
4897  Elf64_External_Phdr * external;
4898  Elf_Internal_Phdr *   internal;
4899  unsigned int i;
4900  unsigned int size = filedata->file_header.e_phentsize;
4901  unsigned int num  = filedata->file_header.e_phnum;
4902
4903  /* PR binutils/17531: Cope with unexpected section header sizes.  */
4904  if (size == 0 || num == 0)
4905    return FALSE;
4906  if (size < sizeof * phdrs)
4907    {
4908      error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4909      return FALSE;
4910    }
4911  if (size > sizeof * phdrs)
4912    warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4913
4914  phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
4915                                            size, num, _("program headers"));
4916  if (!phdrs)
4917    return FALSE;
4918
4919  for (i = 0, internal = pheaders, external = phdrs;
4920       i < filedata->file_header.e_phnum;
4921       i++, internal++, external++)
4922    {
4923      internal->p_type   = BYTE_GET (external->p_type);
4924      internal->p_flags  = BYTE_GET (external->p_flags);
4925      internal->p_offset = BYTE_GET (external->p_offset);
4926      internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4927      internal->p_paddr  = BYTE_GET (external->p_paddr);
4928      internal->p_filesz = BYTE_GET (external->p_filesz);
4929      internal->p_memsz  = BYTE_GET (external->p_memsz);
4930      internal->p_align  = BYTE_GET (external->p_align);
4931    }
4932
4933  free (phdrs);
4934  return TRUE;
4935}
4936
4937/* Returns TRUE if the program headers were read into `program_headers'.  */
4938
4939static bfd_boolean
4940get_program_headers (Filedata * filedata)
4941{
4942  Elf_Internal_Phdr * phdrs;
4943
4944  /* Check cache of prior read.  */
4945  if (filedata->program_headers != NULL)
4946    return TRUE;
4947
4948  /* Be kind to memory checkers by looking for
4949     e_phnum values which we know must be invalid.  */
4950  if (filedata->file_header.e_phnum
4951      * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
4952      >= filedata->file_size)
4953    {
4954      error (_("Too many program headers - %#x - the file is not that big\n"),
4955	     filedata->file_header.e_phnum);
4956      return FALSE;
4957    }
4958
4959  phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
4960					 sizeof (Elf_Internal_Phdr));
4961  if (phdrs == NULL)
4962    {
4963      error (_("Out of memory reading %u program headers\n"),
4964	     filedata->file_header.e_phnum);
4965      return FALSE;
4966    }
4967
4968  if (is_32bit_elf
4969      ? get_32bit_program_headers (filedata, phdrs)
4970      : get_64bit_program_headers (filedata, phdrs))
4971    {
4972      filedata->program_headers = phdrs;
4973      return TRUE;
4974    }
4975
4976  free (phdrs);
4977  return FALSE;
4978}
4979
4980/* Returns TRUE if the program headers were loaded.  */
4981
4982static bfd_boolean
4983process_program_headers (Filedata * filedata)
4984{
4985  Elf_Internal_Phdr * segment;
4986  unsigned int i;
4987  Elf_Internal_Phdr * previous_load = NULL;
4988
4989  if (filedata->file_header.e_phnum == 0)
4990    {
4991      /* PR binutils/12467.  */
4992      if (filedata->file_header.e_phoff != 0)
4993	{
4994	  warn (_("possibly corrupt ELF header - it has a non-zero program"
4995		  " header offset, but no program headers\n"));
4996	  return FALSE;
4997	}
4998      else if (do_segments)
4999	printf (_("\nThere are no program headers in this file.\n"));
5000      return TRUE;
5001    }
5002
5003  if (do_segments && !do_header)
5004    {
5005      printf (_("\nElf file type is %s\n"), get_file_type (filedata->file_header.e_type));
5006      printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5007      printf (ngettext ("There is %d program header, starting at offset %s\n",
5008			"There are %d program headers, starting at offset %s\n",
5009			filedata->file_header.e_phnum),
5010	      filedata->file_header.e_phnum,
5011	      bfd_vmatoa ("u", filedata->file_header.e_phoff));
5012    }
5013
5014  if (! get_program_headers (filedata))
5015    return TRUE;
5016
5017  if (do_segments)
5018    {
5019      if (filedata->file_header.e_phnum > 1)
5020	printf (_("\nProgram Headers:\n"));
5021      else
5022	printf (_("\nProgram Header:\n"));
5023
5024      if (is_32bit_elf)
5025	printf
5026	  (_("  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n"));
5027      else if (do_wide)
5028	printf
5029	  (_("  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align\n"));
5030      else
5031	{
5032	  printf
5033	    (_("  Type           Offset             VirtAddr           PhysAddr\n"));
5034	  printf
5035	    (_("                 FileSiz            MemSiz              Flags  Align\n"));
5036	}
5037    }
5038
5039  dynamic_addr = 0;
5040  dynamic_size = 0;
5041
5042  for (i = 0, segment = filedata->program_headers;
5043       i < filedata->file_header.e_phnum;
5044       i++, segment++)
5045    {
5046      if (do_segments)
5047	{
5048	  printf ("  %-14.14s ", get_segment_type (filedata, segment->p_type));
5049
5050	  if (is_32bit_elf)
5051	    {
5052	      printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5053	      printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5054	      printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5055	      printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5056	      printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5057	      printf ("%c%c%c ",
5058		      (segment->p_flags & PF_R ? 'R' : ' '),
5059		      (segment->p_flags & PF_W ? 'W' : ' '),
5060		      (segment->p_flags & PF_X ? 'E' : ' '));
5061	      printf ("%#lx", (unsigned long) segment->p_align);
5062	    }
5063	  else if (do_wide)
5064	    {
5065	      if ((unsigned long) segment->p_offset == segment->p_offset)
5066		printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5067	      else
5068		{
5069		  print_vma (segment->p_offset, FULL_HEX);
5070		  putchar (' ');
5071		}
5072
5073	      print_vma (segment->p_vaddr, FULL_HEX);
5074	      putchar (' ');
5075	      print_vma (segment->p_paddr, FULL_HEX);
5076	      putchar (' ');
5077
5078	      if ((unsigned long) segment->p_filesz == segment->p_filesz)
5079		printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5080	      else
5081		{
5082		  print_vma (segment->p_filesz, FULL_HEX);
5083		  putchar (' ');
5084		}
5085
5086	      if ((unsigned long) segment->p_memsz == segment->p_memsz)
5087		printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5088	      else
5089		{
5090		  print_vma (segment->p_memsz, FULL_HEX);
5091		}
5092
5093	      printf (" %c%c%c ",
5094		      (segment->p_flags & PF_R ? 'R' : ' '),
5095		      (segment->p_flags & PF_W ? 'W' : ' '),
5096		      (segment->p_flags & PF_X ? 'E' : ' '));
5097
5098	      if ((unsigned long) segment->p_align == segment->p_align)
5099		printf ("%#lx", (unsigned long) segment->p_align);
5100	      else
5101		{
5102		  print_vma (segment->p_align, PREFIX_HEX);
5103		}
5104	    }
5105	  else
5106	    {
5107	      print_vma (segment->p_offset, FULL_HEX);
5108	      putchar (' ');
5109	      print_vma (segment->p_vaddr, FULL_HEX);
5110	      putchar (' ');
5111	      print_vma (segment->p_paddr, FULL_HEX);
5112	      printf ("\n                 ");
5113	      print_vma (segment->p_filesz, FULL_HEX);
5114	      putchar (' ');
5115	      print_vma (segment->p_memsz, FULL_HEX);
5116	      printf ("  %c%c%c    ",
5117		      (segment->p_flags & PF_R ? 'R' : ' '),
5118		      (segment->p_flags & PF_W ? 'W' : ' '),
5119		      (segment->p_flags & PF_X ? 'E' : ' '));
5120	      print_vma (segment->p_align, PREFIX_HEX);
5121	    }
5122
5123	  putc ('\n', stdout);
5124	}
5125
5126      switch (segment->p_type)
5127	{
5128	case PT_LOAD:
5129#if 0 /* Do not warn about out of order PT_LOAD segments.  Although officially
5130	 required by the ELF standard, several programs, including the Linux
5131	 kernel, make use of non-ordered segments.  */
5132	  if (previous_load
5133	      && previous_load->p_vaddr > segment->p_vaddr)
5134	    error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5135#endif
5136	  if (segment->p_memsz < segment->p_filesz)
5137	    error (_("the segment's file size is larger than its memory size\n"));
5138	  previous_load = segment;
5139	  break;
5140
5141	case PT_PHDR:
5142	  /* PR 20815 - Verify that the program header is loaded into memory.  */
5143	  if (i > 0 && previous_load != NULL)
5144	    error (_("the PHDR segment must occur before any LOAD segment\n"));
5145	  if (filedata->file_header.e_machine != EM_PARISC)
5146	    {
5147	      unsigned int j;
5148
5149	      for (j = 1; j < filedata->file_header.e_phnum; j++)
5150		if (filedata->program_headers[j].p_vaddr <= segment->p_vaddr
5151		    && (filedata->program_headers[j].p_vaddr
5152			+ filedata->program_headers[j].p_memsz)
5153		    >= (segment->p_vaddr + segment->p_filesz))
5154		  break;
5155	      if (j == filedata->file_header.e_phnum)
5156		error (_("the PHDR segment is not covered by a LOAD segment\n"));
5157	    }
5158	  break;
5159
5160	case PT_DYNAMIC:
5161	  if (dynamic_addr)
5162	    error (_("more than one dynamic segment\n"));
5163
5164	  /* By default, assume that the .dynamic section is the first
5165	     section in the DYNAMIC segment.  */
5166	  dynamic_addr = segment->p_offset;
5167	  dynamic_size = segment->p_filesz;
5168
5169	  /* Try to locate the .dynamic section. If there is
5170	     a section header table, we can easily locate it.  */
5171	  if (filedata->section_headers != NULL)
5172	    {
5173	      Elf_Internal_Shdr * sec;
5174
5175	      sec = find_section (filedata, ".dynamic");
5176	      if (sec == NULL || sec->sh_size == 0)
5177		{
5178                  /* A corresponding .dynamic section is expected, but on
5179                     IA-64/OpenVMS it is OK for it to be missing.  */
5180                  if (!is_ia64_vms (filedata))
5181                    error (_("no .dynamic section in the dynamic segment\n"));
5182		  break;
5183		}
5184
5185	      if (sec->sh_type == SHT_NOBITS)
5186		{
5187		  dynamic_size = 0;
5188		  break;
5189		}
5190
5191	      dynamic_addr = sec->sh_offset;
5192	      dynamic_size = sec->sh_size;
5193
5194	      if (dynamic_addr < segment->p_offset
5195		  || dynamic_addr > segment->p_offset + segment->p_filesz)
5196		warn (_("the .dynamic section is not contained"
5197			" within the dynamic segment\n"));
5198	      else if (dynamic_addr > segment->p_offset)
5199		warn (_("the .dynamic section is not the first section"
5200			" in the dynamic segment.\n"));
5201	    }
5202
5203	  /* PR binutils/17512: Avoid corrupt dynamic section info in the
5204	     segment.  Check this after matching against the section headers
5205	     so we don't warn on debuginfo file (which have NOBITS .dynamic
5206	     sections).  */
5207	  if (dynamic_addr + dynamic_size >= filedata->file_size)
5208	    {
5209	      error (_("the dynamic segment offset + size exceeds the size of the file\n"));
5210	      dynamic_addr = dynamic_size = 0;
5211	    }
5212	  break;
5213
5214	case PT_INTERP:
5215	  if (fseek (filedata->handle, archive_file_offset + (long) segment->p_offset,
5216		     SEEK_SET))
5217	    error (_("Unable to find program interpreter name\n"));
5218	  else
5219	    {
5220	      char fmt [32];
5221	      int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
5222
5223	      if (ret >= (int) sizeof (fmt) || ret < 0)
5224		error (_("Internal error: failed to create format string to display program interpreter\n"));
5225
5226	      program_interpreter[0] = 0;
5227	      if (fscanf (filedata->handle, fmt, program_interpreter) <= 0)
5228		error (_("Unable to read program interpreter name\n"));
5229
5230	      if (do_segments)
5231		printf (_("      [Requesting program interpreter: %s]\n"),
5232		    program_interpreter);
5233	    }
5234	  break;
5235	}
5236    }
5237
5238  if (do_segments
5239      && filedata->section_headers != NULL
5240      && filedata->string_table != NULL)
5241    {
5242      printf (_("\n Section to Segment mapping:\n"));
5243      printf (_("  Segment Sections...\n"));
5244
5245      for (i = 0; i < filedata->file_header.e_phnum; i++)
5246	{
5247	  unsigned int j;
5248	  Elf_Internal_Shdr * section;
5249
5250	  segment = filedata->program_headers + i;
5251	  section = filedata->section_headers + 1;
5252
5253	  printf ("   %2.2d     ", i);
5254
5255	  for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
5256	    {
5257	      if (!ELF_TBSS_SPECIAL (section, segment)
5258		  && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5259		printf ("%s ", printable_section_name (filedata, section));
5260	    }
5261
5262	  putc ('\n',stdout);
5263	}
5264    }
5265
5266  return TRUE;
5267}
5268
5269
5270/* Find the file offset corresponding to VMA by using the program headers.  */
5271
5272static long
5273offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
5274{
5275  Elf_Internal_Phdr * seg;
5276
5277  if (! get_program_headers (filedata))
5278    {
5279      warn (_("Cannot interpret virtual addresses without program headers.\n"));
5280      return (long) vma;
5281    }
5282
5283  for (seg = filedata->program_headers;
5284       seg < filedata->program_headers + filedata->file_header.e_phnum;
5285       ++seg)
5286    {
5287      if (seg->p_type != PT_LOAD)
5288	continue;
5289
5290      if (vma >= (seg->p_vaddr & -seg->p_align)
5291	  && vma + size <= seg->p_vaddr + seg->p_filesz)
5292	return vma - seg->p_vaddr + seg->p_offset;
5293    }
5294
5295  warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5296	(unsigned long) vma);
5297  return (long) vma;
5298}
5299
5300
5301/* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5302   If PROBE is true, this is just a probe and we do not generate any error
5303   messages if the load fails.  */
5304
5305static bfd_boolean
5306get_32bit_section_headers (Filedata * filedata, bfd_boolean probe)
5307{
5308  Elf32_External_Shdr * shdrs;
5309  Elf_Internal_Shdr *   internal;
5310  unsigned int          i;
5311  unsigned int          size = filedata->file_header.e_shentsize;
5312  unsigned int          num = probe ? 1 : filedata->file_header.e_shnum;
5313
5314  /* PR binutils/17531: Cope with unexpected section header sizes.  */
5315  if (size == 0 || num == 0)
5316    return FALSE;
5317  if (size < sizeof * shdrs)
5318    {
5319      if (! probe)
5320	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5321      return FALSE;
5322    }
5323  if (!probe && size > sizeof * shdrs)
5324    warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5325
5326  shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
5327                                            size, num,
5328					    probe ? NULL : _("section headers"));
5329  if (shdrs == NULL)
5330    return FALSE;
5331
5332  free (filedata->section_headers);
5333  filedata->section_headers = (Elf_Internal_Shdr *)
5334    cmalloc (num, sizeof (Elf_Internal_Shdr));
5335  if (filedata->section_headers == NULL)
5336    {
5337      if (!probe)
5338	error (_("Out of memory reading %u section headers\n"), num);
5339      return FALSE;
5340    }
5341
5342  for (i = 0, internal = filedata->section_headers;
5343       i < num;
5344       i++, internal++)
5345    {
5346      internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
5347      internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
5348      internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
5349      internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
5350      internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
5351      internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
5352      internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
5353      internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
5354      internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5355      internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
5356      if (!probe && internal->sh_link > num)
5357	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5358      if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5359	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5360    }
5361
5362  free (shdrs);
5363  return TRUE;
5364}
5365
5366/* Like get_32bit_section_headers, except that it fetches 64-bit headers.  */
5367
5368static bfd_boolean
5369get_64bit_section_headers (Filedata * filedata, bfd_boolean probe)
5370{
5371  Elf64_External_Shdr *  shdrs;
5372  Elf_Internal_Shdr *    internal;
5373  unsigned int           i;
5374  unsigned int           size = filedata->file_header.e_shentsize;
5375  unsigned int           num = probe ? 1 : filedata->file_header.e_shnum;
5376
5377  /* PR binutils/17531: Cope with unexpected section header sizes.  */
5378  if (size == 0 || num == 0)
5379    return FALSE;
5380
5381  if (size < sizeof * shdrs)
5382    {
5383      if (! probe)
5384	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5385      return FALSE;
5386    }
5387
5388  if (! probe && size > sizeof * shdrs)
5389    warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5390
5391  shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5392					    filedata->file_header.e_shoff,
5393                                            size, num,
5394					    probe ? NULL : _("section headers"));
5395  if (shdrs == NULL)
5396    return FALSE;
5397
5398  free (filedata->section_headers);
5399  filedata->section_headers = (Elf_Internal_Shdr *)
5400    cmalloc (num, sizeof (Elf_Internal_Shdr));
5401  if (filedata->section_headers == NULL)
5402    {
5403      if (! probe)
5404	error (_("Out of memory reading %u section headers\n"), num);
5405      return FALSE;
5406    }
5407
5408  for (i = 0, internal = filedata->section_headers;
5409       i < num;
5410       i++, internal++)
5411    {
5412      internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
5413      internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
5414      internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
5415      internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
5416      internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
5417      internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
5418      internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
5419      internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
5420      internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
5421      internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5422      if (!probe && internal->sh_link > num)
5423	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5424      if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5425	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5426    }
5427
5428  free (shdrs);
5429  return TRUE;
5430}
5431
5432static Elf_Internal_Sym *
5433get_32bit_elf_symbols (Filedata *           filedata,
5434		       Elf_Internal_Shdr *  section,
5435		       unsigned long *      num_syms_return)
5436{
5437  unsigned long number = 0;
5438  Elf32_External_Sym * esyms = NULL;
5439  Elf_External_Sym_Shndx * shndx = NULL;
5440  Elf_Internal_Sym * isyms = NULL;
5441  Elf_Internal_Sym * psym;
5442  unsigned int j;
5443
5444  if (section->sh_size == 0)
5445    {
5446      if (num_syms_return != NULL)
5447	* num_syms_return = 0;
5448      return NULL;
5449    }
5450
5451  /* Run some sanity checks first.  */
5452  if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5453    {
5454      error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5455	     printable_section_name (filedata, section),
5456	     (unsigned long) section->sh_entsize);
5457      goto exit_point;
5458    }
5459
5460  if (section->sh_size > filedata->file_size)
5461    {
5462      error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5463	     printable_section_name (filedata, section),
5464	     (unsigned long) section->sh_size);
5465      goto exit_point;
5466    }
5467
5468  number = section->sh_size / section->sh_entsize;
5469
5470  if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5471    {
5472      error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5473	     (unsigned long) section->sh_size,
5474	     printable_section_name (filedata, section),
5475	     (unsigned long) section->sh_entsize);
5476      goto exit_point;
5477    }
5478
5479  esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5480                                           section->sh_size, _("symbols"));
5481  if (esyms == NULL)
5482    goto exit_point;
5483
5484  {
5485    elf_section_list * entry;
5486
5487    shndx = NULL;
5488    for (entry = symtab_shndx_list; entry != NULL; entry = entry->next)
5489      if (entry->hdr->sh_link == (unsigned long) (section - filedata->section_headers))
5490	{
5491	  shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5492						       entry->hdr->sh_offset,
5493						       1, entry->hdr->sh_size,
5494						       _("symbol table section indicies"));
5495	  if (shndx == NULL)
5496	    goto exit_point;
5497	  /* PR17531: file: heap-buffer-overflow */
5498	  else if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5499	    {
5500	      error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5501		     printable_section_name (filedata, entry->hdr),
5502		     (unsigned long) entry->hdr->sh_size,
5503		     (unsigned long) section->sh_size);
5504	      goto exit_point;
5505	    }
5506	}
5507  }
5508
5509  isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5510
5511  if (isyms == NULL)
5512    {
5513      error (_("Out of memory reading %lu symbols\n"),
5514	     (unsigned long) number);
5515      goto exit_point;
5516    }
5517
5518  for (j = 0, psym = isyms; j < number; j++, psym++)
5519    {
5520      psym->st_name  = BYTE_GET (esyms[j].st_name);
5521      psym->st_value = BYTE_GET (esyms[j].st_value);
5522      psym->st_size  = BYTE_GET (esyms[j].st_size);
5523      psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5524      if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5525	psym->st_shndx
5526	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5527      else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5528	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5529      psym->st_info  = BYTE_GET (esyms[j].st_info);
5530      psym->st_other = BYTE_GET (esyms[j].st_other);
5531    }
5532
5533 exit_point:
5534  if (shndx != NULL)
5535    free (shndx);
5536  if (esyms != NULL)
5537    free (esyms);
5538
5539  if (num_syms_return != NULL)
5540    * num_syms_return = isyms == NULL ? 0 : number;
5541
5542  return isyms;
5543}
5544
5545static Elf_Internal_Sym *
5546get_64bit_elf_symbols (Filedata *           filedata,
5547		       Elf_Internal_Shdr *  section,
5548		       unsigned long *      num_syms_return)
5549{
5550  unsigned long number = 0;
5551  Elf64_External_Sym * esyms = NULL;
5552  Elf_External_Sym_Shndx * shndx = NULL;
5553  Elf_Internal_Sym * isyms = NULL;
5554  Elf_Internal_Sym * psym;
5555  unsigned int j;
5556
5557  if (section->sh_size == 0)
5558    {
5559      if (num_syms_return != NULL)
5560	* num_syms_return = 0;
5561      return NULL;
5562    }
5563
5564  /* Run some sanity checks first.  */
5565  if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5566    {
5567      error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5568	     printable_section_name (filedata, section),
5569	     (unsigned long) section->sh_entsize);
5570      goto exit_point;
5571    }
5572
5573  if (section->sh_size > filedata->file_size)
5574    {
5575      error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5576	     printable_section_name (filedata, section),
5577	     (unsigned long) section->sh_size);
5578      goto exit_point;
5579    }
5580
5581  number = section->sh_size / section->sh_entsize;
5582
5583  if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
5584    {
5585      error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5586	     (unsigned long) section->sh_size,
5587	     printable_section_name (filedata, section),
5588	     (unsigned long) section->sh_entsize);
5589      goto exit_point;
5590    }
5591
5592  esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5593                                           section->sh_size, _("symbols"));
5594  if (!esyms)
5595    goto exit_point;
5596
5597  {
5598    elf_section_list * entry;
5599
5600    shndx = NULL;
5601    for (entry = symtab_shndx_list; entry != NULL; entry = entry->next)
5602      if (entry->hdr->sh_link == (unsigned long) (section - filedata->section_headers))
5603	{
5604	  shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5605						       entry->hdr->sh_offset,
5606						       1, entry->hdr->sh_size,
5607						       _("symbol table section indicies"));
5608	  if (shndx == NULL)
5609	    goto exit_point;
5610	  /* PR17531: file: heap-buffer-overflow */
5611	  else if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5612	    {
5613	      error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5614		     printable_section_name (filedata, entry->hdr),
5615		     (unsigned long) entry->hdr->sh_size,
5616		     (unsigned long) section->sh_size);
5617	      goto exit_point;
5618	    }
5619	}
5620  }
5621
5622  isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5623
5624  if (isyms == NULL)
5625    {
5626      error (_("Out of memory reading %lu symbols\n"),
5627	     (unsigned long) number);
5628      goto exit_point;
5629    }
5630
5631  for (j = 0, psym = isyms; j < number; j++, psym++)
5632    {
5633      psym->st_name  = BYTE_GET (esyms[j].st_name);
5634      psym->st_info  = BYTE_GET (esyms[j].st_info);
5635      psym->st_other = BYTE_GET (esyms[j].st_other);
5636      psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5637
5638      if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5639	psym->st_shndx
5640	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5641      else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5642	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5643
5644      psym->st_value = BYTE_GET (esyms[j].st_value);
5645      psym->st_size  = BYTE_GET (esyms[j].st_size);
5646    }
5647
5648 exit_point:
5649  if (shndx != NULL)
5650    free (shndx);
5651  if (esyms != NULL)
5652    free (esyms);
5653
5654  if (num_syms_return != NULL)
5655    * num_syms_return = isyms == NULL ? 0 : number;
5656
5657  return isyms;
5658}
5659
5660static const char *
5661get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
5662{
5663  static char buff[1024];
5664  char * p = buff;
5665  unsigned int field_size = is_32bit_elf ? 8 : 16;
5666  signed int sindex;
5667  unsigned int size = sizeof (buff) - (field_size + 4 + 1);
5668  bfd_vma os_flags = 0;
5669  bfd_vma proc_flags = 0;
5670  bfd_vma unknown_flags = 0;
5671  static const struct
5672    {
5673      const char * str;
5674      unsigned int len;
5675    }
5676  flags [] =
5677    {
5678      /*  0 */ { STRING_COMMA_LEN ("WRITE") },
5679      /*  1 */ { STRING_COMMA_LEN ("ALLOC") },
5680      /*  2 */ { STRING_COMMA_LEN ("EXEC") },
5681      /*  3 */ { STRING_COMMA_LEN ("MERGE") },
5682      /*  4 */ { STRING_COMMA_LEN ("STRINGS") },
5683      /*  5 */ { STRING_COMMA_LEN ("INFO LINK") },
5684      /*  6 */ { STRING_COMMA_LEN ("LINK ORDER") },
5685      /*  7 */ { STRING_COMMA_LEN ("OS NONCONF") },
5686      /*  8 */ { STRING_COMMA_LEN ("GROUP") },
5687      /*  9 */ { STRING_COMMA_LEN ("TLS") },
5688      /* IA-64 specific.  */
5689      /* 10 */ { STRING_COMMA_LEN ("SHORT") },
5690      /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
5691      /* IA-64 OpenVMS specific.  */
5692      /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
5693      /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
5694      /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
5695      /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
5696      /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
5697      /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
5698      /* Generic.  */
5699      /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
5700      /* SPARC specific.  */
5701      /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
5702      /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
5703      /* ARM specific.  */
5704      /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
5705      /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
5706      /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
5707      /* GNU specific.  */
5708      /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
5709      /* VLE specific.  */
5710      /* 25 */ { STRING_COMMA_LEN ("VLE") },
5711    };
5712
5713  if (do_section_details)
5714    {
5715      sprintf (buff, "[%*.*lx]: ",
5716	       field_size, field_size, (unsigned long) sh_flags);
5717      p += field_size + 4;
5718    }
5719
5720  while (sh_flags)
5721    {
5722      bfd_vma flag;
5723
5724      flag = sh_flags & - sh_flags;
5725      sh_flags &= ~ flag;
5726
5727      if (do_section_details)
5728	{
5729	  switch (flag)
5730	    {
5731	    case SHF_WRITE:		sindex = 0; break;
5732	    case SHF_ALLOC:		sindex = 1; break;
5733	    case SHF_EXECINSTR:		sindex = 2; break;
5734	    case SHF_MERGE:		sindex = 3; break;
5735	    case SHF_STRINGS:		sindex = 4; break;
5736	    case SHF_INFO_LINK:		sindex = 5; break;
5737	    case SHF_LINK_ORDER:	sindex = 6; break;
5738	    case SHF_OS_NONCONFORMING:	sindex = 7; break;
5739	    case SHF_GROUP:		sindex = 8; break;
5740	    case SHF_TLS:		sindex = 9; break;
5741	    case SHF_EXCLUDE:		sindex = 18; break;
5742	    case SHF_COMPRESSED:	sindex = 20; break;
5743	    case SHF_GNU_MBIND:		sindex = 24; break;
5744
5745	    default:
5746	      sindex = -1;
5747	      switch (filedata->file_header.e_machine)
5748		{
5749		case EM_IA_64:
5750		  if (flag == SHF_IA_64_SHORT)
5751		    sindex = 10;
5752		  else if (flag == SHF_IA_64_NORECOV)
5753		    sindex = 11;
5754#ifdef BFD64
5755		  else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
5756		    switch (flag)
5757		      {
5758		      case SHF_IA_64_VMS_GLOBAL:      sindex = 12; break;
5759		      case SHF_IA_64_VMS_OVERLAID:    sindex = 13; break;
5760		      case SHF_IA_64_VMS_SHARED:      sindex = 14; break;
5761		      case SHF_IA_64_VMS_VECTOR:      sindex = 15; break;
5762		      case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
5763		      case SHF_IA_64_VMS_PROTECTED:   sindex = 17; break;
5764		      default:                        break;
5765		      }
5766#endif
5767		  break;
5768
5769		case EM_386:
5770		case EM_IAMCU:
5771		case EM_X86_64:
5772		case EM_L1OM:
5773		case EM_K1OM:
5774		case EM_OLD_SPARCV9:
5775		case EM_SPARC32PLUS:
5776		case EM_SPARCV9:
5777		case EM_SPARC:
5778		  if (flag == SHF_ORDERED)
5779		    sindex = 19;
5780		  break;
5781
5782		case EM_ARM:
5783		  switch (flag)
5784		    {
5785		    case SHF_ENTRYSECT: sindex = 21; break;
5786		    case SHF_ARM_PURECODE: sindex = 22; break;
5787		    case SHF_COMDEF: sindex = 23; break;
5788		    default: break;
5789		    }
5790		  break;
5791		case EM_PPC:
5792		  if (flag == SHF_PPC_VLE)
5793		    sindex = 25;
5794		  break;
5795
5796		default:
5797		  break;
5798		}
5799	    }
5800
5801	  if (sindex != -1)
5802	    {
5803	      if (p != buff + field_size + 4)
5804		{
5805		  if (size < (10 + 2))
5806		    {
5807		      warn (_("Internal error: not enough buffer room for section flag info"));
5808		      return _("<unknown>");
5809		    }
5810		  size -= 2;
5811		  *p++ = ',';
5812		  *p++ = ' ';
5813		}
5814
5815	      size -= flags [sindex].len;
5816	      p = stpcpy (p, flags [sindex].str);
5817	    }
5818	  else if (flag & SHF_MASKOS)
5819	    os_flags |= flag;
5820	  else if (flag & SHF_MASKPROC)
5821	    proc_flags |= flag;
5822	  else
5823	    unknown_flags |= flag;
5824	}
5825      else
5826	{
5827	  switch (flag)
5828	    {
5829	    case SHF_WRITE:		*p = 'W'; break;
5830	    case SHF_ALLOC:		*p = 'A'; break;
5831	    case SHF_EXECINSTR:		*p = 'X'; break;
5832	    case SHF_MERGE:		*p = 'M'; break;
5833	    case SHF_STRINGS:		*p = 'S'; break;
5834	    case SHF_INFO_LINK:		*p = 'I'; break;
5835	    case SHF_LINK_ORDER:	*p = 'L'; break;
5836	    case SHF_OS_NONCONFORMING:	*p = 'O'; break;
5837	    case SHF_GROUP:		*p = 'G'; break;
5838	    case SHF_TLS:		*p = 'T'; break;
5839	    case SHF_EXCLUDE:		*p = 'E'; break;
5840	    case SHF_COMPRESSED:	*p = 'C'; break;
5841	    case SHF_GNU_MBIND:		*p = 'D'; break;
5842
5843	    default:
5844	      if ((filedata->file_header.e_machine == EM_X86_64
5845		   || filedata->file_header.e_machine == EM_L1OM
5846		   || filedata->file_header.e_machine == EM_K1OM)
5847		  && flag == SHF_X86_64_LARGE)
5848		*p = 'l';
5849	      else if (filedata->file_header.e_machine == EM_ARM
5850		       && flag == SHF_ARM_PURECODE)
5851		  *p = 'y';
5852	      else if (filedata->file_header.e_machine == EM_PPC
5853		       && flag == SHF_PPC_VLE)
5854		  *p = 'v';
5855	      else if (flag & SHF_MASKOS)
5856		{
5857		  *p = 'o';
5858		  sh_flags &= ~ SHF_MASKOS;
5859		}
5860	      else if (flag & SHF_MASKPROC)
5861		{
5862		  *p = 'p';
5863		  sh_flags &= ~ SHF_MASKPROC;
5864		}
5865	      else
5866		*p = 'x';
5867	      break;
5868	    }
5869	  p++;
5870	}
5871    }
5872
5873  if (do_section_details)
5874    {
5875      if (os_flags)
5876	{
5877	  size -= 5 + field_size;
5878	  if (p != buff + field_size + 4)
5879	    {
5880	      if (size < (2 + 1))
5881		{
5882		  warn (_("Internal error: not enough buffer room for section flag info"));
5883		  return _("<unknown>");
5884		}
5885	      size -= 2;
5886	      *p++ = ',';
5887	      *p++ = ' ';
5888	    }
5889	  sprintf (p, "OS (%*.*lx)", field_size, field_size,
5890		   (unsigned long) os_flags);
5891	  p += 5 + field_size;
5892	}
5893      if (proc_flags)
5894	{
5895	  size -= 7 + field_size;
5896	  if (p != buff + field_size + 4)
5897	    {
5898	      if (size < (2 + 1))
5899		{
5900		  warn (_("Internal error: not enough buffer room for section flag info"));
5901		  return _("<unknown>");
5902		}
5903	      size -= 2;
5904	      *p++ = ',';
5905	      *p++ = ' ';
5906	    }
5907	  sprintf (p, "PROC (%*.*lx)", field_size, field_size,
5908		   (unsigned long) proc_flags);
5909	  p += 7 + field_size;
5910	}
5911      if (unknown_flags)
5912	{
5913	  size -= 10 + field_size;
5914	  if (p != buff + field_size + 4)
5915	    {
5916	      if (size < (2 + 1))
5917		{
5918		  warn (_("Internal error: not enough buffer room for section flag info"));
5919		  return _("<unknown>");
5920		}
5921	      size -= 2;
5922	      *p++ = ',';
5923	      *p++ = ' ';
5924	    }
5925	  sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
5926		   (unsigned long) unknown_flags);
5927	  p += 10 + field_size;
5928	}
5929    }
5930
5931  *p = '\0';
5932  return buff;
5933}
5934
5935static unsigned int
5936get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
5937{
5938  if (is_32bit_elf)
5939    {
5940      Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
5941
5942      if (size < sizeof (* echdr))
5943	{
5944	  error (_("Compressed section is too small even for a compression header\n"));
5945	  return 0;
5946	}
5947
5948      chdr->ch_type = BYTE_GET (echdr->ch_type);
5949      chdr->ch_size = BYTE_GET (echdr->ch_size);
5950      chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
5951      return sizeof (*echdr);
5952    }
5953  else
5954    {
5955      Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
5956
5957      if (size < sizeof (* echdr))
5958	{
5959	  error (_("Compressed section is too small even for a compression header\n"));
5960	  return 0;
5961	}
5962
5963      chdr->ch_type = BYTE_GET (echdr->ch_type);
5964      chdr->ch_size = BYTE_GET (echdr->ch_size);
5965      chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
5966      return sizeof (*echdr);
5967    }
5968}
5969
5970static bfd_boolean
5971process_section_headers (Filedata * filedata)
5972{
5973  Elf_Internal_Shdr * section;
5974  unsigned int i;
5975
5976  filedata->section_headers = NULL;
5977
5978  if (filedata->file_header.e_shnum == 0)
5979    {
5980      /* PR binutils/12467.  */
5981      if (filedata->file_header.e_shoff != 0)
5982	{
5983	  warn (_("possibly corrupt ELF file header - it has a non-zero"
5984		  " section header offset, but no section headers\n"));
5985	  return FALSE;
5986	}
5987      else if (do_sections)
5988	printf (_("\nThere are no sections in this file.\n"));
5989
5990      return TRUE;
5991    }
5992
5993  if (do_sections && !do_header)
5994    printf (ngettext ("There is %d section header, "
5995		      "starting at offset 0x%lx:\n",
5996		      "There are %d section headers, "
5997		      "starting at offset 0x%lx:\n",
5998		      filedata->file_header.e_shnum),
5999	    filedata->file_header.e_shnum,
6000	    (unsigned long) filedata->file_header.e_shoff);
6001
6002  if (is_32bit_elf)
6003    {
6004      if (! get_32bit_section_headers (filedata, FALSE))
6005	return FALSE;
6006    }
6007  else
6008    {
6009      if (! get_64bit_section_headers (filedata, FALSE))
6010	return FALSE;
6011    }
6012
6013  /* Read in the string table, so that we have names to display.  */
6014  if (filedata->file_header.e_shstrndx != SHN_UNDEF
6015       && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6016    {
6017      section = filedata->section_headers + filedata->file_header.e_shstrndx;
6018
6019      if (section->sh_size != 0)
6020	{
6021	  filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6022						      1, section->sh_size,
6023						      _("string table"));
6024
6025	  filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6026	}
6027    }
6028
6029  /* Scan the sections for the dynamic symbol table
6030     and dynamic string table and debug sections.  */
6031  dynamic_symbols = NULL;
6032  dynamic_strings = NULL;
6033  dynamic_syminfo = NULL;
6034  symtab_shndx_list = NULL;
6035
6036  eh_addr_size = is_32bit_elf ? 4 : 8;
6037  switch (filedata->file_header.e_machine)
6038    {
6039    case EM_MIPS:
6040    case EM_MIPS_RS3_LE:
6041      /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6042	 FDE addresses.  However, the ABI also has a semi-official ILP32
6043	 variant for which the normal FDE address size rules apply.
6044
6045	 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6046	 section, where XX is the size of longs in bits.  Unfortunately,
6047	 earlier compilers provided no way of distinguishing ILP32 objects
6048	 from LP64 objects, so if there's any doubt, we should assume that
6049	 the official LP64 form is being used.  */
6050      if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6051	  && find_section (filedata, ".gcc_compiled_long32") == NULL)
6052	eh_addr_size = 8;
6053      break;
6054
6055    case EM_H8_300:
6056    case EM_H8_300H:
6057      switch (filedata->file_header.e_flags & EF_H8_MACH)
6058	{
6059	case E_H8_MACH_H8300:
6060	case E_H8_MACH_H8300HN:
6061	case E_H8_MACH_H8300SN:
6062	case E_H8_MACH_H8300SXN:
6063	  eh_addr_size = 2;
6064	  break;
6065	case E_H8_MACH_H8300H:
6066	case E_H8_MACH_H8300S:
6067	case E_H8_MACH_H8300SX:
6068	  eh_addr_size = 4;
6069	  break;
6070	}
6071      break;
6072
6073    case EM_M32C_OLD:
6074    case EM_M32C:
6075      switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6076	{
6077	case EF_M32C_CPU_M16C:
6078	  eh_addr_size = 2;
6079	  break;
6080	}
6081      break;
6082    }
6083
6084#define CHECK_ENTSIZE_VALUES(section, i, size32, size64)		\
6085  do									\
6086    {									\
6087      bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64;	\
6088      if (section->sh_entsize != expected_entsize)			\
6089	{								\
6090	  char buf[40];							\
6091	  sprintf_vma (buf, section->sh_entsize);			\
6092	  /* Note: coded this way so that there is a single string for  \
6093	     translation.  */ \
6094	  error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6095	  error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6096		   (unsigned) expected_entsize);			\
6097	  section->sh_entsize = expected_entsize;			\
6098	}								\
6099    }									\
6100  while (0)
6101
6102#define CHECK_ENTSIZE(section, i, type)					\
6103  CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type),	    \
6104			sizeof (Elf64_External_##type))
6105
6106  for (i = 0, section = filedata->section_headers;
6107       i < filedata->file_header.e_shnum;
6108       i++, section++)
6109    {
6110      char * name = SECTION_NAME (section);
6111
6112      if (section->sh_type == SHT_DYNSYM)
6113	{
6114	  if (dynamic_symbols != NULL)
6115	    {
6116	      error (_("File contains multiple dynamic symbol tables\n"));
6117	      continue;
6118	    }
6119
6120	  CHECK_ENTSIZE (section, i, Sym);
6121	  dynamic_symbols = GET_ELF_SYMBOLS (filedata, section, & num_dynamic_syms);
6122	}
6123      else if (section->sh_type == SHT_STRTAB
6124	       && streq (name, ".dynstr"))
6125	{
6126	  if (dynamic_strings != NULL)
6127	    {
6128	      error (_("File contains multiple dynamic string tables\n"));
6129	      continue;
6130	    }
6131
6132	  dynamic_strings = (char *) get_data (NULL, filedata, section->sh_offset,
6133                                               1, section->sh_size,
6134                                               _("dynamic strings"));
6135	  dynamic_strings_length = dynamic_strings == NULL ? 0 : section->sh_size;
6136	}
6137      else if (section->sh_type == SHT_SYMTAB_SHNDX)
6138	{
6139	  elf_section_list * entry = xmalloc (sizeof * entry);
6140
6141	  entry->hdr = section;
6142	  entry->next = symtab_shndx_list;
6143	  symtab_shndx_list = entry;
6144	}
6145      else if (section->sh_type == SHT_SYMTAB)
6146	CHECK_ENTSIZE (section, i, Sym);
6147      else if (section->sh_type == SHT_GROUP)
6148	CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6149      else if (section->sh_type == SHT_REL)
6150	CHECK_ENTSIZE (section, i, Rel);
6151      else if (section->sh_type == SHT_RELA)
6152	CHECK_ENTSIZE (section, i, Rela);
6153      else if ((do_debugging || do_debug_info || do_debug_abbrevs
6154		|| do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6155		|| do_debug_aranges || do_debug_frames || do_debug_macinfo
6156		|| do_debug_str || do_debug_loc || do_debug_ranges
6157		|| do_debug_addr || do_debug_cu_index || do_debug_links)
6158	       && (const_strneq (name, ".debug_")
6159                   || const_strneq (name, ".zdebug_")))
6160	{
6161          if (name[1] == 'z')
6162            name += sizeof (".zdebug_") - 1;
6163          else
6164            name += sizeof (".debug_") - 1;
6165
6166	  if (do_debugging
6167	      || (do_debug_info     && const_strneq (name, "info"))
6168	      || (do_debug_info     && const_strneq (name, "types"))
6169	      || (do_debug_abbrevs  && const_strneq (name, "abbrev"))
6170	      || (do_debug_lines    && strcmp (name, "line") == 0)
6171	      || (do_debug_lines    && const_strneq (name, "line."))
6172	      || (do_debug_pubnames && const_strneq (name, "pubnames"))
6173	      || (do_debug_pubtypes && const_strneq (name, "pubtypes"))
6174	      || (do_debug_pubnames && const_strneq (name, "gnu_pubnames"))
6175	      || (do_debug_pubtypes && const_strneq (name, "gnu_pubtypes"))
6176	      || (do_debug_aranges  && const_strneq (name, "aranges"))
6177	      || (do_debug_ranges   && const_strneq (name, "ranges"))
6178	      || (do_debug_ranges   && const_strneq (name, "rnglists"))
6179	      || (do_debug_frames   && const_strneq (name, "frame"))
6180	      || (do_debug_macinfo  && const_strneq (name, "macinfo"))
6181	      || (do_debug_macinfo  && const_strneq (name, "macro"))
6182	      || (do_debug_str      && const_strneq (name, "str"))
6183	      || (do_debug_loc      && const_strneq (name, "loc"))
6184	      || (do_debug_loc      && const_strneq (name, "loclists"))
6185	      || (do_debug_addr     && const_strneq (name, "addr"))
6186	      || (do_debug_cu_index && const_strneq (name, "cu_index"))
6187	      || (do_debug_cu_index && const_strneq (name, "tu_index"))
6188	      )
6189	    request_dump_bynumber (filedata, i, DEBUG_DUMP);
6190	}
6191      /* Linkonce section to be combined with .debug_info at link time.  */
6192      else if ((do_debugging || do_debug_info)
6193	       && const_strneq (name, ".gnu.linkonce.wi."))
6194	request_dump_bynumber (filedata, i, DEBUG_DUMP);
6195      else if (do_debug_frames && streq (name, ".eh_frame"))
6196	request_dump_bynumber (filedata, i, DEBUG_DUMP);
6197      else if (do_gdb_index && (streq (name, ".gdb_index")
6198				|| streq (name, ".debug_names")))
6199	request_dump_bynumber (filedata, i, DEBUG_DUMP);
6200      /* Trace sections for Itanium VMS.  */
6201      else if ((do_debugging || do_trace_info || do_trace_abbrevs
6202                || do_trace_aranges)
6203	       && const_strneq (name, ".trace_"))
6204	{
6205          name += sizeof (".trace_") - 1;
6206
6207	  if (do_debugging
6208	      || (do_trace_info     && streq (name, "info"))
6209	      || (do_trace_abbrevs  && streq (name, "abbrev"))
6210	      || (do_trace_aranges  && streq (name, "aranges"))
6211	      )
6212	    request_dump_bynumber (filedata, i, DEBUG_DUMP);
6213	}
6214      else if ((do_debugging || do_debug_links)
6215	       && (const_strneq (name, ".gnu_debuglink")
6216		   || const_strneq (name, ".gnu_debugaltlink")))
6217	request_dump_bynumber (filedata, i, DEBUG_DUMP);
6218    }
6219
6220  if (! do_sections)
6221    return TRUE;
6222
6223  if (filedata->file_header.e_shnum > 1)
6224    printf (_("\nSection Headers:\n"));
6225  else
6226    printf (_("\nSection Header:\n"));
6227
6228  if (is_32bit_elf)
6229    {
6230      if (do_section_details)
6231	{
6232	  printf (_("  [Nr] Name\n"));
6233	  printf (_("       Type            Addr     Off    Size   ES   Lk Inf Al\n"));
6234	}
6235      else
6236	printf
6237	  (_("  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al\n"));
6238    }
6239  else if (do_wide)
6240    {
6241      if (do_section_details)
6242	{
6243	  printf (_("  [Nr] Name\n"));
6244	  printf (_("       Type            Address          Off    Size   ES   Lk Inf Al\n"));
6245	}
6246      else
6247	printf
6248	  (_("  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al\n"));
6249    }
6250  else
6251    {
6252      if (do_section_details)
6253	{
6254	  printf (_("  [Nr] Name\n"));
6255	  printf (_("       Type              Address          Offset            Link\n"));
6256	  printf (_("       Size              EntSize          Info              Align\n"));
6257	}
6258      else
6259	{
6260	  printf (_("  [Nr] Name              Type             Address           Offset\n"));
6261	  printf (_("       Size              EntSize          Flags  Link  Info  Align\n"));
6262	}
6263    }
6264
6265  if (do_section_details)
6266    printf (_("       Flags\n"));
6267
6268  for (i = 0, section = filedata->section_headers;
6269       i < filedata->file_header.e_shnum;
6270       i++, section++)
6271    {
6272      /* Run some sanity checks on the section header.  */
6273
6274      /* Check the sh_link field.  */
6275      switch (section->sh_type)
6276	{
6277	case SHT_SYMTAB_SHNDX:
6278	case SHT_GROUP:
6279	case SHT_HASH:
6280	case SHT_GNU_HASH:
6281	case SHT_GNU_versym:
6282	case SHT_REL:
6283	case SHT_RELA:
6284	  if (section->sh_link < 1
6285	      || section->sh_link >= filedata->file_header.e_shnum
6286	      || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6287		  && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
6288	    warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6289		  i, section->sh_link);
6290	  break;
6291
6292	case SHT_DYNAMIC:
6293	case SHT_SYMTAB:
6294	case SHT_DYNSYM:
6295	case SHT_GNU_verneed:
6296	case SHT_GNU_verdef:
6297	case SHT_GNU_LIBLIST:
6298	  if (section->sh_link < 1
6299	      || section->sh_link >= filedata->file_header.e_shnum
6300	      || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
6301	    warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6302		  i, section->sh_link);
6303	  break;
6304
6305	case SHT_INIT_ARRAY:
6306	case SHT_FINI_ARRAY:
6307	case SHT_PREINIT_ARRAY:
6308	  if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6309	    warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6310		  i, section->sh_link);
6311	  break;
6312
6313	default:
6314	  /* FIXME: Add support for target specific section types.  */
6315#if 0 	  /* Currently we do not check other section types as there are too
6316	     many special cases.  Stab sections for example have a type
6317	     of SHT_PROGBITS but an sh_link field that links to the .stabstr
6318	     section.  */
6319	  if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6320	    warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6321		  i, section->sh_link);
6322#endif
6323	  break;
6324	}
6325
6326      /* Check the sh_info field.  */
6327      switch (section->sh_type)
6328	{
6329	case SHT_REL:
6330	case SHT_RELA:
6331	  if (section->sh_info < 1
6332	      || section->sh_info >= filedata->file_header.e_shnum
6333	      || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
6334		  && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
6335		  && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
6336		  && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
6337		  /* FIXME: Are other section types valid ?  */
6338		  && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
6339	    {
6340	      if (section->sh_info == 0
6341		  && (filedata->file_header.e_type == ET_EXEC
6342		      || filedata->file_header.e_type == ET_DYN
6343		      /* These next two tests may be redundant, but
6344			 they have been left in for paranoia's sake.  */
6345		      || streq (SECTION_NAME (section), ".rel.dyn")
6346		      || streq (SECTION_NAME (section), ".rela.dyn")))
6347		/* Dynamic relocations apply to segments, not sections, so
6348		   they do not need an sh_info value.  */
6349		;
6350	      else
6351		warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
6352		      i, section->sh_info);
6353	    }
6354	  break;
6355
6356	case SHT_DYNAMIC:
6357	case SHT_HASH:
6358	case SHT_SYMTAB_SHNDX:
6359	case SHT_INIT_ARRAY:
6360	case SHT_FINI_ARRAY:
6361	case SHT_PREINIT_ARRAY:
6362	  if (section->sh_info != 0)
6363	    warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6364		  i, section->sh_info);
6365	  break;
6366
6367	case SHT_GROUP:
6368	case SHT_SYMTAB:
6369	case SHT_DYNSYM:
6370	  /* A symbol index - we assume that it is valid.  */
6371	  break;
6372
6373	default:
6374	  /* FIXME: Add support for target specific section types.  */
6375	  if (section->sh_type == SHT_NOBITS)
6376	    /* NOBITS section headers with non-zero sh_info fields can be
6377	       created when a binary is stripped of everything but its debug
6378	       information.  The stripped sections have their headers
6379	       preserved but their types set to SHT_NOBITS.  So do not check
6380	       this type of section.  */
6381	    ;
6382	  else if (section->sh_flags & SHF_INFO_LINK)
6383	    {
6384	      if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
6385		warn (_("[%2u]: Expected link to another section in info field"), i);
6386	    }
6387	  else if (section->sh_type < SHT_LOOS
6388		   && (section->sh_flags & SHF_GNU_MBIND) == 0
6389		   && section->sh_info != 0)
6390	    warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6391		  i, section->sh_info);
6392	  break;
6393	}
6394
6395      /* Check the sh_size field.  */
6396      if (section->sh_size > filedata->file_size
6397	  && section->sh_type != SHT_NOBITS
6398	  && section->sh_type != SHT_NULL
6399	  && section->sh_type < SHT_LOOS)
6400	warn (_("Size of section %u is larger than the entire file!\n"), i);
6401
6402      printf ("  [%2u] ", i);
6403      if (do_section_details)
6404	printf ("%s\n      ", printable_section_name (filedata, section));
6405      else
6406	print_symbol (-17, SECTION_NAME (section));
6407
6408      printf (do_wide ? " %-15s " : " %-15.15s ",
6409	      get_section_type_name (filedata, section->sh_type));
6410
6411      if (is_32bit_elf)
6412	{
6413	  const char * link_too_big = NULL;
6414
6415	  print_vma (section->sh_addr, LONG_HEX);
6416
6417	  printf ( " %6.6lx %6.6lx %2.2lx",
6418		   (unsigned long) section->sh_offset,
6419		   (unsigned long) section->sh_size,
6420		   (unsigned long) section->sh_entsize);
6421
6422	  if (do_section_details)
6423	    fputs ("  ", stdout);
6424	  else
6425	    printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6426
6427	  if (section->sh_link >= filedata->file_header.e_shnum)
6428	    {
6429	      link_too_big = "";
6430	      /* The sh_link value is out of range.  Normally this indicates
6431		 an error but it can have special values in Solaris binaries.  */
6432	      switch (filedata->file_header.e_machine)
6433		{
6434		case EM_386:
6435		case EM_IAMCU:
6436		case EM_X86_64:
6437		case EM_L1OM:
6438		case EM_K1OM:
6439		case EM_OLD_SPARCV9:
6440		case EM_SPARC32PLUS:
6441		case EM_SPARCV9:
6442		case EM_SPARC:
6443		  if (section->sh_link == (SHN_BEFORE & 0xffff))
6444		    link_too_big = "BEFORE";
6445		  else if (section->sh_link == (SHN_AFTER & 0xffff))
6446		    link_too_big = "AFTER";
6447		  break;
6448		default:
6449		  break;
6450		}
6451	    }
6452
6453	  if (do_section_details)
6454	    {
6455	      if (link_too_big != NULL && * link_too_big)
6456		printf ("<%s> ", link_too_big);
6457	      else
6458		printf ("%2u ", section->sh_link);
6459	      printf ("%3u %2lu\n", section->sh_info,
6460		      (unsigned long) section->sh_addralign);
6461	    }
6462	  else
6463	    printf ("%2u %3u %2lu\n",
6464		    section->sh_link,
6465		    section->sh_info,
6466		    (unsigned long) section->sh_addralign);
6467
6468	  if (link_too_big && ! * link_too_big)
6469	    warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
6470		  i, section->sh_link);
6471	}
6472      else if (do_wide)
6473	{
6474	  print_vma (section->sh_addr, LONG_HEX);
6475
6476	  if ((long) section->sh_offset == section->sh_offset)
6477	    printf (" %6.6lx", (unsigned long) section->sh_offset);
6478	  else
6479	    {
6480	      putchar (' ');
6481	      print_vma (section->sh_offset, LONG_HEX);
6482	    }
6483
6484	  if ((unsigned long) section->sh_size == section->sh_size)
6485	    printf (" %6.6lx", (unsigned long) section->sh_size);
6486	  else
6487	    {
6488	      putchar (' ');
6489	      print_vma (section->sh_size, LONG_HEX);
6490	    }
6491
6492	  if ((unsigned long) section->sh_entsize == section->sh_entsize)
6493	    printf (" %2.2lx", (unsigned long) section->sh_entsize);
6494	  else
6495	    {
6496	      putchar (' ');
6497	      print_vma (section->sh_entsize, LONG_HEX);
6498	    }
6499
6500	  if (do_section_details)
6501	    fputs ("  ", stdout);
6502	  else
6503	    printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6504
6505	  printf ("%2u %3u ", section->sh_link, section->sh_info);
6506
6507	  if ((unsigned long) section->sh_addralign == section->sh_addralign)
6508	    printf ("%2lu\n", (unsigned long) section->sh_addralign);
6509	  else
6510	    {
6511	      print_vma (section->sh_addralign, DEC);
6512	      putchar ('\n');
6513	    }
6514	}
6515      else if (do_section_details)
6516	{
6517	  printf ("       %-15.15s  ",
6518		  get_section_type_name (filedata, section->sh_type));
6519	  print_vma (section->sh_addr, LONG_HEX);
6520	  if ((long) section->sh_offset == section->sh_offset)
6521	    printf ("  %16.16lx", (unsigned long) section->sh_offset);
6522	  else
6523	    {
6524	      printf ("  ");
6525	      print_vma (section->sh_offset, LONG_HEX);
6526	    }
6527	  printf ("  %u\n       ", section->sh_link);
6528	  print_vma (section->sh_size, LONG_HEX);
6529	  putchar (' ');
6530	  print_vma (section->sh_entsize, LONG_HEX);
6531
6532	  printf ("  %-16u  %lu\n",
6533		  section->sh_info,
6534		  (unsigned long) section->sh_addralign);
6535	}
6536      else
6537	{
6538	  putchar (' ');
6539	  print_vma (section->sh_addr, LONG_HEX);
6540	  if ((long) section->sh_offset == section->sh_offset)
6541	    printf ("  %8.8lx", (unsigned long) section->sh_offset);
6542	  else
6543	    {
6544	      printf ("  ");
6545	      print_vma (section->sh_offset, LONG_HEX);
6546	    }
6547	  printf ("\n       ");
6548	  print_vma (section->sh_size, LONG_HEX);
6549	  printf ("  ");
6550	  print_vma (section->sh_entsize, LONG_HEX);
6551
6552	  printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6553
6554	  printf ("     %2u   %3u     %lu\n",
6555		  section->sh_link,
6556		  section->sh_info,
6557		  (unsigned long) section->sh_addralign);
6558	}
6559
6560      if (do_section_details)
6561	{
6562	  printf ("       %s\n", get_elf_section_flags (filedata, section->sh_flags));
6563	  if ((section->sh_flags & SHF_COMPRESSED) != 0)
6564	    {
6565	      /* Minimum section size is 12 bytes for 32-bit compression
6566		 header + 12 bytes for compressed data header.  */
6567	      unsigned char buf[24];
6568
6569	      assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
6570	      if (get_data (&buf, filedata, section->sh_offset, 1,
6571			    sizeof (buf), _("compression header")))
6572		{
6573		  Elf_Internal_Chdr chdr;
6574
6575		  (void) get_compression_header (&chdr, buf, sizeof (buf));
6576
6577		  if (chdr.ch_type == ELFCOMPRESS_ZLIB)
6578		    printf ("       ZLIB, ");
6579		  else
6580		    printf (_("       [<unknown>: 0x%x], "),
6581			    chdr.ch_type);
6582		  print_vma (chdr.ch_size, LONG_HEX);
6583		  printf (", %lu\n", (unsigned long) chdr.ch_addralign);
6584		}
6585	    }
6586	}
6587    }
6588
6589  if (!do_section_details)
6590    {
6591      /* The ordering of the letters shown here matches the ordering of the
6592	 corresponding SHF_xxx values, and hence the order in which these
6593	 letters will be displayed to the user.  */
6594      printf (_("Key to Flags:\n\
6595  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
6596  L (link order), O (extra OS processing required), G (group), T (TLS),\n\
6597  C (compressed), x (unknown), o (OS specific), E (exclude),\n  "));
6598      if (filedata->file_header.e_machine == EM_X86_64
6599	  || filedata->file_header.e_machine == EM_L1OM
6600	  || filedata->file_header.e_machine == EM_K1OM)
6601	printf (_("l (large), "));
6602      else if (filedata->file_header.e_machine == EM_ARM)
6603	printf (_("y (purecode), "));
6604      else if (filedata->file_header.e_machine == EM_PPC)
6605	printf (_("v (VLE), "));
6606      printf ("p (processor specific)\n");
6607    }
6608
6609  return TRUE;
6610}
6611
6612static const char *
6613get_group_flags (unsigned int flags)
6614{
6615  static char buff[128];
6616
6617  if (flags == 0)
6618    return "";
6619  else if (flags == GRP_COMDAT)
6620    return "COMDAT ";
6621
6622  snprintf (buff, 14, _("[0x%x: "), flags);
6623
6624  flags &= ~ GRP_COMDAT;
6625  if (flags & GRP_MASKOS)
6626    {
6627      strcat (buff, "<OS specific>");
6628      flags &= ~ GRP_MASKOS;
6629    }
6630
6631  if (flags & GRP_MASKPROC)
6632    {
6633      strcat (buff, "<PROC specific>");
6634      flags &= ~ GRP_MASKPROC;
6635    }
6636
6637  if (flags)
6638    strcat (buff, "<unknown>");
6639
6640  strcat (buff, "]");
6641  return buff;
6642}
6643
6644static bfd_boolean
6645process_section_groups (Filedata * filedata)
6646{
6647  Elf_Internal_Shdr * section;
6648  unsigned int i;
6649  struct group * group;
6650  Elf_Internal_Shdr * symtab_sec;
6651  Elf_Internal_Shdr * strtab_sec;
6652  Elf_Internal_Sym * symtab;
6653  unsigned long num_syms;
6654  char * strtab;
6655  size_t strtab_size;
6656
6657  /* Don't process section groups unless needed.  */
6658  if (!do_unwind && !do_section_groups)
6659    return TRUE;
6660
6661  if (filedata->file_header.e_shnum == 0)
6662    {
6663      if (do_section_groups)
6664	printf (_("\nThere are no sections to group in this file.\n"));
6665
6666      return TRUE;
6667    }
6668
6669  if (filedata->section_headers == NULL)
6670    {
6671      error (_("Section headers are not available!\n"));
6672      /* PR 13622: This can happen with a corrupt ELF header.  */
6673      return FALSE;
6674    }
6675
6676  section_headers_groups = (struct group **) calloc (filedata->file_header.e_shnum,
6677                                                     sizeof (struct group *));
6678
6679  if (section_headers_groups == NULL)
6680    {
6681      error (_("Out of memory reading %u section group headers\n"),
6682	     filedata->file_header.e_shnum);
6683      return FALSE;
6684    }
6685
6686  /* Scan the sections for the group section.  */
6687  group_count = 0;
6688  for (i = 0, section = filedata->section_headers;
6689       i < filedata->file_header.e_shnum;
6690       i++, section++)
6691    if (section->sh_type == SHT_GROUP)
6692      group_count++;
6693
6694  if (group_count == 0)
6695    {
6696      if (do_section_groups)
6697	printf (_("\nThere are no section groups in this file.\n"));
6698
6699      return TRUE;
6700    }
6701
6702  section_groups = (struct group *) calloc (group_count, sizeof (struct group));
6703
6704  if (section_groups == NULL)
6705    {
6706      error (_("Out of memory reading %lu groups\n"),
6707	     (unsigned long) group_count);
6708      return FALSE;
6709    }
6710
6711  symtab_sec = NULL;
6712  strtab_sec = NULL;
6713  symtab = NULL;
6714  num_syms = 0;
6715  strtab = NULL;
6716  strtab_size = 0;
6717  for (i = 0, section = filedata->section_headers, group = section_groups;
6718       i < filedata->file_header.e_shnum;
6719       i++, section++)
6720    {
6721      if (section->sh_type == SHT_GROUP)
6722	{
6723	  const char * name = printable_section_name (filedata, section);
6724	  const char * group_name;
6725	  unsigned char * start;
6726	  unsigned char * indices;
6727	  unsigned int entry, j, size;
6728	  Elf_Internal_Shdr * sec;
6729	  Elf_Internal_Sym * sym;
6730
6731	  /* Get the symbol table.  */
6732	  if (section->sh_link >= filedata->file_header.e_shnum
6733	      || ((sec = filedata->section_headers + section->sh_link)->sh_type
6734		  != SHT_SYMTAB))
6735	    {
6736	      error (_("Bad sh_link in group section `%s'\n"), name);
6737	      continue;
6738	    }
6739
6740	  if (symtab_sec != sec)
6741	    {
6742	      symtab_sec = sec;
6743	      if (symtab)
6744		free (symtab);
6745	      symtab = GET_ELF_SYMBOLS (filedata, symtab_sec, & num_syms);
6746	    }
6747
6748	  if (symtab == NULL)
6749	    {
6750	      error (_("Corrupt header in group section `%s'\n"), name);
6751	      continue;
6752	    }
6753
6754	  if (section->sh_info >= num_syms)
6755	    {
6756	      error (_("Bad sh_info in group section `%s'\n"), name);
6757	      continue;
6758	    }
6759
6760	  sym = symtab + section->sh_info;
6761
6762	  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
6763	    {
6764	      if (sym->st_shndx == 0
6765		  || sym->st_shndx >= filedata->file_header.e_shnum)
6766		{
6767		  error (_("Bad sh_info in group section `%s'\n"), name);
6768		  continue;
6769		}
6770
6771	      group_name = SECTION_NAME (filedata->section_headers + sym->st_shndx);
6772	      strtab_sec = NULL;
6773	      if (strtab)
6774		free (strtab);
6775	      strtab = NULL;
6776	      strtab_size = 0;
6777	    }
6778	  else
6779	    {
6780	      /* Get the string table.  */
6781	      if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
6782		{
6783		  strtab_sec = NULL;
6784		  if (strtab)
6785		    free (strtab);
6786		  strtab = NULL;
6787		  strtab_size = 0;
6788		}
6789	      else if (strtab_sec
6790		       != (sec = filedata->section_headers + symtab_sec->sh_link))
6791		{
6792		  strtab_sec = sec;
6793		  if (strtab)
6794		    free (strtab);
6795
6796		  strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
6797					      1, strtab_sec->sh_size,
6798					      _("string table"));
6799		  strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
6800		}
6801	      group_name = sym->st_name < strtab_size
6802		? strtab + sym->st_name : _("<corrupt>");
6803	    }
6804
6805	  /* PR 17531: file: loop.  */
6806	  if (section->sh_entsize > section->sh_size)
6807	    {
6808	      error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
6809		     printable_section_name (filedata, section),
6810		     (unsigned long) section->sh_entsize,
6811		     (unsigned long) section->sh_size);
6812	      break;
6813	    }
6814
6815	  start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
6816                                              1, section->sh_size,
6817                                              _("section data"));
6818	  if (start == NULL)
6819	    continue;
6820
6821	  indices = start;
6822	  size = (section->sh_size / section->sh_entsize) - 1;
6823	  entry = byte_get (indices, 4);
6824	  indices += 4;
6825
6826	  if (do_section_groups)
6827	    {
6828	      printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
6829		      get_group_flags (entry), i, name, group_name, size);
6830
6831	      printf (_("   [Index]    Name\n"));
6832	    }
6833
6834	  group->group_index = i;
6835
6836	  for (j = 0; j < size; j++)
6837	    {
6838	      struct group_list * g;
6839
6840	      entry = byte_get (indices, 4);
6841	      indices += 4;
6842
6843	      if (entry >= filedata->file_header.e_shnum)
6844		{
6845		  static unsigned num_group_errors = 0;
6846
6847		  if (num_group_errors ++ < 10)
6848		    {
6849		      error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
6850			     entry, i, filedata->file_header.e_shnum - 1);
6851		      if (num_group_errors == 10)
6852			warn (_("Further error messages about overlarge group section indicies suppressed\n"));
6853		    }
6854		  continue;
6855		}
6856
6857	      if (section_headers_groups [entry] != NULL)
6858		{
6859		  if (entry)
6860		    {
6861		      static unsigned num_errs = 0;
6862
6863		      if (num_errs ++ < 10)
6864			{
6865			  error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
6866				 entry, i,
6867				 section_headers_groups [entry]->group_index);
6868			  if (num_errs == 10)
6869			    warn (_("Further error messages about already contained group sections suppressed\n"));
6870			}
6871		      continue;
6872		    }
6873		  else
6874		    {
6875		      /* Intel C/C++ compiler may put section 0 in a
6876			 section group.  We just warn it the first time
6877			 and ignore it afterwards.  */
6878		      static bfd_boolean warned = FALSE;
6879		      if (!warned)
6880			{
6881			  error (_("section 0 in group section [%5u]\n"),
6882				 section_headers_groups [entry]->group_index);
6883			  warned = TRUE;
6884			}
6885		    }
6886		}
6887
6888	      section_headers_groups [entry] = group;
6889
6890	      if (do_section_groups)
6891		{
6892		  sec = filedata->section_headers + entry;
6893		  printf ("   [%5u]   %s\n", entry, printable_section_name (filedata, sec));
6894		}
6895
6896	      g = (struct group_list *) xmalloc (sizeof (struct group_list));
6897	      g->section_index = entry;
6898	      g->next = group->root;
6899	      group->root = g;
6900	    }
6901
6902	  if (start)
6903	    free (start);
6904
6905	  group++;
6906	}
6907    }
6908
6909  if (symtab)
6910    free (symtab);
6911  if (strtab)
6912    free (strtab);
6913  return TRUE;
6914}
6915
6916/* Data used to display dynamic fixups.  */
6917
6918struct ia64_vms_dynfixup
6919{
6920  bfd_vma needed_ident;		/* Library ident number.  */
6921  bfd_vma needed;		/* Index in the dstrtab of the library name.  */
6922  bfd_vma fixup_needed;		/* Index of the library.  */
6923  bfd_vma fixup_rela_cnt;	/* Number of fixups.  */
6924  bfd_vma fixup_rela_off;	/* Fixups offset in the dynamic segment.  */
6925};
6926
6927/* Data used to display dynamic relocations.  */
6928
6929struct ia64_vms_dynimgrela
6930{
6931  bfd_vma img_rela_cnt;		/* Number of relocations.  */
6932  bfd_vma img_rela_off;		/* Reloc offset in the dynamic segment.  */
6933};
6934
6935/* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
6936   library).  */
6937
6938static bfd_boolean
6939dump_ia64_vms_dynamic_fixups (Filedata *                  filedata,
6940			      struct ia64_vms_dynfixup *  fixup,
6941                              const char *                strtab,
6942			      unsigned int                strtab_sz)
6943{
6944  Elf64_External_VMS_IMAGE_FIXUP * imfs;
6945  long i;
6946  const char * lib_name;
6947
6948  imfs = get_data (NULL, filedata, dynamic_addr + fixup->fixup_rela_off,
6949		   1, fixup->fixup_rela_cnt * sizeof (*imfs),
6950		   _("dynamic section image fixups"));
6951  if (!imfs)
6952    return FALSE;
6953
6954  if (fixup->needed < strtab_sz)
6955    lib_name = strtab + fixup->needed;
6956  else
6957    {
6958      warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
6959            (unsigned long) fixup->needed);
6960      lib_name = "???";
6961    }
6962  printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
6963	  (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
6964  printf
6965    (_("Seg Offset           Type                             SymVec DataType\n"));
6966
6967  for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
6968    {
6969      unsigned int type;
6970      const char *rtype;
6971
6972      printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
6973      printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
6974      type = BYTE_GET (imfs [i].type);
6975      rtype = elf_ia64_reloc_type (type);
6976      if (rtype == NULL)
6977        printf (" 0x%08x                       ", type);
6978      else
6979        printf (" %-32s ", rtype);
6980      printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
6981      printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
6982    }
6983
6984  free (imfs);
6985  return TRUE;
6986}
6987
6988/* Display IA-64 OpenVMS dynamic relocations (used to relocate an image).  */
6989
6990static bfd_boolean
6991dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
6992{
6993  Elf64_External_VMS_IMAGE_RELA *imrs;
6994  long i;
6995
6996  imrs = get_data (NULL, filedata, dynamic_addr + imgrela->img_rela_off,
6997		   1, imgrela->img_rela_cnt * sizeof (*imrs),
6998		   _("dynamic section image relocations"));
6999  if (!imrs)
7000    return FALSE;
7001
7002  printf (_("\nImage relocs\n"));
7003  printf
7004    (_("Seg Offset   Type                            Addend            Seg Sym Off\n"));
7005
7006  for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7007    {
7008      unsigned int type;
7009      const char *rtype;
7010
7011      printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7012      printf ("%08" BFD_VMA_FMT "x ",
7013              (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7014      type = BYTE_GET (imrs [i].type);
7015      rtype = elf_ia64_reloc_type (type);
7016      if (rtype == NULL)
7017        printf ("0x%08x                      ", type);
7018      else
7019        printf ("%-31s ", rtype);
7020      print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7021      printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7022      printf ("%08" BFD_VMA_FMT "x\n",
7023              (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7024    }
7025
7026  free (imrs);
7027  return TRUE;
7028}
7029
7030/* Display IA-64 OpenVMS dynamic relocations and fixups.  */
7031
7032static bfd_boolean
7033process_ia64_vms_dynamic_relocs (Filedata * filedata)
7034{
7035  struct ia64_vms_dynfixup fixup;
7036  struct ia64_vms_dynimgrela imgrela;
7037  Elf_Internal_Dyn *entry;
7038  bfd_vma strtab_off = 0;
7039  bfd_vma strtab_sz = 0;
7040  char *strtab = NULL;
7041  bfd_boolean res = TRUE;
7042
7043  memset (&fixup, 0, sizeof (fixup));
7044  memset (&imgrela, 0, sizeof (imgrela));
7045
7046  /* Note: the order of the entries is specified by the OpenVMS specs.  */
7047  for (entry = dynamic_section;
7048       entry < dynamic_section + dynamic_nent;
7049       entry++)
7050    {
7051      switch (entry->d_tag)
7052        {
7053        case DT_IA_64_VMS_STRTAB_OFFSET:
7054          strtab_off = entry->d_un.d_val;
7055          break;
7056        case DT_STRSZ:
7057          strtab_sz = entry->d_un.d_val;
7058          if (strtab == NULL)
7059            strtab = get_data (NULL, filedata, dynamic_addr + strtab_off,
7060                               1, strtab_sz, _("dynamic string section"));
7061          break;
7062
7063        case DT_IA_64_VMS_NEEDED_IDENT:
7064          fixup.needed_ident = entry->d_un.d_val;
7065          break;
7066        case DT_NEEDED:
7067          fixup.needed = entry->d_un.d_val;
7068          break;
7069        case DT_IA_64_VMS_FIXUP_NEEDED:
7070          fixup.fixup_needed = entry->d_un.d_val;
7071          break;
7072        case DT_IA_64_VMS_FIXUP_RELA_CNT:
7073          fixup.fixup_rela_cnt = entry->d_un.d_val;
7074          break;
7075        case DT_IA_64_VMS_FIXUP_RELA_OFF:
7076          fixup.fixup_rela_off = entry->d_un.d_val;
7077          if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
7078	    res = FALSE;
7079          break;
7080        case DT_IA_64_VMS_IMG_RELA_CNT:
7081	  imgrela.img_rela_cnt = entry->d_un.d_val;
7082          break;
7083        case DT_IA_64_VMS_IMG_RELA_OFF:
7084	  imgrela.img_rela_off = entry->d_un.d_val;
7085          if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
7086	    res = FALSE;
7087          break;
7088
7089        default:
7090          break;
7091	}
7092    }
7093
7094  if (strtab != NULL)
7095    free (strtab);
7096
7097  return res;
7098}
7099
7100static struct
7101{
7102  const char * name;
7103  int reloc;
7104  int size;
7105  int rela;
7106}
7107  dynamic_relocations [] =
7108{
7109  { "REL", DT_REL, DT_RELSZ, FALSE },
7110  { "RELA", DT_RELA, DT_RELASZ, TRUE },
7111  { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
7112};
7113
7114/* Process the reloc section.  */
7115
7116static bfd_boolean
7117process_relocs (Filedata * filedata)
7118{
7119  unsigned long rel_size;
7120  unsigned long rel_offset;
7121
7122  if (!do_reloc)
7123    return TRUE;
7124
7125  if (do_using_dynamic)
7126    {
7127      int          is_rela;
7128      const char * name;
7129      bfd_boolean  has_dynamic_reloc;
7130      unsigned int i;
7131
7132      has_dynamic_reloc = FALSE;
7133
7134      for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7135	{
7136	  is_rela = dynamic_relocations [i].rela;
7137	  name = dynamic_relocations [i].name;
7138	  rel_size = dynamic_info [dynamic_relocations [i].size];
7139	  rel_offset = dynamic_info [dynamic_relocations [i].reloc];
7140
7141	  if (rel_size)
7142	    has_dynamic_reloc = TRUE;
7143
7144	  if (is_rela == UNKNOWN)
7145	    {
7146	      if (dynamic_relocations [i].reloc == DT_JMPREL)
7147		switch (dynamic_info[DT_PLTREL])
7148		  {
7149		  case DT_REL:
7150		    is_rela = FALSE;
7151		    break;
7152		  case DT_RELA:
7153		    is_rela = TRUE;
7154		    break;
7155		  }
7156	    }
7157
7158	  if (rel_size)
7159	    {
7160	      printf
7161		(_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7162		 name, rel_offset, rel_size);
7163
7164	      dump_relocations (filedata,
7165				offset_from_vma (filedata, rel_offset, rel_size),
7166				rel_size,
7167				dynamic_symbols, num_dynamic_syms,
7168				dynamic_strings, dynamic_strings_length,
7169				is_rela, TRUE /* is_dynamic */);
7170	    }
7171	}
7172
7173      if (is_ia64_vms (filedata))
7174        if (process_ia64_vms_dynamic_relocs (filedata))
7175	  has_dynamic_reloc = TRUE;
7176
7177      if (! has_dynamic_reloc)
7178	printf (_("\nThere are no dynamic relocations in this file.\n"));
7179    }
7180  else
7181    {
7182      Elf_Internal_Shdr * section;
7183      unsigned long i;
7184      bfd_boolean found = FALSE;
7185
7186      for (i = 0, section = filedata->section_headers;
7187	   i < filedata->file_header.e_shnum;
7188	   i++, section++)
7189	{
7190	  if (   section->sh_type != SHT_RELA
7191	      && section->sh_type != SHT_REL)
7192	    continue;
7193
7194	  rel_offset = section->sh_offset;
7195	  rel_size   = section->sh_size;
7196
7197	  if (rel_size)
7198	    {
7199	      Elf_Internal_Shdr * strsec;
7200	      int is_rela;
7201	      unsigned long num_rela;
7202
7203	      printf (_("\nRelocation section "));
7204
7205	      if (filedata->string_table == NULL)
7206		printf ("%d", section->sh_name);
7207	      else
7208		printf ("'%s'", printable_section_name (filedata, section));
7209
7210	      num_rela = rel_size / section->sh_entsize;
7211	      printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7212				" at offset 0x%lx contains %lu entries:\n",
7213				num_rela),
7214		      rel_offset, num_rela);
7215
7216	      is_rela = section->sh_type == SHT_RELA;
7217
7218	      if (section->sh_link != 0
7219		  && section->sh_link < filedata->file_header.e_shnum)
7220		{
7221		  Elf_Internal_Shdr * symsec;
7222		  Elf_Internal_Sym *  symtab;
7223		  unsigned long nsyms;
7224		  unsigned long strtablen = 0;
7225		  char * strtab = NULL;
7226
7227		  symsec = filedata->section_headers + section->sh_link;
7228		  if (symsec->sh_type != SHT_SYMTAB
7229		      && symsec->sh_type != SHT_DYNSYM)
7230                    continue;
7231
7232		  symtab = GET_ELF_SYMBOLS (filedata, symsec, & nsyms);
7233
7234		  if (symtab == NULL)
7235		    continue;
7236
7237		  if (symsec->sh_link != 0
7238		      && symsec->sh_link < filedata->file_header.e_shnum)
7239		    {
7240		      strsec = filedata->section_headers + symsec->sh_link;
7241
7242		      strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7243						  1, strsec->sh_size,
7244						  _("string table"));
7245		      strtablen = strtab == NULL ? 0 : strsec->sh_size;
7246		    }
7247
7248		  dump_relocations (filedata, rel_offset, rel_size,
7249				    symtab, nsyms, strtab, strtablen,
7250				    is_rela,
7251				    symsec->sh_type == SHT_DYNSYM);
7252		  if (strtab)
7253		    free (strtab);
7254		  free (symtab);
7255		}
7256	      else
7257		dump_relocations (filedata, rel_offset, rel_size,
7258				  NULL, 0, NULL, 0, is_rela,
7259				  FALSE /* is_dynamic */);
7260
7261	      found = TRUE;
7262	    }
7263	}
7264
7265      if (! found)
7266	{
7267	  /* Users sometimes forget the -D option, so try to be helpful.  */
7268	  for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7269	    {
7270	      if (dynamic_info [dynamic_relocations [i].size])
7271		{
7272		  printf (_("\nThere are no static relocations in this file."));
7273		  printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
7274
7275		  break;
7276		}
7277	    }
7278	  if (i == ARRAY_SIZE (dynamic_relocations))
7279	    printf (_("\nThere are no relocations in this file.\n"));
7280	}
7281    }
7282
7283  return TRUE;
7284}
7285
7286/* An absolute address consists of a section and an offset.  If the
7287   section is NULL, the offset itself is the address, otherwise, the
7288   address equals to LOAD_ADDRESS(section) + offset.  */
7289
7290struct absaddr
7291{
7292  unsigned short section;
7293  bfd_vma offset;
7294};
7295
7296#define ABSADDR(a) \
7297  ((a).section \
7298   ? filedata->section_headers [(a).section].sh_addr + (a).offset \
7299   : (a).offset)
7300
7301/* Find the nearest symbol at or below ADDR.  Returns the symbol
7302   name, if found, and the offset from the symbol to ADDR.  */
7303
7304static void
7305find_symbol_for_address (Filedata *          filedata,
7306			 Elf_Internal_Sym *  symtab,
7307			 unsigned long       nsyms,
7308			 const char *        strtab,
7309			 unsigned long       strtab_size,
7310			 struct absaddr      addr,
7311			 const char **       symname,
7312			 bfd_vma *           offset)
7313{
7314  bfd_vma dist = 0x100000;
7315  Elf_Internal_Sym * sym;
7316  Elf_Internal_Sym * beg;
7317  Elf_Internal_Sym * end;
7318  Elf_Internal_Sym * best = NULL;
7319
7320  REMOVE_ARCH_BITS (addr.offset);
7321  beg = symtab;
7322  end = symtab + nsyms;
7323
7324  while (beg < end)
7325    {
7326      bfd_vma value;
7327
7328      sym = beg + (end - beg) / 2;
7329
7330      value = sym->st_value;
7331      REMOVE_ARCH_BITS (value);
7332
7333      if (sym->st_name != 0
7334	  && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
7335	  && addr.offset >= value
7336	  && addr.offset - value < dist)
7337	{
7338	  best = sym;
7339	  dist = addr.offset - value;
7340	  if (!dist)
7341	    break;
7342	}
7343
7344      if (addr.offset < value)
7345	end = sym;
7346      else
7347	beg = sym + 1;
7348    }
7349
7350  if (best)
7351    {
7352      *symname = (best->st_name >= strtab_size
7353		  ? _("<corrupt>") : strtab + best->st_name);
7354      *offset = dist;
7355      return;
7356    }
7357
7358  *symname = NULL;
7359  *offset = addr.offset;
7360}
7361
7362static /* signed */ int
7363symcmp (const void *p, const void *q)
7364{
7365  Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
7366  Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
7367
7368  return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
7369}
7370
7371/* Process the unwind section.  */
7372
7373#include "unwind-ia64.h"
7374
7375struct ia64_unw_table_entry
7376{
7377  struct absaddr start;
7378  struct absaddr end;
7379  struct absaddr info;
7380};
7381
7382struct ia64_unw_aux_info
7383{
7384  struct ia64_unw_table_entry * table;		/* Unwind table.  */
7385  unsigned long                 table_len;	/* Length of unwind table.  */
7386  unsigned char *               info;		/* Unwind info.  */
7387  unsigned long                 info_size;	/* Size of unwind info.  */
7388  bfd_vma                       info_addr;	/* Starting address of unwind info.  */
7389  bfd_vma                       seg_base;	/* Starting address of segment.  */
7390  Elf_Internal_Sym *            symtab;		/* The symbol table.  */
7391  unsigned long                 nsyms;		/* Number of symbols.  */
7392  Elf_Internal_Sym *            funtab;		/* Sorted table of STT_FUNC symbols.  */
7393  unsigned long                 nfuns;		/* Number of entries in funtab.  */
7394  char *                        strtab;		/* The string table.  */
7395  unsigned long                 strtab_size;	/* Size of string table.  */
7396};
7397
7398static bfd_boolean
7399dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
7400{
7401  struct ia64_unw_table_entry * tp;
7402  unsigned long j, nfuns;
7403  int in_body;
7404  bfd_boolean res = TRUE;
7405
7406  aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7407  for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7408    if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7409      aux->funtab[nfuns++] = aux->symtab[j];
7410  aux->nfuns = nfuns;
7411  qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7412
7413  for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7414    {
7415      bfd_vma stamp;
7416      bfd_vma offset;
7417      const unsigned char * dp;
7418      const unsigned char * head;
7419      const unsigned char * end;
7420      const char * procname;
7421
7422      find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
7423			       aux->strtab_size, tp->start, &procname, &offset);
7424
7425      fputs ("\n<", stdout);
7426
7427      if (procname)
7428	{
7429	  fputs (procname, stdout);
7430
7431	  if (offset)
7432	    printf ("+%lx", (unsigned long) offset);
7433	}
7434
7435      fputs (">: [", stdout);
7436      print_vma (tp->start.offset, PREFIX_HEX);
7437      fputc ('-', stdout);
7438      print_vma (tp->end.offset, PREFIX_HEX);
7439      printf ("], info at +0x%lx\n",
7440	      (unsigned long) (tp->info.offset - aux->seg_base));
7441
7442      /* PR 17531: file: 86232b32.  */
7443      if (aux->info == NULL)
7444	continue;
7445
7446      /* PR 17531: file: 0997b4d1.  */
7447      if ((ABSADDR (tp->info) - aux->info_addr) >= aux->info_size)
7448	{
7449	  warn (_("Invalid offset %lx in table entry %ld\n"),
7450		(long) tp->info.offset, (long) (tp - aux->table));
7451	  res = FALSE;
7452	  continue;
7453	}
7454
7455      head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
7456      stamp = byte_get ((unsigned char *) head, sizeof (stamp));
7457
7458      printf ("  v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
7459	      (unsigned) UNW_VER (stamp),
7460	      (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
7461	      UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
7462	      UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
7463	      (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
7464
7465      if (UNW_VER (stamp) != 1)
7466	{
7467	  printf (_("\tUnknown version.\n"));
7468	  continue;
7469	}
7470
7471      in_body = 0;
7472      end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
7473      /* PR 17531: file: 16ceda89.  */
7474      if (end > aux->info + aux->info_size)
7475	end = aux->info + aux->info_size;
7476      for (dp = head + 8; dp < end;)
7477	dp = unw_decode (dp, in_body, & in_body, end);
7478    }
7479
7480  free (aux->funtab);
7481
7482  return res;
7483}
7484
7485static bfd_boolean
7486slurp_ia64_unwind_table (Filedata *                  filedata,
7487			 struct ia64_unw_aux_info *  aux,
7488			 Elf_Internal_Shdr *         sec)
7489{
7490  unsigned long size, nrelas, i;
7491  Elf_Internal_Phdr * seg;
7492  struct ia64_unw_table_entry * tep;
7493  Elf_Internal_Shdr * relsec;
7494  Elf_Internal_Rela * rela;
7495  Elf_Internal_Rela * rp;
7496  unsigned char * table;
7497  unsigned char * tp;
7498  Elf_Internal_Sym * sym;
7499  const char * relname;
7500
7501  aux->table_len = 0;
7502
7503  /* First, find the starting address of the segment that includes
7504     this section: */
7505
7506  if (filedata->file_header.e_phnum)
7507    {
7508      if (! get_program_headers (filedata))
7509	  return FALSE;
7510
7511      for (seg = filedata->program_headers;
7512	   seg < filedata->program_headers + filedata->file_header.e_phnum;
7513	   ++seg)
7514	{
7515	  if (seg->p_type != PT_LOAD)
7516	    continue;
7517
7518	  if (sec->sh_addr >= seg->p_vaddr
7519	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
7520	    {
7521	      aux->seg_base = seg->p_vaddr;
7522	      break;
7523	    }
7524	}
7525    }
7526
7527  /* Second, build the unwind table from the contents of the unwind section:  */
7528  size = sec->sh_size;
7529  table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
7530                                      _("unwind table"));
7531  if (!table)
7532    return FALSE;
7533
7534  aux->table_len = size / (3 * eh_addr_size);
7535  aux->table = (struct ia64_unw_table_entry *)
7536    xcmalloc (aux->table_len, sizeof (aux->table[0]));
7537  tep = aux->table;
7538
7539  for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
7540    {
7541      tep->start.section = SHN_UNDEF;
7542      tep->end.section   = SHN_UNDEF;
7543      tep->info.section  = SHN_UNDEF;
7544      tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7545      tep->end.offset   = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7546      tep->info.offset  = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7547      tep->start.offset += aux->seg_base;
7548      tep->end.offset   += aux->seg_base;
7549      tep->info.offset  += aux->seg_base;
7550    }
7551  free (table);
7552
7553  /* Third, apply any relocations to the unwind table:  */
7554  for (relsec = filedata->section_headers;
7555       relsec < filedata->section_headers + filedata->file_header.e_shnum;
7556       ++relsec)
7557    {
7558      if (relsec->sh_type != SHT_RELA
7559	  || relsec->sh_info >= filedata->file_header.e_shnum
7560	  || filedata->section_headers + relsec->sh_info != sec)
7561	continue;
7562
7563      if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
7564			      & rela, & nrelas))
7565	{
7566	  free (aux->table);
7567	  aux->table = NULL;
7568	  aux->table_len = 0;
7569	  return FALSE;
7570	}
7571
7572      for (rp = rela; rp < rela + nrelas; ++rp)
7573	{
7574	  relname = elf_ia64_reloc_type (get_reloc_type (filedata, rp->r_info));
7575	  sym = aux->symtab + get_reloc_symindex (rp->r_info);
7576
7577	  /* PR 17531: file: 9fa67536.  */
7578	  if (relname == NULL)
7579	    {
7580	      warn (_("Skipping unknown relocation type: %u\n"),
7581		    get_reloc_type (filedata, rp->r_info));
7582	      continue;
7583	    }
7584
7585	  if (! const_strneq (relname, "R_IA64_SEGREL"))
7586	    {
7587	      warn (_("Skipping unexpected relocation type: %s\n"), relname);
7588	      continue;
7589	    }
7590
7591	  i = rp->r_offset / (3 * eh_addr_size);
7592
7593	  /* PR 17531: file: 5bc8d9bf.  */
7594	  if (i >= aux->table_len)
7595	    {
7596	      warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
7597	      continue;
7598	    }
7599
7600	  switch (rp->r_offset / eh_addr_size % 3)
7601	    {
7602	    case 0:
7603	      aux->table[i].start.section = sym->st_shndx;
7604	      aux->table[i].start.offset  = rp->r_addend + sym->st_value;
7605	      break;
7606	    case 1:
7607	      aux->table[i].end.section   = sym->st_shndx;
7608	      aux->table[i].end.offset    = rp->r_addend + sym->st_value;
7609	      break;
7610	    case 2:
7611	      aux->table[i].info.section  = sym->st_shndx;
7612	      aux->table[i].info.offset   = rp->r_addend + sym->st_value;
7613	      break;
7614	    default:
7615	      break;
7616	    }
7617	}
7618
7619      free (rela);
7620    }
7621
7622  return TRUE;
7623}
7624
7625static bfd_boolean
7626ia64_process_unwind (Filedata * filedata)
7627{
7628  Elf_Internal_Shdr * sec;
7629  Elf_Internal_Shdr * unwsec = NULL;
7630  Elf_Internal_Shdr * strsec;
7631  unsigned long i, unwcount = 0, unwstart = 0;
7632  struct ia64_unw_aux_info aux;
7633  bfd_boolean res = TRUE;
7634
7635  memset (& aux, 0, sizeof (aux));
7636
7637  for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
7638    {
7639      if (sec->sh_type == SHT_SYMTAB
7640	  && sec->sh_link < filedata->file_header.e_shnum)
7641	{
7642	  aux.symtab = GET_ELF_SYMBOLS (filedata, sec, & aux.nsyms);
7643
7644	  strsec = filedata->section_headers + sec->sh_link;
7645	  if (aux.strtab != NULL)
7646	    {
7647	      error (_("Multiple auxillary string tables encountered\n"));
7648	      free (aux.strtab);
7649	      res = FALSE;
7650	    }
7651	  aux.strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7652                                          1, strsec->sh_size,
7653                                          _("string table"));
7654	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
7655	}
7656      else if (sec->sh_type == SHT_IA_64_UNWIND)
7657	unwcount++;
7658    }
7659
7660  if (!unwcount)
7661    printf (_("\nThere are no unwind sections in this file.\n"));
7662
7663  while (unwcount-- > 0)
7664    {
7665      char * suffix;
7666      size_t len, len2;
7667
7668      for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
7669	   i < filedata->file_header.e_shnum; ++i, ++sec)
7670	if (sec->sh_type == SHT_IA_64_UNWIND)
7671	  {
7672	    unwsec = sec;
7673	    break;
7674	  }
7675      /* We have already counted the number of SHT_IA64_UNWIND
7676	 sections so the loop above should never fail.  */
7677      assert (unwsec != NULL);
7678
7679      unwstart = i + 1;
7680      len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
7681
7682      if ((unwsec->sh_flags & SHF_GROUP) != 0)
7683	{
7684	  /* We need to find which section group it is in.  */
7685	  struct group_list * g;
7686
7687	  if (section_headers_groups == NULL
7688	      || section_headers_groups [i] == NULL)
7689	    i = filedata->file_header.e_shnum;
7690	  else
7691	    {
7692	      g = section_headers_groups [i]->root;
7693
7694	      for (; g != NULL; g = g->next)
7695		{
7696		  sec = filedata->section_headers + g->section_index;
7697
7698		  if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
7699		    break;
7700		}
7701
7702	      if (g == NULL)
7703		i = filedata->file_header.e_shnum;
7704	    }
7705	}
7706      else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
7707	{
7708	  /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO.  */
7709	  len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
7710	  suffix = SECTION_NAME (unwsec) + len;
7711	  for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum;
7712	       ++i, ++sec)
7713	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
7714		&& streq (SECTION_NAME (sec) + len2, suffix))
7715	      break;
7716	}
7717      else
7718	{
7719	  /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
7720	     .IA_64.unwind or BAR -> .IA_64.unwind_info.  */
7721	  len = sizeof (ELF_STRING_ia64_unwind) - 1;
7722	  len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
7723	  suffix = "";
7724	  if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
7725	    suffix = SECTION_NAME (unwsec) + len;
7726	  for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum;
7727	       ++i, ++sec)
7728	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
7729		&& streq (SECTION_NAME (sec) + len2, suffix))
7730	      break;
7731	}
7732
7733      if (i == filedata->file_header.e_shnum)
7734	{
7735	  printf (_("\nCould not find unwind info section for "));
7736
7737	  if (filedata->string_table == NULL)
7738	    printf ("%d", unwsec->sh_name);
7739	  else
7740	    printf ("'%s'", printable_section_name (filedata, unwsec));
7741	}
7742      else
7743	{
7744	  aux.info_addr = sec->sh_addr;
7745	  aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
7746						 sec->sh_size,
7747						 _("unwind info"));
7748	  aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
7749
7750	  printf (_("\nUnwind section "));
7751
7752	  if (filedata->string_table == NULL)
7753	    printf ("%d", unwsec->sh_name);
7754	  else
7755	    printf ("'%s'", printable_section_name (filedata, unwsec));
7756
7757	  printf (_(" at offset 0x%lx contains %lu entries:\n"),
7758		  (unsigned long) unwsec->sh_offset,
7759		  (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
7760
7761	  if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
7762	      && aux.table_len > 0)
7763	    dump_ia64_unwind (filedata, & aux);
7764
7765	  if (aux.table)
7766	    free ((char *) aux.table);
7767	  if (aux.info)
7768	    free ((char *) aux.info);
7769	  aux.table = NULL;
7770	  aux.info = NULL;
7771	}
7772    }
7773
7774  if (aux.symtab)
7775    free (aux.symtab);
7776  if (aux.strtab)
7777    free ((char *) aux.strtab);
7778
7779  return res;
7780}
7781
7782struct hppa_unw_table_entry
7783{
7784  struct absaddr start;
7785  struct absaddr end;
7786  unsigned int Cannot_unwind:1;			/* 0 */
7787  unsigned int Millicode:1;			/* 1 */
7788  unsigned int Millicode_save_sr0:1;		/* 2 */
7789  unsigned int Region_description:2;		/* 3..4 */
7790  unsigned int reserved1:1;			/* 5 */
7791  unsigned int Entry_SR:1;			/* 6 */
7792  unsigned int Entry_FR:4;     /* Number saved     7..10 */
7793  unsigned int Entry_GR:5;     /* Number saved     11..15 */
7794  unsigned int Args_stored:1;			/* 16 */
7795  unsigned int Variable_Frame:1;		/* 17 */
7796  unsigned int Separate_Package_Body:1;		/* 18 */
7797  unsigned int Frame_Extension_Millicode:1;	/* 19 */
7798  unsigned int Stack_Overflow_Check:1;		/* 20 */
7799  unsigned int Two_Instruction_SP_Increment:1;	/* 21 */
7800  unsigned int Ada_Region:1;			/* 22 */
7801  unsigned int cxx_info:1;			/* 23 */
7802  unsigned int cxx_try_catch:1;			/* 24 */
7803  unsigned int sched_entry_seq:1;		/* 25 */
7804  unsigned int reserved2:1;			/* 26 */
7805  unsigned int Save_SP:1;			/* 27 */
7806  unsigned int Save_RP:1;			/* 28 */
7807  unsigned int Save_MRP_in_frame:1;		/* 29 */
7808  unsigned int extn_ptr_defined:1;		/* 30 */
7809  unsigned int Cleanup_defined:1;		/* 31 */
7810
7811  unsigned int MPE_XL_interrupt_marker:1;	/* 0 */
7812  unsigned int HP_UX_interrupt_marker:1;	/* 1 */
7813  unsigned int Large_frame:1;			/* 2 */
7814  unsigned int Pseudo_SP_Set:1;			/* 3 */
7815  unsigned int reserved4:1;			/* 4 */
7816  unsigned int Total_frame_size:27;		/* 5..31 */
7817};
7818
7819struct hppa_unw_aux_info
7820{
7821  struct hppa_unw_table_entry *  table;		/* Unwind table.  */
7822  unsigned long                  table_len;	/* Length of unwind table.  */
7823  bfd_vma                        seg_base;	/* Starting address of segment.  */
7824  Elf_Internal_Sym *             symtab;	/* The symbol table.  */
7825  unsigned long                  nsyms;		/* Number of symbols.  */
7826  Elf_Internal_Sym *             funtab;	/* Sorted table of STT_FUNC symbols.  */
7827  unsigned long                  nfuns;		/* Number of entries in funtab.  */
7828  char *                         strtab;	/* The string table.  */
7829  unsigned long                  strtab_size;	/* Size of string table.  */
7830};
7831
7832static bfd_boolean
7833dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
7834{
7835  struct hppa_unw_table_entry * tp;
7836  unsigned long j, nfuns;
7837  bfd_boolean res = TRUE;
7838
7839  aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7840  for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7841    if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7842      aux->funtab[nfuns++] = aux->symtab[j];
7843  aux->nfuns = nfuns;
7844  qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7845
7846  for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7847    {
7848      bfd_vma offset;
7849      const char * procname;
7850
7851      find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
7852			       aux->strtab_size, tp->start, &procname,
7853			       &offset);
7854
7855      fputs ("\n<", stdout);
7856
7857      if (procname)
7858	{
7859	  fputs (procname, stdout);
7860
7861	  if (offset)
7862	    printf ("+%lx", (unsigned long) offset);
7863	}
7864
7865      fputs (">: [", stdout);
7866      print_vma (tp->start.offset, PREFIX_HEX);
7867      fputc ('-', stdout);
7868      print_vma (tp->end.offset, PREFIX_HEX);
7869      printf ("]\n\t");
7870
7871#define PF(_m) if (tp->_m) printf (#_m " ");
7872#define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
7873      PF(Cannot_unwind);
7874      PF(Millicode);
7875      PF(Millicode_save_sr0);
7876      /* PV(Region_description);  */
7877      PF(Entry_SR);
7878      PV(Entry_FR);
7879      PV(Entry_GR);
7880      PF(Args_stored);
7881      PF(Variable_Frame);
7882      PF(Separate_Package_Body);
7883      PF(Frame_Extension_Millicode);
7884      PF(Stack_Overflow_Check);
7885      PF(Two_Instruction_SP_Increment);
7886      PF(Ada_Region);
7887      PF(cxx_info);
7888      PF(cxx_try_catch);
7889      PF(sched_entry_seq);
7890      PF(Save_SP);
7891      PF(Save_RP);
7892      PF(Save_MRP_in_frame);
7893      PF(extn_ptr_defined);
7894      PF(Cleanup_defined);
7895      PF(MPE_XL_interrupt_marker);
7896      PF(HP_UX_interrupt_marker);
7897      PF(Large_frame);
7898      PF(Pseudo_SP_Set);
7899      PV(Total_frame_size);
7900#undef PF
7901#undef PV
7902    }
7903
7904  printf ("\n");
7905
7906  free (aux->funtab);
7907
7908  return res;
7909}
7910
7911static bfd_boolean
7912slurp_hppa_unwind_table (Filedata *                  filedata,
7913			 struct hppa_unw_aux_info *  aux,
7914			 Elf_Internal_Shdr *         sec)
7915{
7916  unsigned long size, unw_ent_size, nentries, nrelas, i;
7917  Elf_Internal_Phdr * seg;
7918  struct hppa_unw_table_entry * tep;
7919  Elf_Internal_Shdr * relsec;
7920  Elf_Internal_Rela * rela;
7921  Elf_Internal_Rela * rp;
7922  unsigned char * table;
7923  unsigned char * tp;
7924  Elf_Internal_Sym * sym;
7925  const char * relname;
7926
7927  /* First, find the starting address of the segment that includes
7928     this section.  */
7929  if (filedata->file_header.e_phnum)
7930    {
7931      if (! get_program_headers (filedata))
7932	return FALSE;
7933
7934      for (seg = filedata->program_headers;
7935	   seg < filedata->program_headers + filedata->file_header.e_phnum;
7936	   ++seg)
7937	{
7938	  if (seg->p_type != PT_LOAD)
7939	    continue;
7940
7941	  if (sec->sh_addr >= seg->p_vaddr
7942	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
7943	    {
7944	      aux->seg_base = seg->p_vaddr;
7945	      break;
7946	    }
7947	}
7948    }
7949
7950  /* Second, build the unwind table from the contents of the unwind
7951     section.  */
7952  size = sec->sh_size;
7953  table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
7954                                      _("unwind table"));
7955  if (!table)
7956    return FALSE;
7957
7958  unw_ent_size = 16;
7959  nentries = size / unw_ent_size;
7960  size = unw_ent_size * nentries;
7961
7962  tep = aux->table = (struct hppa_unw_table_entry *)
7963      xcmalloc (nentries, sizeof (aux->table[0]));
7964
7965  for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
7966    {
7967      unsigned int tmp1, tmp2;
7968
7969      tep->start.section = SHN_UNDEF;
7970      tep->end.section   = SHN_UNDEF;
7971
7972      tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
7973      tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
7974      tmp1 = byte_get ((unsigned char *) tp + 8, 4);
7975      tmp2 = byte_get ((unsigned char *) tp + 12, 4);
7976
7977      tep->start.offset += aux->seg_base;
7978      tep->end.offset   += aux->seg_base;
7979
7980      tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
7981      tep->Millicode = (tmp1 >> 30) & 0x1;
7982      tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
7983      tep->Region_description = (tmp1 >> 27) & 0x3;
7984      tep->reserved1 = (tmp1 >> 26) & 0x1;
7985      tep->Entry_SR = (tmp1 >> 25) & 0x1;
7986      tep->Entry_FR = (tmp1 >> 21) & 0xf;
7987      tep->Entry_GR = (tmp1 >> 16) & 0x1f;
7988      tep->Args_stored = (tmp1 >> 15) & 0x1;
7989      tep->Variable_Frame = (tmp1 >> 14) & 0x1;
7990      tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
7991      tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
7992      tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
7993      tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
7994      tep->Ada_Region = (tmp1 >> 9) & 0x1;
7995      tep->cxx_info = (tmp1 >> 8) & 0x1;
7996      tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
7997      tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
7998      tep->reserved2 = (tmp1 >> 5) & 0x1;
7999      tep->Save_SP = (tmp1 >> 4) & 0x1;
8000      tep->Save_RP = (tmp1 >> 3) & 0x1;
8001      tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8002      tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8003      tep->Cleanup_defined = tmp1 & 0x1;
8004
8005      tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8006      tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8007      tep->Large_frame = (tmp2 >> 29) & 0x1;
8008      tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8009      tep->reserved4 = (tmp2 >> 27) & 0x1;
8010      tep->Total_frame_size = tmp2 & 0x7ffffff;
8011    }
8012  free (table);
8013
8014  /* Third, apply any relocations to the unwind table.  */
8015  for (relsec = filedata->section_headers;
8016       relsec < filedata->section_headers + filedata->file_header.e_shnum;
8017       ++relsec)
8018    {
8019      if (relsec->sh_type != SHT_RELA
8020	  || relsec->sh_info >= filedata->file_header.e_shnum
8021	  || filedata->section_headers + relsec->sh_info != sec)
8022	continue;
8023
8024      if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8025			      & rela, & nrelas))
8026	return FALSE;
8027
8028      for (rp = rela; rp < rela + nrelas; ++rp)
8029	{
8030	  relname = elf_hppa_reloc_type (get_reloc_type (filedata, rp->r_info));
8031	  sym = aux->symtab + get_reloc_symindex (rp->r_info);
8032
8033	  /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64.  */
8034	  if (! const_strneq (relname, "R_PARISC_SEGREL"))
8035	    {
8036	      warn (_("Skipping unexpected relocation type %s\n"), relname);
8037	      continue;
8038	    }
8039
8040	  i = rp->r_offset / unw_ent_size;
8041
8042	  switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
8043	    {
8044	    case 0:
8045	      aux->table[i].start.section = sym->st_shndx;
8046	      aux->table[i].start.offset  = sym->st_value + rp->r_addend;
8047	      break;
8048	    case 1:
8049	      aux->table[i].end.section   = sym->st_shndx;
8050	      aux->table[i].end.offset    = sym->st_value + rp->r_addend;
8051	      break;
8052	    default:
8053	      break;
8054	    }
8055	}
8056
8057      free (rela);
8058    }
8059
8060  aux->table_len = nentries;
8061
8062  return TRUE;
8063}
8064
8065static bfd_boolean
8066hppa_process_unwind (Filedata * filedata)
8067{
8068  struct hppa_unw_aux_info aux;
8069  Elf_Internal_Shdr * unwsec = NULL;
8070  Elf_Internal_Shdr * strsec;
8071  Elf_Internal_Shdr * sec;
8072  unsigned long i;
8073  bfd_boolean res = TRUE;
8074
8075  if (filedata->string_table == NULL)
8076    return FALSE;
8077
8078  memset (& aux, 0, sizeof (aux));
8079
8080  for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8081    {
8082      if (sec->sh_type == SHT_SYMTAB
8083	  && sec->sh_link < filedata->file_header.e_shnum)
8084	{
8085	  aux.symtab = GET_ELF_SYMBOLS (filedata, sec, & aux.nsyms);
8086
8087	  strsec = filedata->section_headers + sec->sh_link;
8088	  if (aux.strtab != NULL)
8089	    {
8090	      error (_("Multiple auxillary string tables encountered\n"));
8091	      free (aux.strtab);
8092	      res = FALSE;
8093	    }
8094	  aux.strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
8095                                          1, strsec->sh_size,
8096                                          _("string table"));
8097	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
8098	}
8099      else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
8100	unwsec = sec;
8101    }
8102
8103  if (!unwsec)
8104    printf (_("\nThere are no unwind sections in this file.\n"));
8105
8106  for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8107    {
8108      if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
8109	{
8110	  unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size + 8);
8111
8112	  printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8113			    "contains %lu entry:\n",
8114			    "\nUnwind section '%s' at offset 0x%lx "
8115			    "contains %lu entries:\n",
8116			    num_unwind),
8117		  printable_section_name (filedata, sec),
8118		  (unsigned long) sec->sh_offset,
8119		  num_unwind);
8120
8121          if (! slurp_hppa_unwind_table (filedata, &aux, sec))
8122	    res = FALSE;
8123
8124	  if (aux.table_len > 0)
8125	    {
8126	      if (! dump_hppa_unwind (filedata, &aux))
8127		res = FALSE;
8128	    }
8129
8130	  if (aux.table)
8131	    free ((char *) aux.table);
8132	  aux.table = NULL;
8133	}
8134    }
8135
8136  if (aux.symtab)
8137    free (aux.symtab);
8138  if (aux.strtab)
8139    free ((char *) aux.strtab);
8140
8141  return res;
8142}
8143
8144struct arm_section
8145{
8146  unsigned char *      data;		/* The unwind data.  */
8147  Elf_Internal_Shdr *  sec;		/* The cached unwind section header.  */
8148  Elf_Internal_Rela *  rela;		/* The cached relocations for this section.  */
8149  unsigned long        nrelas;		/* The number of relocations.  */
8150  unsigned int         rel_type;	/* REL or RELA ?  */
8151  Elf_Internal_Rela *  next_rela;	/* Cyclic pointer to the next reloc to process.  */
8152};
8153
8154struct arm_unw_aux_info
8155{
8156  Filedata *          filedata;		/* The file containing the unwind sections.  */
8157  Elf_Internal_Sym *  symtab;		/* The file's symbol table.  */
8158  unsigned long       nsyms;		/* Number of symbols.  */
8159  Elf_Internal_Sym *  funtab;		/* Sorted table of STT_FUNC symbols.  */
8160  unsigned long       nfuns;		/* Number of these symbols.  */
8161  char *              strtab;		/* The file's string table.  */
8162  unsigned long       strtab_size;	/* Size of string table.  */
8163};
8164
8165static const char *
8166arm_print_vma_and_name (Filedata *                 filedata,
8167			struct arm_unw_aux_info *  aux,
8168			bfd_vma                    fn,
8169			struct absaddr             addr)
8170{
8171  const char *procname;
8172  bfd_vma sym_offset;
8173
8174  if (addr.section == SHN_UNDEF)
8175    addr.offset = fn;
8176
8177  find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8178			   aux->strtab_size, addr, &procname,
8179			   &sym_offset);
8180
8181  print_vma (fn, PREFIX_HEX);
8182
8183  if (procname)
8184    {
8185      fputs (" <", stdout);
8186      fputs (procname, stdout);
8187
8188      if (sym_offset)
8189	printf ("+0x%lx", (unsigned long) sym_offset);
8190      fputc ('>', stdout);
8191    }
8192
8193  return procname;
8194}
8195
8196static void
8197arm_free_section (struct arm_section *arm_sec)
8198{
8199  if (arm_sec->data != NULL)
8200    free (arm_sec->data);
8201
8202  if (arm_sec->rela != NULL)
8203    free (arm_sec->rela);
8204}
8205
8206/* 1) If SEC does not match the one cached in ARM_SEC, then free the current
8207      cached section and install SEC instead.
8208   2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
8209      and return its valued in * WORDP, relocating if necessary.
8210   3) Update the NEXT_RELA field in ARM_SEC and store the section index and
8211      relocation's offset in ADDR.
8212   4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
8213      into the string table of the symbol associated with the reloc.  If no
8214      reloc was applied store -1 there.
8215   5) Return TRUE upon success, FALSE otherwise.  */
8216
8217static bfd_boolean
8218get_unwind_section_word (Filedata *                 filedata,
8219			 struct arm_unw_aux_info *  aux,
8220			 struct arm_section *       arm_sec,
8221			 Elf_Internal_Shdr *        sec,
8222			 bfd_vma 		    word_offset,
8223			 unsigned int *             wordp,
8224			 struct absaddr *           addr,
8225			 bfd_vma *		    sym_name)
8226{
8227  Elf_Internal_Rela *rp;
8228  Elf_Internal_Sym *sym;
8229  const char * relname;
8230  unsigned int word;
8231  bfd_boolean wrapped;
8232
8233  if (sec == NULL || arm_sec == NULL)
8234    return FALSE;
8235
8236  addr->section = SHN_UNDEF;
8237  addr->offset = 0;
8238
8239  if (sym_name != NULL)
8240    *sym_name = (bfd_vma) -1;
8241
8242  /* If necessary, update the section cache.  */
8243  if (sec != arm_sec->sec)
8244    {
8245      Elf_Internal_Shdr *relsec;
8246
8247      arm_free_section (arm_sec);
8248
8249      arm_sec->sec = sec;
8250      arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
8251				sec->sh_size, _("unwind data"));
8252      arm_sec->rela = NULL;
8253      arm_sec->nrelas = 0;
8254
8255      for (relsec = filedata->section_headers;
8256	   relsec < filedata->section_headers + filedata->file_header.e_shnum;
8257	   ++relsec)
8258	{
8259	  if (relsec->sh_info >= filedata->file_header.e_shnum
8260	      || filedata->section_headers + relsec->sh_info != sec
8261	      /* PR 15745: Check the section type as well.  */
8262	      || (relsec->sh_type != SHT_REL
8263		  && relsec->sh_type != SHT_RELA))
8264	    continue;
8265
8266	  arm_sec->rel_type = relsec->sh_type;
8267	  if (relsec->sh_type == SHT_REL)
8268	    {
8269	      if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
8270				     relsec->sh_size,
8271				     & arm_sec->rela, & arm_sec->nrelas))
8272		return FALSE;
8273	    }
8274	  else /* relsec->sh_type == SHT_RELA */
8275	    {
8276	      if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
8277				      relsec->sh_size,
8278				      & arm_sec->rela, & arm_sec->nrelas))
8279		return FALSE;
8280	    }
8281	  break;
8282	}
8283
8284      arm_sec->next_rela = arm_sec->rela;
8285    }
8286
8287  /* If there is no unwind data we can do nothing.  */
8288  if (arm_sec->data == NULL)
8289    return FALSE;
8290
8291  /* If the offset is invalid then fail.  */
8292  if (/* PR 21343 *//* PR 18879 */
8293      sec->sh_size < 4
8294      || word_offset > (sec->sh_size - 4)
8295      || ((bfd_signed_vma) word_offset) < 0)
8296    return FALSE;
8297
8298  /* Get the word at the required offset.  */
8299  word = byte_get (arm_sec->data + word_offset, 4);
8300
8301  /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128.  */
8302  if (arm_sec->rela == NULL)
8303    {
8304      * wordp = word;
8305      return TRUE;
8306    }
8307
8308  /* Look through the relocs to find the one that applies to the provided offset.  */
8309  wrapped = FALSE;
8310  for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
8311    {
8312      bfd_vma prelval, offset;
8313
8314      if (rp->r_offset > word_offset && !wrapped)
8315	{
8316	  rp = arm_sec->rela;
8317	  wrapped = TRUE;
8318	}
8319      if (rp->r_offset > word_offset)
8320	break;
8321
8322      if (rp->r_offset & 3)
8323	{
8324	  warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
8325		(unsigned long) rp->r_offset);
8326	  continue;
8327	}
8328
8329      if (rp->r_offset < word_offset)
8330	continue;
8331
8332      /* PR 17531: file: 027-161405-0.004  */
8333      if (aux->symtab == NULL)
8334	continue;
8335
8336      if (arm_sec->rel_type == SHT_REL)
8337	{
8338	  offset = word & 0x7fffffff;
8339	  if (offset & 0x40000000)
8340	    offset |= ~ (bfd_vma) 0x7fffffff;
8341	}
8342      else if (arm_sec->rel_type == SHT_RELA)
8343	offset = rp->r_addend;
8344      else
8345	{
8346	  error (_("Unknown section relocation type %d encountered\n"),
8347		 arm_sec->rel_type);
8348	  break;
8349	}
8350
8351      /* PR 17531 file: 027-1241568-0.004.  */
8352      if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
8353	{
8354	  error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
8355		 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
8356	  break;
8357	}
8358
8359      sym = aux->symtab + ELF32_R_SYM (rp->r_info);
8360      offset += sym->st_value;
8361      prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
8362
8363      /* Check that we are processing the expected reloc type.  */
8364      if (filedata->file_header.e_machine == EM_ARM)
8365	{
8366	  relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
8367	  if (relname == NULL)
8368	    {
8369	      warn (_("Skipping unknown ARM relocation type: %d\n"),
8370		    (int) ELF32_R_TYPE (rp->r_info));
8371	      continue;
8372	    }
8373
8374	  if (streq (relname, "R_ARM_NONE"))
8375	      continue;
8376
8377	  if (! streq (relname, "R_ARM_PREL31"))
8378	    {
8379	      warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
8380	      continue;
8381	    }
8382	}
8383      else if (filedata->file_header.e_machine == EM_TI_C6000)
8384	{
8385	  relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
8386	  if (relname == NULL)
8387	    {
8388	      warn (_("Skipping unknown C6000 relocation type: %d\n"),
8389		    (int) ELF32_R_TYPE (rp->r_info));
8390	      continue;
8391	    }
8392
8393	  if (streq (relname, "R_C6000_NONE"))
8394	    continue;
8395
8396	  if (! streq (relname, "R_C6000_PREL31"))
8397	    {
8398	      warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
8399	      continue;
8400	    }
8401
8402	  prelval >>= 1;
8403	}
8404      else
8405	{
8406	  /* This function currently only supports ARM and TI unwinders.  */
8407	  warn (_("Only TI and ARM unwinders are currently supported\n"));
8408	  break;
8409	}
8410
8411      word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
8412      addr->section = sym->st_shndx;
8413      addr->offset = offset;
8414
8415      if (sym_name)
8416	* sym_name = sym->st_name;
8417      break;
8418    }
8419
8420  *wordp = word;
8421  arm_sec->next_rela = rp;
8422
8423  return TRUE;
8424}
8425
8426static const char *tic6x_unwind_regnames[16] =
8427{
8428  "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
8429  "A14", "A13", "A12", "A11", "A10",
8430  "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
8431};
8432
8433static void
8434decode_tic6x_unwind_regmask (unsigned int mask)
8435{
8436  int i;
8437
8438  for (i = 12; mask; mask >>= 1, i--)
8439    {
8440      if (mask & 1)
8441	{
8442	  fputs (tic6x_unwind_regnames[i], stdout);
8443	  if (mask > 1)
8444	    fputs (", ", stdout);
8445	}
8446    }
8447}
8448
8449#define ADVANCE							\
8450  if (remaining == 0 && more_words)				\
8451    {								\
8452      data_offset += 4;						\
8453      if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec,	\
8454				     data_offset, & word, & addr, NULL))	\
8455	return FALSE;						\
8456      remaining = 4;						\
8457      more_words--;						\
8458    }								\
8459
8460#define GET_OP(OP)			\
8461  ADVANCE;				\
8462  if (remaining)			\
8463    {					\
8464      remaining--;			\
8465      (OP) = word >> 24;		\
8466      word <<= 8;			\
8467    }					\
8468  else					\
8469    {					\
8470      printf (_("[Truncated opcode]\n"));	\
8471      return FALSE;			\
8472    }					\
8473  printf ("0x%02x ", OP)
8474
8475static bfd_boolean
8476decode_arm_unwind_bytecode (Filedata *                 filedata,
8477			    struct arm_unw_aux_info *  aux,
8478			    unsigned int               word,
8479			    unsigned int               remaining,
8480			    unsigned int               more_words,
8481			    bfd_vma                    data_offset,
8482			    Elf_Internal_Shdr *        data_sec,
8483			    struct arm_section *       data_arm_sec)
8484{
8485  struct absaddr addr;
8486  bfd_boolean res = TRUE;
8487
8488  /* Decode the unwinding instructions.  */
8489  while (1)
8490    {
8491      unsigned int op, op2;
8492
8493      ADVANCE;
8494      if (remaining == 0)
8495	break;
8496      remaining--;
8497      op = word >> 24;
8498      word <<= 8;
8499
8500      printf ("  0x%02x ", op);
8501
8502      if ((op & 0xc0) == 0x00)
8503	{
8504	  int offset = ((op & 0x3f) << 2) + 4;
8505
8506	  printf ("     vsp = vsp + %d", offset);
8507	}
8508      else if ((op & 0xc0) == 0x40)
8509	{
8510	  int offset = ((op & 0x3f) << 2) + 4;
8511
8512	  printf ("     vsp = vsp - %d", offset);
8513	}
8514      else if ((op & 0xf0) == 0x80)
8515	{
8516	  GET_OP (op2);
8517	  if (op == 0x80 && op2 == 0)
8518	    printf (_("Refuse to unwind"));
8519	  else
8520	    {
8521	      unsigned int mask = ((op & 0x0f) << 8) | op2;
8522	      bfd_boolean first = TRUE;
8523	      int i;
8524
8525	      printf ("pop {");
8526	      for (i = 0; i < 12; i++)
8527		if (mask & (1 << i))
8528		  {
8529		    if (first)
8530		      first = FALSE;
8531		    else
8532		      printf (", ");
8533		    printf ("r%d", 4 + i);
8534		  }
8535	      printf ("}");
8536	    }
8537	}
8538      else if ((op & 0xf0) == 0x90)
8539	{
8540	  if (op == 0x9d || op == 0x9f)
8541	    printf (_("     [Reserved]"));
8542	  else
8543	    printf ("     vsp = r%d", op & 0x0f);
8544	}
8545      else if ((op & 0xf0) == 0xa0)
8546	{
8547	  int end = 4 + (op & 0x07);
8548	  bfd_boolean first = TRUE;
8549	  int i;
8550
8551	  printf ("     pop {");
8552	  for (i = 4; i <= end; i++)
8553	    {
8554	      if (first)
8555		first = FALSE;
8556	      else
8557		printf (", ");
8558	      printf ("r%d", i);
8559	    }
8560	  if (op & 0x08)
8561	    {
8562	      if (!first)
8563		printf (", ");
8564	      printf ("r14");
8565	    }
8566	  printf ("}");
8567	}
8568      else if (op == 0xb0)
8569	printf (_("     finish"));
8570      else if (op == 0xb1)
8571	{
8572	  GET_OP (op2);
8573	  if (op2 == 0 || (op2 & 0xf0) != 0)
8574	    printf (_("[Spare]"));
8575	  else
8576	    {
8577	      unsigned int mask = op2 & 0x0f;
8578	      bfd_boolean first = TRUE;
8579	      int i;
8580
8581	      printf ("pop {");
8582	      for (i = 0; i < 12; i++)
8583		if (mask & (1 << i))
8584		  {
8585		    if (first)
8586		      first = FALSE;
8587		    else
8588		      printf (", ");
8589		    printf ("r%d", i);
8590		  }
8591	      printf ("}");
8592	    }
8593	}
8594      else if (op == 0xb2)
8595	{
8596	  unsigned char buf[9];
8597	  unsigned int i, len;
8598	  unsigned long offset;
8599
8600	  for (i = 0; i < sizeof (buf); i++)
8601	    {
8602	      GET_OP (buf[i]);
8603	      if ((buf[i] & 0x80) == 0)
8604		break;
8605	    }
8606	  if (i == sizeof (buf))
8607	    {
8608	      error (_("corrupt change to vsp"));
8609	      res = FALSE;
8610	    }
8611	  else
8612	    {
8613	      offset = read_uleb128 (buf, &len, buf + i + 1);
8614	      assert (len == i + 1);
8615	      offset = offset * 4 + 0x204;
8616	      printf ("vsp = vsp + %ld", offset);
8617	    }
8618	}
8619      else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
8620	{
8621	  unsigned int first, last;
8622
8623	  GET_OP (op2);
8624	  first = op2 >> 4;
8625	  last = op2 & 0x0f;
8626	  if (op == 0xc8)
8627	    first = first + 16;
8628	  printf ("pop {D%d", first);
8629	  if (last)
8630	    printf ("-D%d", first + last);
8631	  printf ("}");
8632	}
8633      else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
8634	{
8635	  unsigned int count = op & 0x07;
8636
8637	  printf ("pop {D8");
8638	  if (count)
8639	    printf ("-D%d", 8 + count);
8640	  printf ("}");
8641	}
8642      else if (op >= 0xc0 && op <= 0xc5)
8643	{
8644	  unsigned int count = op & 0x07;
8645
8646	  printf ("     pop {wR10");
8647	  if (count)
8648	    printf ("-wR%d", 10 + count);
8649	  printf ("}");
8650	}
8651      else if (op == 0xc6)
8652	{
8653	  unsigned int first, last;
8654
8655	  GET_OP (op2);
8656	  first = op2 >> 4;
8657	  last = op2 & 0x0f;
8658	  printf ("pop {wR%d", first);
8659	  if (last)
8660	    printf ("-wR%d", first + last);
8661	  printf ("}");
8662	}
8663      else if (op == 0xc7)
8664	{
8665	  GET_OP (op2);
8666	  if (op2 == 0 || (op2 & 0xf0) != 0)
8667	    printf (_("[Spare]"));
8668	  else
8669	    {
8670	      unsigned int mask = op2 & 0x0f;
8671	      bfd_boolean first = TRUE;
8672	      int i;
8673
8674	      printf ("pop {");
8675	      for (i = 0; i < 4; i++)
8676		if (mask & (1 << i))
8677		  {
8678		    if (first)
8679		      first = FALSE;
8680		    else
8681		      printf (", ");
8682		    printf ("wCGR%d", i);
8683		  }
8684	      printf ("}");
8685	    }
8686	}
8687      else
8688	{
8689	  printf (_("     [unsupported opcode]"));
8690	  res = FALSE;
8691	}
8692
8693      printf ("\n");
8694    }
8695
8696  return res;
8697}
8698
8699static bfd_boolean
8700decode_tic6x_unwind_bytecode (Filedata *                 filedata,
8701			      struct arm_unw_aux_info *  aux,
8702			      unsigned int               word,
8703			      unsigned int               remaining,
8704			      unsigned int               more_words,
8705			      bfd_vma                    data_offset,
8706			      Elf_Internal_Shdr *        data_sec,
8707			      struct arm_section *       data_arm_sec)
8708{
8709  struct absaddr addr;
8710
8711  /* Decode the unwinding instructions.  */
8712  while (1)
8713    {
8714      unsigned int op, op2;
8715
8716      ADVANCE;
8717      if (remaining == 0)
8718	break;
8719      remaining--;
8720      op = word >> 24;
8721      word <<= 8;
8722
8723      printf ("  0x%02x ", op);
8724
8725      if ((op & 0xc0) == 0x00)
8726	{
8727	  int offset = ((op & 0x3f) << 3) + 8;
8728	  printf ("     sp = sp + %d", offset);
8729	}
8730      else if ((op & 0xc0) == 0x80)
8731	{
8732	  GET_OP (op2);
8733	  if (op == 0x80 && op2 == 0)
8734	    printf (_("Refuse to unwind"));
8735	  else
8736	    {
8737	      unsigned int mask = ((op & 0x1f) << 8) | op2;
8738	      if (op & 0x20)
8739		printf ("pop compact {");
8740	      else
8741		printf ("pop {");
8742
8743	      decode_tic6x_unwind_regmask (mask);
8744	      printf("}");
8745	    }
8746	}
8747      else if ((op & 0xf0) == 0xc0)
8748	{
8749	  unsigned int reg;
8750	  unsigned int nregs;
8751	  unsigned int i;
8752	  const char *name;
8753	  struct
8754	  {
8755	    unsigned int offset;
8756	    unsigned int reg;
8757	  } regpos[16];
8758
8759	  /* Scan entire instruction first so that GET_OP output is not
8760	     interleaved with disassembly.  */
8761	  nregs = 0;
8762	  for (i = 0; nregs < (op & 0xf); i++)
8763	    {
8764	      GET_OP (op2);
8765	      reg = op2 >> 4;
8766	      if (reg != 0xf)
8767		{
8768		  regpos[nregs].offset = i * 2;
8769		  regpos[nregs].reg = reg;
8770		  nregs++;
8771		}
8772
8773	      reg = op2 & 0xf;
8774	      if (reg != 0xf)
8775		{
8776		  regpos[nregs].offset = i * 2 + 1;
8777		  regpos[nregs].reg = reg;
8778		  nregs++;
8779		}
8780	    }
8781
8782	  printf (_("pop frame {"));
8783	  reg = nregs - 1;
8784	  for (i = i * 2; i > 0; i--)
8785	    {
8786	      if (regpos[reg].offset == i - 1)
8787		{
8788		  name = tic6x_unwind_regnames[regpos[reg].reg];
8789		  if (reg > 0)
8790		    reg--;
8791		}
8792	      else
8793		name = _("[pad]");
8794
8795	      fputs (name, stdout);
8796	      if (i > 1)
8797		printf (", ");
8798	    }
8799
8800	  printf ("}");
8801	}
8802      else if (op == 0xd0)
8803	printf ("     MOV FP, SP");
8804      else if (op == 0xd1)
8805	printf ("     __c6xabi_pop_rts");
8806      else if (op == 0xd2)
8807	{
8808	  unsigned char buf[9];
8809	  unsigned int i, len;
8810	  unsigned long offset;
8811
8812	  for (i = 0; i < sizeof (buf); i++)
8813	    {
8814	      GET_OP (buf[i]);
8815	      if ((buf[i] & 0x80) == 0)
8816		break;
8817	    }
8818	  /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2.  */
8819	  if (i == sizeof (buf))
8820	    {
8821	      warn (_("Corrupt stack pointer adjustment detected\n"));
8822	      return FALSE;
8823	    }
8824
8825	  offset = read_uleb128 (buf, &len, buf + i + 1);
8826	  assert (len == i + 1);
8827	  offset = offset * 8 + 0x408;
8828	  printf (_("sp = sp + %ld"), offset);
8829	}
8830      else if ((op & 0xf0) == 0xe0)
8831	{
8832	  if ((op & 0x0f) == 7)
8833	    printf ("     RETURN");
8834	  else
8835	    printf ("     MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
8836	}
8837      else
8838	{
8839	  printf (_("     [unsupported opcode]"));
8840	}
8841      putchar ('\n');
8842    }
8843
8844  return TRUE;
8845}
8846
8847static bfd_vma
8848arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
8849{
8850  bfd_vma offset;
8851
8852  offset = word & 0x7fffffff;
8853  if (offset & 0x40000000)
8854    offset |= ~ (bfd_vma) 0x7fffffff;
8855
8856  if (filedata->file_header.e_machine == EM_TI_C6000)
8857    offset <<= 1;
8858
8859  return offset + where;
8860}
8861
8862static bfd_boolean
8863decode_arm_unwind (Filedata *                 filedata,
8864		   struct arm_unw_aux_info *  aux,
8865		   unsigned int               word,
8866		   unsigned int               remaining,
8867		   bfd_vma                    data_offset,
8868		   Elf_Internal_Shdr *        data_sec,
8869		   struct arm_section *       data_arm_sec)
8870{
8871  int per_index;
8872  unsigned int more_words = 0;
8873  struct absaddr addr;
8874  bfd_vma sym_name = (bfd_vma) -1;
8875  bfd_boolean res = TRUE;
8876
8877  if (remaining == 0)
8878    {
8879      /* Fetch the first word.
8880	 Note - when decoding an object file the address extracted
8881	 here will always be 0.  So we also pass in the sym_name
8882	 parameter so that we can find the symbol associated with
8883	 the personality routine.  */
8884      if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
8885				     & word, & addr, & sym_name))
8886	return FALSE;
8887
8888      remaining = 4;
8889    }
8890  else
8891    {
8892      addr.section = SHN_UNDEF;
8893      addr.offset = 0;
8894    }
8895
8896  if ((word & 0x80000000) == 0)
8897    {
8898      /* Expand prel31 for personality routine.  */
8899      bfd_vma fn;
8900      const char *procname;
8901
8902      fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
8903      printf (_("  Personality routine: "));
8904      if (fn == 0
8905	  && addr.section == SHN_UNDEF && addr.offset == 0
8906	  && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
8907	{
8908	  procname = aux->strtab + sym_name;
8909	  print_vma (fn, PREFIX_HEX);
8910	  if (procname)
8911	    {
8912	      fputs (" <", stdout);
8913	      fputs (procname, stdout);
8914	      fputc ('>', stdout);
8915	    }
8916	}
8917      else
8918	procname = arm_print_vma_and_name (filedata, aux, fn, addr);
8919      fputc ('\n', stdout);
8920
8921      /* The GCC personality routines use the standard compact
8922	 encoding, starting with one byte giving the number of
8923	 words.  */
8924      if (procname != NULL
8925	  && (const_strneq (procname, "__gcc_personality_v0")
8926	      || const_strneq (procname, "__gxx_personality_v0")
8927	      || const_strneq (procname, "__gcj_personality_v0")
8928	      || const_strneq (procname, "__gnu_objc_personality_v0")))
8929	{
8930	  remaining = 0;
8931	  more_words = 1;
8932	  ADVANCE;
8933	  if (!remaining)
8934	    {
8935	      printf (_("  [Truncated data]\n"));
8936	      return FALSE;
8937	    }
8938	  more_words = word >> 24;
8939	  word <<= 8;
8940	  remaining--;
8941	  per_index = -1;
8942	}
8943      else
8944	return TRUE;
8945    }
8946  else
8947    {
8948      /* ARM EHABI Section 6.3:
8949
8950	 An exception-handling table entry for the compact model looks like:
8951
8952           31 30-28 27-24 23-0
8953	   -- ----- ----- ----
8954            1   0   index Data for personalityRoutine[index]    */
8955
8956      if (filedata->file_header.e_machine == EM_ARM
8957	  && (word & 0x70000000))
8958	{
8959	  warn (_("Corrupt ARM compact model table entry: %x \n"), word);
8960	  res = FALSE;
8961	}
8962
8963      per_index = (word >> 24) & 0x7f;
8964      printf (_("  Compact model index: %d\n"), per_index);
8965      if (per_index == 0)
8966	{
8967	  more_words = 0;
8968	  word <<= 8;
8969	  remaining--;
8970	}
8971      else if (per_index < 3)
8972	{
8973	  more_words = (word >> 16) & 0xff;
8974	  word <<= 16;
8975	  remaining -= 2;
8976	}
8977    }
8978
8979  switch (filedata->file_header.e_machine)
8980    {
8981    case EM_ARM:
8982      if (per_index < 3)
8983	{
8984	  if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
8985					    data_offset, data_sec, data_arm_sec))
8986	    res = FALSE;
8987	}
8988      else
8989	{
8990	  warn (_("Unknown ARM compact model index encountered\n"));
8991	  printf (_("  [reserved]\n"));
8992	  res = FALSE;
8993	}
8994      break;
8995
8996    case EM_TI_C6000:
8997      if (per_index < 3)
8998	{
8999	  if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
9000					      data_offset, data_sec, data_arm_sec))
9001	    res = FALSE;
9002	}
9003      else if (per_index < 5)
9004	{
9005	  if (((word >> 17) & 0x7f) == 0x7f)
9006	    printf (_("  Restore stack from frame pointer\n"));
9007	  else
9008	    printf (_("  Stack increment %d\n"), (word >> 14) & 0x1fc);
9009	  printf (_("  Registers restored: "));
9010	  if (per_index == 4)
9011	    printf (" (compact) ");
9012	  decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9013	  putchar ('\n');
9014	  printf (_("  Return register: %s\n"),
9015		  tic6x_unwind_regnames[word & 0xf]);
9016	}
9017      else
9018	printf (_("  [reserved (%d)]\n"), per_index);
9019      break;
9020
9021    default:
9022      error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
9023	     filedata->file_header.e_machine);
9024      res = FALSE;
9025    }
9026
9027  /* Decode the descriptors.  Not implemented.  */
9028
9029  return res;
9030}
9031
9032static bfd_boolean
9033dump_arm_unwind (Filedata *                 filedata,
9034		 struct arm_unw_aux_info *  aux,
9035		 Elf_Internal_Shdr *        exidx_sec)
9036{
9037  struct arm_section exidx_arm_sec, extab_arm_sec;
9038  unsigned int i, exidx_len;
9039  unsigned long j, nfuns;
9040  bfd_boolean res = TRUE;
9041
9042  memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9043  memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9044  exidx_len = exidx_sec->sh_size / 8;
9045
9046  aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9047  for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9048    if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9049      aux->funtab[nfuns++] = aux->symtab[j];
9050  aux->nfuns = nfuns;
9051  qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9052
9053  for (i = 0; i < exidx_len; i++)
9054    {
9055      unsigned int exidx_fn, exidx_entry;
9056      struct absaddr fn_addr, entry_addr;
9057      bfd_vma fn;
9058
9059      fputc ('\n', stdout);
9060
9061      if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9062				     8 * i, & exidx_fn, & fn_addr, NULL)
9063	  || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9064					8 * i + 4, & exidx_entry, & entry_addr, NULL))
9065	{
9066	  free (aux->funtab);
9067	  arm_free_section (& exidx_arm_sec);
9068	  arm_free_section (& extab_arm_sec);
9069	  return FALSE;
9070	}
9071
9072      /* ARM EHABI, Section 5:
9073	 An index table entry consists of 2 words.
9074         The first word contains a prel31 offset to the start of a function, with bit 31 clear.  */
9075      if (exidx_fn & 0x80000000)
9076	{
9077	  warn (_("corrupt index table entry: %x\n"), exidx_fn);
9078	  res = FALSE;
9079	}
9080
9081      fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
9082
9083      arm_print_vma_and_name (filedata, aux, fn, fn_addr);
9084      fputs (": ", stdout);
9085
9086      if (exidx_entry == 1)
9087	{
9088	  print_vma (exidx_entry, PREFIX_HEX);
9089	  fputs (" [cantunwind]\n", stdout);
9090	}
9091      else if (exidx_entry & 0x80000000)
9092	{
9093	  print_vma (exidx_entry, PREFIX_HEX);
9094	  fputc ('\n', stdout);
9095	  decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
9096	}
9097      else
9098	{
9099	  bfd_vma table, table_offset = 0;
9100	  Elf_Internal_Shdr *table_sec;
9101
9102	  fputs ("@", stdout);
9103	  table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
9104	  print_vma (table, PREFIX_HEX);
9105	  printf ("\n");
9106
9107	  /* Locate the matching .ARM.extab.  */
9108	  if (entry_addr.section != SHN_UNDEF
9109	      && entry_addr.section < filedata->file_header.e_shnum)
9110	    {
9111	      table_sec = filedata->section_headers + entry_addr.section;
9112	      table_offset = entry_addr.offset;
9113	      /* PR 18879 */
9114	      if (table_offset > table_sec->sh_size
9115		  || ((bfd_signed_vma) table_offset) < 0)
9116		{
9117		  warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9118			(unsigned long) table_offset,
9119			printable_section_name (filedata, table_sec));
9120		  res = FALSE;
9121		  continue;
9122		}
9123	    }
9124	  else
9125	    {
9126	      table_sec = find_section_by_address (filedata, table);
9127	      if (table_sec != NULL)
9128		table_offset = table - table_sec->sh_addr;
9129	    }
9130
9131	  if (table_sec == NULL)
9132	    {
9133	      warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9134		    (unsigned long) table);
9135	      res = FALSE;
9136	      continue;
9137	    }
9138
9139	  if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
9140				   &extab_arm_sec))
9141	    res = FALSE;
9142	}
9143    }
9144
9145  printf ("\n");
9146
9147  free (aux->funtab);
9148  arm_free_section (&exidx_arm_sec);
9149  arm_free_section (&extab_arm_sec);
9150
9151  return res;
9152}
9153
9154/* Used for both ARM and C6X unwinding tables.  */
9155
9156static bfd_boolean
9157arm_process_unwind (Filedata * filedata)
9158{
9159  struct arm_unw_aux_info aux;
9160  Elf_Internal_Shdr *unwsec = NULL;
9161  Elf_Internal_Shdr *strsec;
9162  Elf_Internal_Shdr *sec;
9163  unsigned long i;
9164  unsigned int sec_type;
9165  bfd_boolean res = TRUE;
9166
9167  switch (filedata->file_header.e_machine)
9168    {
9169    case EM_ARM:
9170      sec_type = SHT_ARM_EXIDX;
9171      break;
9172
9173    case EM_TI_C6000:
9174      sec_type = SHT_C6000_UNWIND;
9175      break;
9176
9177    default:
9178      error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
9179	     filedata->file_header.e_machine);
9180      return FALSE;
9181    }
9182
9183  if (filedata->string_table == NULL)
9184    return FALSE;
9185
9186  memset (& aux, 0, sizeof (aux));
9187  aux.filedata = filedata;
9188
9189  for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9190    {
9191      if (sec->sh_type == SHT_SYMTAB && sec->sh_link < filedata->file_header.e_shnum)
9192	{
9193	  aux.symtab = GET_ELF_SYMBOLS (filedata, sec, & aux.nsyms);
9194
9195	  strsec = filedata->section_headers + sec->sh_link;
9196
9197	  /* PR binutils/17531 file: 011-12666-0.004.  */
9198	  if (aux.strtab != NULL)
9199	    {
9200	      error (_("Multiple string tables found in file.\n"));
9201	      free (aux.strtab);
9202	      res = FALSE;
9203	    }
9204	  aux.strtab = get_data (NULL, filedata, strsec->sh_offset,
9205				 1, strsec->sh_size, _("string table"));
9206	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
9207	}
9208      else if (sec->sh_type == sec_type)
9209	unwsec = sec;
9210    }
9211
9212  if (unwsec == NULL)
9213    printf (_("\nThere are no unwind sections in this file.\n"));
9214  else
9215    for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9216      {
9217	if (sec->sh_type == sec_type)
9218	  {
9219	    unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
9220	    printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9221			      "contains %lu entry:\n",
9222			      "\nUnwind section '%s' at offset 0x%lx "
9223			      "contains %lu entries:\n",
9224			      num_unwind),
9225		    printable_section_name (filedata, sec),
9226		    (unsigned long) sec->sh_offset,
9227		    num_unwind);
9228
9229	    if (! dump_arm_unwind (filedata, &aux, sec))
9230	      res = FALSE;
9231	  }
9232      }
9233
9234  if (aux.symtab)
9235    free (aux.symtab);
9236  if (aux.strtab)
9237    free ((char *) aux.strtab);
9238
9239  return res;
9240}
9241
9242static bfd_boolean
9243process_unwind (Filedata * filedata)
9244{
9245  struct unwind_handler
9246  {
9247    unsigned int machtype;
9248    bfd_boolean (* handler)(Filedata *);
9249  } handlers[] =
9250  {
9251    { EM_ARM, arm_process_unwind },
9252    { EM_IA_64, ia64_process_unwind },
9253    { EM_PARISC, hppa_process_unwind },
9254    { EM_TI_C6000, arm_process_unwind },
9255    { 0, NULL }
9256  };
9257  int i;
9258
9259  if (!do_unwind)
9260    return TRUE;
9261
9262  for (i = 0; handlers[i].handler != NULL; i++)
9263    if (filedata->file_header.e_machine == handlers[i].machtype)
9264      return handlers[i].handler (filedata);
9265
9266  printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
9267	  get_machine_name (filedata->file_header.e_machine));
9268  return TRUE;
9269}
9270
9271static void
9272dynamic_section_mips_val (Elf_Internal_Dyn * entry)
9273{
9274  switch (entry->d_tag)
9275    {
9276    case DT_MIPS_FLAGS:
9277      if (entry->d_un.d_val == 0)
9278	printf (_("NONE"));
9279      else
9280	{
9281	  static const char * opts[] =
9282	  {
9283	    "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
9284	    "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
9285	    "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
9286	    "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
9287	    "RLD_ORDER_SAFE"
9288	  };
9289	  unsigned int cnt;
9290	  bfd_boolean first = TRUE;
9291
9292	  for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
9293	    if (entry->d_un.d_val & (1 << cnt))
9294	      {
9295		printf ("%s%s", first ? "" : " ", opts[cnt]);
9296		first = FALSE;
9297	      }
9298	}
9299      break;
9300
9301    case DT_MIPS_IVERSION:
9302      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
9303	printf (_("Interface Version: %s"), GET_DYNAMIC_NAME (entry->d_un.d_val));
9304      else
9305	{
9306	  char buf[40];
9307	  sprintf_vma (buf, entry->d_un.d_ptr);
9308	  /* Note: coded this way so that there is a single string for translation.  */
9309	  printf (_("<corrupt: %s>"), buf);
9310	}
9311      break;
9312
9313    case DT_MIPS_TIME_STAMP:
9314      {
9315	char timebuf[128];
9316	struct tm * tmp;
9317	time_t atime = entry->d_un.d_val;
9318
9319	tmp = gmtime (&atime);
9320	/* PR 17531: file: 6accc532.  */
9321	if (tmp == NULL)
9322	  snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
9323	else
9324	  snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
9325		    tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9326		    tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9327	printf (_("Time Stamp: %s"), timebuf);
9328      }
9329      break;
9330
9331    case DT_MIPS_RLD_VERSION:
9332    case DT_MIPS_LOCAL_GOTNO:
9333    case DT_MIPS_CONFLICTNO:
9334    case DT_MIPS_LIBLISTNO:
9335    case DT_MIPS_SYMTABNO:
9336    case DT_MIPS_UNREFEXTNO:
9337    case DT_MIPS_HIPAGENO:
9338    case DT_MIPS_DELTA_CLASS_NO:
9339    case DT_MIPS_DELTA_INSTANCE_NO:
9340    case DT_MIPS_DELTA_RELOC_NO:
9341    case DT_MIPS_DELTA_SYM_NO:
9342    case DT_MIPS_DELTA_CLASSSYM_NO:
9343    case DT_MIPS_COMPACT_SIZE:
9344      print_vma (entry->d_un.d_val, DEC);
9345      break;
9346
9347    default:
9348      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9349    }
9350    putchar ('\n');
9351}
9352
9353static void
9354dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
9355{
9356  switch (entry->d_tag)
9357    {
9358    case DT_HP_DLD_FLAGS:
9359      {
9360	static struct
9361	{
9362	  long int bit;
9363	  const char * str;
9364	}
9365	flags[] =
9366	{
9367	  { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
9368	  { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
9369	  { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
9370	  { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
9371	  { DT_HP_BIND_NOW, "HP_BIND_NOW" },
9372	  { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
9373	  { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
9374	  { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
9375	  { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
9376	  { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
9377	  { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
9378	  { DT_HP_GST, "HP_GST" },
9379	  { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
9380	  { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
9381	  { DT_HP_NODELETE, "HP_NODELETE" },
9382	  { DT_HP_GROUP, "HP_GROUP" },
9383	  { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
9384	};
9385	bfd_boolean first = TRUE;
9386	size_t cnt;
9387	bfd_vma val = entry->d_un.d_val;
9388
9389	for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
9390	  if (val & flags[cnt].bit)
9391	    {
9392	      if (! first)
9393		putchar (' ');
9394	      fputs (flags[cnt].str, stdout);
9395	      first = FALSE;
9396	      val ^= flags[cnt].bit;
9397	    }
9398
9399	if (val != 0 || first)
9400	  {
9401	    if (! first)
9402	      putchar (' ');
9403	    print_vma (val, HEX);
9404	  }
9405      }
9406      break;
9407
9408    default:
9409      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9410      break;
9411    }
9412  putchar ('\n');
9413}
9414
9415#ifdef BFD64
9416
9417/* VMS vs Unix time offset and factor.  */
9418
9419#define VMS_EPOCH_OFFSET 35067168000000000LL
9420#define VMS_GRANULARITY_FACTOR 10000000
9421
9422/* Display a VMS time in a human readable format.  */
9423
9424static void
9425print_vms_time (bfd_int64_t vmstime)
9426{
9427  struct tm *tm;
9428  time_t unxtime;
9429
9430  unxtime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
9431  tm = gmtime (&unxtime);
9432  printf ("%04u-%02u-%02uT%02u:%02u:%02u",
9433          tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
9434          tm->tm_hour, tm->tm_min, tm->tm_sec);
9435}
9436#endif /* BFD64 */
9437
9438static void
9439dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
9440{
9441  switch (entry->d_tag)
9442    {
9443    case DT_IA_64_PLT_RESERVE:
9444      /* First 3 slots reserved.  */
9445      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9446      printf (" -- ");
9447      print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
9448      break;
9449
9450    case DT_IA_64_VMS_LINKTIME:
9451#ifdef BFD64
9452      print_vms_time (entry->d_un.d_val);
9453#endif
9454      break;
9455
9456    case DT_IA_64_VMS_LNKFLAGS:
9457      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9458      if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
9459        printf (" CALL_DEBUG");
9460      if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
9461        printf (" NOP0BUFS");
9462      if (entry->d_un.d_val & VMS_LF_P0IMAGE)
9463        printf (" P0IMAGE");
9464      if (entry->d_un.d_val & VMS_LF_MKTHREADS)
9465        printf (" MKTHREADS");
9466      if (entry->d_un.d_val & VMS_LF_UPCALLS)
9467        printf (" UPCALLS");
9468      if (entry->d_un.d_val & VMS_LF_IMGSTA)
9469        printf (" IMGSTA");
9470      if (entry->d_un.d_val & VMS_LF_INITIALIZE)
9471        printf (" INITIALIZE");
9472      if (entry->d_un.d_val & VMS_LF_MAIN)
9473        printf (" MAIN");
9474      if (entry->d_un.d_val & VMS_LF_EXE_INIT)
9475        printf (" EXE_INIT");
9476      if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
9477        printf (" TBK_IN_IMG");
9478      if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
9479        printf (" DBG_IN_IMG");
9480      if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
9481        printf (" TBK_IN_DSF");
9482      if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
9483        printf (" DBG_IN_DSF");
9484      if (entry->d_un.d_val & VMS_LF_SIGNATURES)
9485        printf (" SIGNATURES");
9486      if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
9487        printf (" REL_SEG_OFF");
9488      break;
9489
9490    default:
9491      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9492      break;
9493    }
9494  putchar ('\n');
9495}
9496
9497static bfd_boolean
9498get_32bit_dynamic_section (Filedata * filedata)
9499{
9500  Elf32_External_Dyn * edyn;
9501  Elf32_External_Dyn * ext;
9502  Elf_Internal_Dyn * entry;
9503
9504  edyn = (Elf32_External_Dyn *) get_data (NULL, filedata, dynamic_addr, 1,
9505                                          dynamic_size, _("dynamic section"));
9506  if (!edyn)
9507    return FALSE;
9508
9509  /* SGI's ELF has more than one section in the DYNAMIC segment, and we
9510     might not have the luxury of section headers.  Look for the DT_NULL
9511     terminator to determine the number of entries.  */
9512  for (ext = edyn, dynamic_nent = 0;
9513       (char *) (ext + 1) <= (char *) edyn + dynamic_size;
9514       ext++)
9515    {
9516      dynamic_nent++;
9517      if (BYTE_GET (ext->d_tag) == DT_NULL)
9518	break;
9519    }
9520
9521  dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
9522                                                  sizeof (* entry));
9523  if (dynamic_section == NULL)
9524    {
9525      error (_("Out of memory allocating space for %lu dynamic entries\n"),
9526	     (unsigned long) dynamic_nent);
9527      free (edyn);
9528      return FALSE;
9529    }
9530
9531  for (ext = edyn, entry = dynamic_section;
9532       entry < dynamic_section + dynamic_nent;
9533       ext++, entry++)
9534    {
9535      entry->d_tag      = BYTE_GET (ext->d_tag);
9536      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
9537    }
9538
9539  free (edyn);
9540
9541  return TRUE;
9542}
9543
9544static bfd_boolean
9545get_64bit_dynamic_section (Filedata * filedata)
9546{
9547  Elf64_External_Dyn * edyn;
9548  Elf64_External_Dyn * ext;
9549  Elf_Internal_Dyn * entry;
9550
9551  /* Read in the data.  */
9552  edyn = (Elf64_External_Dyn *) get_data (NULL, filedata, dynamic_addr, 1,
9553                                          dynamic_size, _("dynamic section"));
9554  if (!edyn)
9555    return FALSE;
9556
9557  /* SGI's ELF has more than one section in the DYNAMIC segment, and we
9558     might not have the luxury of section headers.  Look for the DT_NULL
9559     terminator to determine the number of entries.  */
9560  for (ext = edyn, dynamic_nent = 0;
9561       /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer.  */
9562       (char *) (ext + 1) <= (char *) edyn + dynamic_size;
9563       ext++)
9564    {
9565      dynamic_nent++;
9566      if (BYTE_GET (ext->d_tag) == DT_NULL)
9567	break;
9568    }
9569
9570  dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
9571                                                  sizeof (* entry));
9572  if (dynamic_section == NULL)
9573    {
9574      error (_("Out of memory allocating space for %lu dynamic entries\n"),
9575	     (unsigned long) dynamic_nent);
9576      free (edyn);
9577      return FALSE;
9578    }
9579
9580  /* Convert from external to internal formats.  */
9581  for (ext = edyn, entry = dynamic_section;
9582       entry < dynamic_section + dynamic_nent;
9583       ext++, entry++)
9584    {
9585      entry->d_tag      = BYTE_GET (ext->d_tag);
9586      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
9587    }
9588
9589  free (edyn);
9590
9591  return TRUE;
9592}
9593
9594static void
9595print_dynamic_flags (bfd_vma flags)
9596{
9597  bfd_boolean first = TRUE;
9598
9599  while (flags)
9600    {
9601      bfd_vma flag;
9602
9603      flag = flags & - flags;
9604      flags &= ~ flag;
9605
9606      if (first)
9607	first = FALSE;
9608      else
9609	putc (' ', stdout);
9610
9611      switch (flag)
9612	{
9613	case DF_ORIGIN:		fputs ("ORIGIN", stdout); break;
9614	case DF_SYMBOLIC:	fputs ("SYMBOLIC", stdout); break;
9615	case DF_TEXTREL:	fputs ("TEXTREL", stdout); break;
9616	case DF_BIND_NOW:	fputs ("BIND_NOW", stdout); break;
9617	case DF_STATIC_TLS:	fputs ("STATIC_TLS", stdout); break;
9618	default:		fputs (_("unknown"), stdout); break;
9619	}
9620    }
9621  puts ("");
9622}
9623
9624/* Parse and display the contents of the dynamic section.  */
9625
9626static bfd_boolean
9627process_dynamic_section (Filedata * filedata)
9628{
9629  Elf_Internal_Dyn * entry;
9630
9631  if (dynamic_size == 0)
9632    {
9633      if (do_dynamic)
9634	printf (_("\nThere is no dynamic section in this file.\n"));
9635
9636      return TRUE;
9637    }
9638
9639  if (is_32bit_elf)
9640    {
9641      if (! get_32bit_dynamic_section (filedata))
9642	return FALSE;
9643    }
9644  else
9645    {
9646      if (! get_64bit_dynamic_section (filedata))
9647	return FALSE;
9648    }
9649
9650  /* Find the appropriate symbol table.  */
9651  if (dynamic_symbols == NULL)
9652    {
9653      for (entry = dynamic_section;
9654	   entry < dynamic_section + dynamic_nent;
9655	   ++entry)
9656	{
9657	  Elf_Internal_Shdr section;
9658
9659	  if (entry->d_tag != DT_SYMTAB)
9660	    continue;
9661
9662	  dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
9663
9664	  /* Since we do not know how big the symbol table is,
9665	     we default to reading in the entire file (!) and
9666	     processing that.  This is overkill, I know, but it
9667	     should work.  */
9668	  section.sh_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
9669	  if ((bfd_size_type) section.sh_offset > filedata->file_size)
9670	    {
9671	      /* See PR 21379 for a reproducer.  */
9672	      error (_("Invalid DT_SYMTAB entry: %lx"), (long) section.sh_offset);
9673	      return FALSE;
9674	    }
9675
9676	  if (archive_file_offset != 0)
9677	    section.sh_size = archive_file_size - section.sh_offset;
9678	  else
9679	    section.sh_size = filedata->file_size - section.sh_offset;
9680
9681	  if (is_32bit_elf)
9682	    section.sh_entsize = sizeof (Elf32_External_Sym);
9683	  else
9684	    section.sh_entsize = sizeof (Elf64_External_Sym);
9685	  section.sh_name = filedata->string_table_length;
9686
9687	  dynamic_symbols = GET_ELF_SYMBOLS (filedata, &section, & num_dynamic_syms);
9688	  if (num_dynamic_syms < 1)
9689	    {
9690	      error (_("Unable to determine the number of symbols to load\n"));
9691	      continue;
9692	    }
9693	}
9694    }
9695
9696  /* Similarly find a string table.  */
9697  if (dynamic_strings == NULL)
9698    {
9699      for (entry = dynamic_section;
9700	   entry < dynamic_section + dynamic_nent;
9701	   ++entry)
9702	{
9703	  unsigned long offset;
9704	  long str_tab_len;
9705
9706	  if (entry->d_tag != DT_STRTAB)
9707	    continue;
9708
9709	  dynamic_info[DT_STRTAB] = entry->d_un.d_val;
9710
9711	  /* Since we do not know how big the string table is,
9712	     we default to reading in the entire file (!) and
9713	     processing that.  This is overkill, I know, but it
9714	     should work.  */
9715
9716	  offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
9717
9718	  if (archive_file_offset != 0)
9719	    str_tab_len = archive_file_size - offset;
9720	  else
9721	    str_tab_len = filedata->file_size - offset;
9722
9723	  if (str_tab_len < 1)
9724	    {
9725	      error
9726		(_("Unable to determine the length of the dynamic string table\n"));
9727	      continue;
9728	    }
9729
9730	  dynamic_strings = (char *) get_data (NULL, filedata, offset, 1,
9731                                               str_tab_len,
9732                                               _("dynamic string table"));
9733	  dynamic_strings_length = dynamic_strings == NULL ? 0 : str_tab_len;
9734	  break;
9735	}
9736    }
9737
9738  /* And find the syminfo section if available.  */
9739  if (dynamic_syminfo == NULL)
9740    {
9741      unsigned long syminsz = 0;
9742
9743      for (entry = dynamic_section;
9744	   entry < dynamic_section + dynamic_nent;
9745	   ++entry)
9746	{
9747	  if (entry->d_tag == DT_SYMINENT)
9748	    {
9749	      /* Note: these braces are necessary to avoid a syntax
9750		 error from the SunOS4 C compiler.  */
9751	      /* PR binutils/17531: A corrupt file can trigger this test.
9752		 So do not use an assert, instead generate an error message.  */
9753	      if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
9754		error (_("Bad value (%d) for SYMINENT entry\n"),
9755		       (int) entry->d_un.d_val);
9756	    }
9757	  else if (entry->d_tag == DT_SYMINSZ)
9758	    syminsz = entry->d_un.d_val;
9759	  else if (entry->d_tag == DT_SYMINFO)
9760	    dynamic_syminfo_offset = offset_from_vma (filedata, entry->d_un.d_val,
9761						      syminsz);
9762	}
9763
9764      if (dynamic_syminfo_offset != 0 && syminsz != 0)
9765	{
9766	  Elf_External_Syminfo * extsyminfo;
9767	  Elf_External_Syminfo * extsym;
9768	  Elf_Internal_Syminfo * syminfo;
9769
9770	  /* There is a syminfo section.  Read the data.  */
9771	  extsyminfo = (Elf_External_Syminfo *)
9772              get_data (NULL, filedata, dynamic_syminfo_offset, 1, syminsz,
9773                        _("symbol information"));
9774	  if (!extsyminfo)
9775	    return FALSE;
9776
9777	  dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
9778	  if (dynamic_syminfo == NULL)
9779	    {
9780	      error (_("Out of memory allocating %lu byte for dynamic symbol info\n"),
9781		     (unsigned long) syminsz);
9782	      return FALSE;
9783	    }
9784
9785	  dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
9786	  for (syminfo = dynamic_syminfo, extsym = extsyminfo;
9787	       syminfo < dynamic_syminfo + dynamic_syminfo_nent;
9788	       ++syminfo, ++extsym)
9789	    {
9790	      syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
9791	      syminfo->si_flags = BYTE_GET (extsym->si_flags);
9792	    }
9793
9794	  free (extsyminfo);
9795	}
9796    }
9797
9798  if (do_dynamic && dynamic_addr)
9799    printf (ngettext ("\nDynamic section at offset 0x%lx "
9800		      "contains %lu entry:\n",
9801		      "\nDynamic section at offset 0x%lx "
9802		      "contains %lu entries:\n",
9803		      dynamic_nent),
9804	    dynamic_addr, (unsigned long) dynamic_nent);
9805  if (do_dynamic)
9806    printf (_("  Tag        Type                         Name/Value\n"));
9807
9808  for (entry = dynamic_section;
9809       entry < dynamic_section + dynamic_nent;
9810       entry++)
9811    {
9812      if (do_dynamic)
9813	{
9814	  const char * dtype;
9815
9816	  putchar (' ');
9817	  print_vma (entry->d_tag, FULL_HEX);
9818	  dtype = get_dynamic_type (filedata, entry->d_tag);
9819	  printf (" (%s)%*s", dtype,
9820		  ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
9821	}
9822
9823      switch (entry->d_tag)
9824	{
9825	case DT_FLAGS:
9826	  if (do_dynamic)
9827	    print_dynamic_flags (entry->d_un.d_val);
9828	  break;
9829
9830	case DT_AUXILIARY:
9831	case DT_FILTER:
9832	case DT_CONFIG:
9833	case DT_DEPAUDIT:
9834	case DT_AUDIT:
9835	  if (do_dynamic)
9836	    {
9837	      switch (entry->d_tag)
9838		{
9839		case DT_AUXILIARY:
9840		  printf (_("Auxiliary library"));
9841		  break;
9842
9843		case DT_FILTER:
9844		  printf (_("Filter library"));
9845		  break;
9846
9847		case DT_CONFIG:
9848		  printf (_("Configuration file"));
9849		  break;
9850
9851		case DT_DEPAUDIT:
9852		  printf (_("Dependency audit library"));
9853		  break;
9854
9855		case DT_AUDIT:
9856		  printf (_("Audit library"));
9857		  break;
9858		}
9859
9860	      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
9861		printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
9862	      else
9863		{
9864		  printf (": ");
9865		  print_vma (entry->d_un.d_val, PREFIX_HEX);
9866		  putchar ('\n');
9867		}
9868	    }
9869	  break;
9870
9871	case DT_FEATURE:
9872	  if (do_dynamic)
9873	    {
9874	      printf (_("Flags:"));
9875
9876	      if (entry->d_un.d_val == 0)
9877		printf (_(" None\n"));
9878	      else
9879		{
9880		  unsigned long int val = entry->d_un.d_val;
9881
9882		  if (val & DTF_1_PARINIT)
9883		    {
9884		      printf (" PARINIT");
9885		      val ^= DTF_1_PARINIT;
9886		    }
9887		  if (val & DTF_1_CONFEXP)
9888		    {
9889		      printf (" CONFEXP");
9890		      val ^= DTF_1_CONFEXP;
9891		    }
9892		  if (val != 0)
9893		    printf (" %lx", val);
9894		  puts ("");
9895		}
9896	    }
9897	  break;
9898
9899	case DT_POSFLAG_1:
9900	  if (do_dynamic)
9901	    {
9902	      printf (_("Flags:"));
9903
9904	      if (entry->d_un.d_val == 0)
9905		printf (_(" None\n"));
9906	      else
9907		{
9908		  unsigned long int val = entry->d_un.d_val;
9909
9910		  if (val & DF_P1_LAZYLOAD)
9911		    {
9912		      printf (" LAZYLOAD");
9913		      val ^= DF_P1_LAZYLOAD;
9914		    }
9915		  if (val & DF_P1_GROUPPERM)
9916		    {
9917		      printf (" GROUPPERM");
9918		      val ^= DF_P1_GROUPPERM;
9919		    }
9920		  if (val != 0)
9921		    printf (" %lx", val);
9922		  puts ("");
9923		}
9924	    }
9925	  break;
9926
9927	case DT_FLAGS_1:
9928	  if (do_dynamic)
9929	    {
9930	      printf (_("Flags:"));
9931	      if (entry->d_un.d_val == 0)
9932		printf (_(" None\n"));
9933	      else
9934		{
9935		  unsigned long int val = entry->d_un.d_val;
9936
9937		  if (val & DF_1_NOW)
9938		    {
9939		      printf (" NOW");
9940		      val ^= DF_1_NOW;
9941		    }
9942		  if (val & DF_1_GLOBAL)
9943		    {
9944		      printf (" GLOBAL");
9945		      val ^= DF_1_GLOBAL;
9946		    }
9947		  if (val & DF_1_GROUP)
9948		    {
9949		      printf (" GROUP");
9950		      val ^= DF_1_GROUP;
9951		    }
9952		  if (val & DF_1_NODELETE)
9953		    {
9954		      printf (" NODELETE");
9955		      val ^= DF_1_NODELETE;
9956		    }
9957		  if (val & DF_1_LOADFLTR)
9958		    {
9959		      printf (" LOADFLTR");
9960		      val ^= DF_1_LOADFLTR;
9961		    }
9962		  if (val & DF_1_INITFIRST)
9963		    {
9964		      printf (" INITFIRST");
9965		      val ^= DF_1_INITFIRST;
9966		    }
9967		  if (val & DF_1_NOOPEN)
9968		    {
9969		      printf (" NOOPEN");
9970		      val ^= DF_1_NOOPEN;
9971		    }
9972		  if (val & DF_1_ORIGIN)
9973		    {
9974		      printf (" ORIGIN");
9975		      val ^= DF_1_ORIGIN;
9976		    }
9977		  if (val & DF_1_DIRECT)
9978		    {
9979		      printf (" DIRECT");
9980		      val ^= DF_1_DIRECT;
9981		    }
9982		  if (val & DF_1_TRANS)
9983		    {
9984		      printf (" TRANS");
9985		      val ^= DF_1_TRANS;
9986		    }
9987		  if (val & DF_1_INTERPOSE)
9988		    {
9989		      printf (" INTERPOSE");
9990		      val ^= DF_1_INTERPOSE;
9991		    }
9992		  if (val & DF_1_NODEFLIB)
9993		    {
9994		      printf (" NODEFLIB");
9995		      val ^= DF_1_NODEFLIB;
9996		    }
9997		  if (val & DF_1_NODUMP)
9998		    {
9999		      printf (" NODUMP");
10000		      val ^= DF_1_NODUMP;
10001		    }
10002		  if (val & DF_1_CONFALT)
10003		    {
10004		      printf (" CONFALT");
10005		      val ^= DF_1_CONFALT;
10006		    }
10007		  if (val & DF_1_ENDFILTEE)
10008		    {
10009		      printf (" ENDFILTEE");
10010		      val ^= DF_1_ENDFILTEE;
10011		    }
10012		  if (val & DF_1_DISPRELDNE)
10013		    {
10014		      printf (" DISPRELDNE");
10015		      val ^= DF_1_DISPRELDNE;
10016		    }
10017		  if (val & DF_1_DISPRELPND)
10018		    {
10019		      printf (" DISPRELPND");
10020		      val ^= DF_1_DISPRELPND;
10021		    }
10022		  if (val & DF_1_NODIRECT)
10023		    {
10024		      printf (" NODIRECT");
10025		      val ^= DF_1_NODIRECT;
10026		    }
10027		  if (val & DF_1_IGNMULDEF)
10028		    {
10029		      printf (" IGNMULDEF");
10030		      val ^= DF_1_IGNMULDEF;
10031		    }
10032		  if (val & DF_1_NOKSYMS)
10033		    {
10034		      printf (" NOKSYMS");
10035		      val ^= DF_1_NOKSYMS;
10036		    }
10037		  if (val & DF_1_NOHDR)
10038		    {
10039		      printf (" NOHDR");
10040		      val ^= DF_1_NOHDR;
10041		    }
10042		  if (val & DF_1_EDITED)
10043		    {
10044		      printf (" EDITED");
10045		      val ^= DF_1_EDITED;
10046		    }
10047		  if (val & DF_1_NORELOC)
10048		    {
10049		      printf (" NORELOC");
10050		      val ^= DF_1_NORELOC;
10051		    }
10052		  if (val & DF_1_SYMINTPOSE)
10053		    {
10054		      printf (" SYMINTPOSE");
10055		      val ^= DF_1_SYMINTPOSE;
10056		    }
10057		  if (val & DF_1_GLOBAUDIT)
10058		    {
10059		      printf (" GLOBAUDIT");
10060		      val ^= DF_1_GLOBAUDIT;
10061		    }
10062		  if (val & DF_1_SINGLETON)
10063		    {
10064		      printf (" SINGLETON");
10065		      val ^= DF_1_SINGLETON;
10066		    }
10067		  if (val & DF_1_STUB)
10068		    {
10069		      printf (" STUB");
10070		      val ^= DF_1_STUB;
10071		    }
10072		  if (val & DF_1_PIE)
10073		    {
10074		      printf (" PIE");
10075		      val ^= DF_1_PIE;
10076		    }
10077		  if (val & DF_1_KMOD)
10078		    {
10079		      printf (" KMOD");
10080		      val ^= DF_1_KMOD;
10081		    }
10082		  if (val & DF_1_WEAKFILTER)
10083		    {
10084		      printf (" WEAKFILTER");
10085		      val ^= DF_1_WEAKFILTER;
10086		    }
10087		  if (val & DF_1_NOCOMMON)
10088		    {
10089		      printf (" NOCOMMON");
10090		      val ^= DF_1_NOCOMMON;
10091		    }
10092		  if (val != 0)
10093		    printf (" %lx", val);
10094		  puts ("");
10095		}
10096	    }
10097	  break;
10098
10099	case DT_PLTREL:
10100	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
10101	  if (do_dynamic)
10102	    puts (get_dynamic_type (filedata, entry->d_un.d_val));
10103	  break;
10104
10105	case DT_NULL	:
10106	case DT_NEEDED	:
10107	case DT_PLTGOT	:
10108	case DT_HASH	:
10109	case DT_STRTAB	:
10110	case DT_SYMTAB	:
10111	case DT_RELA	:
10112	case DT_INIT	:
10113	case DT_FINI	:
10114	case DT_SONAME	:
10115	case DT_RPATH	:
10116	case DT_SYMBOLIC:
10117	case DT_REL	:
10118	case DT_DEBUG	:
10119	case DT_TEXTREL	:
10120	case DT_JMPREL	:
10121	case DT_RUNPATH	:
10122	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
10123
10124	  if (do_dynamic)
10125	    {
10126	      char * name;
10127
10128	      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
10129		name = GET_DYNAMIC_NAME (entry->d_un.d_val);
10130	      else
10131		name = NULL;
10132
10133	      if (name)
10134		{
10135		  switch (entry->d_tag)
10136		    {
10137		    case DT_NEEDED:
10138		      printf (_("Shared library: [%s]"), name);
10139
10140		      if (streq (name, program_interpreter))
10141			printf (_(" program interpreter"));
10142		      break;
10143
10144		    case DT_SONAME:
10145		      printf (_("Library soname: [%s]"), name);
10146		      break;
10147
10148		    case DT_RPATH:
10149		      printf (_("Library rpath: [%s]"), name);
10150		      break;
10151
10152		    case DT_RUNPATH:
10153		      printf (_("Library runpath: [%s]"), name);
10154		      break;
10155
10156		    default:
10157		      print_vma (entry->d_un.d_val, PREFIX_HEX);
10158		      break;
10159		    }
10160		}
10161	      else
10162		print_vma (entry->d_un.d_val, PREFIX_HEX);
10163
10164	      putchar ('\n');
10165	    }
10166	  break;
10167
10168	case DT_PLTRELSZ:
10169	case DT_RELASZ	:
10170	case DT_STRSZ	:
10171	case DT_RELSZ	:
10172	case DT_RELAENT	:
10173	case DT_SYMENT	:
10174	case DT_RELENT	:
10175	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
10176	  /* Fall through.  */
10177	case DT_PLTPADSZ:
10178	case DT_MOVEENT	:
10179	case DT_MOVESZ	:
10180	case DT_INIT_ARRAYSZ:
10181	case DT_FINI_ARRAYSZ:
10182	case DT_GNU_CONFLICTSZ:
10183	case DT_GNU_LIBLISTSZ:
10184	  if (do_dynamic)
10185	    {
10186	      print_vma (entry->d_un.d_val, UNSIGNED);
10187	      printf (_(" (bytes)\n"));
10188	    }
10189	  break;
10190
10191	case DT_VERDEFNUM:
10192	case DT_VERNEEDNUM:
10193	case DT_RELACOUNT:
10194	case DT_RELCOUNT:
10195	  if (do_dynamic)
10196	    {
10197	      print_vma (entry->d_un.d_val, UNSIGNED);
10198	      putchar ('\n');
10199	    }
10200	  break;
10201
10202	case DT_SYMINSZ:
10203	case DT_SYMINENT:
10204	case DT_SYMINFO:
10205	case DT_USED:
10206	case DT_INIT_ARRAY:
10207	case DT_FINI_ARRAY:
10208	  if (do_dynamic)
10209	    {
10210	      if (entry->d_tag == DT_USED
10211		  && VALID_DYNAMIC_NAME (entry->d_un.d_val))
10212		{
10213		  char * name = GET_DYNAMIC_NAME (entry->d_un.d_val);
10214
10215		  if (*name)
10216		    {
10217		      printf (_("Not needed object: [%s]\n"), name);
10218		      break;
10219		    }
10220		}
10221
10222	      print_vma (entry->d_un.d_val, PREFIX_HEX);
10223	      putchar ('\n');
10224	    }
10225	  break;
10226
10227	case DT_BIND_NOW:
10228	  /* The value of this entry is ignored.  */
10229	  if (do_dynamic)
10230	    putchar ('\n');
10231	  break;
10232
10233	case DT_GNU_PRELINKED:
10234	  if (do_dynamic)
10235	    {
10236	      struct tm * tmp;
10237	      time_t atime = entry->d_un.d_val;
10238
10239	      tmp = gmtime (&atime);
10240	      /* PR 17533 file: 041-1244816-0.004.  */
10241	      if (tmp == NULL)
10242		printf (_("<corrupt time val: %lx"),
10243			(unsigned long) atime);
10244	      else
10245		printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
10246			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10247			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10248
10249	    }
10250	  break;
10251
10252	case DT_GNU_HASH:
10253	  dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10254	  if (do_dynamic)
10255	    {
10256	      print_vma (entry->d_un.d_val, PREFIX_HEX);
10257	      putchar ('\n');
10258	    }
10259	  break;
10260
10261	default:
10262	  if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
10263	    version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
10264	      entry->d_un.d_val;
10265
10266	  if (do_dynamic)
10267	    {
10268	      switch (filedata->file_header.e_machine)
10269		{
10270		case EM_MIPS:
10271		case EM_MIPS_RS3_LE:
10272		  dynamic_section_mips_val (entry);
10273		  break;
10274		case EM_PARISC:
10275		  dynamic_section_parisc_val (entry);
10276		  break;
10277		case EM_IA_64:
10278		  dynamic_section_ia64_val (entry);
10279		  break;
10280		default:
10281		  print_vma (entry->d_un.d_val, PREFIX_HEX);
10282		  putchar ('\n');
10283		}
10284	    }
10285	  break;
10286	}
10287    }
10288
10289  return TRUE;
10290}
10291
10292static char *
10293get_ver_flags (unsigned int flags)
10294{
10295  static char buff[128];
10296
10297  buff[0] = 0;
10298
10299  if (flags == 0)
10300    return _("none");
10301
10302  if (flags & VER_FLG_BASE)
10303    strcat (buff, "BASE");
10304
10305  if (flags & VER_FLG_WEAK)
10306    {
10307      if (flags & VER_FLG_BASE)
10308	strcat (buff, " | ");
10309
10310      strcat (buff, "WEAK");
10311    }
10312
10313  if (flags & VER_FLG_INFO)
10314    {
10315      if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
10316	strcat (buff, " | ");
10317
10318      strcat (buff, "INFO");
10319    }
10320
10321  if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
10322    {
10323      if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
10324	strcat (buff, " | ");
10325
10326      strcat (buff, _("<unknown>"));
10327    }
10328
10329  return buff;
10330}
10331
10332/* Display the contents of the version sections.  */
10333
10334static bfd_boolean
10335process_version_sections (Filedata * filedata)
10336{
10337  Elf_Internal_Shdr * section;
10338  unsigned i;
10339  bfd_boolean found = FALSE;
10340
10341  if (! do_version)
10342    return TRUE;
10343
10344  for (i = 0, section = filedata->section_headers;
10345       i < filedata->file_header.e_shnum;
10346       i++, section++)
10347    {
10348      switch (section->sh_type)
10349	{
10350	case SHT_GNU_verdef:
10351	  {
10352	    Elf_External_Verdef * edefs;
10353	    unsigned long idx;
10354	    unsigned long cnt;
10355	    char * endbuf;
10356
10357	    found = TRUE;
10358
10359	    printf (ngettext ("\nVersion definition section '%s' "
10360			      "contains %u entry:\n",
10361			      "\nVersion definition section '%s' "
10362			      "contains %u entries:\n",
10363			      section->sh_info),
10364		    printable_section_name (filedata, section),
10365		    section->sh_info);
10366
10367	    printf (_("  Addr: 0x"));
10368	    printf_vma (section->sh_addr);
10369	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
10370		    (unsigned long) section->sh_offset, section->sh_link,
10371		    printable_section_name_from_index (filedata, section->sh_link));
10372
10373	    edefs = (Elf_External_Verdef *)
10374                get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
10375                          _("version definition section"));
10376	    if (!edefs)
10377	      break;
10378	    endbuf = (char *) edefs + section->sh_size;
10379
10380	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
10381	      {
10382		char * vstart;
10383		Elf_External_Verdef * edef;
10384		Elf_Internal_Verdef ent;
10385		Elf_External_Verdaux * eaux;
10386		Elf_Internal_Verdaux aux;
10387		unsigned long isum;
10388		int j;
10389
10390		vstart = ((char *) edefs) + idx;
10391		if (vstart + sizeof (*edef) > endbuf)
10392		  break;
10393
10394		edef = (Elf_External_Verdef *) vstart;
10395
10396		ent.vd_version = BYTE_GET (edef->vd_version);
10397		ent.vd_flags   = BYTE_GET (edef->vd_flags);
10398		ent.vd_ndx     = BYTE_GET (edef->vd_ndx);
10399		ent.vd_cnt     = BYTE_GET (edef->vd_cnt);
10400		ent.vd_hash    = BYTE_GET (edef->vd_hash);
10401		ent.vd_aux     = BYTE_GET (edef->vd_aux);
10402		ent.vd_next    = BYTE_GET (edef->vd_next);
10403
10404		printf (_("  %#06lx: Rev: %d  Flags: %s"),
10405			idx, ent.vd_version, get_ver_flags (ent.vd_flags));
10406
10407		printf (_("  Index: %d  Cnt: %d  "),
10408			ent.vd_ndx, ent.vd_cnt);
10409
10410		/* Check for overflow.  */
10411		if (ent.vd_aux > (size_t) (endbuf - vstart))
10412		  break;
10413
10414		vstart += ent.vd_aux;
10415
10416		if (vstart + sizeof (*eaux) > endbuf)
10417		  break;
10418		eaux = (Elf_External_Verdaux *) vstart;
10419
10420		aux.vda_name = BYTE_GET (eaux->vda_name);
10421		aux.vda_next = BYTE_GET (eaux->vda_next);
10422
10423		if (VALID_DYNAMIC_NAME (aux.vda_name))
10424		  printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
10425		else
10426		  printf (_("Name index: %ld\n"), aux.vda_name);
10427
10428		isum = idx + ent.vd_aux;
10429
10430		for (j = 1; j < ent.vd_cnt; j++)
10431		  {
10432		    if (aux.vda_next < sizeof (*eaux)
10433			&& !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
10434		      {
10435			warn (_("Invalid vda_next field of %lx\n"),
10436			      aux.vda_next);
10437			j = ent.vd_cnt;
10438			break;
10439		      }
10440		    /* Check for overflow.  */
10441		    if (aux.vda_next > (size_t) (endbuf - vstart))
10442		      break;
10443
10444		    isum   += aux.vda_next;
10445		    vstart += aux.vda_next;
10446
10447		    if (vstart + sizeof (*eaux) > endbuf)
10448		      break;
10449		    eaux = (Elf_External_Verdaux *) vstart;
10450
10451		    aux.vda_name = BYTE_GET (eaux->vda_name);
10452		    aux.vda_next = BYTE_GET (eaux->vda_next);
10453
10454		    if (VALID_DYNAMIC_NAME (aux.vda_name))
10455		      printf (_("  %#06lx: Parent %d: %s\n"),
10456			      isum, j, GET_DYNAMIC_NAME (aux.vda_name));
10457		    else
10458		      printf (_("  %#06lx: Parent %d, name index: %ld\n"),
10459			      isum, j, aux.vda_name);
10460		  }
10461
10462		if (j < ent.vd_cnt)
10463		  printf (_("  Version def aux past end of section\n"));
10464
10465		/* PR 17531:
10466		   file: id:000001,src:000172+005151,op:splice,rep:2.  */
10467		if (ent.vd_next < sizeof (*edef)
10468		    && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
10469		  {
10470		    warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
10471		    cnt = section->sh_info;
10472		    break;
10473		  }
10474		if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
10475		  break;
10476
10477		idx += ent.vd_next;
10478	      }
10479
10480	    if (cnt < section->sh_info)
10481	      printf (_("  Version definition past end of section\n"));
10482
10483	    free (edefs);
10484	  }
10485	  break;
10486
10487	case SHT_GNU_verneed:
10488	  {
10489	    Elf_External_Verneed * eneed;
10490	    unsigned long idx;
10491	    unsigned long cnt;
10492	    char * endbuf;
10493
10494	    found = TRUE;
10495
10496	    printf (ngettext ("\nVersion needs section '%s' "
10497			      "contains %u entry:\n",
10498			      "\nVersion needs section '%s' "
10499			      "contains %u entries:\n",
10500			      section->sh_info),
10501		    printable_section_name (filedata, section), section->sh_info);
10502
10503	    printf (_(" Addr: 0x"));
10504	    printf_vma (section->sh_addr);
10505	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
10506		    (unsigned long) section->sh_offset, section->sh_link,
10507		    printable_section_name_from_index (filedata, section->sh_link));
10508
10509	    eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
10510                                                       section->sh_offset, 1,
10511                                                       section->sh_size,
10512                                                       _("Version Needs section"));
10513	    if (!eneed)
10514	      break;
10515	    endbuf = (char *) eneed + section->sh_size;
10516
10517	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
10518	      {
10519		Elf_External_Verneed * entry;
10520		Elf_Internal_Verneed ent;
10521		unsigned long isum;
10522		int j;
10523		char * vstart;
10524
10525		vstart = ((char *) eneed) + idx;
10526		if (vstart + sizeof (*entry) > endbuf)
10527		  break;
10528
10529		entry = (Elf_External_Verneed *) vstart;
10530
10531		ent.vn_version = BYTE_GET (entry->vn_version);
10532		ent.vn_cnt     = BYTE_GET (entry->vn_cnt);
10533		ent.vn_file    = BYTE_GET (entry->vn_file);
10534		ent.vn_aux     = BYTE_GET (entry->vn_aux);
10535		ent.vn_next    = BYTE_GET (entry->vn_next);
10536
10537		printf (_("  %#06lx: Version: %d"), idx, ent.vn_version);
10538
10539		if (VALID_DYNAMIC_NAME (ent.vn_file))
10540		  printf (_("  File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
10541		else
10542		  printf (_("  File: %lx"), ent.vn_file);
10543
10544		printf (_("  Cnt: %d\n"), ent.vn_cnt);
10545
10546		/* Check for overflow.  */
10547		if (ent.vn_aux > (size_t) (endbuf - vstart))
10548		  break;
10549		vstart += ent.vn_aux;
10550
10551		for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
10552		  {
10553		    Elf_External_Vernaux * eaux;
10554		    Elf_Internal_Vernaux aux;
10555
10556		    if (vstart + sizeof (*eaux) > endbuf)
10557		      break;
10558		    eaux = (Elf_External_Vernaux *) vstart;
10559
10560		    aux.vna_hash  = BYTE_GET (eaux->vna_hash);
10561		    aux.vna_flags = BYTE_GET (eaux->vna_flags);
10562		    aux.vna_other = BYTE_GET (eaux->vna_other);
10563		    aux.vna_name  = BYTE_GET (eaux->vna_name);
10564		    aux.vna_next  = BYTE_GET (eaux->vna_next);
10565
10566		    if (VALID_DYNAMIC_NAME (aux.vna_name))
10567		      printf (_("  %#06lx:   Name: %s"),
10568			      isum, GET_DYNAMIC_NAME (aux.vna_name));
10569		    else
10570		      printf (_("  %#06lx:   Name index: %lx"),
10571			      isum, aux.vna_name);
10572
10573		    printf (_("  Flags: %s  Version: %d\n"),
10574			    get_ver_flags (aux.vna_flags), aux.vna_other);
10575
10576		    if (aux.vna_next < sizeof (*eaux)
10577			&& !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
10578		      {
10579			warn (_("Invalid vna_next field of %lx\n"),
10580			      aux.vna_next);
10581			j = ent.vn_cnt;
10582			break;
10583		      }
10584		    /* Check for overflow.  */
10585		    if (aux.vna_next > (size_t) (endbuf - vstart))
10586		      break;
10587		    isum   += aux.vna_next;
10588		    vstart += aux.vna_next;
10589		  }
10590
10591		if (j < ent.vn_cnt)
10592		  warn (_("Missing Version Needs auxillary information\n"));
10593
10594		if (ent.vn_next < sizeof (*entry)
10595		    && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
10596		  {
10597		    warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
10598		    cnt = section->sh_info;
10599		    break;
10600		  }
10601		if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
10602		  break;
10603		idx += ent.vn_next;
10604	      }
10605
10606	    if (cnt < section->sh_info)
10607	      warn (_("Missing Version Needs information\n"));
10608
10609	    free (eneed);
10610	  }
10611	  break;
10612
10613	case SHT_GNU_versym:
10614	  {
10615	    Elf_Internal_Shdr * link_section;
10616	    size_t total;
10617	    unsigned int cnt;
10618	    unsigned char * edata;
10619	    unsigned short * data;
10620	    char * strtab;
10621	    Elf_Internal_Sym * symbols;
10622	    Elf_Internal_Shdr * string_sec;
10623	    unsigned long num_syms;
10624	    long off;
10625
10626	    if (section->sh_link >= filedata->file_header.e_shnum)
10627	      break;
10628
10629	    link_section = filedata->section_headers + section->sh_link;
10630	    total = section->sh_size / sizeof (Elf_External_Versym);
10631
10632	    if (link_section->sh_link >= filedata->file_header.e_shnum)
10633	      break;
10634
10635	    found = TRUE;
10636
10637	    symbols = GET_ELF_SYMBOLS (filedata, link_section, & num_syms);
10638	    if (symbols == NULL)
10639	      break;
10640
10641	    string_sec = filedata->section_headers + link_section->sh_link;
10642
10643	    strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
10644                                        string_sec->sh_size,
10645                                        _("version string table"));
10646	    if (!strtab)
10647	      {
10648		free (symbols);
10649		break;
10650	      }
10651
10652	    printf (ngettext ("\nVersion symbols section '%s' "
10653			      "contains %lu entry:\n",
10654			      "\nVersion symbols section '%s' "
10655			      "contains %lu entries:\n",
10656			      total),
10657		    printable_section_name (filedata, section), (unsigned long) total);
10658
10659	    printf (_(" Addr: "));
10660	    printf_vma (section->sh_addr);
10661	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
10662		    (unsigned long) section->sh_offset, section->sh_link,
10663		    printable_section_name (filedata, link_section));
10664
10665	    off = offset_from_vma (filedata,
10666				   version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
10667				   total * sizeof (short));
10668	    edata = (unsigned char *) get_data (NULL, filedata, off, total,
10669                                                sizeof (short),
10670                                                _("version symbol data"));
10671	    if (!edata)
10672	      {
10673		free (strtab);
10674		free (symbols);
10675		break;
10676	      }
10677
10678	    data = (short unsigned int *) cmalloc (total, sizeof (short));
10679
10680	    for (cnt = total; cnt --;)
10681	      data[cnt] = byte_get (edata + cnt * sizeof (short),
10682				    sizeof (short));
10683
10684	    free (edata);
10685
10686	    for (cnt = 0; cnt < total; cnt += 4)
10687	      {
10688		int j, nn;
10689		char *name;
10690		char *invalid = _("*invalid*");
10691
10692		printf ("  %03x:", cnt);
10693
10694		for (j = 0; (j < 4) && (cnt + j) < total; ++j)
10695		  switch (data[cnt + j])
10696		    {
10697		    case 0:
10698		      fputs (_("   0 (*local*)    "), stdout);
10699		      break;
10700
10701		    case 1:
10702		      fputs (_("   1 (*global*)   "), stdout);
10703		      break;
10704
10705		    default:
10706		      nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
10707				   data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
10708
10709		      /* If this index value is greater than the size of the symbols
10710		         array, break to avoid an out-of-bounds read.  */
10711		      if ((unsigned long)(cnt + j) >= num_syms)
10712		        {
10713		          warn (_("invalid index into symbol array\n"));
10714		          break;
10715			}
10716
10717		      name = NULL;
10718		      if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
10719			{
10720			  Elf_Internal_Verneed ivn;
10721			  unsigned long offset;
10722
10723			  offset = offset_from_vma
10724			    (filedata, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
10725			     sizeof (Elf_External_Verneed));
10726
10727			  do
10728			    {
10729			      Elf_Internal_Vernaux ivna;
10730			      Elf_External_Verneed evn;
10731			      Elf_External_Vernaux evna;
10732			      unsigned long a_off;
10733
10734			      if (get_data (&evn, filedata, offset, sizeof (evn), 1,
10735					    _("version need")) == NULL)
10736				break;
10737
10738			      ivn.vn_aux  = BYTE_GET (evn.vn_aux);
10739			      ivn.vn_next = BYTE_GET (evn.vn_next);
10740
10741			      a_off = offset + ivn.vn_aux;
10742
10743			      do
10744				{
10745				  if (get_data (&evna, filedata, a_off, sizeof (evna),
10746						1, _("version need aux (2)")) == NULL)
10747				    {
10748				      ivna.vna_next  = 0;
10749				      ivna.vna_other = 0;
10750				    }
10751				  else
10752				    {
10753				      ivna.vna_next  = BYTE_GET (evna.vna_next);
10754				      ivna.vna_other = BYTE_GET (evna.vna_other);
10755				    }
10756
10757				  a_off += ivna.vna_next;
10758				}
10759			      while (ivna.vna_other != data[cnt + j]
10760				     && ivna.vna_next != 0);
10761
10762			      if (ivna.vna_other == data[cnt + j])
10763				{
10764				  ivna.vna_name = BYTE_GET (evna.vna_name);
10765
10766				  if (ivna.vna_name >= string_sec->sh_size)
10767				    name = invalid;
10768				  else
10769				    name = strtab + ivna.vna_name;
10770				  break;
10771				}
10772
10773			      offset += ivn.vn_next;
10774			    }
10775			  while (ivn.vn_next);
10776			}
10777
10778		      if (data[cnt + j] != 0x8001
10779			  && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
10780			{
10781			  Elf_Internal_Verdef ivd;
10782			  Elf_External_Verdef evd;
10783			  unsigned long offset;
10784
10785			  offset = offset_from_vma
10786			    (filedata, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
10787			     sizeof evd);
10788
10789			  do
10790			    {
10791			      if (get_data (&evd, filedata, offset, sizeof (evd), 1,
10792					    _("version def")) == NULL)
10793				{
10794				  ivd.vd_next = 0;
10795				  /* PR 17531: file: 046-1082287-0.004.  */
10796				  ivd.vd_ndx  = (data[cnt + j] & VERSYM_VERSION) + 1;
10797				  break;
10798				}
10799			      else
10800				{
10801				  ivd.vd_next = BYTE_GET (evd.vd_next);
10802				  ivd.vd_ndx  = BYTE_GET (evd.vd_ndx);
10803				}
10804
10805			      offset += ivd.vd_next;
10806			    }
10807			  while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
10808				 && ivd.vd_next != 0);
10809
10810			  if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
10811			    {
10812			      Elf_External_Verdaux evda;
10813			      Elf_Internal_Verdaux ivda;
10814
10815			      ivd.vd_aux = BYTE_GET (evd.vd_aux);
10816
10817			      if (get_data (&evda, filedata,
10818					    offset - ivd.vd_next + ivd.vd_aux,
10819					    sizeof (evda), 1,
10820					    _("version def aux")) == NULL)
10821				break;
10822
10823			      ivda.vda_name = BYTE_GET (evda.vda_name);
10824
10825			      if (ivda.vda_name >= string_sec->sh_size)
10826				name = invalid;
10827			      else if (name != NULL && name != invalid)
10828				name = _("*both*");
10829			      else
10830				name = strtab + ivda.vda_name;
10831			    }
10832			}
10833		      if (name != NULL)
10834			nn += printf ("(%s%-*s",
10835				      name,
10836				      12 - (int) strlen (name),
10837				      ")");
10838
10839		      if (nn < 18)
10840			printf ("%*c", 18 - nn, ' ');
10841		    }
10842
10843		putchar ('\n');
10844	      }
10845
10846	    free (data);
10847	    free (strtab);
10848	    free (symbols);
10849	  }
10850	  break;
10851
10852	default:
10853	  break;
10854	}
10855    }
10856
10857  if (! found)
10858    printf (_("\nNo version information found in this file.\n"));
10859
10860  return TRUE;
10861}
10862
10863static const char *
10864get_symbol_binding (Filedata * filedata, unsigned int binding)
10865{
10866  static char buff[32];
10867
10868  switch (binding)
10869    {
10870    case STB_LOCAL:	return "LOCAL";
10871    case STB_GLOBAL:	return "GLOBAL";
10872    case STB_WEAK:	return "WEAK";
10873    default:
10874      if (binding >= STB_LOPROC && binding <= STB_HIPROC)
10875	snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
10876		  binding);
10877      else if (binding >= STB_LOOS && binding <= STB_HIOS)
10878	{
10879	  if (binding == STB_GNU_UNIQUE
10880	      && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
10881		  /* GNU is still using the default value 0.  */
10882		  || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
10883	    return "UNIQUE";
10884	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
10885	}
10886      else
10887	snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
10888      return buff;
10889    }
10890}
10891
10892static const char *
10893get_symbol_type (Filedata * filedata, unsigned int type)
10894{
10895  static char buff[32];
10896
10897  switch (type)
10898    {
10899    case STT_NOTYPE:	return "NOTYPE";
10900    case STT_OBJECT:	return "OBJECT";
10901    case STT_FUNC:	return "FUNC";
10902    case STT_SECTION:	return "SECTION";
10903    case STT_FILE:	return "FILE";
10904    case STT_COMMON:	return "COMMON";
10905    case STT_TLS:	return "TLS";
10906    case STT_RELC:      return "RELC";
10907    case STT_SRELC:     return "SRELC";
10908    default:
10909      if (type >= STT_LOPROC && type <= STT_HIPROC)
10910	{
10911	  if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
10912	    return "THUMB_FUNC";
10913
10914	  if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
10915	    return "REGISTER";
10916
10917	  if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
10918	    return "PARISC_MILLI";
10919
10920	  snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
10921	}
10922      else if (type >= STT_LOOS && type <= STT_HIOS)
10923	{
10924	  if (filedata->file_header.e_machine == EM_PARISC)
10925	    {
10926	      if (type == STT_HP_OPAQUE)
10927		return "HP_OPAQUE";
10928	      if (type == STT_HP_STUB)
10929		return "HP_STUB";
10930	    }
10931
10932	  if (type == STT_GNU_IFUNC
10933	      && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
10934		  || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD
10935		  /* GNU is still using the default value 0.  */
10936		  || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
10937	    return "IFUNC";
10938
10939	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
10940	}
10941      else
10942	snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
10943      return buff;
10944    }
10945}
10946
10947static const char *
10948get_symbol_visibility (unsigned int visibility)
10949{
10950  switch (visibility)
10951    {
10952    case STV_DEFAULT:	return "DEFAULT";
10953    case STV_INTERNAL:	return "INTERNAL";
10954    case STV_HIDDEN:	return "HIDDEN";
10955    case STV_PROTECTED: return "PROTECTED";
10956    default:
10957      error (_("Unrecognized visibility value: %u"), visibility);
10958      return _("<unknown>");
10959    }
10960}
10961
10962static const char *
10963get_alpha_symbol_other (unsigned int other)
10964{
10965  switch (other)
10966    {
10967    case STO_ALPHA_NOPV:
10968      return "NOPV";
10969    case STO_ALPHA_STD_GPLOAD:
10970      return "STD GPLOAD";
10971    default:
10972      return NULL;
10973    }
10974}
10975
10976static const char *
10977get_solaris_symbol_visibility (unsigned int visibility)
10978{
10979  switch (visibility)
10980    {
10981    case 4: return "EXPORTED";
10982    case 5: return "SINGLETON";
10983    case 6: return "ELIMINATE";
10984    default: return get_symbol_visibility (visibility);
10985    }
10986}
10987
10988static const char *
10989get_mips_symbol_other (unsigned int other)
10990{
10991  switch (other)
10992    {
10993    case STO_OPTIONAL:      return "OPTIONAL";
10994    case STO_MIPS_PLT:      return "MIPS PLT";
10995    case STO_MIPS_PIC:      return "MIPS PIC";
10996    case STO_MICROMIPS:     return "MICROMIPS";
10997    case STO_MICROMIPS | STO_MIPS_PIC:      return "MICROMIPS, MIPS PIC";
10998    case STO_MIPS16:        return "MIPS16";
10999    default:	            return NULL;
11000    }
11001}
11002
11003static const char *
11004get_ia64_symbol_other (Filedata * filedata, unsigned int other)
11005{
11006  if (is_ia64_vms (filedata))
11007    {
11008      static char res[32];
11009
11010      res[0] = 0;
11011
11012      /* Function types is for images and .STB files only.  */
11013      switch (filedata->file_header.e_type)
11014        {
11015        case ET_DYN:
11016        case ET_EXEC:
11017          switch (VMS_ST_FUNC_TYPE (other))
11018            {
11019            case VMS_SFT_CODE_ADDR:
11020              strcat (res, " CA");
11021              break;
11022            case VMS_SFT_SYMV_IDX:
11023              strcat (res, " VEC");
11024              break;
11025            case VMS_SFT_FD:
11026              strcat (res, " FD");
11027              break;
11028            case VMS_SFT_RESERVE:
11029              strcat (res, " RSV");
11030              break;
11031            default:
11032	      warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
11033		    VMS_ST_FUNC_TYPE (other));
11034	      strcat (res, " <unknown>");
11035	      break;
11036            }
11037          break;
11038        default:
11039          break;
11040        }
11041      switch (VMS_ST_LINKAGE (other))
11042        {
11043        case VMS_STL_IGNORE:
11044          strcat (res, " IGN");
11045          break;
11046        case VMS_STL_RESERVE:
11047          strcat (res, " RSV");
11048          break;
11049        case VMS_STL_STD:
11050          strcat (res, " STD");
11051          break;
11052        case VMS_STL_LNK:
11053          strcat (res, " LNK");
11054          break;
11055        default:
11056	  warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
11057		VMS_ST_LINKAGE (other));
11058	  strcat (res, " <unknown>");
11059	  break;
11060        }
11061
11062      if (res[0] != 0)
11063        return res + 1;
11064      else
11065        return res;
11066    }
11067  return NULL;
11068}
11069
11070static const char *
11071get_ppc64_symbol_other (unsigned int other)
11072{
11073  if (PPC64_LOCAL_ENTRY_OFFSET (other) != 0)
11074    {
11075      static char buf[32];
11076      snprintf (buf, sizeof buf, _("<localentry>: %d"),
11077		PPC64_LOCAL_ENTRY_OFFSET (other));
11078      return buf;
11079    }
11080  return NULL;
11081}
11082
11083static const char *
11084get_symbol_other (Filedata * filedata, unsigned int other)
11085{
11086  const char * result = NULL;
11087  static char buff [32];
11088
11089  if (other == 0)
11090    return "";
11091
11092  switch (filedata->file_header.e_machine)
11093    {
11094    case EM_ALPHA:
11095      result = get_alpha_symbol_other (other);
11096      break;
11097    case EM_MIPS:
11098      result = get_mips_symbol_other (other);
11099      break;
11100    case EM_IA_64:
11101      result = get_ia64_symbol_other (filedata, other);
11102      break;
11103    case EM_PPC64:
11104      result = get_ppc64_symbol_other (other);
11105      break;
11106    default:
11107      result = NULL;
11108      break;
11109    }
11110
11111  if (result)
11112    return result;
11113
11114  snprintf (buff, sizeof buff, _("<other>: %x"), other);
11115  return buff;
11116}
11117
11118static const char *
11119get_symbol_index_type (Filedata * filedata, unsigned int type)
11120{
11121  static char buff[32];
11122
11123  switch (type)
11124    {
11125    case SHN_UNDEF:	return "UND";
11126    case SHN_ABS:	return "ABS";
11127    case SHN_COMMON:	return "COM";
11128    default:
11129      if (type == SHN_IA_64_ANSI_COMMON
11130	  && filedata->file_header.e_machine == EM_IA_64
11131	  && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
11132	return "ANSI_COM";
11133      else if ((filedata->file_header.e_machine == EM_X86_64
11134		|| filedata->file_header.e_machine == EM_L1OM
11135		|| filedata->file_header.e_machine == EM_K1OM)
11136	       && type == SHN_X86_64_LCOMMON)
11137	return "LARGE_COM";
11138      else if ((type == SHN_MIPS_SCOMMON
11139		&& filedata->file_header.e_machine == EM_MIPS)
11140	       || (type == SHN_TIC6X_SCOMMON
11141		   && filedata->file_header.e_machine == EM_TI_C6000))
11142	return "SCOM";
11143      else if (type == SHN_MIPS_SUNDEFINED
11144	       && filedata->file_header.e_machine == EM_MIPS)
11145	return "SUND";
11146      else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
11147	sprintf (buff, "PRC[0x%04x]", type & 0xffff);
11148      else if (type >= SHN_LOOS && type <= SHN_HIOS)
11149	sprintf (buff, "OS [0x%04x]", type & 0xffff);
11150      else if (type >= SHN_LORESERVE)
11151	sprintf (buff, "RSV[0x%04x]", type & 0xffff);
11152      else if (type >= filedata->file_header.e_shnum)
11153	sprintf (buff, _("bad section index[%3d]"), type);
11154      else
11155	sprintf (buff, "%3d", type);
11156      break;
11157    }
11158
11159  return buff;
11160}
11161
11162static bfd_vma *
11163get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
11164{
11165  unsigned char * e_data;
11166  bfd_vma * i_data;
11167
11168  /* If the size_t type is smaller than the bfd_size_type, eg because
11169     you are building a 32-bit tool on a 64-bit host, then make sure
11170     that when (number) is cast to (size_t) no information is lost.  */
11171  if (sizeof (size_t) < sizeof (bfd_size_type)
11172      && (bfd_size_type) ((size_t) number) != number)
11173    {
11174      error (_("Size truncation prevents reading %s elements of size %u\n"),
11175	     bfd_vmatoa ("u", number), ent_size);
11176      return NULL;
11177    }
11178
11179  /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
11180     attempting to allocate memory when the read is bound to fail.  */
11181  if (ent_size * number > filedata->file_size)
11182    {
11183      error (_("Invalid number of dynamic entries: %s\n"),
11184	     bfd_vmatoa ("u", number));
11185      return NULL;
11186    }
11187
11188  e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
11189  if (e_data == NULL)
11190    {
11191      error (_("Out of memory reading %s dynamic entries\n"),
11192	     bfd_vmatoa ("u", number));
11193      return NULL;
11194    }
11195
11196  if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
11197    {
11198      error (_("Unable to read in %s bytes of dynamic data\n"),
11199	     bfd_vmatoa ("u", number * ent_size));
11200      free (e_data);
11201      return NULL;
11202    }
11203
11204  i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
11205  if (i_data == NULL)
11206    {
11207      error (_("Out of memory allocating space for %s dynamic entries\n"),
11208	     bfd_vmatoa ("u", number));
11209      free (e_data);
11210      return NULL;
11211    }
11212
11213  while (number--)
11214    i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11215
11216  free (e_data);
11217
11218  return i_data;
11219}
11220
11221static void
11222print_dynamic_symbol (Filedata * filedata, bfd_vma si, unsigned long hn)
11223{
11224  Elf_Internal_Sym * psym;
11225  int n;
11226
11227  n = print_vma (si, DEC_5);
11228  if (n < 5)
11229    fputs (&"     "[n], stdout);
11230  printf (" %3lu: ", hn);
11231
11232  if (dynamic_symbols == NULL || si >= num_dynamic_syms)
11233    {
11234      printf (_("<No info available for dynamic symbol number %lu>\n"),
11235	      (unsigned long) si);
11236      return;
11237    }
11238
11239  psym = dynamic_symbols + si;
11240  print_vma (psym->st_value, LONG_HEX);
11241  putchar (' ');
11242  print_vma (psym->st_size, DEC_5);
11243
11244  printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
11245  printf (" %-6s",  get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
11246
11247  if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
11248    printf (" %-7s",  get_solaris_symbol_visibility (psym->st_other));
11249  else
11250    {
11251      unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
11252
11253      printf (" %-7s",  get_symbol_visibility (vis));
11254      /* Check to see if any other bits in the st_other field are set.
11255	 Note - displaying this information disrupts the layout of the
11256	 table being generated, but for the moment this case is very
11257	 rare.  */
11258      if (psym->st_other ^ vis)
11259	printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
11260    }
11261
11262  printf (" %3.3s ", get_symbol_index_type (filedata, psym->st_shndx));
11263  if (VALID_DYNAMIC_NAME (psym->st_name))
11264    print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
11265  else
11266    printf (_(" <corrupt: %14ld>"), psym->st_name);
11267  putchar ('\n');
11268}
11269
11270static const char *
11271get_symbol_version_string (Filedata *                   filedata,
11272			   bfd_boolean                  is_dynsym,
11273			   const char *                 strtab,
11274			   unsigned long int            strtab_size,
11275			   unsigned int                 si,
11276			   Elf_Internal_Sym *           psym,
11277			   enum versioned_symbol_info * sym_info,
11278			   unsigned short *             vna_other)
11279{
11280  unsigned char data[2];
11281  unsigned short vers_data;
11282  unsigned long offset;
11283
11284  if (!is_dynsym
11285      || version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
11286    return NULL;
11287
11288  offset = offset_from_vma (filedata, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11289			    sizeof data + si * sizeof (vers_data));
11290
11291  if (get_data (&data, filedata, offset + si * sizeof (vers_data),
11292		sizeof (data), 1, _("version data")) == NULL)
11293    return NULL;
11294
11295  vers_data = byte_get (data, 2);
11296
11297  if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
11298    return NULL;
11299
11300  /* Usually we'd only see verdef for defined symbols, and verneed for
11301     undefined symbols.  However, symbols defined by the linker in
11302     .dynbss for variables copied from a shared library in order to
11303     avoid text relocations are defined yet have verneed.  We could
11304     use a heuristic to detect the special case, for example, check
11305     for verneed first on symbols defined in SHT_NOBITS sections, but
11306     it is simpler and more reliable to just look for both verdef and
11307     verneed.  .dynbss might not be mapped to a SHT_NOBITS section.  */
11308
11309  if (psym->st_shndx != SHN_UNDEF
11310      && vers_data != 0x8001
11311      && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
11312    {
11313      Elf_Internal_Verdef ivd;
11314      Elf_Internal_Verdaux ivda;
11315      Elf_External_Verdaux evda;
11316      unsigned long off;
11317
11318      off = offset_from_vma (filedata,
11319			     version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
11320			     sizeof (Elf_External_Verdef));
11321
11322      do
11323	{
11324	  Elf_External_Verdef evd;
11325
11326	  if (get_data (&evd, filedata, off, sizeof (evd), 1,
11327			_("version def")) == NULL)
11328	    {
11329	      ivd.vd_ndx = 0;
11330	      ivd.vd_aux = 0;
11331	      ivd.vd_next = 0;
11332	      ivd.vd_flags = 0;
11333	    }
11334	  else
11335	    {
11336	      ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
11337	      ivd.vd_aux = BYTE_GET (evd.vd_aux);
11338	      ivd.vd_next = BYTE_GET (evd.vd_next);
11339	      ivd.vd_flags = BYTE_GET (evd.vd_flags);
11340	    }
11341
11342	  off += ivd.vd_next;
11343	}
11344      while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
11345
11346      if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
11347	{
11348	  if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
11349	    return NULL;
11350
11351	  off -= ivd.vd_next;
11352	  off += ivd.vd_aux;
11353
11354	  if (get_data (&evda, filedata, off, sizeof (evda), 1,
11355			_("version def aux")) != NULL)
11356	    {
11357	      ivda.vda_name = BYTE_GET (evda.vda_name);
11358
11359	      if (psym->st_name != ivda.vda_name)
11360		{
11361		  *sym_info = ((vers_data & VERSYM_HIDDEN) != 0
11362			       ? symbol_hidden : symbol_public);
11363		  return (ivda.vda_name < strtab_size
11364			  ? strtab + ivda.vda_name : _("<corrupt>"));
11365		}
11366	    }
11367	}
11368    }
11369
11370  if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
11371    {
11372      Elf_External_Verneed evn;
11373      Elf_Internal_Verneed ivn;
11374      Elf_Internal_Vernaux ivna;
11375
11376      offset = offset_from_vma (filedata,
11377				version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
11378				sizeof evn);
11379      do
11380	{
11381	  unsigned long vna_off;
11382
11383	  if (get_data (&evn, filedata, offset, sizeof (evn), 1,
11384			_("version need")) == NULL)
11385	    {
11386	      ivna.vna_next = 0;
11387	      ivna.vna_other = 0;
11388	      ivna.vna_name = 0;
11389	      break;
11390	    }
11391
11392	  ivn.vn_aux  = BYTE_GET (evn.vn_aux);
11393	  ivn.vn_next = BYTE_GET (evn.vn_next);
11394
11395	  vna_off = offset + ivn.vn_aux;
11396
11397	  do
11398	    {
11399	      Elf_External_Vernaux evna;
11400
11401	      if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
11402			    _("version need aux (3)")) == NULL)
11403		{
11404		  ivna.vna_next = 0;
11405		  ivna.vna_other = 0;
11406		  ivna.vna_name = 0;
11407		}
11408	      else
11409		{
11410		  ivna.vna_other = BYTE_GET (evna.vna_other);
11411		  ivna.vna_next  = BYTE_GET (evna.vna_next);
11412		  ivna.vna_name  = BYTE_GET (evna.vna_name);
11413		}
11414
11415	      vna_off += ivna.vna_next;
11416	    }
11417	  while (ivna.vna_other != vers_data && ivna.vna_next != 0);
11418
11419	  if (ivna.vna_other == vers_data)
11420	    break;
11421
11422	  offset += ivn.vn_next;
11423	}
11424      while (ivn.vn_next != 0);
11425
11426      if (ivna.vna_other == vers_data)
11427	{
11428	  *sym_info = symbol_undefined;
11429	  *vna_other = ivna.vna_other;
11430	  return (ivna.vna_name < strtab_size
11431		  ? strtab + ivna.vna_name : _("<corrupt>"));
11432	}
11433    }
11434  return NULL;
11435}
11436
11437/* Dump the symbol table.  */
11438static bfd_boolean
11439process_symbol_table (Filedata * filedata)
11440{
11441  Elf_Internal_Shdr * section;
11442  bfd_size_type nbuckets = 0;
11443  bfd_size_type nchains = 0;
11444  bfd_vma * buckets = NULL;
11445  bfd_vma * chains = NULL;
11446  bfd_vma ngnubuckets = 0;
11447  bfd_vma * gnubuckets = NULL;
11448  bfd_vma * gnuchains = NULL;
11449  bfd_vma gnusymidx = 0;
11450  bfd_size_type ngnuchains = 0;
11451
11452  if (!do_syms && !do_dyn_syms && !do_histogram)
11453    return TRUE;
11454
11455  if (dynamic_info[DT_HASH]
11456      && (do_histogram
11457	  || (do_using_dynamic
11458	      && !do_dyn_syms
11459	      && dynamic_strings != NULL)))
11460    {
11461      unsigned char nb[8];
11462      unsigned char nc[8];
11463      unsigned int hash_ent_size = 4;
11464
11465      if ((filedata->file_header.e_machine == EM_ALPHA
11466	   || filedata->file_header.e_machine == EM_S390
11467	   || filedata->file_header.e_machine == EM_S390_OLD)
11468	  && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11469	hash_ent_size = 8;
11470
11471      if (fseek (filedata->handle,
11472		 (archive_file_offset
11473		  + offset_from_vma (filedata, dynamic_info[DT_HASH],
11474				     sizeof nb + sizeof nc)),
11475		 SEEK_SET))
11476	{
11477	  error (_("Unable to seek to start of dynamic information\n"));
11478	  goto no_hash;
11479	}
11480
11481      if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11482	{
11483	  error (_("Failed to read in number of buckets\n"));
11484	  goto no_hash;
11485	}
11486
11487      if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11488	{
11489	  error (_("Failed to read in number of chains\n"));
11490	  goto no_hash;
11491	}
11492
11493      nbuckets = byte_get (nb, hash_ent_size);
11494      nchains  = byte_get (nc, hash_ent_size);
11495
11496      buckets = get_dynamic_data (filedata, nbuckets, hash_ent_size);
11497      chains  = get_dynamic_data (filedata, nchains, hash_ent_size);
11498
11499    no_hash:
11500      if (buckets == NULL || chains == NULL)
11501	{
11502	  if (do_using_dynamic)
11503	    return FALSE;
11504	  free (buckets);
11505	  free (chains);
11506	  buckets = NULL;
11507	  chains = NULL;
11508	  nbuckets = 0;
11509	  nchains = 0;
11510	}
11511    }
11512
11513  if (dynamic_info_DT_GNU_HASH
11514      && (do_histogram
11515	  || (do_using_dynamic
11516	      && !do_dyn_syms
11517	      && dynamic_strings != NULL)))
11518    {
11519      unsigned char nb[16];
11520      bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
11521      bfd_vma buckets_vma;
11522
11523      if (fseek (filedata->handle,
11524		 (archive_file_offset
11525		  + offset_from_vma (filedata, dynamic_info_DT_GNU_HASH,
11526				     sizeof nb)),
11527		 SEEK_SET))
11528	{
11529	  error (_("Unable to seek to start of dynamic information\n"));
11530	  goto no_gnu_hash;
11531	}
11532
11533      if (fread (nb, 16, 1, filedata->handle) != 1)
11534	{
11535	  error (_("Failed to read in number of buckets\n"));
11536	  goto no_gnu_hash;
11537	}
11538
11539      ngnubuckets = byte_get (nb, 4);
11540      gnusymidx = byte_get (nb + 4, 4);
11541      bitmaskwords = byte_get (nb + 8, 4);
11542      buckets_vma = dynamic_info_DT_GNU_HASH + 16;
11543      if (is_32bit_elf)
11544	buckets_vma += bitmaskwords * 4;
11545      else
11546	buckets_vma += bitmaskwords * 8;
11547
11548      if (fseek (filedata->handle,
11549		 (archive_file_offset
11550		  + offset_from_vma (filedata, buckets_vma, 4)),
11551		 SEEK_SET))
11552	{
11553	  error (_("Unable to seek to start of dynamic information\n"));
11554	  goto no_gnu_hash;
11555	}
11556
11557      gnubuckets = get_dynamic_data (filedata, ngnubuckets, 4);
11558
11559      if (gnubuckets == NULL)
11560	goto no_gnu_hash;
11561
11562      for (i = 0; i < ngnubuckets; i++)
11563	if (gnubuckets[i] != 0)
11564	  {
11565	    if (gnubuckets[i] < gnusymidx)
11566	      return FALSE;
11567
11568	    if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
11569	      maxchain = gnubuckets[i];
11570	  }
11571
11572      if (maxchain == 0xffffffff)
11573	goto no_gnu_hash;
11574
11575      maxchain -= gnusymidx;
11576
11577      if (fseek (filedata->handle,
11578		 (archive_file_offset
11579		  + offset_from_vma (filedata, buckets_vma
11580					   + 4 * (ngnubuckets + maxchain), 4)),
11581		 SEEK_SET))
11582	{
11583	  error (_("Unable to seek to start of dynamic information\n"));
11584	  goto no_gnu_hash;
11585	}
11586
11587      do
11588	{
11589	  if (fread (nb, 4, 1, filedata->handle) != 1)
11590	    {
11591	      error (_("Failed to determine last chain length\n"));
11592	      goto no_gnu_hash;
11593	    }
11594
11595	  if (maxchain + 1 == 0)
11596	    goto no_gnu_hash;
11597
11598	  ++maxchain;
11599	}
11600      while ((byte_get (nb, 4) & 1) == 0);
11601
11602      if (fseek (filedata->handle,
11603		 (archive_file_offset
11604		  + offset_from_vma (filedata, buckets_vma + 4 * ngnubuckets, 4)),
11605		 SEEK_SET))
11606	{
11607	  error (_("Unable to seek to start of dynamic information\n"));
11608	  goto no_gnu_hash;
11609	}
11610
11611      gnuchains = get_dynamic_data (filedata, maxchain, 4);
11612      ngnuchains = maxchain;
11613
11614    no_gnu_hash:
11615      if (gnuchains == NULL)
11616	{
11617	  free (gnubuckets);
11618	  gnubuckets = NULL;
11619	  ngnubuckets = 0;
11620	  if (do_using_dynamic)
11621	    return FALSE;
11622	}
11623    }
11624
11625  if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
11626      && do_syms
11627      && do_using_dynamic
11628      && dynamic_strings != NULL
11629      && dynamic_symbols != NULL)
11630    {
11631      unsigned long hn;
11632
11633      if (dynamic_info[DT_HASH])
11634	{
11635	  bfd_vma si;
11636	  char *visited;
11637
11638	  printf (_("\nSymbol table for image:\n"));
11639	  if (is_32bit_elf)
11640	    printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
11641	  else
11642	    printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
11643
11644	  visited = xcmalloc (nchains, 1);
11645	  memset (visited, 0, nchains);
11646	  for (hn = 0; hn < nbuckets; hn++)
11647	    {
11648	      for (si = buckets[hn]; si > 0; si = chains[si])
11649		{
11650		  print_dynamic_symbol (filedata, si, hn);
11651		  if (si >= nchains || visited[si])
11652		    {
11653		      error (_("histogram chain is corrupt\n"));
11654		      break;
11655		    }
11656		  visited[si] = 1;
11657		}
11658	    }
11659	  free (visited);
11660	}
11661
11662      if (dynamic_info_DT_GNU_HASH)
11663	{
11664	  printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
11665	  if (is_32bit_elf)
11666	    printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
11667	  else
11668	    printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
11669
11670	  for (hn = 0; hn < ngnubuckets; ++hn)
11671	    if (gnubuckets[hn] != 0)
11672	      {
11673		bfd_vma si = gnubuckets[hn];
11674		bfd_vma off = si - gnusymidx;
11675
11676		do
11677		  {
11678		    print_dynamic_symbol (filedata, si, hn);
11679		    si++;
11680		  }
11681		while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
11682	      }
11683	}
11684    }
11685  else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
11686	   && filedata->section_headers != NULL)
11687    {
11688      unsigned int i;
11689
11690      for (i = 0, section = filedata->section_headers;
11691	   i < filedata->file_header.e_shnum;
11692	   i++, section++)
11693	{
11694	  unsigned int si;
11695	  char * strtab = NULL;
11696	  unsigned long int strtab_size = 0;
11697	  Elf_Internal_Sym * symtab;
11698	  Elf_Internal_Sym * psym;
11699	  unsigned long num_syms;
11700
11701	  if ((section->sh_type != SHT_SYMTAB
11702	       && section->sh_type != SHT_DYNSYM)
11703	      || (!do_syms
11704		  && section->sh_type == SHT_SYMTAB))
11705	    continue;
11706
11707	  if (section->sh_entsize == 0)
11708	    {
11709	      printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
11710		      printable_section_name (filedata, section));
11711	      continue;
11712	    }
11713
11714	  num_syms = section->sh_size / section->sh_entsize;
11715	  printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
11716			    "\nSymbol table '%s' contains %lu entries:\n",
11717			    num_syms),
11718		  printable_section_name (filedata, section),
11719		  num_syms);
11720
11721	  if (is_32bit_elf)
11722	    printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
11723	  else
11724	    printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
11725
11726	  symtab = GET_ELF_SYMBOLS (filedata, section, & num_syms);
11727	  if (symtab == NULL)
11728	    continue;
11729
11730	  if (section->sh_link == filedata->file_header.e_shstrndx)
11731	    {
11732	      strtab = filedata->string_table;
11733	      strtab_size = filedata->string_table_length;
11734	    }
11735	  else if (section->sh_link < filedata->file_header.e_shnum)
11736	    {
11737	      Elf_Internal_Shdr * string_sec;
11738
11739	      string_sec = filedata->section_headers + section->sh_link;
11740
11741	      strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
11742                                          1, string_sec->sh_size,
11743                                          _("string table"));
11744	      strtab_size = strtab != NULL ? string_sec->sh_size : 0;
11745	    }
11746
11747	  for (si = 0, psym = symtab; si < num_syms; si++, psym++)
11748	    {
11749	      const char *version_string;
11750	      enum versioned_symbol_info sym_info;
11751	      unsigned short vna_other;
11752
11753	      printf ("%6d: ", si);
11754	      print_vma (psym->st_value, LONG_HEX);
11755	      putchar (' ');
11756	      print_vma (psym->st_size, DEC_5);
11757	      printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
11758	      printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
11759	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
11760		printf (" %-7s",  get_solaris_symbol_visibility (psym->st_other));
11761	      else
11762		{
11763		  unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
11764
11765		  printf (" %-7s", get_symbol_visibility (vis));
11766		  /* Check to see if any other bits in the st_other field are set.
11767		     Note - displaying this information disrupts the layout of the
11768		     table being generated, but for the moment this case is very rare.  */
11769		  if (psym->st_other ^ vis)
11770		    printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
11771		}
11772	      printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
11773	      print_symbol (25, psym->st_name < strtab_size
11774			    ? strtab + psym->st_name : _("<corrupt>"));
11775
11776	      version_string
11777		= get_symbol_version_string (filedata,
11778					     section->sh_type == SHT_DYNSYM,
11779					     strtab, strtab_size, si,
11780					     psym, &sym_info, &vna_other);
11781	      if (version_string)
11782		{
11783		  if (sym_info == symbol_undefined)
11784		    printf ("@%s (%d)", version_string, vna_other);
11785		  else
11786		    printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
11787			    version_string);
11788		}
11789
11790	      putchar ('\n');
11791
11792	      if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
11793		  && si >= section->sh_info
11794		  /* Irix 5 and 6 MIPS binaries are known to ignore this requirement.  */
11795		  && filedata->file_header.e_machine != EM_MIPS
11796		  /* Solaris binaries have been found to violate this requirement as
11797		     well.  Not sure if this is a bug or an ABI requirement.  */
11798		  && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
11799		warn (_("local symbol %u found at index >= %s's sh_info value of %u\n"),
11800		      si, printable_section_name (filedata, section), section->sh_info);
11801	    }
11802
11803	  free (symtab);
11804	  if (strtab != filedata->string_table)
11805	    free (strtab);
11806	}
11807    }
11808  else if (do_syms)
11809    printf
11810      (_("\nDynamic symbol information is not available for displaying symbols.\n"));
11811
11812  if (do_histogram && buckets != NULL)
11813    {
11814      unsigned long * lengths;
11815      unsigned long * counts;
11816      unsigned long hn;
11817      bfd_vma si;
11818      unsigned long maxlength = 0;
11819      unsigned long nzero_counts = 0;
11820      unsigned long nsyms = 0;
11821      char *visited;
11822
11823      printf (ngettext ("\nHistogram for bucket list length "
11824			"(total of %lu bucket):\n",
11825			"\nHistogram for bucket list length "
11826			"(total of %lu buckets):\n",
11827			(unsigned long) nbuckets),
11828	      (unsigned long) nbuckets);
11829
11830      lengths = (unsigned long *) calloc (nbuckets, sizeof (*lengths));
11831      if (lengths == NULL)
11832	{
11833	  error (_("Out of memory allocating space for histogram buckets\n"));
11834	  return FALSE;
11835	}
11836      visited = xcmalloc (nchains, 1);
11837      memset (visited, 0, nchains);
11838
11839      printf (_(" Length  Number     %% of total  Coverage\n"));
11840      for (hn = 0; hn < nbuckets; ++hn)
11841	{
11842	  for (si = buckets[hn]; si > 0; si = chains[si])
11843	    {
11844	      ++nsyms;
11845	      if (maxlength < ++lengths[hn])
11846		++maxlength;
11847	      if (si >= nchains || visited[si])
11848		{
11849		  error (_("histogram chain is corrupt\n"));
11850		  break;
11851		}
11852	      visited[si] = 1;
11853	    }
11854	}
11855      free (visited);
11856
11857      counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
11858      if (counts == NULL)
11859	{
11860	  free (lengths);
11861	  error (_("Out of memory allocating space for histogram counts\n"));
11862	  return FALSE;
11863	}
11864
11865      for (hn = 0; hn < nbuckets; ++hn)
11866	++counts[lengths[hn]];
11867
11868      if (nbuckets > 0)
11869	{
11870	  unsigned long i;
11871	  printf ("      0  %-10lu (%5.1f%%)\n",
11872		  counts[0], (counts[0] * 100.0) / nbuckets);
11873	  for (i = 1; i <= maxlength; ++i)
11874	    {
11875	      nzero_counts += counts[i] * i;
11876	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
11877		      i, counts[i], (counts[i] * 100.0) / nbuckets,
11878		      (nzero_counts * 100.0) / nsyms);
11879	    }
11880	}
11881
11882      free (counts);
11883      free (lengths);
11884    }
11885
11886  if (buckets != NULL)
11887    {
11888      free (buckets);
11889      free (chains);
11890    }
11891
11892  if (do_histogram && gnubuckets != NULL)
11893    {
11894      unsigned long * lengths;
11895      unsigned long * counts;
11896      unsigned long hn;
11897      unsigned long maxlength = 0;
11898      unsigned long nzero_counts = 0;
11899      unsigned long nsyms = 0;
11900
11901      printf (ngettext ("\nHistogram for `.gnu.hash' bucket list length "
11902			"(total of %lu bucket):\n",
11903			"\nHistogram for `.gnu.hash' bucket list length "
11904			"(total of %lu buckets):\n",
11905			(unsigned long) ngnubuckets),
11906	      (unsigned long) ngnubuckets);
11907
11908      lengths = (unsigned long *) calloc (ngnubuckets, sizeof (*lengths));
11909      if (lengths == NULL)
11910	{
11911	  error (_("Out of memory allocating space for gnu histogram buckets\n"));
11912	  return FALSE;
11913	}
11914
11915      printf (_(" Length  Number     %% of total  Coverage\n"));
11916
11917      for (hn = 0; hn < ngnubuckets; ++hn)
11918	if (gnubuckets[hn] != 0)
11919	  {
11920	    bfd_vma off, length = 1;
11921
11922	    for (off = gnubuckets[hn] - gnusymidx;
11923		 /* PR 17531 file: 010-77222-0.004.  */
11924		 off < ngnuchains && (gnuchains[off] & 1) == 0;
11925		 ++off)
11926	      ++length;
11927	    lengths[hn] = length;
11928	    if (length > maxlength)
11929	      maxlength = length;
11930	    nsyms += length;
11931	  }
11932
11933      counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
11934      if (counts == NULL)
11935	{
11936	  free (lengths);
11937	  error (_("Out of memory allocating space for gnu histogram counts\n"));
11938	  return FALSE;
11939	}
11940
11941      for (hn = 0; hn < ngnubuckets; ++hn)
11942	++counts[lengths[hn]];
11943
11944      if (ngnubuckets > 0)
11945	{
11946	  unsigned long j;
11947	  printf ("      0  %-10lu (%5.1f%%)\n",
11948		  counts[0], (counts[0] * 100.0) / ngnubuckets);
11949	  for (j = 1; j <= maxlength; ++j)
11950	    {
11951	      nzero_counts += counts[j] * j;
11952	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
11953		      j, counts[j], (counts[j] * 100.0) / ngnubuckets,
11954		      (nzero_counts * 100.0) / nsyms);
11955	    }
11956	}
11957
11958      free (counts);
11959      free (lengths);
11960      free (gnubuckets);
11961      free (gnuchains);
11962    }
11963
11964  return TRUE;
11965}
11966
11967static bfd_boolean
11968process_syminfo (Filedata * filedata ATTRIBUTE_UNUSED)
11969{
11970  unsigned int i;
11971
11972  if (dynamic_syminfo == NULL
11973      || !do_dynamic)
11974    /* No syminfo, this is ok.  */
11975    return TRUE;
11976
11977  /* There better should be a dynamic symbol section.  */
11978  if (dynamic_symbols == NULL || dynamic_strings == NULL)
11979    return FALSE;
11980
11981  if (dynamic_addr)
11982    printf (ngettext ("\nDynamic info segment at offset 0x%lx "
11983		      "contains %d entry:\n",
11984		      "\nDynamic info segment at offset 0x%lx "
11985		      "contains %d entries:\n",
11986		      dynamic_syminfo_nent),
11987	    dynamic_syminfo_offset, dynamic_syminfo_nent);
11988
11989  printf (_(" Num: Name                           BoundTo     Flags\n"));
11990  for (i = 0; i < dynamic_syminfo_nent; ++i)
11991    {
11992      unsigned short int flags = dynamic_syminfo[i].si_flags;
11993
11994      printf ("%4d: ", i);
11995      if (i >= num_dynamic_syms)
11996	printf (_("<corrupt index>"));
11997      else if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
11998	print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
11999      else
12000	printf (_("<corrupt: %19ld>"), dynamic_symbols[i].st_name);
12001      putchar (' ');
12002
12003      switch (dynamic_syminfo[i].si_boundto)
12004	{
12005	case SYMINFO_BT_SELF:
12006	  fputs ("SELF       ", stdout);
12007	  break;
12008	case SYMINFO_BT_PARENT:
12009	  fputs ("PARENT     ", stdout);
12010	  break;
12011	default:
12012	  if (dynamic_syminfo[i].si_boundto > 0
12013	      && dynamic_syminfo[i].si_boundto < dynamic_nent
12014	      && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
12015	    {
12016	      print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
12017	      putchar (' ' );
12018	    }
12019	  else
12020	    printf ("%-10d ", dynamic_syminfo[i].si_boundto);
12021	  break;
12022	}
12023
12024      if (flags & SYMINFO_FLG_DIRECT)
12025	printf (" DIRECT");
12026      if (flags & SYMINFO_FLG_PASSTHRU)
12027	printf (" PASSTHRU");
12028      if (flags & SYMINFO_FLG_COPY)
12029	printf (" COPY");
12030      if (flags & SYMINFO_FLG_LAZYLOAD)
12031	printf (" LAZYLOAD");
12032
12033      puts ("");
12034    }
12035
12036  return TRUE;
12037}
12038
12039#define IN_RANGE(START,END,ADDR,OFF)		\
12040  (((ADDR) >= (START)) && ((ADDR) + (OFF) < (END)))
12041
12042/* Check to see if the given reloc needs to be handled in a target specific
12043   manner.  If so then process the reloc and return TRUE otherwise return
12044   FALSE.
12045
12046   If called with reloc == NULL, then this is a signal that reloc processing
12047   for the current section has finished, and any saved state should be
12048   discarded.  */
12049
12050static bfd_boolean
12051target_specific_reloc_handling (Filedata *           filedata,
12052				Elf_Internal_Rela *  reloc,
12053				unsigned char *      start,
12054				unsigned char *      end,
12055				Elf_Internal_Sym *   symtab,
12056				unsigned long        num_syms)
12057{
12058  unsigned int reloc_type = 0;
12059  unsigned long sym_index = 0;
12060
12061  if (reloc)
12062    {
12063      reloc_type = get_reloc_type (filedata, reloc->r_info);
12064      sym_index = get_reloc_symindex (reloc->r_info);
12065    }
12066
12067  switch (filedata->file_header.e_machine)
12068    {
12069    case EM_MSP430:
12070    case EM_MSP430_OLD:
12071      {
12072	static Elf_Internal_Sym * saved_sym = NULL;
12073
12074	if (reloc == NULL)
12075	  {
12076	    saved_sym = NULL;
12077	    return TRUE;
12078	  }
12079
12080	switch (reloc_type)
12081	  {
12082	  case 10: /* R_MSP430_SYM_DIFF */
12083	    if (uses_msp430x_relocs (filedata))
12084	      break;
12085	    /* Fall through.  */
12086	  case 21: /* R_MSP430X_SYM_DIFF */
12087	    /* PR 21139.  */
12088	    if (sym_index >= num_syms)
12089	      error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
12090		     sym_index);
12091	    else
12092	      saved_sym = symtab + sym_index;
12093	    return TRUE;
12094
12095	  case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
12096	  case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
12097	    goto handle_sym_diff;
12098
12099	  case 5: /* R_MSP430_16_BYTE */
12100	  case 9: /* R_MSP430_8 */
12101	    if (uses_msp430x_relocs (filedata))
12102	      break;
12103	    goto handle_sym_diff;
12104
12105	  case 2: /* R_MSP430_ABS16 */
12106	  case 15: /* R_MSP430X_ABS16 */
12107	    if (! uses_msp430x_relocs (filedata))
12108	      break;
12109	    goto handle_sym_diff;
12110
12111	  handle_sym_diff:
12112	    if (saved_sym != NULL)
12113	      {
12114		int reloc_size = reloc_type == 1 ? 4 : 2;
12115		bfd_vma value;
12116
12117		if (sym_index >= num_syms)
12118		  error (_("MSP430 reloc contains invalid symbol index %lu\n"),
12119			 sym_index);
12120		else
12121		  {
12122		    value = reloc->r_addend + (symtab[sym_index].st_value
12123					       - saved_sym->st_value);
12124
12125		    if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
12126		      byte_put (start + reloc->r_offset, value, reloc_size);
12127		    else
12128		      /* PR 21137 */
12129		      error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
12130			     (long) reloc->r_offset);
12131		  }
12132
12133		saved_sym = NULL;
12134		return TRUE;
12135	      }
12136	    break;
12137
12138	  default:
12139	    if (saved_sym != NULL)
12140	      error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
12141	    break;
12142	  }
12143	break;
12144      }
12145
12146    case EM_MN10300:
12147    case EM_CYGNUS_MN10300:
12148      {
12149	static Elf_Internal_Sym * saved_sym = NULL;
12150
12151	if (reloc == NULL)
12152	  {
12153	    saved_sym = NULL;
12154	    return TRUE;
12155	  }
12156
12157	switch (reloc_type)
12158	  {
12159	  case 34: /* R_MN10300_ALIGN */
12160	    return TRUE;
12161	  case 33: /* R_MN10300_SYM_DIFF */
12162	    if (sym_index >= num_syms)
12163	      error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
12164		     sym_index);
12165	    else
12166	      saved_sym = symtab + sym_index;
12167	    return TRUE;
12168
12169	  case 1: /* R_MN10300_32 */
12170	  case 2: /* R_MN10300_16 */
12171	    if (saved_sym != NULL)
12172	      {
12173		int reloc_size = reloc_type == 1 ? 4 : 2;
12174		bfd_vma value;
12175
12176		if (sym_index >= num_syms)
12177		  error (_("MN10300 reloc contains invalid symbol index %lu\n"),
12178			 sym_index);
12179		else
12180		  {
12181		    value = reloc->r_addend + (symtab[sym_index].st_value
12182					       - saved_sym->st_value);
12183
12184		    if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
12185		      byte_put (start + reloc->r_offset, value, reloc_size);
12186		    else
12187		      error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
12188			     (long) reloc->r_offset);
12189		  }
12190
12191		saved_sym = NULL;
12192		return TRUE;
12193	      }
12194	    break;
12195	  default:
12196	    if (saved_sym != NULL)
12197	      error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
12198	    break;
12199	  }
12200	break;
12201      }
12202
12203    case EM_RL78:
12204      {
12205	static bfd_vma saved_sym1 = 0;
12206	static bfd_vma saved_sym2 = 0;
12207	static bfd_vma value;
12208
12209	if (reloc == NULL)
12210	  {
12211	    saved_sym1 = saved_sym2 = 0;
12212	    return TRUE;
12213	  }
12214
12215	switch (reloc_type)
12216	  {
12217	  case 0x80: /* R_RL78_SYM.  */
12218	    saved_sym1 = saved_sym2;
12219	    if (sym_index >= num_syms)
12220	      error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
12221		     sym_index);
12222	    else
12223	      {
12224		saved_sym2 = symtab[sym_index].st_value;
12225		saved_sym2 += reloc->r_addend;
12226	      }
12227	    return TRUE;
12228
12229	  case 0x83: /* R_RL78_OPsub.  */
12230	    value = saved_sym1 - saved_sym2;
12231	    saved_sym2 = saved_sym1 = 0;
12232	    return TRUE;
12233	    break;
12234
12235	  case 0x41: /* R_RL78_ABS32.  */
12236	    if (IN_RANGE (start, end, start + reloc->r_offset, 4))
12237	      byte_put (start + reloc->r_offset, value, 4);
12238	    else
12239	      error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
12240		     (long) reloc->r_offset);
12241	    value = 0;
12242	    return TRUE;
12243
12244	  case 0x43: /* R_RL78_ABS16.  */
12245	    if (IN_RANGE (start, end, start + reloc->r_offset, 2))
12246	      byte_put (start + reloc->r_offset, value, 2);
12247	    else
12248	      error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
12249		     (long) reloc->r_offset);
12250	    value = 0;
12251	    return TRUE;
12252
12253	  default:
12254	    break;
12255	  }
12256	break;
12257      }
12258    }
12259
12260  return FALSE;
12261}
12262
12263/* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
12264   DWARF debug sections.  This is a target specific test.  Note - we do not
12265   go through the whole including-target-headers-multiple-times route, (as
12266   we have already done with <elf/h8.h>) because this would become very
12267   messy and even then this function would have to contain target specific
12268   information (the names of the relocs instead of their numeric values).
12269   FIXME: This is not the correct way to solve this problem.  The proper way
12270   is to have target specific reloc sizing and typing functions created by
12271   the reloc-macros.h header, in the same way that it already creates the
12272   reloc naming functions.  */
12273
12274static bfd_boolean
12275is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12276{
12277  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12278  switch (filedata->file_header.e_machine)
12279    {
12280    case EM_386:
12281    case EM_IAMCU:
12282      return reloc_type == 1; /* R_386_32.  */
12283    case EM_68K:
12284      return reloc_type == 1; /* R_68K_32.  */
12285    case EM_860:
12286      return reloc_type == 1; /* R_860_32.  */
12287    case EM_960:
12288      return reloc_type == 2; /* R_960_32.  */
12289    case EM_AARCH64:
12290      return (reloc_type == 258
12291	      || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
12292    case EM_ADAPTEVA_EPIPHANY:
12293      return reloc_type == 3;
12294    case EM_ALPHA:
12295      return reloc_type == 1; /* R_ALPHA_REFLONG.  */
12296    case EM_ARC:
12297      return reloc_type == 1; /* R_ARC_32.  */
12298    case EM_ARC_COMPACT:
12299    case EM_ARC_COMPACT2:
12300      return reloc_type == 4; /* R_ARC_32.  */
12301    case EM_ARM:
12302      return reloc_type == 2; /* R_ARM_ABS32 */
12303    case EM_AVR_OLD:
12304    case EM_AVR:
12305      return reloc_type == 1;
12306    case EM_BLACKFIN:
12307      return reloc_type == 0x12; /* R_byte4_data.  */
12308    case EM_CRIS:
12309      return reloc_type == 3; /* R_CRIS_32.  */
12310    case EM_CR16:
12311      return reloc_type == 3; /* R_CR16_NUM32.  */
12312    case EM_CRX:
12313      return reloc_type == 15; /* R_CRX_NUM32.  */
12314    case EM_CYGNUS_FRV:
12315      return reloc_type == 1;
12316    case EM_CYGNUS_D10V:
12317    case EM_D10V:
12318      return reloc_type == 6; /* R_D10V_32.  */
12319    case EM_CYGNUS_D30V:
12320    case EM_D30V:
12321      return reloc_type == 12; /* R_D30V_32_NORMAL.  */
12322    case EM_DLX:
12323      return reloc_type == 3; /* R_DLX_RELOC_32.  */
12324    case EM_CYGNUS_FR30:
12325    case EM_FR30:
12326      return reloc_type == 3; /* R_FR30_32.  */
12327    case EM_FT32:
12328      return reloc_type == 1; /* R_FT32_32.  */
12329    case EM_H8S:
12330    case EM_H8_300:
12331    case EM_H8_300H:
12332      return reloc_type == 1; /* R_H8_DIR32.  */
12333    case EM_IA_64:
12334      return (reloc_type == 0x64    /* R_IA64_SECREL32MSB.  */
12335	      || reloc_type == 0x65 /* R_IA64_SECREL32LSB.  */
12336	      || reloc_type == 0x24 /* R_IA64_DIR32MSB.  */
12337	      || reloc_type == 0x25 /* R_IA64_DIR32LSB.  */);
12338    case EM_IP2K_OLD:
12339    case EM_IP2K:
12340      return reloc_type == 2; /* R_IP2K_32.  */
12341    case EM_IQ2000:
12342      return reloc_type == 2; /* R_IQ2000_32.  */
12343    case EM_LATTICEMICO32:
12344      return reloc_type == 3; /* R_LM32_32.  */
12345    case EM_M32C_OLD:
12346    case EM_M32C:
12347      return reloc_type == 3; /* R_M32C_32.  */
12348    case EM_M32R:
12349      return reloc_type == 34; /* R_M32R_32_RELA.  */
12350    case EM_68HC11:
12351    case EM_68HC12:
12352      return reloc_type == 6; /* R_M68HC11_32.  */
12353    case EM_S12Z:
12354      return reloc_type == 6; /* R_S12Z_EXT32.  */
12355    case EM_MCORE:
12356      return reloc_type == 1; /* R_MCORE_ADDR32.  */
12357    case EM_CYGNUS_MEP:
12358      return reloc_type == 4; /* R_MEP_32.  */
12359    case EM_METAG:
12360      return reloc_type == 2; /* R_METAG_ADDR32.  */
12361    case EM_MICROBLAZE:
12362      return reloc_type == 1; /* R_MICROBLAZE_32.  */
12363    case EM_MIPS:
12364      return reloc_type == 2; /* R_MIPS_32.  */
12365    case EM_MMIX:
12366      return reloc_type == 4; /* R_MMIX_32.  */
12367    case EM_CYGNUS_MN10200:
12368    case EM_MN10200:
12369      return reloc_type == 1; /* R_MN10200_32.  */
12370    case EM_CYGNUS_MN10300:
12371    case EM_MN10300:
12372      return reloc_type == 1; /* R_MN10300_32.  */
12373    case EM_MOXIE:
12374      return reloc_type == 1; /* R_MOXIE_32.  */
12375    case EM_MSP430_OLD:
12376    case EM_MSP430:
12377      return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32.  */
12378    case EM_MT:
12379      return reloc_type == 2; /* R_MT_32.  */
12380    case EM_NDS32:
12381      return reloc_type == 20; /* R_NDS32_RELA.  */
12382    case EM_ALTERA_NIOS2:
12383      return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32.  */
12384    case EM_NIOS32:
12385      return reloc_type == 1; /* R_NIOS_32.  */
12386    case EM_OR1K:
12387      return reloc_type == 1; /* R_OR1K_32.  */
12388    case EM_PARISC:
12389      return (reloc_type == 1 /* R_PARISC_DIR32.  */
12390	      || reloc_type == 2 /* R_PARISC_DIR21L.  */
12391	      || reloc_type == 41); /* R_PARISC_SECREL32.  */
12392    case EM_PJ:
12393    case EM_PJ_OLD:
12394      return reloc_type == 1; /* R_PJ_DATA_DIR32.  */
12395    case EM_PPC64:
12396      return reloc_type == 1; /* R_PPC64_ADDR32.  */
12397    case EM_PPC:
12398      return reloc_type == 1; /* R_PPC_ADDR32.  */
12399    case EM_TI_PRU:
12400      return reloc_type == 11; /* R_PRU_BFD_RELOC_32.  */
12401    case EM_RISCV:
12402      return reloc_type == 1; /* R_RISCV_32.  */
12403    case EM_RL78:
12404      return reloc_type == 1; /* R_RL78_DIR32.  */
12405    case EM_RX:
12406      return reloc_type == 1; /* R_RX_DIR32.  */
12407    case EM_S370:
12408      return reloc_type == 1; /* R_I370_ADDR31.  */
12409    case EM_S390_OLD:
12410    case EM_S390:
12411      return reloc_type == 4; /* R_S390_32.  */
12412    case EM_SCORE:
12413      return reloc_type == 8; /* R_SCORE_ABS32.  */
12414    case EM_SH:
12415      return reloc_type == 1; /* R_SH_DIR32.  */
12416    case EM_SPARC32PLUS:
12417    case EM_SPARCV9:
12418    case EM_SPARC:
12419      return reloc_type == 3 /* R_SPARC_32.  */
12420	|| reloc_type == 23; /* R_SPARC_UA32.  */
12421    case EM_SPU:
12422      return reloc_type == 6; /* R_SPU_ADDR32 */
12423    case EM_TI_C6000:
12424      return reloc_type == 1; /* R_C6000_ABS32.  */
12425    case EM_TILEGX:
12426      return reloc_type == 2; /* R_TILEGX_32.  */
12427    case EM_TILEPRO:
12428      return reloc_type == 1; /* R_TILEPRO_32.  */
12429    case EM_CYGNUS_V850:
12430    case EM_V850:
12431      return reloc_type == 6; /* R_V850_ABS32.  */
12432    case EM_V800:
12433      return reloc_type == 0x33; /* R_V810_WORD.  */
12434    case EM_VAX:
12435      return reloc_type == 1; /* R_VAX_32.  */
12436    case EM_VISIUM:
12437      return reloc_type == 3;  /* R_VISIUM_32. */
12438    case EM_WEBASSEMBLY:
12439      return reloc_type == 1;  /* R_WASM32_32.  */
12440    case EM_X86_64:
12441    case EM_L1OM:
12442    case EM_K1OM:
12443      return reloc_type == 10; /* R_X86_64_32.  */
12444    case EM_XC16X:
12445    case EM_C166:
12446      return reloc_type == 3; /* R_XC16C_ABS_32.  */
12447    case EM_XGATE:
12448      return reloc_type == 4; /* R_XGATE_32.  */
12449    case EM_XSTORMY16:
12450      return reloc_type == 1; /* R_XSTROMY16_32.  */
12451    case EM_XTENSA_OLD:
12452    case EM_XTENSA:
12453      return reloc_type == 1; /* R_XTENSA_32.  */
12454    default:
12455      {
12456	static unsigned int prev_warn = 0;
12457
12458	/* Avoid repeating the same warning multiple times.  */
12459	if (prev_warn != filedata->file_header.e_machine)
12460	  error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
12461		 filedata->file_header.e_machine);
12462	prev_warn = filedata->file_header.e_machine;
12463	return FALSE;
12464      }
12465    }
12466}
12467
12468/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12469   a 32-bit pc-relative RELA relocation used in DWARF debug sections.  */
12470
12471static bfd_boolean
12472is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
12473{
12474  switch (filedata->file_header.e_machine)
12475  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12476    {
12477    case EM_386:
12478    case EM_IAMCU:
12479      return reloc_type == 2;  /* R_386_PC32.  */
12480    case EM_68K:
12481      return reloc_type == 4;  /* R_68K_PC32.  */
12482    case EM_AARCH64:
12483      return reloc_type == 261; /* R_AARCH64_PREL32 */
12484    case EM_ADAPTEVA_EPIPHANY:
12485      return reloc_type == 6;
12486    case EM_ALPHA:
12487      return reloc_type == 10; /* R_ALPHA_SREL32.  */
12488    case EM_ARC_COMPACT:
12489    case EM_ARC_COMPACT2:
12490      return reloc_type == 49; /* R_ARC_32_PCREL.  */
12491    case EM_ARM:
12492      return reloc_type == 3;  /* R_ARM_REL32 */
12493    case EM_AVR_OLD:
12494    case EM_AVR:
12495      return reloc_type == 36; /* R_AVR_32_PCREL.  */
12496    case EM_MICROBLAZE:
12497      return reloc_type == 2;  /* R_MICROBLAZE_32_PCREL.  */
12498    case EM_OR1K:
12499      return reloc_type == 9; /* R_OR1K_32_PCREL.  */
12500    case EM_PARISC:
12501      return reloc_type == 9;  /* R_PARISC_PCREL32.  */
12502    case EM_PPC:
12503      return reloc_type == 26; /* R_PPC_REL32.  */
12504    case EM_PPC64:
12505      return reloc_type == 26; /* R_PPC64_REL32.  */
12506    case EM_S390_OLD:
12507    case EM_S390:
12508      return reloc_type == 5;  /* R_390_PC32.  */
12509    case EM_SH:
12510      return reloc_type == 2;  /* R_SH_REL32.  */
12511    case EM_SPARC32PLUS:
12512    case EM_SPARCV9:
12513    case EM_SPARC:
12514      return reloc_type == 6;  /* R_SPARC_DISP32.  */
12515    case EM_SPU:
12516      return reloc_type == 13; /* R_SPU_REL32.  */
12517    case EM_TILEGX:
12518      return reloc_type == 6; /* R_TILEGX_32_PCREL.  */
12519    case EM_TILEPRO:
12520      return reloc_type == 4; /* R_TILEPRO_32_PCREL.  */
12521    case EM_VISIUM:
12522      return reloc_type == 6;  /* R_VISIUM_32_PCREL */
12523    case EM_X86_64:
12524    case EM_L1OM:
12525    case EM_K1OM:
12526      return reloc_type == 2;  /* R_X86_64_PC32.  */
12527    case EM_VAX:
12528      return reloc_type == 4;  /* R_VAX_PCREL32.  */
12529    case EM_XTENSA_OLD:
12530    case EM_XTENSA:
12531      return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
12532    default:
12533      /* Do not abort or issue an error message here.  Not all targets use
12534	 pc-relative 32-bit relocs in their DWARF debug information and we
12535	 have already tested for target coverage in is_32bit_abs_reloc.  A
12536	 more helpful warning message will be generated by apply_relocations
12537	 anyway, so just return.  */
12538      return FALSE;
12539    }
12540}
12541
12542/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12543   a 64-bit absolute RELA relocation used in DWARF debug sections.  */
12544
12545static bfd_boolean
12546is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12547{
12548  switch (filedata->file_header.e_machine)
12549    {
12550    case EM_AARCH64:
12551      return reloc_type == 257;	/* R_AARCH64_ABS64.  */
12552    case EM_ALPHA:
12553      return reloc_type == 2; /* R_ALPHA_REFQUAD.  */
12554    case EM_IA_64:
12555      return (reloc_type == 0x26    /* R_IA64_DIR64MSB.  */
12556	      || reloc_type == 0x27 /* R_IA64_DIR64LSB.  */);
12557    case EM_PARISC:
12558      return reloc_type == 80; /* R_PARISC_DIR64.  */
12559    case EM_PPC64:
12560      return reloc_type == 38; /* R_PPC64_ADDR64.  */
12561    case EM_RISCV:
12562      return reloc_type == 2; /* R_RISCV_64.  */
12563    case EM_SPARC32PLUS:
12564    case EM_SPARCV9:
12565    case EM_SPARC:
12566      return reloc_type == 32 /* R_SPARC_64.  */
12567	|| reloc_type == 54; /* R_SPARC_UA64.  */
12568    case EM_X86_64:
12569    case EM_L1OM:
12570    case EM_K1OM:
12571      return reloc_type == 1; /* R_X86_64_64.  */
12572    case EM_S390_OLD:
12573    case EM_S390:
12574      return reloc_type == 22;	/* R_S390_64.  */
12575    case EM_TILEGX:
12576      return reloc_type == 1; /* R_TILEGX_64.  */
12577    case EM_MIPS:
12578      return reloc_type == 18;	/* R_MIPS_64.  */
12579    default:
12580      return FALSE;
12581    }
12582}
12583
12584/* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
12585   a 64-bit pc-relative RELA relocation used in DWARF debug sections.  */
12586
12587static bfd_boolean
12588is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
12589{
12590  switch (filedata->file_header.e_machine)
12591    {
12592    case EM_AARCH64:
12593      return reloc_type == 260;	/* R_AARCH64_PREL64.  */
12594    case EM_ALPHA:
12595      return reloc_type == 11; /* R_ALPHA_SREL64.  */
12596    case EM_IA_64:
12597      return (reloc_type == 0x4e    /* R_IA64_PCREL64MSB.  */
12598	      || reloc_type == 0x4f /* R_IA64_PCREL64LSB.  */);
12599    case EM_PARISC:
12600      return reloc_type == 72; /* R_PARISC_PCREL64.  */
12601    case EM_PPC64:
12602      return reloc_type == 44; /* R_PPC64_REL64.  */
12603    case EM_SPARC32PLUS:
12604    case EM_SPARCV9:
12605    case EM_SPARC:
12606      return reloc_type == 46; /* R_SPARC_DISP64.  */
12607    case EM_X86_64:
12608    case EM_L1OM:
12609    case EM_K1OM:
12610      return reloc_type == 24; /* R_X86_64_PC64.  */
12611    case EM_S390_OLD:
12612    case EM_S390:
12613      return reloc_type == 23;	/* R_S390_PC64.  */
12614    case EM_TILEGX:
12615      return reloc_type == 5;  /* R_TILEGX_64_PCREL.  */
12616    default:
12617      return FALSE;
12618    }
12619}
12620
12621/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12622   a 24-bit absolute RELA relocation used in DWARF debug sections.  */
12623
12624static bfd_boolean
12625is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12626{
12627  switch (filedata->file_header.e_machine)
12628    {
12629    case EM_CYGNUS_MN10200:
12630    case EM_MN10200:
12631      return reloc_type == 4; /* R_MN10200_24.  */
12632    case EM_FT32:
12633      return reloc_type == 5; /* R_FT32_20.  */
12634    default:
12635      return FALSE;
12636    }
12637}
12638
12639/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12640   a 16-bit absolute RELA relocation used in DWARF debug sections.  */
12641
12642static bfd_boolean
12643is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12644{
12645  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12646  switch (filedata->file_header.e_machine)
12647    {
12648    case EM_ARC:
12649    case EM_ARC_COMPACT:
12650    case EM_ARC_COMPACT2:
12651      return reloc_type == 2; /* R_ARC_16.  */
12652    case EM_ADAPTEVA_EPIPHANY:
12653      return reloc_type == 5;
12654    case EM_AVR_OLD:
12655    case EM_AVR:
12656      return reloc_type == 4; /* R_AVR_16.  */
12657    case EM_CYGNUS_D10V:
12658    case EM_D10V:
12659      return reloc_type == 3; /* R_D10V_16.  */
12660    case EM_FT32:
12661      return reloc_type == 2; /* R_FT32_16.  */
12662    case EM_H8S:
12663    case EM_H8_300:
12664    case EM_H8_300H:
12665      return reloc_type == R_H8_DIR16;
12666    case EM_IP2K_OLD:
12667    case EM_IP2K:
12668      return reloc_type == 1; /* R_IP2K_16.  */
12669    case EM_M32C_OLD:
12670    case EM_M32C:
12671      return reloc_type == 1; /* R_M32C_16 */
12672    case EM_CYGNUS_MN10200:
12673    case EM_MN10200:
12674      return reloc_type == 2; /* R_MN10200_16.  */
12675    case EM_CYGNUS_MN10300:
12676    case EM_MN10300:
12677      return reloc_type == 2; /* R_MN10300_16.  */
12678    case EM_MSP430:
12679      if (uses_msp430x_relocs (filedata))
12680	return reloc_type == 2; /* R_MSP430_ABS16.  */
12681      /* Fall through.  */
12682    case EM_MSP430_OLD:
12683      return reloc_type == 5; /* R_MSP430_16_BYTE.  */
12684    case EM_NDS32:
12685      return reloc_type == 19; /* R_NDS32_RELA.  */
12686    case EM_ALTERA_NIOS2:
12687      return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16.  */
12688    case EM_NIOS32:
12689      return reloc_type == 9; /* R_NIOS_16.  */
12690    case EM_OR1K:
12691      return reloc_type == 2; /* R_OR1K_16.  */
12692    case EM_RISCV:
12693      return reloc_type == 55; /* R_RISCV_SET16.  */
12694    case EM_TI_PRU:
12695      return reloc_type == 8; /* R_PRU_BFD_RELOC_16.  */
12696    case EM_TI_C6000:
12697      return reloc_type == 2; /* R_C6000_ABS16.  */
12698    case EM_VISIUM:
12699      return reloc_type == 2; /* R_VISIUM_16. */
12700    case EM_XC16X:
12701    case EM_C166:
12702      return reloc_type == 2; /* R_XC16C_ABS_16.  */
12703    case EM_XGATE:
12704      return reloc_type == 3; /* R_XGATE_16.  */
12705    default:
12706      return FALSE;
12707    }
12708}
12709
12710/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12711   a 8-bit absolute RELA relocation used in DWARF debug sections.  */
12712
12713static bfd_boolean
12714is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12715{
12716  switch (filedata->file_header.e_machine)
12717    {
12718    case EM_RISCV:
12719      return reloc_type == 54; /* R_RISCV_SET8.  */
12720    default:
12721      return FALSE;
12722    }
12723}
12724
12725/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12726   a 6-bit absolute RELA relocation used in DWARF debug sections.  */
12727
12728static bfd_boolean
12729is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12730{
12731  switch (filedata->file_header.e_machine)
12732    {
12733    case EM_RISCV:
12734      return reloc_type == 53; /* R_RISCV_SET6.  */
12735    default:
12736      return FALSE;
12737    }
12738}
12739
12740/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12741   a 32-bit inplace add RELA relocation used in DWARF debug sections.  */
12742
12743static bfd_boolean
12744is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
12745{
12746  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12747  switch (filedata->file_header.e_machine)
12748    {
12749    case EM_RISCV:
12750      return reloc_type == 35; /* R_RISCV_ADD32.  */
12751    default:
12752      return FALSE;
12753    }
12754}
12755
12756/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12757   a 32-bit inplace sub RELA relocation used in DWARF debug sections.  */
12758
12759static bfd_boolean
12760is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
12761{
12762  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12763  switch (filedata->file_header.e_machine)
12764    {
12765    case EM_RISCV:
12766      return reloc_type == 39; /* R_RISCV_SUB32.  */
12767    default:
12768      return FALSE;
12769    }
12770}
12771
12772/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12773   a 64-bit inplace add RELA relocation used in DWARF debug sections.  */
12774
12775static bfd_boolean
12776is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
12777{
12778  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12779  switch (filedata->file_header.e_machine)
12780    {
12781    case EM_RISCV:
12782      return reloc_type == 36; /* R_RISCV_ADD64.  */
12783    default:
12784      return FALSE;
12785    }
12786}
12787
12788/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12789   a 64-bit inplace sub RELA relocation used in DWARF debug sections.  */
12790
12791static bfd_boolean
12792is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
12793{
12794  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12795  switch (filedata->file_header.e_machine)
12796    {
12797    case EM_RISCV:
12798      return reloc_type == 40; /* R_RISCV_SUB64.  */
12799    default:
12800      return FALSE;
12801    }
12802}
12803
12804/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12805   a 16-bit inplace add RELA relocation used in DWARF debug sections.  */
12806
12807static bfd_boolean
12808is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
12809{
12810  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12811  switch (filedata->file_header.e_machine)
12812    {
12813    case EM_RISCV:
12814      return reloc_type == 34; /* R_RISCV_ADD16.  */
12815    default:
12816      return FALSE;
12817    }
12818}
12819
12820/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12821   a 16-bit inplace sub RELA relocation used in DWARF debug sections.  */
12822
12823static bfd_boolean
12824is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
12825{
12826  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12827  switch (filedata->file_header.e_machine)
12828    {
12829    case EM_RISCV:
12830      return reloc_type == 38; /* R_RISCV_SUB16.  */
12831    default:
12832      return FALSE;
12833    }
12834}
12835
12836/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12837   a 8-bit inplace add RELA relocation used in DWARF debug sections.  */
12838
12839static bfd_boolean
12840is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
12841{
12842  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12843  switch (filedata->file_header.e_machine)
12844    {
12845    case EM_RISCV:
12846      return reloc_type == 33; /* R_RISCV_ADD8.  */
12847    default:
12848      return FALSE;
12849    }
12850}
12851
12852/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12853   a 8-bit inplace sub RELA relocation used in DWARF debug sections.  */
12854
12855static bfd_boolean
12856is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
12857{
12858  /* Please keep this table alpha-sorted for ease of visual lookup.  */
12859  switch (filedata->file_header.e_machine)
12860    {
12861    case EM_RISCV:
12862      return reloc_type == 37; /* R_RISCV_SUB8.  */
12863    default:
12864      return FALSE;
12865    }
12866}
12867
12868/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12869   a 6-bit inplace sub RELA relocation used in DWARF debug sections.  */
12870
12871static bfd_boolean
12872is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
12873{
12874  switch (filedata->file_header.e_machine)
12875    {
12876    case EM_RISCV:
12877      return reloc_type == 52; /* R_RISCV_SUB6.  */
12878    default:
12879      return FALSE;
12880    }
12881}
12882
12883/* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
12884   relocation entries (possibly formerly used for SHT_GROUP sections).  */
12885
12886static bfd_boolean
12887is_none_reloc (Filedata * filedata, unsigned int reloc_type)
12888{
12889  switch (filedata->file_header.e_machine)
12890    {
12891    case EM_386:     /* R_386_NONE.  */
12892    case EM_68K:     /* R_68K_NONE.  */
12893    case EM_ADAPTEVA_EPIPHANY:
12894    case EM_ALPHA:   /* R_ALPHA_NONE.  */
12895    case EM_ALTERA_NIOS2: /* R_NIOS2_NONE.  */
12896    case EM_ARC:     /* R_ARC_NONE.  */
12897    case EM_ARC_COMPACT2: /* R_ARC_NONE.  */
12898    case EM_ARC_COMPACT: /* R_ARC_NONE.  */
12899    case EM_ARM:     /* R_ARM_NONE.  */
12900    case EM_C166:    /* R_XC16X_NONE.  */
12901    case EM_CRIS:    /* R_CRIS_NONE.  */
12902    case EM_FT32:    /* R_FT32_NONE.  */
12903    case EM_IA_64:   /* R_IA64_NONE.  */
12904    case EM_K1OM:    /* R_X86_64_NONE.  */
12905    case EM_L1OM:    /* R_X86_64_NONE.  */
12906    case EM_M32R:    /* R_M32R_NONE.  */
12907    case EM_MIPS:    /* R_MIPS_NONE.  */
12908    case EM_MN10300: /* R_MN10300_NONE.  */
12909    case EM_MOXIE:   /* R_MOXIE_NONE.  */
12910    case EM_NIOS32:  /* R_NIOS_NONE.  */
12911    case EM_OR1K:    /* R_OR1K_NONE. */
12912    case EM_PARISC:  /* R_PARISC_NONE.  */
12913    case EM_PPC64:   /* R_PPC64_NONE.  */
12914    case EM_PPC:     /* R_PPC_NONE.  */
12915    case EM_RISCV:   /* R_RISCV_NONE.  */
12916    case EM_S390:    /* R_390_NONE.  */
12917    case EM_S390_OLD:
12918    case EM_SH:      /* R_SH_NONE.  */
12919    case EM_SPARC32PLUS:
12920    case EM_SPARC:   /* R_SPARC_NONE.  */
12921    case EM_SPARCV9:
12922    case EM_TILEGX:  /* R_TILEGX_NONE.  */
12923    case EM_TILEPRO: /* R_TILEPRO_NONE.  */
12924    case EM_TI_C6000:/* R_C6000_NONE.  */
12925    case EM_X86_64:  /* R_X86_64_NONE.  */
12926    case EM_XC16X:
12927    case EM_WEBASSEMBLY: /* R_WASM32_NONE.  */
12928      return reloc_type == 0;
12929
12930    case EM_AARCH64:
12931      return reloc_type == 0 || reloc_type == 256;
12932    case EM_AVR_OLD:
12933    case EM_AVR:
12934      return (reloc_type == 0 /* R_AVR_NONE.  */
12935	      || reloc_type == 30 /* R_AVR_DIFF8.  */
12936	      || reloc_type == 31 /* R_AVR_DIFF16.  */
12937	      || reloc_type == 32 /* R_AVR_DIFF32.  */);
12938    case EM_METAG:
12939      return reloc_type == 3; /* R_METAG_NONE.  */
12940    case EM_NDS32:
12941      return (reloc_type == 0       /* R_XTENSA_NONE.  */
12942	      || reloc_type == 204  /* R_NDS32_DIFF8.  */
12943	      || reloc_type == 205  /* R_NDS32_DIFF16.  */
12944	      || reloc_type == 206  /* R_NDS32_DIFF32.  */
12945	      || reloc_type == 207  /* R_NDS32_ULEB128.  */);
12946    case EM_TI_PRU:
12947      return (reloc_type == 0       /* R_PRU_NONE.  */
12948	      || reloc_type == 65   /* R_PRU_DIFF8.  */
12949	      || reloc_type == 66   /* R_PRU_DIFF16.  */
12950	      || reloc_type == 67   /* R_PRU_DIFF32.  */);
12951    case EM_XTENSA_OLD:
12952    case EM_XTENSA:
12953      return (reloc_type == 0      /* R_XTENSA_NONE.  */
12954	      || reloc_type == 17  /* R_XTENSA_DIFF8.  */
12955	      || reloc_type == 18  /* R_XTENSA_DIFF16.  */
12956	      || reloc_type == 19  /* R_XTENSA_DIFF32.  */);
12957    }
12958  return FALSE;
12959}
12960
12961/* Returns TRUE if there is a relocation against
12962   section NAME at OFFSET bytes.  */
12963
12964bfd_boolean
12965reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
12966{
12967  Elf_Internal_Rela * relocs;
12968  Elf_Internal_Rela * rp;
12969
12970  if (dsec == NULL || dsec->reloc_info == NULL)
12971    return FALSE;
12972
12973  relocs = (Elf_Internal_Rela *) dsec->reloc_info;
12974
12975  for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
12976    if (rp->r_offset == offset)
12977      return TRUE;
12978
12979   return FALSE;
12980}
12981
12982/* Apply relocations to a section.
12983   Returns TRUE upon success, FALSE otherwise.
12984   If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
12985   It is then the caller's responsibility to free them.  NUM_RELOCS_RETURN
12986   will be set to the number of relocs loaded.
12987
12988   Note: So far support has been added only for those relocations
12989   which can be found in debug sections. FIXME: Add support for
12990   more relocations ?  */
12991
12992static bfd_boolean
12993apply_relocations (Filedata *                 filedata,
12994		   const Elf_Internal_Shdr *  section,
12995		   unsigned char *            start,
12996		   bfd_size_type              size,
12997		   void **                    relocs_return,
12998		   unsigned long *            num_relocs_return)
12999{
13000  Elf_Internal_Shdr * relsec;
13001  unsigned char * end = start + size;
13002  bfd_boolean res = TRUE;
13003
13004  if (relocs_return != NULL)
13005    {
13006      * (Elf_Internal_Rela **) relocs_return = NULL;
13007      * num_relocs_return = 0;
13008    }
13009
13010  if (filedata->file_header.e_type != ET_REL)
13011    /* No relocs to apply.  */
13012    return TRUE;
13013
13014  /* Find the reloc section associated with the section.  */
13015  for (relsec = filedata->section_headers;
13016       relsec < filedata->section_headers + filedata->file_header.e_shnum;
13017       ++relsec)
13018    {
13019      bfd_boolean is_rela;
13020      unsigned long num_relocs;
13021      Elf_Internal_Rela * relocs;
13022      Elf_Internal_Rela * rp;
13023      Elf_Internal_Shdr * symsec;
13024      Elf_Internal_Sym * symtab;
13025      unsigned long num_syms;
13026      Elf_Internal_Sym * sym;
13027
13028      if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
13029	  || relsec->sh_info >= filedata->file_header.e_shnum
13030	  || filedata->section_headers + relsec->sh_info != section
13031	  || relsec->sh_size == 0
13032	  || relsec->sh_link >= filedata->file_header.e_shnum)
13033	continue;
13034
13035      is_rela = relsec->sh_type == SHT_RELA;
13036
13037      if (is_rela)
13038	{
13039	  if (!slurp_rela_relocs (filedata, relsec->sh_offset,
13040                                  relsec->sh_size, & relocs, & num_relocs))
13041	    return FALSE;
13042	}
13043      else
13044	{
13045	  if (!slurp_rel_relocs (filedata, relsec->sh_offset,
13046                                 relsec->sh_size, & relocs, & num_relocs))
13047	    return FALSE;
13048	}
13049
13050      /* SH uses RELA but uses in place value instead of the addend field.  */
13051      if (filedata->file_header.e_machine == EM_SH)
13052	is_rela = FALSE;
13053
13054      symsec = filedata->section_headers + relsec->sh_link;
13055      if (symsec->sh_type != SHT_SYMTAB
13056	  && symsec->sh_type != SHT_DYNSYM)
13057	return FALSE;
13058      symtab = GET_ELF_SYMBOLS (filedata, symsec, & num_syms);
13059
13060      for (rp = relocs; rp < relocs + num_relocs; ++rp)
13061	{
13062	  bfd_vma         addend;
13063	  unsigned int    reloc_type;
13064	  unsigned int    reloc_size;
13065	  bfd_boolean     reloc_inplace = FALSE;
13066	  bfd_boolean     reloc_subtract = FALSE;
13067	  unsigned char * rloc;
13068	  unsigned long   sym_index;
13069
13070	  reloc_type = get_reloc_type (filedata, rp->r_info);
13071
13072	  if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
13073	    continue;
13074	  else if (is_none_reloc (filedata, reloc_type))
13075	    continue;
13076	  else if (is_32bit_abs_reloc (filedata, reloc_type)
13077		   || is_32bit_pcrel_reloc (filedata, reloc_type))
13078	    reloc_size = 4;
13079	  else if (is_64bit_abs_reloc (filedata, reloc_type)
13080		   || is_64bit_pcrel_reloc (filedata, reloc_type))
13081	    reloc_size = 8;
13082	  else if (is_24bit_abs_reloc (filedata, reloc_type))
13083	    reloc_size = 3;
13084	  else if (is_16bit_abs_reloc (filedata, reloc_type))
13085	    reloc_size = 2;
13086	  else if (is_8bit_abs_reloc (filedata, reloc_type)
13087		   || is_6bit_abs_reloc (filedata, reloc_type))
13088	    reloc_size = 1;
13089	  else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
13090								 reloc_type))
13091		   || is_32bit_inplace_add_reloc (filedata, reloc_type))
13092	    {
13093	      reloc_size = 4;
13094	      reloc_inplace = TRUE;
13095	    }
13096	  else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
13097								 reloc_type))
13098		   || is_64bit_inplace_add_reloc (filedata, reloc_type))
13099	    {
13100	      reloc_size = 8;
13101	      reloc_inplace = TRUE;
13102	    }
13103	  else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
13104								 reloc_type))
13105		   || is_16bit_inplace_add_reloc (filedata, reloc_type))
13106	    {
13107	      reloc_size = 2;
13108	      reloc_inplace = TRUE;
13109	    }
13110	  else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
13111								reloc_type))
13112		   || is_8bit_inplace_add_reloc (filedata, reloc_type))
13113	    {
13114	      reloc_size = 1;
13115	      reloc_inplace = TRUE;
13116	    }
13117	  else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
13118								reloc_type)))
13119	    {
13120	      reloc_size = 1;
13121	      reloc_inplace = TRUE;
13122	    }
13123	  else
13124	    {
13125	      static unsigned int prev_reloc = 0;
13126
13127	      if (reloc_type != prev_reloc)
13128		warn (_("unable to apply unsupported reloc type %d to section %s\n"),
13129		      reloc_type, printable_section_name (filedata, section));
13130	      prev_reloc = reloc_type;
13131	      res = FALSE;
13132	      continue;
13133	    }
13134
13135	  rloc = start + rp->r_offset;
13136	  if ((rloc + reloc_size) > end || (rloc < start))
13137	    {
13138	      warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
13139		    (unsigned long) rp->r_offset,
13140		    printable_section_name (filedata, section));
13141	      res = FALSE;
13142	      continue;
13143	    }
13144
13145	  sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
13146	  if (sym_index >= num_syms)
13147	    {
13148	      warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
13149		    sym_index, printable_section_name (filedata, section));
13150	      res = FALSE;
13151	      continue;
13152	    }
13153	  sym = symtab + sym_index;
13154
13155	  /* If the reloc has a symbol associated with it,
13156	     make sure that it is of an appropriate type.
13157
13158	     Relocations against symbols without type can happen.
13159	     Gcc -feliminate-dwarf2-dups may generate symbols
13160	     without type for debug info.
13161
13162	     Icc generates relocations against function symbols
13163	     instead of local labels.
13164
13165	     Relocations against object symbols can happen, eg when
13166	     referencing a global array.  For an example of this see
13167	     the _clz.o binary in libgcc.a.  */
13168	  if (sym != symtab
13169	      && ELF_ST_TYPE (sym->st_info) != STT_COMMON
13170	      && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
13171	    {
13172	      warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
13173		    get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
13174		    printable_section_name (filedata, relsec),
13175		    (long int)(rp - relocs));
13176	      res = FALSE;
13177	      continue;
13178	    }
13179
13180	  addend = 0;
13181	  if (is_rela)
13182	    addend += rp->r_addend;
13183	  /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
13184	     partial_inplace.  */
13185	  if (!is_rela
13186	      || (filedata->file_header.e_machine == EM_XTENSA
13187		  && reloc_type == 1)
13188	      || ((filedata->file_header.e_machine == EM_PJ
13189		   || filedata->file_header.e_machine == EM_PJ_OLD)
13190		  && reloc_type == 1)
13191	      || ((filedata->file_header.e_machine == EM_D30V
13192		   || filedata->file_header.e_machine == EM_CYGNUS_D30V)
13193		  && reloc_type == 12)
13194	      || reloc_inplace)
13195	    {
13196	      if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
13197		addend += byte_get (rloc, reloc_size) & 0x3f;
13198	      else
13199		addend += byte_get (rloc, reloc_size);
13200	    }
13201
13202	  if (is_32bit_pcrel_reloc (filedata, reloc_type)
13203	      || is_64bit_pcrel_reloc (filedata, reloc_type))
13204	    {
13205	      /* On HPPA, all pc-relative relocations are biased by 8.  */
13206	      if (filedata->file_header.e_machine == EM_PARISC)
13207		addend -= 8;
13208	      byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
13209		        reloc_size);
13210	    }
13211	  else if (is_6bit_abs_reloc (filedata, reloc_type)
13212		   || is_6bit_inplace_sub_reloc (filedata, reloc_type))
13213	    {
13214	      if (reloc_subtract)
13215		addend -= sym->st_value;
13216	      else
13217		addend += sym->st_value;
13218	      addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
13219	      byte_put (rloc, addend, reloc_size);
13220	    }
13221	  else if (reloc_subtract)
13222	    byte_put (rloc, addend - sym->st_value, reloc_size);
13223	  else
13224	    byte_put (rloc, addend + sym->st_value, reloc_size);
13225	}
13226
13227      free (symtab);
13228      /* Let the target specific reloc processing code know that
13229	 we have finished with these relocs.  */
13230      target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
13231
13232      if (relocs_return)
13233	{
13234	  * (Elf_Internal_Rela **) relocs_return = relocs;
13235	  * num_relocs_return = num_relocs;
13236	}
13237      else
13238	free (relocs);
13239
13240      break;
13241    }
13242
13243  return res;
13244}
13245
13246#ifdef SUPPORT_DISASSEMBLY
13247static bfd_boolean
13248disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
13249{
13250  printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
13251
13252  /* FIXME: XXX -- to be done --- XXX */
13253
13254  return TRUE;
13255}
13256#endif
13257
13258/* Reads in the contents of SECTION from FILE, returning a pointer
13259   to a malloc'ed buffer or NULL if something went wrong.  */
13260
13261static char *
13262get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
13263{
13264  bfd_size_type num_bytes = section->sh_size;
13265
13266  if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
13267    {
13268      printf (_("Section '%s' has no data to dump.\n"),
13269	      printable_section_name (filedata, section));
13270      return NULL;
13271    }
13272
13273  return  (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
13274                             _("section contents"));
13275}
13276
13277/* Uncompresses a section that was compressed using zlib, in place.  */
13278
13279static bfd_boolean
13280uncompress_section_contents (unsigned char **   buffer,
13281			     dwarf_size_type    uncompressed_size,
13282			     dwarf_size_type *  size)
13283{
13284  dwarf_size_type compressed_size = *size;
13285  unsigned char * compressed_buffer = *buffer;
13286  unsigned char * uncompressed_buffer;
13287  z_stream strm;
13288  int rc;
13289
13290  /* It is possible the section consists of several compressed
13291     buffers concatenated together, so we uncompress in a loop.  */
13292  /* PR 18313: The state field in the z_stream structure is supposed
13293     to be invisible to the user (ie us), but some compilers will
13294     still complain about it being used without initialisation.  So
13295     we first zero the entire z_stream structure and then set the fields
13296     that we need.  */
13297  memset (& strm, 0, sizeof strm);
13298  strm.avail_in = compressed_size;
13299  strm.next_in = (Bytef *) compressed_buffer;
13300  strm.avail_out = uncompressed_size;
13301  uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
13302
13303  rc = inflateInit (& strm);
13304  while (strm.avail_in > 0)
13305    {
13306      if (rc != Z_OK)
13307        goto fail;
13308      strm.next_out = ((Bytef *) uncompressed_buffer
13309                       + (uncompressed_size - strm.avail_out));
13310      rc = inflate (&strm, Z_FINISH);
13311      if (rc != Z_STREAM_END)
13312        goto fail;
13313      rc = inflateReset (& strm);
13314    }
13315  rc = inflateEnd (& strm);
13316  if (rc != Z_OK
13317      || strm.avail_out != 0)
13318    goto fail;
13319
13320  *buffer = uncompressed_buffer;
13321  *size = uncompressed_size;
13322  return TRUE;
13323
13324 fail:
13325  free (uncompressed_buffer);
13326  /* Indicate decompression failure.  */
13327  *buffer = NULL;
13328  return FALSE;
13329}
13330
13331static bfd_boolean
13332dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
13333{
13334  Elf_Internal_Shdr *  relsec;
13335  bfd_size_type        num_bytes;
13336  unsigned char *      data;
13337  unsigned char *      end;
13338  unsigned char *      real_start;
13339  unsigned char *      start;
13340  bfd_boolean          some_strings_shown;
13341
13342  real_start = start = (unsigned char *) get_section_contents (section, filedata);
13343  if (start == NULL)
13344    /* PR 21820: Do not fail if the section was empty.  */
13345    return (section->sh_size == 0 || section->sh_type == SHT_NOBITS) ? TRUE : FALSE;
13346
13347  num_bytes = section->sh_size;
13348
13349  printf (_("\nString dump of section '%s':\n"), printable_section_name (filedata, section));
13350
13351  if (decompress_dumps)
13352    {
13353      dwarf_size_type new_size = num_bytes;
13354      dwarf_size_type uncompressed_size = 0;
13355
13356      if ((section->sh_flags & SHF_COMPRESSED) != 0)
13357	{
13358	  Elf_Internal_Chdr chdr;
13359	  unsigned int compression_header_size
13360	    = get_compression_header (& chdr, (unsigned char *) start,
13361				      num_bytes);
13362
13363	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
13364	    {
13365	      warn (_("section '%s' has unsupported compress type: %d\n"),
13366		    printable_section_name (filedata, section), chdr.ch_type);
13367	      return FALSE;
13368	    }
13369	  else if (chdr.ch_addralign != section->sh_addralign)
13370	    {
13371	      warn (_("compressed section '%s' is corrupted\n"),
13372		    printable_section_name (filedata, section));
13373	      return FALSE;
13374	    }
13375	  uncompressed_size = chdr.ch_size;
13376	  start += compression_header_size;
13377	  new_size -= compression_header_size;
13378	}
13379      else if (new_size > 12 && streq ((char *) start, "ZLIB"))
13380	{
13381	  /* Read the zlib header.  In this case, it should be "ZLIB"
13382	     followed by the uncompressed section size, 8 bytes in
13383	     big-endian order.  */
13384	  uncompressed_size = start[4]; uncompressed_size <<= 8;
13385	  uncompressed_size += start[5]; uncompressed_size <<= 8;
13386	  uncompressed_size += start[6]; uncompressed_size <<= 8;
13387	  uncompressed_size += start[7]; uncompressed_size <<= 8;
13388	  uncompressed_size += start[8]; uncompressed_size <<= 8;
13389	  uncompressed_size += start[9]; uncompressed_size <<= 8;
13390	  uncompressed_size += start[10]; uncompressed_size <<= 8;
13391	  uncompressed_size += start[11];
13392	  start += 12;
13393	  new_size -= 12;
13394	}
13395
13396      if (uncompressed_size)
13397	{
13398	  if (uncompress_section_contents (& start,
13399					   uncompressed_size, & new_size))
13400	    num_bytes = new_size;
13401	  else
13402	    {
13403	      error (_("Unable to decompress section %s\n"),
13404		     printable_section_name (filedata, section));
13405	      return FALSE;
13406	    }
13407	}
13408      else
13409	start = real_start;
13410    }
13411
13412  /* If the section being dumped has relocations against it the user might
13413     be expecting these relocations to have been applied.  Check for this
13414     case and issue a warning message in order to avoid confusion.
13415     FIXME: Maybe we ought to have an option that dumps a section with
13416     relocs applied ?  */
13417  for (relsec = filedata->section_headers;
13418       relsec < filedata->section_headers + filedata->file_header.e_shnum;
13419       ++relsec)
13420    {
13421      if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
13422	  || relsec->sh_info >= filedata->file_header.e_shnum
13423	  || filedata->section_headers + relsec->sh_info != section
13424	  || relsec->sh_size == 0
13425	  || relsec->sh_link >= filedata->file_header.e_shnum)
13426	continue;
13427
13428      printf (_("  Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
13429      break;
13430    }
13431
13432  data = start;
13433  end  = start + num_bytes;
13434  some_strings_shown = FALSE;
13435
13436  while (data < end)
13437    {
13438      while (!ISPRINT (* data))
13439	if (++ data >= end)
13440	  break;
13441
13442      if (data < end)
13443	{
13444	  size_t maxlen = end - data;
13445
13446#ifndef __MSVCRT__
13447	  /* PR 11128: Use two separate invocations in order to work
13448             around bugs in the Solaris 8 implementation of printf.  */
13449	  printf ("  [%6tx]  ", data - start);
13450#else
13451	  printf ("  [%6Ix]  ", (size_t) (data - start));
13452#endif
13453	  if (maxlen > 0)
13454	    {
13455	      print_symbol ((int) maxlen, (const char *) data);
13456	      putchar ('\n');
13457	      data += strnlen ((const char *) data, maxlen);
13458	    }
13459	  else
13460	    {
13461	      printf (_("<corrupt>\n"));
13462	      data = end;
13463	    }
13464	  some_strings_shown = TRUE;
13465	}
13466    }
13467
13468  if (! some_strings_shown)
13469    printf (_("  No strings found in this section."));
13470
13471  free (real_start);
13472
13473  putchar ('\n');
13474  return TRUE;
13475}
13476
13477static bfd_boolean
13478dump_section_as_bytes (Elf_Internal_Shdr *  section,
13479		       Filedata *           filedata,
13480		       bfd_boolean          relocate)
13481{
13482  Elf_Internal_Shdr * relsec;
13483  bfd_size_type       bytes;
13484  bfd_size_type       section_size;
13485  bfd_vma             addr;
13486  unsigned char *     data;
13487  unsigned char *     real_start;
13488  unsigned char *     start;
13489
13490  real_start = start = (unsigned char *) get_section_contents (section, filedata);
13491  if (start == NULL)
13492    /* PR 21820: Do not fail if the section was empty.  */
13493    return (section->sh_size == 0 || section->sh_type == SHT_NOBITS) ? TRUE : FALSE;
13494
13495  section_size = section->sh_size;
13496
13497  printf (_("\nHex dump of section '%s':\n"), printable_section_name (filedata, section));
13498
13499  if (decompress_dumps)
13500    {
13501      dwarf_size_type new_size = section_size;
13502      dwarf_size_type uncompressed_size = 0;
13503
13504      if ((section->sh_flags & SHF_COMPRESSED) != 0)
13505	{
13506	  Elf_Internal_Chdr chdr;
13507	  unsigned int compression_header_size
13508	    = get_compression_header (& chdr, start, section_size);
13509
13510	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
13511	    {
13512	      warn (_("section '%s' has unsupported compress type: %d\n"),
13513		    printable_section_name (filedata, section), chdr.ch_type);
13514	      return FALSE;
13515	    }
13516	  else if (chdr.ch_addralign != section->sh_addralign)
13517	    {
13518	      warn (_("compressed section '%s' is corrupted\n"),
13519		    printable_section_name (filedata, section));
13520	      return FALSE;
13521	    }
13522	  uncompressed_size = chdr.ch_size;
13523	  start += compression_header_size;
13524	  new_size -= compression_header_size;
13525	}
13526      else if (new_size > 12 && streq ((char *) start, "ZLIB"))
13527	{
13528	  /* Read the zlib header.  In this case, it should be "ZLIB"
13529	     followed by the uncompressed section size, 8 bytes in
13530	     big-endian order.  */
13531	  uncompressed_size = start[4]; uncompressed_size <<= 8;
13532	  uncompressed_size += start[5]; uncompressed_size <<= 8;
13533	  uncompressed_size += start[6]; uncompressed_size <<= 8;
13534	  uncompressed_size += start[7]; uncompressed_size <<= 8;
13535	  uncompressed_size += start[8]; uncompressed_size <<= 8;
13536	  uncompressed_size += start[9]; uncompressed_size <<= 8;
13537	  uncompressed_size += start[10]; uncompressed_size <<= 8;
13538	  uncompressed_size += start[11];
13539	  start += 12;
13540	  new_size -= 12;
13541	}
13542
13543      if (uncompressed_size)
13544	{
13545	  if (uncompress_section_contents (& start, uncompressed_size,
13546					   & new_size))
13547	    {
13548	      section_size = new_size;
13549	    }
13550	  else
13551	    {
13552	      error (_("Unable to decompress section %s\n"),
13553		     printable_section_name (filedata, section));
13554	      /* FIXME: Print the section anyway ?  */
13555	      return FALSE;
13556	    }
13557	}
13558      else
13559	start = real_start;
13560    }
13561
13562  if (relocate)
13563    {
13564      if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
13565	return FALSE;
13566    }
13567  else
13568    {
13569      /* If the section being dumped has relocations against it the user might
13570	 be expecting these relocations to have been applied.  Check for this
13571	 case and issue a warning message in order to avoid confusion.
13572	 FIXME: Maybe we ought to have an option that dumps a section with
13573	 relocs applied ?  */
13574      for (relsec = filedata->section_headers;
13575	   relsec < filedata->section_headers + filedata->file_header.e_shnum;
13576	   ++relsec)
13577	{
13578	  if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
13579	      || relsec->sh_info >= filedata->file_header.e_shnum
13580	      || filedata->section_headers + relsec->sh_info != section
13581	      || relsec->sh_size == 0
13582	      || relsec->sh_link >= filedata->file_header.e_shnum)
13583	    continue;
13584
13585	  printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
13586	  break;
13587	}
13588    }
13589
13590  addr = section->sh_addr;
13591  bytes = section_size;
13592  data = start;
13593
13594  while (bytes)
13595    {
13596      int j;
13597      int k;
13598      int lbytes;
13599
13600      lbytes = (bytes > 16 ? 16 : bytes);
13601
13602      printf ("  0x%8.8lx ", (unsigned long) addr);
13603
13604      for (j = 0; j < 16; j++)
13605	{
13606	  if (j < lbytes)
13607	    printf ("%2.2x", data[j]);
13608	  else
13609	    printf ("  ");
13610
13611	  if ((j & 3) == 3)
13612	    printf (" ");
13613	}
13614
13615      for (j = 0; j < lbytes; j++)
13616	{
13617	  k = data[j];
13618	  if (k >= ' ' && k < 0x7f)
13619	    printf ("%c", k);
13620	  else
13621	    printf (".");
13622	}
13623
13624      putchar ('\n');
13625
13626      data  += lbytes;
13627      addr  += lbytes;
13628      bytes -= lbytes;
13629    }
13630
13631  free (real_start);
13632
13633  putchar ('\n');
13634  return TRUE;
13635}
13636
13637static bfd_boolean
13638load_specific_debug_section (enum dwarf_section_display_enum  debug,
13639			     const Elf_Internal_Shdr *        sec,
13640			     void *                           data)
13641{
13642  struct dwarf_section * section = &debug_displays [debug].section;
13643  char buf [64];
13644  Filedata * filedata = (Filedata *) data;
13645
13646  if (section->start != NULL)
13647    {
13648      /* If it is already loaded, do nothing.  */
13649      if (streq (section->filename, filedata->file_name))
13650	return TRUE;
13651      free (section->start);
13652    }
13653
13654  snprintf (buf, sizeof (buf), _("%s section data"), section->name);
13655  section->address = sec->sh_addr;
13656  section->user_data = NULL;
13657  section->filename = filedata->file_name;
13658  section->start = (unsigned char *) get_data (NULL, filedata,
13659                                               sec->sh_offset, 1,
13660                                               sec->sh_size, buf);
13661  if (section->start == NULL)
13662    section->size = 0;
13663  else
13664    {
13665      unsigned char *start = section->start;
13666      dwarf_size_type size = sec->sh_size;
13667      dwarf_size_type uncompressed_size = 0;
13668
13669      if ((sec->sh_flags & SHF_COMPRESSED) != 0)
13670	{
13671	  Elf_Internal_Chdr chdr;
13672	  unsigned int compression_header_size;
13673
13674	  if (size < (is_32bit_elf
13675		      ? sizeof (Elf32_External_Chdr)
13676		      : sizeof (Elf64_External_Chdr)))
13677	    {
13678	      warn (_("compressed section %s is too small to contain a compression header"),
13679		    section->name);
13680	      return FALSE;
13681	    }
13682
13683	  compression_header_size = get_compression_header (&chdr, start, size);
13684
13685	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
13686	    {
13687	      warn (_("section '%s' has unsupported compress type: %d\n"),
13688		    section->name, chdr.ch_type);
13689	      return FALSE;
13690	    }
13691	  else if (chdr.ch_addralign != sec->sh_addralign)
13692	    {
13693	      warn (_("compressed section '%s' is corrupted\n"),
13694		    section->name);
13695	      return FALSE;
13696	    }
13697	  uncompressed_size = chdr.ch_size;
13698	  start += compression_header_size;
13699	  size -= compression_header_size;
13700	}
13701      else if (size > 12 && streq ((char *) start, "ZLIB"))
13702	{
13703	  /* Read the zlib header.  In this case, it should be "ZLIB"
13704	     followed by the uncompressed section size, 8 bytes in
13705	     big-endian order.  */
13706	  uncompressed_size = start[4]; uncompressed_size <<= 8;
13707	  uncompressed_size += start[5]; uncompressed_size <<= 8;
13708	  uncompressed_size += start[6]; uncompressed_size <<= 8;
13709	  uncompressed_size += start[7]; uncompressed_size <<= 8;
13710	  uncompressed_size += start[8]; uncompressed_size <<= 8;
13711	  uncompressed_size += start[9]; uncompressed_size <<= 8;
13712	  uncompressed_size += start[10]; uncompressed_size <<= 8;
13713	  uncompressed_size += start[11];
13714	  start += 12;
13715	  size -= 12;
13716	}
13717
13718      if (uncompressed_size)
13719	{
13720	  if (uncompress_section_contents (&start, uncompressed_size,
13721					   &size))
13722	    {
13723	      /* Free the compressed buffer, update the section buffer
13724		 and the section size if uncompress is successful.  */
13725	      free (section->start);
13726	      section->start = start;
13727	    }
13728	  else
13729	    {
13730	      error (_("Unable to decompress section %s\n"),
13731		     printable_section_name (filedata, sec));
13732	      return FALSE;
13733	    }
13734	}
13735
13736      section->size = size;
13737    }
13738
13739  if (section->start == NULL)
13740    return FALSE;
13741
13742  if (debug_displays [debug].relocate)
13743    {
13744      if (! apply_relocations (filedata, sec, section->start, section->size,
13745			       & section->reloc_info, & section->num_relocs))
13746	return FALSE;
13747    }
13748  else
13749    {
13750      section->reloc_info = NULL;
13751      section->num_relocs = 0;
13752    }
13753
13754  return TRUE;
13755}
13756
13757/* If this is not NULL, load_debug_section will only look for sections
13758   within the list of sections given here.  */
13759static unsigned int * section_subset = NULL;
13760
13761bfd_boolean
13762load_debug_section (enum dwarf_section_display_enum debug, void * data)
13763{
13764  struct dwarf_section * section = &debug_displays [debug].section;
13765  Elf_Internal_Shdr * sec;
13766  Filedata * filedata = (Filedata *) data;
13767
13768  /* Without section headers we cannot find any sections.  */
13769  if (filedata->section_headers == NULL)
13770    return FALSE;
13771
13772  if (filedata->string_table == NULL
13773      && filedata->file_header.e_shstrndx != SHN_UNDEF
13774      && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
13775    {
13776      Elf_Internal_Shdr * strs;
13777
13778      /* Read in the string table, so that we have section names to scan.  */
13779      strs = filedata->section_headers + filedata->file_header.e_shstrndx;
13780
13781      if (strs != NULL && strs->sh_size != 0)
13782	{
13783	  filedata->string_table
13784	    = (char *) get_data (NULL, filedata, strs->sh_offset,
13785				 1, strs->sh_size, _("string table"));
13786
13787	  filedata->string_table_length
13788	    = filedata->string_table != NULL ? strs->sh_size : 0;
13789	}
13790    }
13791
13792  /* Locate the debug section.  */
13793  sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
13794  if (sec != NULL)
13795    section->name = section->uncompressed_name;
13796  else
13797    {
13798      sec = find_section_in_set (filedata, section->compressed_name, section_subset);
13799      if (sec != NULL)
13800	section->name = section->compressed_name;
13801    }
13802  if (sec == NULL)
13803    return FALSE;
13804
13805  /* If we're loading from a subset of sections, and we've loaded
13806     a section matching this name before, it's likely that it's a
13807     different one.  */
13808  if (section_subset != NULL)
13809    free_debug_section (debug);
13810
13811  return load_specific_debug_section (debug, sec, data);
13812}
13813
13814void
13815free_debug_section (enum dwarf_section_display_enum debug)
13816{
13817  struct dwarf_section * section = &debug_displays [debug].section;
13818
13819  if (section->start == NULL)
13820    return;
13821
13822  free ((char *) section->start);
13823  section->start = NULL;
13824  section->address = 0;
13825  section->size = 0;
13826}
13827
13828static bfd_boolean
13829display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
13830{
13831  char * name = SECTION_NAME (section);
13832  const char * print_name = printable_section_name (filedata, section);
13833  bfd_size_type length;
13834  bfd_boolean result = TRUE;
13835  int i;
13836
13837  length = section->sh_size;
13838  if (length == 0)
13839    {
13840      printf (_("\nSection '%s' has no debugging data.\n"), print_name);
13841      return TRUE;
13842    }
13843  if (section->sh_type == SHT_NOBITS)
13844    {
13845      /* There is no point in dumping the contents of a debugging section
13846	 which has the NOBITS type - the bits in the file will be random.
13847	 This can happen when a file containing a .eh_frame section is
13848	 stripped with the --only-keep-debug command line option.  */
13849      printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
13850	      print_name);
13851      return FALSE;
13852    }
13853
13854  if (const_strneq (name, ".gnu.linkonce.wi."))
13855    name = ".debug_info";
13856
13857  /* See if we know how to display the contents of this section.  */
13858  for (i = 0; i < max; i++)
13859    {
13860      enum dwarf_section_display_enum  id = (enum dwarf_section_display_enum) i;
13861      struct dwarf_section_display *   display = debug_displays + i;
13862      struct dwarf_section *           sec = & display->section;
13863
13864      if (streq (sec->uncompressed_name, name)
13865	  || (id == line && const_strneq (name, ".debug_line."))
13866	  || streq (sec->compressed_name, name))
13867	{
13868	  bfd_boolean secondary = (section != find_section (filedata, name));
13869
13870	  if (secondary)
13871	    free_debug_section (id);
13872
13873	  if (i == line && const_strneq (name, ".debug_line."))
13874	    sec->name = name;
13875	  else if (streq (sec->uncompressed_name, name))
13876	    sec->name = sec->uncompressed_name;
13877	  else
13878	    sec->name = sec->compressed_name;
13879
13880	  if (load_specific_debug_section (id, section, filedata))
13881	    {
13882	      /* If this debug section is part of a CU/TU set in a .dwp file,
13883		 restrict load_debug_section to the sections in that set.  */
13884	      section_subset = find_cu_tu_set (filedata, shndx);
13885
13886	      result &= display->display (sec, filedata);
13887
13888	      section_subset = NULL;
13889
13890	      if (secondary || (id != info && id != abbrev))
13891		free_debug_section (id);
13892	    }
13893	  break;
13894	}
13895    }
13896
13897  if (i == max)
13898    {
13899      printf (_("Unrecognized debug section: %s\n"), print_name);
13900      result = FALSE;
13901    }
13902
13903  return result;
13904}
13905
13906/* Set DUMP_SECTS for all sections where dumps were requested
13907   based on section name.  */
13908
13909static void
13910initialise_dumps_byname (Filedata * filedata)
13911{
13912  struct dump_list_entry * cur;
13913
13914  for (cur = dump_sects_byname; cur; cur = cur->next)
13915    {
13916      unsigned int i;
13917      bfd_boolean any = FALSE;
13918
13919      for (i = 0; i < filedata->file_header.e_shnum; i++)
13920	if (streq (SECTION_NAME (filedata->section_headers + i), cur->name))
13921	  {
13922	    request_dump_bynumber (filedata, i, cur->type);
13923	    any = TRUE;
13924	  }
13925
13926      if (!any)
13927	warn (_("Section '%s' was not dumped because it does not exist!\n"),
13928	      cur->name);
13929    }
13930}
13931
13932static bfd_boolean
13933process_section_contents (Filedata * filedata)
13934{
13935  Elf_Internal_Shdr * section;
13936  unsigned int i;
13937  bfd_boolean res = TRUE;
13938
13939  if (! do_dump)
13940    return TRUE;
13941
13942  initialise_dumps_byname (filedata);
13943
13944  for (i = 0, section = filedata->section_headers;
13945       i < filedata->file_header.e_shnum && i < filedata->num_dump_sects;
13946       i++, section++)
13947    {
13948      dump_type dump = filedata->dump_sects[i];
13949
13950#ifdef SUPPORT_DISASSEMBLY
13951      if (dump & DISASS_DUMP)
13952	{
13953	  if (! disassemble_section (section, filedata))
13954	    res = FALSE;
13955	}
13956#endif
13957      if (dump & HEX_DUMP)
13958	{
13959	  if (! dump_section_as_bytes (section, filedata, FALSE))
13960	    res = FALSE;
13961	}
13962
13963      if (dump & RELOC_DUMP)
13964	{
13965	  if (! dump_section_as_bytes (section, filedata, TRUE))
13966	    res = FALSE;
13967	}
13968
13969      if (dump & STRING_DUMP)
13970	{
13971	  if (! dump_section_as_strings (section, filedata))
13972	    res = FALSE;
13973	}
13974
13975      if (dump & DEBUG_DUMP)
13976	{
13977	  if (! display_debug_section (i, section, filedata))
13978	    res = FALSE;
13979	}
13980    }
13981
13982  /* Check to see if the user requested a
13983     dump of a section that does not exist.  */
13984  while (i < filedata->num_dump_sects)
13985    {
13986      if (filedata->dump_sects[i])
13987	{
13988	  warn (_("Section %d was not dumped because it does not exist!\n"), i);
13989	  res = FALSE;
13990	}
13991      i++;
13992    }
13993
13994  return res;
13995}
13996
13997static void
13998process_mips_fpe_exception (int mask)
13999{
14000  if (mask)
14001    {
14002      bfd_boolean first = TRUE;
14003
14004      if (mask & OEX_FPU_INEX)
14005	fputs ("INEX", stdout), first = FALSE;
14006      if (mask & OEX_FPU_UFLO)
14007	printf ("%sUFLO", first ? "" : "|"), first = FALSE;
14008      if (mask & OEX_FPU_OFLO)
14009	printf ("%sOFLO", first ? "" : "|"), first = FALSE;
14010      if (mask & OEX_FPU_DIV0)
14011	printf ("%sDIV0", first ? "" : "|"), first = FALSE;
14012      if (mask & OEX_FPU_INVAL)
14013	printf ("%sINVAL", first ? "" : "|");
14014    }
14015  else
14016    fputs ("0", stdout);
14017}
14018
14019/* Display's the value of TAG at location P.  If TAG is
14020   greater than 0 it is assumed to be an unknown tag, and
14021   a message is printed to this effect.  Otherwise it is
14022   assumed that a message has already been printed.
14023
14024   If the bottom bit of TAG is set it assumed to have a
14025   string value, otherwise it is assumed to have an integer
14026   value.
14027
14028   Returns an updated P pointing to the first unread byte
14029   beyond the end of TAG's value.
14030
14031   Reads at or beyond END will not be made.  */
14032
14033static unsigned char *
14034display_tag_value (signed int tag,
14035		   unsigned char * p,
14036		   const unsigned char * const end)
14037{
14038  unsigned long val;
14039
14040  if (tag > 0)
14041    printf ("  Tag_unknown_%d: ", tag);
14042
14043  if (p >= end)
14044    {
14045      warn (_("<corrupt tag>\n"));
14046    }
14047  else if (tag & 1)
14048    {
14049      /* PR 17531 file: 027-19978-0.004.  */
14050      size_t maxlen = (end - p) - 1;
14051
14052      putchar ('"');
14053      if (maxlen > 0)
14054	{
14055	  print_symbol ((int) maxlen, (const char *) p);
14056	  p += strnlen ((char *) p, maxlen) + 1;
14057	}
14058      else
14059	{
14060	  printf (_("<corrupt string tag>"));
14061	  p = (unsigned char *) end;
14062	}
14063      printf ("\"\n");
14064    }
14065  else
14066    {
14067      unsigned int len;
14068
14069      val = read_uleb128 (p, &len, end);
14070      p += len;
14071      printf ("%ld (0x%lx)\n", val, val);
14072    }
14073
14074  assert (p <= end);
14075  return p;
14076}
14077
14078/* ARC ABI attributes section.  */
14079
14080static unsigned char *
14081display_arc_attribute (unsigned char * p,
14082		       const unsigned char * const end)
14083{
14084  unsigned int tag;
14085  unsigned int len;
14086  unsigned int val;
14087
14088  tag = read_uleb128 (p, &len, end);
14089  p += len;
14090
14091  switch (tag)
14092    {
14093    case Tag_ARC_PCS_config:
14094      val = read_uleb128 (p, &len, end);
14095      p += len;
14096      printf ("  Tag_ARC_PCS_config: ");
14097      switch (val)
14098	{
14099	case 0:
14100	  printf (_("Absent/Non standard\n"));
14101	  break;
14102	case 1:
14103	  printf (_("Bare metal/mwdt\n"));
14104	  break;
14105	case 2:
14106	  printf (_("Bare metal/newlib\n"));
14107	  break;
14108	case 3:
14109	  printf (_("Linux/uclibc\n"));
14110	  break;
14111	case 4:
14112	  printf (_("Linux/glibc\n"));
14113	  break;
14114	default:
14115	  printf (_("Unknown\n"));
14116	  break;
14117	}
14118      break;
14119
14120    case Tag_ARC_CPU_base:
14121      val = read_uleb128 (p, &len, end);
14122      p += len;
14123      printf ("  Tag_ARC_CPU_base: ");
14124      switch (val)
14125	{
14126	default:
14127	case TAG_CPU_NONE:
14128	  printf (_("Absent\n"));
14129	  break;
14130	case TAG_CPU_ARC6xx:
14131	  printf ("ARC6xx\n");
14132	  break;
14133	case TAG_CPU_ARC7xx:
14134	  printf ("ARC7xx\n");
14135	  break;
14136	case TAG_CPU_ARCEM:
14137	  printf ("ARCEM\n");
14138	  break;
14139	case TAG_CPU_ARCHS:
14140	  printf ("ARCHS\n");
14141	  break;
14142	}
14143      break;
14144
14145    case Tag_ARC_CPU_variation:
14146      val = read_uleb128 (p, &len, end);
14147      p += len;
14148      printf ("  Tag_ARC_CPU_variation: ");
14149      switch (val)
14150	{
14151	default:
14152	  if (val > 0 && val < 16)
14153	      printf ("Core%d\n", val);
14154	  else
14155	      printf ("Unknown\n");
14156	  break;
14157
14158	case 0:
14159	  printf (_("Absent\n"));
14160	  break;
14161	}
14162      break;
14163
14164    case Tag_ARC_CPU_name:
14165      printf ("  Tag_ARC_CPU_name: ");
14166      p = display_tag_value (-1, p, end);
14167      break;
14168
14169    case Tag_ARC_ABI_rf16:
14170      val = read_uleb128 (p, &len, end);
14171      p += len;
14172      printf ("  Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
14173      break;
14174
14175    case Tag_ARC_ABI_osver:
14176      val = read_uleb128 (p, &len, end);
14177      p += len;
14178      printf ("  Tag_ARC_ABI_osver: v%d\n", val);
14179      break;
14180
14181    case Tag_ARC_ABI_pic:
14182    case Tag_ARC_ABI_sda:
14183      val = read_uleb128 (p, &len, end);
14184      p += len;
14185      printf (tag == Tag_ARC_ABI_sda ? "  Tag_ARC_ABI_sda: "
14186	      : "  Tag_ARC_ABI_pic: ");
14187      switch (val)
14188	{
14189	case 0:
14190	  printf (_("Absent\n"));
14191	  break;
14192	case 1:
14193	  printf ("MWDT\n");
14194	  break;
14195	case 2:
14196	  printf ("GNU\n");
14197	  break;
14198	default:
14199	  printf (_("Unknown\n"));
14200	  break;
14201	}
14202      break;
14203
14204    case Tag_ARC_ABI_tls:
14205      val = read_uleb128 (p, &len, end);
14206      p += len;
14207      printf ("  Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
14208      break;
14209
14210    case Tag_ARC_ABI_enumsize:
14211      val = read_uleb128 (p, &len, end);
14212      p += len;
14213      printf ("  Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
14214	      _("smallest"));
14215      break;
14216
14217    case Tag_ARC_ABI_exceptions:
14218      val = read_uleb128 (p, &len, end);
14219      p += len;
14220      printf ("  Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
14221	      : _("default"));
14222      break;
14223
14224    case Tag_ARC_ABI_double_size:
14225      val = read_uleb128 (p, &len, end);
14226      p += len;
14227      printf ("  Tag_ARC_ABI_double_size: %d\n", val);
14228      break;
14229
14230    case Tag_ARC_ISA_config:
14231      printf ("  Tag_ARC_ISA_config: ");
14232      p = display_tag_value (-1, p, end);
14233      break;
14234
14235    case Tag_ARC_ISA_apex:
14236      printf ("  Tag_ARC_ISA_apex: ");
14237      p = display_tag_value (-1, p, end);
14238      break;
14239
14240    case Tag_ARC_ISA_mpy_option:
14241      val = read_uleb128 (p, &len, end);
14242      p += len;
14243      printf ("  Tag_ARC_ISA_mpy_option: %d\n", val);
14244      break;
14245
14246    default:
14247      return display_tag_value (tag & 1, p, end);
14248    }
14249
14250  return p;
14251}
14252
14253/* ARM EABI attributes section.  */
14254typedef struct
14255{
14256  unsigned int tag;
14257  const char * name;
14258  /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup.  */
14259  unsigned int type;
14260  const char ** table;
14261} arm_attr_public_tag;
14262
14263static const char * arm_attr_tag_CPU_arch[] =
14264  {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
14265   "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
14266   "v8-M.mainline"};
14267static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
14268static const char * arm_attr_tag_THUMB_ISA_use[] =
14269  {"No", "Thumb-1", "Thumb-2", "Yes"};
14270static const char * arm_attr_tag_FP_arch[] =
14271  {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
14272   "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
14273static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
14274static const char * arm_attr_tag_Advanced_SIMD_arch[] =
14275  {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
14276   "NEON for ARMv8.1"};
14277static const char * arm_attr_tag_PCS_config[] =
14278  {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
14279   "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
14280static const char * arm_attr_tag_ABI_PCS_R9_use[] =
14281  {"V6", "SB", "TLS", "Unused"};
14282static const char * arm_attr_tag_ABI_PCS_RW_data[] =
14283  {"Absolute", "PC-relative", "SB-relative", "None"};
14284static const char * arm_attr_tag_ABI_PCS_RO_data[] =
14285  {"Absolute", "PC-relative", "None"};
14286static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
14287  {"None", "direct", "GOT-indirect"};
14288static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
14289  {"None", "??? 1", "2", "??? 3", "4"};
14290static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
14291static const char * arm_attr_tag_ABI_FP_denormal[] =
14292  {"Unused", "Needed", "Sign only"};
14293static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
14294static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
14295static const char * arm_attr_tag_ABI_FP_number_model[] =
14296  {"Unused", "Finite", "RTABI", "IEEE 754"};
14297static const char * arm_attr_tag_ABI_enum_size[] =
14298  {"Unused", "small", "int", "forced to int"};
14299static const char * arm_attr_tag_ABI_HardFP_use[] =
14300  {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
14301static const char * arm_attr_tag_ABI_VFP_args[] =
14302  {"AAPCS", "VFP registers", "custom", "compatible"};
14303static const char * arm_attr_tag_ABI_WMMX_args[] =
14304  {"AAPCS", "WMMX registers", "custom"};
14305static const char * arm_attr_tag_ABI_optimization_goals[] =
14306  {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
14307    "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
14308static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
14309  {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
14310    "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
14311static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
14312static const char * arm_attr_tag_FP_HP_extension[] =
14313  {"Not Allowed", "Allowed"};
14314static const char * arm_attr_tag_ABI_FP_16bit_format[] =
14315  {"None", "IEEE 754", "Alternative Format"};
14316static const char * arm_attr_tag_DSP_extension[] =
14317  {"Follow architecture", "Allowed"};
14318static const char * arm_attr_tag_MPextension_use[] =
14319  {"Not Allowed", "Allowed"};
14320static const char * arm_attr_tag_DIV_use[] =
14321  {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
14322    "Allowed in v7-A with integer division extension"};
14323static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
14324static const char * arm_attr_tag_Virtualization_use[] =
14325  {"Not Allowed", "TrustZone", "Virtualization Extensions",
14326    "TrustZone and Virtualization Extensions"};
14327static const char * arm_attr_tag_MPextension_use_legacy[] =
14328  {"Not Allowed", "Allowed"};
14329
14330#define LOOKUP(id, name) \
14331  {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
14332static arm_attr_public_tag arm_attr_public_tags[] =
14333{
14334  {4, "CPU_raw_name", 1, NULL},
14335  {5, "CPU_name", 1, NULL},
14336  LOOKUP(6, CPU_arch),
14337  {7, "CPU_arch_profile", 0, NULL},
14338  LOOKUP(8, ARM_ISA_use),
14339  LOOKUP(9, THUMB_ISA_use),
14340  LOOKUP(10, FP_arch),
14341  LOOKUP(11, WMMX_arch),
14342  LOOKUP(12, Advanced_SIMD_arch),
14343  LOOKUP(13, PCS_config),
14344  LOOKUP(14, ABI_PCS_R9_use),
14345  LOOKUP(15, ABI_PCS_RW_data),
14346  LOOKUP(16, ABI_PCS_RO_data),
14347  LOOKUP(17, ABI_PCS_GOT_use),
14348  LOOKUP(18, ABI_PCS_wchar_t),
14349  LOOKUP(19, ABI_FP_rounding),
14350  LOOKUP(20, ABI_FP_denormal),
14351  LOOKUP(21, ABI_FP_exceptions),
14352  LOOKUP(22, ABI_FP_user_exceptions),
14353  LOOKUP(23, ABI_FP_number_model),
14354  {24, "ABI_align_needed", 0, NULL},
14355  {25, "ABI_align_preserved", 0, NULL},
14356  LOOKUP(26, ABI_enum_size),
14357  LOOKUP(27, ABI_HardFP_use),
14358  LOOKUP(28, ABI_VFP_args),
14359  LOOKUP(29, ABI_WMMX_args),
14360  LOOKUP(30, ABI_optimization_goals),
14361  LOOKUP(31, ABI_FP_optimization_goals),
14362  {32, "compatibility", 0, NULL},
14363  LOOKUP(34, CPU_unaligned_access),
14364  LOOKUP(36, FP_HP_extension),
14365  LOOKUP(38, ABI_FP_16bit_format),
14366  LOOKUP(42, MPextension_use),
14367  LOOKUP(44, DIV_use),
14368  LOOKUP(46, DSP_extension),
14369  {64, "nodefaults", 0, NULL},
14370  {65, "also_compatible_with", 0, NULL},
14371  LOOKUP(66, T2EE_use),
14372  {67, "conformance", 1, NULL},
14373  LOOKUP(68, Virtualization_use),
14374  LOOKUP(70, MPextension_use_legacy)
14375};
14376#undef LOOKUP
14377
14378static unsigned char *
14379display_arm_attribute (unsigned char * p,
14380		       const unsigned char * const end)
14381{
14382  unsigned int tag;
14383  unsigned int len;
14384  unsigned int val;
14385  arm_attr_public_tag * attr;
14386  unsigned i;
14387  unsigned int type;
14388
14389  tag = read_uleb128 (p, &len, end);
14390  p += len;
14391  attr = NULL;
14392  for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
14393    {
14394      if (arm_attr_public_tags[i].tag == tag)
14395	{
14396	  attr = &arm_attr_public_tags[i];
14397	  break;
14398	}
14399    }
14400
14401  if (attr)
14402    {
14403      printf ("  Tag_%s: ", attr->name);
14404      switch (attr->type)
14405	{
14406	case 0:
14407	  switch (tag)
14408	    {
14409	    case 7: /* Tag_CPU_arch_profile.  */
14410	      val = read_uleb128 (p, &len, end);
14411	      p += len;
14412	      switch (val)
14413		{
14414		case 0: printf (_("None\n")); break;
14415		case 'A': printf (_("Application\n")); break;
14416		case 'R': printf (_("Realtime\n")); break;
14417		case 'M': printf (_("Microcontroller\n")); break;
14418		case 'S': printf (_("Application or Realtime\n")); break;
14419		default: printf ("??? (%d)\n", val); break;
14420		}
14421	      break;
14422
14423	    case 24: /* Tag_align_needed.  */
14424	      val = read_uleb128 (p, &len, end);
14425	      p += len;
14426	      switch (val)
14427		{
14428		case 0: printf (_("None\n")); break;
14429		case 1: printf (_("8-byte\n")); break;
14430		case 2: printf (_("4-byte\n")); break;
14431		case 3: printf ("??? 3\n"); break;
14432		default:
14433		  if (val <= 12)
14434		    printf (_("8-byte and up to %d-byte extended\n"),
14435			    1 << val);
14436		  else
14437		    printf ("??? (%d)\n", val);
14438		  break;
14439		}
14440	      break;
14441
14442	    case 25: /* Tag_align_preserved.  */
14443	      val = read_uleb128 (p, &len, end);
14444	      p += len;
14445	      switch (val)
14446		{
14447		case 0: printf (_("None\n")); break;
14448		case 1: printf (_("8-byte, except leaf SP\n")); break;
14449		case 2: printf (_("8-byte\n")); break;
14450		case 3: printf ("??? 3\n"); break;
14451		default:
14452		  if (val <= 12)
14453		    printf (_("8-byte and up to %d-byte extended\n"),
14454			    1 << val);
14455		  else
14456		    printf ("??? (%d)\n", val);
14457		  break;
14458		}
14459	      break;
14460
14461	    case 32: /* Tag_compatibility.  */
14462	      {
14463		val = read_uleb128 (p, &len, end);
14464		p += len;
14465		printf (_("flag = %d, vendor = "), val);
14466		if (p < end - 1)
14467		  {
14468		    size_t maxlen = (end - p) - 1;
14469
14470		    print_symbol ((int) maxlen, (const char *) p);
14471		    p += strnlen ((char *) p, maxlen) + 1;
14472		  }
14473		else
14474		  {
14475		    printf (_("<corrupt>"));
14476		    p = (unsigned char *) end;
14477		  }
14478		putchar ('\n');
14479	      }
14480	      break;
14481
14482	    case 64: /* Tag_nodefaults.  */
14483	      /* PR 17531: file: 001-505008-0.01.  */
14484	      if (p < end)
14485		p++;
14486	      printf (_("True\n"));
14487	      break;
14488
14489	    case 65: /* Tag_also_compatible_with.  */
14490	      val = read_uleb128 (p, &len, end);
14491	      p += len;
14492	      if (val == 6 /* Tag_CPU_arch.  */)
14493		{
14494		  val = read_uleb128 (p, &len, end);
14495		  p += len;
14496		  if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
14497		    printf ("??? (%d)\n", val);
14498		  else
14499		    printf ("%s\n", arm_attr_tag_CPU_arch[val]);
14500		}
14501	      else
14502		printf ("???\n");
14503	      while (p < end && *(p++) != '\0' /* NUL terminator.  */)
14504		;
14505	      break;
14506
14507	    default:
14508	      printf (_("<unknown: %d>\n"), tag);
14509	      break;
14510	    }
14511	  return p;
14512
14513	case 1:
14514	  return display_tag_value (-1, p, end);
14515	case 2:
14516	  return display_tag_value (0, p, end);
14517
14518	default:
14519	  assert (attr->type & 0x80);
14520	  val = read_uleb128 (p, &len, end);
14521	  p += len;
14522	  type = attr->type & 0x7f;
14523	  if (val >= type)
14524	    printf ("??? (%d)\n", val);
14525	  else
14526	    printf ("%s\n", attr->table[val]);
14527	  return p;
14528	}
14529    }
14530
14531  return display_tag_value (tag, p, end);
14532}
14533
14534static unsigned char *
14535display_gnu_attribute (unsigned char * p,
14536		       unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
14537		       const unsigned char * const end)
14538{
14539  int tag;
14540  unsigned int len;
14541  unsigned int val;
14542
14543  tag = read_uleb128 (p, &len, end);
14544  p += len;
14545
14546  /* Tag_compatibility is the only generic GNU attribute defined at
14547     present.  */
14548  if (tag == 32)
14549    {
14550      val = read_uleb128 (p, &len, end);
14551      p += len;
14552
14553      printf (_("flag = %d, vendor = "), val);
14554      if (p == end)
14555	{
14556	  printf (_("<corrupt>\n"));
14557	  warn (_("corrupt vendor attribute\n"));
14558	}
14559      else
14560	{
14561	  if (p < end - 1)
14562	    {
14563	      size_t maxlen = (end - p) - 1;
14564
14565	      print_symbol ((int) maxlen, (const char *) p);
14566	      p += strnlen ((char *) p, maxlen) + 1;
14567	    }
14568	  else
14569	    {
14570	      printf (_("<corrupt>"));
14571	      p = (unsigned char *) end;
14572	    }
14573	  putchar ('\n');
14574	}
14575      return p;
14576    }
14577
14578  if ((tag & 2) == 0 && display_proc_gnu_attribute)
14579    return display_proc_gnu_attribute (p, tag, end);
14580
14581  return display_tag_value (tag, p, end);
14582}
14583
14584static unsigned char *
14585display_power_gnu_attribute (unsigned char * p,
14586			     unsigned int tag,
14587			     const unsigned char * const end)
14588{
14589  unsigned int len;
14590  unsigned int val;
14591
14592  if (tag == Tag_GNU_Power_ABI_FP)
14593    {
14594      val = read_uleb128 (p, &len, end);
14595      p += len;
14596      printf ("  Tag_GNU_Power_ABI_FP: ");
14597      if (len == 0)
14598	{
14599	  printf (_("<corrupt>\n"));
14600	  return p;
14601	}
14602
14603      if (val > 15)
14604	printf ("(%#x), ", val);
14605
14606      switch (val & 3)
14607	{
14608	case 0:
14609	  printf (_("unspecified hard/soft float, "));
14610	  break;
14611	case 1:
14612	  printf (_("hard float, "));
14613	  break;
14614	case 2:
14615	  printf (_("soft float, "));
14616	  break;
14617	case 3:
14618	  printf (_("single-precision hard float, "));
14619	  break;
14620	}
14621
14622      switch (val & 0xC)
14623	{
14624	case 0:
14625	  printf (_("unspecified long double\n"));
14626	  break;
14627	case 4:
14628	  printf (_("128-bit IBM long double\n"));
14629	  break;
14630	case 8:
14631	  printf (_("64-bit long double\n"));
14632	  break;
14633	case 12:
14634	  printf (_("128-bit IEEE long double\n"));
14635	  break;
14636	}
14637      return p;
14638    }
14639
14640  if (tag == Tag_GNU_Power_ABI_Vector)
14641    {
14642      val = read_uleb128 (p, &len, end);
14643      p += len;
14644      printf ("  Tag_GNU_Power_ABI_Vector: ");
14645      if (len == 0)
14646	{
14647	  printf (_("<corrupt>\n"));
14648	  return p;
14649	}
14650
14651      if (val > 3)
14652	printf ("(%#x), ", val);
14653
14654      switch (val & 3)
14655	{
14656	case 0:
14657	  printf (_("unspecified\n"));
14658	  break;
14659	case 1:
14660	  printf (_("generic\n"));
14661	  break;
14662	case 2:
14663	  printf ("AltiVec\n");
14664	  break;
14665	case 3:
14666	  printf ("SPE\n");
14667	  break;
14668	}
14669      return p;
14670    }
14671
14672  if (tag == Tag_GNU_Power_ABI_Struct_Return)
14673    {
14674      val = read_uleb128 (p, &len, end);
14675      p += len;
14676      printf ("  Tag_GNU_Power_ABI_Struct_Return: ");
14677      if (len == 0)
14678	{
14679	  printf (_("<corrupt>\n"));
14680	  return p;
14681	}
14682
14683      if (val > 2)
14684	printf ("(%#x), ", val);
14685
14686      switch (val & 3)
14687	{
14688	case 0:
14689	  printf (_("unspecified\n"));
14690	  break;
14691	case 1:
14692	  printf ("r3/r4\n");
14693	  break;
14694	case 2:
14695	  printf (_("memory\n"));
14696	  break;
14697	case 3:
14698	  printf ("???\n");
14699	  break;
14700	}
14701      return p;
14702    }
14703
14704  return display_tag_value (tag & 1, p, end);
14705}
14706
14707static unsigned char *
14708display_s390_gnu_attribute (unsigned char * p,
14709			    unsigned int tag,
14710			    const unsigned char * const end)
14711{
14712  unsigned int len;
14713  int val;
14714
14715  if (tag == Tag_GNU_S390_ABI_Vector)
14716    {
14717      val = read_uleb128 (p, &len, end);
14718      p += len;
14719      printf ("  Tag_GNU_S390_ABI_Vector: ");
14720
14721      switch (val)
14722	{
14723	case 0:
14724	  printf (_("any\n"));
14725	  break;
14726	case 1:
14727	  printf (_("software\n"));
14728	  break;
14729	case 2:
14730	  printf (_("hardware\n"));
14731	  break;
14732	default:
14733	  printf ("??? (%d)\n", val);
14734	  break;
14735	}
14736      return p;
14737   }
14738
14739  return display_tag_value (tag & 1, p, end);
14740}
14741
14742static void
14743display_sparc_hwcaps (unsigned int mask)
14744{
14745  if (mask)
14746    {
14747      bfd_boolean first = TRUE;
14748
14749      if (mask & ELF_SPARC_HWCAP_MUL32)
14750	fputs ("mul32", stdout), first = FALSE;
14751      if (mask & ELF_SPARC_HWCAP_DIV32)
14752	printf ("%sdiv32", first ? "" : "|"), first = FALSE;
14753      if (mask & ELF_SPARC_HWCAP_FSMULD)
14754	printf ("%sfsmuld", first ? "" : "|"), first = FALSE;
14755      if (mask & ELF_SPARC_HWCAP_V8PLUS)
14756	printf ("%sv8plus", first ? "" : "|"), first = FALSE;
14757      if (mask & ELF_SPARC_HWCAP_POPC)
14758	printf ("%spopc", first ? "" : "|"), first = FALSE;
14759      if (mask & ELF_SPARC_HWCAP_VIS)
14760	printf ("%svis", first ? "" : "|"), first = FALSE;
14761      if (mask & ELF_SPARC_HWCAP_VIS2)
14762	printf ("%svis2", first ? "" : "|"), first = FALSE;
14763      if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
14764	printf ("%sASIBlkInit", first ? "" : "|"), first = FALSE;
14765      if (mask & ELF_SPARC_HWCAP_FMAF)
14766	printf ("%sfmaf", first ? "" : "|"), first = FALSE;
14767      if (mask & ELF_SPARC_HWCAP_VIS3)
14768	printf ("%svis3", first ? "" : "|"), first = FALSE;
14769      if (mask & ELF_SPARC_HWCAP_HPC)
14770	printf ("%shpc", first ? "" : "|"), first = FALSE;
14771      if (mask & ELF_SPARC_HWCAP_RANDOM)
14772	printf ("%srandom", first ? "" : "|"), first = FALSE;
14773      if (mask & ELF_SPARC_HWCAP_TRANS)
14774	printf ("%strans", first ? "" : "|"), first = FALSE;
14775      if (mask & ELF_SPARC_HWCAP_FJFMAU)
14776	printf ("%sfjfmau", first ? "" : "|"), first = FALSE;
14777      if (mask & ELF_SPARC_HWCAP_IMA)
14778	printf ("%sima", first ? "" : "|"), first = FALSE;
14779      if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
14780	printf ("%scspare", first ? "" : "|"), first = FALSE;
14781    }
14782  else
14783    fputc ('0', stdout);
14784  fputc ('\n', stdout);
14785}
14786
14787static void
14788display_sparc_hwcaps2 (unsigned int mask)
14789{
14790  if (mask)
14791    {
14792      bfd_boolean first = TRUE;
14793
14794      if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
14795	fputs ("fjathplus", stdout), first = FALSE;
14796      if (mask & ELF_SPARC_HWCAP2_VIS3B)
14797	printf ("%svis3b", first ? "" : "|"), first = FALSE;
14798      if (mask & ELF_SPARC_HWCAP2_ADP)
14799	printf ("%sadp", first ? "" : "|"), first = FALSE;
14800      if (mask & ELF_SPARC_HWCAP2_SPARC5)
14801	printf ("%ssparc5", first ? "" : "|"), first = FALSE;
14802      if (mask & ELF_SPARC_HWCAP2_MWAIT)
14803	printf ("%smwait", first ? "" : "|"), first = FALSE;
14804      if (mask & ELF_SPARC_HWCAP2_XMPMUL)
14805	printf ("%sxmpmul", first ? "" : "|"), first = FALSE;
14806      if (mask & ELF_SPARC_HWCAP2_XMONT)
14807	printf ("%sxmont2", first ? "" : "|"), first = FALSE;
14808      if (mask & ELF_SPARC_HWCAP2_NSEC)
14809	printf ("%snsec", first ? "" : "|"), first = FALSE;
14810      if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
14811	printf ("%sfjathhpc", first ? "" : "|"), first = FALSE;
14812      if (mask & ELF_SPARC_HWCAP2_FJDES)
14813	printf ("%sfjdes", first ? "" : "|"), first = FALSE;
14814      if (mask & ELF_SPARC_HWCAP2_FJAES)
14815	printf ("%sfjaes", first ? "" : "|"), first = FALSE;
14816    }
14817  else
14818    fputc ('0', stdout);
14819  fputc ('\n', stdout);
14820}
14821
14822static unsigned char *
14823display_sparc_gnu_attribute (unsigned char * p,
14824			     unsigned int tag,
14825			     const unsigned char * const end)
14826{
14827  unsigned int len;
14828  int val;
14829
14830  if (tag == Tag_GNU_Sparc_HWCAPS)
14831    {
14832      val = read_uleb128 (p, &len, end);
14833      p += len;
14834      printf ("  Tag_GNU_Sparc_HWCAPS: ");
14835      display_sparc_hwcaps (val);
14836      return p;
14837    }
14838  if (tag == Tag_GNU_Sparc_HWCAPS2)
14839    {
14840      val = read_uleb128 (p, &len, end);
14841      p += len;
14842      printf ("  Tag_GNU_Sparc_HWCAPS2: ");
14843      display_sparc_hwcaps2 (val);
14844      return p;
14845    }
14846
14847  return display_tag_value (tag, p, end);
14848}
14849
14850static void
14851print_mips_fp_abi_value (unsigned int val)
14852{
14853  switch (val)
14854    {
14855    case Val_GNU_MIPS_ABI_FP_ANY:
14856      printf (_("Hard or soft float\n"));
14857      break;
14858    case Val_GNU_MIPS_ABI_FP_DOUBLE:
14859      printf (_("Hard float (double precision)\n"));
14860      break;
14861    case Val_GNU_MIPS_ABI_FP_SINGLE:
14862      printf (_("Hard float (single precision)\n"));
14863      break;
14864    case Val_GNU_MIPS_ABI_FP_SOFT:
14865      printf (_("Soft float\n"));
14866      break;
14867    case Val_GNU_MIPS_ABI_FP_OLD_64:
14868      printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
14869      break;
14870    case Val_GNU_MIPS_ABI_FP_XX:
14871      printf (_("Hard float (32-bit CPU, Any FPU)\n"));
14872      break;
14873    case Val_GNU_MIPS_ABI_FP_64:
14874      printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
14875      break;
14876    case Val_GNU_MIPS_ABI_FP_64A:
14877      printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
14878      break;
14879    case Val_GNU_MIPS_ABI_FP_NAN2008:
14880      printf (_("NaN 2008 compatibility\n"));
14881      break;
14882    default:
14883      printf ("??? (%d)\n", val);
14884      break;
14885    }
14886}
14887
14888static unsigned char *
14889display_mips_gnu_attribute (unsigned char * p,
14890			    unsigned int tag,
14891			    const unsigned char * const end)
14892{
14893  if (tag == Tag_GNU_MIPS_ABI_FP)
14894    {
14895      unsigned int len;
14896      unsigned int val;
14897
14898      val = read_uleb128 (p, &len, end);
14899      p += len;
14900      printf ("  Tag_GNU_MIPS_ABI_FP: ");
14901
14902      print_mips_fp_abi_value (val);
14903
14904      return p;
14905   }
14906
14907  if (tag == Tag_GNU_MIPS_ABI_MSA)
14908    {
14909      unsigned int len;
14910      unsigned int val;
14911
14912      val = read_uleb128 (p, &len, end);
14913      p += len;
14914      printf ("  Tag_GNU_MIPS_ABI_MSA: ");
14915
14916      switch (val)
14917	{
14918	case Val_GNU_MIPS_ABI_MSA_ANY:
14919	  printf (_("Any MSA or not\n"));
14920	  break;
14921	case Val_GNU_MIPS_ABI_MSA_128:
14922	  printf (_("128-bit MSA\n"));
14923	  break;
14924	default:
14925	  printf ("??? (%d)\n", val);
14926	  break;
14927	}
14928      return p;
14929    }
14930
14931  return display_tag_value (tag & 1, p, end);
14932}
14933
14934static unsigned char *
14935display_tic6x_attribute (unsigned char * p,
14936			 const unsigned char * const end)
14937{
14938  unsigned int tag;
14939  unsigned int len;
14940  int val;
14941
14942  tag = read_uleb128 (p, &len, end);
14943  p += len;
14944
14945  switch (tag)
14946    {
14947    case Tag_ISA:
14948      val = read_uleb128 (p, &len, end);
14949      p += len;
14950      printf ("  Tag_ISA: ");
14951
14952      switch (val)
14953	{
14954	case C6XABI_Tag_ISA_none:
14955	  printf (_("None\n"));
14956	  break;
14957	case C6XABI_Tag_ISA_C62X:
14958	  printf ("C62x\n");
14959	  break;
14960	case C6XABI_Tag_ISA_C67X:
14961	  printf ("C67x\n");
14962	  break;
14963	case C6XABI_Tag_ISA_C67XP:
14964	  printf ("C67x+\n");
14965	  break;
14966	case C6XABI_Tag_ISA_C64X:
14967	  printf ("C64x\n");
14968	  break;
14969	case C6XABI_Tag_ISA_C64XP:
14970	  printf ("C64x+\n");
14971	  break;
14972	case C6XABI_Tag_ISA_C674X:
14973	  printf ("C674x\n");
14974	  break;
14975	default:
14976	  printf ("??? (%d)\n", val);
14977	  break;
14978	}
14979      return p;
14980
14981    case Tag_ABI_wchar_t:
14982      val = read_uleb128 (p, &len, end);
14983      p += len;
14984      printf ("  Tag_ABI_wchar_t: ");
14985      switch (val)
14986	{
14987	case 0:
14988	  printf (_("Not used\n"));
14989	  break;
14990	case 1:
14991	  printf (_("2 bytes\n"));
14992	  break;
14993	case 2:
14994	  printf (_("4 bytes\n"));
14995	  break;
14996	default:
14997	  printf ("??? (%d)\n", val);
14998	  break;
14999	}
15000      return p;
15001
15002    case Tag_ABI_stack_align_needed:
15003      val = read_uleb128 (p, &len, end);
15004      p += len;
15005      printf ("  Tag_ABI_stack_align_needed: ");
15006      switch (val)
15007	{
15008	case 0:
15009	  printf (_("8-byte\n"));
15010	  break;
15011	case 1:
15012	  printf (_("16-byte\n"));
15013	  break;
15014	default:
15015	  printf ("??? (%d)\n", val);
15016	  break;
15017	}
15018      return p;
15019
15020    case Tag_ABI_stack_align_preserved:
15021      val = read_uleb128 (p, &len, end);
15022      p += len;
15023      printf ("  Tag_ABI_stack_align_preserved: ");
15024      switch (val)
15025	{
15026	case 0:
15027	  printf (_("8-byte\n"));
15028	  break;
15029	case 1:
15030	  printf (_("16-byte\n"));
15031	  break;
15032	default:
15033	  printf ("??? (%d)\n", val);
15034	  break;
15035	}
15036      return p;
15037
15038    case Tag_ABI_DSBT:
15039      val = read_uleb128 (p, &len, end);
15040      p += len;
15041      printf ("  Tag_ABI_DSBT: ");
15042      switch (val)
15043	{
15044	case 0:
15045	  printf (_("DSBT addressing not used\n"));
15046	  break;
15047	case 1:
15048	  printf (_("DSBT addressing used\n"));
15049	  break;
15050	default:
15051	  printf ("??? (%d)\n", val);
15052	  break;
15053	}
15054      return p;
15055
15056    case Tag_ABI_PID:
15057      val = read_uleb128 (p, &len, end);
15058      p += len;
15059      printf ("  Tag_ABI_PID: ");
15060      switch (val)
15061	{
15062	case 0:
15063	  printf (_("Data addressing position-dependent\n"));
15064	  break;
15065	case 1:
15066	  printf (_("Data addressing position-independent, GOT near DP\n"));
15067	  break;
15068	case 2:
15069	  printf (_("Data addressing position-independent, GOT far from DP\n"));
15070	  break;
15071	default:
15072	  printf ("??? (%d)\n", val);
15073	  break;
15074	}
15075      return p;
15076
15077    case Tag_ABI_PIC:
15078      val = read_uleb128 (p, &len, end);
15079      p += len;
15080      printf ("  Tag_ABI_PIC: ");
15081      switch (val)
15082	{
15083	case 0:
15084	  printf (_("Code addressing position-dependent\n"));
15085	  break;
15086	case 1:
15087	  printf (_("Code addressing position-independent\n"));
15088	  break;
15089	default:
15090	  printf ("??? (%d)\n", val);
15091	  break;
15092	}
15093      return p;
15094
15095    case Tag_ABI_array_object_alignment:
15096      val = read_uleb128 (p, &len, end);
15097      p += len;
15098      printf ("  Tag_ABI_array_object_alignment: ");
15099      switch (val)
15100	{
15101	case 0:
15102	  printf (_("8-byte\n"));
15103	  break;
15104	case 1:
15105	  printf (_("4-byte\n"));
15106	  break;
15107	case 2:
15108	  printf (_("16-byte\n"));
15109	  break;
15110	default:
15111	  printf ("??? (%d)\n", val);
15112	  break;
15113	}
15114      return p;
15115
15116    case Tag_ABI_array_object_align_expected:
15117      val = read_uleb128 (p, &len, end);
15118      p += len;
15119      printf ("  Tag_ABI_array_object_align_expected: ");
15120      switch (val)
15121	{
15122	case 0:
15123	  printf (_("8-byte\n"));
15124	  break;
15125	case 1:
15126	  printf (_("4-byte\n"));
15127	  break;
15128	case 2:
15129	  printf (_("16-byte\n"));
15130	  break;
15131	default:
15132	  printf ("??? (%d)\n", val);
15133	  break;
15134	}
15135      return p;
15136
15137    case Tag_ABI_compatibility:
15138      {
15139	val = read_uleb128 (p, &len, end);
15140	p += len;
15141	printf ("  Tag_ABI_compatibility: ");
15142	printf (_("flag = %d, vendor = "), val);
15143	if (p < end - 1)
15144	  {
15145	    size_t maxlen = (end - p) - 1;
15146
15147	    print_symbol ((int) maxlen, (const char *) p);
15148	    p += strnlen ((char *) p, maxlen) + 1;
15149	  }
15150	else
15151	  {
15152	    printf (_("<corrupt>"));
15153	    p = (unsigned char *) end;
15154	  }
15155	putchar ('\n');
15156	return p;
15157      }
15158
15159    case Tag_ABI_conformance:
15160      {
15161	printf ("  Tag_ABI_conformance: \"");
15162	if (p < end - 1)
15163	  {
15164	    size_t maxlen = (end - p) - 1;
15165
15166	    print_symbol ((int) maxlen, (const char *) p);
15167	    p += strnlen ((char *) p, maxlen) + 1;
15168	  }
15169	else
15170	  {
15171	    printf (_("<corrupt>"));
15172	    p = (unsigned char *) end;
15173	  }
15174	printf ("\"\n");
15175	return p;
15176      }
15177    }
15178
15179  return display_tag_value (tag, p, end);
15180}
15181
15182static void
15183display_raw_attribute (unsigned char * p, unsigned char const * const end)
15184{
15185  unsigned long addr = 0;
15186  size_t bytes = end - p;
15187
15188  assert (end > p);
15189  while (bytes)
15190    {
15191      int j;
15192      int k;
15193      int lbytes = (bytes > 16 ? 16 : bytes);
15194
15195      printf ("  0x%8.8lx ", addr);
15196
15197      for (j = 0; j < 16; j++)
15198	{
15199	  if (j < lbytes)
15200	    printf ("%2.2x", p[j]);
15201	  else
15202	    printf ("  ");
15203
15204	  if ((j & 3) == 3)
15205	    printf (" ");
15206	}
15207
15208      for (j = 0; j < lbytes; j++)
15209	{
15210	  k = p[j];
15211	  if (k >= ' ' && k < 0x7f)
15212	    printf ("%c", k);
15213	  else
15214	    printf (".");
15215	}
15216
15217      putchar ('\n');
15218
15219      p  += lbytes;
15220      bytes -= lbytes;
15221      addr += lbytes;
15222    }
15223
15224  putchar ('\n');
15225}
15226
15227static unsigned char *
15228display_msp430x_attribute (unsigned char * p,
15229			   const unsigned char * const end)
15230{
15231  unsigned int len;
15232  unsigned int val;
15233  unsigned int tag;
15234
15235  tag = read_uleb128 (p, & len, end);
15236  p += len;
15237
15238  switch (tag)
15239    {
15240    case OFBA_MSPABI_Tag_ISA:
15241      val = read_uleb128 (p, &len, end);
15242      p += len;
15243      printf ("  Tag_ISA: ");
15244      switch (val)
15245	{
15246	case 0: printf (_("None\n")); break;
15247	case 1: printf (_("MSP430\n")); break;
15248	case 2: printf (_("MSP430X\n")); break;
15249	default: printf ("??? (%d)\n", val); break;
15250	}
15251      break;
15252
15253    case OFBA_MSPABI_Tag_Code_Model:
15254      val = read_uleb128 (p, &len, end);
15255      p += len;
15256      printf ("  Tag_Code_Model: ");
15257      switch (val)
15258	{
15259	case 0: printf (_("None\n")); break;
15260	case 1: printf (_("Small\n")); break;
15261	case 2: printf (_("Large\n")); break;
15262	default: printf ("??? (%d)\n", val); break;
15263	}
15264      break;
15265
15266    case OFBA_MSPABI_Tag_Data_Model:
15267      val = read_uleb128 (p, &len, end);
15268      p += len;
15269      printf ("  Tag_Data_Model: ");
15270      switch (val)
15271	{
15272	case 0: printf (_("None\n")); break;
15273	case 1: printf (_("Small\n")); break;
15274	case 2: printf (_("Large\n")); break;
15275	case 3: printf (_("Restricted Large\n")); break;
15276	default: printf ("??? (%d)\n", val); break;
15277	}
15278      break;
15279
15280    default:
15281      printf (_("  <unknown tag %d>: "), tag);
15282
15283      if (tag & 1)
15284	{
15285	  putchar ('"');
15286	  if (p < end - 1)
15287	    {
15288	      size_t maxlen = (end - p) - 1;
15289
15290	      print_symbol ((int) maxlen, (const char *) p);
15291	      p += strnlen ((char *) p, maxlen) + 1;
15292	    }
15293	  else
15294	    {
15295	      printf (_("<corrupt>"));
15296	      p = (unsigned char *) end;
15297	    }
15298	  printf ("\"\n");
15299	}
15300      else
15301	{
15302	  val = read_uleb128 (p, &len, end);
15303	  p += len;
15304	  printf ("%d (0x%x)\n", val, val);
15305	}
15306      break;
15307   }
15308
15309  assert (p <= end);
15310  return p;
15311}
15312
15313static bfd_boolean
15314process_attributes (Filedata * filedata,
15315		    const char * public_name,
15316		    unsigned int proc_type,
15317		    unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
15318		    unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
15319{
15320  Elf_Internal_Shdr * sect;
15321  unsigned i;
15322  bfd_boolean res = TRUE;
15323
15324  /* Find the section header so that we get the size.  */
15325  for (i = 0, sect = filedata->section_headers;
15326       i < filedata->file_header.e_shnum;
15327       i++, sect++)
15328    {
15329      unsigned char * contents;
15330      unsigned char * p;
15331
15332      if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
15333	continue;
15334
15335      contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
15336                                             sect->sh_size, _("attributes"));
15337      if (contents == NULL)
15338	{
15339	  res = FALSE;
15340	  continue;
15341	}
15342
15343      p = contents;
15344      /* The first character is the version of the attributes.
15345	 Currently only version 1, (aka 'A') is recognised here.  */
15346      if (*p != 'A')
15347	{
15348	  printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
15349	  res = FALSE;
15350	}
15351      else
15352	{
15353	  bfd_vma section_len;
15354
15355	  section_len = sect->sh_size - 1;
15356	  p++;
15357
15358	  while (section_len > 0)
15359	    {
15360	      bfd_vma attr_len;
15361	      unsigned int namelen;
15362	      bfd_boolean public_section;
15363	      bfd_boolean gnu_section;
15364
15365	      if (section_len <= 4)
15366		{
15367		  error (_("Tag section ends prematurely\n"));
15368		  res = FALSE;
15369		  break;
15370		}
15371	      attr_len = byte_get (p, 4);
15372	      p += 4;
15373
15374	      if (attr_len > section_len)
15375		{
15376		  error (_("Bad attribute length (%u > %u)\n"),
15377			  (unsigned) attr_len, (unsigned) section_len);
15378		  attr_len = section_len;
15379		  res = FALSE;
15380		}
15381	      /* PR 17531: file: 001-101425-0.004  */
15382	      else if (attr_len < 5)
15383		{
15384		  error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
15385		  res = FALSE;
15386		  break;
15387		}
15388
15389	      section_len -= attr_len;
15390	      attr_len -= 4;
15391
15392	      namelen = strnlen ((char *) p, attr_len) + 1;
15393	      if (namelen == 0 || namelen >= attr_len)
15394		{
15395		  error (_("Corrupt attribute section name\n"));
15396		  res = FALSE;
15397		  break;
15398		}
15399
15400	      printf (_("Attribute Section: "));
15401	      print_symbol (INT_MAX, (const char *) p);
15402	      putchar ('\n');
15403
15404	      if (public_name && streq ((char *) p, public_name))
15405		public_section = TRUE;
15406	      else
15407		public_section = FALSE;
15408
15409	      if (streq ((char *) p, "gnu"))
15410		gnu_section = TRUE;
15411	      else
15412		gnu_section = FALSE;
15413
15414	      p += namelen;
15415	      attr_len -= namelen;
15416
15417	      while (attr_len > 0 && p < contents + sect->sh_size)
15418		{
15419		  int tag;
15420		  int val;
15421		  bfd_vma size;
15422		  unsigned char * end;
15423
15424		  /* PR binutils/17531: Safe handling of corrupt files.  */
15425		  if (attr_len < 6)
15426		    {
15427		      error (_("Unused bytes at end of section\n"));
15428		      res = FALSE;
15429		      section_len = 0;
15430		      break;
15431		    }
15432
15433		  tag = *(p++);
15434		  size = byte_get (p, 4);
15435		  if (size > attr_len)
15436		    {
15437		      error (_("Bad subsection length (%u > %u)\n"),
15438			      (unsigned) size, (unsigned) attr_len);
15439		      res = FALSE;
15440		      size = attr_len;
15441		    }
15442		  /* PR binutils/17531: Safe handling of corrupt files.  */
15443		  if (size < 6)
15444		    {
15445		      error (_("Bad subsection length (%u < 6)\n"),
15446			      (unsigned) size);
15447		      res = FALSE;
15448		      section_len = 0;
15449		      break;
15450		    }
15451
15452		  attr_len -= size;
15453		  end = p + size - 1;
15454		  assert (end <= contents + sect->sh_size);
15455		  p += 4;
15456
15457		  switch (tag)
15458		    {
15459		    case 1:
15460		      printf (_("File Attributes\n"));
15461		      break;
15462		    case 2:
15463		      printf (_("Section Attributes:"));
15464		      goto do_numlist;
15465		    case 3:
15466		      printf (_("Symbol Attributes:"));
15467		      /* Fall through.  */
15468		    do_numlist:
15469		      for (;;)
15470			{
15471			  unsigned int j;
15472
15473			  val = read_uleb128 (p, &j, end);
15474			  p += j;
15475			  if (val == 0)
15476			    break;
15477			  printf (" %d", val);
15478			}
15479		      printf ("\n");
15480		      break;
15481		    default:
15482		      printf (_("Unknown tag: %d\n"), tag);
15483		      public_section = FALSE;
15484		      break;
15485		    }
15486
15487		  if (public_section && display_pub_attribute != NULL)
15488		    {
15489		      while (p < end)
15490			p = display_pub_attribute (p, end);
15491		      assert (p == end);
15492		    }
15493		  else if (gnu_section && display_proc_gnu_attribute != NULL)
15494		    {
15495		      while (p < end)
15496			p = display_gnu_attribute (p,
15497						   display_proc_gnu_attribute,
15498						   end);
15499		      assert (p == end);
15500		    }
15501		  else if (p < end)
15502		    {
15503		      printf (_("  Unknown attribute:\n"));
15504		      display_raw_attribute (p, end);
15505		      p = end;
15506		    }
15507		  else
15508		    attr_len = 0;
15509		}
15510	    }
15511	}
15512
15513      free (contents);
15514    }
15515
15516  return res;
15517}
15518
15519/* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
15520   Print the Address, Access and Initial fields of an entry at VMA ADDR
15521   and return the VMA of the next entry, or -1 if there was a problem.
15522   Does not read from DATA_END or beyond.  */
15523
15524static bfd_vma
15525print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
15526		      unsigned char * data_end)
15527{
15528  printf ("  ");
15529  print_vma (addr, LONG_HEX);
15530  printf (" ");
15531  if (addr < pltgot + 0xfff0)
15532    printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
15533  else
15534    printf ("%10s", "");
15535  printf (" ");
15536  if (data == NULL)
15537    printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
15538  else
15539    {
15540      bfd_vma entry;
15541      unsigned char * from = data + addr - pltgot;
15542
15543      if (from + (is_32bit_elf ? 4 : 8) > data_end)
15544	{
15545	  warn (_("MIPS GOT entry extends beyond the end of available data\n"));
15546	  printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
15547	  return (bfd_vma) -1;
15548	}
15549      else
15550	{
15551	  entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
15552	  print_vma (entry, LONG_HEX);
15553	}
15554    }
15555  return addr + (is_32bit_elf ? 4 : 8);
15556}
15557
15558/* DATA points to the contents of a MIPS PLT GOT that starts at VMA
15559   PLTGOT.  Print the Address and Initial fields of an entry at VMA
15560   ADDR and return the VMA of the next entry.  */
15561
15562static bfd_vma
15563print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
15564{
15565  printf ("  ");
15566  print_vma (addr, LONG_HEX);
15567  printf (" ");
15568  if (data == NULL)
15569    printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
15570  else
15571    {
15572      bfd_vma entry;
15573
15574      entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
15575      print_vma (entry, LONG_HEX);
15576    }
15577  return addr + (is_32bit_elf ? 4 : 8);
15578}
15579
15580static void
15581print_mips_ases (unsigned int mask)
15582{
15583  if (mask & AFL_ASE_DSP)
15584    fputs ("\n\tDSP ASE", stdout);
15585  if (mask & AFL_ASE_DSPR2)
15586    fputs ("\n\tDSP R2 ASE", stdout);
15587  if (mask & AFL_ASE_DSPR3)
15588    fputs ("\n\tDSP R3 ASE", stdout);
15589  if (mask & AFL_ASE_EVA)
15590    fputs ("\n\tEnhanced VA Scheme", stdout);
15591  if (mask & AFL_ASE_MCU)
15592    fputs ("\n\tMCU (MicroController) ASE", stdout);
15593  if (mask & AFL_ASE_MDMX)
15594    fputs ("\n\tMDMX ASE", stdout);
15595  if (mask & AFL_ASE_MIPS3D)
15596    fputs ("\n\tMIPS-3D ASE", stdout);
15597  if (mask & AFL_ASE_MT)
15598    fputs ("\n\tMT ASE", stdout);
15599  if (mask & AFL_ASE_SMARTMIPS)
15600    fputs ("\n\tSmartMIPS ASE", stdout);
15601  if (mask & AFL_ASE_VIRT)
15602    fputs ("\n\tVZ ASE", stdout);
15603  if (mask & AFL_ASE_MSA)
15604    fputs ("\n\tMSA ASE", stdout);
15605  if (mask & AFL_ASE_MIPS16)
15606    fputs ("\n\tMIPS16 ASE", stdout);
15607  if (mask & AFL_ASE_MICROMIPS)
15608    fputs ("\n\tMICROMIPS ASE", stdout);
15609  if (mask & AFL_ASE_XPA)
15610    fputs ("\n\tXPA ASE", stdout);
15611  if (mask & AFL_ASE_MIPS16E2)
15612    fputs ("\n\tMIPS16e2 ASE", stdout);
15613  if (mask & AFL_ASE_CRC)
15614    fputs ("\n\tCRC ASE", stdout);
15615  if (mask & AFL_ASE_GINV)
15616    fputs ("\n\tGINV ASE", stdout);
15617  if (mask == 0)
15618    fprintf (stdout, "\n\t%s", _("None"));
15619  else if ((mask & ~AFL_ASE_MASK) != 0)
15620    fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
15621}
15622
15623static void
15624print_mips_isa_ext (unsigned int isa_ext)
15625{
15626  switch (isa_ext)
15627    {
15628    case 0:
15629      fputs (_("None"), stdout);
15630      break;
15631    case AFL_EXT_XLR:
15632      fputs ("RMI XLR", stdout);
15633      break;
15634    case AFL_EXT_OCTEON3:
15635      fputs ("Cavium Networks Octeon3", stdout);
15636      break;
15637    case AFL_EXT_OCTEON2:
15638      fputs ("Cavium Networks Octeon2", stdout);
15639      break;
15640    case AFL_EXT_OCTEONP:
15641      fputs ("Cavium Networks OcteonP", stdout);
15642      break;
15643    case AFL_EXT_LOONGSON_3A:
15644      fputs ("Loongson 3A", stdout);
15645      break;
15646    case AFL_EXT_OCTEON:
15647      fputs ("Cavium Networks Octeon", stdout);
15648      break;
15649    case AFL_EXT_5900:
15650      fputs ("Toshiba R5900", stdout);
15651      break;
15652    case AFL_EXT_4650:
15653      fputs ("MIPS R4650", stdout);
15654      break;
15655    case AFL_EXT_4010:
15656      fputs ("LSI R4010", stdout);
15657      break;
15658    case AFL_EXT_4100:
15659      fputs ("NEC VR4100", stdout);
15660      break;
15661    case AFL_EXT_3900:
15662      fputs ("Toshiba R3900", stdout);
15663      break;
15664    case AFL_EXT_10000:
15665      fputs ("MIPS R10000", stdout);
15666      break;
15667    case AFL_EXT_SB1:
15668      fputs ("Broadcom SB-1", stdout);
15669      break;
15670    case AFL_EXT_4111:
15671      fputs ("NEC VR4111/VR4181", stdout);
15672      break;
15673    case AFL_EXT_4120:
15674      fputs ("NEC VR4120", stdout);
15675      break;
15676    case AFL_EXT_5400:
15677      fputs ("NEC VR5400", stdout);
15678      break;
15679    case AFL_EXT_5500:
15680      fputs ("NEC VR5500", stdout);
15681      break;
15682    case AFL_EXT_LOONGSON_2E:
15683      fputs ("ST Microelectronics Loongson 2E", stdout);
15684      break;
15685    case AFL_EXT_LOONGSON_2F:
15686      fputs ("ST Microelectronics Loongson 2F", stdout);
15687      break;
15688    case AFL_EXT_INTERAPTIV_MR2:
15689      fputs ("Imagination interAptiv MR2", stdout);
15690      break;
15691    default:
15692      fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
15693    }
15694}
15695
15696static signed int
15697get_mips_reg_size (int reg_size)
15698{
15699  return (reg_size == AFL_REG_NONE) ? 0
15700	 : (reg_size == AFL_REG_32) ? 32
15701	 : (reg_size == AFL_REG_64) ? 64
15702	 : (reg_size == AFL_REG_128) ? 128
15703	 : -1;
15704}
15705
15706static bfd_boolean
15707process_mips_specific (Filedata * filedata)
15708{
15709  Elf_Internal_Dyn * entry;
15710  Elf_Internal_Shdr *sect = NULL;
15711  size_t liblist_offset = 0;
15712  size_t liblistno = 0;
15713  size_t conflictsno = 0;
15714  size_t options_offset = 0;
15715  size_t conflicts_offset = 0;
15716  size_t pltrelsz = 0;
15717  size_t pltrel = 0;
15718  bfd_vma pltgot = 0;
15719  bfd_vma mips_pltgot = 0;
15720  bfd_vma jmprel = 0;
15721  bfd_vma local_gotno = 0;
15722  bfd_vma gotsym = 0;
15723  bfd_vma symtabno = 0;
15724  bfd_boolean res = TRUE;
15725
15726  if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
15727			    display_mips_gnu_attribute))
15728    res = FALSE;
15729
15730  sect = find_section (filedata, ".MIPS.abiflags");
15731
15732  if (sect != NULL)
15733    {
15734      Elf_External_ABIFlags_v0 *abiflags_ext;
15735      Elf_Internal_ABIFlags_v0 abiflags_in;
15736
15737      if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
15738	{
15739	  error (_("Corrupt MIPS ABI Flags section.\n"));
15740	  res = FALSE;
15741	}
15742      else
15743	{
15744	  abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
15745				   sect->sh_size, _("MIPS ABI Flags section"));
15746	  if (abiflags_ext)
15747	    {
15748	      abiflags_in.version = BYTE_GET (abiflags_ext->version);
15749	      abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
15750	      abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
15751	      abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
15752	      abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
15753	      abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
15754	      abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
15755	      abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
15756	      abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
15757	      abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
15758	      abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
15759
15760	      printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
15761	      printf ("\nISA: MIPS%d", abiflags_in.isa_level);
15762	      if (abiflags_in.isa_rev > 1)
15763		printf ("r%d", abiflags_in.isa_rev);
15764	      printf ("\nGPR size: %d",
15765		      get_mips_reg_size (abiflags_in.gpr_size));
15766	      printf ("\nCPR1 size: %d",
15767		      get_mips_reg_size (abiflags_in.cpr1_size));
15768	      printf ("\nCPR2 size: %d",
15769		      get_mips_reg_size (abiflags_in.cpr2_size));
15770	      fputs ("\nFP ABI: ", stdout);
15771	      print_mips_fp_abi_value (abiflags_in.fp_abi);
15772	      fputs ("ISA Extension: ", stdout);
15773	      print_mips_isa_ext (abiflags_in.isa_ext);
15774	      fputs ("\nASEs:", stdout);
15775	      print_mips_ases (abiflags_in.ases);
15776	      printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
15777	      printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
15778	      fputc ('\n', stdout);
15779	      free (abiflags_ext);
15780	    }
15781	}
15782    }
15783
15784  /* We have a lot of special sections.  Thanks SGI!  */
15785  if (dynamic_section == NULL)
15786    {
15787      /* No dynamic information available.  See if there is static GOT.  */
15788      sect = find_section (filedata, ".got");
15789      if (sect != NULL)
15790	{
15791	  unsigned char *data_end;
15792	  unsigned char *data;
15793	  bfd_vma ent, end;
15794	  int addr_size;
15795
15796	  pltgot = sect->sh_addr;
15797
15798	  ent = pltgot;
15799	  addr_size = (is_32bit_elf ? 4 : 8);
15800	  end = pltgot + sect->sh_size;
15801
15802	  data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
15803					     end - pltgot, 1,
15804					     _("Global Offset Table data"));
15805	  /* PR 12855: Null data is handled gracefully throughout.  */
15806	  data_end = data + (end - pltgot);
15807
15808	  printf (_("\nStatic GOT:\n"));
15809	  printf (_(" Canonical gp value: "));
15810	  print_vma (ent + 0x7ff0, LONG_HEX);
15811	  printf ("\n\n");
15812
15813	  /* In a dynamic binary GOT[0] is reserved for the dynamic
15814	     loader to store the lazy resolver pointer, however in
15815	     a static binary it may well have been omitted and GOT
15816	     reduced to a table of addresses.
15817	     PR 21344: Check for the entry being fully available
15818	     before fetching it.  */
15819	  if (data
15820	      && data + ent - pltgot + addr_size <= data_end
15821	      && byte_get (data + ent - pltgot, addr_size) == 0)
15822	    {
15823	      printf (_(" Reserved entries:\n"));
15824	      printf (_("  %*s %10s %*s\n"),
15825		      addr_size * 2, _("Address"), _("Access"),
15826		      addr_size * 2, _("Value"));
15827	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
15828	      printf ("\n");
15829	      if (ent == (bfd_vma) -1)
15830		goto sgot_print_fail;
15831
15832	      /* Check for the MSB of GOT[1] being set, identifying a
15833		 GNU object.  This entry will be used by some runtime
15834		 loaders, to store the module pointer.  Otherwise this
15835		 is an ordinary local entry.
15836		 PR 21344: Check for the entry being fully available
15837		 before fetching it.  */
15838	      if (data
15839		  && data + ent - pltgot + addr_size <= data_end
15840		  && (byte_get (data + ent - pltgot, addr_size)
15841		      >> (addr_size * 8 - 1)) != 0)
15842		{
15843		  ent = print_mips_got_entry (data, pltgot, ent, data_end);
15844		  printf ("\n");
15845		  if (ent == (bfd_vma) -1)
15846		    goto sgot_print_fail;
15847		}
15848	      printf ("\n");
15849	    }
15850
15851	  if (data != NULL && ent < end)
15852	    {
15853	      printf (_(" Local entries:\n"));
15854	      printf ("  %*s %10s %*s\n",
15855		      addr_size * 2, _("Address"), _("Access"),
15856		      addr_size * 2, _("Value"));
15857	      while (ent < end)
15858		{
15859		  ent = print_mips_got_entry (data, pltgot, ent, data_end);
15860		  printf ("\n");
15861		  if (ent == (bfd_vma) -1)
15862		    goto sgot_print_fail;
15863		}
15864	      printf ("\n");
15865	    }
15866
15867	sgot_print_fail:
15868	  if (data)
15869	    free (data);
15870	}
15871      return res;
15872    }
15873
15874  for (entry = dynamic_section;
15875       /* PR 17531 file: 012-50589-0.004.  */
15876       entry < dynamic_section + dynamic_nent && entry->d_tag != DT_NULL;
15877       ++entry)
15878    switch (entry->d_tag)
15879      {
15880      case DT_MIPS_LIBLIST:
15881	liblist_offset
15882	  = offset_from_vma (filedata, entry->d_un.d_val,
15883			     liblistno * sizeof (Elf32_External_Lib));
15884	break;
15885      case DT_MIPS_LIBLISTNO:
15886	liblistno = entry->d_un.d_val;
15887	break;
15888      case DT_MIPS_OPTIONS:
15889	options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
15890	break;
15891      case DT_MIPS_CONFLICT:
15892	conflicts_offset
15893	  = offset_from_vma (filedata, entry->d_un.d_val,
15894			     conflictsno * sizeof (Elf32_External_Conflict));
15895	break;
15896      case DT_MIPS_CONFLICTNO:
15897	conflictsno = entry->d_un.d_val;
15898	break;
15899      case DT_PLTGOT:
15900	pltgot = entry->d_un.d_ptr;
15901	break;
15902      case DT_MIPS_LOCAL_GOTNO:
15903	local_gotno = entry->d_un.d_val;
15904	break;
15905      case DT_MIPS_GOTSYM:
15906	gotsym = entry->d_un.d_val;
15907	break;
15908      case DT_MIPS_SYMTABNO:
15909	symtabno = entry->d_un.d_val;
15910	break;
15911      case DT_MIPS_PLTGOT:
15912	mips_pltgot = entry->d_un.d_ptr;
15913	break;
15914      case DT_PLTREL:
15915	pltrel = entry->d_un.d_val;
15916	break;
15917      case DT_PLTRELSZ:
15918	pltrelsz = entry->d_un.d_val;
15919	break;
15920      case DT_JMPREL:
15921	jmprel = entry->d_un.d_ptr;
15922	break;
15923      default:
15924	break;
15925      }
15926
15927  if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
15928    {
15929      Elf32_External_Lib * elib;
15930      size_t cnt;
15931
15932      elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
15933                                              liblistno,
15934                                              sizeof (Elf32_External_Lib),
15935                                              _("liblist section data"));
15936      if (elib)
15937	{
15938	  printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
15939			    "\nSection '.liblist' contains %lu entries:\n",
15940			    (unsigned long) liblistno),
15941		  (unsigned long) liblistno);
15942	  fputs (_("     Library              Time Stamp          Checksum   Version Flags\n"),
15943		 stdout);
15944
15945	  for (cnt = 0; cnt < liblistno; ++cnt)
15946	    {
15947	      Elf32_Lib liblist;
15948	      time_t atime;
15949	      char timebuf[128];
15950	      struct tm * tmp;
15951
15952	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
15953	      atime = BYTE_GET (elib[cnt].l_time_stamp);
15954	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
15955	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
15956	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
15957
15958	      tmp = gmtime (&atime);
15959	      snprintf (timebuf, sizeof (timebuf),
15960			"%04u-%02u-%02uT%02u:%02u:%02u",
15961			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
15962			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
15963
15964	      printf ("%3lu: ", (unsigned long) cnt);
15965	      if (VALID_DYNAMIC_NAME (liblist.l_name))
15966		print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
15967	      else
15968		printf (_("<corrupt: %9ld>"), liblist.l_name);
15969	      printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
15970		      liblist.l_version);
15971
15972	      if (liblist.l_flags == 0)
15973		puts (_(" NONE"));
15974	      else
15975		{
15976		  static const struct
15977		  {
15978		    const char * name;
15979		    int bit;
15980		  }
15981		  l_flags_vals[] =
15982		  {
15983		    { " EXACT_MATCH", LL_EXACT_MATCH },
15984		    { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
15985		    { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
15986		    { " EXPORTS", LL_EXPORTS },
15987		    { " DELAY_LOAD", LL_DELAY_LOAD },
15988		    { " DELTA", LL_DELTA }
15989		  };
15990		  int flags = liblist.l_flags;
15991		  size_t fcnt;
15992
15993		  for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
15994		    if ((flags & l_flags_vals[fcnt].bit) != 0)
15995		      {
15996			fputs (l_flags_vals[fcnt].name, stdout);
15997			flags ^= l_flags_vals[fcnt].bit;
15998		      }
15999		  if (flags != 0)
16000		    printf (" %#x", (unsigned int) flags);
16001
16002		  puts ("");
16003		}
16004	    }
16005
16006	  free (elib);
16007	}
16008      else
16009	res = FALSE;
16010    }
16011
16012  if (options_offset != 0)
16013    {
16014      Elf_External_Options * eopt;
16015      Elf_Internal_Options * iopt;
16016      Elf_Internal_Options * option;
16017      size_t offset;
16018      int cnt;
16019      sect = filedata->section_headers;
16020
16021      /* Find the section header so that we get the size.  */
16022      sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
16023      /* PR 17533 file: 012-277276-0.004.  */
16024      if (sect == NULL)
16025	{
16026	  error (_("No MIPS_OPTIONS header found\n"));
16027	  return FALSE;
16028	}
16029
16030      eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
16031                                                sect->sh_size, _("options"));
16032      if (eopt)
16033	{
16034	  iopt = (Elf_Internal_Options *)
16035              cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (* iopt));
16036	  if (iopt == NULL)
16037	    {
16038	      error (_("Out of memory allocating space for MIPS options\n"));
16039	      return FALSE;
16040	    }
16041
16042	  offset = cnt = 0;
16043	  option = iopt;
16044
16045	  while (offset <= sect->sh_size - sizeof (* eopt))
16046	    {
16047	      Elf_External_Options * eoption;
16048
16049	      eoption = (Elf_External_Options *) ((char *) eopt + offset);
16050
16051	      option->kind = BYTE_GET (eoption->kind);
16052	      option->size = BYTE_GET (eoption->size);
16053	      option->section = BYTE_GET (eoption->section);
16054	      option->info = BYTE_GET (eoption->info);
16055
16056	      /* PR 17531: file: ffa0fa3b.  */
16057	      if (option->size < sizeof (* eopt)
16058		  || offset + option->size > sect->sh_size)
16059		{
16060		  error (_("Invalid size (%u) for MIPS option\n"), option->size);
16061		  return FALSE;
16062		}
16063	      offset += option->size;
16064
16065	      ++option;
16066	      ++cnt;
16067	    }
16068
16069	  printf (ngettext ("\nSection '%s' contains %d entry:\n",
16070			    "\nSection '%s' contains %d entries:\n",
16071			    cnt),
16072		  printable_section_name (filedata, sect), cnt);
16073
16074	  option = iopt;
16075	  offset = 0;
16076
16077	  while (cnt-- > 0)
16078	    {
16079	      size_t len;
16080
16081	      switch (option->kind)
16082		{
16083		case ODK_NULL:
16084		  /* This shouldn't happen.  */
16085		  printf (" NULL       %d %lx", option->section, option->info);
16086		  break;
16087		case ODK_REGINFO:
16088		  printf (" REGINFO    ");
16089		  if (filedata->file_header.e_machine == EM_MIPS)
16090		    {
16091		      /* 32bit form.  */
16092		      Elf32_External_RegInfo * ereg;
16093		      Elf32_RegInfo reginfo;
16094
16095		      ereg = (Elf32_External_RegInfo *) (option + 1);
16096		      reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
16097		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
16098		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
16099		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
16100		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
16101		      reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
16102
16103		      printf ("GPR %08lx  GP 0x%lx\n",
16104			      reginfo.ri_gprmask,
16105			      (unsigned long) reginfo.ri_gp_value);
16106		      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
16107			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
16108			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
16109		    }
16110		  else
16111		    {
16112		      /* 64 bit form.  */
16113		      Elf64_External_RegInfo * ereg;
16114		      Elf64_Internal_RegInfo reginfo;
16115
16116		      ereg = (Elf64_External_RegInfo *) (option + 1);
16117		      reginfo.ri_gprmask    = BYTE_GET (ereg->ri_gprmask);
16118		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
16119		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
16120		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
16121		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
16122		      reginfo.ri_gp_value   = BYTE_GET (ereg->ri_gp_value);
16123
16124		      printf ("GPR %08lx  GP 0x",
16125			      reginfo.ri_gprmask);
16126		      printf_vma (reginfo.ri_gp_value);
16127		      printf ("\n");
16128
16129		      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
16130			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
16131			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
16132		    }
16133		  ++option;
16134		  continue;
16135		case ODK_EXCEPTIONS:
16136		  fputs (" EXCEPTIONS fpe_min(", stdout);
16137		  process_mips_fpe_exception (option->info & OEX_FPU_MIN);
16138		  fputs (") fpe_max(", stdout);
16139		  process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
16140		  fputs (")", stdout);
16141
16142		  if (option->info & OEX_PAGE0)
16143		    fputs (" PAGE0", stdout);
16144		  if (option->info & OEX_SMM)
16145		    fputs (" SMM", stdout);
16146		  if (option->info & OEX_FPDBUG)
16147		    fputs (" FPDBUG", stdout);
16148		  if (option->info & OEX_DISMISS)
16149		    fputs (" DISMISS", stdout);
16150		  break;
16151		case ODK_PAD:
16152		  fputs (" PAD       ", stdout);
16153		  if (option->info & OPAD_PREFIX)
16154		    fputs (" PREFIX", stdout);
16155		  if (option->info & OPAD_POSTFIX)
16156		    fputs (" POSTFIX", stdout);
16157		  if (option->info & OPAD_SYMBOL)
16158		    fputs (" SYMBOL", stdout);
16159		  break;
16160		case ODK_HWPATCH:
16161		  fputs (" HWPATCH   ", stdout);
16162		  if (option->info & OHW_R4KEOP)
16163		    fputs (" R4KEOP", stdout);
16164		  if (option->info & OHW_R8KPFETCH)
16165		    fputs (" R8KPFETCH", stdout);
16166		  if (option->info & OHW_R5KEOP)
16167		    fputs (" R5KEOP", stdout);
16168		  if (option->info & OHW_R5KCVTL)
16169		    fputs (" R5KCVTL", stdout);
16170		  break;
16171		case ODK_FILL:
16172		  fputs (" FILL       ", stdout);
16173		  /* XXX Print content of info word?  */
16174		  break;
16175		case ODK_TAGS:
16176		  fputs (" TAGS       ", stdout);
16177		  /* XXX Print content of info word?  */
16178		  break;
16179		case ODK_HWAND:
16180		  fputs (" HWAND     ", stdout);
16181		  if (option->info & OHWA0_R4KEOP_CHECKED)
16182		    fputs (" R4KEOP_CHECKED", stdout);
16183		  if (option->info & OHWA0_R4KEOP_CLEAN)
16184		    fputs (" R4KEOP_CLEAN", stdout);
16185		  break;
16186		case ODK_HWOR:
16187		  fputs (" HWOR      ", stdout);
16188		  if (option->info & OHWA0_R4KEOP_CHECKED)
16189		    fputs (" R4KEOP_CHECKED", stdout);
16190		  if (option->info & OHWA0_R4KEOP_CLEAN)
16191		    fputs (" R4KEOP_CLEAN", stdout);
16192		  break;
16193		case ODK_GP_GROUP:
16194		  printf (" GP_GROUP  %#06lx  self-contained %#06lx",
16195			  option->info & OGP_GROUP,
16196			  (option->info & OGP_SELF) >> 16);
16197		  break;
16198		case ODK_IDENT:
16199		  printf (" IDENT     %#06lx  self-contained %#06lx",
16200			  option->info & OGP_GROUP,
16201			  (option->info & OGP_SELF) >> 16);
16202		  break;
16203		default:
16204		  /* This shouldn't happen.  */
16205		  printf (" %3d ???     %d %lx",
16206			  option->kind, option->section, option->info);
16207		  break;
16208		}
16209
16210	      len = sizeof (* eopt);
16211	      while (len < option->size)
16212		{
16213		  unsigned char datum = * ((unsigned char *) eopt + offset + len);
16214
16215		  if (ISPRINT (datum))
16216		    printf ("%c", datum);
16217		  else
16218		    printf ("\\%03o", datum);
16219		  len ++;
16220		}
16221	      fputs ("\n", stdout);
16222
16223	      offset += option->size;
16224	      ++option;
16225	    }
16226
16227	  free (eopt);
16228	}
16229      else
16230	res = FALSE;
16231    }
16232
16233  if (conflicts_offset != 0 && conflictsno != 0)
16234    {
16235      Elf32_Conflict * iconf;
16236      size_t cnt;
16237
16238      if (dynamic_symbols == NULL)
16239	{
16240	  error (_("conflict list found without a dynamic symbol table\n"));
16241	  return FALSE;
16242	}
16243
16244      /* PR 21345 - print a slightly more helpful error message
16245	 if we are sure that the cmalloc will fail.  */
16246      if (conflictsno * sizeof (* iconf) > filedata->file_size)
16247	{
16248	  error (_("Overlarge number of conflicts detected: %lx\n"),
16249		 (long) conflictsno);
16250	  return FALSE;
16251	}
16252
16253      iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
16254      if (iconf == NULL)
16255	{
16256	  error (_("Out of memory allocating space for dynamic conflicts\n"));
16257	  return FALSE;
16258	}
16259
16260      if (is_32bit_elf)
16261	{
16262	  Elf32_External_Conflict * econf32;
16263
16264	  econf32 = (Elf32_External_Conflict *)
16265              get_data (NULL, filedata, conflicts_offset, conflictsno,
16266                        sizeof (* econf32), _("conflict"));
16267	  if (!econf32)
16268	    return FALSE;
16269
16270	  for (cnt = 0; cnt < conflictsno; ++cnt)
16271	    iconf[cnt] = BYTE_GET (econf32[cnt]);
16272
16273	  free (econf32);
16274	}
16275      else
16276	{
16277	  Elf64_External_Conflict * econf64;
16278
16279	  econf64 = (Elf64_External_Conflict *)
16280              get_data (NULL, filedata, conflicts_offset, conflictsno,
16281                        sizeof (* econf64), _("conflict"));
16282	  if (!econf64)
16283	    return FALSE;
16284
16285	  for (cnt = 0; cnt < conflictsno; ++cnt)
16286	    iconf[cnt] = BYTE_GET (econf64[cnt]);
16287
16288	  free (econf64);
16289	}
16290
16291      printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
16292			"\nSection '.conflict' contains %lu entries:\n",
16293			(unsigned long) conflictsno),
16294	      (unsigned long) conflictsno);
16295      puts (_("  Num:    Index       Value  Name"));
16296
16297      for (cnt = 0; cnt < conflictsno; ++cnt)
16298	{
16299	  printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
16300
16301	  if (iconf[cnt] >= num_dynamic_syms)
16302	    printf (_("<corrupt symbol index>"));
16303	  else
16304	    {
16305	      Elf_Internal_Sym * psym;
16306
16307	      psym = & dynamic_symbols[iconf[cnt]];
16308	      print_vma (psym->st_value, FULL_HEX);
16309	      putchar (' ');
16310	      if (VALID_DYNAMIC_NAME (psym->st_name))
16311		print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
16312	      else
16313		printf (_("<corrupt: %14ld>"), psym->st_name);
16314	    }
16315	  putchar ('\n');
16316	}
16317
16318      free (iconf);
16319    }
16320
16321  if (pltgot != 0 && local_gotno != 0)
16322    {
16323      bfd_vma ent, local_end, global_end;
16324      size_t i, offset;
16325      unsigned char * data;
16326      unsigned char * data_end;
16327      int addr_size;
16328
16329      ent = pltgot;
16330      addr_size = (is_32bit_elf ? 4 : 8);
16331      local_end = pltgot + local_gotno * addr_size;
16332
16333      /* PR binutils/17533 file: 012-111227-0.004  */
16334      if (symtabno < gotsym)
16335	{
16336	  error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
16337		 (unsigned long) gotsym, (unsigned long) symtabno);
16338	  return FALSE;
16339	}
16340
16341      global_end = local_end + (symtabno - gotsym) * addr_size;
16342      /* PR 17531: file: 54c91a34.  */
16343      if (global_end < local_end)
16344	{
16345	  error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
16346	  return FALSE;
16347	}
16348
16349      offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
16350      data = (unsigned char *) get_data (NULL, filedata, offset,
16351                                         global_end - pltgot, 1,
16352					 _("Global Offset Table data"));
16353      /* PR 12855: Null data is handled gracefully throughout.  */
16354      data_end = data + (global_end - pltgot);
16355
16356      printf (_("\nPrimary GOT:\n"));
16357      printf (_(" Canonical gp value: "));
16358      print_vma (pltgot + 0x7ff0, LONG_HEX);
16359      printf ("\n\n");
16360
16361      printf (_(" Reserved entries:\n"));
16362      printf (_("  %*s %10s %*s Purpose\n"),
16363	      addr_size * 2, _("Address"), _("Access"),
16364	      addr_size * 2, _("Initial"));
16365      ent = print_mips_got_entry (data, pltgot, ent, data_end);
16366      printf (_(" Lazy resolver\n"));
16367      if (ent == (bfd_vma) -1)
16368	goto got_print_fail;
16369
16370      /* Check for the MSB of GOT[1] being set, denoting a GNU object.
16371	 This entry will be used by some runtime loaders, to store the
16372	 module pointer.  Otherwise this is an ordinary local entry.
16373	 PR 21344: Check for the entry being fully available before
16374	 fetching it.  */
16375      if (data
16376	  && data + ent - pltgot + addr_size <= data_end
16377	  && (byte_get (data + ent - pltgot, addr_size)
16378	      >> (addr_size * 8 - 1)) != 0)
16379	{
16380	  ent = print_mips_got_entry (data, pltgot, ent, data_end);
16381	  printf (_(" Module pointer (GNU extension)\n"));
16382	  if (ent == (bfd_vma) -1)
16383	    goto got_print_fail;
16384	}
16385      printf ("\n");
16386
16387      if (data != NULL && ent < local_end)
16388	{
16389	  printf (_(" Local entries:\n"));
16390	  printf ("  %*s %10s %*s\n",
16391		  addr_size * 2, _("Address"), _("Access"),
16392		  addr_size * 2, _("Initial"));
16393	  while (ent < local_end)
16394	    {
16395	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
16396	      printf ("\n");
16397	      if (ent == (bfd_vma) -1)
16398		goto got_print_fail;
16399	    }
16400	  printf ("\n");
16401	}
16402
16403      if (data != NULL && gotsym < symtabno)
16404	{
16405	  int sym_width;
16406
16407	  printf (_(" Global entries:\n"));
16408	  printf ("  %*s %10s %*s %*s %-7s %3s %s\n",
16409		  addr_size * 2, _("Address"),
16410		  _("Access"),
16411		  addr_size * 2, _("Initial"),
16412		  addr_size * 2, _("Sym.Val."),
16413		  _("Type"),
16414		  /* Note for translators: "Ndx" = abbreviated form of "Index".  */
16415		  _("Ndx"), _("Name"));
16416
16417	  sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
16418
16419	  for (i = gotsym; i < symtabno; i++)
16420	    {
16421	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
16422	      printf (" ");
16423
16424	      if (dynamic_symbols == NULL)
16425		printf (_("<no dynamic symbols>"));
16426	      else if (i < num_dynamic_syms)
16427		{
16428		  Elf_Internal_Sym * psym = dynamic_symbols + i;
16429
16430		  print_vma (psym->st_value, LONG_HEX);
16431		  printf (" %-7s %3s ",
16432			  get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
16433			  get_symbol_index_type (filedata, psym->st_shndx));
16434
16435		  if (VALID_DYNAMIC_NAME (psym->st_name))
16436		    print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
16437		  else
16438		    printf (_("<corrupt: %14ld>"), psym->st_name);
16439		}
16440	      else
16441		printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
16442			(unsigned long) i);
16443
16444	      printf ("\n");
16445	      if (ent == (bfd_vma) -1)
16446		break;
16447	    }
16448	  printf ("\n");
16449	}
16450
16451    got_print_fail:
16452      if (data)
16453	free (data);
16454    }
16455
16456  if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
16457    {
16458      bfd_vma ent, end;
16459      size_t offset, rel_offset;
16460      unsigned long count, i;
16461      unsigned char * data;
16462      int addr_size, sym_width;
16463      Elf_Internal_Rela * rels;
16464
16465      rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
16466      if (pltrel == DT_RELA)
16467	{
16468	  if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
16469	    return FALSE;
16470	}
16471      else
16472	{
16473	  if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
16474	    return FALSE;
16475	}
16476
16477      ent = mips_pltgot;
16478      addr_size = (is_32bit_elf ? 4 : 8);
16479      end = mips_pltgot + (2 + count) * addr_size;
16480
16481      offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
16482      data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
16483                                         1, _("Procedure Linkage Table data"));
16484      if (data == NULL)
16485	return FALSE;
16486
16487      printf ("\nPLT GOT:\n\n");
16488      printf (_(" Reserved entries:\n"));
16489      printf (_("  %*s %*s Purpose\n"),
16490	      addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
16491      ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
16492      printf (_(" PLT lazy resolver\n"));
16493      ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
16494      printf (_(" Module pointer\n"));
16495      printf ("\n");
16496
16497      printf (_(" Entries:\n"));
16498      printf ("  %*s %*s %*s %-7s %3s %s\n",
16499	      addr_size * 2, _("Address"),
16500	      addr_size * 2, _("Initial"),
16501	      addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
16502      sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
16503      for (i = 0; i < count; i++)
16504	{
16505	  unsigned long idx = get_reloc_symindex (rels[i].r_info);
16506
16507	  ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
16508	  printf (" ");
16509
16510	  if (idx >= num_dynamic_syms)
16511	    printf (_("<corrupt symbol index: %lu>"), idx);
16512	  else
16513	    {
16514	      Elf_Internal_Sym * psym = dynamic_symbols + idx;
16515
16516	      print_vma (psym->st_value, LONG_HEX);
16517	      printf (" %-7s %3s ",
16518		      get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
16519		      get_symbol_index_type (filedata, psym->st_shndx));
16520	      if (VALID_DYNAMIC_NAME (psym->st_name))
16521		print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
16522	      else
16523		printf (_("<corrupt: %14ld>"), psym->st_name);
16524	    }
16525	  printf ("\n");
16526	}
16527      printf ("\n");
16528
16529      if (data)
16530	free (data);
16531      free (rels);
16532    }
16533
16534  return res;
16535}
16536
16537static bfd_boolean
16538process_nds32_specific (Filedata * filedata)
16539{
16540  Elf_Internal_Shdr *sect = NULL;
16541
16542  sect = find_section (filedata, ".nds32_e_flags");
16543  if (sect != NULL)
16544    {
16545      unsigned int *flag;
16546
16547      printf ("\nNDS32 elf flags section:\n");
16548      flag = get_data (NULL, filedata, sect->sh_offset, 1,
16549		       sect->sh_size, _("NDS32 elf flags section"));
16550
16551      if (! flag)
16552	return FALSE;
16553
16554      switch ((*flag) & 0x3)
16555	{
16556	case 0:
16557	  printf ("(VEC_SIZE):\tNo entry.\n");
16558	  break;
16559	case 1:
16560	  printf ("(VEC_SIZE):\t4 bytes\n");
16561	  break;
16562	case 2:
16563	  printf ("(VEC_SIZE):\t16 bytes\n");
16564	  break;
16565	case 3:
16566	  printf ("(VEC_SIZE):\treserved\n");
16567	  break;
16568	}
16569    }
16570
16571  return TRUE;
16572}
16573
16574static bfd_boolean
16575process_gnu_liblist (Filedata * filedata)
16576{
16577  Elf_Internal_Shdr * section;
16578  Elf_Internal_Shdr * string_sec;
16579  Elf32_External_Lib * elib;
16580  char * strtab;
16581  size_t strtab_size;
16582  size_t cnt;
16583  unsigned long num_liblist;
16584  unsigned i;
16585  bfd_boolean res = TRUE;
16586
16587  if (! do_arch)
16588    return TRUE;
16589
16590  for (i = 0, section = filedata->section_headers;
16591       i < filedata->file_header.e_shnum;
16592       i++, section++)
16593    {
16594      switch (section->sh_type)
16595	{
16596	case SHT_GNU_LIBLIST:
16597	  if (section->sh_link >= filedata->file_header.e_shnum)
16598	    break;
16599
16600	  elib = (Elf32_External_Lib *)
16601              get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
16602                        _("liblist section data"));
16603
16604	  if (elib == NULL)
16605	    {
16606	      res = FALSE;
16607	      break;
16608	    }
16609
16610	  string_sec = filedata->section_headers + section->sh_link;
16611	  strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
16612                                      string_sec->sh_size,
16613                                      _("liblist string table"));
16614	  if (strtab == NULL
16615	      || section->sh_entsize != sizeof (Elf32_External_Lib))
16616	    {
16617	      free (elib);
16618	      free (strtab);
16619	      res = FALSE;
16620	      break;
16621	    }
16622	  strtab_size = string_sec->sh_size;
16623
16624	  num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
16625	  printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
16626			    "\nLibrary list section '%s' contains %lu entries:\n",
16627			    num_liblist),
16628		  printable_section_name (filedata, section),
16629		  num_liblist);
16630
16631	  puts (_("     Library              Time Stamp          Checksum   Version Flags"));
16632
16633	  for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
16634	       ++cnt)
16635	    {
16636	      Elf32_Lib liblist;
16637	      time_t atime;
16638	      char timebuf[128];
16639	      struct tm * tmp;
16640
16641	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
16642	      atime = BYTE_GET (elib[cnt].l_time_stamp);
16643	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
16644	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
16645	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
16646
16647	      tmp = gmtime (&atime);
16648	      snprintf (timebuf, sizeof (timebuf),
16649			"%04u-%02u-%02uT%02u:%02u:%02u",
16650			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
16651			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
16652
16653	      printf ("%3lu: ", (unsigned long) cnt);
16654	      if (do_wide)
16655		printf ("%-20s", liblist.l_name < strtab_size
16656			? strtab + liblist.l_name : _("<corrupt>"));
16657	      else
16658		printf ("%-20.20s", liblist.l_name < strtab_size
16659			? strtab + liblist.l_name : _("<corrupt>"));
16660	      printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
16661		      liblist.l_version, liblist.l_flags);
16662	    }
16663
16664	  free (elib);
16665	  free (strtab);
16666	}
16667    }
16668
16669  return res;
16670}
16671
16672static const char *
16673get_note_type (Filedata * filedata, unsigned e_type)
16674{
16675  static char buff[64];
16676
16677  if (filedata->file_header.e_type == ET_CORE)
16678    switch (e_type)
16679      {
16680      case NT_AUXV:
16681	return _("NT_AUXV (auxiliary vector)");
16682      case NT_PRSTATUS:
16683	return _("NT_PRSTATUS (prstatus structure)");
16684      case NT_FPREGSET:
16685	return _("NT_FPREGSET (floating point registers)");
16686      case NT_PRPSINFO:
16687	return _("NT_PRPSINFO (prpsinfo structure)");
16688      case NT_TASKSTRUCT:
16689	return _("NT_TASKSTRUCT (task structure)");
16690      case NT_PRXFPREG:
16691	return _("NT_PRXFPREG (user_xfpregs structure)");
16692      case NT_PPC_VMX:
16693	return _("NT_PPC_VMX (ppc Altivec registers)");
16694      case NT_PPC_VSX:
16695	return _("NT_PPC_VSX (ppc VSX registers)");
16696      case NT_PPC_TAR:
16697	return _("NT_PPC_TAR (ppc TAR register)");
16698      case NT_PPC_PPR:
16699	return _("NT_PPC_PPR (ppc PPR register)");
16700      case NT_PPC_DSCR:
16701	return _("NT_PPC_DSCR (ppc DSCR register)");
16702      case NT_PPC_EBB:
16703	return _("NT_PPC_EBB (ppc EBB registers)");
16704      case NT_PPC_PMU:
16705	return _("NT_PPC_PMU (ppc PMU registers)");
16706      case NT_PPC_TM_CGPR:
16707	return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
16708      case NT_PPC_TM_CFPR:
16709	return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
16710      case NT_PPC_TM_CVMX:
16711	return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
16712      case NT_PPC_TM_CVSX:
16713	return _("NT_PPC_TM_VSX (ppc checkpointed VSX registers)");
16714      case NT_PPC_TM_SPR:
16715	return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
16716      case NT_PPC_TM_CTAR:
16717	return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
16718      case NT_PPC_TM_CPPR:
16719	return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
16720      case NT_PPC_TM_CDSCR:
16721	return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
16722      case NT_386_TLS:
16723	return _("NT_386_TLS (x86 TLS information)");
16724      case NT_386_IOPERM:
16725	return _("NT_386_IOPERM (x86 I/O permissions)");
16726      case NT_X86_XSTATE:
16727	return _("NT_X86_XSTATE (x86 XSAVE extended state)");
16728      case NT_S390_HIGH_GPRS:
16729	return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
16730      case NT_S390_TIMER:
16731	return _("NT_S390_TIMER (s390 timer register)");
16732      case NT_S390_TODCMP:
16733	return _("NT_S390_TODCMP (s390 TOD comparator register)");
16734      case NT_S390_TODPREG:
16735	return _("NT_S390_TODPREG (s390 TOD programmable register)");
16736      case NT_S390_CTRS:
16737	return _("NT_S390_CTRS (s390 control registers)");
16738      case NT_S390_PREFIX:
16739	return _("NT_S390_PREFIX (s390 prefix register)");
16740      case NT_S390_LAST_BREAK:
16741	return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
16742      case NT_S390_SYSTEM_CALL:
16743	return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
16744      case NT_S390_TDB:
16745	return _("NT_S390_TDB (s390 transaction diagnostic block)");
16746      case NT_S390_VXRS_LOW:
16747	return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
16748      case NT_S390_VXRS_HIGH:
16749	return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
16750      case NT_S390_GS_CB:
16751	return _("NT_S390_GS_CB (s390 guarded-storage registers)");
16752      case NT_S390_GS_BC:
16753	return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
16754      case NT_ARM_VFP:
16755	return _("NT_ARM_VFP (arm VFP registers)");
16756      case NT_ARM_TLS:
16757	return _("NT_ARM_TLS (AArch TLS registers)");
16758      case NT_ARM_HW_BREAK:
16759	return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
16760      case NT_ARM_HW_WATCH:
16761	return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
16762      case NT_PSTATUS:
16763	return _("NT_PSTATUS (pstatus structure)");
16764      case NT_FPREGS:
16765	return _("NT_FPREGS (floating point registers)");
16766      case NT_PSINFO:
16767	return _("NT_PSINFO (psinfo structure)");
16768      case NT_LWPSTATUS:
16769	return _("NT_LWPSTATUS (lwpstatus_t structure)");
16770      case NT_LWPSINFO:
16771	return _("NT_LWPSINFO (lwpsinfo_t structure)");
16772      case NT_WIN32PSTATUS:
16773	return _("NT_WIN32PSTATUS (win32_pstatus structure)");
16774      case NT_SIGINFO:
16775	return _("NT_SIGINFO (siginfo_t data)");
16776      case NT_FILE:
16777	return _("NT_FILE (mapped files)");
16778      default:
16779	break;
16780      }
16781  else
16782    switch (e_type)
16783      {
16784      case NT_VERSION:
16785	return _("NT_VERSION (version)");
16786      case NT_ARCH:
16787	return _("NT_ARCH (architecture)");
16788      case NT_GNU_BUILD_ATTRIBUTE_OPEN:
16789	return _("OPEN");
16790      case NT_GNU_BUILD_ATTRIBUTE_FUNC:
16791	return _("func");
16792      default:
16793	break;
16794      }
16795
16796  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
16797  return buff;
16798}
16799
16800static bfd_boolean
16801print_core_note (Elf_Internal_Note *pnote)
16802{
16803  unsigned int addr_size = is_32bit_elf ? 4 : 8;
16804  bfd_vma count, page_size;
16805  unsigned char *descdata, *filenames, *descend;
16806
16807  if (pnote->type != NT_FILE)
16808    {
16809      if (do_wide)
16810	printf ("\n");
16811      return TRUE;
16812    }
16813
16814#ifndef BFD64
16815  if (!is_32bit_elf)
16816    {
16817      printf (_("    Cannot decode 64-bit note in 32-bit build\n"));
16818      /* Still "successful".  */
16819      return TRUE;
16820    }
16821#endif
16822
16823  if (pnote->descsz < 2 * addr_size)
16824    {
16825      error (_("    Malformed note - too short for header\n"));
16826      return FALSE;
16827    }
16828
16829  descdata = (unsigned char *) pnote->descdata;
16830  descend = descdata + pnote->descsz;
16831
16832  if (descdata[pnote->descsz - 1] != '\0')
16833    {
16834      error (_("    Malformed note - does not end with \\0\n"));
16835      return FALSE;
16836    }
16837
16838  count = byte_get (descdata, addr_size);
16839  descdata += addr_size;
16840
16841  page_size = byte_get (descdata, addr_size);
16842  descdata += addr_size;
16843
16844  if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
16845      || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
16846    {
16847      error (_("    Malformed note - too short for supplied file count\n"));
16848      return FALSE;
16849    }
16850
16851  printf (_("    Page size: "));
16852  print_vma (page_size, DEC);
16853  printf ("\n");
16854
16855  printf (_("    %*s%*s%*s\n"),
16856	  (int) (2 + 2 * addr_size), _("Start"),
16857	  (int) (4 + 2 * addr_size), _("End"),
16858	  (int) (4 + 2 * addr_size), _("Page Offset"));
16859  filenames = descdata + count * 3 * addr_size;
16860  while (count-- > 0)
16861    {
16862      bfd_vma start, end, file_ofs;
16863
16864      if (filenames == descend)
16865	{
16866	  error (_("    Malformed note - filenames end too early\n"));
16867	  return FALSE;
16868	}
16869
16870      start = byte_get (descdata, addr_size);
16871      descdata += addr_size;
16872      end = byte_get (descdata, addr_size);
16873      descdata += addr_size;
16874      file_ofs = byte_get (descdata, addr_size);
16875      descdata += addr_size;
16876
16877      printf ("    ");
16878      print_vma (start, FULL_HEX);
16879      printf ("  ");
16880      print_vma (end, FULL_HEX);
16881      printf ("  ");
16882      print_vma (file_ofs, FULL_HEX);
16883      printf ("\n        %s\n", filenames);
16884
16885      filenames += 1 + strlen ((char *) filenames);
16886    }
16887
16888  return TRUE;
16889}
16890
16891static const char *
16892get_gnu_elf_note_type (unsigned e_type)
16893{
16894  /* NB/ Keep this switch statement in sync with print_gnu_note ().  */
16895  switch (e_type)
16896    {
16897    case NT_GNU_ABI_TAG:
16898      return _("NT_GNU_ABI_TAG (ABI version tag)");
16899    case NT_GNU_HWCAP:
16900      return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
16901    case NT_GNU_BUILD_ID:
16902      return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
16903    case NT_GNU_GOLD_VERSION:
16904      return _("NT_GNU_GOLD_VERSION (gold version)");
16905    case NT_GNU_PROPERTY_TYPE_0:
16906      return _("NT_GNU_PROPERTY_TYPE_0");
16907    case NT_GNU_BUILD_ATTRIBUTE_OPEN:
16908      return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
16909    case NT_GNU_BUILD_ATTRIBUTE_FUNC:
16910      return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
16911    default:
16912      {
16913	static char buff[64];
16914
16915	snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
16916	return buff;
16917      }
16918    }
16919}
16920
16921static void
16922decode_x86_isa (unsigned int bitmask)
16923{
16924  while (bitmask)
16925    {
16926      unsigned int bit = bitmask & (- bitmask);
16927
16928      bitmask &= ~ bit;
16929      switch (bit)
16930	{
16931	case GNU_PROPERTY_X86_ISA_1_486: printf ("i486"); break;
16932	case GNU_PROPERTY_X86_ISA_1_586: printf ("586"); break;
16933	case GNU_PROPERTY_X86_ISA_1_686: printf ("686"); break;
16934	case GNU_PROPERTY_X86_ISA_1_SSE: printf ("SSE"); break;
16935	case GNU_PROPERTY_X86_ISA_1_SSE2: printf ("SSE2"); break;
16936	case GNU_PROPERTY_X86_ISA_1_SSE3: printf ("SSE3"); break;
16937	case GNU_PROPERTY_X86_ISA_1_SSSE3: printf ("SSSE3"); break;
16938	case GNU_PROPERTY_X86_ISA_1_SSE4_1: printf ("SSE4_1"); break;
16939	case GNU_PROPERTY_X86_ISA_1_SSE4_2: printf ("SSE4_2"); break;
16940	case GNU_PROPERTY_X86_ISA_1_AVX: printf ("AVX"); break;
16941	case GNU_PROPERTY_X86_ISA_1_AVX2: printf ("AVX2"); break;
16942	case GNU_PROPERTY_X86_ISA_1_AVX512F: printf ("AVX512F"); break;
16943	case GNU_PROPERTY_X86_ISA_1_AVX512CD: printf ("AVX512CD"); break;
16944	case GNU_PROPERTY_X86_ISA_1_AVX512ER: printf ("AVX512ER"); break;
16945	case GNU_PROPERTY_X86_ISA_1_AVX512PF: printf ("AVX512PF"); break;
16946	case GNU_PROPERTY_X86_ISA_1_AVX512VL: printf ("AVX512VL"); break;
16947	case GNU_PROPERTY_X86_ISA_1_AVX512DQ: printf ("AVX512DQ"); break;
16948	case GNU_PROPERTY_X86_ISA_1_AVX512BW: printf ("AVX512BW"); break;
16949	default: printf (_("<unknown: %x>"), bit); break;
16950	}
16951      if (bitmask)
16952	printf (", ");
16953    }
16954}
16955
16956static void
16957decode_x86_feature (unsigned int type, unsigned int bitmask)
16958{
16959  while (bitmask)
16960    {
16961      unsigned int bit = bitmask & (- bitmask);
16962
16963      bitmask &= ~ bit;
16964      switch (bit)
16965	{
16966	case GNU_PROPERTY_X86_FEATURE_1_IBT:
16967	  switch (type)
16968	    {
16969	    case GNU_PROPERTY_X86_FEATURE_1_AND:
16970	      printf ("IBT");
16971	      break;
16972	    default:
16973	      /* This should never happen.  */
16974	      abort ();
16975	    }
16976	  break;
16977	case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
16978	  switch (type)
16979	    {
16980	    case GNU_PROPERTY_X86_FEATURE_1_AND:
16981	      printf ("SHSTK");
16982	      break;
16983	    default:
16984	      /* This should never happen.  */
16985	      abort ();
16986	    }
16987	  break;
16988	default:
16989	  printf (_("<unknown: %x>"), bit);
16990	  break;
16991	}
16992      if (bitmask)
16993	printf (", ");
16994    }
16995}
16996
16997static void
16998print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
16999{
17000  unsigned char * ptr = (unsigned char *) pnote->descdata;
17001  unsigned char * ptr_end = ptr + pnote->descsz;
17002  unsigned int    size = is_32bit_elf ? 4 : 8;
17003
17004  printf (_("      Properties: "));
17005
17006  if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
17007    {
17008      printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
17009      return;
17010    }
17011
17012  while (ptr < ptr_end)
17013    {
17014      unsigned int j;
17015      unsigned int type;
17016      unsigned int datasz;
17017
17018      if ((size_t) (ptr_end - ptr) < 8)
17019	{
17020	  printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
17021	  break;
17022	}
17023
17024      type = byte_get (ptr, 4);
17025      datasz = byte_get (ptr + 4, 4);
17026
17027      ptr += 8;
17028
17029      if (datasz > (size_t) (ptr_end - ptr))
17030	{
17031	  printf (_("<corrupt type (%#x) datasz: %#x>\n"),
17032		  type, datasz);
17033	  break;
17034	}
17035
17036      if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
17037	{
17038	  if (filedata->file_header.e_machine == EM_X86_64
17039	      || filedata->file_header.e_machine == EM_IAMCU
17040	      || filedata->file_header.e_machine == EM_386)
17041	    {
17042	      switch (type)
17043		{
17044		case GNU_PROPERTY_X86_ISA_1_USED:
17045		  printf ("x86 ISA used: ");
17046		  if (datasz != 4)
17047		    printf (_("<corrupt length: %#x> "), datasz);
17048		  else
17049		    decode_x86_isa (byte_get (ptr, 4));
17050		  goto next;
17051
17052		case GNU_PROPERTY_X86_ISA_1_NEEDED:
17053		  printf ("x86 ISA needed: ");
17054		  if (datasz != 4)
17055		    printf (_("<corrupt length: %#x> "), datasz);
17056		  else
17057		    decode_x86_isa (byte_get (ptr, 4));
17058		  goto next;
17059
17060		case GNU_PROPERTY_X86_FEATURE_1_AND:
17061		  printf ("x86 feature: ");
17062		  if (datasz != 4)
17063		    printf (_("<corrupt length: %#x> "), datasz);
17064		  else
17065		    decode_x86_feature (type, byte_get (ptr, 4));
17066		  goto next;
17067
17068		default:
17069		  break;
17070		}
17071	    }
17072	}
17073      else
17074	{
17075	  switch (type)
17076	    {
17077	    case GNU_PROPERTY_STACK_SIZE:
17078	      printf (_("stack size: "));
17079	      if (datasz != size)
17080		printf (_("<corrupt length: %#x> "), datasz);
17081	      else
17082		printf ("%#lx", (unsigned long) byte_get (ptr, size));
17083	      goto next;
17084
17085	    case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
17086	      printf ("no copy on protected ");
17087	      if (datasz)
17088		printf (_("<corrupt length: %#x> "), datasz);
17089	      goto next;
17090
17091	    default:
17092	      break;
17093	    }
17094	}
17095
17096      if (type < GNU_PROPERTY_LOPROC)
17097	printf (_("<unknown type %#x data: "), type);
17098      else if (type < GNU_PROPERTY_LOUSER)
17099	printf (_("<procesor-specific type %#x data: "), type);
17100      else
17101	printf (_("<application-specific type %#x data: "), type);
17102      for (j = 0; j < datasz; ++j)
17103	printf ("%02x ", ptr[j] & 0xff);
17104      printf (">");
17105
17106next:
17107      ptr += ((datasz + (size - 1)) & ~ (size - 1));
17108      if (ptr == ptr_end)
17109	break;
17110
17111      if (do_wide)
17112	printf (", ");
17113      else
17114	printf ("\n\t");
17115    }
17116
17117  printf ("\n");
17118}
17119
17120static bfd_boolean
17121print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
17122{
17123  /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type ().  */
17124  switch (pnote->type)
17125    {
17126    case NT_GNU_BUILD_ID:
17127      {
17128	unsigned long i;
17129
17130	printf (_("    Build ID: "));
17131	for (i = 0; i < pnote->descsz; ++i)
17132	  printf ("%02x", pnote->descdata[i] & 0xff);
17133	printf ("\n");
17134      }
17135      break;
17136
17137    case NT_GNU_ABI_TAG:
17138      {
17139	unsigned long os, major, minor, subminor;
17140	const char *osname;
17141
17142	/* PR 17531: file: 030-599401-0.004.  */
17143	if (pnote->descsz < 16)
17144	  {
17145	    printf (_("    <corrupt GNU_ABI_TAG>\n"));
17146	    break;
17147	  }
17148
17149	os = byte_get ((unsigned char *) pnote->descdata, 4);
17150	major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
17151	minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
17152	subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
17153
17154	switch (os)
17155	  {
17156	  case GNU_ABI_TAG_LINUX:
17157	    osname = "Linux";
17158	    break;
17159	  case GNU_ABI_TAG_HURD:
17160	    osname = "Hurd";
17161	    break;
17162	  case GNU_ABI_TAG_SOLARIS:
17163	    osname = "Solaris";
17164	    break;
17165	  case GNU_ABI_TAG_FREEBSD:
17166	    osname = "FreeBSD";
17167	    break;
17168	  case GNU_ABI_TAG_NETBSD:
17169	    osname = "NetBSD";
17170	    break;
17171	  case GNU_ABI_TAG_SYLLABLE:
17172	    osname = "Syllable";
17173	    break;
17174	  case GNU_ABI_TAG_NACL:
17175	    osname = "NaCl";
17176	    break;
17177	  default:
17178	    osname = "Unknown";
17179	    break;
17180	  }
17181
17182	printf (_("    OS: %s, ABI: %ld.%ld.%ld\n"), osname,
17183		major, minor, subminor);
17184      }
17185      break;
17186
17187    case NT_GNU_GOLD_VERSION:
17188      {
17189	unsigned long i;
17190
17191	printf (_("    Version: "));
17192	for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
17193	  printf ("%c", pnote->descdata[i]);
17194	printf ("\n");
17195      }
17196      break;
17197
17198    case NT_GNU_HWCAP:
17199      {
17200	unsigned long num_entries, mask;
17201
17202	/* Hardware capabilities information.  Word 0 is the number of entries.
17203	   Word 1 is a bitmask of enabled entries.  The rest of the descriptor
17204	   is a series of entries, where each entry is a single byte followed
17205	   by a nul terminated string.  The byte gives the bit number to test
17206	   if enabled in the bitmask.  */
17207	printf (_("      Hardware Capabilities: "));
17208	if (pnote->descsz < 8)
17209	  {
17210	    error (_("<corrupt GNU_HWCAP>\n"));
17211	    return FALSE;
17212	  }
17213	num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
17214	mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
17215	printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
17216	/* FIXME: Add code to display the entries... */
17217      }
17218      break;
17219
17220    case NT_GNU_PROPERTY_TYPE_0:
17221      print_gnu_property_note (filedata, pnote);
17222      break;
17223
17224    default:
17225      /* Handle unrecognised types.  An error message should have already been
17226	 created by get_gnu_elf_note_type(), so all that we need to do is to
17227	 display the data.  */
17228      {
17229	unsigned long i;
17230
17231	printf (_("    Description data: "));
17232	for (i = 0; i < pnote->descsz; ++i)
17233	  printf ("%02x ", pnote->descdata[i] & 0xff);
17234	printf ("\n");
17235      }
17236      break;
17237    }
17238
17239  return TRUE;
17240}
17241
17242static const char *
17243get_v850_elf_note_type (enum v850_notes n_type)
17244{
17245  static char buff[64];
17246
17247  switch (n_type)
17248    {
17249    case V850_NOTE_ALIGNMENT:  return _("Alignment of 8-byte objects");
17250    case V850_NOTE_DATA_SIZE:  return _("Sizeof double and long double");
17251    case V850_NOTE_FPU_INFO:   return _("Type of FPU support needed");
17252    case V850_NOTE_SIMD_INFO:  return _("Use of SIMD instructions");
17253    case V850_NOTE_CACHE_INFO: return _("Use of cache");
17254    case V850_NOTE_MMU_INFO:   return _("Use of MMU");
17255    default:
17256      snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
17257      return buff;
17258    }
17259}
17260
17261static bfd_boolean
17262print_v850_note (Elf_Internal_Note * pnote)
17263{
17264  unsigned int val;
17265
17266  if (pnote->descsz != 4)
17267    return FALSE;
17268
17269  val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
17270
17271  if (val == 0)
17272    {
17273      printf (_("not set\n"));
17274      return TRUE;
17275    }
17276
17277  switch (pnote->type)
17278    {
17279    case V850_NOTE_ALIGNMENT:
17280      switch (val)
17281	{
17282	case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return TRUE;
17283	case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return TRUE;
17284	}
17285      break;
17286
17287    case V850_NOTE_DATA_SIZE:
17288      switch (val)
17289	{
17290	case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return TRUE;
17291	case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return TRUE;
17292	}
17293      break;
17294
17295    case V850_NOTE_FPU_INFO:
17296      switch (val)
17297	{
17298	case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return TRUE;
17299	case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return TRUE;
17300	}
17301      break;
17302
17303    case V850_NOTE_MMU_INFO:
17304    case V850_NOTE_CACHE_INFO:
17305    case V850_NOTE_SIMD_INFO:
17306      if (val == EF_RH850_SIMD)
17307	{
17308	  printf (_("yes\n"));
17309	  return TRUE;
17310	}
17311      break;
17312
17313    default:
17314      /* An 'unknown note type' message will already have been displayed.  */
17315      break;
17316    }
17317
17318  printf (_("unknown value: %x\n"), val);
17319  return FALSE;
17320}
17321
17322static bfd_boolean
17323process_netbsd_elf_note (Elf_Internal_Note * pnote)
17324{
17325  unsigned int version;
17326
17327  switch (pnote->type)
17328    {
17329    case NT_NETBSD_IDENT:
17330      version = byte_get((unsigned char *)pnote->descdata, sizeof(version));
17331      if ((version / 10000) % 100)
17332        printf ("  NetBSD\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
17333		version, version / 100000000, (version / 1000000) % 100,
17334		(version / 10000) % 100 > 26 ? "Z" : "",
17335		'A' + (version / 10000) % 26);
17336      else
17337	printf ("  NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
17338	        version, version / 100000000, (version / 1000000) % 100,
17339		(version / 100) % 100);
17340      return TRUE;
17341
17342    case NT_NETBSD_MARCH:
17343      printf ("  NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
17344	      pnote->descdata);
17345      return TRUE;
17346
17347    case NT_NETBSD_PAX:
17348      version = byte_get((unsigned char *)pnote->descdata, sizeof(version));
17349      printf ("  NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
17350	      ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
17351	      ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
17352	      ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
17353	      ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
17354	      ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
17355	      ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
17356      return TRUE;
17357
17358    default:
17359      printf ("  NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n", pnote->descsz,
17360	      pnote->type);
17361      return FALSE;
17362    }
17363}
17364
17365static const char *
17366get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
17367{
17368  switch (e_type)
17369    {
17370    case NT_FREEBSD_THRMISC:
17371      return _("NT_THRMISC (thrmisc structure)");
17372    case NT_FREEBSD_PROCSTAT_PROC:
17373      return _("NT_PROCSTAT_PROC (proc data)");
17374    case NT_FREEBSD_PROCSTAT_FILES:
17375      return _("NT_PROCSTAT_FILES (files data)");
17376    case NT_FREEBSD_PROCSTAT_VMMAP:
17377      return _("NT_PROCSTAT_VMMAP (vmmap data)");
17378    case NT_FREEBSD_PROCSTAT_GROUPS:
17379      return _("NT_PROCSTAT_GROUPS (groups data)");
17380    case NT_FREEBSD_PROCSTAT_UMASK:
17381      return _("NT_PROCSTAT_UMASK (umask data)");
17382    case NT_FREEBSD_PROCSTAT_RLIMIT:
17383      return _("NT_PROCSTAT_RLIMIT (rlimit data)");
17384    case NT_FREEBSD_PROCSTAT_OSREL:
17385      return _("NT_PROCSTAT_OSREL (osreldate data)");
17386    case NT_FREEBSD_PROCSTAT_PSSTRINGS:
17387      return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
17388    case NT_FREEBSD_PROCSTAT_AUXV:
17389      return _("NT_PROCSTAT_AUXV (auxv data)");
17390    case NT_FREEBSD_PTLWPINFO:
17391      return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
17392    }
17393  return get_note_type (filedata, e_type);
17394}
17395
17396static const char *
17397get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
17398{
17399  static char buff[64];
17400
17401  switch (e_type)
17402    {
17403    case NT_NETBSDCORE_PROCINFO:
17404      /* NetBSD core "procinfo" structure.  */
17405      return _("NetBSD procinfo structure");
17406    case NT_NETBSDCORE_AUXV:
17407      return _("NetBSD ELF auxiliary vector data");
17408    default:
17409      break;
17410    }
17411
17412  /* As of Jan 2002 there are no other machine-independent notes
17413     defined for NetBSD core files.  If the note type is less
17414     than the start of the machine-dependent note types, we don't
17415     understand it.  */
17416
17417  if (e_type < NT_NETBSDCORE_FIRSTMACH)
17418    {
17419      snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
17420      return buff;
17421    }
17422
17423  switch (filedata->file_header.e_machine)
17424    {
17425    /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
17426       and PT_GETFPREGS == mach+2.  */
17427
17428    case EM_OLD_ALPHA:
17429    case EM_ALPHA:
17430    case EM_SPARC:
17431    case EM_SPARC32PLUS:
17432    case EM_SPARCV9:
17433      switch (e_type)
17434	{
17435	case NT_NETBSDCORE_FIRSTMACH + 0:
17436	  return _("PT_GETREGS (reg structure)");
17437	case NT_NETBSDCORE_FIRSTMACH + 2:
17438	  return _("PT_GETFPREGS (fpreg structure)");
17439	default:
17440	  break;
17441	}
17442      break;
17443
17444    /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
17445       There's also old PT___GETREGS40 == mach + 1 for old reg
17446       structure which lacks GBR.  */
17447    case EM_SH:
17448      switch (e_type)
17449	{
17450	case NT_NETBSDCORE_FIRSTMACH + 1:
17451	  return _("PT___GETREGS40 (old reg structure)");
17452	case NT_NETBSDCORE_FIRSTMACH + 3:
17453	  return _("PT_GETREGS (reg structure)");
17454	case NT_NETBSDCORE_FIRSTMACH + 5:
17455	  return _("PT_GETFPREGS (fpreg structure)");
17456	default:
17457	  break;
17458	}
17459      break;
17460
17461    /* On all other arch's, PT_GETREGS == mach+1 and
17462       PT_GETFPREGS == mach+3.  */
17463    default:
17464      switch (e_type)
17465	{
17466	case NT_NETBSDCORE_FIRSTMACH + 1:
17467	  return _("PT_GETREGS (reg structure)");
17468	case NT_NETBSDCORE_FIRSTMACH + 3:
17469	  return _("PT_GETFPREGS (fpreg structure)");
17470	default:
17471	  break;
17472	}
17473    }
17474
17475  snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
17476	    e_type - NT_NETBSDCORE_FIRSTMACH);
17477  return buff;
17478}
17479
17480static const char *
17481get_stapsdt_note_type (unsigned e_type)
17482{
17483  static char buff[64];
17484
17485  switch (e_type)
17486    {
17487    case NT_STAPSDT:
17488      return _("NT_STAPSDT (SystemTap probe descriptors)");
17489
17490    default:
17491      break;
17492    }
17493
17494  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
17495  return buff;
17496}
17497
17498static bfd_boolean
17499print_stapsdt_note (Elf_Internal_Note *pnote)
17500{
17501  int addr_size = is_32bit_elf ? 4 : 8;
17502  char *data = pnote->descdata;
17503  char *data_end = pnote->descdata + pnote->descsz;
17504  bfd_vma pc, base_addr, semaphore;
17505  char *provider, *probe, *arg_fmt;
17506
17507  pc = byte_get ((unsigned char *) data, addr_size);
17508  data += addr_size;
17509  base_addr = byte_get ((unsigned char *) data, addr_size);
17510  data += addr_size;
17511  semaphore = byte_get ((unsigned char *) data, addr_size);
17512  data += addr_size;
17513
17514  provider = data;
17515  data += strlen (data) + 1;
17516  probe = data;
17517  data += strlen (data) + 1;
17518  arg_fmt = data;
17519  data += strlen (data) + 1;
17520
17521  printf (_("    Provider: %s\n"), provider);
17522  printf (_("    Name: %s\n"), probe);
17523  printf (_("    Location: "));
17524  print_vma (pc, FULL_HEX);
17525  printf (_(", Base: "));
17526  print_vma (base_addr, FULL_HEX);
17527  printf (_(", Semaphore: "));
17528  print_vma (semaphore, FULL_HEX);
17529  printf ("\n");
17530  printf (_("    Arguments: %s\n"), arg_fmt);
17531
17532  return data == data_end;
17533}
17534
17535static const char *
17536get_ia64_vms_note_type (unsigned e_type)
17537{
17538  static char buff[64];
17539
17540  switch (e_type)
17541    {
17542    case NT_VMS_MHD:
17543      return _("NT_VMS_MHD (module header)");
17544    case NT_VMS_LNM:
17545      return _("NT_VMS_LNM (language name)");
17546    case NT_VMS_SRC:
17547      return _("NT_VMS_SRC (source files)");
17548    case NT_VMS_TITLE:
17549      return "NT_VMS_TITLE";
17550    case NT_VMS_EIDC:
17551      return _("NT_VMS_EIDC (consistency check)");
17552    case NT_VMS_FPMODE:
17553      return _("NT_VMS_FPMODE (FP mode)");
17554    case NT_VMS_LINKTIME:
17555      return "NT_VMS_LINKTIME";
17556    case NT_VMS_IMGNAM:
17557      return _("NT_VMS_IMGNAM (image name)");
17558    case NT_VMS_IMGID:
17559      return _("NT_VMS_IMGID (image id)");
17560    case NT_VMS_LINKID:
17561      return _("NT_VMS_LINKID (link id)");
17562    case NT_VMS_IMGBID:
17563      return _("NT_VMS_IMGBID (build id)");
17564    case NT_VMS_GSTNAM:
17565      return _("NT_VMS_GSTNAM (sym table name)");
17566    case NT_VMS_ORIG_DYN:
17567      return "NT_VMS_ORIG_DYN";
17568    case NT_VMS_PATCHTIME:
17569      return "NT_VMS_PATCHTIME";
17570    default:
17571      snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
17572      return buff;
17573    }
17574}
17575
17576static bfd_boolean
17577print_ia64_vms_note (Elf_Internal_Note * pnote)
17578{
17579  switch (pnote->type)
17580    {
17581    case NT_VMS_MHD:
17582      if (pnote->descsz > 36)
17583        {
17584          size_t l = strlen (pnote->descdata + 34);
17585          printf (_("    Creation date  : %.17s\n"), pnote->descdata);
17586          printf (_("    Last patch date: %.17s\n"), pnote->descdata + 17);
17587          printf (_("    Module name    : %s\n"), pnote->descdata + 34);
17588          printf (_("    Module version : %s\n"), pnote->descdata + 34 + l + 1);
17589        }
17590      else
17591        printf (_("    Invalid size\n"));
17592      break;
17593    case NT_VMS_LNM:
17594      printf (_("   Language: %s\n"), pnote->descdata);
17595      break;
17596#ifdef BFD64
17597    case NT_VMS_FPMODE:
17598      printf (_("   Floating Point mode: "));
17599      printf ("0x%016" BFD_VMA_FMT "x\n",
17600              (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
17601      break;
17602    case NT_VMS_LINKTIME:
17603      printf (_("   Link time: "));
17604      print_vms_time
17605        ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
17606      printf ("\n");
17607      break;
17608    case NT_VMS_PATCHTIME:
17609      printf (_("   Patch time: "));
17610      print_vms_time
17611        ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
17612      printf ("\n");
17613      break;
17614    case NT_VMS_ORIG_DYN:
17615      printf (_("   Major id: %u,  minor id: %u\n"),
17616              (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
17617              (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
17618      printf (_("   Last modified  : "));
17619      print_vms_time
17620        ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
17621      printf (_("\n   Link flags  : "));
17622      printf ("0x%016" BFD_VMA_FMT "x\n",
17623              (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
17624      printf (_("   Header flags: 0x%08x\n"),
17625              (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
17626      printf (_("   Image id    : %s\n"), pnote->descdata + 32);
17627      break;
17628#endif
17629    case NT_VMS_IMGNAM:
17630      printf (_("    Image name: %s\n"), pnote->descdata);
17631      break;
17632    case NT_VMS_GSTNAM:
17633      printf (_("    Global symbol table name: %s\n"), pnote->descdata);
17634      break;
17635    case NT_VMS_IMGID:
17636      printf (_("    Image id: %s\n"), pnote->descdata);
17637      break;
17638    case NT_VMS_LINKID:
17639      printf (_("    Linker id: %s\n"), pnote->descdata);
17640      break;
17641    default:
17642      return FALSE;
17643    }
17644  return TRUE;
17645}
17646
17647/* Find the symbol associated with a build attribute that is attached
17648   to address OFFSET.  If PNAME is non-NULL then store the name of
17649   the symbol (if found) in the provided pointer,  Returns NULL if a
17650   symbol could not be found.  */
17651
17652static Elf_Internal_Sym *
17653get_symbol_for_build_attribute (Filedata *       filedata,
17654				unsigned long    offset,
17655				bfd_boolean      is_open_attr,
17656				const char **    pname)
17657{
17658  static Filedata *         saved_filedata = NULL;
17659  static char *             strtab;
17660  static unsigned long      strtablen;
17661  static Elf_Internal_Sym * symtab;
17662  static unsigned long      nsyms;
17663  Elf_Internal_Sym *        saved_sym = NULL;
17664  Elf_Internal_Sym *        sym;
17665
17666  if (filedata->section_headers != NULL
17667      && (saved_filedata == NULL || filedata != saved_filedata))
17668    {
17669      Elf_Internal_Shdr * symsec;
17670
17671      /* Load the symbol and string sections.  */
17672      for (symsec = filedata->section_headers;
17673	   symsec < filedata->section_headers + filedata->file_header.e_shnum;
17674	   symsec ++)
17675	{
17676	  if (symsec->sh_type == SHT_SYMTAB)
17677	    {
17678	      symtab = GET_ELF_SYMBOLS (filedata, symsec, & nsyms);
17679
17680	      if (symsec->sh_link < filedata->file_header.e_shnum)
17681		{
17682		  Elf_Internal_Shdr * strtab_sec = filedata->section_headers + symsec->sh_link;
17683
17684		  strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
17685					      1, strtab_sec->sh_size,
17686					      _("string table"));
17687		  strtablen = strtab != NULL ? strtab_sec->sh_size : 0;
17688		}
17689	    }
17690	}
17691      saved_filedata = filedata;
17692    }
17693
17694  if (symtab == NULL || strtab == NULL)
17695    return NULL;
17696
17697  /* Find a symbol whose value matches offset.  */
17698  for (sym = symtab; sym < symtab + nsyms; sym ++)
17699    if (sym->st_value == offset)
17700      {
17701	if (sym->st_name >= strtablen)
17702	  /* Huh ?  This should not happen.  */
17703	  continue;
17704
17705	if (strtab[sym->st_name] == 0)
17706	  continue;
17707
17708	/* The AArch64 and ARM architectures define mapping symbols
17709	   (eg $d, $x, $t) which we want to ignore.  */
17710	if (strtab[sym->st_name] == '$'
17711	    && strtab[sym->st_name + 1] != 0
17712	    && strtab[sym->st_name + 2] == 0)
17713	  continue;
17714
17715	if (is_open_attr)
17716	  {
17717	    /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
17718	       and FILE or OBJECT symbols over NOTYPE symbols.  We skip
17719	       FUNC symbols entirely.  */
17720	    switch (ELF_ST_TYPE (sym->st_info))
17721	      {
17722	      case STT_OBJECT:
17723	      case STT_FILE:
17724		saved_sym = sym;
17725		if (sym->st_size)
17726		  {
17727		    /* If the symbol has a size associated
17728		       with it then we can stop searching.  */
17729		    sym = symtab + nsyms;
17730		  }
17731		continue;
17732
17733	      case STT_FUNC:
17734		/* Ignore function symbols.  */
17735		continue;
17736
17737	      default:
17738		break;
17739	      }
17740
17741	    switch (ELF_ST_BIND (sym->st_info))
17742	      {
17743	      case STB_GLOBAL:
17744		if (saved_sym == NULL
17745		    || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
17746		  saved_sym = sym;
17747		break;
17748
17749	      case STB_LOCAL:
17750		if (saved_sym == NULL)
17751		  saved_sym = sym;
17752		break;
17753
17754	      default:
17755		break;
17756	      }
17757	  }
17758	else
17759	  {
17760	    if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
17761	      continue;
17762
17763	    saved_sym = sym;
17764	    break;
17765	  }
17766      }
17767
17768  if (saved_sym && pname)
17769    * pname = strtab + saved_sym->st_name;
17770
17771  return saved_sym;
17772}
17773
17774static bfd_boolean
17775print_gnu_build_attribute_description (Elf_Internal_Note *  pnote,
17776				       Filedata *           filedata)
17777{
17778  static unsigned long  global_offset = 0;
17779  static unsigned long  global_end = 0;
17780  static unsigned long  func_offset = 0;
17781  static unsigned long  func_end = 0;
17782
17783  Elf_Internal_Sym *    sym;
17784  const char *          name;
17785  unsigned long         start;
17786  unsigned long         end;
17787  bfd_boolean           is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
17788
17789  switch (pnote->descsz)
17790    {
17791    case 0:
17792      /* A zero-length description means that the range of
17793	 the previous note of the same type should be used.  */
17794      if (is_open_attr)
17795	{
17796	  if (global_end > global_offset)
17797	    printf (_("    Applies to region from %#lx to %#lx\n"),
17798		    global_offset, global_end);
17799	  else
17800	    printf (_("    Applies to region from %#lx\n"), global_offset);
17801	}
17802      else
17803	{
17804	  if (func_end > func_offset)
17805	    printf (_("    Applies to region from %#lx to %#lx\n"), func_offset, func_end);
17806	  else
17807	    printf (_("    Applies to region from %#lx\n"), func_offset);
17808	}
17809      return TRUE;
17810
17811    case 4:
17812      start = byte_get ((unsigned char *) pnote->descdata, 4);
17813      end = 0;
17814      break;
17815
17816    case 8:
17817      if (is_32bit_elf)
17818	{
17819	  /* FIXME: We should check that version 3+ notes are being used here...  */
17820	  start = byte_get ((unsigned char *) pnote->descdata, 4);
17821	  end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
17822	}
17823      else
17824	{
17825	  start = byte_get ((unsigned char *) pnote->descdata, 8);
17826	  end = 0;
17827	}
17828      break;
17829
17830    case 16:
17831      start = byte_get ((unsigned char *) pnote->descdata, 8);
17832      end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
17833      break;
17834
17835    default:
17836      error (_("    <invalid description size: %lx>\n"), pnote->descsz);
17837      printf (_("    <invalid descsz>"));
17838      return FALSE;
17839    }
17840
17841  name = NULL;
17842  sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
17843  /* As of version 5 of the annobin plugin, filename symbols are biased by 2
17844     in order to avoid them being confused with the start address of the
17845     first function in the file...  */
17846  if (sym == NULL && is_open_attr)
17847    sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
17848					  & name);
17849
17850  if (end == 0 && sym != NULL && sym->st_size > 0)
17851    end = start + sym->st_size;
17852
17853  if (is_open_attr)
17854    {
17855      /* FIXME: Need to properly allow for section alignment.  16 is just the alignment used on x86_64.  */
17856      if (global_end > 0 && start > BFD_ALIGN (global_end, 16))
17857	warn (_("Gap in build notes detected from %#lx to %#lx\n"),
17858	      global_end + 1, start - 1);
17859
17860      printf (_("    Applies to region from %#lx"), start);
17861      global_offset = start;
17862
17863      if (end)
17864	{
17865	  printf (_(" to %#lx"), end);
17866	  global_end = end;
17867	}
17868    }
17869  else
17870    {
17871      printf (_("    Applies to region from %#lx"), start);
17872      func_offset = start;
17873
17874      if (end)
17875	{
17876	  printf (_(" to %#lx"), end);
17877	  func_end = end;
17878	}
17879    }
17880
17881  if (sym && name)
17882    printf (_(" (%s)"), name);
17883
17884  printf ("\n");
17885  return TRUE;
17886}
17887
17888static bfd_boolean
17889print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
17890{
17891  static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
17892  static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
17893  static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
17894  char         name_type;
17895  char         name_attribute;
17896  const char * expected_types;
17897  const char * name = pnote->namedata;
17898  const char * text;
17899  signed int   left;
17900
17901  if (name == NULL || pnote->namesz < 2)
17902    {
17903      error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
17904      print_symbol (-20, _("  <corrupt name>"));
17905      return FALSE;
17906    }
17907
17908  if (do_wide)
17909    left = 28;
17910  else
17911    left = 20;
17912
17913  /* Version 2 of the spec adds a "GA" prefix to the name field.  */
17914  if (name[0] == 'G' && name[1] == 'A')
17915    {
17916      if (pnote->namesz < 4)
17917	{
17918	  error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
17919	  print_symbol (-20, _("  <corrupt name>"));
17920	  return FALSE;
17921	}
17922
17923      printf ("GA");
17924      name += 2;
17925      left -= 2;
17926    }
17927
17928  switch ((name_type = * name))
17929    {
17930    case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
17931    case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
17932    case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
17933    case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
17934      printf ("%c", * name);
17935      left --;
17936      break;
17937    default:
17938      error (_("unrecognised attribute type in name field: %d\n"), name_type);
17939      print_symbol (-20, _("<unknown name type>"));
17940      return FALSE;
17941    }
17942
17943  ++ name;
17944  text = NULL;
17945
17946  switch ((name_attribute = * name))
17947    {
17948    case GNU_BUILD_ATTRIBUTE_VERSION:
17949      text = _("<version>");
17950      expected_types = string_expected;
17951      ++ name;
17952      break;
17953    case GNU_BUILD_ATTRIBUTE_STACK_PROT:
17954      text = _("<stack prot>");
17955      expected_types = "!+*";
17956      ++ name;
17957      break;
17958    case GNU_BUILD_ATTRIBUTE_RELRO:
17959      text = _("<relro>");
17960      expected_types = bool_expected;
17961      ++ name;
17962      break;
17963    case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
17964      text = _("<stack size>");
17965      expected_types = number_expected;
17966      ++ name;
17967      break;
17968    case GNU_BUILD_ATTRIBUTE_TOOL:
17969      text = _("<tool>");
17970      expected_types = string_expected;
17971      ++ name;
17972      break;
17973    case GNU_BUILD_ATTRIBUTE_ABI:
17974      text = _("<ABI>");
17975      expected_types = "$*";
17976      ++ name;
17977      break;
17978    case GNU_BUILD_ATTRIBUTE_PIC:
17979      text = _("<PIC>");
17980      expected_types = number_expected;
17981      ++ name;
17982      break;
17983    case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
17984      text = _("<short enum>");
17985      expected_types = bool_expected;
17986      ++ name;
17987      break;
17988    default:
17989      if (ISPRINT (* name))
17990	{
17991	  int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
17992
17993	  if (len > left && ! do_wide)
17994	    len = left;
17995	  printf ("%.*s:", len, name);
17996	  left -= len;
17997	  name += len;
17998	}
17999      else
18000	{
18001	  static char tmpbuf [128];
18002
18003	  error (_("unrecognised byte in name field: %d\n"), * name);
18004	  sprintf (tmpbuf, _("<unknown:_%d>"), * name);
18005	  text = tmpbuf;
18006	  name ++;
18007	}
18008      expected_types = "*$!+";
18009      break;
18010    }
18011
18012  if (text)
18013    left -= printf ("%s", text);
18014
18015  if (strchr (expected_types, name_type) == NULL)
18016    warn (_("attribute does not have an expected type (%c)\n"), name_type);
18017
18018  if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
18019    {
18020      error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
18021	     (unsigned long) pnote->namesz,
18022	     (long) (name - pnote->namedata));
18023      return FALSE;
18024    }
18025
18026  if (left < 1 && ! do_wide)
18027    return TRUE;
18028
18029  switch (name_type)
18030    {
18031    case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
18032      {
18033	unsigned int        bytes;
18034	unsigned long long  val = 0;
18035	unsigned int        shift = 0;
18036	char *              decoded = NULL;
18037
18038	bytes = pnote->namesz - (name - pnote->namedata);
18039	if (bytes > 0)
18040	  /* The -1 is because the name field is always 0 terminated, and we
18041	     want to be able to ensure that the shift in the while loop below
18042	     will not overflow.  */
18043	  -- bytes;
18044
18045	if (bytes > sizeof (val))
18046	  {
18047	    error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
18048		   bytes);
18049	    bytes = sizeof (val);
18050	  }
18051	/* We do not bother to warn if bytes == 0 as this can
18052	   happen with some early versions of the gcc plugin.  */
18053
18054	while (bytes --)
18055	  {
18056	    unsigned long byte = (* name ++) & 0xff;
18057
18058	    val |= byte << shift;
18059	    shift += 8;
18060	  }
18061
18062	switch (name_attribute)
18063	  {
18064	  case GNU_BUILD_ATTRIBUTE_PIC:
18065	    switch (val)
18066	      {
18067	      case 0: decoded = "static"; break;
18068	      case 1: decoded = "pic"; break;
18069	      case 2: decoded = "PIC"; break;
18070	      case 3: decoded = "pie"; break;
18071	      case 4: decoded = "PIE"; break;
18072	      default: break;
18073	      }
18074	    break;
18075	  case GNU_BUILD_ATTRIBUTE_STACK_PROT:
18076	    switch (val)
18077	      {
18078		/* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c.  */
18079	      case 0: decoded = "off"; break;
18080	      case 1: decoded = "on"; break;
18081	      case 2: decoded = "all"; break;
18082	      case 3: decoded = "strong"; break;
18083	      case 4: decoded = "explicit"; break;
18084	      default: break;
18085	      }
18086	    break;
18087	  default:
18088	    break;
18089	  }
18090
18091	if (decoded != NULL)
18092	  {
18093	    print_symbol (-left, decoded);
18094	    left = 0;
18095	  }
18096	else if (val == 0)
18097	  {
18098	    printf ("0x0");
18099	    left -= 3;
18100	  }
18101	else
18102	  {
18103	    if (do_wide)
18104	      left -= printf ("0x%llx", val);
18105	    else
18106	      left -= printf ("0x%-.*llx", left, val);
18107	  }
18108      }
18109      break;
18110    case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
18111      left -= print_symbol (- left, name);
18112      break;
18113    case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
18114      left -= print_symbol (- left, "true");
18115      break;
18116    case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
18117      left -= print_symbol (- left, "false");
18118      break;
18119    }
18120
18121  if (do_wide && left > 0)
18122    printf ("%-*s", left, " ");
18123
18124  return TRUE;
18125}
18126
18127/* Note that by the ELF standard, the name field is already null byte
18128   terminated, and namesz includes the terminating null byte.
18129   I.E. the value of namesz for the name "FSF" is 4.
18130
18131   If the value of namesz is zero, there is no name present.  */
18132
18133static bfd_boolean
18134process_note (Elf_Internal_Note *  pnote,
18135	      Filedata *           filedata)
18136{
18137  const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
18138  const char * nt;
18139
18140  if (pnote->namesz == 0)
18141    /* If there is no note name, then use the default set of
18142       note type strings.  */
18143    nt = get_note_type (filedata, pnote->type);
18144
18145  else if (const_strneq (pnote->namedata, "GNU"))
18146    /* GNU-specific object file notes.  */
18147    nt = get_gnu_elf_note_type (pnote->type);
18148
18149  else if (const_strneq (pnote->namedata, "FreeBSD"))
18150    /* FreeBSD-specific core file notes.  */
18151    nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
18152
18153  else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
18154    /* NetBSD-specific core file notes.  */
18155    nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
18156
18157  else if (const_strneq (pnote->namedata, "NetBSD"))
18158    /* NetBSD-specific core file notes.  */
18159    return process_netbsd_elf_note (pnote);
18160
18161  else if (const_strneq (pnote->namedata, "PaX"))
18162    /* NetBSD-specific core file notes.  */
18163    return process_netbsd_elf_note (pnote);
18164
18165  else if (strneq (pnote->namedata, "SPU/", 4))
18166    {
18167      /* SPU-specific core file notes.  */
18168      nt = pnote->namedata + 4;
18169      name = "SPU";
18170    }
18171
18172  else if (const_strneq (pnote->namedata, "IPF/VMS"))
18173    /* VMS/ia64-specific file notes.  */
18174    nt = get_ia64_vms_note_type (pnote->type);
18175
18176  else if (const_strneq (pnote->namedata, "stapsdt"))
18177    nt = get_stapsdt_note_type (pnote->type);
18178
18179  else
18180    /* Don't recognize this note name; just use the default set of
18181       note type strings.  */
18182    nt = get_note_type (filedata, pnote->type);
18183
18184  printf ("  ");
18185
18186  if (((const_strneq (pnote->namedata, "GA")
18187	&& strchr ("*$!+", pnote->namedata[2]) != NULL)
18188       || strchr ("*$!+", pnote->namedata[0]) != NULL)
18189      && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
18190	  || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
18191    print_gnu_build_attribute_name (pnote);
18192  else
18193    print_symbol (-20, name);
18194
18195  if (do_wide)
18196    printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
18197  else
18198    printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
18199
18200  if (const_strneq (pnote->namedata, "IPF/VMS"))
18201    return print_ia64_vms_note (pnote);
18202  else if (const_strneq (pnote->namedata, "GNU"))
18203    return print_gnu_note (filedata, pnote);
18204  else if (const_strneq (pnote->namedata, "stapsdt"))
18205    return print_stapsdt_note (pnote);
18206  else if (const_strneq (pnote->namedata, "CORE"))
18207    return print_core_note (pnote);
18208  else if (((const_strneq (pnote->namedata, "GA")
18209	     && strchr ("*$!+", pnote->namedata[2]) != NULL)
18210	    || strchr ("*$!+", pnote->namedata[0]) != NULL)
18211	   && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
18212	       || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
18213    return print_gnu_build_attribute_description (pnote, filedata);
18214
18215  if (pnote->descsz)
18216    {
18217      unsigned long i;
18218
18219      printf (_("   description data: "));
18220      for (i = 0; i < pnote->descsz; i++)
18221	printf ("%02x ", pnote->descdata[i] & 0xff);
18222      if (!do_wide)
18223	printf ("\n");
18224    }
18225
18226  if (do_wide)
18227    printf ("\n");
18228
18229  return TRUE;
18230}
18231
18232static bfd_boolean
18233process_notes_at (Filedata *           filedata,
18234		  Elf_Internal_Shdr *  section,
18235		  bfd_vma              offset,
18236		  bfd_vma              length,
18237		  bfd_vma              align)
18238{
18239  Elf_External_Note * pnotes;
18240  Elf_External_Note * external;
18241  char *              end;
18242  bfd_boolean         res = TRUE;
18243
18244  if (length <= 0)
18245    return FALSE;
18246
18247  if (section)
18248    {
18249      pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
18250      if (pnotes)
18251	{
18252	  if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
18253	    return FALSE;
18254	}
18255    }
18256  else
18257    pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
18258					     _("notes"));
18259
18260  if (pnotes == NULL)
18261    return FALSE;
18262
18263  external = pnotes;
18264
18265  if (section)
18266    printf (_("\nDisplaying notes found in: %s\n"), printable_section_name (filedata, section));
18267  else
18268    printf (_("\nDisplaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
18269	    (unsigned long) offset, (unsigned long) length);
18270
18271  /* NB: Some note sections may have alignment value of 0 or 1.  gABI
18272     specifies that notes should be aligned to 4 bytes in 32-bit
18273     objects and to 8 bytes in 64-bit objects.  As a Linux extension,
18274     we also support 4 byte alignment in 64-bit objects.  If section
18275     alignment is less than 4, we treate alignment as 4 bytes.   */
18276  if (align < 4)
18277    align = 4;
18278  else if (align != 4 && align != 8)
18279    {
18280      warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
18281	    (long) align);
18282      return FALSE;
18283    }
18284
18285  printf (_("  %-20s %10s\tDescription\n"), _("Owner"), _("Data size"));
18286
18287  end = (char *) pnotes + length;
18288  while ((char *) external < end)
18289    {
18290      Elf_Internal_Note inote;
18291      size_t min_notesz;
18292      char * next;
18293      char * temp = NULL;
18294      size_t data_remaining = end - (char *) external;
18295
18296      if (!is_ia64_vms (filedata))
18297	{
18298	  /* PR binutils/15191
18299	     Make sure that there is enough data to read.  */
18300	  min_notesz = offsetof (Elf_External_Note, name);
18301	  if (data_remaining < min_notesz)
18302	    {
18303	      warn (ngettext ("Corrupt note: only %ld byte remains, "
18304			      "not enough for a full note\n",
18305			      "Corrupt note: only %ld bytes remain, "
18306			      "not enough for a full note\n",
18307			      data_remaining),
18308		    (long) data_remaining);
18309	      break;
18310	    }
18311	  data_remaining -= min_notesz;
18312
18313	  inote.type     = BYTE_GET (external->type);
18314	  inote.namesz   = BYTE_GET (external->namesz);
18315	  inote.namedata = external->name;
18316	  inote.descsz   = BYTE_GET (external->descsz);
18317	  inote.descdata = ((char *) external
18318			    + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
18319	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
18320	  next = ((char *) external
18321		  + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
18322	}
18323      else
18324	{
18325	  Elf64_External_VMS_Note *vms_external;
18326
18327	  /* PR binutils/15191
18328	     Make sure that there is enough data to read.  */
18329	  min_notesz = offsetof (Elf64_External_VMS_Note, name);
18330	  if (data_remaining < min_notesz)
18331	    {
18332	      warn (ngettext ("Corrupt note: only %ld byte remains, "
18333			      "not enough for a full note\n",
18334			      "Corrupt note: only %ld bytes remain, "
18335			      "not enough for a full note\n",
18336			      data_remaining),
18337		    (long) data_remaining);
18338	      break;
18339	    }
18340	  data_remaining -= min_notesz;
18341
18342	  vms_external = (Elf64_External_VMS_Note *) external;
18343	  inote.type     = BYTE_GET (vms_external->type);
18344	  inote.namesz   = BYTE_GET (vms_external->namesz);
18345	  inote.namedata = vms_external->name;
18346	  inote.descsz   = BYTE_GET (vms_external->descsz);
18347	  inote.descdata = inote.namedata + align_power (inote.namesz, 3);
18348	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
18349	  next = inote.descdata + align_power (inote.descsz, 3);
18350	}
18351
18352      /* PR 17531: file: 3443835e.  */
18353      /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4.  */
18354      if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
18355	  || (size_t) (inote.descdata - inote.namedata) > data_remaining
18356	  || (size_t) (next - inote.descdata) < inote.descsz
18357	  || ((size_t) (next - inote.descdata)
18358	      > data_remaining - (size_t) (inote.descdata - inote.namedata)))
18359	{
18360	  warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
18361		(unsigned long) ((char *) external - (char *) pnotes));
18362	  warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
18363		inote.type, inote.namesz, inote.descsz, (int) align);
18364	  break;
18365	}
18366
18367      external = (Elf_External_Note *) next;
18368
18369      /* Verify that name is null terminated.  It appears that at least
18370	 one version of Linux (RedHat 6.0) generates corefiles that don't
18371	 comply with the ELF spec by failing to include the null byte in
18372	 namesz.  */
18373      if (inote.namedata[inote.namesz - 1] != '\0')
18374	{
18375	  if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
18376	    {
18377	      temp = (char *) malloc (inote.namesz + 1);
18378	      if (temp == NULL)
18379		{
18380		  error (_("Out of memory allocating space for inote name\n"));
18381		  res = FALSE;
18382		  break;
18383		}
18384
18385	      memcpy (temp, inote.namedata, inote.namesz);
18386	      inote.namedata = temp;
18387	    }
18388	  inote.namedata[inote.namesz] = 0;
18389	}
18390
18391      if (! process_note (& inote, filedata))
18392	res = FALSE;
18393
18394      if (temp != NULL)
18395	{
18396	  free (temp);
18397	  temp = NULL;
18398	}
18399    }
18400
18401  free (pnotes);
18402
18403  return res;
18404}
18405
18406static bfd_boolean
18407process_corefile_note_segments (Filedata * filedata)
18408{
18409  Elf_Internal_Phdr * segment;
18410  unsigned int i;
18411  bfd_boolean res = TRUE;
18412
18413  if (! get_program_headers (filedata))
18414    return TRUE;
18415
18416  for (i = 0, segment = filedata->program_headers;
18417       i < filedata->file_header.e_phnum;
18418       i++, segment++)
18419    {
18420      if (segment->p_type == PT_NOTE)
18421	if (! process_notes_at (filedata, NULL,
18422				(bfd_vma) segment->p_offset,
18423				(bfd_vma) segment->p_filesz,
18424				(bfd_vma) segment->p_align))
18425	  res = FALSE;
18426    }
18427
18428  return res;
18429}
18430
18431static bfd_boolean
18432process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
18433{
18434  Elf_External_Note * pnotes;
18435  Elf_External_Note * external;
18436  char * end;
18437  bfd_boolean res = TRUE;
18438
18439  if (length <= 0)
18440    return FALSE;
18441
18442  pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
18443                                           _("v850 notes"));
18444  if (pnotes == NULL)
18445    return FALSE;
18446
18447  external = pnotes;
18448  end = (char*) pnotes + length;
18449
18450  printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
18451	  (unsigned long) offset, (unsigned long) length);
18452
18453  while ((char *) external + sizeof (Elf_External_Note) < end)
18454    {
18455      Elf_External_Note * next;
18456      Elf_Internal_Note inote;
18457
18458      inote.type     = BYTE_GET (external->type);
18459      inote.namesz   = BYTE_GET (external->namesz);
18460      inote.namedata = external->name;
18461      inote.descsz   = BYTE_GET (external->descsz);
18462      inote.descdata = inote.namedata + align_power (inote.namesz, 2);
18463      inote.descpos  = offset + (inote.descdata - (char *) pnotes);
18464
18465      if (inote.descdata < (char *) pnotes || inote.descdata >= end)
18466	{
18467	  warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
18468	  inote.descdata = inote.namedata;
18469	  inote.namesz   = 0;
18470	}
18471
18472      next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
18473
18474      if (   ((char *) next > end)
18475	  || ((char *) next <  (char *) pnotes))
18476	{
18477	  warn (_("corrupt descsz found in note at offset 0x%lx\n"),
18478		(unsigned long) ((char *) external - (char *) pnotes));
18479	  warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
18480		inote.type, inote.namesz, inote.descsz);
18481	  break;
18482	}
18483
18484      external = next;
18485
18486      /* Prevent out-of-bounds indexing.  */
18487      if (   inote.namedata + inote.namesz > end
18488	  || inote.namedata + inote.namesz < inote.namedata)
18489        {
18490          warn (_("corrupt namesz found in note at offset 0x%lx\n"),
18491                (unsigned long) ((char *) external - (char *) pnotes));
18492          warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
18493                inote.type, inote.namesz, inote.descsz);
18494          break;
18495        }
18496
18497      printf ("  %s: ", get_v850_elf_note_type (inote.type));
18498
18499      if (! print_v850_note (& inote))
18500	{
18501	  res = FALSE;
18502	  printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
18503		  inote.namesz, inote.descsz);
18504	}
18505    }
18506
18507  free (pnotes);
18508
18509  return res;
18510}
18511
18512static bfd_boolean
18513process_note_sections (Filedata * filedata)
18514{
18515  Elf_Internal_Shdr * section;
18516  unsigned long i;
18517  unsigned int n = 0;
18518  bfd_boolean res = TRUE;
18519
18520  for (i = 0, section = filedata->section_headers;
18521       i < filedata->file_header.e_shnum && section != NULL;
18522       i++, section++)
18523    {
18524      if (section->sh_type == SHT_NOTE)
18525	{
18526	  if (! process_notes_at (filedata, section,
18527				  (bfd_vma) section->sh_offset,
18528				  (bfd_vma) section->sh_size,
18529				  (bfd_vma) section->sh_addralign))
18530	    res = FALSE;
18531	  n++;
18532	}
18533
18534      if ((   filedata->file_header.e_machine == EM_V800
18535	   || filedata->file_header.e_machine == EM_V850
18536	   || filedata->file_header.e_machine == EM_CYGNUS_V850)
18537	  && section->sh_type == SHT_RENESAS_INFO)
18538	{
18539	  if (! process_v850_notes (filedata,
18540				    (bfd_vma) section->sh_offset,
18541				    (bfd_vma) section->sh_size))
18542	    res = FALSE;
18543	  n++;
18544	}
18545    }
18546
18547  if (n == 0)
18548    /* Try processing NOTE segments instead.  */
18549    return process_corefile_note_segments (filedata);
18550
18551  return res;
18552}
18553
18554static bfd_boolean
18555process_notes (Filedata * filedata)
18556{
18557  /* If we have not been asked to display the notes then do nothing.  */
18558  if (! do_notes)
18559    return TRUE;
18560
18561  if (filedata->file_header.e_type != ET_CORE)
18562    return process_note_sections (filedata);
18563
18564  /* No program headers means no NOTE segment.  */
18565  if (filedata->file_header.e_phnum > 0)
18566    return process_corefile_note_segments (filedata);
18567
18568  printf (_("No note segments present in the core file.\n"));
18569  return TRUE;
18570}
18571
18572static unsigned char *
18573display_public_gnu_attributes (unsigned char * start,
18574			       const unsigned char * const end)
18575{
18576  printf (_("  Unknown GNU attribute: %s\n"), start);
18577
18578  start += strnlen ((char *) start, end - start);
18579  display_raw_attribute (start, end);
18580
18581  return (unsigned char *) end;
18582}
18583
18584static unsigned char *
18585display_generic_attribute (unsigned char * start,
18586			   unsigned int tag,
18587			   const unsigned char * const end)
18588{
18589  if (tag == 0)
18590    return (unsigned char *) end;
18591
18592  return display_tag_value (tag, start, end);
18593}
18594
18595static bfd_boolean
18596process_arch_specific (Filedata * filedata)
18597{
18598  if (! do_arch)
18599    return TRUE;
18600
18601  switch (filedata->file_header.e_machine)
18602    {
18603    case EM_ARC:
18604    case EM_ARC_COMPACT:
18605    case EM_ARC_COMPACT2:
18606      return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
18607				 display_arc_attribute,
18608				 display_generic_attribute);
18609    case EM_ARM:
18610      return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
18611				 display_arm_attribute,
18612				 display_generic_attribute);
18613
18614    case EM_MIPS:
18615    case EM_MIPS_RS3_LE:
18616      return process_mips_specific (filedata);
18617
18618    case EM_MSP430:
18619     return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
18620				display_msp430x_attribute,
18621				display_generic_attribute);
18622
18623    case EM_NDS32:
18624      return process_nds32_specific (filedata);
18625
18626    case EM_PPC:
18627    case EM_PPC64:
18628      return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18629				 display_power_gnu_attribute);
18630
18631    case EM_S390:
18632    case EM_S390_OLD:
18633      return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18634				 display_s390_gnu_attribute);
18635
18636    case EM_SPARC:
18637    case EM_SPARC32PLUS:
18638    case EM_SPARCV9:
18639      return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18640				 display_sparc_gnu_attribute);
18641
18642    case EM_TI_C6000:
18643      return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
18644				 display_tic6x_attribute,
18645				 display_generic_attribute);
18646
18647    default:
18648      return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
18649				 display_public_gnu_attributes,
18650				 display_generic_attribute);
18651    }
18652}
18653
18654static bfd_boolean
18655get_file_header (Filedata * filedata)
18656{
18657  /* Read in the identity array.  */
18658  if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
18659    return FALSE;
18660
18661  /* Determine how to read the rest of the header.  */
18662  switch (filedata->file_header.e_ident[EI_DATA])
18663    {
18664    default:
18665    case ELFDATANONE:
18666    case ELFDATA2LSB:
18667      byte_get = byte_get_little_endian;
18668      byte_put = byte_put_little_endian;
18669      break;
18670    case ELFDATA2MSB:
18671      byte_get = byte_get_big_endian;
18672      byte_put = byte_put_big_endian;
18673      break;
18674    }
18675
18676  /* For now we only support 32 bit and 64 bit ELF files.  */
18677  is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
18678
18679  /* Read in the rest of the header.  */
18680  if (is_32bit_elf)
18681    {
18682      Elf32_External_Ehdr ehdr32;
18683
18684      if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
18685	return FALSE;
18686
18687      filedata->file_header.e_type      = BYTE_GET (ehdr32.e_type);
18688      filedata->file_header.e_machine   = BYTE_GET (ehdr32.e_machine);
18689      filedata->file_header.e_version   = BYTE_GET (ehdr32.e_version);
18690      filedata->file_header.e_entry     = BYTE_GET (ehdr32.e_entry);
18691      filedata->file_header.e_phoff     = BYTE_GET (ehdr32.e_phoff);
18692      filedata->file_header.e_shoff     = BYTE_GET (ehdr32.e_shoff);
18693      filedata->file_header.e_flags     = BYTE_GET (ehdr32.e_flags);
18694      filedata->file_header.e_ehsize    = BYTE_GET (ehdr32.e_ehsize);
18695      filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
18696      filedata->file_header.e_phnum     = BYTE_GET (ehdr32.e_phnum);
18697      filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
18698      filedata->file_header.e_shnum     = BYTE_GET (ehdr32.e_shnum);
18699      filedata->file_header.e_shstrndx  = BYTE_GET (ehdr32.e_shstrndx);
18700    }
18701  else
18702    {
18703      Elf64_External_Ehdr ehdr64;
18704
18705      /* If we have been compiled with sizeof (bfd_vma) == 4, then
18706	 we will not be able to cope with the 64bit data found in
18707	 64 ELF files.  Detect this now and abort before we start
18708	 overwriting things.  */
18709      if (sizeof (bfd_vma) < 8)
18710	{
18711	  error (_("This instance of readelf has been built without support for a\n\
1871264 bit data type and so it cannot read 64 bit ELF files.\n"));
18713	  return FALSE;
18714	}
18715
18716      if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
18717	return FALSE;
18718
18719      filedata->file_header.e_type      = BYTE_GET (ehdr64.e_type);
18720      filedata->file_header.e_machine   = BYTE_GET (ehdr64.e_machine);
18721      filedata->file_header.e_version   = BYTE_GET (ehdr64.e_version);
18722      filedata->file_header.e_entry     = BYTE_GET (ehdr64.e_entry);
18723      filedata->file_header.e_phoff     = BYTE_GET (ehdr64.e_phoff);
18724      filedata->file_header.e_shoff     = BYTE_GET (ehdr64.e_shoff);
18725      filedata->file_header.e_flags     = BYTE_GET (ehdr64.e_flags);
18726      filedata->file_header.e_ehsize    = BYTE_GET (ehdr64.e_ehsize);
18727      filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
18728      filedata->file_header.e_phnum     = BYTE_GET (ehdr64.e_phnum);
18729      filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
18730      filedata->file_header.e_shnum     = BYTE_GET (ehdr64.e_shnum);
18731      filedata->file_header.e_shstrndx  = BYTE_GET (ehdr64.e_shstrndx);
18732    }
18733
18734  if (filedata->file_header.e_shoff)
18735    {
18736      /* There may be some extensions in the first section header.  Don't
18737	 bomb if we can't read it.  */
18738      if (is_32bit_elf)
18739	get_32bit_section_headers (filedata, TRUE);
18740      else
18741	get_64bit_section_headers (filedata, TRUE);
18742    }
18743
18744  return TRUE;
18745}
18746
18747static void
18748close_file (Filedata * filedata)
18749{
18750  if (filedata)
18751    {
18752      if (filedata->handle)
18753	fclose (filedata->handle);
18754      free (filedata);
18755    }
18756}
18757
18758void
18759close_debug_file (void * data)
18760{
18761  close_file ((Filedata *) data);
18762}
18763
18764static Filedata *
18765open_file (const char * pathname)
18766{
18767  struct stat  statbuf;
18768  Filedata *   filedata = NULL;
18769
18770  if (stat (pathname, & statbuf) < 0
18771      || ! S_ISREG (statbuf.st_mode))
18772    goto fail;
18773
18774  filedata = calloc (1, sizeof * filedata);
18775  if (filedata == NULL)
18776    goto fail;
18777
18778  filedata->handle = fopen (pathname, "rb");
18779  if (filedata->handle == NULL)
18780    goto fail;
18781
18782  filedata->file_size = (bfd_size_type) statbuf.st_size;
18783  filedata->file_name = pathname;
18784
18785  if (! get_file_header (filedata))
18786    goto fail;
18787
18788  if (filedata->file_header.e_shoff)
18789    {
18790      bfd_boolean res;
18791
18792      /* Read the section headers again, this time for real.  */
18793      if (is_32bit_elf)
18794	res = get_32bit_section_headers (filedata, FALSE);
18795      else
18796	res = get_64bit_section_headers (filedata, FALSE);
18797
18798      if (!res)
18799	goto fail;
18800    }
18801
18802  return filedata;
18803
18804 fail:
18805  if (filedata)
18806    {
18807      if (filedata->handle)
18808        fclose (filedata->handle);
18809      free (filedata);
18810    }
18811  return NULL;
18812}
18813
18814void *
18815open_debug_file (const char * pathname)
18816{
18817  return open_file (pathname);
18818}
18819
18820/* Process one ELF object file according to the command line options.
18821   This file may actually be stored in an archive.  The file is
18822   positioned at the start of the ELF object.  Returns TRUE if no
18823   problems were encountered, FALSE otherwise.  */
18824
18825static bfd_boolean
18826process_object (Filedata * filedata)
18827{
18828  Filedata * separates;
18829  unsigned int i;
18830  bfd_boolean res = TRUE;
18831
18832  if (! get_file_header (filedata))
18833    {
18834      error (_("%s: Failed to read file header\n"), filedata->file_name);
18835      return FALSE;
18836    }
18837
18838  /* Initialise per file variables.  */
18839  for (i = ARRAY_SIZE (version_info); i--;)
18840    version_info[i] = 0;
18841
18842  for (i = ARRAY_SIZE (dynamic_info); i--;)
18843    dynamic_info[i] = 0;
18844  dynamic_info_DT_GNU_HASH = 0;
18845
18846  /* Process the file.  */
18847  if (show_name)
18848    printf (_("\nFile: %s\n"), filedata->file_name);
18849
18850  /* Initialise the dump_sects array from the cmdline_dump_sects array.
18851     Note we do this even if cmdline_dump_sects is empty because we
18852     must make sure that the dump_sets array is zeroed out before each
18853     object file is processed.  */
18854  if (filedata->num_dump_sects > cmdline.num_dump_sects)
18855    memset (filedata->dump_sects, 0, filedata->num_dump_sects * sizeof (* filedata->dump_sects));
18856
18857  if (cmdline.num_dump_sects > 0)
18858    {
18859      if (filedata->num_dump_sects == 0)
18860	/* A sneaky way of allocating the dump_sects array.  */
18861	request_dump_bynumber (filedata, cmdline.num_dump_sects, 0);
18862
18863      assert (filedata->num_dump_sects >= cmdline.num_dump_sects);
18864      memcpy (filedata->dump_sects, cmdline.dump_sects,
18865	      cmdline.num_dump_sects * sizeof (* filedata->dump_sects));
18866    }
18867
18868  if (! process_file_header (filedata))
18869    return FALSE;
18870
18871  if (! process_section_headers (filedata))
18872    {
18873      /* Without loaded section headers we cannot process lots of things.  */
18874      do_unwind = do_version = do_dump = do_arch = FALSE;
18875
18876      if (! do_using_dynamic)
18877	do_syms = do_dyn_syms = do_reloc = FALSE;
18878    }
18879
18880  if (! process_section_groups (filedata))
18881    /* Without loaded section groups we cannot process unwind.  */
18882    do_unwind = FALSE;
18883
18884  if (process_program_headers (filedata))
18885    process_dynamic_section (filedata);
18886  else
18887    res = FALSE;
18888
18889  if (! process_relocs (filedata))
18890    res = FALSE;
18891
18892  if (! process_unwind (filedata))
18893    res = FALSE;
18894
18895  if (! process_symbol_table (filedata))
18896    res = FALSE;
18897
18898  if (! process_syminfo (filedata))
18899    res = FALSE;
18900
18901  if (! process_version_sections (filedata))
18902    res = FALSE;
18903
18904  if (filedata->file_header.e_shstrndx != SHN_UNDEF)
18905    separates = load_separate_debug_file (filedata, filedata->file_name);
18906  else
18907    separates = NULL;
18908
18909  if (! process_section_contents (filedata))
18910    res = FALSE;
18911
18912  if (separates)
18913    {
18914      if (! process_section_headers (separates))
18915	res = FALSE;
18916      else if (! process_section_contents (separates))
18917	res = FALSE;
18918    }
18919
18920  if (! process_notes (filedata))
18921    res = FALSE;
18922
18923  if (! process_gnu_liblist (filedata))
18924    res = FALSE;
18925
18926  if (! process_arch_specific (filedata))
18927    res = FALSE;
18928
18929  free (filedata->program_headers);
18930  filedata->program_headers = NULL;
18931
18932  free (filedata->section_headers);
18933  filedata->section_headers = NULL;
18934
18935  free (filedata->string_table);
18936  filedata->string_table = NULL;
18937  filedata->string_table_length = 0;
18938
18939  if (dynamic_strings)
18940    {
18941      free (dynamic_strings);
18942      dynamic_strings = NULL;
18943      dynamic_strings_length = 0;
18944    }
18945
18946  if (dynamic_symbols)
18947    {
18948      free (dynamic_symbols);
18949      dynamic_symbols = NULL;
18950      num_dynamic_syms = 0;
18951    }
18952
18953  if (dynamic_syminfo)
18954    {
18955      free (dynamic_syminfo);
18956      dynamic_syminfo = NULL;
18957    }
18958
18959  if (dynamic_section)
18960    {
18961      free (dynamic_section);
18962      dynamic_section = NULL;
18963    }
18964
18965  if (section_headers_groups)
18966    {
18967      free (section_headers_groups);
18968      section_headers_groups = NULL;
18969    }
18970
18971  if (section_groups)
18972    {
18973      struct group_list * g;
18974      struct group_list * next;
18975
18976      for (i = 0; i < group_count; i++)
18977	{
18978	  for (g = section_groups [i].root; g != NULL; g = next)
18979	    {
18980	      next = g->next;
18981	      free (g);
18982	    }
18983	}
18984
18985      free (section_groups);
18986      section_groups = NULL;
18987    }
18988
18989  free_debug_memory ();
18990
18991  return res;
18992}
18993
18994/* Process an ELF archive.
18995   On entry the file is positioned just after the ARMAG string.
18996   Returns TRUE upon success, FALSE otherwise.  */
18997
18998static bfd_boolean
18999process_archive (Filedata * filedata, bfd_boolean is_thin_archive)
19000{
19001  struct archive_info arch;
19002  struct archive_info nested_arch;
19003  size_t got;
19004  bfd_boolean ret = TRUE;
19005
19006  show_name = TRUE;
19007
19008  /* The ARCH structure is used to hold information about this archive.  */
19009  arch.file_name = NULL;
19010  arch.file = NULL;
19011  arch.index_array = NULL;
19012  arch.sym_table = NULL;
19013  arch.longnames = NULL;
19014
19015  /* The NESTED_ARCH structure is used as a single-item cache of information
19016     about a nested archive (when members of a thin archive reside within
19017     another regular archive file).  */
19018  nested_arch.file_name = NULL;
19019  nested_arch.file = NULL;
19020  nested_arch.index_array = NULL;
19021  nested_arch.sym_table = NULL;
19022  nested_arch.longnames = NULL;
19023
19024  if (setup_archive (&arch, filedata->file_name, filedata->handle,
19025		     is_thin_archive, do_archive_index) != 0)
19026    {
19027      ret = FALSE;
19028      goto out;
19029    }
19030
19031  if (do_archive_index)
19032    {
19033      if (arch.sym_table == NULL)
19034	error (_("%s: unable to dump the index as none was found\n"), filedata->file_name);
19035      else
19036	{
19037	  unsigned long i, l;
19038	  unsigned long current_pos;
19039
19040	  printf (_("Index of archive %s: (%lu entries, 0x%lx bytes in the symbol table)\n"),
19041		  filedata->file_name, (unsigned long) arch.index_num, arch.sym_size);
19042
19043	  current_pos = ftell (filedata->handle);
19044
19045	  for (i = l = 0; i < arch.index_num; i++)
19046	    {
19047	      if ((i == 0) || ((i > 0) && (arch.index_array[i] != arch.index_array[i - 1])))
19048	        {
19049	          char * member_name;
19050
19051		  member_name = get_archive_member_name_at (&arch, arch.index_array[i], &nested_arch);
19052
19053                  if (member_name != NULL)
19054                    {
19055	              char * qualified_name = make_qualified_name (&arch, &nested_arch, member_name);
19056
19057                      if (qualified_name != NULL)
19058                        {
19059		          printf (_("Contents of binary %s at offset "), qualified_name);
19060			  (void) print_vma (arch.index_array[i], PREFIX_HEX);
19061			  putchar ('\n');
19062		          free (qualified_name);
19063		        }
19064		    }
19065		}
19066
19067	      if (l >= arch.sym_size)
19068		{
19069		  error (_("%s: end of the symbol table reached before the end of the index\n"),
19070			 filedata->file_name);
19071		  ret = FALSE;
19072		  break;
19073		}
19074	      /* PR 17531: file: 0b6630b2.  */
19075	      printf ("\t%.*s\n", (int) (arch.sym_size - l), arch.sym_table + l);
19076	      l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
19077	    }
19078
19079	  if (arch.uses_64bit_indicies)
19080	    l = (l + 7) & ~ 7;
19081	  else
19082	    l += l & 1;
19083
19084	  if (l < arch.sym_size)
19085	    {
19086	      error (ngettext ("%s: %ld byte remains in the symbol table, "
19087			       "but without corresponding entries in "
19088			       "the index table\n",
19089			       "%s: %ld bytes remain in the symbol table, "
19090			       "but without corresponding entries in "
19091			       "the index table\n",
19092			       arch.sym_size - l),
19093		     filedata->file_name, arch.sym_size - l);
19094	      ret = FALSE;
19095	    }
19096
19097	  if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
19098	    {
19099	      error (_("%s: failed to seek back to start of object files in the archive\n"),
19100		     filedata->file_name);
19101	      ret = FALSE;
19102	      goto out;
19103	    }
19104	}
19105
19106      if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
19107	  && !do_segments && !do_header && !do_dump && !do_version
19108	  && !do_histogram && !do_debugging && !do_arch && !do_notes
19109	  && !do_section_groups && !do_dyn_syms)
19110	{
19111	  ret = TRUE; /* Archive index only.  */
19112	  goto out;
19113	}
19114    }
19115
19116  while (1)
19117    {
19118      char * name;
19119      size_t namelen;
19120      char * qualified_name;
19121
19122      /* Read the next archive header.  */
19123      if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
19124        {
19125          error (_("%s: failed to seek to next archive header\n"), filedata->file_name);
19126          return FALSE;
19127        }
19128      got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
19129      if (got != sizeof arch.arhdr)
19130        {
19131          if (got == 0)
19132	    break;
19133          error (_("%s: failed to read archive header\n"), filedata->file_name);
19134          ret = FALSE;
19135          break;
19136        }
19137      if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
19138        {
19139          error (_("%s: did not find a valid archive header\n"), arch.file_name);
19140          ret = FALSE;
19141          break;
19142        }
19143
19144      arch.next_arhdr_offset += sizeof arch.arhdr;
19145
19146      archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
19147      if (archive_file_size & 01)
19148        ++archive_file_size;
19149
19150      name = get_archive_member_name (&arch, &nested_arch);
19151      if (name == NULL)
19152	{
19153	  error (_("%s: bad archive file name\n"), filedata->file_name);
19154	  ret = FALSE;
19155	  break;
19156	}
19157      namelen = strlen (name);
19158
19159      qualified_name = make_qualified_name (&arch, &nested_arch, name);
19160      if (qualified_name == NULL)
19161	{
19162	  error (_("%s: bad archive file name\n"), filedata->file_name);
19163	  ret = FALSE;
19164	  break;
19165	}
19166
19167      if (is_thin_archive && arch.nested_member_origin == 0)
19168        {
19169          /* This is a proxy for an external member of a thin archive.  */
19170          Filedata * member_filedata;
19171          char * member_file_name = adjust_relative_path
19172	    (filedata->file_name, name, namelen);
19173
19174          if (member_file_name == NULL)
19175            {
19176              ret = FALSE;
19177              break;
19178            }
19179
19180          member_filedata = open_file (member_file_name);
19181          if (member_filedata == NULL)
19182            {
19183              error (_("Input file '%s' is not readable.\n"), member_file_name);
19184              free (member_file_name);
19185              ret = FALSE;
19186              break;
19187            }
19188
19189          archive_file_offset = arch.nested_member_origin;
19190	  member_filedata->file_name = qualified_name;
19191
19192          if (! process_object (member_filedata))
19193	    ret = FALSE;
19194
19195          close_file (member_filedata);
19196          free (member_file_name);
19197        }
19198      else if (is_thin_archive)
19199        {
19200          Filedata thin_filedata;
19201
19202          memset (&thin_filedata, 0, sizeof (thin_filedata));
19203
19204	  /* PR 15140: Allow for corrupt thin archives.  */
19205	  if (nested_arch.file == NULL)
19206	    {
19207	      error (_("%s: contains corrupt thin archive: %s\n"),
19208		     filedata->file_name, name);
19209	      ret = FALSE;
19210	      break;
19211	    }
19212
19213          /* This is a proxy for a member of a nested archive.  */
19214          archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
19215
19216          /* The nested archive file will have been opened and setup by
19217             get_archive_member_name.  */
19218          if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
19219            {
19220              error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
19221              ret = FALSE;
19222              break;
19223            }
19224
19225	  thin_filedata.handle = nested_arch.file;
19226	  thin_filedata.file_name = qualified_name;
19227
19228          if (! process_object (& thin_filedata))
19229	    ret = FALSE;
19230        }
19231      else
19232        {
19233          archive_file_offset = arch.next_arhdr_offset;
19234          arch.next_arhdr_offset += archive_file_size;
19235
19236	  filedata->file_name = qualified_name;
19237          if (! process_object (filedata))
19238	    ret = FALSE;
19239        }
19240
19241      if (filedata->dump_sects != NULL)
19242	{
19243	  free (filedata->dump_sects);
19244	  filedata->dump_sects = NULL;
19245	  filedata->num_dump_sects = 0;
19246	}
19247
19248      free (qualified_name);
19249    }
19250
19251 out:
19252  if (nested_arch.file != NULL)
19253    fclose (nested_arch.file);
19254  release_archive (&nested_arch);
19255  release_archive (&arch);
19256
19257  return ret;
19258}
19259
19260static bfd_boolean
19261process_file (char * file_name)
19262{
19263  Filedata * filedata = NULL;
19264  struct stat statbuf;
19265  char armag[SARMAG];
19266  bfd_boolean ret = TRUE;
19267
19268  if (stat (file_name, &statbuf) < 0)
19269    {
19270      if (errno == ENOENT)
19271	error (_("'%s': No such file\n"), file_name);
19272      else
19273	error (_("Could not locate '%s'.  System error message: %s\n"),
19274	       file_name, strerror (errno));
19275      return FALSE;
19276    }
19277
19278  if (!do_special_files && ! S_ISREG (statbuf.st_mode))
19279    {
19280      error (_("'%s' is not an ordinary file\n"), file_name);
19281      return FALSE;
19282    }
19283
19284  filedata = calloc (1, sizeof * filedata);
19285  if (filedata == NULL)
19286    {
19287      error (_("Out of memory allocating file data structure\n"));
19288      return FALSE;
19289    }
19290
19291  filedata->file_name = file_name;
19292  filedata->handle = fopen (file_name, "rb");
19293  if (filedata->handle == NULL)
19294    {
19295      error (_("Input file '%s' is not readable.\n"), file_name);
19296      free (filedata);
19297      return FALSE;
19298    }
19299
19300  if (fread (armag, SARMAG, 1, filedata->handle) != 1)
19301    {
19302      error (_("%s: Failed to read file's magic number\n"), file_name);
19303      fclose (filedata->handle);
19304      free (filedata);
19305      return FALSE;
19306    }
19307
19308  filedata->file_size = (bfd_size_type) statbuf.st_size;
19309
19310  if (memcmp (armag, ARMAG, SARMAG) == 0)
19311    {
19312      if (! process_archive (filedata, FALSE))
19313	ret = FALSE;
19314    }
19315  else if (memcmp (armag, ARMAGT, SARMAG) == 0)
19316    {
19317      if ( ! process_archive (filedata, TRUE))
19318	ret = FALSE;
19319    }
19320  else
19321    {
19322      if (do_archive_index)
19323	error (_("File %s is not an archive so its index cannot be displayed.\n"),
19324	       file_name);
19325
19326      rewind (filedata->handle);
19327      archive_file_size = archive_file_offset = 0;
19328
19329      if (! process_object (filedata))
19330	ret = FALSE;
19331    }
19332
19333  fclose (filedata->handle);
19334  free (filedata);
19335
19336  return ret;
19337}
19338
19339#ifdef SUPPORT_DISASSEMBLY
19340/* Needed by the i386 disassembler.  For extra credit, someone could
19341   fix this so that we insert symbolic addresses here, esp for GOT/PLT
19342   symbols.  */
19343
19344void
19345print_address (unsigned int addr, FILE * outfile)
19346{
19347  fprintf (outfile,"0x%8.8x", addr);
19348}
19349
19350/* Needed by the i386 disassembler.  */
19351
19352void
19353db_task_printsym (unsigned int addr)
19354{
19355  print_address (addr, stderr);
19356}
19357#endif
19358
19359int
19360main (int argc, char ** argv)
19361{
19362  int err;
19363
19364#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
19365  setlocale (LC_MESSAGES, "");
19366#endif
19367#if defined (HAVE_SETLOCALE)
19368  setlocale (LC_CTYPE, "");
19369#endif
19370  bindtextdomain (PACKAGE, LOCALEDIR);
19371  textdomain (PACKAGE);
19372
19373  expandargv (&argc, &argv);
19374
19375  cmdline.file_name = "<cmdline>";
19376  parse_args (& cmdline, argc, argv);
19377
19378  if (optind < (argc - 1))
19379    show_name = TRUE;
19380  else if (optind >= argc)
19381    {
19382      warn (_("Nothing to do.\n"));
19383      usage (stderr);
19384    }
19385
19386  err = FALSE;
19387  while (optind < argc)
19388    if (! process_file (argv[optind++]))
19389      err = TRUE;
19390
19391  if (cmdline.dump_sects != NULL)
19392    free (cmdline.dump_sects);
19393
19394  return err ? EXIT_FAILURE : EXIT_SUCCESS;
19395}
19396