xref: /netbsd-src/external/gpl3/binutils.old/dist/binutils/readelf.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /* readelf.c -- display contents of an ELF format file
2    Copyright (C) 1998-2022 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 #include <wchar.h>
48 
49 #if defined HAVE_MSGPACK
50 #include <msgpack.h>
51 #endif
52 
53 #if __GNUC__ >= 2
54 /* Define BFD64 here, even if our default architecture is 32 bit ELF
55    as this will allow us to read in and parse 64bit and 32bit ELF files.
56    Only do this if we believe that the compiler can support a 64 bit
57    data type.  For now we only rely on GCC being able to do this.  */
58 #define BFD64
59 #endif
60 
61 #include "bfd.h"
62 #include "bucomm.h"
63 #include "elfcomm.h"
64 #include "demanguse.h"
65 #include "dwarf.h"
66 #include "ctf-api.h"
67 #include "demangle.h"
68 
69 #include "elf/common.h"
70 #include "elf/external.h"
71 #include "elf/internal.h"
72 
73 
74 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
75    we can obtain the H8 reloc numbers.  We need these for the
76    get_reloc_size() function.  We include h8.h again after defining
77    RELOC_MACROS_GEN_FUNC so that we get the naming function as well.  */
78 
79 #include "elf/h8.h"
80 #undef _ELF_H8_H
81 
82 /* Undo the effects of #including reloc-macros.h.  */
83 
84 #undef START_RELOC_NUMBERS
85 #undef RELOC_NUMBER
86 #undef FAKE_RELOC
87 #undef EMPTY_RELOC
88 #undef END_RELOC_NUMBERS
89 #undef _RELOC_MACROS_H
90 
91 /* The following headers use the elf/reloc-macros.h file to
92    automatically generate relocation recognition functions
93    such as elf_mips_reloc_type()  */
94 
95 #define RELOC_MACROS_GEN_FUNC
96 
97 #include "elf/aarch64.h"
98 #include "elf/alpha.h"
99 #include "elf/amdgpu.h"
100 #include "elf/arc.h"
101 #include "elf/arm.h"
102 #include "elf/avr.h"
103 #include "elf/bfin.h"
104 #include "elf/cr16.h"
105 #include "elf/cris.h"
106 #include "elf/crx.h"
107 #include "elf/csky.h"
108 #include "elf/d10v.h"
109 #include "elf/d30v.h"
110 #include "elf/dlx.h"
111 #include "elf/bpf.h"
112 #include "elf/epiphany.h"
113 #include "elf/fr30.h"
114 #include "elf/frv.h"
115 #include "elf/ft32.h"
116 #include "elf/h8.h"
117 #include "elf/hppa.h"
118 #include "elf/i386.h"
119 #include "elf/i370.h"
120 #include "elf/i860.h"
121 #include "elf/i960.h"
122 #include "elf/ia64.h"
123 #include "elf/ip2k.h"
124 #include "elf/lm32.h"
125 #include "elf/iq2000.h"
126 #include "elf/m32c.h"
127 #include "elf/m32r.h"
128 #include "elf/m68k.h"
129 #include "elf/m68hc11.h"
130 #include "elf/s12z.h"
131 #include "elf/mcore.h"
132 #include "elf/mep.h"
133 #include "elf/metag.h"
134 #include "elf/microblaze.h"
135 #include "elf/mips.h"
136 #include "elf/mmix.h"
137 #include "elf/mn10200.h"
138 #include "elf/mn10300.h"
139 #include "elf/moxie.h"
140 #include "elf/mt.h"
141 #include "elf/msp430.h"
142 #include "elf/nds32.h"
143 #include "elf/nfp.h"
144 #include "elf/nios2.h"
145 #include "elf/or1k.h"
146 #include "elf/pj.h"
147 #include "elf/ppc.h"
148 #include "elf/ppc64.h"
149 #include "elf/pru.h"
150 #include "elf/riscv.h"
151 #include "elf/rl78.h"
152 #include "elf/rx.h"
153 #include "elf/s390.h"
154 #include "elf/score.h"
155 #include "elf/sh.h"
156 #include "elf/sparc.h"
157 #include "elf/spu.h"
158 #include "elf/tic6x.h"
159 #include "elf/tilegx.h"
160 #include "elf/tilepro.h"
161 #include "elf/v850.h"
162 #include "elf/vax.h"
163 #include "elf/visium.h"
164 #include "elf/wasm32.h"
165 #include "elf/x86-64.h"
166 #include "elf/xgate.h"
167 #include "elf/xstormy16.h"
168 #include "elf/xtensa.h"
169 #include "elf/z80.h"
170 #include "elf/loongarch.h"
171 
172 #include "getopt.h"
173 #include "libiberty.h"
174 #include "safe-ctype.h"
175 #include "filenames.h"
176 
177 #ifndef offsetof
178 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
179 #endif
180 
181 typedef struct elf_section_list
182 {
183   Elf_Internal_Shdr *        hdr;
184   struct elf_section_list *  next;
185 } elf_section_list;
186 
187 /* Flag bits indicating particular types of dump.  */
188 #define HEX_DUMP	(1 << 0)	/* The -x command line switch.  */
189 #define DISASS_DUMP	(1 << 1)	/* The -i command line switch.  */
190 #define DEBUG_DUMP	(1 << 2)	/* The -w command line switch.  */
191 #define STRING_DUMP     (1 << 3)	/* The -p command line switch.  */
192 #define RELOC_DUMP      (1 << 4)	/* The -R command line switch.  */
193 #define CTF_DUMP	(1 << 5)	/* The --ctf command line switch.  */
194 
195 typedef unsigned char dump_type;
196 
197 /* A linked list of the section names for which dumps were requested.  */
198 struct dump_list_entry
199 {
200   char *                    name;
201   dump_type                 type;
202   struct dump_list_entry *  next;
203 };
204 
205 /* A dynamic array of flags indicating for which sections a dump
206    has been requested via command line switches.  */
207 struct dump_data
208 {
209   dump_type *          dump_sects;
210   unsigned int         num_dump_sects;
211 };
212 
213 static struct dump_data cmdline;
214 
215 static struct dump_list_entry * dump_sects_byname;
216 
217 char * program_name = "readelf";
218 
219 static bool show_name = false;
220 static bool do_dynamic = false;
221 static bool do_syms = false;
222 static bool do_dyn_syms = false;
223 static bool do_lto_syms = false;
224 static bool do_reloc = false;
225 static bool do_sections = false;
226 static bool do_section_groups = false;
227 static bool do_section_details = false;
228 static bool do_segments = false;
229 static bool do_unwind = false;
230 static bool do_using_dynamic = false;
231 static bool do_header = false;
232 static bool do_dump = false;
233 static bool do_version = false;
234 static bool do_histogram = false;
235 static bool do_debugging = false;
236 static bool do_ctf = false;
237 static bool do_arch = false;
238 static bool do_notes = false;
239 static bool do_archive_index = false;
240 static bool check_all = false;
241 static bool is_32bit_elf = false;
242 static bool decompress_dumps = false;
243 static bool do_not_show_symbol_truncation = false;
244 static bool do_demangle = false;	/* Pretty print C++ symbol names.  */
245 static bool process_links = false;
246 static bool dump_any_debugging = false;
247 static bool do_special_files = false;
248 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
249 static int sym_base = 0;
250 
251 static char *dump_ctf_parent_name;
252 static char *dump_ctf_symtab_name;
253 static char *dump_ctf_strtab_name;
254 
255 struct group_list
256 {
257   struct group_list *  next;
258   unsigned int         section_index;
259 };
260 
261 struct group
262 {
263   struct group_list *  root;
264   unsigned int         group_index;
265 };
266 
267 typedef struct filedata
268 {
269   const char *         file_name;
270   bool                 is_separate;
271   FILE *               handle;
272   bfd_size_type        file_size;
273   Elf_Internal_Ehdr    file_header;
274   unsigned long        archive_file_offset;
275   unsigned long        archive_file_size;
276   /* Everything below this point is cleared out by free_filedata.  */
277   Elf_Internal_Shdr *  section_headers;
278   Elf_Internal_Phdr *  program_headers;
279   char *               string_table;
280   unsigned long        string_table_length;
281   unsigned long        dynamic_addr;
282   bfd_size_type        dynamic_size;
283   size_t               dynamic_nent;
284   Elf_Internal_Dyn *   dynamic_section;
285   Elf_Internal_Shdr *  dynamic_strtab_section;
286   char *               dynamic_strings;
287   unsigned long        dynamic_strings_length;
288   Elf_Internal_Shdr *  dynamic_symtab_section;
289   unsigned long        num_dynamic_syms;
290   Elf_Internal_Sym *   dynamic_symbols;
291   bfd_vma              version_info[16];
292   unsigned int         dynamic_syminfo_nent;
293   Elf_Internal_Syminfo * dynamic_syminfo;
294   unsigned long        dynamic_syminfo_offset;
295   bfd_size_type        nbuckets;
296   bfd_size_type        nchains;
297   bfd_vma *            buckets;
298   bfd_vma *            chains;
299   bfd_size_type        ngnubuckets;
300   bfd_size_type        ngnuchains;
301   bfd_vma *            gnubuckets;
302   bfd_vma *            gnuchains;
303   bfd_vma *            mipsxlat;
304   bfd_vma              gnusymidx;
305   char *               program_interpreter;
306   bfd_vma              dynamic_info[DT_ENCODING];
307   bfd_vma              dynamic_info_DT_GNU_HASH;
308   bfd_vma              dynamic_info_DT_MIPS_XHASH;
309   elf_section_list *   symtab_shndx_list;
310   size_t               group_count;
311   struct group *       section_groups;
312   struct group **      section_headers_groups;
313   /* A dynamic array of flags indicating for which sections a dump of
314      some kind has been requested.  It is reset on a per-object file
315      basis and then initialised from the cmdline_dump_sects array,
316      the results of interpreting the -w switch, and the
317      dump_sects_byname list.  */
318   struct dump_data     dump;
319 } Filedata;
320 
321 /* How to print a vma value.  */
322 typedef enum print_mode
323 {
324   HEX,
325   HEX_5,
326   DEC,
327   DEC_5,
328   UNSIGNED,
329   UNSIGNED_5,
330   PREFIX_HEX,
331   PREFIX_HEX_5,
332   FULL_HEX,
333   LONG_HEX,
334   OCTAL,
335   OCTAL_5
336 }
337 print_mode;
338 
339 typedef enum unicode_display_type
340 {
341   unicode_default = 0,
342   unicode_locale,
343   unicode_escape,
344   unicode_hex,
345   unicode_highlight,
346   unicode_invalid
347 } unicode_display_type;
348 
349 static unicode_display_type unicode_display = unicode_default;
350 
351 typedef enum
352 {
353   reltype_unknown,
354   reltype_rel,
355   reltype_rela,
356   reltype_relr
357 } relocation_type;
358 
359 /* Versioned symbol info.  */
360 enum versioned_symbol_info
361 {
362   symbol_undefined,
363   symbol_hidden,
364   symbol_public
365 };
366 
367 static const char * get_symbol_version_string
368   (Filedata *, bool, const char *, unsigned long, unsigned,
369    Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
370 
371 #define UNKNOWN -1
372 
373 static inline const char *
374 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
375 {
376   return filedata->string_table + hdr->sh_name;
377 }
378 
379 static inline bool
380 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
381 {
382   return (hdr != NULL
383 	  && filedata->string_table != NULL
384 	  && hdr->sh_name < filedata->string_table_length);
385 }
386 
387 static inline const char *
388 section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
389 {
390   if (hdr == NULL)
391     return _("<none>");
392   if (filedata->string_table == NULL)
393     return _("<no-strings>");
394   if (hdr->sh_name >= filedata->string_table_length)
395     return _("<corrupt>");
396   return section_name (filedata, hdr);
397 }
398 
399 #define DT_VERSIONTAGIDX(tag)	(DT_VERNEEDNUM - (tag))	/* Reverse order!  */
400 
401 static inline bool
402 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
403 {
404   return strtab != NULL && offset < strtab_size;
405 }
406 
407 static inline bool
408 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
409 {
410   return valid_symbol_name (filedata->dynamic_strings,
411 			    filedata->dynamic_strings_length, offset);
412 }
413 
414 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
415    already been called and verified that the string exists.  */
416 static inline const char *
417 get_dynamic_name (const Filedata *filedata, size_t offset)
418 {
419   return filedata->dynamic_strings + offset;
420 }
421 
422 #define REMOVE_ARCH_BITS(ADDR)			\
423   do						\
424     {						\
425       if (filedata->file_header.e_machine == EM_ARM)	\
426 	(ADDR) &= ~1;				\
427     }						\
428   while (0)
429 
430 /* Get the correct GNU hash section name.  */
431 #define GNU_HASH_SECTION_NAME(filedata)		\
432   filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
433 
434 /* Print a BFD_VMA to an internal buffer, for use in error messages.
435    BFD_FMA_FMT can't be used in translated strings.  */
436 
437 static const char *
438 bfd_vmatoa (char *fmtch, bfd_vma value)
439 {
440   /* bfd_vmatoa is used more then once in a printf call for output.
441      Cycle through an array of buffers.  */
442   static int buf_pos = 0;
443   static struct bfd_vmatoa_buf
444   {
445     char place[64];
446   } buf[4];
447   char *ret;
448   char fmt[32];
449 
450   ret = buf[buf_pos++].place;
451   buf_pos %= ARRAY_SIZE (buf);
452 
453   sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
454   snprintf (ret, sizeof (buf[0].place), fmt, value);
455   return ret;
456 }
457 
458 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
459    OFFSET + the offset of the current archive member, if we are examining an
460    archive.  Put the retrieved data into VAR, if it is not NULL.  Otherwise
461    allocate a buffer using malloc and fill that.  In either case return the
462    pointer to the start of the retrieved data or NULL if something went wrong.
463    If something does go wrong and REASON is not NULL then emit an error
464    message using REASON as part of the context.  */
465 
466 static void *
467 get_data (void *         var,
468 	  Filedata *     filedata,
469 	  unsigned long  offset,
470 	  bfd_size_type  size,
471 	  bfd_size_type  nmemb,
472 	  const char *   reason)
473 {
474   void * mvar;
475   bfd_size_type amt = size * nmemb;
476 
477   if (size == 0 || nmemb == 0)
478     return NULL;
479 
480   /* If the size_t type is smaller than the bfd_size_type, eg because
481      you are building a 32-bit tool on a 64-bit host, then make sure
482      that when the sizes are cast to (size_t) no information is lost.  */
483   if ((size_t) size != size
484       || (size_t) nmemb != nmemb
485       || (size_t) amt != amt)
486     {
487       if (reason)
488 	error (_("Size truncation prevents reading %s"
489 		 " elements of size %s for %s\n"),
490 	       bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
491       return NULL;
492     }
493 
494   /* Check for size overflow.  */
495   if (amt / size != nmemb || (size_t) amt + 1 == 0)
496     {
497       if (reason)
498 	error (_("Size overflow prevents reading %s"
499 		 " elements of size %s for %s\n"),
500 	       bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
501       return NULL;
502     }
503 
504   /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
505      attempting to allocate memory when the read is bound to fail.  */
506   if (filedata->archive_file_offset > filedata->file_size
507       || offset > filedata->file_size - filedata->archive_file_offset
508       || amt > filedata->file_size - filedata->archive_file_offset - offset)
509     {
510       if (reason)
511 	error (_("Reading %s bytes extends past end of file for %s\n"),
512 	       bfd_vmatoa ("u", amt), reason);
513       return NULL;
514     }
515 
516   if (fseek (filedata->handle, filedata->archive_file_offset + offset,
517 	     SEEK_SET))
518     {
519       if (reason)
520 	error (_("Unable to seek to 0x%lx for %s\n"),
521 	       filedata->archive_file_offset + offset, reason);
522       return NULL;
523     }
524 
525   mvar = var;
526   if (mvar == NULL)
527     {
528       /* + 1 so that we can '\0' terminate invalid string table sections.  */
529       mvar = malloc ((size_t) amt + 1);
530 
531       if (mvar == NULL)
532 	{
533 	  if (reason)
534 	    error (_("Out of memory allocating %s bytes for %s\n"),
535 		   bfd_vmatoa ("u", amt), reason);
536 	  return NULL;
537 	}
538 
539       ((char *) mvar)[amt] = '\0';
540     }
541 
542   if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
543     {
544       if (reason)
545 	error (_("Unable to read in %s bytes of %s\n"),
546 	       bfd_vmatoa ("u", amt), reason);
547       if (mvar != var)
548 	free (mvar);
549       return NULL;
550     }
551 
552   return mvar;
553 }
554 
555 /* Print a VMA value in the MODE specified.
556    Returns the number of characters displayed.  */
557 
558 static unsigned int
559 print_vma (bfd_vma vma, print_mode mode)
560 {
561   unsigned int nc = 0;
562 
563   switch (mode)
564     {
565     case FULL_HEX:
566       nc = printf ("0x");
567       /* Fall through.  */
568     case LONG_HEX:
569 #ifdef BFD64
570       if (is_32bit_elf)
571 	return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
572 #endif
573       printf_vma (vma);
574       return nc + 16;
575 
576     case DEC_5:
577       if (vma <= 99999)
578 	return printf ("%5" BFD_VMA_FMT "d", vma);
579       /* Fall through.  */
580     case PREFIX_HEX:
581       nc = printf ("0x");
582       /* Fall through.  */
583     case HEX:
584       return nc + printf ("%" BFD_VMA_FMT "x", vma);
585 
586     case PREFIX_HEX_5:
587       nc = printf ("0x");
588       /* Fall through.  */
589     case HEX_5:
590       return nc + printf ("%05" BFD_VMA_FMT "x", vma);
591 
592     case DEC:
593       return printf ("%" BFD_VMA_FMT "d", vma);
594 
595     case UNSIGNED:
596       return printf ("%" BFD_VMA_FMT "u", vma);
597 
598     case UNSIGNED_5:
599       return printf ("%5" BFD_VMA_FMT "u", vma);
600 
601     case OCTAL:
602       return printf ("%" BFD_VMA_FMT "o", vma);
603 
604     case OCTAL_5:
605       return printf ("%5" BFD_VMA_FMT "o", vma);
606 
607     default:
608       /* FIXME: Report unrecognised mode ?  */
609       return 0;
610     }
611 }
612 
613 
614 /* Display a symbol on stdout.  Handles the display of control characters and
615    multibye characters (assuming the host environment supports them).
616 
617    Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
618 
619    If truncation will happen and do_not_show_symbol_truncation is FALSE then display
620    abs(WIDTH) - 5 characters followed by "[...]".
621 
622    If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
623    padding as necessary.
624 
625    Returns the number of emitted characters.  */
626 
627 static unsigned int
628 print_symbol (signed int width, const char * symbol)
629 {
630   bool extra_padding = false;
631   bool do_dots = false;
632   signed int num_printed = 0;
633 #ifdef HAVE_MBSTATE_T
634   mbstate_t state;
635 #endif
636   unsigned int width_remaining;
637   const void * alloced_symbol = NULL;
638 
639   if (width < 0)
640     {
641       /* Keep the width positive.  This helps the code below.  */
642       width = - width;
643       extra_padding = true;
644     }
645   else if (width == 0)
646     return 0;
647 
648   if (do_wide)
649     /* Set the remaining width to a very large value.
650        This simplifies the code below.  */
651     width_remaining = INT_MAX;
652   else
653     {
654       width_remaining = width;
655       if (! do_not_show_symbol_truncation
656 	  && (int) strlen (symbol) > width)
657 	{
658 	  width_remaining -= 5;
659 	  if ((int) width_remaining < 0)
660 	    width_remaining = 0;
661 	  do_dots = true;
662 	}
663     }
664 
665 #ifdef HAVE_MBSTATE_T
666   /* Initialise the multibyte conversion state.  */
667   memset (& state, 0, sizeof (state));
668 #endif
669 
670   if (do_demangle && *symbol)
671     {
672       const char * res = cplus_demangle (symbol, demangle_flags);
673 
674       if (res != NULL)
675 	alloced_symbol = symbol = res;
676     }
677 
678   while (width_remaining)
679     {
680       size_t  n;
681       const char c = *symbol++;
682 
683       if (c == 0)
684 	break;
685 
686       if (ISPRINT (c))
687 	{
688 	  putchar (c);
689 	  width_remaining --;
690 	  num_printed ++;
691 	}
692       else if (ISCNTRL (c))
693 	{
694 	  /* Do not print control characters directly as they can affect terminal
695 	     settings.  Such characters usually appear in the names generated
696 	     by the assembler for local labels.  */
697 
698 	  if (width_remaining < 2)
699 	    break;
700 
701 	  printf ("^%c", c + 0x40);
702 	  width_remaining -= 2;
703 	  num_printed += 2;
704 	}
705       else if (c == 0x7f)
706 	{
707 	  if (width_remaining < 5)
708 	    break;
709 	  printf ("<DEL>");
710 	  width_remaining -= 5;
711 	  num_printed += 5;
712 	}
713       else if (unicode_display != unicode_locale
714 	       && unicode_display != unicode_default)
715 	{
716 	  /* Display unicode characters as something else.  */
717 	  unsigned char bytes[4];
718 	  bool          is_utf8;
719 	  unsigned int  nbytes;
720 
721 	  bytes[0] = c;
722 
723 	  if (bytes[0] < 0xc0)
724 	    {
725 	      nbytes = 1;
726 	      is_utf8 = false;
727 	    }
728 	  else
729 	    {
730 	      bytes[1] = *symbol++;
731 
732 	      if ((bytes[1] & 0xc0) != 0x80)
733 		{
734 		  is_utf8 = false;
735 		  /* Do not consume this character.  It may only
736 		     be the first byte in the sequence that was
737 		     corrupt.  */
738 		  --symbol;
739 		  nbytes = 1;
740 		}
741 	      else if ((bytes[0] & 0x20) == 0)
742 		{
743 		  is_utf8 = true;
744 		  nbytes = 2;
745 		}
746 	      else
747 		{
748 		  bytes[2] = *symbol++;
749 
750 		  if ((bytes[2] & 0xc0) != 0x80)
751 		    {
752 		      is_utf8 = false;
753 		      symbol -= 2;
754 		      nbytes = 1;
755 		    }
756 		  else if ((bytes[0] & 0x10) == 0)
757 		    {
758 		      is_utf8 = true;
759 		      nbytes = 3;
760 		    }
761 		  else
762 		    {
763 		      bytes[3] = *symbol++;
764 
765 		      nbytes = 4;
766 
767 		      if ((bytes[3] & 0xc0) != 0x80)
768 			{
769 			  is_utf8 = false;
770 			  symbol -= 3;
771 			  nbytes = 1;
772 			}
773 		      else
774 			is_utf8 = true;
775 		    }
776 		}
777 	    }
778 
779 	  if (unicode_display == unicode_invalid)
780 	    is_utf8 = false;
781 
782 	  if (unicode_display == unicode_hex || ! is_utf8)
783 	    {
784 	      unsigned int i;
785 
786 	      if (width_remaining < (nbytes * 2) + 2)
787 		break;
788 
789 	      putchar (is_utf8 ? '<' : '{');
790 	      printf ("0x");
791 	      for (i = 0; i < nbytes; i++)
792 		printf ("%02x", bytes[i]);
793 	      putchar (is_utf8 ? '>' : '}');
794 	    }
795 	  else
796 	    {
797 	      if (unicode_display == unicode_highlight && isatty (1))
798 		printf ("\x1B[31;47m"); /* Red.  */
799 
800 	      switch (nbytes)
801 		{
802 		case 2:
803 		  if (width_remaining < 6)
804 		    break;
805 		  printf ("\\u%02x%02x",
806 			  (bytes[0] & 0x1c) >> 2,
807 			  ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
808 		  break;
809 		case 3:
810 		  if (width_remaining < 6)
811 		    break;
812 		  printf ("\\u%02x%02x",
813 			  ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
814 			  ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
815 		  break;
816 		case 4:
817 		  if (width_remaining < 8)
818 		    break;
819 		  printf ("\\u%02x%02x%02x",
820 			  ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
821 			  ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
822 			  ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
823 
824 		  break;
825 		default:
826 		  /* URG.  */
827 		  break;
828 		}
829 
830 	      if (unicode_display == unicode_highlight && isatty (1))
831 		printf ("\033[0m"); /* Default colour.  */
832 	    }
833 
834 	  if (bytes[nbytes - 1] == 0)
835 	    break;
836 	}
837       else
838 	{
839 #ifdef HAVE_MBSTATE_T
840 	  wchar_t w;
841 #endif
842 	  /* Let printf do the hard work of displaying multibyte characters.  */
843 	  printf ("%.1s", symbol - 1);
844 	  width_remaining --;
845 	  num_printed ++;
846 
847 #ifdef HAVE_MBSTATE_T
848 	  /* Try to find out how many bytes made up the character that was
849 	     just printed.  Advance the symbol pointer past the bytes that
850 	     were displayed.  */
851 	  n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
852 #else
853 	  n = 1;
854 #endif
855 	  if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
856 	    symbol += (n - 1);
857 	}
858     }
859 
860   if (do_dots)
861     num_printed += printf ("[...]");
862 
863   if (extra_padding && num_printed < width)
864     {
865       /* Fill in the remaining spaces.  */
866       printf ("%-*s", width - num_printed, " ");
867       num_printed = width;
868     }
869 
870   free ((void *) alloced_symbol);
871   return num_printed;
872 }
873 
874 /* Returns a pointer to a static buffer containing a printable version of
875    the given section's name.  Like print_symbol, except that it does not try
876    to print multibyte characters, it just interprets them as hex values.  */
877 
878 static const char *
879 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
880 {
881 #define MAX_PRINT_SEC_NAME_LEN 256
882   static char  sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
883   const char * name = section_name_print (filedata, sec);
884   char *       buf = sec_name_buf;
885   char         c;
886   unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
887 
888   while ((c = * name ++) != 0)
889     {
890       if (ISCNTRL (c))
891 	{
892 	  if (remaining < 2)
893 	    break;
894 
895 	  * buf ++ = '^';
896 	  * buf ++ = c + 0x40;
897 	  remaining -= 2;
898 	}
899       else if (ISPRINT (c))
900 	{
901 	  * buf ++ = c;
902 	  remaining -= 1;
903 	}
904       else
905 	{
906 	  static char hex[17] = "0123456789ABCDEF";
907 
908 	  if (remaining < 4)
909 	    break;
910 	  * buf ++ = '<';
911 	  * buf ++ = hex[(c & 0xf0) >> 4];
912 	  * buf ++ = hex[c & 0x0f];
913 	  * buf ++ = '>';
914 	  remaining -= 4;
915 	}
916 
917       if (remaining == 0)
918 	break;
919     }
920 
921   * buf = 0;
922   return sec_name_buf;
923 }
924 
925 static const char *
926 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
927 {
928   if (ndx >= filedata->file_header.e_shnum)
929     return _("<corrupt>");
930 
931   return printable_section_name (filedata, filedata->section_headers + ndx);
932 }
933 
934 /* Return a pointer to section NAME, or NULL if no such section exists.  */
935 
936 static Elf_Internal_Shdr *
937 find_section (Filedata * filedata, const char * name)
938 {
939   unsigned int i;
940 
941   if (filedata->section_headers == NULL)
942     return NULL;
943 
944   for (i = 0; i < filedata->file_header.e_shnum; i++)
945     if (section_name_valid (filedata, filedata->section_headers + i)
946 	&& streq (section_name (filedata, filedata->section_headers + i),
947 		  name))
948       return filedata->section_headers + i;
949 
950   return NULL;
951 }
952 
953 /* Return a pointer to a section containing ADDR, or NULL if no such
954    section exists.  */
955 
956 static Elf_Internal_Shdr *
957 find_section_by_address (Filedata * filedata, bfd_vma addr)
958 {
959   unsigned int i;
960 
961   if (filedata->section_headers == NULL)
962     return NULL;
963 
964   for (i = 0; i < filedata->file_header.e_shnum; i++)
965     {
966       Elf_Internal_Shdr *sec = filedata->section_headers + i;
967 
968       if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
969 	return sec;
970     }
971 
972   return NULL;
973 }
974 
975 static Elf_Internal_Shdr *
976 find_section_by_type (Filedata * filedata, unsigned int type)
977 {
978   unsigned int i;
979 
980   if (filedata->section_headers == NULL)
981     return NULL;
982 
983   for (i = 0; i < filedata->file_header.e_shnum; i++)
984     {
985       Elf_Internal_Shdr *sec = filedata->section_headers + i;
986 
987       if (sec->sh_type == type)
988 	return sec;
989     }
990 
991   return NULL;
992 }
993 
994 /* Return a pointer to section NAME, or NULL if no such section exists,
995    restricted to the list of sections given in SET.  */
996 
997 static Elf_Internal_Shdr *
998 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
999 {
1000   unsigned int i;
1001 
1002   if (filedata->section_headers == NULL)
1003     return NULL;
1004 
1005   if (set != NULL)
1006     {
1007       while ((i = *set++) > 0)
1008 	{
1009 	  /* See PR 21156 for a reproducer.  */
1010 	  if (i >= filedata->file_header.e_shnum)
1011 	    continue; /* FIXME: Should we issue an error message ?  */
1012 
1013 	  if (section_name_valid (filedata, filedata->section_headers + i)
1014 	      && streq (section_name (filedata, filedata->section_headers + i),
1015 			name))
1016 	    return filedata->section_headers + i;
1017 	}
1018     }
1019 
1020   return find_section (filedata, name);
1021 }
1022 
1023 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
1024    This OS has so many departures from the ELF standard that we test it at
1025    many places.  */
1026 
1027 static inline bool
1028 is_ia64_vms (Filedata * filedata)
1029 {
1030   return filedata->file_header.e_machine == EM_IA_64
1031     && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
1032 }
1033 
1034 /* Guess the relocation size commonly used by the specific machines.  */
1035 
1036 static bool
1037 guess_is_rela (unsigned int e_machine)
1038 {
1039   switch (e_machine)
1040     {
1041       /* Targets that use REL relocations.  */
1042     case EM_386:
1043     case EM_IAMCU:
1044     case EM_960:
1045     case EM_ARM:
1046     case EM_D10V:
1047     case EM_CYGNUS_D10V:
1048     case EM_DLX:
1049     case EM_MIPS:
1050     case EM_MIPS_RS3_LE:
1051     case EM_CYGNUS_M32R:
1052     case EM_SCORE:
1053     case EM_XGATE:
1054     case EM_NFP:
1055     case EM_BPF:
1056       return false;
1057 
1058       /* Targets that use RELA relocations.  */
1059     case EM_68K:
1060     case EM_860:
1061     case EM_AARCH64:
1062     case EM_ADAPTEVA_EPIPHANY:
1063     case EM_ALPHA:
1064     case EM_ALTERA_NIOS2:
1065     case EM_ARC:
1066     case EM_ARC_COMPACT:
1067     case EM_ARC_COMPACT2:
1068     case EM_AVR:
1069     case EM_AVR_OLD:
1070     case EM_BLACKFIN:
1071     case EM_CR16:
1072     case EM_CRIS:
1073     case EM_CRX:
1074     case EM_CSKY:
1075     case EM_D30V:
1076     case EM_CYGNUS_D30V:
1077     case EM_FR30:
1078     case EM_FT32:
1079     case EM_CYGNUS_FR30:
1080     case EM_CYGNUS_FRV:
1081     case EM_H8S:
1082     case EM_H8_300:
1083     case EM_H8_300H:
1084     case EM_IA_64:
1085     case EM_IP2K:
1086     case EM_IP2K_OLD:
1087     case EM_IQ2000:
1088     case EM_LATTICEMICO32:
1089     case EM_M32C_OLD:
1090     case EM_M32C:
1091     case EM_M32R:
1092     case EM_MCORE:
1093     case EM_CYGNUS_MEP:
1094     case EM_METAG:
1095     case EM_MMIX:
1096     case EM_MN10200:
1097     case EM_CYGNUS_MN10200:
1098     case EM_MN10300:
1099     case EM_CYGNUS_MN10300:
1100     case EM_MOXIE:
1101     case EM_MSP430:
1102     case EM_MSP430_OLD:
1103     case EM_MT:
1104     case EM_NDS32:
1105     case EM_NIOS32:
1106     case EM_OR1K:
1107     case EM_PPC64:
1108     case EM_PPC:
1109     case EM_TI_PRU:
1110     case EM_RISCV:
1111     case EM_RL78:
1112     case EM_RX:
1113     case EM_S390:
1114     case EM_S390_OLD:
1115     case EM_SH:
1116     case EM_SPARC:
1117     case EM_SPARC32PLUS:
1118     case EM_SPARCV9:
1119     case EM_SPU:
1120     case EM_TI_C6000:
1121     case EM_TILEGX:
1122     case EM_TILEPRO:
1123     case EM_V800:
1124     case EM_V850:
1125     case EM_CYGNUS_V850:
1126     case EM_VAX:
1127     case EM_VISIUM:
1128     case EM_X86_64:
1129     case EM_L1OM:
1130     case EM_K1OM:
1131     case EM_XSTORMY16:
1132     case EM_XTENSA:
1133     case EM_XTENSA_OLD:
1134     case EM_MICROBLAZE:
1135     case EM_MICROBLAZE_OLD:
1136     case EM_WEBASSEMBLY:
1137       return true;
1138 
1139     case EM_68HC05:
1140     case EM_68HC08:
1141     case EM_68HC11:
1142     case EM_68HC16:
1143     case EM_FX66:
1144     case EM_ME16:
1145     case EM_MMA:
1146     case EM_NCPU:
1147     case EM_NDR1:
1148     case EM_PCP:
1149     case EM_ST100:
1150     case EM_ST19:
1151     case EM_ST7:
1152     case EM_ST9PLUS:
1153     case EM_STARCORE:
1154     case EM_SVX:
1155     case EM_TINYJ:
1156     default:
1157       warn (_("Don't know about relocations on this machine architecture\n"));
1158       return false;
1159     }
1160 }
1161 
1162 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1163    Returns TRUE upon success, FALSE otherwise.  If successful then a
1164    pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1165    and the number of relocs loaded is placed in *NRELASP.  It is the caller's
1166    responsibility to free the allocated buffer.  */
1167 
1168 static bool
1169 slurp_rela_relocs (Filedata *            filedata,
1170 		   unsigned long         rel_offset,
1171 		   unsigned long         rel_size,
1172 		   Elf_Internal_Rela **  relasp,
1173 		   unsigned long *       nrelasp)
1174 {
1175   Elf_Internal_Rela * relas;
1176   size_t nrelas;
1177   unsigned int i;
1178 
1179   if (is_32bit_elf)
1180     {
1181       Elf32_External_Rela * erelas;
1182 
1183       erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1184                                                  rel_size, _("32-bit relocation data"));
1185       if (!erelas)
1186 	return false;
1187 
1188       nrelas = rel_size / sizeof (Elf32_External_Rela);
1189 
1190       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1191                                              sizeof (Elf_Internal_Rela));
1192 
1193       if (relas == NULL)
1194 	{
1195 	  free (erelas);
1196 	  error (_("out of memory parsing relocs\n"));
1197 	  return false;
1198 	}
1199 
1200       for (i = 0; i < nrelas; i++)
1201 	{
1202 	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1203 	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
1204 	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1205 	}
1206 
1207       free (erelas);
1208     }
1209   else
1210     {
1211       Elf64_External_Rela * erelas;
1212 
1213       erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1214                                                  rel_size, _("64-bit relocation data"));
1215       if (!erelas)
1216 	return false;
1217 
1218       nrelas = rel_size / sizeof (Elf64_External_Rela);
1219 
1220       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1221                                              sizeof (Elf_Internal_Rela));
1222 
1223       if (relas == NULL)
1224 	{
1225 	  free (erelas);
1226 	  error (_("out of memory parsing relocs\n"));
1227 	  return false;
1228 	}
1229 
1230       for (i = 0; i < nrelas; i++)
1231 	{
1232 	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1233 	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
1234 	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1235 
1236 	  /* The #ifdef BFD64 below is to prevent a compile time
1237 	     warning.  We know that if we do not have a 64 bit data
1238 	     type that we will never execute this code anyway.  */
1239 #ifdef BFD64
1240 	  if (filedata->file_header.e_machine == EM_MIPS
1241 	      && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1242 	    {
1243 	      /* In little-endian objects, r_info isn't really a
1244 		 64-bit little-endian value: it has a 32-bit
1245 		 little-endian symbol index followed by four
1246 		 individual byte fields.  Reorder INFO
1247 		 accordingly.  */
1248 	      bfd_vma inf = relas[i].r_info;
1249 	      inf = (((inf & 0xffffffff) << 32)
1250 		      | ((inf >> 56) & 0xff)
1251 		      | ((inf >> 40) & 0xff00)
1252 		      | ((inf >> 24) & 0xff0000)
1253 		      | ((inf >> 8) & 0xff000000));
1254 	      relas[i].r_info = inf;
1255 	    }
1256 #endif /* BFD64 */
1257 	}
1258 
1259       free (erelas);
1260     }
1261 
1262   *relasp = relas;
1263   *nrelasp = nrelas;
1264   return true;
1265 }
1266 
1267 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1268    Returns TRUE upon success, FALSE otherwise.  If successful then a
1269    pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1270    and the number of relocs loaded is placed in *NRELSP.  It is the caller's
1271    responsibility to free the allocated buffer.  */
1272 
1273 static bool
1274 slurp_rel_relocs (Filedata *            filedata,
1275 		  unsigned long         rel_offset,
1276 		  unsigned long         rel_size,
1277 		  Elf_Internal_Rela **  relsp,
1278 		  unsigned long *       nrelsp)
1279 {
1280   Elf_Internal_Rela * rels;
1281   size_t nrels;
1282   unsigned int i;
1283 
1284   if (is_32bit_elf)
1285     {
1286       Elf32_External_Rel * erels;
1287 
1288       erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1289                                                rel_size, _("32-bit relocation data"));
1290       if (!erels)
1291 	return false;
1292 
1293       nrels = rel_size / sizeof (Elf32_External_Rel);
1294 
1295       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1296 
1297       if (rels == NULL)
1298 	{
1299 	  free (erels);
1300 	  error (_("out of memory parsing relocs\n"));
1301 	  return false;
1302 	}
1303 
1304       for (i = 0; i < nrels; i++)
1305 	{
1306 	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1307 	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
1308 	  rels[i].r_addend = 0;
1309 	}
1310 
1311       free (erels);
1312     }
1313   else
1314     {
1315       Elf64_External_Rel * erels;
1316 
1317       erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1318                                                rel_size, _("64-bit relocation data"));
1319       if (!erels)
1320 	return false;
1321 
1322       nrels = rel_size / sizeof (Elf64_External_Rel);
1323 
1324       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1325 
1326       if (rels == NULL)
1327 	{
1328 	  free (erels);
1329 	  error (_("out of memory parsing relocs\n"));
1330 	  return false;
1331 	}
1332 
1333       for (i = 0; i < nrels; i++)
1334 	{
1335 	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1336 	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
1337 	  rels[i].r_addend = 0;
1338 
1339 	  /* The #ifdef BFD64 below is to prevent a compile time
1340 	     warning.  We know that if we do not have a 64 bit data
1341 	     type that we will never execute this code anyway.  */
1342 #ifdef BFD64
1343 	  if (filedata->file_header.e_machine == EM_MIPS
1344 	      && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1345 	    {
1346 	      /* In little-endian objects, r_info isn't really a
1347 		 64-bit little-endian value: it has a 32-bit
1348 		 little-endian symbol index followed by four
1349 		 individual byte fields.  Reorder INFO
1350 		 accordingly.  */
1351 	      bfd_vma inf = rels[i].r_info;
1352 	      inf = (((inf & 0xffffffff) << 32)
1353 		     | ((inf >> 56) & 0xff)
1354 		     | ((inf >> 40) & 0xff00)
1355 		     | ((inf >> 24) & 0xff0000)
1356 		     | ((inf >> 8) & 0xff000000));
1357 	      rels[i].r_info = inf;
1358 	    }
1359 #endif /* BFD64 */
1360 	}
1361 
1362       free (erels);
1363     }
1364 
1365   *relsp = rels;
1366   *nrelsp = nrels;
1367   return true;
1368 }
1369 
1370 static bool
1371 slurp_relr_relocs (Filedata * filedata,
1372 		   unsigned long relr_offset,
1373 		   unsigned long relr_size,
1374 		   bfd_vma ** relrsp,
1375 		   unsigned long * nrelrsp)
1376 {
1377   void *relrs;
1378   size_t size = 0, nentries, i;
1379   bfd_vma base = 0, addr, entry;
1380 
1381   relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1382 		    _("RELR relocation data"));
1383   if (!relrs)
1384     return false;
1385 
1386   if (is_32bit_elf)
1387     nentries = relr_size / sizeof (Elf32_External_Relr);
1388   else
1389     nentries = relr_size / sizeof (Elf64_External_Relr);
1390   for (i = 0; i < nentries; i++)
1391     {
1392       if (is_32bit_elf)
1393 	entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1394       else
1395 	entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1396       if ((entry & 1) == 0)
1397 	size++;
1398       else
1399 	while ((entry >>= 1) != 0)
1400 	  if ((entry & 1) == 1)
1401 	    size++;
1402     }
1403 
1404   *relrsp = (bfd_vma *) malloc (size * sizeof (bfd_vma));
1405   if (*relrsp == NULL)
1406     {
1407       free (relrs);
1408       error (_("out of memory parsing relocs\n"));
1409       return false;
1410     }
1411 
1412   size = 0;
1413   for (i = 0; i < nentries; i++)
1414     {
1415       const bfd_vma entry_bytes = is_32bit_elf ? 4 : 8;
1416 
1417       if (is_32bit_elf)
1418 	entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1419       else
1420 	entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1421       if ((entry & 1) == 0)
1422 	{
1423 	  (*relrsp)[size++] = entry;
1424 	  base = entry + entry_bytes;
1425 	}
1426       else
1427 	{
1428 	  for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1429 	    if ((entry & 1) != 0)
1430 	      (*relrsp)[size++] = addr;
1431 	  base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1432 	}
1433     }
1434 
1435   *nrelrsp = size;
1436   free (relrs);
1437   return true;
1438 }
1439 
1440 /* Returns the reloc type extracted from the reloc info field.  */
1441 
1442 static unsigned int
1443 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1444 {
1445   if (is_32bit_elf)
1446     return ELF32_R_TYPE (reloc_info);
1447 
1448   switch (filedata->file_header.e_machine)
1449     {
1450     case EM_MIPS:
1451       /* Note: We assume that reloc_info has already been adjusted for us.  */
1452       return ELF64_MIPS_R_TYPE (reloc_info);
1453 
1454     case EM_SPARCV9:
1455       return ELF64_R_TYPE_ID (reloc_info);
1456 
1457     default:
1458       return ELF64_R_TYPE (reloc_info);
1459     }
1460 }
1461 
1462 /* Return the symbol index extracted from the reloc info field.  */
1463 
1464 static bfd_vma
1465 get_reloc_symindex (bfd_vma reloc_info)
1466 {
1467   return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1468 }
1469 
1470 static inline bool
1471 uses_msp430x_relocs (Filedata * filedata)
1472 {
1473   return
1474     filedata->file_header.e_machine == EM_MSP430 /* Paranoia.  */
1475     /* GCC uses osabi == ELFOSBI_STANDALONE.  */
1476     && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1477 	/* TI compiler uses ELFOSABI_NONE.  */
1478 	|| (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1479 }
1480 
1481 /* Display the contents of the relocation data found at the specified
1482    offset.  */
1483 
1484 static bool
1485 dump_relocations (Filedata *          filedata,
1486 		  unsigned long       rel_offset,
1487 		  unsigned long       rel_size,
1488 		  Elf_Internal_Sym *  symtab,
1489 		  unsigned long       nsyms,
1490 		  char *              strtab,
1491 		  unsigned long       strtablen,
1492 		  relocation_type     rel_type,
1493 		  bool                is_dynsym)
1494 {
1495   unsigned long i;
1496   Elf_Internal_Rela * rels;
1497   bool res = true;
1498 
1499   if (rel_type == reltype_unknown)
1500     rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1501 
1502   if (rel_type == reltype_rela)
1503     {
1504       if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1505 	return false;
1506     }
1507   else if (rel_type == reltype_rel)
1508     {
1509       if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1510 	return false;
1511     }
1512   else if (rel_type == reltype_relr)
1513     {
1514       bfd_vma * relrs;
1515       const char *format
1516 	  = is_32bit_elf ? "%08" BFD_VMA_FMT "x\n" : "%016" BFD_VMA_FMT "x\n";
1517 
1518       if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1519 			      &rel_size))
1520 	return false;
1521 
1522       printf (ngettext ("  %lu offset\n", "  %lu offsets\n", rel_size), rel_size);
1523       for (i = 0; i < rel_size; i++)
1524 	printf (format, relrs[i]);
1525       free (relrs);
1526       return true;
1527     }
1528 
1529   if (is_32bit_elf)
1530     {
1531       if (rel_type == reltype_rela)
1532 	{
1533 	  if (do_wide)
1534 	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name + Addend\n"));
1535 	  else
1536 	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name + Addend\n"));
1537 	}
1538       else
1539 	{
1540 	  if (do_wide)
1541 	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name\n"));
1542 	  else
1543 	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name\n"));
1544 	}
1545     }
1546   else
1547     {
1548       if (rel_type == reltype_rela)
1549 	{
1550 	  if (do_wide)
1551 	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend\n"));
1552 	  else
1553 	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name + Addend\n"));
1554 	}
1555       else
1556 	{
1557 	  if (do_wide)
1558 	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name\n"));
1559 	  else
1560 	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name\n"));
1561 	}
1562     }
1563 
1564   for (i = 0; i < rel_size; i++)
1565     {
1566       const char * rtype;
1567       bfd_vma offset;
1568       bfd_vma inf;
1569       bfd_vma symtab_index;
1570       bfd_vma type;
1571 
1572       offset = rels[i].r_offset;
1573       inf    = rels[i].r_info;
1574 
1575       type = get_reloc_type (filedata, inf);
1576       symtab_index = get_reloc_symindex  (inf);
1577 
1578       if (is_32bit_elf)
1579 	{
1580 	  printf ("%8.8lx  %8.8lx ",
1581 		  (unsigned long) offset & 0xffffffff,
1582 		  (unsigned long) inf & 0xffffffff);
1583 	}
1584       else
1585 	{
1586 	  printf (do_wide
1587 		  ? "%16.16" BFD_VMA_FMT "x  %16.16" BFD_VMA_FMT "x "
1588 		  : "%12.12" BFD_VMA_FMT "x  %12.12" BFD_VMA_FMT "x ",
1589 		  offset, inf);
1590 	}
1591 
1592       switch (filedata->file_header.e_machine)
1593 	{
1594 	default:
1595 	  rtype = NULL;
1596 	  break;
1597 
1598 	case EM_AARCH64:
1599 	  rtype = elf_aarch64_reloc_type (type);
1600 	  break;
1601 
1602 	case EM_M32R:
1603 	case EM_CYGNUS_M32R:
1604 	  rtype = elf_m32r_reloc_type (type);
1605 	  break;
1606 
1607 	case EM_386:
1608 	case EM_IAMCU:
1609 	  rtype = elf_i386_reloc_type (type);
1610 	  break;
1611 
1612 	case EM_68HC11:
1613 	case EM_68HC12:
1614 	  rtype = elf_m68hc11_reloc_type (type);
1615 	  break;
1616 
1617 	case EM_S12Z:
1618 	  rtype = elf_s12z_reloc_type (type);
1619 	  break;
1620 
1621 	case EM_68K:
1622 	  rtype = elf_m68k_reloc_type (type);
1623 	  break;
1624 
1625 	case EM_960:
1626 	  rtype = elf_i960_reloc_type (type);
1627 	  break;
1628 
1629 	case EM_AVR:
1630 	case EM_AVR_OLD:
1631 	  rtype = elf_avr_reloc_type (type);
1632 	  break;
1633 
1634 	case EM_OLD_SPARCV9:
1635 	case EM_SPARC32PLUS:
1636 	case EM_SPARCV9:
1637 	case EM_SPARC:
1638 	  rtype = elf_sparc_reloc_type (type);
1639 	  break;
1640 
1641 	case EM_SPU:
1642 	  rtype = elf_spu_reloc_type (type);
1643 	  break;
1644 
1645 	case EM_V800:
1646 	  rtype = v800_reloc_type (type);
1647 	  break;
1648 	case EM_V850:
1649 	case EM_CYGNUS_V850:
1650 	  rtype = v850_reloc_type (type);
1651 	  break;
1652 
1653 	case EM_D10V:
1654 	case EM_CYGNUS_D10V:
1655 	  rtype = elf_d10v_reloc_type (type);
1656 	  break;
1657 
1658 	case EM_D30V:
1659 	case EM_CYGNUS_D30V:
1660 	  rtype = elf_d30v_reloc_type (type);
1661 	  break;
1662 
1663 	case EM_DLX:
1664 	  rtype = elf_dlx_reloc_type (type);
1665 	  break;
1666 
1667 	case EM_SH:
1668 	  rtype = elf_sh_reloc_type (type);
1669 	  break;
1670 
1671 	case EM_MN10300:
1672 	case EM_CYGNUS_MN10300:
1673 	  rtype = elf_mn10300_reloc_type (type);
1674 	  break;
1675 
1676 	case EM_MN10200:
1677 	case EM_CYGNUS_MN10200:
1678 	  rtype = elf_mn10200_reloc_type (type);
1679 	  break;
1680 
1681 	case EM_FR30:
1682 	case EM_CYGNUS_FR30:
1683 	  rtype = elf_fr30_reloc_type (type);
1684 	  break;
1685 
1686 	case EM_CYGNUS_FRV:
1687 	  rtype = elf_frv_reloc_type (type);
1688 	  break;
1689 
1690 	case EM_CSKY:
1691 	  rtype = elf_csky_reloc_type (type);
1692 	  break;
1693 
1694 	case EM_FT32:
1695 	  rtype = elf_ft32_reloc_type (type);
1696 	  break;
1697 
1698 	case EM_MCORE:
1699 	  rtype = elf_mcore_reloc_type (type);
1700 	  break;
1701 
1702 	case EM_MMIX:
1703 	  rtype = elf_mmix_reloc_type (type);
1704 	  break;
1705 
1706 	case EM_MOXIE:
1707 	  rtype = elf_moxie_reloc_type (type);
1708 	  break;
1709 
1710 	case EM_MSP430:
1711 	  if (uses_msp430x_relocs (filedata))
1712 	    {
1713 	      rtype = elf_msp430x_reloc_type (type);
1714 	      break;
1715 	    }
1716 	  /* Fall through.  */
1717 	case EM_MSP430_OLD:
1718 	  rtype = elf_msp430_reloc_type (type);
1719 	  break;
1720 
1721 	case EM_NDS32:
1722 	  rtype = elf_nds32_reloc_type (type);
1723 	  break;
1724 
1725 	case EM_PPC:
1726 	  rtype = elf_ppc_reloc_type (type);
1727 	  break;
1728 
1729 	case EM_PPC64:
1730 	  rtype = elf_ppc64_reloc_type (type);
1731 	  break;
1732 
1733 	case EM_MIPS:
1734 	case EM_MIPS_RS3_LE:
1735 	  rtype = elf_mips_reloc_type (type);
1736 	  break;
1737 
1738 	case EM_RISCV:
1739 	  rtype = elf_riscv_reloc_type (type);
1740 	  break;
1741 
1742 	case EM_ALPHA:
1743 	  rtype = elf_alpha_reloc_type (type);
1744 	  break;
1745 
1746 	case EM_ARM:
1747 	  rtype = elf_arm_reloc_type (type);
1748 	  break;
1749 
1750 	case EM_ARC:
1751 	case EM_ARC_COMPACT:
1752 	case EM_ARC_COMPACT2:
1753 	  rtype = elf_arc_reloc_type (type);
1754 	  break;
1755 
1756 	case EM_PARISC:
1757 	  rtype = elf_hppa_reloc_type (type);
1758 	  break;
1759 
1760 	case EM_H8_300:
1761 	case EM_H8_300H:
1762 	case EM_H8S:
1763 	  rtype = elf_h8_reloc_type (type);
1764 	  break;
1765 
1766 	case EM_OR1K:
1767 	  rtype = elf_or1k_reloc_type (type);
1768 	  break;
1769 
1770 	case EM_PJ:
1771 	case EM_PJ_OLD:
1772 	  rtype = elf_pj_reloc_type (type);
1773 	  break;
1774 	case EM_IA_64:
1775 	  rtype = elf_ia64_reloc_type (type);
1776 	  break;
1777 
1778 	case EM_CRIS:
1779 	  rtype = elf_cris_reloc_type (type);
1780 	  break;
1781 
1782 	case EM_860:
1783 	  rtype = elf_i860_reloc_type (type);
1784 	  break;
1785 
1786 	case EM_X86_64:
1787 	case EM_L1OM:
1788 	case EM_K1OM:
1789 	  rtype = elf_x86_64_reloc_type (type);
1790 	  break;
1791 
1792 	case EM_S370:
1793 	  rtype = i370_reloc_type (type);
1794 	  break;
1795 
1796 	case EM_S390_OLD:
1797 	case EM_S390:
1798 	  rtype = elf_s390_reloc_type (type);
1799 	  break;
1800 
1801 	case EM_SCORE:
1802 	  rtype = elf_score_reloc_type (type);
1803 	  break;
1804 
1805 	case EM_XSTORMY16:
1806 	  rtype = elf_xstormy16_reloc_type (type);
1807 	  break;
1808 
1809 	case EM_CRX:
1810 	  rtype = elf_crx_reloc_type (type);
1811 	  break;
1812 
1813 	case EM_VAX:
1814 	  rtype = elf_vax_reloc_type (type);
1815 	  break;
1816 
1817 	case EM_VISIUM:
1818 	  rtype = elf_visium_reloc_type (type);
1819 	  break;
1820 
1821         case EM_BPF:
1822           rtype = elf_bpf_reloc_type (type);
1823           break;
1824 
1825 	case EM_ADAPTEVA_EPIPHANY:
1826 	  rtype = elf_epiphany_reloc_type (type);
1827 	  break;
1828 
1829 	case EM_IP2K:
1830 	case EM_IP2K_OLD:
1831 	  rtype = elf_ip2k_reloc_type (type);
1832 	  break;
1833 
1834 	case EM_IQ2000:
1835 	  rtype = elf_iq2000_reloc_type (type);
1836 	  break;
1837 
1838 	case EM_XTENSA_OLD:
1839 	case EM_XTENSA:
1840 	  rtype = elf_xtensa_reloc_type (type);
1841 	  break;
1842 
1843 	case EM_LATTICEMICO32:
1844 	  rtype = elf_lm32_reloc_type (type);
1845 	  break;
1846 
1847 	case EM_M32C_OLD:
1848 	case EM_M32C:
1849 	  rtype = elf_m32c_reloc_type (type);
1850 	  break;
1851 
1852 	case EM_MT:
1853 	  rtype = elf_mt_reloc_type (type);
1854 	  break;
1855 
1856 	case EM_BLACKFIN:
1857 	  rtype = elf_bfin_reloc_type (type);
1858 	  break;
1859 
1860 	case EM_CYGNUS_MEP:
1861 	  rtype = elf_mep_reloc_type (type);
1862 	  break;
1863 
1864 	case EM_CR16:
1865 	  rtype = elf_cr16_reloc_type (type);
1866 	  break;
1867 
1868 	case EM_MICROBLAZE:
1869 	case EM_MICROBLAZE_OLD:
1870 	  rtype = elf_microblaze_reloc_type (type);
1871 	  break;
1872 
1873 	case EM_RL78:
1874 	  rtype = elf_rl78_reloc_type (type);
1875 	  break;
1876 
1877 	case EM_RX:
1878 	  rtype = elf_rx_reloc_type (type);
1879 	  break;
1880 
1881 	case EM_METAG:
1882 	  rtype = elf_metag_reloc_type (type);
1883 	  break;
1884 
1885 	case EM_TI_C6000:
1886 	  rtype = elf_tic6x_reloc_type (type);
1887 	  break;
1888 
1889 	case EM_TILEGX:
1890 	  rtype = elf_tilegx_reloc_type (type);
1891 	  break;
1892 
1893 	case EM_TILEPRO:
1894 	  rtype = elf_tilepro_reloc_type (type);
1895 	  break;
1896 
1897 	case EM_WEBASSEMBLY:
1898 	  rtype = elf_wasm32_reloc_type (type);
1899 	  break;
1900 
1901 	case EM_XGATE:
1902 	  rtype = elf_xgate_reloc_type (type);
1903 	  break;
1904 
1905 	case EM_ALTERA_NIOS2:
1906 	  rtype = elf_nios2_reloc_type (type);
1907 	  break;
1908 
1909 	case EM_TI_PRU:
1910 	  rtype = elf_pru_reloc_type (type);
1911 	  break;
1912 
1913 	case EM_NFP:
1914 	  if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1915 	    rtype = elf_nfp3200_reloc_type (type);
1916 	  else
1917 	    rtype = elf_nfp_reloc_type (type);
1918 	  break;
1919 
1920 	case EM_Z80:
1921 	  rtype = elf_z80_reloc_type (type);
1922 	  break;
1923 
1924 	case EM_LOONGARCH:
1925 	  rtype = elf_loongarch_reloc_type (type);
1926 	  break;
1927 
1928 	case EM_AMDGPU:
1929 	  rtype = elf_amdgpu_reloc_type (type);
1930 	  break;
1931 	}
1932 
1933       if (rtype == NULL)
1934 	printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1935       else
1936 	printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1937 
1938       if (filedata->file_header.e_machine == EM_ALPHA
1939 	  && rtype != NULL
1940 	  && streq (rtype, "R_ALPHA_LITUSE")
1941 	  && rel_type == reltype_rela)
1942 	{
1943 	  switch (rels[i].r_addend)
1944 	    {
1945 	    case LITUSE_ALPHA_ADDR:   rtype = "ADDR";   break;
1946 	    case LITUSE_ALPHA_BASE:   rtype = "BASE";   break;
1947 	    case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1948 	    case LITUSE_ALPHA_JSR:    rtype = "JSR";    break;
1949 	    case LITUSE_ALPHA_TLSGD:  rtype = "TLSGD";  break;
1950 	    case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1951 	    case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1952 	    default: rtype = NULL;
1953 	    }
1954 
1955 	  if (rtype)
1956 	    printf (" (%s)", rtype);
1957 	  else
1958 	    {
1959 	      putchar (' ');
1960 	      printf (_("<unknown addend: %lx>"),
1961 		      (unsigned long) rels[i].r_addend);
1962 	      res = false;
1963 	    }
1964 	}
1965       else if (symtab_index)
1966 	{
1967 	  if (symtab == NULL || symtab_index >= nsyms)
1968 	    {
1969 	      error (_(" bad symbol index: %08lx in reloc\n"),
1970 		     (unsigned long) symtab_index);
1971 	      res = false;
1972 	    }
1973 	  else
1974 	    {
1975 	      Elf_Internal_Sym * psym;
1976 	      const char * version_string;
1977 	      enum versioned_symbol_info sym_info;
1978 	      unsigned short vna_other;
1979 
1980 	      psym = symtab + symtab_index;
1981 
1982 	      version_string
1983 		= get_symbol_version_string (filedata, is_dynsym,
1984 					     strtab, strtablen,
1985 					     symtab_index,
1986 					     psym,
1987 					     &sym_info,
1988 					     &vna_other);
1989 
1990 	      printf (" ");
1991 
1992 	      if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1993 		{
1994 		  const char * name;
1995 		  unsigned int len;
1996 		  unsigned int width = is_32bit_elf ? 8 : 14;
1997 
1998 		  /* Relocations against GNU_IFUNC symbols do not use the value
1999 		     of the symbol as the address to relocate against.  Instead
2000 		     they invoke the function named by the symbol and use its
2001 		     result as the address for relocation.
2002 
2003 		     To indicate this to the user, do not display the value of
2004 		     the symbol in the "Symbols's Value" field.  Instead show
2005 		     its name followed by () as a hint that the symbol is
2006 		     invoked.  */
2007 
2008 		  if (strtab == NULL
2009 		      || psym->st_name == 0
2010 		      || psym->st_name >= strtablen)
2011 		    name = "??";
2012 		  else
2013 		    name = strtab + psym->st_name;
2014 
2015 		  len = print_symbol (width, name);
2016 		  if (version_string)
2017 		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
2018 			    version_string);
2019 		  printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
2020 		}
2021 	      else
2022 		{
2023 		  print_vma (psym->st_value, LONG_HEX);
2024 
2025 		  printf (is_32bit_elf ? "   " : " ");
2026 		}
2027 
2028 	      if (psym->st_name == 0)
2029 		{
2030 		  const char * sec_name = "<null>";
2031 		  char name_buf[40];
2032 
2033 		  if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2034 		    {
2035 		      if (psym->st_shndx < filedata->file_header.e_shnum
2036 			  && filedata->section_headers != NULL)
2037 			sec_name = section_name_print (filedata,
2038 						       filedata->section_headers
2039 						       + psym->st_shndx);
2040 		      else if (psym->st_shndx == SHN_ABS)
2041 			sec_name = "ABS";
2042 		      else if (psym->st_shndx == SHN_COMMON)
2043 			sec_name = "COMMON";
2044 		      else if ((filedata->file_header.e_machine == EM_MIPS
2045 				&& psym->st_shndx == SHN_MIPS_SCOMMON)
2046 			       || (filedata->file_header.e_machine == EM_TI_C6000
2047 				   && psym->st_shndx == SHN_TIC6X_SCOMMON))
2048 			sec_name = "SCOMMON";
2049 		      else if (filedata->file_header.e_machine == EM_MIPS
2050 			       && psym->st_shndx == SHN_MIPS_SUNDEFINED)
2051 			sec_name = "SUNDEF";
2052 		      else if ((filedata->file_header.e_machine == EM_X86_64
2053 				|| filedata->file_header.e_machine == EM_L1OM
2054 				|| filedata->file_header.e_machine == EM_K1OM)
2055 			       && psym->st_shndx == SHN_X86_64_LCOMMON)
2056 			sec_name = "LARGE_COMMON";
2057 		      else if (filedata->file_header.e_machine == EM_IA_64
2058 			       && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
2059 			       && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
2060 			sec_name = "ANSI_COM";
2061 		      else if (is_ia64_vms (filedata)
2062 			       && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
2063 			sec_name = "VMS_SYMVEC";
2064 		      else
2065 			{
2066 			  sprintf (name_buf, "<section 0x%x>",
2067 				   (unsigned int) psym->st_shndx);
2068 			  sec_name = name_buf;
2069 			}
2070 		    }
2071 		  print_symbol (22, sec_name);
2072 		}
2073 	      else if (strtab == NULL)
2074 		printf (_("<string table index: %3ld>"), psym->st_name);
2075 	      else if (psym->st_name >= strtablen)
2076 		{
2077 		  error (_("<corrupt string table index: %3ld>\n"),
2078 			 psym->st_name);
2079 		  res = false;
2080 		}
2081 	      else
2082 		{
2083 		  print_symbol (22, strtab + psym->st_name);
2084 		  if (version_string)
2085 		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
2086 			    version_string);
2087 		}
2088 
2089 	      if (rel_type == reltype_rela)
2090 		{
2091 		  bfd_vma off = rels[i].r_addend;
2092 
2093 		  if ((bfd_signed_vma) off < 0)
2094 		    printf (" - %" BFD_VMA_FMT "x", - off);
2095 		  else
2096 		    printf (" + %" BFD_VMA_FMT "x", off);
2097 		}
2098 	    }
2099 	}
2100       else if (rel_type == reltype_rela)
2101 	{
2102 	  bfd_vma off = rels[i].r_addend;
2103 
2104 	  printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2105 	  if ((bfd_signed_vma) off < 0)
2106 	    printf ("-%" BFD_VMA_FMT "x", - off);
2107 	  else
2108 	    printf ("%" BFD_VMA_FMT "x", off);
2109 	}
2110 
2111       if (filedata->file_header.e_machine == EM_SPARCV9
2112 	  && rtype != NULL
2113 	  && streq (rtype, "R_SPARC_OLO10"))
2114 	printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
2115 
2116       putchar ('\n');
2117 
2118 #ifdef BFD64
2119       if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2120 	{
2121 	  bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
2122 	  bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
2123 	  const char * rtype2 = elf_mips_reloc_type (type2);
2124 	  const char * rtype3 = elf_mips_reloc_type (type3);
2125 
2126 	  printf ("                    Type2: ");
2127 
2128 	  if (rtype2 == NULL)
2129 	    printf (_("unrecognized: %-7lx"),
2130 		    (unsigned long) type2 & 0xffffffff);
2131 	  else
2132 	    printf ("%-17.17s", rtype2);
2133 
2134 	  printf ("\n                    Type3: ");
2135 
2136 	  if (rtype3 == NULL)
2137 	    printf (_("unrecognized: %-7lx"),
2138 		    (unsigned long) type3 & 0xffffffff);
2139 	  else
2140 	    printf ("%-17.17s", rtype3);
2141 
2142 	  putchar ('\n');
2143 	}
2144 #endif /* BFD64 */
2145     }
2146 
2147   free (rels);
2148 
2149   return res;
2150 }
2151 
2152 static const char *
2153 get_aarch64_dynamic_type (unsigned long type)
2154 {
2155   switch (type)
2156     {
2157     case DT_AARCH64_BTI_PLT:  return "AARCH64_BTI_PLT";
2158     case DT_AARCH64_PAC_PLT:  return "AARCH64_PAC_PLT";
2159     case DT_AARCH64_VARIANT_PCS:  return "AARCH64_VARIANT_PCS";
2160     default:
2161       return NULL;
2162     }
2163 }
2164 
2165 static const char *
2166 get_mips_dynamic_type (unsigned long type)
2167 {
2168   switch (type)
2169     {
2170     case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2171     case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2172     case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2173     case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2174     case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2175     case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2176     case DT_MIPS_MSYM: return "MIPS_MSYM";
2177     case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2178     case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2179     case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2180     case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2181     case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2182     case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2183     case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2184     case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2185     case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2186     case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2187     case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2188     case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2189     case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2190     case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2191     case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2192     case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2193     case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2194     case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2195     case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2196     case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2197     case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2198     case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2199     case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2200     case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2201     case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2202     case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2203     case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2204     case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2205     case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2206     case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2207     case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2208     case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2209     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2210     case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2211     case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2212     case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2213     case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2214     case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2215     case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2216     case DT_MIPS_XHASH: return "MIPS_XHASH";
2217     default:
2218       return NULL;
2219     }
2220 }
2221 
2222 static const char *
2223 get_sparc64_dynamic_type (unsigned long type)
2224 {
2225   switch (type)
2226     {
2227     case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2228     default:
2229       return NULL;
2230     }
2231 }
2232 
2233 static const char *
2234 get_ppc_dynamic_type (unsigned long type)
2235 {
2236   switch (type)
2237     {
2238     case DT_PPC_GOT:    return "PPC_GOT";
2239     case DT_PPC_OPT:    return "PPC_OPT";
2240     default:
2241       return NULL;
2242     }
2243 }
2244 
2245 static const char *
2246 get_ppc64_dynamic_type (unsigned long type)
2247 {
2248   switch (type)
2249     {
2250     case DT_PPC64_GLINK:  return "PPC64_GLINK";
2251     case DT_PPC64_OPD:    return "PPC64_OPD";
2252     case DT_PPC64_OPDSZ:  return "PPC64_OPDSZ";
2253     case DT_PPC64_OPT:    return "PPC64_OPT";
2254     default:
2255       return NULL;
2256     }
2257 }
2258 
2259 static const char *
2260 get_parisc_dynamic_type (unsigned long type)
2261 {
2262   switch (type)
2263     {
2264     case DT_HP_LOAD_MAP:	return "HP_LOAD_MAP";
2265     case DT_HP_DLD_FLAGS:	return "HP_DLD_FLAGS";
2266     case DT_HP_DLD_HOOK:	return "HP_DLD_HOOK";
2267     case DT_HP_UX10_INIT:	return "HP_UX10_INIT";
2268     case DT_HP_UX10_INITSZ:	return "HP_UX10_INITSZ";
2269     case DT_HP_PREINIT:		return "HP_PREINIT";
2270     case DT_HP_PREINITSZ:	return "HP_PREINITSZ";
2271     case DT_HP_NEEDED:		return "HP_NEEDED";
2272     case DT_HP_TIME_STAMP:	return "HP_TIME_STAMP";
2273     case DT_HP_CHECKSUM:	return "HP_CHECKSUM";
2274     case DT_HP_GST_SIZE:	return "HP_GST_SIZE";
2275     case DT_HP_GST_VERSION:	return "HP_GST_VERSION";
2276     case DT_HP_GST_HASHVAL:	return "HP_GST_HASHVAL";
2277     case DT_HP_EPLTREL:		return "HP_GST_EPLTREL";
2278     case DT_HP_EPLTRELSZ:	return "HP_GST_EPLTRELSZ";
2279     case DT_HP_FILTERED:	return "HP_FILTERED";
2280     case DT_HP_FILTER_TLS:	return "HP_FILTER_TLS";
2281     case DT_HP_COMPAT_FILTERED:	return "HP_COMPAT_FILTERED";
2282     case DT_HP_LAZYLOAD:	return "HP_LAZYLOAD";
2283     case DT_HP_BIND_NOW_COUNT:	return "HP_BIND_NOW_COUNT";
2284     case DT_PLT:		return "PLT";
2285     case DT_PLT_SIZE:		return "PLT_SIZE";
2286     case DT_DLT:		return "DLT";
2287     case DT_DLT_SIZE:		return "DLT_SIZE";
2288     default:
2289       return NULL;
2290     }
2291 }
2292 
2293 static const char *
2294 get_ia64_dynamic_type (unsigned long type)
2295 {
2296   switch (type)
2297     {
2298     case DT_IA_64_PLT_RESERVE:         return "IA_64_PLT_RESERVE";
2299     case DT_IA_64_VMS_SUBTYPE:         return "VMS_SUBTYPE";
2300     case DT_IA_64_VMS_IMGIOCNT:        return "VMS_IMGIOCNT";
2301     case DT_IA_64_VMS_LNKFLAGS:        return "VMS_LNKFLAGS";
2302     case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2303     case DT_IA_64_VMS_IDENT:           return "VMS_IDENT";
2304     case DT_IA_64_VMS_NEEDED_IDENT:    return "VMS_NEEDED_IDENT";
2305     case DT_IA_64_VMS_IMG_RELA_CNT:    return "VMS_IMG_RELA_CNT";
2306     case DT_IA_64_VMS_SEG_RELA_CNT:    return "VMS_SEG_RELA_CNT";
2307     case DT_IA_64_VMS_FIXUP_RELA_CNT:  return "VMS_FIXUP_RELA_CNT";
2308     case DT_IA_64_VMS_FIXUP_NEEDED:    return "VMS_FIXUP_NEEDED";
2309     case DT_IA_64_VMS_SYMVEC_CNT:      return "VMS_SYMVEC_CNT";
2310     case DT_IA_64_VMS_XLATED:          return "VMS_XLATED";
2311     case DT_IA_64_VMS_STACKSIZE:       return "VMS_STACKSIZE";
2312     case DT_IA_64_VMS_UNWINDSZ:        return "VMS_UNWINDSZ";
2313     case DT_IA_64_VMS_UNWIND_CODSEG:   return "VMS_UNWIND_CODSEG";
2314     case DT_IA_64_VMS_UNWIND_INFOSEG:  return "VMS_UNWIND_INFOSEG";
2315     case DT_IA_64_VMS_LINKTIME:        return "VMS_LINKTIME";
2316     case DT_IA_64_VMS_SEG_NO:          return "VMS_SEG_NO";
2317     case DT_IA_64_VMS_SYMVEC_OFFSET:   return "VMS_SYMVEC_OFFSET";
2318     case DT_IA_64_VMS_SYMVEC_SEG:      return "VMS_SYMVEC_SEG";
2319     case DT_IA_64_VMS_UNWIND_OFFSET:   return "VMS_UNWIND_OFFSET";
2320     case DT_IA_64_VMS_UNWIND_SEG:      return "VMS_UNWIND_SEG";
2321     case DT_IA_64_VMS_STRTAB_OFFSET:   return "VMS_STRTAB_OFFSET";
2322     case DT_IA_64_VMS_SYSVER_OFFSET:   return "VMS_SYSVER_OFFSET";
2323     case DT_IA_64_VMS_IMG_RELA_OFF:    return "VMS_IMG_RELA_OFF";
2324     case DT_IA_64_VMS_SEG_RELA_OFF:    return "VMS_SEG_RELA_OFF";
2325     case DT_IA_64_VMS_FIXUP_RELA_OFF:  return "VMS_FIXUP_RELA_OFF";
2326     case DT_IA_64_VMS_PLTGOT_OFFSET:   return "VMS_PLTGOT_OFFSET";
2327     case DT_IA_64_VMS_PLTGOT_SEG:      return "VMS_PLTGOT_SEG";
2328     case DT_IA_64_VMS_FPMODE:          return "VMS_FPMODE";
2329     default:
2330       return NULL;
2331     }
2332 }
2333 
2334 static const char *
2335 get_solaris_section_type (unsigned long type)
2336 {
2337   switch (type)
2338     {
2339     case 0x6fffffee: return "SUNW_ancillary";
2340     case 0x6fffffef: return "SUNW_capchain";
2341     case 0x6ffffff0: return "SUNW_capinfo";
2342     case 0x6ffffff1: return "SUNW_symsort";
2343     case 0x6ffffff2: return "SUNW_tlssort";
2344     case 0x6ffffff3: return "SUNW_LDYNSYM";
2345     case 0x6ffffff4: return "SUNW_dof";
2346     case 0x6ffffff5: return "SUNW_cap";
2347     case 0x6ffffff6: return "SUNW_SIGNATURE";
2348     case 0x6ffffff7: return "SUNW_ANNOTATE";
2349     case 0x6ffffff8: return "SUNW_DEBUGSTR";
2350     case 0x6ffffff9: return "SUNW_DEBUG";
2351     case 0x6ffffffa: return "SUNW_move";
2352     case 0x6ffffffb: return "SUNW_COMDAT";
2353     case 0x6ffffffc: return "SUNW_syminfo";
2354     case 0x6ffffffd: return "SUNW_verdef";
2355     case 0x6ffffffe: return "SUNW_verneed";
2356     case 0x6fffffff: return "SUNW_versym";
2357     case 0x70000000: return "SPARC_GOTDATA";
2358     default: return NULL;
2359     }
2360 }
2361 
2362 static const char *
2363 get_alpha_dynamic_type (unsigned long type)
2364 {
2365   switch (type)
2366     {
2367     case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2368     default: return NULL;
2369     }
2370 }
2371 
2372 static const char *
2373 get_score_dynamic_type (unsigned long type)
2374 {
2375   switch (type)
2376     {
2377     case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2378     case DT_SCORE_LOCAL_GOTNO:  return "SCORE_LOCAL_GOTNO";
2379     case DT_SCORE_SYMTABNO:     return "SCORE_SYMTABNO";
2380     case DT_SCORE_GOTSYM:       return "SCORE_GOTSYM";
2381     case DT_SCORE_UNREFEXTNO:   return "SCORE_UNREFEXTNO";
2382     case DT_SCORE_HIPAGENO:     return "SCORE_HIPAGENO";
2383     default:                    return NULL;
2384     }
2385 }
2386 
2387 static const char *
2388 get_tic6x_dynamic_type (unsigned long type)
2389 {
2390   switch (type)
2391     {
2392     case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2393     case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2394     case DT_C6000_DSBT_BASE:   return "C6000_DSBT_BASE";
2395     case DT_C6000_DSBT_SIZE:   return "C6000_DSBT_SIZE";
2396     case DT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
2397     case DT_C6000_DSBT_INDEX:  return "C6000_DSBT_INDEX";
2398     default:                   return NULL;
2399     }
2400 }
2401 
2402 static const char *
2403 get_nios2_dynamic_type (unsigned long type)
2404 {
2405   switch (type)
2406     {
2407     case DT_NIOS2_GP: return "NIOS2_GP";
2408     default:          return NULL;
2409     }
2410 }
2411 
2412 static const char *
2413 get_solaris_dynamic_type (unsigned long type)
2414 {
2415   switch (type)
2416     {
2417     case 0x6000000d: return "SUNW_AUXILIARY";
2418     case 0x6000000e: return "SUNW_RTLDINF";
2419     case 0x6000000f: return "SUNW_FILTER";
2420     case 0x60000010: return "SUNW_CAP";
2421     case 0x60000011: return "SUNW_SYMTAB";
2422     case 0x60000012: return "SUNW_SYMSZ";
2423     case 0x60000013: return "SUNW_SORTENT";
2424     case 0x60000014: return "SUNW_SYMSORT";
2425     case 0x60000015: return "SUNW_SYMSORTSZ";
2426     case 0x60000016: return "SUNW_TLSSORT";
2427     case 0x60000017: return "SUNW_TLSSORTSZ";
2428     case 0x60000018: return "SUNW_CAPINFO";
2429     case 0x60000019: return "SUNW_STRPAD";
2430     case 0x6000001a: return "SUNW_CAPCHAIN";
2431     case 0x6000001b: return "SUNW_LDMACH";
2432     case 0x6000001d: return "SUNW_CAPCHAINENT";
2433     case 0x6000001f: return "SUNW_CAPCHAINSZ";
2434     case 0x60000021: return "SUNW_PARENT";
2435     case 0x60000023: return "SUNW_ASLR";
2436     case 0x60000025: return "SUNW_RELAX";
2437     case 0x60000029: return "SUNW_NXHEAP";
2438     case 0x6000002b: return "SUNW_NXSTACK";
2439 
2440     case 0x70000001: return "SPARC_REGISTER";
2441     case 0x7ffffffd: return "AUXILIARY";
2442     case 0x7ffffffe: return "USED";
2443     case 0x7fffffff: return "FILTER";
2444 
2445     default: return NULL;
2446     }
2447 }
2448 
2449 static const char *
2450 get_riscv_dynamic_type (unsigned long type)
2451 {
2452   switch (type)
2453     {
2454     case DT_RISCV_VARIANT_CC:	return "RISCV_VARIANT_CC";
2455     default:
2456       return NULL;
2457     }
2458 }
2459 
2460 static const char *
2461 get_dynamic_type (Filedata * filedata, unsigned long type)
2462 {
2463   static char buff[64];
2464 
2465   switch (type)
2466     {
2467     case DT_NULL:	return "NULL";
2468     case DT_NEEDED:	return "NEEDED";
2469     case DT_PLTRELSZ:	return "PLTRELSZ";
2470     case DT_PLTGOT:	return "PLTGOT";
2471     case DT_HASH:	return "HASH";
2472     case DT_STRTAB:	return "STRTAB";
2473     case DT_SYMTAB:	return "SYMTAB";
2474     case DT_RELA:	return "RELA";
2475     case DT_RELASZ:	return "RELASZ";
2476     case DT_RELAENT:	return "RELAENT";
2477     case DT_STRSZ:	return "STRSZ";
2478     case DT_SYMENT:	return "SYMENT";
2479     case DT_INIT:	return "INIT";
2480     case DT_FINI:	return "FINI";
2481     case DT_SONAME:	return "SONAME";
2482     case DT_RPATH:	return "RPATH";
2483     case DT_SYMBOLIC:	return "SYMBOLIC";
2484     case DT_REL:	return "REL";
2485     case DT_RELSZ:	return "RELSZ";
2486     case DT_RELENT:	return "RELENT";
2487     case DT_RELR:	return "RELR";
2488     case DT_RELRSZ:	return "RELRSZ";
2489     case DT_RELRENT:	return "RELRENT";
2490     case DT_PLTREL:	return "PLTREL";
2491     case DT_DEBUG:	return "DEBUG";
2492     case DT_TEXTREL:	return "TEXTREL";
2493     case DT_JMPREL:	return "JMPREL";
2494     case DT_BIND_NOW:   return "BIND_NOW";
2495     case DT_INIT_ARRAY: return "INIT_ARRAY";
2496     case DT_FINI_ARRAY: return "FINI_ARRAY";
2497     case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2498     case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2499     case DT_RUNPATH:    return "RUNPATH";
2500     case DT_FLAGS:      return "FLAGS";
2501 
2502     case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2503     case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2504     case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2505 
2506     case DT_CHECKSUM:	return "CHECKSUM";
2507     case DT_PLTPADSZ:	return "PLTPADSZ";
2508     case DT_MOVEENT:	return "MOVEENT";
2509     case DT_MOVESZ:	return "MOVESZ";
2510     case DT_FEATURE:	return "FEATURE";
2511     case DT_POSFLAG_1:	return "POSFLAG_1";
2512     case DT_SYMINSZ:	return "SYMINSZ";
2513     case DT_SYMINENT:	return "SYMINENT"; /* aka VALRNGHI */
2514 
2515     case DT_ADDRRNGLO:  return "ADDRRNGLO";
2516     case DT_CONFIG:	return "CONFIG";
2517     case DT_DEPAUDIT:	return "DEPAUDIT";
2518     case DT_AUDIT:	return "AUDIT";
2519     case DT_PLTPAD:	return "PLTPAD";
2520     case DT_MOVETAB:	return "MOVETAB";
2521     case DT_SYMINFO:	return "SYMINFO"; /* aka ADDRRNGHI */
2522 
2523     case DT_VERSYM:	return "VERSYM";
2524 
2525     case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2526     case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2527     case DT_RELACOUNT:	return "RELACOUNT";
2528     case DT_RELCOUNT:	return "RELCOUNT";
2529     case DT_FLAGS_1:	return "FLAGS_1";
2530     case DT_VERDEF:	return "VERDEF";
2531     case DT_VERDEFNUM:	return "VERDEFNUM";
2532     case DT_VERNEED:	return "VERNEED";
2533     case DT_VERNEEDNUM:	return "VERNEEDNUM";
2534 
2535     case DT_AUXILIARY:	return "AUXILIARY";
2536     case DT_USED:	return "USED";
2537     case DT_FILTER:	return "FILTER";
2538 
2539     case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2540     case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2541     case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2542     case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2543     case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2544     case DT_GNU_HASH:	return "GNU_HASH";
2545     case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2546 
2547     default:
2548       if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2549 	{
2550 	  const char * result;
2551 
2552 	  switch (filedata->file_header.e_machine)
2553 	    {
2554 	    case EM_AARCH64:
2555 	      result = get_aarch64_dynamic_type (type);
2556 	      break;
2557 	    case EM_MIPS:
2558 	    case EM_MIPS_RS3_LE:
2559 	      result = get_mips_dynamic_type (type);
2560 	      break;
2561 	    case EM_SPARCV9:
2562 	      result = get_sparc64_dynamic_type (type);
2563 	      break;
2564 	    case EM_PPC:
2565 	      result = get_ppc_dynamic_type (type);
2566 	      break;
2567 	    case EM_PPC64:
2568 	      result = get_ppc64_dynamic_type (type);
2569 	      break;
2570 	    case EM_IA_64:
2571 	      result = get_ia64_dynamic_type (type);
2572 	      break;
2573 	    case EM_ALPHA:
2574 	      result = get_alpha_dynamic_type (type);
2575 	      break;
2576 	    case EM_SCORE:
2577 	      result = get_score_dynamic_type (type);
2578 	      break;
2579 	    case EM_TI_C6000:
2580 	      result = get_tic6x_dynamic_type (type);
2581 	      break;
2582 	    case EM_ALTERA_NIOS2:
2583 	      result = get_nios2_dynamic_type (type);
2584 	      break;
2585 	    case EM_RISCV:
2586 	      result = get_riscv_dynamic_type (type);
2587 	      break;
2588 	    default:
2589 	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2590 		result = get_solaris_dynamic_type (type);
2591 	      else
2592 		result = NULL;
2593 	      break;
2594 	    }
2595 
2596 	  if (result != NULL)
2597 	    return result;
2598 
2599 	  snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2600 	}
2601       else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2602 	       || (filedata->file_header.e_machine == EM_PARISC
2603 		   && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2604 	{
2605 	  const char * result;
2606 
2607 	  switch (filedata->file_header.e_machine)
2608 	    {
2609 	    case EM_PARISC:
2610 	      result = get_parisc_dynamic_type (type);
2611 	      break;
2612 	    case EM_IA_64:
2613 	      result = get_ia64_dynamic_type (type);
2614 	      break;
2615 	    default:
2616 	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2617 		result = get_solaris_dynamic_type (type);
2618 	      else
2619 		result = NULL;
2620 	      break;
2621 	    }
2622 
2623 	  if (result != NULL)
2624 	    return result;
2625 
2626 	  snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2627 		    type);
2628 	}
2629       else
2630 	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2631 
2632       return buff;
2633     }
2634 }
2635 
2636 static bool get_program_headers (Filedata *);
2637 static bool get_dynamic_section (Filedata *);
2638 
2639 static void
2640 locate_dynamic_section (Filedata *filedata)
2641 {
2642   unsigned long dynamic_addr = 0;
2643   bfd_size_type dynamic_size = 0;
2644 
2645   if (filedata->file_header.e_phnum != 0
2646       && get_program_headers (filedata))
2647     {
2648       Elf_Internal_Phdr *segment;
2649       unsigned int i;
2650 
2651       for (i = 0, segment = filedata->program_headers;
2652 	   i < filedata->file_header.e_phnum;
2653 	   i++, segment++)
2654 	{
2655 	  if (segment->p_type == PT_DYNAMIC)
2656 	    {
2657 	      dynamic_addr = segment->p_offset;
2658 	      dynamic_size = segment->p_filesz;
2659 
2660 	      if (filedata->section_headers != NULL)
2661 		{
2662 		  Elf_Internal_Shdr *sec;
2663 
2664 		  sec = find_section (filedata, ".dynamic");
2665 		  if (sec != NULL)
2666 		    {
2667 		      if (sec->sh_size == 0
2668 			  || sec->sh_type == SHT_NOBITS)
2669 			{
2670 			  dynamic_addr = 0;
2671 			  dynamic_size = 0;
2672 			}
2673 		      else
2674 			{
2675 			  dynamic_addr = sec->sh_offset;
2676 			  dynamic_size = sec->sh_size;
2677 			}
2678 		    }
2679 		}
2680 
2681 	      if (dynamic_addr > filedata->file_size
2682 		  || (dynamic_size > filedata->file_size - dynamic_addr))
2683 		{
2684 		  dynamic_addr = 0;
2685 		  dynamic_size = 0;
2686 		}
2687 	      break;
2688 	    }
2689 	}
2690     }
2691   filedata->dynamic_addr = dynamic_addr;
2692   filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2693 }
2694 
2695 static bool
2696 is_pie (Filedata *filedata)
2697 {
2698   Elf_Internal_Dyn *entry;
2699 
2700   if (filedata->dynamic_size == 0)
2701     locate_dynamic_section (filedata);
2702   if (filedata->dynamic_size <= 1)
2703     return false;
2704 
2705   if (!get_dynamic_section (filedata))
2706     return false;
2707 
2708   for (entry = filedata->dynamic_section;
2709        entry < filedata->dynamic_section + filedata->dynamic_nent;
2710        entry++)
2711     {
2712       if (entry->d_tag == DT_FLAGS_1)
2713 	{
2714 	  if ((entry->d_un.d_val & DF_1_PIE) != 0)
2715 	    return true;
2716 	  break;
2717 	}
2718     }
2719   return false;
2720 }
2721 
2722 static char *
2723 get_file_type (Filedata *filedata)
2724 {
2725   unsigned e_type = filedata->file_header.e_type;
2726   static char buff[64];
2727 
2728   switch (e_type)
2729     {
2730     case ET_NONE: return _("NONE (None)");
2731     case ET_REL:  return _("REL (Relocatable file)");
2732     case ET_EXEC: return _("EXEC (Executable file)");
2733     case ET_DYN:
2734       if (is_pie (filedata))
2735 	return _("DYN (Position-Independent Executable file)");
2736       else
2737 	return _("DYN (Shared object file)");
2738     case ET_CORE: return _("CORE (Core file)");
2739 
2740     default:
2741       if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2742 	snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2743       else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2744 	snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2745       else
2746 	snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2747       return buff;
2748     }
2749 }
2750 
2751 static char *
2752 get_machine_name (unsigned e_machine)
2753 {
2754   static char buff[64]; /* XXX */
2755 
2756   switch (e_machine)
2757     {
2758       /* Please keep this switch table sorted by increasing EM_ value.  */
2759       /* 0 */
2760     case EM_NONE:		return _("None");
2761     case EM_M32:		return "WE32100";
2762     case EM_SPARC:		return "Sparc";
2763     case EM_386:		return "Intel 80386";
2764     case EM_68K:		return "MC68000";
2765     case EM_88K:		return "MC88000";
2766     case EM_IAMCU:		return "Intel MCU";
2767     case EM_860:		return "Intel 80860";
2768     case EM_MIPS:		return "MIPS R3000";
2769     case EM_S370:		return "IBM System/370";
2770       /* 10 */
2771     case EM_MIPS_RS3_LE:	return "MIPS R4000 big-endian";
2772     case EM_OLD_SPARCV9:	return "Sparc v9 (old)";
2773     case EM_PARISC:		return "HPPA";
2774     case EM_VPP550:		return "Fujitsu VPP500";
2775     case EM_SPARC32PLUS:	return "Sparc v8+" ;
2776     case EM_960:		return "Intel 80960";
2777     case EM_PPC:		return "PowerPC";
2778       /* 20 */
2779     case EM_PPC64:		return "PowerPC64";
2780     case EM_S390_OLD:
2781     case EM_S390:		return "IBM S/390";
2782     case EM_SPU:		return "SPU";
2783       /* 30 */
2784     case EM_V800:		return "Renesas V850 (using RH850 ABI)";
2785     case EM_FR20:		return "Fujitsu FR20";
2786     case EM_RH32:		return "TRW RH32";
2787     case EM_MCORE:		return "MCORE";
2788       /* 40 */
2789     case EM_ARM:		return "ARM";
2790     case EM_OLD_ALPHA:		return "Digital Alpha (old)";
2791     case EM_SH:			return "Renesas / SuperH SH";
2792     case EM_SPARCV9:		return "Sparc v9";
2793     case EM_TRICORE:		return "Siemens Tricore";
2794     case EM_ARC:		return "ARC";
2795     case EM_H8_300:		return "Renesas H8/300";
2796     case EM_H8_300H:		return "Renesas H8/300H";
2797     case EM_H8S:		return "Renesas H8S";
2798     case EM_H8_500:		return "Renesas H8/500";
2799       /* 50 */
2800     case EM_IA_64:		return "Intel IA-64";
2801     case EM_MIPS_X:		return "Stanford MIPS-X";
2802     case EM_COLDFIRE:		return "Motorola Coldfire";
2803     case EM_68HC12:		return "Motorola MC68HC12 Microcontroller";
2804     case EM_MMA:		return "Fujitsu Multimedia Accelerator";
2805     case EM_PCP:		return "Siemens PCP";
2806     case EM_NCPU:		return "Sony nCPU embedded RISC processor";
2807     case EM_NDR1:		return "Denso NDR1 microprocesspr";
2808     case EM_STARCORE:		return "Motorola Star*Core processor";
2809     case EM_ME16:		return "Toyota ME16 processor";
2810       /* 60 */
2811     case EM_ST100:		return "STMicroelectronics ST100 processor";
2812     case EM_TINYJ:		return "Advanced Logic Corp. TinyJ embedded processor";
2813     case EM_X86_64:		return "Advanced Micro Devices X86-64";
2814     case EM_PDSP:		return "Sony DSP processor";
2815     case EM_PDP10:		return "Digital Equipment Corp. PDP-10";
2816     case EM_PDP11:		return "Digital Equipment Corp. PDP-11";
2817     case EM_FX66:		return "Siemens FX66 microcontroller";
2818     case EM_ST9PLUS:		return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2819     case EM_ST7:		return "STMicroelectronics ST7 8-bit microcontroller";
2820     case EM_68HC16:		return "Motorola MC68HC16 Microcontroller";
2821       /* 70 */
2822     case EM_68HC11:		return "Motorola MC68HC11 Microcontroller";
2823     case EM_68HC08:		return "Motorola MC68HC08 Microcontroller";
2824     case EM_68HC05:		return "Motorola MC68HC05 Microcontroller";
2825     case EM_SVX:		return "Silicon Graphics SVx";
2826     case EM_ST19:		return "STMicroelectronics ST19 8-bit microcontroller";
2827     case EM_VAX:		return "Digital VAX";
2828     case EM_CRIS:		return "Axis Communications 32-bit embedded processor";
2829     case EM_JAVELIN:		return "Infineon Technologies 32-bit embedded cpu";
2830     case EM_FIREPATH:		return "Element 14 64-bit DSP processor";
2831     case EM_ZSP:		return "LSI Logic's 16-bit DSP processor";
2832       /* 80 */
2833     case EM_MMIX:		return "Donald Knuth's educational 64-bit processor";
2834     case EM_HUANY:		return "Harvard Universitys's machine-independent object format";
2835     case EM_PRISM:		return "Vitesse Prism";
2836     case EM_AVR_OLD:
2837     case EM_AVR:		return "Atmel AVR 8-bit microcontroller";
2838     case EM_CYGNUS_FR30:
2839     case EM_FR30:		return "Fujitsu FR30";
2840     case EM_CYGNUS_D10V:
2841     case EM_D10V:		return "d10v";
2842     case EM_CYGNUS_D30V:
2843     case EM_D30V:		return "d30v";
2844     case EM_CYGNUS_V850:
2845     case EM_V850:		return "Renesas V850";
2846     case EM_CYGNUS_M32R:
2847     case EM_M32R:		return "Renesas M32R (formerly Mitsubishi M32r)";
2848     case EM_CYGNUS_MN10300:
2849     case EM_MN10300:		return "mn10300";
2850       /* 90 */
2851     case EM_CYGNUS_MN10200:
2852     case EM_MN10200:		return "mn10200";
2853     case EM_PJ:			return "picoJava";
2854     case EM_OR1K:		return "OpenRISC 1000";
2855     case EM_ARC_COMPACT:	return "ARCompact";
2856     case EM_XTENSA_OLD:
2857     case EM_XTENSA:		return "Tensilica Xtensa Processor";
2858     case EM_VIDEOCORE:		return "Alphamosaic VideoCore processor";
2859     case EM_TMM_GPP:		return "Thompson Multimedia General Purpose Processor";
2860     case EM_NS32K:		return "National Semiconductor 32000 series";
2861     case EM_TPC:		return "Tenor Network TPC processor";
2862     case EM_SNP1K:	        return "Trebia SNP 1000 processor";
2863       /* 100 */
2864     case EM_ST200:		return "STMicroelectronics ST200 microcontroller";
2865     case EM_IP2K_OLD:
2866     case EM_IP2K:		return "Ubicom IP2xxx 8-bit microcontrollers";
2867     case EM_MAX:		return "MAX Processor";
2868     case EM_CR:			return "National Semiconductor CompactRISC";
2869     case EM_F2MC16:		return "Fujitsu F2MC16";
2870     case EM_MSP430:		return "Texas Instruments msp430 microcontroller";
2871     case EM_BLACKFIN:		return "Analog Devices Blackfin";
2872     case EM_SE_C33:		return "S1C33 Family of Seiko Epson processors";
2873     case EM_SEP:		return "Sharp embedded microprocessor";
2874     case EM_ARCA:		return "Arca RISC microprocessor";
2875       /* 110 */
2876     case EM_UNICORE:		return "Unicore";
2877     case EM_EXCESS:		return "eXcess 16/32/64-bit configurable embedded CPU";
2878     case EM_DXP:		return "Icera Semiconductor Inc. Deep Execution Processor";
2879     case EM_ALTERA_NIOS2:	return "Altera Nios II";
2880     case EM_CRX:		return "National Semiconductor CRX microprocessor";
2881     case EM_XGATE:		return "Motorola XGATE embedded processor";
2882     case EM_C166:
2883     case EM_XC16X:		return "Infineon Technologies xc16x";
2884     case EM_M16C:		return "Renesas M16C series microprocessors";
2885     case EM_DSPIC30F:		return "Microchip Technology dsPIC30F Digital Signal Controller";
2886     case EM_CE:			return "Freescale Communication Engine RISC core";
2887       /* 120 */
2888     case EM_M32C:	        return "Renesas M32c";
2889       /* 130 */
2890     case EM_TSK3000:		return "Altium TSK3000 core";
2891     case EM_RS08:		return "Freescale RS08 embedded processor";
2892     case EM_ECOG2:		return "Cyan Technology eCOG2 microprocessor";
2893     case EM_SCORE:		return "SUNPLUS S+Core";
2894     case EM_DSP24:		return "New Japan Radio (NJR) 24-bit DSP Processor";
2895     case EM_VIDEOCORE3:		return "Broadcom VideoCore III processor";
2896     case EM_LATTICEMICO32:	return "Lattice Mico32";
2897     case EM_SE_C17:		return "Seiko Epson C17 family";
2898       /* 140 */
2899     case EM_TI_C6000:		return "Texas Instruments TMS320C6000 DSP family";
2900     case EM_TI_C2000:		return "Texas Instruments TMS320C2000 DSP family";
2901     case EM_TI_C5500:		return "Texas Instruments TMS320C55x DSP family";
2902     case EM_TI_PRU:		return "TI PRU I/O processor";
2903       /* 160 */
2904     case EM_MMDSP_PLUS:		return "STMicroelectronics 64bit VLIW Data Signal Processor";
2905     case EM_CYPRESS_M8C:	return "Cypress M8C microprocessor";
2906     case EM_R32C:		return "Renesas R32C series microprocessors";
2907     case EM_TRIMEDIA:		return "NXP Semiconductors TriMedia architecture family";
2908     case EM_QDSP6:		return "QUALCOMM DSP6 Processor";
2909     case EM_8051:		return "Intel 8051 and variants";
2910     case EM_STXP7X:		return "STMicroelectronics STxP7x family";
2911     case EM_NDS32:		return "Andes Technology compact code size embedded RISC processor family";
2912     case EM_ECOG1X:		return "Cyan Technology eCOG1X family";
2913     case EM_MAXQ30:		return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2914       /* 170 */
2915     case EM_XIMO16:		return "New Japan Radio (NJR) 16-bit DSP Processor";
2916     case EM_MANIK:		return "M2000 Reconfigurable RISC Microprocessor";
2917     case EM_CRAYNV2:		return "Cray Inc. NV2 vector architecture";
2918     case EM_RX:			return "Renesas RX";
2919     case EM_METAG:		return "Imagination Technologies Meta processor architecture";
2920     case EM_MCST_ELBRUS:	return "MCST Elbrus general purpose hardware architecture";
2921     case EM_ECOG16:		return "Cyan Technology eCOG16 family";
2922     case EM_CR16:
2923     case EM_MICROBLAZE:
2924     case EM_MICROBLAZE_OLD:	return "Xilinx MicroBlaze";
2925     case EM_ETPU:		return "Freescale Extended Time Processing Unit";
2926     case EM_SLE9X:		return "Infineon Technologies SLE9X core";
2927       /* 180 */
2928     case EM_L1OM:		return "Intel L1OM";
2929     case EM_K1OM:		return "Intel K1OM";
2930     case EM_INTEL182:		return "Intel (reserved)";
2931     case EM_AARCH64:		return "AArch64";
2932     case EM_ARM184:		return "ARM (reserved)";
2933     case EM_AVR32:		return "Atmel Corporation 32-bit microprocessor";
2934     case EM_STM8:		return "STMicroeletronics STM8 8-bit microcontroller";
2935     case EM_TILE64:		return "Tilera TILE64 multicore architecture family";
2936     case EM_TILEPRO:		return "Tilera TILEPro multicore architecture family";
2937       /* 190 */
2938     case EM_CUDA:		return "NVIDIA CUDA architecture";
2939     case EM_TILEGX:		return "Tilera TILE-Gx multicore architecture family";
2940     case EM_CLOUDSHIELD:	return "CloudShield architecture family";
2941     case EM_COREA_1ST:		return "KIPO-KAIST Core-A 1st generation processor family";
2942     case EM_COREA_2ND:		return "KIPO-KAIST Core-A 2nd generation processor family";
2943     case EM_ARC_COMPACT2:	return "ARCv2";
2944     case EM_OPEN8:		return "Open8 8-bit RISC soft processor core";
2945     case EM_RL78:		return "Renesas RL78";
2946     case EM_VIDEOCORE5:		return "Broadcom VideoCore V processor";
2947     case EM_78K0R:		return "Renesas 78K0R";
2948       /* 200 */
2949     case EM_56800EX:		return "Freescale 56800EX Digital Signal Controller (DSC)";
2950     case EM_BA1:		return "Beyond BA1 CPU architecture";
2951     case EM_BA2:		return "Beyond BA2 CPU architecture";
2952     case EM_XCORE:		return "XMOS xCORE processor family";
2953     case EM_MCHP_PIC:		return "Microchip 8-bit PIC(r) family";
2954     case EM_INTELGT:		return "Intel Graphics Technology";
2955       /* 210 */
2956     case EM_KM32:		return "KM211 KM32 32-bit processor";
2957     case EM_KMX32:		return "KM211 KMX32 32-bit processor";
2958     case EM_KMX16:		return "KM211 KMX16 16-bit processor";
2959     case EM_KMX8:		return "KM211 KMX8 8-bit processor";
2960     case EM_KVARC:		return "KM211 KVARC processor";
2961     case EM_CDP:		return "Paneve CDP architecture family";
2962     case EM_COGE:		return "Cognitive Smart Memory Processor";
2963     case EM_COOL:		return "Bluechip Systems CoolEngine";
2964     case EM_NORC:		return "Nanoradio Optimized RISC";
2965     case EM_CSR_KALIMBA:	return "CSR Kalimba architecture family";
2966       /* 220 */
2967     case EM_Z80:		return "Zilog Z80";
2968     case EM_VISIUM:		return "CDS VISIUMcore processor";
2969     case EM_FT32:               return "FTDI Chip FT32";
2970     case EM_MOXIE:              return "Moxie";
2971     case EM_AMDGPU: 	 	return "AMD GPU";
2972       /* 230 (all reserved) */
2973       /* 240 */
2974     case EM_RISCV: 	 	return "RISC-V";
2975     case EM_LANAI:		return "Lanai 32-bit processor";
2976     case EM_CEVA:		return "CEVA Processor Architecture Family";
2977     case EM_CEVA_X2:		return "CEVA X2 Processor Family";
2978     case EM_BPF:		return "Linux BPF";
2979     case EM_GRAPHCORE_IPU:	return "Graphcore Intelligent Processing Unit";
2980     case EM_IMG1:		return "Imagination Technologies";
2981       /* 250 */
2982     case EM_NFP:		return "Netronome Flow Processor";
2983     case EM_VE:			return "NEC Vector Engine";
2984     case EM_CSKY:		return "C-SKY";
2985     case EM_ARC_COMPACT3_64:	return "Synopsys ARCv2.3 64-bit";
2986     case EM_MCS6502:		return "MOS Technology MCS 6502 processor";
2987     case EM_ARC_COMPACT3:	return "Synopsys ARCv2.3 32-bit";
2988     case EM_KVX:		return "Kalray VLIW core of the MPPA processor family";
2989     case EM_65816:		return "WDC 65816/65C816";
2990     case EM_LOONGARCH:		return "LoongArch";
2991     case EM_KF32:		return "ChipON KungFu32";
2992 
2993       /* Large numbers...  */
2994     case EM_MT:                 return "Morpho Techologies MT processor";
2995     case EM_ALPHA:		return "Alpha";
2996     case EM_WEBASSEMBLY:	return "Web Assembly";
2997     case EM_DLX:		return "OpenDLX";
2998     case EM_XSTORMY16:		return "Sanyo XStormy16 CPU core";
2999     case EM_IQ2000:       	return "Vitesse IQ2000";
3000     case EM_M32C_OLD:
3001     case EM_NIOS32:		return "Altera Nios";
3002     case EM_CYGNUS_MEP:         return "Toshiba MeP Media Engine";
3003     case EM_ADAPTEVA_EPIPHANY:	return "Adapteva EPIPHANY";
3004     case EM_CYGNUS_FRV:		return "Fujitsu FR-V";
3005     case EM_S12Z:               return "Freescale S12Z";
3006 
3007     default:
3008       snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
3009       return buff;
3010     }
3011 }
3012 
3013 static void
3014 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
3015 {
3016   /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2.  Some
3017      other compilers don't specify an architecture type in the e_flags, and
3018      instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3019      architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3020      architectures.
3021 
3022      Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3023      but also sets a specific architecture type in the e_flags field.
3024 
3025      However, when decoding the flags we don't worry if we see an
3026      unexpected pairing, for example EM_ARC_COMPACT machine type, with
3027      ARCEM architecture type.  */
3028 
3029   switch (e_flags & EF_ARC_MACH_MSK)
3030     {
3031       /* We only expect these to occur for EM_ARC_COMPACT2.  */
3032     case EF_ARC_CPU_ARCV2EM:
3033       strcat (buf, ", ARC EM");
3034       break;
3035     case EF_ARC_CPU_ARCV2HS:
3036       strcat (buf, ", ARC HS");
3037       break;
3038 
3039       /* We only expect these to occur for EM_ARC_COMPACT.  */
3040     case E_ARC_MACH_ARC600:
3041       strcat (buf, ", ARC600");
3042       break;
3043     case E_ARC_MACH_ARC601:
3044       strcat (buf, ", ARC601");
3045       break;
3046     case E_ARC_MACH_ARC700:
3047       strcat (buf, ", ARC700");
3048       break;
3049 
3050       /* The only times we should end up here are (a) A corrupt ELF, (b) A
3051          new ELF with new architecture being read by an old version of
3052          readelf, or (c) An ELF built with non-GNU compiler that does not
3053          set the architecture in the e_flags.  */
3054     default:
3055       if (e_machine == EM_ARC_COMPACT)
3056         strcat (buf, ", Unknown ARCompact");
3057       else
3058         strcat (buf, ", Unknown ARC");
3059       break;
3060     }
3061 
3062   switch (e_flags & EF_ARC_OSABI_MSK)
3063     {
3064     case E_ARC_OSABI_ORIG:
3065       strcat (buf, ", (ABI:legacy)");
3066       break;
3067     case E_ARC_OSABI_V2:
3068       strcat (buf, ", (ABI:v2)");
3069       break;
3070       /* Only upstream 3.9+ kernels will support ARCv2 ISA.  */
3071     case E_ARC_OSABI_V3:
3072       strcat (buf, ", v3 no-legacy-syscalls ABI");
3073       break;
3074     case E_ARC_OSABI_V4:
3075       strcat (buf, ", v4 ABI");
3076       break;
3077     default:
3078       strcat (buf, ", unrecognised ARC OSABI flag");
3079       break;
3080     }
3081 }
3082 
3083 static void
3084 decode_ARM_machine_flags (unsigned e_flags, char buf[])
3085 {
3086   unsigned eabi;
3087   bool unknown = false;
3088 
3089   eabi = EF_ARM_EABI_VERSION (e_flags);
3090   e_flags &= ~ EF_ARM_EABIMASK;
3091 
3092   /* Handle "generic" ARM flags.  */
3093   if (e_flags & EF_ARM_RELEXEC)
3094     {
3095       strcat (buf, ", relocatable executable");
3096       e_flags &= ~ EF_ARM_RELEXEC;
3097     }
3098 
3099   if (e_flags & EF_ARM_PIC)
3100     {
3101       strcat (buf, ", position independent");
3102       e_flags &= ~ EF_ARM_PIC;
3103     }
3104 
3105   /* Now handle EABI specific flags.  */
3106   switch (eabi)
3107     {
3108     default:
3109       strcat (buf, ", <unrecognized EABI>");
3110       if (e_flags)
3111 	unknown = true;
3112       break;
3113 
3114     case EF_ARM_EABI_VER1:
3115       strcat (buf, ", Version1 EABI");
3116       while (e_flags)
3117 	{
3118 	  unsigned flag;
3119 
3120 	  /* Process flags one bit at a time.  */
3121 	  flag = e_flags & - e_flags;
3122 	  e_flags &= ~ flag;
3123 
3124 	  switch (flag)
3125 	    {
3126 	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
3127 	      strcat (buf, ", sorted symbol tables");
3128 	      break;
3129 
3130 	    default:
3131 	      unknown = true;
3132 	      break;
3133 	    }
3134 	}
3135       break;
3136 
3137     case EF_ARM_EABI_VER2:
3138       strcat (buf, ", Version2 EABI");
3139       while (e_flags)
3140 	{
3141 	  unsigned flag;
3142 
3143 	  /* Process flags one bit at a time.  */
3144 	  flag = e_flags & - e_flags;
3145 	  e_flags &= ~ flag;
3146 
3147 	  switch (flag)
3148 	    {
3149 	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
3150 	      strcat (buf, ", sorted symbol tables");
3151 	      break;
3152 
3153 	    case EF_ARM_DYNSYMSUSESEGIDX:
3154 	      strcat (buf, ", dynamic symbols use segment index");
3155 	      break;
3156 
3157 	    case EF_ARM_MAPSYMSFIRST:
3158 	      strcat (buf, ", mapping symbols precede others");
3159 	      break;
3160 
3161 	    default:
3162 	      unknown = true;
3163 	      break;
3164 	    }
3165 	}
3166       break;
3167 
3168     case EF_ARM_EABI_VER3:
3169       strcat (buf, ", Version3 EABI");
3170       break;
3171 
3172     case EF_ARM_EABI_VER4:
3173       strcat (buf, ", Version4 EABI");
3174       while (e_flags)
3175 	{
3176 	  unsigned flag;
3177 
3178 	  /* Process flags one bit at a time.  */
3179 	  flag = e_flags & - e_flags;
3180 	  e_flags &= ~ flag;
3181 
3182 	  switch (flag)
3183 	    {
3184 	    case EF_ARM_BE8:
3185 	      strcat (buf, ", BE8");
3186 	      break;
3187 
3188 	    case EF_ARM_LE8:
3189 	      strcat (buf, ", LE8");
3190 	      break;
3191 
3192 	    default:
3193 	      unknown = true;
3194 	      break;
3195 	    }
3196 	}
3197       break;
3198 
3199     case EF_ARM_EABI_VER5:
3200       strcat (buf, ", Version5 EABI");
3201       while (e_flags)
3202 	{
3203 	  unsigned flag;
3204 
3205 	  /* Process flags one bit at a time.  */
3206 	  flag = e_flags & - e_flags;
3207 	  e_flags &= ~ flag;
3208 
3209 	  switch (flag)
3210 	    {
3211 	    case EF_ARM_BE8:
3212 	      strcat (buf, ", BE8");
3213 	      break;
3214 
3215 	    case EF_ARM_LE8:
3216 	      strcat (buf, ", LE8");
3217 	      break;
3218 
3219 	    case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT.  */
3220 	      strcat (buf, ", soft-float ABI");
3221 	      break;
3222 
3223 	    case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT.  */
3224 	      strcat (buf, ", hard-float ABI");
3225 	      break;
3226 
3227 	    default:
3228 	      unknown = true;
3229 	      break;
3230 	    }
3231 	}
3232       break;
3233 
3234     case EF_ARM_EABI_UNKNOWN:
3235       strcat (buf, ", GNU EABI");
3236       while (e_flags)
3237 	{
3238 	  unsigned flag;
3239 
3240 	  /* Process flags one bit at a time.  */
3241 	  flag = e_flags & - e_flags;
3242 	  e_flags &= ~ flag;
3243 
3244 	  switch (flag)
3245 	    {
3246 	    case EF_ARM_INTERWORK:
3247 	      strcat (buf, ", interworking enabled");
3248 	      break;
3249 
3250 	    case EF_ARM_APCS_26:
3251 	      strcat (buf, ", uses APCS/26");
3252 	      break;
3253 
3254 	    case EF_ARM_APCS_FLOAT:
3255 	      strcat (buf, ", uses APCS/float");
3256 	      break;
3257 
3258 	    case EF_ARM_PIC:
3259 	      strcat (buf, ", position independent");
3260 	      break;
3261 
3262 	    case EF_ARM_ALIGN8:
3263 	      strcat (buf, ", 8 bit structure alignment");
3264 	      break;
3265 
3266 	    case EF_ARM_NEW_ABI:
3267 	      strcat (buf, ", uses new ABI");
3268 	      break;
3269 
3270 	    case EF_ARM_OLD_ABI:
3271 	      strcat (buf, ", uses old ABI");
3272 	      break;
3273 
3274 	    case EF_ARM_SOFT_FLOAT:
3275 	      strcat (buf, ", software FP");
3276 	      break;
3277 
3278 	    case EF_ARM_VFP_FLOAT:
3279 	      strcat (buf, ", VFP");
3280 	      break;
3281 
3282 	    case EF_ARM_MAVERICK_FLOAT:
3283 	      strcat (buf, ", Maverick FP");
3284 	      break;
3285 
3286 	    default:
3287 	      unknown = true;
3288 	      break;
3289 	    }
3290 	}
3291     }
3292 
3293   if (unknown)
3294     strcat (buf,_(", <unknown>"));
3295 }
3296 
3297 static void
3298 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3299 {
3300   --size; /* Leave space for null terminator.  */
3301 
3302   switch (e_flags & EF_AVR_MACH)
3303     {
3304     case E_AVR_MACH_AVR1:
3305       strncat (buf, ", avr:1", size);
3306       break;
3307     case E_AVR_MACH_AVR2:
3308       strncat (buf, ", avr:2", size);
3309       break;
3310     case E_AVR_MACH_AVR25:
3311       strncat (buf, ", avr:25", size);
3312       break;
3313     case E_AVR_MACH_AVR3:
3314       strncat (buf, ", avr:3", size);
3315       break;
3316     case E_AVR_MACH_AVR31:
3317       strncat (buf, ", avr:31", size);
3318       break;
3319     case E_AVR_MACH_AVR35:
3320       strncat (buf, ", avr:35", size);
3321       break;
3322     case E_AVR_MACH_AVR4:
3323       strncat (buf, ", avr:4", size);
3324       break;
3325     case E_AVR_MACH_AVR5:
3326       strncat (buf, ", avr:5", size);
3327       break;
3328     case E_AVR_MACH_AVR51:
3329       strncat (buf, ", avr:51", size);
3330       break;
3331     case E_AVR_MACH_AVR6:
3332       strncat (buf, ", avr:6", size);
3333       break;
3334     case E_AVR_MACH_AVRTINY:
3335       strncat (buf, ", avr:100", size);
3336       break;
3337     case E_AVR_MACH_XMEGA1:
3338       strncat (buf, ", avr:101", size);
3339       break;
3340     case E_AVR_MACH_XMEGA2:
3341       strncat (buf, ", avr:102", size);
3342       break;
3343     case E_AVR_MACH_XMEGA3:
3344       strncat (buf, ", avr:103", size);
3345       break;
3346     case E_AVR_MACH_XMEGA4:
3347       strncat (buf, ", avr:104", size);
3348       break;
3349     case E_AVR_MACH_XMEGA5:
3350       strncat (buf, ", avr:105", size);
3351       break;
3352     case E_AVR_MACH_XMEGA6:
3353       strncat (buf, ", avr:106", size);
3354       break;
3355     case E_AVR_MACH_XMEGA7:
3356       strncat (buf, ", avr:107", size);
3357       break;
3358     default:
3359       strncat (buf, ", avr:<unknown>", size);
3360       break;
3361     }
3362 
3363   size -= strlen (buf);
3364   if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3365     strncat (buf, ", link-relax", size);
3366 }
3367 
3368 static void
3369 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3370 {
3371   unsigned abi;
3372   unsigned arch;
3373   unsigned config;
3374   unsigned version;
3375   bool has_fpu = false;
3376   unsigned int r = 0;
3377 
3378   static const char *ABI_STRINGS[] =
3379   {
3380     "ABI v0", /* use r5 as return register; only used in N1213HC */
3381     "ABI v1", /* use r0 as return register */
3382     "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3383     "ABI v2fp", /* for FPU */
3384     "AABI",
3385     "ABI2 FP+"
3386   };
3387   static const char *VER_STRINGS[] =
3388   {
3389     "Andes ELF V1.3 or older",
3390     "Andes ELF V1.3.1",
3391     "Andes ELF V1.4"
3392   };
3393   static const char *ARCH_STRINGS[] =
3394   {
3395     "",
3396     "Andes Star v1.0",
3397     "Andes Star v2.0",
3398     "Andes Star v3.0",
3399     "Andes Star v3.0m"
3400   };
3401 
3402   abi = EF_NDS_ABI & e_flags;
3403   arch = EF_NDS_ARCH & e_flags;
3404   config = EF_NDS_INST & e_flags;
3405   version = EF_NDS32_ELF_VERSION & e_flags;
3406 
3407   memset (buf, 0, size);
3408 
3409   switch (abi)
3410     {
3411     case E_NDS_ABI_V0:
3412     case E_NDS_ABI_V1:
3413     case E_NDS_ABI_V2:
3414     case E_NDS_ABI_V2FP:
3415     case E_NDS_ABI_AABI:
3416     case E_NDS_ABI_V2FP_PLUS:
3417       /* In case there are holes in the array.  */
3418       r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3419       break;
3420 
3421     default:
3422       r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3423       break;
3424     }
3425 
3426   switch (version)
3427     {
3428     case E_NDS32_ELF_VER_1_2:
3429     case E_NDS32_ELF_VER_1_3:
3430     case E_NDS32_ELF_VER_1_4:
3431       r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3432       break;
3433 
3434     default:
3435       r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3436       break;
3437     }
3438 
3439   if (E_NDS_ABI_V0 == abi)
3440     {
3441       /* OLD ABI; only used in N1213HC, has performance extension 1.  */
3442       r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3443       if (arch == E_NDS_ARCH_STAR_V1_0)
3444 	r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3445       return;
3446     }
3447 
3448   switch (arch)
3449     {
3450     case E_NDS_ARCH_STAR_V1_0:
3451     case E_NDS_ARCH_STAR_V2_0:
3452     case E_NDS_ARCH_STAR_V3_0:
3453     case E_NDS_ARCH_STAR_V3_M:
3454       r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3455       break;
3456 
3457     default:
3458       r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3459       /* ARCH version determines how the e_flags are interpreted.
3460 	 If it is unknown, we cannot proceed.  */
3461       return;
3462     }
3463 
3464   /* Newer ABI; Now handle architecture specific flags.  */
3465   if (arch == E_NDS_ARCH_STAR_V1_0)
3466     {
3467       if (config & E_NDS32_HAS_MFUSR_PC_INST)
3468 	r += snprintf (buf + r, size -r, ", MFUSR_PC");
3469 
3470       if (!(config & E_NDS32_HAS_NO_MAC_INST))
3471 	r += snprintf (buf + r, size -r, ", MAC");
3472 
3473       if (config & E_NDS32_HAS_DIV_INST)
3474 	r += snprintf (buf + r, size -r, ", DIV");
3475 
3476       if (config & E_NDS32_HAS_16BIT_INST)
3477 	r += snprintf (buf + r, size -r, ", 16b");
3478     }
3479   else
3480     {
3481       if (config & E_NDS32_HAS_MFUSR_PC_INST)
3482 	{
3483 	  if (version <= E_NDS32_ELF_VER_1_3)
3484 	    r += snprintf (buf + r, size -r, ", [B8]");
3485 	  else
3486 	    r += snprintf (buf + r, size -r, ", EX9");
3487 	}
3488 
3489       if (config & E_NDS32_HAS_MAC_DX_INST)
3490 	r += snprintf (buf + r, size -r, ", MAC_DX");
3491 
3492       if (config & E_NDS32_HAS_DIV_DX_INST)
3493 	r += snprintf (buf + r, size -r, ", DIV_DX");
3494 
3495       if (config & E_NDS32_HAS_16BIT_INST)
3496 	{
3497 	  if (version <= E_NDS32_ELF_VER_1_3)
3498 	    r += snprintf (buf + r, size -r, ", 16b");
3499 	  else
3500 	    r += snprintf (buf + r, size -r, ", IFC");
3501 	}
3502     }
3503 
3504   if (config & E_NDS32_HAS_EXT_INST)
3505     r += snprintf (buf + r, size -r, ", PERF1");
3506 
3507   if (config & E_NDS32_HAS_EXT2_INST)
3508     r += snprintf (buf + r, size -r, ", PERF2");
3509 
3510   if (config & E_NDS32_HAS_FPU_INST)
3511     {
3512       has_fpu = true;
3513       r += snprintf (buf + r, size -r, ", FPU_SP");
3514     }
3515 
3516   if (config & E_NDS32_HAS_FPU_DP_INST)
3517     {
3518       has_fpu = true;
3519       r += snprintf (buf + r, size -r, ", FPU_DP");
3520     }
3521 
3522   if (config & E_NDS32_HAS_FPU_MAC_INST)
3523     {
3524       has_fpu = true;
3525       r += snprintf (buf + r, size -r, ", FPU_MAC");
3526     }
3527 
3528   if (has_fpu)
3529     {
3530       switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3531 	{
3532 	case E_NDS32_FPU_REG_8SP_4DP:
3533 	  r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3534 	  break;
3535 	case E_NDS32_FPU_REG_16SP_8DP:
3536 	  r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3537 	  break;
3538 	case E_NDS32_FPU_REG_32SP_16DP:
3539 	  r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3540 	  break;
3541 	case E_NDS32_FPU_REG_32SP_32DP:
3542 	  r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3543 	  break;
3544 	}
3545     }
3546 
3547   if (config & E_NDS32_HAS_AUDIO_INST)
3548     r += snprintf (buf + r, size -r, ", AUDIO");
3549 
3550   if (config & E_NDS32_HAS_STRING_INST)
3551     r += snprintf (buf + r, size -r, ", STR");
3552 
3553   if (config & E_NDS32_HAS_REDUCED_REGS)
3554     r += snprintf (buf + r, size -r, ", 16REG");
3555 
3556   if (config & E_NDS32_HAS_VIDEO_INST)
3557     {
3558       if (version <= E_NDS32_ELF_VER_1_3)
3559 	r += snprintf (buf + r, size -r, ", VIDEO");
3560       else
3561 	r += snprintf (buf + r, size -r, ", SATURATION");
3562     }
3563 
3564   if (config & E_NDS32_HAS_ENCRIPT_INST)
3565     r += snprintf (buf + r, size -r, ", ENCRP");
3566 
3567   if (config & E_NDS32_HAS_L2C_INST)
3568     r += snprintf (buf + r, size -r, ", L2C");
3569 }
3570 
3571 static void
3572 decode_AMDGPU_machine_flags (Filedata *filedata, unsigned int e_flags,
3573 			     char *buf)
3574 {
3575   unsigned char *e_ident = filedata->file_header.e_ident;
3576   unsigned char osabi = e_ident[EI_OSABI];
3577   unsigned char abiversion = e_ident[EI_ABIVERSION];
3578   unsigned int mach;
3579 
3580   /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
3581      it has been deprecated for a while.
3582 
3583      The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
3584      of writing, they use the same flags as HSA v3, so the code below uses that
3585      assumption.  */
3586   if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
3587     return;
3588 
3589   mach = e_flags & EF_AMDGPU_MACH;
3590   switch (mach)
3591     {
3592 #define AMDGPU_CASE(code, string) \
3593   case code: strcat (buf, ", " string); break;
3594     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
3595     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
3596     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
3597     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
3598     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
3599     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
3600     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
3601     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
3602     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
3603     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
3604     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
3605     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
3606     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
3607     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
3608     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
3609     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
3610     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
3611     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
3612     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
3613     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
3614     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
3615     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
3616     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
3617     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
3618     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
3619     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
3620     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
3621     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
3622     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
3623     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
3624     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
3625     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
3626     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
3627     AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
3628     default:
3629       sprintf (buf, _(", <unknown AMDGPU GPU type: %#x>"), mach);
3630       break;
3631 #undef AMDGPU_CASE
3632     }
3633 
3634   buf += strlen (buf);
3635   e_flags &= ~EF_AMDGPU_MACH;
3636 
3637   if ((osabi == ELFOSABI_AMDGPU_HSA
3638        && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
3639       || osabi != ELFOSABI_AMDGPU_HSA)
3640     {
3641       /* For HSA v3 and other OS ABIs.  */
3642       if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
3643 	{
3644 	  strcat (buf, ", xnack on");
3645 	  buf += strlen (buf);
3646 	  e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
3647 	}
3648 
3649       if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
3650 	{
3651 	  strcat (buf, ", sramecc on");
3652 	  buf += strlen (buf);
3653 	  e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
3654 	}
3655     }
3656   else
3657     {
3658       /* For HSA v4+.  */
3659       int xnack, sramecc;
3660 
3661       xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
3662       switch (xnack)
3663 	{
3664 	case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
3665 	  break;
3666 
3667 	case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
3668 	  strcat (buf, ", xnack any");
3669 	  break;
3670 
3671 	case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
3672 	  strcat (buf, ", xnack off");
3673 	  break;
3674 
3675 	case EF_AMDGPU_FEATURE_XNACK_ON_V4:
3676 	  strcat (buf, ", xnack on");
3677 	  break;
3678 
3679 	default:
3680 	  sprintf (buf, _(", <unknown xnack value: %#x>"), xnack);
3681 	  break;
3682 	}
3683 
3684       buf += strlen (buf);
3685       e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
3686 
3687       sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
3688       switch (sramecc)
3689 	{
3690 	case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
3691 	  break;
3692 
3693 	case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
3694 	  strcat (buf, ", sramecc any");
3695 	  break;
3696 
3697 	case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
3698 	  strcat (buf, ", sramecc off");
3699 	  break;
3700 
3701 	case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
3702 	  strcat (buf, ", sramecc on");
3703 	  break;
3704 
3705 	default:
3706 	  sprintf (buf, _(", <unknown sramecc value: %#x>"), sramecc);
3707 	  break;
3708 	}
3709 
3710       buf += strlen (buf);
3711       e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
3712     }
3713 
3714   if (e_flags != 0)
3715     sprintf (buf, _(", unknown flags bits: %#x"), e_flags);
3716 }
3717 
3718 static char *
3719 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3720 {
3721   static char buf[1024];
3722 
3723   buf[0] = '\0';
3724 
3725   if (e_flags)
3726     {
3727       switch (e_machine)
3728 	{
3729 	default:
3730 	  break;
3731 
3732 	case EM_ARC_COMPACT2:
3733 	case EM_ARC_COMPACT:
3734           decode_ARC_machine_flags (e_flags, e_machine, buf);
3735           break;
3736 
3737 	case EM_ARM:
3738 	  decode_ARM_machine_flags (e_flags, buf);
3739 	  break;
3740 
3741         case EM_AVR:
3742           decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3743           break;
3744 
3745 	case EM_BLACKFIN:
3746 	  if (e_flags & EF_BFIN_PIC)
3747 	    strcat (buf, ", PIC");
3748 
3749 	  if (e_flags & EF_BFIN_FDPIC)
3750 	    strcat (buf, ", FDPIC");
3751 
3752 	  if (e_flags & EF_BFIN_CODE_IN_L1)
3753 	    strcat (buf, ", code in L1");
3754 
3755 	  if (e_flags & EF_BFIN_DATA_IN_L1)
3756 	    strcat (buf, ", data in L1");
3757 
3758 	  break;
3759 
3760 	case EM_CYGNUS_FRV:
3761 	  switch (e_flags & EF_FRV_CPU_MASK)
3762 	    {
3763 	    case EF_FRV_CPU_GENERIC:
3764 	      break;
3765 
3766 	    default:
3767 	      strcat (buf, ", fr???");
3768 	      break;
3769 
3770 	    case EF_FRV_CPU_FR300:
3771 	      strcat (buf, ", fr300");
3772 	      break;
3773 
3774 	    case EF_FRV_CPU_FR400:
3775 	      strcat (buf, ", fr400");
3776 	      break;
3777 	    case EF_FRV_CPU_FR405:
3778 	      strcat (buf, ", fr405");
3779 	      break;
3780 
3781 	    case EF_FRV_CPU_FR450:
3782 	      strcat (buf, ", fr450");
3783 	      break;
3784 
3785 	    case EF_FRV_CPU_FR500:
3786 	      strcat (buf, ", fr500");
3787 	      break;
3788 	    case EF_FRV_CPU_FR550:
3789 	      strcat (buf, ", fr550");
3790 	      break;
3791 
3792 	    case EF_FRV_CPU_SIMPLE:
3793 	      strcat (buf, ", simple");
3794 	      break;
3795 	    case EF_FRV_CPU_TOMCAT:
3796 	      strcat (buf, ", tomcat");
3797 	      break;
3798 	    }
3799 	  break;
3800 
3801 	case EM_68K:
3802 	  if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3803 	    strcat (buf, ", m68000");
3804 	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3805 	    strcat (buf, ", cpu32");
3806 	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3807 	    strcat (buf, ", fido_a");
3808 	  else
3809 	    {
3810 	      char const * isa = _("unknown");
3811 	      char const * mac = _("unknown mac");
3812 	      char const * additional = NULL;
3813 
3814 	      switch (e_flags & EF_M68K_CF_ISA_MASK)
3815 		{
3816 		case EF_M68K_CF_ISA_A_NODIV:
3817 		  isa = "A";
3818 		  additional = ", nodiv";
3819 		  break;
3820 		case EF_M68K_CF_ISA_A:
3821 		  isa = "A";
3822 		  break;
3823 		case EF_M68K_CF_ISA_A_PLUS:
3824 		  isa = "A+";
3825 		  break;
3826 		case EF_M68K_CF_ISA_B_NOUSP:
3827 		  isa = "B";
3828 		  additional = ", nousp";
3829 		  break;
3830 		case EF_M68K_CF_ISA_B:
3831 		  isa = "B";
3832 		  break;
3833 		case EF_M68K_CF_ISA_C:
3834 		  isa = "C";
3835 		  break;
3836 		case EF_M68K_CF_ISA_C_NODIV:
3837 		  isa = "C";
3838 		  additional = ", nodiv";
3839 		  break;
3840 		}
3841 	      strcat (buf, ", cf, isa ");
3842 	      strcat (buf, isa);
3843 	      if (additional)
3844 		strcat (buf, additional);
3845 	      if (e_flags & EF_M68K_CF_FLOAT)
3846 		strcat (buf, ", float");
3847 	      switch (e_flags & EF_M68K_CF_MAC_MASK)
3848 		{
3849 		case 0:
3850 		  mac = NULL;
3851 		  break;
3852 		case EF_M68K_CF_MAC:
3853 		  mac = "mac";
3854 		  break;
3855 		case EF_M68K_CF_EMAC:
3856 		  mac = "emac";
3857 		  break;
3858 		case EF_M68K_CF_EMAC_B:
3859 		  mac = "emac_b";
3860 		  break;
3861 		}
3862 	      if (mac)
3863 		{
3864 		  strcat (buf, ", ");
3865 		  strcat (buf, mac);
3866 		}
3867 	    }
3868 	  break;
3869 
3870 	case EM_AMDGPU:
3871 	  decode_AMDGPU_machine_flags (filedata, e_flags, buf);
3872 	  break;
3873 
3874 	case EM_CYGNUS_MEP:
3875 	  switch (e_flags & EF_MEP_CPU_MASK)
3876 	    {
3877 	    case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3878 	    case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3879 	    case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3880 	    case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3881 	    case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3882 	    case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3883 	    default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3884 	    }
3885 
3886 	  switch (e_flags & EF_MEP_COP_MASK)
3887 	    {
3888 	    case EF_MEP_COP_NONE: break;
3889 	    case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3890 	    case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3891 	    case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3892 	    case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3893 	    default: strcat (buf, _("<unknown MeP copro type>")); break;
3894 	    }
3895 
3896 	  if (e_flags & EF_MEP_LIBRARY)
3897 	    strcat (buf, ", Built for Library");
3898 
3899 	  if (e_flags & EF_MEP_INDEX_MASK)
3900 	    sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3901 		     e_flags & EF_MEP_INDEX_MASK);
3902 
3903 	  if (e_flags & ~ EF_MEP_ALL_FLAGS)
3904 	    sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3905 		     e_flags & ~ EF_MEP_ALL_FLAGS);
3906 	  break;
3907 
3908 	case EM_PPC:
3909 	  if (e_flags & EF_PPC_EMB)
3910 	    strcat (buf, ", emb");
3911 
3912 	  if (e_flags & EF_PPC_RELOCATABLE)
3913 	    strcat (buf, _(", relocatable"));
3914 
3915 	  if (e_flags & EF_PPC_RELOCATABLE_LIB)
3916 	    strcat (buf, _(", relocatable-lib"));
3917 	  break;
3918 
3919 	case EM_PPC64:
3920 	  if (e_flags & EF_PPC64_ABI)
3921 	    {
3922 	      char abi[] = ", abiv0";
3923 
3924 	      abi[6] += e_flags & EF_PPC64_ABI;
3925 	      strcat (buf, abi);
3926 	    }
3927 	  break;
3928 
3929 	case EM_V800:
3930 	  if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3931 	    strcat (buf, ", RH850 ABI");
3932 
3933 	  if (e_flags & EF_V800_850E3)
3934 	    strcat (buf, ", V3 architecture");
3935 
3936 	  if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3937 	    strcat (buf, ", FPU not used");
3938 
3939 	  if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3940 	    strcat (buf, ", regmode: COMMON");
3941 
3942 	  if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3943 	    strcat (buf, ", r4 not used");
3944 
3945 	  if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3946 	    strcat (buf, ", r30 not used");
3947 
3948 	  if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3949 	    strcat (buf, ", r5 not used");
3950 
3951 	  if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3952 	    strcat (buf, ", r2 not used");
3953 
3954 	  for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3955 	    {
3956 	      switch (e_flags & - e_flags)
3957 		{
3958 		case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3959 		case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3960 		case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3961 		case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3962 		case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3963 		case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3964 		case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3965 		case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3966 		case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3967 		case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3968 		case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3969 		case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3970 		default: break;
3971 		}
3972 	    }
3973 	  break;
3974 
3975 	case EM_V850:
3976 	case EM_CYGNUS_V850:
3977 	  switch (e_flags & EF_V850_ARCH)
3978 	    {
3979 	    case E_V850E3V5_ARCH:
3980 	      strcat (buf, ", v850e3v5");
3981 	      break;
3982 	    case E_V850E2V3_ARCH:
3983 	      strcat (buf, ", v850e2v3");
3984 	      break;
3985 	    case E_V850E2_ARCH:
3986 	      strcat (buf, ", v850e2");
3987 	      break;
3988             case E_V850E1_ARCH:
3989               strcat (buf, ", v850e1");
3990 	      break;
3991 	    case E_V850E_ARCH:
3992 	      strcat (buf, ", v850e");
3993 	      break;
3994 	    case E_V850_ARCH:
3995 	      strcat (buf, ", v850");
3996 	      break;
3997 	    default:
3998 	      strcat (buf, _(", unknown v850 architecture variant"));
3999 	      break;
4000 	    }
4001 	  break;
4002 
4003 	case EM_M32R:
4004 	case EM_CYGNUS_M32R:
4005 	  if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4006 	    strcat (buf, ", m32r");
4007 	  break;
4008 
4009 	case EM_MIPS:
4010 	case EM_MIPS_RS3_LE:
4011 	  if (e_flags & EF_MIPS_NOREORDER)
4012 	    strcat (buf, ", noreorder");
4013 
4014 	  if (e_flags & EF_MIPS_PIC)
4015 	    strcat (buf, ", pic");
4016 
4017 	  if (e_flags & EF_MIPS_CPIC)
4018 	    strcat (buf, ", cpic");
4019 
4020 	  if (e_flags & EF_MIPS_UCODE)
4021 	    strcat (buf, ", ugen_reserved");
4022 
4023 	  if (e_flags & EF_MIPS_ABI2)
4024 	    strcat (buf, ", abi2");
4025 
4026 	  if (e_flags & EF_MIPS_OPTIONS_FIRST)
4027 	    strcat (buf, ", odk first");
4028 
4029 	  if (e_flags & EF_MIPS_32BITMODE)
4030 	    strcat (buf, ", 32bitmode");
4031 
4032 	  if (e_flags & EF_MIPS_NAN2008)
4033 	    strcat (buf, ", nan2008");
4034 
4035 	  if (e_flags & EF_MIPS_FP64)
4036 	    strcat (buf, ", fp64");
4037 
4038 	  switch ((e_flags & EF_MIPS_MACH))
4039 	    {
4040 	    case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
4041 	    case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
4042 	    case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
4043 	    case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
4044 	    case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
4045 	    case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
4046 	    case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
4047 	    case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
4048 	    case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
4049 	    case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
4050 	    case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
4051   	    case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
4052   	    case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
4053 	    case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
4054 	    case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
4055 	    case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
4056 	    case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
4057 	    case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
4058 	    case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
4059 	    case E_MIPS_MACH_XLR:  strcat (buf, ", xlr"); break;
4060 	    case E_MIPS_MACH_IAMR2:  strcat (buf, ", interaptiv-mr2"); break;
4061 	    case 0:
4062 	    /* We simply ignore the field in this case to avoid confusion:
4063 	       MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4064 	       extension.  */
4065 	      break;
4066 	    default: strcat (buf, _(", unknown CPU")); break;
4067 	    }
4068 
4069 	  switch ((e_flags & EF_MIPS_ABI))
4070 	    {
4071 	    case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
4072 	    case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
4073 	    case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
4074 	    case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
4075 	    case 0:
4076 	    /* We simply ignore the field in this case to avoid confusion:
4077 	       MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4078 	       This means it is likely to be an o32 file, but not for
4079 	       sure.  */
4080 	      break;
4081 	    default: strcat (buf, _(", unknown ABI")); break;
4082 	    }
4083 
4084 	  if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4085 	    strcat (buf, ", mdmx");
4086 
4087 	  if (e_flags & EF_MIPS_ARCH_ASE_M16)
4088 	    strcat (buf, ", mips16");
4089 
4090 	  if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4091 	    strcat (buf, ", micromips");
4092 
4093 	  switch ((e_flags & EF_MIPS_ARCH))
4094 	    {
4095 	    case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
4096 	    case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
4097 	    case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
4098 	    case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
4099 	    case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
4100 	    case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
4101 	    case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
4102 	    case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
4103 	    case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
4104 	    case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
4105 	    case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
4106 	    default: strcat (buf, _(", unknown ISA")); break;
4107 	    }
4108 	  break;
4109 
4110 	case EM_NDS32:
4111 	  decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
4112 	  break;
4113 
4114 	case EM_NFP:
4115 	  switch (EF_NFP_MACH (e_flags))
4116 	    {
4117 	    case E_NFP_MACH_3200:
4118 	      strcat (buf, ", NFP-32xx");
4119 	      break;
4120 	    case E_NFP_MACH_6000:
4121 	      strcat (buf, ", NFP-6xxx");
4122 	      break;
4123 	    }
4124 	  break;
4125 
4126 	case EM_RISCV:
4127 	  if (e_flags & EF_RISCV_RVC)
4128 	    strcat (buf, ", RVC");
4129 
4130 	  if (e_flags & EF_RISCV_RVE)
4131 	    strcat (buf, ", RVE");
4132 
4133 	  switch (e_flags & EF_RISCV_FLOAT_ABI)
4134 	    {
4135 	    case EF_RISCV_FLOAT_ABI_SOFT:
4136 	      strcat (buf, ", soft-float ABI");
4137 	      break;
4138 
4139 	    case EF_RISCV_FLOAT_ABI_SINGLE:
4140 	      strcat (buf, ", single-float ABI");
4141 	      break;
4142 
4143 	    case EF_RISCV_FLOAT_ABI_DOUBLE:
4144 	      strcat (buf, ", double-float ABI");
4145 	      break;
4146 
4147 	    case EF_RISCV_FLOAT_ABI_QUAD:
4148 	      strcat (buf, ", quad-float ABI");
4149 	      break;
4150 	    }
4151 	  break;
4152 
4153 	case EM_SH:
4154 	  switch ((e_flags & EF_SH_MACH_MASK))
4155 	    {
4156 	    case EF_SH1: strcat (buf, ", sh1"); break;
4157 	    case EF_SH2: strcat (buf, ", sh2"); break;
4158 	    case EF_SH3: strcat (buf, ", sh3"); break;
4159 	    case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
4160 	    case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
4161 	    case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
4162 	    case EF_SH3E: strcat (buf, ", sh3e"); break;
4163 	    case EF_SH4: strcat (buf, ", sh4"); break;
4164 	    case EF_SH5: strcat (buf, ", sh5"); break;
4165 	    case EF_SH2E: strcat (buf, ", sh2e"); break;
4166 	    case EF_SH4A: strcat (buf, ", sh4a"); break;
4167 	    case EF_SH2A: strcat (buf, ", sh2a"); break;
4168 	    case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
4169 	    case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
4170 	    case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
4171 	    case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
4172 	    case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
4173 	    case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
4174 	    case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
4175 	    case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
4176 	    case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
4177 	    default: strcat (buf, _(", unknown ISA")); break;
4178 	    }
4179 
4180 	  if (e_flags & EF_SH_PIC)
4181 	    strcat (buf, ", pic");
4182 
4183 	  if (e_flags & EF_SH_FDPIC)
4184 	    strcat (buf, ", fdpic");
4185 	  break;
4186 
4187         case EM_OR1K:
4188           if (e_flags & EF_OR1K_NODELAY)
4189             strcat (buf, ", no delay");
4190           break;
4191 
4192 	case EM_SPARCV9:
4193 	  if (e_flags & EF_SPARC_32PLUS)
4194 	    strcat (buf, ", v8+");
4195 
4196 	  if (e_flags & EF_SPARC_SUN_US1)
4197 	    strcat (buf, ", ultrasparcI");
4198 
4199 	  if (e_flags & EF_SPARC_SUN_US3)
4200 	    strcat (buf, ", ultrasparcIII");
4201 
4202 	  if (e_flags & EF_SPARC_HAL_R1)
4203 	    strcat (buf, ", halr1");
4204 
4205 	  if (e_flags & EF_SPARC_LEDATA)
4206 	    strcat (buf, ", ledata");
4207 
4208 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4209 	    strcat (buf, ", tso");
4210 
4211 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4212 	    strcat (buf, ", pso");
4213 
4214 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4215 	    strcat (buf, ", rmo");
4216 	  break;
4217 
4218 	case EM_PARISC:
4219 	  switch (e_flags & EF_PARISC_ARCH)
4220 	    {
4221 	    case EFA_PARISC_1_0:
4222 	      strcpy (buf, ", PA-RISC 1.0");
4223 	      break;
4224 	    case EFA_PARISC_1_1:
4225 	      strcpy (buf, ", PA-RISC 1.1");
4226 	      break;
4227 	    case EFA_PARISC_2_0:
4228 	      strcpy (buf, ", PA-RISC 2.0");
4229 	      break;
4230 	    default:
4231 	      break;
4232 	    }
4233 	  if (e_flags & EF_PARISC_TRAPNIL)
4234 	    strcat (buf, ", trapnil");
4235 	  if (e_flags & EF_PARISC_EXT)
4236 	    strcat (buf, ", ext");
4237 	  if (e_flags & EF_PARISC_LSB)
4238 	    strcat (buf, ", lsb");
4239 	  if (e_flags & EF_PARISC_WIDE)
4240 	    strcat (buf, ", wide");
4241 	  if (e_flags & EF_PARISC_NO_KABP)
4242 	    strcat (buf, ", no kabp");
4243 	  if (e_flags & EF_PARISC_LAZYSWAP)
4244 	    strcat (buf, ", lazyswap");
4245 	  break;
4246 
4247 	case EM_PJ:
4248 	case EM_PJ_OLD:
4249 	  if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4250 	    strcat (buf, ", new calling convention");
4251 
4252 	  if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4253 	    strcat (buf, ", gnu calling convention");
4254 	  break;
4255 
4256 	case EM_IA_64:
4257 	  if ((e_flags & EF_IA_64_ABI64))
4258 	    strcat (buf, ", 64-bit");
4259 	  else
4260 	    strcat (buf, ", 32-bit");
4261 	  if ((e_flags & EF_IA_64_REDUCEDFP))
4262 	    strcat (buf, ", reduced fp model");
4263 	  if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4264 	    strcat (buf, ", no function descriptors, constant gp");
4265 	  else if ((e_flags & EF_IA_64_CONS_GP))
4266 	    strcat (buf, ", constant gp");
4267 	  if ((e_flags & EF_IA_64_ABSOLUTE))
4268 	    strcat (buf, ", absolute");
4269           if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4270             {
4271               if ((e_flags & EF_IA_64_VMS_LINKAGES))
4272                 strcat (buf, ", vms_linkages");
4273               switch ((e_flags & EF_IA_64_VMS_COMCOD))
4274                 {
4275                 case EF_IA_64_VMS_COMCOD_SUCCESS:
4276                   break;
4277                 case EF_IA_64_VMS_COMCOD_WARNING:
4278                   strcat (buf, ", warning");
4279                   break;
4280                 case EF_IA_64_VMS_COMCOD_ERROR:
4281                   strcat (buf, ", error");
4282                   break;
4283                 case EF_IA_64_VMS_COMCOD_ABORT:
4284                   strcat (buf, ", abort");
4285                   break;
4286                 default:
4287 		  warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4288 			e_flags & EF_IA_64_VMS_COMCOD);
4289 		  strcat (buf, ", <unknown>");
4290                 }
4291             }
4292 	  break;
4293 
4294 	case EM_VAX:
4295 	  if ((e_flags & EF_VAX_NONPIC))
4296 	    strcat (buf, ", non-PIC");
4297 	  if ((e_flags & EF_VAX_DFLOAT))
4298 	    strcat (buf, ", D-Float");
4299 	  if ((e_flags & EF_VAX_GFLOAT))
4300 	    strcat (buf, ", G-Float");
4301 	  break;
4302 
4303         case EM_VISIUM:
4304 	  if (e_flags & EF_VISIUM_ARCH_MCM)
4305 	    strcat (buf, ", mcm");
4306 	  else if (e_flags & EF_VISIUM_ARCH_MCM24)
4307 	    strcat (buf, ", mcm24");
4308 	  if (e_flags & EF_VISIUM_ARCH_GR6)
4309 	    strcat (buf, ", gr6");
4310 	  break;
4311 
4312 	case EM_RL78:
4313 	  switch (e_flags & E_FLAG_RL78_CPU_MASK)
4314 	    {
4315 	    case E_FLAG_RL78_ANY_CPU: break;
4316 	    case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
4317 	    case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
4318 	    case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
4319 	    }
4320 	  if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4321 	    strcat (buf, ", 64-bit doubles");
4322 	  break;
4323 
4324 	case EM_RX:
4325 	  if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4326 	    strcat (buf, ", 64-bit doubles");
4327 	  if (e_flags & E_FLAG_RX_DSP)
4328 	    strcat (buf, ", dsp");
4329 	  if (e_flags & E_FLAG_RX_PID)
4330 	    strcat (buf, ", pid");
4331 	  if (e_flags & E_FLAG_RX_ABI)
4332 	    strcat (buf, ", RX ABI");
4333 	  if (e_flags & E_FLAG_RX_SINSNS_SET)
4334 	    strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
4335 		    ? ", uses String instructions" : ", bans String instructions");
4336 	  if (e_flags & E_FLAG_RX_V2)
4337 	    strcat (buf, ", V2");
4338 	  if (e_flags & E_FLAG_RX_V3)
4339 	    strcat (buf, ", V3");
4340 	  break;
4341 
4342 	case EM_S390:
4343 	  if (e_flags & EF_S390_HIGH_GPRS)
4344 	    strcat (buf, ", highgprs");
4345 	  break;
4346 
4347 	case EM_TI_C6000:
4348 	  if ((e_flags & EF_C6000_REL))
4349 	    strcat (buf, ", relocatable module");
4350 	  break;
4351 
4352 	case EM_MSP430:
4353 	  strcat (buf, _(": architecture variant: "));
4354 	  switch (e_flags & EF_MSP430_MACH)
4355 	    {
4356 	    case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
4357 	    case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
4358 	    case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
4359 	    case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
4360 	    case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
4361 	    case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
4362 	    case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
4363 	    case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
4364 	    case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
4365 	    case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
4366 	    case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
4367 	    case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
4368 	    case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
4369 	    case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
4370 	    case E_MSP430_MACH_MSP430X  : strcat (buf, "MSP430X"); break;
4371 	    default:
4372 	      strcat (buf, _(": unknown")); break;
4373 	    }
4374 
4375 	  if (e_flags & ~ EF_MSP430_MACH)
4376 	    strcat (buf, _(": unknown extra flag bits also present"));
4377 	  break;
4378 
4379 	case EM_Z80:
4380 	  switch (e_flags & EF_Z80_MACH_MSK)
4381 	    {
4382 	    case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
4383 	    case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
4384 	    case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
4385 	    case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
4386 	    case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
4387 	    case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
4388 	    case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
4389 	    default:
4390 	      strcat (buf, _(", unknown")); break;
4391 	    }
4392 	  break;
4393 	case EM_LOONGARCH:
4394 	  if (EF_LOONGARCH_IS_LP64 (e_flags))
4395 	    strcat (buf, ", LP64");
4396 	  else if (EF_LOONGARCH_IS_ILP32 (e_flags))
4397 	    strcat (buf, ", ILP32");
4398 
4399 	  if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4400 	    strcat (buf, ", SOFT-FLOAT");
4401 	  else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4402 	    strcat (buf, ", SINGLE-FLOAT");
4403 	  else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4404 	    strcat (buf, ", DOUBLE-FLOAT");
4405 
4406 	  break;
4407 	}
4408     }
4409 
4410   return buf;
4411 }
4412 
4413 static const char *
4414 get_osabi_name (Filedata * filedata, unsigned int osabi)
4415 {
4416   static char buff[32];
4417 
4418   switch (osabi)
4419     {
4420     case ELFOSABI_NONE:		return "UNIX - System V";
4421     case ELFOSABI_HPUX:		return "UNIX - HP-UX";
4422     case ELFOSABI_NETBSD:	return "UNIX - NetBSD";
4423     case ELFOSABI_GNU:		return "UNIX - GNU";
4424     case ELFOSABI_SOLARIS:	return "UNIX - Solaris";
4425     case ELFOSABI_AIX:		return "UNIX - AIX";
4426     case ELFOSABI_IRIX:		return "UNIX - IRIX";
4427     case ELFOSABI_FREEBSD:	return "UNIX - FreeBSD";
4428     case ELFOSABI_TRU64:	return "UNIX - TRU64";
4429     case ELFOSABI_MODESTO:	return "Novell - Modesto";
4430     case ELFOSABI_OPENBSD:	return "UNIX - OpenBSD";
4431     case ELFOSABI_OPENVMS:	return "VMS - OpenVMS";
4432     case ELFOSABI_NSK:		return "HP - Non-Stop Kernel";
4433     case ELFOSABI_AROS:		return "AROS";
4434     case ELFOSABI_FENIXOS:	return "FenixOS";
4435     case ELFOSABI_CLOUDABI:	return "Nuxi CloudABI";
4436     case ELFOSABI_OPENVOS:	return "Stratus Technologies OpenVOS";
4437     default:
4438       if (osabi >= 64)
4439 	switch (filedata->file_header.e_machine)
4440 	  {
4441 	  case EM_AMDGPU:
4442 	    switch (osabi)
4443 	      {
4444 	      case ELFOSABI_AMDGPU_HSA:    return "AMD HSA";
4445 	      case ELFOSABI_AMDGPU_PAL:    return "AMD PAL";
4446 	      case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4447 	      default:
4448 		break;
4449 	      }
4450 	    break;
4451 
4452 	  case EM_ARM:
4453 	    switch (osabi)
4454 	      {
4455 	      case ELFOSABI_ARM:	return "ARM";
4456 	      case ELFOSABI_ARM_FDPIC:	return "ARM FDPIC";
4457 	      default:
4458 		break;
4459 	      }
4460 	    break;
4461 
4462 	  case EM_MSP430:
4463 	  case EM_MSP430_OLD:
4464 	  case EM_VISIUM:
4465 	    switch (osabi)
4466 	      {
4467 	      case ELFOSABI_STANDALONE:	return _("Standalone App");
4468 	      default:
4469 		break;
4470 	      }
4471 	    break;
4472 
4473 	  case EM_TI_C6000:
4474 	    switch (osabi)
4475 	      {
4476 	      case ELFOSABI_C6000_ELFABI:	return _("Bare-metal C6000");
4477 	      case ELFOSABI_C6000_LINUX:	return "Linux C6000";
4478 	      default:
4479 		break;
4480 	      }
4481 	    break;
4482 
4483 	  default:
4484 	    break;
4485 	  }
4486       snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4487       return buff;
4488     }
4489 }
4490 
4491 static const char *
4492 get_aarch64_segment_type (unsigned long type)
4493 {
4494   switch (type)
4495     {
4496     case PT_AARCH64_ARCHEXT:  return "AARCH64_ARCHEXT";
4497     default:                  return NULL;
4498     }
4499 }
4500 
4501 static const char *
4502 get_arm_segment_type (unsigned long type)
4503 {
4504   switch (type)
4505     {
4506     case PT_ARM_EXIDX: return "EXIDX";
4507     default:           return NULL;
4508     }
4509 }
4510 
4511 static const char *
4512 get_s390_segment_type (unsigned long type)
4513 {
4514   switch (type)
4515     {
4516     case PT_S390_PGSTE: return "S390_PGSTE";
4517     default:            return NULL;
4518     }
4519 }
4520 
4521 static const char *
4522 get_mips_segment_type (unsigned long type)
4523 {
4524   switch (type)
4525     {
4526     case PT_MIPS_REGINFO:   return "REGINFO";
4527     case PT_MIPS_RTPROC:    return "RTPROC";
4528     case PT_MIPS_OPTIONS:   return "OPTIONS";
4529     case PT_MIPS_ABIFLAGS:  return "ABIFLAGS";
4530     default:                return NULL;
4531     }
4532 }
4533 
4534 static const char *
4535 get_parisc_segment_type (unsigned long type)
4536 {
4537   switch (type)
4538     {
4539     case PT_PARISC_ARCHEXT:	return "PARISC_ARCHEXT";
4540     case PT_PARISC_UNWIND:	return "PARISC_UNWIND";
4541     case PT_PARISC_WEAKORDER:	return "PARISC_WEAKORDER";
4542     default:                    return NULL;
4543     }
4544 }
4545 
4546 static const char *
4547 get_ia64_segment_type (unsigned long type)
4548 {
4549   switch (type)
4550     {
4551     case PT_IA_64_ARCHEXT:	return "IA_64_ARCHEXT";
4552     case PT_IA_64_UNWIND:	return "IA_64_UNWIND";
4553     default:                    return NULL;
4554     }
4555 }
4556 
4557 static const char *
4558 get_tic6x_segment_type (unsigned long type)
4559 {
4560   switch (type)
4561     {
4562     case PT_C6000_PHATTR:  return "C6000_PHATTR";
4563     default:               return NULL;
4564     }
4565 }
4566 
4567 static const char *
4568 get_riscv_segment_type (unsigned long type)
4569 {
4570   switch (type)
4571     {
4572     case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4573     default:                  return NULL;
4574     }
4575 }
4576 
4577 static const char *
4578 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4579 {
4580   if (e_machine == EM_PARISC)
4581     switch (type)
4582       {
4583       case PT_HP_TLS:		return "HP_TLS";
4584       case PT_HP_CORE_NONE:	return "HP_CORE_NONE";
4585       case PT_HP_CORE_VERSION:	return "HP_CORE_VERSION";
4586       case PT_HP_CORE_KERNEL:	return "HP_CORE_KERNEL";
4587       case PT_HP_CORE_COMM:	return "HP_CORE_COMM";
4588       case PT_HP_CORE_PROC:	return "HP_CORE_PROC";
4589       case PT_HP_CORE_LOADABLE:	return "HP_CORE_LOADABLE";
4590       case PT_HP_CORE_STACK:	return "HP_CORE_STACK";
4591       case PT_HP_CORE_SHM:	return "HP_CORE_SHM";
4592       case PT_HP_CORE_MMF:	return "HP_CORE_MMF";
4593       case PT_HP_PARALLEL:	return "HP_PARALLEL";
4594       case PT_HP_FASTBIND:	return "HP_FASTBIND";
4595       case PT_HP_OPT_ANNOT:	return "HP_OPT_ANNOT";
4596       case PT_HP_HSL_ANNOT:	return "HP_HSL_ANNOT";
4597       case PT_HP_STACK:		return "HP_STACK";
4598       case PT_HP_CORE_UTSNAME:	return "HP_CORE_UTSNAME";
4599       default:			return NULL;
4600       }
4601 
4602   if (e_machine == EM_IA_64)
4603     switch (type)
4604       {
4605       case PT_HP_TLS:		 return "HP_TLS";
4606       case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4607       case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4608       case PT_IA_64_HP_STACK:	 return "HP_STACK";
4609       default:			 return NULL;
4610       }
4611 
4612   return NULL;
4613 }
4614 
4615 static const char *
4616 get_solaris_segment_type (unsigned long type)
4617 {
4618   switch (type)
4619     {
4620     case 0x6464e550: return "PT_SUNW_UNWIND";
4621     case 0x6474e550: return "PT_SUNW_EH_FRAME";
4622     case 0x6ffffff7: return "PT_LOSUNW";
4623     case 0x6ffffffa: return "PT_SUNWBSS";
4624     case 0x6ffffffb: return "PT_SUNWSTACK";
4625     case 0x6ffffffc: return "PT_SUNWDTRACE";
4626     case 0x6ffffffd: return "PT_SUNWCAP";
4627     case 0x6fffffff: return "PT_HISUNW";
4628     default:         return NULL;
4629     }
4630 }
4631 
4632 static const char *
4633 get_segment_type (Filedata * filedata, unsigned long p_type)
4634 {
4635   static char buff[32];
4636 
4637   switch (p_type)
4638     {
4639     case PT_NULL:	return "NULL";
4640     case PT_LOAD:	return "LOAD";
4641     case PT_DYNAMIC:	return "DYNAMIC";
4642     case PT_INTERP:	return "INTERP";
4643     case PT_NOTE:	return "NOTE";
4644     case PT_SHLIB:	return "SHLIB";
4645     case PT_PHDR:	return "PHDR";
4646     case PT_TLS:	return "TLS";
4647     case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4648     case PT_GNU_STACK:	return "GNU_STACK";
4649     case PT_GNU_RELRO:  return "GNU_RELRO";
4650     case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4651 
4652     case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4653     case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4654     case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4655 
4656     default:
4657       if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4658 	{
4659 	  const char * result;
4660 
4661 	  switch (filedata->file_header.e_machine)
4662 	    {
4663 	    case EM_AARCH64:
4664 	      result = get_aarch64_segment_type (p_type);
4665 	      break;
4666 	    case EM_ARM:
4667 	      result = get_arm_segment_type (p_type);
4668 	      break;
4669 	    case EM_MIPS:
4670 	    case EM_MIPS_RS3_LE:
4671 	      result = get_mips_segment_type (p_type);
4672 	      break;
4673 	    case EM_PARISC:
4674 	      result = get_parisc_segment_type (p_type);
4675 	      break;
4676 	    case EM_IA_64:
4677 	      result = get_ia64_segment_type (p_type);
4678 	      break;
4679 	    case EM_TI_C6000:
4680 	      result = get_tic6x_segment_type (p_type);
4681 	      break;
4682 	    case EM_S390:
4683 	    case EM_S390_OLD:
4684 	      result = get_s390_segment_type (p_type);
4685 	      break;
4686 	    case EM_RISCV:
4687 	      result = get_riscv_segment_type (p_type);
4688 	      break;
4689 	    default:
4690 	      result = NULL;
4691 	      break;
4692 	    }
4693 
4694 	  if (result != NULL)
4695 	    return result;
4696 
4697 	  sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4698 	}
4699       else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4700 	{
4701 	  const char * result = NULL;
4702 
4703 	  switch (filedata->file_header.e_ident[EI_OSABI])
4704 	    {
4705 	    case ELFOSABI_GNU:
4706 	    case ELFOSABI_FREEBSD:
4707 	      if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4708 		{
4709 		  sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4710 		  result = buff;
4711 		}
4712 	      break;
4713 	    case ELFOSABI_HPUX:
4714 	      result = get_hpux_segment_type (p_type,
4715 					      filedata->file_header.e_machine);
4716 	      break;
4717 	    case ELFOSABI_SOLARIS:
4718 	      result = get_solaris_segment_type (p_type);
4719 	      break;
4720 	    default:
4721 	      break;
4722 	    }
4723 	  if (result != NULL)
4724 	    return result;
4725 
4726 	  sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4727 	}
4728       else
4729 	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4730 
4731       return buff;
4732     }
4733 }
4734 
4735 static const char *
4736 get_arc_section_type_name (unsigned int sh_type)
4737 {
4738   switch (sh_type)
4739     {
4740     case SHT_ARC_ATTRIBUTES:      return "ARC_ATTRIBUTES";
4741     default:
4742       break;
4743     }
4744   return NULL;
4745 }
4746 
4747 static const char *
4748 get_mips_section_type_name (unsigned int sh_type)
4749 {
4750   switch (sh_type)
4751     {
4752     case SHT_MIPS_LIBLIST:	 return "MIPS_LIBLIST";
4753     case SHT_MIPS_MSYM:		 return "MIPS_MSYM";
4754     case SHT_MIPS_CONFLICT:	 return "MIPS_CONFLICT";
4755     case SHT_MIPS_GPTAB:	 return "MIPS_GPTAB";
4756     case SHT_MIPS_UCODE:	 return "MIPS_UCODE";
4757     case SHT_MIPS_DEBUG:	 return "MIPS_DEBUG";
4758     case SHT_MIPS_REGINFO:	 return "MIPS_REGINFO";
4759     case SHT_MIPS_PACKAGE:	 return "MIPS_PACKAGE";
4760     case SHT_MIPS_PACKSYM:	 return "MIPS_PACKSYM";
4761     case SHT_MIPS_RELD:		 return "MIPS_RELD";
4762     case SHT_MIPS_IFACE:	 return "MIPS_IFACE";
4763     case SHT_MIPS_CONTENT:	 return "MIPS_CONTENT";
4764     case SHT_MIPS_OPTIONS:	 return "MIPS_OPTIONS";
4765     case SHT_MIPS_SHDR:		 return "MIPS_SHDR";
4766     case SHT_MIPS_FDESC:	 return "MIPS_FDESC";
4767     case SHT_MIPS_EXTSYM:	 return "MIPS_EXTSYM";
4768     case SHT_MIPS_DENSE:	 return "MIPS_DENSE";
4769     case SHT_MIPS_PDESC:	 return "MIPS_PDESC";
4770     case SHT_MIPS_LOCSYM:	 return "MIPS_LOCSYM";
4771     case SHT_MIPS_AUXSYM:	 return "MIPS_AUXSYM";
4772     case SHT_MIPS_OPTSYM:	 return "MIPS_OPTSYM";
4773     case SHT_MIPS_LOCSTR:	 return "MIPS_LOCSTR";
4774     case SHT_MIPS_LINE:		 return "MIPS_LINE";
4775     case SHT_MIPS_RFDESC:	 return "MIPS_RFDESC";
4776     case SHT_MIPS_DELTASYM:	 return "MIPS_DELTASYM";
4777     case SHT_MIPS_DELTAINST:	 return "MIPS_DELTAINST";
4778     case SHT_MIPS_DELTACLASS:	 return "MIPS_DELTACLASS";
4779     case SHT_MIPS_DWARF:	 return "MIPS_DWARF";
4780     case SHT_MIPS_DELTADECL:	 return "MIPS_DELTADECL";
4781     case SHT_MIPS_SYMBOL_LIB:	 return "MIPS_SYMBOL_LIB";
4782     case SHT_MIPS_EVENTS:	 return "MIPS_EVENTS";
4783     case SHT_MIPS_TRANSLATE:	 return "MIPS_TRANSLATE";
4784     case SHT_MIPS_PIXIE:	 return "MIPS_PIXIE";
4785     case SHT_MIPS_XLATE:	 return "MIPS_XLATE";
4786     case SHT_MIPS_XLATE_DEBUG:	 return "MIPS_XLATE_DEBUG";
4787     case SHT_MIPS_WHIRL:	 return "MIPS_WHIRL";
4788     case SHT_MIPS_EH_REGION:	 return "MIPS_EH_REGION";
4789     case SHT_MIPS_XLATE_OLD:	 return "MIPS_XLATE_OLD";
4790     case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4791     case SHT_MIPS_ABIFLAGS:	 return "MIPS_ABIFLAGS";
4792     case SHT_MIPS_XHASH:	 return "MIPS_XHASH";
4793     default:
4794       break;
4795     }
4796   return NULL;
4797 }
4798 
4799 static const char *
4800 get_parisc_section_type_name (unsigned int sh_type)
4801 {
4802   switch (sh_type)
4803     {
4804     case SHT_PARISC_EXT:	return "PARISC_EXT";
4805     case SHT_PARISC_UNWIND:	return "PARISC_UNWIND";
4806     case SHT_PARISC_DOC:	return "PARISC_DOC";
4807     case SHT_PARISC_ANNOT:	return "PARISC_ANNOT";
4808     case SHT_PARISC_SYMEXTN:	return "PARISC_SYMEXTN";
4809     case SHT_PARISC_STUBS:	return "PARISC_STUBS";
4810     case SHT_PARISC_DLKM:	return "PARISC_DLKM";
4811     default:             	return NULL;
4812     }
4813 }
4814 
4815 static const char *
4816 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4817 {
4818   /* If the top 8 bits are 0x78 the next 8 are the os/abi ID.  */
4819   if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4820     return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4821 
4822   switch (sh_type)
4823     {
4824     case SHT_IA_64_EXT:		       return "IA_64_EXT";
4825     case SHT_IA_64_UNWIND:	       return "IA_64_UNWIND";
4826     case SHT_IA_64_PRIORITY_INIT:      return "IA_64_PRIORITY_INIT";
4827     case SHT_IA_64_VMS_TRACE:          return "VMS_TRACE";
4828     case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4829     case SHT_IA_64_VMS_DEBUG:          return "VMS_DEBUG";
4830     case SHT_IA_64_VMS_DEBUG_STR:      return "VMS_DEBUG_STR";
4831     case SHT_IA_64_VMS_LINKAGES:       return "VMS_LINKAGES";
4832     case SHT_IA_64_VMS_SYMBOL_VECTOR:  return "VMS_SYMBOL_VECTOR";
4833     case SHT_IA_64_VMS_FIXUP:          return "VMS_FIXUP";
4834     default:
4835       break;
4836     }
4837   return NULL;
4838 }
4839 
4840 static const char *
4841 get_x86_64_section_type_name (unsigned int sh_type)
4842 {
4843   switch (sh_type)
4844     {
4845     case SHT_X86_64_UNWIND:	return "X86_64_UNWIND";
4846     default:			return NULL;
4847     }
4848 }
4849 
4850 static const char *
4851 get_aarch64_section_type_name (unsigned int sh_type)
4852 {
4853   switch (sh_type)
4854     {
4855     case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4856     default:			 return NULL;
4857     }
4858 }
4859 
4860 static const char *
4861 get_arm_section_type_name (unsigned int sh_type)
4862 {
4863   switch (sh_type)
4864     {
4865     case SHT_ARM_EXIDX:           return "ARM_EXIDX";
4866     case SHT_ARM_PREEMPTMAP:      return "ARM_PREEMPTMAP";
4867     case SHT_ARM_ATTRIBUTES:      return "ARM_ATTRIBUTES";
4868     case SHT_ARM_DEBUGOVERLAY:    return "ARM_DEBUGOVERLAY";
4869     case SHT_ARM_OVERLAYSECTION:  return "ARM_OVERLAYSECTION";
4870     default:			  return NULL;
4871     }
4872 }
4873 
4874 static const char *
4875 get_tic6x_section_type_name (unsigned int sh_type)
4876 {
4877   switch (sh_type)
4878     {
4879     case SHT_C6000_UNWIND:      return "C6000_UNWIND";
4880     case SHT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
4881     case SHT_C6000_ATTRIBUTES:  return "C6000_ATTRIBUTES";
4882     case SHT_TI_ICODE:          return "TI_ICODE";
4883     case SHT_TI_XREF:           return "TI_XREF";
4884     case SHT_TI_HANDLER:        return "TI_HANDLER";
4885     case SHT_TI_INITINFO:       return "TI_INITINFO";
4886     case SHT_TI_PHATTRS:        return "TI_PHATTRS";
4887     default:                    return NULL;
4888     }
4889 }
4890 
4891 static const char *
4892 get_msp430_section_type_name (unsigned int sh_type)
4893 {
4894   switch (sh_type)
4895     {
4896     case SHT_MSP430_SEC_FLAGS:    return "MSP430_SEC_FLAGS";
4897     case SHT_MSP430_SYM_ALIASES:  return "MSP430_SYM_ALIASES";
4898     case SHT_MSP430_ATTRIBUTES:   return "MSP430_ATTRIBUTES";
4899     default:                      return NULL;
4900     }
4901 }
4902 
4903 static const char *
4904 get_nfp_section_type_name (unsigned int sh_type)
4905 {
4906   switch (sh_type)
4907     {
4908     case SHT_NFP_MECONFIG:	return "NFP_MECONFIG";
4909     case SHT_NFP_INITREG:	return "NFP_INITREG";
4910     case SHT_NFP_UDEBUG:	return "NFP_UDEBUG";
4911     default:			return NULL;
4912     }
4913 }
4914 
4915 static const char *
4916 get_v850_section_type_name (unsigned int sh_type)
4917 {
4918   switch (sh_type)
4919     {
4920     case SHT_V850_SCOMMON:  return "V850 Small Common";
4921     case SHT_V850_TCOMMON:  return "V850 Tiny Common";
4922     case SHT_V850_ZCOMMON:  return "V850 Zero Common";
4923     case SHT_RENESAS_IOP:   return "RENESAS IOP";
4924     case SHT_RENESAS_INFO:  return "RENESAS INFO";
4925     default:                return NULL;
4926     }
4927 }
4928 
4929 static const char *
4930 get_riscv_section_type_name (unsigned int sh_type)
4931 {
4932   switch (sh_type)
4933     {
4934     case SHT_RISCV_ATTRIBUTES:  return "RISCV_ATTRIBUTES";
4935     default: return NULL;
4936     }
4937 }
4938 
4939 static const char *
4940 get_csky_section_type_name (unsigned int sh_type)
4941 {
4942   switch (sh_type)
4943     {
4944     case SHT_CSKY_ATTRIBUTES:  return "CSKY_ATTRIBUTES";
4945     default:  return NULL;
4946     }
4947 }
4948 
4949 static const char *
4950 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4951 {
4952   static char buff[32];
4953   const char * result;
4954 
4955   switch (sh_type)
4956     {
4957     case SHT_NULL:		return "NULL";
4958     case SHT_PROGBITS:		return "PROGBITS";
4959     case SHT_SYMTAB:		return "SYMTAB";
4960     case SHT_STRTAB:		return "STRTAB";
4961     case SHT_RELA:		return "RELA";
4962     case SHT_RELR:		return "RELR";
4963     case SHT_HASH:		return "HASH";
4964     case SHT_DYNAMIC:		return "DYNAMIC";
4965     case SHT_NOTE:		return "NOTE";
4966     case SHT_NOBITS:		return "NOBITS";
4967     case SHT_REL:		return "REL";
4968     case SHT_SHLIB:		return "SHLIB";
4969     case SHT_DYNSYM:		return "DYNSYM";
4970     case SHT_INIT_ARRAY:	return "INIT_ARRAY";
4971     case SHT_FINI_ARRAY:	return "FINI_ARRAY";
4972     case SHT_PREINIT_ARRAY:	return "PREINIT_ARRAY";
4973     case SHT_GNU_HASH:		return "GNU_HASH";
4974     case SHT_GROUP:		return "GROUP";
4975     case SHT_SYMTAB_SHNDX:	return "SYMTAB SECTION INDICES";
4976     case SHT_GNU_verdef:	return "VERDEF";
4977     case SHT_GNU_verneed:	return "VERNEED";
4978     case SHT_GNU_versym:	return "VERSYM";
4979     case 0x6ffffff0:		return "VERSYM";
4980     case 0x6ffffffc:		return "VERDEF";
4981     case 0x7ffffffd:		return "AUXILIARY";
4982     case 0x7fffffff:		return "FILTER";
4983     case SHT_GNU_LIBLIST:	return "GNU_LIBLIST";
4984 
4985     default:
4986       if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4987 	{
4988 	  switch (filedata->file_header.e_machine)
4989 	    {
4990 	    case EM_ARC:
4991 	    case EM_ARC_COMPACT:
4992 	    case EM_ARC_COMPACT2:
4993 	      result = get_arc_section_type_name (sh_type);
4994 	      break;
4995 	    case EM_MIPS:
4996 	    case EM_MIPS_RS3_LE:
4997 	      result = get_mips_section_type_name (sh_type);
4998 	      break;
4999 	    case EM_PARISC:
5000 	      result = get_parisc_section_type_name (sh_type);
5001 	      break;
5002 	    case EM_IA_64:
5003 	      result = get_ia64_section_type_name (filedata, sh_type);
5004 	      break;
5005 	    case EM_X86_64:
5006 	    case EM_L1OM:
5007 	    case EM_K1OM:
5008 	      result = get_x86_64_section_type_name (sh_type);
5009 	      break;
5010 	    case EM_AARCH64:
5011 	      result = get_aarch64_section_type_name (sh_type);
5012 	      break;
5013 	    case EM_ARM:
5014 	      result = get_arm_section_type_name (sh_type);
5015 	      break;
5016 	    case EM_TI_C6000:
5017 	      result = get_tic6x_section_type_name (sh_type);
5018 	      break;
5019 	    case EM_MSP430:
5020 	      result = get_msp430_section_type_name (sh_type);
5021 	      break;
5022 	    case EM_NFP:
5023 	      result = get_nfp_section_type_name (sh_type);
5024 	      break;
5025 	    case EM_V800:
5026 	    case EM_V850:
5027 	    case EM_CYGNUS_V850:
5028 	      result = get_v850_section_type_name (sh_type);
5029 	      break;
5030 	    case EM_RISCV:
5031 	      result = get_riscv_section_type_name (sh_type);
5032 	      break;
5033 	    case EM_CSKY:
5034 	      result = get_csky_section_type_name (sh_type);
5035 	      break;
5036 	    default:
5037 	      result = NULL;
5038 	      break;
5039 	    }
5040 
5041 	  if (result != NULL)
5042 	    return result;
5043 
5044 	  sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
5045 	}
5046       else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5047 	{
5048 	  switch (filedata->file_header.e_machine)
5049 	    {
5050 	    case EM_IA_64:
5051 	      result = get_ia64_section_type_name (filedata, sh_type);
5052 	      break;
5053 	    default:
5054 	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5055 		result = get_solaris_section_type (sh_type);
5056 	      else
5057 		{
5058 		  switch (sh_type)
5059 		    {
5060 		    case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5061 		    case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5062 		    case SHT_GNU_HASH: result = "GNU_HASH"; break;
5063 		    case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5064 		    default:
5065 		      result = NULL;
5066 		      break;
5067 		    }
5068 		}
5069 	      break;
5070 	    }
5071 
5072 	  if (result != NULL)
5073 	    return result;
5074 
5075 	  sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5076 	}
5077       else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5078 	{
5079 	  switch (filedata->file_header.e_machine)
5080 	    {
5081 	    case EM_V800:
5082 	    case EM_V850:
5083 	    case EM_CYGNUS_V850:
5084 	      result = get_v850_section_type_name (sh_type);
5085 	      break;
5086 	    default:
5087 	      result = NULL;
5088 	      break;
5089 	    }
5090 
5091 	  if (result != NULL)
5092 	    return result;
5093 
5094 	  sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5095 	}
5096       else
5097 	/* This message is probably going to be displayed in a 15
5098 	   character wide field, so put the hex value first.  */
5099 	snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5100 
5101       return buff;
5102     }
5103 }
5104 
5105 enum long_option_values
5106 {
5107   OPTION_DEBUG_DUMP = 512,
5108   OPTION_DYN_SYMS,
5109   OPTION_LTO_SYMS,
5110   OPTION_DWARF_DEPTH,
5111   OPTION_DWARF_START,
5112   OPTION_DWARF_CHECK,
5113   OPTION_CTF_DUMP,
5114   OPTION_CTF_PARENT,
5115   OPTION_CTF_SYMBOLS,
5116   OPTION_CTF_STRINGS,
5117   OPTION_WITH_SYMBOL_VERSIONS,
5118   OPTION_RECURSE_LIMIT,
5119   OPTION_NO_RECURSE_LIMIT,
5120   OPTION_NO_DEMANGLING,
5121   OPTION_SYM_BASE
5122 };
5123 
5124 static struct option options[] =
5125 {
5126  /* Note - This table is alpha-sorted on the 'val'
5127     field in order to make adding new options easier.  */
5128   {"arch-specific",    no_argument, 0, 'A'},
5129   {"all",	       no_argument, 0, 'a'},
5130   {"demangle",         optional_argument, 0, 'C'},
5131   {"archive-index",    no_argument, 0, 'c'},
5132   {"use-dynamic",      no_argument, 0, 'D'},
5133   {"dynamic",	       no_argument, 0, 'd'},
5134   {"headers",	       no_argument, 0, 'e'},
5135   {"special-files",    no_argument, 0, 'f'},
5136   {"section-groups",   no_argument, 0, 'g'},
5137   {"help",	       no_argument, 0, 'H'},
5138   {"file-header",      no_argument, 0, 'h'},
5139   {"histogram",	       no_argument, 0, 'I'},
5140   {"lint",             no_argument, 0, 'L'},
5141   {"enable-checks",    no_argument, 0, 'L'},
5142   {"program-headers",  no_argument, 0, 'l'},
5143   {"segments",	       no_argument, 0, 'l'},
5144   {"full-section-name",no_argument, 0, 'N'},
5145   {"notes",	       no_argument, 0, 'n'},
5146   {"process-links",    no_argument, 0, 'P'},
5147   {"string-dump",      required_argument, 0, 'p'},
5148   {"relocated-dump",   required_argument, 0, 'R'},
5149   {"relocs",	       no_argument, 0, 'r'},
5150   {"section-headers",  no_argument, 0, 'S'},
5151   {"sections",	       no_argument, 0, 'S'},
5152   {"symbols",	       no_argument, 0, 's'},
5153   {"syms",	       no_argument, 0, 's'},
5154   {"silent-truncation",no_argument, 0, 'T'},
5155   {"section-details",  no_argument, 0, 't'},
5156   {"unicode",          required_argument, NULL, 'U'},
5157   {"unwind",	       no_argument, 0, 'u'},
5158   {"version-info",     no_argument, 0, 'V'},
5159   {"version",	       no_argument, 0, 'v'},
5160   {"wide",	       no_argument, 0, 'W'},
5161   {"hex-dump",	       required_argument, 0, 'x'},
5162   {"decompress",       no_argument, 0, 'z'},
5163 
5164   {"no-demangle",      no_argument, 0, OPTION_NO_DEMANGLING},
5165   {"recurse-limit",    no_argument, NULL, OPTION_RECURSE_LIMIT},
5166   {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5167   {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5168   {"dyn-syms",	       no_argument, 0, OPTION_DYN_SYMS},
5169   {"lto-syms",         no_argument, 0, OPTION_LTO_SYMS},
5170   {"debug-dump",       optional_argument, 0, OPTION_DEBUG_DUMP},
5171   {"dwarf-depth",      required_argument, 0, OPTION_DWARF_DEPTH},
5172   {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
5173   {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
5174 #ifdef ENABLE_LIBCTF
5175   {"ctf",	       required_argument, 0, OPTION_CTF_DUMP},
5176   {"ctf-symbols",      required_argument, 0, OPTION_CTF_SYMBOLS},
5177   {"ctf-strings",      required_argument, 0, OPTION_CTF_STRINGS},
5178   {"ctf-parent",       required_argument, 0, OPTION_CTF_PARENT},
5179 #endif
5180   {"sym-base",	       optional_argument, 0, OPTION_SYM_BASE},
5181 
5182   {0,		       no_argument, 0, 0}
5183 };
5184 
5185 static void
5186 usage (FILE * stream)
5187 {
5188   fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5189   fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5190   fprintf (stream, _(" Options are:\n"));
5191   fprintf (stream, _("\
5192   -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5193   fprintf (stream, _("\
5194   -h --file-header       Display the ELF file header\n"));
5195   fprintf (stream, _("\
5196   -l --program-headers   Display the program headers\n"));
5197   fprintf (stream, _("\
5198      --segments          An alias for --program-headers\n"));
5199   fprintf (stream, _("\
5200   -S --section-headers   Display the sections' header\n"));
5201   fprintf (stream, _("\
5202      --sections          An alias for --section-headers\n"));
5203   fprintf (stream, _("\
5204   -g --section-groups    Display the section groups\n"));
5205   fprintf (stream, _("\
5206   -t --section-details   Display the section details\n"));
5207   fprintf (stream, _("\
5208   -e --headers           Equivalent to: -h -l -S\n"));
5209   fprintf (stream, _("\
5210   -s --syms              Display the symbol table\n"));
5211   fprintf (stream, _("\
5212      --symbols           An alias for --syms\n"));
5213   fprintf (stream, _("\
5214      --dyn-syms          Display the dynamic symbol table\n"));
5215   fprintf (stream, _("\
5216      --lto-syms          Display LTO symbol tables\n"));
5217   fprintf (stream, _("\
5218      --sym-base=[0|8|10|16] \n\
5219                          Force base for symbol sizes.  The options are \n\
5220                          mixed (the default), octal, decimal, hexadecimal.\n"));
5221   fprintf (stream, _("\
5222   -C --demangle[=STYLE]  Decode mangled/processed symbol names\n"));
5223   display_demangler_styles (stream, _("\
5224                            STYLE can be "));
5225   fprintf (stream, _("\
5226      --no-demangle       Do not demangle low-level symbol names.  (default)\n"));
5227   fprintf (stream, _("\
5228      --recurse-limit     Enable a demangling recursion limit.  (default)\n"));
5229   fprintf (stream, _("\
5230      --no-recurse-limit  Disable a demangling recursion limit\n"));
5231   fprintf (stream, _("\
5232      -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5233                          Display unicode characters as determined by the current locale\n\
5234                           (default), escape sequences, \"<hex sequences>\", highlighted\n\
5235                           escape sequences, or treat them as invalid and display as\n\
5236                           \"{hex sequences}\"\n"));
5237   fprintf (stream, _("\
5238   -n --notes             Display the core notes (if present)\n"));
5239   fprintf (stream, _("\
5240   -r --relocs            Display the relocations (if present)\n"));
5241   fprintf (stream, _("\
5242   -u --unwind            Display the unwind info (if present)\n"));
5243   fprintf (stream, _("\
5244   -d --dynamic           Display the dynamic section (if present)\n"));
5245   fprintf (stream, _("\
5246   -V --version-info      Display the version sections (if present)\n"));
5247   fprintf (stream, _("\
5248   -A --arch-specific     Display architecture specific information (if any)\n"));
5249   fprintf (stream, _("\
5250   -c --archive-index     Display the symbol/file index in an archive\n"));
5251   fprintf (stream, _("\
5252   -D --use-dynamic       Use the dynamic section info when displaying symbols\n"));
5253   fprintf (stream, _("\
5254   -L --lint|--enable-checks\n\
5255                          Display warning messages for possible problems\n"));
5256   fprintf (stream, _("\
5257   -x --hex-dump=<number|name>\n\
5258                          Dump the contents of section <number|name> as bytes\n"));
5259   fprintf (stream, _("\
5260   -p --string-dump=<number|name>\n\
5261                          Dump the contents of section <number|name> as strings\n"));
5262   fprintf (stream, _("\
5263   -R --relocated-dump=<number|name>\n\
5264                          Dump the relocated contents of section <number|name>\n"));
5265   fprintf (stream, _("\
5266   -z --decompress        Decompress section before dumping it\n"));
5267   fprintf (stream, _("\
5268   -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5269                   f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5270                   m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5271                   s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5272                   U/=trace_info]\n\
5273                          Display the contents of DWARF debug sections\n"));
5274   fprintf (stream, _("\
5275   -wk --debug-dump=links Display the contents of sections that link to separate\n\
5276                           debuginfo files\n"));
5277   fprintf (stream, _("\
5278   -P --process-links     Display the contents of non-debug sections in separate\n\
5279                           debuginfo files.  (Implies -wK)\n"));
5280 #if DEFAULT_FOR_FOLLOW_LINKS
5281   fprintf (stream, _("\
5282   -wK --debug-dump=follow-links\n\
5283                          Follow links to separate debug info files (default)\n"));
5284   fprintf (stream, _("\
5285   -wN --debug-dump=no-follow-links\n\
5286                          Do not follow links to separate debug info files\n"));
5287 #else
5288   fprintf (stream, _("\
5289   -wK --debug-dump=follow-links\n\
5290                          Follow links to separate debug info files\n"));
5291   fprintf (stream, _("\
5292   -wN --debug-dump=no-follow-links\n\
5293                          Do not follow links to separate debug info files\n\
5294                           (default)\n"));
5295 #endif
5296 #if HAVE_LIBDEBUGINFOD
5297   fprintf (stream, _("\
5298   -wD --debug-dump=use-debuginfod\n\
5299                          When following links, also query debuginfod servers (default)\n"));
5300   fprintf (stream, _("\
5301   -wE --debug-dump=do-not-use-debuginfod\n\
5302                          When following links, do not query debuginfod servers\n"));
5303 #endif
5304   fprintf (stream, _("\
5305   --dwarf-depth=N        Do not display DIEs at depth N or greater\n"));
5306   fprintf (stream, _("\
5307   --dwarf-start=N        Display DIEs starting at offset N\n"));
5308 #ifdef ENABLE_LIBCTF
5309   fprintf (stream, _("\
5310   --ctf=<number|name>    Display CTF info from section <number|name>\n"));
5311   fprintf (stream, _("\
5312   --ctf-parent=<name>    Use CTF archive member <name> as the CTF parent\n"));
5313   fprintf (stream, _("\
5314   --ctf-symbols=<number|name>\n\
5315                          Use section <number|name> as the CTF external symtab\n"));
5316   fprintf (stream, _("\
5317   --ctf-strings=<number|name>\n\
5318                          Use section <number|name> as the CTF external strtab\n"));
5319 #endif
5320 
5321 #ifdef SUPPORT_DISASSEMBLY
5322   fprintf (stream, _("\
5323   -i --instruction-dump=<number|name>\n\
5324                          Disassemble the contents of section <number|name>\n"));
5325 #endif
5326   fprintf (stream, _("\
5327   -I --histogram         Display histogram of bucket list lengths\n"));
5328   fprintf (stream, _("\
5329   -W --wide              Allow output width to exceed 80 characters\n"));
5330   fprintf (stream, _("\
5331   -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5332   fprintf (stream, _("\
5333   -f --special-files     Process non-plain files too\n"));
5334   fprintf (stream, _("\
5335   @<file>                Read options from <file>\n"));
5336   fprintf (stream, _("\
5337   -H --help              Display this information\n"));
5338   fprintf (stream, _("\
5339   -v --version           Display the version number of readelf\n"));
5340 
5341   if (REPORT_BUGS_TO[0] && stream == stdout)
5342     fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5343 
5344   exit (stream == stdout ? 0 : 1);
5345 }
5346 
5347 /* Record the fact that the user wants the contents of section number
5348    SECTION to be displayed using the method(s) encoded as flags bits
5349    in TYPE.  Note, TYPE can be zero if we are creating the array for
5350    the first time.  */
5351 
5352 static void
5353 request_dump_bynumber (struct dump_data *dumpdata,
5354 		       unsigned int section, dump_type type)
5355 {
5356   if (section >= dumpdata->num_dump_sects)
5357     {
5358       dump_type * new_dump_sects;
5359 
5360       new_dump_sects = (dump_type *) calloc (section + 1,
5361                                              sizeof (* new_dump_sects));
5362 
5363       if (new_dump_sects == NULL)
5364 	error (_("Out of memory allocating dump request table.\n"));
5365       else
5366 	{
5367 	  if (dumpdata->dump_sects)
5368 	    {
5369 	      /* Copy current flag settings.  */
5370 	      memcpy (new_dump_sects, dumpdata->dump_sects,
5371 		      dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5372 
5373 	      free (dumpdata->dump_sects);
5374 	    }
5375 
5376 	  dumpdata->dump_sects = new_dump_sects;
5377 	  dumpdata->num_dump_sects = section + 1;
5378 	}
5379     }
5380 
5381   if (dumpdata->dump_sects)
5382     dumpdata->dump_sects[section] |= type;
5383 }
5384 
5385 /* Request a dump by section name.  */
5386 
5387 static void
5388 request_dump_byname (const char * section, dump_type type)
5389 {
5390   struct dump_list_entry * new_request;
5391 
5392   new_request = (struct dump_list_entry *)
5393       malloc (sizeof (struct dump_list_entry));
5394   if (!new_request)
5395     error (_("Out of memory allocating dump request table.\n"));
5396 
5397   new_request->name = strdup (section);
5398   if (!new_request->name)
5399     error (_("Out of memory allocating dump request table.\n"));
5400 
5401   new_request->type = type;
5402 
5403   new_request->next = dump_sects_byname;
5404   dump_sects_byname = new_request;
5405 }
5406 
5407 static inline void
5408 request_dump (struct dump_data *dumpdata, dump_type type)
5409 {
5410   int section;
5411   char * cp;
5412 
5413   do_dump = true;
5414   section = strtoul (optarg, & cp, 0);
5415 
5416   if (! *cp && section >= 0)
5417     request_dump_bynumber (dumpdata, section, type);
5418   else
5419     request_dump_byname (optarg, type);
5420 }
5421 
5422 static void
5423 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5424 {
5425   int c;
5426 
5427   if (argc < 2)
5428     usage (stderr);
5429 
5430   while ((c = getopt_long
5431 	  (argc, argv, "ACDHILNPR:STU:VWacdefghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5432     {
5433       switch (c)
5434 	{
5435 	case 0:
5436 	  /* Long options.  */
5437 	  break;
5438 	case 'H':
5439 	  usage (stdout);
5440 	  break;
5441 
5442 	case 'a':
5443 	  do_syms = true;
5444 	  do_reloc = true;
5445 	  do_unwind = true;
5446 	  do_dynamic = true;
5447 	  do_header = true;
5448 	  do_sections = true;
5449 	  do_section_groups = true;
5450 	  do_segments = true;
5451 	  do_version = true;
5452 	  do_histogram = true;
5453 	  do_arch = true;
5454 	  do_notes = true;
5455 	  break;
5456 
5457 	case 'g':
5458 	  do_section_groups = true;
5459 	  break;
5460 	case 't':
5461 	case 'N':
5462 	  do_sections = true;
5463 	  do_section_details = true;
5464 	  break;
5465 	case 'e':
5466 	  do_header = true;
5467 	  do_sections = true;
5468 	  do_segments = true;
5469 	  break;
5470 	case 'A':
5471 	  do_arch = true;
5472 	  break;
5473 	case 'D':
5474 	  do_using_dynamic = true;
5475 	  break;
5476 	case 'r':
5477 	  do_reloc = true;
5478 	  break;
5479 	case 'u':
5480 	  do_unwind = true;
5481 	  break;
5482 	case 'f':
5483 	  do_special_files = true;
5484 	  break;
5485 	case 'h':
5486 	  do_header = true;
5487 	  break;
5488 	case 'l':
5489 	  do_segments = true;
5490 	  break;
5491 	case 's':
5492 	  do_syms = true;
5493 	  break;
5494 	case 'S':
5495 	  do_sections = true;
5496 	  break;
5497 	case 'd':
5498 	  do_dynamic = true;
5499 	  break;
5500 	case 'I':
5501 	  do_histogram = true;
5502 	  break;
5503 	case 'n':
5504 	  do_notes = true;
5505 	  break;
5506 	case 'c':
5507 	  do_archive_index = true;
5508 	  break;
5509 	case 'L':
5510 	  do_checks = true;
5511 	  break;
5512 	case 'P':
5513 	  process_links = true;
5514 	  do_follow_links = true;
5515 	  dump_any_debugging = true;
5516 	  break;
5517 	case 'x':
5518 	  request_dump (dumpdata, HEX_DUMP);
5519 	  break;
5520 	case 'p':
5521 	  request_dump (dumpdata, STRING_DUMP);
5522 	  break;
5523 	case 'R':
5524 	  request_dump (dumpdata, RELOC_DUMP);
5525 	  break;
5526 	case 'z':
5527 	  decompress_dumps = true;
5528 	  break;
5529 	case 'w':
5530 	  if (optarg == NULL)
5531 	    {
5532 	      do_debugging = true;
5533 	      do_dump = true;
5534 	      dump_any_debugging = true;
5535 	      dwarf_select_sections_all ();
5536 	    }
5537 	  else
5538 	    {
5539 	      do_debugging = false;
5540 	      if (dwarf_select_sections_by_letters (optarg))
5541 		{
5542 		  do_dump = true;
5543 		  dump_any_debugging = true;
5544 		}
5545 	    }
5546 	  break;
5547 	case OPTION_DEBUG_DUMP:
5548 	  if (optarg == NULL)
5549 	    {
5550 	      do_dump = true;
5551 	      do_debugging = true;
5552 	      dump_any_debugging = true;
5553 	      dwarf_select_sections_all ();
5554 	    }
5555 	  else
5556 	    {
5557 	      do_debugging = false;
5558 	      if (dwarf_select_sections_by_names (optarg))
5559 		{
5560 		  do_dump = true;
5561 		  dump_any_debugging = true;
5562 		}
5563 	    }
5564 	  break;
5565 	case OPTION_DWARF_DEPTH:
5566 	  {
5567 	    char *cp;
5568 
5569 	    dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5570 	  }
5571 	  break;
5572 	case OPTION_DWARF_START:
5573 	  {
5574 	    char *cp;
5575 
5576 	    dwarf_start_die = strtoul (optarg, & cp, 0);
5577 	  }
5578 	  break;
5579 	case OPTION_DWARF_CHECK:
5580 	  dwarf_check = true;
5581 	  break;
5582 	case OPTION_CTF_DUMP:
5583 	  do_ctf = true;
5584 	  request_dump (dumpdata, CTF_DUMP);
5585 	  break;
5586 	case OPTION_CTF_SYMBOLS:
5587 	  free (dump_ctf_symtab_name);
5588 	  dump_ctf_symtab_name = strdup (optarg);
5589 	  break;
5590 	case OPTION_CTF_STRINGS:
5591 	  free (dump_ctf_strtab_name);
5592 	  dump_ctf_strtab_name = strdup (optarg);
5593 	  break;
5594 	case OPTION_CTF_PARENT:
5595 	  free (dump_ctf_parent_name);
5596 	  dump_ctf_parent_name = strdup (optarg);
5597 	  break;
5598 	case OPTION_DYN_SYMS:
5599 	  do_dyn_syms = true;
5600 	  break;
5601 	case OPTION_LTO_SYMS:
5602 	  do_lto_syms = true;
5603 	  break;
5604 #ifdef SUPPORT_DISASSEMBLY
5605 	case 'i':
5606 	  request_dump (dumpdata, DISASS_DUMP);
5607 	  break;
5608 #endif
5609 	case 'v':
5610 	  print_version (program_name);
5611 	  break;
5612 	case 'V':
5613 	  do_version = true;
5614 	  break;
5615 	case 'W':
5616 	  do_wide = true;
5617 	  break;
5618 	case 'T':
5619 	  do_not_show_symbol_truncation = true;
5620 	  break;
5621 	case 'C':
5622 	  do_demangle = true;
5623 	  if (optarg != NULL)
5624 	    {
5625 	      enum demangling_styles style;
5626 
5627 	      style = cplus_demangle_name_to_style (optarg);
5628 	      if (style == unknown_demangling)
5629 		error (_("unknown demangling style `%s'"), optarg);
5630 
5631 	      cplus_demangle_set_style (style);
5632 	    }
5633 	  break;
5634 	case OPTION_NO_DEMANGLING:
5635 	  do_demangle = false;
5636 	  break;
5637 	case OPTION_RECURSE_LIMIT:
5638 	  demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5639 	  break;
5640 	case OPTION_NO_RECURSE_LIMIT:
5641 	  demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5642 	  break;
5643 	case OPTION_WITH_SYMBOL_VERSIONS:
5644 	  /* Ignored for backward compatibility.  */
5645 	  break;
5646 
5647 	case 'U':
5648 	  if (optarg == NULL)
5649 	    error (_("Missing arg to -U/--unicode")); /* Can this happen ?  */
5650 	  else if (streq (optarg, "default") || streq (optarg, "d"))
5651 	    unicode_display = unicode_default;
5652 	  else if (streq (optarg, "locale") || streq (optarg, "l"))
5653 	    unicode_display = unicode_locale;
5654 	  else if (streq (optarg, "escape") || streq (optarg, "e"))
5655 	    unicode_display = unicode_escape;
5656 	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
5657 	    unicode_display = unicode_invalid;
5658 	  else if (streq (optarg, "hex") || streq (optarg, "x"))
5659 	    unicode_display = unicode_hex;
5660 	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
5661 	    unicode_display = unicode_highlight;
5662 	  else
5663 	    error (_("invalid argument to -U/--unicode: %s"), optarg);
5664 	  break;
5665 
5666 	case OPTION_SYM_BASE:
5667 	  sym_base = 0;
5668 	  if (optarg != NULL)
5669 	    {
5670 	      sym_base = strtoul (optarg, NULL, 0);
5671 	      switch (sym_base)
5672 		{
5673 		  case 0:
5674 		  case 8:
5675 		  case 10:
5676 		  case 16:
5677 		    break;
5678 
5679 		  default:
5680 		    sym_base = 0;
5681 		    break;
5682 		}
5683 	    }
5684 	  break;
5685 
5686 	default:
5687 	  /* xgettext:c-format */
5688 	  error (_("Invalid option '-%c'\n"), c);
5689 	  /* Fall through.  */
5690 	case '?':
5691 	  usage (stderr);
5692 	}
5693     }
5694 
5695   if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5696       && !do_segments && !do_header && !do_dump && !do_version
5697       && !do_histogram && !do_debugging && !do_arch && !do_notes
5698       && !do_section_groups && !do_archive_index
5699       && !do_dyn_syms && !do_lto_syms)
5700     {
5701       if (do_checks)
5702 	{
5703 	  check_all = true;
5704 	  do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5705 	  do_segments = do_header = do_dump = do_version = true;
5706 	  do_histogram = do_debugging = do_arch = do_notes = true;
5707 	  do_section_groups = do_archive_index = do_dyn_syms = true;
5708 	  do_lto_syms = true;
5709 	}
5710       else
5711 	usage (stderr);
5712     }
5713 }
5714 
5715 static const char *
5716 get_elf_class (unsigned int elf_class)
5717 {
5718   static char buff[32];
5719 
5720   switch (elf_class)
5721     {
5722     case ELFCLASSNONE: return _("none");
5723     case ELFCLASS32:   return "ELF32";
5724     case ELFCLASS64:   return "ELF64";
5725     default:
5726       snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5727       return buff;
5728     }
5729 }
5730 
5731 static const char *
5732 get_data_encoding (unsigned int encoding)
5733 {
5734   static char buff[32];
5735 
5736   switch (encoding)
5737     {
5738     case ELFDATANONE: return _("none");
5739     case ELFDATA2LSB: return _("2's complement, little endian");
5740     case ELFDATA2MSB: return _("2's complement, big endian");
5741     default:
5742       snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5743       return buff;
5744     }
5745 }
5746 
5747 /* Decode the data held in 'filedata->file_header'.  */
5748 
5749 static bool
5750 process_file_header (Filedata * filedata)
5751 {
5752   Elf_Internal_Ehdr * header = & filedata->file_header;
5753 
5754   if (   header->e_ident[EI_MAG0] != ELFMAG0
5755       || header->e_ident[EI_MAG1] != ELFMAG1
5756       || header->e_ident[EI_MAG2] != ELFMAG2
5757       || header->e_ident[EI_MAG3] != ELFMAG3)
5758     {
5759       error
5760 	(_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5761       return false;
5762     }
5763 
5764   if (! filedata->is_separate)
5765     init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5766 
5767   if (do_header)
5768     {
5769       unsigned i;
5770 
5771       if (filedata->is_separate)
5772 	printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5773       else
5774 	printf (_("ELF Header:\n"));
5775       printf (_("  Magic:   "));
5776       for (i = 0; i < EI_NIDENT; i++)
5777 	printf ("%2.2x ", header->e_ident[i]);
5778       printf ("\n");
5779       printf (_("  Class:                             %s\n"),
5780 	      get_elf_class (header->e_ident[EI_CLASS]));
5781       printf (_("  Data:                              %s\n"),
5782 	      get_data_encoding (header->e_ident[EI_DATA]));
5783       printf (_("  Version:                           %d%s\n"),
5784 	      header->e_ident[EI_VERSION],
5785 	      (header->e_ident[EI_VERSION] == EV_CURRENT
5786 	       ? _(" (current)")
5787 	       : (header->e_ident[EI_VERSION] != EV_NONE
5788 		  ? _(" <unknown>")
5789 		  : "")));
5790       printf (_("  OS/ABI:                            %s\n"),
5791 	      get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5792       printf (_("  ABI Version:                       %d\n"),
5793 	      header->e_ident[EI_ABIVERSION]);
5794       printf (_("  Type:                              %s\n"),
5795 	      get_file_type (filedata));
5796       printf (_("  Machine:                           %s\n"),
5797 	      get_machine_name (header->e_machine));
5798       printf (_("  Version:                           0x%lx\n"),
5799 	      header->e_version);
5800 
5801       printf (_("  Entry point address:               "));
5802       print_vma (header->e_entry, PREFIX_HEX);
5803       printf (_("\n  Start of program headers:          "));
5804       print_vma (header->e_phoff, DEC);
5805       printf (_(" (bytes into file)\n  Start of section headers:          "));
5806       print_vma (header->e_shoff, DEC);
5807       printf (_(" (bytes into file)\n"));
5808 
5809       printf (_("  Flags:                             0x%lx%s\n"),
5810 	      header->e_flags,
5811 	      get_machine_flags (filedata, header->e_flags, header->e_machine));
5812       printf (_("  Size of this header:               %u (bytes)\n"),
5813 	      header->e_ehsize);
5814       printf (_("  Size of program headers:           %u (bytes)\n"),
5815 	      header->e_phentsize);
5816       printf (_("  Number of program headers:         %u"),
5817 	      header->e_phnum);
5818       if (filedata->section_headers != NULL
5819 	  && header->e_phnum == PN_XNUM
5820 	  && filedata->section_headers[0].sh_info != 0)
5821 	printf (" (%u)", filedata->section_headers[0].sh_info);
5822       putc ('\n', stdout);
5823       printf (_("  Size of section headers:           %u (bytes)\n"),
5824 	      header->e_shentsize);
5825       printf (_("  Number of section headers:         %u"),
5826 	      header->e_shnum);
5827       if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5828 	{
5829 	  header->e_shnum = filedata->section_headers[0].sh_size;
5830 	  printf (" (%u)", header->e_shnum);
5831 	}
5832       putc ('\n', stdout);
5833       printf (_("  Section header string table index: %u"),
5834 	      header->e_shstrndx);
5835       if (filedata->section_headers != NULL
5836 	  && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5837 	{
5838 	  header->e_shstrndx = filedata->section_headers[0].sh_link;
5839 	  printf (" (%u)", header->e_shstrndx);
5840 	}
5841       if (header->e_shstrndx != SHN_UNDEF
5842 	  && header->e_shstrndx >= header->e_shnum)
5843 	{
5844 	  header->e_shstrndx = SHN_UNDEF;
5845 	  printf (_(" <corrupt: out of range>"));
5846 	}
5847       putc ('\n', stdout);
5848     }
5849 
5850   if (filedata->section_headers != NULL)
5851     {
5852       if (header->e_phnum == PN_XNUM
5853 	  && filedata->section_headers[0].sh_info != 0)
5854 	{
5855 	  /* Throw away any cached read of PN_XNUM headers.  */
5856 	  free (filedata->program_headers);
5857 	  filedata->program_headers = NULL;
5858 	  header->e_phnum = filedata->section_headers[0].sh_info;
5859 	}
5860       if (header->e_shnum == SHN_UNDEF)
5861 	header->e_shnum = filedata->section_headers[0].sh_size;
5862       if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5863 	header->e_shstrndx = filedata->section_headers[0].sh_link;
5864       if (header->e_shstrndx >= header->e_shnum)
5865 	header->e_shstrndx = SHN_UNDEF;
5866     }
5867 
5868   return true;
5869 }
5870 
5871 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5872    Returns TRUE upon success, FALSE otherwise.  Loads 32-bit headers.  */
5873 
5874 static bool
5875 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5876 {
5877   Elf32_External_Phdr * phdrs;
5878   Elf32_External_Phdr * external;
5879   Elf_Internal_Phdr *   internal;
5880   unsigned int i;
5881   unsigned int size = filedata->file_header.e_phentsize;
5882   unsigned int num  = filedata->file_header.e_phnum;
5883 
5884   /* PR binutils/17531: Cope with unexpected section header sizes.  */
5885   if (size == 0 || num == 0)
5886     return false;
5887   if (size < sizeof * phdrs)
5888     {
5889       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5890       return false;
5891     }
5892   if (size > sizeof * phdrs)
5893     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5894 
5895   phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5896                                             size, num, _("program headers"));
5897   if (phdrs == NULL)
5898     return false;
5899 
5900   for (i = 0, internal = pheaders, external = phdrs;
5901        i < filedata->file_header.e_phnum;
5902        i++, internal++, external++)
5903     {
5904       internal->p_type   = BYTE_GET (external->p_type);
5905       internal->p_offset = BYTE_GET (external->p_offset);
5906       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
5907       internal->p_paddr  = BYTE_GET (external->p_paddr);
5908       internal->p_filesz = BYTE_GET (external->p_filesz);
5909       internal->p_memsz  = BYTE_GET (external->p_memsz);
5910       internal->p_flags  = BYTE_GET (external->p_flags);
5911       internal->p_align  = BYTE_GET (external->p_align);
5912     }
5913 
5914   free (phdrs);
5915   return true;
5916 }
5917 
5918 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5919    Returns TRUE upon success, FALSE otherwise.  Loads 64-bit headers.  */
5920 
5921 static bool
5922 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5923 {
5924   Elf64_External_Phdr * phdrs;
5925   Elf64_External_Phdr * external;
5926   Elf_Internal_Phdr *   internal;
5927   unsigned int i;
5928   unsigned int size = filedata->file_header.e_phentsize;
5929   unsigned int num  = filedata->file_header.e_phnum;
5930 
5931   /* PR binutils/17531: Cope with unexpected section header sizes.  */
5932   if (size == 0 || num == 0)
5933     return false;
5934   if (size < sizeof * phdrs)
5935     {
5936       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5937       return false;
5938     }
5939   if (size > sizeof * phdrs)
5940     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5941 
5942   phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5943                                             size, num, _("program headers"));
5944   if (!phdrs)
5945     return false;
5946 
5947   for (i = 0, internal = pheaders, external = phdrs;
5948        i < filedata->file_header.e_phnum;
5949        i++, internal++, external++)
5950     {
5951       internal->p_type   = BYTE_GET (external->p_type);
5952       internal->p_flags  = BYTE_GET (external->p_flags);
5953       internal->p_offset = BYTE_GET (external->p_offset);
5954       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
5955       internal->p_paddr  = BYTE_GET (external->p_paddr);
5956       internal->p_filesz = BYTE_GET (external->p_filesz);
5957       internal->p_memsz  = BYTE_GET (external->p_memsz);
5958       internal->p_align  = BYTE_GET (external->p_align);
5959     }
5960 
5961   free (phdrs);
5962   return true;
5963 }
5964 
5965 /* Returns TRUE if the program headers were read into `program_headers'.  */
5966 
5967 static bool
5968 get_program_headers (Filedata * filedata)
5969 {
5970   Elf_Internal_Phdr * phdrs;
5971 
5972   /* Check cache of prior read.  */
5973   if (filedata->program_headers != NULL)
5974     return true;
5975 
5976   /* Be kind to memory checkers by looking for
5977      e_phnum values which we know must be invalid.  */
5978   if (filedata->file_header.e_phnum
5979       * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5980       >= filedata->file_size)
5981     {
5982       error (_("Too many program headers - %#x - the file is not that big\n"),
5983 	     filedata->file_header.e_phnum);
5984       return false;
5985     }
5986 
5987   phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5988 					 sizeof (Elf_Internal_Phdr));
5989   if (phdrs == NULL)
5990     {
5991       error (_("Out of memory reading %u program headers\n"),
5992 	     filedata->file_header.e_phnum);
5993       return false;
5994     }
5995 
5996   if (is_32bit_elf
5997       ? get_32bit_program_headers (filedata, phdrs)
5998       : get_64bit_program_headers (filedata, phdrs))
5999     {
6000       filedata->program_headers = phdrs;
6001       return true;
6002     }
6003 
6004   free (phdrs);
6005   return false;
6006 }
6007 
6008 /* Print program header info and locate dynamic section.  */
6009 
6010 static void
6011 process_program_headers (Filedata * filedata)
6012 {
6013   Elf_Internal_Phdr * segment;
6014   unsigned int i;
6015   Elf_Internal_Phdr * previous_load = NULL;
6016 
6017   if (filedata->file_header.e_phnum == 0)
6018     {
6019       /* PR binutils/12467.  */
6020       if (filedata->file_header.e_phoff != 0)
6021 	warn (_("possibly corrupt ELF header - it has a non-zero program"
6022 		" header offset, but no program headers\n"));
6023       else if (do_segments)
6024 	{
6025 	  if (filedata->is_separate)
6026 	    printf (_("\nThere are no program headers in linked file '%s'.\n"),
6027 		    filedata->file_name);
6028 	  else
6029 	    printf (_("\nThere are no program headers in this file.\n"));
6030 	}
6031       goto no_headers;
6032     }
6033 
6034   if (do_segments && !do_header)
6035     {
6036       if (filedata->is_separate)
6037 	printf ("\nIn linked file '%s' the ELF file type is %s\n",
6038 		filedata->file_name, get_file_type (filedata));
6039       else
6040 	printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6041       printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
6042       printf (ngettext ("There is %d program header, starting at offset %s\n",
6043 			"There are %d program headers, starting at offset %s\n",
6044 			filedata->file_header.e_phnum),
6045 	      filedata->file_header.e_phnum,
6046 	      bfd_vmatoa ("u", filedata->file_header.e_phoff));
6047     }
6048 
6049   if (! get_program_headers (filedata))
6050     goto no_headers;
6051 
6052   if (do_segments)
6053     {
6054       if (filedata->file_header.e_phnum > 1)
6055 	printf (_("\nProgram Headers:\n"));
6056       else
6057 	printf (_("\nProgram Header:\n"));
6058 
6059       if (is_32bit_elf)
6060 	printf
6061 	  (_("  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n"));
6062       else if (do_wide)
6063 	printf
6064 	  (_("  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align\n"));
6065       else
6066 	{
6067 	  printf
6068 	    (_("  Type           Offset             VirtAddr           PhysAddr\n"));
6069 	  printf
6070 	    (_("                 FileSiz            MemSiz              Flags  Align\n"));
6071 	}
6072     }
6073 
6074   unsigned long dynamic_addr = 0;
6075   bfd_size_type dynamic_size = 0;
6076   for (i = 0, segment = filedata->program_headers;
6077        i < filedata->file_header.e_phnum;
6078        i++, segment++)
6079     {
6080       if (do_segments)
6081 	{
6082 	  printf ("  %-14.14s ", get_segment_type (filedata, segment->p_type));
6083 
6084 	  if (is_32bit_elf)
6085 	    {
6086 	      printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6087 	      printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6088 	      printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6089 	      printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6090 	      printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6091 	      printf ("%c%c%c ",
6092 		      (segment->p_flags & PF_R ? 'R' : ' '),
6093 		      (segment->p_flags & PF_W ? 'W' : ' '),
6094 		      (segment->p_flags & PF_X ? 'E' : ' '));
6095 	      printf ("%#lx", (unsigned long) segment->p_align);
6096 	    }
6097 	  else if (do_wide)
6098 	    {
6099 	      if ((unsigned long) segment->p_offset == segment->p_offset)
6100 		printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6101 	      else
6102 		{
6103 		  print_vma (segment->p_offset, FULL_HEX);
6104 		  putchar (' ');
6105 		}
6106 
6107 	      print_vma (segment->p_vaddr, FULL_HEX);
6108 	      putchar (' ');
6109 	      print_vma (segment->p_paddr, FULL_HEX);
6110 	      putchar (' ');
6111 
6112 	      if ((unsigned long) segment->p_filesz == segment->p_filesz)
6113 		printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6114 	      else
6115 		{
6116 		  print_vma (segment->p_filesz, FULL_HEX);
6117 		  putchar (' ');
6118 		}
6119 
6120 	      if ((unsigned long) segment->p_memsz == segment->p_memsz)
6121 		printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6122 	      else
6123 		{
6124 		  print_vma (segment->p_memsz, FULL_HEX);
6125 		}
6126 
6127 	      printf (" %c%c%c ",
6128 		      (segment->p_flags & PF_R ? 'R' : ' '),
6129 		      (segment->p_flags & PF_W ? 'W' : ' '),
6130 		      (segment->p_flags & PF_X ? 'E' : ' '));
6131 
6132 	      if ((unsigned long) segment->p_align == segment->p_align)
6133 		printf ("%#lx", (unsigned long) segment->p_align);
6134 	      else
6135 		{
6136 		  print_vma (segment->p_align, PREFIX_HEX);
6137 		}
6138 	    }
6139 	  else
6140 	    {
6141 	      print_vma (segment->p_offset, FULL_HEX);
6142 	      putchar (' ');
6143 	      print_vma (segment->p_vaddr, FULL_HEX);
6144 	      putchar (' ');
6145 	      print_vma (segment->p_paddr, FULL_HEX);
6146 	      printf ("\n                 ");
6147 	      print_vma (segment->p_filesz, FULL_HEX);
6148 	      putchar (' ');
6149 	      print_vma (segment->p_memsz, FULL_HEX);
6150 	      printf ("  %c%c%c    ",
6151 		      (segment->p_flags & PF_R ? 'R' : ' '),
6152 		      (segment->p_flags & PF_W ? 'W' : ' '),
6153 		      (segment->p_flags & PF_X ? 'E' : ' '));
6154 	      print_vma (segment->p_align, PREFIX_HEX);
6155 	    }
6156 
6157 	  putc ('\n', stdout);
6158 	}
6159 
6160       switch (segment->p_type)
6161 	{
6162 	case PT_LOAD:
6163 #if 0 /* Do not warn about out of order PT_LOAD segments.  Although officially
6164 	 required by the ELF standard, several programs, including the Linux
6165 	 kernel, make use of non-ordered segments.  */
6166 	  if (previous_load
6167 	      && previous_load->p_vaddr > segment->p_vaddr)
6168 	    error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6169 #endif
6170 	  if (segment->p_memsz < segment->p_filesz)
6171 	    error (_("the segment's file size is larger than its memory size\n"));
6172 	  previous_load = segment;
6173 	  break;
6174 
6175 	case PT_PHDR:
6176 	  /* PR 20815 - Verify that the program header is loaded into memory.  */
6177 	  if (i > 0 && previous_load != NULL)
6178 	    error (_("the PHDR segment must occur before any LOAD segment\n"));
6179 	  if (filedata->file_header.e_machine != EM_PARISC)
6180 	    {
6181 	      unsigned int j;
6182 
6183 	      for (j = 1; j < filedata->file_header.e_phnum; j++)
6184 		{
6185 		  Elf_Internal_Phdr *load = filedata->program_headers + j;
6186 		  if (load->p_type == PT_LOAD
6187 		      && load->p_offset <= segment->p_offset
6188 		      && (load->p_offset + load->p_filesz
6189 			  >= segment->p_offset + segment->p_filesz)
6190 		      && load->p_vaddr <= segment->p_vaddr
6191 		      && (load->p_vaddr + load->p_filesz
6192 			  >= segment->p_vaddr + segment->p_filesz))
6193 		    break;
6194 		}
6195 	      if (j == filedata->file_header.e_phnum)
6196 		error (_("the PHDR segment is not covered by a LOAD segment\n"));
6197 	    }
6198 	  break;
6199 
6200 	case PT_DYNAMIC:
6201 	  if (dynamic_addr)
6202 	    error (_("more than one dynamic segment\n"));
6203 
6204 	  /* By default, assume that the .dynamic section is the first
6205 	     section in the DYNAMIC segment.  */
6206 	  dynamic_addr = segment->p_offset;
6207 	  dynamic_size = segment->p_filesz;
6208 
6209 	  /* Try to locate the .dynamic section. If there is
6210 	     a section header table, we can easily locate it.  */
6211 	  if (filedata->section_headers != NULL)
6212 	    {
6213 	      Elf_Internal_Shdr * sec;
6214 
6215 	      sec = find_section (filedata, ".dynamic");
6216 	      if (sec == NULL || sec->sh_size == 0)
6217 		{
6218 		  /* A corresponding .dynamic section is expected, but on
6219 		     IA-64/OpenVMS it is OK for it to be missing.  */
6220 		  if (!is_ia64_vms (filedata))
6221 		    error (_("no .dynamic section in the dynamic segment\n"));
6222 		  break;
6223 		}
6224 
6225 	      if (sec->sh_type == SHT_NOBITS)
6226 		{
6227 		  dynamic_addr = 0;
6228 		  dynamic_size = 0;
6229 		  break;
6230 		}
6231 
6232 	      dynamic_addr = sec->sh_offset;
6233 	      dynamic_size = sec->sh_size;
6234 
6235 	      /* The PT_DYNAMIC segment, which is used by the run-time
6236 		 loader,  should exactly match the .dynamic section.  */
6237 	      if (do_checks
6238 		  && (dynamic_addr != segment->p_offset
6239 		      || dynamic_size != segment->p_filesz))
6240 		warn (_("\
6241 the .dynamic section is not the same as the dynamic segment\n"));
6242 	    }
6243 
6244 	  /* PR binutils/17512: Avoid corrupt dynamic section info in the
6245 	     segment.  Check this after matching against the section headers
6246 	     so we don't warn on debuginfo file (which have NOBITS .dynamic
6247 	     sections).  */
6248 	  if (dynamic_addr > filedata->file_size
6249 	      || (dynamic_size > filedata->file_size - dynamic_addr))
6250 	    {
6251 	      error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6252 	      dynamic_addr = 0;
6253 	      dynamic_size = 0;
6254 	    }
6255 	  break;
6256 
6257 	case PT_INTERP:
6258 	  if (segment->p_offset >= filedata->file_size
6259 	      || segment->p_filesz > filedata->file_size - segment->p_offset
6260 	      || segment->p_filesz - 1 >= (size_t) -2
6261 	      || fseek (filedata->handle,
6262 			filedata->archive_file_offset + (long) segment->p_offset,
6263 			SEEK_SET))
6264 	    error (_("Unable to find program interpreter name\n"));
6265 	  else
6266 	    {
6267 	      size_t len = segment->p_filesz;
6268 	      free (filedata->program_interpreter);
6269 	      filedata->program_interpreter = xmalloc (len + 1);
6270 	      len = fread (filedata->program_interpreter, 1, len,
6271 			   filedata->handle);
6272 	      filedata->program_interpreter[len] = 0;
6273 
6274 	      if (do_segments)
6275 		printf (_("      [Requesting program interpreter: %s]\n"),
6276 		    filedata->program_interpreter);
6277 	    }
6278 	  break;
6279 	}
6280     }
6281 
6282   if (do_segments
6283       && filedata->section_headers != NULL
6284       && filedata->string_table != NULL)
6285     {
6286       printf (_("\n Section to Segment mapping:\n"));
6287       printf (_("  Segment Sections...\n"));
6288 
6289       for (i = 0; i < filedata->file_header.e_phnum; i++)
6290 	{
6291 	  unsigned int j;
6292 	  Elf_Internal_Shdr * section;
6293 
6294 	  segment = filedata->program_headers + i;
6295 	  section = filedata->section_headers + 1;
6296 
6297 	  printf ("   %2.2d     ", i);
6298 
6299 	  for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6300 	    {
6301 	      if (!ELF_TBSS_SPECIAL (section, segment)
6302 		  && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6303 		printf ("%s ", printable_section_name (filedata, section));
6304 	    }
6305 
6306 	  putc ('\n',stdout);
6307 	}
6308     }
6309 
6310   filedata->dynamic_addr = dynamic_addr;
6311   filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6312   return;
6313 
6314  no_headers:
6315   filedata->dynamic_addr = 0;
6316   filedata->dynamic_size = 1;
6317 }
6318 
6319 
6320 /* Find the file offset corresponding to VMA by using the program headers.  */
6321 
6322 static long
6323 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
6324 {
6325   Elf_Internal_Phdr * seg;
6326 
6327   if (! get_program_headers (filedata))
6328     {
6329       warn (_("Cannot interpret virtual addresses without program headers.\n"));
6330       return (long) vma;
6331     }
6332 
6333   for (seg = filedata->program_headers;
6334        seg < filedata->program_headers + filedata->file_header.e_phnum;
6335        ++seg)
6336     {
6337       if (seg->p_type != PT_LOAD)
6338 	continue;
6339 
6340       if (vma >= (seg->p_vaddr & -seg->p_align)
6341 	  && vma + size <= seg->p_vaddr + seg->p_filesz)
6342 	return vma - seg->p_vaddr + seg->p_offset;
6343     }
6344 
6345   warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
6346 	(unsigned long) vma);
6347   return (long) vma;
6348 }
6349 
6350 
6351 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6352    If PROBE is true, this is just a probe and we do not generate any error
6353    messages if the load fails.  */
6354 
6355 static bool
6356 get_32bit_section_headers (Filedata * filedata, bool probe)
6357 {
6358   Elf32_External_Shdr * shdrs;
6359   Elf_Internal_Shdr *   internal;
6360   unsigned int          i;
6361   unsigned int          size = filedata->file_header.e_shentsize;
6362   unsigned int          num = probe ? 1 : filedata->file_header.e_shnum;
6363 
6364   /* PR binutils/17531: Cope with unexpected section header sizes.  */
6365   if (size == 0 || num == 0)
6366     return false;
6367   if (size < sizeof * shdrs)
6368     {
6369       if (! probe)
6370 	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6371       return false;
6372     }
6373   if (!probe && size > sizeof * shdrs)
6374     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6375 
6376   shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6377                                             size, num,
6378 					    probe ? NULL : _("section headers"));
6379   if (shdrs == NULL)
6380     return false;
6381 
6382   filedata->section_headers = (Elf_Internal_Shdr *)
6383     cmalloc (num, sizeof (Elf_Internal_Shdr));
6384   if (filedata->section_headers == NULL)
6385     {
6386       if (!probe)
6387 	error (_("Out of memory reading %u section headers\n"), num);
6388       free (shdrs);
6389       return false;
6390     }
6391 
6392   for (i = 0, internal = filedata->section_headers;
6393        i < num;
6394        i++, internal++)
6395     {
6396       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
6397       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
6398       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
6399       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
6400       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
6401       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
6402       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
6403       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
6404       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6405       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
6406       if (!probe && internal->sh_link > num)
6407 	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6408       if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6409 	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6410     }
6411 
6412   free (shdrs);
6413   return true;
6414 }
6415 
6416 /* Like get_32bit_section_headers, except that it fetches 64-bit headers.  */
6417 
6418 static bool
6419 get_64bit_section_headers (Filedata * filedata, bool probe)
6420 {
6421   Elf64_External_Shdr *  shdrs;
6422   Elf_Internal_Shdr *    internal;
6423   unsigned int           i;
6424   unsigned int           size = filedata->file_header.e_shentsize;
6425   unsigned int           num = probe ? 1 : filedata->file_header.e_shnum;
6426 
6427   /* PR binutils/17531: Cope with unexpected section header sizes.  */
6428   if (size == 0 || num == 0)
6429     return false;
6430 
6431   if (size < sizeof * shdrs)
6432     {
6433       if (! probe)
6434 	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6435       return false;
6436     }
6437 
6438   if (! probe && size > sizeof * shdrs)
6439     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6440 
6441   shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6442 					    filedata->file_header.e_shoff,
6443                                             size, num,
6444 					    probe ? NULL : _("section headers"));
6445   if (shdrs == NULL)
6446     return false;
6447 
6448   filedata->section_headers = (Elf_Internal_Shdr *)
6449     cmalloc (num, sizeof (Elf_Internal_Shdr));
6450   if (filedata->section_headers == NULL)
6451     {
6452       if (! probe)
6453 	error (_("Out of memory reading %u section headers\n"), num);
6454       free (shdrs);
6455       return false;
6456     }
6457 
6458   for (i = 0, internal = filedata->section_headers;
6459        i < num;
6460        i++, internal++)
6461     {
6462       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
6463       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
6464       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
6465       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
6466       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
6467       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
6468       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
6469       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
6470       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
6471       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6472       if (!probe && internal->sh_link > num)
6473 	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6474       if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6475 	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6476     }
6477 
6478   free (shdrs);
6479   return true;
6480 }
6481 
6482 static bool
6483 get_section_headers (Filedata *filedata, bool probe)
6484 {
6485   if (filedata->section_headers != NULL)
6486     return true;
6487 
6488   if (is_32bit_elf)
6489     return get_32bit_section_headers (filedata, probe);
6490   else
6491     return get_64bit_section_headers (filedata, probe);
6492 }
6493 
6494 static Elf_Internal_Sym *
6495 get_32bit_elf_symbols (Filedata *           filedata,
6496 		       Elf_Internal_Shdr *  section,
6497 		       unsigned long *      num_syms_return)
6498 {
6499   unsigned long number = 0;
6500   Elf32_External_Sym * esyms = NULL;
6501   Elf_External_Sym_Shndx * shndx = NULL;
6502   Elf_Internal_Sym * isyms = NULL;
6503   Elf_Internal_Sym * psym;
6504   unsigned int j;
6505   elf_section_list * entry;
6506 
6507   if (section->sh_size == 0)
6508     {
6509       if (num_syms_return != NULL)
6510 	* num_syms_return = 0;
6511       return NULL;
6512     }
6513 
6514   /* Run some sanity checks first.  */
6515   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6516     {
6517       error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6518 	     printable_section_name (filedata, section),
6519 	     (unsigned long) section->sh_entsize);
6520       goto exit_point;
6521     }
6522 
6523   if (section->sh_size > filedata->file_size)
6524     {
6525       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6526 	     printable_section_name (filedata, section),
6527 	     (unsigned long) section->sh_size);
6528       goto exit_point;
6529     }
6530 
6531   number = section->sh_size / section->sh_entsize;
6532 
6533   if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6534     {
6535       error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6536 	     (unsigned long) section->sh_size,
6537 	     printable_section_name (filedata, section),
6538 	     (unsigned long) section->sh_entsize);
6539       goto exit_point;
6540     }
6541 
6542   esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6543                                            section->sh_size, _("symbols"));
6544   if (esyms == NULL)
6545     goto exit_point;
6546 
6547   shndx = NULL;
6548   for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6549     {
6550       if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6551 	continue;
6552 
6553       if (shndx != NULL)
6554 	{
6555 	  error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6556 	  free (shndx);
6557 	}
6558 
6559       shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6560 						   entry->hdr->sh_offset,
6561 						   1, entry->hdr->sh_size,
6562 						   _("symbol table section indices"));
6563       if (shndx == NULL)
6564 	goto exit_point;
6565 
6566       /* PR17531: file: heap-buffer-overflow */
6567       if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6568 	{
6569 	  error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6570 		 printable_section_name (filedata, entry->hdr),
6571 		 (unsigned long) entry->hdr->sh_size,
6572 		 (unsigned long) section->sh_size);
6573 	  goto exit_point;
6574 	}
6575     }
6576 
6577   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6578 
6579   if (isyms == NULL)
6580     {
6581       error (_("Out of memory reading %lu symbols\n"),
6582 	     (unsigned long) number);
6583       goto exit_point;
6584     }
6585 
6586   for (j = 0, psym = isyms; j < number; j++, psym++)
6587     {
6588       psym->st_name  = BYTE_GET (esyms[j].st_name);
6589       psym->st_value = BYTE_GET (esyms[j].st_value);
6590       psym->st_size  = BYTE_GET (esyms[j].st_size);
6591       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6592       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6593 	psym->st_shndx
6594 	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6595       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6596 	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6597       psym->st_info  = BYTE_GET (esyms[j].st_info);
6598       psym->st_other = BYTE_GET (esyms[j].st_other);
6599     }
6600 
6601  exit_point:
6602   free (shndx);
6603   free (esyms);
6604 
6605   if (num_syms_return != NULL)
6606     * num_syms_return = isyms == NULL ? 0 : number;
6607 
6608   return isyms;
6609 }
6610 
6611 static Elf_Internal_Sym *
6612 get_64bit_elf_symbols (Filedata *           filedata,
6613 		       Elf_Internal_Shdr *  section,
6614 		       unsigned long *      num_syms_return)
6615 {
6616   unsigned long number = 0;
6617   Elf64_External_Sym * esyms = NULL;
6618   Elf_External_Sym_Shndx * shndx = NULL;
6619   Elf_Internal_Sym * isyms = NULL;
6620   Elf_Internal_Sym * psym;
6621   unsigned int j;
6622   elf_section_list * entry;
6623 
6624   if (section->sh_size == 0)
6625     {
6626       if (num_syms_return != NULL)
6627 	* num_syms_return = 0;
6628       return NULL;
6629     }
6630 
6631   /* Run some sanity checks first.  */
6632   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6633     {
6634       error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6635 	     printable_section_name (filedata, section),
6636 	     (unsigned long) section->sh_entsize);
6637       goto exit_point;
6638     }
6639 
6640   if (section->sh_size > filedata->file_size)
6641     {
6642       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6643 	     printable_section_name (filedata, section),
6644 	     (unsigned long) section->sh_size);
6645       goto exit_point;
6646     }
6647 
6648   number = section->sh_size / section->sh_entsize;
6649 
6650   if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6651     {
6652       error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6653 	     (unsigned long) section->sh_size,
6654 	     printable_section_name (filedata, section),
6655 	     (unsigned long) section->sh_entsize);
6656       goto exit_point;
6657     }
6658 
6659   esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6660                                            section->sh_size, _("symbols"));
6661   if (!esyms)
6662     goto exit_point;
6663 
6664   shndx = NULL;
6665   for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6666     {
6667       if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6668 	continue;
6669 
6670       if (shndx != NULL)
6671 	{
6672 	  error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6673 	  free (shndx);
6674 	}
6675 
6676       shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6677 						   entry->hdr->sh_offset,
6678 						   1, entry->hdr->sh_size,
6679 						   _("symbol table section indices"));
6680       if (shndx == NULL)
6681 	goto exit_point;
6682 
6683       /* PR17531: file: heap-buffer-overflow */
6684       if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6685 	{
6686 	  error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6687 		 printable_section_name (filedata, entry->hdr),
6688 		 (unsigned long) entry->hdr->sh_size,
6689 		 (unsigned long) section->sh_size);
6690 	  goto exit_point;
6691 	}
6692     }
6693 
6694   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6695 
6696   if (isyms == NULL)
6697     {
6698       error (_("Out of memory reading %lu symbols\n"),
6699 	     (unsigned long) number);
6700       goto exit_point;
6701     }
6702 
6703   for (j = 0, psym = isyms; j < number; j++, psym++)
6704     {
6705       psym->st_name  = BYTE_GET (esyms[j].st_name);
6706       psym->st_info  = BYTE_GET (esyms[j].st_info);
6707       psym->st_other = BYTE_GET (esyms[j].st_other);
6708       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6709 
6710       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6711 	psym->st_shndx
6712 	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6713       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6714 	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6715 
6716       psym->st_value = BYTE_GET (esyms[j].st_value);
6717       psym->st_size  = BYTE_GET (esyms[j].st_size);
6718     }
6719 
6720  exit_point:
6721   free (shndx);
6722   free (esyms);
6723 
6724   if (num_syms_return != NULL)
6725     * num_syms_return = isyms == NULL ? 0 : number;
6726 
6727   return isyms;
6728 }
6729 
6730 static Elf_Internal_Sym *
6731 get_elf_symbols (Filedata *filedata,
6732 		 Elf_Internal_Shdr *section,
6733 		 unsigned long *num_syms_return)
6734 {
6735   if (is_32bit_elf)
6736     return get_32bit_elf_symbols (filedata, section, num_syms_return);
6737   else
6738     return get_64bit_elf_symbols (filedata, section, num_syms_return);
6739 }
6740 
6741 static const char *
6742 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6743 {
6744   static char buff[1024];
6745   char * p = buff;
6746   unsigned int field_size = is_32bit_elf ? 8 : 16;
6747   signed int sindex;
6748   unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6749   bfd_vma os_flags = 0;
6750   bfd_vma proc_flags = 0;
6751   bfd_vma unknown_flags = 0;
6752   static const struct
6753     {
6754       const char * str;
6755       unsigned int len;
6756     }
6757   flags [] =
6758     {
6759       /*  0 */ { STRING_COMMA_LEN ("WRITE") },
6760       /*  1 */ { STRING_COMMA_LEN ("ALLOC") },
6761       /*  2 */ { STRING_COMMA_LEN ("EXEC") },
6762       /*  3 */ { STRING_COMMA_LEN ("MERGE") },
6763       /*  4 */ { STRING_COMMA_LEN ("STRINGS") },
6764       /*  5 */ { STRING_COMMA_LEN ("INFO LINK") },
6765       /*  6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6766       /*  7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6767       /*  8 */ { STRING_COMMA_LEN ("GROUP") },
6768       /*  9 */ { STRING_COMMA_LEN ("TLS") },
6769       /* IA-64 specific.  */
6770       /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6771       /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6772       /* IA-64 OpenVMS specific.  */
6773       /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6774       /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6775       /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6776       /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6777       /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6778       /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6779       /* Generic.  */
6780       /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6781       /* SPARC specific.  */
6782       /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6783       /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6784       /* ARM specific.  */
6785       /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6786       /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6787       /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6788       /* GNU specific.  */
6789       /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6790       /* VLE specific.  */
6791       /* 25 */ { STRING_COMMA_LEN ("VLE") },
6792       /* GNU specific.  */
6793       /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6794     };
6795 
6796   if (do_section_details)
6797     {
6798       sprintf (buff, "[%*.*lx]: ",
6799 	       field_size, field_size, (unsigned long) sh_flags);
6800       p += field_size + 4;
6801     }
6802 
6803   while (sh_flags)
6804     {
6805       bfd_vma flag;
6806 
6807       flag = sh_flags & - sh_flags;
6808       sh_flags &= ~ flag;
6809 
6810       if (do_section_details)
6811 	{
6812 	  switch (flag)
6813 	    {
6814 	    case SHF_WRITE:		sindex = 0; break;
6815 	    case SHF_ALLOC:		sindex = 1; break;
6816 	    case SHF_EXECINSTR:		sindex = 2; break;
6817 	    case SHF_MERGE:		sindex = 3; break;
6818 	    case SHF_STRINGS:		sindex = 4; break;
6819 	    case SHF_INFO_LINK:		sindex = 5; break;
6820 	    case SHF_LINK_ORDER:	sindex = 6; break;
6821 	    case SHF_OS_NONCONFORMING:	sindex = 7; break;
6822 	    case SHF_GROUP:		sindex = 8; break;
6823 	    case SHF_TLS:		sindex = 9; break;
6824 	    case SHF_EXCLUDE:		sindex = 18; break;
6825 	    case SHF_COMPRESSED:	sindex = 20; break;
6826 
6827 	    default:
6828 	      sindex = -1;
6829 	      switch (filedata->file_header.e_machine)
6830 		{
6831 		case EM_IA_64:
6832 		  if (flag == SHF_IA_64_SHORT)
6833 		    sindex = 10;
6834 		  else if (flag == SHF_IA_64_NORECOV)
6835 		    sindex = 11;
6836 #ifdef BFD64
6837 		  else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6838 		    switch (flag)
6839 		      {
6840 		      case SHF_IA_64_VMS_GLOBAL:      sindex = 12; break;
6841 		      case SHF_IA_64_VMS_OVERLAID:    sindex = 13; break;
6842 		      case SHF_IA_64_VMS_SHARED:      sindex = 14; break;
6843 		      case SHF_IA_64_VMS_VECTOR:      sindex = 15; break;
6844 		      case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6845 		      case SHF_IA_64_VMS_PROTECTED:   sindex = 17; break;
6846 		      default:                        break;
6847 		      }
6848 #endif
6849 		  break;
6850 
6851 		case EM_386:
6852 		case EM_IAMCU:
6853 		case EM_X86_64:
6854 		case EM_L1OM:
6855 		case EM_K1OM:
6856 		case EM_OLD_SPARCV9:
6857 		case EM_SPARC32PLUS:
6858 		case EM_SPARCV9:
6859 		case EM_SPARC:
6860 		  if (flag == SHF_ORDERED)
6861 		    sindex = 19;
6862 		  break;
6863 
6864 		case EM_ARM:
6865 		  switch (flag)
6866 		    {
6867 		    case SHF_ENTRYSECT: sindex = 21; break;
6868 		    case SHF_ARM_PURECODE: sindex = 22; break;
6869 		    case SHF_COMDEF: sindex = 23; break;
6870 		    default: break;
6871 		    }
6872 		  break;
6873 		case EM_PPC:
6874 		  if (flag == SHF_PPC_VLE)
6875 		    sindex = 25;
6876 		  break;
6877 		default:
6878 		  break;
6879 		}
6880 
6881 	      switch (filedata->file_header.e_ident[EI_OSABI])
6882 		{
6883 		case ELFOSABI_GNU:
6884 		case ELFOSABI_FREEBSD:
6885 		  if (flag == SHF_GNU_RETAIN)
6886 		    sindex = 26;
6887 		  /* Fall through */
6888 		case ELFOSABI_NONE:
6889 		  if (flag == SHF_GNU_MBIND)
6890 		    /* We should not recognize SHF_GNU_MBIND for
6891 		       ELFOSABI_NONE, but binutils as of 2019-07-23 did
6892 		       not set the EI_OSABI header byte.  */
6893 		    sindex = 24;
6894 		  break;
6895 		default:
6896 		  break;
6897 		}
6898 	      break;
6899 	    }
6900 
6901 	  if (sindex != -1)
6902 	    {
6903 	      if (p != buff + field_size + 4)
6904 		{
6905 		  if (size < (10 + 2))
6906 		    {
6907 		      warn (_("Internal error: not enough buffer room for section flag info"));
6908 		      return _("<unknown>");
6909 		    }
6910 		  size -= 2;
6911 		  *p++ = ',';
6912 		  *p++ = ' ';
6913 		}
6914 
6915 	      size -= flags [sindex].len;
6916 	      p = stpcpy (p, flags [sindex].str);
6917 	    }
6918 	  else if (flag & SHF_MASKOS)
6919 	    os_flags |= flag;
6920 	  else if (flag & SHF_MASKPROC)
6921 	    proc_flags |= flag;
6922 	  else
6923 	    unknown_flags |= flag;
6924 	}
6925       else
6926 	{
6927 	  switch (flag)
6928 	    {
6929 	    case SHF_WRITE:		*p = 'W'; break;
6930 	    case SHF_ALLOC:		*p = 'A'; break;
6931 	    case SHF_EXECINSTR:		*p = 'X'; break;
6932 	    case SHF_MERGE:		*p = 'M'; break;
6933 	    case SHF_STRINGS:		*p = 'S'; break;
6934 	    case SHF_INFO_LINK:		*p = 'I'; break;
6935 	    case SHF_LINK_ORDER:	*p = 'L'; break;
6936 	    case SHF_OS_NONCONFORMING:	*p = 'O'; break;
6937 	    case SHF_GROUP:		*p = 'G'; break;
6938 	    case SHF_TLS:		*p = 'T'; break;
6939 	    case SHF_EXCLUDE:		*p = 'E'; break;
6940 	    case SHF_COMPRESSED:	*p = 'C'; break;
6941 
6942 	    default:
6943 	      if ((filedata->file_header.e_machine == EM_X86_64
6944 		   || filedata->file_header.e_machine == EM_L1OM
6945 		   || filedata->file_header.e_machine == EM_K1OM)
6946 		  && flag == SHF_X86_64_LARGE)
6947 		*p = 'l';
6948 	      else if (filedata->file_header.e_machine == EM_ARM
6949 		       && flag == SHF_ARM_PURECODE)
6950 		*p = 'y';
6951 	      else if (filedata->file_header.e_machine == EM_PPC
6952 		       && flag == SHF_PPC_VLE)
6953 		*p = 'v';
6954 	      else if (flag & SHF_MASKOS)
6955 		{
6956 		  switch (filedata->file_header.e_ident[EI_OSABI])
6957 		    {
6958 		    case ELFOSABI_GNU:
6959 		    case ELFOSABI_FREEBSD:
6960 		      if (flag == SHF_GNU_RETAIN)
6961 			{
6962 			  *p = 'R';
6963 			  break;
6964 			}
6965 		      /* Fall through */
6966 		    case ELFOSABI_NONE:
6967 		      if (flag == SHF_GNU_MBIND)
6968 			{
6969 			  /* We should not recognize SHF_GNU_MBIND for
6970 			     ELFOSABI_NONE, but binutils as of 2019-07-23 did
6971 			     not set the EI_OSABI header byte.  */
6972 			  *p = 'D';
6973 			  break;
6974 			}
6975 		      /* Fall through */
6976 		    default:
6977 		      *p = 'o';
6978 		      sh_flags &= ~SHF_MASKOS;
6979 		      break;
6980 		    }
6981 		}
6982 	      else if (flag & SHF_MASKPROC)
6983 		{
6984 		  *p = 'p';
6985 		  sh_flags &= ~ SHF_MASKPROC;
6986 		}
6987 	      else
6988 		*p = 'x';
6989 	      break;
6990 	    }
6991 	  p++;
6992 	}
6993     }
6994 
6995   if (do_section_details)
6996     {
6997       if (os_flags)
6998 	{
6999 	  size -= 5 + field_size;
7000 	  if (p != buff + field_size + 4)
7001 	    {
7002 	      if (size < (2 + 1))
7003 		{
7004 		  warn (_("Internal error: not enough buffer room for section flag info"));
7005 		  return _("<unknown>");
7006 		}
7007 	      size -= 2;
7008 	      *p++ = ',';
7009 	      *p++ = ' ';
7010 	    }
7011 	  sprintf (p, "OS (%*.*lx)", field_size, field_size,
7012 		   (unsigned long) os_flags);
7013 	  p += 5 + field_size;
7014 	}
7015       if (proc_flags)
7016 	{
7017 	  size -= 7 + field_size;
7018 	  if (p != buff + field_size + 4)
7019 	    {
7020 	      if (size < (2 + 1))
7021 		{
7022 		  warn (_("Internal error: not enough buffer room for section flag info"));
7023 		  return _("<unknown>");
7024 		}
7025 	      size -= 2;
7026 	      *p++ = ',';
7027 	      *p++ = ' ';
7028 	    }
7029 	  sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7030 		   (unsigned long) proc_flags);
7031 	  p += 7 + field_size;
7032 	}
7033       if (unknown_flags)
7034 	{
7035 	  size -= 10 + field_size;
7036 	  if (p != buff + field_size + 4)
7037 	    {
7038 	      if (size < (2 + 1))
7039 		{
7040 		  warn (_("Internal error: not enough buffer room for section flag info"));
7041 		  return _("<unknown>");
7042 		}
7043 	      size -= 2;
7044 	      *p++ = ',';
7045 	      *p++ = ' ';
7046 	    }
7047 	  sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7048 		   (unsigned long) unknown_flags);
7049 	  p += 10 + field_size;
7050 	}
7051     }
7052 
7053   *p = '\0';
7054   return buff;
7055 }
7056 
7057 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7058 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
7059 {
7060   if (is_32bit_elf)
7061     {
7062       Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7063 
7064       if (size < sizeof (* echdr))
7065 	{
7066 	  error (_("Compressed section is too small even for a compression header\n"));
7067 	  return 0;
7068 	}
7069 
7070       chdr->ch_type = BYTE_GET (echdr->ch_type);
7071       chdr->ch_size = BYTE_GET (echdr->ch_size);
7072       chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7073       return sizeof (*echdr);
7074     }
7075   else
7076     {
7077       Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7078 
7079       if (size < sizeof (* echdr))
7080 	{
7081 	  error (_("Compressed section is too small even for a compression header\n"));
7082 	  return 0;
7083 	}
7084 
7085       chdr->ch_type = BYTE_GET (echdr->ch_type);
7086       chdr->ch_size = BYTE_GET (echdr->ch_size);
7087       chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7088       return sizeof (*echdr);
7089     }
7090 }
7091 
7092 static bool
7093 process_section_headers (Filedata * filedata)
7094 {
7095   Elf_Internal_Shdr * section;
7096   unsigned int i;
7097 
7098   if (filedata->file_header.e_shnum == 0)
7099     {
7100       /* PR binutils/12467.  */
7101       if (filedata->file_header.e_shoff != 0)
7102 	{
7103 	  warn (_("possibly corrupt ELF file header - it has a non-zero"
7104 		  " section header offset, but no section headers\n"));
7105 	  return false;
7106 	}
7107       else if (do_sections)
7108 	printf (_("\nThere are no sections in this file.\n"));
7109 
7110       return true;
7111     }
7112 
7113   if (do_sections && !do_header)
7114     {
7115       if (filedata->is_separate && process_links)
7116 	printf (_("In linked file '%s': "), filedata->file_name);
7117       if (! filedata->is_separate || process_links)
7118 	printf (ngettext ("There is %d section header, "
7119 			  "starting at offset 0x%lx:\n",
7120 			  "There are %d section headers, "
7121 			  "starting at offset 0x%lx:\n",
7122 			  filedata->file_header.e_shnum),
7123 		filedata->file_header.e_shnum,
7124 		(unsigned long) filedata->file_header.e_shoff);
7125     }
7126 
7127   if (!get_section_headers (filedata, false))
7128     return false;
7129 
7130   /* Read in the string table, so that we have names to display.  */
7131   if (filedata->file_header.e_shstrndx != SHN_UNDEF
7132        && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7133     {
7134       section = filedata->section_headers + filedata->file_header.e_shstrndx;
7135 
7136       if (section->sh_size != 0)
7137 	{
7138 	  filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7139 						      1, section->sh_size,
7140 						      _("string table"));
7141 
7142 	  filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7143 	}
7144     }
7145 
7146   /* Scan the sections for the dynamic symbol table
7147      and dynamic string table and debug sections.  */
7148   eh_addr_size = is_32bit_elf ? 4 : 8;
7149   switch (filedata->file_header.e_machine)
7150     {
7151     case EM_MIPS:
7152     case EM_MIPS_RS3_LE:
7153       /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7154 	 FDE addresses.  However, the ABI also has a semi-official ILP32
7155 	 variant for which the normal FDE address size rules apply.
7156 
7157 	 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7158 	 section, where XX is the size of longs in bits.  Unfortunately,
7159 	 earlier compilers provided no way of distinguishing ILP32 objects
7160 	 from LP64 objects, so if there's any doubt, we should assume that
7161 	 the official LP64 form is being used.  */
7162       if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7163 	  && find_section (filedata, ".gcc_compiled_long32") == NULL)
7164 	eh_addr_size = 8;
7165       break;
7166 
7167     case EM_H8_300:
7168     case EM_H8_300H:
7169       switch (filedata->file_header.e_flags & EF_H8_MACH)
7170 	{
7171 	case E_H8_MACH_H8300:
7172 	case E_H8_MACH_H8300HN:
7173 	case E_H8_MACH_H8300SN:
7174 	case E_H8_MACH_H8300SXN:
7175 	  eh_addr_size = 2;
7176 	  break;
7177 	case E_H8_MACH_H8300H:
7178 	case E_H8_MACH_H8300S:
7179 	case E_H8_MACH_H8300SX:
7180 	  eh_addr_size = 4;
7181 	  break;
7182 	}
7183       break;
7184 
7185     case EM_M32C_OLD:
7186     case EM_M32C:
7187       switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7188 	{
7189 	case EF_M32C_CPU_M16C:
7190 	  eh_addr_size = 2;
7191 	  break;
7192 	}
7193       break;
7194     }
7195 
7196 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64)		\
7197   do									\
7198     {									\
7199       bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64;	\
7200       if (section->sh_entsize != expected_entsize)			\
7201 	{								\
7202 	  char buf[40];							\
7203 	  sprintf_vma (buf, section->sh_entsize);			\
7204 	  /* Note: coded this way so that there is a single string for  \
7205 	     translation.  */ \
7206 	  error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
7207 	  error (_("(Using the expected size of %u for the rest of this dump)\n"), \
7208 		   (unsigned) expected_entsize);			\
7209 	  section->sh_entsize = expected_entsize;			\
7210 	}								\
7211     }									\
7212   while (0)
7213 
7214 #define CHECK_ENTSIZE(section, i, type)					\
7215   CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type),	\
7216 			sizeof (Elf64_External_##type))
7217 
7218   for (i = 0, section = filedata->section_headers;
7219        i < filedata->file_header.e_shnum;
7220        i++, section++)
7221     {
7222       const char *name = section_name_print (filedata, section);
7223 
7224       /* Run some sanity checks on the headers and
7225 	 possibly fill in some file data as well.  */
7226       switch (section->sh_type)
7227 	{
7228 	case SHT_DYNSYM:
7229 	  if (filedata->dynamic_symbols != NULL)
7230 	    {
7231 	      error (_("File contains multiple dynamic symbol tables\n"));
7232 	      continue;
7233 	    }
7234 
7235 	  CHECK_ENTSIZE (section, i, Sym);
7236 	  filedata->dynamic_symbols
7237 	    = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7238 	  filedata->dynamic_symtab_section = section;
7239 	  break;
7240 
7241 	case SHT_STRTAB:
7242 	  if (streq (name, ".dynstr"))
7243 	    {
7244 	      if (filedata->dynamic_strings != NULL)
7245 		{
7246 		  error (_("File contains multiple dynamic string tables\n"));
7247 		  continue;
7248 		}
7249 
7250 	      filedata->dynamic_strings
7251 		= (char *) get_data (NULL, filedata, section->sh_offset,
7252 				     1, section->sh_size, _("dynamic strings"));
7253 	      filedata->dynamic_strings_length
7254 		= filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7255 	      filedata->dynamic_strtab_section = section;
7256 	    }
7257 	  break;
7258 
7259 	case SHT_SYMTAB_SHNDX:
7260 	  {
7261 	    elf_section_list * entry = xmalloc (sizeof * entry);
7262 
7263 	    entry->hdr = section;
7264 	    entry->next = filedata->symtab_shndx_list;
7265 	    filedata->symtab_shndx_list = entry;
7266 	  }
7267 	  break;
7268 
7269 	case SHT_SYMTAB:
7270 	  CHECK_ENTSIZE (section, i, Sym);
7271 	  break;
7272 
7273 	case SHT_GROUP:
7274 	  CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7275 	  break;
7276 
7277 	case SHT_REL:
7278 	  CHECK_ENTSIZE (section, i, Rel);
7279 	  if (do_checks && section->sh_size == 0)
7280 	    warn (_("Section '%s': zero-sized relocation section\n"), name);
7281 	  break;
7282 
7283 	case SHT_RELA:
7284 	  CHECK_ENTSIZE (section, i, Rela);
7285 	  if (do_checks && section->sh_size == 0)
7286 	    warn (_("Section '%s': zero-sized relocation section\n"), name);
7287 	  break;
7288 
7289 	case SHT_RELR:
7290 	  CHECK_ENTSIZE (section, i, Relr);
7291 	  break;
7292 
7293 	case SHT_NOTE:
7294 	case SHT_PROGBITS:
7295 	  /* Having a zero sized section is not illegal according to the
7296 	     ELF standard, but it might be an indication that something
7297 	     is wrong.  So issue a warning if we are running in lint mode.  */
7298 	  if (do_checks && section->sh_size == 0)
7299 	    warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7300 	  break;
7301 
7302 	default:
7303 	  break;
7304 	}
7305 
7306       if ((do_debugging || do_debug_info || do_debug_abbrevs
7307 	   || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7308 	   || do_debug_aranges || do_debug_frames || do_debug_macinfo
7309 	   || do_debug_str || do_debug_str_offsets || do_debug_loc
7310 	   || do_debug_ranges
7311 	   || do_debug_addr || do_debug_cu_index || do_debug_links)
7312 	  && (startswith (name, ".debug_")
7313 	      || startswith (name, ".zdebug_")))
7314 	{
7315           if (name[1] == 'z')
7316             name += sizeof (".zdebug_") - 1;
7317           else
7318             name += sizeof (".debug_") - 1;
7319 
7320 	  if (do_debugging
7321 	      || (do_debug_info     && startswith (name, "info"))
7322 	      || (do_debug_info     && startswith (name, "types"))
7323 	      || (do_debug_abbrevs  && startswith (name, "abbrev"))
7324 	      || (do_debug_lines    && strcmp (name, "line") == 0)
7325 	      || (do_debug_lines    && startswith (name, "line."))
7326 	      || (do_debug_pubnames && startswith (name, "pubnames"))
7327 	      || (do_debug_pubtypes && startswith (name, "pubtypes"))
7328 	      || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7329 	      || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7330 	      || (do_debug_aranges  && startswith (name, "aranges"))
7331 	      || (do_debug_ranges   && startswith (name, "ranges"))
7332 	      || (do_debug_ranges   && startswith (name, "rnglists"))
7333 	      || (do_debug_frames   && startswith (name, "frame"))
7334 	      || (do_debug_macinfo  && startswith (name, "macinfo"))
7335 	      || (do_debug_macinfo  && startswith (name, "macro"))
7336 	      || (do_debug_str      && startswith (name, "str"))
7337 	      || (do_debug_links    && startswith (name, "sup"))
7338 	      || (do_debug_str_offsets && startswith (name, "str_offsets"))
7339 	      || (do_debug_loc      && startswith (name, "loc"))
7340 	      || (do_debug_loc      && startswith (name, "loclists"))
7341 	      || (do_debug_addr     && startswith (name, "addr"))
7342 	      || (do_debug_cu_index && startswith (name, "cu_index"))
7343 	      || (do_debug_cu_index && startswith (name, "tu_index"))
7344 	      )
7345 	    request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7346 	}
7347       /* Linkonce section to be combined with .debug_info at link time.  */
7348       else if ((do_debugging || do_debug_info)
7349 	       && startswith (name, ".gnu.linkonce.wi."))
7350 	request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7351       else if (do_debug_frames && streq (name, ".eh_frame"))
7352 	request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7353       else if (do_gdb_index && (streq (name, ".gdb_index")
7354 				|| streq (name, ".debug_names")))
7355 	request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7356       /* Trace sections for Itanium VMS.  */
7357       else if ((do_debugging || do_trace_info || do_trace_abbrevs
7358                 || do_trace_aranges)
7359 	       && startswith (name, ".trace_"))
7360 	{
7361           name += sizeof (".trace_") - 1;
7362 
7363 	  if (do_debugging
7364 	      || (do_trace_info     && streq (name, "info"))
7365 	      || (do_trace_abbrevs  && streq (name, "abbrev"))
7366 	      || (do_trace_aranges  && streq (name, "aranges"))
7367 	      )
7368 	    request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7369 	}
7370       else if ((do_debugging || do_debug_links)
7371 	       && (startswith (name, ".gnu_debuglink")
7372 		   || startswith (name, ".gnu_debugaltlink")))
7373 	request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7374     }
7375 
7376   if (! do_sections)
7377     return true;
7378 
7379   if (filedata->is_separate && ! process_links)
7380     return true;
7381 
7382   if (filedata->is_separate)
7383     printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7384   else if (filedata->file_header.e_shnum > 1)
7385     printf (_("\nSection Headers:\n"));
7386   else
7387     printf (_("\nSection Header:\n"));
7388 
7389   if (is_32bit_elf)
7390     {
7391       if (do_section_details)
7392 	{
7393 	  printf (_("  [Nr] Name\n"));
7394 	  printf (_("       Type            Addr     Off    Size   ES   Lk Inf Al\n"));
7395 	}
7396       else
7397 	printf
7398 	  (_("  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al\n"));
7399     }
7400   else if (do_wide)
7401     {
7402       if (do_section_details)
7403 	{
7404 	  printf (_("  [Nr] Name\n"));
7405 	  printf (_("       Type            Address          Off    Size   ES   Lk Inf Al\n"));
7406 	}
7407       else
7408 	printf
7409 	  (_("  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al\n"));
7410     }
7411   else
7412     {
7413       if (do_section_details)
7414 	{
7415 	  printf (_("  [Nr] Name\n"));
7416 	  printf (_("       Type              Address          Offset            Link\n"));
7417 	  printf (_("       Size              EntSize          Info              Align\n"));
7418 	}
7419       else
7420 	{
7421 	  printf (_("  [Nr] Name              Type             Address           Offset\n"));
7422 	  printf (_("       Size              EntSize          Flags  Link  Info  Align\n"));
7423 	}
7424     }
7425 
7426   if (do_section_details)
7427     printf (_("       Flags\n"));
7428 
7429   for (i = 0, section = filedata->section_headers;
7430        i < filedata->file_header.e_shnum;
7431        i++, section++)
7432     {
7433       /* Run some sanity checks on the section header.  */
7434 
7435       /* Check the sh_link field.  */
7436       switch (section->sh_type)
7437 	{
7438 	case SHT_REL:
7439 	case SHT_RELA:
7440 	  if (section->sh_link == 0
7441 	      && (filedata->file_header.e_type == ET_EXEC
7442 		  || filedata->file_header.e_type == ET_DYN))
7443 	    /* A dynamic relocation section where all entries use a
7444 	       zero symbol index need not specify a symtab section.  */
7445 	    break;
7446 	  /* Fall through.  */
7447 	case SHT_SYMTAB_SHNDX:
7448 	case SHT_GROUP:
7449 	case SHT_HASH:
7450 	case SHT_GNU_HASH:
7451 	case SHT_GNU_versym:
7452 	  if (section->sh_link == 0
7453 	      || section->sh_link >= filedata->file_header.e_shnum
7454 	      || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7455 		  && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7456 	    warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7457 		  i, section->sh_link);
7458 	  break;
7459 
7460 	case SHT_DYNAMIC:
7461 	case SHT_SYMTAB:
7462 	case SHT_DYNSYM:
7463 	case SHT_GNU_verneed:
7464 	case SHT_GNU_verdef:
7465 	case SHT_GNU_LIBLIST:
7466 	  if (section->sh_link == 0
7467 	      || section->sh_link >= filedata->file_header.e_shnum
7468 	      || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
7469 	    warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7470 		  i, section->sh_link);
7471 	  break;
7472 
7473 	case SHT_INIT_ARRAY:
7474 	case SHT_FINI_ARRAY:
7475 	case SHT_PREINIT_ARRAY:
7476 	  if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7477 	    warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7478 		  i, section->sh_link);
7479 	  break;
7480 
7481 	default:
7482 	  /* FIXME: Add support for target specific section types.  */
7483 #if 0 	  /* Currently we do not check other section types as there are too
7484 	     many special cases.  Stab sections for example have a type
7485 	     of SHT_PROGBITS but an sh_link field that links to the .stabstr
7486 	     section.  */
7487 	  if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7488 	    warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7489 		  i, section->sh_link);
7490 #endif
7491 	  break;
7492 	}
7493 
7494       /* Check the sh_info field.  */
7495       switch (section->sh_type)
7496 	{
7497 	case SHT_REL:
7498 	case SHT_RELA:
7499 	  if (section->sh_info == 0
7500 	      && (filedata->file_header.e_type == ET_EXEC
7501 		  || filedata->file_header.e_type == ET_DYN))
7502 	    /* Dynamic relocations apply to segments, so they do not
7503 	       need to specify the section they relocate.  */
7504 	    break;
7505 	  if (section->sh_info == 0
7506 	      || section->sh_info >= filedata->file_header.e_shnum
7507 	      || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7508 		  && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7509 		  && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7510 		  && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7511 		  && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7512 		  && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7513 		  /* FIXME: Are other section types valid ?  */
7514 		  && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7515 	    warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7516 		  i, section->sh_info);
7517 	  break;
7518 
7519 	case SHT_DYNAMIC:
7520 	case SHT_HASH:
7521 	case SHT_SYMTAB_SHNDX:
7522 	case SHT_INIT_ARRAY:
7523 	case SHT_FINI_ARRAY:
7524 	case SHT_PREINIT_ARRAY:
7525 	  if (section->sh_info != 0)
7526 	    warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7527 		  i, section->sh_info);
7528 	  break;
7529 
7530 	case SHT_GROUP:
7531 	case SHT_SYMTAB:
7532 	case SHT_DYNSYM:
7533 	  /* A symbol index - we assume that it is valid.  */
7534 	  break;
7535 
7536 	default:
7537 	  /* FIXME: Add support for target specific section types.  */
7538 	  if (section->sh_type == SHT_NOBITS)
7539 	    /* NOBITS section headers with non-zero sh_info fields can be
7540 	       created when a binary is stripped of everything but its debug
7541 	       information.  The stripped sections have their headers
7542 	       preserved but their types set to SHT_NOBITS.  So do not check
7543 	       this type of section.  */
7544 	    ;
7545 	  else if (section->sh_flags & SHF_INFO_LINK)
7546 	    {
7547 	      if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7548 		warn (_("[%2u]: Expected link to another section in info field"), i);
7549 	    }
7550 	  else if (section->sh_type < SHT_LOOS
7551 		   && (section->sh_flags & SHF_GNU_MBIND) == 0
7552 		   && section->sh_info != 0)
7553 	    warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7554 		  i, section->sh_info);
7555 	  break;
7556 	}
7557 
7558       /* Check the sh_size field.  */
7559       if (section->sh_size > filedata->file_size
7560 	  && section->sh_type != SHT_NOBITS
7561 	  && section->sh_type != SHT_NULL
7562 	  && section->sh_type < SHT_LOOS)
7563 	warn (_("Size of section %u is larger than the entire file!\n"), i);
7564 
7565       printf ("  [%2u] ", i);
7566       if (do_section_details)
7567 	printf ("%s\n      ", printable_section_name (filedata, section));
7568       else
7569 	print_symbol (-17, section_name_print (filedata, section));
7570 
7571       printf (do_wide ? " %-15s " : " %-15.15s ",
7572 	      get_section_type_name (filedata, section->sh_type));
7573 
7574       if (is_32bit_elf)
7575 	{
7576 	  const char * link_too_big = NULL;
7577 
7578 	  print_vma (section->sh_addr, LONG_HEX);
7579 
7580 	  printf ( " %6.6lx %6.6lx %2.2lx",
7581 		   (unsigned long) section->sh_offset,
7582 		   (unsigned long) section->sh_size,
7583 		   (unsigned long) section->sh_entsize);
7584 
7585 	  if (do_section_details)
7586 	    fputs ("  ", stdout);
7587 	  else
7588 	    printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7589 
7590 	  if (section->sh_link >= filedata->file_header.e_shnum)
7591 	    {
7592 	      link_too_big = "";
7593 	      /* The sh_link value is out of range.  Normally this indicates
7594 		 an error but it can have special values in Solaris binaries.  */
7595 	      switch (filedata->file_header.e_machine)
7596 		{
7597 		case EM_386:
7598 		case EM_IAMCU:
7599 		case EM_X86_64:
7600 		case EM_L1OM:
7601 		case EM_K1OM:
7602 		case EM_OLD_SPARCV9:
7603 		case EM_SPARC32PLUS:
7604 		case EM_SPARCV9:
7605 		case EM_SPARC:
7606 		  if (section->sh_link == (SHN_BEFORE & 0xffff))
7607 		    link_too_big = "BEFORE";
7608 		  else if (section->sh_link == (SHN_AFTER & 0xffff))
7609 		    link_too_big = "AFTER";
7610 		  break;
7611 		default:
7612 		  break;
7613 		}
7614 	    }
7615 
7616 	  if (do_section_details)
7617 	    {
7618 	      if (link_too_big != NULL && * link_too_big)
7619 		printf ("<%s> ", link_too_big);
7620 	      else
7621 		printf ("%2u ", section->sh_link);
7622 	      printf ("%3u %2lu\n", section->sh_info,
7623 		      (unsigned long) section->sh_addralign);
7624 	    }
7625 	  else
7626 	    printf ("%2u %3u %2lu\n",
7627 		    section->sh_link,
7628 		    section->sh_info,
7629 		    (unsigned long) section->sh_addralign);
7630 
7631 	  if (link_too_big && ! * link_too_big)
7632 	    warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7633 		  i, section->sh_link);
7634 	}
7635       else if (do_wide)
7636 	{
7637 	  print_vma (section->sh_addr, LONG_HEX);
7638 
7639 	  if ((long) section->sh_offset == section->sh_offset)
7640 	    printf (" %6.6lx", (unsigned long) section->sh_offset);
7641 	  else
7642 	    {
7643 	      putchar (' ');
7644 	      print_vma (section->sh_offset, LONG_HEX);
7645 	    }
7646 
7647 	  if ((unsigned long) section->sh_size == section->sh_size)
7648 	    printf (" %6.6lx", (unsigned long) section->sh_size);
7649 	  else
7650 	    {
7651 	      putchar (' ');
7652 	      print_vma (section->sh_size, LONG_HEX);
7653 	    }
7654 
7655 	  if ((unsigned long) section->sh_entsize == section->sh_entsize)
7656 	    printf (" %2.2lx", (unsigned long) section->sh_entsize);
7657 	  else
7658 	    {
7659 	      putchar (' ');
7660 	      print_vma (section->sh_entsize, LONG_HEX);
7661 	    }
7662 
7663 	  if (do_section_details)
7664 	    fputs ("  ", stdout);
7665 	  else
7666 	    printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7667 
7668 	  printf ("%2u %3u ", section->sh_link, section->sh_info);
7669 
7670 	  if ((unsigned long) section->sh_addralign == section->sh_addralign)
7671 	    printf ("%2lu\n", (unsigned long) section->sh_addralign);
7672 	  else
7673 	    {
7674 	      print_vma (section->sh_addralign, DEC);
7675 	      putchar ('\n');
7676 	    }
7677 	}
7678       else if (do_section_details)
7679 	{
7680 	  putchar (' ');
7681 	  print_vma (section->sh_addr, LONG_HEX);
7682 	  if ((long) section->sh_offset == section->sh_offset)
7683 	    printf ("  %16.16lx", (unsigned long) section->sh_offset);
7684 	  else
7685 	    {
7686 	      printf ("  ");
7687 	      print_vma (section->sh_offset, LONG_HEX);
7688 	    }
7689 	  printf ("  %u\n       ", section->sh_link);
7690 	  print_vma (section->sh_size, LONG_HEX);
7691 	  putchar (' ');
7692 	  print_vma (section->sh_entsize, LONG_HEX);
7693 
7694 	  printf ("  %-16u  %lu\n",
7695 		  section->sh_info,
7696 		  (unsigned long) section->sh_addralign);
7697 	}
7698       else
7699 	{
7700 	  putchar (' ');
7701 	  print_vma (section->sh_addr, LONG_HEX);
7702 	  if ((long) section->sh_offset == section->sh_offset)
7703 	    printf ("  %8.8lx", (unsigned long) section->sh_offset);
7704 	  else
7705 	    {
7706 	      printf ("  ");
7707 	      print_vma (section->sh_offset, LONG_HEX);
7708 	    }
7709 	  printf ("\n       ");
7710 	  print_vma (section->sh_size, LONG_HEX);
7711 	  printf ("  ");
7712 	  print_vma (section->sh_entsize, LONG_HEX);
7713 
7714 	  printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7715 
7716 	  printf ("     %2u   %3u     %lu\n",
7717 		  section->sh_link,
7718 		  section->sh_info,
7719 		  (unsigned long) section->sh_addralign);
7720 	}
7721 
7722       if (do_section_details)
7723 	{
7724 	  printf ("       %s\n", get_elf_section_flags (filedata, section->sh_flags));
7725 	  if ((section->sh_flags & SHF_COMPRESSED) != 0)
7726 	    {
7727 	      /* Minimum section size is 12 bytes for 32-bit compression
7728 		 header + 12 bytes for compressed data header.  */
7729 	      unsigned char buf[24];
7730 
7731 	      assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7732 	      if (get_data (&buf, filedata, section->sh_offset, 1,
7733 			    sizeof (buf), _("compression header")))
7734 		{
7735 		  Elf_Internal_Chdr chdr;
7736 
7737 		  if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7738 		    printf (_("       [<corrupt>]\n"));
7739 		  else
7740 		    {
7741 		      if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7742 			printf ("       ZLIB, ");
7743 		      else
7744 			printf (_("       [<unknown>: 0x%x], "),
7745 				chdr.ch_type);
7746 		      print_vma (chdr.ch_size, LONG_HEX);
7747 		      printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7748 		    }
7749 		}
7750 	    }
7751 	}
7752     }
7753 
7754   if (!do_section_details)
7755     {
7756       /* The ordering of the letters shown here matches the ordering of the
7757 	 corresponding SHF_xxx values, and hence the order in which these
7758 	 letters will be displayed to the user.  */
7759       printf (_("Key to Flags:\n\
7760   W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7761   L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7762   C (compressed), x (unknown), o (OS specific), E (exclude),\n  "));
7763       switch (filedata->file_header.e_ident[EI_OSABI])
7764 	{
7765 	case ELFOSABI_GNU:
7766 	case ELFOSABI_FREEBSD:
7767 	  printf (_("R (retain), "));
7768 	  /* Fall through */
7769 	case ELFOSABI_NONE:
7770 	  printf (_("D (mbind), "));
7771 	  break;
7772 	default:
7773 	  break;
7774 	}
7775       if (filedata->file_header.e_machine == EM_X86_64
7776 	  || filedata->file_header.e_machine == EM_L1OM
7777 	  || filedata->file_header.e_machine == EM_K1OM)
7778 	printf (_("l (large), "));
7779       else if (filedata->file_header.e_machine == EM_ARM)
7780 	printf (_("y (purecode), "));
7781       else if (filedata->file_header.e_machine == EM_PPC)
7782 	printf (_("v (VLE), "));
7783       printf ("p (processor specific)\n");
7784     }
7785 
7786   return true;
7787 }
7788 
7789 static bool
7790 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7791 	    Elf_Internal_Sym **symtab, unsigned long *nsyms,
7792 	    char **strtab, unsigned long *strtablen)
7793 {
7794   *strtab = NULL;
7795   *strtablen = 0;
7796   *symtab = get_elf_symbols (filedata, symsec, nsyms);
7797 
7798   if (*symtab == NULL)
7799     return false;
7800 
7801   if (symsec->sh_link != 0)
7802     {
7803       Elf_Internal_Shdr *strsec;
7804 
7805       if (symsec->sh_link >= filedata->file_header.e_shnum)
7806 	{
7807 	  error (_("Bad sh_link in symbol table section\n"));
7808 	  free (*symtab);
7809 	  *symtab = NULL;
7810 	  *nsyms = 0;
7811 	  return false;
7812 	}
7813 
7814       strsec = filedata->section_headers + symsec->sh_link;
7815 
7816       *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7817 				   1, strsec->sh_size, _("string table"));
7818       if (*strtab == NULL)
7819 	{
7820 	  free (*symtab);
7821 	  *symtab = NULL;
7822 	  *nsyms = 0;
7823 	  return false;
7824 	}
7825       *strtablen = strsec->sh_size;
7826     }
7827   return true;
7828 }
7829 
7830 static const char *
7831 get_group_flags (unsigned int flags)
7832 {
7833   static char buff[128];
7834 
7835   if (flags == 0)
7836     return "";
7837   else if (flags == GRP_COMDAT)
7838     return "COMDAT ";
7839 
7840   snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7841 	    flags,
7842 	    flags & GRP_MASKOS ? _("<OS specific>") : "",
7843 	    flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7844 	    (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7845 	     ? _("<unknown>") : ""));
7846 
7847   return buff;
7848 }
7849 
7850 static bool
7851 process_section_groups (Filedata * filedata)
7852 {
7853   Elf_Internal_Shdr * section;
7854   unsigned int i;
7855   struct group * group;
7856   Elf_Internal_Shdr * symtab_sec;
7857   Elf_Internal_Shdr * strtab_sec;
7858   Elf_Internal_Sym * symtab;
7859   unsigned long num_syms;
7860   char * strtab;
7861   size_t strtab_size;
7862 
7863   /* Don't process section groups unless needed.  */
7864   if (!do_unwind && !do_section_groups)
7865     return true;
7866 
7867   if (filedata->file_header.e_shnum == 0)
7868     {
7869       if (do_section_groups)
7870 	{
7871 	  if (filedata->is_separate)
7872 	    printf (_("\nThere are no sections group in linked file '%s'.\n"),
7873 		    filedata->file_name);
7874 	  else
7875 	    printf (_("\nThere are no section groups in this file.\n"));
7876 	}
7877       return true;
7878     }
7879 
7880   if (filedata->section_headers == NULL)
7881     {
7882       error (_("Section headers are not available!\n"));
7883       /* PR 13622: This can happen with a corrupt ELF header.  */
7884       return false;
7885     }
7886 
7887   filedata->section_headers_groups
7888     = (struct group **) calloc (filedata->file_header.e_shnum,
7889 				sizeof (struct group *));
7890 
7891   if (filedata->section_headers_groups == NULL)
7892     {
7893       error (_("Out of memory reading %u section group headers\n"),
7894 	     filedata->file_header.e_shnum);
7895       return false;
7896     }
7897 
7898   /* Scan the sections for the group section.  */
7899   filedata->group_count = 0;
7900   for (i = 0, section = filedata->section_headers;
7901        i < filedata->file_header.e_shnum;
7902        i++, section++)
7903     if (section->sh_type == SHT_GROUP)
7904       filedata->group_count++;
7905 
7906   if (filedata->group_count == 0)
7907     {
7908       if (do_section_groups)
7909 	{
7910 	  if (filedata->is_separate)
7911 	    printf (_("\nThere are no section groups in linked file '%s'.\n"),
7912 		    filedata->file_name);
7913 	  else
7914 	    printf (_("\nThere are no section groups in this file.\n"));
7915 	}
7916 
7917       return true;
7918     }
7919 
7920   filedata->section_groups = (struct group *) calloc (filedata->group_count,
7921 						      sizeof (struct group));
7922 
7923   if (filedata->section_groups == NULL)
7924     {
7925       error (_("Out of memory reading %lu groups\n"),
7926 	     (unsigned long) filedata->group_count);
7927       return false;
7928     }
7929 
7930   symtab_sec = NULL;
7931   strtab_sec = NULL;
7932   symtab = NULL;
7933   num_syms = 0;
7934   strtab = NULL;
7935   strtab_size = 0;
7936 
7937   if (filedata->is_separate)
7938     printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7939 
7940   for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7941        i < filedata->file_header.e_shnum;
7942        i++, section++)
7943     {
7944       if (section->sh_type == SHT_GROUP)
7945 	{
7946 	  const char * name = printable_section_name (filedata, section);
7947 	  const char * group_name;
7948 	  unsigned char * start;
7949 	  unsigned char * indices;
7950 	  unsigned int entry, j, size;
7951 	  Elf_Internal_Shdr * sec;
7952 	  Elf_Internal_Sym * sym;
7953 
7954 	  /* Get the symbol table.  */
7955 	  if (section->sh_link >= filedata->file_header.e_shnum
7956 	      || ((sec = filedata->section_headers + section->sh_link)->sh_type
7957 		  != SHT_SYMTAB))
7958 	    {
7959 	      error (_("Bad sh_link in group section `%s'\n"), name);
7960 	      continue;
7961 	    }
7962 
7963 	  if (symtab_sec != sec)
7964 	    {
7965 	      symtab_sec = sec;
7966 	      free (symtab);
7967 	      symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7968 	    }
7969 
7970 	  if (symtab == NULL)
7971 	    {
7972 	      error (_("Corrupt header in group section `%s'\n"), name);
7973 	      continue;
7974 	    }
7975 
7976 	  if (section->sh_info >= num_syms)
7977 	    {
7978 	      error (_("Bad sh_info in group section `%s'\n"), name);
7979 	      continue;
7980 	    }
7981 
7982 	  sym = symtab + section->sh_info;
7983 
7984 	  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7985 	    {
7986 	      if (sym->st_shndx == 0
7987 		  || sym->st_shndx >= filedata->file_header.e_shnum)
7988 		{
7989 		  error (_("Bad sh_info in group section `%s'\n"), name);
7990 		  continue;
7991 		}
7992 
7993 	      group_name = section_name_print (filedata,
7994 					       filedata->section_headers
7995 					       + sym->st_shndx);
7996 	      strtab_sec = NULL;
7997 	      free (strtab);
7998 	      strtab = NULL;
7999 	      strtab_size = 0;
8000 	    }
8001 	  else
8002 	    {
8003 	      /* Get the string table.  */
8004 	      if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
8005 		{
8006 		  strtab_sec = NULL;
8007 		  free (strtab);
8008 		  strtab = NULL;
8009 		  strtab_size = 0;
8010 		}
8011 	      else if (strtab_sec
8012 		       != (sec = filedata->section_headers + symtab_sec->sh_link))
8013 		{
8014 		  strtab_sec = sec;
8015 		  free (strtab);
8016 
8017 		  strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8018 					      1, strtab_sec->sh_size,
8019 					      _("string table"));
8020 		  strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8021 		}
8022 	      group_name = sym->st_name < strtab_size
8023 		? strtab + sym->st_name : _("<corrupt>");
8024 	    }
8025 
8026 	  /* PR 17531: file: loop.  */
8027 	  if (section->sh_entsize > section->sh_size)
8028 	    {
8029 	      error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
8030 		     printable_section_name (filedata, section),
8031 		     (unsigned long) section->sh_entsize,
8032 		     (unsigned long) section->sh_size);
8033 	      continue;
8034 	    }
8035 
8036 	  start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8037                                               1, section->sh_size,
8038                                               _("section data"));
8039 	  if (start == NULL)
8040 	    continue;
8041 
8042 	  indices = start;
8043 	  size = (section->sh_size / section->sh_entsize) - 1;
8044 	  entry = byte_get (indices, 4);
8045 	  indices += 4;
8046 
8047 	  if (do_section_groups)
8048 	    {
8049 	      printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8050 		      get_group_flags (entry), i, name, group_name, size);
8051 
8052 	      printf (_("   [Index]    Name\n"));
8053 	    }
8054 
8055 	  group->group_index = i;
8056 
8057 	  for (j = 0; j < size; j++)
8058 	    {
8059 	      struct group_list * g;
8060 
8061 	      entry = byte_get (indices, 4);
8062 	      indices += 4;
8063 
8064 	      if (entry >= filedata->file_header.e_shnum)
8065 		{
8066 		  static unsigned num_group_errors = 0;
8067 
8068 		  if (num_group_errors ++ < 10)
8069 		    {
8070 		      error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8071 			     entry, i, filedata->file_header.e_shnum - 1);
8072 		      if (num_group_errors == 10)
8073 			warn (_("Further error messages about overlarge group section indices suppressed\n"));
8074 		    }
8075 		  continue;
8076 		}
8077 
8078 	      if (filedata->section_headers_groups [entry] != NULL)
8079 		{
8080 		  if (entry)
8081 		    {
8082 		      static unsigned num_errs = 0;
8083 
8084 		      if (num_errs ++ < 10)
8085 			{
8086 			  error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8087 				 entry, i,
8088 				 filedata->section_headers_groups [entry]->group_index);
8089 			  if (num_errs == 10)
8090 			    warn (_("Further error messages about already contained group sections suppressed\n"));
8091 			}
8092 		      continue;
8093 		    }
8094 		  else
8095 		    {
8096 		      /* Intel C/C++ compiler may put section 0 in a
8097 			 section group.  We just warn it the first time
8098 			 and ignore it afterwards.  */
8099 		      static bool warned = false;
8100 		      if (!warned)
8101 			{
8102 			  error (_("section 0 in group section [%5u]\n"),
8103 				 filedata->section_headers_groups [entry]->group_index);
8104 			  warned = true;
8105 			}
8106 		    }
8107 		}
8108 
8109 	      filedata->section_headers_groups [entry] = group;
8110 
8111 	      if (do_section_groups)
8112 		{
8113 		  sec = filedata->section_headers + entry;
8114 		  printf ("   [%5u]   %s\n", entry, printable_section_name (filedata, sec));
8115 		}
8116 
8117 	      g = (struct group_list *) xmalloc (sizeof (struct group_list));
8118 	      g->section_index = entry;
8119 	      g->next = group->root;
8120 	      group->root = g;
8121 	    }
8122 
8123 	  free (start);
8124 
8125 	  group++;
8126 	}
8127     }
8128 
8129   free (symtab);
8130   free (strtab);
8131   return true;
8132 }
8133 
8134 /* Data used to display dynamic fixups.  */
8135 
8136 struct ia64_vms_dynfixup
8137 {
8138   bfd_vma needed_ident;		/* Library ident number.  */
8139   bfd_vma needed;		/* Index in the dstrtab of the library name.  */
8140   bfd_vma fixup_needed;		/* Index of the library.  */
8141   bfd_vma fixup_rela_cnt;	/* Number of fixups.  */
8142   bfd_vma fixup_rela_off;	/* Fixups offset in the dynamic segment.  */
8143 };
8144 
8145 /* Data used to display dynamic relocations.  */
8146 
8147 struct ia64_vms_dynimgrela
8148 {
8149   bfd_vma img_rela_cnt;		/* Number of relocations.  */
8150   bfd_vma img_rela_off;		/* Reloc offset in the dynamic segment.  */
8151 };
8152 
8153 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8154    library).  */
8155 
8156 static bool
8157 dump_ia64_vms_dynamic_fixups (Filedata *                  filedata,
8158 			      struct ia64_vms_dynfixup *  fixup,
8159                               const char *                strtab,
8160 			      unsigned int                strtab_sz)
8161 {
8162   Elf64_External_VMS_IMAGE_FIXUP * imfs;
8163   long i;
8164   const char * lib_name;
8165 
8166   imfs = get_data (NULL, filedata,
8167 		   filedata->dynamic_addr + fixup->fixup_rela_off,
8168 		   sizeof (*imfs), fixup->fixup_rela_cnt,
8169 		   _("dynamic section image fixups"));
8170   if (!imfs)
8171     return false;
8172 
8173   if (fixup->needed < strtab_sz)
8174     lib_name = strtab + fixup->needed;
8175   else
8176     {
8177       warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
8178             (unsigned long) fixup->needed);
8179       lib_name = "???";
8180     }
8181 
8182   printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
8183 	  (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
8184   printf
8185     (_("Seg Offset           Type                             SymVec DataType\n"));
8186 
8187   for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
8188     {
8189       unsigned int type;
8190       const char *rtype;
8191 
8192       printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8193       printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
8194       type = BYTE_GET (imfs [i].type);
8195       rtype = elf_ia64_reloc_type (type);
8196       if (rtype == NULL)
8197         printf (" 0x%08x                       ", type);
8198       else
8199         printf (" %-32s ", rtype);
8200       printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8201       printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8202     }
8203 
8204   free (imfs);
8205   return true;
8206 }
8207 
8208 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image).  */
8209 
8210 static bool
8211 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8212 {
8213   Elf64_External_VMS_IMAGE_RELA *imrs;
8214   long i;
8215 
8216   imrs = get_data (NULL, filedata,
8217 		   filedata->dynamic_addr + imgrela->img_rela_off,
8218 		   sizeof (*imrs), imgrela->img_rela_cnt,
8219 		   _("dynamic section image relocations"));
8220   if (!imrs)
8221     return false;
8222 
8223   printf (_("\nImage relocs\n"));
8224   printf
8225     (_("Seg Offset   Type                            Addend            Seg Sym Off\n"));
8226 
8227   for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
8228     {
8229       unsigned int type;
8230       const char *rtype;
8231 
8232       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8233       printf ("%08" BFD_VMA_FMT "x ",
8234               (bfd_vma) BYTE_GET (imrs [i].rela_offset));
8235       type = BYTE_GET (imrs [i].type);
8236       rtype = elf_ia64_reloc_type (type);
8237       if (rtype == NULL)
8238         printf ("0x%08x                      ", type);
8239       else
8240         printf ("%-31s ", rtype);
8241       print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8242       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8243       printf ("%08" BFD_VMA_FMT "x\n",
8244               (bfd_vma) BYTE_GET (imrs [i].sym_offset));
8245     }
8246 
8247   free (imrs);
8248   return true;
8249 }
8250 
8251 /* Display IA-64 OpenVMS dynamic relocations and fixups.  */
8252 
8253 static bool
8254 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8255 {
8256   struct ia64_vms_dynfixup fixup;
8257   struct ia64_vms_dynimgrela imgrela;
8258   Elf_Internal_Dyn *entry;
8259   bfd_vma strtab_off = 0;
8260   bfd_vma strtab_sz = 0;
8261   char *strtab = NULL;
8262   bool res = true;
8263 
8264   memset (&fixup, 0, sizeof (fixup));
8265   memset (&imgrela, 0, sizeof (imgrela));
8266 
8267   /* Note: the order of the entries is specified by the OpenVMS specs.  */
8268   for (entry = filedata->dynamic_section;
8269        entry < filedata->dynamic_section + filedata->dynamic_nent;
8270        entry++)
8271     {
8272       switch (entry->d_tag)
8273         {
8274         case DT_IA_64_VMS_STRTAB_OFFSET:
8275           strtab_off = entry->d_un.d_val;
8276           break;
8277         case DT_STRSZ:
8278           strtab_sz = entry->d_un.d_val;
8279           if (strtab == NULL)
8280 	    strtab = get_data (NULL, filedata,
8281 			       filedata->dynamic_addr + strtab_off,
8282                                1, strtab_sz, _("dynamic string section"));
8283 	  if (strtab == NULL)
8284 	    strtab_sz = 0;
8285           break;
8286 
8287         case DT_IA_64_VMS_NEEDED_IDENT:
8288           fixup.needed_ident = entry->d_un.d_val;
8289           break;
8290         case DT_NEEDED:
8291           fixup.needed = entry->d_un.d_val;
8292           break;
8293         case DT_IA_64_VMS_FIXUP_NEEDED:
8294           fixup.fixup_needed = entry->d_un.d_val;
8295           break;
8296         case DT_IA_64_VMS_FIXUP_RELA_CNT:
8297           fixup.fixup_rela_cnt = entry->d_un.d_val;
8298           break;
8299         case DT_IA_64_VMS_FIXUP_RELA_OFF:
8300           fixup.fixup_rela_off = entry->d_un.d_val;
8301           if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8302 	    res = false;
8303           break;
8304         case DT_IA_64_VMS_IMG_RELA_CNT:
8305 	  imgrela.img_rela_cnt = entry->d_un.d_val;
8306           break;
8307         case DT_IA_64_VMS_IMG_RELA_OFF:
8308 	  imgrela.img_rela_off = entry->d_un.d_val;
8309           if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8310 	    res = false;
8311           break;
8312 
8313         default:
8314           break;
8315 	}
8316     }
8317 
8318   free (strtab);
8319 
8320   return res;
8321 }
8322 
8323 static struct
8324 {
8325   const char * name;
8326   int reloc;
8327   int size;
8328   relocation_type rel_type;
8329 }
8330   dynamic_relocations [] =
8331 {
8332   { "REL", DT_REL, DT_RELSZ, reltype_rel },
8333   { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8334   { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8335   { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8336 };
8337 
8338 /* Process the reloc section.  */
8339 
8340 static bool
8341 process_relocs (Filedata * filedata)
8342 {
8343   unsigned long rel_size;
8344   unsigned long rel_offset;
8345 
8346   if (!do_reloc)
8347     return true;
8348 
8349   if (do_using_dynamic)
8350     {
8351       relocation_type rel_type;
8352       const char * name;
8353       bool  has_dynamic_reloc;
8354       unsigned int i;
8355 
8356       has_dynamic_reloc = false;
8357 
8358       for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8359 	{
8360 	  rel_type = dynamic_relocations [i].rel_type;
8361 	  name = dynamic_relocations [i].name;
8362 	  rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8363 	  rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8364 
8365 	  if (rel_size)
8366 	    has_dynamic_reloc = true;
8367 
8368 	  if (rel_type == reltype_unknown)
8369 	    {
8370 	      if (dynamic_relocations [i].reloc == DT_JMPREL)
8371 		switch (filedata->dynamic_info[DT_PLTREL])
8372 		  {
8373 		  case DT_REL:
8374 		    rel_type = reltype_rel;
8375 		    break;
8376 		  case DT_RELA:
8377 		    rel_type = reltype_rela;
8378 		    break;
8379 		  }
8380 	    }
8381 
8382 	  if (rel_size)
8383 	    {
8384 	      if (filedata->is_separate)
8385 		printf
8386 		  (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
8387 		   filedata->file_name, name, rel_offset, rel_size);
8388 	      else
8389 		printf
8390 		  (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
8391 		   name, rel_offset, rel_size);
8392 
8393 	      dump_relocations (filedata,
8394 				offset_from_vma (filedata, rel_offset, rel_size),
8395 				rel_size,
8396 				filedata->dynamic_symbols,
8397 				filedata->num_dynamic_syms,
8398 				filedata->dynamic_strings,
8399 				filedata->dynamic_strings_length,
8400 				rel_type, true /* is_dynamic */);
8401 	    }
8402 	}
8403 
8404       if (is_ia64_vms (filedata))
8405         if (process_ia64_vms_dynamic_relocs (filedata))
8406 	  has_dynamic_reloc = true;
8407 
8408       if (! has_dynamic_reloc)
8409 	{
8410 	  if (filedata->is_separate)
8411 	    printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8412 		    filedata->file_name);
8413 	  else
8414 	    printf (_("\nThere are no dynamic relocations in this file.\n"));
8415 	}
8416     }
8417   else
8418     {
8419       Elf_Internal_Shdr * section;
8420       unsigned long i;
8421       bool found = false;
8422 
8423       for (i = 0, section = filedata->section_headers;
8424 	   i < filedata->file_header.e_shnum;
8425 	   i++, section++)
8426 	{
8427 	  if (   section->sh_type != SHT_RELA
8428 	      && section->sh_type != SHT_REL
8429 	      && section->sh_type != SHT_RELR)
8430 	    continue;
8431 
8432 	  rel_offset = section->sh_offset;
8433 	  rel_size   = section->sh_size;
8434 
8435 	  if (rel_size)
8436 	    {
8437 	      relocation_type rel_type;
8438 	      unsigned long num_rela;
8439 
8440 	      if (filedata->is_separate)
8441 		printf (_("\nIn linked file '%s' relocation section "),
8442 			filedata->file_name);
8443 	      else
8444 		printf (_("\nRelocation section "));
8445 
8446 	      if (filedata->string_table == NULL)
8447 		printf ("%d", section->sh_name);
8448 	      else
8449 		printf ("'%s'", printable_section_name (filedata, section));
8450 
8451 	      num_rela = rel_size / section->sh_entsize;
8452 	      printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
8453 				" at offset 0x%lx contains %lu entries:\n",
8454 				num_rela),
8455 		      rel_offset, num_rela);
8456 
8457 	      rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8458 		section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8459 
8460 	      if (section->sh_link != 0
8461 		  && section->sh_link < filedata->file_header.e_shnum)
8462 		{
8463 		  Elf_Internal_Shdr * symsec;
8464 		  Elf_Internal_Sym *  symtab;
8465 		  unsigned long nsyms;
8466 		  unsigned long strtablen = 0;
8467 		  char * strtab = NULL;
8468 
8469 		  symsec = filedata->section_headers + section->sh_link;
8470 		  if (symsec->sh_type != SHT_SYMTAB
8471 		      && symsec->sh_type != SHT_DYNSYM)
8472                     continue;
8473 
8474 		  if (!get_symtab (filedata, symsec,
8475 				   &symtab, &nsyms, &strtab, &strtablen))
8476 		    continue;
8477 
8478 		  dump_relocations (filedata, rel_offset, rel_size,
8479 				    symtab, nsyms, strtab, strtablen,
8480 				    rel_type,
8481 				    symsec->sh_type == SHT_DYNSYM);
8482 		  free (strtab);
8483 		  free (symtab);
8484 		}
8485 	      else
8486 		dump_relocations (filedata, rel_offset, rel_size,
8487 				  NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
8488 
8489 	      found = true;
8490 	    }
8491 	}
8492 
8493       if (! found)
8494 	{
8495 	  /* Users sometimes forget the -D option, so try to be helpful.  */
8496 	  for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8497 	    {
8498 	      if (filedata->dynamic_info[dynamic_relocations [i].size])
8499 		{
8500 		  if (filedata->is_separate)
8501 		    printf (_("\nThere are no static relocations in linked file '%s'."),
8502 			    filedata->file_name);
8503 		  else
8504 		    printf (_("\nThere are no static relocations in this file."));
8505 		  printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8506 
8507 		  break;
8508 		}
8509 	    }
8510 	  if (i == ARRAY_SIZE (dynamic_relocations))
8511 	    {
8512 	      if (filedata->is_separate)
8513 		printf (_("\nThere are no relocations in linked file '%s'.\n"),
8514 			filedata->file_name);
8515 	      else
8516 		printf (_("\nThere are no relocations in this file.\n"));
8517 	    }
8518 	}
8519     }
8520 
8521   return true;
8522 }
8523 
8524 /* An absolute address consists of a section and an offset.  If the
8525    section is NULL, the offset itself is the address, otherwise, the
8526    address equals to LOAD_ADDRESS(section) + offset.  */
8527 
8528 struct absaddr
8529 {
8530   unsigned short section;
8531   bfd_vma offset;
8532 };
8533 
8534 /* Find the nearest symbol at or below ADDR.  Returns the symbol
8535    name, if found, and the offset from the symbol to ADDR.  */
8536 
8537 static void
8538 find_symbol_for_address (Filedata *          filedata,
8539 			 Elf_Internal_Sym *  symtab,
8540 			 unsigned long       nsyms,
8541 			 const char *        strtab,
8542 			 unsigned long       strtab_size,
8543 			 struct absaddr      addr,
8544 			 const char **       symname,
8545 			 bfd_vma *           offset)
8546 {
8547   bfd_vma dist = 0x100000;
8548   Elf_Internal_Sym * sym;
8549   Elf_Internal_Sym * beg;
8550   Elf_Internal_Sym * end;
8551   Elf_Internal_Sym * best = NULL;
8552 
8553   REMOVE_ARCH_BITS (addr.offset);
8554   beg = symtab;
8555   end = symtab + nsyms;
8556 
8557   while (beg < end)
8558     {
8559       bfd_vma value;
8560 
8561       sym = beg + (end - beg) / 2;
8562 
8563       value = sym->st_value;
8564       REMOVE_ARCH_BITS (value);
8565 
8566       if (sym->st_name != 0
8567 	  && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8568 	  && addr.offset >= value
8569 	  && addr.offset - value < dist)
8570 	{
8571 	  best = sym;
8572 	  dist = addr.offset - value;
8573 	  if (!dist)
8574 	    break;
8575 	}
8576 
8577       if (addr.offset < value)
8578 	end = sym;
8579       else
8580 	beg = sym + 1;
8581     }
8582 
8583   if (best)
8584     {
8585       *symname = (best->st_name >= strtab_size
8586 		  ? _("<corrupt>") : strtab + best->st_name);
8587       *offset = dist;
8588       return;
8589     }
8590 
8591   *symname = NULL;
8592   *offset = addr.offset;
8593 }
8594 
8595 static /* signed */ int
8596 symcmp (const void *p, const void *q)
8597 {
8598   Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8599   Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8600 
8601   return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8602 }
8603 
8604 /* Process the unwind section.  */
8605 
8606 #include "unwind-ia64.h"
8607 
8608 struct ia64_unw_table_entry
8609 {
8610   struct absaddr start;
8611   struct absaddr end;
8612   struct absaddr info;
8613 };
8614 
8615 struct ia64_unw_aux_info
8616 {
8617   struct ia64_unw_table_entry * table;		/* Unwind table.  */
8618   unsigned long                 table_len;	/* Length of unwind table.  */
8619   unsigned char *               info;		/* Unwind info.  */
8620   unsigned long                 info_size;	/* Size of unwind info.  */
8621   bfd_vma                       info_addr;	/* Starting address of unwind info.  */
8622   bfd_vma                       seg_base;	/* Starting address of segment.  */
8623   Elf_Internal_Sym *            symtab;		/* The symbol table.  */
8624   unsigned long                 nsyms;		/* Number of symbols.  */
8625   Elf_Internal_Sym *            funtab;		/* Sorted table of STT_FUNC symbols.  */
8626   unsigned long                 nfuns;		/* Number of entries in funtab.  */
8627   char *                        strtab;		/* The string table.  */
8628   unsigned long                 strtab_size;	/* Size of string table.  */
8629 };
8630 
8631 static bool
8632 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8633 {
8634   struct ia64_unw_table_entry * tp;
8635   unsigned long j, nfuns;
8636   int in_body;
8637   bool res = true;
8638 
8639   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8640   for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8641     if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8642       aux->funtab[nfuns++] = aux->symtab[j];
8643   aux->nfuns = nfuns;
8644   qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8645 
8646   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8647     {
8648       bfd_vma stamp;
8649       bfd_vma offset;
8650       const unsigned char * dp;
8651       const unsigned char * head;
8652       const unsigned char * end;
8653       const char * procname;
8654 
8655       find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8656 			       aux->strtab_size, tp->start, &procname, &offset);
8657 
8658       fputs ("\n<", stdout);
8659 
8660       if (procname)
8661 	{
8662 	  fputs (procname, stdout);
8663 
8664 	  if (offset)
8665 	    printf ("+%lx", (unsigned long) offset);
8666 	}
8667 
8668       fputs (">: [", stdout);
8669       print_vma (tp->start.offset, PREFIX_HEX);
8670       fputc ('-', stdout);
8671       print_vma (tp->end.offset, PREFIX_HEX);
8672       printf ("], info at +0x%lx\n",
8673 	      (unsigned long) (tp->info.offset - aux->seg_base));
8674 
8675       /* PR 17531: file: 86232b32.  */
8676       if (aux->info == NULL)
8677 	continue;
8678 
8679       offset = tp->info.offset;
8680       if (tp->info.section)
8681 	{
8682 	  if (tp->info.section >= filedata->file_header.e_shnum)
8683 	    {
8684 	      warn (_("Invalid section %u in table entry %ld\n"),
8685 		    tp->info.section, (long) (tp - aux->table));
8686 	      res = false;
8687 	      continue;
8688 	    }
8689 	  offset += filedata->section_headers[tp->info.section].sh_addr;
8690 	}
8691       offset -= aux->info_addr;
8692       /* PR 17531: file: 0997b4d1.  */
8693       if (offset >= aux->info_size
8694 	  || aux->info_size - offset < 8)
8695 	{
8696 	  warn (_("Invalid offset %lx in table entry %ld\n"),
8697 		(long) tp->info.offset, (long) (tp - aux->table));
8698 	  res = false;
8699 	  continue;
8700 	}
8701 
8702       head = aux->info + offset;
8703       stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8704 
8705       printf ("  v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8706 	      (unsigned) UNW_VER (stamp),
8707 	      (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8708 	      UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8709 	      UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8710 	      (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8711 
8712       if (UNW_VER (stamp) != 1)
8713 	{
8714 	  printf (_("\tUnknown version.\n"));
8715 	  continue;
8716 	}
8717 
8718       in_body = 0;
8719       end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8720       /* PR 17531: file: 16ceda89.  */
8721       if (end > aux->info + aux->info_size)
8722 	end = aux->info + aux->info_size;
8723       for (dp = head + 8; dp < end;)
8724 	dp = unw_decode (dp, in_body, & in_body, end);
8725     }
8726 
8727   free (aux->funtab);
8728 
8729   return res;
8730 }
8731 
8732 static bool
8733 slurp_ia64_unwind_table (Filedata *                  filedata,
8734 			 struct ia64_unw_aux_info *  aux,
8735 			 Elf_Internal_Shdr *         sec)
8736 {
8737   unsigned long size, nrelas, i;
8738   Elf_Internal_Phdr * seg;
8739   struct ia64_unw_table_entry * tep;
8740   Elf_Internal_Shdr * relsec;
8741   Elf_Internal_Rela * rela;
8742   Elf_Internal_Rela * rp;
8743   unsigned char * table;
8744   unsigned char * tp;
8745   Elf_Internal_Sym * sym;
8746   const char * relname;
8747 
8748   aux->table_len = 0;
8749 
8750   /* First, find the starting address of the segment that includes
8751      this section: */
8752 
8753   if (filedata->file_header.e_phnum)
8754     {
8755       if (! get_program_headers (filedata))
8756 	  return false;
8757 
8758       for (seg = filedata->program_headers;
8759 	   seg < filedata->program_headers + filedata->file_header.e_phnum;
8760 	   ++seg)
8761 	{
8762 	  if (seg->p_type != PT_LOAD)
8763 	    continue;
8764 
8765 	  if (sec->sh_addr >= seg->p_vaddr
8766 	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8767 	    {
8768 	      aux->seg_base = seg->p_vaddr;
8769 	      break;
8770 	    }
8771 	}
8772     }
8773 
8774   /* Second, build the unwind table from the contents of the unwind section:  */
8775   size = sec->sh_size;
8776   table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8777                                       _("unwind table"));
8778   if (!table)
8779     return false;
8780 
8781   aux->table_len = size / (3 * eh_addr_size);
8782   aux->table = (struct ia64_unw_table_entry *)
8783     xcmalloc (aux->table_len, sizeof (aux->table[0]));
8784   tep = aux->table;
8785 
8786   for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8787     {
8788       tep->start.section = SHN_UNDEF;
8789       tep->end.section   = SHN_UNDEF;
8790       tep->info.section  = SHN_UNDEF;
8791       tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8792       tep->end.offset   = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8793       tep->info.offset  = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8794       tep->start.offset += aux->seg_base;
8795       tep->end.offset   += aux->seg_base;
8796       tep->info.offset  += aux->seg_base;
8797     }
8798   free (table);
8799 
8800   /* Third, apply any relocations to the unwind table:  */
8801   for (relsec = filedata->section_headers;
8802        relsec < filedata->section_headers + filedata->file_header.e_shnum;
8803        ++relsec)
8804     {
8805       if (relsec->sh_type != SHT_RELA
8806 	  || relsec->sh_info >= filedata->file_header.e_shnum
8807 	  || filedata->section_headers + relsec->sh_info != sec)
8808 	continue;
8809 
8810       if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8811 			      & rela, & nrelas))
8812 	{
8813 	  free (aux->table);
8814 	  aux->table = NULL;
8815 	  aux->table_len = 0;
8816 	  return false;
8817 	}
8818 
8819       for (rp = rela; rp < rela + nrelas; ++rp)
8820 	{
8821 	  unsigned int sym_ndx;
8822 	  unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8823 	  relname = elf_ia64_reloc_type (r_type);
8824 
8825 	  /* PR 17531: file: 9fa67536.  */
8826 	  if (relname == NULL)
8827 	    {
8828 	      warn (_("Skipping unknown relocation type: %u\n"), r_type);
8829 	      continue;
8830 	    }
8831 
8832 	  if (! startswith (relname, "R_IA64_SEGREL"))
8833 	    {
8834 	      warn (_("Skipping unexpected relocation type: %s\n"), relname);
8835 	      continue;
8836 	    }
8837 
8838 	  i = rp->r_offset / (3 * eh_addr_size);
8839 
8840 	  /* PR 17531: file: 5bc8d9bf.  */
8841 	  if (i >= aux->table_len)
8842 	    {
8843 	      warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8844 	      continue;
8845 	    }
8846 
8847 	  sym_ndx = get_reloc_symindex (rp->r_info);
8848 	  if (sym_ndx >= aux->nsyms)
8849 	    {
8850 	      warn (_("Skipping reloc with invalid symbol index: %u\n"),
8851 		    sym_ndx);
8852 	      continue;
8853 	    }
8854 	  sym = aux->symtab + sym_ndx;
8855 
8856 	  switch (rp->r_offset / eh_addr_size % 3)
8857 	    {
8858 	    case 0:
8859 	      aux->table[i].start.section = sym->st_shndx;
8860 	      aux->table[i].start.offset  = rp->r_addend + sym->st_value;
8861 	      break;
8862 	    case 1:
8863 	      aux->table[i].end.section   = sym->st_shndx;
8864 	      aux->table[i].end.offset    = rp->r_addend + sym->st_value;
8865 	      break;
8866 	    case 2:
8867 	      aux->table[i].info.section  = sym->st_shndx;
8868 	      aux->table[i].info.offset   = rp->r_addend + sym->st_value;
8869 	      break;
8870 	    default:
8871 	      break;
8872 	    }
8873 	}
8874 
8875       free (rela);
8876     }
8877 
8878   return true;
8879 }
8880 
8881 static bool
8882 ia64_process_unwind (Filedata * filedata)
8883 {
8884   Elf_Internal_Shdr * sec;
8885   Elf_Internal_Shdr * unwsec = NULL;
8886   unsigned long i, unwcount = 0, unwstart = 0;
8887   struct ia64_unw_aux_info aux;
8888   bool res = true;
8889 
8890   memset (& aux, 0, sizeof (aux));
8891 
8892   for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8893     {
8894       if (sec->sh_type == SHT_SYMTAB)
8895 	{
8896 	  if (aux.symtab)
8897 	    {
8898 	      error (_("Multiple symbol tables encountered\n"));
8899 	      free (aux.symtab);
8900 	      aux.symtab = NULL;
8901 	      free (aux.strtab);
8902 	      aux.strtab = NULL;
8903 	    }
8904 	  if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8905 			   &aux.strtab, &aux.strtab_size))
8906 	    return false;
8907 	}
8908       else if (sec->sh_type == SHT_IA_64_UNWIND)
8909 	unwcount++;
8910     }
8911 
8912   if (!unwcount)
8913     printf (_("\nThere are no unwind sections in this file.\n"));
8914 
8915   while (unwcount-- > 0)
8916     {
8917       const char *suffix;
8918       size_t len, len2;
8919 
8920       for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8921 	   i < filedata->file_header.e_shnum; ++i, ++sec)
8922 	if (sec->sh_type == SHT_IA_64_UNWIND)
8923 	  {
8924 	    unwsec = sec;
8925 	    break;
8926 	  }
8927       /* We have already counted the number of SHT_IA64_UNWIND
8928 	 sections so the loop above should never fail.  */
8929       assert (unwsec != NULL);
8930 
8931       unwstart = i + 1;
8932       len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8933 
8934       if ((unwsec->sh_flags & SHF_GROUP) != 0)
8935 	{
8936 	  /* We need to find which section group it is in.  */
8937 	  struct group_list * g;
8938 
8939 	  if (filedata->section_headers_groups == NULL
8940 	      || filedata->section_headers_groups[i] == NULL)
8941 	    i = filedata->file_header.e_shnum;
8942 	  else
8943 	    {
8944 	      g = filedata->section_headers_groups[i]->root;
8945 
8946 	      for (; g != NULL; g = g->next)
8947 		{
8948 		  sec = filedata->section_headers + g->section_index;
8949 
8950 		  if (section_name_valid (filedata, sec)
8951 		      && streq (section_name (filedata, sec),
8952 				ELF_STRING_ia64_unwind_info))
8953 		    break;
8954 		}
8955 
8956 	      if (g == NULL)
8957 		i = filedata->file_header.e_shnum;
8958 	    }
8959 	}
8960       else if (section_name_valid (filedata, unwsec)
8961 	       && startswith (section_name (filedata, unwsec),
8962 			      ELF_STRING_ia64_unwind_once))
8963 	{
8964 	  /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO.  */
8965 	  len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8966 	  suffix = section_name (filedata, unwsec) + len;
8967 	  for (i = 0, sec = filedata->section_headers;
8968 	       i < filedata->file_header.e_shnum;
8969 	       ++i, ++sec)
8970 	    if (section_name_valid (filedata, sec)
8971 		&& startswith (section_name (filedata, sec),
8972 			       ELF_STRING_ia64_unwind_info_once)
8973 		&& streq (section_name (filedata, sec) + len2, suffix))
8974 	      break;
8975 	}
8976       else
8977 	{
8978 	  /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8979 	     .IA_64.unwind or BAR -> .IA_64.unwind_info.  */
8980 	  len = sizeof (ELF_STRING_ia64_unwind) - 1;
8981 	  len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8982 	  suffix = "";
8983 	  if (section_name_valid (filedata, unwsec)
8984 	      && startswith (section_name (filedata, unwsec),
8985 			     ELF_STRING_ia64_unwind))
8986 	    suffix = section_name (filedata, unwsec) + len;
8987 	  for (i = 0, sec = filedata->section_headers;
8988 	       i < filedata->file_header.e_shnum;
8989 	       ++i, ++sec)
8990 	    if (section_name_valid (filedata, sec)
8991 		&& startswith (section_name (filedata, sec),
8992 			       ELF_STRING_ia64_unwind_info)
8993 		&& streq (section_name (filedata, sec) + len2, suffix))
8994 	      break;
8995 	}
8996 
8997       if (i == filedata->file_header.e_shnum)
8998 	{
8999 	  printf (_("\nCould not find unwind info section for "));
9000 
9001 	  if (filedata->string_table == NULL)
9002 	    printf ("%d", unwsec->sh_name);
9003 	  else
9004 	    printf ("'%s'", printable_section_name (filedata, unwsec));
9005 	}
9006       else
9007 	{
9008 	  aux.info_addr = sec->sh_addr;
9009 	  aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9010 						 sec->sh_size,
9011 						 _("unwind info"));
9012 	  aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9013 
9014 	  printf (_("\nUnwind section "));
9015 
9016 	  if (filedata->string_table == NULL)
9017 	    printf ("%d", unwsec->sh_name);
9018 	  else
9019 	    printf ("'%s'", printable_section_name (filedata, unwsec));
9020 
9021 	  printf (_(" at offset 0x%lx contains %lu entries:\n"),
9022 		  (unsigned long) unwsec->sh_offset,
9023 		  (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
9024 
9025 	  if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9026 	      && aux.table_len > 0)
9027 	    dump_ia64_unwind (filedata, & aux);
9028 
9029 	  free ((char *) aux.table);
9030 	  free ((char *) aux.info);
9031 	  aux.table = NULL;
9032 	  aux.info = NULL;
9033 	}
9034     }
9035 
9036   free (aux.symtab);
9037   free ((char *) aux.strtab);
9038 
9039   return res;
9040 }
9041 
9042 struct hppa_unw_table_entry
9043 {
9044   struct absaddr start;
9045   struct absaddr end;
9046   unsigned int Cannot_unwind:1;			/* 0 */
9047   unsigned int Millicode:1;			/* 1 */
9048   unsigned int Millicode_save_sr0:1;		/* 2 */
9049   unsigned int Region_description:2;		/* 3..4 */
9050   unsigned int reserved1:1;			/* 5 */
9051   unsigned int Entry_SR:1;			/* 6 */
9052   unsigned int Entry_FR:4;     /* Number saved     7..10 */
9053   unsigned int Entry_GR:5;     /* Number saved     11..15 */
9054   unsigned int Args_stored:1;			/* 16 */
9055   unsigned int Variable_Frame:1;		/* 17 */
9056   unsigned int Separate_Package_Body:1;		/* 18 */
9057   unsigned int Frame_Extension_Millicode:1;	/* 19 */
9058   unsigned int Stack_Overflow_Check:1;		/* 20 */
9059   unsigned int Two_Instruction_SP_Increment:1;	/* 21 */
9060   unsigned int Ada_Region:1;			/* 22 */
9061   unsigned int cxx_info:1;			/* 23 */
9062   unsigned int cxx_try_catch:1;			/* 24 */
9063   unsigned int sched_entry_seq:1;		/* 25 */
9064   unsigned int reserved2:1;			/* 26 */
9065   unsigned int Save_SP:1;			/* 27 */
9066   unsigned int Save_RP:1;			/* 28 */
9067   unsigned int Save_MRP_in_frame:1;		/* 29 */
9068   unsigned int extn_ptr_defined:1;		/* 30 */
9069   unsigned int Cleanup_defined:1;		/* 31 */
9070 
9071   unsigned int MPE_XL_interrupt_marker:1;	/* 0 */
9072   unsigned int HP_UX_interrupt_marker:1;	/* 1 */
9073   unsigned int Large_frame:1;			/* 2 */
9074   unsigned int Pseudo_SP_Set:1;			/* 3 */
9075   unsigned int reserved4:1;			/* 4 */
9076   unsigned int Total_frame_size:27;		/* 5..31 */
9077 };
9078 
9079 struct hppa_unw_aux_info
9080 {
9081   struct hppa_unw_table_entry *  table;		/* Unwind table.  */
9082   unsigned long                  table_len;	/* Length of unwind table.  */
9083   bfd_vma                        seg_base;	/* Starting address of segment.  */
9084   Elf_Internal_Sym *             symtab;	/* The symbol table.  */
9085   unsigned long                  nsyms;		/* Number of symbols.  */
9086   Elf_Internal_Sym *             funtab;	/* Sorted table of STT_FUNC symbols.  */
9087   unsigned long                  nfuns;		/* Number of entries in funtab.  */
9088   char *                         strtab;	/* The string table.  */
9089   unsigned long                  strtab_size;	/* Size of string table.  */
9090 };
9091 
9092 static bool
9093 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9094 {
9095   struct hppa_unw_table_entry * tp;
9096   unsigned long j, nfuns;
9097   bool res = true;
9098 
9099   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9100   for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9101     if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9102       aux->funtab[nfuns++] = aux->symtab[j];
9103   aux->nfuns = nfuns;
9104   qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9105 
9106   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9107     {
9108       bfd_vma offset;
9109       const char * procname;
9110 
9111       find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9112 			       aux->strtab_size, tp->start, &procname,
9113 			       &offset);
9114 
9115       fputs ("\n<", stdout);
9116 
9117       if (procname)
9118 	{
9119 	  fputs (procname, stdout);
9120 
9121 	  if (offset)
9122 	    printf ("+%lx", (unsigned long) offset);
9123 	}
9124 
9125       fputs (">: [", stdout);
9126       print_vma (tp->start.offset, PREFIX_HEX);
9127       fputc ('-', stdout);
9128       print_vma (tp->end.offset, PREFIX_HEX);
9129       printf ("]\n\t");
9130 
9131 #define PF(_m) if (tp->_m) printf (#_m " ");
9132 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9133       PF(Cannot_unwind);
9134       PF(Millicode);
9135       PF(Millicode_save_sr0);
9136       /* PV(Region_description);  */
9137       PF(Entry_SR);
9138       PV(Entry_FR);
9139       PV(Entry_GR);
9140       PF(Args_stored);
9141       PF(Variable_Frame);
9142       PF(Separate_Package_Body);
9143       PF(Frame_Extension_Millicode);
9144       PF(Stack_Overflow_Check);
9145       PF(Two_Instruction_SP_Increment);
9146       PF(Ada_Region);
9147       PF(cxx_info);
9148       PF(cxx_try_catch);
9149       PF(sched_entry_seq);
9150       PF(Save_SP);
9151       PF(Save_RP);
9152       PF(Save_MRP_in_frame);
9153       PF(extn_ptr_defined);
9154       PF(Cleanup_defined);
9155       PF(MPE_XL_interrupt_marker);
9156       PF(HP_UX_interrupt_marker);
9157       PF(Large_frame);
9158       PF(Pseudo_SP_Set);
9159       PV(Total_frame_size);
9160 #undef PF
9161 #undef PV
9162     }
9163 
9164   printf ("\n");
9165 
9166   free (aux->funtab);
9167 
9168   return res;
9169 }
9170 
9171 static bool
9172 slurp_hppa_unwind_table (Filedata *                  filedata,
9173 			 struct hppa_unw_aux_info *  aux,
9174 			 Elf_Internal_Shdr *         sec)
9175 {
9176   unsigned long size, unw_ent_size, nentries, nrelas, i;
9177   Elf_Internal_Phdr * seg;
9178   struct hppa_unw_table_entry * tep;
9179   Elf_Internal_Shdr * relsec;
9180   Elf_Internal_Rela * rela;
9181   Elf_Internal_Rela * rp;
9182   unsigned char * table;
9183   unsigned char * tp;
9184   Elf_Internal_Sym * sym;
9185   const char * relname;
9186 
9187   /* First, find the starting address of the segment that includes
9188      this section.  */
9189   if (filedata->file_header.e_phnum)
9190     {
9191       if (! get_program_headers (filedata))
9192 	return false;
9193 
9194       for (seg = filedata->program_headers;
9195 	   seg < filedata->program_headers + filedata->file_header.e_phnum;
9196 	   ++seg)
9197 	{
9198 	  if (seg->p_type != PT_LOAD)
9199 	    continue;
9200 
9201 	  if (sec->sh_addr >= seg->p_vaddr
9202 	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9203 	    {
9204 	      aux->seg_base = seg->p_vaddr;
9205 	      break;
9206 	    }
9207 	}
9208     }
9209 
9210   /* Second, build the unwind table from the contents of the unwind
9211      section.  */
9212   size = sec->sh_size;
9213   table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9214                                       _("unwind table"));
9215   if (!table)
9216     return false;
9217 
9218   unw_ent_size = 16;
9219   nentries = size / unw_ent_size;
9220   size = unw_ent_size * nentries;
9221 
9222   aux->table_len = nentries;
9223   tep = aux->table = (struct hppa_unw_table_entry *)
9224       xcmalloc (nentries, sizeof (aux->table[0]));
9225 
9226   for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9227     {
9228       unsigned int tmp1, tmp2;
9229 
9230       tep->start.section = SHN_UNDEF;
9231       tep->end.section   = SHN_UNDEF;
9232 
9233       tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9234       tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9235       tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9236       tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9237 
9238       tep->start.offset += aux->seg_base;
9239       tep->end.offset   += aux->seg_base;
9240 
9241       tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9242       tep->Millicode = (tmp1 >> 30) & 0x1;
9243       tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9244       tep->Region_description = (tmp1 >> 27) & 0x3;
9245       tep->reserved1 = (tmp1 >> 26) & 0x1;
9246       tep->Entry_SR = (tmp1 >> 25) & 0x1;
9247       tep->Entry_FR = (tmp1 >> 21) & 0xf;
9248       tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9249       tep->Args_stored = (tmp1 >> 15) & 0x1;
9250       tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9251       tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9252       tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9253       tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9254       tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9255       tep->Ada_Region = (tmp1 >> 9) & 0x1;
9256       tep->cxx_info = (tmp1 >> 8) & 0x1;
9257       tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9258       tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9259       tep->reserved2 = (tmp1 >> 5) & 0x1;
9260       tep->Save_SP = (tmp1 >> 4) & 0x1;
9261       tep->Save_RP = (tmp1 >> 3) & 0x1;
9262       tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9263       tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9264       tep->Cleanup_defined = tmp1 & 0x1;
9265 
9266       tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9267       tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9268       tep->Large_frame = (tmp2 >> 29) & 0x1;
9269       tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9270       tep->reserved4 = (tmp2 >> 27) & 0x1;
9271       tep->Total_frame_size = tmp2 & 0x7ffffff;
9272     }
9273   free (table);
9274 
9275   /* Third, apply any relocations to the unwind table.  */
9276   for (relsec = filedata->section_headers;
9277        relsec < filedata->section_headers + filedata->file_header.e_shnum;
9278        ++relsec)
9279     {
9280       if (relsec->sh_type != SHT_RELA
9281 	  || relsec->sh_info >= filedata->file_header.e_shnum
9282 	  || filedata->section_headers + relsec->sh_info != sec)
9283 	continue;
9284 
9285       if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9286 			      & rela, & nrelas))
9287 	return false;
9288 
9289       for (rp = rela; rp < rela + nrelas; ++rp)
9290 	{
9291 	  unsigned int sym_ndx;
9292 	  unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9293 	  relname = elf_hppa_reloc_type (r_type);
9294 
9295 	  if (relname == NULL)
9296 	    {
9297 	      warn (_("Skipping unknown relocation type: %u\n"), r_type);
9298 	      continue;
9299 	    }
9300 
9301 	  /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64.  */
9302 	  if (! startswith (relname, "R_PARISC_SEGREL"))
9303 	    {
9304 	      warn (_("Skipping unexpected relocation type: %s\n"), relname);
9305 	      continue;
9306 	    }
9307 
9308 	  i = rp->r_offset / unw_ent_size;
9309 	  if (i >= aux->table_len)
9310 	    {
9311 	      warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
9312 	      continue;
9313 	    }
9314 
9315 	  sym_ndx = get_reloc_symindex (rp->r_info);
9316 	  if (sym_ndx >= aux->nsyms)
9317 	    {
9318 	      warn (_("Skipping reloc with invalid symbol index: %u\n"),
9319 		    sym_ndx);
9320 	      continue;
9321 	    }
9322 	  sym = aux->symtab + sym_ndx;
9323 
9324 	  switch ((rp->r_offset % unw_ent_size) / 4)
9325 	    {
9326 	    case 0:
9327 	      aux->table[i].start.section = sym->st_shndx;
9328 	      aux->table[i].start.offset  = sym->st_value + rp->r_addend;
9329 	      break;
9330 	    case 1:
9331 	      aux->table[i].end.section   = sym->st_shndx;
9332 	      aux->table[i].end.offset    = sym->st_value + rp->r_addend;
9333 	      break;
9334 	    default:
9335 	      break;
9336 	    }
9337 	}
9338 
9339       free (rela);
9340     }
9341 
9342   return true;
9343 }
9344 
9345 static bool
9346 hppa_process_unwind (Filedata * filedata)
9347 {
9348   struct hppa_unw_aux_info aux;
9349   Elf_Internal_Shdr * unwsec = NULL;
9350   Elf_Internal_Shdr * sec;
9351   unsigned long i;
9352   bool res = true;
9353 
9354   if (filedata->string_table == NULL)
9355     return false;
9356 
9357   memset (& aux, 0, sizeof (aux));
9358 
9359   for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9360     {
9361       if (sec->sh_type == SHT_SYMTAB)
9362 	{
9363 	  if (aux.symtab)
9364 	    {
9365 	      error (_("Multiple symbol tables encountered\n"));
9366 	      free (aux.symtab);
9367 	      aux.symtab = NULL;
9368 	      free (aux.strtab);
9369 	      aux.strtab = NULL;
9370 	    }
9371 	  if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9372 			   &aux.strtab, &aux.strtab_size))
9373 	    return false;
9374 	}
9375       else if (section_name_valid (filedata, sec)
9376 	       && streq (section_name (filedata, sec), ".PARISC.unwind"))
9377 	unwsec = sec;
9378     }
9379 
9380   if (!unwsec)
9381     printf (_("\nThere are no unwind sections in this file.\n"));
9382 
9383   for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9384     {
9385       if (section_name_valid (filedata, sec)
9386 	  && streq (section_name (filedata, sec), ".PARISC.unwind"))
9387 	{
9388 	  unsigned long num_unwind = sec->sh_size / 16;
9389 
9390 	  printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9391 			    "contains %lu entry:\n",
9392 			    "\nUnwind section '%s' at offset 0x%lx "
9393 			    "contains %lu entries:\n",
9394 			    num_unwind),
9395 		  printable_section_name (filedata, sec),
9396 		  (unsigned long) sec->sh_offset,
9397 		  num_unwind);
9398 
9399           if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9400 	    res = false;
9401 
9402 	  if (res && aux.table_len > 0)
9403 	    {
9404 	      if (! dump_hppa_unwind (filedata, &aux))
9405 		res = false;
9406 	    }
9407 
9408 	  free ((char *) aux.table);
9409 	  aux.table = NULL;
9410 	}
9411     }
9412 
9413   free (aux.symtab);
9414   free ((char *) aux.strtab);
9415 
9416   return res;
9417 }
9418 
9419 struct arm_section
9420 {
9421   unsigned char *      data;		/* The unwind data.  */
9422   Elf_Internal_Shdr *  sec;		/* The cached unwind section header.  */
9423   Elf_Internal_Rela *  rela;		/* The cached relocations for this section.  */
9424   unsigned long        nrelas;		/* The number of relocations.  */
9425   unsigned int         rel_type;	/* REL or RELA ?  */
9426   Elf_Internal_Rela *  next_rela;	/* Cyclic pointer to the next reloc to process.  */
9427 };
9428 
9429 struct arm_unw_aux_info
9430 {
9431   Filedata *          filedata;		/* The file containing the unwind sections.  */
9432   Elf_Internal_Sym *  symtab;		/* The file's symbol table.  */
9433   unsigned long       nsyms;		/* Number of symbols.  */
9434   Elf_Internal_Sym *  funtab;		/* Sorted table of STT_FUNC symbols.  */
9435   unsigned long       nfuns;		/* Number of these symbols.  */
9436   char *              strtab;		/* The file's string table.  */
9437   unsigned long       strtab_size;	/* Size of string table.  */
9438 };
9439 
9440 static const char *
9441 arm_print_vma_and_name (Filedata *                 filedata,
9442 			struct arm_unw_aux_info *  aux,
9443 			bfd_vma                    fn,
9444 			struct absaddr             addr)
9445 {
9446   const char *procname;
9447   bfd_vma sym_offset;
9448 
9449   if (addr.section == SHN_UNDEF)
9450     addr.offset = fn;
9451 
9452   find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9453 			   aux->strtab_size, addr, &procname,
9454 			   &sym_offset);
9455 
9456   print_vma (fn, PREFIX_HEX);
9457 
9458   if (procname)
9459     {
9460       fputs (" <", stdout);
9461       fputs (procname, stdout);
9462 
9463       if (sym_offset)
9464 	printf ("+0x%lx", (unsigned long) sym_offset);
9465       fputc ('>', stdout);
9466     }
9467 
9468   return procname;
9469 }
9470 
9471 static void
9472 arm_free_section (struct arm_section *arm_sec)
9473 {
9474   free (arm_sec->data);
9475   free (arm_sec->rela);
9476 }
9477 
9478 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9479       cached section and install SEC instead.
9480    2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9481       and return its valued in * WORDP, relocating if necessary.
9482    3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9483       relocation's offset in ADDR.
9484    4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9485       into the string table of the symbol associated with the reloc.  If no
9486       reloc was applied store -1 there.
9487    5) Return TRUE upon success, FALSE otherwise.  */
9488 
9489 static bool
9490 get_unwind_section_word (Filedata *                 filedata,
9491 			 struct arm_unw_aux_info *  aux,
9492 			 struct arm_section *       arm_sec,
9493 			 Elf_Internal_Shdr *        sec,
9494 			 bfd_vma 		    word_offset,
9495 			 unsigned int *             wordp,
9496 			 struct absaddr *           addr,
9497 			 bfd_vma *		    sym_name)
9498 {
9499   Elf_Internal_Rela *rp;
9500   Elf_Internal_Sym *sym;
9501   const char * relname;
9502   unsigned int word;
9503   bool wrapped;
9504 
9505   if (sec == NULL || arm_sec == NULL)
9506     return false;
9507 
9508   addr->section = SHN_UNDEF;
9509   addr->offset = 0;
9510 
9511   if (sym_name != NULL)
9512     *sym_name = (bfd_vma) -1;
9513 
9514   /* If necessary, update the section cache.  */
9515   if (sec != arm_sec->sec)
9516     {
9517       Elf_Internal_Shdr *relsec;
9518 
9519       arm_free_section (arm_sec);
9520 
9521       arm_sec->sec = sec;
9522       arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9523 				sec->sh_size, _("unwind data"));
9524       arm_sec->rela = NULL;
9525       arm_sec->nrelas = 0;
9526 
9527       for (relsec = filedata->section_headers;
9528 	   relsec < filedata->section_headers + filedata->file_header.e_shnum;
9529 	   ++relsec)
9530 	{
9531 	  if (relsec->sh_info >= filedata->file_header.e_shnum
9532 	      || filedata->section_headers + relsec->sh_info != sec
9533 	      /* PR 15745: Check the section type as well.  */
9534 	      || (relsec->sh_type != SHT_REL
9535 		  && relsec->sh_type != SHT_RELA))
9536 	    continue;
9537 
9538 	  arm_sec->rel_type = relsec->sh_type;
9539 	  if (relsec->sh_type == SHT_REL)
9540 	    {
9541 	      if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9542 				     relsec->sh_size,
9543 				     & arm_sec->rela, & arm_sec->nrelas))
9544 		return false;
9545 	    }
9546 	  else /* relsec->sh_type == SHT_RELA */
9547 	    {
9548 	      if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9549 				      relsec->sh_size,
9550 				      & arm_sec->rela, & arm_sec->nrelas))
9551 		return false;
9552 	    }
9553 	  break;
9554 	}
9555 
9556       arm_sec->next_rela = arm_sec->rela;
9557     }
9558 
9559   /* If there is no unwind data we can do nothing.  */
9560   if (arm_sec->data == NULL)
9561     return false;
9562 
9563   /* If the offset is invalid then fail.  */
9564   if (/* PR 21343 *//* PR 18879 */
9565       sec->sh_size < 4
9566       || word_offset > (sec->sh_size - 4)
9567       || ((bfd_signed_vma) word_offset) < 0)
9568     return false;
9569 
9570   /* Get the word at the required offset.  */
9571   word = byte_get (arm_sec->data + word_offset, 4);
9572 
9573   /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128.  */
9574   if (arm_sec->rela == NULL)
9575     {
9576       * wordp = word;
9577       return true;
9578     }
9579 
9580   /* Look through the relocs to find the one that applies to the provided offset.  */
9581   wrapped = false;
9582   for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9583     {
9584       bfd_vma prelval, offset;
9585 
9586       if (rp->r_offset > word_offset && !wrapped)
9587 	{
9588 	  rp = arm_sec->rela;
9589 	  wrapped = true;
9590 	}
9591       if (rp->r_offset > word_offset)
9592 	break;
9593 
9594       if (rp->r_offset & 3)
9595 	{
9596 	  warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9597 		(unsigned long) rp->r_offset);
9598 	  continue;
9599 	}
9600 
9601       if (rp->r_offset < word_offset)
9602 	continue;
9603 
9604       /* PR 17531: file: 027-161405-0.004  */
9605       if (aux->symtab == NULL)
9606 	continue;
9607 
9608       if (arm_sec->rel_type == SHT_REL)
9609 	{
9610 	  offset = word & 0x7fffffff;
9611 	  if (offset & 0x40000000)
9612 	    offset |= ~ (bfd_vma) 0x7fffffff;
9613 	}
9614       else if (arm_sec->rel_type == SHT_RELA)
9615 	offset = rp->r_addend;
9616       else
9617 	{
9618 	  error (_("Unknown section relocation type %d encountered\n"),
9619 		 arm_sec->rel_type);
9620 	  break;
9621 	}
9622 
9623       /* PR 17531 file: 027-1241568-0.004.  */
9624       if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9625 	{
9626 	  error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9627 		 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9628 	  break;
9629 	}
9630 
9631       sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9632       offset += sym->st_value;
9633       prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9634 
9635       /* Check that we are processing the expected reloc type.  */
9636       if (filedata->file_header.e_machine == EM_ARM)
9637 	{
9638 	  relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9639 	  if (relname == NULL)
9640 	    {
9641 	      warn (_("Skipping unknown ARM relocation type: %d\n"),
9642 		    (int) ELF32_R_TYPE (rp->r_info));
9643 	      continue;
9644 	    }
9645 
9646 	  if (streq (relname, "R_ARM_NONE"))
9647 	      continue;
9648 
9649 	  if (! streq (relname, "R_ARM_PREL31"))
9650 	    {
9651 	      warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9652 	      continue;
9653 	    }
9654 	}
9655       else if (filedata->file_header.e_machine == EM_TI_C6000)
9656 	{
9657 	  relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9658 	  if (relname == NULL)
9659 	    {
9660 	      warn (_("Skipping unknown C6000 relocation type: %d\n"),
9661 		    (int) ELF32_R_TYPE (rp->r_info));
9662 	      continue;
9663 	    }
9664 
9665 	  if (streq (relname, "R_C6000_NONE"))
9666 	    continue;
9667 
9668 	  if (! streq (relname, "R_C6000_PREL31"))
9669 	    {
9670 	      warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9671 	      continue;
9672 	    }
9673 
9674 	  prelval >>= 1;
9675 	}
9676       else
9677 	{
9678 	  /* This function currently only supports ARM and TI unwinders.  */
9679 	  warn (_("Only TI and ARM unwinders are currently supported\n"));
9680 	  break;
9681 	}
9682 
9683       word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
9684       addr->section = sym->st_shndx;
9685       addr->offset = offset;
9686 
9687       if (sym_name)
9688 	* sym_name = sym->st_name;
9689       break;
9690     }
9691 
9692   *wordp = word;
9693   arm_sec->next_rela = rp;
9694 
9695   return true;
9696 }
9697 
9698 static const char *tic6x_unwind_regnames[16] =
9699 {
9700   "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9701   "A14", "A13", "A12", "A11", "A10",
9702   "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9703 };
9704 
9705 static void
9706 decode_tic6x_unwind_regmask (unsigned int mask)
9707 {
9708   int i;
9709 
9710   for (i = 12; mask; mask >>= 1, i--)
9711     {
9712       if (mask & 1)
9713 	{
9714 	  fputs (tic6x_unwind_regnames[i], stdout);
9715 	  if (mask > 1)
9716 	    fputs (", ", stdout);
9717 	}
9718     }
9719 }
9720 
9721 #define ADVANCE							\
9722   if (remaining == 0 && more_words)				\
9723     {								\
9724       data_offset += 4;						\
9725       if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec,	\
9726 				     data_offset, & word, & addr, NULL))	\
9727 	return false;						\
9728       remaining = 4;						\
9729       more_words--;						\
9730     }								\
9731 
9732 #define GET_OP(OP)			\
9733   ADVANCE;				\
9734   if (remaining)			\
9735     {					\
9736       remaining--;			\
9737       (OP) = word >> 24;		\
9738       word <<= 8;			\
9739     }					\
9740   else					\
9741     {					\
9742       printf (_("[Truncated opcode]\n"));	\
9743       return false;			\
9744     }					\
9745   printf ("0x%02x ", OP)
9746 
9747 static bool
9748 decode_arm_unwind_bytecode (Filedata *                 filedata,
9749 			    struct arm_unw_aux_info *  aux,
9750 			    unsigned int               word,
9751 			    unsigned int               remaining,
9752 			    unsigned int               more_words,
9753 			    bfd_vma                    data_offset,
9754 			    Elf_Internal_Shdr *        data_sec,
9755 			    struct arm_section *       data_arm_sec)
9756 {
9757   struct absaddr addr;
9758   bool res = true;
9759 
9760   /* Decode the unwinding instructions.  */
9761   while (1)
9762     {
9763       unsigned int op, op2;
9764 
9765       ADVANCE;
9766       if (remaining == 0)
9767 	break;
9768       remaining--;
9769       op = word >> 24;
9770       word <<= 8;
9771 
9772       printf ("  0x%02x ", op);
9773 
9774       if ((op & 0xc0) == 0x00)
9775 	{
9776 	  int offset = ((op & 0x3f) << 2) + 4;
9777 
9778 	  printf ("     vsp = vsp + %d", offset);
9779 	}
9780       else if ((op & 0xc0) == 0x40)
9781 	{
9782 	  int offset = ((op & 0x3f) << 2) + 4;
9783 
9784 	  printf ("     vsp = vsp - %d", offset);
9785 	}
9786       else if ((op & 0xf0) == 0x80)
9787 	{
9788 	  GET_OP (op2);
9789 	  if (op == 0x80 && op2 == 0)
9790 	    printf (_("Refuse to unwind"));
9791 	  else
9792 	    {
9793 	      unsigned int mask = ((op & 0x0f) << 8) | op2;
9794 	      bool first = true;
9795 	      int i;
9796 
9797 	      printf ("pop {");
9798 	      for (i = 0; i < 12; i++)
9799 		if (mask & (1 << i))
9800 		  {
9801 		    if (first)
9802 		      first = false;
9803 		    else
9804 		      printf (", ");
9805 		    printf ("r%d", 4 + i);
9806 		  }
9807 	      printf ("}");
9808 	    }
9809 	}
9810       else if ((op & 0xf0) == 0x90)
9811 	{
9812 	  if (op == 0x9d || op == 0x9f)
9813 	    printf (_("     [Reserved]"));
9814 	  else
9815 	    printf ("     vsp = r%d", op & 0x0f);
9816 	}
9817       else if ((op & 0xf0) == 0xa0)
9818 	{
9819 	  int end = 4 + (op & 0x07);
9820 	  bool first = true;
9821 	  int i;
9822 
9823 	  printf ("     pop {");
9824 	  for (i = 4; i <= end; i++)
9825 	    {
9826 	      if (first)
9827 		first = false;
9828 	      else
9829 		printf (", ");
9830 	      printf ("r%d", i);
9831 	    }
9832 	  if (op & 0x08)
9833 	    {
9834 	      if (!first)
9835 		printf (", ");
9836 	      printf ("r14");
9837 	    }
9838 	  printf ("}");
9839 	}
9840       else if (op == 0xb0)
9841 	printf (_("     finish"));
9842       else if (op == 0xb1)
9843 	{
9844 	  GET_OP (op2);
9845 	  if (op2 == 0 || (op2 & 0xf0) != 0)
9846 	    printf (_("[Spare]"));
9847 	  else
9848 	    {
9849 	      unsigned int mask = op2 & 0x0f;
9850 	      bool first = true;
9851 	      int i;
9852 
9853 	      printf ("pop {");
9854 	      for (i = 0; i < 12; i++)
9855 		if (mask & (1 << i))
9856 		  {
9857 		    if (first)
9858 		      first = false;
9859 		    else
9860 		      printf (", ");
9861 		    printf ("r%d", i);
9862 		  }
9863 	      printf ("}");
9864 	    }
9865 	}
9866       else if (op == 0xb2)
9867 	{
9868 	  unsigned char buf[9];
9869 	  unsigned int i, len;
9870 	  unsigned long offset;
9871 
9872 	  for (i = 0; i < sizeof (buf); i++)
9873 	    {
9874 	      GET_OP (buf[i]);
9875 	      if ((buf[i] & 0x80) == 0)
9876 		break;
9877 	    }
9878 	  if (i == sizeof (buf))
9879 	    {
9880 	      error (_("corrupt change to vsp\n"));
9881 	      res = false;
9882 	    }
9883 	  else
9884 	    {
9885 	      offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9886 	      assert (len == i + 1);
9887 	      offset = offset * 4 + 0x204;
9888 	      printf ("vsp = vsp + %ld", offset);
9889 	    }
9890 	}
9891       else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9892 	{
9893 	  unsigned int first, last;
9894 
9895 	  GET_OP (op2);
9896 	  first = op2 >> 4;
9897 	  last = op2 & 0x0f;
9898 	  if (op == 0xc8)
9899 	    first = first + 16;
9900 	  printf ("pop {D%d", first);
9901 	  if (last)
9902 	    printf ("-D%d", first + last);
9903 	  printf ("}");
9904 	}
9905       else if (op == 0xb4)
9906 	printf (_("     pop {ra_auth_code}"));
9907       else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9908 	{
9909 	  unsigned int count = op & 0x07;
9910 
9911 	  printf ("pop {D8");
9912 	  if (count)
9913 	    printf ("-D%d", 8 + count);
9914 	  printf ("}");
9915 	}
9916       else if (op >= 0xc0 && op <= 0xc5)
9917 	{
9918 	  unsigned int count = op & 0x07;
9919 
9920 	  printf ("     pop {wR10");
9921 	  if (count)
9922 	    printf ("-wR%d", 10 + count);
9923 	  printf ("}");
9924 	}
9925       else if (op == 0xc6)
9926 	{
9927 	  unsigned int first, last;
9928 
9929 	  GET_OP (op2);
9930 	  first = op2 >> 4;
9931 	  last = op2 & 0x0f;
9932 	  printf ("pop {wR%d", first);
9933 	  if (last)
9934 	    printf ("-wR%d", first + last);
9935 	  printf ("}");
9936 	}
9937       else if (op == 0xc7)
9938 	{
9939 	  GET_OP (op2);
9940 	  if (op2 == 0 || (op2 & 0xf0) != 0)
9941 	    printf (_("[Spare]"));
9942 	  else
9943 	    {
9944 	      unsigned int mask = op2 & 0x0f;
9945 	      bool first = true;
9946 	      int i;
9947 
9948 	      printf ("pop {");
9949 	      for (i = 0; i < 4; i++)
9950 		if (mask & (1 << i))
9951 		  {
9952 		    if (first)
9953 		      first = false;
9954 		    else
9955 		      printf (", ");
9956 		    printf ("wCGR%d", i);
9957 		  }
9958 	      printf ("}");
9959 	    }
9960 	}
9961       else
9962 	{
9963 	  printf (_("     [unsupported opcode]"));
9964 	  res = false;
9965 	}
9966 
9967       printf ("\n");
9968     }
9969 
9970   return res;
9971 }
9972 
9973 static bool
9974 decode_tic6x_unwind_bytecode (Filedata *                 filedata,
9975 			      struct arm_unw_aux_info *  aux,
9976 			      unsigned int               word,
9977 			      unsigned int               remaining,
9978 			      unsigned int               more_words,
9979 			      bfd_vma                    data_offset,
9980 			      Elf_Internal_Shdr *        data_sec,
9981 			      struct arm_section *       data_arm_sec)
9982 {
9983   struct absaddr addr;
9984 
9985   /* Decode the unwinding instructions.  */
9986   while (1)
9987     {
9988       unsigned int op, op2;
9989 
9990       ADVANCE;
9991       if (remaining == 0)
9992 	break;
9993       remaining--;
9994       op = word >> 24;
9995       word <<= 8;
9996 
9997       printf ("  0x%02x ", op);
9998 
9999       if ((op & 0xc0) == 0x00)
10000 	{
10001 	  int offset = ((op & 0x3f) << 3) + 8;
10002 	  printf ("     sp = sp + %d", offset);
10003 	}
10004       else if ((op & 0xc0) == 0x80)
10005 	{
10006 	  GET_OP (op2);
10007 	  if (op == 0x80 && op2 == 0)
10008 	    printf (_("Refuse to unwind"));
10009 	  else
10010 	    {
10011 	      unsigned int mask = ((op & 0x1f) << 8) | op2;
10012 	      if (op & 0x20)
10013 		printf ("pop compact {");
10014 	      else
10015 		printf ("pop {");
10016 
10017 	      decode_tic6x_unwind_regmask (mask);
10018 	      printf("}");
10019 	    }
10020 	}
10021       else if ((op & 0xf0) == 0xc0)
10022 	{
10023 	  unsigned int reg;
10024 	  unsigned int nregs;
10025 	  unsigned int i;
10026 	  const char *name;
10027 	  struct
10028 	  {
10029 	    unsigned int offset;
10030 	    unsigned int reg;
10031 	  } regpos[16];
10032 
10033 	  /* Scan entire instruction first so that GET_OP output is not
10034 	     interleaved with disassembly.  */
10035 	  nregs = 0;
10036 	  for (i = 0; nregs < (op & 0xf); i++)
10037 	    {
10038 	      GET_OP (op2);
10039 	      reg = op2 >> 4;
10040 	      if (reg != 0xf)
10041 		{
10042 		  regpos[nregs].offset = i * 2;
10043 		  regpos[nregs].reg = reg;
10044 		  nregs++;
10045 		}
10046 
10047 	      reg = op2 & 0xf;
10048 	      if (reg != 0xf)
10049 		{
10050 		  regpos[nregs].offset = i * 2 + 1;
10051 		  regpos[nregs].reg = reg;
10052 		  nregs++;
10053 		}
10054 	    }
10055 
10056 	  printf (_("pop frame {"));
10057 	  if (nregs == 0)
10058 	    {
10059 	      printf (_("*corrupt* - no registers specified"));
10060 	    }
10061 	  else
10062 	    {
10063 	      reg = nregs - 1;
10064 	      for (i = i * 2; i > 0; i--)
10065 		{
10066 		  if (regpos[reg].offset == i - 1)
10067 		    {
10068 		      name = tic6x_unwind_regnames[regpos[reg].reg];
10069 		      if (reg > 0)
10070 			reg--;
10071 		    }
10072 		  else
10073 		    name = _("[pad]");
10074 
10075 		  fputs (name, stdout);
10076 		  if (i > 1)
10077 		    printf (", ");
10078 		}
10079 	    }
10080 
10081 	  printf ("}");
10082 	}
10083       else if (op == 0xd0)
10084 	printf ("     MOV FP, SP");
10085       else if (op == 0xd1)
10086 	printf ("     __c6xabi_pop_rts");
10087       else if (op == 0xd2)
10088 	{
10089 	  unsigned char buf[9];
10090 	  unsigned int i, len;
10091 	  unsigned long offset;
10092 
10093 	  for (i = 0; i < sizeof (buf); i++)
10094 	    {
10095 	      GET_OP (buf[i]);
10096 	      if ((buf[i] & 0x80) == 0)
10097 		break;
10098 	    }
10099 	  /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2.  */
10100 	  if (i == sizeof (buf))
10101 	    {
10102 	      warn (_("Corrupt stack pointer adjustment detected\n"));
10103 	      return false;
10104 	    }
10105 
10106 	  offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10107 	  assert (len == i + 1);
10108 	  offset = offset * 8 + 0x408;
10109 	  printf (_("sp = sp + %ld"), offset);
10110 	}
10111       else if ((op & 0xf0) == 0xe0)
10112 	{
10113 	  if ((op & 0x0f) == 7)
10114 	    printf ("     RETURN");
10115 	  else
10116 	    printf ("     MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10117 	}
10118       else
10119 	{
10120 	  printf (_("     [unsupported opcode]"));
10121 	}
10122       putchar ('\n');
10123     }
10124 
10125   return true;
10126 }
10127 
10128 static bfd_vma
10129 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
10130 {
10131   bfd_vma offset;
10132 
10133   offset = word & 0x7fffffff;
10134   if (offset & 0x40000000)
10135     offset |= ~ (bfd_vma) 0x7fffffff;
10136 
10137   if (filedata->file_header.e_machine == EM_TI_C6000)
10138     offset <<= 1;
10139 
10140   return offset + where;
10141 }
10142 
10143 static bool
10144 decode_arm_unwind (Filedata *                 filedata,
10145 		   struct arm_unw_aux_info *  aux,
10146 		   unsigned int               word,
10147 		   unsigned int               remaining,
10148 		   bfd_vma                    data_offset,
10149 		   Elf_Internal_Shdr *        data_sec,
10150 		   struct arm_section *       data_arm_sec)
10151 {
10152   int per_index;
10153   unsigned int more_words = 0;
10154   struct absaddr addr;
10155   bfd_vma sym_name = (bfd_vma) -1;
10156   bool res = true;
10157 
10158   if (remaining == 0)
10159     {
10160       /* Fetch the first word.
10161 	 Note - when decoding an object file the address extracted
10162 	 here will always be 0.  So we also pass in the sym_name
10163 	 parameter so that we can find the symbol associated with
10164 	 the personality routine.  */
10165       if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10166 				     & word, & addr, & sym_name))
10167 	return false;
10168 
10169       remaining = 4;
10170     }
10171   else
10172     {
10173       addr.section = SHN_UNDEF;
10174       addr.offset = 0;
10175     }
10176 
10177   if ((word & 0x80000000) == 0)
10178     {
10179       /* Expand prel31 for personality routine.  */
10180       bfd_vma fn;
10181       const char *procname;
10182 
10183       fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
10184       printf (_("  Personality routine: "));
10185       if (fn == 0
10186 	  && addr.section == SHN_UNDEF && addr.offset == 0
10187 	  && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
10188 	{
10189 	  procname = aux->strtab + sym_name;
10190 	  print_vma (fn, PREFIX_HEX);
10191 	  if (procname)
10192 	    {
10193 	      fputs (" <", stdout);
10194 	      fputs (procname, stdout);
10195 	      fputc ('>', stdout);
10196 	    }
10197 	}
10198       else
10199 	procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10200       fputc ('\n', stdout);
10201 
10202       /* The GCC personality routines use the standard compact
10203 	 encoding, starting with one byte giving the number of
10204 	 words.  */
10205       if (procname != NULL
10206 	  && (startswith (procname, "__gcc_personality_v0")
10207 	      || startswith (procname, "__gxx_personality_v0")
10208 	      || startswith (procname, "__gcj_personality_v0")
10209 	      || startswith (procname, "__gnu_objc_personality_v0")))
10210 	{
10211 	  remaining = 0;
10212 	  more_words = 1;
10213 	  ADVANCE;
10214 	  if (!remaining)
10215 	    {
10216 	      printf (_("  [Truncated data]\n"));
10217 	      return false;
10218 	    }
10219 	  more_words = word >> 24;
10220 	  word <<= 8;
10221 	  remaining--;
10222 	  per_index = -1;
10223 	}
10224       else
10225 	return true;
10226     }
10227   else
10228     {
10229       /* ARM EHABI Section 6.3:
10230 
10231 	 An exception-handling table entry for the compact model looks like:
10232 
10233            31 30-28 27-24 23-0
10234 	   -- ----- ----- ----
10235             1   0   index Data for personalityRoutine[index]    */
10236 
10237       if (filedata->file_header.e_machine == EM_ARM
10238 	  && (word & 0x70000000))
10239 	{
10240 	  warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10241 	  res = false;
10242 	}
10243 
10244       per_index = (word >> 24) & 0x7f;
10245       printf (_("  Compact model index: %d\n"), per_index);
10246       if (per_index == 0)
10247 	{
10248 	  more_words = 0;
10249 	  word <<= 8;
10250 	  remaining--;
10251 	}
10252       else if (per_index < 3)
10253 	{
10254 	  more_words = (word >> 16) & 0xff;
10255 	  word <<= 16;
10256 	  remaining -= 2;
10257 	}
10258     }
10259 
10260   switch (filedata->file_header.e_machine)
10261     {
10262     case EM_ARM:
10263       if (per_index < 3)
10264 	{
10265 	  if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10266 					    data_offset, data_sec, data_arm_sec))
10267 	    res = false;
10268 	}
10269       else
10270 	{
10271 	  warn (_("Unknown ARM compact model index encountered\n"));
10272 	  printf (_("  [reserved]\n"));
10273 	  res = false;
10274 	}
10275       break;
10276 
10277     case EM_TI_C6000:
10278       if (per_index < 3)
10279 	{
10280 	  if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10281 					      data_offset, data_sec, data_arm_sec))
10282 	    res = false;
10283 	}
10284       else if (per_index < 5)
10285 	{
10286 	  if (((word >> 17) & 0x7f) == 0x7f)
10287 	    printf (_("  Restore stack from frame pointer\n"));
10288 	  else
10289 	    printf (_("  Stack increment %d\n"), (word >> 14) & 0x1fc);
10290 	  printf (_("  Registers restored: "));
10291 	  if (per_index == 4)
10292 	    printf (" (compact) ");
10293 	  decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10294 	  putchar ('\n');
10295 	  printf (_("  Return register: %s\n"),
10296 		  tic6x_unwind_regnames[word & 0xf]);
10297 	}
10298       else
10299 	printf (_("  [reserved (%d)]\n"), per_index);
10300       break;
10301 
10302     default:
10303       error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10304 	     filedata->file_header.e_machine);
10305       res = false;
10306     }
10307 
10308   /* Decode the descriptors.  Not implemented.  */
10309 
10310   return res;
10311 }
10312 
10313 static bool
10314 dump_arm_unwind (Filedata *                 filedata,
10315 		 struct arm_unw_aux_info *  aux,
10316 		 Elf_Internal_Shdr *        exidx_sec)
10317 {
10318   struct arm_section exidx_arm_sec, extab_arm_sec;
10319   unsigned int i, exidx_len;
10320   unsigned long j, nfuns;
10321   bool res = true;
10322 
10323   memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10324   memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10325   exidx_len = exidx_sec->sh_size / 8;
10326 
10327   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10328   for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10329     if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10330       aux->funtab[nfuns++] = aux->symtab[j];
10331   aux->nfuns = nfuns;
10332   qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10333 
10334   for (i = 0; i < exidx_len; i++)
10335     {
10336       unsigned int exidx_fn, exidx_entry;
10337       struct absaddr fn_addr, entry_addr;
10338       bfd_vma fn;
10339 
10340       fputc ('\n', stdout);
10341 
10342       if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10343 				     8 * i, & exidx_fn, & fn_addr, NULL)
10344 	  || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10345 					8 * i + 4, & exidx_entry, & entry_addr, NULL))
10346 	{
10347 	  free (aux->funtab);
10348 	  arm_free_section (& exidx_arm_sec);
10349 	  arm_free_section (& extab_arm_sec);
10350 	  return false;
10351 	}
10352 
10353       /* ARM EHABI, Section 5:
10354 	 An index table entry consists of 2 words.
10355          The first word contains a prel31 offset to the start of a function, with bit 31 clear.  */
10356       if (exidx_fn & 0x80000000)
10357 	{
10358 	  warn (_("corrupt index table entry: %x\n"), exidx_fn);
10359 	  res = false;
10360 	}
10361 
10362       fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10363 
10364       arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10365       fputs (": ", stdout);
10366 
10367       if (exidx_entry == 1)
10368 	{
10369 	  print_vma (exidx_entry, PREFIX_HEX);
10370 	  fputs (" [cantunwind]\n", stdout);
10371 	}
10372       else if (exidx_entry & 0x80000000)
10373 	{
10374 	  print_vma (exidx_entry, PREFIX_HEX);
10375 	  fputc ('\n', stdout);
10376 	  decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10377 	}
10378       else
10379 	{
10380 	  bfd_vma table, table_offset = 0;
10381 	  Elf_Internal_Shdr *table_sec;
10382 
10383 	  fputs ("@", stdout);
10384 	  table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10385 	  print_vma (table, PREFIX_HEX);
10386 	  printf ("\n");
10387 
10388 	  /* Locate the matching .ARM.extab.  */
10389 	  if (entry_addr.section != SHN_UNDEF
10390 	      && entry_addr.section < filedata->file_header.e_shnum)
10391 	    {
10392 	      table_sec = filedata->section_headers + entry_addr.section;
10393 	      table_offset = entry_addr.offset;
10394 	      /* PR 18879 */
10395 	      if (table_offset > table_sec->sh_size
10396 		  || ((bfd_signed_vma) table_offset) < 0)
10397 		{
10398 		  warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
10399 			(unsigned long) table_offset,
10400 			printable_section_name (filedata, table_sec));
10401 		  res = false;
10402 		  continue;
10403 		}
10404 	    }
10405 	  else
10406 	    {
10407 	      table_sec = find_section_by_address (filedata, table);
10408 	      if (table_sec != NULL)
10409 		table_offset = table - table_sec->sh_addr;
10410 	    }
10411 
10412 	  if (table_sec == NULL)
10413 	    {
10414 	      warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
10415 		    (unsigned long) table);
10416 	      res = false;
10417 	      continue;
10418 	    }
10419 
10420 	  if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10421 				   &extab_arm_sec))
10422 	    res = false;
10423 	}
10424     }
10425 
10426   printf ("\n");
10427 
10428   free (aux->funtab);
10429   arm_free_section (&exidx_arm_sec);
10430   arm_free_section (&extab_arm_sec);
10431 
10432   return res;
10433 }
10434 
10435 /* Used for both ARM and C6X unwinding tables.  */
10436 
10437 static bool
10438 arm_process_unwind (Filedata * filedata)
10439 {
10440   struct arm_unw_aux_info aux;
10441   Elf_Internal_Shdr *unwsec = NULL;
10442   Elf_Internal_Shdr *sec;
10443   unsigned long i;
10444   unsigned int sec_type;
10445   bool res = true;
10446 
10447   switch (filedata->file_header.e_machine)
10448     {
10449     case EM_ARM:
10450       sec_type = SHT_ARM_EXIDX;
10451       break;
10452 
10453     case EM_TI_C6000:
10454       sec_type = SHT_C6000_UNWIND;
10455       break;
10456 
10457     default:
10458       error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
10459 	     filedata->file_header.e_machine);
10460       return false;
10461     }
10462 
10463   if (filedata->string_table == NULL)
10464     return false;
10465 
10466   memset (& aux, 0, sizeof (aux));
10467   aux.filedata = filedata;
10468 
10469   for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10470     {
10471       if (sec->sh_type == SHT_SYMTAB)
10472 	{
10473 	  if (aux.symtab)
10474 	    {
10475 	      error (_("Multiple symbol tables encountered\n"));
10476 	      free (aux.symtab);
10477 	      aux.symtab = NULL;
10478 	      free (aux.strtab);
10479 	      aux.strtab = NULL;
10480 	    }
10481 	  if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10482 			   &aux.strtab, &aux.strtab_size))
10483 	    return false;
10484 	}
10485       else if (sec->sh_type == sec_type)
10486 	unwsec = sec;
10487     }
10488 
10489   if (unwsec == NULL)
10490     printf (_("\nThere are no unwind sections in this file.\n"));
10491   else
10492     for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10493       {
10494 	if (sec->sh_type == sec_type)
10495 	  {
10496 	    unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10497 	    printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10498 			      "contains %lu entry:\n",
10499 			      "\nUnwind section '%s' at offset 0x%lx "
10500 			      "contains %lu entries:\n",
10501 			      num_unwind),
10502 		    printable_section_name (filedata, sec),
10503 		    (unsigned long) sec->sh_offset,
10504 		    num_unwind);
10505 
10506 	    if (! dump_arm_unwind (filedata, &aux, sec))
10507 	      res = false;
10508 	  }
10509       }
10510 
10511   free (aux.symtab);
10512   free ((char *) aux.strtab);
10513 
10514   return res;
10515 }
10516 
10517 static bool
10518 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10519 {
10520   printf (_("No processor specific unwind information to decode\n"));
10521   return true;
10522 }
10523 
10524 static bool
10525 process_unwind (Filedata * filedata)
10526 {
10527   struct unwind_handler
10528   {
10529     unsigned int machtype;
10530     bool (* handler)(Filedata *);
10531   } handlers[] =
10532   {
10533     { EM_ARM, arm_process_unwind },
10534     { EM_IA_64, ia64_process_unwind },
10535     { EM_PARISC, hppa_process_unwind },
10536     { EM_TI_C6000, arm_process_unwind },
10537     { EM_386, no_processor_specific_unwind },
10538     { EM_X86_64, no_processor_specific_unwind },
10539     { 0, NULL }
10540   };
10541   int i;
10542 
10543   if (!do_unwind)
10544     return true;
10545 
10546   for (i = 0; handlers[i].handler != NULL; i++)
10547     if (filedata->file_header.e_machine == handlers[i].machtype)
10548       return handlers[i].handler (filedata);
10549 
10550   printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10551 	  get_machine_name (filedata->file_header.e_machine));
10552   return true;
10553 }
10554 
10555 static void
10556 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10557 {
10558   switch (entry->d_tag)
10559     {
10560     case DT_AARCH64_BTI_PLT:
10561     case DT_AARCH64_PAC_PLT:
10562       break;
10563     default:
10564       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10565       break;
10566     }
10567   putchar ('\n');
10568 }
10569 
10570 static void
10571 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10572 {
10573   switch (entry->d_tag)
10574     {
10575     case DT_MIPS_FLAGS:
10576       if (entry->d_un.d_val == 0)
10577 	printf (_("NONE"));
10578       else
10579 	{
10580 	  static const char * opts[] =
10581 	  {
10582 	    "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10583 	    "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10584 	    "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10585 	    "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10586 	    "RLD_ORDER_SAFE"
10587 	  };
10588 	  unsigned int cnt;
10589 	  bool first = true;
10590 
10591 	  for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10592 	    if (entry->d_un.d_val & (1 << cnt))
10593 	      {
10594 		printf ("%s%s", first ? "" : " ", opts[cnt]);
10595 		first = false;
10596 	      }
10597 	}
10598       break;
10599 
10600     case DT_MIPS_IVERSION:
10601       if (valid_dynamic_name (filedata, entry->d_un.d_val))
10602 	printf (_("Interface Version: %s"),
10603 		get_dynamic_name (filedata, entry->d_un.d_val));
10604       else
10605 	{
10606 	  char buf[40];
10607 	  sprintf_vma (buf, entry->d_un.d_ptr);
10608 	  /* Note: coded this way so that there is a single string for translation.  */
10609 	  printf (_("<corrupt: %s>"), buf);
10610 	}
10611       break;
10612 
10613     case DT_MIPS_TIME_STAMP:
10614       {
10615 	char timebuf[128];
10616 	struct tm * tmp;
10617 	time_t atime = entry->d_un.d_val;
10618 
10619 	tmp = gmtime (&atime);
10620 	/* PR 17531: file: 6accc532.  */
10621 	if (tmp == NULL)
10622 	  snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10623 	else
10624 	  snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10625 		    tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10626 		    tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10627 	printf (_("Time Stamp: %s"), timebuf);
10628       }
10629       break;
10630 
10631     case DT_MIPS_RLD_VERSION:
10632     case DT_MIPS_LOCAL_GOTNO:
10633     case DT_MIPS_CONFLICTNO:
10634     case DT_MIPS_LIBLISTNO:
10635     case DT_MIPS_SYMTABNO:
10636     case DT_MIPS_UNREFEXTNO:
10637     case DT_MIPS_HIPAGENO:
10638     case DT_MIPS_DELTA_CLASS_NO:
10639     case DT_MIPS_DELTA_INSTANCE_NO:
10640     case DT_MIPS_DELTA_RELOC_NO:
10641     case DT_MIPS_DELTA_SYM_NO:
10642     case DT_MIPS_DELTA_CLASSSYM_NO:
10643     case DT_MIPS_COMPACT_SIZE:
10644       print_vma (entry->d_un.d_val, DEC);
10645       break;
10646 
10647     case DT_MIPS_XHASH:
10648       filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10649       filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10650       /* Falls through.  */
10651 
10652     default:
10653       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10654     }
10655     putchar ('\n');
10656 }
10657 
10658 static void
10659 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10660 {
10661   switch (entry->d_tag)
10662     {
10663     case DT_HP_DLD_FLAGS:
10664       {
10665 	static struct
10666 	{
10667 	  long int bit;
10668 	  const char * str;
10669 	}
10670 	flags[] =
10671 	{
10672 	  { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10673 	  { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10674 	  { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10675 	  { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10676 	  { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10677 	  { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10678 	  { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10679 	  { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10680 	  { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10681 	  { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10682 	  { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10683 	  { DT_HP_GST, "HP_GST" },
10684 	  { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10685 	  { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10686 	  { DT_HP_NODELETE, "HP_NODELETE" },
10687 	  { DT_HP_GROUP, "HP_GROUP" },
10688 	  { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10689 	};
10690 	bool first = true;
10691 	size_t cnt;
10692 	bfd_vma val = entry->d_un.d_val;
10693 
10694 	for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10695 	  if (val & flags[cnt].bit)
10696 	    {
10697 	      if (! first)
10698 		putchar (' ');
10699 	      fputs (flags[cnt].str, stdout);
10700 	      first = false;
10701 	      val ^= flags[cnt].bit;
10702 	    }
10703 
10704 	if (val != 0 || first)
10705 	  {
10706 	    if (! first)
10707 	      putchar (' ');
10708 	    print_vma (val, HEX);
10709 	  }
10710       }
10711       break;
10712 
10713     default:
10714       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10715       break;
10716     }
10717   putchar ('\n');
10718 }
10719 
10720 #ifdef BFD64
10721 
10722 /* VMS vs Unix time offset and factor.  */
10723 
10724 #define VMS_EPOCH_OFFSET 35067168000000000LL
10725 #define VMS_GRANULARITY_FACTOR 10000000
10726 #ifndef INT64_MIN
10727 #define INT64_MIN (-9223372036854775807LL - 1)
10728 #endif
10729 
10730 /* Display a VMS time in a human readable format.  */
10731 
10732 static void
10733 print_vms_time (int64_t vmstime)
10734 {
10735   struct tm *tm = NULL;
10736   time_t unxtime;
10737 
10738   if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10739     {
10740       vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10741       unxtime = vmstime;
10742       if (unxtime == vmstime)
10743 	tm = gmtime (&unxtime);
10744     }
10745   if (tm != NULL)
10746     printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10747 	    tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10748 	    tm->tm_hour, tm->tm_min, tm->tm_sec);
10749 }
10750 #endif /* BFD64 */
10751 
10752 static void
10753 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10754 {
10755   switch (entry->d_tag)
10756     {
10757     case DT_IA_64_PLT_RESERVE:
10758       /* First 3 slots reserved.  */
10759       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10760       printf (" -- ");
10761       print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10762       break;
10763 
10764     case DT_IA_64_VMS_LINKTIME:
10765 #ifdef BFD64
10766       print_vms_time (entry->d_un.d_val);
10767 #endif
10768       break;
10769 
10770     case DT_IA_64_VMS_LNKFLAGS:
10771       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10772       if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10773         printf (" CALL_DEBUG");
10774       if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10775         printf (" NOP0BUFS");
10776       if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10777         printf (" P0IMAGE");
10778       if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10779         printf (" MKTHREADS");
10780       if (entry->d_un.d_val & VMS_LF_UPCALLS)
10781         printf (" UPCALLS");
10782       if (entry->d_un.d_val & VMS_LF_IMGSTA)
10783         printf (" IMGSTA");
10784       if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10785         printf (" INITIALIZE");
10786       if (entry->d_un.d_val & VMS_LF_MAIN)
10787         printf (" MAIN");
10788       if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10789         printf (" EXE_INIT");
10790       if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10791         printf (" TBK_IN_IMG");
10792       if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10793         printf (" DBG_IN_IMG");
10794       if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10795         printf (" TBK_IN_DSF");
10796       if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10797         printf (" DBG_IN_DSF");
10798       if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10799         printf (" SIGNATURES");
10800       if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10801         printf (" REL_SEG_OFF");
10802       break;
10803 
10804     default:
10805       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10806       break;
10807     }
10808   putchar ('\n');
10809 }
10810 
10811 static bool
10812 get_32bit_dynamic_section (Filedata * filedata)
10813 {
10814   Elf32_External_Dyn * edyn;
10815   Elf32_External_Dyn * ext;
10816   Elf_Internal_Dyn * entry;
10817 
10818   edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10819 					  filedata->dynamic_addr, 1,
10820 					  filedata->dynamic_size,
10821 					  _("dynamic section"));
10822   if (!edyn)
10823     return false;
10824 
10825   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10826      might not have the luxury of section headers.  Look for the DT_NULL
10827      terminator to determine the number of entries.  */
10828   for (ext = edyn, filedata->dynamic_nent = 0;
10829        (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10830        ext++)
10831     {
10832       filedata->dynamic_nent++;
10833       if (BYTE_GET (ext->d_tag) == DT_NULL)
10834 	break;
10835     }
10836 
10837   filedata->dynamic_section
10838     = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10839   if (filedata->dynamic_section == NULL)
10840     {
10841       error (_("Out of memory allocating space for %lu dynamic entries\n"),
10842 	     (unsigned long) filedata->dynamic_nent);
10843       free (edyn);
10844       return false;
10845     }
10846 
10847   for (ext = edyn, entry = filedata->dynamic_section;
10848        entry < filedata->dynamic_section + filedata->dynamic_nent;
10849        ext++, entry++)
10850     {
10851       entry->d_tag      = BYTE_GET (ext->d_tag);
10852       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10853     }
10854 
10855   free (edyn);
10856 
10857   return true;
10858 }
10859 
10860 static bool
10861 get_64bit_dynamic_section (Filedata * filedata)
10862 {
10863   Elf64_External_Dyn * edyn;
10864   Elf64_External_Dyn * ext;
10865   Elf_Internal_Dyn * entry;
10866 
10867   /* Read in the data.  */
10868   edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10869 					  filedata->dynamic_addr, 1,
10870 					  filedata->dynamic_size,
10871 					  _("dynamic section"));
10872   if (!edyn)
10873     return false;
10874 
10875   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10876      might not have the luxury of section headers.  Look for the DT_NULL
10877      terminator to determine the number of entries.  */
10878   for (ext = edyn, filedata->dynamic_nent = 0;
10879        /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer.  */
10880        (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10881        ext++)
10882     {
10883       filedata->dynamic_nent++;
10884       if (BYTE_GET (ext->d_tag) == DT_NULL)
10885 	break;
10886     }
10887 
10888   filedata->dynamic_section
10889     = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10890   if (filedata->dynamic_section == NULL)
10891     {
10892       error (_("Out of memory allocating space for %lu dynamic entries\n"),
10893 	     (unsigned long) filedata->dynamic_nent);
10894       free (edyn);
10895       return false;
10896     }
10897 
10898   /* Convert from external to internal formats.  */
10899   for (ext = edyn, entry = filedata->dynamic_section;
10900        entry < filedata->dynamic_section + filedata->dynamic_nent;
10901        ext++, entry++)
10902     {
10903       entry->d_tag      = BYTE_GET (ext->d_tag);
10904       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10905     }
10906 
10907   free (edyn);
10908 
10909   return true;
10910 }
10911 
10912 static bool
10913 get_dynamic_section (Filedata *filedata)
10914 {
10915   if (filedata->dynamic_section)
10916     return true;
10917 
10918   if (is_32bit_elf)
10919     return get_32bit_dynamic_section (filedata);
10920   else
10921     return get_64bit_dynamic_section (filedata);
10922 }
10923 
10924 static void
10925 print_dynamic_flags (bfd_vma flags)
10926 {
10927   bool first = true;
10928 
10929   while (flags)
10930     {
10931       bfd_vma flag;
10932 
10933       flag = flags & - flags;
10934       flags &= ~ flag;
10935 
10936       if (first)
10937 	first = false;
10938       else
10939 	putc (' ', stdout);
10940 
10941       switch (flag)
10942 	{
10943 	case DF_ORIGIN:		fputs ("ORIGIN", stdout); break;
10944 	case DF_SYMBOLIC:	fputs ("SYMBOLIC", stdout); break;
10945 	case DF_TEXTREL:	fputs ("TEXTREL", stdout); break;
10946 	case DF_BIND_NOW:	fputs ("BIND_NOW", stdout); break;
10947 	case DF_STATIC_TLS:	fputs ("STATIC_TLS", stdout); break;
10948 	default:		fputs (_("unknown"), stdout); break;
10949 	}
10950     }
10951   puts ("");
10952 }
10953 
10954 static bfd_vma *
10955 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10956 {
10957   unsigned char * e_data;
10958   bfd_vma * i_data;
10959 
10960   /* If the size_t type is smaller than the bfd_size_type, eg because
10961      you are building a 32-bit tool on a 64-bit host, then make sure
10962      that when (number) is cast to (size_t) no information is lost.  */
10963   if (sizeof (size_t) < sizeof (bfd_size_type)
10964       && (bfd_size_type) ((size_t) number) != number)
10965     {
10966       error (_("Size truncation prevents reading %s elements of size %u\n"),
10967 	     bfd_vmatoa ("u", number), ent_size);
10968       return NULL;
10969     }
10970 
10971   /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10972      attempting to allocate memory when the read is bound to fail.  */
10973   if (ent_size * number > filedata->file_size)
10974     {
10975       error (_("Invalid number of dynamic entries: %s\n"),
10976 	     bfd_vmatoa ("u", number));
10977       return NULL;
10978     }
10979 
10980   e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10981   if (e_data == NULL)
10982     {
10983       error (_("Out of memory reading %s dynamic entries\n"),
10984 	     bfd_vmatoa ("u", number));
10985       return NULL;
10986     }
10987 
10988   if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10989     {
10990       error (_("Unable to read in %s bytes of dynamic data\n"),
10991 	     bfd_vmatoa ("u", number * ent_size));
10992       free (e_data);
10993       return NULL;
10994     }
10995 
10996   i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10997   if (i_data == NULL)
10998     {
10999       error (_("Out of memory allocating space for %s dynamic entries\n"),
11000 	     bfd_vmatoa ("u", number));
11001       free (e_data);
11002       return NULL;
11003     }
11004 
11005   while (number--)
11006     i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11007 
11008   free (e_data);
11009 
11010   return i_data;
11011 }
11012 
11013 static unsigned long
11014 get_num_dynamic_syms (Filedata * filedata)
11015 {
11016   unsigned long num_of_syms = 0;
11017 
11018   if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11019     return num_of_syms;
11020 
11021   if (filedata->dynamic_info[DT_HASH])
11022     {
11023       unsigned char nb[8];
11024       unsigned char nc[8];
11025       unsigned int hash_ent_size = 4;
11026 
11027       if ((filedata->file_header.e_machine == EM_ALPHA
11028 	   || filedata->file_header.e_machine == EM_S390
11029 	   || filedata->file_header.e_machine == EM_S390_OLD)
11030 	  && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11031 	hash_ent_size = 8;
11032 
11033       if (fseek (filedata->handle,
11034 		 (filedata->archive_file_offset
11035 		  + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
11036 				     sizeof nb + sizeof nc)),
11037 		 SEEK_SET))
11038 	{
11039 	  error (_("Unable to seek to start of dynamic information\n"));
11040 	  goto no_hash;
11041 	}
11042 
11043       if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11044 	{
11045 	  error (_("Failed to read in number of buckets\n"));
11046 	  goto no_hash;
11047 	}
11048 
11049       if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11050 	{
11051 	  error (_("Failed to read in number of chains\n"));
11052 	  goto no_hash;
11053 	}
11054 
11055       filedata->nbuckets = byte_get (nb, hash_ent_size);
11056       filedata->nchains = byte_get (nc, hash_ent_size);
11057 
11058       if (filedata->nbuckets != 0 && filedata->nchains != 0)
11059 	{
11060 	  filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11061 						hash_ent_size);
11062 	  filedata->chains  = get_dynamic_data (filedata, filedata->nchains,
11063 						hash_ent_size);
11064 
11065 	  if (filedata->buckets != NULL && filedata->chains != NULL)
11066 	    num_of_syms = filedata->nchains;
11067 	}
11068     no_hash:
11069       if (num_of_syms == 0)
11070 	{
11071 	  free (filedata->buckets);
11072 	  filedata->buckets = NULL;
11073 	  free (filedata->chains);
11074 	  filedata->chains = NULL;
11075 	  filedata->nbuckets = 0;
11076 	}
11077     }
11078 
11079   if (filedata->dynamic_info_DT_GNU_HASH)
11080     {
11081       unsigned char nb[16];
11082       bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
11083       bfd_vma buckets_vma;
11084       unsigned long hn;
11085 
11086       if (fseek (filedata->handle,
11087 		 (filedata->archive_file_offset
11088 		  + offset_from_vma (filedata,
11089 				     filedata->dynamic_info_DT_GNU_HASH,
11090 				     sizeof nb)),
11091 		 SEEK_SET))
11092 	{
11093 	  error (_("Unable to seek to start of dynamic information\n"));
11094 	  goto no_gnu_hash;
11095 	}
11096 
11097       if (fread (nb, 16, 1, filedata->handle) != 1)
11098 	{
11099 	  error (_("Failed to read in number of buckets\n"));
11100 	  goto no_gnu_hash;
11101 	}
11102 
11103       filedata->ngnubuckets = byte_get (nb, 4);
11104       filedata->gnusymidx = byte_get (nb + 4, 4);
11105       bitmaskwords = byte_get (nb + 8, 4);
11106       buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11107       if (is_32bit_elf)
11108 	buckets_vma += bitmaskwords * 4;
11109       else
11110 	buckets_vma += bitmaskwords * 8;
11111 
11112       if (fseek (filedata->handle,
11113 		 (filedata->archive_file_offset
11114 		  + offset_from_vma (filedata, buckets_vma, 4)),
11115 		 SEEK_SET))
11116 	{
11117 	  error (_("Unable to seek to start of dynamic information\n"));
11118 	  goto no_gnu_hash;
11119 	}
11120 
11121       filedata->gnubuckets
11122 	= get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11123 
11124       if (filedata->gnubuckets == NULL)
11125 	goto no_gnu_hash;
11126 
11127       for (i = 0; i < filedata->ngnubuckets; i++)
11128 	if (filedata->gnubuckets[i] != 0)
11129 	  {
11130 	    if (filedata->gnubuckets[i] < filedata->gnusymidx)
11131 	      goto no_gnu_hash;
11132 
11133 	    if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11134 	      maxchain = filedata->gnubuckets[i];
11135 	  }
11136 
11137       if (maxchain == 0xffffffff)
11138 	goto no_gnu_hash;
11139 
11140       maxchain -= filedata->gnusymidx;
11141 
11142       if (fseek (filedata->handle,
11143 		 (filedata->archive_file_offset
11144 		  + offset_from_vma (filedata,
11145 				     buckets_vma + 4 * (filedata->ngnubuckets
11146 							+ maxchain),
11147 				      4)),
11148 		 SEEK_SET))
11149 	{
11150 	  error (_("Unable to seek to start of dynamic information\n"));
11151 	  goto no_gnu_hash;
11152 	}
11153 
11154       do
11155 	{
11156 	  if (fread (nb, 4, 1, filedata->handle) != 1)
11157 	    {
11158 	      error (_("Failed to determine last chain length\n"));
11159 	      goto no_gnu_hash;
11160 	    }
11161 
11162 	  if (maxchain + 1 == 0)
11163 	    goto no_gnu_hash;
11164 
11165 	  ++maxchain;
11166 	}
11167       while ((byte_get (nb, 4) & 1) == 0);
11168 
11169       if (fseek (filedata->handle,
11170 		 (filedata->archive_file_offset
11171 		  + offset_from_vma (filedata, (buckets_vma
11172 						+ 4 * filedata->ngnubuckets),
11173 				     4)),
11174 		 SEEK_SET))
11175 	{
11176 	  error (_("Unable to seek to start of dynamic information\n"));
11177 	  goto no_gnu_hash;
11178 	}
11179 
11180       filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11181       filedata->ngnuchains = maxchain;
11182 
11183       if (filedata->gnuchains == NULL)
11184 	goto no_gnu_hash;
11185 
11186       if (filedata->dynamic_info_DT_MIPS_XHASH)
11187 	{
11188 	  if (fseek (filedata->handle,
11189 		     (filedata->archive_file_offset
11190 		      + offset_from_vma (filedata, (buckets_vma
11191 						    + 4 * (filedata->ngnubuckets
11192 							   + maxchain)), 4)),
11193 		     SEEK_SET))
11194 	    {
11195 	      error (_("Unable to seek to start of dynamic information\n"));
11196 	      goto no_gnu_hash;
11197 	    }
11198 
11199 	  filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11200 	  if (filedata->mipsxlat == NULL)
11201 	    goto no_gnu_hash;
11202 	}
11203 
11204       for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11205 	if (filedata->gnubuckets[hn] != 0)
11206 	  {
11207 	    bfd_vma si = filedata->gnubuckets[hn];
11208 	    bfd_vma off = si - filedata->gnusymidx;
11209 
11210 	    do
11211 	      {
11212 		if (filedata->dynamic_info_DT_MIPS_XHASH)
11213 		  {
11214 		    if (off < filedata->ngnuchains
11215 			&& filedata->mipsxlat[off] >= num_of_syms)
11216 		      num_of_syms = filedata->mipsxlat[off] + 1;
11217 		  }
11218 		else
11219 		  {
11220 		    if (si >= num_of_syms)
11221 		      num_of_syms = si + 1;
11222 		  }
11223 		si++;
11224 	      }
11225 	    while (off < filedata->ngnuchains
11226 		   && (filedata->gnuchains[off++] & 1) == 0);
11227 	  }
11228 
11229       if (num_of_syms == 0)
11230 	{
11231 	no_gnu_hash:
11232 	  free (filedata->mipsxlat);
11233 	  filedata->mipsxlat = NULL;
11234 	  free (filedata->gnuchains);
11235 	  filedata->gnuchains = NULL;
11236 	  free (filedata->gnubuckets);
11237 	  filedata->gnubuckets = NULL;
11238 	  filedata->ngnubuckets = 0;
11239 	  filedata->ngnuchains = 0;
11240 	}
11241     }
11242 
11243   return num_of_syms;
11244 }
11245 
11246 /* Parse and display the contents of the dynamic section.  */
11247 
11248 static bool
11249 process_dynamic_section (Filedata * filedata)
11250 {
11251   Elf_Internal_Dyn * entry;
11252 
11253   if (filedata->dynamic_size <= 1)
11254     {
11255       if (do_dynamic)
11256 	{
11257 	  if (filedata->is_separate)
11258 	    printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11259 		    filedata->file_name);
11260 	  else
11261 	    printf (_("\nThere is no dynamic section in this file.\n"));
11262 	}
11263 
11264       return true;
11265     }
11266 
11267   if (!get_dynamic_section (filedata))
11268     return false;
11269 
11270   /* Find the appropriate symbol table.  */
11271   if (filedata->dynamic_symbols == NULL || do_histogram)
11272     {
11273       unsigned long num_of_syms;
11274 
11275       for (entry = filedata->dynamic_section;
11276 	   entry < filedata->dynamic_section + filedata->dynamic_nent;
11277 	   ++entry)
11278 	if (entry->d_tag == DT_SYMTAB)
11279 	  filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11280 	else if (entry->d_tag == DT_SYMENT)
11281 	  filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11282 	else if (entry->d_tag == DT_HASH)
11283 	  filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11284 	else if (entry->d_tag == DT_GNU_HASH)
11285 	  filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11286 	else if ((filedata->file_header.e_machine == EM_MIPS
11287 		  || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11288 		 && entry->d_tag == DT_MIPS_XHASH)
11289 	  {
11290 	    filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11291 	    filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11292 	  }
11293 
11294       num_of_syms = get_num_dynamic_syms (filedata);
11295 
11296       if (num_of_syms != 0
11297 	  && filedata->dynamic_symbols == NULL
11298 	  && filedata->dynamic_info[DT_SYMTAB]
11299 	  && filedata->dynamic_info[DT_SYMENT])
11300 	{
11301 	  Elf_Internal_Phdr *seg;
11302 	  bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
11303 
11304 	  if (! get_program_headers (filedata))
11305 	    {
11306 	      error (_("Cannot interpret virtual addresses "
11307 		       "without program headers.\n"));
11308 	      return false;
11309 	    }
11310 
11311 	  for (seg = filedata->program_headers;
11312 	       seg < filedata->program_headers + filedata->file_header.e_phnum;
11313 	       ++seg)
11314 	    {
11315 	      if (seg->p_type != PT_LOAD)
11316 		continue;
11317 
11318 	      if (seg->p_offset + seg->p_filesz > filedata->file_size)
11319 		{
11320 		  /* See PR 21379 for a reproducer.  */
11321 		  error (_("Invalid PT_LOAD entry\n"));
11322 		  return false;
11323 		}
11324 
11325 	      if (vma >= (seg->p_vaddr & -seg->p_align)
11326 		  && vma < seg->p_vaddr + seg->p_filesz)
11327 		{
11328 		  /* Since we do not know how big the symbol table is,
11329 		     we default to reading in up to the end of PT_LOAD
11330 		     segment and processing that.  This is overkill, I
11331 		     know, but it should work.  */
11332 		  Elf_Internal_Shdr section;
11333 		  section.sh_offset = (vma - seg->p_vaddr
11334 				       + seg->p_offset);
11335 		  section.sh_size = (num_of_syms
11336 				     * filedata->dynamic_info[DT_SYMENT]);
11337 		  section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11338 
11339 		  if (do_checks
11340 		      && filedata->dynamic_symtab_section != NULL
11341 		      && ((filedata->dynamic_symtab_section->sh_offset
11342 			   != section.sh_offset)
11343 			  || (filedata->dynamic_symtab_section->sh_size
11344 			      != section.sh_size)
11345 			  || (filedata->dynamic_symtab_section->sh_entsize
11346 			      != section.sh_entsize)))
11347 		    warn (_("\
11348 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11349 
11350 		  section.sh_name = filedata->string_table_length;
11351 		  filedata->dynamic_symbols
11352 		    = get_elf_symbols (filedata, &section,
11353 				       &filedata->num_dynamic_syms);
11354 		  if (filedata->dynamic_symbols == NULL
11355 		      || filedata->num_dynamic_syms != num_of_syms)
11356 		    {
11357 		      error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11358 		      return false;
11359 		    }
11360 		  break;
11361 		}
11362 	    }
11363 	}
11364     }
11365 
11366   /* Similarly find a string table.  */
11367   if (filedata->dynamic_strings == NULL)
11368     for (entry = filedata->dynamic_section;
11369 	 entry < filedata->dynamic_section + filedata->dynamic_nent;
11370 	 ++entry)
11371       {
11372 	if (entry->d_tag == DT_STRTAB)
11373 	  filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11374 
11375 	if (entry->d_tag == DT_STRSZ)
11376 	  filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11377 
11378 	if (filedata->dynamic_info[DT_STRTAB]
11379 	    && filedata->dynamic_info[DT_STRSZ])
11380 	  {
11381 	    unsigned long offset;
11382 	    bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
11383 
11384 	    offset = offset_from_vma (filedata,
11385 				      filedata->dynamic_info[DT_STRTAB],
11386 				      str_tab_len);
11387 	    if (do_checks
11388 		&& filedata->dynamic_strtab_section
11389 		&& ((filedata->dynamic_strtab_section->sh_offset
11390 		     != (file_ptr) offset)
11391 		    || (filedata->dynamic_strtab_section->sh_size
11392 			!= str_tab_len)))
11393 	      warn (_("\
11394 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11395 
11396 	    filedata->dynamic_strings
11397 	      = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11398 				   _("dynamic string table"));
11399 	    if (filedata->dynamic_strings == NULL)
11400 	      {
11401 		error (_("Corrupt DT_STRTAB dynamic entry\n"));
11402 		break;
11403 	      }
11404 
11405 	    filedata->dynamic_strings_length = str_tab_len;
11406 	    break;
11407 	  }
11408       }
11409 
11410   /* And find the syminfo section if available.  */
11411   if (filedata->dynamic_syminfo == NULL)
11412     {
11413       unsigned long syminsz = 0;
11414 
11415       for (entry = filedata->dynamic_section;
11416 	   entry < filedata->dynamic_section + filedata->dynamic_nent;
11417 	   ++entry)
11418 	{
11419 	  if (entry->d_tag == DT_SYMINENT)
11420 	    {
11421 	      /* Note: these braces are necessary to avoid a syntax
11422 		 error from the SunOS4 C compiler.  */
11423 	      /* PR binutils/17531: A corrupt file can trigger this test.
11424 		 So do not use an assert, instead generate an error message.  */
11425 	      if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11426 		error (_("Bad value (%d) for SYMINENT entry\n"),
11427 		       (int) entry->d_un.d_val);
11428 	    }
11429 	  else if (entry->d_tag == DT_SYMINSZ)
11430 	    syminsz = entry->d_un.d_val;
11431 	  else if (entry->d_tag == DT_SYMINFO)
11432 	    filedata->dynamic_syminfo_offset
11433 	      = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11434 	}
11435 
11436       if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11437 	{
11438 	  Elf_External_Syminfo * extsyminfo;
11439 	  Elf_External_Syminfo * extsym;
11440 	  Elf_Internal_Syminfo * syminfo;
11441 
11442 	  /* There is a syminfo section.  Read the data.  */
11443 	  extsyminfo = (Elf_External_Syminfo *)
11444 	    get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11445 		      1, syminsz, _("symbol information"));
11446 	  if (!extsyminfo)
11447 	    return false;
11448 
11449 	  if (filedata->dynamic_syminfo != NULL)
11450 	    {
11451 	      error (_("Multiple dynamic symbol information sections found\n"));
11452 	      free (filedata->dynamic_syminfo);
11453 	    }
11454 	  filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11455 	  if (filedata->dynamic_syminfo == NULL)
11456 	    {
11457 	      error (_("Out of memory allocating %lu bytes "
11458 		       "for dynamic symbol info\n"),
11459 		     (unsigned long) syminsz);
11460 	      return false;
11461 	    }
11462 
11463 	  filedata->dynamic_syminfo_nent
11464 	    = syminsz / sizeof (Elf_External_Syminfo);
11465 	  for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
11466 	       syminfo < (filedata->dynamic_syminfo
11467 			  + filedata->dynamic_syminfo_nent);
11468 	       ++syminfo, ++extsym)
11469 	    {
11470 	      syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11471 	      syminfo->si_flags = BYTE_GET (extsym->si_flags);
11472 	    }
11473 
11474 	  free (extsyminfo);
11475 	}
11476     }
11477 
11478   if (do_dynamic && filedata->dynamic_addr)
11479     {
11480       if (filedata->is_separate)
11481 	printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11482 			  "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11483 			  (unsigned long) filedata->dynamic_nent),
11484 		filedata->file_name,
11485 		filedata->dynamic_addr,
11486 		(unsigned long) filedata->dynamic_nent);
11487       else
11488 	printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11489 			  "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11490 			  (unsigned long) filedata->dynamic_nent),
11491 		filedata->dynamic_addr,
11492 		(unsigned long) filedata->dynamic_nent);
11493     }
11494   if (do_dynamic)
11495     printf (_("  Tag        Type                         Name/Value\n"));
11496 
11497   for (entry = filedata->dynamic_section;
11498        entry < filedata->dynamic_section + filedata->dynamic_nent;
11499        entry++)
11500     {
11501       if (do_dynamic)
11502 	{
11503 	  const char * dtype;
11504 
11505 	  putchar (' ');
11506 	  print_vma (entry->d_tag, FULL_HEX);
11507 	  dtype = get_dynamic_type (filedata, entry->d_tag);
11508 	  printf (" (%s)%*s", dtype,
11509 		  ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11510 	}
11511 
11512       switch (entry->d_tag)
11513 	{
11514 	case DT_FLAGS:
11515 	  if (do_dynamic)
11516 	    print_dynamic_flags (entry->d_un.d_val);
11517 	  break;
11518 
11519 	case DT_AUXILIARY:
11520 	case DT_FILTER:
11521 	case DT_CONFIG:
11522 	case DT_DEPAUDIT:
11523 	case DT_AUDIT:
11524 	  if (do_dynamic)
11525 	    {
11526 	      switch (entry->d_tag)
11527 		{
11528 		case DT_AUXILIARY:
11529 		  printf (_("Auxiliary library"));
11530 		  break;
11531 
11532 		case DT_FILTER:
11533 		  printf (_("Filter library"));
11534 		  break;
11535 
11536 		case DT_CONFIG:
11537 		  printf (_("Configuration file"));
11538 		  break;
11539 
11540 		case DT_DEPAUDIT:
11541 		  printf (_("Dependency audit library"));
11542 		  break;
11543 
11544 		case DT_AUDIT:
11545 		  printf (_("Audit library"));
11546 		  break;
11547 		}
11548 
11549 	      if (valid_dynamic_name (filedata, entry->d_un.d_val))
11550 		printf (": [%s]\n",
11551 			get_dynamic_name (filedata, entry->d_un.d_val));
11552 	      else
11553 		{
11554 		  printf (": ");
11555 		  print_vma (entry->d_un.d_val, PREFIX_HEX);
11556 		  putchar ('\n');
11557 		}
11558 	    }
11559 	  break;
11560 
11561 	case DT_FEATURE:
11562 	  if (do_dynamic)
11563 	    {
11564 	      printf (_("Flags:"));
11565 
11566 	      if (entry->d_un.d_val == 0)
11567 		printf (_(" None\n"));
11568 	      else
11569 		{
11570 		  unsigned long int val = entry->d_un.d_val;
11571 
11572 		  if (val & DTF_1_PARINIT)
11573 		    {
11574 		      printf (" PARINIT");
11575 		      val ^= DTF_1_PARINIT;
11576 		    }
11577 		  if (val & DTF_1_CONFEXP)
11578 		    {
11579 		      printf (" CONFEXP");
11580 		      val ^= DTF_1_CONFEXP;
11581 		    }
11582 		  if (val != 0)
11583 		    printf (" %lx", val);
11584 		  puts ("");
11585 		}
11586 	    }
11587 	  break;
11588 
11589 	case DT_POSFLAG_1:
11590 	  if (do_dynamic)
11591 	    {
11592 	      printf (_("Flags:"));
11593 
11594 	      if (entry->d_un.d_val == 0)
11595 		printf (_(" None\n"));
11596 	      else
11597 		{
11598 		  unsigned long int val = entry->d_un.d_val;
11599 
11600 		  if (val & DF_P1_LAZYLOAD)
11601 		    {
11602 		      printf (" LAZYLOAD");
11603 		      val ^= DF_P1_LAZYLOAD;
11604 		    }
11605 		  if (val & DF_P1_GROUPPERM)
11606 		    {
11607 		      printf (" GROUPPERM");
11608 		      val ^= DF_P1_GROUPPERM;
11609 		    }
11610 		  if (val != 0)
11611 		    printf (" %lx", val);
11612 		  puts ("");
11613 		}
11614 	    }
11615 	  break;
11616 
11617 	case DT_FLAGS_1:
11618 	  if (do_dynamic)
11619 	    {
11620 	      printf (_("Flags:"));
11621 	      if (entry->d_un.d_val == 0)
11622 		printf (_(" None\n"));
11623 	      else
11624 		{
11625 		  unsigned long int val = entry->d_un.d_val;
11626 
11627 		  if (val & DF_1_NOW)
11628 		    {
11629 		      printf (" NOW");
11630 		      val ^= DF_1_NOW;
11631 		    }
11632 		  if (val & DF_1_GLOBAL)
11633 		    {
11634 		      printf (" GLOBAL");
11635 		      val ^= DF_1_GLOBAL;
11636 		    }
11637 		  if (val & DF_1_GROUP)
11638 		    {
11639 		      printf (" GROUP");
11640 		      val ^= DF_1_GROUP;
11641 		    }
11642 		  if (val & DF_1_NODELETE)
11643 		    {
11644 		      printf (" NODELETE");
11645 		      val ^= DF_1_NODELETE;
11646 		    }
11647 		  if (val & DF_1_LOADFLTR)
11648 		    {
11649 		      printf (" LOADFLTR");
11650 		      val ^= DF_1_LOADFLTR;
11651 		    }
11652 		  if (val & DF_1_INITFIRST)
11653 		    {
11654 		      printf (" INITFIRST");
11655 		      val ^= DF_1_INITFIRST;
11656 		    }
11657 		  if (val & DF_1_NOOPEN)
11658 		    {
11659 		      printf (" NOOPEN");
11660 		      val ^= DF_1_NOOPEN;
11661 		    }
11662 		  if (val & DF_1_ORIGIN)
11663 		    {
11664 		      printf (" ORIGIN");
11665 		      val ^= DF_1_ORIGIN;
11666 		    }
11667 		  if (val & DF_1_DIRECT)
11668 		    {
11669 		      printf (" DIRECT");
11670 		      val ^= DF_1_DIRECT;
11671 		    }
11672 		  if (val & DF_1_TRANS)
11673 		    {
11674 		      printf (" TRANS");
11675 		      val ^= DF_1_TRANS;
11676 		    }
11677 		  if (val & DF_1_INTERPOSE)
11678 		    {
11679 		      printf (" INTERPOSE");
11680 		      val ^= DF_1_INTERPOSE;
11681 		    }
11682 		  if (val & DF_1_NODEFLIB)
11683 		    {
11684 		      printf (" NODEFLIB");
11685 		      val ^= DF_1_NODEFLIB;
11686 		    }
11687 		  if (val & DF_1_NODUMP)
11688 		    {
11689 		      printf (" NODUMP");
11690 		      val ^= DF_1_NODUMP;
11691 		    }
11692 		  if (val & DF_1_CONFALT)
11693 		    {
11694 		      printf (" CONFALT");
11695 		      val ^= DF_1_CONFALT;
11696 		    }
11697 		  if (val & DF_1_ENDFILTEE)
11698 		    {
11699 		      printf (" ENDFILTEE");
11700 		      val ^= DF_1_ENDFILTEE;
11701 		    }
11702 		  if (val & DF_1_DISPRELDNE)
11703 		    {
11704 		      printf (" DISPRELDNE");
11705 		      val ^= DF_1_DISPRELDNE;
11706 		    }
11707 		  if (val & DF_1_DISPRELPND)
11708 		    {
11709 		      printf (" DISPRELPND");
11710 		      val ^= DF_1_DISPRELPND;
11711 		    }
11712 		  if (val & DF_1_NODIRECT)
11713 		    {
11714 		      printf (" NODIRECT");
11715 		      val ^= DF_1_NODIRECT;
11716 		    }
11717 		  if (val & DF_1_IGNMULDEF)
11718 		    {
11719 		      printf (" IGNMULDEF");
11720 		      val ^= DF_1_IGNMULDEF;
11721 		    }
11722 		  if (val & DF_1_NOKSYMS)
11723 		    {
11724 		      printf (" NOKSYMS");
11725 		      val ^= DF_1_NOKSYMS;
11726 		    }
11727 		  if (val & DF_1_NOHDR)
11728 		    {
11729 		      printf (" NOHDR");
11730 		      val ^= DF_1_NOHDR;
11731 		    }
11732 		  if (val & DF_1_EDITED)
11733 		    {
11734 		      printf (" EDITED");
11735 		      val ^= DF_1_EDITED;
11736 		    }
11737 		  if (val & DF_1_NORELOC)
11738 		    {
11739 		      printf (" NORELOC");
11740 		      val ^= DF_1_NORELOC;
11741 		    }
11742 		  if (val & DF_1_SYMINTPOSE)
11743 		    {
11744 		      printf (" SYMINTPOSE");
11745 		      val ^= DF_1_SYMINTPOSE;
11746 		    }
11747 		  if (val & DF_1_GLOBAUDIT)
11748 		    {
11749 		      printf (" GLOBAUDIT");
11750 		      val ^= DF_1_GLOBAUDIT;
11751 		    }
11752 		  if (val & DF_1_SINGLETON)
11753 		    {
11754 		      printf (" SINGLETON");
11755 		      val ^= DF_1_SINGLETON;
11756 		    }
11757 		  if (val & DF_1_STUB)
11758 		    {
11759 		      printf (" STUB");
11760 		      val ^= DF_1_STUB;
11761 		    }
11762 		  if (val & DF_1_PIE)
11763 		    {
11764 		      printf (" PIE");
11765 		      val ^= DF_1_PIE;
11766 		    }
11767 		  if (val & DF_1_KMOD)
11768 		    {
11769 		      printf (" KMOD");
11770 		      val ^= DF_1_KMOD;
11771 		    }
11772 		  if (val & DF_1_WEAKFILTER)
11773 		    {
11774 		      printf (" WEAKFILTER");
11775 		      val ^= DF_1_WEAKFILTER;
11776 		    }
11777 		  if (val & DF_1_NOCOMMON)
11778 		    {
11779 		      printf (" NOCOMMON");
11780 		      val ^= DF_1_NOCOMMON;
11781 		    }
11782 		  if (val != 0)
11783 		    printf (" %lx", val);
11784 		  puts ("");
11785 		}
11786 	    }
11787 	  break;
11788 
11789 	case DT_PLTREL:
11790 	  filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11791 	  if (do_dynamic)
11792 	    puts (get_dynamic_type (filedata, entry->d_un.d_val));
11793 	  break;
11794 
11795 	case DT_NULL	:
11796 	case DT_NEEDED	:
11797 	case DT_PLTGOT	:
11798 	case DT_HASH	:
11799 	case DT_STRTAB	:
11800 	case DT_SYMTAB	:
11801 	case DT_RELA	:
11802 	case DT_INIT	:
11803 	case DT_FINI	:
11804 	case DT_SONAME	:
11805 	case DT_RPATH	:
11806 	case DT_SYMBOLIC:
11807 	case DT_REL	:
11808 	case DT_RELR    :
11809 	case DT_DEBUG	:
11810 	case DT_TEXTREL	:
11811 	case DT_JMPREL	:
11812 	case DT_RUNPATH	:
11813 	  filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11814 
11815 	  if (do_dynamic)
11816 	    {
11817 	      const char *name;
11818 
11819 	      if (valid_dynamic_name (filedata, entry->d_un.d_val))
11820 		name = get_dynamic_name (filedata, entry->d_un.d_val);
11821 	      else
11822 		name = NULL;
11823 
11824 	      if (name)
11825 		{
11826 		  switch (entry->d_tag)
11827 		    {
11828 		    case DT_NEEDED:
11829 		      printf (_("Shared library: [%s]"), name);
11830 
11831 		      if (filedata->program_interpreter
11832 			  && streq (name, filedata->program_interpreter))
11833 			printf (_(" program interpreter"));
11834 		      break;
11835 
11836 		    case DT_SONAME:
11837 		      printf (_("Library soname: [%s]"), name);
11838 		      break;
11839 
11840 		    case DT_RPATH:
11841 		      printf (_("Library rpath: [%s]"), name);
11842 		      break;
11843 
11844 		    case DT_RUNPATH:
11845 		      printf (_("Library runpath: [%s]"), name);
11846 		      break;
11847 
11848 		    default:
11849 		      print_vma (entry->d_un.d_val, PREFIX_HEX);
11850 		      break;
11851 		    }
11852 		}
11853 	      else
11854 		print_vma (entry->d_un.d_val, PREFIX_HEX);
11855 
11856 	      putchar ('\n');
11857 	    }
11858 	  break;
11859 
11860 	case DT_PLTRELSZ:
11861 	case DT_RELASZ	:
11862 	case DT_STRSZ	:
11863 	case DT_RELSZ	:
11864 	case DT_RELAENT	:
11865 	case DT_RELRENT	:
11866 	case DT_RELRSZ	:
11867 	case DT_SYMENT	:
11868 	case DT_RELENT	:
11869 	  filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11870 	  /* Fall through.  */
11871 	case DT_PLTPADSZ:
11872 	case DT_MOVEENT	:
11873 	case DT_MOVESZ	:
11874 	case DT_PREINIT_ARRAYSZ:
11875 	case DT_INIT_ARRAYSZ:
11876 	case DT_FINI_ARRAYSZ:
11877 	case DT_GNU_CONFLICTSZ:
11878 	case DT_GNU_LIBLISTSZ:
11879 	  if (do_dynamic)
11880 	    {
11881 	      print_vma (entry->d_un.d_val, UNSIGNED);
11882 	      printf (_(" (bytes)\n"));
11883 	    }
11884 	  break;
11885 
11886 	case DT_VERDEFNUM:
11887 	case DT_VERNEEDNUM:
11888 	case DT_RELACOUNT:
11889 	case DT_RELCOUNT:
11890 	  if (do_dynamic)
11891 	    {
11892 	      print_vma (entry->d_un.d_val, UNSIGNED);
11893 	      putchar ('\n');
11894 	    }
11895 	  break;
11896 
11897 	case DT_SYMINSZ:
11898 	case DT_SYMINENT:
11899 	case DT_SYMINFO:
11900 	case DT_USED:
11901 	case DT_INIT_ARRAY:
11902 	case DT_FINI_ARRAY:
11903 	  if (do_dynamic)
11904 	    {
11905 	      if (entry->d_tag == DT_USED
11906 		  && valid_dynamic_name (filedata, entry->d_un.d_val))
11907 		{
11908 		  const char *name
11909 		    = get_dynamic_name (filedata, entry->d_un.d_val);
11910 
11911 		  if (*name)
11912 		    {
11913 		      printf (_("Not needed object: [%s]\n"), name);
11914 		      break;
11915 		    }
11916 		}
11917 
11918 	      print_vma (entry->d_un.d_val, PREFIX_HEX);
11919 	      putchar ('\n');
11920 	    }
11921 	  break;
11922 
11923 	case DT_BIND_NOW:
11924 	  /* The value of this entry is ignored.  */
11925 	  if (do_dynamic)
11926 	    putchar ('\n');
11927 	  break;
11928 
11929 	case DT_GNU_PRELINKED:
11930 	  if (do_dynamic)
11931 	    {
11932 	      struct tm * tmp;
11933 	      time_t atime = entry->d_un.d_val;
11934 
11935 	      tmp = gmtime (&atime);
11936 	      /* PR 17533 file: 041-1244816-0.004.  */
11937 	      if (tmp == NULL)
11938 		printf (_("<corrupt time val: %lx"),
11939 			(unsigned long) atime);
11940 	      else
11941 		printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11942 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11943 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11944 
11945 	    }
11946 	  break;
11947 
11948 	case DT_GNU_HASH:
11949 	  filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11950 	  if (do_dynamic)
11951 	    {
11952 	      print_vma (entry->d_un.d_val, PREFIX_HEX);
11953 	      putchar ('\n');
11954 	    }
11955 	  break;
11956 
11957 	case DT_GNU_FLAGS_1:
11958 	  if (do_dynamic)
11959 	    {
11960 	      printf (_("Flags:"));
11961 	      if (entry->d_un.d_val == 0)
11962 		printf (_(" None\n"));
11963 	      else
11964 		{
11965 		  unsigned long int val = entry->d_un.d_val;
11966 
11967 		  if (val & DF_GNU_1_UNIQUE)
11968 		    {
11969 		      printf (" UNIQUE");
11970 		      val ^= DF_GNU_1_UNIQUE;
11971 		    }
11972 		  if (val != 0)
11973 		    printf (" %lx", val);
11974 		  puts ("");
11975 		}
11976 	    }
11977 	  break;
11978 
11979 	default:
11980 	  if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11981 	    filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11982 	      = entry->d_un.d_val;
11983 
11984 	  if (do_dynamic)
11985 	    {
11986 	      switch (filedata->file_header.e_machine)
11987 		{
11988 		case EM_AARCH64:
11989 		  dynamic_section_aarch64_val (entry);
11990 		  break;
11991 		case EM_MIPS:
11992 		case EM_MIPS_RS3_LE:
11993 		  dynamic_section_mips_val (filedata, entry);
11994 		  break;
11995 		case EM_PARISC:
11996 		  dynamic_section_parisc_val (entry);
11997 		  break;
11998 		case EM_IA_64:
11999 		  dynamic_section_ia64_val (entry);
12000 		  break;
12001 		default:
12002 		  print_vma (entry->d_un.d_val, PREFIX_HEX);
12003 		  putchar ('\n');
12004 		}
12005 	    }
12006 	  break;
12007 	}
12008     }
12009 
12010   return true;
12011 }
12012 
12013 static char *
12014 get_ver_flags (unsigned int flags)
12015 {
12016   static char buff[128];
12017 
12018   buff[0] = 0;
12019 
12020   if (flags == 0)
12021     return _("none");
12022 
12023   if (flags & VER_FLG_BASE)
12024     strcat (buff, "BASE");
12025 
12026   if (flags & VER_FLG_WEAK)
12027     {
12028       if (flags & VER_FLG_BASE)
12029 	strcat (buff, " | ");
12030 
12031       strcat (buff, "WEAK");
12032     }
12033 
12034   if (flags & VER_FLG_INFO)
12035     {
12036       if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12037 	strcat (buff, " | ");
12038 
12039       strcat (buff, "INFO");
12040     }
12041 
12042   if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12043     {
12044       if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12045 	strcat (buff, " | ");
12046 
12047       strcat (buff, _("<unknown>"));
12048     }
12049 
12050   return buff;
12051 }
12052 
12053 /* Display the contents of the version sections.  */
12054 
12055 static bool
12056 process_version_sections (Filedata * filedata)
12057 {
12058   Elf_Internal_Shdr * section;
12059   unsigned i;
12060   bool found = false;
12061 
12062   if (! do_version)
12063     return true;
12064 
12065   for (i = 0, section = filedata->section_headers;
12066        i < filedata->file_header.e_shnum;
12067        i++, section++)
12068     {
12069       switch (section->sh_type)
12070 	{
12071 	case SHT_GNU_verdef:
12072 	  {
12073 	    Elf_External_Verdef * edefs;
12074 	    unsigned long idx;
12075 	    unsigned long cnt;
12076 	    char * endbuf;
12077 
12078 	    found = true;
12079 
12080 	    if (filedata->is_separate)
12081 	      printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12082 				"\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12083 				section->sh_info),
12084 		      filedata->file_name,
12085 		      printable_section_name (filedata, section),
12086 		      section->sh_info);
12087 	    else
12088 	      printf (ngettext ("\nVersion definition section '%s' "
12089 				"contains %u entry:\n",
12090 				"\nVersion definition section '%s' "
12091 				"contains %u entries:\n",
12092 				section->sh_info),
12093 		      printable_section_name (filedata, section),
12094 		      section->sh_info);
12095 
12096 	    printf (_(" Addr: 0x"));
12097 	    printf_vma (section->sh_addr);
12098 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
12099 		    (unsigned long) section->sh_offset, section->sh_link,
12100 		    printable_section_name_from_index (filedata, section->sh_link));
12101 
12102 	    edefs = (Elf_External_Verdef *)
12103                 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12104                           _("version definition section"));
12105 	    if (!edefs)
12106 	      break;
12107 	    endbuf = (char *) edefs + section->sh_size;
12108 
12109 	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12110 	      {
12111 		char * vstart;
12112 		Elf_External_Verdef * edef;
12113 		Elf_Internal_Verdef ent;
12114 		Elf_External_Verdaux * eaux;
12115 		Elf_Internal_Verdaux aux;
12116 		unsigned long isum;
12117 		int j;
12118 
12119 		vstart = ((char *) edefs) + idx;
12120 		if (vstart + sizeof (*edef) > endbuf)
12121 		  break;
12122 
12123 		edef = (Elf_External_Verdef *) vstart;
12124 
12125 		ent.vd_version = BYTE_GET (edef->vd_version);
12126 		ent.vd_flags   = BYTE_GET (edef->vd_flags);
12127 		ent.vd_ndx     = BYTE_GET (edef->vd_ndx);
12128 		ent.vd_cnt     = BYTE_GET (edef->vd_cnt);
12129 		ent.vd_hash    = BYTE_GET (edef->vd_hash);
12130 		ent.vd_aux     = BYTE_GET (edef->vd_aux);
12131 		ent.vd_next    = BYTE_GET (edef->vd_next);
12132 
12133 		printf (_("  %#06lx: Rev: %d  Flags: %s"),
12134 			idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12135 
12136 		printf (_("  Index: %d  Cnt: %d  "),
12137 			ent.vd_ndx, ent.vd_cnt);
12138 
12139 		/* Check for overflow.  */
12140 		if (ent.vd_aux > (size_t) (endbuf - vstart))
12141 		  break;
12142 
12143 		vstart += ent.vd_aux;
12144 
12145 		if (vstart + sizeof (*eaux) > endbuf)
12146 		  break;
12147 		eaux = (Elf_External_Verdaux *) vstart;
12148 
12149 		aux.vda_name = BYTE_GET (eaux->vda_name);
12150 		aux.vda_next = BYTE_GET (eaux->vda_next);
12151 
12152 		if (valid_dynamic_name (filedata, aux.vda_name))
12153 		  printf (_("Name: %s\n"),
12154 			  get_dynamic_name (filedata, aux.vda_name));
12155 		else
12156 		  printf (_("Name index: %ld\n"), aux.vda_name);
12157 
12158 		isum = idx + ent.vd_aux;
12159 
12160 		for (j = 1; j < ent.vd_cnt; j++)
12161 		  {
12162 		    if (aux.vda_next < sizeof (*eaux)
12163 			&& !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12164 		      {
12165 			warn (_("Invalid vda_next field of %lx\n"),
12166 			      aux.vda_next);
12167 			j = ent.vd_cnt;
12168 			break;
12169 		      }
12170 		    /* Check for overflow.  */
12171 		    if (aux.vda_next > (size_t) (endbuf - vstart))
12172 		      break;
12173 
12174 		    isum   += aux.vda_next;
12175 		    vstart += aux.vda_next;
12176 
12177 		    if (vstart + sizeof (*eaux) > endbuf)
12178 		      break;
12179 		    eaux = (Elf_External_Verdaux *) vstart;
12180 
12181 		    aux.vda_name = BYTE_GET (eaux->vda_name);
12182 		    aux.vda_next = BYTE_GET (eaux->vda_next);
12183 
12184 		    if (valid_dynamic_name (filedata, aux.vda_name))
12185 		      printf (_("  %#06lx: Parent %d: %s\n"),
12186 			      isum, j,
12187 			      get_dynamic_name (filedata, aux.vda_name));
12188 		    else
12189 		      printf (_("  %#06lx: Parent %d, name index: %ld\n"),
12190 			      isum, j, aux.vda_name);
12191 		  }
12192 
12193 		if (j < ent.vd_cnt)
12194 		  printf (_("  Version def aux past end of section\n"));
12195 
12196 		/* PR 17531:
12197 		   file: id:000001,src:000172+005151,op:splice,rep:2.  */
12198 		if (ent.vd_next < sizeof (*edef)
12199 		    && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12200 		  {
12201 		    warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12202 		    cnt = section->sh_info;
12203 		    break;
12204 		  }
12205 		if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12206 		  break;
12207 
12208 		idx += ent.vd_next;
12209 	      }
12210 
12211 	    if (cnt < section->sh_info)
12212 	      printf (_("  Version definition past end of section\n"));
12213 
12214 	    free (edefs);
12215 	  }
12216 	  break;
12217 
12218 	case SHT_GNU_verneed:
12219 	  {
12220 	    Elf_External_Verneed * eneed;
12221 	    unsigned long idx;
12222 	    unsigned long cnt;
12223 	    char * endbuf;
12224 
12225 	    found = true;
12226 
12227 	    if (filedata->is_separate)
12228 	      printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12229 				"\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12230 				section->sh_info),
12231 		      filedata->file_name,
12232 		      printable_section_name (filedata, section),
12233 		      section->sh_info);
12234 	    else
12235 	      printf (ngettext ("\nVersion needs section '%s' "
12236 				"contains %u entry:\n",
12237 				"\nVersion needs section '%s' "
12238 				"contains %u entries:\n",
12239 				section->sh_info),
12240 		      printable_section_name (filedata, section),
12241 		      section->sh_info);
12242 
12243 	    printf (_(" Addr: 0x"));
12244 	    printf_vma (section->sh_addr);
12245 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
12246 		    (unsigned long) section->sh_offset, section->sh_link,
12247 		    printable_section_name_from_index (filedata, section->sh_link));
12248 
12249 	    eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12250                                                        section->sh_offset, 1,
12251                                                        section->sh_size,
12252                                                        _("Version Needs section"));
12253 	    if (!eneed)
12254 	      break;
12255 	    endbuf = (char *) eneed + section->sh_size;
12256 
12257 	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12258 	      {
12259 		Elf_External_Verneed * entry;
12260 		Elf_Internal_Verneed ent;
12261 		unsigned long isum;
12262 		int j;
12263 		char * vstart;
12264 
12265 		vstart = ((char *) eneed) + idx;
12266 		if (vstart + sizeof (*entry) > endbuf)
12267 		  break;
12268 
12269 		entry = (Elf_External_Verneed *) vstart;
12270 
12271 		ent.vn_version = BYTE_GET (entry->vn_version);
12272 		ent.vn_cnt     = BYTE_GET (entry->vn_cnt);
12273 		ent.vn_file    = BYTE_GET (entry->vn_file);
12274 		ent.vn_aux     = BYTE_GET (entry->vn_aux);
12275 		ent.vn_next    = BYTE_GET (entry->vn_next);
12276 
12277 		printf (_("  %#06lx: Version: %d"), idx, ent.vn_version);
12278 
12279 		if (valid_dynamic_name (filedata, ent.vn_file))
12280 		  printf (_("  File: %s"),
12281 			  get_dynamic_name (filedata, ent.vn_file));
12282 		else
12283 		  printf (_("  File: %lx"), ent.vn_file);
12284 
12285 		printf (_("  Cnt: %d\n"), ent.vn_cnt);
12286 
12287 		/* Check for overflow.  */
12288 		if (ent.vn_aux > (size_t) (endbuf - vstart))
12289 		  break;
12290 		vstart += ent.vn_aux;
12291 
12292 		for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12293 		  {
12294 		    Elf_External_Vernaux * eaux;
12295 		    Elf_Internal_Vernaux aux;
12296 
12297 		    if (vstart + sizeof (*eaux) > endbuf)
12298 		      break;
12299 		    eaux = (Elf_External_Vernaux *) vstart;
12300 
12301 		    aux.vna_hash  = BYTE_GET (eaux->vna_hash);
12302 		    aux.vna_flags = BYTE_GET (eaux->vna_flags);
12303 		    aux.vna_other = BYTE_GET (eaux->vna_other);
12304 		    aux.vna_name  = BYTE_GET (eaux->vna_name);
12305 		    aux.vna_next  = BYTE_GET (eaux->vna_next);
12306 
12307 		    if (valid_dynamic_name (filedata, aux.vna_name))
12308 		      printf (_("  %#06lx:   Name: %s"),
12309 			      isum, get_dynamic_name (filedata, aux.vna_name));
12310 		    else
12311 		      printf (_("  %#06lx:   Name index: %lx"),
12312 			      isum, aux.vna_name);
12313 
12314 		    printf (_("  Flags: %s  Version: %d\n"),
12315 			    get_ver_flags (aux.vna_flags), aux.vna_other);
12316 
12317 		    if (aux.vna_next < sizeof (*eaux)
12318 			&& !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12319 		      {
12320 			warn (_("Invalid vna_next field of %lx\n"),
12321 			      aux.vna_next);
12322 			j = ent.vn_cnt;
12323 			break;
12324 		      }
12325 		    /* Check for overflow.  */
12326 		    if (aux.vna_next > (size_t) (endbuf - vstart))
12327 		      break;
12328 		    isum   += aux.vna_next;
12329 		    vstart += aux.vna_next;
12330 		  }
12331 
12332 		if (j < ent.vn_cnt)
12333 		  warn (_("Missing Version Needs auxiliary information\n"));
12334 
12335 		if (ent.vn_next < sizeof (*entry)
12336 		    && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12337 		  {
12338 		    warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12339 		    cnt = section->sh_info;
12340 		    break;
12341 		  }
12342 		if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12343 		  break;
12344 		idx += ent.vn_next;
12345 	      }
12346 
12347 	    if (cnt < section->sh_info)
12348 	      warn (_("Missing Version Needs information\n"));
12349 
12350 	    free (eneed);
12351 	  }
12352 	  break;
12353 
12354 	case SHT_GNU_versym:
12355 	  {
12356 	    Elf_Internal_Shdr * link_section;
12357 	    size_t total;
12358 	    unsigned int cnt;
12359 	    unsigned char * edata;
12360 	    unsigned short * data;
12361 	    char * strtab;
12362 	    Elf_Internal_Sym * symbols;
12363 	    Elf_Internal_Shdr * string_sec;
12364 	    unsigned long num_syms;
12365 	    long off;
12366 
12367 	    if (section->sh_link >= filedata->file_header.e_shnum)
12368 	      break;
12369 
12370 	    link_section = filedata->section_headers + section->sh_link;
12371 	    total = section->sh_size / sizeof (Elf_External_Versym);
12372 
12373 	    if (link_section->sh_link >= filedata->file_header.e_shnum)
12374 	      break;
12375 
12376 	    found = true;
12377 
12378 	    symbols = get_elf_symbols (filedata, link_section, & num_syms);
12379 	    if (symbols == NULL)
12380 	      break;
12381 
12382 	    string_sec = filedata->section_headers + link_section->sh_link;
12383 
12384 	    strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12385                                         string_sec->sh_size,
12386                                         _("version string table"));
12387 	    if (!strtab)
12388 	      {
12389 		free (symbols);
12390 		break;
12391 	      }
12392 
12393 	    if (filedata->is_separate)
12394 	      printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
12395 				"\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
12396 				total),
12397 		      filedata->file_name,
12398 		      printable_section_name (filedata, section),
12399 		      (unsigned long) total);
12400 	    else
12401 	      printf (ngettext ("\nVersion symbols section '%s' "
12402 				"contains %lu entry:\n",
12403 				"\nVersion symbols section '%s' "
12404 				"contains %lu entries:\n",
12405 				total),
12406 		      printable_section_name (filedata, section),
12407 		      (unsigned long) total);
12408 
12409 	    printf (_(" Addr: 0x"));
12410 	    printf_vma (section->sh_addr);
12411 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
12412 		    (unsigned long) section->sh_offset, section->sh_link,
12413 		    printable_section_name (filedata, link_section));
12414 
12415 	    off = offset_from_vma (filedata,
12416 				   filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12417 				   total * sizeof (short));
12418 	    edata = (unsigned char *) get_data (NULL, filedata, off,
12419 						sizeof (short), total,
12420 						_("version symbol data"));
12421 	    if (!edata)
12422 	      {
12423 		free (strtab);
12424 		free (symbols);
12425 		break;
12426 	      }
12427 
12428 	    data = (short unsigned int *) cmalloc (total, sizeof (short));
12429 
12430 	    for (cnt = total; cnt --;)
12431 	      data[cnt] = byte_get (edata + cnt * sizeof (short),
12432 				    sizeof (short));
12433 
12434 	    free (edata);
12435 
12436 	    for (cnt = 0; cnt < total; cnt += 4)
12437 	      {
12438 		int j, nn;
12439 		char *name;
12440 		char *invalid = _("*invalid*");
12441 
12442 		printf ("  %03x:", cnt);
12443 
12444 		for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12445 		  switch (data[cnt + j])
12446 		    {
12447 		    case 0:
12448 		      fputs (_("   0 (*local*)    "), stdout);
12449 		      break;
12450 
12451 		    case 1:
12452 		      fputs (_("   1 (*global*)   "), stdout);
12453 		      break;
12454 
12455 		    default:
12456 		      nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12457 				   data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12458 
12459 		      /* If this index value is greater than the size of the symbols
12460 		         array, break to avoid an out-of-bounds read.  */
12461 		      if ((unsigned long)(cnt + j) >= num_syms)
12462 		        {
12463 		          warn (_("invalid index into symbol array\n"));
12464 		          break;
12465 			}
12466 
12467 		      name = NULL;
12468 		      if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12469 			{
12470 			  Elf_Internal_Verneed ivn;
12471 			  unsigned long offset;
12472 
12473 			  offset = offset_from_vma
12474 			    (filedata,
12475 			     filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12476 			     sizeof (Elf_External_Verneed));
12477 
12478 			  do
12479 			    {
12480 			      Elf_Internal_Vernaux ivna;
12481 			      Elf_External_Verneed evn;
12482 			      Elf_External_Vernaux evna;
12483 			      unsigned long a_off;
12484 
12485 			      if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12486 					    _("version need")) == NULL)
12487 				break;
12488 
12489 			      ivn.vn_aux  = BYTE_GET (evn.vn_aux);
12490 			      ivn.vn_next = BYTE_GET (evn.vn_next);
12491 
12492 			      a_off = offset + ivn.vn_aux;
12493 
12494 			      do
12495 				{
12496 				  if (get_data (&evna, filedata, a_off, sizeof (evna),
12497 						1, _("version need aux (2)")) == NULL)
12498 				    {
12499 				      ivna.vna_next  = 0;
12500 				      ivna.vna_other = 0;
12501 				    }
12502 				  else
12503 				    {
12504 				      ivna.vna_next  = BYTE_GET (evna.vna_next);
12505 				      ivna.vna_other = BYTE_GET (evna.vna_other);
12506 				    }
12507 
12508 				  a_off += ivna.vna_next;
12509 				}
12510 			      while (ivna.vna_other != data[cnt + j]
12511 				     && ivna.vna_next != 0);
12512 
12513 			      if (ivna.vna_other == data[cnt + j])
12514 				{
12515 				  ivna.vna_name = BYTE_GET (evna.vna_name);
12516 
12517 				  if (ivna.vna_name >= string_sec->sh_size)
12518 				    name = invalid;
12519 				  else
12520 				    name = strtab + ivna.vna_name;
12521 				  break;
12522 				}
12523 
12524 			      offset += ivn.vn_next;
12525 			    }
12526 			  while (ivn.vn_next);
12527 			}
12528 
12529 		      if (data[cnt + j] != 0x8001
12530 			  && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12531 			{
12532 			  Elf_Internal_Verdef ivd;
12533 			  Elf_External_Verdef evd;
12534 			  unsigned long offset;
12535 
12536 			  offset = offset_from_vma
12537 			    (filedata,
12538 			     filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12539 			     sizeof evd);
12540 
12541 			  do
12542 			    {
12543 			      if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12544 					    _("version def")) == NULL)
12545 				{
12546 				  ivd.vd_next = 0;
12547 				  /* PR 17531: file: 046-1082287-0.004.  */
12548 				  ivd.vd_ndx  = (data[cnt + j] & VERSYM_VERSION) + 1;
12549 				  break;
12550 				}
12551 			      else
12552 				{
12553 				  ivd.vd_next = BYTE_GET (evd.vd_next);
12554 				  ivd.vd_ndx  = BYTE_GET (evd.vd_ndx);
12555 				}
12556 
12557 			      offset += ivd.vd_next;
12558 			    }
12559 			  while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12560 				 && ivd.vd_next != 0);
12561 
12562 			  if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12563 			    {
12564 			      Elf_External_Verdaux evda;
12565 			      Elf_Internal_Verdaux ivda;
12566 
12567 			      ivd.vd_aux = BYTE_GET (evd.vd_aux);
12568 
12569 			      if (get_data (&evda, filedata,
12570 					    offset - ivd.vd_next + ivd.vd_aux,
12571 					    sizeof (evda), 1,
12572 					    _("version def aux")) == NULL)
12573 				break;
12574 
12575 			      ivda.vda_name = BYTE_GET (evda.vda_name);
12576 
12577 			      if (ivda.vda_name >= string_sec->sh_size)
12578 				name = invalid;
12579 			      else if (name != NULL && name != invalid)
12580 				name = _("*both*");
12581 			      else
12582 				name = strtab + ivda.vda_name;
12583 			    }
12584 			}
12585 		      if (name != NULL)
12586 			nn += printf ("(%s%-*s",
12587 				      name,
12588 				      12 - (int) strlen (name),
12589 				      ")");
12590 
12591 		      if (nn < 18)
12592 			printf ("%*c", 18 - nn, ' ');
12593 		    }
12594 
12595 		putchar ('\n');
12596 	      }
12597 
12598 	    free (data);
12599 	    free (strtab);
12600 	    free (symbols);
12601 	  }
12602 	  break;
12603 
12604 	default:
12605 	  break;
12606 	}
12607     }
12608 
12609   if (! found)
12610     {
12611       if (filedata->is_separate)
12612 	printf (_("\nNo version information found in linked file '%s'.\n"),
12613 		filedata->file_name);
12614       else
12615 	printf (_("\nNo version information found in this file.\n"));
12616     }
12617 
12618   return true;
12619 }
12620 
12621 static const char *
12622 get_symbol_binding (Filedata * filedata, unsigned int binding)
12623 {
12624   static char buff[64];
12625 
12626   switch (binding)
12627     {
12628     case STB_LOCAL:	return "LOCAL";
12629     case STB_GLOBAL:	return "GLOBAL";
12630     case STB_WEAK:	return "WEAK";
12631     default:
12632       if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12633 	snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12634 		  binding);
12635       else if (binding >= STB_LOOS && binding <= STB_HIOS)
12636 	{
12637 	  if (binding == STB_GNU_UNIQUE
12638 	      && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12639 	    return "UNIQUE";
12640 	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12641 	}
12642       else
12643 	snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12644       return buff;
12645     }
12646 }
12647 
12648 static const char *
12649 get_symbol_type (Filedata * filedata, unsigned int type)
12650 {
12651   static char buff[64];
12652 
12653   switch (type)
12654     {
12655     case STT_NOTYPE:	return "NOTYPE";
12656     case STT_OBJECT:	return "OBJECT";
12657     case STT_FUNC:	return "FUNC";
12658     case STT_SECTION:	return "SECTION";
12659     case STT_FILE:	return "FILE";
12660     case STT_COMMON:	return "COMMON";
12661     case STT_TLS:	return "TLS";
12662     case STT_RELC:      return "RELC";
12663     case STT_SRELC:     return "SRELC";
12664     default:
12665       if (type >= STT_LOPROC && type <= STT_HIPROC)
12666 	{
12667 	  if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12668 	    return "THUMB_FUNC";
12669 
12670 	  if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12671 	    return "REGISTER";
12672 
12673 	  if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12674 	    return "PARISC_MILLI";
12675 
12676 	  snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12677 	}
12678       else if (type >= STT_LOOS && type <= STT_HIOS)
12679 	{
12680 	  if (filedata->file_header.e_machine == EM_PARISC)
12681 	    {
12682 	      if (type == STT_HP_OPAQUE)
12683 		return "HP_OPAQUE";
12684 	      if (type == STT_HP_STUB)
12685 		return "HP_STUB";
12686 	    }
12687 
12688 	  if (type == STT_GNU_IFUNC
12689 	      && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12690 		  || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12691 	    return "IFUNC";
12692 
12693 	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12694 	}
12695       else
12696 	snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12697       return buff;
12698     }
12699 }
12700 
12701 static const char *
12702 get_symbol_visibility (unsigned int visibility)
12703 {
12704   switch (visibility)
12705     {
12706     case STV_DEFAULT:	return "DEFAULT";
12707     case STV_INTERNAL:	return "INTERNAL";
12708     case STV_HIDDEN:	return "HIDDEN";
12709     case STV_PROTECTED: return "PROTECTED";
12710     default:
12711       error (_("Unrecognized visibility value: %u\n"), visibility);
12712       return _("<unknown>");
12713     }
12714 }
12715 
12716 static const char *
12717 get_alpha_symbol_other (unsigned int other)
12718 {
12719   switch (other)
12720     {
12721     case STO_ALPHA_NOPV:       return "NOPV";
12722     case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12723     default:
12724       error (_("Unrecognized alpha specific other value: %u\n"), other);
12725       return _("<unknown>");
12726     }
12727 }
12728 
12729 static const char *
12730 get_solaris_symbol_visibility (unsigned int visibility)
12731 {
12732   switch (visibility)
12733     {
12734     case 4: return "EXPORTED";
12735     case 5: return "SINGLETON";
12736     case 6: return "ELIMINATE";
12737     default: return get_symbol_visibility (visibility);
12738     }
12739 }
12740 
12741 static const char *
12742 get_aarch64_symbol_other (unsigned int other)
12743 {
12744   static char buf[32];
12745 
12746   if (other & STO_AARCH64_VARIANT_PCS)
12747     {
12748       other &= ~STO_AARCH64_VARIANT_PCS;
12749       if (other == 0)
12750 	return "VARIANT_PCS";
12751       snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12752       return buf;
12753     }
12754   return NULL;
12755 }
12756 
12757 static const char *
12758 get_mips_symbol_other (unsigned int other)
12759 {
12760   switch (other)
12761     {
12762     case STO_OPTIONAL:      return "OPTIONAL";
12763     case STO_MIPS_PLT:      return "MIPS PLT";
12764     case STO_MIPS_PIC:      return "MIPS PIC";
12765     case STO_MICROMIPS:     return "MICROMIPS";
12766     case STO_MICROMIPS | STO_MIPS_PIC:      return "MICROMIPS, MIPS PIC";
12767     case STO_MIPS16:        return "MIPS16";
12768     default:	            return NULL;
12769     }
12770 }
12771 
12772 static const char *
12773 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12774 {
12775   if (is_ia64_vms (filedata))
12776     {
12777       static char res[32];
12778 
12779       res[0] = 0;
12780 
12781       /* Function types is for images and .STB files only.  */
12782       switch (filedata->file_header.e_type)
12783         {
12784         case ET_DYN:
12785         case ET_EXEC:
12786           switch (VMS_ST_FUNC_TYPE (other))
12787             {
12788             case VMS_SFT_CODE_ADDR:
12789               strcat (res, " CA");
12790               break;
12791             case VMS_SFT_SYMV_IDX:
12792               strcat (res, " VEC");
12793               break;
12794             case VMS_SFT_FD:
12795               strcat (res, " FD");
12796               break;
12797             case VMS_SFT_RESERVE:
12798               strcat (res, " RSV");
12799               break;
12800             default:
12801 	      warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12802 		    VMS_ST_FUNC_TYPE (other));
12803 	      strcat (res, " <unknown>");
12804 	      break;
12805             }
12806           break;
12807         default:
12808           break;
12809         }
12810       switch (VMS_ST_LINKAGE (other))
12811         {
12812         case VMS_STL_IGNORE:
12813           strcat (res, " IGN");
12814           break;
12815         case VMS_STL_RESERVE:
12816           strcat (res, " RSV");
12817           break;
12818         case VMS_STL_STD:
12819           strcat (res, " STD");
12820           break;
12821         case VMS_STL_LNK:
12822           strcat (res, " LNK");
12823           break;
12824         default:
12825 	  warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12826 		VMS_ST_LINKAGE (other));
12827 	  strcat (res, " <unknown>");
12828 	  break;
12829         }
12830 
12831       if (res[0] != 0)
12832         return res + 1;
12833       else
12834         return res;
12835     }
12836   return NULL;
12837 }
12838 
12839 static const char *
12840 get_ppc64_symbol_other (unsigned int other)
12841 {
12842   if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12843     return NULL;
12844 
12845   other >>= STO_PPC64_LOCAL_BIT;
12846   if (other <= 6)
12847     {
12848       static char buf[64];
12849       if (other >= 2)
12850 	other = ppc64_decode_local_entry (other);
12851       snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12852       return buf;
12853     }
12854   return NULL;
12855 }
12856 
12857 static const char *
12858 get_riscv_symbol_other (unsigned int other)
12859 {
12860   static char buf[32];
12861   buf[0] = 0;
12862 
12863   if (other & STO_RISCV_VARIANT_CC)
12864     {
12865       strcat (buf, _(" VARIANT_CC"));
12866       other &= ~STO_RISCV_VARIANT_CC;
12867     }
12868 
12869   if (other != 0)
12870     snprintf (buf, sizeof buf, " %x", other);
12871 
12872 
12873   if (buf[0] != 0)
12874     return buf + 1;
12875   else
12876     return buf;
12877 }
12878 
12879 static const char *
12880 get_symbol_other (Filedata * filedata, unsigned int other)
12881 {
12882   const char * result = NULL;
12883   static char buff [64];
12884 
12885   if (other == 0)
12886     return "";
12887 
12888   switch (filedata->file_header.e_machine)
12889     {
12890     case EM_ALPHA:
12891       result = get_alpha_symbol_other (other);
12892       break;
12893     case EM_AARCH64:
12894       result = get_aarch64_symbol_other (other);
12895       break;
12896     case EM_MIPS:
12897       result = get_mips_symbol_other (other);
12898       break;
12899     case EM_IA_64:
12900       result = get_ia64_symbol_other (filedata, other);
12901       break;
12902     case EM_PPC64:
12903       result = get_ppc64_symbol_other (other);
12904       break;
12905     case EM_RISCV:
12906       result = get_riscv_symbol_other (other);
12907       break;
12908     default:
12909       result = NULL;
12910       break;
12911     }
12912 
12913   if (result)
12914     return result;
12915 
12916   snprintf (buff, sizeof buff, _("<other>: %x"), other);
12917   return buff;
12918 }
12919 
12920 static const char *
12921 get_symbol_index_type (Filedata * filedata, unsigned int type)
12922 {
12923   static char buff[32];
12924 
12925   switch (type)
12926     {
12927     case SHN_UNDEF:	return "UND";
12928     case SHN_ABS:	return "ABS";
12929     case SHN_COMMON:	return "COM";
12930     default:
12931       if (type == SHN_IA_64_ANSI_COMMON
12932 	  && filedata->file_header.e_machine == EM_IA_64
12933 	  && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12934 	return "ANSI_COM";
12935       else if ((filedata->file_header.e_machine == EM_X86_64
12936 		|| filedata->file_header.e_machine == EM_L1OM
12937 		|| filedata->file_header.e_machine == EM_K1OM)
12938 	       && type == SHN_X86_64_LCOMMON)
12939 	return "LARGE_COM";
12940       else if ((type == SHN_MIPS_SCOMMON
12941 		&& filedata->file_header.e_machine == EM_MIPS)
12942 	       || (type == SHN_TIC6X_SCOMMON
12943 		   && filedata->file_header.e_machine == EM_TI_C6000))
12944 	return "SCOM";
12945       else if (type == SHN_MIPS_SUNDEFINED
12946 	       && filedata->file_header.e_machine == EM_MIPS)
12947 	return "SUND";
12948       else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12949 	sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12950       else if (type >= SHN_LOOS && type <= SHN_HIOS)
12951 	sprintf (buff, "OS [0x%04x]", type & 0xffff);
12952       else if (type >= SHN_LORESERVE)
12953 	sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12954       else if (filedata->file_header.e_shnum != 0
12955 	       && type >= filedata->file_header.e_shnum)
12956 	sprintf (buff, _("bad section index[%3d]"), type);
12957       else
12958 	sprintf (buff, "%3d", type);
12959       break;
12960     }
12961 
12962   return buff;
12963 }
12964 
12965 static const char *
12966 get_symbol_version_string (Filedata *                   filedata,
12967 			   bool                         is_dynsym,
12968 			   const char *                 strtab,
12969 			   unsigned long int            strtab_size,
12970 			   unsigned int                 si,
12971 			   Elf_Internal_Sym *           psym,
12972 			   enum versioned_symbol_info * sym_info,
12973 			   unsigned short *             vna_other)
12974 {
12975   unsigned char data[2];
12976   unsigned short vers_data;
12977   unsigned long offset;
12978   unsigned short max_vd_ndx;
12979 
12980   if (!is_dynsym
12981       || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12982     return NULL;
12983 
12984   offset = offset_from_vma (filedata,
12985 			    filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12986 			    sizeof data + si * sizeof (vers_data));
12987 
12988   if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12989 		sizeof (data), 1, _("version data")) == NULL)
12990     return NULL;
12991 
12992   vers_data = byte_get (data, 2);
12993 
12994   if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12995     return NULL;
12996 
12997   *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12998   max_vd_ndx = 0;
12999 
13000   /* Usually we'd only see verdef for defined symbols, and verneed for
13001      undefined symbols.  However, symbols defined by the linker in
13002      .dynbss for variables copied from a shared library in order to
13003      avoid text relocations are defined yet have verneed.  We could
13004      use a heuristic to detect the special case, for example, check
13005      for verneed first on symbols defined in SHT_NOBITS sections, but
13006      it is simpler and more reliable to just look for both verdef and
13007      verneed.  .dynbss might not be mapped to a SHT_NOBITS section.  */
13008 
13009   if (psym->st_shndx != SHN_UNDEF
13010       && vers_data != 0x8001
13011       && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13012     {
13013       Elf_Internal_Verdef ivd;
13014       Elf_Internal_Verdaux ivda;
13015       Elf_External_Verdaux evda;
13016       unsigned long off;
13017 
13018       off = offset_from_vma (filedata,
13019 			     filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13020 			     sizeof (Elf_External_Verdef));
13021 
13022       do
13023 	{
13024 	  Elf_External_Verdef evd;
13025 
13026 	  if (get_data (&evd, filedata, off, sizeof (evd), 1,
13027 			_("version def")) == NULL)
13028 	    {
13029 	      ivd.vd_ndx = 0;
13030 	      ivd.vd_aux = 0;
13031 	      ivd.vd_next = 0;
13032 	      ivd.vd_flags = 0;
13033 	    }
13034 	  else
13035 	    {
13036 	      ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13037 	      ivd.vd_aux = BYTE_GET (evd.vd_aux);
13038 	      ivd.vd_next = BYTE_GET (evd.vd_next);
13039 	      ivd.vd_flags = BYTE_GET (evd.vd_flags);
13040 	    }
13041 
13042 	  if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13043 	    max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13044 
13045 	  off += ivd.vd_next;
13046 	}
13047       while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13048 
13049       if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13050 	{
13051 	  if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13052 	    return NULL;
13053 
13054 	  off -= ivd.vd_next;
13055 	  off += ivd.vd_aux;
13056 
13057 	  if (get_data (&evda, filedata, off, sizeof (evda), 1,
13058 			_("version def aux")) != NULL)
13059 	    {
13060 	      ivda.vda_name = BYTE_GET (evda.vda_name);
13061 
13062 	      if (psym->st_name != ivda.vda_name)
13063 		return (ivda.vda_name < strtab_size
13064 			? strtab + ivda.vda_name : _("<corrupt>"));
13065 	    }
13066 	}
13067     }
13068 
13069   if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13070     {
13071       Elf_External_Verneed evn;
13072       Elf_Internal_Verneed ivn;
13073       Elf_Internal_Vernaux ivna;
13074 
13075       offset = offset_from_vma (filedata,
13076 				filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13077 				sizeof evn);
13078       do
13079 	{
13080 	  unsigned long vna_off;
13081 
13082 	  if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13083 			_("version need")) == NULL)
13084 	    {
13085 	      ivna.vna_next = 0;
13086 	      ivna.vna_other = 0;
13087 	      ivna.vna_name = 0;
13088 	      break;
13089 	    }
13090 
13091 	  ivn.vn_aux  = BYTE_GET (evn.vn_aux);
13092 	  ivn.vn_next = BYTE_GET (evn.vn_next);
13093 
13094 	  vna_off = offset + ivn.vn_aux;
13095 
13096 	  do
13097 	    {
13098 	      Elf_External_Vernaux evna;
13099 
13100 	      if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13101 			    _("version need aux (3)")) == NULL)
13102 		{
13103 		  ivna.vna_next = 0;
13104 		  ivna.vna_other = 0;
13105 		  ivna.vna_name = 0;
13106 		}
13107 	      else
13108 		{
13109 		  ivna.vna_other = BYTE_GET (evna.vna_other);
13110 		  ivna.vna_next  = BYTE_GET (evna.vna_next);
13111 		  ivna.vna_name  = BYTE_GET (evna.vna_name);
13112 		}
13113 
13114 	      vna_off += ivna.vna_next;
13115 	    }
13116 	  while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13117 
13118 	  if (ivna.vna_other == vers_data)
13119 	    break;
13120 
13121 	  offset += ivn.vn_next;
13122 	}
13123       while (ivn.vn_next != 0);
13124 
13125       if (ivna.vna_other == vers_data)
13126 	{
13127 	  *sym_info = symbol_undefined;
13128 	  *vna_other = ivna.vna_other;
13129 	  return (ivna.vna_name < strtab_size
13130 		  ? strtab + ivna.vna_name : _("<corrupt>"));
13131 	}
13132       else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13133 	       && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13134 	return _("<corrupt>");
13135     }
13136   return NULL;
13137 }
13138 
13139 /* Display a symbol size on stdout.  Format is based on --sym-base setting.  */
13140 
13141 static unsigned int
13142 print_dynamic_symbol_size (bfd_vma vma, int base)
13143 {
13144   switch (base)
13145     {
13146     case 8:
13147       return print_vma (vma, OCTAL_5);
13148 
13149     case 10:
13150       return print_vma (vma, UNSIGNED_5);
13151 
13152     case 16:
13153       return print_vma (vma, PREFIX_HEX_5);
13154 
13155     case 0:
13156     default:
13157       return print_vma (vma, DEC_5);
13158     }
13159 }
13160 
13161 static void
13162 print_dynamic_symbol (Filedata *filedata, unsigned long si,
13163 		      Elf_Internal_Sym *symtab,
13164 		      Elf_Internal_Shdr *section,
13165 		      char *strtab, size_t strtab_size)
13166 {
13167   const char *version_string;
13168   enum versioned_symbol_info sym_info;
13169   unsigned short vna_other;
13170   bool is_valid;
13171   const char * sstr;
13172   Elf_Internal_Sym *psym = symtab + si;
13173 
13174   printf ("%6ld: ", si);
13175   print_vma (psym->st_value, LONG_HEX);
13176   putchar (' ');
13177   print_dynamic_symbol_size (psym->st_size, sym_base);
13178   printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13179   printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13180   if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13181     printf (" %-7s",  get_solaris_symbol_visibility (psym->st_other));
13182   else
13183     {
13184       unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13185 
13186       printf (" %-7s", get_symbol_visibility (vis));
13187       /* Check to see if any other bits in the st_other field are set.
13188 	 Note - displaying this information disrupts the layout of the
13189 	 table being generated, but for the moment this case is very rare.  */
13190       if (psym->st_other ^ vis)
13191 	printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13192     }
13193   printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
13194 
13195   if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13196       && psym->st_shndx < filedata->file_header.e_shnum
13197       && filedata->section_headers != NULL
13198       && psym->st_name == 0)
13199     {
13200       is_valid
13201 	= section_name_valid (filedata,
13202 			      filedata->section_headers + psym->st_shndx);
13203       sstr = is_valid ?
13204 	section_name_print (filedata,
13205 			    filedata->section_headers + psym->st_shndx)
13206 	: _("<corrupt>");
13207     }
13208   else
13209     {
13210       is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13211       sstr = is_valid  ? strtab + psym->st_name : _("<corrupt>");
13212     }
13213 
13214   version_string
13215     = get_symbol_version_string (filedata,
13216 				 (section == NULL
13217 				  || section->sh_type == SHT_DYNSYM),
13218 				 strtab, strtab_size, si,
13219 				 psym, &sym_info, &vna_other);
13220 
13221   int len_avail = 21;
13222   if (! do_wide && version_string != NULL)
13223     {
13224       char buffer[16];
13225 
13226       len_avail -= 1 + strlen (version_string);
13227 
13228       if (sym_info == symbol_undefined)
13229 	len_avail -= sprintf (buffer," (%d)", vna_other);
13230       else if (sym_info != symbol_hidden)
13231 	len_avail -= 1;
13232     }
13233 
13234   print_symbol (len_avail, sstr);
13235 
13236   if (version_string)
13237     {
13238       if (sym_info == symbol_undefined)
13239 	printf ("@%s (%d)", version_string, vna_other);
13240       else
13241 	printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13242 		version_string);
13243     }
13244 
13245   putchar ('\n');
13246 
13247   if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13248       && section != NULL
13249       && si >= section->sh_info
13250       /* Irix 5 and 6 MIPS binaries are known to ignore this requirement.  */
13251       && filedata->file_header.e_machine != EM_MIPS
13252       /* Solaris binaries have been found to violate this requirement as
13253 	 well.  Not sure if this is a bug or an ABI requirement.  */
13254       && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13255     warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
13256 	  si, printable_section_name (filedata, section), section->sh_info);
13257 }
13258 
13259 static const char *
13260 get_lto_kind (unsigned int kind)
13261 {
13262   switch (kind)
13263     {
13264     case 0: return "DEF";
13265     case 1: return "WEAKDEF";
13266     case 2: return "UNDEF";
13267     case 3: return "WEAKUNDEF";
13268     case 4: return "COMMON";
13269     default:
13270       break;
13271     }
13272 
13273   static char buffer[30];
13274   error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13275   sprintf (buffer, "<unknown: %u>", kind);
13276   return buffer;
13277 }
13278 
13279 static const char *
13280 get_lto_visibility (unsigned int visibility)
13281 {
13282   switch (visibility)
13283     {
13284     case 0: return "DEFAULT";
13285     case 1: return "PROTECTED";
13286     case 2: return "INTERNAL";
13287     case 3: return "HIDDEN";
13288     default:
13289       break;
13290     }
13291 
13292   static char buffer[30];
13293   error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13294   sprintf (buffer, "<unknown: %u>", visibility);
13295   return buffer;
13296 }
13297 
13298 static const char *
13299 get_lto_sym_type (unsigned int sym_type)
13300 {
13301   switch (sym_type)
13302     {
13303     case 0: return "UNKNOWN";
13304     case 1: return "FUNCTION";
13305     case 2: return "VARIABLE";
13306     default:
13307       break;
13308     }
13309 
13310   static char buffer[30];
13311   error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13312   sprintf (buffer, "<unknown: %u>", sym_type);
13313   return buffer;
13314 }
13315 
13316 /* Display an LTO format symbol table.
13317    FIXME: The format of LTO symbol tables is not formalized.
13318    So this code could need changing in the future.  */
13319 
13320 static bool
13321 display_lto_symtab (Filedata *           filedata,
13322 		    Elf_Internal_Shdr *  section)
13323 {
13324   if (section->sh_size == 0)
13325     {
13326       if (filedata->is_separate)
13327 	printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13328 		printable_section_name (filedata, section),
13329 		filedata->file_name);
13330       else
13331 	printf (_("\nLTO Symbol table '%s' is empty!\n"),
13332 		printable_section_name (filedata, section));
13333 
13334       return true;
13335     }
13336 
13337   if (section->sh_size > filedata->file_size)
13338     {
13339       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
13340 	     printable_section_name (filedata, section),
13341 	     (unsigned long) section->sh_size);
13342       return false;
13343     }
13344 
13345   void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13346 				  section->sh_size, 1, _("LTO symbols"));
13347   if (alloced_data == NULL)
13348     return false;
13349 
13350   /* Look for extended data for the symbol table.  */
13351   Elf_Internal_Shdr * ext;
13352   void * ext_data_orig = NULL;
13353   char * ext_data = NULL;
13354   char * ext_data_end = NULL;
13355   char * ext_name = NULL;
13356 
13357   if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13358 		(section_name (filedata, section)
13359 		 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13360       && ext_name != NULL /* Paranoia.  */
13361       && (ext = find_section (filedata, ext_name)) != NULL)
13362     {
13363       if (ext->sh_size < 3)
13364 	error (_("LTO Symbol extension table '%s' is empty!\n"),
13365 	       printable_section_name (filedata, ext));
13366       else
13367 	{
13368 	  ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13369 					       ext->sh_size, 1,
13370 					       _("LTO ext symbol data"));
13371 	  if (ext_data != NULL)
13372 	    {
13373 	      ext_data_end = ext_data + ext->sh_size;
13374 	      if (* ext_data++ != 1)
13375 		error (_("Unexpected version number in symbol extension table\n"));
13376 	    }
13377 	}
13378     }
13379 
13380   const unsigned char * data = (const unsigned char *) alloced_data;
13381   const unsigned char * end = data + section->sh_size;
13382 
13383   if (filedata->is_separate)
13384     printf (_("\nIn linked file '%s': "), filedata->file_name);
13385   else
13386     printf ("\n");
13387 
13388   if (ext_data_orig != NULL)
13389     {
13390       if (do_wide)
13391 	printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13392 		printable_section_name (filedata, section),
13393 		printable_section_name (filedata, ext));
13394       else
13395 	{
13396 	  printf (_("LTO Symbol table '%s'\n"),
13397 		  printable_section_name (filedata, section));
13398 	  printf (_(" and extension table '%s' contain:\n"),
13399 		  printable_section_name (filedata, ext));
13400 	}
13401     }
13402   else
13403     printf (_("LTO Symbol table '%s' contains:\n"),
13404 	    printable_section_name (filedata, section));
13405 
13406   /* FIXME: Add a wide version.  */
13407   if (ext_data_orig != NULL)
13408     printf (_("  Comdat_Key       Kind  Visibility     Size      Slot      Type  Section Name\n"));
13409   else
13410     printf (_("  Comdat_Key       Kind  Visibility     Size      Slot Name\n"));
13411 
13412   /* FIXME: We do not handle style prefixes.  */
13413 
13414   while (data < end)
13415     {
13416       const unsigned char * sym_name = data;
13417       data += strnlen ((const char *) sym_name, end - data) + 1;
13418       if (data >= end)
13419 	goto fail;
13420 
13421       const unsigned char * comdat_key = data;
13422       data += strnlen ((const char *) comdat_key, end - data) + 1;
13423       if (data >= end)
13424 	goto fail;
13425 
13426       if (data + 2 + 8 + 4 > end)
13427 	goto fail;
13428 
13429       unsigned int kind = *data++;
13430       unsigned int visibility = *data++;
13431 
13432       elf_vma size = byte_get (data, 8);
13433       data += 8;
13434 
13435       elf_vma slot = byte_get (data, 4);
13436       data += 4;
13437 
13438       if (ext_data != NULL)
13439 	{
13440 	  if (ext_data < (ext_data_end - 1))
13441 	    {
13442 	      unsigned int sym_type = * ext_data ++;
13443 	      unsigned int sec_kind = * ext_data ++;
13444 
13445 	      printf ("  %10s %10s %11s %08lx  %08lx %9s %08lx _",
13446 		      * comdat_key == 0 ? "-" : (char *) comdat_key,
13447 		      get_lto_kind (kind),
13448 		      get_lto_visibility (visibility),
13449 		      (long) size,
13450 		      (long) slot,
13451 		      get_lto_sym_type (sym_type),
13452 		      (long) sec_kind);
13453 	      print_symbol (6, (const char *) sym_name);
13454 	    }
13455 	  else
13456 	    {
13457 	      error (_("Ran out of LTO symbol extension data\n"));
13458 	      ext_data = NULL;
13459 	      /* FIXME: return FAIL result ?  */
13460 	    }
13461 	}
13462       else
13463 	{
13464 	  printf ("  %10s %10s %11s %08lx  %08lx _",
13465 		  * comdat_key == 0 ? "-" : (char *) comdat_key,
13466 		  get_lto_kind (kind),
13467 		  get_lto_visibility (visibility),
13468 		  (long) size,
13469 		  (long) slot);
13470 	  print_symbol (21, (const char *) sym_name);
13471 	}
13472       putchar ('\n');
13473     }
13474 
13475   if (ext_data != NULL && ext_data < ext_data_end)
13476     {
13477       error (_("Data remains in the LTO symbol extension table\n"));
13478       goto fail;
13479     }
13480 
13481   free (alloced_data);
13482   free (ext_data_orig);
13483   free (ext_name);
13484   return true;
13485 
13486  fail:
13487   error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13488   free (alloced_data);
13489   free (ext_data_orig);
13490   free (ext_name);
13491   return false;
13492 }
13493 
13494 /* Display LTO symbol tables.  */
13495 
13496 static bool
13497 process_lto_symbol_tables (Filedata * filedata)
13498 {
13499   Elf_Internal_Shdr * section;
13500   unsigned int i;
13501   bool res = true;
13502 
13503   if (!do_lto_syms)
13504     return true;
13505 
13506   if (filedata->section_headers == NULL)
13507     return true;
13508 
13509   for (i = 0, section = filedata->section_headers;
13510        i < filedata->file_header.e_shnum;
13511        i++, section++)
13512     if (section_name_valid (filedata, section)
13513 	&& startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13514       res &= display_lto_symtab (filedata, section);
13515 
13516   return res;
13517 }
13518 
13519 /* Dump the symbol table.  */
13520 
13521 static bool
13522 process_symbol_table (Filedata * filedata)
13523 {
13524   Elf_Internal_Shdr * section;
13525 
13526   if (!do_syms && !do_dyn_syms && !do_histogram)
13527     return true;
13528 
13529   if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13530       && do_syms
13531       && do_using_dynamic
13532       && filedata->dynamic_strings != NULL
13533       && filedata->dynamic_symbols != NULL)
13534     {
13535       unsigned long si;
13536 
13537       if (filedata->is_separate)
13538 	{
13539 	  printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13540 			    "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13541 			    filedata->num_dynamic_syms),
13542 		  filedata->file_name,
13543 		  filedata->num_dynamic_syms);
13544 	}
13545       else
13546 	{
13547 	  printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13548 			    "\nSymbol table for image contains %lu entries:\n",
13549 			    filedata->num_dynamic_syms),
13550 		  filedata->num_dynamic_syms);
13551 	}
13552       if (is_32bit_elf)
13553 	printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
13554       else
13555 	printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
13556 
13557       for (si = 0; si < filedata->num_dynamic_syms; si++)
13558 	print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13559 			      filedata->dynamic_strings,
13560 			      filedata->dynamic_strings_length);
13561     }
13562   else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13563 	   && filedata->section_headers != NULL)
13564     {
13565       unsigned int i;
13566 
13567       for (i = 0, section = filedata->section_headers;
13568 	   i < filedata->file_header.e_shnum;
13569 	   i++, section++)
13570 	{
13571 	  char * strtab = NULL;
13572 	  unsigned long int strtab_size = 0;
13573 	  Elf_Internal_Sym * symtab;
13574 	  unsigned long si, num_syms;
13575 
13576 	  if ((section->sh_type != SHT_SYMTAB
13577 	       && section->sh_type != SHT_DYNSYM)
13578 	      || (!do_syms
13579 		  && section->sh_type == SHT_SYMTAB))
13580 	    continue;
13581 
13582 	  if (section->sh_entsize == 0)
13583 	    {
13584 	      printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13585 		      printable_section_name (filedata, section));
13586 	      continue;
13587 	    }
13588 
13589 	  num_syms = section->sh_size / section->sh_entsize;
13590 
13591 	  if (filedata->is_separate)
13592 	    printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13593 			      "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13594 			      num_syms),
13595 		    filedata->file_name,
13596 		    printable_section_name (filedata, section),
13597 		    num_syms);
13598 	  else
13599 	    printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13600 			      "\nSymbol table '%s' contains %lu entries:\n",
13601 			      num_syms),
13602 		    printable_section_name (filedata, section),
13603 		    num_syms);
13604 
13605 	  if (is_32bit_elf)
13606 	    printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
13607 	  else
13608 	    printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
13609 
13610 	  symtab = get_elf_symbols (filedata, section, & num_syms);
13611 	  if (symtab == NULL)
13612 	    continue;
13613 
13614 	  if (section->sh_link == filedata->file_header.e_shstrndx)
13615 	    {
13616 	      strtab = filedata->string_table;
13617 	      strtab_size = filedata->string_table_length;
13618 	    }
13619 	  else if (section->sh_link < filedata->file_header.e_shnum)
13620 	    {
13621 	      Elf_Internal_Shdr * string_sec;
13622 
13623 	      string_sec = filedata->section_headers + section->sh_link;
13624 
13625 	      strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13626                                           1, string_sec->sh_size,
13627                                           _("string table"));
13628 	      strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13629 	    }
13630 
13631 	  for (si = 0; si < num_syms; si++)
13632 	    print_dynamic_symbol (filedata, si, symtab, section,
13633 				  strtab, strtab_size);
13634 
13635 	  free (symtab);
13636 	  if (strtab != filedata->string_table)
13637 	    free (strtab);
13638 	}
13639     }
13640   else if (do_syms)
13641     printf
13642       (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13643 
13644   if (do_histogram && filedata->buckets != NULL)
13645     {
13646       unsigned long * lengths;
13647       unsigned long * counts;
13648       unsigned long hn;
13649       bfd_vma si;
13650       unsigned long maxlength = 0;
13651       unsigned long nzero_counts = 0;
13652       unsigned long nsyms = 0;
13653       char *visited;
13654 
13655       printf (ngettext ("\nHistogram for bucket list length "
13656 			"(total of %lu bucket):\n",
13657 			"\nHistogram for bucket list length "
13658 			"(total of %lu buckets):\n",
13659 			(unsigned long) filedata->nbuckets),
13660 	      (unsigned long) filedata->nbuckets);
13661 
13662       lengths = (unsigned long *) calloc (filedata->nbuckets,
13663 					  sizeof (*lengths));
13664       if (lengths == NULL)
13665 	{
13666 	  error (_("Out of memory allocating space for histogram buckets\n"));
13667 	  goto err_out;
13668 	}
13669       visited = xcmalloc (filedata->nchains, 1);
13670       memset (visited, 0, filedata->nchains);
13671 
13672       printf (_(" Length  Number     %% of total  Coverage\n"));
13673       for (hn = 0; hn < filedata->nbuckets; ++hn)
13674 	{
13675 	  for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13676 	    {
13677 	      ++nsyms;
13678 	      if (maxlength < ++lengths[hn])
13679 		++maxlength;
13680 	      if (si >= filedata->nchains || visited[si])
13681 		{
13682 		  error (_("histogram chain is corrupt\n"));
13683 		  break;
13684 		}
13685 	      visited[si] = 1;
13686 	    }
13687 	}
13688       free (visited);
13689 
13690       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13691       if (counts == NULL)
13692 	{
13693 	  free (lengths);
13694 	  error (_("Out of memory allocating space for histogram counts\n"));
13695 	  goto err_out;
13696 	}
13697 
13698       for (hn = 0; hn < filedata->nbuckets; ++hn)
13699 	++counts[lengths[hn]];
13700 
13701       if (filedata->nbuckets > 0)
13702 	{
13703 	  unsigned long i;
13704 	  printf ("      0  %-10lu (%5.1f%%)\n",
13705 		  counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13706 	  for (i = 1; i <= maxlength; ++i)
13707 	    {
13708 	      nzero_counts += counts[i] * i;
13709 	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
13710 		      i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13711 		      (nzero_counts * 100.0) / nsyms);
13712 	    }
13713 	}
13714 
13715       free (counts);
13716       free (lengths);
13717     }
13718 
13719   free (filedata->buckets);
13720   filedata->buckets = NULL;
13721   filedata->nbuckets = 0;
13722   free (filedata->chains);
13723   filedata->chains = NULL;
13724 
13725   if (do_histogram && filedata->gnubuckets != NULL)
13726     {
13727       unsigned long * lengths;
13728       unsigned long * counts;
13729       unsigned long hn;
13730       unsigned long maxlength = 0;
13731       unsigned long nzero_counts = 0;
13732       unsigned long nsyms = 0;
13733 
13734       printf (ngettext ("\nHistogram for `%s' bucket list length "
13735 			"(total of %lu bucket):\n",
13736 			"\nHistogram for `%s' bucket list length "
13737 			"(total of %lu buckets):\n",
13738 			(unsigned long) filedata->ngnubuckets),
13739 	      GNU_HASH_SECTION_NAME (filedata),
13740 	      (unsigned long) filedata->ngnubuckets);
13741 
13742       lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13743 					  sizeof (*lengths));
13744       if (lengths == NULL)
13745 	{
13746 	  error (_("Out of memory allocating space for gnu histogram buckets\n"));
13747 	  goto err_out;
13748 	}
13749 
13750       printf (_(" Length  Number     %% of total  Coverage\n"));
13751 
13752       for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13753 	if (filedata->gnubuckets[hn] != 0)
13754 	  {
13755 	    bfd_vma off, length = 1;
13756 
13757 	    for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13758 		 /* PR 17531 file: 010-77222-0.004.  */
13759 		 off < filedata->ngnuchains
13760 		   && (filedata->gnuchains[off] & 1) == 0;
13761 		 ++off)
13762 	      ++length;
13763 	    lengths[hn] = length;
13764 	    if (length > maxlength)
13765 	      maxlength = length;
13766 	    nsyms += length;
13767 	  }
13768 
13769       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13770       if (counts == NULL)
13771 	{
13772 	  free (lengths);
13773 	  error (_("Out of memory allocating space for gnu histogram counts\n"));
13774 	  goto err_out;
13775 	}
13776 
13777       for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13778 	++counts[lengths[hn]];
13779 
13780       if (filedata->ngnubuckets > 0)
13781 	{
13782 	  unsigned long j;
13783 	  printf ("      0  %-10lu (%5.1f%%)\n",
13784 		  counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13785 	  for (j = 1; j <= maxlength; ++j)
13786 	    {
13787 	      nzero_counts += counts[j] * j;
13788 	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
13789 		      j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13790 		      (nzero_counts * 100.0) / nsyms);
13791 	    }
13792 	}
13793 
13794       free (counts);
13795       free (lengths);
13796     }
13797   free (filedata->gnubuckets);
13798   filedata->gnubuckets = NULL;
13799   filedata->ngnubuckets = 0;
13800   free (filedata->gnuchains);
13801   filedata->gnuchains = NULL;
13802   filedata->ngnuchains = 0;
13803   free (filedata->mipsxlat);
13804   filedata->mipsxlat = NULL;
13805   return true;
13806 
13807  err_out:
13808   free (filedata->gnubuckets);
13809   filedata->gnubuckets = NULL;
13810   filedata->ngnubuckets = 0;
13811   free (filedata->gnuchains);
13812   filedata->gnuchains = NULL;
13813   filedata->ngnuchains = 0;
13814   free (filedata->mipsxlat);
13815   filedata->mipsxlat = NULL;
13816   free (filedata->buckets);
13817   filedata->buckets = NULL;
13818   filedata->nbuckets = 0;
13819   free (filedata->chains);
13820   filedata->chains = NULL;
13821   return false;
13822 }
13823 
13824 static bool
13825 process_syminfo (Filedata * filedata)
13826 {
13827   unsigned int i;
13828 
13829   if (filedata->dynamic_syminfo == NULL
13830       || !do_dynamic)
13831     /* No syminfo, this is ok.  */
13832     return true;
13833 
13834   /* There better should be a dynamic symbol section.  */
13835   if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13836     return false;
13837 
13838   if (filedata->is_separate)
13839     printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13840 		      "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13841 		      filedata->dynamic_syminfo_nent),
13842 	    filedata->file_name,
13843 	    filedata->dynamic_syminfo_offset,
13844 	    filedata->dynamic_syminfo_nent);
13845   else
13846     printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13847 		      "contains %d entry:\n",
13848 		      "\nDynamic info segment at offset 0x%lx "
13849 		      "contains %d entries:\n",
13850 		      filedata->dynamic_syminfo_nent),
13851 	    filedata->dynamic_syminfo_offset,
13852 	    filedata->dynamic_syminfo_nent);
13853 
13854   printf (_(" Num: Name                           BoundTo     Flags\n"));
13855   for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13856     {
13857       unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13858 
13859       printf ("%4d: ", i);
13860       if (i >= filedata->num_dynamic_syms)
13861 	printf (_("<corrupt index>"));
13862       else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13863 	print_symbol (30, get_dynamic_name (filedata,
13864 					    filedata->dynamic_symbols[i].st_name));
13865       else
13866 	printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13867       putchar (' ');
13868 
13869       switch (filedata->dynamic_syminfo[i].si_boundto)
13870 	{
13871 	case SYMINFO_BT_SELF:
13872 	  fputs ("SELF       ", stdout);
13873 	  break;
13874 	case SYMINFO_BT_PARENT:
13875 	  fputs ("PARENT     ", stdout);
13876 	  break;
13877 	default:
13878 	  if (filedata->dynamic_syminfo[i].si_boundto > 0
13879 	      && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13880 	      && valid_dynamic_name (filedata,
13881 				     filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13882 	    {
13883 	      print_symbol (10, get_dynamic_name (filedata,
13884 						  filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13885 	      putchar (' ' );
13886 	    }
13887 	  else
13888 	    printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13889 	  break;
13890 	}
13891 
13892       if (flags & SYMINFO_FLG_DIRECT)
13893 	printf (" DIRECT");
13894       if (flags & SYMINFO_FLG_PASSTHRU)
13895 	printf (" PASSTHRU");
13896       if (flags & SYMINFO_FLG_COPY)
13897 	printf (" COPY");
13898       if (flags & SYMINFO_FLG_LAZYLOAD)
13899 	printf (" LAZYLOAD");
13900 
13901       puts ("");
13902     }
13903 
13904   return true;
13905 }
13906 
13907 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13908    is contained by the region START .. END.  The types of ADDR, START
13909    and END should all be the same.  Note both ADDR + NELEM and END
13910    point to just beyond the end of the regions that are being tested.  */
13911 #define IN_RANGE(START,END,ADDR,NELEM)		\
13912   (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13913 
13914 /* Check to see if the given reloc needs to be handled in a target specific
13915    manner.  If so then process the reloc and return TRUE otherwise return
13916    FALSE.
13917 
13918    If called with reloc == NULL, then this is a signal that reloc processing
13919    for the current section has finished, and any saved state should be
13920    discarded.  */
13921 
13922 static bool
13923 target_specific_reloc_handling (Filedata *           filedata,
13924 				Elf_Internal_Rela *  reloc,
13925 				unsigned char *      start,
13926 				unsigned char *      end,
13927 				Elf_Internal_Sym *   symtab,
13928 				unsigned long        num_syms)
13929 {
13930   unsigned int reloc_type = 0;
13931   unsigned long sym_index = 0;
13932 
13933   if (reloc)
13934     {
13935       reloc_type = get_reloc_type (filedata, reloc->r_info);
13936       sym_index = get_reloc_symindex (reloc->r_info);
13937     }
13938 
13939   switch (filedata->file_header.e_machine)
13940     {
13941     case EM_MSP430:
13942     case EM_MSP430_OLD:
13943       {
13944 	static Elf_Internal_Sym * saved_sym = NULL;
13945 
13946 	if (reloc == NULL)
13947 	  {
13948 	    saved_sym = NULL;
13949 	    return true;
13950 	  }
13951 
13952 	switch (reloc_type)
13953 	  {
13954 	  case 10: /* R_MSP430_SYM_DIFF */
13955 	  case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13956 	    if (uses_msp430x_relocs (filedata))
13957 	      break;
13958 	    /* Fall through.  */
13959 	  case 21: /* R_MSP430X_SYM_DIFF */
13960 	  case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13961 	    /* PR 21139.  */
13962 	    if (sym_index >= num_syms)
13963 	      error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13964 		     sym_index);
13965 	    else
13966 	      saved_sym = symtab + sym_index;
13967 	    return true;
13968 
13969 	  case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13970 	  case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13971 	    goto handle_sym_diff;
13972 
13973 	  case 5: /* R_MSP430_16_BYTE */
13974 	  case 9: /* R_MSP430_8 */
13975 	  case 11: /* R_MSP430_GNU_SET_ULEB128 */
13976 	    if (uses_msp430x_relocs (filedata))
13977 	      break;
13978 	    goto handle_sym_diff;
13979 
13980 	  case 2: /* R_MSP430_ABS16 */
13981 	  case 15: /* R_MSP430X_ABS16 */
13982 	  case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13983 	    if (! uses_msp430x_relocs (filedata))
13984 	      break;
13985 	    goto handle_sym_diff;
13986 
13987 	  handle_sym_diff:
13988 	    if (saved_sym != NULL)
13989 	      {
13990 		bfd_vma value;
13991 		unsigned int reloc_size = 0;
13992 		int leb_ret = 0;
13993 		switch (reloc_type)
13994 		  {
13995 		  case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13996 		    reloc_size = 4;
13997 		    break;
13998 		  case 11: /* R_MSP430_GNU_SET_ULEB128 */
13999 		  case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14000 		    if (reloc->r_offset < (size_t) (end - start))
14001 		      read_leb128 (start + reloc->r_offset, end, false,
14002 				   &reloc_size, &leb_ret);
14003 		    break;
14004 		  default:
14005 		    reloc_size = 2;
14006 		    break;
14007 		  }
14008 
14009 		if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14010 		  error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
14011 			   "ULEB128 value\n"),
14012 			 (long) reloc->r_offset);
14013 		else if (sym_index >= num_syms)
14014 		  error (_("MSP430 reloc contains invalid symbol index %lu\n"),
14015 			 sym_index);
14016 		else
14017 		  {
14018 		    value = reloc->r_addend + (symtab[sym_index].st_value
14019 					       - saved_sym->st_value);
14020 
14021 		    if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14022 		      byte_put (start + reloc->r_offset, value, reloc_size);
14023 		    else
14024 		      /* PR 21137 */
14025 		      error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
14026 			     (long) reloc->r_offset);
14027 		  }
14028 
14029 		saved_sym = NULL;
14030 		return true;
14031 	      }
14032 	    break;
14033 
14034 	  default:
14035 	    if (saved_sym != NULL)
14036 	      error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14037 	    break;
14038 	  }
14039 	break;
14040       }
14041 
14042     case EM_MN10300:
14043     case EM_CYGNUS_MN10300:
14044       {
14045 	static Elf_Internal_Sym * saved_sym = NULL;
14046 
14047 	if (reloc == NULL)
14048 	  {
14049 	    saved_sym = NULL;
14050 	    return true;
14051 	  }
14052 
14053 	switch (reloc_type)
14054 	  {
14055 	  case 34: /* R_MN10300_ALIGN */
14056 	    return true;
14057 	  case 33: /* R_MN10300_SYM_DIFF */
14058 	    if (sym_index >= num_syms)
14059 	      error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
14060 		     sym_index);
14061 	    else
14062 	      saved_sym = symtab + sym_index;
14063 	    return true;
14064 
14065 	  case 1: /* R_MN10300_32 */
14066 	  case 2: /* R_MN10300_16 */
14067 	    if (saved_sym != NULL)
14068 	      {
14069 		int reloc_size = reloc_type == 1 ? 4 : 2;
14070 		bfd_vma value;
14071 
14072 		if (sym_index >= num_syms)
14073 		  error (_("MN10300 reloc contains invalid symbol index %lu\n"),
14074 			 sym_index);
14075 		else
14076 		  {
14077 		    value = reloc->r_addend + (symtab[sym_index].st_value
14078 					       - saved_sym->st_value);
14079 
14080 		    if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14081 		      byte_put (start + reloc->r_offset, value, reloc_size);
14082 		    else
14083 		      error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
14084 			     (long) reloc->r_offset);
14085 		  }
14086 
14087 		saved_sym = NULL;
14088 		return true;
14089 	      }
14090 	    break;
14091 	  default:
14092 	    if (saved_sym != NULL)
14093 	      error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
14094 	    break;
14095 	  }
14096 	break;
14097       }
14098 
14099     case EM_RL78:
14100       {
14101 	static bfd_vma saved_sym1 = 0;
14102 	static bfd_vma saved_sym2 = 0;
14103 	static bfd_vma value;
14104 
14105 	if (reloc == NULL)
14106 	  {
14107 	    saved_sym1 = saved_sym2 = 0;
14108 	    return true;
14109 	  }
14110 
14111 	switch (reloc_type)
14112 	  {
14113 	  case 0x80: /* R_RL78_SYM.  */
14114 	    saved_sym1 = saved_sym2;
14115 	    if (sym_index >= num_syms)
14116 	      error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
14117 		     sym_index);
14118 	    else
14119 	      {
14120 		saved_sym2 = symtab[sym_index].st_value;
14121 		saved_sym2 += reloc->r_addend;
14122 	      }
14123 	    return true;
14124 
14125 	  case 0x83: /* R_RL78_OPsub.  */
14126 	    value = saved_sym1 - saved_sym2;
14127 	    saved_sym2 = saved_sym1 = 0;
14128 	    return true;
14129 	    break;
14130 
14131 	  case 0x41: /* R_RL78_ABS32.  */
14132 	    if (IN_RANGE (start, end, start + reloc->r_offset, 4))
14133 	      byte_put (start + reloc->r_offset, value, 4);
14134 	    else
14135 	      error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14136 		     (long) reloc->r_offset);
14137 	    value = 0;
14138 	    return true;
14139 
14140 	  case 0x43: /* R_RL78_ABS16.  */
14141 	    if (IN_RANGE (start, end, start + reloc->r_offset, 2))
14142 	      byte_put (start + reloc->r_offset, value, 2);
14143 	    else
14144 	      error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14145 		     (long) reloc->r_offset);
14146 	    value = 0;
14147 	    return true;
14148 
14149 	  default:
14150 	    break;
14151 	  }
14152 	break;
14153       }
14154     }
14155 
14156   return false;
14157 }
14158 
14159 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14160    DWARF debug sections.  This is a target specific test.  Note - we do not
14161    go through the whole including-target-headers-multiple-times route, (as
14162    we have already done with <elf/h8.h>) because this would become very
14163    messy and even then this function would have to contain target specific
14164    information (the names of the relocs instead of their numeric values).
14165    FIXME: This is not the correct way to solve this problem.  The proper way
14166    is to have target specific reloc sizing and typing functions created by
14167    the reloc-macros.h header, in the same way that it already creates the
14168    reloc naming functions.  */
14169 
14170 static bool
14171 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14172 {
14173   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14174   switch (filedata->file_header.e_machine)
14175     {
14176     case EM_386:
14177     case EM_IAMCU:
14178       return reloc_type == 1; /* R_386_32.  */
14179     case EM_68K:
14180       return reloc_type == 1; /* R_68K_32.  */
14181     case EM_860:
14182       return reloc_type == 1; /* R_860_32.  */
14183     case EM_960:
14184       return reloc_type == 2; /* R_960_32.  */
14185     case EM_AARCH64:
14186       return (reloc_type == 258
14187 	      || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
14188     case EM_BPF:
14189       return reloc_type == 11; /* R_BPF_DATA_32 */
14190     case EM_ADAPTEVA_EPIPHANY:
14191       return reloc_type == 3;
14192     case EM_ALPHA:
14193       return reloc_type == 1; /* R_ALPHA_REFLONG.  */
14194     case EM_ARC:
14195       return reloc_type == 1; /* R_ARC_32.  */
14196     case EM_ARC_COMPACT:
14197     case EM_ARC_COMPACT2:
14198       return reloc_type == 4; /* R_ARC_32.  */
14199     case EM_ARM:
14200       return reloc_type == 2; /* R_ARM_ABS32 */
14201     case EM_AVR_OLD:
14202     case EM_AVR:
14203       return reloc_type == 1;
14204     case EM_BLACKFIN:
14205       return reloc_type == 0x12; /* R_byte4_data.  */
14206     case EM_CRIS:
14207       return reloc_type == 3; /* R_CRIS_32.  */
14208     case EM_CR16:
14209       return reloc_type == 3; /* R_CR16_NUM32.  */
14210     case EM_CRX:
14211       return reloc_type == 15; /* R_CRX_NUM32.  */
14212     case EM_CSKY:
14213       return reloc_type == 1; /* R_CKCORE_ADDR32.  */
14214     case EM_CYGNUS_FRV:
14215       return reloc_type == 1;
14216     case EM_CYGNUS_D10V:
14217     case EM_D10V:
14218       return reloc_type == 6; /* R_D10V_32.  */
14219     case EM_CYGNUS_D30V:
14220     case EM_D30V:
14221       return reloc_type == 12; /* R_D30V_32_NORMAL.  */
14222     case EM_DLX:
14223       return reloc_type == 3; /* R_DLX_RELOC_32.  */
14224     case EM_CYGNUS_FR30:
14225     case EM_FR30:
14226       return reloc_type == 3; /* R_FR30_32.  */
14227     case EM_FT32:
14228       return reloc_type == 1; /* R_FT32_32.  */
14229     case EM_H8S:
14230     case EM_H8_300:
14231     case EM_H8_300H:
14232       return reloc_type == 1; /* R_H8_DIR32.  */
14233     case EM_IA_64:
14234       return (reloc_type == 0x64    /* R_IA64_SECREL32MSB.  */
14235 	      || reloc_type == 0x65 /* R_IA64_SECREL32LSB.  */
14236 	      || reloc_type == 0x24 /* R_IA64_DIR32MSB.  */
14237 	      || reloc_type == 0x25 /* R_IA64_DIR32LSB.  */);
14238     case EM_IP2K_OLD:
14239     case EM_IP2K:
14240       return reloc_type == 2; /* R_IP2K_32.  */
14241     case EM_IQ2000:
14242       return reloc_type == 2; /* R_IQ2000_32.  */
14243     case EM_LATTICEMICO32:
14244       return reloc_type == 3; /* R_LM32_32.  */
14245     case EM_LOONGARCH:
14246       return reloc_type == 1; /* R_LARCH_32. */
14247     case EM_M32C_OLD:
14248     case EM_M32C:
14249       return reloc_type == 3; /* R_M32C_32.  */
14250     case EM_M32R:
14251       return reloc_type == 34; /* R_M32R_32_RELA.  */
14252     case EM_68HC11:
14253     case EM_68HC12:
14254       return reloc_type == 6; /* R_M68HC11_32.  */
14255     case EM_S12Z:
14256       return reloc_type == 7 || /* R_S12Z_EXT32 */
14257 	reloc_type == 6;        /* R_S12Z_CW32.  */
14258     case EM_MCORE:
14259       return reloc_type == 1; /* R_MCORE_ADDR32.  */
14260     case EM_CYGNUS_MEP:
14261       return reloc_type == 4; /* R_MEP_32.  */
14262     case EM_METAG:
14263       return reloc_type == 2; /* R_METAG_ADDR32.  */
14264     case EM_MICROBLAZE:
14265       return reloc_type == 1; /* R_MICROBLAZE_32.  */
14266     case EM_MIPS:
14267       return reloc_type == 2; /* R_MIPS_32.  */
14268     case EM_MMIX:
14269       return reloc_type == 4; /* R_MMIX_32.  */
14270     case EM_CYGNUS_MN10200:
14271     case EM_MN10200:
14272       return reloc_type == 1; /* R_MN10200_32.  */
14273     case EM_CYGNUS_MN10300:
14274     case EM_MN10300:
14275       return reloc_type == 1; /* R_MN10300_32.  */
14276     case EM_MOXIE:
14277       return reloc_type == 1; /* R_MOXIE_32.  */
14278     case EM_MSP430_OLD:
14279     case EM_MSP430:
14280       return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32.  */
14281     case EM_MT:
14282       return reloc_type == 2; /* R_MT_32.  */
14283     case EM_NDS32:
14284       return reloc_type == 20; /* R_NDS32_32_RELA.  */
14285     case EM_ALTERA_NIOS2:
14286       return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32.  */
14287     case EM_NIOS32:
14288       return reloc_type == 1; /* R_NIOS_32.  */
14289     case EM_OR1K:
14290       return reloc_type == 1; /* R_OR1K_32.  */
14291     case EM_PARISC:
14292       return (reloc_type == 1 /* R_PARISC_DIR32.  */
14293 	      || reloc_type == 2 /* R_PARISC_DIR21L.  */
14294 	      || reloc_type == 41); /* R_PARISC_SECREL32.  */
14295     case EM_PJ:
14296     case EM_PJ_OLD:
14297       return reloc_type == 1; /* R_PJ_DATA_DIR32.  */
14298     case EM_PPC64:
14299       return reloc_type == 1; /* R_PPC64_ADDR32.  */
14300     case EM_PPC:
14301       return reloc_type == 1; /* R_PPC_ADDR32.  */
14302     case EM_TI_PRU:
14303       return reloc_type == 11; /* R_PRU_BFD_RELOC_32.  */
14304     case EM_RISCV:
14305       return reloc_type == 1; /* R_RISCV_32.  */
14306     case EM_RL78:
14307       return reloc_type == 1; /* R_RL78_DIR32.  */
14308     case EM_RX:
14309       return reloc_type == 1; /* R_RX_DIR32.  */
14310     case EM_S370:
14311       return reloc_type == 1; /* R_I370_ADDR31.  */
14312     case EM_S390_OLD:
14313     case EM_S390:
14314       return reloc_type == 4; /* R_S390_32.  */
14315     case EM_SCORE:
14316       return reloc_type == 8; /* R_SCORE_ABS32.  */
14317     case EM_SH:
14318       return reloc_type == 1; /* R_SH_DIR32.  */
14319     case EM_SPARC32PLUS:
14320     case EM_SPARCV9:
14321     case EM_SPARC:
14322       return reloc_type == 3 /* R_SPARC_32.  */
14323 	|| reloc_type == 23; /* R_SPARC_UA32.  */
14324     case EM_SPU:
14325       return reloc_type == 6; /* R_SPU_ADDR32 */
14326     case EM_TI_C6000:
14327       return reloc_type == 1; /* R_C6000_ABS32.  */
14328     case EM_TILEGX:
14329       return reloc_type == 2; /* R_TILEGX_32.  */
14330     case EM_TILEPRO:
14331       return reloc_type == 1; /* R_TILEPRO_32.  */
14332     case EM_CYGNUS_V850:
14333     case EM_V850:
14334       return reloc_type == 6; /* R_V850_ABS32.  */
14335     case EM_V800:
14336       return reloc_type == 0x33; /* R_V810_WORD.  */
14337     case EM_VAX:
14338       return reloc_type == 1; /* R_VAX_32.  */
14339     case EM_VISIUM:
14340       return reloc_type == 3;  /* R_VISIUM_32. */
14341     case EM_WEBASSEMBLY:
14342       return reloc_type == 1;  /* R_WASM32_32.  */
14343     case EM_X86_64:
14344     case EM_L1OM:
14345     case EM_K1OM:
14346       return reloc_type == 10; /* R_X86_64_32.  */
14347     case EM_XGATE:
14348       return reloc_type == 4; /* R_XGATE_32.  */
14349     case EM_XSTORMY16:
14350       return reloc_type == 1; /* R_XSTROMY16_32.  */
14351     case EM_XTENSA_OLD:
14352     case EM_XTENSA:
14353       return reloc_type == 1; /* R_XTENSA_32.  */
14354     case EM_Z80:
14355       return reloc_type == 6; /* R_Z80_32.  */
14356     default:
14357       {
14358 	static unsigned int prev_warn = 0;
14359 
14360 	/* Avoid repeating the same warning multiple times.  */
14361 	if (prev_warn != filedata->file_header.e_machine)
14362 	  error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
14363 		 filedata->file_header.e_machine);
14364 	prev_warn = filedata->file_header.e_machine;
14365 	return false;
14366       }
14367     }
14368 }
14369 
14370 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14371    a 32-bit pc-relative RELA relocation used in DWARF debug sections.  */
14372 
14373 static bool
14374 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14375 {
14376   switch (filedata->file_header.e_machine)
14377   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14378     {
14379     case EM_386:
14380     case EM_IAMCU:
14381       return reloc_type == 2;  /* R_386_PC32.  */
14382     case EM_68K:
14383       return reloc_type == 4;  /* R_68K_PC32.  */
14384     case EM_AARCH64:
14385       return reloc_type == 261; /* R_AARCH64_PREL32 */
14386     case EM_ADAPTEVA_EPIPHANY:
14387       return reloc_type == 6;
14388     case EM_ALPHA:
14389       return reloc_type == 10; /* R_ALPHA_SREL32.  */
14390     case EM_ARC_COMPACT:
14391     case EM_ARC_COMPACT2:
14392       return reloc_type == 49; /* R_ARC_32_PCREL.  */
14393     case EM_ARM:
14394       return reloc_type == 3;  /* R_ARM_REL32 */
14395     case EM_AVR_OLD:
14396     case EM_AVR:
14397       return reloc_type == 36; /* R_AVR_32_PCREL.  */
14398     case EM_MICROBLAZE:
14399       return reloc_type == 2;  /* R_MICROBLAZE_32_PCREL.  */
14400     case EM_OR1K:
14401       return reloc_type == 9; /* R_OR1K_32_PCREL.  */
14402     case EM_PARISC:
14403       return reloc_type == 9;  /* R_PARISC_PCREL32.  */
14404     case EM_PPC:
14405       return reloc_type == 26; /* R_PPC_REL32.  */
14406     case EM_PPC64:
14407       return reloc_type == 26; /* R_PPC64_REL32.  */
14408     case EM_RISCV:
14409       return reloc_type == 57;	/* R_RISCV_32_PCREL.  */
14410     case EM_S390_OLD:
14411     case EM_S390:
14412       return reloc_type == 5;  /* R_390_PC32.  */
14413     case EM_SH:
14414       return reloc_type == 2;  /* R_SH_REL32.  */
14415     case EM_SPARC32PLUS:
14416     case EM_SPARCV9:
14417     case EM_SPARC:
14418       return reloc_type == 6;  /* R_SPARC_DISP32.  */
14419     case EM_SPU:
14420       return reloc_type == 13; /* R_SPU_REL32.  */
14421     case EM_TILEGX:
14422       return reloc_type == 6; /* R_TILEGX_32_PCREL.  */
14423     case EM_TILEPRO:
14424       return reloc_type == 4; /* R_TILEPRO_32_PCREL.  */
14425     case EM_VISIUM:
14426       return reloc_type == 6;  /* R_VISIUM_32_PCREL */
14427     case EM_X86_64:
14428     case EM_L1OM:
14429     case EM_K1OM:
14430       return reloc_type == 2;  /* R_X86_64_PC32.  */
14431     case EM_VAX:
14432       return reloc_type == 4;  /* R_VAX_PCREL32.  */
14433     case EM_XTENSA_OLD:
14434     case EM_XTENSA:
14435       return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
14436     default:
14437       /* Do not abort or issue an error message here.  Not all targets use
14438 	 pc-relative 32-bit relocs in their DWARF debug information and we
14439 	 have already tested for target coverage in is_32bit_abs_reloc.  A
14440 	 more helpful warning message will be generated by apply_relocations
14441 	 anyway, so just return.  */
14442       return false;
14443     }
14444 }
14445 
14446 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14447    a 64-bit absolute RELA relocation used in DWARF debug sections.  */
14448 
14449 static bool
14450 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14451 {
14452   switch (filedata->file_header.e_machine)
14453     {
14454     case EM_AARCH64:
14455       return reloc_type == 257;	/* R_AARCH64_ABS64.  */
14456     case EM_ALPHA:
14457       return reloc_type == 2; /* R_ALPHA_REFQUAD.  */
14458     case EM_IA_64:
14459       return (reloc_type == 0x26    /* R_IA64_DIR64MSB.  */
14460 	      || reloc_type == 0x27 /* R_IA64_DIR64LSB.  */);
14461     case EM_LOONGARCH:
14462       return reloc_type == 2;      /* R_LARCH_64 */
14463     case EM_PARISC:
14464       return reloc_type == 80; /* R_PARISC_DIR64.  */
14465     case EM_PPC64:
14466       return reloc_type == 38; /* R_PPC64_ADDR64.  */
14467     case EM_RISCV:
14468       return reloc_type == 2; /* R_RISCV_64.  */
14469     case EM_SPARC32PLUS:
14470     case EM_SPARCV9:
14471     case EM_SPARC:
14472       return reloc_type == 32 /* R_SPARC_64.  */
14473 	|| reloc_type == 54; /* R_SPARC_UA64.  */
14474     case EM_X86_64:
14475     case EM_L1OM:
14476     case EM_K1OM:
14477       return reloc_type == 1; /* R_X86_64_64.  */
14478     case EM_S390_OLD:
14479     case EM_S390:
14480       return reloc_type == 22;	/* R_S390_64.  */
14481     case EM_TILEGX:
14482       return reloc_type == 1; /* R_TILEGX_64.  */
14483     case EM_MIPS:
14484       return reloc_type == 18;	/* R_MIPS_64.  */
14485     default:
14486       return false;
14487     }
14488 }
14489 
14490 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
14491    a 64-bit pc-relative RELA relocation used in DWARF debug sections.  */
14492 
14493 static bool
14494 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14495 {
14496   switch (filedata->file_header.e_machine)
14497     {
14498     case EM_AARCH64:
14499       return reloc_type == 260;	/* R_AARCH64_PREL64.  */
14500     case EM_ALPHA:
14501       return reloc_type == 11; /* R_ALPHA_SREL64.  */
14502     case EM_IA_64:
14503       return (reloc_type == 0x4e    /* R_IA64_PCREL64MSB.  */
14504 	      || reloc_type == 0x4f /* R_IA64_PCREL64LSB.  */);
14505     case EM_PARISC:
14506       return reloc_type == 72; /* R_PARISC_PCREL64.  */
14507     case EM_PPC64:
14508       return reloc_type == 44; /* R_PPC64_REL64.  */
14509     case EM_SPARC32PLUS:
14510     case EM_SPARCV9:
14511     case EM_SPARC:
14512       return reloc_type == 46; /* R_SPARC_DISP64.  */
14513     case EM_X86_64:
14514     case EM_L1OM:
14515     case EM_K1OM:
14516       return reloc_type == 24; /* R_X86_64_PC64.  */
14517     case EM_S390_OLD:
14518     case EM_S390:
14519       return reloc_type == 23;	/* R_S390_PC64.  */
14520     case EM_TILEGX:
14521       return reloc_type == 5;  /* R_TILEGX_64_PCREL.  */
14522     default:
14523       return false;
14524     }
14525 }
14526 
14527 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14528    a 24-bit absolute RELA relocation used in DWARF debug sections.  */
14529 
14530 static bool
14531 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14532 {
14533   switch (filedata->file_header.e_machine)
14534     {
14535     case EM_CYGNUS_MN10200:
14536     case EM_MN10200:
14537       return reloc_type == 4; /* R_MN10200_24.  */
14538     case EM_FT32:
14539       return reloc_type == 5; /* R_FT32_20.  */
14540     case EM_Z80:
14541       return reloc_type == 5; /* R_Z80_24. */
14542     default:
14543       return false;
14544     }
14545 }
14546 
14547 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14548    a 16-bit absolute RELA relocation used in DWARF debug sections.  */
14549 
14550 static bool
14551 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14552 {
14553   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14554   switch (filedata->file_header.e_machine)
14555     {
14556     case EM_ARC:
14557     case EM_ARC_COMPACT:
14558     case EM_ARC_COMPACT2:
14559       return reloc_type == 2; /* R_ARC_16.  */
14560     case EM_ADAPTEVA_EPIPHANY:
14561       return reloc_type == 5;
14562     case EM_AVR_OLD:
14563     case EM_AVR:
14564       return reloc_type == 4; /* R_AVR_16.  */
14565     case EM_CYGNUS_D10V:
14566     case EM_D10V:
14567       return reloc_type == 3; /* R_D10V_16.  */
14568     case EM_FT32:
14569       return reloc_type == 2; /* R_FT32_16.  */
14570     case EM_H8S:
14571     case EM_H8_300:
14572     case EM_H8_300H:
14573       return reloc_type == R_H8_DIR16;
14574     case EM_IP2K_OLD:
14575     case EM_IP2K:
14576       return reloc_type == 1; /* R_IP2K_16.  */
14577     case EM_M32C_OLD:
14578     case EM_M32C:
14579       return reloc_type == 1; /* R_M32C_16 */
14580     case EM_CYGNUS_MN10200:
14581     case EM_MN10200:
14582       return reloc_type == 2; /* R_MN10200_16.  */
14583     case EM_CYGNUS_MN10300:
14584     case EM_MN10300:
14585       return reloc_type == 2; /* R_MN10300_16.  */
14586     case EM_MSP430:
14587       if (uses_msp430x_relocs (filedata))
14588 	return reloc_type == 2; /* R_MSP430_ABS16.  */
14589       /* Fall through.  */
14590     case EM_MSP430_OLD:
14591       return reloc_type == 5; /* R_MSP430_16_BYTE.  */
14592     case EM_NDS32:
14593       return reloc_type == 19; /* R_NDS32_16_RELA.  */
14594     case EM_ALTERA_NIOS2:
14595       return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16.  */
14596     case EM_NIOS32:
14597       return reloc_type == 9; /* R_NIOS_16.  */
14598     case EM_OR1K:
14599       return reloc_type == 2; /* R_OR1K_16.  */
14600     case EM_RISCV:
14601       return reloc_type == 55; /* R_RISCV_SET16.  */
14602     case EM_TI_PRU:
14603       return reloc_type == 8; /* R_PRU_BFD_RELOC_16.  */
14604     case EM_TI_C6000:
14605       return reloc_type == 2; /* R_C6000_ABS16.  */
14606     case EM_VISIUM:
14607       return reloc_type == 2; /* R_VISIUM_16. */
14608     case EM_XGATE:
14609       return reloc_type == 3; /* R_XGATE_16.  */
14610     case EM_Z80:
14611       return reloc_type == 4; /* R_Z80_16.  */
14612     default:
14613       return false;
14614     }
14615 }
14616 
14617 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14618    a 8-bit absolute RELA relocation used in DWARF debug sections.  */
14619 
14620 static bool
14621 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14622 {
14623   switch (filedata->file_header.e_machine)
14624     {
14625     case EM_RISCV:
14626       return reloc_type == 54; /* R_RISCV_SET8.  */
14627     case EM_Z80:
14628       return reloc_type == 1;  /* R_Z80_8.  */
14629     default:
14630       return false;
14631     }
14632 }
14633 
14634 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14635    a 6-bit absolute RELA relocation used in DWARF debug sections.  */
14636 
14637 static bool
14638 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14639 {
14640   switch (filedata->file_header.e_machine)
14641     {
14642     case EM_RISCV:
14643       return reloc_type == 53; /* R_RISCV_SET6.  */
14644     default:
14645       return false;
14646     }
14647 }
14648 
14649 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14650    a 32-bit inplace add RELA relocation used in DWARF debug sections.  */
14651 
14652 static bool
14653 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14654 {
14655   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14656   switch (filedata->file_header.e_machine)
14657     {
14658     case EM_RISCV:
14659       return reloc_type == 35; /* R_RISCV_ADD32.  */
14660     default:
14661       return false;
14662     }
14663 }
14664 
14665 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14666    a 32-bit inplace sub RELA relocation used in DWARF debug sections.  */
14667 
14668 static bool
14669 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14670 {
14671   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14672   switch (filedata->file_header.e_machine)
14673     {
14674     case EM_RISCV:
14675       return reloc_type == 39; /* R_RISCV_SUB32.  */
14676     default:
14677       return false;
14678     }
14679 }
14680 
14681 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14682    a 64-bit inplace add RELA relocation used in DWARF debug sections.  */
14683 
14684 static bool
14685 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14686 {
14687   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14688   switch (filedata->file_header.e_machine)
14689     {
14690     case EM_RISCV:
14691       return reloc_type == 36; /* R_RISCV_ADD64.  */
14692     default:
14693       return false;
14694     }
14695 }
14696 
14697 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14698    a 64-bit inplace sub RELA relocation used in DWARF debug sections.  */
14699 
14700 static bool
14701 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14702 {
14703   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14704   switch (filedata->file_header.e_machine)
14705     {
14706     case EM_RISCV:
14707       return reloc_type == 40; /* R_RISCV_SUB64.  */
14708     default:
14709       return false;
14710     }
14711 }
14712 
14713 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14714    a 16-bit inplace add RELA relocation used in DWARF debug sections.  */
14715 
14716 static bool
14717 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14718 {
14719   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14720   switch (filedata->file_header.e_machine)
14721     {
14722     case EM_RISCV:
14723       return reloc_type == 34; /* R_RISCV_ADD16.  */
14724     default:
14725       return false;
14726     }
14727 }
14728 
14729 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14730    a 16-bit inplace sub RELA relocation used in DWARF debug sections.  */
14731 
14732 static bool
14733 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14734 {
14735   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14736   switch (filedata->file_header.e_machine)
14737     {
14738     case EM_RISCV:
14739       return reloc_type == 38; /* R_RISCV_SUB16.  */
14740     default:
14741       return false;
14742     }
14743 }
14744 
14745 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14746    a 8-bit inplace add RELA relocation used in DWARF debug sections.  */
14747 
14748 static bool
14749 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14750 {
14751   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14752   switch (filedata->file_header.e_machine)
14753     {
14754     case EM_RISCV:
14755       return reloc_type == 33; /* R_RISCV_ADD8.  */
14756     default:
14757       return false;
14758     }
14759 }
14760 
14761 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14762    a 8-bit inplace sub RELA relocation used in DWARF debug sections.  */
14763 
14764 static bool
14765 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14766 {
14767   /* Please keep this table alpha-sorted for ease of visual lookup.  */
14768   switch (filedata->file_header.e_machine)
14769     {
14770     case EM_RISCV:
14771       return reloc_type == 37; /* R_RISCV_SUB8.  */
14772     default:
14773       return false;
14774     }
14775 }
14776 
14777 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14778    a 6-bit inplace sub RELA relocation used in DWARF debug sections.  */
14779 
14780 static bool
14781 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14782 {
14783   switch (filedata->file_header.e_machine)
14784     {
14785     case EM_RISCV:
14786       return reloc_type == 52; /* R_RISCV_SUB6.  */
14787     default:
14788       return false;
14789     }
14790 }
14791 
14792 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14793    relocation entries (possibly formerly used for SHT_GROUP sections).  */
14794 
14795 static bool
14796 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14797 {
14798   switch (filedata->file_header.e_machine)
14799     {
14800     case EM_386:     /* R_386_NONE.  */
14801     case EM_68K:     /* R_68K_NONE.  */
14802     case EM_ADAPTEVA_EPIPHANY:
14803     case EM_ALPHA:   /* R_ALPHA_NONE.  */
14804     case EM_ALTERA_NIOS2: /* R_NIOS2_NONE.  */
14805     case EM_ARC:     /* R_ARC_NONE.  */
14806     case EM_ARC_COMPACT2: /* R_ARC_NONE.  */
14807     case EM_ARC_COMPACT: /* R_ARC_NONE.  */
14808     case EM_ARM:     /* R_ARM_NONE.  */
14809     case EM_CRIS:    /* R_CRIS_NONE.  */
14810     case EM_FT32:    /* R_FT32_NONE.  */
14811     case EM_IA_64:   /* R_IA64_NONE.  */
14812     case EM_K1OM:    /* R_X86_64_NONE.  */
14813     case EM_L1OM:    /* R_X86_64_NONE.  */
14814     case EM_M32R:    /* R_M32R_NONE.  */
14815     case EM_MIPS:    /* R_MIPS_NONE.  */
14816     case EM_MN10300: /* R_MN10300_NONE.  */
14817     case EM_MOXIE:   /* R_MOXIE_NONE.  */
14818     case EM_NIOS32:  /* R_NIOS_NONE.  */
14819     case EM_OR1K:    /* R_OR1K_NONE. */
14820     case EM_PARISC:  /* R_PARISC_NONE.  */
14821     case EM_PPC64:   /* R_PPC64_NONE.  */
14822     case EM_PPC:     /* R_PPC_NONE.  */
14823     case EM_RISCV:   /* R_RISCV_NONE.  */
14824     case EM_S390:    /* R_390_NONE.  */
14825     case EM_S390_OLD:
14826     case EM_SH:      /* R_SH_NONE.  */
14827     case EM_SPARC32PLUS:
14828     case EM_SPARC:   /* R_SPARC_NONE.  */
14829     case EM_SPARCV9:
14830     case EM_TILEGX:  /* R_TILEGX_NONE.  */
14831     case EM_TILEPRO: /* R_TILEPRO_NONE.  */
14832     case EM_TI_C6000:/* R_C6000_NONE.  */
14833     case EM_X86_64:  /* R_X86_64_NONE.  */
14834     case EM_Z80:     /* R_Z80_NONE. */
14835     case EM_WEBASSEMBLY: /* R_WASM32_NONE.  */
14836       return reloc_type == 0;
14837 
14838     case EM_AARCH64:
14839       return reloc_type == 0 || reloc_type == 256;
14840     case EM_AVR_OLD:
14841     case EM_AVR:
14842       return (reloc_type == 0 /* R_AVR_NONE.  */
14843 	      || reloc_type == 30 /* R_AVR_DIFF8.  */
14844 	      || reloc_type == 31 /* R_AVR_DIFF16.  */
14845 	      || reloc_type == 32 /* R_AVR_DIFF32.  */);
14846     case EM_METAG:
14847       return reloc_type == 3; /* R_METAG_NONE.  */
14848     case EM_NDS32:
14849       return (reloc_type == 0       /* R_NDS32_NONE.  */
14850 	      || reloc_type == 205  /* R_NDS32_DIFF8.  */
14851 	      || reloc_type == 206  /* R_NDS32_DIFF16.  */
14852 	      || reloc_type == 207  /* R_NDS32_DIFF32.  */
14853 	      || reloc_type == 208  /* R_NDS32_DIFF_ULEB128.  */);
14854     case EM_TI_PRU:
14855       return (reloc_type == 0       /* R_PRU_NONE.  */
14856 	      || reloc_type == 65   /* R_PRU_DIFF8.  */
14857 	      || reloc_type == 66   /* R_PRU_DIFF16.  */
14858 	      || reloc_type == 67   /* R_PRU_DIFF32.  */);
14859     case EM_XTENSA_OLD:
14860     case EM_XTENSA:
14861       return (reloc_type == 0      /* R_XTENSA_NONE.  */
14862 	      || reloc_type == 17  /* R_XTENSA_DIFF8.  */
14863 	      || reloc_type == 18  /* R_XTENSA_DIFF16.  */
14864 	      || reloc_type == 19  /* R_XTENSA_DIFF32.  */
14865 	      || reloc_type == 57  /* R_XTENSA_PDIFF8.  */
14866 	      || reloc_type == 58  /* R_XTENSA_PDIFF16.  */
14867 	      || reloc_type == 59  /* R_XTENSA_PDIFF32.  */
14868 	      || reloc_type == 60  /* R_XTENSA_NDIFF8.  */
14869 	      || reloc_type == 61  /* R_XTENSA_NDIFF16.  */
14870 	      || reloc_type == 62  /* R_XTENSA_NDIFF32.  */);
14871     }
14872   return false;
14873 }
14874 
14875 /* Returns TRUE if there is a relocation against
14876    section NAME at OFFSET bytes.  */
14877 
14878 bool
14879 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14880 {
14881   Elf_Internal_Rela * relocs;
14882   Elf_Internal_Rela * rp;
14883 
14884   if (dsec == NULL || dsec->reloc_info == NULL)
14885     return false;
14886 
14887   relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14888 
14889   for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14890     if (rp->r_offset == offset)
14891       return true;
14892 
14893    return false;
14894 }
14895 
14896 /* Apply relocations to a section.
14897    Returns TRUE upon success, FALSE otherwise.
14898    If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14899    It is then the caller's responsibility to free them.  NUM_RELOCS_RETURN
14900    will be set to the number of relocs loaded.
14901 
14902    Note: So far support has been added only for those relocations
14903    which can be found in debug sections. FIXME: Add support for
14904    more relocations ?  */
14905 
14906 static bool
14907 apply_relocations (Filedata *                 filedata,
14908 		   const Elf_Internal_Shdr *  section,
14909 		   unsigned char *            start,
14910 		   bfd_size_type              size,
14911 		   void **                    relocs_return,
14912 		   unsigned long *            num_relocs_return)
14913 {
14914   Elf_Internal_Shdr * relsec;
14915   unsigned char * end = start + size;
14916 
14917   if (relocs_return != NULL)
14918     {
14919       * (Elf_Internal_Rela **) relocs_return = NULL;
14920       * num_relocs_return = 0;
14921     }
14922 
14923   if (filedata->file_header.e_type != ET_REL)
14924     /* No relocs to apply.  */
14925     return true;
14926 
14927   /* Find the reloc section associated with the section.  */
14928   for (relsec = filedata->section_headers;
14929        relsec < filedata->section_headers + filedata->file_header.e_shnum;
14930        ++relsec)
14931     {
14932       bool is_rela;
14933       unsigned long num_relocs;
14934       Elf_Internal_Rela * relocs;
14935       Elf_Internal_Rela * rp;
14936       Elf_Internal_Shdr * symsec;
14937       Elf_Internal_Sym * symtab;
14938       unsigned long num_syms;
14939       Elf_Internal_Sym * sym;
14940 
14941       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14942 	  || relsec->sh_info >= filedata->file_header.e_shnum
14943 	  || filedata->section_headers + relsec->sh_info != section
14944 	  || relsec->sh_size == 0
14945 	  || relsec->sh_link >= filedata->file_header.e_shnum)
14946 	continue;
14947 
14948       symsec = filedata->section_headers + relsec->sh_link;
14949       if (symsec->sh_type != SHT_SYMTAB
14950 	  && symsec->sh_type != SHT_DYNSYM)
14951 	return false;
14952 
14953       is_rela = relsec->sh_type == SHT_RELA;
14954 
14955       if (is_rela)
14956 	{
14957 	  if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14958                                   relsec->sh_size, & relocs, & num_relocs))
14959 	    return false;
14960 	}
14961       else
14962 	{
14963 	  if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14964                                  relsec->sh_size, & relocs, & num_relocs))
14965 	    return false;
14966 	}
14967 
14968       /* SH uses RELA but uses in place value instead of the addend field.  */
14969       if (filedata->file_header.e_machine == EM_SH)
14970 	is_rela = false;
14971 
14972       symtab = get_elf_symbols (filedata, symsec, & num_syms);
14973 
14974       for (rp = relocs; rp < relocs + num_relocs; ++rp)
14975 	{
14976 	  bfd_vma addend;
14977 	  unsigned int reloc_type;
14978 	  unsigned int reloc_size;
14979 	  bool reloc_inplace = false;
14980 	  bool reloc_subtract = false;
14981 	  unsigned char *rloc;
14982 	  unsigned long sym_index;
14983 
14984 	  reloc_type = get_reloc_type (filedata, rp->r_info);
14985 
14986 	  if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14987 	    continue;
14988 	  else if (is_none_reloc (filedata, reloc_type))
14989 	    continue;
14990 	  else if (is_32bit_abs_reloc (filedata, reloc_type)
14991 		   || is_32bit_pcrel_reloc (filedata, reloc_type))
14992 	    reloc_size = 4;
14993 	  else if (is_64bit_abs_reloc (filedata, reloc_type)
14994 		   || is_64bit_pcrel_reloc (filedata, reloc_type))
14995 	    reloc_size = 8;
14996 	  else if (is_24bit_abs_reloc (filedata, reloc_type))
14997 	    reloc_size = 3;
14998 	  else if (is_16bit_abs_reloc (filedata, reloc_type))
14999 	    reloc_size = 2;
15000 	  else if (is_8bit_abs_reloc (filedata, reloc_type)
15001 		   || is_6bit_abs_reloc (filedata, reloc_type))
15002 	    reloc_size = 1;
15003 	  else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15004 								 reloc_type))
15005 		   || is_32bit_inplace_add_reloc (filedata, reloc_type))
15006 	    {
15007 	      reloc_size = 4;
15008 	      reloc_inplace = true;
15009 	    }
15010 	  else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15011 								 reloc_type))
15012 		   || is_64bit_inplace_add_reloc (filedata, reloc_type))
15013 	    {
15014 	      reloc_size = 8;
15015 	      reloc_inplace = true;
15016 	    }
15017 	  else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15018 								 reloc_type))
15019 		   || is_16bit_inplace_add_reloc (filedata, reloc_type))
15020 	    {
15021 	      reloc_size = 2;
15022 	      reloc_inplace = true;
15023 	    }
15024 	  else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15025 								reloc_type))
15026 		   || is_8bit_inplace_add_reloc (filedata, reloc_type))
15027 	    {
15028 	      reloc_size = 1;
15029 	      reloc_inplace = true;
15030 	    }
15031 	  else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
15032 								reloc_type)))
15033 	    {
15034 	      reloc_size = 1;
15035 	      reloc_inplace = true;
15036 	    }
15037 	  else
15038 	    {
15039 	      static unsigned int prev_reloc = 0;
15040 
15041 	      if (reloc_type != prev_reloc)
15042 		warn (_("unable to apply unsupported reloc type %d to section %s\n"),
15043 		      reloc_type, printable_section_name (filedata, section));
15044 	      prev_reloc = reloc_type;
15045 	      continue;
15046 	    }
15047 
15048 	  rloc = start + rp->r_offset;
15049 	  if (!IN_RANGE (start, end, rloc, reloc_size))
15050 	    {
15051 	      warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
15052 		    (unsigned long) rp->r_offset,
15053 		    printable_section_name (filedata, section));
15054 	      continue;
15055 	    }
15056 
15057 	  sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
15058 	  if (sym_index >= num_syms)
15059 	    {
15060 	      warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
15061 		    sym_index, printable_section_name (filedata, section));
15062 	      continue;
15063 	    }
15064 	  sym = symtab + sym_index;
15065 
15066 	  /* If the reloc has a symbol associated with it,
15067 	     make sure that it is of an appropriate type.
15068 
15069 	     Relocations against symbols without type can happen.
15070 	     Gcc -feliminate-dwarf2-dups may generate symbols
15071 	     without type for debug info.
15072 
15073 	     Icc generates relocations against function symbols
15074 	     instead of local labels.
15075 
15076 	     Relocations against object symbols can happen, eg when
15077 	     referencing a global array.  For an example of this see
15078 	     the _clz.o binary in libgcc.a.  */
15079 	  if (sym != symtab
15080 	      && ELF_ST_TYPE (sym->st_info) != STT_COMMON
15081 	      && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
15082 	    {
15083 	      warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
15084 		    get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15085 		    printable_section_name (filedata, relsec),
15086 		    (long int)(rp - relocs));
15087 	      continue;
15088 	    }
15089 
15090 	  addend = 0;
15091 	  if (is_rela)
15092 	    addend += rp->r_addend;
15093 	  /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15094 	     partial_inplace.  */
15095 	  if (!is_rela
15096 	      || (filedata->file_header.e_machine == EM_XTENSA
15097 		  && reloc_type == 1)
15098 	      || ((filedata->file_header.e_machine == EM_PJ
15099 		   || filedata->file_header.e_machine == EM_PJ_OLD)
15100 		  && reloc_type == 1)
15101 	      || ((filedata->file_header.e_machine == EM_D30V
15102 		   || filedata->file_header.e_machine == EM_CYGNUS_D30V)
15103 		  && reloc_type == 12)
15104 	      || reloc_inplace)
15105 	    {
15106 	      if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15107 		addend += byte_get (rloc, reloc_size) & 0x3f;
15108 	      else
15109 		addend += byte_get (rloc, reloc_size);
15110 	    }
15111 
15112 	  if (is_32bit_pcrel_reloc (filedata, reloc_type)
15113 	      || is_64bit_pcrel_reloc (filedata, reloc_type))
15114 	    {
15115 	      /* On HPPA, all pc-relative relocations are biased by 8.  */
15116 	      if (filedata->file_header.e_machine == EM_PARISC)
15117 		addend -= 8;
15118 	      byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
15119 		        reloc_size);
15120 	    }
15121 	  else if (is_6bit_abs_reloc (filedata, reloc_type)
15122 		   || is_6bit_inplace_sub_reloc (filedata, reloc_type))
15123 	    {
15124 	      if (reloc_subtract)
15125 		addend -= sym->st_value;
15126 	      else
15127 		addend += sym->st_value;
15128 	      addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15129 	      byte_put (rloc, addend, reloc_size);
15130 	    }
15131 	  else if (reloc_subtract)
15132 	    byte_put (rloc, addend - sym->st_value, reloc_size);
15133 	  else
15134 	    byte_put (rloc, addend + sym->st_value, reloc_size);
15135 	}
15136 
15137       free (symtab);
15138       /* Let the target specific reloc processing code know that
15139 	 we have finished with these relocs.  */
15140       target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
15141 
15142       if (relocs_return)
15143 	{
15144 	  * (Elf_Internal_Rela **) relocs_return = relocs;
15145 	  * num_relocs_return = num_relocs;
15146 	}
15147       else
15148 	free (relocs);
15149 
15150       break;
15151     }
15152 
15153   return true;
15154 }
15155 
15156 #ifdef SUPPORT_DISASSEMBLY
15157 static bool
15158 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
15159 {
15160   printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
15161 
15162   /* FIXME: XXX -- to be done --- XXX */
15163 
15164   return true;
15165 }
15166 #endif
15167 
15168 /* Reads in the contents of SECTION from FILE, returning a pointer
15169    to a malloc'ed buffer or NULL if something went wrong.  */
15170 
15171 static char *
15172 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
15173 {
15174   bfd_size_type num_bytes = section->sh_size;
15175 
15176   if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15177     {
15178       printf (_("Section '%s' has no data to dump.\n"),
15179 	      printable_section_name (filedata, section));
15180       return NULL;
15181     }
15182 
15183   return  (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
15184                              _("section contents"));
15185 }
15186 
15187 /* Uncompresses a section that was compressed using zlib, in place.  */
15188 
15189 static bool
15190 uncompress_section_contents (unsigned char **   buffer,
15191 			     dwarf_size_type    uncompressed_size,
15192 			     dwarf_size_type *  size)
15193 {
15194   dwarf_size_type compressed_size = *size;
15195   unsigned char * compressed_buffer = *buffer;
15196   unsigned char * uncompressed_buffer;
15197   z_stream strm;
15198   int rc;
15199 
15200   /* It is possible the section consists of several compressed
15201      buffers concatenated together, so we uncompress in a loop.  */
15202   /* PR 18313: The state field in the z_stream structure is supposed
15203      to be invisible to the user (ie us), but some compilers will
15204      still complain about it being used without initialisation.  So
15205      we first zero the entire z_stream structure and then set the fields
15206      that we need.  */
15207   memset (& strm, 0, sizeof strm);
15208   strm.avail_in = compressed_size;
15209   strm.next_in = (Bytef *) compressed_buffer;
15210   strm.avail_out = uncompressed_size;
15211   uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
15212 
15213   rc = inflateInit (& strm);
15214   while (strm.avail_in > 0)
15215     {
15216       if (rc != Z_OK)
15217         break;
15218       strm.next_out = ((Bytef *) uncompressed_buffer
15219                        + (uncompressed_size - strm.avail_out));
15220       rc = inflate (&strm, Z_FINISH);
15221       if (rc != Z_STREAM_END)
15222         break;
15223       rc = inflateReset (& strm);
15224     }
15225   if (inflateEnd (& strm) != Z_OK
15226       || rc != Z_OK
15227       || strm.avail_out != 0)
15228     goto fail;
15229 
15230   *buffer = uncompressed_buffer;
15231   *size = uncompressed_size;
15232   return true;
15233 
15234  fail:
15235   free (uncompressed_buffer);
15236   /* Indicate decompression failure.  */
15237   *buffer = NULL;
15238   return false;
15239 }
15240 
15241 static bool
15242 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15243 {
15244   Elf_Internal_Shdr *relsec;
15245   bfd_size_type num_bytes;
15246   unsigned char *data;
15247   unsigned char *end;
15248   unsigned char *real_start;
15249   unsigned char *start;
15250   bool some_strings_shown;
15251 
15252   real_start = start = (unsigned char *) get_section_contents (section, filedata);
15253   if (start == NULL)
15254     /* PR 21820: Do not fail if the section was empty.  */
15255     return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15256 
15257   num_bytes = section->sh_size;
15258 
15259   if (filedata->is_separate)
15260     printf (_("\nString dump of section '%s' in linked file %s:\n"),
15261 	    printable_section_name (filedata, section),
15262 	    filedata->file_name);
15263   else
15264     printf (_("\nString dump of section '%s':\n"),
15265 	    printable_section_name (filedata, section));
15266 
15267   if (decompress_dumps)
15268     {
15269       dwarf_size_type new_size = num_bytes;
15270       dwarf_size_type uncompressed_size = 0;
15271 
15272       if ((section->sh_flags & SHF_COMPRESSED) != 0)
15273 	{
15274 	  Elf_Internal_Chdr chdr;
15275 	  unsigned int compression_header_size
15276 	    = get_compression_header (& chdr, (unsigned char *) start,
15277 				      num_bytes);
15278 	  if (compression_header_size == 0)
15279 	    /* An error message will have already been generated
15280 	       by get_compression_header.  */
15281 	    goto error_out;
15282 
15283 	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15284 	    {
15285 	      warn (_("section '%s' has unsupported compress type: %d\n"),
15286 		    printable_section_name (filedata, section), chdr.ch_type);
15287 	      goto error_out;
15288 	    }
15289 	  uncompressed_size = chdr.ch_size;
15290 	  start += compression_header_size;
15291 	  new_size -= compression_header_size;
15292 	}
15293       else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15294 	{
15295 	  /* Read the zlib header.  In this case, it should be "ZLIB"
15296 	     followed by the uncompressed section size, 8 bytes in
15297 	     big-endian order.  */
15298 	  uncompressed_size = start[4]; uncompressed_size <<= 8;
15299 	  uncompressed_size += start[5]; uncompressed_size <<= 8;
15300 	  uncompressed_size += start[6]; uncompressed_size <<= 8;
15301 	  uncompressed_size += start[7]; uncompressed_size <<= 8;
15302 	  uncompressed_size += start[8]; uncompressed_size <<= 8;
15303 	  uncompressed_size += start[9]; uncompressed_size <<= 8;
15304 	  uncompressed_size += start[10]; uncompressed_size <<= 8;
15305 	  uncompressed_size += start[11];
15306 	  start += 12;
15307 	  new_size -= 12;
15308 	}
15309 
15310       if (uncompressed_size)
15311 	{
15312 	  if (uncompress_section_contents (& start,
15313 					   uncompressed_size, & new_size))
15314 	    num_bytes = new_size;
15315 	  else
15316 	    {
15317 	      error (_("Unable to decompress section %s\n"),
15318 		     printable_section_name (filedata, section));
15319 	      goto error_out;
15320 	    }
15321 	}
15322       else
15323 	start = real_start;
15324     }
15325 
15326   /* If the section being dumped has relocations against it the user might
15327      be expecting these relocations to have been applied.  Check for this
15328      case and issue a warning message in order to avoid confusion.
15329      FIXME: Maybe we ought to have an option that dumps a section with
15330      relocs applied ?  */
15331   for (relsec = filedata->section_headers;
15332        relsec < filedata->section_headers + filedata->file_header.e_shnum;
15333        ++relsec)
15334     {
15335       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15336 	  || relsec->sh_info >= filedata->file_header.e_shnum
15337 	  || filedata->section_headers + relsec->sh_info != section
15338 	  || relsec->sh_size == 0
15339 	  || relsec->sh_link >= filedata->file_header.e_shnum)
15340 	continue;
15341 
15342       printf (_("  Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15343       break;
15344     }
15345 
15346   data = start;
15347   end  = start + num_bytes;
15348   some_strings_shown = false;
15349 
15350 #ifdef HAVE_MBSTATE_T
15351   mbstate_t state;
15352   /* Initialise the multibyte conversion state.  */
15353   memset (& state, 0, sizeof (state));
15354 #endif
15355 
15356   bool continuing = false;
15357 
15358   while (data < end)
15359     {
15360       while (!ISPRINT (* data))
15361 	if (++ data >= end)
15362 	  break;
15363 
15364       if (data < end)
15365 	{
15366 	  size_t maxlen = end - data;
15367 
15368 	  if (continuing)
15369 	    {
15370 	      printf ("            ");
15371 	      continuing = false;
15372 	    }
15373 	  else
15374 	    {
15375 	      printf ("  [%6lx]  ", (unsigned long) (data - start));
15376 	    }
15377 
15378 	  if (maxlen > 0)
15379 	    {
15380 	      char c = 0;
15381 
15382 	      while (maxlen)
15383 		{
15384 		  c = *data++;
15385 
15386 		  if (c == 0)
15387 		    break;
15388 
15389 		  /* PR 25543: Treat new-lines as string-ending characters.  */
15390 		  if (c == '\n')
15391 		    {
15392 		      printf ("\\n\n");
15393 		      if (*data != 0)
15394 			continuing = true;
15395 		      break;
15396 		    }
15397 
15398 		  /* Do not print control characters directly as they can affect terminal
15399 		     settings.  Such characters usually appear in the names generated
15400 		     by the assembler for local labels.  */
15401 		  if (ISCNTRL (c))
15402 		    {
15403 		      printf ("^%c", c + 0x40);
15404 		    }
15405 		  else if (ISPRINT (c))
15406 		    {
15407 		      putchar (c);
15408 		    }
15409 		  else
15410 		    {
15411 		      size_t  n;
15412 #ifdef HAVE_MBSTATE_T
15413 		      wchar_t w;
15414 #endif
15415 		      /* Let printf do the hard work of displaying multibyte characters.  */
15416 		      printf ("%.1s", data - 1);
15417 #ifdef HAVE_MBSTATE_T
15418 		      /* Try to find out how many bytes made up the character that was
15419 			 just printed.  Advance the symbol pointer past the bytes that
15420 			 were displayed.  */
15421 		      n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
15422 #else
15423 		      n = 1;
15424 #endif
15425 		      if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
15426 			data += (n - 1);
15427 		    }
15428 		}
15429 
15430 	      if (c != '\n')
15431 		putchar ('\n');
15432 	    }
15433 	  else
15434 	    {
15435 	      printf (_("<corrupt>\n"));
15436 	      data = end;
15437 	    }
15438 	  some_strings_shown = true;
15439 	}
15440     }
15441 
15442   if (! some_strings_shown)
15443     printf (_("  No strings found in this section."));
15444 
15445   free (real_start);
15446 
15447   putchar ('\n');
15448   return true;
15449 
15450 error_out:
15451   free (real_start);
15452   return false;
15453 }
15454 
15455 static bool
15456 dump_section_as_bytes (Elf_Internal_Shdr *section,
15457 		       Filedata *filedata,
15458 		       bool relocate)
15459 {
15460   Elf_Internal_Shdr * relsec;
15461   bfd_size_type       bytes;
15462   bfd_size_type       section_size;
15463   bfd_vma             addr;
15464   unsigned char *     data;
15465   unsigned char *     real_start;
15466   unsigned char *     start;
15467 
15468   real_start = start = (unsigned char *) get_section_contents (section, filedata);
15469   if (start == NULL)
15470     /* PR 21820: Do not fail if the section was empty.  */
15471     return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15472 
15473   section_size = section->sh_size;
15474 
15475   if (filedata->is_separate)
15476     printf (_("\nHex dump of section '%s' in linked file %s:\n"),
15477 	    printable_section_name (filedata, section),
15478 	    filedata->file_name);
15479   else
15480     printf (_("\nHex dump of section '%s':\n"),
15481 	    printable_section_name (filedata, section));
15482 
15483   if (decompress_dumps)
15484     {
15485       dwarf_size_type new_size = section_size;
15486       dwarf_size_type uncompressed_size = 0;
15487 
15488       if ((section->sh_flags & SHF_COMPRESSED) != 0)
15489 	{
15490 	  Elf_Internal_Chdr chdr;
15491 	  unsigned int compression_header_size
15492 	    = get_compression_header (& chdr, start, section_size);
15493 
15494 	  if (compression_header_size == 0)
15495 	    /* An error message will have already been generated
15496 	       by get_compression_header.  */
15497 	    goto error_out;
15498 
15499 	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15500 	    {
15501 	      warn (_("section '%s' has unsupported compress type: %d\n"),
15502 		    printable_section_name (filedata, section), chdr.ch_type);
15503 	      goto error_out;
15504 	    }
15505 	  uncompressed_size = chdr.ch_size;
15506 	  start += compression_header_size;
15507 	  new_size -= compression_header_size;
15508 	}
15509       else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15510 	{
15511 	  /* Read the zlib header.  In this case, it should be "ZLIB"
15512 	     followed by the uncompressed section size, 8 bytes in
15513 	     big-endian order.  */
15514 	  uncompressed_size = start[4]; uncompressed_size <<= 8;
15515 	  uncompressed_size += start[5]; uncompressed_size <<= 8;
15516 	  uncompressed_size += start[6]; uncompressed_size <<= 8;
15517 	  uncompressed_size += start[7]; uncompressed_size <<= 8;
15518 	  uncompressed_size += start[8]; uncompressed_size <<= 8;
15519 	  uncompressed_size += start[9]; uncompressed_size <<= 8;
15520 	  uncompressed_size += start[10]; uncompressed_size <<= 8;
15521 	  uncompressed_size += start[11];
15522 	  start += 12;
15523 	  new_size -= 12;
15524 	}
15525 
15526       if (uncompressed_size)
15527 	{
15528 	  if (uncompress_section_contents (& start, uncompressed_size,
15529 					   & new_size))
15530 	    {
15531 	      section_size = new_size;
15532 	    }
15533 	  else
15534 	    {
15535 	      error (_("Unable to decompress section %s\n"),
15536 		     printable_section_name (filedata, section));
15537 	      /* FIXME: Print the section anyway ?  */
15538 	      goto error_out;
15539 	    }
15540 	}
15541       else
15542 	start = real_start;
15543     }
15544 
15545   if (relocate)
15546     {
15547       if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15548 	goto error_out;
15549     }
15550   else
15551     {
15552       /* If the section being dumped has relocations against it the user might
15553 	 be expecting these relocations to have been applied.  Check for this
15554 	 case and issue a warning message in order to avoid confusion.
15555 	 FIXME: Maybe we ought to have an option that dumps a section with
15556 	 relocs applied ?  */
15557       for (relsec = filedata->section_headers;
15558 	   relsec < filedata->section_headers + filedata->file_header.e_shnum;
15559 	   ++relsec)
15560 	{
15561 	  if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15562 	      || relsec->sh_info >= filedata->file_header.e_shnum
15563 	      || filedata->section_headers + relsec->sh_info != section
15564 	      || relsec->sh_size == 0
15565 	      || relsec->sh_link >= filedata->file_header.e_shnum)
15566 	    continue;
15567 
15568 	  printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15569 	  break;
15570 	}
15571     }
15572 
15573   addr = section->sh_addr;
15574   bytes = section_size;
15575   data = start;
15576 
15577   while (bytes)
15578     {
15579       int j;
15580       int k;
15581       int lbytes;
15582 
15583       lbytes = (bytes > 16 ? 16 : bytes);
15584 
15585       printf ("  0x%8.8lx ", (unsigned long) addr);
15586 
15587       for (j = 0; j < 16; j++)
15588 	{
15589 	  if (j < lbytes)
15590 	    printf ("%2.2x", data[j]);
15591 	  else
15592 	    printf ("  ");
15593 
15594 	  if ((j & 3) == 3)
15595 	    printf (" ");
15596 	}
15597 
15598       for (j = 0; j < lbytes; j++)
15599 	{
15600 	  k = data[j];
15601 	  if (k >= ' ' && k < 0x7f)
15602 	    printf ("%c", k);
15603 	  else
15604 	    printf (".");
15605 	}
15606 
15607       putchar ('\n');
15608 
15609       data  += lbytes;
15610       addr  += lbytes;
15611       bytes -= lbytes;
15612     }
15613 
15614   free (real_start);
15615 
15616   putchar ('\n');
15617   return true;
15618 
15619  error_out:
15620   free (real_start);
15621   return false;
15622 }
15623 
15624 #ifdef ENABLE_LIBCTF
15625 static ctf_sect_t *
15626 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15627 {
15628   buf->cts_name = section_name_print (filedata, shdr);
15629   buf->cts_size = shdr->sh_size;
15630   buf->cts_entsize = shdr->sh_entsize;
15631 
15632   return buf;
15633 }
15634 
15635 /* Formatting callback function passed to ctf_dump.  Returns either the pointer
15636    it is passed, or a pointer to newly-allocated storage, in which case
15637    dump_ctf() will free it when it no longer needs it.  */
15638 
15639 static char *
15640 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15641 		       char *s, void *arg)
15642 {
15643   const char *blanks = arg;
15644   char *new_s;
15645 
15646   if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15647     return s;
15648   return new_s;
15649 }
15650 
15651 /* Dump CTF errors/warnings.  */
15652 static void
15653 dump_ctf_errs (ctf_dict_t *fp)
15654 {
15655   ctf_next_t *it = NULL;
15656   char *errtext;
15657   int is_warning;
15658   int err;
15659 
15660   /* Dump accumulated errors and warnings.  */
15661   while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15662     {
15663       error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15664 	     errtext);
15665       free (errtext);
15666     }
15667   if (err != ECTF_NEXT_END)
15668     error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15669 }
15670 
15671 /* Dump one CTF archive member.  */
15672 
15673 static void
15674 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15675 			 size_t member)
15676 {
15677   const char *things[] = {"Header", "Labels", "Data objects",
15678 			  "Function objects", "Variables", "Types", "Strings",
15679 			  ""};
15680   const char **thing;
15681   size_t i;
15682 
15683   /* Don't print out the name of the default-named archive member if it appears
15684      first in the list.  The name .ctf appears everywhere, even for things that
15685      aren't really archives, so printing it out is liable to be confusing; also,
15686      the common case by far is for only one archive member to exist, and hiding
15687      it in that case seems worthwhile.  */
15688 
15689   if (strcmp (name, ".ctf") != 0 || member != 0)
15690     printf (_("\nCTF archive member: %s:\n"), name);
15691 
15692   if (ctf_parent_name (ctf) != NULL)
15693     ctf_import (ctf, parent);
15694 
15695   for (i = 0, thing = things; *thing[0]; thing++, i++)
15696     {
15697       ctf_dump_state_t *s = NULL;
15698       char *item;
15699 
15700       printf ("\n  %s:\n", *thing);
15701       while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15702 			       (void *) "    ")) != NULL)
15703 	{
15704 	  printf ("%s\n", item);
15705 	  free (item);
15706 	}
15707 
15708       if (ctf_errno (ctf))
15709 	{
15710 	  error (_("Iteration failed: %s, %s\n"), *thing,
15711 		 ctf_errmsg (ctf_errno (ctf)));
15712 	  break;
15713 	}
15714     }
15715 
15716   dump_ctf_errs (ctf);
15717 }
15718 
15719 static bool
15720 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15721 {
15722   Elf_Internal_Shdr *  symtab_sec = NULL;
15723   Elf_Internal_Shdr *  strtab_sec = NULL;
15724   void *	       data = NULL;
15725   void *	       symdata = NULL;
15726   void *	       strdata = NULL;
15727   ctf_sect_t	       ctfsect, symsect, strsect;
15728   ctf_sect_t *	       symsectp = NULL;
15729   ctf_sect_t *	       strsectp = NULL;
15730   ctf_archive_t *      ctfa = NULL;
15731   ctf_dict_t *         parent = NULL;
15732   ctf_dict_t *         fp;
15733 
15734   ctf_next_t *i = NULL;
15735   const char *name;
15736   size_t member = 0;
15737   int err;
15738   bool ret = false;
15739 
15740   shdr_to_ctf_sect (&ctfsect, section, filedata);
15741   data = get_section_contents (section, filedata);
15742   ctfsect.cts_data = data;
15743 
15744   if (!dump_ctf_symtab_name)
15745     dump_ctf_symtab_name = strdup (".dynsym");
15746 
15747   if (!dump_ctf_strtab_name)
15748     dump_ctf_strtab_name = strdup (".dynstr");
15749 
15750   if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15751     {
15752       if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15753 	{
15754 	  error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15755 	  goto fail;
15756 	}
15757       if ((symdata = (void *) get_data (NULL, filedata,
15758 					symtab_sec->sh_offset, 1,
15759 					symtab_sec->sh_size,
15760 					_("symbols"))) == NULL)
15761 	goto fail;
15762       symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15763       symsect.cts_data = symdata;
15764     }
15765 
15766   if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15767     {
15768       if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15769 	{
15770 	  error (_("No string table section named %s\n"),
15771 		 dump_ctf_strtab_name);
15772 	  goto fail;
15773 	}
15774       if ((strdata = (void *) get_data (NULL, filedata,
15775 					strtab_sec->sh_offset, 1,
15776 					strtab_sec->sh_size,
15777 					_("strings"))) == NULL)
15778 	goto fail;
15779       strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15780       strsect.cts_data = strdata;
15781     }
15782 
15783   /* Load the CTF file and dump it.  It may be a raw CTF section, or an archive:
15784      libctf papers over the difference, so we can pretend it is always an
15785      archive.  */
15786 
15787   if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15788     {
15789       dump_ctf_errs (NULL);
15790       error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15791       goto fail;
15792     }
15793 
15794   ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15795 			      != ELFDATA2MSB);
15796 
15797   /* Preload the parent dict, since it will need to be imported into every
15798      child in turn.  */
15799   if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15800     {
15801       dump_ctf_errs (NULL);
15802       error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15803       goto fail;
15804     }
15805 
15806   ret = true;
15807 
15808   if (filedata->is_separate)
15809     printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15810 	    printable_section_name (filedata, section),
15811 	    filedata->file_name);
15812   else
15813     printf (_("\nDump of CTF section '%s':\n"),
15814 	    printable_section_name (filedata, section));
15815 
15816  while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15817     dump_ctf_archive_member (fp, name, parent, member++);
15818  if (err != ECTF_NEXT_END)
15819    {
15820      dump_ctf_errs (NULL);
15821      error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15822      ret = false;
15823    }
15824 
15825  fail:
15826   ctf_dict_close (parent);
15827   ctf_close (ctfa);
15828   free (data);
15829   free (symdata);
15830   free (strdata);
15831   return ret;
15832 }
15833 #endif
15834 
15835 static bool
15836 load_specific_debug_section (enum dwarf_section_display_enum  debug,
15837 			     const Elf_Internal_Shdr *        sec,
15838 			     void *                           data)
15839 {
15840   struct dwarf_section * section = &debug_displays [debug].section;
15841   char buf [64];
15842   Filedata * filedata = (Filedata *) data;
15843 
15844   if (section->start != NULL)
15845     {
15846       /* If it is already loaded, do nothing.  */
15847       if (streq (section->filename, filedata->file_name))
15848 	return true;
15849       free (section->start);
15850     }
15851 
15852   snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15853   section->address = sec->sh_addr;
15854   section->filename = filedata->file_name;
15855   section->start = (unsigned char *) get_data (NULL, filedata,
15856                                                sec->sh_offset, 1,
15857                                                sec->sh_size, buf);
15858   if (section->start == NULL)
15859     section->size = 0;
15860   else
15861     {
15862       unsigned char *start = section->start;
15863       dwarf_size_type size = sec->sh_size;
15864       dwarf_size_type uncompressed_size = 0;
15865 
15866       if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15867 	{
15868 	  Elf_Internal_Chdr chdr;
15869 	  unsigned int compression_header_size;
15870 
15871 	  if (size < (is_32bit_elf
15872 		      ? sizeof (Elf32_External_Chdr)
15873 		      : sizeof (Elf64_External_Chdr)))
15874 	    {
15875 	      warn (_("compressed section %s is too small to contain a compression header\n"),
15876 		    section->name);
15877 	      return false;
15878 	    }
15879 
15880 	  compression_header_size = get_compression_header (&chdr, start, size);
15881 	  if (compression_header_size == 0)
15882 	    /* An error message will have already been generated
15883 	       by get_compression_header.  */
15884 	    return false;
15885 
15886 	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15887 	    {
15888 	      warn (_("section '%s' has unsupported compress type: %d\n"),
15889 		    section->name, chdr.ch_type);
15890 	      return false;
15891 	    }
15892 	  uncompressed_size = chdr.ch_size;
15893 	  start += compression_header_size;
15894 	  size -= compression_header_size;
15895 	}
15896       else if (size > 12 && streq ((char *) start, "ZLIB"))
15897 	{
15898 	  /* Read the zlib header.  In this case, it should be "ZLIB"
15899 	     followed by the uncompressed section size, 8 bytes in
15900 	     big-endian order.  */
15901 	  uncompressed_size = start[4]; uncompressed_size <<= 8;
15902 	  uncompressed_size += start[5]; uncompressed_size <<= 8;
15903 	  uncompressed_size += start[6]; uncompressed_size <<= 8;
15904 	  uncompressed_size += start[7]; uncompressed_size <<= 8;
15905 	  uncompressed_size += start[8]; uncompressed_size <<= 8;
15906 	  uncompressed_size += start[9]; uncompressed_size <<= 8;
15907 	  uncompressed_size += start[10]; uncompressed_size <<= 8;
15908 	  uncompressed_size += start[11];
15909 	  start += 12;
15910 	  size -= 12;
15911 	}
15912 
15913       if (uncompressed_size)
15914 	{
15915 	  if (uncompress_section_contents (&start, uncompressed_size,
15916 					   &size))
15917 	    {
15918 	      /* Free the compressed buffer, update the section buffer
15919 		 and the section size if uncompress is successful.  */
15920 	      free (section->start);
15921 	      section->start = start;
15922 	    }
15923 	  else
15924 	    {
15925 	      error (_("Unable to decompress section %s\n"),
15926 		     printable_section_name (filedata, sec));
15927 	      return false;
15928 	    }
15929 	}
15930 
15931       section->size = size;
15932     }
15933 
15934   if (section->start == NULL)
15935     return false;
15936 
15937   if (debug_displays [debug].relocate)
15938     {
15939       if (! apply_relocations (filedata, sec, section->start, section->size,
15940 			       & section->reloc_info, & section->num_relocs))
15941 	return false;
15942     }
15943   else
15944     {
15945       section->reloc_info = NULL;
15946       section->num_relocs = 0;
15947     }
15948 
15949   return true;
15950 }
15951 
15952 #if HAVE_LIBDEBUGINFOD
15953 /* Return a hex string representation of the build-id.  */
15954 unsigned char *
15955 get_build_id (void * data)
15956 {
15957   Filedata * filedata = (Filedata *) data;
15958   Elf_Internal_Shdr * shdr;
15959   unsigned long i;
15960 
15961   /* Iterate through notes to find note.gnu.build-id.
15962      FIXME: Only the first note in any note section is examined.  */
15963   for (i = 0, shdr = filedata->section_headers;
15964        i < filedata->file_header.e_shnum && shdr != NULL;
15965        i++, shdr++)
15966     {
15967       if (shdr->sh_type != SHT_NOTE)
15968         continue;
15969 
15970       char * next;
15971       char * end;
15972       size_t data_remaining;
15973       size_t min_notesz;
15974       Elf_External_Note * enote;
15975       Elf_Internal_Note inote;
15976 
15977       bfd_vma offset = shdr->sh_offset;
15978       bfd_vma align = shdr->sh_addralign;
15979       bfd_vma length = shdr->sh_size;
15980 
15981       enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15982       if (enote == NULL)
15983         continue;
15984 
15985       if (align < 4)
15986         align = 4;
15987       else if (align != 4 && align != 8)
15988 	{
15989 	  free (enote);
15990 	  continue;
15991 	}
15992 
15993       end = (char *) enote + length;
15994       data_remaining = end - (char *) enote;
15995 
15996       if (!is_ia64_vms (filedata))
15997         {
15998           min_notesz = offsetof (Elf_External_Note, name);
15999           if (data_remaining < min_notesz)
16000             {
16001 	      warn (_("\
16002 malformed note encountered in section %s whilst scanning for build-id note\n"),
16003 		    printable_section_name (filedata, shdr));
16004 	      free (enote);
16005               continue;
16006             }
16007           data_remaining -= min_notesz;
16008 
16009           inote.type     = BYTE_GET (enote->type);
16010           inote.namesz   = BYTE_GET (enote->namesz);
16011           inote.namedata = enote->name;
16012           inote.descsz   = BYTE_GET (enote->descsz);
16013           inote.descdata = ((char *) enote
16014                             + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16015           inote.descpos  = offset + (inote.descdata - (char *) enote);
16016           next = ((char *) enote
16017                   + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16018         }
16019       else
16020         {
16021           Elf64_External_VMS_Note *vms_enote;
16022 
16023           /* PR binutils/15191
16024              Make sure that there is enough data to read.  */
16025           min_notesz = offsetof (Elf64_External_VMS_Note, name);
16026           if (data_remaining < min_notesz)
16027             {
16028 	      warn (_("\
16029 malformed note encountered in section %s whilst scanning for build-id note\n"),
16030 		    printable_section_name (filedata, shdr));
16031 	      free (enote);
16032               continue;
16033             }
16034           data_remaining -= min_notesz;
16035 
16036           vms_enote = (Elf64_External_VMS_Note *) enote;
16037           inote.type     = BYTE_GET (vms_enote->type);
16038           inote.namesz   = BYTE_GET (vms_enote->namesz);
16039           inote.namedata = vms_enote->name;
16040           inote.descsz   = BYTE_GET (vms_enote->descsz);
16041           inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16042           inote.descpos  = offset + (inote.descdata - (char *) enote);
16043           next = inote.descdata + align_power (inote.descsz, 3);
16044         }
16045 
16046       /* Skip malformed notes.  */
16047       if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16048           || (size_t) (inote.descdata - inote.namedata) > data_remaining
16049           || (size_t) (next - inote.descdata) < inote.descsz
16050           || ((size_t) (next - inote.descdata)
16051               > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16052         {
16053 	  warn (_("\
16054 malformed note encountered in section %s whilst scanning for build-id note\n"),
16055 		printable_section_name (filedata, shdr));
16056 	  free (enote);
16057           continue;
16058         }
16059 
16060       /* Check if this is the build-id note. If so then convert the build-id
16061          bytes to a hex string.  */
16062       if (inote.namesz > 0
16063           && startswith (inote.namedata, "GNU")
16064           && inote.type == NT_GNU_BUILD_ID)
16065         {
16066           unsigned long j;
16067           char * build_id;
16068 
16069           build_id = malloc (inote.descsz * 2 + 1);
16070           if (build_id == NULL)
16071 	    {
16072 	      free (enote);
16073 	      return NULL;
16074 	    }
16075 
16076           for (j = 0; j < inote.descsz; ++j)
16077             sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16078           build_id[inote.descsz * 2] = '\0';
16079 	  free (enote);
16080 
16081           return (unsigned char *) build_id;
16082         }
16083       free (enote);
16084     }
16085 
16086   return NULL;
16087 }
16088 #endif /* HAVE_LIBDEBUGINFOD */
16089 
16090 /* If this is not NULL, load_debug_section will only look for sections
16091    within the list of sections given here.  */
16092 static unsigned int * section_subset = NULL;
16093 
16094 bool
16095 load_debug_section (enum dwarf_section_display_enum debug, void * data)
16096 {
16097   struct dwarf_section * section = &debug_displays [debug].section;
16098   Elf_Internal_Shdr * sec;
16099   Filedata * filedata = (Filedata *) data;
16100 
16101   if (!dump_any_debugging)
16102     return false;
16103 
16104   /* Without section headers we cannot find any sections.  */
16105   if (filedata->section_headers == NULL)
16106     return false;
16107 
16108   if (filedata->string_table == NULL
16109       && filedata->file_header.e_shstrndx != SHN_UNDEF
16110       && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
16111     {
16112       Elf_Internal_Shdr * strs;
16113 
16114       /* Read in the string table, so that we have section names to scan.  */
16115       strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16116 
16117       if (strs != NULL && strs->sh_size != 0)
16118 	{
16119 	  filedata->string_table
16120 	    = (char *) get_data (NULL, filedata, strs->sh_offset,
16121 				 1, strs->sh_size, _("string table"));
16122 
16123 	  filedata->string_table_length
16124 	    = filedata->string_table != NULL ? strs->sh_size : 0;
16125 	}
16126     }
16127 
16128   /* Locate the debug section.  */
16129   sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
16130   if (sec != NULL)
16131     section->name = section->uncompressed_name;
16132   else
16133     {
16134       sec = find_section_in_set (filedata, section->compressed_name, section_subset);
16135       if (sec != NULL)
16136 	section->name = section->compressed_name;
16137     }
16138   if (sec == NULL)
16139     return false;
16140 
16141   /* If we're loading from a subset of sections, and we've loaded
16142      a section matching this name before, it's likely that it's a
16143      different one.  */
16144   if (section_subset != NULL)
16145     free_debug_section (debug);
16146 
16147   return load_specific_debug_section (debug, sec, data);
16148 }
16149 
16150 void
16151 free_debug_section (enum dwarf_section_display_enum debug)
16152 {
16153   struct dwarf_section * section = &debug_displays [debug].section;
16154 
16155   if (section->start == NULL)
16156     return;
16157 
16158   free ((char *) section->start);
16159   section->start = NULL;
16160   section->address = 0;
16161   section->size = 0;
16162 
16163   free (section->reloc_info);
16164   section->reloc_info = NULL;
16165   section->num_relocs = 0;
16166 }
16167 
16168 static bool
16169 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
16170 {
16171   const char *name = (section_name_valid (filedata, section)
16172 		      ? section_name (filedata, section) : "");
16173   const char *print_name = printable_section_name (filedata, section);
16174   bfd_size_type length;
16175   bool result = true;
16176   int i;
16177 
16178   length = section->sh_size;
16179   if (length == 0)
16180     {
16181       printf (_("\nSection '%s' has no debugging data.\n"), print_name);
16182       return true;
16183     }
16184   if (section->sh_type == SHT_NOBITS)
16185     {
16186       /* There is no point in dumping the contents of a debugging section
16187 	 which has the NOBITS type - the bits in the file will be random.
16188 	 This can happen when a file containing a .eh_frame section is
16189 	 stripped with the --only-keep-debug command line option.  */
16190       printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16191 	      print_name);
16192       return false;
16193     }
16194 
16195   if (startswith (name, ".gnu.linkonce.wi."))
16196     name = ".debug_info";
16197 
16198   /* See if we know how to display the contents of this section.  */
16199   for (i = 0; i < max; i++)
16200     {
16201       enum dwarf_section_display_enum  id = (enum dwarf_section_display_enum) i;
16202       struct dwarf_section_display *   display = debug_displays + i;
16203       struct dwarf_section *           sec = & display->section;
16204 
16205       if (streq (sec->uncompressed_name, name)
16206 	  || (id == line && startswith (name, ".debug_line."))
16207 	  || streq (sec->compressed_name, name))
16208 	{
16209 	  bool secondary = (section != find_section (filedata, name));
16210 
16211 	  if (secondary)
16212 	    free_debug_section (id);
16213 
16214 	  if (i == line && startswith (name, ".debug_line."))
16215 	    sec->name = name;
16216 	  else if (streq (sec->uncompressed_name, name))
16217 	    sec->name = sec->uncompressed_name;
16218 	  else
16219 	    sec->name = sec->compressed_name;
16220 
16221 	  if (load_specific_debug_section (id, section, filedata))
16222 	    {
16223 	      /* If this debug section is part of a CU/TU set in a .dwp file,
16224 		 restrict load_debug_section to the sections in that set.  */
16225 	      section_subset = find_cu_tu_set (filedata, shndx);
16226 
16227 	      result &= display->display (sec, filedata);
16228 
16229 	      section_subset = NULL;
16230 
16231 	      if (secondary || (id != info && id != abbrev && id != debug_addr))
16232 		free_debug_section (id);
16233 	    }
16234 	  break;
16235 	}
16236     }
16237 
16238   if (i == max)
16239     {
16240       printf (_("Unrecognized debug section: %s\n"), print_name);
16241       result = false;
16242     }
16243 
16244   return result;
16245 }
16246 
16247 /* Set DUMP_SECTS for all sections where dumps were requested
16248    based on section name.  */
16249 
16250 static void
16251 initialise_dumps_byname (Filedata * filedata)
16252 {
16253   struct dump_list_entry * cur;
16254 
16255   for (cur = dump_sects_byname; cur; cur = cur->next)
16256     {
16257       unsigned int i;
16258       bool any = false;
16259 
16260       for (i = 0; i < filedata->file_header.e_shnum; i++)
16261 	if (section_name_valid (filedata, filedata->section_headers + i)
16262 	    && streq (section_name (filedata, filedata->section_headers + i),
16263 		      cur->name))
16264 	  {
16265 	    request_dump_bynumber (&filedata->dump, i, cur->type);
16266 	    any = true;
16267 	  }
16268 
16269       if (!any && !filedata->is_separate)
16270 	warn (_("Section '%s' was not dumped because it does not exist\n"),
16271 	      cur->name);
16272     }
16273 }
16274 
16275 static bool
16276 process_section_contents (Filedata * filedata)
16277 {
16278   Elf_Internal_Shdr * section;
16279   unsigned int i;
16280   bool res = true;
16281 
16282   if (! do_dump)
16283     return true;
16284 
16285   initialise_dumps_byname (filedata);
16286 
16287   for (i = 0, section = filedata->section_headers;
16288        i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
16289        i++, section++)
16290     {
16291       dump_type dump = filedata->dump.dump_sects[i];
16292 
16293       if (filedata->is_separate && ! process_links)
16294 	dump &= DEBUG_DUMP;
16295 
16296 #ifdef SUPPORT_DISASSEMBLY
16297       if (dump & DISASS_DUMP)
16298 	{
16299 	  if (! disassemble_section (section, filedata))
16300 	    res = false;
16301 	}
16302 #endif
16303       if (dump & HEX_DUMP)
16304 	{
16305 	  if (! dump_section_as_bytes (section, filedata, false))
16306 	    res = false;
16307 	}
16308 
16309       if (dump & RELOC_DUMP)
16310 	{
16311 	  if (! dump_section_as_bytes (section, filedata, true))
16312 	    res = false;
16313 	}
16314 
16315       if (dump & STRING_DUMP)
16316 	{
16317 	  if (! dump_section_as_strings (section, filedata))
16318 	    res = false;
16319 	}
16320 
16321       if (dump & DEBUG_DUMP)
16322 	{
16323 	  if (! display_debug_section (i, section, filedata))
16324 	    res = false;
16325 	}
16326 
16327 #ifdef ENABLE_LIBCTF
16328       if (dump & CTF_DUMP)
16329 	{
16330 	  if (! dump_section_as_ctf (section, filedata))
16331 	    res = false;
16332 	}
16333 #endif
16334     }
16335 
16336   if (! filedata->is_separate)
16337     {
16338       /* Check to see if the user requested a
16339 	 dump of a section that does not exist.  */
16340       for (; i < filedata->dump.num_dump_sects; i++)
16341 	if (filedata->dump.dump_sects[i])
16342 	  {
16343 	    warn (_("Section %d was not dumped because it does not exist!\n"), i);
16344 	    res = false;
16345 	  }
16346     }
16347 
16348   return res;
16349 }
16350 
16351 static void
16352 process_mips_fpe_exception (int mask)
16353 {
16354   if (mask)
16355     {
16356       bool first = true;
16357 
16358       if (mask & OEX_FPU_INEX)
16359 	fputs ("INEX", stdout), first = false;
16360       if (mask & OEX_FPU_UFLO)
16361 	printf ("%sUFLO", first ? "" : "|"), first = false;
16362       if (mask & OEX_FPU_OFLO)
16363 	printf ("%sOFLO", first ? "" : "|"), first = false;
16364       if (mask & OEX_FPU_DIV0)
16365 	printf ("%sDIV0", first ? "" : "|"), first = false;
16366       if (mask & OEX_FPU_INVAL)
16367 	printf ("%sINVAL", first ? "" : "|");
16368     }
16369   else
16370     fputs ("0", stdout);
16371 }
16372 
16373 /* Display's the value of TAG at location P.  If TAG is
16374    greater than 0 it is assumed to be an unknown tag, and
16375    a message is printed to this effect.  Otherwise it is
16376    assumed that a message has already been printed.
16377 
16378    If the bottom bit of TAG is set it assumed to have a
16379    string value, otherwise it is assumed to have an integer
16380    value.
16381 
16382    Returns an updated P pointing to the first unread byte
16383    beyond the end of TAG's value.
16384 
16385    Reads at or beyond END will not be made.  */
16386 
16387 static unsigned char *
16388 display_tag_value (signed int tag,
16389 		   unsigned char * p,
16390 		   const unsigned char * const end)
16391 {
16392   unsigned long val;
16393 
16394   if (tag > 0)
16395     printf ("  Tag_unknown_%d: ", tag);
16396 
16397   if (p >= end)
16398     {
16399       warn (_("<corrupt tag>\n"));
16400     }
16401   else if (tag & 1)
16402     {
16403       /* PR 17531 file: 027-19978-0.004.  */
16404       size_t maxlen = (end - p) - 1;
16405 
16406       putchar ('"');
16407       if (maxlen > 0)
16408 	{
16409 	  print_symbol ((int) maxlen, (const char *) p);
16410 	  p += strnlen ((char *) p, maxlen) + 1;
16411 	}
16412       else
16413 	{
16414 	  printf (_("<corrupt string tag>"));
16415 	  p = (unsigned char *) end;
16416 	}
16417       printf ("\"\n");
16418     }
16419   else
16420     {
16421       READ_ULEB (val, p, end);
16422       printf ("%ld (0x%lx)\n", val, val);
16423     }
16424 
16425   assert (p <= end);
16426   return p;
16427 }
16428 
16429 /* ARC ABI attributes section.  */
16430 
16431 static unsigned char *
16432 display_arc_attribute (unsigned char * p,
16433 		       const unsigned char * const end)
16434 {
16435   unsigned int tag;
16436   unsigned int val;
16437 
16438   READ_ULEB (tag, p, end);
16439 
16440   switch (tag)
16441     {
16442     case Tag_ARC_PCS_config:
16443       READ_ULEB (val, p, end);
16444       printf ("  Tag_ARC_PCS_config: ");
16445       switch (val)
16446 	{
16447 	case 0:
16448 	  printf (_("Absent/Non standard\n"));
16449 	  break;
16450 	case 1:
16451 	  printf (_("Bare metal/mwdt\n"));
16452 	  break;
16453 	case 2:
16454 	  printf (_("Bare metal/newlib\n"));
16455 	  break;
16456 	case 3:
16457 	  printf (_("Linux/uclibc\n"));
16458 	  break;
16459 	case 4:
16460 	  printf (_("Linux/glibc\n"));
16461 	  break;
16462 	default:
16463 	  printf (_("Unknown\n"));
16464 	  break;
16465 	}
16466       break;
16467 
16468     case Tag_ARC_CPU_base:
16469       READ_ULEB (val, p, end);
16470       printf ("  Tag_ARC_CPU_base: ");
16471       switch (val)
16472 	{
16473 	default:
16474 	case TAG_CPU_NONE:
16475 	  printf (_("Absent\n"));
16476 	  break;
16477 	case TAG_CPU_ARC6xx:
16478 	  printf ("ARC6xx\n");
16479 	  break;
16480 	case TAG_CPU_ARC7xx:
16481 	  printf ("ARC7xx\n");
16482 	  break;
16483 	case TAG_CPU_ARCEM:
16484 	  printf ("ARCEM\n");
16485 	  break;
16486 	case TAG_CPU_ARCHS:
16487 	  printf ("ARCHS\n");
16488 	  break;
16489 	}
16490       break;
16491 
16492     case Tag_ARC_CPU_variation:
16493       READ_ULEB (val, p, end);
16494       printf ("  Tag_ARC_CPU_variation: ");
16495       switch (val)
16496 	{
16497 	default:
16498 	  if (val > 0 && val < 16)
16499 	      printf ("Core%d\n", val);
16500 	  else
16501 	      printf ("Unknown\n");
16502 	  break;
16503 
16504 	case 0:
16505 	  printf (_("Absent\n"));
16506 	  break;
16507 	}
16508       break;
16509 
16510     case Tag_ARC_CPU_name:
16511       printf ("  Tag_ARC_CPU_name: ");
16512       p = display_tag_value (-1, p, end);
16513       break;
16514 
16515     case Tag_ARC_ABI_rf16:
16516       READ_ULEB (val, p, end);
16517       printf ("  Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16518       break;
16519 
16520     case Tag_ARC_ABI_osver:
16521       READ_ULEB (val, p, end);
16522       printf ("  Tag_ARC_ABI_osver: v%d\n", val);
16523       break;
16524 
16525     case Tag_ARC_ABI_pic:
16526     case Tag_ARC_ABI_sda:
16527       READ_ULEB (val, p, end);
16528       printf (tag == Tag_ARC_ABI_sda ? "  Tag_ARC_ABI_sda: "
16529 	      : "  Tag_ARC_ABI_pic: ");
16530       switch (val)
16531 	{
16532 	case 0:
16533 	  printf (_("Absent\n"));
16534 	  break;
16535 	case 1:
16536 	  printf ("MWDT\n");
16537 	  break;
16538 	case 2:
16539 	  printf ("GNU\n");
16540 	  break;
16541 	default:
16542 	  printf (_("Unknown\n"));
16543 	  break;
16544 	}
16545       break;
16546 
16547     case Tag_ARC_ABI_tls:
16548       READ_ULEB (val, p, end);
16549       printf ("  Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16550       break;
16551 
16552     case Tag_ARC_ABI_enumsize:
16553       READ_ULEB (val, p, end);
16554       printf ("  Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16555 	      _("smallest"));
16556       break;
16557 
16558     case Tag_ARC_ABI_exceptions:
16559       READ_ULEB (val, p, end);
16560       printf ("  Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16561 	      : _("default"));
16562       break;
16563 
16564     case Tag_ARC_ABI_double_size:
16565       READ_ULEB (val, p, end);
16566       printf ("  Tag_ARC_ABI_double_size: %d\n", val);
16567       break;
16568 
16569     case Tag_ARC_ISA_config:
16570       printf ("  Tag_ARC_ISA_config: ");
16571       p = display_tag_value (-1, p, end);
16572       break;
16573 
16574     case Tag_ARC_ISA_apex:
16575       printf ("  Tag_ARC_ISA_apex: ");
16576       p = display_tag_value (-1, p, end);
16577       break;
16578 
16579     case Tag_ARC_ISA_mpy_option:
16580       READ_ULEB (val, p, end);
16581       printf ("  Tag_ARC_ISA_mpy_option: %d\n", val);
16582       break;
16583 
16584     case Tag_ARC_ATR_version:
16585       READ_ULEB (val, p, end);
16586       printf ("  Tag_ARC_ATR_version: %d\n", val);
16587       break;
16588 
16589     default:
16590       return display_tag_value (tag & 1, p, end);
16591     }
16592 
16593   return p;
16594 }
16595 
16596 /* ARM EABI attributes section.  */
16597 typedef struct
16598 {
16599   unsigned int tag;
16600   const char * name;
16601   /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup.  */
16602   unsigned int type;
16603   const char *const *table;
16604 } arm_attr_public_tag;
16605 
16606 static const char *const arm_attr_tag_CPU_arch[] =
16607   {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16608    "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16609    "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16610    "v8.1-M.mainline", "v9"};
16611 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16612 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16613   {"No", "Thumb-1", "Thumb-2", "Yes"};
16614 static const char *const arm_attr_tag_FP_arch[] =
16615   {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16616    "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16617 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16618 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16619   {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16620    "NEON for ARMv8.1"};
16621 static const char *const arm_attr_tag_PCS_config[] =
16622   {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16623    "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16624 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16625   {"V6", "SB", "TLS", "Unused"};
16626 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16627   {"Absolute", "PC-relative", "SB-relative", "None"};
16628 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16629   {"Absolute", "PC-relative", "None"};
16630 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16631   {"None", "direct", "GOT-indirect"};
16632 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16633   {"None", "??? 1", "2", "??? 3", "4"};
16634 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16635 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16636   {"Unused", "Needed", "Sign only"};
16637 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16638 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16639 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16640   {"Unused", "Finite", "RTABI", "IEEE 754"};
16641 static const char *const arm_attr_tag_ABI_enum_size[] =
16642   {"Unused", "small", "int", "forced to int"};
16643 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16644   {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16645 static const char *const arm_attr_tag_ABI_VFP_args[] =
16646   {"AAPCS", "VFP registers", "custom", "compatible"};
16647 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16648   {"AAPCS", "WMMX registers", "custom"};
16649 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16650   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16651     "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16652 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16653   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16654     "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16655 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16656 static const char *const arm_attr_tag_FP_HP_extension[] =
16657   {"Not Allowed", "Allowed"};
16658 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16659   {"None", "IEEE 754", "Alternative Format"};
16660 static const char *const arm_attr_tag_DSP_extension[] =
16661   {"Follow architecture", "Allowed"};
16662 static const char *const arm_attr_tag_MPextension_use[] =
16663   {"Not Allowed", "Allowed"};
16664 static const char *const arm_attr_tag_DIV_use[] =
16665   {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16666     "Allowed in v7-A with integer division extension"};
16667 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16668 static const char *const arm_attr_tag_Virtualization_use[] =
16669   {"Not Allowed", "TrustZone", "Virtualization Extensions",
16670     "TrustZone and Virtualization Extensions"};
16671 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16672   {"Not Allowed", "Allowed"};
16673 
16674 static const char *const arm_attr_tag_MVE_arch[] =
16675   {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16676 
16677 static const char * arm_attr_tag_PAC_extension[] =
16678   {"No PAC/AUT instructions",
16679    "PAC/AUT instructions permitted in the NOP space",
16680    "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16681 
16682 static const char * arm_attr_tag_BTI_extension[] =
16683   {"BTI instructions not permitted",
16684    "BTI instructions permitted in the NOP space",
16685    "BTI instructions permitted in the NOP and in the non-NOP space"};
16686 
16687 static const char * arm_attr_tag_BTI_use[] =
16688   {"Compiled without branch target enforcement",
16689    "Compiled with branch target enforcement"};
16690 
16691 static const char * arm_attr_tag_PACRET_use[] =
16692   {"Compiled without return address signing and authentication",
16693    "Compiled with return address signing and authentication"};
16694 
16695 #define LOOKUP(id, name) \
16696   {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16697 static arm_attr_public_tag arm_attr_public_tags[] =
16698 {
16699   {4, "CPU_raw_name", 1, NULL},
16700   {5, "CPU_name", 1, NULL},
16701   LOOKUP(6, CPU_arch),
16702   {7, "CPU_arch_profile", 0, NULL},
16703   LOOKUP(8, ARM_ISA_use),
16704   LOOKUP(9, THUMB_ISA_use),
16705   LOOKUP(10, FP_arch),
16706   LOOKUP(11, WMMX_arch),
16707   LOOKUP(12, Advanced_SIMD_arch),
16708   LOOKUP(13, PCS_config),
16709   LOOKUP(14, ABI_PCS_R9_use),
16710   LOOKUP(15, ABI_PCS_RW_data),
16711   LOOKUP(16, ABI_PCS_RO_data),
16712   LOOKUP(17, ABI_PCS_GOT_use),
16713   LOOKUP(18, ABI_PCS_wchar_t),
16714   LOOKUP(19, ABI_FP_rounding),
16715   LOOKUP(20, ABI_FP_denormal),
16716   LOOKUP(21, ABI_FP_exceptions),
16717   LOOKUP(22, ABI_FP_user_exceptions),
16718   LOOKUP(23, ABI_FP_number_model),
16719   {24, "ABI_align_needed", 0, NULL},
16720   {25, "ABI_align_preserved", 0, NULL},
16721   LOOKUP(26, ABI_enum_size),
16722   LOOKUP(27, ABI_HardFP_use),
16723   LOOKUP(28, ABI_VFP_args),
16724   LOOKUP(29, ABI_WMMX_args),
16725   LOOKUP(30, ABI_optimization_goals),
16726   LOOKUP(31, ABI_FP_optimization_goals),
16727   {32, "compatibility", 0, NULL},
16728   LOOKUP(34, CPU_unaligned_access),
16729   LOOKUP(36, FP_HP_extension),
16730   LOOKUP(38, ABI_FP_16bit_format),
16731   LOOKUP(42, MPextension_use),
16732   LOOKUP(44, DIV_use),
16733   LOOKUP(46, DSP_extension),
16734   LOOKUP(48, MVE_arch),
16735   LOOKUP(50, PAC_extension),
16736   LOOKUP(52, BTI_extension),
16737   LOOKUP(74, BTI_use),
16738   LOOKUP(76, PACRET_use),
16739   {64, "nodefaults", 0, NULL},
16740   {65, "also_compatible_with", 0, NULL},
16741   LOOKUP(66, T2EE_use),
16742   {67, "conformance", 1, NULL},
16743   LOOKUP(68, Virtualization_use),
16744   LOOKUP(70, MPextension_use_legacy)
16745 };
16746 #undef LOOKUP
16747 
16748 static unsigned char *
16749 display_arm_attribute (unsigned char * p,
16750 		       const unsigned char * const end)
16751 {
16752   unsigned int tag;
16753   unsigned int val;
16754   arm_attr_public_tag * attr;
16755   unsigned i;
16756   unsigned int type;
16757 
16758   READ_ULEB (tag, p, end);
16759   attr = NULL;
16760   for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16761     {
16762       if (arm_attr_public_tags[i].tag == tag)
16763 	{
16764 	  attr = &arm_attr_public_tags[i];
16765 	  break;
16766 	}
16767     }
16768 
16769   if (attr)
16770     {
16771       printf ("  Tag_%s: ", attr->name);
16772       switch (attr->type)
16773 	{
16774 	case 0:
16775 	  switch (tag)
16776 	    {
16777 	    case 7: /* Tag_CPU_arch_profile.  */
16778 	      READ_ULEB (val, p, end);
16779 	      switch (val)
16780 		{
16781 		case 0: printf (_("None\n")); break;
16782 		case 'A': printf (_("Application\n")); break;
16783 		case 'R': printf (_("Realtime\n")); break;
16784 		case 'M': printf (_("Microcontroller\n")); break;
16785 		case 'S': printf (_("Application or Realtime\n")); break;
16786 		default: printf ("??? (%d)\n", val); break;
16787 		}
16788 	      break;
16789 
16790 	    case 24: /* Tag_align_needed.  */
16791 	      READ_ULEB (val, p, end);
16792 	      switch (val)
16793 		{
16794 		case 0: printf (_("None\n")); break;
16795 		case 1: printf (_("8-byte\n")); break;
16796 		case 2: printf (_("4-byte\n")); break;
16797 		case 3: printf ("??? 3\n"); break;
16798 		default:
16799 		  if (val <= 12)
16800 		    printf (_("8-byte and up to %d-byte extended\n"),
16801 			    1 << val);
16802 		  else
16803 		    printf ("??? (%d)\n", val);
16804 		  break;
16805 		}
16806 	      break;
16807 
16808 	    case 25: /* Tag_align_preserved.  */
16809 	      READ_ULEB (val, p, end);
16810 	      switch (val)
16811 		{
16812 		case 0: printf (_("None\n")); break;
16813 		case 1: printf (_("8-byte, except leaf SP\n")); break;
16814 		case 2: printf (_("8-byte\n")); break;
16815 		case 3: printf ("??? 3\n"); break;
16816 		default:
16817 		  if (val <= 12)
16818 		    printf (_("8-byte and up to %d-byte extended\n"),
16819 			    1 << val);
16820 		  else
16821 		    printf ("??? (%d)\n", val);
16822 		  break;
16823 		}
16824 	      break;
16825 
16826 	    case 32: /* Tag_compatibility.  */
16827 	      {
16828 		READ_ULEB (val, p, end);
16829 		printf (_("flag = %d, vendor = "), val);
16830 		if (p < end - 1)
16831 		  {
16832 		    size_t maxlen = (end - p) - 1;
16833 
16834 		    print_symbol ((int) maxlen, (const char *) p);
16835 		    p += strnlen ((char *) p, maxlen) + 1;
16836 		  }
16837 		else
16838 		  {
16839 		    printf (_("<corrupt>"));
16840 		    p = (unsigned char *) end;
16841 		  }
16842 		putchar ('\n');
16843 	      }
16844 	      break;
16845 
16846 	    case 64: /* Tag_nodefaults.  */
16847 	      /* PR 17531: file: 001-505008-0.01.  */
16848 	      if (p < end)
16849 		p++;
16850 	      printf (_("True\n"));
16851 	      break;
16852 
16853 	    case 65: /* Tag_also_compatible_with.  */
16854 	      READ_ULEB (val, p, end);
16855 	      if (val == 6 /* Tag_CPU_arch.  */)
16856 		{
16857 		  READ_ULEB (val, p, end);
16858 		  if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16859 		    printf ("??? (%d)\n", val);
16860 		  else
16861 		    printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16862 		}
16863 	      else
16864 		printf ("???\n");
16865 	      while (p < end && *(p++) != '\0' /* NUL terminator.  */)
16866 		;
16867 	      break;
16868 
16869 	    default:
16870 	      printf (_("<unknown: %d>\n"), tag);
16871 	      break;
16872 	    }
16873 	  return p;
16874 
16875 	case 1:
16876 	  return display_tag_value (-1, p, end);
16877 	case 2:
16878 	  return display_tag_value (0, p, end);
16879 
16880 	default:
16881 	  assert (attr->type & 0x80);
16882 	  READ_ULEB (val, p, end);
16883 	  type = attr->type & 0x7f;
16884 	  if (val >= type)
16885 	    printf ("??? (%d)\n", val);
16886 	  else
16887 	    printf ("%s\n", attr->table[val]);
16888 	  return p;
16889 	}
16890     }
16891 
16892   return display_tag_value (tag, p, end);
16893 }
16894 
16895 static unsigned char *
16896 display_gnu_attribute (unsigned char * p,
16897 		       unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16898 		       const unsigned char * const end)
16899 {
16900   unsigned int tag;
16901   unsigned int val;
16902 
16903   READ_ULEB (tag, p, end);
16904 
16905   /* Tag_compatibility is the only generic GNU attribute defined at
16906      present.  */
16907   if (tag == 32)
16908     {
16909       READ_ULEB (val, p, end);
16910 
16911       printf (_("flag = %d, vendor = "), val);
16912       if (p == end)
16913 	{
16914 	  printf (_("<corrupt>\n"));
16915 	  warn (_("corrupt vendor attribute\n"));
16916 	}
16917       else
16918 	{
16919 	  if (p < end - 1)
16920 	    {
16921 	      size_t maxlen = (end - p) - 1;
16922 
16923 	      print_symbol ((int) maxlen, (const char *) p);
16924 	      p += strnlen ((char *) p, maxlen) + 1;
16925 	    }
16926 	  else
16927 	    {
16928 	      printf (_("<corrupt>"));
16929 	      p = (unsigned char *) end;
16930 	    }
16931 	  putchar ('\n');
16932 	}
16933       return p;
16934     }
16935 
16936   if ((tag & 2) == 0 && display_proc_gnu_attribute)
16937     return display_proc_gnu_attribute (p, tag, end);
16938 
16939   return display_tag_value (tag, p, end);
16940 }
16941 
16942 static unsigned char *
16943 display_m68k_gnu_attribute (unsigned char * p,
16944 			    unsigned int tag,
16945 			    const unsigned char * const end)
16946 {
16947   unsigned int val;
16948 
16949   if (tag == Tag_GNU_M68K_ABI_FP)
16950     {
16951       printf ("  Tag_GNU_M68K_ABI_FP: ");
16952       if (p == end)
16953 	{
16954 	  printf (_("<corrupt>\n"));
16955 	  return p;
16956 	}
16957       READ_ULEB (val, p, end);
16958 
16959       if (val > 3)
16960 	printf ("(%#x), ", val);
16961 
16962       switch (val & 3)
16963 	{
16964 	case 0:
16965 	  printf (_("unspecified hard/soft float\n"));
16966 	  break;
16967 	case 1:
16968 	  printf (_("hard float\n"));
16969 	  break;
16970 	case 2:
16971 	  printf (_("soft float\n"));
16972 	  break;
16973 	}
16974       return p;
16975     }
16976 
16977   return display_tag_value (tag & 1, p, end);
16978 }
16979 
16980 static unsigned char *
16981 display_power_gnu_attribute (unsigned char * p,
16982 			     unsigned int tag,
16983 			     const unsigned char * const end)
16984 {
16985   unsigned int val;
16986 
16987   if (tag == Tag_GNU_Power_ABI_FP)
16988     {
16989       printf ("  Tag_GNU_Power_ABI_FP: ");
16990       if (p == end)
16991 	{
16992 	  printf (_("<corrupt>\n"));
16993 	  return p;
16994 	}
16995       READ_ULEB (val, p, end);
16996 
16997       if (val > 15)
16998 	printf ("(%#x), ", val);
16999 
17000       switch (val & 3)
17001 	{
17002 	case 0:
17003 	  printf (_("unspecified hard/soft float, "));
17004 	  break;
17005 	case 1:
17006 	  printf (_("hard float, "));
17007 	  break;
17008 	case 2:
17009 	  printf (_("soft float, "));
17010 	  break;
17011 	case 3:
17012 	  printf (_("single-precision hard float, "));
17013 	  break;
17014 	}
17015 
17016       switch (val & 0xC)
17017 	{
17018 	case 0:
17019 	  printf (_("unspecified long double\n"));
17020 	  break;
17021 	case 4:
17022 	  printf (_("128-bit IBM long double\n"));
17023 	  break;
17024 	case 8:
17025 	  printf (_("64-bit long double\n"));
17026 	  break;
17027 	case 12:
17028 	  printf (_("128-bit IEEE long double\n"));
17029 	  break;
17030 	}
17031       return p;
17032     }
17033 
17034   if (tag == Tag_GNU_Power_ABI_Vector)
17035     {
17036       printf ("  Tag_GNU_Power_ABI_Vector: ");
17037       if (p == end)
17038 	{
17039 	  printf (_("<corrupt>\n"));
17040 	  return p;
17041 	}
17042       READ_ULEB (val, p, end);
17043 
17044       if (val > 3)
17045 	printf ("(%#x), ", val);
17046 
17047       switch (val & 3)
17048 	{
17049 	case 0:
17050 	  printf (_("unspecified\n"));
17051 	  break;
17052 	case 1:
17053 	  printf (_("generic\n"));
17054 	  break;
17055 	case 2:
17056 	  printf ("AltiVec\n");
17057 	  break;
17058 	case 3:
17059 	  printf ("SPE\n");
17060 	  break;
17061 	}
17062       return p;
17063     }
17064 
17065   if (tag == Tag_GNU_Power_ABI_Struct_Return)
17066     {
17067       printf ("  Tag_GNU_Power_ABI_Struct_Return: ");
17068       if (p == end)
17069 	{
17070 	  printf (_("<corrupt>\n"));
17071 	  return p;
17072 	}
17073       READ_ULEB (val, p, end);
17074 
17075       if (val > 2)
17076 	printf ("(%#x), ", val);
17077 
17078       switch (val & 3)
17079 	{
17080 	case 0:
17081 	  printf (_("unspecified\n"));
17082 	  break;
17083 	case 1:
17084 	  printf ("r3/r4\n");
17085 	  break;
17086 	case 2:
17087 	  printf (_("memory\n"));
17088 	  break;
17089 	case 3:
17090 	  printf ("???\n");
17091 	  break;
17092 	}
17093       return p;
17094     }
17095 
17096   return display_tag_value (tag & 1, p, end);
17097 }
17098 
17099 static unsigned char *
17100 display_s390_gnu_attribute (unsigned char * p,
17101 			    unsigned int tag,
17102 			    const unsigned char * const end)
17103 {
17104   unsigned int val;
17105 
17106   if (tag == Tag_GNU_S390_ABI_Vector)
17107     {
17108       printf ("  Tag_GNU_S390_ABI_Vector: ");
17109       READ_ULEB (val, p, end);
17110 
17111       switch (val)
17112 	{
17113 	case 0:
17114 	  printf (_("any\n"));
17115 	  break;
17116 	case 1:
17117 	  printf (_("software\n"));
17118 	  break;
17119 	case 2:
17120 	  printf (_("hardware\n"));
17121 	  break;
17122 	default:
17123 	  printf ("??? (%d)\n", val);
17124 	  break;
17125 	}
17126       return p;
17127    }
17128 
17129   return display_tag_value (tag & 1, p, end);
17130 }
17131 
17132 static void
17133 display_sparc_hwcaps (unsigned int mask)
17134 {
17135   if (mask)
17136     {
17137       bool first = true;
17138 
17139       if (mask & ELF_SPARC_HWCAP_MUL32)
17140 	fputs ("mul32", stdout), first = false;
17141       if (mask & ELF_SPARC_HWCAP_DIV32)
17142 	printf ("%sdiv32", first ? "" : "|"), first = false;
17143       if (mask & ELF_SPARC_HWCAP_FSMULD)
17144 	printf ("%sfsmuld", first ? "" : "|"), first = false;
17145       if (mask & ELF_SPARC_HWCAP_V8PLUS)
17146 	printf ("%sv8plus", first ? "" : "|"), first = false;
17147       if (mask & ELF_SPARC_HWCAP_POPC)
17148 	printf ("%spopc", first ? "" : "|"), first = false;
17149       if (mask & ELF_SPARC_HWCAP_VIS)
17150 	printf ("%svis", first ? "" : "|"), first = false;
17151       if (mask & ELF_SPARC_HWCAP_VIS2)
17152 	printf ("%svis2", first ? "" : "|"), first = false;
17153       if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
17154 	printf ("%sASIBlkInit", first ? "" : "|"), first = false;
17155       if (mask & ELF_SPARC_HWCAP_FMAF)
17156 	printf ("%sfmaf", first ? "" : "|"), first = false;
17157       if (mask & ELF_SPARC_HWCAP_VIS3)
17158 	printf ("%svis3", first ? "" : "|"), first = false;
17159       if (mask & ELF_SPARC_HWCAP_HPC)
17160 	printf ("%shpc", first ? "" : "|"), first = false;
17161       if (mask & ELF_SPARC_HWCAP_RANDOM)
17162 	printf ("%srandom", first ? "" : "|"), first = false;
17163       if (mask & ELF_SPARC_HWCAP_TRANS)
17164 	printf ("%strans", first ? "" : "|"), first = false;
17165       if (mask & ELF_SPARC_HWCAP_FJFMAU)
17166 	printf ("%sfjfmau", first ? "" : "|"), first = false;
17167       if (mask & ELF_SPARC_HWCAP_IMA)
17168 	printf ("%sima", first ? "" : "|"), first = false;
17169       if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
17170 	printf ("%scspare", first ? "" : "|"), first = false;
17171     }
17172   else
17173     fputc ('0', stdout);
17174   fputc ('\n', stdout);
17175 }
17176 
17177 static void
17178 display_sparc_hwcaps2 (unsigned int mask)
17179 {
17180   if (mask)
17181     {
17182       bool first = true;
17183 
17184       if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
17185 	fputs ("fjathplus", stdout), first = false;
17186       if (mask & ELF_SPARC_HWCAP2_VIS3B)
17187 	printf ("%svis3b", first ? "" : "|"), first = false;
17188       if (mask & ELF_SPARC_HWCAP2_ADP)
17189 	printf ("%sadp", first ? "" : "|"), first = false;
17190       if (mask & ELF_SPARC_HWCAP2_SPARC5)
17191 	printf ("%ssparc5", first ? "" : "|"), first = false;
17192       if (mask & ELF_SPARC_HWCAP2_MWAIT)
17193 	printf ("%smwait", first ? "" : "|"), first = false;
17194       if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17195 	printf ("%sxmpmul", first ? "" : "|"), first = false;
17196       if (mask & ELF_SPARC_HWCAP2_XMONT)
17197 	printf ("%sxmont2", first ? "" : "|"), first = false;
17198       if (mask & ELF_SPARC_HWCAP2_NSEC)
17199 	printf ("%snsec", first ? "" : "|"), first = false;
17200       if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17201 	printf ("%sfjathhpc", first ? "" : "|"), first = false;
17202       if (mask & ELF_SPARC_HWCAP2_FJDES)
17203 	printf ("%sfjdes", first ? "" : "|"), first = false;
17204       if (mask & ELF_SPARC_HWCAP2_FJAES)
17205 	printf ("%sfjaes", first ? "" : "|"), first = false;
17206     }
17207   else
17208     fputc ('0', stdout);
17209   fputc ('\n', stdout);
17210 }
17211 
17212 static unsigned char *
17213 display_sparc_gnu_attribute (unsigned char * p,
17214 			     unsigned int tag,
17215 			     const unsigned char * const end)
17216 {
17217   unsigned int val;
17218 
17219   if (tag == Tag_GNU_Sparc_HWCAPS)
17220     {
17221       READ_ULEB (val, p, end);
17222       printf ("  Tag_GNU_Sparc_HWCAPS: ");
17223       display_sparc_hwcaps (val);
17224       return p;
17225     }
17226   if (tag == Tag_GNU_Sparc_HWCAPS2)
17227     {
17228       READ_ULEB (val, p, end);
17229       printf ("  Tag_GNU_Sparc_HWCAPS2: ");
17230       display_sparc_hwcaps2 (val);
17231       return p;
17232     }
17233 
17234   return display_tag_value (tag, p, end);
17235 }
17236 
17237 static void
17238 print_mips_fp_abi_value (unsigned int val)
17239 {
17240   switch (val)
17241     {
17242     case Val_GNU_MIPS_ABI_FP_ANY:
17243       printf (_("Hard or soft float\n"));
17244       break;
17245     case Val_GNU_MIPS_ABI_FP_DOUBLE:
17246       printf (_("Hard float (double precision)\n"));
17247       break;
17248     case Val_GNU_MIPS_ABI_FP_SINGLE:
17249       printf (_("Hard float (single precision)\n"));
17250       break;
17251     case Val_GNU_MIPS_ABI_FP_SOFT:
17252       printf (_("Soft float\n"));
17253       break;
17254     case Val_GNU_MIPS_ABI_FP_OLD_64:
17255       printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
17256       break;
17257     case Val_GNU_MIPS_ABI_FP_XX:
17258       printf (_("Hard float (32-bit CPU, Any FPU)\n"));
17259       break;
17260     case Val_GNU_MIPS_ABI_FP_64:
17261       printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
17262       break;
17263     case Val_GNU_MIPS_ABI_FP_64A:
17264       printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
17265       break;
17266     case Val_GNU_MIPS_ABI_FP_NAN2008:
17267       printf (_("NaN 2008 compatibility\n"));
17268       break;
17269     default:
17270       printf ("??? (%d)\n", val);
17271       break;
17272     }
17273 }
17274 
17275 static unsigned char *
17276 display_mips_gnu_attribute (unsigned char * p,
17277 			    unsigned int tag,
17278 			    const unsigned char * const end)
17279 {
17280   if (tag == Tag_GNU_MIPS_ABI_FP)
17281     {
17282       unsigned int val;
17283 
17284       printf ("  Tag_GNU_MIPS_ABI_FP: ");
17285       READ_ULEB (val, p, end);
17286       print_mips_fp_abi_value (val);
17287       return p;
17288    }
17289 
17290   if (tag == Tag_GNU_MIPS_ABI_MSA)
17291     {
17292       unsigned int val;
17293 
17294       printf ("  Tag_GNU_MIPS_ABI_MSA: ");
17295       READ_ULEB (val, p, end);
17296 
17297       switch (val)
17298 	{
17299 	case Val_GNU_MIPS_ABI_MSA_ANY:
17300 	  printf (_("Any MSA or not\n"));
17301 	  break;
17302 	case Val_GNU_MIPS_ABI_MSA_128:
17303 	  printf (_("128-bit MSA\n"));
17304 	  break;
17305 	default:
17306 	  printf ("??? (%d)\n", val);
17307 	  break;
17308 	}
17309       return p;
17310     }
17311 
17312   return display_tag_value (tag & 1, p, end);
17313 }
17314 
17315 static unsigned char *
17316 display_tic6x_attribute (unsigned char * p,
17317 			 const unsigned char * const end)
17318 {
17319   unsigned int tag;
17320   unsigned int val;
17321 
17322   READ_ULEB (tag, p, end);
17323 
17324   switch (tag)
17325     {
17326     case Tag_ISA:
17327       printf ("  Tag_ISA: ");
17328       READ_ULEB (val, p, end);
17329 
17330       switch (val)
17331 	{
17332 	case C6XABI_Tag_ISA_none:
17333 	  printf (_("None\n"));
17334 	  break;
17335 	case C6XABI_Tag_ISA_C62X:
17336 	  printf ("C62x\n");
17337 	  break;
17338 	case C6XABI_Tag_ISA_C67X:
17339 	  printf ("C67x\n");
17340 	  break;
17341 	case C6XABI_Tag_ISA_C67XP:
17342 	  printf ("C67x+\n");
17343 	  break;
17344 	case C6XABI_Tag_ISA_C64X:
17345 	  printf ("C64x\n");
17346 	  break;
17347 	case C6XABI_Tag_ISA_C64XP:
17348 	  printf ("C64x+\n");
17349 	  break;
17350 	case C6XABI_Tag_ISA_C674X:
17351 	  printf ("C674x\n");
17352 	  break;
17353 	default:
17354 	  printf ("??? (%d)\n", val);
17355 	  break;
17356 	}
17357       return p;
17358 
17359     case Tag_ABI_wchar_t:
17360       printf ("  Tag_ABI_wchar_t: ");
17361       READ_ULEB (val, p, end);
17362       switch (val)
17363 	{
17364 	case 0:
17365 	  printf (_("Not used\n"));
17366 	  break;
17367 	case 1:
17368 	  printf (_("2 bytes\n"));
17369 	  break;
17370 	case 2:
17371 	  printf (_("4 bytes\n"));
17372 	  break;
17373 	default:
17374 	  printf ("??? (%d)\n", val);
17375 	  break;
17376 	}
17377       return p;
17378 
17379     case Tag_ABI_stack_align_needed:
17380       printf ("  Tag_ABI_stack_align_needed: ");
17381       READ_ULEB (val, p, end);
17382       switch (val)
17383 	{
17384 	case 0:
17385 	  printf (_("8-byte\n"));
17386 	  break;
17387 	case 1:
17388 	  printf (_("16-byte\n"));
17389 	  break;
17390 	default:
17391 	  printf ("??? (%d)\n", val);
17392 	  break;
17393 	}
17394       return p;
17395 
17396     case Tag_ABI_stack_align_preserved:
17397       READ_ULEB (val, p, end);
17398       printf ("  Tag_ABI_stack_align_preserved: ");
17399       switch (val)
17400 	{
17401 	case 0:
17402 	  printf (_("8-byte\n"));
17403 	  break;
17404 	case 1:
17405 	  printf (_("16-byte\n"));
17406 	  break;
17407 	default:
17408 	  printf ("??? (%d)\n", val);
17409 	  break;
17410 	}
17411       return p;
17412 
17413     case Tag_ABI_DSBT:
17414       READ_ULEB (val, p, end);
17415       printf ("  Tag_ABI_DSBT: ");
17416       switch (val)
17417 	{
17418 	case 0:
17419 	  printf (_("DSBT addressing not used\n"));
17420 	  break;
17421 	case 1:
17422 	  printf (_("DSBT addressing used\n"));
17423 	  break;
17424 	default:
17425 	  printf ("??? (%d)\n", val);
17426 	  break;
17427 	}
17428       return p;
17429 
17430     case Tag_ABI_PID:
17431       READ_ULEB (val, p, end);
17432       printf ("  Tag_ABI_PID: ");
17433       switch (val)
17434 	{
17435 	case 0:
17436 	  printf (_("Data addressing position-dependent\n"));
17437 	  break;
17438 	case 1:
17439 	  printf (_("Data addressing position-independent, GOT near DP\n"));
17440 	  break;
17441 	case 2:
17442 	  printf (_("Data addressing position-independent, GOT far from DP\n"));
17443 	  break;
17444 	default:
17445 	  printf ("??? (%d)\n", val);
17446 	  break;
17447 	}
17448       return p;
17449 
17450     case Tag_ABI_PIC:
17451       READ_ULEB (val, p, end);
17452       printf ("  Tag_ABI_PIC: ");
17453       switch (val)
17454 	{
17455 	case 0:
17456 	  printf (_("Code addressing position-dependent\n"));
17457 	  break;
17458 	case 1:
17459 	  printf (_("Code addressing position-independent\n"));
17460 	  break;
17461 	default:
17462 	  printf ("??? (%d)\n", val);
17463 	  break;
17464 	}
17465       return p;
17466 
17467     case Tag_ABI_array_object_alignment:
17468       READ_ULEB (val, p, end);
17469       printf ("  Tag_ABI_array_object_alignment: ");
17470       switch (val)
17471 	{
17472 	case 0:
17473 	  printf (_("8-byte\n"));
17474 	  break;
17475 	case 1:
17476 	  printf (_("4-byte\n"));
17477 	  break;
17478 	case 2:
17479 	  printf (_("16-byte\n"));
17480 	  break;
17481 	default:
17482 	  printf ("??? (%d)\n", val);
17483 	  break;
17484 	}
17485       return p;
17486 
17487     case Tag_ABI_array_object_align_expected:
17488       READ_ULEB (val, p, end);
17489       printf ("  Tag_ABI_array_object_align_expected: ");
17490       switch (val)
17491 	{
17492 	case 0:
17493 	  printf (_("8-byte\n"));
17494 	  break;
17495 	case 1:
17496 	  printf (_("4-byte\n"));
17497 	  break;
17498 	case 2:
17499 	  printf (_("16-byte\n"));
17500 	  break;
17501 	default:
17502 	  printf ("??? (%d)\n", val);
17503 	  break;
17504 	}
17505       return p;
17506 
17507     case Tag_ABI_compatibility:
17508       {
17509 	READ_ULEB (val, p, end);
17510 	printf ("  Tag_ABI_compatibility: ");
17511 	printf (_("flag = %d, vendor = "), val);
17512 	if (p < end - 1)
17513 	  {
17514 	    size_t maxlen = (end - p) - 1;
17515 
17516 	    print_symbol ((int) maxlen, (const char *) p);
17517 	    p += strnlen ((char *) p, maxlen) + 1;
17518 	  }
17519 	else
17520 	  {
17521 	    printf (_("<corrupt>"));
17522 	    p = (unsigned char *) end;
17523 	  }
17524 	putchar ('\n');
17525 	return p;
17526       }
17527 
17528     case Tag_ABI_conformance:
17529       {
17530 	printf ("  Tag_ABI_conformance: \"");
17531 	if (p < end - 1)
17532 	  {
17533 	    size_t maxlen = (end - p) - 1;
17534 
17535 	    print_symbol ((int) maxlen, (const char *) p);
17536 	    p += strnlen ((char *) p, maxlen) + 1;
17537 	  }
17538 	else
17539 	  {
17540 	    printf (_("<corrupt>"));
17541 	    p = (unsigned char *) end;
17542 	  }
17543 	printf ("\"\n");
17544 	return p;
17545       }
17546     }
17547 
17548   return display_tag_value (tag, p, end);
17549 }
17550 
17551 static void
17552 display_raw_attribute (unsigned char * p, unsigned char const * const end)
17553 {
17554   unsigned long addr = 0;
17555   size_t bytes = end - p;
17556 
17557   assert (end >= p);
17558   while (bytes)
17559     {
17560       int j;
17561       int k;
17562       int lbytes = (bytes > 16 ? 16 : bytes);
17563 
17564       printf ("  0x%8.8lx ", addr);
17565 
17566       for (j = 0; j < 16; j++)
17567 	{
17568 	  if (j < lbytes)
17569 	    printf ("%2.2x", p[j]);
17570 	  else
17571 	    printf ("  ");
17572 
17573 	  if ((j & 3) == 3)
17574 	    printf (" ");
17575 	}
17576 
17577       for (j = 0; j < lbytes; j++)
17578 	{
17579 	  k = p[j];
17580 	  if (k >= ' ' && k < 0x7f)
17581 	    printf ("%c", k);
17582 	  else
17583 	    printf (".");
17584 	}
17585 
17586       putchar ('\n');
17587 
17588       p  += lbytes;
17589       bytes -= lbytes;
17590       addr += lbytes;
17591     }
17592 
17593   putchar ('\n');
17594 }
17595 
17596 static unsigned char *
17597 display_msp430_attribute (unsigned char * p,
17598 			   const unsigned char * const end)
17599 {
17600   unsigned int val;
17601   unsigned int tag;
17602 
17603   READ_ULEB (tag, p, end);
17604 
17605   switch (tag)
17606     {
17607     case OFBA_MSPABI_Tag_ISA:
17608       printf ("  Tag_ISA: ");
17609       READ_ULEB (val, p, end);
17610       switch (val)
17611 	{
17612 	case 0: printf (_("None\n")); break;
17613 	case 1: printf (_("MSP430\n")); break;
17614 	case 2: printf (_("MSP430X\n")); break;
17615 	default: printf ("??? (%d)\n", val); break;
17616 	}
17617       break;
17618 
17619     case OFBA_MSPABI_Tag_Code_Model:
17620       printf ("  Tag_Code_Model: ");
17621       READ_ULEB (val, p, end);
17622       switch (val)
17623 	{
17624 	case 0: printf (_("None\n")); break;
17625 	case 1: printf (_("Small\n")); break;
17626 	case 2: printf (_("Large\n")); break;
17627 	default: printf ("??? (%d)\n", val); break;
17628 	}
17629       break;
17630 
17631     case OFBA_MSPABI_Tag_Data_Model:
17632       printf ("  Tag_Data_Model: ");
17633       READ_ULEB (val, p, end);
17634       switch (val)
17635 	{
17636 	case 0: printf (_("None\n")); break;
17637 	case 1: printf (_("Small\n")); break;
17638 	case 2: printf (_("Large\n")); break;
17639 	case 3: printf (_("Restricted Large\n")); break;
17640 	default: printf ("??? (%d)\n", val); break;
17641 	}
17642       break;
17643 
17644     default:
17645       printf (_("  <unknown tag %d>: "), tag);
17646 
17647       if (tag & 1)
17648 	{
17649 	  putchar ('"');
17650 	  if (p < end - 1)
17651 	    {
17652 	      size_t maxlen = (end - p) - 1;
17653 
17654 	      print_symbol ((int) maxlen, (const char *) p);
17655 	      p += strnlen ((char *) p, maxlen) + 1;
17656 	    }
17657 	  else
17658 	    {
17659 	      printf (_("<corrupt>"));
17660 	      p = (unsigned char *) end;
17661 	    }
17662 	  printf ("\"\n");
17663 	}
17664       else
17665 	{
17666 	  READ_ULEB (val, p, end);
17667 	  printf ("%d (0x%x)\n", val, val);
17668 	}
17669       break;
17670    }
17671 
17672   assert (p <= end);
17673   return p;
17674 }
17675 
17676 static unsigned char *
17677 display_msp430_gnu_attribute (unsigned char * p,
17678 			      unsigned int tag,
17679 			      const unsigned char * const end)
17680 {
17681   if (tag == Tag_GNU_MSP430_Data_Region)
17682     {
17683       unsigned int val;
17684 
17685       printf ("  Tag_GNU_MSP430_Data_Region: ");
17686       READ_ULEB (val, p, end);
17687 
17688       switch (val)
17689 	{
17690 	case Val_GNU_MSP430_Data_Region_Any:
17691 	  printf (_("Any Region\n"));
17692 	  break;
17693 	case Val_GNU_MSP430_Data_Region_Lower:
17694 	  printf (_("Lower Region Only\n"));
17695 	  break;
17696 	default:
17697 	  printf ("??? (%u)\n", val);
17698 	}
17699       return p;
17700     }
17701   return display_tag_value (tag & 1, p, end);
17702 }
17703 
17704 struct riscv_attr_tag_t {
17705   const char *name;
17706   unsigned int tag;
17707 };
17708 
17709 static struct riscv_attr_tag_t riscv_attr_tag[] =
17710 {
17711 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17712   T(arch),
17713   T(priv_spec),
17714   T(priv_spec_minor),
17715   T(priv_spec_revision),
17716   T(unaligned_access),
17717   T(stack_align),
17718 #undef T
17719 };
17720 
17721 static unsigned char *
17722 display_riscv_attribute (unsigned char *p,
17723 			 const unsigned char * const end)
17724 {
17725   unsigned int val;
17726   unsigned int tag;
17727   struct riscv_attr_tag_t *attr = NULL;
17728   unsigned i;
17729 
17730   READ_ULEB (tag, p, end);
17731 
17732   /* Find the name of attribute. */
17733   for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17734     {
17735       if (riscv_attr_tag[i].tag == tag)
17736 	{
17737 	  attr = &riscv_attr_tag[i];
17738 	  break;
17739 	}
17740     }
17741 
17742   if (attr)
17743     printf ("  %s: ", attr->name);
17744   else
17745     return display_tag_value (tag, p, end);
17746 
17747   switch (tag)
17748     {
17749     case Tag_RISCV_priv_spec:
17750     case Tag_RISCV_priv_spec_minor:
17751     case Tag_RISCV_priv_spec_revision:
17752       READ_ULEB (val, p, end);
17753       printf (_("%u\n"), val);
17754       break;
17755     case Tag_RISCV_unaligned_access:
17756       READ_ULEB (val, p, end);
17757       switch (val)
17758 	{
17759 	case 0:
17760 	  printf (_("No unaligned access\n"));
17761 	  break;
17762 	case 1:
17763 	  printf (_("Unaligned access\n"));
17764 	  break;
17765 	}
17766       break;
17767     case Tag_RISCV_stack_align:
17768       READ_ULEB (val, p, end);
17769       printf (_("%u-bytes\n"), val);
17770       break;
17771     case Tag_RISCV_arch:
17772       p = display_tag_value (-1, p, end);
17773       break;
17774     default:
17775       return display_tag_value (tag, p, end);
17776     }
17777 
17778   return p;
17779 }
17780 
17781 static unsigned char *
17782 display_csky_attribute (unsigned char * p,
17783 			const unsigned char * const end)
17784 {
17785   unsigned int tag;
17786   unsigned int val;
17787   READ_ULEB (tag, p, end);
17788 
17789   if (tag >= Tag_CSKY_MAX)
17790     {
17791       return display_tag_value (-1, p, end);
17792     }
17793 
17794   switch (tag)
17795     {
17796     case Tag_CSKY_ARCH_NAME:
17797       printf ("  Tag_CSKY_ARCH_NAME:\t\t");
17798       return display_tag_value (-1, p, end);
17799     case Tag_CSKY_CPU_NAME:
17800       printf ("  Tag_CSKY_CPU_NAME:\t\t");
17801       return display_tag_value (-1, p, end);
17802 
17803     case Tag_CSKY_ISA_FLAGS:
17804       printf ("  Tag_CSKY_ISA_FLAGS:\t\t");
17805       return display_tag_value (0, p, end);
17806     case Tag_CSKY_ISA_EXT_FLAGS:
17807       printf ("  Tag_CSKY_ISA_EXT_FLAGS:\t");
17808       return display_tag_value (0, p, end);
17809 
17810     case Tag_CSKY_DSP_VERSION:
17811       printf ("  Tag_CSKY_DSP_VERSION:\t\t");
17812       READ_ULEB (val, p, end);
17813       if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17814 	printf ("DSP Extension\n");
17815       else if (val == VAL_CSKY_DSP_VERSION_2)
17816 	printf ("DSP 2.0\n");
17817       break;
17818 
17819     case Tag_CSKY_VDSP_VERSION:
17820       printf ("  Tag_CSKY_VDSP_VERSION:\t");
17821       READ_ULEB (val, p, end);
17822       printf ("VDSP Version %d\n", val);
17823       break;
17824 
17825     case Tag_CSKY_FPU_VERSION:
17826       printf ("  Tag_CSKY_FPU_VERSION:\t\t");
17827       READ_ULEB (val, p, end);
17828       if (val == VAL_CSKY_FPU_VERSION_1)
17829 	printf ("ABIV1 FPU Version 1\n");
17830       else if (val == VAL_CSKY_FPU_VERSION_2)
17831 	printf ("FPU Version 2\n");
17832       break;
17833 
17834     case Tag_CSKY_FPU_ABI:
17835       printf ("  Tag_CSKY_FPU_ABI:\t\t");
17836       READ_ULEB (val, p, end);
17837       if (val == VAL_CSKY_FPU_ABI_HARD)
17838 	printf ("Hard\n");
17839       else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17840 	printf ("SoftFP\n");
17841       else if (val == VAL_CSKY_FPU_ABI_SOFT)
17842 	printf ("Soft\n");
17843       break;
17844     case Tag_CSKY_FPU_ROUNDING:
17845       READ_ULEB (val, p, end);
17846       if (val == 1)
17847 	{
17848 	  printf ("  Tag_CSKY_FPU_ROUNDING:\t");
17849 	  printf ("Needed\n");
17850 	}
17851       break;
17852     case Tag_CSKY_FPU_DENORMAL:
17853       READ_ULEB (val, p, end);
17854       if (val == 1)
17855 	{
17856 	  printf ("  Tag_CSKY_FPU_DENORMAL:\t");
17857 	  printf ("Needed\n");
17858 	}
17859       break;
17860     case Tag_CSKY_FPU_Exception:
17861       READ_ULEB (val, p, end);
17862       if (val == 1)
17863 	{
17864 	  printf ("  Tag_CSKY_FPU_Exception:\t");
17865 	  printf ("Needed\n");
17866 	}
17867       break;
17868     case Tag_CSKY_FPU_NUMBER_MODULE:
17869       printf ("  Tag_CSKY_FPU_NUMBER_MODULE:\t");
17870       return display_tag_value (-1, p, end);
17871     case Tag_CSKY_FPU_HARDFP:
17872       printf ("  Tag_CSKY_FPU_HARDFP:\t\t");
17873       READ_ULEB (val, p, end);
17874       if (val & VAL_CSKY_FPU_HARDFP_HALF)
17875 	printf (" Half");
17876       if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17877 	printf (" Single");
17878       if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17879 	printf (" Double");
17880       printf ("\n");
17881       break;
17882     default:
17883       return display_tag_value (tag, p, end);
17884      }
17885   return p;
17886 }
17887 
17888 static bool
17889 process_attributes (Filedata * filedata,
17890 		    const char * public_name,
17891 		    unsigned int proc_type,
17892 		    unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17893 		    unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17894 {
17895   Elf_Internal_Shdr * sect;
17896   unsigned i;
17897   bool res = true;
17898 
17899   /* Find the section header so that we get the size.  */
17900   for (i = 0, sect = filedata->section_headers;
17901        i < filedata->file_header.e_shnum;
17902        i++, sect++)
17903     {
17904       unsigned char * contents;
17905       unsigned char * p;
17906 
17907       if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17908 	continue;
17909 
17910       contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17911                                              sect->sh_size, _("attributes"));
17912       if (contents == NULL)
17913 	{
17914 	  res = false;
17915 	  continue;
17916 	}
17917 
17918       p = contents;
17919       /* The first character is the version of the attributes.
17920 	 Currently only version 1, (aka 'A') is recognised here.  */
17921       if (*p != 'A')
17922 	{
17923 	  printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17924 	  res = false;
17925 	}
17926       else
17927 	{
17928 	  bfd_vma section_len;
17929 
17930 	  section_len = sect->sh_size - 1;
17931 	  p++;
17932 
17933 	  while (section_len > 0)
17934 	    {
17935 	      bfd_vma attr_len;
17936 	      unsigned int namelen;
17937 	      bool public_section;
17938 	      bool gnu_section;
17939 
17940 	      if (section_len <= 4)
17941 		{
17942 		  error (_("Tag section ends prematurely\n"));
17943 		  res = false;
17944 		  break;
17945 		}
17946 	      attr_len = byte_get (p, 4);
17947 	      p += 4;
17948 
17949 	      if (attr_len > section_len)
17950 		{
17951 		  error (_("Bad attribute length (%u > %u)\n"),
17952 			  (unsigned) attr_len, (unsigned) section_len);
17953 		  attr_len = section_len;
17954 		  res = false;
17955 		}
17956 	      /* PR 17531: file: 001-101425-0.004  */
17957 	      else if (attr_len < 5)
17958 		{
17959 		  error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17960 		  res = false;
17961 		  break;
17962 		}
17963 
17964 	      section_len -= attr_len;
17965 	      attr_len -= 4;
17966 
17967 	      namelen = strnlen ((char *) p, attr_len) + 1;
17968 	      if (namelen == 0 || namelen >= attr_len)
17969 		{
17970 		  error (_("Corrupt attribute section name\n"));
17971 		  res = false;
17972 		  break;
17973 		}
17974 
17975 	      printf (_("Attribute Section: "));
17976 	      print_symbol (INT_MAX, (const char *) p);
17977 	      putchar ('\n');
17978 
17979 	      if (public_name && streq ((char *) p, public_name))
17980 		public_section = true;
17981 	      else
17982 		public_section = false;
17983 
17984 	      if (streq ((char *) p, "gnu"))
17985 		gnu_section = true;
17986 	      else
17987 		gnu_section = false;
17988 
17989 	      p += namelen;
17990 	      attr_len -= namelen;
17991 
17992 	      while (attr_len > 0 && p < contents + sect->sh_size)
17993 		{
17994 		  int tag;
17995 		  unsigned int val;
17996 		  bfd_vma size;
17997 		  unsigned char * end;
17998 
17999 		  /* PR binutils/17531: Safe handling of corrupt files.  */
18000 		  if (attr_len < 6)
18001 		    {
18002 		      error (_("Unused bytes at end of section\n"));
18003 		      res = false;
18004 		      section_len = 0;
18005 		      break;
18006 		    }
18007 
18008 		  tag = *(p++);
18009 		  size = byte_get (p, 4);
18010 		  if (size > attr_len)
18011 		    {
18012 		      error (_("Bad subsection length (%u > %u)\n"),
18013 			      (unsigned) size, (unsigned) attr_len);
18014 		      res = false;
18015 		      size = attr_len;
18016 		    }
18017 		  /* PR binutils/17531: Safe handling of corrupt files.  */
18018 		  if (size < 6)
18019 		    {
18020 		      error (_("Bad subsection length (%u < 6)\n"),
18021 			      (unsigned) size);
18022 		      res = false;
18023 		      section_len = 0;
18024 		      break;
18025 		    }
18026 
18027 		  attr_len -= size;
18028 		  end = p + size - 1;
18029 		  assert (end <= contents + sect->sh_size);
18030 		  p += 4;
18031 
18032 		  switch (tag)
18033 		    {
18034 		    case 1:
18035 		      printf (_("File Attributes\n"));
18036 		      break;
18037 		    case 2:
18038 		      printf (_("Section Attributes:"));
18039 		      goto do_numlist;
18040 		    case 3:
18041 		      printf (_("Symbol Attributes:"));
18042 		      /* Fall through.  */
18043 		    do_numlist:
18044 		      for (;;)
18045 			{
18046 			  READ_ULEB (val, p, end);
18047 			  if (val == 0)
18048 			    break;
18049 			  printf (" %d", val);
18050 			}
18051 		      printf ("\n");
18052 		      break;
18053 		    default:
18054 		      printf (_("Unknown tag: %d\n"), tag);
18055 		      public_section = false;
18056 		      break;
18057 		    }
18058 
18059 		  if (public_section && display_pub_attribute != NULL)
18060 		    {
18061 		      while (p < end)
18062 			p = display_pub_attribute (p, end);
18063 		      assert (p == end);
18064 		    }
18065 		  else if (gnu_section && display_proc_gnu_attribute != NULL)
18066 		    {
18067 		      while (p < end)
18068 			p = display_gnu_attribute (p,
18069 						   display_proc_gnu_attribute,
18070 						   end);
18071 		      assert (p == end);
18072 		    }
18073 		  else if (p < end)
18074 		    {
18075 		      printf (_("  Unknown attribute:\n"));
18076 		      display_raw_attribute (p, end);
18077 		      p = end;
18078 		    }
18079 		  else
18080 		    attr_len = 0;
18081 		}
18082 	    }
18083 	}
18084 
18085       free (contents);
18086     }
18087 
18088   return res;
18089 }
18090 
18091 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18092    Print the Address, Access and Initial fields of an entry at VMA ADDR
18093    and return the VMA of the next entry, or -1 if there was a problem.
18094    Does not read from DATA_END or beyond.  */
18095 
18096 static bfd_vma
18097 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
18098 		      unsigned char * data_end)
18099 {
18100   printf ("  ");
18101   print_vma (addr, LONG_HEX);
18102   printf (" ");
18103   if (addr < pltgot + 0xfff0)
18104     printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18105   else
18106     printf ("%10s", "");
18107   printf (" ");
18108   if (data == NULL)
18109     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18110   else
18111     {
18112       bfd_vma entry;
18113       unsigned char * from = data + addr - pltgot;
18114 
18115       if (from + (is_32bit_elf ? 4 : 8) > data_end)
18116 	{
18117 	  warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18118 	  printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
18119 	  return (bfd_vma) -1;
18120 	}
18121       else
18122 	{
18123 	  entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18124 	  print_vma (entry, LONG_HEX);
18125 	}
18126     }
18127   return addr + (is_32bit_elf ? 4 : 8);
18128 }
18129 
18130 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18131    PLTGOT.  Print the Address and Initial fields of an entry at VMA
18132    ADDR and return the VMA of the next entry.  */
18133 
18134 static bfd_vma
18135 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
18136 {
18137   printf ("  ");
18138   print_vma (addr, LONG_HEX);
18139   printf (" ");
18140   if (data == NULL)
18141     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18142   else
18143     {
18144       bfd_vma entry;
18145 
18146       entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18147       print_vma (entry, LONG_HEX);
18148     }
18149   return addr + (is_32bit_elf ? 4 : 8);
18150 }
18151 
18152 static void
18153 print_mips_ases (unsigned int mask)
18154 {
18155   if (mask & AFL_ASE_DSP)
18156     fputs ("\n\tDSP ASE", stdout);
18157   if (mask & AFL_ASE_DSPR2)
18158     fputs ("\n\tDSP R2 ASE", stdout);
18159   if (mask & AFL_ASE_DSPR3)
18160     fputs ("\n\tDSP R3 ASE", stdout);
18161   if (mask & AFL_ASE_EVA)
18162     fputs ("\n\tEnhanced VA Scheme", stdout);
18163   if (mask & AFL_ASE_MCU)
18164     fputs ("\n\tMCU (MicroController) ASE", stdout);
18165   if (mask & AFL_ASE_MDMX)
18166     fputs ("\n\tMDMX ASE", stdout);
18167   if (mask & AFL_ASE_MIPS3D)
18168     fputs ("\n\tMIPS-3D ASE", stdout);
18169   if (mask & AFL_ASE_MT)
18170     fputs ("\n\tMT ASE", stdout);
18171   if (mask & AFL_ASE_SMARTMIPS)
18172     fputs ("\n\tSmartMIPS ASE", stdout);
18173   if (mask & AFL_ASE_VIRT)
18174     fputs ("\n\tVZ ASE", stdout);
18175   if (mask & AFL_ASE_MSA)
18176     fputs ("\n\tMSA ASE", stdout);
18177   if (mask & AFL_ASE_MIPS16)
18178     fputs ("\n\tMIPS16 ASE", stdout);
18179   if (mask & AFL_ASE_MICROMIPS)
18180     fputs ("\n\tMICROMIPS ASE", stdout);
18181   if (mask & AFL_ASE_XPA)
18182     fputs ("\n\tXPA ASE", stdout);
18183   if (mask & AFL_ASE_MIPS16E2)
18184     fputs ("\n\tMIPS16e2 ASE", stdout);
18185   if (mask & AFL_ASE_CRC)
18186     fputs ("\n\tCRC ASE", stdout);
18187   if (mask & AFL_ASE_GINV)
18188     fputs ("\n\tGINV ASE", stdout);
18189   if (mask & AFL_ASE_LOONGSON_MMI)
18190     fputs ("\n\tLoongson MMI ASE", stdout);
18191   if (mask & AFL_ASE_LOONGSON_CAM)
18192     fputs ("\n\tLoongson CAM ASE", stdout);
18193   if (mask & AFL_ASE_LOONGSON_EXT)
18194     fputs ("\n\tLoongson EXT ASE", stdout);
18195   if (mask & AFL_ASE_LOONGSON_EXT2)
18196     fputs ("\n\tLoongson EXT2 ASE", stdout);
18197   if (mask == 0)
18198     fprintf (stdout, "\n\t%s", _("None"));
18199   else if ((mask & ~AFL_ASE_MASK) != 0)
18200     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18201 }
18202 
18203 static void
18204 print_mips_isa_ext (unsigned int isa_ext)
18205 {
18206   switch (isa_ext)
18207     {
18208     case 0:
18209       fputs (_("None"), stdout);
18210       break;
18211     case AFL_EXT_XLR:
18212       fputs ("RMI XLR", stdout);
18213       break;
18214     case AFL_EXT_OCTEON3:
18215       fputs ("Cavium Networks Octeon3", stdout);
18216       break;
18217     case AFL_EXT_OCTEON2:
18218       fputs ("Cavium Networks Octeon2", stdout);
18219       break;
18220     case AFL_EXT_OCTEONP:
18221       fputs ("Cavium Networks OcteonP", stdout);
18222       break;
18223     case AFL_EXT_OCTEON:
18224       fputs ("Cavium Networks Octeon", stdout);
18225       break;
18226     case AFL_EXT_5900:
18227       fputs ("Toshiba R5900", stdout);
18228       break;
18229     case AFL_EXT_4650:
18230       fputs ("MIPS R4650", stdout);
18231       break;
18232     case AFL_EXT_4010:
18233       fputs ("LSI R4010", stdout);
18234       break;
18235     case AFL_EXT_4100:
18236       fputs ("NEC VR4100", stdout);
18237       break;
18238     case AFL_EXT_3900:
18239       fputs ("Toshiba R3900", stdout);
18240       break;
18241     case AFL_EXT_10000:
18242       fputs ("MIPS R10000", stdout);
18243       break;
18244     case AFL_EXT_SB1:
18245       fputs ("Broadcom SB-1", stdout);
18246       break;
18247     case AFL_EXT_4111:
18248       fputs ("NEC VR4111/VR4181", stdout);
18249       break;
18250     case AFL_EXT_4120:
18251       fputs ("NEC VR4120", stdout);
18252       break;
18253     case AFL_EXT_5400:
18254       fputs ("NEC VR5400", stdout);
18255       break;
18256     case AFL_EXT_5500:
18257       fputs ("NEC VR5500", stdout);
18258       break;
18259     case AFL_EXT_LOONGSON_2E:
18260       fputs ("ST Microelectronics Loongson 2E", stdout);
18261       break;
18262     case AFL_EXT_LOONGSON_2F:
18263       fputs ("ST Microelectronics Loongson 2F", stdout);
18264       break;
18265     case AFL_EXT_INTERAPTIV_MR2:
18266       fputs ("Imagination interAptiv MR2", stdout);
18267       break;
18268     default:
18269       fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
18270     }
18271 }
18272 
18273 static signed int
18274 get_mips_reg_size (int reg_size)
18275 {
18276   return (reg_size == AFL_REG_NONE) ? 0
18277 	 : (reg_size == AFL_REG_32) ? 32
18278 	 : (reg_size == AFL_REG_64) ? 64
18279 	 : (reg_size == AFL_REG_128) ? 128
18280 	 : -1;
18281 }
18282 
18283 static bool
18284 process_mips_specific (Filedata * filedata)
18285 {
18286   Elf_Internal_Dyn * entry;
18287   Elf_Internal_Shdr *sect = NULL;
18288   size_t liblist_offset = 0;
18289   size_t liblistno = 0;
18290   size_t conflictsno = 0;
18291   size_t options_offset = 0;
18292   size_t conflicts_offset = 0;
18293   size_t pltrelsz = 0;
18294   size_t pltrel = 0;
18295   bfd_vma pltgot = 0;
18296   bfd_vma mips_pltgot = 0;
18297   bfd_vma jmprel = 0;
18298   bfd_vma local_gotno = 0;
18299   bfd_vma gotsym = 0;
18300   bfd_vma symtabno = 0;
18301   bool res = true;
18302 
18303   if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18304 			    display_mips_gnu_attribute))
18305     res = false;
18306 
18307   sect = find_section (filedata, ".MIPS.abiflags");
18308 
18309   if (sect != NULL)
18310     {
18311       Elf_External_ABIFlags_v0 *abiflags_ext;
18312       Elf_Internal_ABIFlags_v0 abiflags_in;
18313 
18314       if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
18315 	{
18316 	  error (_("Corrupt MIPS ABI Flags section.\n"));
18317 	  res = false;
18318 	}
18319       else
18320 	{
18321 	  abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
18322 				   sect->sh_size, _("MIPS ABI Flags section"));
18323 	  if (abiflags_ext)
18324 	    {
18325 	      abiflags_in.version = BYTE_GET (abiflags_ext->version);
18326 	      abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
18327 	      abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
18328 	      abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
18329 	      abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
18330 	      abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
18331 	      abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
18332 	      abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
18333 	      abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
18334 	      abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
18335 	      abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
18336 
18337 	      printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
18338 	      printf ("\nISA: MIPS%d", abiflags_in.isa_level);
18339 	      if (abiflags_in.isa_rev > 1)
18340 		printf ("r%d", abiflags_in.isa_rev);
18341 	      printf ("\nGPR size: %d",
18342 		      get_mips_reg_size (abiflags_in.gpr_size));
18343 	      printf ("\nCPR1 size: %d",
18344 		      get_mips_reg_size (abiflags_in.cpr1_size));
18345 	      printf ("\nCPR2 size: %d",
18346 		      get_mips_reg_size (abiflags_in.cpr2_size));
18347 	      fputs ("\nFP ABI: ", stdout);
18348 	      print_mips_fp_abi_value (abiflags_in.fp_abi);
18349 	      fputs ("ISA Extension: ", stdout);
18350 	      print_mips_isa_ext (abiflags_in.isa_ext);
18351 	      fputs ("\nASEs:", stdout);
18352 	      print_mips_ases (abiflags_in.ases);
18353 	      printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
18354 	      printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
18355 	      fputc ('\n', stdout);
18356 	      free (abiflags_ext);
18357 	    }
18358 	}
18359     }
18360 
18361   /* We have a lot of special sections.  Thanks SGI!  */
18362   if (filedata->dynamic_section == NULL)
18363     {
18364       /* No dynamic information available.  See if there is static GOT.  */
18365       sect = find_section (filedata, ".got");
18366       if (sect != NULL)
18367 	{
18368 	  unsigned char *data_end;
18369 	  unsigned char *data;
18370 	  bfd_vma ent, end;
18371 	  int addr_size;
18372 
18373 	  pltgot = sect->sh_addr;
18374 
18375 	  ent = pltgot;
18376 	  addr_size = (is_32bit_elf ? 4 : 8);
18377 	  end = pltgot + sect->sh_size;
18378 
18379 	  data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
18380 					     end - pltgot, 1,
18381 					     _("Global Offset Table data"));
18382 	  /* PR 12855: Null data is handled gracefully throughout.  */
18383 	  data_end = data + (end - pltgot);
18384 
18385 	  printf (_("\nStatic GOT:\n"));
18386 	  printf (_(" Canonical gp value: "));
18387 	  print_vma (ent + 0x7ff0, LONG_HEX);
18388 	  printf ("\n\n");
18389 
18390 	  /* In a dynamic binary GOT[0] is reserved for the dynamic
18391 	     loader to store the lazy resolver pointer, however in
18392 	     a static binary it may well have been omitted and GOT
18393 	     reduced to a table of addresses.
18394 	     PR 21344: Check for the entry being fully available
18395 	     before fetching it.  */
18396 	  if (data
18397 	      && data + ent - pltgot + addr_size <= data_end
18398 	      && byte_get (data + ent - pltgot, addr_size) == 0)
18399 	    {
18400 	      printf (_(" Reserved entries:\n"));
18401 	      printf (_("  %*s %10s %*s\n"),
18402 		      addr_size * 2, _("Address"), _("Access"),
18403 		      addr_size * 2, _("Value"));
18404 	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
18405 	      printf ("\n");
18406 	      if (ent == (bfd_vma) -1)
18407 		goto sgot_print_fail;
18408 
18409 	      /* Check for the MSB of GOT[1] being set, identifying a
18410 		 GNU object.  This entry will be used by some runtime
18411 		 loaders, to store the module pointer.  Otherwise this
18412 		 is an ordinary local entry.
18413 		 PR 21344: Check for the entry being fully available
18414 		 before fetching it.  */
18415 	      if (data
18416 		  && data + ent - pltgot + addr_size <= data_end
18417 		  && (byte_get (data + ent - pltgot, addr_size)
18418 		      >> (addr_size * 8 - 1)) != 0)
18419 		{
18420 		  ent = print_mips_got_entry (data, pltgot, ent, data_end);
18421 		  printf ("\n");
18422 		  if (ent == (bfd_vma) -1)
18423 		    goto sgot_print_fail;
18424 		}
18425 	      printf ("\n");
18426 	    }
18427 
18428 	  if (data != NULL && ent < end)
18429 	    {
18430 	      printf (_(" Local entries:\n"));
18431 	      printf ("  %*s %10s %*s\n",
18432 		      addr_size * 2, _("Address"), _("Access"),
18433 		      addr_size * 2, _("Value"));
18434 	      while (ent < end)
18435 		{
18436 		  ent = print_mips_got_entry (data, pltgot, ent, data_end);
18437 		  printf ("\n");
18438 		  if (ent == (bfd_vma) -1)
18439 		    goto sgot_print_fail;
18440 		}
18441 	      printf ("\n");
18442 	    }
18443 
18444 	sgot_print_fail:
18445 	  free (data);
18446 	}
18447       return res;
18448     }
18449 
18450   for (entry = filedata->dynamic_section;
18451        /* PR 17531 file: 012-50589-0.004.  */
18452        (entry < filedata->dynamic_section + filedata->dynamic_nent
18453 	&& entry->d_tag != DT_NULL);
18454        ++entry)
18455     switch (entry->d_tag)
18456       {
18457       case DT_MIPS_LIBLIST:
18458 	liblist_offset
18459 	  = offset_from_vma (filedata, entry->d_un.d_val,
18460 			     liblistno * sizeof (Elf32_External_Lib));
18461 	break;
18462       case DT_MIPS_LIBLISTNO:
18463 	liblistno = entry->d_un.d_val;
18464 	break;
18465       case DT_MIPS_OPTIONS:
18466 	options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
18467 	break;
18468       case DT_MIPS_CONFLICT:
18469 	conflicts_offset
18470 	  = offset_from_vma (filedata, entry->d_un.d_val,
18471 			     conflictsno * sizeof (Elf32_External_Conflict));
18472 	break;
18473       case DT_MIPS_CONFLICTNO:
18474 	conflictsno = entry->d_un.d_val;
18475 	break;
18476       case DT_PLTGOT:
18477 	pltgot = entry->d_un.d_ptr;
18478 	break;
18479       case DT_MIPS_LOCAL_GOTNO:
18480 	local_gotno = entry->d_un.d_val;
18481 	break;
18482       case DT_MIPS_GOTSYM:
18483 	gotsym = entry->d_un.d_val;
18484 	break;
18485       case DT_MIPS_SYMTABNO:
18486 	symtabno = entry->d_un.d_val;
18487 	break;
18488       case DT_MIPS_PLTGOT:
18489 	mips_pltgot = entry->d_un.d_ptr;
18490 	break;
18491       case DT_PLTREL:
18492 	pltrel = entry->d_un.d_val;
18493 	break;
18494       case DT_PLTRELSZ:
18495 	pltrelsz = entry->d_un.d_val;
18496 	break;
18497       case DT_JMPREL:
18498 	jmprel = entry->d_un.d_ptr;
18499 	break;
18500       default:
18501 	break;
18502       }
18503 
18504   if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18505     {
18506       Elf32_External_Lib * elib;
18507       size_t cnt;
18508 
18509       elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18510 					      sizeof (Elf32_External_Lib),
18511 					      liblistno,
18512 					      _("liblist section data"));
18513       if (elib)
18514 	{
18515 	  printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18516 			    "\nSection '.liblist' contains %lu entries:\n",
18517 			    (unsigned long) liblistno),
18518 		  (unsigned long) liblistno);
18519 	  fputs (_("     Library              Time Stamp          Checksum   Version Flags\n"),
18520 		 stdout);
18521 
18522 	  for (cnt = 0; cnt < liblistno; ++cnt)
18523 	    {
18524 	      Elf32_Lib liblist;
18525 	      time_t atime;
18526 	      char timebuf[128];
18527 	      struct tm * tmp;
18528 
18529 	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
18530 	      atime = BYTE_GET (elib[cnt].l_time_stamp);
18531 	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18532 	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
18533 	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18534 
18535 	      tmp = gmtime (&atime);
18536 	      snprintf (timebuf, sizeof (timebuf),
18537 			"%04u-%02u-%02uT%02u:%02u:%02u",
18538 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18539 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18540 
18541 	      printf ("%3lu: ", (unsigned long) cnt);
18542 	      if (valid_dynamic_name (filedata, liblist.l_name))
18543 		print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18544 	      else
18545 		printf (_("<corrupt: %9ld>"), liblist.l_name);
18546 	      printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18547 		      liblist.l_version);
18548 
18549 	      if (liblist.l_flags == 0)
18550 		puts (_(" NONE"));
18551 	      else
18552 		{
18553 		  static const struct
18554 		  {
18555 		    const char * name;
18556 		    int bit;
18557 		  }
18558 		  l_flags_vals[] =
18559 		  {
18560 		    { " EXACT_MATCH", LL_EXACT_MATCH },
18561 		    { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18562 		    { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18563 		    { " EXPORTS", LL_EXPORTS },
18564 		    { " DELAY_LOAD", LL_DELAY_LOAD },
18565 		    { " DELTA", LL_DELTA }
18566 		  };
18567 		  int flags = liblist.l_flags;
18568 		  size_t fcnt;
18569 
18570 		  for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18571 		    if ((flags & l_flags_vals[fcnt].bit) != 0)
18572 		      {
18573 			fputs (l_flags_vals[fcnt].name, stdout);
18574 			flags ^= l_flags_vals[fcnt].bit;
18575 		      }
18576 		  if (flags != 0)
18577 		    printf (" %#x", (unsigned int) flags);
18578 
18579 		  puts ("");
18580 		}
18581 	    }
18582 
18583 	  free (elib);
18584 	}
18585       else
18586 	res = false;
18587     }
18588 
18589   if (options_offset != 0)
18590     {
18591       Elf_External_Options * eopt;
18592       size_t offset;
18593       int cnt;
18594 
18595       /* Find the section header so that we get the size.  */
18596       sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18597       /* PR 17533 file: 012-277276-0.004.  */
18598       if (sect == NULL)
18599 	{
18600 	  error (_("No MIPS_OPTIONS header found\n"));
18601 	  return false;
18602 	}
18603       /* PR 24243  */
18604       if (sect->sh_size < sizeof (* eopt))
18605 	{
18606 	  error (_("The MIPS options section is too small.\n"));
18607 	  return false;
18608 	}
18609 
18610       eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18611                                                 sect->sh_size, _("options"));
18612       if (eopt)
18613 	{
18614 	  Elf_Internal_Options option;
18615 
18616 	  offset = cnt = 0;
18617 	  while (offset <= sect->sh_size - sizeof (* eopt))
18618 	    {
18619 	      Elf_External_Options * eoption;
18620 	      unsigned int optsize;
18621 
18622 	      eoption = (Elf_External_Options *) ((char *) eopt + offset);
18623 
18624 	      optsize = BYTE_GET (eoption->size);
18625 
18626 	      /* PR 17531: file: ffa0fa3b.  */
18627 	      if (optsize < sizeof (* eopt)
18628 		  || optsize > sect->sh_size - offset)
18629 		{
18630 		  error (_("Invalid size (%u) for MIPS option\n"),
18631 			 optsize);
18632 		  free (eopt);
18633 		  return false;
18634 		}
18635 	      offset += optsize;
18636 	      ++cnt;
18637 	    }
18638 
18639 	  printf (ngettext ("\nSection '%s' contains %d entry:\n",
18640 			    "\nSection '%s' contains %d entries:\n",
18641 			    cnt),
18642 		  printable_section_name (filedata, sect), cnt);
18643 
18644 	  offset = 0;
18645 	  while (cnt-- > 0)
18646 	    {
18647 	      size_t len;
18648 	      Elf_External_Options * eoption;
18649 
18650 	      eoption = (Elf_External_Options *) ((char *) eopt + offset);
18651 
18652 	      option.kind = BYTE_GET (eoption->kind);
18653 	      option.size = BYTE_GET (eoption->size);
18654 	      option.section = BYTE_GET (eoption->section);
18655 	      option.info = BYTE_GET (eoption->info);
18656 
18657 	      switch (option.kind)
18658 		{
18659 		case ODK_NULL:
18660 		  /* This shouldn't happen.  */
18661 		  printf (" NULL       %" PRId16 " %" PRIx32,
18662 			  option.section, option.info);
18663 		  break;
18664 
18665 		case ODK_REGINFO:
18666 		  printf (" REGINFO    ");
18667 		  if (filedata->file_header.e_machine == EM_MIPS)
18668 		    {
18669 		      Elf32_External_RegInfo * ereg;
18670 		      Elf32_RegInfo reginfo;
18671 
18672 		      /* 32bit form.  */
18673 		      if (option.size < (sizeof (Elf_External_Options)
18674 					 + sizeof (Elf32_External_RegInfo)))
18675 			{
18676 			  printf (_("<corrupt>\n"));
18677 			  error (_("Truncated MIPS REGINFO option\n"));
18678 			  cnt = 0;
18679 			  break;
18680 			}
18681 
18682 		      ereg = (Elf32_External_RegInfo *) (eoption + 1);
18683 
18684 		      reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18685 		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18686 		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18687 		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18688 		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18689 		      reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18690 
18691 		      printf ("GPR %08" PRIx32 "  GP 0x%" PRIx32 "\n",
18692 			      reginfo.ri_gprmask, reginfo.ri_gp_value);
18693 		      printf ("          "
18694 			      "  CPR0 %08" PRIx32 "  CPR1 %08" PRIx32
18695 			      "  CPR2 %08" PRIx32 "  CPR3 %08" PRIx32 "\n",
18696 			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18697 			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18698 		    }
18699 		  else
18700 		    {
18701 		      /* 64 bit form.  */
18702 		      Elf64_External_RegInfo * ereg;
18703 		      Elf64_Internal_RegInfo reginfo;
18704 
18705 		      if (option.size < (sizeof (Elf_External_Options)
18706 					 + sizeof (Elf64_External_RegInfo)))
18707 			{
18708 			  printf (_("<corrupt>\n"));
18709 			  error (_("Truncated MIPS REGINFO option\n"));
18710 			  cnt = 0;
18711 			  break;
18712 			}
18713 
18714 		      ereg = (Elf64_External_RegInfo *) (eoption + 1);
18715 		      reginfo.ri_gprmask    = BYTE_GET (ereg->ri_gprmask);
18716 		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18717 		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18718 		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18719 		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18720 		      reginfo.ri_gp_value   = BYTE_GET (ereg->ri_gp_value);
18721 
18722 		      printf ("GPR %08" PRIx32 "  GP 0x%" PRIx64 "\n",
18723 			      reginfo.ri_gprmask, reginfo.ri_gp_value);
18724 		      printf ("          "
18725 			      "  CPR0 %08" PRIx32 "  CPR1 %08" PRIx32
18726 			      "  CPR2 %08" PRIx32 "  CPR3 %08" PRIx32 "\n",
18727 			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18728 			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18729 		    }
18730 		  offset += option.size;
18731 		  continue;
18732 
18733 		case ODK_EXCEPTIONS:
18734 		  fputs (" EXCEPTIONS fpe_min(", stdout);
18735 		  process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18736 		  fputs (") fpe_max(", stdout);
18737 		  process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18738 		  fputs (")", stdout);
18739 
18740 		  if (option.info & OEX_PAGE0)
18741 		    fputs (" PAGE0", stdout);
18742 		  if (option.info & OEX_SMM)
18743 		    fputs (" SMM", stdout);
18744 		  if (option.info & OEX_FPDBUG)
18745 		    fputs (" FPDBUG", stdout);
18746 		  if (option.info & OEX_DISMISS)
18747 		    fputs (" DISMISS", stdout);
18748 		  break;
18749 
18750 		case ODK_PAD:
18751 		  fputs (" PAD       ", stdout);
18752 		  if (option.info & OPAD_PREFIX)
18753 		    fputs (" PREFIX", stdout);
18754 		  if (option.info & OPAD_POSTFIX)
18755 		    fputs (" POSTFIX", stdout);
18756 		  if (option.info & OPAD_SYMBOL)
18757 		    fputs (" SYMBOL", stdout);
18758 		  break;
18759 
18760 		case ODK_HWPATCH:
18761 		  fputs (" HWPATCH   ", stdout);
18762 		  if (option.info & OHW_R4KEOP)
18763 		    fputs (" R4KEOP", stdout);
18764 		  if (option.info & OHW_R8KPFETCH)
18765 		    fputs (" R8KPFETCH", stdout);
18766 		  if (option.info & OHW_R5KEOP)
18767 		    fputs (" R5KEOP", stdout);
18768 		  if (option.info & OHW_R5KCVTL)
18769 		    fputs (" R5KCVTL", stdout);
18770 		  break;
18771 
18772 		case ODK_FILL:
18773 		  fputs (" FILL       ", stdout);
18774 		  /* XXX Print content of info word?  */
18775 		  break;
18776 
18777 		case ODK_TAGS:
18778 		  fputs (" TAGS       ", stdout);
18779 		  /* XXX Print content of info word?  */
18780 		  break;
18781 
18782 		case ODK_HWAND:
18783 		  fputs (" HWAND     ", stdout);
18784 		  if (option.info & OHWA0_R4KEOP_CHECKED)
18785 		    fputs (" R4KEOP_CHECKED", stdout);
18786 		  if (option.info & OHWA0_R4KEOP_CLEAN)
18787 		    fputs (" R4KEOP_CLEAN", stdout);
18788 		  break;
18789 
18790 		case ODK_HWOR:
18791 		  fputs (" HWOR      ", stdout);
18792 		  if (option.info & OHWA0_R4KEOP_CHECKED)
18793 		    fputs (" R4KEOP_CHECKED", stdout);
18794 		  if (option.info & OHWA0_R4KEOP_CLEAN)
18795 		    fputs (" R4KEOP_CLEAN", stdout);
18796 		  break;
18797 
18798 		case ODK_GP_GROUP:
18799 		  printf (" GP_GROUP  %#06x  self-contained %#06x",
18800 			  option.info & OGP_GROUP,
18801 			  (option.info & OGP_SELF) >> 16);
18802 		  break;
18803 
18804 		case ODK_IDENT:
18805 		  printf (" IDENT     %#06x  self-contained %#06x",
18806 			  option.info & OGP_GROUP,
18807 			  (option.info & OGP_SELF) >> 16);
18808 		  break;
18809 
18810 		default:
18811 		  /* This shouldn't happen.  */
18812 		  printf (" %3d ???     %" PRId16 " %" PRIx32,
18813 			  option.kind, option.section, option.info);
18814 		  break;
18815 		}
18816 
18817 	      len = sizeof (* eopt);
18818 	      while (len < option.size)
18819 		{
18820 		  unsigned char datum = *((unsigned char *) eoption + len);
18821 
18822 		  if (ISPRINT (datum))
18823 		    printf ("%c", datum);
18824 		  else
18825 		    printf ("\\%03o", datum);
18826 		  len ++;
18827 		}
18828 	      fputs ("\n", stdout);
18829 
18830 	      offset += option.size;
18831 	    }
18832 	  free (eopt);
18833 	}
18834       else
18835 	res = false;
18836     }
18837 
18838   if (conflicts_offset != 0 && conflictsno != 0)
18839     {
18840       Elf32_Conflict * iconf;
18841       size_t cnt;
18842 
18843       if (filedata->dynamic_symbols == NULL)
18844 	{
18845 	  error (_("conflict list found without a dynamic symbol table\n"));
18846 	  return false;
18847 	}
18848 
18849       /* PR 21345 - print a slightly more helpful error message
18850 	 if we are sure that the cmalloc will fail.  */
18851       if (conflictsno > filedata->file_size / sizeof (* iconf))
18852 	{
18853 	  error (_("Overlarge number of conflicts detected: %lx\n"),
18854 		 (long) conflictsno);
18855 	  return false;
18856 	}
18857 
18858       iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18859       if (iconf == NULL)
18860 	{
18861 	  error (_("Out of memory allocating space for dynamic conflicts\n"));
18862 	  return false;
18863 	}
18864 
18865       if (is_32bit_elf)
18866 	{
18867 	  Elf32_External_Conflict * econf32;
18868 
18869 	  econf32 = (Elf32_External_Conflict *)
18870 	    get_data (NULL, filedata, conflicts_offset,
18871 		      sizeof (*econf32), conflictsno, _("conflict"));
18872 	  if (!econf32)
18873 	    {
18874 	      free (iconf);
18875 	      return false;
18876 	    }
18877 
18878 	  for (cnt = 0; cnt < conflictsno; ++cnt)
18879 	    iconf[cnt] = BYTE_GET (econf32[cnt]);
18880 
18881 	  free (econf32);
18882 	}
18883       else
18884 	{
18885 	  Elf64_External_Conflict * econf64;
18886 
18887 	  econf64 = (Elf64_External_Conflict *)
18888 	    get_data (NULL, filedata, conflicts_offset,
18889 		      sizeof (*econf64), conflictsno, _("conflict"));
18890 	  if (!econf64)
18891 	    {
18892 	      free (iconf);
18893 	      return false;
18894 	    }
18895 
18896 	  for (cnt = 0; cnt < conflictsno; ++cnt)
18897 	    iconf[cnt] = BYTE_GET (econf64[cnt]);
18898 
18899 	  free (econf64);
18900 	}
18901 
18902       printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18903 			"\nSection '.conflict' contains %lu entries:\n",
18904 			(unsigned long) conflictsno),
18905 	      (unsigned long) conflictsno);
18906       puts (_("  Num:    Index       Value  Name"));
18907 
18908       for (cnt = 0; cnt < conflictsno; ++cnt)
18909 	{
18910 	  printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
18911 
18912 	  if (iconf[cnt] >= filedata->num_dynamic_syms)
18913 	    printf (_("<corrupt symbol index>"));
18914 	  else
18915 	    {
18916 	      Elf_Internal_Sym * psym;
18917 
18918 	      psym = & filedata->dynamic_symbols[iconf[cnt]];
18919 	      print_vma (psym->st_value, FULL_HEX);
18920 	      putchar (' ');
18921 	      if (valid_dynamic_name (filedata, psym->st_name))
18922 		print_symbol (25, get_dynamic_name (filedata, psym->st_name));
18923 	      else
18924 		printf (_("<corrupt: %14ld>"), psym->st_name);
18925 	    }
18926 	  putchar ('\n');
18927 	}
18928 
18929       free (iconf);
18930     }
18931 
18932   if (pltgot != 0 && local_gotno != 0)
18933     {
18934       bfd_vma ent, local_end, global_end;
18935       size_t i, offset;
18936       unsigned char * data;
18937       unsigned char * data_end;
18938       int addr_size;
18939 
18940       ent = pltgot;
18941       addr_size = (is_32bit_elf ? 4 : 8);
18942       local_end = pltgot + local_gotno * addr_size;
18943 
18944       /* PR binutils/17533 file: 012-111227-0.004  */
18945       if (symtabno < gotsym)
18946 	{
18947 	  error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18948 		 (unsigned long) gotsym, (unsigned long) symtabno);
18949 	  return false;
18950 	}
18951 
18952       global_end = local_end + (symtabno - gotsym) * addr_size;
18953       /* PR 17531: file: 54c91a34.  */
18954       if (global_end < local_end)
18955 	{
18956 	  error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18957 	  return false;
18958 	}
18959 
18960       offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18961       data = (unsigned char *) get_data (NULL, filedata, offset,
18962                                          global_end - pltgot, 1,
18963 					 _("Global Offset Table data"));
18964       /* PR 12855: Null data is handled gracefully throughout.  */
18965       data_end = data + (global_end - pltgot);
18966 
18967       printf (_("\nPrimary GOT:\n"));
18968       printf (_(" Canonical gp value: "));
18969       print_vma (pltgot + 0x7ff0, LONG_HEX);
18970       printf ("\n\n");
18971 
18972       printf (_(" Reserved entries:\n"));
18973       printf (_("  %*s %10s %*s Purpose\n"),
18974 	      addr_size * 2, _("Address"), _("Access"),
18975 	      addr_size * 2, _("Initial"));
18976       ent = print_mips_got_entry (data, pltgot, ent, data_end);
18977       printf (_(" Lazy resolver\n"));
18978       if (ent == (bfd_vma) -1)
18979 	goto got_print_fail;
18980 
18981       /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18982 	 This entry will be used by some runtime loaders, to store the
18983 	 module pointer.  Otherwise this is an ordinary local entry.
18984 	 PR 21344: Check for the entry being fully available before
18985 	 fetching it.  */
18986       if (data
18987 	  && data + ent - pltgot + addr_size <= data_end
18988 	  && (byte_get (data + ent - pltgot, addr_size)
18989 	      >> (addr_size * 8 - 1)) != 0)
18990 	{
18991 	  ent = print_mips_got_entry (data, pltgot, ent, data_end);
18992 	  printf (_(" Module pointer (GNU extension)\n"));
18993 	  if (ent == (bfd_vma) -1)
18994 	    goto got_print_fail;
18995 	}
18996       printf ("\n");
18997 
18998       if (data != NULL && ent < local_end)
18999 	{
19000 	  printf (_(" Local entries:\n"));
19001 	  printf ("  %*s %10s %*s\n",
19002 		  addr_size * 2, _("Address"), _("Access"),
19003 		  addr_size * 2, _("Initial"));
19004 	  while (ent < local_end)
19005 	    {
19006 	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
19007 	      printf ("\n");
19008 	      if (ent == (bfd_vma) -1)
19009 		goto got_print_fail;
19010 	    }
19011 	  printf ("\n");
19012 	}
19013 
19014       if (data != NULL && gotsym < symtabno)
19015 	{
19016 	  int sym_width;
19017 
19018 	  printf (_(" Global entries:\n"));
19019 	  printf ("  %*s %10s %*s %*s %-7s %3s %s\n",
19020 		  addr_size * 2, _("Address"),
19021 		  _("Access"),
19022 		  addr_size * 2, _("Initial"),
19023 		  addr_size * 2, _("Sym.Val."),
19024 		  _("Type"),
19025 		  /* Note for translators: "Ndx" = abbreviated form of "Index".  */
19026 		  _("Ndx"), _("Name"));
19027 
19028 	  sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
19029 
19030 	  for (i = gotsym; i < symtabno; i++)
19031 	    {
19032 	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
19033 	      printf (" ");
19034 
19035 	      if (filedata->dynamic_symbols == NULL)
19036 		printf (_("<no dynamic symbols>"));
19037 	      else if (i < filedata->num_dynamic_syms)
19038 		{
19039 		  Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
19040 
19041 		  print_vma (psym->st_value, LONG_HEX);
19042 		  printf (" %-7s %3s ",
19043 			  get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19044 			  get_symbol_index_type (filedata, psym->st_shndx));
19045 
19046 		  if (valid_dynamic_name (filedata, psym->st_name))
19047 		    print_symbol (sym_width,
19048 				  get_dynamic_name (filedata, psym->st_name));
19049 		  else
19050 		    printf (_("<corrupt: %14ld>"), psym->st_name);
19051 		}
19052 	      else
19053 		printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
19054 			(unsigned long) i);
19055 
19056 	      printf ("\n");
19057 	      if (ent == (bfd_vma) -1)
19058 		break;
19059 	    }
19060 	  printf ("\n");
19061 	}
19062 
19063     got_print_fail:
19064       free (data);
19065     }
19066 
19067   if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19068     {
19069       bfd_vma ent, end;
19070       size_t offset, rel_offset;
19071       unsigned long count, i;
19072       unsigned char * data;
19073       int addr_size, sym_width;
19074       Elf_Internal_Rela * rels;
19075 
19076       rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
19077       if (pltrel == DT_RELA)
19078 	{
19079 	  if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19080 	    return false;
19081 	}
19082       else
19083 	{
19084 	  if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19085 	    return false;
19086 	}
19087 
19088       ent = mips_pltgot;
19089       addr_size = (is_32bit_elf ? 4 : 8);
19090       end = mips_pltgot + (2 + count) * addr_size;
19091 
19092       offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19093       data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
19094                                          1, _("Procedure Linkage Table data"));
19095       if (data == NULL)
19096 	{
19097 	  free (rels);
19098 	  return false;
19099 	}
19100 
19101       printf ("\nPLT GOT:\n\n");
19102       printf (_(" Reserved entries:\n"));
19103       printf (_("  %*s %*s Purpose\n"),
19104 	      addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
19105       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19106       printf (_(" PLT lazy resolver\n"));
19107       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19108       printf (_(" Module pointer\n"));
19109       printf ("\n");
19110 
19111       printf (_(" Entries:\n"));
19112       printf ("  %*s %*s %*s %-7s %3s %s\n",
19113 	      addr_size * 2, _("Address"),
19114 	      addr_size * 2, _("Initial"),
19115 	      addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
19116       sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19117       for (i = 0; i < count; i++)
19118 	{
19119 	  unsigned long idx = get_reloc_symindex (rels[i].r_info);
19120 
19121 	  ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19122 	  printf (" ");
19123 
19124 	  if (idx >= filedata->num_dynamic_syms)
19125 	    printf (_("<corrupt symbol index: %lu>"), idx);
19126 	  else
19127 	    {
19128 	      Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
19129 
19130 	      print_vma (psym->st_value, LONG_HEX);
19131 	      printf (" %-7s %3s ",
19132 		      get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19133 		      get_symbol_index_type (filedata, psym->st_shndx));
19134 	      if (valid_dynamic_name (filedata, psym->st_name))
19135 		print_symbol (sym_width,
19136 			      get_dynamic_name (filedata, psym->st_name));
19137 	      else
19138 		printf (_("<corrupt: %14ld>"), psym->st_name);
19139 	    }
19140 	  printf ("\n");
19141 	}
19142       printf ("\n");
19143 
19144       free (data);
19145       free (rels);
19146     }
19147 
19148   return res;
19149 }
19150 
19151 static bool
19152 process_nds32_specific (Filedata * filedata)
19153 {
19154   Elf_Internal_Shdr *sect = NULL;
19155 
19156   sect = find_section (filedata, ".nds32_e_flags");
19157   if (sect != NULL && sect->sh_size >= 4)
19158     {
19159       unsigned char *buf;
19160       unsigned int flag;
19161 
19162       printf ("\nNDS32 elf flags section:\n");
19163       buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19164 		      _("NDS32 elf flags section"));
19165 
19166       if (buf == NULL)
19167 	return false;
19168 
19169       flag = byte_get (buf, 4);
19170       free (buf);
19171       switch (flag & 0x3)
19172 	{
19173 	case 0:
19174 	  printf ("(VEC_SIZE):\tNo entry.\n");
19175 	  break;
19176 	case 1:
19177 	  printf ("(VEC_SIZE):\t4 bytes\n");
19178 	  break;
19179 	case 2:
19180 	  printf ("(VEC_SIZE):\t16 bytes\n");
19181 	  break;
19182 	case 3:
19183 	  printf ("(VEC_SIZE):\treserved\n");
19184 	  break;
19185 	}
19186     }
19187 
19188   return true;
19189 }
19190 
19191 static bool
19192 process_gnu_liblist (Filedata * filedata)
19193 {
19194   Elf_Internal_Shdr * section;
19195   Elf_Internal_Shdr * string_sec;
19196   Elf32_External_Lib * elib;
19197   char * strtab;
19198   size_t strtab_size;
19199   size_t cnt;
19200   unsigned long num_liblist;
19201   unsigned i;
19202   bool res = true;
19203 
19204   if (! do_arch)
19205     return true;
19206 
19207   for (i = 0, section = filedata->section_headers;
19208        i < filedata->file_header.e_shnum;
19209        i++, section++)
19210     {
19211       switch (section->sh_type)
19212 	{
19213 	case SHT_GNU_LIBLIST:
19214 	  if (section->sh_link >= filedata->file_header.e_shnum)
19215 	    break;
19216 
19217 	  elib = (Elf32_External_Lib *)
19218               get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
19219                         _("liblist section data"));
19220 
19221 	  if (elib == NULL)
19222 	    {
19223 	      res = false;
19224 	      break;
19225 	    }
19226 
19227 	  string_sec = filedata->section_headers + section->sh_link;
19228 	  strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
19229                                       string_sec->sh_size,
19230                                       _("liblist string table"));
19231 	  if (strtab == NULL
19232 	      || section->sh_entsize != sizeof (Elf32_External_Lib))
19233 	    {
19234 	      free (elib);
19235 	      free (strtab);
19236 	      res = false;
19237 	      break;
19238 	    }
19239 	  strtab_size = string_sec->sh_size;
19240 
19241 	  num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
19242 	  printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
19243 			    "\nLibrary list section '%s' contains %lu entries:\n",
19244 			    num_liblist),
19245 		  printable_section_name (filedata, section),
19246 		  num_liblist);
19247 
19248 	  puts (_("     Library              Time Stamp          Checksum   Version Flags"));
19249 
19250 	  for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
19251 	       ++cnt)
19252 	    {
19253 	      Elf32_Lib liblist;
19254 	      time_t atime;
19255 	      char timebuf[128];
19256 	      struct tm * tmp;
19257 
19258 	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
19259 	      atime = BYTE_GET (elib[cnt].l_time_stamp);
19260 	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19261 	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
19262 	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19263 
19264 	      tmp = gmtime (&atime);
19265 	      snprintf (timebuf, sizeof (timebuf),
19266 			"%04u-%02u-%02uT%02u:%02u:%02u",
19267 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19268 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19269 
19270 	      printf ("%3lu: ", (unsigned long) cnt);
19271 	      if (do_wide)
19272 		printf ("%-20s", liblist.l_name < strtab_size
19273 			? strtab + liblist.l_name : _("<corrupt>"));
19274 	      else
19275 		printf ("%-20.20s", liblist.l_name < strtab_size
19276 			? strtab + liblist.l_name : _("<corrupt>"));
19277 	      printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
19278 		      liblist.l_version, liblist.l_flags);
19279 	    }
19280 
19281 	  free (elib);
19282 	  free (strtab);
19283 	}
19284     }
19285 
19286   return res;
19287 }
19288 
19289 static const char *
19290 get_note_type (Filedata * filedata, unsigned e_type)
19291 {
19292   static char buff[64];
19293 
19294   if (filedata->file_header.e_type == ET_CORE)
19295     switch (e_type)
19296       {
19297       case NT_AUXV:
19298 	return _("NT_AUXV (auxiliary vector)");
19299       case NT_PRSTATUS:
19300 	return _("NT_PRSTATUS (prstatus structure)");
19301       case NT_FPREGSET:
19302 	return _("NT_FPREGSET (floating point registers)");
19303       case NT_PRPSINFO:
19304 	return _("NT_PRPSINFO (prpsinfo structure)");
19305       case NT_TASKSTRUCT:
19306 	return _("NT_TASKSTRUCT (task structure)");
19307       case NT_GDB_TDESC:
19308         return _("NT_GDB_TDESC (GDB XML target description)");
19309       case NT_PRXFPREG:
19310 	return _("NT_PRXFPREG (user_xfpregs structure)");
19311       case NT_PPC_VMX:
19312 	return _("NT_PPC_VMX (ppc Altivec registers)");
19313       case NT_PPC_VSX:
19314 	return _("NT_PPC_VSX (ppc VSX registers)");
19315       case NT_PPC_TAR:
19316 	return _("NT_PPC_TAR (ppc TAR register)");
19317       case NT_PPC_PPR:
19318 	return _("NT_PPC_PPR (ppc PPR register)");
19319       case NT_PPC_DSCR:
19320 	return _("NT_PPC_DSCR (ppc DSCR register)");
19321       case NT_PPC_EBB:
19322 	return _("NT_PPC_EBB (ppc EBB registers)");
19323       case NT_PPC_PMU:
19324 	return _("NT_PPC_PMU (ppc PMU registers)");
19325       case NT_PPC_TM_CGPR:
19326 	return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
19327       case NT_PPC_TM_CFPR:
19328 	return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
19329       case NT_PPC_TM_CVMX:
19330 	return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
19331       case NT_PPC_TM_CVSX:
19332 	return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
19333       case NT_PPC_TM_SPR:
19334 	return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
19335       case NT_PPC_TM_CTAR:
19336 	return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
19337       case NT_PPC_TM_CPPR:
19338 	return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
19339       case NT_PPC_TM_CDSCR:
19340 	return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
19341       case NT_386_TLS:
19342 	return _("NT_386_TLS (x86 TLS information)");
19343       case NT_386_IOPERM:
19344 	return _("NT_386_IOPERM (x86 I/O permissions)");
19345       case NT_X86_XSTATE:
19346 	return _("NT_X86_XSTATE (x86 XSAVE extended state)");
19347       case NT_X86_CET:
19348 	return _("NT_X86_CET (x86 CET state)");
19349       case NT_S390_HIGH_GPRS:
19350 	return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
19351       case NT_S390_TIMER:
19352 	return _("NT_S390_TIMER (s390 timer register)");
19353       case NT_S390_TODCMP:
19354 	return _("NT_S390_TODCMP (s390 TOD comparator register)");
19355       case NT_S390_TODPREG:
19356 	return _("NT_S390_TODPREG (s390 TOD programmable register)");
19357       case NT_S390_CTRS:
19358 	return _("NT_S390_CTRS (s390 control registers)");
19359       case NT_S390_PREFIX:
19360 	return _("NT_S390_PREFIX (s390 prefix register)");
19361       case NT_S390_LAST_BREAK:
19362 	return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
19363       case NT_S390_SYSTEM_CALL:
19364 	return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
19365       case NT_S390_TDB:
19366 	return _("NT_S390_TDB (s390 transaction diagnostic block)");
19367       case NT_S390_VXRS_LOW:
19368 	return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
19369       case NT_S390_VXRS_HIGH:
19370 	return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
19371       case NT_S390_GS_CB:
19372 	return _("NT_S390_GS_CB (s390 guarded-storage registers)");
19373       case NT_S390_GS_BC:
19374 	return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
19375       case NT_ARM_VFP:
19376 	return _("NT_ARM_VFP (arm VFP registers)");
19377       case NT_ARM_TLS:
19378 	return _("NT_ARM_TLS (AArch TLS registers)");
19379       case NT_ARM_HW_BREAK:
19380 	return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
19381       case NT_ARM_HW_WATCH:
19382 	return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
19383       case NT_ARM_SYSTEM_CALL:
19384 	return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
19385       case NT_ARM_SVE:
19386 	return _("NT_ARM_SVE (AArch SVE registers)");
19387       case NT_ARM_PAC_MASK:
19388 	return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
19389       case NT_ARM_PACA_KEYS:
19390 	return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
19391       case NT_ARM_PACG_KEYS:
19392 	return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
19393       case NT_ARM_TAGGED_ADDR_CTRL:
19394 	return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
19395       case NT_ARM_PAC_ENABLED_KEYS:
19396 	return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
19397       case NT_ARC_V2:
19398 	return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
19399       case NT_RISCV_CSR:
19400 	return _("NT_RISCV_CSR (RISC-V control and status registers)");
19401       case NT_PSTATUS:
19402 	return _("NT_PSTATUS (pstatus structure)");
19403       case NT_FPREGS:
19404 	return _("NT_FPREGS (floating point registers)");
19405       case NT_PSINFO:
19406 	return _("NT_PSINFO (psinfo structure)");
19407       case NT_LWPSTATUS:
19408 	return _("NT_LWPSTATUS (lwpstatus_t structure)");
19409       case NT_LWPSINFO:
19410 	return _("NT_LWPSINFO (lwpsinfo_t structure)");
19411       case NT_WIN32PSTATUS:
19412 	return _("NT_WIN32PSTATUS (win32_pstatus structure)");
19413       case NT_SIGINFO:
19414 	return _("NT_SIGINFO (siginfo_t data)");
19415       case NT_FILE:
19416 	return _("NT_FILE (mapped files)");
19417       default:
19418 	break;
19419       }
19420   else
19421     switch (e_type)
19422       {
19423       case NT_VERSION:
19424 	return _("NT_VERSION (version)");
19425       case NT_ARCH:
19426 	return _("NT_ARCH (architecture)");
19427       case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19428 	return _("OPEN");
19429       case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19430 	return _("func");
19431       case NT_GO_BUILDID:
19432 	return _("GO BUILDID");
19433       case FDO_PACKAGING_METADATA:
19434 	return _("FDO_PACKAGING_METADATA");
19435       default:
19436 	break;
19437       }
19438 
19439   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19440   return buff;
19441 }
19442 
19443 static bool
19444 print_core_note (Elf_Internal_Note *pnote)
19445 {
19446   unsigned int addr_size = is_32bit_elf ? 4 : 8;
19447   bfd_vma count, page_size;
19448   unsigned char *descdata, *filenames, *descend;
19449 
19450   if (pnote->type != NT_FILE)
19451     {
19452       if (do_wide)
19453 	printf ("\n");
19454       return true;
19455     }
19456 
19457 #ifndef BFD64
19458   if (!is_32bit_elf)
19459     {
19460       printf (_("    Cannot decode 64-bit note in 32-bit build\n"));
19461       /* Still "successful".  */
19462       return true;
19463     }
19464 #endif
19465 
19466   if (pnote->descsz < 2 * addr_size)
19467     {
19468       error (_("    Malformed note - too short for header\n"));
19469       return false;
19470     }
19471 
19472   descdata = (unsigned char *) pnote->descdata;
19473   descend = descdata + pnote->descsz;
19474 
19475   if (descdata[pnote->descsz - 1] != '\0')
19476     {
19477       error (_("    Malformed note - does not end with \\0\n"));
19478       return false;
19479     }
19480 
19481   count = byte_get (descdata, addr_size);
19482   descdata += addr_size;
19483 
19484   page_size = byte_get (descdata, addr_size);
19485   descdata += addr_size;
19486 
19487   if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
19488       || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
19489     {
19490       error (_("    Malformed note - too short for supplied file count\n"));
19491       return false;
19492     }
19493 
19494   printf (_("    Page size: "));
19495   print_vma (page_size, DEC);
19496   printf ("\n");
19497 
19498   printf (_("    %*s%*s%*s\n"),
19499 	  (int) (2 + 2 * addr_size), _("Start"),
19500 	  (int) (4 + 2 * addr_size), _("End"),
19501 	  (int) (4 + 2 * addr_size), _("Page Offset"));
19502   filenames = descdata + count * 3 * addr_size;
19503   while (count-- > 0)
19504     {
19505       bfd_vma start, end, file_ofs;
19506 
19507       if (filenames == descend)
19508 	{
19509 	  error (_("    Malformed note - filenames end too early\n"));
19510 	  return false;
19511 	}
19512 
19513       start = byte_get (descdata, addr_size);
19514       descdata += addr_size;
19515       end = byte_get (descdata, addr_size);
19516       descdata += addr_size;
19517       file_ofs = byte_get (descdata, addr_size);
19518       descdata += addr_size;
19519 
19520       printf ("    ");
19521       print_vma (start, FULL_HEX);
19522       printf ("  ");
19523       print_vma (end, FULL_HEX);
19524       printf ("  ");
19525       print_vma (file_ofs, FULL_HEX);
19526       printf ("\n        %s\n", filenames);
19527 
19528       filenames += 1 + strlen ((char *) filenames);
19529     }
19530 
19531   return true;
19532 }
19533 
19534 static const char *
19535 get_gnu_elf_note_type (unsigned e_type)
19536 {
19537   /* NB/ Keep this switch statement in sync with print_gnu_note ().  */
19538   switch (e_type)
19539     {
19540     case NT_GNU_ABI_TAG:
19541       return _("NT_GNU_ABI_TAG (ABI version tag)");
19542     case NT_GNU_HWCAP:
19543       return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19544     case NT_GNU_BUILD_ID:
19545       return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19546     case NT_GNU_GOLD_VERSION:
19547       return _("NT_GNU_GOLD_VERSION (gold version)");
19548     case NT_GNU_PROPERTY_TYPE_0:
19549       return _("NT_GNU_PROPERTY_TYPE_0");
19550     case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19551       return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19552     case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19553       return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19554     default:
19555       {
19556 	static char buff[64];
19557 
19558 	snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19559 	return buff;
19560       }
19561     }
19562 }
19563 
19564 static void
19565 decode_x86_compat_isa (unsigned int bitmask)
19566 {
19567   while (bitmask)
19568     {
19569       unsigned int bit = bitmask & (- bitmask);
19570 
19571       bitmask &= ~ bit;
19572       switch (bit)
19573 	{
19574 	case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19575 	  printf ("i486");
19576 	  break;
19577 	case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19578 	  printf ("586");
19579 	  break;
19580 	case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19581 	  printf ("686");
19582 	  break;
19583 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19584 	  printf ("SSE");
19585 	  break;
19586 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19587 	  printf ("SSE2");
19588 	  break;
19589 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19590 	  printf ("SSE3");
19591 	  break;
19592 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19593 	  printf ("SSSE3");
19594 	  break;
19595 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19596 	  printf ("SSE4_1");
19597 	  break;
19598 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19599 	  printf ("SSE4_2");
19600 	  break;
19601 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19602 	  printf ("AVX");
19603 	  break;
19604 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19605 	  printf ("AVX2");
19606 	  break;
19607 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19608 	  printf ("AVX512F");
19609 	  break;
19610 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19611 	  printf ("AVX512CD");
19612 	  break;
19613 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19614 	  printf ("AVX512ER");
19615 	  break;
19616 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19617 	  printf ("AVX512PF");
19618 	  break;
19619 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19620 	  printf ("AVX512VL");
19621 	  break;
19622 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19623 	  printf ("AVX512DQ");
19624 	  break;
19625 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19626 	  printf ("AVX512BW");
19627 	  break;
19628 	default:
19629 	  printf (_("<unknown: %x>"), bit);
19630 	  break;
19631 	}
19632       if (bitmask)
19633 	printf (", ");
19634     }
19635 }
19636 
19637 static void
19638 decode_x86_compat_2_isa (unsigned int bitmask)
19639 {
19640   if (!bitmask)
19641     {
19642       printf (_("<None>"));
19643       return;
19644     }
19645 
19646   while (bitmask)
19647     {
19648       unsigned int bit = bitmask & (- bitmask);
19649 
19650       bitmask &= ~ bit;
19651       switch (bit)
19652 	{
19653 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19654 	  printf ("CMOV");
19655 	  break;
19656 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19657 	  printf ("SSE");
19658 	  break;
19659 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19660 	  printf ("SSE2");
19661 	  break;
19662 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19663 	  printf ("SSE3");
19664 	  break;
19665 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19666 	  printf ("SSSE3");
19667 	  break;
19668 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19669 	  printf ("SSE4_1");
19670 	  break;
19671 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19672 	  printf ("SSE4_2");
19673 	  break;
19674 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19675 	  printf ("AVX");
19676 	  break;
19677 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19678 	  printf ("AVX2");
19679 	  break;
19680 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19681 	  printf ("FMA");
19682 	  break;
19683 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19684 	  printf ("AVX512F");
19685 	  break;
19686 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19687 	  printf ("AVX512CD");
19688 	  break;
19689 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19690 	  printf ("AVX512ER");
19691 	  break;
19692 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19693 	  printf ("AVX512PF");
19694 	  break;
19695 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19696 	  printf ("AVX512VL");
19697 	  break;
19698 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19699 	  printf ("AVX512DQ");
19700 	  break;
19701 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19702 	  printf ("AVX512BW");
19703 	  break;
19704 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19705 	  printf ("AVX512_4FMAPS");
19706 	  break;
19707 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19708 	  printf ("AVX512_4VNNIW");
19709 	  break;
19710 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19711 	  printf ("AVX512_BITALG");
19712 	  break;
19713 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19714 	  printf ("AVX512_IFMA");
19715 	  break;
19716 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19717 	  printf ("AVX512_VBMI");
19718 	  break;
19719 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19720 	  printf ("AVX512_VBMI2");
19721 	  break;
19722 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19723 	  printf ("AVX512_VNNI");
19724 	  break;
19725 	case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19726 	  printf ("AVX512_BF16");
19727 	  break;
19728 	default:
19729 	  printf (_("<unknown: %x>"), bit);
19730 	  break;
19731 	}
19732       if (bitmask)
19733 	printf (", ");
19734     }
19735 }
19736 
19737 static const char *
19738 get_amdgpu_elf_note_type (unsigned int e_type)
19739 {
19740   switch (e_type)
19741     {
19742     case NT_AMDGPU_METADATA:
19743       return _("NT_AMDGPU_METADATA (code object metadata)");
19744     default:
19745       {
19746 	static char buf[64];
19747 	snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
19748 	return buf;
19749       }
19750     }
19751 }
19752 
19753 static void
19754 decode_x86_isa (unsigned int bitmask)
19755 {
19756   while (bitmask)
19757     {
19758       unsigned int bit = bitmask & (- bitmask);
19759 
19760       bitmask &= ~ bit;
19761       switch (bit)
19762 	{
19763 	case GNU_PROPERTY_X86_ISA_1_BASELINE:
19764 	  printf ("x86-64-baseline");
19765 	  break;
19766 	case GNU_PROPERTY_X86_ISA_1_V2:
19767 	  printf ("x86-64-v2");
19768 	  break;
19769 	case GNU_PROPERTY_X86_ISA_1_V3:
19770 	  printf ("x86-64-v3");
19771 	  break;
19772 	case GNU_PROPERTY_X86_ISA_1_V4:
19773 	  printf ("x86-64-v4");
19774 	  break;
19775 	default:
19776 	  printf (_("<unknown: %x>"), bit);
19777 	  break;
19778 	}
19779       if (bitmask)
19780 	printf (", ");
19781     }
19782 }
19783 
19784 static void
19785 decode_x86_feature_1 (unsigned int bitmask)
19786 {
19787   if (!bitmask)
19788     {
19789       printf (_("<None>"));
19790       return;
19791     }
19792 
19793   while (bitmask)
19794     {
19795       unsigned int bit = bitmask & (- bitmask);
19796 
19797       bitmask &= ~ bit;
19798       switch (bit)
19799 	{
19800 	case GNU_PROPERTY_X86_FEATURE_1_IBT:
19801 	  printf ("IBT");
19802 	  break;
19803 	case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19804 	  printf ("SHSTK");
19805 	  break;
19806 	case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19807 	  printf ("LAM_U48");
19808 	  break;
19809 	case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19810 	  printf ("LAM_U57");
19811 	  break;
19812 	default:
19813 	  printf (_("<unknown: %x>"), bit);
19814 	  break;
19815 	}
19816       if (bitmask)
19817 	printf (", ");
19818     }
19819 }
19820 
19821 static void
19822 decode_x86_feature_2 (unsigned int bitmask)
19823 {
19824   if (!bitmask)
19825     {
19826       printf (_("<None>"));
19827       return;
19828     }
19829 
19830   while (bitmask)
19831     {
19832       unsigned int bit = bitmask & (- bitmask);
19833 
19834       bitmask &= ~ bit;
19835       switch (bit)
19836 	{
19837 	case GNU_PROPERTY_X86_FEATURE_2_X86:
19838 	  printf ("x86");
19839 	  break;
19840 	case GNU_PROPERTY_X86_FEATURE_2_X87:
19841 	  printf ("x87");
19842 	  break;
19843 	case GNU_PROPERTY_X86_FEATURE_2_MMX:
19844 	  printf ("MMX");
19845 	  break;
19846 	case GNU_PROPERTY_X86_FEATURE_2_XMM:
19847 	  printf ("XMM");
19848 	  break;
19849 	case GNU_PROPERTY_X86_FEATURE_2_YMM:
19850 	  printf ("YMM");
19851 	  break;
19852 	case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19853 	  printf ("ZMM");
19854 	  break;
19855 	case GNU_PROPERTY_X86_FEATURE_2_TMM:
19856 	  printf ("TMM");
19857 	  break;
19858 	case GNU_PROPERTY_X86_FEATURE_2_MASK:
19859 	  printf ("MASK");
19860 	  break;
19861 	case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19862 	  printf ("FXSR");
19863 	  break;
19864 	case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19865 	  printf ("XSAVE");
19866 	  break;
19867 	case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19868 	  printf ("XSAVEOPT");
19869 	  break;
19870 	case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19871 	  printf ("XSAVEC");
19872 	  break;
19873 	default:
19874 	  printf (_("<unknown: %x>"), bit);
19875 	  break;
19876 	}
19877       if (bitmask)
19878 	printf (", ");
19879     }
19880 }
19881 
19882 static void
19883 decode_aarch64_feature_1_and (unsigned int bitmask)
19884 {
19885   while (bitmask)
19886     {
19887       unsigned int bit = bitmask & (- bitmask);
19888 
19889       bitmask &= ~ bit;
19890       switch (bit)
19891 	{
19892 	case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19893 	  printf ("BTI");
19894 	  break;
19895 
19896 	case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19897 	  printf ("PAC");
19898 	  break;
19899 
19900 	default:
19901 	  printf (_("<unknown: %x>"), bit);
19902 	  break;
19903 	}
19904       if (bitmask)
19905 	printf (", ");
19906     }
19907 }
19908 
19909 static void
19910 decode_1_needed (unsigned int bitmask)
19911 {
19912   while (bitmask)
19913     {
19914       unsigned int bit = bitmask & (- bitmask);
19915 
19916       bitmask &= ~ bit;
19917       switch (bit)
19918 	{
19919 	case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
19920 	  printf ("indirect external access");
19921 	  break;
19922 	default:
19923 	  printf (_("<unknown: %x>"), bit);
19924 	  break;
19925 	}
19926       if (bitmask)
19927 	printf (", ");
19928     }
19929 }
19930 
19931 static void
19932 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19933 {
19934   unsigned char * ptr = (unsigned char *) pnote->descdata;
19935   unsigned char * ptr_end = ptr + pnote->descsz;
19936   unsigned int    size = is_32bit_elf ? 4 : 8;
19937 
19938   printf (_("      Properties: "));
19939 
19940   if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19941     {
19942       printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19943       return;
19944     }
19945 
19946   while (ptr < ptr_end)
19947     {
19948       unsigned int j;
19949       unsigned int type;
19950       unsigned int datasz;
19951 
19952       if ((size_t) (ptr_end - ptr) < 8)
19953 	{
19954 	  printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19955 	  break;
19956 	}
19957 
19958       type = byte_get (ptr, 4);
19959       datasz = byte_get (ptr + 4, 4);
19960 
19961       ptr += 8;
19962 
19963       if (datasz > (size_t) (ptr_end - ptr))
19964 	{
19965 	  printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19966 		  type, datasz);
19967 	  break;
19968 	}
19969 
19970       if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19971 	{
19972 	  if (filedata->file_header.e_machine == EM_X86_64
19973 	      || filedata->file_header.e_machine == EM_IAMCU
19974 	      || filedata->file_header.e_machine == EM_386)
19975 	    {
19976 	      unsigned int bitmask;
19977 
19978 	      if (datasz == 4)
19979 		bitmask = byte_get (ptr, 4);
19980 	      else
19981 		bitmask = 0;
19982 
19983 	      switch (type)
19984 		{
19985 		case GNU_PROPERTY_X86_ISA_1_USED:
19986 		  if (datasz != 4)
19987 		    printf (_("x86 ISA used: <corrupt length: %#x> "),
19988 			    datasz);
19989 		  else
19990 		    {
19991 		      printf ("x86 ISA used: ");
19992 		      decode_x86_isa (bitmask);
19993 		    }
19994 		  goto next;
19995 
19996 		case GNU_PROPERTY_X86_ISA_1_NEEDED:
19997 		  if (datasz != 4)
19998 		    printf (_("x86 ISA needed: <corrupt length: %#x> "),
19999 			    datasz);
20000 		  else
20001 		    {
20002 		      printf ("x86 ISA needed: ");
20003 		      decode_x86_isa (bitmask);
20004 		    }
20005 		  goto next;
20006 
20007 		case GNU_PROPERTY_X86_FEATURE_1_AND:
20008 		  if (datasz != 4)
20009 		    printf (_("x86 feature: <corrupt length: %#x> "),
20010 			    datasz);
20011 		  else
20012 		    {
20013 		      printf ("x86 feature: ");
20014 		      decode_x86_feature_1 (bitmask);
20015 		    }
20016 		  goto next;
20017 
20018 		case GNU_PROPERTY_X86_FEATURE_2_USED:
20019 		  if (datasz != 4)
20020 		    printf (_("x86 feature used: <corrupt length: %#x> "),
20021 			    datasz);
20022 		  else
20023 		    {
20024 		      printf ("x86 feature used: ");
20025 		      decode_x86_feature_2 (bitmask);
20026 		    }
20027 		  goto next;
20028 
20029 		case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20030 		  if (datasz != 4)
20031 		    printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20032 		  else
20033 		    {
20034 		      printf ("x86 feature needed: ");
20035 		      decode_x86_feature_2 (bitmask);
20036 		    }
20037 		  goto next;
20038 
20039 		case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20040 		  if (datasz != 4)
20041 		    printf (_("x86 ISA used: <corrupt length: %#x> "),
20042 			    datasz);
20043 		  else
20044 		    {
20045 		      printf ("x86 ISA used: ");
20046 		      decode_x86_compat_isa (bitmask);
20047 		    }
20048 		  goto next;
20049 
20050 		case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20051 		  if (datasz != 4)
20052 		    printf (_("x86 ISA needed: <corrupt length: %#x> "),
20053 			    datasz);
20054 		  else
20055 		    {
20056 		      printf ("x86 ISA needed: ");
20057 		      decode_x86_compat_isa (bitmask);
20058 		    }
20059 		  goto next;
20060 
20061 		case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20062 		  if (datasz != 4)
20063 		    printf (_("x86 ISA used: <corrupt length: %#x> "),
20064 			    datasz);
20065 		  else
20066 		    {
20067 		      printf ("x86 ISA used: ");
20068 		      decode_x86_compat_2_isa (bitmask);
20069 		    }
20070 		  goto next;
20071 
20072 		case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20073 		  if (datasz != 4)
20074 		    printf (_("x86 ISA needed: <corrupt length: %#x> "),
20075 			    datasz);
20076 		  else
20077 		    {
20078 		      printf ("x86 ISA needed: ");
20079 		      decode_x86_compat_2_isa (bitmask);
20080 		    }
20081 		  goto next;
20082 
20083 		default:
20084 		  break;
20085 		}
20086 	    }
20087 	  else if (filedata->file_header.e_machine == EM_AARCH64)
20088 	    {
20089 	      if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20090 		{
20091 		  printf ("AArch64 feature: ");
20092 		  if (datasz != 4)
20093 		    printf (_("<corrupt length: %#x> "), datasz);
20094 		  else
20095 		    decode_aarch64_feature_1_and (byte_get (ptr, 4));
20096 		  goto next;
20097 		}
20098 	    }
20099 	}
20100       else
20101 	{
20102 	  switch (type)
20103 	    {
20104 	    case GNU_PROPERTY_STACK_SIZE:
20105 	      printf (_("stack size: "));
20106 	      if (datasz != size)
20107 		printf (_("<corrupt length: %#x> "), datasz);
20108 	      else
20109 		printf ("%#lx", (unsigned long) byte_get (ptr, size));
20110 	      goto next;
20111 
20112 	    case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20113 	      printf ("no copy on protected ");
20114 	      if (datasz)
20115 		printf (_("<corrupt length: %#x> "), datasz);
20116 	      goto next;
20117 
20118 	    default:
20119 	      if ((type >= GNU_PROPERTY_UINT32_AND_LO
20120 		   && type <= GNU_PROPERTY_UINT32_AND_HI)
20121 		  || (type >= GNU_PROPERTY_UINT32_OR_LO
20122 		      && type <= GNU_PROPERTY_UINT32_OR_HI))
20123 		{
20124 		  switch (type)
20125 		    {
20126 		    case GNU_PROPERTY_1_NEEDED:
20127 		      if (datasz != 4)
20128 			printf (_("1_needed: <corrupt length: %#x> "),
20129 				datasz);
20130 		      else
20131 			{
20132 			  unsigned int bitmask = byte_get (ptr, 4);
20133 			  printf ("1_needed: ");
20134 			  decode_1_needed (bitmask);
20135 			}
20136 		      goto next;
20137 
20138 		    default:
20139 		      break;
20140 		    }
20141 		  if (type <= GNU_PROPERTY_UINT32_AND_HI)
20142 		    printf (_("UINT32_AND (%#x): "), type);
20143 		  else
20144 		    printf (_("UINT32_OR (%#x): "), type);
20145 		  if (datasz != 4)
20146 		    printf (_("<corrupt length: %#x> "), datasz);
20147 		  else
20148 		    printf ("%#x", (unsigned int) byte_get (ptr, 4));
20149 		  goto next;
20150 		}
20151 	      break;
20152 	    }
20153 	}
20154 
20155       if (type < GNU_PROPERTY_LOPROC)
20156 	printf (_("<unknown type %#x data: "), type);
20157       else if (type < GNU_PROPERTY_LOUSER)
20158 	printf (_("<processor-specific type %#x data: "), type);
20159       else
20160 	printf (_("<application-specific type %#x data: "), type);
20161       for (j = 0; j < datasz; ++j)
20162 	printf ("%02x ", ptr[j] & 0xff);
20163       printf (">");
20164 
20165     next:
20166       ptr += ((datasz + (size - 1)) & ~ (size - 1));
20167       if (ptr == ptr_end)
20168 	break;
20169 
20170       if (do_wide)
20171 	printf (", ");
20172       else
20173 	printf ("\n\t");
20174     }
20175 
20176   printf ("\n");
20177 }
20178 
20179 static bool
20180 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
20181 {
20182   /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type ().  */
20183   switch (pnote->type)
20184     {
20185     case NT_GNU_BUILD_ID:
20186       {
20187 	unsigned long i;
20188 
20189 	printf (_("    Build ID: "));
20190 	for (i = 0; i < pnote->descsz; ++i)
20191 	  printf ("%02x", pnote->descdata[i] & 0xff);
20192 	printf ("\n");
20193       }
20194       break;
20195 
20196     case NT_GNU_ABI_TAG:
20197       {
20198 	unsigned long os, major, minor, subminor;
20199 	const char *osname;
20200 
20201 	/* PR 17531: file: 030-599401-0.004.  */
20202 	if (pnote->descsz < 16)
20203 	  {
20204 	    printf (_("    <corrupt GNU_ABI_TAG>\n"));
20205 	    break;
20206 	  }
20207 
20208 	os = byte_get ((unsigned char *) pnote->descdata, 4);
20209 	major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20210 	minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20211 	subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20212 
20213 	switch (os)
20214 	  {
20215 	  case GNU_ABI_TAG_LINUX:
20216 	    osname = "Linux";
20217 	    break;
20218 	  case GNU_ABI_TAG_HURD:
20219 	    osname = "Hurd";
20220 	    break;
20221 	  case GNU_ABI_TAG_SOLARIS:
20222 	    osname = "Solaris";
20223 	    break;
20224 	  case GNU_ABI_TAG_FREEBSD:
20225 	    osname = "FreeBSD";
20226 	    break;
20227 	  case GNU_ABI_TAG_NETBSD:
20228 	    osname = "NetBSD";
20229 	    break;
20230 	  case GNU_ABI_TAG_SYLLABLE:
20231 	    osname = "Syllable";
20232 	    break;
20233 	  case GNU_ABI_TAG_NACL:
20234 	    osname = "NaCl";
20235 	    break;
20236 	  default:
20237 	    osname = "Unknown";
20238 	    break;
20239 	  }
20240 
20241 	printf (_("    OS: %s, ABI: %ld.%ld.%ld\n"), osname,
20242 		major, minor, subminor);
20243       }
20244       break;
20245 
20246     case NT_GNU_GOLD_VERSION:
20247       {
20248 	unsigned long i;
20249 
20250 	printf (_("    Version: "));
20251 	for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
20252 	  printf ("%c", pnote->descdata[i]);
20253 	printf ("\n");
20254       }
20255       break;
20256 
20257     case NT_GNU_HWCAP:
20258       {
20259 	unsigned long num_entries, mask;
20260 
20261 	/* Hardware capabilities information.  Word 0 is the number of entries.
20262 	   Word 1 is a bitmask of enabled entries.  The rest of the descriptor
20263 	   is a series of entries, where each entry is a single byte followed
20264 	   by a nul terminated string.  The byte gives the bit number to test
20265 	   if enabled in the bitmask.  */
20266 	printf (_("      Hardware Capabilities: "));
20267 	if (pnote->descsz < 8)
20268 	  {
20269 	    error (_("<corrupt GNU_HWCAP>\n"));
20270 	    return false;
20271 	  }
20272 	num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
20273 	mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20274 	printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
20275 	/* FIXME: Add code to display the entries... */
20276       }
20277       break;
20278 
20279     case NT_GNU_PROPERTY_TYPE_0:
20280       print_gnu_property_note (filedata, pnote);
20281       break;
20282 
20283     default:
20284       /* Handle unrecognised types.  An error message should have already been
20285 	 created by get_gnu_elf_note_type(), so all that we need to do is to
20286 	 display the data.  */
20287       {
20288 	unsigned long i;
20289 
20290 	printf (_("    Description data: "));
20291 	for (i = 0; i < pnote->descsz; ++i)
20292 	  printf ("%02x ", pnote->descdata[i] & 0xff);
20293 	printf ("\n");
20294       }
20295       break;
20296     }
20297 
20298   return true;
20299 }
20300 
20301 static const char *
20302 get_v850_elf_note_type (enum v850_notes n_type)
20303 {
20304   static char buff[64];
20305 
20306   switch (n_type)
20307     {
20308     case V850_NOTE_ALIGNMENT:  return _("Alignment of 8-byte objects");
20309     case V850_NOTE_DATA_SIZE:  return _("Sizeof double and long double");
20310     case V850_NOTE_FPU_INFO:   return _("Type of FPU support needed");
20311     case V850_NOTE_SIMD_INFO:  return _("Use of SIMD instructions");
20312     case V850_NOTE_CACHE_INFO: return _("Use of cache");
20313     case V850_NOTE_MMU_INFO:   return _("Use of MMU");
20314     default:
20315       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
20316       return buff;
20317     }
20318 }
20319 
20320 static bool
20321 print_v850_note (Elf_Internal_Note * pnote)
20322 {
20323   unsigned int val;
20324 
20325   if (pnote->descsz != 4)
20326     return false;
20327 
20328   val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
20329 
20330   if (val == 0)
20331     {
20332       printf (_("not set\n"));
20333       return true;
20334     }
20335 
20336   switch (pnote->type)
20337     {
20338     case V850_NOTE_ALIGNMENT:
20339       switch (val)
20340 	{
20341 	case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
20342 	case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
20343 	}
20344       break;
20345 
20346     case V850_NOTE_DATA_SIZE:
20347       switch (val)
20348 	{
20349 	case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
20350 	case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
20351 	}
20352       break;
20353 
20354     case V850_NOTE_FPU_INFO:
20355       switch (val)
20356 	{
20357 	case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
20358 	case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
20359 	}
20360       break;
20361 
20362     case V850_NOTE_MMU_INFO:
20363     case V850_NOTE_CACHE_INFO:
20364     case V850_NOTE_SIMD_INFO:
20365       if (val == EF_RH850_SIMD)
20366 	{
20367 	  printf (_("yes\n"));
20368 	  return true;
20369 	}
20370       break;
20371 
20372     default:
20373       /* An 'unknown note type' message will already have been displayed.  */
20374       break;
20375     }
20376 
20377   printf (_("unknown value: %x\n"), val);
20378   return false;
20379 }
20380 
20381 static bool
20382 process_netbsd_elf_note (Elf_Internal_Note * pnote)
20383 {
20384   unsigned int version;
20385 
20386   switch (pnote->type)
20387     {
20388     case NT_NETBSD_IDENT:
20389       if (pnote->descsz < 1)
20390 	break;
20391       version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20392       if ((version / 10000) % 100)
20393 	printf ("  NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
20394 		version, version / 100000000, (version / 1000000) % 100,
20395 		(version / 10000) % 100 > 26 ? "Z" : "",
20396 		'A' + (version / 10000) % 26);
20397       else
20398 	printf ("  NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
20399 		version, version / 100000000, (version / 1000000) % 100,
20400 		(version / 100) % 100);
20401       return true;
20402 
20403     case NT_NETBSD_MARCH:
20404       printf ("  NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
20405 	      pnote->descdata);
20406       return true;
20407 
20408     case NT_NETBSD_PAX:
20409       if (pnote->descsz < 1)
20410 	break;
20411       version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20412       printf ("  NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
20413 	      ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
20414 	      ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
20415 	      ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
20416 	      ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
20417 	      ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
20418 	      ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
20419       return true;
20420     }
20421 
20422   printf ("  NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
20423 	  pnote->descsz, pnote->type);
20424   return false;
20425 }
20426 
20427 static const char *
20428 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20429 {
20430   switch (e_type)
20431     {
20432     case NT_FREEBSD_THRMISC:
20433       return _("NT_THRMISC (thrmisc structure)");
20434     case NT_FREEBSD_PROCSTAT_PROC:
20435       return _("NT_PROCSTAT_PROC (proc data)");
20436     case NT_FREEBSD_PROCSTAT_FILES:
20437       return _("NT_PROCSTAT_FILES (files data)");
20438     case NT_FREEBSD_PROCSTAT_VMMAP:
20439       return _("NT_PROCSTAT_VMMAP (vmmap data)");
20440     case NT_FREEBSD_PROCSTAT_GROUPS:
20441       return _("NT_PROCSTAT_GROUPS (groups data)");
20442     case NT_FREEBSD_PROCSTAT_UMASK:
20443       return _("NT_PROCSTAT_UMASK (umask data)");
20444     case NT_FREEBSD_PROCSTAT_RLIMIT:
20445       return _("NT_PROCSTAT_RLIMIT (rlimit data)");
20446     case NT_FREEBSD_PROCSTAT_OSREL:
20447       return _("NT_PROCSTAT_OSREL (osreldate data)");
20448     case NT_FREEBSD_PROCSTAT_PSSTRINGS:
20449       return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
20450     case NT_FREEBSD_PROCSTAT_AUXV:
20451       return _("NT_PROCSTAT_AUXV (auxv data)");
20452     case NT_FREEBSD_PTLWPINFO:
20453       return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
20454     case NT_FREEBSD_X86_SEGBASES:
20455       return _("NT_X86_SEGBASES (x86 segment base registers)");
20456     }
20457   return get_note_type (filedata, e_type);
20458 }
20459 
20460 static const char *
20461 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20462 {
20463   static char buff[64];
20464 
20465   switch (e_type)
20466     {
20467     case NT_NETBSDCORE_PROCINFO:
20468       /* NetBSD core "procinfo" structure.  */
20469       return _("NetBSD procinfo structure");
20470 
20471     case NT_NETBSDCORE_AUXV:
20472       return _("NetBSD ELF auxiliary vector data");
20473 
20474     case NT_NETBSDCORE_LWPSTATUS:
20475       return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
20476 
20477     default:
20478       /* As of Jan 2020 there are no other machine-independent notes
20479 	 defined for NetBSD core files.  If the note type is less
20480 	 than the start of the machine-dependent note types, we don't
20481 	 understand it.  */
20482 
20483       if (e_type < NT_NETBSDCORE_FIRSTMACH)
20484 	{
20485 	  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20486 	  return buff;
20487 	}
20488       break;
20489     }
20490 
20491   switch (filedata->file_header.e_machine)
20492     {
20493     /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
20494        and PT_GETFPREGS == mach+2.  */
20495 
20496     case EM_OLD_ALPHA:
20497     case EM_ALPHA:
20498     case EM_SPARC:
20499     case EM_SPARC32PLUS:
20500     case EM_SPARCV9:
20501       switch (e_type)
20502 	{
20503 	case NT_NETBSDCORE_FIRSTMACH + 0:
20504 	  return _("PT_GETREGS (reg structure)");
20505 	case NT_NETBSDCORE_FIRSTMACH + 2:
20506 	  return _("PT_GETFPREGS (fpreg structure)");
20507 	default:
20508 	  break;
20509 	}
20510       break;
20511 
20512     /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
20513        There's also old PT___GETREGS40 == mach + 1 for old reg
20514        structure which lacks GBR.  */
20515     case EM_SH:
20516       switch (e_type)
20517 	{
20518 	case NT_NETBSDCORE_FIRSTMACH + 1:
20519 	  return _("PT___GETREGS40 (old reg structure)");
20520 	case NT_NETBSDCORE_FIRSTMACH + 3:
20521 	  return _("PT_GETREGS (reg structure)");
20522 	case NT_NETBSDCORE_FIRSTMACH + 5:
20523 	  return _("PT_GETFPREGS (fpreg structure)");
20524 	default:
20525 	  break;
20526 	}
20527       break;
20528 
20529     /* On all other arch's, PT_GETREGS == mach+1 and
20530        PT_GETFPREGS == mach+3.  */
20531     default:
20532       switch (e_type)
20533 	{
20534 	case NT_NETBSDCORE_FIRSTMACH + 1:
20535 	  return _("PT_GETREGS (reg structure)");
20536 	case NT_NETBSDCORE_FIRSTMACH + 3:
20537 	  return _("PT_GETFPREGS (fpreg structure)");
20538 	default:
20539 	  break;
20540 	}
20541     }
20542 
20543   snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20544 	    e_type - NT_NETBSDCORE_FIRSTMACH);
20545   return buff;
20546 }
20547 
20548 static const char *
20549 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20550 {
20551   switch (e_type)
20552     {
20553     case NT_OPENBSD_PROCINFO:
20554       return _("OpenBSD procinfo structure");
20555     case NT_OPENBSD_AUXV:
20556       return _("OpenBSD ELF auxiliary vector data");
20557     case NT_OPENBSD_REGS:
20558       return _("OpenBSD regular registers");
20559     case NT_OPENBSD_FPREGS:
20560       return _("OpenBSD floating point registers");
20561     case NT_OPENBSD_WCOOKIE:
20562       return _("OpenBSD window cookie");
20563     }
20564 
20565   return get_note_type (filedata, e_type);
20566 }
20567 
20568 static const char *
20569 get_stapsdt_note_type (unsigned e_type)
20570 {
20571   static char buff[64];
20572 
20573   switch (e_type)
20574     {
20575     case NT_STAPSDT:
20576       return _("NT_STAPSDT (SystemTap probe descriptors)");
20577 
20578     default:
20579       break;
20580     }
20581 
20582   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20583   return buff;
20584 }
20585 
20586 static bool
20587 print_stapsdt_note (Elf_Internal_Note *pnote)
20588 {
20589   size_t len, maxlen;
20590   unsigned long addr_size = is_32bit_elf ? 4 : 8;
20591   char *data = pnote->descdata;
20592   char *data_end = pnote->descdata + pnote->descsz;
20593   bfd_vma pc, base_addr, semaphore;
20594   char *provider, *probe, *arg_fmt;
20595 
20596   if (pnote->descsz < (addr_size * 3))
20597     goto stapdt_note_too_small;
20598 
20599   pc = byte_get ((unsigned char *) data, addr_size);
20600   data += addr_size;
20601 
20602   base_addr = byte_get ((unsigned char *) data, addr_size);
20603   data += addr_size;
20604 
20605   semaphore = byte_get ((unsigned char *) data, addr_size);
20606   data += addr_size;
20607 
20608   if (data >= data_end)
20609     goto stapdt_note_too_small;
20610   maxlen = data_end - data;
20611   len = strnlen (data, maxlen);
20612   if (len < maxlen)
20613     {
20614       provider = data;
20615       data += len + 1;
20616     }
20617   else
20618     goto stapdt_note_too_small;
20619 
20620   if (data >= data_end)
20621     goto stapdt_note_too_small;
20622   maxlen = data_end - data;
20623   len = strnlen (data, maxlen);
20624   if (len < maxlen)
20625     {
20626       probe = data;
20627       data += len + 1;
20628     }
20629   else
20630     goto stapdt_note_too_small;
20631 
20632   if (data >= data_end)
20633     goto stapdt_note_too_small;
20634   maxlen = data_end - data;
20635   len = strnlen (data, maxlen);
20636   if (len < maxlen)
20637     {
20638       arg_fmt = data;
20639       data += len + 1;
20640     }
20641   else
20642     goto stapdt_note_too_small;
20643 
20644   printf (_("    Provider: %s\n"), provider);
20645   printf (_("    Name: %s\n"), probe);
20646   printf (_("    Location: "));
20647   print_vma (pc, FULL_HEX);
20648   printf (_(", Base: "));
20649   print_vma (base_addr, FULL_HEX);
20650   printf (_(", Semaphore: "));
20651   print_vma (semaphore, FULL_HEX);
20652   printf ("\n");
20653   printf (_("    Arguments: %s\n"), arg_fmt);
20654 
20655   return data == data_end;
20656 
20657  stapdt_note_too_small:
20658   printf (_("  <corrupt - note is too small>\n"));
20659   error (_("corrupt stapdt note - the data size is too small\n"));
20660   return false;
20661 }
20662 
20663 static bool
20664 print_fdo_note (Elf_Internal_Note * pnote)
20665 {
20666   if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
20667     {
20668       printf (_("    Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
20669       return true;
20670     }
20671   return false;
20672 }
20673 
20674 static const char *
20675 get_ia64_vms_note_type (unsigned e_type)
20676 {
20677   static char buff[64];
20678 
20679   switch (e_type)
20680     {
20681     case NT_VMS_MHD:
20682       return _("NT_VMS_MHD (module header)");
20683     case NT_VMS_LNM:
20684       return _("NT_VMS_LNM (language name)");
20685     case NT_VMS_SRC:
20686       return _("NT_VMS_SRC (source files)");
20687     case NT_VMS_TITLE:
20688       return "NT_VMS_TITLE";
20689     case NT_VMS_EIDC:
20690       return _("NT_VMS_EIDC (consistency check)");
20691     case NT_VMS_FPMODE:
20692       return _("NT_VMS_FPMODE (FP mode)");
20693     case NT_VMS_LINKTIME:
20694       return "NT_VMS_LINKTIME";
20695     case NT_VMS_IMGNAM:
20696       return _("NT_VMS_IMGNAM (image name)");
20697     case NT_VMS_IMGID:
20698       return _("NT_VMS_IMGID (image id)");
20699     case NT_VMS_LINKID:
20700       return _("NT_VMS_LINKID (link id)");
20701     case NT_VMS_IMGBID:
20702       return _("NT_VMS_IMGBID (build id)");
20703     case NT_VMS_GSTNAM:
20704       return _("NT_VMS_GSTNAM (sym table name)");
20705     case NT_VMS_ORIG_DYN:
20706       return "NT_VMS_ORIG_DYN";
20707     case NT_VMS_PATCHTIME:
20708       return "NT_VMS_PATCHTIME";
20709     default:
20710       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20711       return buff;
20712     }
20713 }
20714 
20715 static bool
20716 print_ia64_vms_note (Elf_Internal_Note * pnote)
20717 {
20718   int maxlen = pnote->descsz;
20719 
20720   if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20721     goto desc_size_fail;
20722 
20723   switch (pnote->type)
20724     {
20725     case NT_VMS_MHD:
20726       if (maxlen <= 36)
20727 	goto desc_size_fail;
20728 
20729       int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20730 
20731       printf (_("    Creation date  : %.17s\n"), pnote->descdata);
20732       printf (_("    Last patch date: %.17s\n"), pnote->descdata + 17);
20733       if (l + 34 < maxlen)
20734 	{
20735 	  printf (_("    Module name    : %s\n"), pnote->descdata + 34);
20736 	  if (l + 35 < maxlen)
20737 	    printf (_("    Module version : %s\n"), pnote->descdata + 34 + l + 1);
20738 	  else
20739 	    printf (_("    Module version : <missing>\n"));
20740 	}
20741       else
20742 	{
20743 	  printf (_("    Module name    : <missing>\n"));
20744 	  printf (_("    Module version : <missing>\n"));
20745 	}
20746       break;
20747 
20748     case NT_VMS_LNM:
20749       printf (_("   Language: %.*s\n"), maxlen, pnote->descdata);
20750       break;
20751 
20752 #ifdef BFD64
20753     case NT_VMS_FPMODE:
20754       printf (_("   Floating Point mode: "));
20755       if (maxlen < 8)
20756 	goto desc_size_fail;
20757       /* FIXME: Generate an error if descsz > 8 ?  */
20758 
20759       printf ("0x%016" BFD_VMA_FMT "x\n",
20760 	      (bfd_vma) byte_get ((unsigned char *) pnote->descdata, 8));
20761       break;
20762 
20763     case NT_VMS_LINKTIME:
20764       printf (_("   Link time: "));
20765       if (maxlen < 8)
20766 	goto desc_size_fail;
20767       /* FIXME: Generate an error if descsz > 8 ?  */
20768 
20769       print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20770       printf ("\n");
20771       break;
20772 
20773     case NT_VMS_PATCHTIME:
20774       printf (_("   Patch time: "));
20775       if (maxlen < 8)
20776 	goto desc_size_fail;
20777       /* FIXME: Generate an error if descsz > 8 ?  */
20778 
20779       print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20780       printf ("\n");
20781       break;
20782 
20783     case NT_VMS_ORIG_DYN:
20784       if (maxlen < 34)
20785 	goto desc_size_fail;
20786 
20787       printf (_("   Major id: %u,  minor id: %u\n"),
20788 	      (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
20789 	      (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
20790       printf (_("   Last modified  : "));
20791       print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
20792       printf (_("\n   Link flags  : "));
20793       printf ("0x%016" BFD_VMA_FMT "x\n",
20794 	      (bfd_vma) byte_get ((unsigned char *) pnote->descdata + 16, 8));
20795       printf (_("   Header flags: 0x%08x\n"),
20796 	      (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
20797       printf (_("   Image id    : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20798       break;
20799 #endif
20800 
20801     case NT_VMS_IMGNAM:
20802       printf (_("    Image name: %.*s\n"), maxlen, pnote->descdata);
20803       break;
20804 
20805     case NT_VMS_GSTNAM:
20806       printf (_("    Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20807       break;
20808 
20809     case NT_VMS_IMGID:
20810       printf (_("    Image id: %.*s\n"), maxlen, pnote->descdata);
20811       break;
20812 
20813     case NT_VMS_LINKID:
20814       printf (_("    Linker id: %.*s\n"), maxlen, pnote->descdata);
20815       break;
20816 
20817     default:
20818       return false;
20819     }
20820 
20821   return true;
20822 
20823  desc_size_fail:
20824   printf (_("  <corrupt - data size is too small>\n"));
20825   error (_("corrupt IA64 note: data size is too small\n"));
20826   return false;
20827 }
20828 
20829 struct build_attr_cache {
20830   Filedata *filedata;
20831   char *strtab;
20832   unsigned long strtablen;
20833   Elf_Internal_Sym *symtab;
20834   unsigned long nsyms;
20835 } ba_cache;
20836 
20837 /* Find the symbol associated with a build attribute that is attached
20838    to address OFFSET.  If PNAME is non-NULL then store the name of
20839    the symbol (if found) in the provided pointer,  Returns NULL if a
20840    symbol could not be found.  */
20841 
20842 static Elf_Internal_Sym *
20843 get_symbol_for_build_attribute (Filedata *filedata,
20844 				unsigned long offset,
20845 				bool is_open_attr,
20846 				const char **pname)
20847 {
20848   Elf_Internal_Sym *saved_sym = NULL;
20849   Elf_Internal_Sym *sym;
20850 
20851   if (filedata->section_headers != NULL
20852       && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20853     {
20854       Elf_Internal_Shdr * symsec;
20855 
20856       free (ba_cache.strtab);
20857       ba_cache.strtab = NULL;
20858       free (ba_cache.symtab);
20859       ba_cache.symtab = NULL;
20860 
20861       /* Load the symbol and string sections.  */
20862       for (symsec = filedata->section_headers;
20863 	   symsec < filedata->section_headers + filedata->file_header.e_shnum;
20864 	   symsec ++)
20865 	{
20866 	  if (symsec->sh_type == SHT_SYMTAB
20867 	      && get_symtab (filedata, symsec,
20868 			     &ba_cache.symtab, &ba_cache.nsyms,
20869 			     &ba_cache.strtab, &ba_cache.strtablen))
20870 	    break;
20871 	}
20872       ba_cache.filedata = filedata;
20873     }
20874 
20875   if (ba_cache.symtab == NULL)
20876     return NULL;
20877 
20878   /* Find a symbol whose value matches offset.  */
20879   for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20880     if (sym->st_value == offset)
20881       {
20882 	if (sym->st_name >= ba_cache.strtablen)
20883 	  /* Huh ?  This should not happen.  */
20884 	  continue;
20885 
20886 	if (ba_cache.strtab[sym->st_name] == 0)
20887 	  continue;
20888 
20889 	/* The AArch64, ARM and RISC-V architectures define mapping symbols
20890 	   (eg $d, $x, $t) which we want to ignore.  */
20891 	if (ba_cache.strtab[sym->st_name] == '$'
20892 	    && ba_cache.strtab[sym->st_name + 1] != 0
20893 	    && ba_cache.strtab[sym->st_name + 2] == 0)
20894 	  continue;
20895 
20896 	if (is_open_attr)
20897 	  {
20898 	    /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20899 	       and FILE or OBJECT symbols over NOTYPE symbols.  We skip
20900 	       FUNC symbols entirely.  */
20901 	    switch (ELF_ST_TYPE (sym->st_info))
20902 	      {
20903 	      case STT_OBJECT:
20904 	      case STT_FILE:
20905 		saved_sym = sym;
20906 		if (sym->st_size)
20907 		  {
20908 		    /* If the symbol has a size associated
20909 		       with it then we can stop searching.  */
20910 		    sym = ba_cache.symtab + ba_cache.nsyms;
20911 		  }
20912 		continue;
20913 
20914 	      case STT_FUNC:
20915 		/* Ignore function symbols.  */
20916 		continue;
20917 
20918 	      default:
20919 		break;
20920 	      }
20921 
20922 	    switch (ELF_ST_BIND (sym->st_info))
20923 	      {
20924 	      case STB_GLOBAL:
20925 		if (saved_sym == NULL
20926 		    || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20927 		  saved_sym = sym;
20928 		break;
20929 
20930 	      case STB_LOCAL:
20931 		if (saved_sym == NULL)
20932 		  saved_sym = sym;
20933 		break;
20934 
20935 	      default:
20936 		break;
20937 	      }
20938 	  }
20939 	else
20940 	  {
20941 	    if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20942 	      continue;
20943 
20944 	    saved_sym = sym;
20945 	    break;
20946 	  }
20947       }
20948 
20949   if (saved_sym && pname)
20950     * pname = ba_cache.strtab + saved_sym->st_name;
20951 
20952   return saved_sym;
20953 }
20954 
20955 /* Returns true iff addr1 and addr2 are in the same section.  */
20956 
20957 static bool
20958 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20959 {
20960   Elf_Internal_Shdr * a1;
20961   Elf_Internal_Shdr * a2;
20962 
20963   a1 = find_section_by_address (filedata, addr1);
20964   a2 = find_section_by_address (filedata, addr2);
20965 
20966   return a1 == a2 && a1 != NULL;
20967 }
20968 
20969 static bool
20970 print_gnu_build_attribute_description (Elf_Internal_Note *  pnote,
20971 				       Filedata *           filedata)
20972 {
20973   static unsigned long global_offset = 0;
20974   static unsigned long global_end = 0;
20975   static unsigned long func_offset = 0;
20976   static unsigned long func_end = 0;
20977 
20978   Elf_Internal_Sym *sym;
20979   const char *name;
20980   unsigned long start;
20981   unsigned long end;
20982   bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20983 
20984   switch (pnote->descsz)
20985     {
20986     case 0:
20987       /* A zero-length description means that the range of
20988 	 the previous note of the same type should be used.  */
20989       if (is_open_attr)
20990 	{
20991 	  if (global_end > global_offset)
20992 	    printf (_("    Applies to region from %#lx to %#lx\n"),
20993 		    global_offset, global_end);
20994 	  else
20995 	    printf (_("    Applies to region from %#lx\n"), global_offset);
20996 	}
20997       else
20998 	{
20999 	  if (func_end > func_offset)
21000 	    printf (_("    Applies to region from %#lx to %#lx\n"), func_offset, func_end);
21001 	  else
21002 	    printf (_("    Applies to region from %#lx\n"), func_offset);
21003 	}
21004       return true;
21005 
21006     case 4:
21007       start = byte_get ((unsigned char *) pnote->descdata, 4);
21008       end = 0;
21009       break;
21010 
21011     case 8:
21012       start = byte_get ((unsigned char *) pnote->descdata, 4);
21013       end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21014       break;
21015 
21016     case 16:
21017       start = byte_get ((unsigned char *) pnote->descdata, 8);
21018       end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21019       break;
21020 
21021     default:
21022       error (_("    <invalid description size: %lx>\n"), pnote->descsz);
21023       printf (_("    <invalid descsz>"));
21024       return false;
21025     }
21026 
21027   name = NULL;
21028   sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
21029   /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21030      in order to avoid them being confused with the start address of the
21031      first function in the file...  */
21032   if (sym == NULL && is_open_attr)
21033     sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21034 					  & name);
21035 
21036   if (end == 0 && sym != NULL && sym->st_size > 0)
21037     end = start + sym->st_size;
21038 
21039   if (is_open_attr)
21040     {
21041       /* FIXME: Need to properly allow for section alignment.
21042 	 16 is just the alignment used on x86_64.  */
21043       if (global_end > 0
21044 	  && start > BFD_ALIGN (global_end, 16)
21045 	  /* Build notes are not guaranteed to be organised in order of
21046 	     increasing address, but we should find the all of the notes
21047 	     for one section in the same place.  */
21048 	  && same_section (filedata, start, global_end))
21049 	warn (_("Gap in build notes detected from %#lx to %#lx\n"),
21050 	      global_end + 1, start - 1);
21051 
21052       printf (_("    Applies to region from %#lx"), start);
21053       global_offset = start;
21054 
21055       if (end)
21056 	{
21057 	  printf (_(" to %#lx"), end);
21058 	  global_end = end;
21059 	}
21060     }
21061   else
21062     {
21063       printf (_("    Applies to region from %#lx"), start);
21064       func_offset = start;
21065 
21066       if (end)
21067 	{
21068 	  printf (_(" to %#lx"), end);
21069 	  func_end = end;
21070 	}
21071     }
21072 
21073   if (sym && name)
21074     printf (_(" (%s)"), name);
21075 
21076   printf ("\n");
21077   return true;
21078 }
21079 
21080 static bool
21081 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21082 {
21083   static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21084   static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21085   static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
21086   char         name_type;
21087   char         name_attribute;
21088   const char * expected_types;
21089   const char * name = pnote->namedata;
21090   const char * text;
21091   signed int   left;
21092 
21093   if (name == NULL || pnote->namesz < 2)
21094     {
21095       error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21096       print_symbol (-20, _("  <corrupt name>"));
21097       return false;
21098     }
21099 
21100   if (do_wide)
21101     left = 28;
21102   else
21103     left = 20;
21104 
21105   /* Version 2 of the spec adds a "GA" prefix to the name field.  */
21106   if (name[0] == 'G' && name[1] == 'A')
21107     {
21108       if (pnote->namesz < 4)
21109 	{
21110 	  error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21111 	  print_symbol (-20, _("  <corrupt name>"));
21112 	  return false;
21113 	}
21114 
21115       printf ("GA");
21116       name += 2;
21117       left -= 2;
21118     }
21119 
21120   switch ((name_type = * name))
21121     {
21122     case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21123     case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21124     case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21125     case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21126       printf ("%c", * name);
21127       left --;
21128       break;
21129     default:
21130       error (_("unrecognised attribute type in name field: %d\n"), name_type);
21131       print_symbol (-20, _("<unknown name type>"));
21132       return false;
21133     }
21134 
21135   ++ name;
21136   text = NULL;
21137 
21138   switch ((name_attribute = * name))
21139     {
21140     case GNU_BUILD_ATTRIBUTE_VERSION:
21141       text = _("<version>");
21142       expected_types = string_expected;
21143       ++ name;
21144       break;
21145     case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21146       text = _("<stack prot>");
21147       expected_types = "!+*";
21148       ++ name;
21149       break;
21150     case GNU_BUILD_ATTRIBUTE_RELRO:
21151       text = _("<relro>");
21152       expected_types = bool_expected;
21153       ++ name;
21154       break;
21155     case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21156       text = _("<stack size>");
21157       expected_types = number_expected;
21158       ++ name;
21159       break;
21160     case GNU_BUILD_ATTRIBUTE_TOOL:
21161       text = _("<tool>");
21162       expected_types = string_expected;
21163       ++ name;
21164       break;
21165     case GNU_BUILD_ATTRIBUTE_ABI:
21166       text = _("<ABI>");
21167       expected_types = "$*";
21168       ++ name;
21169       break;
21170     case GNU_BUILD_ATTRIBUTE_PIC:
21171       text = _("<PIC>");
21172       expected_types = number_expected;
21173       ++ name;
21174       break;
21175     case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
21176       text = _("<short enum>");
21177       expected_types = bool_expected;
21178       ++ name;
21179       break;
21180     default:
21181       if (ISPRINT (* name))
21182 	{
21183 	  int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
21184 
21185 	  if (len > left && ! do_wide)
21186 	    len = left;
21187 	  printf ("%.*s:", len, name);
21188 	  left -= len;
21189 	  name += len;
21190 	}
21191       else
21192 	{
21193 	  static char tmpbuf [128];
21194 
21195 	  error (_("unrecognised byte in name field: %d\n"), * name);
21196 	  sprintf (tmpbuf, _("<unknown:_%d>"), * name);
21197 	  text = tmpbuf;
21198 	  name ++;
21199 	}
21200       expected_types = "*$!+";
21201       break;
21202     }
21203 
21204   if (text)
21205     left -= printf ("%s", text);
21206 
21207   if (strchr (expected_types, name_type) == NULL)
21208     warn (_("attribute does not have an expected type (%c)\n"), name_type);
21209 
21210   if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
21211     {
21212       error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
21213 	     (unsigned long) pnote->namesz,
21214 	     (long) (name - pnote->namedata));
21215       return false;
21216     }
21217 
21218   if (left < 1 && ! do_wide)
21219     return true;
21220 
21221   switch (name_type)
21222     {
21223     case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21224       {
21225 	unsigned int        bytes;
21226 	unsigned long long  val = 0;
21227 	unsigned int        shift = 0;
21228 	char *              decoded = NULL;
21229 
21230 	bytes = pnote->namesz - (name - pnote->namedata);
21231 	if (bytes > 0)
21232 	  /* The -1 is because the name field is always 0 terminated, and we
21233 	     want to be able to ensure that the shift in the while loop below
21234 	     will not overflow.  */
21235 	  -- bytes;
21236 
21237 	if (bytes > sizeof (val))
21238 	  {
21239 	    error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
21240 		   bytes);
21241 	    bytes = sizeof (val);
21242 	  }
21243 	/* We do not bother to warn if bytes == 0 as this can
21244 	   happen with some early versions of the gcc plugin.  */
21245 
21246 	while (bytes --)
21247 	  {
21248 	    unsigned long long byte = *name++ & 0xff;
21249 
21250 	    val |= byte << shift;
21251 	    shift += 8;
21252 	  }
21253 
21254 	switch (name_attribute)
21255 	  {
21256 	  case GNU_BUILD_ATTRIBUTE_PIC:
21257 	    switch (val)
21258 	      {
21259 	      case 0: decoded = "static"; break;
21260 	      case 1: decoded = "pic"; break;
21261 	      case 2: decoded = "PIC"; break;
21262 	      case 3: decoded = "pie"; break;
21263 	      case 4: decoded = "PIE"; break;
21264 	      default: break;
21265 	      }
21266 	    break;
21267 	  case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21268 	    switch (val)
21269 	      {
21270 		/* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c.  */
21271 	      case 0: decoded = "off"; break;
21272 	      case 1: decoded = "on"; break;
21273 	      case 2: decoded = "all"; break;
21274 	      case 3: decoded = "strong"; break;
21275 	      case 4: decoded = "explicit"; break;
21276 	      default: break;
21277 	      }
21278 	    break;
21279 	  default:
21280 	    break;
21281 	  }
21282 
21283 	if (decoded != NULL)
21284 	  {
21285 	    print_symbol (-left, decoded);
21286 	    left = 0;
21287 	  }
21288 	else if (val == 0)
21289 	  {
21290 	    printf ("0x0");
21291 	    left -= 3;
21292 	  }
21293 	else
21294 	  {
21295 	    if (do_wide)
21296 	      left -= printf ("0x%llx", val);
21297 	    else
21298 	      left -= printf ("0x%-.*llx", left, val);
21299 	  }
21300       }
21301       break;
21302     case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21303       left -= print_symbol (- left, name);
21304       break;
21305     case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21306       left -= print_symbol (- left, "true");
21307       break;
21308     case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21309       left -= print_symbol (- left, "false");
21310       break;
21311     }
21312 
21313   if (do_wide && left > 0)
21314     printf ("%-*s", left, " ");
21315 
21316   return true;
21317 }
21318 
21319 /* Print the contents of PNOTE as hex.  */
21320 
21321 static void
21322 print_note_contents_hex (Elf_Internal_Note *pnote)
21323 {
21324   if (pnote->descsz)
21325     {
21326       unsigned long i;
21327 
21328       printf (_("   description data: "));
21329       for (i = 0; i < pnote->descsz; i++)
21330 	printf ("%02x ", pnote->descdata[i] & 0xff);
21331       if (!do_wide)
21332 	printf ("\n");
21333     }
21334 
21335   if (do_wide)
21336     printf ("\n");
21337 }
21338 
21339 #if defined HAVE_MSGPACK
21340 
21341 static void
21342 print_indents (int n)
21343 {
21344   printf ("    ");
21345 
21346   for (int i = 0; i < n; i++)
21347     printf ("  ");
21348 }
21349 
21350 /* Print OBJ in human-readable form.  */
21351 
21352 static void
21353 dump_msgpack_obj (const msgpack_object *obj, int indent)
21354 {
21355   switch (obj->type)
21356     {
21357     case MSGPACK_OBJECT_NIL:
21358       printf ("(nil)");
21359       break;
21360 
21361     case MSGPACK_OBJECT_BOOLEAN:
21362       printf ("%s", obj->via.boolean ? "true" : "false");
21363       break;
21364 
21365     case MSGPACK_OBJECT_POSITIVE_INTEGER:
21366       printf ("%" PRIu64, obj->via.u64);
21367       break;
21368 
21369     case MSGPACK_OBJECT_NEGATIVE_INTEGER:
21370       printf ("%" PRIi64, obj->via.i64);
21371       break;
21372 
21373     case MSGPACK_OBJECT_FLOAT32:
21374     case MSGPACK_OBJECT_FLOAT64:
21375       printf ("%f", obj->via.f64);
21376       break;
21377 
21378     case MSGPACK_OBJECT_STR:
21379       printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
21380       break;
21381 
21382     case MSGPACK_OBJECT_ARRAY:
21383       {
21384 	const msgpack_object_array *array = &obj->via.array;
21385 
21386 	printf ("[\n");
21387 	++indent;
21388 
21389 	for (uint32_t i = 0; i < array->size; ++i)
21390 	  {
21391 	    const msgpack_object *item = &array->ptr[i];
21392 
21393 	    print_indents (indent);
21394 	    dump_msgpack_obj (item, indent);
21395 	    printf (",\n");
21396 	  }
21397 
21398 	--indent;
21399 	print_indents (indent);
21400 	printf ("]");
21401 	break;
21402       }
21403       break;
21404 
21405     case MSGPACK_OBJECT_MAP:
21406       {
21407 	const msgpack_object_map *map = &obj->via.map;
21408 
21409 	printf ("{\n");
21410 	++indent;
21411 
21412 	for (uint32_t i = 0; i < map->size; ++i)
21413 	  {
21414 	    const msgpack_object_kv *kv = &map->ptr[i];
21415 	    const msgpack_object *key = &kv->key;
21416 	    const msgpack_object *val = &kv->val;
21417 
21418 	    print_indents (indent);
21419 	    dump_msgpack_obj (key, indent);
21420 	    printf (": ");
21421 	    dump_msgpack_obj (val, indent);
21422 
21423 	    printf (",\n");
21424 	  }
21425 
21426 	--indent;
21427 	print_indents (indent);
21428 	printf ("}");
21429 
21430 	break;
21431       }
21432 
21433     case MSGPACK_OBJECT_BIN:
21434       printf ("(bin)");
21435       break;
21436 
21437     case MSGPACK_OBJECT_EXT:
21438       printf ("(ext)");
21439       break;
21440     }
21441 }
21442 
21443 static void
21444 dump_msgpack (const msgpack_unpacked *msg)
21445 {
21446   print_indents (0);
21447   dump_msgpack_obj (&msg->data, 0);
21448   printf ("\n");
21449 }
21450 
21451 #endif /* defined HAVE_MSGPACK */
21452 
21453 static bool
21454 print_amdgpu_note (Elf_Internal_Note *pnote)
21455 {
21456 #if defined HAVE_MSGPACK
21457   /* If msgpack is available, decode and dump the note's content.  */
21458   bool ret;
21459   msgpack_unpacked msg;
21460   msgpack_unpack_return msgpack_ret;
21461 
21462   assert (pnote->type == NT_AMDGPU_METADATA);
21463 
21464   msgpack_unpacked_init (&msg);
21465   msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
21466 				     NULL);
21467 
21468   switch (msgpack_ret)
21469     {
21470     case MSGPACK_UNPACK_SUCCESS:
21471       dump_msgpack (&msg);
21472       ret = true;
21473       break;
21474 
21475     default:
21476       error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
21477       ret = false;
21478       break;
21479     }
21480 
21481   msgpack_unpacked_destroy (&msg);
21482   return ret;
21483 #else
21484   /* msgpack is not available, dump contents as hex.  */
21485   print_note_contents_hex (pnote);
21486   return true;
21487 #endif
21488 }
21489 
21490 /* Note that by the ELF standard, the name field is already null byte
21491    terminated, and namesz includes the terminating null byte.
21492    I.E. the value of namesz for the name "FSF" is 4.
21493 
21494    If the value of namesz is zero, there is no name present.  */
21495 
21496 static bool
21497 process_note (Elf_Internal_Note *  pnote,
21498 	      Filedata *           filedata)
21499 {
21500   const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
21501   const char * nt;
21502 
21503   if (pnote->namesz == 0)
21504     /* If there is no note name, then use the default set of
21505        note type strings.  */
21506     nt = get_note_type (filedata, pnote->type);
21507 
21508   else if (startswith (pnote->namedata, "GNU"))
21509     /* GNU-specific object file notes.  */
21510     nt = get_gnu_elf_note_type (pnote->type);
21511 
21512   else if (startswith (pnote->namedata, "AMDGPU"))
21513     /* AMDGPU-specific object file notes.  */
21514     nt = get_amdgpu_elf_note_type (pnote->type);
21515 
21516   else if (startswith (pnote->namedata, "FreeBSD"))
21517     /* FreeBSD-specific core file notes.  */
21518     nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
21519 
21520   else if (startswith (pnote->namedata, "NetBSD-CORE"))
21521     /* NetBSD-specific core file notes.  */
21522     nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
21523 
21524   else if (startswith (pnote->namedata, "NetBSD"))
21525     /* NetBSD-specific core file notes.  */
21526     return process_netbsd_elf_note (pnote);
21527 
21528   else if (startswith (pnote->namedata, "PaX"))
21529     /* NetBSD-specific core file notes.  */
21530     return process_netbsd_elf_note (pnote);
21531 
21532   else if (startswith (pnote->namedata, "OpenBSD"))
21533     /* OpenBSD-specific core file notes.  */
21534     nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
21535 
21536   else if (startswith (pnote->namedata, "SPU/"))
21537     {
21538       /* SPU-specific core file notes.  */
21539       nt = pnote->namedata + 4;
21540       name = "SPU";
21541     }
21542 
21543   else if (startswith (pnote->namedata, "IPF/VMS"))
21544     /* VMS/ia64-specific file notes.  */
21545     nt = get_ia64_vms_note_type (pnote->type);
21546 
21547   else if (startswith (pnote->namedata, "stapsdt"))
21548     nt = get_stapsdt_note_type (pnote->type);
21549 
21550   else
21551     /* Don't recognize this note name; just use the default set of
21552        note type strings.  */
21553     nt = get_note_type (filedata, pnote->type);
21554 
21555   printf ("  ");
21556 
21557   if (((startswith (pnote->namedata, "GA")
21558 	&& strchr ("*$!+", pnote->namedata[2]) != NULL)
21559        || strchr ("*$!+", pnote->namedata[0]) != NULL)
21560       && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21561 	  || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21562     print_gnu_build_attribute_name (pnote);
21563   else
21564     print_symbol (-20, name);
21565 
21566   if (do_wide)
21567     printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
21568   else
21569     printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
21570 
21571   if (startswith (pnote->namedata, "IPF/VMS"))
21572     return print_ia64_vms_note (pnote);
21573   else if (startswith (pnote->namedata, "GNU"))
21574     return print_gnu_note (filedata, pnote);
21575   else if (startswith (pnote->namedata, "stapsdt"))
21576     return print_stapsdt_note (pnote);
21577   else if (startswith (pnote->namedata, "CORE"))
21578     return print_core_note (pnote);
21579   else if (startswith (pnote->namedata, "FDO"))
21580     return print_fdo_note (pnote);
21581   else if (((startswith (pnote->namedata, "GA")
21582 	     && strchr ("*$!+", pnote->namedata[2]) != NULL)
21583 	    || strchr ("*$!+", pnote->namedata[0]) != NULL)
21584 	   && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21585 	       || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21586     return print_gnu_build_attribute_description (pnote, filedata);
21587   else if (startswith (pnote->namedata, "AMDGPU")
21588 	   && pnote->type == NT_AMDGPU_METADATA)
21589     return print_amdgpu_note (pnote);
21590 
21591   print_note_contents_hex (pnote);
21592   return true;
21593 }
21594 
21595 static bool
21596 process_notes_at (Filedata *           filedata,
21597 		  Elf_Internal_Shdr *  section,
21598 		  bfd_vma              offset,
21599 		  bfd_vma              length,
21600 		  bfd_vma              align)
21601 {
21602   Elf_External_Note *pnotes;
21603   Elf_External_Note *external;
21604   char *end;
21605   bool res = true;
21606 
21607   if (length <= 0)
21608     return false;
21609 
21610   if (section)
21611     {
21612       pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
21613       if (pnotes)
21614 	{
21615 	  if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
21616 	    {
21617 	      free (pnotes);
21618 	      return false;
21619 	    }
21620 	}
21621     }
21622   else
21623     pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21624 					     _("notes"));
21625 
21626   if (pnotes == NULL)
21627     return false;
21628 
21629   external = pnotes;
21630 
21631   if (filedata->is_separate)
21632     printf (_("In linked file '%s': "), filedata->file_name);
21633   else
21634     printf ("\n");
21635   if (section)
21636     printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
21637   else
21638     printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
21639 	    (unsigned long) offset, (unsigned long) length);
21640 
21641   /* NB: Some note sections may have alignment value of 0 or 1.  gABI
21642      specifies that notes should be aligned to 4 bytes in 32-bit
21643      objects and to 8 bytes in 64-bit objects.  As a Linux extension,
21644      we also support 4 byte alignment in 64-bit objects.  If section
21645      alignment is less than 4, we treate alignment as 4 bytes.   */
21646   if (align < 4)
21647     align = 4;
21648   else if (align != 4 && align != 8)
21649     {
21650       warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
21651 	    (long) align);
21652       free (pnotes);
21653       return false;
21654     }
21655 
21656   printf (_("  %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
21657 
21658   end = (char *) pnotes + length;
21659   while ((char *) external < end)
21660     {
21661       Elf_Internal_Note inote;
21662       size_t min_notesz;
21663       char * next;
21664       char * temp = NULL;
21665       size_t data_remaining = end - (char *) external;
21666 
21667       if (!is_ia64_vms (filedata))
21668 	{
21669 	  /* PR binutils/15191
21670 	     Make sure that there is enough data to read.  */
21671 	  min_notesz = offsetof (Elf_External_Note, name);
21672 	  if (data_remaining < min_notesz)
21673 	    {
21674 	      warn (ngettext ("Corrupt note: only %ld byte remains, "
21675 			      "not enough for a full note\n",
21676 			      "Corrupt note: only %ld bytes remain, "
21677 			      "not enough for a full note\n",
21678 			      data_remaining),
21679 		    (long) data_remaining);
21680 	      break;
21681 	    }
21682 	  data_remaining -= min_notesz;
21683 
21684 	  inote.type     = BYTE_GET (external->type);
21685 	  inote.namesz   = BYTE_GET (external->namesz);
21686 	  inote.namedata = external->name;
21687 	  inote.descsz   = BYTE_GET (external->descsz);
21688 	  inote.descdata = ((char *) external
21689 			    + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
21690 	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
21691 	  next = ((char *) external
21692 		  + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
21693 	}
21694       else
21695 	{
21696 	  Elf64_External_VMS_Note *vms_external;
21697 
21698 	  /* PR binutils/15191
21699 	     Make sure that there is enough data to read.  */
21700 	  min_notesz = offsetof (Elf64_External_VMS_Note, name);
21701 	  if (data_remaining < min_notesz)
21702 	    {
21703 	      warn (ngettext ("Corrupt note: only %ld byte remains, "
21704 			      "not enough for a full note\n",
21705 			      "Corrupt note: only %ld bytes remain, "
21706 			      "not enough for a full note\n",
21707 			      data_remaining),
21708 		    (long) data_remaining);
21709 	      break;
21710 	    }
21711 	  data_remaining -= min_notesz;
21712 
21713 	  vms_external = (Elf64_External_VMS_Note *) external;
21714 	  inote.type     = BYTE_GET (vms_external->type);
21715 	  inote.namesz   = BYTE_GET (vms_external->namesz);
21716 	  inote.namedata = vms_external->name;
21717 	  inote.descsz   = BYTE_GET (vms_external->descsz);
21718 	  inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21719 	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
21720 	  next = inote.descdata + align_power (inote.descsz, 3);
21721 	}
21722 
21723       /* PR 17531: file: 3443835e.  */
21724       /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4.  */
21725       if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21726 	  || (size_t) (inote.descdata - inote.namedata) > data_remaining
21727 	  || (size_t) (next - inote.descdata) < inote.descsz
21728 	  || ((size_t) (next - inote.descdata)
21729 	      > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21730 	{
21731 	  warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21732 		(unsigned long) ((char *) external - (char *) pnotes));
21733 	  warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21734 		inote.type, inote.namesz, inote.descsz, (int) align);
21735 	  break;
21736 	}
21737 
21738       external = (Elf_External_Note *) next;
21739 
21740       /* Verify that name is null terminated.  It appears that at least
21741 	 one version of Linux (RedHat 6.0) generates corefiles that don't
21742 	 comply with the ELF spec by failing to include the null byte in
21743 	 namesz.  */
21744       if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21745 	{
21746 	  if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21747 	    {
21748 	      temp = (char *) malloc (inote.namesz + 1);
21749 	      if (temp == NULL)
21750 		{
21751 		  error (_("Out of memory allocating space for inote name\n"));
21752 		  res = false;
21753 		  break;
21754 		}
21755 
21756 	      memcpy (temp, inote.namedata, inote.namesz);
21757 	      inote.namedata = temp;
21758 	    }
21759 	  inote.namedata[inote.namesz] = 0;
21760 	}
21761 
21762       if (! process_note (& inote, filedata))
21763 	res = false;
21764 
21765       free (temp);
21766       temp = NULL;
21767     }
21768 
21769   free (pnotes);
21770 
21771   return res;
21772 }
21773 
21774 static bool
21775 process_corefile_note_segments (Filedata * filedata)
21776 {
21777   Elf_Internal_Phdr *segment;
21778   unsigned int i;
21779   bool res = true;
21780 
21781   if (! get_program_headers (filedata))
21782     return true;
21783 
21784   for (i = 0, segment = filedata->program_headers;
21785        i < filedata->file_header.e_phnum;
21786        i++, segment++)
21787     {
21788       if (segment->p_type == PT_NOTE)
21789 	if (! process_notes_at (filedata, NULL,
21790 				(bfd_vma) segment->p_offset,
21791 				(bfd_vma) segment->p_filesz,
21792 				(bfd_vma) segment->p_align))
21793 	  res = false;
21794     }
21795 
21796   return res;
21797 }
21798 
21799 static bool
21800 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
21801 {
21802   Elf_External_Note * pnotes;
21803   Elf_External_Note * external;
21804   char * end;
21805   bool res = true;
21806 
21807   if (length <= 0)
21808     return false;
21809 
21810   pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21811                                            _("v850 notes"));
21812   if (pnotes == NULL)
21813     return false;
21814 
21815   external = pnotes;
21816   end = (char*) pnotes + length;
21817 
21818   printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21819 	  (unsigned long) offset, (unsigned long) length);
21820 
21821   while ((char *) external + sizeof (Elf_External_Note) < end)
21822     {
21823       Elf_External_Note * next;
21824       Elf_Internal_Note inote;
21825 
21826       inote.type     = BYTE_GET (external->type);
21827       inote.namesz   = BYTE_GET (external->namesz);
21828       inote.namedata = external->name;
21829       inote.descsz   = BYTE_GET (external->descsz);
21830       inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21831       inote.descpos  = offset + (inote.descdata - (char *) pnotes);
21832 
21833       if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21834 	{
21835 	  warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21836 	  inote.descdata = inote.namedata;
21837 	  inote.namesz   = 0;
21838 	}
21839 
21840       next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21841 
21842       if (   ((char *) next > end)
21843 	  || ((char *) next <  (char *) pnotes))
21844 	{
21845 	  warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21846 		(unsigned long) ((char *) external - (char *) pnotes));
21847 	  warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21848 		inote.type, inote.namesz, inote.descsz);
21849 	  break;
21850 	}
21851 
21852       external = next;
21853 
21854       /* Prevent out-of-bounds indexing.  */
21855       if (   inote.namedata + inote.namesz > end
21856 	  || inote.namedata + inote.namesz < inote.namedata)
21857         {
21858           warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21859                 (unsigned long) ((char *) external - (char *) pnotes));
21860           warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21861                 inote.type, inote.namesz, inote.descsz);
21862           break;
21863         }
21864 
21865       printf ("  %s: ", get_v850_elf_note_type (inote.type));
21866 
21867       if (! print_v850_note (& inote))
21868 	{
21869 	  res = false;
21870 	  printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21871 		  inote.namesz, inote.descsz);
21872 	}
21873     }
21874 
21875   free (pnotes);
21876 
21877   return res;
21878 }
21879 
21880 static bool
21881 process_note_sections (Filedata * filedata)
21882 {
21883   Elf_Internal_Shdr *section;
21884   unsigned long i;
21885   unsigned int n = 0;
21886   bool res = true;
21887 
21888   for (i = 0, section = filedata->section_headers;
21889        i < filedata->file_header.e_shnum && section != NULL;
21890        i++, section++)
21891     {
21892       if (section->sh_type == SHT_NOTE)
21893 	{
21894 	  if (! process_notes_at (filedata, section,
21895 				  (bfd_vma) section->sh_offset,
21896 				  (bfd_vma) section->sh_size,
21897 				  (bfd_vma) section->sh_addralign))
21898 	    res = false;
21899 	  n++;
21900 	}
21901 
21902       if ((   filedata->file_header.e_machine == EM_V800
21903 	   || filedata->file_header.e_machine == EM_V850
21904 	   || filedata->file_header.e_machine == EM_CYGNUS_V850)
21905 	  && section->sh_type == SHT_RENESAS_INFO)
21906 	{
21907 	  if (! process_v850_notes (filedata,
21908 				    (bfd_vma) section->sh_offset,
21909 				    (bfd_vma) section->sh_size))
21910 	    res = false;
21911 	  n++;
21912 	}
21913     }
21914 
21915   if (n == 0)
21916     /* Try processing NOTE segments instead.  */
21917     return process_corefile_note_segments (filedata);
21918 
21919   return res;
21920 }
21921 
21922 static bool
21923 process_notes (Filedata * filedata)
21924 {
21925   /* If we have not been asked to display the notes then do nothing.  */
21926   if (! do_notes)
21927     return true;
21928 
21929   if (filedata->file_header.e_type != ET_CORE)
21930     return process_note_sections (filedata);
21931 
21932   /* No program headers means no NOTE segment.  */
21933   if (filedata->file_header.e_phnum > 0)
21934     return process_corefile_note_segments (filedata);
21935 
21936   if (filedata->is_separate)
21937     printf (_("No notes found in linked file '%s'.\n"),
21938 	    filedata->file_name);
21939   else
21940     printf (_("No notes found file.\n"));
21941 
21942   return true;
21943 }
21944 
21945 static unsigned char *
21946 display_public_gnu_attributes (unsigned char * start,
21947 			       const unsigned char * const end)
21948 {
21949   printf (_("  Unknown GNU attribute: %s\n"), start);
21950 
21951   start += strnlen ((char *) start, end - start);
21952   display_raw_attribute (start, end);
21953 
21954   return (unsigned char *) end;
21955 }
21956 
21957 static unsigned char *
21958 display_generic_attribute (unsigned char * start,
21959 			   unsigned int tag,
21960 			   const unsigned char * const end)
21961 {
21962   if (tag == 0)
21963     return (unsigned char *) end;
21964 
21965   return display_tag_value (tag, start, end);
21966 }
21967 
21968 static bool
21969 process_arch_specific (Filedata * filedata)
21970 {
21971   if (! do_arch)
21972     return true;
21973 
21974   switch (filedata->file_header.e_machine)
21975     {
21976     case EM_ARC:
21977     case EM_ARC_COMPACT:
21978     case EM_ARC_COMPACT2:
21979       return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
21980 				 display_arc_attribute,
21981 				 display_generic_attribute);
21982     case EM_ARM:
21983       return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
21984 				 display_arm_attribute,
21985 				 display_generic_attribute);
21986 
21987     case EM_MIPS:
21988     case EM_MIPS_RS3_LE:
21989       return process_mips_specific (filedata);
21990 
21991     case EM_MSP430:
21992      return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
21993 				display_msp430_attribute,
21994 				display_msp430_gnu_attribute);
21995 
21996     case EM_RISCV:
21997      return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
21998 				display_riscv_attribute,
21999 				display_generic_attribute);
22000 
22001     case EM_NDS32:
22002       return process_nds32_specific (filedata);
22003 
22004     case EM_68K:
22005       return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22006 				 display_m68k_gnu_attribute);
22007 
22008     case EM_PPC:
22009     case EM_PPC64:
22010       return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22011 				 display_power_gnu_attribute);
22012 
22013     case EM_S390:
22014     case EM_S390_OLD:
22015       return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22016 				 display_s390_gnu_attribute);
22017 
22018     case EM_SPARC:
22019     case EM_SPARC32PLUS:
22020     case EM_SPARCV9:
22021       return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22022 				 display_sparc_gnu_attribute);
22023 
22024     case EM_TI_C6000:
22025       return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
22026 				 display_tic6x_attribute,
22027 				 display_generic_attribute);
22028 
22029     case EM_CSKY:
22030       return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22031 				 display_csky_attribute, NULL);
22032 
22033     default:
22034       return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
22035 				 display_public_gnu_attributes,
22036 				 display_generic_attribute);
22037     }
22038 }
22039 
22040 static bool
22041 get_file_header (Filedata * filedata)
22042 {
22043   /* Read in the identity array.  */
22044   if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
22045     return false;
22046 
22047   /* Determine how to read the rest of the header.  */
22048   switch (filedata->file_header.e_ident[EI_DATA])
22049     {
22050     default:
22051     case ELFDATANONE:
22052     case ELFDATA2LSB:
22053       byte_get = byte_get_little_endian;
22054       byte_put = byte_put_little_endian;
22055       break;
22056     case ELFDATA2MSB:
22057       byte_get = byte_get_big_endian;
22058       byte_put = byte_put_big_endian;
22059       break;
22060     }
22061 
22062   /* For now we only support 32 bit and 64 bit ELF files.  */
22063   is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
22064 
22065   /* Read in the rest of the header.  */
22066   if (is_32bit_elf)
22067     {
22068       Elf32_External_Ehdr ehdr32;
22069 
22070       if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
22071 	return false;
22072 
22073       filedata->file_header.e_type      = BYTE_GET (ehdr32.e_type);
22074       filedata->file_header.e_machine   = BYTE_GET (ehdr32.e_machine);
22075       filedata->file_header.e_version   = BYTE_GET (ehdr32.e_version);
22076       filedata->file_header.e_entry     = BYTE_GET (ehdr32.e_entry);
22077       filedata->file_header.e_phoff     = BYTE_GET (ehdr32.e_phoff);
22078       filedata->file_header.e_shoff     = BYTE_GET (ehdr32.e_shoff);
22079       filedata->file_header.e_flags     = BYTE_GET (ehdr32.e_flags);
22080       filedata->file_header.e_ehsize    = BYTE_GET (ehdr32.e_ehsize);
22081       filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22082       filedata->file_header.e_phnum     = BYTE_GET (ehdr32.e_phnum);
22083       filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22084       filedata->file_header.e_shnum     = BYTE_GET (ehdr32.e_shnum);
22085       filedata->file_header.e_shstrndx  = BYTE_GET (ehdr32.e_shstrndx);
22086     }
22087   else
22088     {
22089       Elf64_External_Ehdr ehdr64;
22090 
22091       /* If we have been compiled with sizeof (bfd_vma) == 4, then
22092 	 we will not be able to cope with the 64bit data found in
22093 	 64 ELF files.  Detect this now and abort before we start
22094 	 overwriting things.  */
22095       if (sizeof (bfd_vma) < 8)
22096 	{
22097 	  error (_("This instance of readelf has been built without support for a\n\
22098 64 bit data type and so it cannot read 64 bit ELF files.\n"));
22099 	  return false;
22100 	}
22101 
22102       if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
22103 	return false;
22104 
22105       filedata->file_header.e_type      = BYTE_GET (ehdr64.e_type);
22106       filedata->file_header.e_machine   = BYTE_GET (ehdr64.e_machine);
22107       filedata->file_header.e_version   = BYTE_GET (ehdr64.e_version);
22108       filedata->file_header.e_entry     = BYTE_GET (ehdr64.e_entry);
22109       filedata->file_header.e_phoff     = BYTE_GET (ehdr64.e_phoff);
22110       filedata->file_header.e_shoff     = BYTE_GET (ehdr64.e_shoff);
22111       filedata->file_header.e_flags     = BYTE_GET (ehdr64.e_flags);
22112       filedata->file_header.e_ehsize    = BYTE_GET (ehdr64.e_ehsize);
22113       filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22114       filedata->file_header.e_phnum     = BYTE_GET (ehdr64.e_phnum);
22115       filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22116       filedata->file_header.e_shnum     = BYTE_GET (ehdr64.e_shnum);
22117       filedata->file_header.e_shstrndx  = BYTE_GET (ehdr64.e_shstrndx);
22118     }
22119 
22120   return true;
22121 }
22122 
22123 static void
22124 free_filedata (Filedata *filedata)
22125 {
22126   free (filedata->program_interpreter);
22127   free (filedata->program_headers);
22128   free (filedata->section_headers);
22129   free (filedata->string_table);
22130   free (filedata->dump.dump_sects);
22131   free (filedata->dynamic_strings);
22132   free (filedata->dynamic_symbols);
22133   free (filedata->dynamic_syminfo);
22134   free (filedata->dynamic_section);
22135 
22136   while (filedata->symtab_shndx_list != NULL)
22137     {
22138       elf_section_list *next = filedata->symtab_shndx_list->next;
22139       free (filedata->symtab_shndx_list);
22140       filedata->symtab_shndx_list = next;
22141     }
22142 
22143   free (filedata->section_headers_groups);
22144 
22145   if (filedata->section_groups)
22146     {
22147       size_t i;
22148       struct group_list * g;
22149       struct group_list * next;
22150 
22151       for (i = 0; i < filedata->group_count; i++)
22152 	{
22153 	  for (g = filedata->section_groups [i].root; g != NULL; g = next)
22154 	    {
22155 	      next = g->next;
22156 	      free (g);
22157 	    }
22158 	}
22159 
22160       free (filedata->section_groups);
22161     }
22162   memset (&filedata->section_headers, 0,
22163 	  sizeof (Filedata) - offsetof (Filedata, section_headers));
22164 }
22165 
22166 static void
22167 close_file (Filedata * filedata)
22168 {
22169   if (filedata)
22170     {
22171       if (filedata->handle)
22172 	fclose (filedata->handle);
22173       free (filedata);
22174     }
22175 }
22176 
22177 void
22178 close_debug_file (void * data)
22179 {
22180   free_filedata ((Filedata *) data);
22181   close_file ((Filedata *) data);
22182 }
22183 
22184 static Filedata *
22185 open_file (const char * pathname, bool is_separate)
22186 {
22187   struct stat  statbuf;
22188   Filedata *   filedata = NULL;
22189 
22190   if (stat (pathname, & statbuf) < 0
22191       || ! S_ISREG (statbuf.st_mode))
22192     goto fail;
22193 
22194   filedata = calloc (1, sizeof * filedata);
22195   if (filedata == NULL)
22196     goto fail;
22197 
22198   filedata->handle = fopen (pathname, "rb");
22199   if (filedata->handle == NULL)
22200     goto fail;
22201 
22202   filedata->file_size = (bfd_size_type) statbuf.st_size;
22203   filedata->file_name = pathname;
22204   filedata->is_separate = is_separate;
22205 
22206   if (! get_file_header (filedata))
22207     goto fail;
22208 
22209   if (!get_section_headers (filedata, false))
22210     goto fail;
22211 
22212   return filedata;
22213 
22214  fail:
22215   if (filedata)
22216     {
22217       if (filedata->handle)
22218         fclose (filedata->handle);
22219       free (filedata);
22220     }
22221   return NULL;
22222 }
22223 
22224 void *
22225 open_debug_file (const char * pathname)
22226 {
22227   return open_file (pathname, true);
22228 }
22229 
22230 static void
22231 initialise_dump_sects (Filedata * filedata)
22232 {
22233   /* Initialise the dump_sects array from the cmdline_dump_sects array.
22234      Note we do this even if cmdline_dump_sects is empty because we
22235      must make sure that the dump_sets array is zeroed out before each
22236      object file is processed.  */
22237   if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
22238     memset (filedata->dump.dump_sects, 0,
22239 	    filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22240 
22241   if (cmdline.num_dump_sects > 0)
22242     {
22243       if (filedata->dump.num_dump_sects == 0)
22244 	/* A sneaky way of allocating the dump_sects array.  */
22245 	request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
22246 
22247       assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
22248       memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
22249 	      cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22250     }
22251 }
22252 
22253 static bool
22254 might_need_separate_debug_info (Filedata * filedata)
22255 {
22256   /* Debuginfo files do not need further separate file loading.  */
22257   if (filedata->file_header.e_shstrndx == SHN_UNDEF)
22258     return false;
22259 
22260   /* Since do_follow_links might be enabled by default, only treat it as an
22261      indication that separate files should be loaded if setting it was a
22262      deliberate user action.  */
22263   if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
22264     return true;
22265 
22266   if (process_links || do_syms || do_unwind
22267       || dump_any_debugging || do_dump || do_debugging)
22268     return true;
22269 
22270   return false;
22271 }
22272 
22273 /* Process one ELF object file according to the command line options.
22274    This file may actually be stored in an archive.  The file is
22275    positioned at the start of the ELF object.  Returns TRUE if no
22276    problems were encountered, FALSE otherwise.  */
22277 
22278 static bool
22279 process_object (Filedata * filedata)
22280 {
22281   bool have_separate_files;
22282   unsigned int i;
22283   bool res;
22284 
22285   if (! get_file_header (filedata))
22286     {
22287       error (_("%s: Failed to read file header\n"), filedata->file_name);
22288       return false;
22289     }
22290 
22291   /* Initialise per file variables.  */
22292   for (i = ARRAY_SIZE (filedata->version_info); i--;)
22293     filedata->version_info[i] = 0;
22294 
22295   for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
22296     filedata->dynamic_info[i] = 0;
22297   filedata->dynamic_info_DT_GNU_HASH = 0;
22298   filedata->dynamic_info_DT_MIPS_XHASH = 0;
22299 
22300   /* Process the file.  */
22301   if (show_name)
22302     printf (_("\nFile: %s\n"), filedata->file_name);
22303 
22304   initialise_dump_sects (filedata);
22305 
22306   /* There may be some extensions in the first section header.  Don't
22307      bomb if we can't read it.  */
22308   get_section_headers (filedata, true);
22309 
22310   if (! process_file_header (filedata))
22311     {
22312       res = false;
22313       goto out;
22314     }
22315 
22316   /* Throw away the single section header read above, so that we
22317      re-read the entire set.  */
22318   free (filedata->section_headers);
22319   filedata->section_headers = NULL;
22320 
22321   if (! process_section_headers (filedata))
22322     {
22323       /* Without loaded section headers we cannot process lots of things.  */
22324       do_unwind = do_version = do_dump = do_arch = false;
22325 
22326       if (! do_using_dynamic)
22327 	do_syms = do_dyn_syms = do_reloc = false;
22328     }
22329 
22330   if (! process_section_groups (filedata))
22331     /* Without loaded section groups we cannot process unwind.  */
22332     do_unwind = false;
22333 
22334   process_program_headers (filedata);
22335 
22336   res = process_dynamic_section (filedata);
22337 
22338   if (! process_relocs (filedata))
22339     res = false;
22340 
22341   if (! process_unwind (filedata))
22342     res = false;
22343 
22344   if (! process_symbol_table (filedata))
22345     res = false;
22346 
22347   if (! process_lto_symbol_tables (filedata))
22348     res = false;
22349 
22350   if (! process_syminfo (filedata))
22351     res = false;
22352 
22353   if (! process_version_sections (filedata))
22354     res = false;
22355 
22356   if (might_need_separate_debug_info (filedata))
22357     have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
22358   else
22359     have_separate_files = false;
22360 
22361   if (! process_section_contents (filedata))
22362     res = false;
22363 
22364   if (have_separate_files)
22365     {
22366       separate_info * d;
22367 
22368       for (d = first_separate_info; d != NULL; d = d->next)
22369 	{
22370 	  initialise_dump_sects (d->handle);
22371 
22372 	  if (process_links && ! process_file_header (d->handle))
22373 	    res = false;
22374 	  else if (! process_section_headers (d->handle))
22375 	    res = false;
22376 	  else if (! process_section_contents (d->handle))
22377 	    res = false;
22378 	  else if (process_links)
22379 	    {
22380 	      if (! process_section_groups (d->handle))
22381 		res = false;
22382 	      process_program_headers (d->handle);
22383 	      if (! process_dynamic_section (d->handle))
22384 		res = false;
22385 	      if (! process_relocs (d->handle))
22386 		res = false;
22387 	      if (! process_unwind (d->handle))
22388 		res = false;
22389 	      if (! process_symbol_table (d->handle))
22390 		res = false;
22391 	      if (! process_lto_symbol_tables (d->handle))
22392 		res = false;
22393 	      if (! process_syminfo (d->handle))
22394 		res = false;
22395 	      if (! process_version_sections (d->handle))
22396 		res = false;
22397 	      if (! process_notes (d->handle))
22398 		res = false;
22399 	    }
22400 	}
22401 
22402       /* The file handles are closed by the call to free_debug_memory() below.  */
22403     }
22404 
22405   if (! process_notes (filedata))
22406     res = false;
22407 
22408   if (! process_gnu_liblist (filedata))
22409     res = false;
22410 
22411   if (! process_arch_specific (filedata))
22412     res = false;
22413 
22414  out:
22415   free_filedata (filedata);
22416 
22417   free_debug_memory ();
22418 
22419   return res;
22420 }
22421 
22422 /* Process an ELF archive.
22423    On entry the file is positioned just after the ARMAG string.
22424    Returns TRUE upon success, FALSE otherwise.  */
22425 
22426 static bool
22427 process_archive (Filedata * filedata, bool is_thin_archive)
22428 {
22429   struct archive_info arch;
22430   struct archive_info nested_arch;
22431   size_t got;
22432   bool ret = true;
22433 
22434   show_name = true;
22435 
22436   /* The ARCH structure is used to hold information about this archive.  */
22437   arch.file_name = NULL;
22438   arch.file = NULL;
22439   arch.index_array = NULL;
22440   arch.sym_table = NULL;
22441   arch.longnames = NULL;
22442 
22443   /* The NESTED_ARCH structure is used as a single-item cache of information
22444      about a nested archive (when members of a thin archive reside within
22445      another regular archive file).  */
22446   nested_arch.file_name = NULL;
22447   nested_arch.file = NULL;
22448   nested_arch.index_array = NULL;
22449   nested_arch.sym_table = NULL;
22450   nested_arch.longnames = NULL;
22451 
22452   if (setup_archive (&arch, filedata->file_name, filedata->handle,
22453 		     filedata->file_size, is_thin_archive,
22454 		     do_archive_index) != 0)
22455     {
22456       ret = false;
22457       goto out;
22458     }
22459 
22460   if (do_archive_index)
22461     {
22462       if (arch.sym_table == NULL)
22463 	error (_("%s: unable to dump the index as none was found\n"),
22464 	       filedata->file_name);
22465       else
22466 	{
22467 	  unsigned long i, l;
22468 	  unsigned long current_pos;
22469 
22470 	  printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
22471 		    "in the symbol table)\n"),
22472 		  filedata->file_name, (unsigned long) arch.index_num,
22473 		  arch.sym_size);
22474 
22475 	  current_pos = ftell (filedata->handle);
22476 
22477 	  for (i = l = 0; i < arch.index_num; i++)
22478 	    {
22479 	      if (i == 0
22480 		  || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
22481 		{
22482 		  char * member_name
22483 		    = get_archive_member_name_at (&arch, arch.index_array[i],
22484 						  &nested_arch);
22485 
22486 		  if (member_name != NULL)
22487 		    {
22488 		      char * qualified_name
22489 			= make_qualified_name (&arch, &nested_arch,
22490 					       member_name);
22491 
22492 		      if (qualified_name != NULL)
22493 			{
22494 			  printf (_("Contents of binary %s at offset "),
22495 				  qualified_name);
22496 			  (void) print_vma (arch.index_array[i], PREFIX_HEX);
22497 			  putchar ('\n');
22498 			  free (qualified_name);
22499 			}
22500 		      free (member_name);
22501 		    }
22502 		}
22503 
22504 	      if (l >= arch.sym_size)
22505 		{
22506 		  error (_("%s: end of the symbol table reached "
22507 			   "before the end of the index\n"),
22508 			 filedata->file_name);
22509 		  ret = false;
22510 		  break;
22511 		}
22512 	      /* PR 17531: file: 0b6630b2.  */
22513 	      printf ("\t%.*s\n",
22514 		      (int) (arch.sym_size - l), arch.sym_table + l);
22515 	      l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
22516 	    }
22517 
22518 	  if (arch.uses_64bit_indices)
22519 	    l = (l + 7) & ~ 7;
22520 	  else
22521 	    l += l & 1;
22522 
22523 	  if (l < arch.sym_size)
22524 	    {
22525 	      error (ngettext ("%s: %ld byte remains in the symbol table, "
22526 			       "but without corresponding entries in "
22527 			       "the index table\n",
22528 			       "%s: %ld bytes remain in the symbol table, "
22529 			       "but without corresponding entries in "
22530 			       "the index table\n",
22531 			       arch.sym_size - l),
22532 		     filedata->file_name, arch.sym_size - l);
22533 	      ret = false;
22534 	    }
22535 
22536 	  if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
22537 	    {
22538 	      error (_("%s: failed to seek back to start of object files "
22539 		       "in the archive\n"),
22540 		     filedata->file_name);
22541 	      ret = false;
22542 	      goto out;
22543 	    }
22544 	}
22545 
22546       if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
22547 	  && !do_segments && !do_header && !do_dump && !do_version
22548 	  && !do_histogram && !do_debugging && !do_arch && !do_notes
22549 	  && !do_section_groups && !do_dyn_syms)
22550 	{
22551 	  ret = true; /* Archive index only.  */
22552 	  goto out;
22553 	}
22554     }
22555 
22556   while (1)
22557     {
22558       char * name;
22559       size_t namelen;
22560       char * qualified_name;
22561 
22562       /* Read the next archive header.  */
22563       if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
22564 	{
22565 	  error (_("%s: failed to seek to next archive header\n"),
22566 		 arch.file_name);
22567 	  ret = false;
22568 	  break;
22569 	}
22570       got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
22571       if (got != sizeof arch.arhdr)
22572 	{
22573 	  if (got == 0)
22574 	    break;
22575 	  /* PR 24049 - we cannot use filedata->file_name as this will
22576 	     have already been freed.  */
22577 	  error (_("%s: failed to read archive header\n"), arch.file_name);
22578 
22579 	  ret = false;
22580 	  break;
22581 	}
22582       if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
22583 	{
22584 	  error (_("%s: did not find a valid archive header\n"),
22585 		 arch.file_name);
22586 	  ret = false;
22587 	  break;
22588 	}
22589 
22590       arch.next_arhdr_offset += sizeof arch.arhdr;
22591 
22592       filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
22593 
22594       name = get_archive_member_name (&arch, &nested_arch);
22595       if (name == NULL)
22596 	{
22597 	  error (_("%s: bad archive file name\n"), arch.file_name);
22598 	  ret = false;
22599 	  break;
22600 	}
22601       namelen = strlen (name);
22602 
22603       qualified_name = make_qualified_name (&arch, &nested_arch, name);
22604       if (qualified_name == NULL)
22605 	{
22606 	  error (_("%s: bad archive file name\n"), arch.file_name);
22607 	  free (name);
22608 	  ret = false;
22609 	  break;
22610 	}
22611 
22612       if (is_thin_archive && arch.nested_member_origin == 0)
22613 	{
22614 	  /* This is a proxy for an external member of a thin archive.  */
22615 	  Filedata * member_filedata;
22616 	  char * member_file_name = adjust_relative_path
22617 	    (filedata->file_name, name, namelen);
22618 
22619 	  free (name);
22620 	  if (member_file_name == NULL)
22621 	    {
22622 	      free (qualified_name);
22623 	      ret = false;
22624 	      break;
22625 	    }
22626 
22627 	  member_filedata = open_file (member_file_name, false);
22628 	  if (member_filedata == NULL)
22629 	    {
22630 	      error (_("Input file '%s' is not readable.\n"), member_file_name);
22631 	      free (member_file_name);
22632 	      free (qualified_name);
22633 	      ret = false;
22634 	      break;
22635 	    }
22636 
22637 	  filedata->archive_file_offset = arch.nested_member_origin;
22638 	  member_filedata->file_name = qualified_name;
22639 
22640 	  /* The call to process_object() expects the file to be at the beginning.  */
22641 	  rewind (member_filedata->handle);
22642 
22643 	  if (! process_object (member_filedata))
22644 	    ret = false;
22645 
22646 	  close_file (member_filedata);
22647 	  free (member_file_name);
22648 	}
22649       else if (is_thin_archive)
22650 	{
22651 	  Filedata thin_filedata;
22652 
22653 	  memset (&thin_filedata, 0, sizeof (thin_filedata));
22654 
22655 	  /* PR 15140: Allow for corrupt thin archives.  */
22656 	  if (nested_arch.file == NULL)
22657 	    {
22658 	      error (_("%s: contains corrupt thin archive: %s\n"),
22659 		     qualified_name, name);
22660 	      free (qualified_name);
22661 	      free (name);
22662 	      ret = false;
22663 	      break;
22664 	    }
22665 	  free (name);
22666 
22667 	  /* This is a proxy for a member of a nested archive.  */
22668 	  filedata->archive_file_offset
22669 	    = arch.nested_member_origin + sizeof arch.arhdr;
22670 
22671 	  /* The nested archive file will have been opened and setup by
22672 	     get_archive_member_name.  */
22673 	  if (fseek (nested_arch.file, filedata->archive_file_offset,
22674 		     SEEK_SET) != 0)
22675 	    {
22676 	      error (_("%s: failed to seek to archive member.\n"),
22677 		     nested_arch.file_name);
22678 	      free (qualified_name);
22679 	      ret = false;
22680 	      break;
22681 	    }
22682 
22683 	  thin_filedata.handle = nested_arch.file;
22684 	  thin_filedata.file_name = qualified_name;
22685 
22686 	  if (! process_object (& thin_filedata))
22687 	    ret = false;
22688 	}
22689       else
22690 	{
22691 	  free (name);
22692 	  filedata->archive_file_offset = arch.next_arhdr_offset;
22693 	  filedata->file_name = qualified_name;
22694 	  if (! process_object (filedata))
22695 	    ret = false;
22696 	  arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
22697 	  /* Stop looping with "negative" archive_file_size.  */
22698 	  if (arch.next_arhdr_offset < filedata->archive_file_size)
22699 	    arch.next_arhdr_offset = -1ul;
22700 	}
22701 
22702       free (qualified_name);
22703     }
22704 
22705  out:
22706   if (nested_arch.file != NULL)
22707     fclose (nested_arch.file);
22708   release_archive (&nested_arch);
22709   release_archive (&arch);
22710 
22711   return ret;
22712 }
22713 
22714 static bool
22715 process_file (char * file_name)
22716 {
22717   Filedata * filedata = NULL;
22718   struct stat statbuf;
22719   char armag[SARMAG];
22720   bool ret = true;
22721 
22722   if (stat (file_name, &statbuf) < 0)
22723     {
22724       if (errno == ENOENT)
22725 	error (_("'%s': No such file\n"), file_name);
22726       else
22727 	error (_("Could not locate '%s'.  System error message: %s\n"),
22728 	       file_name, strerror (errno));
22729       return false;
22730     }
22731 
22732   if (!do_special_files && ! S_ISREG (statbuf.st_mode))
22733     {
22734       error (_("'%s' is not an ordinary file\n"), file_name);
22735       return false;
22736     }
22737 
22738   filedata = calloc (1, sizeof * filedata);
22739   if (filedata == NULL)
22740     {
22741       error (_("Out of memory allocating file data structure\n"));
22742       return false;
22743     }
22744 
22745   filedata->file_name = file_name;
22746   filedata->handle = fopen (file_name, "rb");
22747   if (filedata->handle == NULL)
22748     {
22749       error (_("Input file '%s' is not readable.\n"), file_name);
22750       free (filedata);
22751       return false;
22752     }
22753 
22754   if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22755     {
22756       error (_("%s: Failed to read file's magic number\n"), file_name);
22757       fclose (filedata->handle);
22758       free (filedata);
22759       return false;
22760     }
22761 
22762   filedata->file_size = (bfd_size_type) statbuf.st_size;
22763   filedata->is_separate = false;
22764 
22765   if (memcmp (armag, ARMAG, SARMAG) == 0)
22766     {
22767       if (! process_archive (filedata, false))
22768 	ret = false;
22769     }
22770   else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22771     {
22772       if ( ! process_archive (filedata, true))
22773 	ret = false;
22774     }
22775   else
22776     {
22777       if (do_archive_index && !check_all)
22778 	error (_("File %s is not an archive so its index cannot be displayed.\n"),
22779 	       file_name);
22780 
22781       rewind (filedata->handle);
22782       filedata->archive_file_size = filedata->archive_file_offset = 0;
22783 
22784       if (! process_object (filedata))
22785 	ret = false;
22786     }
22787 
22788   fclose (filedata->handle);
22789   free (filedata->section_headers);
22790   free (filedata->program_headers);
22791   free (filedata->string_table);
22792   free (filedata->dump.dump_sects);
22793   free (filedata);
22794 
22795   free (ba_cache.strtab);
22796   ba_cache.strtab = NULL;
22797   free (ba_cache.symtab);
22798   ba_cache.symtab = NULL;
22799   ba_cache.filedata = NULL;
22800 
22801   return ret;
22802 }
22803 
22804 #ifdef SUPPORT_DISASSEMBLY
22805 /* Needed by the i386 disassembler.  For extra credit, someone could
22806    fix this so that we insert symbolic addresses here, esp for GOT/PLT
22807    symbols.  */
22808 
22809 void
22810 print_address (unsigned int addr, FILE * outfile)
22811 {
22812   fprintf (outfile,"0x%8.8x", addr);
22813 }
22814 
22815 /* Needed by the i386 disassembler.  */
22816 
22817 void
22818 db_task_printsym (unsigned int addr)
22819 {
22820   print_address (addr, stderr);
22821 }
22822 #endif
22823 
22824 int
22825 main (int argc, char ** argv)
22826 {
22827   int err;
22828 
22829 #ifdef HAVE_LC_MESSAGES
22830   setlocale (LC_MESSAGES, "");
22831 #endif
22832   setlocale (LC_CTYPE, "");
22833   bindtextdomain (PACKAGE, LOCALEDIR);
22834   textdomain (PACKAGE);
22835 
22836   expandargv (&argc, &argv);
22837 
22838   parse_args (& cmdline, argc, argv);
22839 
22840   if (optind < (argc - 1))
22841     /* When displaying information for more than one file,
22842        prefix the information with the file name.  */
22843     show_name = true;
22844   else if (optind >= argc)
22845     {
22846       /* Ensure that the warning is always displayed.  */
22847       do_checks = true;
22848 
22849       warn (_("Nothing to do.\n"));
22850       usage (stderr);
22851     }
22852 
22853   err = false;
22854   while (optind < argc)
22855     if (! process_file (argv[optind++]))
22856       err = true;
22857 
22858   free (cmdline.dump_sects);
22859 
22860   free (dump_ctf_symtab_name);
22861   free (dump_ctf_strtab_name);
22862   free (dump_ctf_parent_name);
22863 
22864   return err ? EXIT_FAILURE : EXIT_SUCCESS;
22865 }
22866