1 /* readelf.c -- display contents of an ELF format file
2    Copyright (C) 1998-2020 Free Software Foundation, Inc.
3 
4    Originally developed by Eric Youngdale <eric@andante.jic.com>
5    Modifications by Nick Clifton <nickc@redhat.com>
6 
7    This file is part of GNU Binutils.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22    02110-1301, USA.  */
23 
24 /* The difference between readelf and objdump:
25 
26   Both programs are capable of displaying the contents of ELF format files,
27   so why does the binutils project have two file dumpers ?
28 
29   The reason is that objdump sees an ELF file through a BFD filter of the
30   world; if BFD has a bug where, say, it disagrees about a machine constant
31   in e_flags, then the odds are good that it will remain internally
32   consistent.  The linker sees it the BFD way, objdump sees it the BFD way,
33   GAS sees it the BFD way.  There was need for a tool to go find out what
34   the file actually says.
35 
36   This is why the readelf program does not link against the BFD library - it
37   exists as an independent program to help verify the correct working of BFD.
38 
39   There is also the case that readelf can provide more information about an
40   ELF file than is provided by objdump.  In particular it can display DWARF
41   debugging information which (at the moment) objdump cannot.  */
42 
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #ifdef HAVE_WCHAR_H
48 #include <wchar.h>
49 #endif
50 
51 #if __GNUC__ >= 2
52 /* Define BFD64 here, even if our default architecture is 32 bit ELF
53    as this will allow us to read in and parse 64bit and 32bit ELF files.
54    Only do this if we believe that the compiler can support a 64 bit
55    data type.  For now we only rely on GCC being able to do this.  */
56 #define BFD64
57 #endif
58 
59 #include "bfd.h"
60 #include "bucomm.h"
61 #include "elfcomm.h"
62 #include "dwarf.h"
63 #include "ctf-api.h"
64 #include "demangle.h"
65 
66 #include "elf/common.h"
67 #include "elf/external.h"
68 #include "elf/internal.h"
69 
70 
71 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
72    we can obtain the H8 reloc numbers.  We need these for the
73    get_reloc_size() function.  We include h8.h again after defining
74    RELOC_MACROS_GEN_FUNC so that we get the naming function as well.  */
75 
76 #include "elf/h8.h"
77 #undef _ELF_H8_H
78 
79 /* Undo the effects of #including reloc-macros.h.  */
80 
81 #undef START_RELOC_NUMBERS
82 #undef RELOC_NUMBER
83 #undef FAKE_RELOC
84 #undef EMPTY_RELOC
85 #undef END_RELOC_NUMBERS
86 #undef _RELOC_MACROS_H
87 
88 /* The following headers use the elf/reloc-macros.h file to
89    automatically generate relocation recognition functions
90    such as elf_mips_reloc_type()  */
91 
92 #define RELOC_MACROS_GEN_FUNC
93 
94 #include "elf/aarch64.h"
95 #include "elf/alpha.h"
96 #include "elf/arc.h"
97 #include "elf/arm.h"
98 #include "elf/avr.h"
99 #include "elf/bfin.h"
100 #include "elf/cr16.h"
101 #include "elf/cris.h"
102 #include "elf/crx.h"
103 #include "elf/csky.h"
104 #include "elf/d10v.h"
105 #include "elf/d30v.h"
106 #include "elf/dlx.h"
107 #include "elf/bpf.h"
108 #include "elf/epiphany.h"
109 #include "elf/fr30.h"
110 #include "elf/frv.h"
111 #include "elf/ft32.h"
112 #include "elf/h8.h"
113 #include "elf/hppa.h"
114 #include "elf/i386.h"
115 #include "elf/i370.h"
116 #include "elf/i860.h"
117 #include "elf/i960.h"
118 #include "elf/ia64.h"
119 #include "elf/ip2k.h"
120 #include "elf/lm32.h"
121 #include "elf/iq2000.h"
122 #include "elf/m32c.h"
123 #include "elf/m32r.h"
124 #include "elf/m68k.h"
125 #include "elf/m68hc11.h"
126 #include "elf/s12z.h"
127 #include "elf/mcore.h"
128 #include "elf/mep.h"
129 #include "elf/metag.h"
130 #include "elf/microblaze.h"
131 #include "elf/mips.h"
132 #include "elf/mmix.h"
133 #include "elf/mn10200.h"
134 #include "elf/mn10300.h"
135 #include "elf/moxie.h"
136 #include "elf/mt.h"
137 #include "elf/msp430.h"
138 #include "elf/nds32.h"
139 #include "elf/nfp.h"
140 #include "elf/nios2.h"
141 #include "elf/or1k.h"
142 #include "elf/pj.h"
143 #include "elf/ppc.h"
144 #include "elf/ppc64.h"
145 #include "elf/pru.h"
146 #include "elf/riscv.h"
147 #include "elf/rl78.h"
148 #include "elf/rx.h"
149 #include "elf/s390.h"
150 #include "elf/score.h"
151 #include "elf/sh.h"
152 #include "elf/sparc.h"
153 #include "elf/spu.h"
154 #include "elf/tic6x.h"
155 #include "elf/tilegx.h"
156 #include "elf/tilepro.h"
157 #include "elf/v850.h"
158 #include "elf/vax.h"
159 #include "elf/visium.h"
160 #include "elf/wasm32.h"
161 #include "elf/x86-64.h"
162 #include "elf/xc16x.h"
163 #include "elf/xgate.h"
164 #include "elf/xstormy16.h"
165 #include "elf/xtensa.h"
166 #include "elf/z80.h"
167 
168 #include "getopt.h"
169 #include "libiberty.h"
170 #include "safe-ctype.h"
171 #include "filenames.h"
172 
173 #ifndef offsetof
174 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
175 #endif
176 
177 typedef struct elf_section_list
178 {
179   Elf_Internal_Shdr *        hdr;
180   struct elf_section_list *  next;
181 } elf_section_list;
182 
183 /* Flag bits indicating particular types of dump.  */
184 #define HEX_DUMP	(1 << 0)	/* The -x command line switch.  */
185 #define DISASS_DUMP	(1 << 1)	/* The -i command line switch.  */
186 #define DEBUG_DUMP	(1 << 2)	/* The -w command line switch.  */
187 #define STRING_DUMP     (1 << 3)	/* The -p command line switch.  */
188 #define RELOC_DUMP      (1 << 4)	/* The -R command line switch.  */
189 #define CTF_DUMP	(1 << 5)	/* The --ctf command line switch.  */
190 
191 typedef unsigned char dump_type;
192 
193 /* A linked list of the section names for which dumps were requested.  */
194 struct dump_list_entry
195 {
196   char *                    name;
197   dump_type                 type;
198   struct dump_list_entry *  next;
199 };
200 
201 /* A dynamic array of flags indicating for which sections a dump
202    has been requested via command line switches.  */
203 struct dump_data
204 {
205   dump_type *          dump_sects;
206   unsigned int         num_dump_sects;
207 };
208 
209 static struct dump_data cmdline;
210 
211 static struct dump_list_entry * dump_sects_byname;
212 
213 char * program_name = "readelf";
214 
215 static bfd_boolean show_name = FALSE;
216 static bfd_boolean do_dynamic = FALSE;
217 static bfd_boolean do_syms = FALSE;
218 static bfd_boolean do_dyn_syms = FALSE;
219 static bfd_boolean do_reloc = FALSE;
220 static bfd_boolean do_sections = FALSE;
221 static bfd_boolean do_section_groups = FALSE;
222 static bfd_boolean do_section_details = FALSE;
223 static bfd_boolean do_segments = FALSE;
224 static bfd_boolean do_unwind = FALSE;
225 static bfd_boolean do_using_dynamic = FALSE;
226 static bfd_boolean do_header = FALSE;
227 static bfd_boolean do_dump = FALSE;
228 static bfd_boolean do_version = FALSE;
229 static bfd_boolean do_histogram = FALSE;
230 static bfd_boolean do_debugging = FALSE;
231 static bfd_boolean do_ctf = FALSE;
232 static bfd_boolean do_arch = FALSE;
233 static bfd_boolean do_notes = FALSE;
234 static bfd_boolean do_archive_index = FALSE;
235 static bfd_boolean check_all = FALSE;
236 static bfd_boolean is_32bit_elf = FALSE;
237 static bfd_boolean decompress_dumps = FALSE;
238 static bfd_boolean do_not_show_symbol_truncation = FALSE;
239 static bfd_boolean do_demangle = FALSE;	/* Pretty print C++ symbol names.  */
240 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
241 
242 static char *dump_ctf_parent_name;
243 static char *dump_ctf_symtab_name;
244 static char *dump_ctf_strtab_name;
245 
246 struct group_list
247 {
248   struct group_list *  next;
249   unsigned int         section_index;
250 };
251 
252 struct group
253 {
254   struct group_list *  root;
255   unsigned int         group_index;
256 };
257 
258 typedef struct filedata
259 {
260   const char *         file_name;
261   FILE *               handle;
262   bfd_size_type        file_size;
263   Elf_Internal_Ehdr    file_header;
264   Elf_Internal_Shdr *  section_headers;
265   Elf_Internal_Phdr *  program_headers;
266   char *               string_table;
267   unsigned long        string_table_length;
268   unsigned long        archive_file_offset;
269   unsigned long        archive_file_size;
270   unsigned long        dynamic_addr;
271   bfd_size_type        dynamic_size;
272   size_t               dynamic_nent;
273   Elf_Internal_Dyn *   dynamic_section;
274   Elf_Internal_Shdr *  dynamic_strtab_section;
275   char *               dynamic_strings;
276   unsigned long        dynamic_strings_length;
277   Elf_Internal_Shdr *  dynamic_symtab_section;
278   unsigned long        num_dynamic_syms;
279   Elf_Internal_Sym *   dynamic_symbols;
280   bfd_vma              version_info[16];
281   unsigned int         dynamic_syminfo_nent;
282   Elf_Internal_Syminfo * dynamic_syminfo;
283   unsigned long        dynamic_syminfo_offset;
284   bfd_size_type        nbuckets;
285   bfd_size_type        nchains;
286   bfd_vma *            buckets;
287   bfd_vma *            chains;
288   bfd_size_type        ngnubuckets;
289   bfd_size_type        ngnuchains;
290   bfd_vma *            gnubuckets;
291   bfd_vma *            gnuchains;
292   bfd_vma *            mipsxlat;
293   bfd_vma              gnusymidx;
294   char                 program_interpreter[PATH_MAX];
295   bfd_vma              dynamic_info[DT_ENCODING];
296   bfd_vma              dynamic_info_DT_GNU_HASH;
297   bfd_vma              dynamic_info_DT_MIPS_XHASH;
298   elf_section_list *   symtab_shndx_list;
299   size_t               group_count;
300   struct group *       section_groups;
301   struct group **      section_headers_groups;
302   /* A dynamic array of flags indicating for which sections a dump of
303      some kind has been requested.  It is reset on a per-object file
304      basis and then initialised from the cmdline_dump_sects array,
305      the results of interpreting the -w switch, and the
306      dump_sects_byname list.  */
307   struct dump_data     dump;
308 } Filedata;
309 
310 /* How to print a vma value.  */
311 typedef enum print_mode
312 {
313   HEX,
314   DEC,
315   DEC_5,
316   UNSIGNED,
317   PREFIX_HEX,
318   FULL_HEX,
319   LONG_HEX
320 }
321 print_mode;
322 
323 /* Versioned symbol info.  */
324 enum versioned_symbol_info
325 {
326   symbol_undefined,
327   symbol_hidden,
328   symbol_public
329 };
330 
331 static const char * get_symbol_version_string
332   (Filedata *, bfd_boolean, const char *, unsigned long, unsigned,
333    Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
334 
335 #define UNKNOWN -1
336 
337 #define SECTION_NAME(X)						\
338   ((X) == NULL ? _("<none>")					\
339    : filedata->string_table == NULL ? _("<no-strings>")		\
340    : ((X)->sh_name >= filedata->string_table_length ? _("<corrupt>")	\
341   : filedata->string_table + (X)->sh_name))
342 
343 #define DT_VERSIONTAGIDX(tag)	(DT_VERNEEDNUM - (tag))	/* Reverse order!  */
344 
345 #define GET_ELF_SYMBOLS(file, section, sym_count)			\
346   (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count)	\
347    : get_64bit_elf_symbols (file, section, sym_count))
348 
349 #define VALID_SYMBOL_NAME(strtab, strtab_size, offset) \
350    (strtab != NULL && offset < strtab_size)
351 #define VALID_DYNAMIC_NAME(filedata, offset) \
352   VALID_SYMBOL_NAME (filedata->dynamic_strings, \
353 		     filedata->dynamic_strings_length, offset)
354 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
355    already been called and verified that the string exists.  */
356 #define GET_DYNAMIC_NAME(filedata, offset) \
357   (filedata->dynamic_strings + offset)
358 
359 #define REMOVE_ARCH_BITS(ADDR)			\
360   do						\
361     {						\
362       if (filedata->file_header.e_machine == EM_ARM)	\
363 	(ADDR) &= ~1;				\
364     }						\
365   while (0)
366 
367 /* Get the correct GNU hash section name.  */
368 #define GNU_HASH_SECTION_NAME(filedata)		\
369   filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
370 
371 /* Print a BFD_VMA to an internal buffer, for use in error messages.
372    BFD_FMA_FMT can't be used in translated strings.  */
373 
374 static const char *
bfd_vmatoa(char * fmtch,bfd_vma value)375 bfd_vmatoa (char *fmtch, bfd_vma value)
376 {
377   /* bfd_vmatoa is used more then once in a printf call for output.
378      Cycle through an array of buffers.  */
379   static int buf_pos = 0;
380   static struct bfd_vmatoa_buf
381   {
382     char place[64];
383   } buf[4];
384   char *ret;
385   char fmt[32];
386 
387   ret = buf[buf_pos++].place;
388   buf_pos %= ARRAY_SIZE (buf);
389 
390   sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
391   snprintf (ret, sizeof (buf[0].place), fmt, value);
392   return ret;
393 }
394 
395 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
396    OFFSET + the offset of the current archive member, if we are examining an
397    archive.  Put the retrieved data into VAR, if it is not NULL.  Otherwise
398    allocate a buffer using malloc and fill that.  In either case return the
399    pointer to the start of the retrieved data or NULL if something went wrong.
400    If something does go wrong and REASON is not NULL then emit an error
401    message using REASON as part of the context.  */
402 
403 static void *
get_data(void * var,Filedata * filedata,unsigned long offset,bfd_size_type size,bfd_size_type nmemb,const char * reason)404 get_data (void *         var,
405 	  Filedata *     filedata,
406 	  unsigned long  offset,
407 	  bfd_size_type  size,
408 	  bfd_size_type  nmemb,
409 	  const char *   reason)
410 {
411   void * mvar;
412   bfd_size_type amt = size * nmemb;
413 
414   if (size == 0 || nmemb == 0)
415     return NULL;
416 
417   /* If the size_t type is smaller than the bfd_size_type, eg because
418      you are building a 32-bit tool on a 64-bit host, then make sure
419      that when the sizes are cast to (size_t) no information is lost.  */
420   if ((size_t) size != size
421       || (size_t) nmemb != nmemb
422       || (size_t) amt != amt)
423     {
424       if (reason)
425 	error (_("Size truncation prevents reading %s"
426 		 " elements of size %s for %s\n"),
427 	       bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
428       return NULL;
429     }
430 
431   /* Check for size overflow.  */
432   if (amt / size != nmemb || (size_t) amt + 1 == 0)
433     {
434       if (reason)
435 	error (_("Size overflow prevents reading %s"
436 		 " elements of size %s for %s\n"),
437 	       bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
438       return NULL;
439     }
440 
441   /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
442      attempting to allocate memory when the read is bound to fail.  */
443   if (filedata->archive_file_offset > filedata->file_size
444       || offset > filedata->file_size - filedata->archive_file_offset
445       || amt > filedata->file_size - filedata->archive_file_offset - offset)
446     {
447       if (reason)
448 	error (_("Reading %s bytes extends past end of file for %s\n"),
449 	       bfd_vmatoa ("u", amt), reason);
450       return NULL;
451     }
452 
453   if (fseek (filedata->handle, filedata->archive_file_offset + offset,
454 	     SEEK_SET))
455     {
456       if (reason)
457 	error (_("Unable to seek to 0x%lx for %s\n"),
458 	       filedata->archive_file_offset + offset, reason);
459       return NULL;
460     }
461 
462   mvar = var;
463   if (mvar == NULL)
464     {
465       /* + 1 so that we can '\0' terminate invalid string table sections.  */
466       mvar = malloc ((size_t) amt + 1);
467 
468       if (mvar == NULL)
469 	{
470 	  if (reason)
471 	    error (_("Out of memory allocating %s bytes for %s\n"),
472 		   bfd_vmatoa ("u", amt), reason);
473 	  return NULL;
474 	}
475 
476       ((char *) mvar)[amt] = '\0';
477     }
478 
479   if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
480     {
481       if (reason)
482 	error (_("Unable to read in %s bytes of %s\n"),
483 	       bfd_vmatoa ("u", amt), reason);
484       if (mvar != var)
485 	free (mvar);
486       return NULL;
487     }
488 
489   return mvar;
490 }
491 
492 /* Print a VMA value in the MODE specified.
493    Returns the number of characters displayed.  */
494 
495 static unsigned int
print_vma(bfd_vma vma,print_mode mode)496 print_vma (bfd_vma vma, print_mode mode)
497 {
498   unsigned int nc = 0;
499 
500   switch (mode)
501     {
502     case FULL_HEX:
503       nc = printf ("0x");
504       /* Fall through.  */
505     case LONG_HEX:
506 #ifdef BFD64
507       if (is_32bit_elf)
508 	return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
509 #endif
510       printf_vma (vma);
511       return nc + 16;
512 
513     case DEC_5:
514       if (vma <= 99999)
515 	return printf ("%5" BFD_VMA_FMT "d", vma);
516       /* Fall through.  */
517     case PREFIX_HEX:
518       nc = printf ("0x");
519       /* Fall through.  */
520     case HEX:
521       return nc + printf ("%" BFD_VMA_FMT "x", vma);
522 
523     case DEC:
524       return printf ("%" BFD_VMA_FMT "d", vma);
525 
526     case UNSIGNED:
527       return printf ("%" BFD_VMA_FMT "u", vma);
528 
529     default:
530       /* FIXME: Report unrecognised mode ?  */
531       return 0;
532     }
533 }
534 
535 /* Display a symbol on stdout.  Handles the display of control characters and
536    multibye characters (assuming the host environment supports them).
537 
538    Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
539 
540    If truncation will happen and do_not_show_symbol_truncation is FALSE then display
541    abs(WIDTH) - 5 characters followed by "[...]".
542 
543    If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
544    padding as necessary.
545 
546    Returns the number of emitted characters.  */
547 
548 static unsigned int
print_symbol(signed int width,const char * symbol)549 print_symbol (signed int width, const char * symbol)
550 {
551   bfd_boolean extra_padding = FALSE;
552   bfd_boolean do_dots = FALSE;
553   signed int num_printed = 0;
554 #ifdef HAVE_MBSTATE_T
555   mbstate_t state;
556 #endif
557   unsigned int width_remaining;
558   const void * alloced_symbol = NULL;
559 
560   if (width < 0)
561     {
562       /* Keep the width positive.  This helps the code below.  */
563       width = - width;
564       extra_padding = TRUE;
565     }
566   else if (width == 0)
567     return 0;
568 
569   if (do_wide)
570     /* Set the remaining width to a very large value.
571        This simplifies the code below.  */
572     width_remaining = INT_MAX;
573   else
574     {
575       width_remaining = width;
576       if (! do_not_show_symbol_truncation
577 	  && (int) strlen (symbol) > width)
578 	{
579 	  width_remaining -= 5;
580 	  if ((int) width_remaining < 0)
581 	    width_remaining = 0;
582 	  do_dots = TRUE;
583 	}
584     }
585 
586 #ifdef HAVE_MBSTATE_T
587   /* Initialise the multibyte conversion state.  */
588   memset (& state, 0, sizeof (state));
589 #endif
590 
591   if (do_demangle && *symbol)
592     {
593       const char * res = cplus_demangle (symbol, demangle_flags);
594 
595       if (res != NULL)
596 	alloced_symbol = symbol = res;
597     }
598 
599   while (width_remaining)
600     {
601       size_t  n;
602       const char c = *symbol++;
603 
604       if (c == 0)
605 	break;
606 
607       /* Do not print control characters directly as they can affect terminal
608 	 settings.  Such characters usually appear in the names generated
609 	 by the assembler for local labels.  */
610       if (ISCNTRL (c))
611 	{
612 	  if (width_remaining < 2)
613 	    break;
614 
615 	  printf ("^%c", c + 0x40);
616 	  width_remaining -= 2;
617 	  num_printed += 2;
618 	}
619       else if (ISPRINT (c))
620 	{
621 	  putchar (c);
622 	  width_remaining --;
623 	  num_printed ++;
624 	}
625       else
626 	{
627 #ifdef HAVE_MBSTATE_T
628 	  wchar_t w;
629 #endif
630 	  /* Let printf do the hard work of displaying multibyte characters.  */
631 	  printf ("%.1s", symbol - 1);
632 	  width_remaining --;
633 	  num_printed ++;
634 
635 #ifdef HAVE_MBSTATE_T
636 	  /* Try to find out how many bytes made up the character that was
637 	     just printed.  Advance the symbol pointer past the bytes that
638 	     were displayed.  */
639 	  n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
640 #else
641 	  n = 1;
642 #endif
643 	  if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
644 	    symbol += (n - 1);
645 	}
646     }
647 
648   if (do_dots)
649     num_printed += printf ("[...]");
650 
651   if (extra_padding && num_printed < width)
652     {
653       /* Fill in the remaining spaces.  */
654       printf ("%-*s", width - num_printed, " ");
655       num_printed = width;
656     }
657 
658   free ((void *) alloced_symbol);
659   return num_printed;
660 }
661 
662 /* Returns a pointer to a static buffer containing a printable version of
663    the given section's name.  Like print_symbol, except that it does not try
664    to print multibyte characters, it just interprets them as hex values.  */
665 
666 static const char *
printable_section_name(Filedata * filedata,const Elf_Internal_Shdr * sec)667 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
668 {
669 #define MAX_PRINT_SEC_NAME_LEN 128
670   static char  sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
671   const char * name = SECTION_NAME (sec);
672   char *       buf = sec_name_buf;
673   char         c;
674   unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
675 
676   while ((c = * name ++) != 0)
677     {
678       if (ISCNTRL (c))
679 	{
680 	  if (remaining < 2)
681 	    break;
682 
683 	  * buf ++ = '^';
684 	  * buf ++ = c + 0x40;
685 	  remaining -= 2;
686 	}
687       else if (ISPRINT (c))
688 	{
689 	  * buf ++ = c;
690 	  remaining -= 1;
691 	}
692       else
693 	{
694 	  static char hex[17] = "0123456789ABCDEF";
695 
696 	  if (remaining < 4)
697 	    break;
698 	  * buf ++ = '<';
699 	  * buf ++ = hex[(c & 0xf0) >> 4];
700 	  * buf ++ = hex[c & 0x0f];
701 	  * buf ++ = '>';
702 	  remaining -= 4;
703 	}
704 
705       if (remaining == 0)
706 	break;
707     }
708 
709   * buf = 0;
710   return sec_name_buf;
711 }
712 
713 static const char *
printable_section_name_from_index(Filedata * filedata,unsigned long ndx)714 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
715 {
716   if (ndx >= filedata->file_header.e_shnum)
717     return _("<corrupt>");
718 
719   return printable_section_name (filedata, filedata->section_headers + ndx);
720 }
721 
722 /* Return a pointer to section NAME, or NULL if no such section exists.  */
723 
724 static Elf_Internal_Shdr *
find_section(Filedata * filedata,const char * name)725 find_section (Filedata * filedata, const char * name)
726 {
727   unsigned int i;
728 
729   if (filedata->section_headers == NULL)
730     return NULL;
731 
732   for (i = 0; i < filedata->file_header.e_shnum; i++)
733     if (streq (SECTION_NAME (filedata->section_headers + i), name))
734       return filedata->section_headers + i;
735 
736   return NULL;
737 }
738 
739 /* Return a pointer to a section containing ADDR, or NULL if no such
740    section exists.  */
741 
742 static Elf_Internal_Shdr *
find_section_by_address(Filedata * filedata,bfd_vma addr)743 find_section_by_address (Filedata * filedata, bfd_vma addr)
744 {
745   unsigned int i;
746 
747   if (filedata->section_headers == NULL)
748     return NULL;
749 
750   for (i = 0; i < filedata->file_header.e_shnum; i++)
751     {
752       Elf_Internal_Shdr *sec = filedata->section_headers + i;
753 
754       if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
755 	return sec;
756     }
757 
758   return NULL;
759 }
760 
761 static Elf_Internal_Shdr *
find_section_by_type(Filedata * filedata,unsigned int type)762 find_section_by_type (Filedata * filedata, unsigned int type)
763 {
764   unsigned int i;
765 
766   if (filedata->section_headers == NULL)
767     return NULL;
768 
769   for (i = 0; i < filedata->file_header.e_shnum; i++)
770     {
771       Elf_Internal_Shdr *sec = filedata->section_headers + i;
772 
773       if (sec->sh_type == type)
774 	return sec;
775     }
776 
777   return NULL;
778 }
779 
780 /* Return a pointer to section NAME, or NULL if no such section exists,
781    restricted to the list of sections given in SET.  */
782 
783 static Elf_Internal_Shdr *
find_section_in_set(Filedata * filedata,const char * name,unsigned int * set)784 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
785 {
786   unsigned int i;
787 
788   if (filedata->section_headers == NULL)
789     return NULL;
790 
791   if (set != NULL)
792     {
793       while ((i = *set++) > 0)
794 	{
795 	  /* See PR 21156 for a reproducer.  */
796 	  if (i >= filedata->file_header.e_shnum)
797 	    continue; /* FIXME: Should we issue an error message ?  */
798 
799 	  if (streq (SECTION_NAME (filedata->section_headers + i), name))
800 	    return filedata->section_headers + i;
801 	}
802     }
803 
804   return find_section (filedata, name);
805 }
806 
807 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
808    This OS has so many departures from the ELF standard that we test it at
809    many places.  */
810 
811 static inline bfd_boolean
is_ia64_vms(Filedata * filedata)812 is_ia64_vms (Filedata * filedata)
813 {
814   return filedata->file_header.e_machine == EM_IA_64
815     && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
816 }
817 
818 /* Guess the relocation size commonly used by the specific machines.  */
819 
820 static bfd_boolean
guess_is_rela(unsigned int e_machine)821 guess_is_rela (unsigned int e_machine)
822 {
823   switch (e_machine)
824     {
825       /* Targets that use REL relocations.  */
826     case EM_386:
827     case EM_IAMCU:
828     case EM_960:
829     case EM_ARM:
830     case EM_D10V:
831     case EM_CYGNUS_D10V:
832     case EM_DLX:
833     case EM_MIPS:
834     case EM_MIPS_RS3_LE:
835     case EM_CYGNUS_M32R:
836     case EM_SCORE:
837     case EM_XGATE:
838     case EM_NFP:
839     case EM_BPF:
840       return FALSE;
841 
842       /* Targets that use RELA relocations.  */
843     case EM_68K:
844     case EM_860:
845     case EM_AARCH64:
846     case EM_ADAPTEVA_EPIPHANY:
847     case EM_ALPHA:
848     case EM_ALTERA_NIOS2:
849     case EM_ARC:
850     case EM_ARC_COMPACT:
851     case EM_ARC_COMPACT2:
852     case EM_AVR:
853     case EM_AVR_OLD:
854     case EM_BLACKFIN:
855     case EM_CR16:
856     case EM_CRIS:
857     case EM_CRX:
858     case EM_CSKY:
859     case EM_D30V:
860     case EM_CYGNUS_D30V:
861     case EM_FR30:
862     case EM_FT32:
863     case EM_CYGNUS_FR30:
864     case EM_CYGNUS_FRV:
865     case EM_H8S:
866     case EM_H8_300:
867     case EM_H8_300H:
868     case EM_IA_64:
869     case EM_IP2K:
870     case EM_IP2K_OLD:
871     case EM_IQ2000:
872     case EM_LATTICEMICO32:
873     case EM_M32C_OLD:
874     case EM_M32C:
875     case EM_M32R:
876     case EM_MCORE:
877     case EM_CYGNUS_MEP:
878     case EM_METAG:
879     case EM_MMIX:
880     case EM_MN10200:
881     case EM_CYGNUS_MN10200:
882     case EM_MN10300:
883     case EM_CYGNUS_MN10300:
884     case EM_MOXIE:
885     case EM_MSP430:
886     case EM_MSP430_OLD:
887     case EM_MT:
888     case EM_NDS32:
889     case EM_NIOS32:
890     case EM_OR1K:
891     case EM_PPC64:
892     case EM_PPC:
893     case EM_TI_PRU:
894     case EM_RISCV:
895     case EM_RL78:
896     case EM_RX:
897     case EM_S390:
898     case EM_S390_OLD:
899     case EM_SH:
900     case EM_SPARC:
901     case EM_SPARC32PLUS:
902     case EM_SPARCV9:
903     case EM_SPU:
904     case EM_TI_C6000:
905     case EM_TILEGX:
906     case EM_TILEPRO:
907     case EM_V800:
908     case EM_V850:
909     case EM_CYGNUS_V850:
910     case EM_VAX:
911     case EM_VISIUM:
912     case EM_X86_64:
913     case EM_L1OM:
914     case EM_K1OM:
915     case EM_XSTORMY16:
916     case EM_XTENSA:
917     case EM_XTENSA_OLD:
918     case EM_MICROBLAZE:
919     case EM_MICROBLAZE_OLD:
920     case EM_WEBASSEMBLY:
921       return TRUE;
922 
923     case EM_68HC05:
924     case EM_68HC08:
925     case EM_68HC11:
926     case EM_68HC16:
927     case EM_FX66:
928     case EM_ME16:
929     case EM_MMA:
930     case EM_NCPU:
931     case EM_NDR1:
932     case EM_PCP:
933     case EM_ST100:
934     case EM_ST19:
935     case EM_ST7:
936     case EM_ST9PLUS:
937     case EM_STARCORE:
938     case EM_SVX:
939     case EM_TINYJ:
940     default:
941       warn (_("Don't know about relocations on this machine architecture\n"));
942       return FALSE;
943     }
944 }
945 
946 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
947    Returns TRUE upon success, FALSE otherwise.  If successful then a
948    pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
949    and the number of relocs loaded is placed in *NRELASP.  It is the caller's
950    responsibility to free the allocated buffer.  */
951 
952 static bfd_boolean
slurp_rela_relocs(Filedata * filedata,unsigned long rel_offset,unsigned long rel_size,Elf_Internal_Rela ** relasp,unsigned long * nrelasp)953 slurp_rela_relocs (Filedata *            filedata,
954 		   unsigned long         rel_offset,
955 		   unsigned long         rel_size,
956 		   Elf_Internal_Rela **  relasp,
957 		   unsigned long *       nrelasp)
958 {
959   Elf_Internal_Rela * relas;
960   size_t nrelas;
961   unsigned int i;
962 
963   if (is_32bit_elf)
964     {
965       Elf32_External_Rela * erelas;
966 
967       erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
968                                                  rel_size, _("32-bit relocation data"));
969       if (!erelas)
970 	return FALSE;
971 
972       nrelas = rel_size / sizeof (Elf32_External_Rela);
973 
974       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
975                                              sizeof (Elf_Internal_Rela));
976 
977       if (relas == NULL)
978 	{
979 	  free (erelas);
980 	  error (_("out of memory parsing relocs\n"));
981 	  return FALSE;
982 	}
983 
984       for (i = 0; i < nrelas; i++)
985 	{
986 	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
987 	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
988 	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
989 	}
990 
991       free (erelas);
992     }
993   else
994     {
995       Elf64_External_Rela * erelas;
996 
997       erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
998                                                  rel_size, _("64-bit relocation data"));
999       if (!erelas)
1000 	return FALSE;
1001 
1002       nrelas = rel_size / sizeof (Elf64_External_Rela);
1003 
1004       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1005                                              sizeof (Elf_Internal_Rela));
1006 
1007       if (relas == NULL)
1008 	{
1009 	  free (erelas);
1010 	  error (_("out of memory parsing relocs\n"));
1011 	  return FALSE;
1012 	}
1013 
1014       for (i = 0; i < nrelas; i++)
1015 	{
1016 	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1017 	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
1018 	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1019 
1020 	  /* The #ifdef BFD64 below is to prevent a compile time
1021 	     warning.  We know that if we do not have a 64 bit data
1022 	     type that we will never execute this code anyway.  */
1023 #ifdef BFD64
1024 	  if (filedata->file_header.e_machine == EM_MIPS
1025 	      && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1026 	    {
1027 	      /* In little-endian objects, r_info isn't really a
1028 		 64-bit little-endian value: it has a 32-bit
1029 		 little-endian symbol index followed by four
1030 		 individual byte fields.  Reorder INFO
1031 		 accordingly.  */
1032 	      bfd_vma inf = relas[i].r_info;
1033 	      inf = (((inf & 0xffffffff) << 32)
1034 		      | ((inf >> 56) & 0xff)
1035 		      | ((inf >> 40) & 0xff00)
1036 		      | ((inf >> 24) & 0xff0000)
1037 		      | ((inf >> 8) & 0xff000000));
1038 	      relas[i].r_info = inf;
1039 	    }
1040 #endif /* BFD64 */
1041 	}
1042 
1043       free (erelas);
1044     }
1045 
1046   *relasp = relas;
1047   *nrelasp = nrelas;
1048   return TRUE;
1049 }
1050 
1051 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1052    Returns TRUE upon success, FALSE otherwise.  If successful then a
1053    pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1054    and the number of relocs loaded is placed in *NRELSP.  It is the caller's
1055    responsibility to free the allocated buffer.  */
1056 
1057 static bfd_boolean
slurp_rel_relocs(Filedata * filedata,unsigned long rel_offset,unsigned long rel_size,Elf_Internal_Rela ** relsp,unsigned long * nrelsp)1058 slurp_rel_relocs (Filedata *            filedata,
1059 		  unsigned long         rel_offset,
1060 		  unsigned long         rel_size,
1061 		  Elf_Internal_Rela **  relsp,
1062 		  unsigned long *       nrelsp)
1063 {
1064   Elf_Internal_Rela * rels;
1065   size_t nrels;
1066   unsigned int i;
1067 
1068   if (is_32bit_elf)
1069     {
1070       Elf32_External_Rel * erels;
1071 
1072       erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1073                                                rel_size, _("32-bit relocation data"));
1074       if (!erels)
1075 	return FALSE;
1076 
1077       nrels = rel_size / sizeof (Elf32_External_Rel);
1078 
1079       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1080 
1081       if (rels == NULL)
1082 	{
1083 	  free (erels);
1084 	  error (_("out of memory parsing relocs\n"));
1085 	  return FALSE;
1086 	}
1087 
1088       for (i = 0; i < nrels; i++)
1089 	{
1090 	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1091 	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
1092 	  rels[i].r_addend = 0;
1093 	}
1094 
1095       free (erels);
1096     }
1097   else
1098     {
1099       Elf64_External_Rel * erels;
1100 
1101       erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1102                                                rel_size, _("64-bit relocation data"));
1103       if (!erels)
1104 	return FALSE;
1105 
1106       nrels = rel_size / sizeof (Elf64_External_Rel);
1107 
1108       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1109 
1110       if (rels == NULL)
1111 	{
1112 	  free (erels);
1113 	  error (_("out of memory parsing relocs\n"));
1114 	  return FALSE;
1115 	}
1116 
1117       for (i = 0; i < nrels; i++)
1118 	{
1119 	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1120 	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
1121 	  rels[i].r_addend = 0;
1122 
1123 	  /* The #ifdef BFD64 below is to prevent a compile time
1124 	     warning.  We know that if we do not have a 64 bit data
1125 	     type that we will never execute this code anyway.  */
1126 #ifdef BFD64
1127 	  if (filedata->file_header.e_machine == EM_MIPS
1128 	      && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1129 	    {
1130 	      /* In little-endian objects, r_info isn't really a
1131 		 64-bit little-endian value: it has a 32-bit
1132 		 little-endian symbol index followed by four
1133 		 individual byte fields.  Reorder INFO
1134 		 accordingly.  */
1135 	      bfd_vma inf = rels[i].r_info;
1136 	      inf = (((inf & 0xffffffff) << 32)
1137 		     | ((inf >> 56) & 0xff)
1138 		     | ((inf >> 40) & 0xff00)
1139 		     | ((inf >> 24) & 0xff0000)
1140 		     | ((inf >> 8) & 0xff000000));
1141 	      rels[i].r_info = inf;
1142 	    }
1143 #endif /* BFD64 */
1144 	}
1145 
1146       free (erels);
1147     }
1148 
1149   *relsp = rels;
1150   *nrelsp = nrels;
1151   return TRUE;
1152 }
1153 
1154 /* Returns the reloc type extracted from the reloc info field.  */
1155 
1156 static unsigned int
get_reloc_type(Filedata * filedata,bfd_vma reloc_info)1157 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1158 {
1159   if (is_32bit_elf)
1160     return ELF32_R_TYPE (reloc_info);
1161 
1162   switch (filedata->file_header.e_machine)
1163     {
1164     case EM_MIPS:
1165       /* Note: We assume that reloc_info has already been adjusted for us.  */
1166       return ELF64_MIPS_R_TYPE (reloc_info);
1167 
1168     case EM_SPARCV9:
1169       return ELF64_R_TYPE_ID (reloc_info);
1170 
1171     default:
1172       return ELF64_R_TYPE (reloc_info);
1173     }
1174 }
1175 
1176 /* Return the symbol index extracted from the reloc info field.  */
1177 
1178 static bfd_vma
get_reloc_symindex(bfd_vma reloc_info)1179 get_reloc_symindex (bfd_vma reloc_info)
1180 {
1181   return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1182 }
1183 
1184 static inline bfd_boolean
uses_msp430x_relocs(Filedata * filedata)1185 uses_msp430x_relocs (Filedata * filedata)
1186 {
1187   return
1188     filedata->file_header.e_machine == EM_MSP430 /* Paranoia.  */
1189     /* GCC uses osabi == ELFOSBI_STANDALONE.  */
1190     && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1191 	/* TI compiler uses ELFOSABI_NONE.  */
1192 	|| (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1193 }
1194 
1195 /* Display the contents of the relocation data found at the specified
1196    offset.  */
1197 
1198 static bfd_boolean
dump_relocations(Filedata * filedata,unsigned long rel_offset,unsigned long rel_size,Elf_Internal_Sym * symtab,unsigned long nsyms,char * strtab,unsigned long strtablen,int is_rela,bfd_boolean is_dynsym)1199 dump_relocations (Filedata *          filedata,
1200 		  unsigned long       rel_offset,
1201 		  unsigned long       rel_size,
1202 		  Elf_Internal_Sym *  symtab,
1203 		  unsigned long       nsyms,
1204 		  char *              strtab,
1205 		  unsigned long       strtablen,
1206 		  int                 is_rela,
1207 		  bfd_boolean         is_dynsym)
1208 {
1209   unsigned long i;
1210   Elf_Internal_Rela * rels;
1211   bfd_boolean res = TRUE;
1212 
1213   if (is_rela == UNKNOWN)
1214     is_rela = guess_is_rela (filedata->file_header.e_machine);
1215 
1216   if (is_rela)
1217     {
1218       if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1219 	return FALSE;
1220     }
1221   else
1222     {
1223       if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1224 	return FALSE;
1225     }
1226 
1227   if (is_32bit_elf)
1228     {
1229       if (is_rela)
1230 	{
1231 	  if (do_wide)
1232 	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name + Addend\n"));
1233 	  else
1234 	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name + Addend\n"));
1235 	}
1236       else
1237 	{
1238 	  if (do_wide)
1239 	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name\n"));
1240 	  else
1241 	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name\n"));
1242 	}
1243     }
1244   else
1245     {
1246       if (is_rela)
1247 	{
1248 	  if (do_wide)
1249 	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend\n"));
1250 	  else
1251 	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name + Addend\n"));
1252 	}
1253       else
1254 	{
1255 	  if (do_wide)
1256 	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name\n"));
1257 	  else
1258 	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name\n"));
1259 	}
1260     }
1261 
1262   for (i = 0; i < rel_size; i++)
1263     {
1264       const char * rtype;
1265       bfd_vma offset;
1266       bfd_vma inf;
1267       bfd_vma symtab_index;
1268       bfd_vma type;
1269 
1270       offset = rels[i].r_offset;
1271       inf    = rels[i].r_info;
1272 
1273       type = get_reloc_type (filedata, inf);
1274       symtab_index = get_reloc_symindex  (inf);
1275 
1276       if (is_32bit_elf)
1277 	{
1278 	  printf ("%8.8lx  %8.8lx ",
1279 		  (unsigned long) offset & 0xffffffff,
1280 		  (unsigned long) inf & 0xffffffff);
1281 	}
1282       else
1283 	{
1284 	  printf (do_wide
1285 		  ? "%16.16" BFD_VMA_FMT "x  %16.16" BFD_VMA_FMT "x "
1286 		  : "%12.12" BFD_VMA_FMT "x  %12.12" BFD_VMA_FMT "x ",
1287 		  offset, inf);
1288 	}
1289 
1290       switch (filedata->file_header.e_machine)
1291 	{
1292 	default:
1293 	  rtype = NULL;
1294 	  break;
1295 
1296 	case EM_AARCH64:
1297 	  rtype = elf_aarch64_reloc_type (type);
1298 	  break;
1299 
1300 	case EM_M32R:
1301 	case EM_CYGNUS_M32R:
1302 	  rtype = elf_m32r_reloc_type (type);
1303 	  break;
1304 
1305 	case EM_386:
1306 	case EM_IAMCU:
1307 	  rtype = elf_i386_reloc_type (type);
1308 	  break;
1309 
1310 	case EM_68HC11:
1311 	case EM_68HC12:
1312 	  rtype = elf_m68hc11_reloc_type (type);
1313 	  break;
1314 
1315 	case EM_S12Z:
1316 	  rtype = elf_s12z_reloc_type (type);
1317 	  break;
1318 
1319 	case EM_68K:
1320 	  rtype = elf_m68k_reloc_type (type);
1321 	  break;
1322 
1323 	case EM_960:
1324 	  rtype = elf_i960_reloc_type (type);
1325 	  break;
1326 
1327 	case EM_AVR:
1328 	case EM_AVR_OLD:
1329 	  rtype = elf_avr_reloc_type (type);
1330 	  break;
1331 
1332 	case EM_OLD_SPARCV9:
1333 	case EM_SPARC32PLUS:
1334 	case EM_SPARCV9:
1335 	case EM_SPARC:
1336 	  rtype = elf_sparc_reloc_type (type);
1337 	  break;
1338 
1339 	case EM_SPU:
1340 	  rtype = elf_spu_reloc_type (type);
1341 	  break;
1342 
1343 	case EM_V800:
1344 	  rtype = v800_reloc_type (type);
1345 	  break;
1346 	case EM_V850:
1347 	case EM_CYGNUS_V850:
1348 	  rtype = v850_reloc_type (type);
1349 	  break;
1350 
1351 	case EM_D10V:
1352 	case EM_CYGNUS_D10V:
1353 	  rtype = elf_d10v_reloc_type (type);
1354 	  break;
1355 
1356 	case EM_D30V:
1357 	case EM_CYGNUS_D30V:
1358 	  rtype = elf_d30v_reloc_type (type);
1359 	  break;
1360 
1361 	case EM_DLX:
1362 	  rtype = elf_dlx_reloc_type (type);
1363 	  break;
1364 
1365 	case EM_SH:
1366 	  rtype = elf_sh_reloc_type (type);
1367 	  break;
1368 
1369 	case EM_MN10300:
1370 	case EM_CYGNUS_MN10300:
1371 	  rtype = elf_mn10300_reloc_type (type);
1372 	  break;
1373 
1374 	case EM_MN10200:
1375 	case EM_CYGNUS_MN10200:
1376 	  rtype = elf_mn10200_reloc_type (type);
1377 	  break;
1378 
1379 	case EM_FR30:
1380 	case EM_CYGNUS_FR30:
1381 	  rtype = elf_fr30_reloc_type (type);
1382 	  break;
1383 
1384 	case EM_CYGNUS_FRV:
1385 	  rtype = elf_frv_reloc_type (type);
1386 	  break;
1387 
1388 	case EM_CSKY:
1389 	  rtype = elf_csky_reloc_type (type);
1390 	  break;
1391 
1392 	case EM_FT32:
1393 	  rtype = elf_ft32_reloc_type (type);
1394 	  break;
1395 
1396 	case EM_MCORE:
1397 	  rtype = elf_mcore_reloc_type (type);
1398 	  break;
1399 
1400 	case EM_MMIX:
1401 	  rtype = elf_mmix_reloc_type (type);
1402 	  break;
1403 
1404 	case EM_MOXIE:
1405 	  rtype = elf_moxie_reloc_type (type);
1406 	  break;
1407 
1408 	case EM_MSP430:
1409 	  if (uses_msp430x_relocs (filedata))
1410 	    {
1411 	      rtype = elf_msp430x_reloc_type (type);
1412 	      break;
1413 	    }
1414 	  /* Fall through.  */
1415 	case EM_MSP430_OLD:
1416 	  rtype = elf_msp430_reloc_type (type);
1417 	  break;
1418 
1419 	case EM_NDS32:
1420 	  rtype = elf_nds32_reloc_type (type);
1421 	  break;
1422 
1423 	case EM_PPC:
1424 	  rtype = elf_ppc_reloc_type (type);
1425 	  break;
1426 
1427 	case EM_PPC64:
1428 	  rtype = elf_ppc64_reloc_type (type);
1429 	  break;
1430 
1431 	case EM_MIPS:
1432 	case EM_MIPS_RS3_LE:
1433 	  rtype = elf_mips_reloc_type (type);
1434 	  break;
1435 
1436 	case EM_RISCV:
1437 	  rtype = elf_riscv_reloc_type (type);
1438 	  break;
1439 
1440 	case EM_ALPHA:
1441 	  rtype = elf_alpha_reloc_type (type);
1442 	  break;
1443 
1444 	case EM_ARM:
1445 	  rtype = elf_arm_reloc_type (type);
1446 	  break;
1447 
1448 	case EM_ARC:
1449 	case EM_ARC_COMPACT:
1450 	case EM_ARC_COMPACT2:
1451 	  rtype = elf_arc_reloc_type (type);
1452 	  break;
1453 
1454 	case EM_PARISC:
1455 	  rtype = elf_hppa_reloc_type (type);
1456 	  break;
1457 
1458 	case EM_H8_300:
1459 	case EM_H8_300H:
1460 	case EM_H8S:
1461 	  rtype = elf_h8_reloc_type (type);
1462 	  break;
1463 
1464 	case EM_OR1K:
1465 	  rtype = elf_or1k_reloc_type (type);
1466 	  break;
1467 
1468 	case EM_PJ:
1469 	case EM_PJ_OLD:
1470 	  rtype = elf_pj_reloc_type (type);
1471 	  break;
1472 	case EM_IA_64:
1473 	  rtype = elf_ia64_reloc_type (type);
1474 	  break;
1475 
1476 	case EM_CRIS:
1477 	  rtype = elf_cris_reloc_type (type);
1478 	  break;
1479 
1480 	case EM_860:
1481 	  rtype = elf_i860_reloc_type (type);
1482 	  break;
1483 
1484 	case EM_X86_64:
1485 	case EM_L1OM:
1486 	case EM_K1OM:
1487 	  rtype = elf_x86_64_reloc_type (type);
1488 	  break;
1489 
1490 	case EM_S370:
1491 	  rtype = i370_reloc_type (type);
1492 	  break;
1493 
1494 	case EM_S390_OLD:
1495 	case EM_S390:
1496 	  rtype = elf_s390_reloc_type (type);
1497 	  break;
1498 
1499 	case EM_SCORE:
1500 	  rtype = elf_score_reloc_type (type);
1501 	  break;
1502 
1503 	case EM_XSTORMY16:
1504 	  rtype = elf_xstormy16_reloc_type (type);
1505 	  break;
1506 
1507 	case EM_CRX:
1508 	  rtype = elf_crx_reloc_type (type);
1509 	  break;
1510 
1511 	case EM_VAX:
1512 	  rtype = elf_vax_reloc_type (type);
1513 	  break;
1514 
1515 	case EM_VISIUM:
1516 	  rtype = elf_visium_reloc_type (type);
1517 	  break;
1518 
1519         case EM_BPF:
1520           rtype = elf_bpf_reloc_type (type);
1521           break;
1522 
1523 	case EM_ADAPTEVA_EPIPHANY:
1524 	  rtype = elf_epiphany_reloc_type (type);
1525 	  break;
1526 
1527 	case EM_IP2K:
1528 	case EM_IP2K_OLD:
1529 	  rtype = elf_ip2k_reloc_type (type);
1530 	  break;
1531 
1532 	case EM_IQ2000:
1533 	  rtype = elf_iq2000_reloc_type (type);
1534 	  break;
1535 
1536 	case EM_XTENSA_OLD:
1537 	case EM_XTENSA:
1538 	  rtype = elf_xtensa_reloc_type (type);
1539 	  break;
1540 
1541 	case EM_LATTICEMICO32:
1542 	  rtype = elf_lm32_reloc_type (type);
1543 	  break;
1544 
1545 	case EM_M32C_OLD:
1546 	case EM_M32C:
1547 	  rtype = elf_m32c_reloc_type (type);
1548 	  break;
1549 
1550 	case EM_MT:
1551 	  rtype = elf_mt_reloc_type (type);
1552 	  break;
1553 
1554 	case EM_BLACKFIN:
1555 	  rtype = elf_bfin_reloc_type (type);
1556 	  break;
1557 
1558 	case EM_CYGNUS_MEP:
1559 	  rtype = elf_mep_reloc_type (type);
1560 	  break;
1561 
1562 	case EM_CR16:
1563 	  rtype = elf_cr16_reloc_type (type);
1564 	  break;
1565 
1566 	case EM_MICROBLAZE:
1567 	case EM_MICROBLAZE_OLD:
1568 	  rtype = elf_microblaze_reloc_type (type);
1569 	  break;
1570 
1571 	case EM_RL78:
1572 	  rtype = elf_rl78_reloc_type (type);
1573 	  break;
1574 
1575 	case EM_RX:
1576 	  rtype = elf_rx_reloc_type (type);
1577 	  break;
1578 
1579 	case EM_METAG:
1580 	  rtype = elf_metag_reloc_type (type);
1581 	  break;
1582 
1583 	case EM_XC16X:
1584 	case EM_C166:
1585 	  rtype = elf_xc16x_reloc_type (type);
1586 	  break;
1587 
1588 	case EM_TI_C6000:
1589 	  rtype = elf_tic6x_reloc_type (type);
1590 	  break;
1591 
1592 	case EM_TILEGX:
1593 	  rtype = elf_tilegx_reloc_type (type);
1594 	  break;
1595 
1596 	case EM_TILEPRO:
1597 	  rtype = elf_tilepro_reloc_type (type);
1598 	  break;
1599 
1600 	case EM_WEBASSEMBLY:
1601 	  rtype = elf_wasm32_reloc_type (type);
1602 	  break;
1603 
1604 	case EM_XGATE:
1605 	  rtype = elf_xgate_reloc_type (type);
1606 	  break;
1607 
1608 	case EM_ALTERA_NIOS2:
1609 	  rtype = elf_nios2_reloc_type (type);
1610 	  break;
1611 
1612 	case EM_TI_PRU:
1613 	  rtype = elf_pru_reloc_type (type);
1614 	  break;
1615 
1616 	case EM_NFP:
1617 	  if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1618 	    rtype = elf_nfp3200_reloc_type (type);
1619 	  else
1620 	    rtype = elf_nfp_reloc_type (type);
1621 	  break;
1622 
1623 	case EM_Z80:
1624 	  rtype = elf_z80_reloc_type (type);
1625 	  break;
1626 	}
1627 
1628       if (rtype == NULL)
1629 	printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1630       else
1631 	printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1632 
1633       if (filedata->file_header.e_machine == EM_ALPHA
1634 	  && rtype != NULL
1635 	  && streq (rtype, "R_ALPHA_LITUSE")
1636 	  && is_rela)
1637 	{
1638 	  switch (rels[i].r_addend)
1639 	    {
1640 	    case LITUSE_ALPHA_ADDR:   rtype = "ADDR";   break;
1641 	    case LITUSE_ALPHA_BASE:   rtype = "BASE";   break;
1642 	    case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1643 	    case LITUSE_ALPHA_JSR:    rtype = "JSR";    break;
1644 	    case LITUSE_ALPHA_TLSGD:  rtype = "TLSGD";  break;
1645 	    case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1646 	    case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1647 	    default: rtype = NULL;
1648 	    }
1649 
1650 	  if (rtype)
1651 	    printf (" (%s)", rtype);
1652 	  else
1653 	    {
1654 	      putchar (' ');
1655 	      printf (_("<unknown addend: %lx>"),
1656 		      (unsigned long) rels[i].r_addend);
1657 	      res = FALSE;
1658 	    }
1659 	}
1660       else if (symtab_index)
1661 	{
1662 	  if (symtab == NULL || symtab_index >= nsyms)
1663 	    {
1664 	      error (_(" bad symbol index: %08lx in reloc\n"),
1665 		     (unsigned long) symtab_index);
1666 	      res = FALSE;
1667 	    }
1668 	  else
1669 	    {
1670 	      Elf_Internal_Sym * psym;
1671 	      const char * version_string;
1672 	      enum versioned_symbol_info sym_info;
1673 	      unsigned short vna_other;
1674 
1675 	      psym = symtab + symtab_index;
1676 
1677 	      version_string
1678 		= get_symbol_version_string (filedata, is_dynsym,
1679 					     strtab, strtablen,
1680 					     symtab_index,
1681 					     psym,
1682 					     &sym_info,
1683 					     &vna_other);
1684 
1685 	      printf (" ");
1686 
1687 	      if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1688 		{
1689 		  const char * name;
1690 		  unsigned int len;
1691 		  unsigned int width = is_32bit_elf ? 8 : 14;
1692 
1693 		  /* Relocations against GNU_IFUNC symbols do not use the value
1694 		     of the symbol as the address to relocate against.  Instead
1695 		     they invoke the function named by the symbol and use its
1696 		     result as the address for relocation.
1697 
1698 		     To indicate this to the user, do not display the value of
1699 		     the symbol in the "Symbols's Value" field.  Instead show
1700 		     its name followed by () as a hint that the symbol is
1701 		     invoked.  */
1702 
1703 		  if (strtab == NULL
1704 		      || psym->st_name == 0
1705 		      || psym->st_name >= strtablen)
1706 		    name = "??";
1707 		  else
1708 		    name = strtab + psym->st_name;
1709 
1710 		  len = print_symbol (width, name);
1711 		  if (version_string)
1712 		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
1713 			    version_string);
1714 		  printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1715 		}
1716 	      else
1717 		{
1718 		  print_vma (psym->st_value, LONG_HEX);
1719 
1720 		  printf (is_32bit_elf ? "   " : " ");
1721 		}
1722 
1723 	      if (psym->st_name == 0)
1724 		{
1725 		  const char * sec_name = "<null>";
1726 		  char name_buf[40];
1727 
1728 		  if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1729 		    {
1730 		      if (psym->st_shndx < filedata->file_header.e_shnum)
1731 			sec_name = SECTION_NAME (filedata->section_headers + psym->st_shndx);
1732 		      else if (psym->st_shndx == SHN_ABS)
1733 			sec_name = "ABS";
1734 		      else if (psym->st_shndx == SHN_COMMON)
1735 			sec_name = "COMMON";
1736 		      else if ((filedata->file_header.e_machine == EM_MIPS
1737 				&& psym->st_shndx == SHN_MIPS_SCOMMON)
1738 			       || (filedata->file_header.e_machine == EM_TI_C6000
1739 				   && psym->st_shndx == SHN_TIC6X_SCOMMON))
1740 			sec_name = "SCOMMON";
1741 		      else if (filedata->file_header.e_machine == EM_MIPS
1742 			       && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1743 			sec_name = "SUNDEF";
1744 		      else if ((filedata->file_header.e_machine == EM_X86_64
1745 				|| filedata->file_header.e_machine == EM_L1OM
1746 				|| filedata->file_header.e_machine == EM_K1OM)
1747 			       && psym->st_shndx == SHN_X86_64_LCOMMON)
1748 			sec_name = "LARGE_COMMON";
1749 		      else if (filedata->file_header.e_machine == EM_IA_64
1750 			       && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1751 			       && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1752 			sec_name = "ANSI_COM";
1753 		      else if (is_ia64_vms (filedata)
1754 			       && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1755 			sec_name = "VMS_SYMVEC";
1756 		      else
1757 			{
1758 			  sprintf (name_buf, "<section 0x%x>",
1759 				   (unsigned int) psym->st_shndx);
1760 			  sec_name = name_buf;
1761 			}
1762 		    }
1763 		  print_symbol (22, sec_name);
1764 		}
1765 	      else if (strtab == NULL)
1766 		printf (_("<string table index: %3ld>"), psym->st_name);
1767 	      else if (psym->st_name >= strtablen)
1768 		{
1769 		  error (_("<corrupt string table index: %3ld>\n"),
1770 			 psym->st_name);
1771 		  res = FALSE;
1772 		}
1773 	      else
1774 		{
1775 		  print_symbol (22, strtab + psym->st_name);
1776 		  if (version_string)
1777 		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
1778 			    version_string);
1779 		}
1780 
1781 	      if (is_rela)
1782 		{
1783 		  bfd_vma off = rels[i].r_addend;
1784 
1785 		  if ((bfd_signed_vma) off < 0)
1786 		    printf (" - %" BFD_VMA_FMT "x", - off);
1787 		  else
1788 		    printf (" + %" BFD_VMA_FMT "x", off);
1789 		}
1790 	    }
1791 	}
1792       else if (is_rela)
1793 	{
1794 	  bfd_vma off = rels[i].r_addend;
1795 
1796 	  printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1797 	  if ((bfd_signed_vma) off < 0)
1798 	    printf ("-%" BFD_VMA_FMT "x", - off);
1799 	  else
1800 	    printf ("%" BFD_VMA_FMT "x", off);
1801 	}
1802 
1803       if (filedata->file_header.e_machine == EM_SPARCV9
1804 	  && rtype != NULL
1805 	  && streq (rtype, "R_SPARC_OLO10"))
1806 	printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1807 
1808       putchar ('\n');
1809 
1810 #ifdef BFD64
1811       if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
1812 	{
1813 	  bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1814 	  bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1815 	  const char * rtype2 = elf_mips_reloc_type (type2);
1816 	  const char * rtype3 = elf_mips_reloc_type (type3);
1817 
1818 	  printf ("                    Type2: ");
1819 
1820 	  if (rtype2 == NULL)
1821 	    printf (_("unrecognized: %-7lx"),
1822 		    (unsigned long) type2 & 0xffffffff);
1823 	  else
1824 	    printf ("%-17.17s", rtype2);
1825 
1826 	  printf ("\n                    Type3: ");
1827 
1828 	  if (rtype3 == NULL)
1829 	    printf (_("unrecognized: %-7lx"),
1830 		    (unsigned long) type3 & 0xffffffff);
1831 	  else
1832 	    printf ("%-17.17s", rtype3);
1833 
1834 	  putchar ('\n');
1835 	}
1836 #endif /* BFD64 */
1837     }
1838 
1839   free (rels);
1840 
1841   return res;
1842 }
1843 
1844 static const char *
get_aarch64_dynamic_type(unsigned long type)1845 get_aarch64_dynamic_type (unsigned long type)
1846 {
1847   switch (type)
1848     {
1849     case DT_AARCH64_BTI_PLT:  return "AARCH64_BTI_PLT";
1850     case DT_AARCH64_PAC_PLT:  return "AARCH64_PAC_PLT";
1851     case DT_AARCH64_VARIANT_PCS:  return "AARCH64_VARIANT_PCS";
1852     default:
1853       return NULL;
1854     }
1855 }
1856 
1857 static const char *
get_mips_dynamic_type(unsigned long type)1858 get_mips_dynamic_type (unsigned long type)
1859 {
1860   switch (type)
1861     {
1862     case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1863     case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1864     case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1865     case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1866     case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1867     case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1868     case DT_MIPS_MSYM: return "MIPS_MSYM";
1869     case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1870     case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1871     case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1872     case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1873     case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1874     case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1875     case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1876     case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1877     case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1878     case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1879     case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1880     case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1881     case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1882     case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1883     case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1884     case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1885     case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1886     case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1887     case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1888     case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1889     case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1890     case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1891     case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1892     case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1893     case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1894     case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1895     case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1896     case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1897     case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1898     case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1899     case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1900     case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1901     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1902     case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1903     case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1904     case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1905     case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1906     case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1907     case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1908     case DT_MIPS_XHASH: return "MIPS_XHASH";
1909     default:
1910       return NULL;
1911     }
1912 }
1913 
1914 static const char *
get_sparc64_dynamic_type(unsigned long type)1915 get_sparc64_dynamic_type (unsigned long type)
1916 {
1917   switch (type)
1918     {
1919     case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1920     default:
1921       return NULL;
1922     }
1923 }
1924 
1925 static const char *
get_ppc_dynamic_type(unsigned long type)1926 get_ppc_dynamic_type (unsigned long type)
1927 {
1928   switch (type)
1929     {
1930     case DT_PPC_GOT:    return "PPC_GOT";
1931     case DT_PPC_OPT:    return "PPC_OPT";
1932     default:
1933       return NULL;
1934     }
1935 }
1936 
1937 static const char *
get_ppc64_dynamic_type(unsigned long type)1938 get_ppc64_dynamic_type (unsigned long type)
1939 {
1940   switch (type)
1941     {
1942     case DT_PPC64_GLINK:  return "PPC64_GLINK";
1943     case DT_PPC64_OPD:    return "PPC64_OPD";
1944     case DT_PPC64_OPDSZ:  return "PPC64_OPDSZ";
1945     case DT_PPC64_OPT:    return "PPC64_OPT";
1946     default:
1947       return NULL;
1948     }
1949 }
1950 
1951 static const char *
get_parisc_dynamic_type(unsigned long type)1952 get_parisc_dynamic_type (unsigned long type)
1953 {
1954   switch (type)
1955     {
1956     case DT_HP_LOAD_MAP:	return "HP_LOAD_MAP";
1957     case DT_HP_DLD_FLAGS:	return "HP_DLD_FLAGS";
1958     case DT_HP_DLD_HOOK:	return "HP_DLD_HOOK";
1959     case DT_HP_UX10_INIT:	return "HP_UX10_INIT";
1960     case DT_HP_UX10_INITSZ:	return "HP_UX10_INITSZ";
1961     case DT_HP_PREINIT:		return "HP_PREINIT";
1962     case DT_HP_PREINITSZ:	return "HP_PREINITSZ";
1963     case DT_HP_NEEDED:		return "HP_NEEDED";
1964     case DT_HP_TIME_STAMP:	return "HP_TIME_STAMP";
1965     case DT_HP_CHECKSUM:	return "HP_CHECKSUM";
1966     case DT_HP_GST_SIZE:	return "HP_GST_SIZE";
1967     case DT_HP_GST_VERSION:	return "HP_GST_VERSION";
1968     case DT_HP_GST_HASHVAL:	return "HP_GST_HASHVAL";
1969     case DT_HP_EPLTREL:		return "HP_GST_EPLTREL";
1970     case DT_HP_EPLTRELSZ:	return "HP_GST_EPLTRELSZ";
1971     case DT_HP_FILTERED:	return "HP_FILTERED";
1972     case DT_HP_FILTER_TLS:	return "HP_FILTER_TLS";
1973     case DT_HP_COMPAT_FILTERED:	return "HP_COMPAT_FILTERED";
1974     case DT_HP_LAZYLOAD:	return "HP_LAZYLOAD";
1975     case DT_HP_BIND_NOW_COUNT:	return "HP_BIND_NOW_COUNT";
1976     case DT_PLT:		return "PLT";
1977     case DT_PLT_SIZE:		return "PLT_SIZE";
1978     case DT_DLT:		return "DLT";
1979     case DT_DLT_SIZE:		return "DLT_SIZE";
1980     default:
1981       return NULL;
1982     }
1983 }
1984 
1985 static const char *
get_ia64_dynamic_type(unsigned long type)1986 get_ia64_dynamic_type (unsigned long type)
1987 {
1988   switch (type)
1989     {
1990     case DT_IA_64_PLT_RESERVE:         return "IA_64_PLT_RESERVE";
1991     case DT_IA_64_VMS_SUBTYPE:         return "VMS_SUBTYPE";
1992     case DT_IA_64_VMS_IMGIOCNT:        return "VMS_IMGIOCNT";
1993     case DT_IA_64_VMS_LNKFLAGS:        return "VMS_LNKFLAGS";
1994     case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1995     case DT_IA_64_VMS_IDENT:           return "VMS_IDENT";
1996     case DT_IA_64_VMS_NEEDED_IDENT:    return "VMS_NEEDED_IDENT";
1997     case DT_IA_64_VMS_IMG_RELA_CNT:    return "VMS_IMG_RELA_CNT";
1998     case DT_IA_64_VMS_SEG_RELA_CNT:    return "VMS_SEG_RELA_CNT";
1999     case DT_IA_64_VMS_FIXUP_RELA_CNT:  return "VMS_FIXUP_RELA_CNT";
2000     case DT_IA_64_VMS_FIXUP_NEEDED:    return "VMS_FIXUP_NEEDED";
2001     case DT_IA_64_VMS_SYMVEC_CNT:      return "VMS_SYMVEC_CNT";
2002     case DT_IA_64_VMS_XLATED:          return "VMS_XLATED";
2003     case DT_IA_64_VMS_STACKSIZE:       return "VMS_STACKSIZE";
2004     case DT_IA_64_VMS_UNWINDSZ:        return "VMS_UNWINDSZ";
2005     case DT_IA_64_VMS_UNWIND_CODSEG:   return "VMS_UNWIND_CODSEG";
2006     case DT_IA_64_VMS_UNWIND_INFOSEG:  return "VMS_UNWIND_INFOSEG";
2007     case DT_IA_64_VMS_LINKTIME:        return "VMS_LINKTIME";
2008     case DT_IA_64_VMS_SEG_NO:          return "VMS_SEG_NO";
2009     case DT_IA_64_VMS_SYMVEC_OFFSET:   return "VMS_SYMVEC_OFFSET";
2010     case DT_IA_64_VMS_SYMVEC_SEG:      return "VMS_SYMVEC_SEG";
2011     case DT_IA_64_VMS_UNWIND_OFFSET:   return "VMS_UNWIND_OFFSET";
2012     case DT_IA_64_VMS_UNWIND_SEG:      return "VMS_UNWIND_SEG";
2013     case DT_IA_64_VMS_STRTAB_OFFSET:   return "VMS_STRTAB_OFFSET";
2014     case DT_IA_64_VMS_SYSVER_OFFSET:   return "VMS_SYSVER_OFFSET";
2015     case DT_IA_64_VMS_IMG_RELA_OFF:    return "VMS_IMG_RELA_OFF";
2016     case DT_IA_64_VMS_SEG_RELA_OFF:    return "VMS_SEG_RELA_OFF";
2017     case DT_IA_64_VMS_FIXUP_RELA_OFF:  return "VMS_FIXUP_RELA_OFF";
2018     case DT_IA_64_VMS_PLTGOT_OFFSET:   return "VMS_PLTGOT_OFFSET";
2019     case DT_IA_64_VMS_PLTGOT_SEG:      return "VMS_PLTGOT_SEG";
2020     case DT_IA_64_VMS_FPMODE:          return "VMS_FPMODE";
2021     default:
2022       return NULL;
2023     }
2024 }
2025 
2026 static const char *
get_solaris_section_type(unsigned long type)2027 get_solaris_section_type (unsigned long type)
2028 {
2029   switch (type)
2030     {
2031     case 0x6fffffee: return "SUNW_ancillary";
2032     case 0x6fffffef: return "SUNW_capchain";
2033     case 0x6ffffff0: return "SUNW_capinfo";
2034     case 0x6ffffff1: return "SUNW_symsort";
2035     case 0x6ffffff2: return "SUNW_tlssort";
2036     case 0x6ffffff3: return "SUNW_LDYNSYM";
2037     case 0x6ffffff4: return "SUNW_dof";
2038     case 0x6ffffff5: return "SUNW_cap";
2039     case 0x6ffffff6: return "SUNW_SIGNATURE";
2040     case 0x6ffffff7: return "SUNW_ANNOTATE";
2041     case 0x6ffffff8: return "SUNW_DEBUGSTR";
2042     case 0x6ffffff9: return "SUNW_DEBUG";
2043     case 0x6ffffffa: return "SUNW_move";
2044     case 0x6ffffffb: return "SUNW_COMDAT";
2045     case 0x6ffffffc: return "SUNW_syminfo";
2046     case 0x6ffffffd: return "SUNW_verdef";
2047     case 0x6ffffffe: return "SUNW_verneed";
2048     case 0x6fffffff: return "SUNW_versym";
2049     case 0x70000000: return "SPARC_GOTDATA";
2050     default: return NULL;
2051     }
2052 }
2053 
2054 static const char *
get_alpha_dynamic_type(unsigned long type)2055 get_alpha_dynamic_type (unsigned long type)
2056 {
2057   switch (type)
2058     {
2059     case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2060     default: return NULL;
2061     }
2062 }
2063 
2064 static const char *
get_score_dynamic_type(unsigned long type)2065 get_score_dynamic_type (unsigned long type)
2066 {
2067   switch (type)
2068     {
2069     case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2070     case DT_SCORE_LOCAL_GOTNO:  return "SCORE_LOCAL_GOTNO";
2071     case DT_SCORE_SYMTABNO:     return "SCORE_SYMTABNO";
2072     case DT_SCORE_GOTSYM:       return "SCORE_GOTSYM";
2073     case DT_SCORE_UNREFEXTNO:   return "SCORE_UNREFEXTNO";
2074     case DT_SCORE_HIPAGENO:     return "SCORE_HIPAGENO";
2075     default:                    return NULL;
2076     }
2077 }
2078 
2079 static const char *
get_tic6x_dynamic_type(unsigned long type)2080 get_tic6x_dynamic_type (unsigned long type)
2081 {
2082   switch (type)
2083     {
2084     case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2085     case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2086     case DT_C6000_DSBT_BASE:   return "C6000_DSBT_BASE";
2087     case DT_C6000_DSBT_SIZE:   return "C6000_DSBT_SIZE";
2088     case DT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
2089     case DT_C6000_DSBT_INDEX:  return "C6000_DSBT_INDEX";
2090     default:                   return NULL;
2091     }
2092 }
2093 
2094 static const char *
get_nios2_dynamic_type(unsigned long type)2095 get_nios2_dynamic_type (unsigned long type)
2096 {
2097   switch (type)
2098     {
2099     case DT_NIOS2_GP: return "NIOS2_GP";
2100     default:          return NULL;
2101     }
2102 }
2103 
2104 static const char *
get_solaris_dynamic_type(unsigned long type)2105 get_solaris_dynamic_type (unsigned long type)
2106 {
2107   switch (type)
2108     {
2109     case 0x6000000d: return "SUNW_AUXILIARY";
2110     case 0x6000000e: return "SUNW_RTLDINF";
2111     case 0x6000000f: return "SUNW_FILTER";
2112     case 0x60000010: return "SUNW_CAP";
2113     case 0x60000011: return "SUNW_SYMTAB";
2114     case 0x60000012: return "SUNW_SYMSZ";
2115     case 0x60000013: return "SUNW_SORTENT";
2116     case 0x60000014: return "SUNW_SYMSORT";
2117     case 0x60000015: return "SUNW_SYMSORTSZ";
2118     case 0x60000016: return "SUNW_TLSSORT";
2119     case 0x60000017: return "SUNW_TLSSORTSZ";
2120     case 0x60000018: return "SUNW_CAPINFO";
2121     case 0x60000019: return "SUNW_STRPAD";
2122     case 0x6000001a: return "SUNW_CAPCHAIN";
2123     case 0x6000001b: return "SUNW_LDMACH";
2124     case 0x6000001d: return "SUNW_CAPCHAINENT";
2125     case 0x6000001f: return "SUNW_CAPCHAINSZ";
2126     case 0x60000021: return "SUNW_PARENT";
2127     case 0x60000023: return "SUNW_ASLR";
2128     case 0x60000025: return "SUNW_RELAX";
2129     case 0x60000029: return "SUNW_NXHEAP";
2130     case 0x6000002b: return "SUNW_NXSTACK";
2131 
2132     case 0x70000001: return "SPARC_REGISTER";
2133     case 0x7ffffffd: return "AUXILIARY";
2134     case 0x7ffffffe: return "USED";
2135     case 0x7fffffff: return "FILTER";
2136 
2137     default: return NULL;
2138     }
2139 }
2140 
2141 static const char *
get_dynamic_type(Filedata * filedata,unsigned long type)2142 get_dynamic_type (Filedata * filedata, unsigned long type)
2143 {
2144   static char buff[64];
2145 
2146   switch (type)
2147     {
2148     case DT_NULL:	return "NULL";
2149     case DT_NEEDED:	return "NEEDED";
2150     case DT_PLTRELSZ:	return "PLTRELSZ";
2151     case DT_PLTGOT:	return "PLTGOT";
2152     case DT_HASH:	return "HASH";
2153     case DT_STRTAB:	return "STRTAB";
2154     case DT_SYMTAB:	return "SYMTAB";
2155     case DT_RELA:	return "RELA";
2156     case DT_RELASZ:	return "RELASZ";
2157     case DT_RELAENT:	return "RELAENT";
2158     case DT_STRSZ:	return "STRSZ";
2159     case DT_SYMENT:	return "SYMENT";
2160     case DT_INIT:	return "INIT";
2161     case DT_FINI:	return "FINI";
2162     case DT_SONAME:	return "SONAME";
2163     case DT_RPATH:	return "RPATH";
2164     case DT_SYMBOLIC:	return "SYMBOLIC";
2165     case DT_REL:	return "REL";
2166     case DT_RELSZ:	return "RELSZ";
2167     case DT_RELENT:	return "RELENT";
2168     case DT_PLTREL:	return "PLTREL";
2169     case DT_DEBUG:	return "DEBUG";
2170     case DT_TEXTREL:	return "TEXTREL";
2171     case DT_JMPREL:	return "JMPREL";
2172     case DT_BIND_NOW:   return "BIND_NOW";
2173     case DT_INIT_ARRAY: return "INIT_ARRAY";
2174     case DT_FINI_ARRAY: return "FINI_ARRAY";
2175     case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2176     case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2177     case DT_RUNPATH:    return "RUNPATH";
2178     case DT_FLAGS:      return "FLAGS";
2179 
2180     case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2181     case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2182     case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2183 
2184     case DT_CHECKSUM:	return "CHECKSUM";
2185     case DT_PLTPADSZ:	return "PLTPADSZ";
2186     case DT_MOVEENT:	return "MOVEENT";
2187     case DT_MOVESZ:	return "MOVESZ";
2188     case DT_FEATURE:	return "FEATURE";
2189     case DT_POSFLAG_1:	return "POSFLAG_1";
2190     case DT_SYMINSZ:	return "SYMINSZ";
2191     case DT_SYMINENT:	return "SYMINENT"; /* aka VALRNGHI */
2192 
2193     case DT_ADDRRNGLO:  return "ADDRRNGLO";
2194     case DT_CONFIG:	return "CONFIG";
2195     case DT_DEPAUDIT:	return "DEPAUDIT";
2196     case DT_AUDIT:	return "AUDIT";
2197     case DT_PLTPAD:	return "PLTPAD";
2198     case DT_MOVETAB:	return "MOVETAB";
2199     case DT_SYMINFO:	return "SYMINFO"; /* aka ADDRRNGHI */
2200 
2201     case DT_VERSYM:	return "VERSYM";
2202 
2203     case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2204     case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2205     case DT_RELACOUNT:	return "RELACOUNT";
2206     case DT_RELCOUNT:	return "RELCOUNT";
2207     case DT_FLAGS_1:	return "FLAGS_1";
2208     case DT_VERDEF:	return "VERDEF";
2209     case DT_VERDEFNUM:	return "VERDEFNUM";
2210     case DT_VERNEED:	return "VERNEED";
2211     case DT_VERNEEDNUM:	return "VERNEEDNUM";
2212 
2213     case DT_AUXILIARY:	return "AUXILIARY";
2214     case DT_USED:	return "USED";
2215     case DT_FILTER:	return "FILTER";
2216 
2217     case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2218     case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2219     case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2220     case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2221     case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2222     case DT_GNU_HASH:	return "GNU_HASH";
2223 
2224     default:
2225       if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2226 	{
2227 	  const char * result;
2228 
2229 	  switch (filedata->file_header.e_machine)
2230 	    {
2231 	    case EM_AARCH64:
2232 	      result = get_aarch64_dynamic_type (type);
2233 	      break;
2234 	    case EM_MIPS:
2235 	    case EM_MIPS_RS3_LE:
2236 	      result = get_mips_dynamic_type (type);
2237 	      break;
2238 	    case EM_SPARCV9:
2239 	      result = get_sparc64_dynamic_type (type);
2240 	      break;
2241 	    case EM_PPC:
2242 	      result = get_ppc_dynamic_type (type);
2243 	      break;
2244 	    case EM_PPC64:
2245 	      result = get_ppc64_dynamic_type (type);
2246 	      break;
2247 	    case EM_IA_64:
2248 	      result = get_ia64_dynamic_type (type);
2249 	      break;
2250 	    case EM_ALPHA:
2251 	      result = get_alpha_dynamic_type (type);
2252 	      break;
2253 	    case EM_SCORE:
2254 	      result = get_score_dynamic_type (type);
2255 	      break;
2256 	    case EM_TI_C6000:
2257 	      result = get_tic6x_dynamic_type (type);
2258 	      break;
2259 	    case EM_ALTERA_NIOS2:
2260 	      result = get_nios2_dynamic_type (type);
2261 	      break;
2262 	    default:
2263 	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2264 		result = get_solaris_dynamic_type (type);
2265 	      else
2266 		result = NULL;
2267 	      break;
2268 	    }
2269 
2270 	  if (result != NULL)
2271 	    return result;
2272 
2273 	  snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2274 	}
2275       else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2276 	       || (filedata->file_header.e_machine == EM_PARISC
2277 		   && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2278 	{
2279 	  const char * result;
2280 
2281 	  switch (filedata->file_header.e_machine)
2282 	    {
2283 	    case EM_PARISC:
2284 	      result = get_parisc_dynamic_type (type);
2285 	      break;
2286 	    case EM_IA_64:
2287 	      result = get_ia64_dynamic_type (type);
2288 	      break;
2289 	    default:
2290 	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2291 		result = get_solaris_dynamic_type (type);
2292 	      else
2293 		result = NULL;
2294 	      break;
2295 	    }
2296 
2297 	  if (result != NULL)
2298 	    return result;
2299 
2300 	  snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2301 		    type);
2302 	}
2303       else
2304 	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2305 
2306       return buff;
2307     }
2308 }
2309 
2310 static char *
get_file_type(unsigned e_type)2311 get_file_type (unsigned e_type)
2312 {
2313   static char buff[64];
2314 
2315   switch (e_type)
2316     {
2317     case ET_NONE: return _("NONE (None)");
2318     case ET_REL:  return _("REL (Relocatable file)");
2319     case ET_EXEC: return _("EXEC (Executable file)");
2320     case ET_DYN:  return _("DYN (Shared object file)");
2321     case ET_CORE: return _("CORE (Core file)");
2322 
2323     default:
2324       if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2325 	snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2326       else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2327 	snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2328       else
2329 	snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2330       return buff;
2331     }
2332 }
2333 
2334 static char *
get_machine_name(unsigned e_machine)2335 get_machine_name (unsigned e_machine)
2336 {
2337   static char buff[64]; /* XXX */
2338 
2339   switch (e_machine)
2340     {
2341       /* Please keep this switch table sorted by increasing EM_ value.  */
2342       /* 0 */
2343     case EM_NONE:		return _("None");
2344     case EM_M32:		return "WE32100";
2345     case EM_SPARC:		return "Sparc";
2346     case EM_386:		return "Intel 80386";
2347     case EM_68K:		return "MC68000";
2348     case EM_88K:		return "MC88000";
2349     case EM_IAMCU:		return "Intel MCU";
2350     case EM_860:		return "Intel 80860";
2351     case EM_MIPS:		return "MIPS R3000";
2352     case EM_S370:		return "IBM System/370";
2353       /* 10 */
2354     case EM_MIPS_RS3_LE:	return "MIPS R4000 big-endian";
2355     case EM_OLD_SPARCV9:	return "Sparc v9 (old)";
2356     case EM_PARISC:		return "HPPA";
2357     case EM_VPP550:		return "Fujitsu VPP500";
2358     case EM_SPARC32PLUS:	return "Sparc v8+" ;
2359     case EM_960:		return "Intel 80960";
2360     case EM_PPC:		return "PowerPC";
2361       /* 20 */
2362     case EM_PPC64:		return "PowerPC64";
2363     case EM_S390_OLD:
2364     case EM_S390:		return "IBM S/390";
2365     case EM_SPU:		return "SPU";
2366       /* 30 */
2367     case EM_V800:		return "Renesas V850 (using RH850 ABI)";
2368     case EM_FR20:		return "Fujitsu FR20";
2369     case EM_RH32:		return "TRW RH32";
2370     case EM_MCORE:		return "MCORE";
2371       /* 40 */
2372     case EM_ARM:		return "ARM";
2373     case EM_OLD_ALPHA:		return "Digital Alpha (old)";
2374     case EM_SH:			return "Renesas / SuperH SH";
2375     case EM_SPARCV9:		return "Sparc v9";
2376     case EM_TRICORE:		return "Siemens Tricore";
2377     case EM_ARC:		return "ARC";
2378     case EM_H8_300:		return "Renesas H8/300";
2379     case EM_H8_300H:		return "Renesas H8/300H";
2380     case EM_H8S:		return "Renesas H8S";
2381     case EM_H8_500:		return "Renesas H8/500";
2382       /* 50 */
2383     case EM_IA_64:		return "Intel IA-64";
2384     case EM_MIPS_X:		return "Stanford MIPS-X";
2385     case EM_COLDFIRE:		return "Motorola Coldfire";
2386     case EM_68HC12:		return "Motorola MC68HC12 Microcontroller";
2387     case EM_MMA:		return "Fujitsu Multimedia Accelerator";
2388     case EM_PCP:		return "Siemens PCP";
2389     case EM_NCPU:		return "Sony nCPU embedded RISC processor";
2390     case EM_NDR1:		return "Denso NDR1 microprocesspr";
2391     case EM_STARCORE:		return "Motorola Star*Core processor";
2392     case EM_ME16:		return "Toyota ME16 processor";
2393       /* 60 */
2394     case EM_ST100:		return "STMicroelectronics ST100 processor";
2395     case EM_TINYJ:		return "Advanced Logic Corp. TinyJ embedded processor";
2396     case EM_X86_64:		return "Advanced Micro Devices X86-64";
2397     case EM_PDSP:		return "Sony DSP processor";
2398     case EM_PDP10:		return "Digital Equipment Corp. PDP-10";
2399     case EM_PDP11:		return "Digital Equipment Corp. PDP-11";
2400     case EM_FX66:		return "Siemens FX66 microcontroller";
2401     case EM_ST9PLUS:		return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2402     case EM_ST7:		return "STMicroelectronics ST7 8-bit microcontroller";
2403     case EM_68HC16:		return "Motorola MC68HC16 Microcontroller";
2404       /* 70 */
2405     case EM_68HC11:		return "Motorola MC68HC11 Microcontroller";
2406     case EM_68HC08:		return "Motorola MC68HC08 Microcontroller";
2407     case EM_68HC05:		return "Motorola MC68HC05 Microcontroller";
2408     case EM_SVX:		return "Silicon Graphics SVx";
2409     case EM_ST19:		return "STMicroelectronics ST19 8-bit microcontroller";
2410     case EM_VAX:		return "Digital VAX";
2411     case EM_CRIS:		return "Axis Communications 32-bit embedded processor";
2412     case EM_JAVELIN:		return "Infineon Technologies 32-bit embedded cpu";
2413     case EM_FIREPATH:		return "Element 14 64-bit DSP processor";
2414     case EM_ZSP:		return "LSI Logic's 16-bit DSP processor";
2415       /* 80 */
2416     case EM_MMIX:		return "Donald Knuth's educational 64-bit processor";
2417     case EM_HUANY:		return "Harvard Universitys's machine-independent object format";
2418     case EM_PRISM:		return "Vitesse Prism";
2419     case EM_AVR_OLD:
2420     case EM_AVR:		return "Atmel AVR 8-bit microcontroller";
2421     case EM_CYGNUS_FR30:
2422     case EM_FR30:		return "Fujitsu FR30";
2423     case EM_CYGNUS_D10V:
2424     case EM_D10V:		return "d10v";
2425     case EM_CYGNUS_D30V:
2426     case EM_D30V:		return "d30v";
2427     case EM_CYGNUS_V850:
2428     case EM_V850:		return "Renesas V850";
2429     case EM_CYGNUS_M32R:
2430     case EM_M32R:		return "Renesas M32R (formerly Mitsubishi M32r)";
2431     case EM_CYGNUS_MN10300:
2432     case EM_MN10300:		return "mn10300";
2433       /* 90 */
2434     case EM_CYGNUS_MN10200:
2435     case EM_MN10200:		return "mn10200";
2436     case EM_PJ:			return "picoJava";
2437     case EM_OR1K:		return "OpenRISC 1000";
2438     case EM_ARC_COMPACT:	return "ARCompact";
2439     case EM_XTENSA_OLD:
2440     case EM_XTENSA:		return "Tensilica Xtensa Processor";
2441     case EM_VIDEOCORE:		return "Alphamosaic VideoCore processor";
2442     case EM_TMM_GPP:		return "Thompson Multimedia General Purpose Processor";
2443     case EM_NS32K:		return "National Semiconductor 32000 series";
2444     case EM_TPC:		return "Tenor Network TPC processor";
2445     case EM_SNP1K:	        return "Trebia SNP 1000 processor";
2446       /* 100 */
2447     case EM_ST200:		return "STMicroelectronics ST200 microcontroller";
2448     case EM_IP2K_OLD:
2449     case EM_IP2K:		return "Ubicom IP2xxx 8-bit microcontrollers";
2450     case EM_MAX:		return "MAX Processor";
2451     case EM_CR:			return "National Semiconductor CompactRISC";
2452     case EM_F2MC16:		return "Fujitsu F2MC16";
2453     case EM_MSP430:		return "Texas Instruments msp430 microcontroller";
2454     case EM_BLACKFIN:		return "Analog Devices Blackfin";
2455     case EM_SE_C33:		return "S1C33 Family of Seiko Epson processors";
2456     case EM_SEP:		return "Sharp embedded microprocessor";
2457     case EM_ARCA:		return "Arca RISC microprocessor";
2458       /* 110 */
2459     case EM_UNICORE:		return "Unicore";
2460     case EM_EXCESS:		return "eXcess 16/32/64-bit configurable embedded CPU";
2461     case EM_DXP:		return "Icera Semiconductor Inc. Deep Execution Processor";
2462     case EM_ALTERA_NIOS2:	return "Altera Nios II";
2463     case EM_CRX:		return "National Semiconductor CRX microprocessor";
2464     case EM_XGATE:		return "Motorola XGATE embedded processor";
2465     case EM_C166:
2466     case EM_XC16X:		return "Infineon Technologies xc16x";
2467     case EM_M16C:		return "Renesas M16C series microprocessors";
2468     case EM_DSPIC30F:		return "Microchip Technology dsPIC30F Digital Signal Controller";
2469     case EM_CE:			return "Freescale Communication Engine RISC core";
2470       /* 120 */
2471     case EM_M32C:	        return "Renesas M32c";
2472       /* 130 */
2473     case EM_TSK3000:		return "Altium TSK3000 core";
2474     case EM_RS08:		return "Freescale RS08 embedded processor";
2475     case EM_ECOG2:		return "Cyan Technology eCOG2 microprocessor";
2476     case EM_SCORE:		return "SUNPLUS S+Core";
2477     case EM_DSP24:		return "New Japan Radio (NJR) 24-bit DSP Processor";
2478     case EM_VIDEOCORE3:		return "Broadcom VideoCore III processor";
2479     case EM_LATTICEMICO32:	return "Lattice Mico32";
2480     case EM_SE_C17:		return "Seiko Epson C17 family";
2481       /* 140 */
2482     case EM_TI_C6000:		return "Texas Instruments TMS320C6000 DSP family";
2483     case EM_TI_C2000:		return "Texas Instruments TMS320C2000 DSP family";
2484     case EM_TI_C5500:		return "Texas Instruments TMS320C55x DSP family";
2485     case EM_TI_PRU:		return "TI PRU I/O processor";
2486       /* 160 */
2487     case EM_MMDSP_PLUS:		return "STMicroelectronics 64bit VLIW Data Signal Processor";
2488     case EM_CYPRESS_M8C:	return "Cypress M8C microprocessor";
2489     case EM_R32C:		return "Renesas R32C series microprocessors";
2490     case EM_TRIMEDIA:		return "NXP Semiconductors TriMedia architecture family";
2491     case EM_QDSP6:		return "QUALCOMM DSP6 Processor";
2492     case EM_8051:		return "Intel 8051 and variants";
2493     case EM_STXP7X:		return "STMicroelectronics STxP7x family";
2494     case EM_NDS32:		return "Andes Technology compact code size embedded RISC processor family";
2495     case EM_ECOG1X:		return "Cyan Technology eCOG1X family";
2496     case EM_MAXQ30:		return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2497       /* 170 */
2498     case EM_XIMO16:		return "New Japan Radio (NJR) 16-bit DSP Processor";
2499     case EM_MANIK:		return "M2000 Reconfigurable RISC Microprocessor";
2500     case EM_CRAYNV2:		return "Cray Inc. NV2 vector architecture";
2501     case EM_RX:			return "Renesas RX";
2502     case EM_METAG:		return "Imagination Technologies Meta processor architecture";
2503     case EM_MCST_ELBRUS:	return "MCST Elbrus general purpose hardware architecture";
2504     case EM_ECOG16:		return "Cyan Technology eCOG16 family";
2505     case EM_CR16:
2506     case EM_MICROBLAZE:
2507     case EM_MICROBLAZE_OLD:	return "Xilinx MicroBlaze";
2508     case EM_ETPU:		return "Freescale Extended Time Processing Unit";
2509     case EM_SLE9X:		return "Infineon Technologies SLE9X core";
2510       /* 180 */
2511     case EM_L1OM:		return "Intel L1OM";
2512     case EM_K1OM:		return "Intel K1OM";
2513     case EM_INTEL182:		return "Intel (reserved)";
2514     case EM_AARCH64:		return "AArch64";
2515     case EM_ARM184:		return "ARM (reserved)";
2516     case EM_AVR32:		return "Atmel Corporation 32-bit microprocessor";
2517     case EM_STM8:		return "STMicroeletronics STM8 8-bit microcontroller";
2518     case EM_TILE64:		return "Tilera TILE64 multicore architecture family";
2519     case EM_TILEPRO:		return "Tilera TILEPro multicore architecture family";
2520       /* 190 */
2521     case EM_CUDA:		return "NVIDIA CUDA architecture";
2522     case EM_TILEGX:		return "Tilera TILE-Gx multicore architecture family";
2523     case EM_CLOUDSHIELD:	return "CloudShield architecture family";
2524     case EM_COREA_1ST:		return "KIPO-KAIST Core-A 1st generation processor family";
2525     case EM_COREA_2ND:		return "KIPO-KAIST Core-A 2nd generation processor family";
2526     case EM_ARC_COMPACT2:	return "ARCv2";
2527     case EM_OPEN8:		return "Open8 8-bit RISC soft processor core";
2528     case EM_RL78:		return "Renesas RL78";
2529     case EM_VIDEOCORE5:		return "Broadcom VideoCore V processor";
2530     case EM_78K0R:		return "Renesas 78K0R";
2531       /* 200 */
2532     case EM_56800EX:		return "Freescale 56800EX Digital Signal Controller (DSC)";
2533     case EM_BA1:		return "Beyond BA1 CPU architecture";
2534     case EM_BA2:		return "Beyond BA2 CPU architecture";
2535     case EM_XCORE:		return "XMOS xCORE processor family";
2536     case EM_MCHP_PIC:		return "Microchip 8-bit PIC(r) family";
2537       /* 210 */
2538     case EM_KM32:		return "KM211 KM32 32-bit processor";
2539     case EM_KMX32:		return "KM211 KMX32 32-bit processor";
2540     case EM_KMX16:		return "KM211 KMX16 16-bit processor";
2541     case EM_KMX8:		return "KM211 KMX8 8-bit processor";
2542     case EM_KVARC:		return "KM211 KVARC processor";
2543     case EM_CDP:		return "Paneve CDP architecture family";
2544     case EM_COGE:		return "Cognitive Smart Memory Processor";
2545     case EM_COOL:		return "Bluechip Systems CoolEngine";
2546     case EM_NORC:		return "Nanoradio Optimized RISC";
2547     case EM_CSR_KALIMBA:	return "CSR Kalimba architecture family";
2548       /* 220 */
2549     case EM_Z80:		return "Zilog Z80";
2550     case EM_VISIUM:		return "CDS VISIUMcore processor";
2551     case EM_FT32:               return "FTDI Chip FT32";
2552     case EM_MOXIE:              return "Moxie";
2553     case EM_AMDGPU: 	 	return "AMD GPU";
2554     case EM_RISCV: 	 	return "RISC-V";
2555     case EM_LANAI:		return "Lanai 32-bit processor";
2556     case EM_BPF:		return "Linux BPF";
2557     case EM_NFP:		return "Netronome Flow Processor";
2558 
2559       /* Large numbers...  */
2560     case EM_MT:                 return "Morpho Techologies MT processor";
2561     case EM_ALPHA:		return "Alpha";
2562     case EM_WEBASSEMBLY:	return "Web Assembly";
2563     case EM_DLX:		return "OpenDLX";
2564     case EM_XSTORMY16:		return "Sanyo XStormy16 CPU core";
2565     case EM_IQ2000:       	return "Vitesse IQ2000";
2566     case EM_M32C_OLD:
2567     case EM_NIOS32:		return "Altera Nios";
2568     case EM_CYGNUS_MEP:         return "Toshiba MeP Media Engine";
2569     case EM_ADAPTEVA_EPIPHANY:	return "Adapteva EPIPHANY";
2570     case EM_CYGNUS_FRV:		return "Fujitsu FR-V";
2571     case EM_S12Z:               return "Freescale S12Z";
2572     case EM_CSKY:		return "C-SKY";
2573 
2574     default:
2575       snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2576       return buff;
2577     }
2578 }
2579 
2580 static void
decode_ARC_machine_flags(unsigned e_flags,unsigned e_machine,char buf[])2581 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2582 {
2583   /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2.  Some
2584      other compilers don't a specific architecture type in the e_flags, and
2585      instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2586      architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2587      architectures.
2588 
2589      Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2590      but also sets a specific architecture type in the e_flags field.
2591 
2592      However, when decoding the flags we don't worry if we see an
2593      unexpected pairing, for example EM_ARC_COMPACT machine type, with
2594      ARCEM architecture type.  */
2595 
2596   switch (e_flags & EF_ARC_MACH_MSK)
2597     {
2598       /* We only expect these to occur for EM_ARC_COMPACT2.  */
2599     case EF_ARC_CPU_ARCV2EM:
2600       strcat (buf, ", ARC EM");
2601       break;
2602     case EF_ARC_CPU_ARCV2HS:
2603       strcat (buf, ", ARC HS");
2604       break;
2605 
2606       /* We only expect these to occur for EM_ARC_COMPACT.  */
2607     case E_ARC_MACH_ARC600:
2608       strcat (buf, ", ARC600");
2609       break;
2610     case E_ARC_MACH_ARC601:
2611       strcat (buf, ", ARC601");
2612       break;
2613     case E_ARC_MACH_ARC700:
2614       strcat (buf, ", ARC700");
2615       break;
2616 
2617       /* The only times we should end up here are (a) A corrupt ELF, (b) A
2618          new ELF with new architecture being read by an old version of
2619          readelf, or (c) An ELF built with non-GNU compiler that does not
2620          set the architecture in the e_flags.  */
2621     default:
2622       if (e_machine == EM_ARC_COMPACT)
2623         strcat (buf, ", Unknown ARCompact");
2624       else
2625         strcat (buf, ", Unknown ARC");
2626       break;
2627     }
2628 
2629   switch (e_flags & EF_ARC_OSABI_MSK)
2630     {
2631     case E_ARC_OSABI_ORIG:
2632       strcat (buf, ", (ABI:legacy)");
2633       break;
2634     case E_ARC_OSABI_V2:
2635       strcat (buf, ", (ABI:v2)");
2636       break;
2637       /* Only upstream 3.9+ kernels will support ARCv2 ISA.  */
2638     case E_ARC_OSABI_V3:
2639       strcat (buf, ", v3 no-legacy-syscalls ABI");
2640       break;
2641     case E_ARC_OSABI_V4:
2642       strcat (buf, ", v4 ABI");
2643       break;
2644     default:
2645       strcat (buf, ", unrecognised ARC OSABI flag");
2646       break;
2647     }
2648 }
2649 
2650 static void
decode_ARM_machine_flags(unsigned e_flags,char buf[])2651 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2652 {
2653   unsigned eabi;
2654   bfd_boolean unknown = FALSE;
2655 
2656   eabi = EF_ARM_EABI_VERSION (e_flags);
2657   e_flags &= ~ EF_ARM_EABIMASK;
2658 
2659   /* Handle "generic" ARM flags.  */
2660   if (e_flags & EF_ARM_RELEXEC)
2661     {
2662       strcat (buf, ", relocatable executable");
2663       e_flags &= ~ EF_ARM_RELEXEC;
2664     }
2665 
2666   if (e_flags & EF_ARM_PIC)
2667     {
2668       strcat (buf, ", position independent");
2669       e_flags &= ~ EF_ARM_PIC;
2670     }
2671 
2672   /* Now handle EABI specific flags.  */
2673   switch (eabi)
2674     {
2675     default:
2676       strcat (buf, ", <unrecognized EABI>");
2677       if (e_flags)
2678 	unknown = TRUE;
2679       break;
2680 
2681     case EF_ARM_EABI_VER1:
2682       strcat (buf, ", Version1 EABI");
2683       while (e_flags)
2684 	{
2685 	  unsigned flag;
2686 
2687 	  /* Process flags one bit at a time.  */
2688 	  flag = e_flags & - e_flags;
2689 	  e_flags &= ~ flag;
2690 
2691 	  switch (flag)
2692 	    {
2693 	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2694 	      strcat (buf, ", sorted symbol tables");
2695 	      break;
2696 
2697 	    default:
2698 	      unknown = TRUE;
2699 	      break;
2700 	    }
2701 	}
2702       break;
2703 
2704     case EF_ARM_EABI_VER2:
2705       strcat (buf, ", Version2 EABI");
2706       while (e_flags)
2707 	{
2708 	  unsigned flag;
2709 
2710 	  /* Process flags one bit at a time.  */
2711 	  flag = e_flags & - e_flags;
2712 	  e_flags &= ~ flag;
2713 
2714 	  switch (flag)
2715 	    {
2716 	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2717 	      strcat (buf, ", sorted symbol tables");
2718 	      break;
2719 
2720 	    case EF_ARM_DYNSYMSUSESEGIDX:
2721 	      strcat (buf, ", dynamic symbols use segment index");
2722 	      break;
2723 
2724 	    case EF_ARM_MAPSYMSFIRST:
2725 	      strcat (buf, ", mapping symbols precede others");
2726 	      break;
2727 
2728 	    default:
2729 	      unknown = TRUE;
2730 	      break;
2731 	    }
2732 	}
2733       break;
2734 
2735     case EF_ARM_EABI_VER3:
2736       strcat (buf, ", Version3 EABI");
2737       break;
2738 
2739     case EF_ARM_EABI_VER4:
2740       strcat (buf, ", Version4 EABI");
2741       while (e_flags)
2742 	{
2743 	  unsigned flag;
2744 
2745 	  /* Process flags one bit at a time.  */
2746 	  flag = e_flags & - e_flags;
2747 	  e_flags &= ~ flag;
2748 
2749 	  switch (flag)
2750 	    {
2751 	    case EF_ARM_BE8:
2752 	      strcat (buf, ", BE8");
2753 	      break;
2754 
2755 	    case EF_ARM_LE8:
2756 	      strcat (buf, ", LE8");
2757 	      break;
2758 
2759 	    default:
2760 	      unknown = TRUE;
2761 	      break;
2762 	    }
2763 	}
2764       break;
2765 
2766     case EF_ARM_EABI_VER5:
2767       strcat (buf, ", Version5 EABI");
2768       while (e_flags)
2769 	{
2770 	  unsigned flag;
2771 
2772 	  /* Process flags one bit at a time.  */
2773 	  flag = e_flags & - e_flags;
2774 	  e_flags &= ~ flag;
2775 
2776 	  switch (flag)
2777 	    {
2778 	    case EF_ARM_BE8:
2779 	      strcat (buf, ", BE8");
2780 	      break;
2781 
2782 	    case EF_ARM_LE8:
2783 	      strcat (buf, ", LE8");
2784 	      break;
2785 
2786 	    case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT.  */
2787 	      strcat (buf, ", soft-float ABI");
2788 	      break;
2789 
2790 	    case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT.  */
2791 	      strcat (buf, ", hard-float ABI");
2792 	      break;
2793 
2794 	    default:
2795 	      unknown = TRUE;
2796 	      break;
2797 	    }
2798 	}
2799       break;
2800 
2801     case EF_ARM_EABI_UNKNOWN:
2802       strcat (buf, ", GNU EABI");
2803       while (e_flags)
2804 	{
2805 	  unsigned flag;
2806 
2807 	  /* Process flags one bit at a time.  */
2808 	  flag = e_flags & - e_flags;
2809 	  e_flags &= ~ flag;
2810 
2811 	  switch (flag)
2812 	    {
2813 	    case EF_ARM_INTERWORK:
2814 	      strcat (buf, ", interworking enabled");
2815 	      break;
2816 
2817 	    case EF_ARM_APCS_26:
2818 	      strcat (buf, ", uses APCS/26");
2819 	      break;
2820 
2821 	    case EF_ARM_APCS_FLOAT:
2822 	      strcat (buf, ", uses APCS/float");
2823 	      break;
2824 
2825 	    case EF_ARM_PIC:
2826 	      strcat (buf, ", position independent");
2827 	      break;
2828 
2829 	    case EF_ARM_ALIGN8:
2830 	      strcat (buf, ", 8 bit structure alignment");
2831 	      break;
2832 
2833 	    case EF_ARM_NEW_ABI:
2834 	      strcat (buf, ", uses new ABI");
2835 	      break;
2836 
2837 	    case EF_ARM_OLD_ABI:
2838 	      strcat (buf, ", uses old ABI");
2839 	      break;
2840 
2841 	    case EF_ARM_SOFT_FLOAT:
2842 	      strcat (buf, ", software FP");
2843 	      break;
2844 
2845 	    case EF_ARM_VFP_FLOAT:
2846 	      strcat (buf, ", VFP");
2847 	      break;
2848 
2849 	    case EF_ARM_MAVERICK_FLOAT:
2850 	      strcat (buf, ", Maverick FP");
2851 	      break;
2852 
2853 	    default:
2854 	      unknown = TRUE;
2855 	      break;
2856 	    }
2857 	}
2858     }
2859 
2860   if (unknown)
2861     strcat (buf,_(", <unknown>"));
2862 }
2863 
2864 static void
decode_AVR_machine_flags(unsigned e_flags,char buf[],size_t size)2865 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
2866 {
2867   --size; /* Leave space for null terminator.  */
2868 
2869   switch (e_flags & EF_AVR_MACH)
2870     {
2871     case E_AVR_MACH_AVR1:
2872       strncat (buf, ", avr:1", size);
2873       break;
2874     case E_AVR_MACH_AVR2:
2875       strncat (buf, ", avr:2", size);
2876       break;
2877     case E_AVR_MACH_AVR25:
2878       strncat (buf, ", avr:25", size);
2879       break;
2880     case E_AVR_MACH_AVR3:
2881       strncat (buf, ", avr:3", size);
2882       break;
2883     case E_AVR_MACH_AVR31:
2884       strncat (buf, ", avr:31", size);
2885       break;
2886     case E_AVR_MACH_AVR35:
2887       strncat (buf, ", avr:35", size);
2888       break;
2889     case E_AVR_MACH_AVR4:
2890       strncat (buf, ", avr:4", size);
2891       break;
2892     case E_AVR_MACH_AVR5:
2893       strncat (buf, ", avr:5", size);
2894       break;
2895     case E_AVR_MACH_AVR51:
2896       strncat (buf, ", avr:51", size);
2897       break;
2898     case E_AVR_MACH_AVR6:
2899       strncat (buf, ", avr:6", size);
2900       break;
2901     case E_AVR_MACH_AVRTINY:
2902       strncat (buf, ", avr:100", size);
2903       break;
2904     case E_AVR_MACH_XMEGA1:
2905       strncat (buf, ", avr:101", size);
2906       break;
2907     case E_AVR_MACH_XMEGA2:
2908       strncat (buf, ", avr:102", size);
2909       break;
2910     case E_AVR_MACH_XMEGA3:
2911       strncat (buf, ", avr:103", size);
2912       break;
2913     case E_AVR_MACH_XMEGA4:
2914       strncat (buf, ", avr:104", size);
2915       break;
2916     case E_AVR_MACH_XMEGA5:
2917       strncat (buf, ", avr:105", size);
2918       break;
2919     case E_AVR_MACH_XMEGA6:
2920       strncat (buf, ", avr:106", size);
2921       break;
2922     case E_AVR_MACH_XMEGA7:
2923       strncat (buf, ", avr:107", size);
2924       break;
2925     default:
2926       strncat (buf, ", avr:<unknown>", size);
2927       break;
2928     }
2929 
2930   size -= strlen (buf);
2931   if (e_flags & EF_AVR_LINKRELAX_PREPARED)
2932     strncat (buf, ", link-relax", size);
2933 }
2934 
2935 static void
decode_NDS32_machine_flags(unsigned e_flags,char buf[],size_t size)2936 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2937 {
2938   unsigned abi;
2939   unsigned arch;
2940   unsigned config;
2941   unsigned version;
2942   bfd_boolean has_fpu = FALSE;
2943   unsigned int r = 0;
2944 
2945   static const char *ABI_STRINGS[] =
2946   {
2947     "ABI v0", /* use r5 as return register; only used in N1213HC */
2948     "ABI v1", /* use r0 as return register */
2949     "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2950     "ABI v2fp", /* for FPU */
2951     "AABI",
2952     "ABI2 FP+"
2953   };
2954   static const char *VER_STRINGS[] =
2955   {
2956     "Andes ELF V1.3 or older",
2957     "Andes ELF V1.3.1",
2958     "Andes ELF V1.4"
2959   };
2960   static const char *ARCH_STRINGS[] =
2961   {
2962     "",
2963     "Andes Star v1.0",
2964     "Andes Star v2.0",
2965     "Andes Star v3.0",
2966     "Andes Star v3.0m"
2967   };
2968 
2969   abi = EF_NDS_ABI & e_flags;
2970   arch = EF_NDS_ARCH & e_flags;
2971   config = EF_NDS_INST & e_flags;
2972   version = EF_NDS32_ELF_VERSION & e_flags;
2973 
2974   memset (buf, 0, size);
2975 
2976   switch (abi)
2977     {
2978     case E_NDS_ABI_V0:
2979     case E_NDS_ABI_V1:
2980     case E_NDS_ABI_V2:
2981     case E_NDS_ABI_V2FP:
2982     case E_NDS_ABI_AABI:
2983     case E_NDS_ABI_V2FP_PLUS:
2984       /* In case there are holes in the array.  */
2985       r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
2986       break;
2987 
2988     default:
2989       r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
2990       break;
2991     }
2992 
2993   switch (version)
2994     {
2995     case E_NDS32_ELF_VER_1_2:
2996     case E_NDS32_ELF_VER_1_3:
2997     case E_NDS32_ELF_VER_1_4:
2998       r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
2999       break;
3000 
3001     default:
3002       r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3003       break;
3004     }
3005 
3006   if (E_NDS_ABI_V0 == abi)
3007     {
3008       /* OLD ABI; only used in N1213HC, has performance extension 1.  */
3009       r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3010       if (arch == E_NDS_ARCH_STAR_V1_0)
3011 	r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3012       return;
3013     }
3014 
3015   switch (arch)
3016     {
3017     case E_NDS_ARCH_STAR_V1_0:
3018     case E_NDS_ARCH_STAR_V2_0:
3019     case E_NDS_ARCH_STAR_V3_0:
3020     case E_NDS_ARCH_STAR_V3_M:
3021       r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3022       break;
3023 
3024     default:
3025       r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3026       /* ARCH version determines how the e_flags are interpreted.
3027 	 If it is unknown, we cannot proceed.  */
3028       return;
3029     }
3030 
3031   /* Newer ABI; Now handle architecture specific flags.  */
3032   if (arch == E_NDS_ARCH_STAR_V1_0)
3033     {
3034       if (config & E_NDS32_HAS_MFUSR_PC_INST)
3035 	r += snprintf (buf + r, size -r, ", MFUSR_PC");
3036 
3037       if (!(config & E_NDS32_HAS_NO_MAC_INST))
3038 	r += snprintf (buf + r, size -r, ", MAC");
3039 
3040       if (config & E_NDS32_HAS_DIV_INST)
3041 	r += snprintf (buf + r, size -r, ", DIV");
3042 
3043       if (config & E_NDS32_HAS_16BIT_INST)
3044 	r += snprintf (buf + r, size -r, ", 16b");
3045     }
3046   else
3047     {
3048       if (config & E_NDS32_HAS_MFUSR_PC_INST)
3049 	{
3050 	  if (version <= E_NDS32_ELF_VER_1_3)
3051 	    r += snprintf (buf + r, size -r, ", [B8]");
3052 	  else
3053 	    r += snprintf (buf + r, size -r, ", EX9");
3054 	}
3055 
3056       if (config & E_NDS32_HAS_MAC_DX_INST)
3057 	r += snprintf (buf + r, size -r, ", MAC_DX");
3058 
3059       if (config & E_NDS32_HAS_DIV_DX_INST)
3060 	r += snprintf (buf + r, size -r, ", DIV_DX");
3061 
3062       if (config & E_NDS32_HAS_16BIT_INST)
3063 	{
3064 	  if (version <= E_NDS32_ELF_VER_1_3)
3065 	    r += snprintf (buf + r, size -r, ", 16b");
3066 	  else
3067 	    r += snprintf (buf + r, size -r, ", IFC");
3068 	}
3069     }
3070 
3071   if (config & E_NDS32_HAS_EXT_INST)
3072     r += snprintf (buf + r, size -r, ", PERF1");
3073 
3074   if (config & E_NDS32_HAS_EXT2_INST)
3075     r += snprintf (buf + r, size -r, ", PERF2");
3076 
3077   if (config & E_NDS32_HAS_FPU_INST)
3078     {
3079       has_fpu = TRUE;
3080       r += snprintf (buf + r, size -r, ", FPU_SP");
3081     }
3082 
3083   if (config & E_NDS32_HAS_FPU_DP_INST)
3084     {
3085       has_fpu = TRUE;
3086       r += snprintf (buf + r, size -r, ", FPU_DP");
3087     }
3088 
3089   if (config & E_NDS32_HAS_FPU_MAC_INST)
3090     {
3091       has_fpu = TRUE;
3092       r += snprintf (buf + r, size -r, ", FPU_MAC");
3093     }
3094 
3095   if (has_fpu)
3096     {
3097       switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3098 	{
3099 	case E_NDS32_FPU_REG_8SP_4DP:
3100 	  r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3101 	  break;
3102 	case E_NDS32_FPU_REG_16SP_8DP:
3103 	  r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3104 	  break;
3105 	case E_NDS32_FPU_REG_32SP_16DP:
3106 	  r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3107 	  break;
3108 	case E_NDS32_FPU_REG_32SP_32DP:
3109 	  r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3110 	  break;
3111 	}
3112     }
3113 
3114   if (config & E_NDS32_HAS_AUDIO_INST)
3115     r += snprintf (buf + r, size -r, ", AUDIO");
3116 
3117   if (config & E_NDS32_HAS_STRING_INST)
3118     r += snprintf (buf + r, size -r, ", STR");
3119 
3120   if (config & E_NDS32_HAS_REDUCED_REGS)
3121     r += snprintf (buf + r, size -r, ", 16REG");
3122 
3123   if (config & E_NDS32_HAS_VIDEO_INST)
3124     {
3125       if (version <= E_NDS32_ELF_VER_1_3)
3126 	r += snprintf (buf + r, size -r, ", VIDEO");
3127       else
3128 	r += snprintf (buf + r, size -r, ", SATURATION");
3129     }
3130 
3131   if (config & E_NDS32_HAS_ENCRIPT_INST)
3132     r += snprintf (buf + r, size -r, ", ENCRP");
3133 
3134   if (config & E_NDS32_HAS_L2C_INST)
3135     r += snprintf (buf + r, size -r, ", L2C");
3136 }
3137 
3138 static char *
get_machine_flags(Filedata * filedata,unsigned e_flags,unsigned e_machine)3139 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3140 {
3141   static char buf[1024];
3142 
3143   buf[0] = '\0';
3144 
3145   if (e_flags)
3146     {
3147       switch (e_machine)
3148 	{
3149 	default:
3150 	  break;
3151 
3152 	case EM_ARC_COMPACT2:
3153 	case EM_ARC_COMPACT:
3154           decode_ARC_machine_flags (e_flags, e_machine, buf);
3155           break;
3156 
3157 	case EM_ARM:
3158 	  decode_ARM_machine_flags (e_flags, buf);
3159 	  break;
3160 
3161         case EM_AVR:
3162           decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3163           break;
3164 
3165 	case EM_BLACKFIN:
3166 	  if (e_flags & EF_BFIN_PIC)
3167 	    strcat (buf, ", PIC");
3168 
3169 	  if (e_flags & EF_BFIN_FDPIC)
3170 	    strcat (buf, ", FDPIC");
3171 
3172 	  if (e_flags & EF_BFIN_CODE_IN_L1)
3173 	    strcat (buf, ", code in L1");
3174 
3175 	  if (e_flags & EF_BFIN_DATA_IN_L1)
3176 	    strcat (buf, ", data in L1");
3177 
3178 	  break;
3179 
3180 	case EM_CYGNUS_FRV:
3181 	  switch (e_flags & EF_FRV_CPU_MASK)
3182 	    {
3183 	    case EF_FRV_CPU_GENERIC:
3184 	      break;
3185 
3186 	    default:
3187 	      strcat (buf, ", fr???");
3188 	      break;
3189 
3190 	    case EF_FRV_CPU_FR300:
3191 	      strcat (buf, ", fr300");
3192 	      break;
3193 
3194 	    case EF_FRV_CPU_FR400:
3195 	      strcat (buf, ", fr400");
3196 	      break;
3197 	    case EF_FRV_CPU_FR405:
3198 	      strcat (buf, ", fr405");
3199 	      break;
3200 
3201 	    case EF_FRV_CPU_FR450:
3202 	      strcat (buf, ", fr450");
3203 	      break;
3204 
3205 	    case EF_FRV_CPU_FR500:
3206 	      strcat (buf, ", fr500");
3207 	      break;
3208 	    case EF_FRV_CPU_FR550:
3209 	      strcat (buf, ", fr550");
3210 	      break;
3211 
3212 	    case EF_FRV_CPU_SIMPLE:
3213 	      strcat (buf, ", simple");
3214 	      break;
3215 	    case EF_FRV_CPU_TOMCAT:
3216 	      strcat (buf, ", tomcat");
3217 	      break;
3218 	    }
3219 	  break;
3220 
3221 	case EM_68K:
3222 	  if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3223 	    strcat (buf, ", m68000");
3224 	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3225 	    strcat (buf, ", cpu32");
3226 	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3227 	    strcat (buf, ", fido_a");
3228 	  else
3229 	    {
3230 	      char const * isa = _("unknown");
3231 	      char const * mac = _("unknown mac");
3232 	      char const * additional = NULL;
3233 
3234 	      switch (e_flags & EF_M68K_CF_ISA_MASK)
3235 		{
3236 		case EF_M68K_CF_ISA_A_NODIV:
3237 		  isa = "A";
3238 		  additional = ", nodiv";
3239 		  break;
3240 		case EF_M68K_CF_ISA_A:
3241 		  isa = "A";
3242 		  break;
3243 		case EF_M68K_CF_ISA_A_PLUS:
3244 		  isa = "A+";
3245 		  break;
3246 		case EF_M68K_CF_ISA_B_NOUSP:
3247 		  isa = "B";
3248 		  additional = ", nousp";
3249 		  break;
3250 		case EF_M68K_CF_ISA_B:
3251 		  isa = "B";
3252 		  break;
3253 		case EF_M68K_CF_ISA_C:
3254 		  isa = "C";
3255 		  break;
3256 		case EF_M68K_CF_ISA_C_NODIV:
3257 		  isa = "C";
3258 		  additional = ", nodiv";
3259 		  break;
3260 		}
3261 	      strcat (buf, ", cf, isa ");
3262 	      strcat (buf, isa);
3263 	      if (additional)
3264 		strcat (buf, additional);
3265 	      if (e_flags & EF_M68K_CF_FLOAT)
3266 		strcat (buf, ", float");
3267 	      switch (e_flags & EF_M68K_CF_MAC_MASK)
3268 		{
3269 		case 0:
3270 		  mac = NULL;
3271 		  break;
3272 		case EF_M68K_CF_MAC:
3273 		  mac = "mac";
3274 		  break;
3275 		case EF_M68K_CF_EMAC:
3276 		  mac = "emac";
3277 		  break;
3278 		case EF_M68K_CF_EMAC_B:
3279 		  mac = "emac_b";
3280 		  break;
3281 		}
3282 	      if (mac)
3283 		{
3284 		  strcat (buf, ", ");
3285 		  strcat (buf, mac);
3286 		}
3287 	    }
3288 	  break;
3289 
3290 	case EM_CYGNUS_MEP:
3291 	  switch (e_flags & EF_MEP_CPU_MASK)
3292 	    {
3293 	    case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3294 	    case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3295 	    case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3296 	    case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3297 	    case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3298 	    case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3299 	    default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3300 	    }
3301 
3302 	  switch (e_flags & EF_MEP_COP_MASK)
3303 	    {
3304 	    case EF_MEP_COP_NONE: break;
3305 	    case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3306 	    case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3307 	    case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3308 	    case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3309 	    default: strcat (buf, _("<unknown MeP copro type>")); break;
3310 	    }
3311 
3312 	  if (e_flags & EF_MEP_LIBRARY)
3313 	    strcat (buf, ", Built for Library");
3314 
3315 	  if (e_flags & EF_MEP_INDEX_MASK)
3316 	    sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3317 		     e_flags & EF_MEP_INDEX_MASK);
3318 
3319 	  if (e_flags & ~ EF_MEP_ALL_FLAGS)
3320 	    sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3321 		     e_flags & ~ EF_MEP_ALL_FLAGS);
3322 	  break;
3323 
3324 	case EM_PPC:
3325 	  if (e_flags & EF_PPC_EMB)
3326 	    strcat (buf, ", emb");
3327 
3328 	  if (e_flags & EF_PPC_RELOCATABLE)
3329 	    strcat (buf, _(", relocatable"));
3330 
3331 	  if (e_flags & EF_PPC_RELOCATABLE_LIB)
3332 	    strcat (buf, _(", relocatable-lib"));
3333 	  break;
3334 
3335 	case EM_PPC64:
3336 	  if (e_flags & EF_PPC64_ABI)
3337 	    {
3338 	      char abi[] = ", abiv0";
3339 
3340 	      abi[6] += e_flags & EF_PPC64_ABI;
3341 	      strcat (buf, abi);
3342 	    }
3343 	  break;
3344 
3345 	case EM_V800:
3346 	  if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3347 	    strcat (buf, ", RH850 ABI");
3348 
3349 	  if (e_flags & EF_V800_850E3)
3350 	    strcat (buf, ", V3 architecture");
3351 
3352 	  if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3353 	    strcat (buf, ", FPU not used");
3354 
3355 	  if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3356 	    strcat (buf, ", regmode: COMMON");
3357 
3358 	  if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3359 	    strcat (buf, ", r4 not used");
3360 
3361 	  if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3362 	    strcat (buf, ", r30 not used");
3363 
3364 	  if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3365 	    strcat (buf, ", r5 not used");
3366 
3367 	  if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3368 	    strcat (buf, ", r2 not used");
3369 
3370 	  for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3371 	    {
3372 	      switch (e_flags & - e_flags)
3373 		{
3374 		case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3375 		case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3376 		case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3377 		case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3378 		case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3379 		case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3380 		case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3381 		case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3382 		case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3383 		case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3384 		case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3385 		case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3386 		default: break;
3387 		}
3388 	    }
3389 	  break;
3390 
3391 	case EM_V850:
3392 	case EM_CYGNUS_V850:
3393 	  switch (e_flags & EF_V850_ARCH)
3394 	    {
3395 	    case E_V850E3V5_ARCH:
3396 	      strcat (buf, ", v850e3v5");
3397 	      break;
3398 	    case E_V850E2V3_ARCH:
3399 	      strcat (buf, ", v850e2v3");
3400 	      break;
3401 	    case E_V850E2_ARCH:
3402 	      strcat (buf, ", v850e2");
3403 	      break;
3404             case E_V850E1_ARCH:
3405               strcat (buf, ", v850e1");
3406 	      break;
3407 	    case E_V850E_ARCH:
3408 	      strcat (buf, ", v850e");
3409 	      break;
3410 	    case E_V850_ARCH:
3411 	      strcat (buf, ", v850");
3412 	      break;
3413 	    default:
3414 	      strcat (buf, _(", unknown v850 architecture variant"));
3415 	      break;
3416 	    }
3417 	  break;
3418 
3419 	case EM_M32R:
3420 	case EM_CYGNUS_M32R:
3421 	  if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3422 	    strcat (buf, ", m32r");
3423 	  break;
3424 
3425 	case EM_MIPS:
3426 	case EM_MIPS_RS3_LE:
3427 	  if (e_flags & EF_MIPS_NOREORDER)
3428 	    strcat (buf, ", noreorder");
3429 
3430 	  if (e_flags & EF_MIPS_PIC)
3431 	    strcat (buf, ", pic");
3432 
3433 	  if (e_flags & EF_MIPS_CPIC)
3434 	    strcat (buf, ", cpic");
3435 
3436 	  if (e_flags & EF_MIPS_UCODE)
3437 	    strcat (buf, ", ugen_reserved");
3438 
3439 	  if (e_flags & EF_MIPS_ABI2)
3440 	    strcat (buf, ", abi2");
3441 
3442 	  if (e_flags & EF_MIPS_OPTIONS_FIRST)
3443 	    strcat (buf, ", odk first");
3444 
3445 	  if (e_flags & EF_MIPS_32BITMODE)
3446 	    strcat (buf, ", 32bitmode");
3447 
3448 	  if (e_flags & EF_MIPS_NAN2008)
3449 	    strcat (buf, ", nan2008");
3450 
3451 	  if (e_flags & EF_MIPS_FP64)
3452 	    strcat (buf, ", fp64");
3453 
3454 	  switch ((e_flags & EF_MIPS_MACH))
3455 	    {
3456 	    case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3457 	    case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3458 	    case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3459 	    case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3460 	    case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3461 	    case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3462 	    case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3463 	    case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3464 	    case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3465 	    case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
3466 	    case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3467   	    case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3468   	    case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3469 	    case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
3470 	    case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
3471 	    case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
3472 	    case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3473 	    case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3474 	    case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3475 	    case E_MIPS_MACH_XLR:  strcat (buf, ", xlr"); break;
3476 	    case E_MIPS_MACH_IAMR2:  strcat (buf, ", interaptiv-mr2"); break;
3477 	    case 0:
3478 	    /* We simply ignore the field in this case to avoid confusion:
3479 	       MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3480 	       extension.  */
3481 	      break;
3482 	    default: strcat (buf, _(", unknown CPU")); break;
3483 	    }
3484 
3485 	  switch ((e_flags & EF_MIPS_ABI))
3486 	    {
3487 	    case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3488 	    case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3489 	    case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3490 	    case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3491 	    case 0:
3492 	    /* We simply ignore the field in this case to avoid confusion:
3493 	       MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3494 	       This means it is likely to be an o32 file, but not for
3495 	       sure.  */
3496 	      break;
3497 	    default: strcat (buf, _(", unknown ABI")); break;
3498 	    }
3499 
3500 	  if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3501 	    strcat (buf, ", mdmx");
3502 
3503 	  if (e_flags & EF_MIPS_ARCH_ASE_M16)
3504 	    strcat (buf, ", mips16");
3505 
3506 	  if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3507 	    strcat (buf, ", micromips");
3508 
3509 	  switch ((e_flags & EF_MIPS_ARCH))
3510 	    {
3511 	    case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3512 	    case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3513 	    case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3514 	    case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3515 	    case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3516 	    case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3517 	    case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3518 	    case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3519 	    case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3520 	    case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3521 	    case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3522 	    default: strcat (buf, _(", unknown ISA")); break;
3523 	    }
3524 	  break;
3525 
3526 	case EM_NDS32:
3527 	  decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3528 	  break;
3529 
3530 	case EM_NFP:
3531 	  switch (EF_NFP_MACH (e_flags))
3532 	    {
3533 	    case E_NFP_MACH_3200:
3534 	      strcat (buf, ", NFP-32xx");
3535 	      break;
3536 	    case E_NFP_MACH_6000:
3537 	      strcat (buf, ", NFP-6xxx");
3538 	      break;
3539 	    }
3540 	  break;
3541 
3542 	case EM_RISCV:
3543 	  if (e_flags & EF_RISCV_RVC)
3544 	    strcat (buf, ", RVC");
3545 
3546 	  if (e_flags & EF_RISCV_RVE)
3547 	    strcat (buf, ", RVE");
3548 
3549 	  switch (e_flags & EF_RISCV_FLOAT_ABI)
3550 	    {
3551 	    case EF_RISCV_FLOAT_ABI_SOFT:
3552 	      strcat (buf, ", soft-float ABI");
3553 	      break;
3554 
3555 	    case EF_RISCV_FLOAT_ABI_SINGLE:
3556 	      strcat (buf, ", single-float ABI");
3557 	      break;
3558 
3559 	    case EF_RISCV_FLOAT_ABI_DOUBLE:
3560 	      strcat (buf, ", double-float ABI");
3561 	      break;
3562 
3563 	    case EF_RISCV_FLOAT_ABI_QUAD:
3564 	      strcat (buf, ", quad-float ABI");
3565 	      break;
3566 	    }
3567 	  break;
3568 
3569 	case EM_SH:
3570 	  switch ((e_flags & EF_SH_MACH_MASK))
3571 	    {
3572 	    case EF_SH1: strcat (buf, ", sh1"); break;
3573 	    case EF_SH2: strcat (buf, ", sh2"); break;
3574 	    case EF_SH3: strcat (buf, ", sh3"); break;
3575 	    case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3576 	    case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3577 	    case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3578 	    case EF_SH3E: strcat (buf, ", sh3e"); break;
3579 	    case EF_SH4: strcat (buf, ", sh4"); break;
3580 	    case EF_SH5: strcat (buf, ", sh5"); break;
3581 	    case EF_SH2E: strcat (buf, ", sh2e"); break;
3582 	    case EF_SH4A: strcat (buf, ", sh4a"); break;
3583 	    case EF_SH2A: strcat (buf, ", sh2a"); break;
3584 	    case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3585 	    case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3586 	    case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3587 	    case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3588 	    case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3589 	    case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3590 	    case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3591 	    case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3592 	    case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3593 	    default: strcat (buf, _(", unknown ISA")); break;
3594 	    }
3595 
3596 	  if (e_flags & EF_SH_PIC)
3597 	    strcat (buf, ", pic");
3598 
3599 	  if (e_flags & EF_SH_FDPIC)
3600 	    strcat (buf, ", fdpic");
3601 	  break;
3602 
3603         case EM_OR1K:
3604           if (e_flags & EF_OR1K_NODELAY)
3605             strcat (buf, ", no delay");
3606           break;
3607 
3608 	case EM_SPARCV9:
3609 	  if (e_flags & EF_SPARC_32PLUS)
3610 	    strcat (buf, ", v8+");
3611 
3612 	  if (e_flags & EF_SPARC_SUN_US1)
3613 	    strcat (buf, ", ultrasparcI");
3614 
3615 	  if (e_flags & EF_SPARC_SUN_US3)
3616 	    strcat (buf, ", ultrasparcIII");
3617 
3618 	  if (e_flags & EF_SPARC_HAL_R1)
3619 	    strcat (buf, ", halr1");
3620 
3621 	  if (e_flags & EF_SPARC_LEDATA)
3622 	    strcat (buf, ", ledata");
3623 
3624 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3625 	    strcat (buf, ", tso");
3626 
3627 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3628 	    strcat (buf, ", pso");
3629 
3630 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3631 	    strcat (buf, ", rmo");
3632 	  break;
3633 
3634 	case EM_PARISC:
3635 	  switch (e_flags & EF_PARISC_ARCH)
3636 	    {
3637 	    case EFA_PARISC_1_0:
3638 	      strcpy (buf, ", PA-RISC 1.0");
3639 	      break;
3640 	    case EFA_PARISC_1_1:
3641 	      strcpy (buf, ", PA-RISC 1.1");
3642 	      break;
3643 	    case EFA_PARISC_2_0:
3644 	      strcpy (buf, ", PA-RISC 2.0");
3645 	      break;
3646 	    default:
3647 	      break;
3648 	    }
3649 	  if (e_flags & EF_PARISC_TRAPNIL)
3650 	    strcat (buf, ", trapnil");
3651 	  if (e_flags & EF_PARISC_EXT)
3652 	    strcat (buf, ", ext");
3653 	  if (e_flags & EF_PARISC_LSB)
3654 	    strcat (buf, ", lsb");
3655 	  if (e_flags & EF_PARISC_WIDE)
3656 	    strcat (buf, ", wide");
3657 	  if (e_flags & EF_PARISC_NO_KABP)
3658 	    strcat (buf, ", no kabp");
3659 	  if (e_flags & EF_PARISC_LAZYSWAP)
3660 	    strcat (buf, ", lazyswap");
3661 	  break;
3662 
3663 	case EM_PJ:
3664 	case EM_PJ_OLD:
3665 	  if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3666 	    strcat (buf, ", new calling convention");
3667 
3668 	  if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3669 	    strcat (buf, ", gnu calling convention");
3670 	  break;
3671 
3672 	case EM_IA_64:
3673 	  if ((e_flags & EF_IA_64_ABI64))
3674 	    strcat (buf, ", 64-bit");
3675 	  else
3676 	    strcat (buf, ", 32-bit");
3677 	  if ((e_flags & EF_IA_64_REDUCEDFP))
3678 	    strcat (buf, ", reduced fp model");
3679 	  if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3680 	    strcat (buf, ", no function descriptors, constant gp");
3681 	  else if ((e_flags & EF_IA_64_CONS_GP))
3682 	    strcat (buf, ", constant gp");
3683 	  if ((e_flags & EF_IA_64_ABSOLUTE))
3684 	    strcat (buf, ", absolute");
3685           if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3686             {
3687               if ((e_flags & EF_IA_64_VMS_LINKAGES))
3688                 strcat (buf, ", vms_linkages");
3689               switch ((e_flags & EF_IA_64_VMS_COMCOD))
3690                 {
3691                 case EF_IA_64_VMS_COMCOD_SUCCESS:
3692                   break;
3693                 case EF_IA_64_VMS_COMCOD_WARNING:
3694                   strcat (buf, ", warning");
3695                   break;
3696                 case EF_IA_64_VMS_COMCOD_ERROR:
3697                   strcat (buf, ", error");
3698                   break;
3699                 case EF_IA_64_VMS_COMCOD_ABORT:
3700                   strcat (buf, ", abort");
3701                   break;
3702                 default:
3703 		  warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3704 			e_flags & EF_IA_64_VMS_COMCOD);
3705 		  strcat (buf, ", <unknown>");
3706                 }
3707             }
3708 	  break;
3709 
3710 	case EM_VAX:
3711 	  if ((e_flags & EF_VAX_NONPIC))
3712 	    strcat (buf, ", non-PIC");
3713 	  if ((e_flags & EF_VAX_DFLOAT))
3714 	    strcat (buf, ", D-Float");
3715 	  if ((e_flags & EF_VAX_GFLOAT))
3716 	    strcat (buf, ", G-Float");
3717 	  break;
3718 
3719         case EM_VISIUM:
3720 	  if (e_flags & EF_VISIUM_ARCH_MCM)
3721 	    strcat (buf, ", mcm");
3722 	  else if (e_flags & EF_VISIUM_ARCH_MCM24)
3723 	    strcat (buf, ", mcm24");
3724 	  if (e_flags & EF_VISIUM_ARCH_GR6)
3725 	    strcat (buf, ", gr6");
3726 	  break;
3727 
3728 	case EM_RL78:
3729 	  switch (e_flags & E_FLAG_RL78_CPU_MASK)
3730 	    {
3731 	    case E_FLAG_RL78_ANY_CPU: break;
3732 	    case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3733 	    case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3734 	    case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3735 	    }
3736 	  if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3737 	    strcat (buf, ", 64-bit doubles");
3738 	  break;
3739 
3740 	case EM_RX:
3741 	  if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3742 	    strcat (buf, ", 64-bit doubles");
3743 	  if (e_flags & E_FLAG_RX_DSP)
3744 	    strcat (buf, ", dsp");
3745 	  if (e_flags & E_FLAG_RX_PID)
3746 	    strcat (buf, ", pid");
3747 	  if (e_flags & E_FLAG_RX_ABI)
3748 	    strcat (buf, ", RX ABI");
3749 	  if (e_flags & E_FLAG_RX_SINSNS_SET)
3750 	    strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3751 		    ? ", uses String instructions" : ", bans String instructions");
3752 	  if (e_flags & E_FLAG_RX_V2)
3753 	    strcat (buf, ", V2");
3754 	  if (e_flags & E_FLAG_RX_V3)
3755 	    strcat (buf, ", V3");
3756 	  break;
3757 
3758 	case EM_S390:
3759 	  if (e_flags & EF_S390_HIGH_GPRS)
3760 	    strcat (buf, ", highgprs");
3761 	  break;
3762 
3763 	case EM_TI_C6000:
3764 	  if ((e_flags & EF_C6000_REL))
3765 	    strcat (buf, ", relocatable module");
3766 	  break;
3767 
3768 	case EM_MSP430:
3769 	  strcat (buf, _(": architecture variant: "));
3770 	  switch (e_flags & EF_MSP430_MACH)
3771 	    {
3772 	    case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3773 	    case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3774 	    case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3775 	    case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3776 	    case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3777 	    case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3778 	    case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3779 	    case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3780 	    case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3781 	    case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3782 	    case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3783 	    case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3784 	    case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3785 	    case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3786 	    case E_MSP430_MACH_MSP430X  : strcat (buf, "MSP430X"); break;
3787 	    default:
3788 	      strcat (buf, _(": unknown")); break;
3789 	    }
3790 
3791 	  if (e_flags & ~ EF_MSP430_MACH)
3792 	    strcat (buf, _(": unknown extra flag bits also present"));
3793 	  break;
3794 
3795 	case EM_Z80:
3796 	  switch (e_flags & EF_Z80_MACH_MSK)
3797 	    {
3798 	    case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
3799 	    case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
3800 	    case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
3801 	    case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
3802 	    case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
3803 	    case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
3804 	    case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
3805 	    default:
3806 	      strcat (buf, _(", unknown")); break;
3807 	    }
3808 	  break;
3809 	}
3810     }
3811 
3812   return buf;
3813 }
3814 
3815 static const char *
get_osabi_name(Filedata * filedata,unsigned int osabi)3816 get_osabi_name (Filedata * filedata, unsigned int osabi)
3817 {
3818   static char buff[32];
3819 
3820   switch (osabi)
3821     {
3822     case ELFOSABI_NONE:		return "UNIX - System V";
3823     case ELFOSABI_HPUX:		return "UNIX - HP-UX";
3824     case ELFOSABI_NETBSD:	return "UNIX - NetBSD";
3825     case ELFOSABI_GNU:		return "UNIX - GNU";
3826     case ELFOSABI_SOLARIS:	return "UNIX - Solaris";
3827     case ELFOSABI_AIX:		return "UNIX - AIX";
3828     case ELFOSABI_IRIX:		return "UNIX - IRIX";
3829     case ELFOSABI_FREEBSD:	return "UNIX - FreeBSD";
3830     case ELFOSABI_TRU64:	return "UNIX - TRU64";
3831     case ELFOSABI_MODESTO:	return "Novell - Modesto";
3832     case ELFOSABI_OPENBSD:	return "UNIX - OpenBSD";
3833     case ELFOSABI_OPENVMS:	return "VMS - OpenVMS";
3834     case ELFOSABI_NSK:		return "HP - Non-Stop Kernel";
3835     case ELFOSABI_AROS:		return "AROS";
3836     case ELFOSABI_FENIXOS:	return "FenixOS";
3837     case ELFOSABI_CLOUDABI:	return "Nuxi CloudABI";
3838     case ELFOSABI_OPENVOS:	return "Stratus Technologies OpenVOS";
3839     default:
3840       if (osabi >= 64)
3841 	switch (filedata->file_header.e_machine)
3842 	  {
3843 	  case EM_ARM:
3844 	    switch (osabi)
3845 	      {
3846 	      case ELFOSABI_ARM:	return "ARM";
3847 	      case ELFOSABI_ARM_FDPIC:	return "ARM FDPIC";
3848 	      default:
3849 		break;
3850 	      }
3851 	    break;
3852 
3853 	  case EM_MSP430:
3854 	  case EM_MSP430_OLD:
3855 	  case EM_VISIUM:
3856 	    switch (osabi)
3857 	      {
3858 	      case ELFOSABI_STANDALONE:	return _("Standalone App");
3859 	      default:
3860 		break;
3861 	      }
3862 	    break;
3863 
3864 	  case EM_TI_C6000:
3865 	    switch (osabi)
3866 	      {
3867 	      case ELFOSABI_C6000_ELFABI:	return _("Bare-metal C6000");
3868 	      case ELFOSABI_C6000_LINUX:	return "Linux C6000";
3869 	      default:
3870 		break;
3871 	      }
3872 	    break;
3873 
3874 	  default:
3875 	    break;
3876 	  }
3877       snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3878       return buff;
3879     }
3880 }
3881 
3882 static const char *
get_aarch64_segment_type(unsigned long type)3883 get_aarch64_segment_type (unsigned long type)
3884 {
3885   switch (type)
3886     {
3887     case PT_AARCH64_ARCHEXT:  return "AARCH64_ARCHEXT";
3888     default:                  return NULL;
3889     }
3890 }
3891 
3892 static const char *
get_arm_segment_type(unsigned long type)3893 get_arm_segment_type (unsigned long type)
3894 {
3895   switch (type)
3896     {
3897     case PT_ARM_EXIDX: return "EXIDX";
3898     default:           return NULL;
3899     }
3900 }
3901 
3902 static const char *
get_s390_segment_type(unsigned long type)3903 get_s390_segment_type (unsigned long type)
3904 {
3905   switch (type)
3906     {
3907     case PT_S390_PGSTE: return "S390_PGSTE";
3908     default:            return NULL;
3909     }
3910 }
3911 
3912 static const char *
get_mips_segment_type(unsigned long type)3913 get_mips_segment_type (unsigned long type)
3914 {
3915   switch (type)
3916     {
3917     case PT_MIPS_REGINFO:   return "REGINFO";
3918     case PT_MIPS_RTPROC:    return "RTPROC";
3919     case PT_MIPS_OPTIONS:   return "OPTIONS";
3920     case PT_MIPS_ABIFLAGS:  return "ABIFLAGS";
3921     default:                return NULL;
3922     }
3923 }
3924 
3925 static const char *
get_parisc_segment_type(unsigned long type)3926 get_parisc_segment_type (unsigned long type)
3927 {
3928   switch (type)
3929     {
3930     case PT_PARISC_ARCHEXT:	return "PARISC_ARCHEXT";
3931     case PT_PARISC_UNWIND:	return "PARISC_UNWIND";
3932     case PT_PARISC_WEAKORDER:	return "PARISC_WEAKORDER";
3933     default:                    return NULL;
3934     }
3935 }
3936 
3937 static const char *
get_ia64_segment_type(unsigned long type)3938 get_ia64_segment_type (unsigned long type)
3939 {
3940   switch (type)
3941     {
3942     case PT_IA_64_ARCHEXT:	return "IA_64_ARCHEXT";
3943     case PT_IA_64_UNWIND:	return "IA_64_UNWIND";
3944     default:                    return NULL;
3945     }
3946 }
3947 
3948 static const char *
get_tic6x_segment_type(unsigned long type)3949 get_tic6x_segment_type (unsigned long type)
3950 {
3951   switch (type)
3952     {
3953     case PT_C6000_PHATTR:  return "C6000_PHATTR";
3954     default:               return NULL;
3955     }
3956 }
3957 
3958 static const char *
get_hpux_segment_type(unsigned long type,unsigned e_machine)3959 get_hpux_segment_type (unsigned long type, unsigned e_machine)
3960 {
3961   if (e_machine == EM_PARISC)
3962     switch (type)
3963       {
3964       case PT_HP_TLS:		return "HP_TLS";
3965       case PT_HP_CORE_NONE:	return "HP_CORE_NONE";
3966       case PT_HP_CORE_VERSION:	return "HP_CORE_VERSION";
3967       case PT_HP_CORE_KERNEL:	return "HP_CORE_KERNEL";
3968       case PT_HP_CORE_COMM:	return "HP_CORE_COMM";
3969       case PT_HP_CORE_PROC:	return "HP_CORE_PROC";
3970       case PT_HP_CORE_LOADABLE:	return "HP_CORE_LOADABLE";
3971       case PT_HP_CORE_STACK:	return "HP_CORE_STACK";
3972       case PT_HP_CORE_SHM:	return "HP_CORE_SHM";
3973       case PT_HP_CORE_MMF:	return "HP_CORE_MMF";
3974       case PT_HP_PARALLEL:	return "HP_PARALLEL";
3975       case PT_HP_FASTBIND:	return "HP_FASTBIND";
3976       case PT_HP_OPT_ANNOT:	return "HP_OPT_ANNOT";
3977       case PT_HP_HSL_ANNOT:	return "HP_HSL_ANNOT";
3978       case PT_HP_STACK:		return "HP_STACK";
3979       case PT_HP_CORE_UTSNAME:	return "HP_CORE_UTSNAME";
3980       default:			return NULL;
3981       }
3982 
3983   if (e_machine == EM_IA_64)
3984     switch (type)
3985       {
3986       case PT_HP_TLS:		 return "HP_TLS";
3987       case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
3988       case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
3989       case PT_IA_64_HP_STACK:	 return "HP_STACK";
3990       default:			 return NULL;
3991       }
3992 
3993   return NULL;
3994 }
3995 
3996 static const char *
get_solaris_segment_type(unsigned long type)3997 get_solaris_segment_type (unsigned long type)
3998 {
3999   switch (type)
4000     {
4001     case 0x6464e550: return "PT_SUNW_UNWIND";
4002     case 0x6474e550: return "PT_SUNW_EH_FRAME";
4003     case 0x6ffffff7: return "PT_LOSUNW";
4004     case 0x6ffffffa: return "PT_SUNWBSS";
4005     case 0x6ffffffb: return "PT_SUNWSTACK";
4006     case 0x6ffffffc: return "PT_SUNWDTRACE";
4007     case 0x6ffffffd: return "PT_SUNWCAP";
4008     case 0x6fffffff: return "PT_HISUNW";
4009     default:         return NULL;
4010     }
4011 }
4012 
4013 static const char *
get_segment_type(Filedata * filedata,unsigned long p_type)4014 get_segment_type (Filedata * filedata, unsigned long p_type)
4015 {
4016   static char buff[32];
4017 
4018   switch (p_type)
4019     {
4020     case PT_NULL:	return "NULL";
4021     case PT_LOAD:	return "LOAD";
4022     case PT_DYNAMIC:	return "DYNAMIC";
4023     case PT_INTERP:	return "INTERP";
4024     case PT_NOTE:	return "NOTE";
4025     case PT_SHLIB:	return "SHLIB";
4026     case PT_PHDR:	return "PHDR";
4027     case PT_TLS:	return "TLS";
4028     case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4029     case PT_GNU_STACK:	return "GNU_STACK";
4030     case PT_GNU_RELRO:  return "GNU_RELRO";
4031     case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4032 
4033     case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4034     case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4035     case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4036 
4037     default:
4038       if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4039 	{
4040 	  const char * result;
4041 
4042 	  switch (filedata->file_header.e_machine)
4043 	    {
4044 	    case EM_AARCH64:
4045 	      result = get_aarch64_segment_type (p_type);
4046 	      break;
4047 	    case EM_ARM:
4048 	      result = get_arm_segment_type (p_type);
4049 	      break;
4050 	    case EM_MIPS:
4051 	    case EM_MIPS_RS3_LE:
4052 	      result = get_mips_segment_type (p_type);
4053 	      break;
4054 	    case EM_PARISC:
4055 	      result = get_parisc_segment_type (p_type);
4056 	      break;
4057 	    case EM_IA_64:
4058 	      result = get_ia64_segment_type (p_type);
4059 	      break;
4060 	    case EM_TI_C6000:
4061 	      result = get_tic6x_segment_type (p_type);
4062 	      break;
4063 	    case EM_S390:
4064 	    case EM_S390_OLD:
4065 	      result = get_s390_segment_type (p_type);
4066 	      break;
4067 	    default:
4068 	      result = NULL;
4069 	      break;
4070 	    }
4071 
4072 	  if (result != NULL)
4073 	    return result;
4074 
4075 	  sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4076 	}
4077       else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4078 	{
4079 	  const char * result = NULL;
4080 
4081 	  switch (filedata->file_header.e_ident[EI_OSABI])
4082 	    {
4083 	    case ELFOSABI_GNU:
4084 	    case ELFOSABI_FREEBSD:
4085 	      if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4086 		{
4087 		  sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4088 		  result = buff;
4089 		}
4090 	      break;
4091 	    case ELFOSABI_HPUX:
4092 	      result = get_hpux_segment_type (p_type,
4093 					      filedata->file_header.e_machine);
4094 	      break;
4095 	    case ELFOSABI_SOLARIS:
4096 	      result = get_solaris_segment_type (p_type);
4097 	      break;
4098 	    default:
4099 	      break;
4100 	    }
4101 	  if (result != NULL)
4102 	    return result;
4103 
4104 	  sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4105 	}
4106       else
4107 	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4108 
4109       return buff;
4110     }
4111 }
4112 
4113 static const char *
get_arc_section_type_name(unsigned int sh_type)4114 get_arc_section_type_name (unsigned int sh_type)
4115 {
4116   switch (sh_type)
4117     {
4118     case SHT_ARC_ATTRIBUTES:      return "ARC_ATTRIBUTES";
4119     default:
4120       break;
4121     }
4122   return NULL;
4123 }
4124 
4125 static const char *
get_mips_section_type_name(unsigned int sh_type)4126 get_mips_section_type_name (unsigned int sh_type)
4127 {
4128   switch (sh_type)
4129     {
4130     case SHT_MIPS_LIBLIST:	 return "MIPS_LIBLIST";
4131     case SHT_MIPS_MSYM:		 return "MIPS_MSYM";
4132     case SHT_MIPS_CONFLICT:	 return "MIPS_CONFLICT";
4133     case SHT_MIPS_GPTAB:	 return "MIPS_GPTAB";
4134     case SHT_MIPS_UCODE:	 return "MIPS_UCODE";
4135     case SHT_MIPS_DEBUG:	 return "MIPS_DEBUG";
4136     case SHT_MIPS_REGINFO:	 return "MIPS_REGINFO";
4137     case SHT_MIPS_PACKAGE:	 return "MIPS_PACKAGE";
4138     case SHT_MIPS_PACKSYM:	 return "MIPS_PACKSYM";
4139     case SHT_MIPS_RELD:		 return "MIPS_RELD";
4140     case SHT_MIPS_IFACE:	 return "MIPS_IFACE";
4141     case SHT_MIPS_CONTENT:	 return "MIPS_CONTENT";
4142     case SHT_MIPS_OPTIONS:	 return "MIPS_OPTIONS";
4143     case SHT_MIPS_SHDR:		 return "MIPS_SHDR";
4144     case SHT_MIPS_FDESC:	 return "MIPS_FDESC";
4145     case SHT_MIPS_EXTSYM:	 return "MIPS_EXTSYM";
4146     case SHT_MIPS_DENSE:	 return "MIPS_DENSE";
4147     case SHT_MIPS_PDESC:	 return "MIPS_PDESC";
4148     case SHT_MIPS_LOCSYM:	 return "MIPS_LOCSYM";
4149     case SHT_MIPS_AUXSYM:	 return "MIPS_AUXSYM";
4150     case SHT_MIPS_OPTSYM:	 return "MIPS_OPTSYM";
4151     case SHT_MIPS_LOCSTR:	 return "MIPS_LOCSTR";
4152     case SHT_MIPS_LINE:		 return "MIPS_LINE";
4153     case SHT_MIPS_RFDESC:	 return "MIPS_RFDESC";
4154     case SHT_MIPS_DELTASYM:	 return "MIPS_DELTASYM";
4155     case SHT_MIPS_DELTAINST:	 return "MIPS_DELTAINST";
4156     case SHT_MIPS_DELTACLASS:	 return "MIPS_DELTACLASS";
4157     case SHT_MIPS_DWARF:	 return "MIPS_DWARF";
4158     case SHT_MIPS_DELTADECL:	 return "MIPS_DELTADECL";
4159     case SHT_MIPS_SYMBOL_LIB:	 return "MIPS_SYMBOL_LIB";
4160     case SHT_MIPS_EVENTS:	 return "MIPS_EVENTS";
4161     case SHT_MIPS_TRANSLATE:	 return "MIPS_TRANSLATE";
4162     case SHT_MIPS_PIXIE:	 return "MIPS_PIXIE";
4163     case SHT_MIPS_XLATE:	 return "MIPS_XLATE";
4164     case SHT_MIPS_XLATE_DEBUG:	 return "MIPS_XLATE_DEBUG";
4165     case SHT_MIPS_WHIRL:	 return "MIPS_WHIRL";
4166     case SHT_MIPS_EH_REGION:	 return "MIPS_EH_REGION";
4167     case SHT_MIPS_XLATE_OLD:	 return "MIPS_XLATE_OLD";
4168     case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4169     case SHT_MIPS_ABIFLAGS:	 return "MIPS_ABIFLAGS";
4170     case SHT_MIPS_XHASH:	 return "MIPS_XHASH";
4171     default:
4172       break;
4173     }
4174   return NULL;
4175 }
4176 
4177 static const char *
get_parisc_section_type_name(unsigned int sh_type)4178 get_parisc_section_type_name (unsigned int sh_type)
4179 {
4180   switch (sh_type)
4181     {
4182     case SHT_PARISC_EXT:	return "PARISC_EXT";
4183     case SHT_PARISC_UNWIND:	return "PARISC_UNWIND";
4184     case SHT_PARISC_DOC:	return "PARISC_DOC";
4185     case SHT_PARISC_ANNOT:	return "PARISC_ANNOT";
4186     case SHT_PARISC_SYMEXTN:	return "PARISC_SYMEXTN";
4187     case SHT_PARISC_STUBS:	return "PARISC_STUBS";
4188     case SHT_PARISC_DLKM:	return "PARISC_DLKM";
4189     default:             	return NULL;
4190     }
4191 }
4192 
4193 static const char *
get_ia64_section_type_name(Filedata * filedata,unsigned int sh_type)4194 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4195 {
4196   /* If the top 8 bits are 0x78 the next 8 are the os/abi ID.  */
4197   if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4198     return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4199 
4200   switch (sh_type)
4201     {
4202     case SHT_IA_64_EXT:		       return "IA_64_EXT";
4203     case SHT_IA_64_UNWIND:	       return "IA_64_UNWIND";
4204     case SHT_IA_64_PRIORITY_INIT:      return "IA_64_PRIORITY_INIT";
4205     case SHT_IA_64_VMS_TRACE:          return "VMS_TRACE";
4206     case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4207     case SHT_IA_64_VMS_DEBUG:          return "VMS_DEBUG";
4208     case SHT_IA_64_VMS_DEBUG_STR:      return "VMS_DEBUG_STR";
4209     case SHT_IA_64_VMS_LINKAGES:       return "VMS_LINKAGES";
4210     case SHT_IA_64_VMS_SYMBOL_VECTOR:  return "VMS_SYMBOL_VECTOR";
4211     case SHT_IA_64_VMS_FIXUP:          return "VMS_FIXUP";
4212     default:
4213       break;
4214     }
4215   return NULL;
4216 }
4217 
4218 static const char *
get_x86_64_section_type_name(unsigned int sh_type)4219 get_x86_64_section_type_name (unsigned int sh_type)
4220 {
4221   switch (sh_type)
4222     {
4223     case SHT_X86_64_UNWIND:	return "X86_64_UNWIND";
4224     default:			return NULL;
4225     }
4226 }
4227 
4228 static const char *
get_aarch64_section_type_name(unsigned int sh_type)4229 get_aarch64_section_type_name (unsigned int sh_type)
4230 {
4231   switch (sh_type)
4232     {
4233     case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4234     default:			 return NULL;
4235     }
4236 }
4237 
4238 static const char *
get_arm_section_type_name(unsigned int sh_type)4239 get_arm_section_type_name (unsigned int sh_type)
4240 {
4241   switch (sh_type)
4242     {
4243     case SHT_ARM_EXIDX:           return "ARM_EXIDX";
4244     case SHT_ARM_PREEMPTMAP:      return "ARM_PREEMPTMAP";
4245     case SHT_ARM_ATTRIBUTES:      return "ARM_ATTRIBUTES";
4246     case SHT_ARM_DEBUGOVERLAY:    return "ARM_DEBUGOVERLAY";
4247     case SHT_ARM_OVERLAYSECTION:  return "ARM_OVERLAYSECTION";
4248     default:			  return NULL;
4249     }
4250 }
4251 
4252 static const char *
get_tic6x_section_type_name(unsigned int sh_type)4253 get_tic6x_section_type_name (unsigned int sh_type)
4254 {
4255   switch (sh_type)
4256     {
4257     case SHT_C6000_UNWIND:      return "C6000_UNWIND";
4258     case SHT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
4259     case SHT_C6000_ATTRIBUTES:  return "C6000_ATTRIBUTES";
4260     case SHT_TI_ICODE:          return "TI_ICODE";
4261     case SHT_TI_XREF:           return "TI_XREF";
4262     case SHT_TI_HANDLER:        return "TI_HANDLER";
4263     case SHT_TI_INITINFO:       return "TI_INITINFO";
4264     case SHT_TI_PHATTRS:        return "TI_PHATTRS";
4265     default:                    return NULL;
4266     }
4267 }
4268 
4269 static const char *
get_msp430_section_type_name(unsigned int sh_type)4270 get_msp430_section_type_name (unsigned int sh_type)
4271 {
4272   switch (sh_type)
4273     {
4274     case SHT_MSP430_SEC_FLAGS:    return "MSP430_SEC_FLAGS";
4275     case SHT_MSP430_SYM_ALIASES:  return "MSP430_SYM_ALIASES";
4276     case SHT_MSP430_ATTRIBUTES:   return "MSP430_ATTRIBUTES";
4277     default:                      return NULL;
4278     }
4279 }
4280 
4281 static const char *
get_nfp_section_type_name(unsigned int sh_type)4282 get_nfp_section_type_name (unsigned int sh_type)
4283 {
4284   switch (sh_type)
4285     {
4286     case SHT_NFP_MECONFIG:	return "NFP_MECONFIG";
4287     case SHT_NFP_INITREG:	return "NFP_INITREG";
4288     case SHT_NFP_UDEBUG:	return "NFP_UDEBUG";
4289     default:			return NULL;
4290     }
4291 }
4292 
4293 static const char *
get_v850_section_type_name(unsigned int sh_type)4294 get_v850_section_type_name (unsigned int sh_type)
4295 {
4296   switch (sh_type)
4297     {
4298     case SHT_V850_SCOMMON:  return "V850 Small Common";
4299     case SHT_V850_TCOMMON:  return "V850 Tiny Common";
4300     case SHT_V850_ZCOMMON:  return "V850 Zero Common";
4301     case SHT_RENESAS_IOP:   return "RENESAS IOP";
4302     case SHT_RENESAS_INFO:  return "RENESAS INFO";
4303     default:                return NULL;
4304     }
4305 }
4306 
4307 static const char *
get_riscv_section_type_name(unsigned int sh_type)4308 get_riscv_section_type_name (unsigned int sh_type)
4309 {
4310   switch (sh_type)
4311     {
4312     case SHT_RISCV_ATTRIBUTES:  return "RISCV_ATTRIBUTES";
4313     default: return NULL;
4314     }
4315 }
4316 
4317 static const char *
get_csky_section_type_name(unsigned int sh_type)4318 get_csky_section_type_name (unsigned int sh_type)
4319 {
4320   switch (sh_type)
4321     {
4322     case SHT_CSKY_ATTRIBUTES:  return "CSKY_ATTRIBUTES";
4323     default:  return NULL;
4324     }
4325 }
4326 
4327 static const char *
get_section_type_name(Filedata * filedata,unsigned int sh_type)4328 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4329 {
4330   static char buff[32];
4331   const char * result;
4332 
4333   switch (sh_type)
4334     {
4335     case SHT_NULL:		return "NULL";
4336     case SHT_PROGBITS:		return "PROGBITS";
4337     case SHT_SYMTAB:		return "SYMTAB";
4338     case SHT_STRTAB:		return "STRTAB";
4339     case SHT_RELA:		return "RELA";
4340     case SHT_HASH:		return "HASH";
4341     case SHT_DYNAMIC:		return "DYNAMIC";
4342     case SHT_NOTE:		return "NOTE";
4343     case SHT_NOBITS:		return "NOBITS";
4344     case SHT_REL:		return "REL";
4345     case SHT_SHLIB:		return "SHLIB";
4346     case SHT_DYNSYM:		return "DYNSYM";
4347     case SHT_INIT_ARRAY:	return "INIT_ARRAY";
4348     case SHT_FINI_ARRAY:	return "FINI_ARRAY";
4349     case SHT_PREINIT_ARRAY:	return "PREINIT_ARRAY";
4350     case SHT_GNU_HASH:		return "GNU_HASH";
4351     case SHT_GROUP:		return "GROUP";
4352     case SHT_SYMTAB_SHNDX:	return "SYMTAB SECTION INDICES";
4353     case SHT_GNU_verdef:	return "VERDEF";
4354     case SHT_GNU_verneed:	return "VERNEED";
4355     case SHT_GNU_versym:	return "VERSYM";
4356     case 0x6ffffff0:		return "VERSYM";
4357     case 0x6ffffffc:		return "VERDEF";
4358     case 0x7ffffffd:		return "AUXILIARY";
4359     case 0x7fffffff:		return "FILTER";
4360     case SHT_GNU_LIBLIST:	return "GNU_LIBLIST";
4361 
4362     default:
4363       if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4364 	{
4365 	  switch (filedata->file_header.e_machine)
4366 	    {
4367 	    case EM_ARC:
4368 	    case EM_ARC_COMPACT:
4369 	    case EM_ARC_COMPACT2:
4370 	      result = get_arc_section_type_name (sh_type);
4371 	      break;
4372 	    case EM_MIPS:
4373 	    case EM_MIPS_RS3_LE:
4374 	      result = get_mips_section_type_name (sh_type);
4375 	      break;
4376 	    case EM_PARISC:
4377 	      result = get_parisc_section_type_name (sh_type);
4378 	      break;
4379 	    case EM_IA_64:
4380 	      result = get_ia64_section_type_name (filedata, sh_type);
4381 	      break;
4382 	    case EM_X86_64:
4383 	    case EM_L1OM:
4384 	    case EM_K1OM:
4385 	      result = get_x86_64_section_type_name (sh_type);
4386 	      break;
4387 	    case EM_AARCH64:
4388 	      result = get_aarch64_section_type_name (sh_type);
4389 	      break;
4390 	    case EM_ARM:
4391 	      result = get_arm_section_type_name (sh_type);
4392 	      break;
4393 	    case EM_TI_C6000:
4394 	      result = get_tic6x_section_type_name (sh_type);
4395 	      break;
4396 	    case EM_MSP430:
4397 	      result = get_msp430_section_type_name (sh_type);
4398 	      break;
4399 	    case EM_NFP:
4400 	      result = get_nfp_section_type_name (sh_type);
4401 	      break;
4402 	    case EM_V800:
4403 	    case EM_V850:
4404 	    case EM_CYGNUS_V850:
4405 	      result = get_v850_section_type_name (sh_type);
4406 	      break;
4407 	    case EM_RISCV:
4408 	      result = get_riscv_section_type_name (sh_type);
4409 	      break;
4410 	    case EM_CSKY:
4411 	      result = get_csky_section_type_name (sh_type);
4412 	      break;
4413 	    default:
4414 	      result = NULL;
4415 	      break;
4416 	    }
4417 
4418 	  if (result != NULL)
4419 	    return result;
4420 
4421 	  sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4422 	}
4423       else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4424 	{
4425 	  switch (filedata->file_header.e_machine)
4426 	    {
4427 	    case EM_IA_64:
4428 	      result = get_ia64_section_type_name (filedata, sh_type);
4429 	      break;
4430 	    default:
4431 	      if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4432 		result = get_solaris_section_type (sh_type);
4433 	      else
4434 		{
4435 		  switch (sh_type)
4436 		    {
4437 		    case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4438 		    case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4439 		    case SHT_GNU_HASH: result = "GNU_HASH"; break;
4440 		    case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4441 		    default:
4442 		      result = NULL;
4443 		      break;
4444 		    }
4445 		}
4446 	      break;
4447 	    }
4448 
4449 	  if (result != NULL)
4450 	    return result;
4451 
4452 	  sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4453 	}
4454       else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4455 	{
4456 	  switch (filedata->file_header.e_machine)
4457 	    {
4458 	    case EM_V800:
4459 	    case EM_V850:
4460 	    case EM_CYGNUS_V850:
4461 	      result = get_v850_section_type_name (sh_type);
4462 	      break;
4463 	    default:
4464 	      result = NULL;
4465 	      break;
4466 	    }
4467 
4468 	  if (result != NULL)
4469 	    return result;
4470 
4471 	  sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4472 	}
4473       else
4474 	/* This message is probably going to be displayed in a 15
4475 	   character wide field, so put the hex value first.  */
4476 	snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4477 
4478       return buff;
4479     }
4480 }
4481 
4482 enum long_option_values
4483 {
4484   OPTION_DEBUG_DUMP = 512,
4485   OPTION_DYN_SYMS,
4486   OPTION_DWARF_DEPTH,
4487   OPTION_DWARF_START,
4488   OPTION_DWARF_CHECK,
4489   OPTION_CTF_DUMP,
4490   OPTION_CTF_PARENT,
4491   OPTION_CTF_SYMBOLS,
4492   OPTION_CTF_STRINGS,
4493   OPTION_WITH_SYMBOL_VERSIONS,
4494   OPTION_RECURSE_LIMIT,
4495   OPTION_NO_RECURSE_LIMIT,
4496   OPTION_NO_DEMANGLING
4497 };
4498 
4499 static struct option options[] =
4500 {
4501  /* Note - This table is alpha-sorted on the 'val'
4502     field in order to make adding new options easier.  */
4503   {"arch-specific",    no_argument, 0, 'A'},
4504   {"all",	       no_argument, 0, 'a'},
4505   {"demangle",         optional_argument, 0, 'C'},
4506   {"archive-index",    no_argument, 0, 'c'},
4507   {"use-dynamic",      no_argument, 0, 'D'},
4508   {"dynamic",	       no_argument, 0, 'd'},
4509   {"headers",	       no_argument, 0, 'e'},
4510   {"section-groups",   no_argument, 0, 'g'},
4511   {"help",	       no_argument, 0, 'H'},
4512   {"file-header",      no_argument, 0, 'h'},
4513   {"histogram",	       no_argument, 0, 'I'},
4514   {"lint",             no_argument, 0, 'L'},
4515   {"enable-checks",    no_argument, 0, 'L'},
4516   {"program-headers",  no_argument, 0, 'l'},
4517   {"segments",	       no_argument, 0, 'l'},
4518   {"full-section-name",no_argument, 0, 'N'},
4519   {"notes",	       no_argument, 0, 'n'},
4520   {"string-dump",      required_argument, 0, 'p'},
4521   {"relocated-dump",   required_argument, 0, 'R'},
4522   {"relocs",	       no_argument, 0, 'r'},
4523   {"section-headers",  no_argument, 0, 'S'},
4524   {"sections",	       no_argument, 0, 'S'},
4525   {"symbols",	       no_argument, 0, 's'},
4526   {"syms",	       no_argument, 0, 's'},
4527   {"silent-truncation",no_argument, 0, 'T'},
4528   {"section-details",  no_argument, 0, 't'},
4529   {"unwind",	       no_argument, 0, 'u'},
4530   {"version-info",     no_argument, 0, 'V'},
4531   {"version",	       no_argument, 0, 'v'},
4532   {"wide",	       no_argument, 0, 'W'},
4533   {"hex-dump",	       required_argument, 0, 'x'},
4534   {"decompress",       no_argument, 0, 'z'},
4535 
4536   {"no-demangle",      no_argument, 0, OPTION_NO_DEMANGLING},
4537   {"recurse-limit",    no_argument, NULL, OPTION_RECURSE_LIMIT},
4538   {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4539   {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4540   {"dyn-syms",	       no_argument, 0, OPTION_DYN_SYMS},
4541   {"debug-dump",       optional_argument, 0, OPTION_DEBUG_DUMP},
4542   {"dwarf-depth",      required_argument, 0, OPTION_DWARF_DEPTH},
4543   {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
4544   {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
4545 #ifdef ENABLE_LIBCTF
4546   {"ctf",	       required_argument, 0, OPTION_CTF_DUMP},
4547   {"ctf-symbols",      required_argument, 0, OPTION_CTF_SYMBOLS},
4548   {"ctf-strings",      required_argument, 0, OPTION_CTF_STRINGS},
4549   {"ctf-parent",       required_argument, 0, OPTION_CTF_PARENT},
4550 #endif
4551 
4552   {0,		       no_argument, 0, 0}
4553 };
4554 
4555 static void
usage(FILE * stream)4556 usage (FILE * stream)
4557 {
4558   fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4559   fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4560   fprintf (stream, _(" Options are:\n\
4561   -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n\
4562   -h --file-header       Display the ELF file header\n\
4563   -l --program-headers   Display the program headers\n\
4564      --segments          An alias for --program-headers\n\
4565   -S --section-headers   Display the sections' header\n\
4566      --sections          An alias for --section-headers\n\
4567   -g --section-groups    Display the section groups\n\
4568   -t --section-details   Display the section details\n\
4569   -e --headers           Equivalent to: -h -l -S\n\
4570   -s --syms              Display the symbol table\n\
4571      --symbols           An alias for --syms\n\
4572      --dyn-syms          Display the dynamic symbol table\n\
4573   -C --demangle[=STYLE]  Decode low-level symbol names into user-level names\n\
4574                           The STYLE, if specified, can be `auto' (the default),\n\
4575                           `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
4576                           or `gnat'\n\
4577      --no-demangle       Do not demangle low-level symbol names.  (This is the default)\n\
4578      --recurse-limit     Enable a demangling recursion limit.  (This is the default)\n\
4579      --no-recurse-limit  Disable a demangling recursion limit\n\
4580   -n --notes             Display the core notes (if present)\n\
4581   -r --relocs            Display the relocations (if present)\n\
4582   -u --unwind            Display the unwind info (if present)\n\
4583   -d --dynamic           Display the dynamic section (if present)\n\
4584   -V --version-info      Display the version sections (if present)\n\
4585   -A --arch-specific     Display architecture specific information (if any)\n\
4586   -c --archive-index     Display the symbol/file index in an archive\n\
4587   -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
4588   -L --lint|--enable-checks  Display warning messages for possible problems\n\
4589   -x --hex-dump=<number|name>\n\
4590                          Dump the contents of section <number|name> as bytes\n\
4591   -p --string-dump=<number|name>\n\
4592                          Dump the contents of section <number|name> as strings\n\
4593   -R --relocated-dump=<number|name>\n\
4594                          Dump the contents of section <number|name> as relocated bytes\n\
4595   -z --decompress        Decompress section before dumping it\n\
4596   -w[lLiaprmfFsoORtUuTgAckK] or\n\
4597   --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
4598                =frames-interp,=str,=str-offsets,=loc,=Ranges,=pubtypes,\n\
4599                =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
4600                =addr,=cu_index,=links,=follow-links]\n\
4601                          Display the contents of DWARF debug sections\n"));
4602   fprintf (stream, _("\
4603   --dwarf-depth=N        Do not display DIEs at depth N or greater\n\
4604   --dwarf-start=N        Display DIEs starting with N, at the same depth\n\
4605                          or deeper\n"));
4606 #ifdef ENABLE_LIBCTF
4607   fprintf (stream, _("\
4608   --ctf=<number|name>    Display CTF info from section <number|name>\n\
4609   --ctf-parent=<number|name>\n\
4610                          Use section <number|name> as the CTF parent\n\n\
4611   --ctf-symbols=<number|name>\n\
4612                          Use section <number|name> as the CTF external symtab\n\n\
4613   --ctf-strings=<number|name>\n\
4614                          Use section <number|name> as the CTF external strtab\n\n"));
4615 #endif
4616 
4617 #ifdef SUPPORT_DISASSEMBLY
4618   fprintf (stream, _("\
4619   -i --instruction-dump=<number|name>\n\
4620                          Disassemble the contents of section <number|name>\n"));
4621 #endif
4622   fprintf (stream, _("\
4623   -I --histogram         Display histogram of bucket list lengths\n\
4624   -W --wide              Allow output width to exceed 80 characters\n\
4625   -T --silent-truncation If a symbol name is truncated, do not add a suffix [...]\n\
4626   @<file>                Read options from <file>\n\
4627   -H --help              Display this information\n\
4628   -v --version           Display the version number of readelf\n"));
4629 
4630   if (REPORT_BUGS_TO[0] && stream == stdout)
4631     fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4632 
4633   exit (stream == stdout ? 0 : 1);
4634 }
4635 
4636 /* Record the fact that the user wants the contents of section number
4637    SECTION to be displayed using the method(s) encoded as flags bits
4638    in TYPE.  Note, TYPE can be zero if we are creating the array for
4639    the first time.  */
4640 
4641 static void
request_dump_bynumber(struct dump_data * dumpdata,unsigned int section,dump_type type)4642 request_dump_bynumber (struct dump_data *dumpdata,
4643 		       unsigned int section, dump_type type)
4644 {
4645   if (section >= dumpdata->num_dump_sects)
4646     {
4647       dump_type * new_dump_sects;
4648 
4649       new_dump_sects = (dump_type *) calloc (section + 1,
4650                                              sizeof (* new_dump_sects));
4651 
4652       if (new_dump_sects == NULL)
4653 	error (_("Out of memory allocating dump request table.\n"));
4654       else
4655 	{
4656 	  if (dumpdata->dump_sects)
4657 	    {
4658 	      /* Copy current flag settings.  */
4659 	      memcpy (new_dump_sects, dumpdata->dump_sects,
4660 		      dumpdata->num_dump_sects * sizeof (* new_dump_sects));
4661 
4662 	      free (dumpdata->dump_sects);
4663 	    }
4664 
4665 	  dumpdata->dump_sects = new_dump_sects;
4666 	  dumpdata->num_dump_sects = section + 1;
4667 	}
4668     }
4669 
4670   if (dumpdata->dump_sects)
4671     dumpdata->dump_sects[section] |= type;
4672 }
4673 
4674 /* Request a dump by section name.  */
4675 
4676 static void
request_dump_byname(const char * section,dump_type type)4677 request_dump_byname (const char * section, dump_type type)
4678 {
4679   struct dump_list_entry * new_request;
4680 
4681   new_request = (struct dump_list_entry *)
4682       malloc (sizeof (struct dump_list_entry));
4683   if (!new_request)
4684     error (_("Out of memory allocating dump request table.\n"));
4685 
4686   new_request->name = strdup (section);
4687   if (!new_request->name)
4688     error (_("Out of memory allocating dump request table.\n"));
4689 
4690   new_request->type = type;
4691 
4692   new_request->next = dump_sects_byname;
4693   dump_sects_byname = new_request;
4694 }
4695 
4696 static inline void
request_dump(struct dump_data * dumpdata,dump_type type)4697 request_dump (struct dump_data *dumpdata, dump_type type)
4698 {
4699   int section;
4700   char * cp;
4701 
4702   do_dump++;
4703   section = strtoul (optarg, & cp, 0);
4704 
4705   if (! *cp && section >= 0)
4706     request_dump_bynumber (dumpdata, section, type);
4707   else
4708     request_dump_byname (optarg, type);
4709 }
4710 
4711 static void
parse_args(struct dump_data * dumpdata,int argc,char ** argv)4712 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
4713 {
4714   int c;
4715 
4716   if (argc < 2)
4717     usage (stderr);
4718 
4719   while ((c = getopt_long
4720 	  (argc, argv, "ACDHILNR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4721     {
4722       switch (c)
4723 	{
4724 	case 0:
4725 	  /* Long options.  */
4726 	  break;
4727 	case 'H':
4728 	  usage (stdout);
4729 	  break;
4730 
4731 	case 'a':
4732 	  do_syms = TRUE;
4733 	  do_reloc = TRUE;
4734 	  do_unwind = TRUE;
4735 	  do_dynamic = TRUE;
4736 	  do_header = TRUE;
4737 	  do_sections = TRUE;
4738 	  do_section_groups = TRUE;
4739 	  do_segments = TRUE;
4740 	  do_version = TRUE;
4741 	  do_histogram = TRUE;
4742 	  do_arch = TRUE;
4743 	  do_notes = TRUE;
4744 	  break;
4745 
4746 	case 'g':
4747 	  do_section_groups = TRUE;
4748 	  break;
4749 	case 't':
4750 	case 'N':
4751 	  do_sections = TRUE;
4752 	  do_section_details = TRUE;
4753 	  break;
4754 	case 'e':
4755 	  do_header = TRUE;
4756 	  do_sections = TRUE;
4757 	  do_segments = TRUE;
4758 	  break;
4759 	case 'A':
4760 	  do_arch = TRUE;
4761 	  break;
4762 	case 'D':
4763 	  do_using_dynamic = TRUE;
4764 	  break;
4765 	case 'r':
4766 	  do_reloc = TRUE;
4767 	  break;
4768 	case 'u':
4769 	  do_unwind = TRUE;
4770 	  break;
4771 	case 'h':
4772 	  do_header = TRUE;
4773 	  break;
4774 	case 'l':
4775 	  do_segments = TRUE;
4776 	  break;
4777 	case 's':
4778 	  do_syms = TRUE;
4779 	  break;
4780 	case 'S':
4781 	  do_sections = TRUE;
4782 	  break;
4783 	case 'd':
4784 	  do_dynamic = TRUE;
4785 	  break;
4786 	case 'I':
4787 	  do_histogram = TRUE;
4788 	  break;
4789 	case 'n':
4790 	  do_notes = TRUE;
4791 	  break;
4792 	case 'c':
4793 	  do_archive_index = TRUE;
4794 	  break;
4795 	case 'L':
4796 	  do_checks = TRUE;
4797 	  break;
4798 	case 'x':
4799 	  request_dump (dumpdata, HEX_DUMP);
4800 	  break;
4801 	case 'p':
4802 	  request_dump (dumpdata, STRING_DUMP);
4803 	  break;
4804 	case 'R':
4805 	  request_dump (dumpdata, RELOC_DUMP);
4806 	  break;
4807 	case 'z':
4808 	  decompress_dumps = TRUE;
4809 	  break;
4810 	case 'w':
4811 	  do_dump = TRUE;
4812 	  if (optarg == 0)
4813 	    {
4814 	      do_debugging = TRUE;
4815 	      dwarf_select_sections_all ();
4816 	    }
4817 	  else
4818 	    {
4819 	      do_debugging = FALSE;
4820 	      dwarf_select_sections_by_letters (optarg);
4821 	    }
4822 	  break;
4823 	case OPTION_DEBUG_DUMP:
4824 	  do_dump = TRUE;
4825 	  if (optarg == 0)
4826 	    do_debugging = TRUE;
4827 	  else
4828 	    {
4829 	      do_debugging = FALSE;
4830 	      dwarf_select_sections_by_names (optarg);
4831 	    }
4832 	  break;
4833 	case OPTION_DWARF_DEPTH:
4834 	  {
4835 	    char *cp;
4836 
4837 	    dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4838 	  }
4839 	  break;
4840 	case OPTION_DWARF_START:
4841 	  {
4842 	    char *cp;
4843 
4844 	    dwarf_start_die = strtoul (optarg, & cp, 0);
4845 	  }
4846 	  break;
4847 	case OPTION_DWARF_CHECK:
4848 	  dwarf_check = TRUE;
4849 	  break;
4850 	case OPTION_CTF_DUMP:
4851 	  do_ctf = TRUE;
4852 	  request_dump (dumpdata, CTF_DUMP);
4853 	  break;
4854 	case OPTION_CTF_SYMBOLS:
4855 	  free (dump_ctf_symtab_name);
4856 	  dump_ctf_symtab_name = strdup (optarg);
4857 	  break;
4858 	case OPTION_CTF_STRINGS:
4859 	  free (dump_ctf_strtab_name);
4860 	  dump_ctf_strtab_name = strdup (optarg);
4861 	  break;
4862 	case OPTION_CTF_PARENT:
4863 	  free (dump_ctf_parent_name);
4864 	  dump_ctf_parent_name = strdup (optarg);
4865 	  break;
4866 	case OPTION_DYN_SYMS:
4867 	  do_dyn_syms = TRUE;
4868 	  break;
4869 #ifdef SUPPORT_DISASSEMBLY
4870 	case 'i':
4871 	  request_dump (dumpdata, DISASS_DUMP);
4872 	  break;
4873 #endif
4874 	case 'v':
4875 	  print_version (program_name);
4876 	  break;
4877 	case 'V':
4878 	  do_version = TRUE;
4879 	  break;
4880 	case 'W':
4881 	  do_wide = TRUE;
4882 	  break;
4883 	case 'T':
4884 	  do_not_show_symbol_truncation = TRUE;
4885 	  break;
4886 	case 'C':
4887 	  do_demangle = TRUE;
4888 	  if (optarg != NULL)
4889 	    {
4890 	      enum demangling_styles style;
4891 
4892 	      style = cplus_demangle_name_to_style (optarg);
4893 	      if (style == unknown_demangling)
4894 		error (_("unknown demangling style `%s'"), optarg);
4895 
4896 	      cplus_demangle_set_style (style);
4897 	    }
4898 	  break;
4899 	case OPTION_NO_DEMANGLING:
4900 	  do_demangle = FALSE;
4901 	  break;
4902 	case OPTION_RECURSE_LIMIT:
4903 	  demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
4904 	  break;
4905 	case OPTION_NO_RECURSE_LIMIT:
4906 	  demangle_flags |= DMGL_NO_RECURSE_LIMIT;
4907 	  break;
4908 	case OPTION_WITH_SYMBOL_VERSIONS:
4909 	  /* Ignored for backward compatibility.  */
4910 	  break;
4911 
4912 	default:
4913 	  /* xgettext:c-format */
4914 	  error (_("Invalid option '-%c'\n"), c);
4915 	  /* Fall through.  */
4916 	case '?':
4917 	  usage (stderr);
4918 	}
4919     }
4920 
4921   if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
4922       && !do_segments && !do_header && !do_dump && !do_version
4923       && !do_histogram && !do_debugging && !do_arch && !do_notes
4924       && !do_section_groups && !do_archive_index
4925       && !do_dyn_syms)
4926     {
4927       if (do_checks)
4928 	{
4929 	  check_all = TRUE;
4930 	  do_dynamic = do_syms = do_reloc = do_unwind = do_sections = TRUE;
4931 	  do_segments = do_header = do_dump = do_version = TRUE;
4932 	  do_histogram = do_debugging = do_arch = do_notes = TRUE;
4933 	  do_section_groups = do_archive_index = do_dyn_syms = TRUE;
4934 	}
4935       else
4936 	usage (stderr);
4937     }
4938 }
4939 
4940 static const char *
get_elf_class(unsigned int elf_class)4941 get_elf_class (unsigned int elf_class)
4942 {
4943   static char buff[32];
4944 
4945   switch (elf_class)
4946     {
4947     case ELFCLASSNONE: return _("none");
4948     case ELFCLASS32:   return "ELF32";
4949     case ELFCLASS64:   return "ELF64";
4950     default:
4951       snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
4952       return buff;
4953     }
4954 }
4955 
4956 static const char *
get_data_encoding(unsigned int encoding)4957 get_data_encoding (unsigned int encoding)
4958 {
4959   static char buff[32];
4960 
4961   switch (encoding)
4962     {
4963     case ELFDATANONE: return _("none");
4964     case ELFDATA2LSB: return _("2's complement, little endian");
4965     case ELFDATA2MSB: return _("2's complement, big endian");
4966     default:
4967       snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
4968       return buff;
4969     }
4970 }
4971 
4972 /* Decode the data held in 'filedata->file_header'.  */
4973 
4974 static bfd_boolean
process_file_header(Filedata * filedata)4975 process_file_header (Filedata * filedata)
4976 {
4977   Elf_Internal_Ehdr * header = & filedata->file_header;
4978 
4979   if (   header->e_ident[EI_MAG0] != ELFMAG0
4980       || header->e_ident[EI_MAG1] != ELFMAG1
4981       || header->e_ident[EI_MAG2] != ELFMAG2
4982       || header->e_ident[EI_MAG3] != ELFMAG3)
4983     {
4984       error
4985 	(_("Not an ELF file - it has the wrong magic bytes at the start\n"));
4986       return FALSE;
4987     }
4988 
4989   init_dwarf_regnames_by_elf_machine_code (header->e_machine);
4990 
4991   if (do_header)
4992     {
4993       unsigned i;
4994 
4995       printf (_("ELF Header:\n"));
4996       printf (_("  Magic:   "));
4997       for (i = 0; i < EI_NIDENT; i++)
4998 	printf ("%2.2x ", header->e_ident[i]);
4999       printf ("\n");
5000       printf (_("  Class:                             %s\n"),
5001 	      get_elf_class (header->e_ident[EI_CLASS]));
5002       printf (_("  Data:                              %s\n"),
5003 	      get_data_encoding (header->e_ident[EI_DATA]));
5004       printf (_("  Version:                           %d%s\n"),
5005 	      header->e_ident[EI_VERSION],
5006 	      (header->e_ident[EI_VERSION] == EV_CURRENT
5007 	       ? _(" (current)")
5008 	       : (header->e_ident[EI_VERSION] != EV_NONE
5009 		  ? _(" <unknown>")
5010 		  : "")));
5011       printf (_("  OS/ABI:                            %s\n"),
5012 	      get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5013       printf (_("  ABI Version:                       %d\n"),
5014 	      header->e_ident[EI_ABIVERSION]);
5015       printf (_("  Type:                              %s\n"),
5016 	      get_file_type (header->e_type));
5017       printf (_("  Machine:                           %s\n"),
5018 	      get_machine_name (header->e_machine));
5019       printf (_("  Version:                           0x%lx\n"),
5020 	      header->e_version);
5021 
5022       printf (_("  Entry point address:               "));
5023       print_vma (header->e_entry, PREFIX_HEX);
5024       printf (_("\n  Start of program headers:          "));
5025       print_vma (header->e_phoff, DEC);
5026       printf (_(" (bytes into file)\n  Start of section headers:          "));
5027       print_vma (header->e_shoff, DEC);
5028       printf (_(" (bytes into file)\n"));
5029 
5030       printf (_("  Flags:                             0x%lx%s\n"),
5031 	      header->e_flags,
5032 	      get_machine_flags (filedata, header->e_flags, header->e_machine));
5033       printf (_("  Size of this header:               %u (bytes)\n"),
5034 	      header->e_ehsize);
5035       printf (_("  Size of program headers:           %u (bytes)\n"),
5036 	      header->e_phentsize);
5037       printf (_("  Number of program headers:         %u"),
5038 	      header->e_phnum);
5039       if (filedata->section_headers != NULL
5040 	  && header->e_phnum == PN_XNUM
5041 	  && filedata->section_headers[0].sh_info != 0)
5042 	{
5043 	  header->e_phnum = filedata->section_headers[0].sh_info;
5044 	  printf (" (%u)", header->e_phnum);
5045 	}
5046       putc ('\n', stdout);
5047       printf (_("  Size of section headers:           %u (bytes)\n"),
5048 	      header->e_shentsize);
5049       printf (_("  Number of section headers:         %u"),
5050 	      header->e_shnum);
5051       if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5052 	{
5053 	  header->e_shnum = filedata->section_headers[0].sh_size;
5054 	  printf (" (%u)", header->e_shnum);
5055 	}
5056       putc ('\n', stdout);
5057       printf (_("  Section header string table index: %u"),
5058 	      header->e_shstrndx);
5059       if (filedata->section_headers != NULL
5060 	  && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5061 	{
5062 	  header->e_shstrndx = filedata->section_headers[0].sh_link;
5063 	  printf (" (%u)", header->e_shstrndx);
5064 	}
5065       if (header->e_shstrndx != SHN_UNDEF
5066 	  && header->e_shstrndx >= header->e_shnum)
5067 	{
5068 	  header->e_shstrndx = SHN_UNDEF;
5069 	  printf (_(" <corrupt: out of range>"));
5070 	}
5071       putc ('\n', stdout);
5072     }
5073 
5074   if (filedata->section_headers != NULL)
5075     {
5076       if (header->e_phnum == PN_XNUM
5077 	  && filedata->section_headers[0].sh_info != 0)
5078 	header->e_phnum = filedata->section_headers[0].sh_info;
5079       if (header->e_shnum == SHN_UNDEF)
5080 	header->e_shnum = filedata->section_headers[0].sh_size;
5081       if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5082 	header->e_shstrndx = filedata->section_headers[0].sh_link;
5083       if (header->e_shstrndx >= header->e_shnum)
5084 	header->e_shstrndx = SHN_UNDEF;
5085       free (filedata->section_headers);
5086       filedata->section_headers = NULL;
5087     }
5088 
5089   return TRUE;
5090 }
5091 
5092 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5093    Returns TRUE upon success, FALSE otherwise.  Loads 32-bit headers.  */
5094 
5095 static bfd_boolean
get_32bit_program_headers(Filedata * filedata,Elf_Internal_Phdr * pheaders)5096 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5097 {
5098   Elf32_External_Phdr * phdrs;
5099   Elf32_External_Phdr * external;
5100   Elf_Internal_Phdr *   internal;
5101   unsigned int i;
5102   unsigned int size = filedata->file_header.e_phentsize;
5103   unsigned int num  = filedata->file_header.e_phnum;
5104 
5105   /* PR binutils/17531: Cope with unexpected section header sizes.  */
5106   if (size == 0 || num == 0)
5107     return FALSE;
5108   if (size < sizeof * phdrs)
5109     {
5110       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5111       return FALSE;
5112     }
5113   if (size > sizeof * phdrs)
5114     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5115 
5116   phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5117                                             size, num, _("program headers"));
5118   if (phdrs == NULL)
5119     return FALSE;
5120 
5121   for (i = 0, internal = pheaders, external = phdrs;
5122        i < filedata->file_header.e_phnum;
5123        i++, internal++, external++)
5124     {
5125       internal->p_type   = BYTE_GET (external->p_type);
5126       internal->p_offset = BYTE_GET (external->p_offset);
5127       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
5128       internal->p_paddr  = BYTE_GET (external->p_paddr);
5129       internal->p_filesz = BYTE_GET (external->p_filesz);
5130       internal->p_memsz  = BYTE_GET (external->p_memsz);
5131       internal->p_flags  = BYTE_GET (external->p_flags);
5132       internal->p_align  = BYTE_GET (external->p_align);
5133     }
5134 
5135   free (phdrs);
5136   return TRUE;
5137 }
5138 
5139 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5140    Returns TRUE upon success, FALSE otherwise.  Loads 64-bit headers.  */
5141 
5142 static bfd_boolean
get_64bit_program_headers(Filedata * filedata,Elf_Internal_Phdr * pheaders)5143 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5144 {
5145   Elf64_External_Phdr * phdrs;
5146   Elf64_External_Phdr * external;
5147   Elf_Internal_Phdr *   internal;
5148   unsigned int i;
5149   unsigned int size = filedata->file_header.e_phentsize;
5150   unsigned int num  = filedata->file_header.e_phnum;
5151 
5152   /* PR binutils/17531: Cope with unexpected section header sizes.  */
5153   if (size == 0 || num == 0)
5154     return FALSE;
5155   if (size < sizeof * phdrs)
5156     {
5157       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5158       return FALSE;
5159     }
5160   if (size > sizeof * phdrs)
5161     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5162 
5163   phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5164                                             size, num, _("program headers"));
5165   if (!phdrs)
5166     return FALSE;
5167 
5168   for (i = 0, internal = pheaders, external = phdrs;
5169        i < filedata->file_header.e_phnum;
5170        i++, internal++, external++)
5171     {
5172       internal->p_type   = BYTE_GET (external->p_type);
5173       internal->p_flags  = BYTE_GET (external->p_flags);
5174       internal->p_offset = BYTE_GET (external->p_offset);
5175       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
5176       internal->p_paddr  = BYTE_GET (external->p_paddr);
5177       internal->p_filesz = BYTE_GET (external->p_filesz);
5178       internal->p_memsz  = BYTE_GET (external->p_memsz);
5179       internal->p_align  = BYTE_GET (external->p_align);
5180     }
5181 
5182   free (phdrs);
5183   return TRUE;
5184 }
5185 
5186 /* Returns TRUE if the program headers were read into `program_headers'.  */
5187 
5188 static bfd_boolean
get_program_headers(Filedata * filedata)5189 get_program_headers (Filedata * filedata)
5190 {
5191   Elf_Internal_Phdr * phdrs;
5192 
5193   /* Check cache of prior read.  */
5194   if (filedata->program_headers != NULL)
5195     return TRUE;
5196 
5197   /* Be kind to memory checkers by looking for
5198      e_phnum values which we know must be invalid.  */
5199   if (filedata->file_header.e_phnum
5200       * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5201       >= filedata->file_size)
5202     {
5203       error (_("Too many program headers - %#x - the file is not that big\n"),
5204 	     filedata->file_header.e_phnum);
5205       return FALSE;
5206     }
5207 
5208   phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5209 					 sizeof (Elf_Internal_Phdr));
5210   if (phdrs == NULL)
5211     {
5212       error (_("Out of memory reading %u program headers\n"),
5213 	     filedata->file_header.e_phnum);
5214       return FALSE;
5215     }
5216 
5217   if (is_32bit_elf
5218       ? get_32bit_program_headers (filedata, phdrs)
5219       : get_64bit_program_headers (filedata, phdrs))
5220     {
5221       filedata->program_headers = phdrs;
5222       return TRUE;
5223     }
5224 
5225   free (phdrs);
5226   return FALSE;
5227 }
5228 
5229 /* Returns TRUE if the program headers were loaded.  */
5230 
5231 static bfd_boolean
process_program_headers(Filedata * filedata)5232 process_program_headers (Filedata * filedata)
5233 {
5234   Elf_Internal_Phdr * segment;
5235   unsigned int i;
5236   Elf_Internal_Phdr * previous_load = NULL;
5237 
5238   filedata->dynamic_addr = 0;
5239   filedata->dynamic_size = 0;
5240 
5241   if (filedata->file_header.e_phnum == 0)
5242     {
5243       /* PR binutils/12467.  */
5244       if (filedata->file_header.e_phoff != 0)
5245 	{
5246 	  warn (_("possibly corrupt ELF header - it has a non-zero program"
5247 		  " header offset, but no program headers\n"));
5248 	  return FALSE;
5249 	}
5250       else if (do_segments)
5251 	printf (_("\nThere are no program headers in this file.\n"));
5252       return TRUE;
5253     }
5254 
5255   if (do_segments && !do_header)
5256     {
5257       printf (_("\nElf file type is %s\n"), get_file_type (filedata->file_header.e_type));
5258       printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5259       printf (ngettext ("There is %d program header, starting at offset %s\n",
5260 			"There are %d program headers, starting at offset %s\n",
5261 			filedata->file_header.e_phnum),
5262 	      filedata->file_header.e_phnum,
5263 	      bfd_vmatoa ("u", filedata->file_header.e_phoff));
5264     }
5265 
5266   if (! get_program_headers (filedata))
5267     return TRUE;
5268 
5269   if (do_segments)
5270     {
5271       if (filedata->file_header.e_phnum > 1)
5272 	printf (_("\nProgram Headers:\n"));
5273       else
5274 	printf (_("\nProgram Headers:\n"));
5275 
5276       if (is_32bit_elf)
5277 	printf
5278 	  (_("  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n"));
5279       else if (do_wide)
5280 	printf
5281 	  (_("  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align\n"));
5282       else
5283 	{
5284 	  printf
5285 	    (_("  Type           Offset             VirtAddr           PhysAddr\n"));
5286 	  printf
5287 	    (_("                 FileSiz            MemSiz              Flags  Align\n"));
5288 	}
5289     }
5290 
5291   for (i = 0, segment = filedata->program_headers;
5292        i < filedata->file_header.e_phnum;
5293        i++, segment++)
5294     {
5295       if (do_segments)
5296 	{
5297 	  printf ("  %-14.14s ", get_segment_type (filedata, segment->p_type));
5298 
5299 	  if (is_32bit_elf)
5300 	    {
5301 	      printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5302 	      printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5303 	      printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5304 	      printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5305 	      printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5306 	      printf ("%c%c%c ",
5307 		      (segment->p_flags & PF_R ? 'R' : ' '),
5308 		      (segment->p_flags & PF_W ? 'W' : ' '),
5309 		      (segment->p_flags & PF_X ? 'E' : ' '));
5310 	      printf ("%#lx", (unsigned long) segment->p_align);
5311 	    }
5312 	  else if (do_wide)
5313 	    {
5314 	      if ((unsigned long) segment->p_offset == segment->p_offset)
5315 		printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5316 	      else
5317 		{
5318 		  print_vma (segment->p_offset, FULL_HEX);
5319 		  putchar (' ');
5320 		}
5321 
5322 	      print_vma (segment->p_vaddr, FULL_HEX);
5323 	      putchar (' ');
5324 	      print_vma (segment->p_paddr, FULL_HEX);
5325 	      putchar (' ');
5326 
5327 	      if ((unsigned long) segment->p_filesz == segment->p_filesz)
5328 		printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5329 	      else
5330 		{
5331 		  print_vma (segment->p_filesz, FULL_HEX);
5332 		  putchar (' ');
5333 		}
5334 
5335 	      if ((unsigned long) segment->p_memsz == segment->p_memsz)
5336 		printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5337 	      else
5338 		{
5339 		  print_vma (segment->p_memsz, FULL_HEX);
5340 		}
5341 
5342 	      printf (" %c%c%c ",
5343 		      (segment->p_flags & PF_R ? 'R' : ' '),
5344 		      (segment->p_flags & PF_W ? 'W' : ' '),
5345 		      (segment->p_flags & PF_X ? 'E' : ' '));
5346 
5347 	      if ((unsigned long) segment->p_align == segment->p_align)
5348 		printf ("%#lx", (unsigned long) segment->p_align);
5349 	      else
5350 		{
5351 		  print_vma (segment->p_align, PREFIX_HEX);
5352 		}
5353 	    }
5354 	  else
5355 	    {
5356 	      print_vma (segment->p_offset, FULL_HEX);
5357 	      putchar (' ');
5358 	      print_vma (segment->p_vaddr, FULL_HEX);
5359 	      putchar (' ');
5360 	      print_vma (segment->p_paddr, FULL_HEX);
5361 	      printf ("\n                 ");
5362 	      print_vma (segment->p_filesz, FULL_HEX);
5363 	      putchar (' ');
5364 	      print_vma (segment->p_memsz, FULL_HEX);
5365 	      printf ("  %c%c%c    ",
5366 		      (segment->p_flags & PF_R ? 'R' : ' '),
5367 		      (segment->p_flags & PF_W ? 'W' : ' '),
5368 		      (segment->p_flags & PF_X ? 'E' : ' '));
5369 	      print_vma (segment->p_align, PREFIX_HEX);
5370 	    }
5371 
5372 	  putc ('\n', stdout);
5373 	}
5374 
5375       switch (segment->p_type)
5376 	{
5377 	case PT_LOAD:
5378 #if 0 /* Do not warn about out of order PT_LOAD segments.  Although officially
5379 	 required by the ELF standard, several programs, including the Linux
5380 	 kernel, make use of non-ordered segments.  */
5381 	  if (previous_load
5382 	      && previous_load->p_vaddr > segment->p_vaddr)
5383 	    error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5384 #endif
5385 	  if (segment->p_memsz < segment->p_filesz)
5386 	    error (_("the segment's file size is larger than its memory size\n"));
5387 	  previous_load = segment;
5388 	  break;
5389 
5390 	case PT_PHDR:
5391 	  /* PR 20815 - Verify that the program header is loaded into memory.  */
5392 	  if (i > 0 && previous_load != NULL)
5393 	    error (_("the PHDR segment must occur before any LOAD segment\n"));
5394 	  if (filedata->file_header.e_machine != EM_PARISC)
5395 	    {
5396 	      unsigned int j;
5397 
5398 	      for (j = 1; j < filedata->file_header.e_phnum; j++)
5399 		{
5400 		  Elf_Internal_Phdr *load = filedata->program_headers + j;
5401 		  if (load->p_type == PT_LOAD
5402 		      && load->p_offset <= segment->p_offset
5403 		      && (load->p_offset + load->p_filesz
5404 			  >= segment->p_offset + segment->p_filesz)
5405 		      && load->p_vaddr <= segment->p_vaddr
5406 		      && (load->p_vaddr + load->p_filesz
5407 			  >= segment->p_vaddr + segment->p_filesz))
5408 		    break;
5409 		}
5410 	      if (j == filedata->file_header.e_phnum)
5411 		error (_("the PHDR segment is not covered by a LOAD segment\n"));
5412 	    }
5413 	  break;
5414 
5415 	case PT_DYNAMIC:
5416 	  if (filedata->dynamic_addr)
5417 	    error (_("more than one dynamic segment\n"));
5418 
5419 	  /* By default, assume that the .dynamic section is the first
5420 	     section in the DYNAMIC segment.  */
5421 	  filedata->dynamic_addr = segment->p_offset;
5422 	  filedata->dynamic_size = segment->p_filesz;
5423 
5424 	  /* Try to locate the .dynamic section. If there is
5425 	     a section header table, we can easily locate it.  */
5426 	  if (filedata->section_headers != NULL)
5427 	    {
5428 	      Elf_Internal_Shdr * sec;
5429 
5430 	      sec = find_section (filedata, ".dynamic");
5431 	      if (sec == NULL || sec->sh_size == 0)
5432 		{
5433                   /* A corresponding .dynamic section is expected, but on
5434                      IA-64/OpenVMS it is OK for it to be missing.  */
5435                   if (!is_ia64_vms (filedata))
5436                     error (_("no .dynamic section in the dynamic segment\n"));
5437 		  break;
5438 		}
5439 
5440 	      if (sec->sh_type == SHT_NOBITS)
5441 		{
5442 		  filedata->dynamic_size = 0;
5443 		  break;
5444 		}
5445 
5446 	      filedata->dynamic_addr = sec->sh_offset;
5447 	      filedata->dynamic_size = sec->sh_size;
5448 
5449 	      /* The PT_DYNAMIC segment, which is used by the run-time
5450 		 loader,  should exactly match the .dynamic section.  */
5451 	      if (do_checks
5452 		  && (filedata->dynamic_addr != segment->p_offset
5453 		      || filedata->dynamic_size != segment->p_filesz))
5454 		warn (_("\
5455 the .dynamic section is not the same as the dynamic segment\n"));
5456 	    }
5457 
5458 	  /* PR binutils/17512: Avoid corrupt dynamic section info in the
5459 	     segment.  Check this after matching against the section headers
5460 	     so we don't warn on debuginfo file (which have NOBITS .dynamic
5461 	     sections).  */
5462 	  if (filedata->dynamic_addr > filedata->file_size
5463 	      || (filedata->dynamic_size
5464 		  > filedata->file_size - filedata->dynamic_addr))
5465 	    {
5466 	      error (_("the dynamic segment offset + size exceeds the size of the file\n"));
5467 	      filedata->dynamic_addr = filedata->dynamic_size = 0;
5468 	    }
5469 	  break;
5470 
5471 	case PT_INTERP:
5472 	  if (fseek (filedata->handle,
5473 		     filedata->archive_file_offset + (long) segment->p_offset,
5474 		     SEEK_SET))
5475 	    error (_("Unable to find program interpreter name\n"));
5476 	  else
5477 	    {
5478 	      char fmt [32];
5479 	      int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
5480 
5481 	      if (ret >= (int) sizeof (fmt) || ret < 0)
5482 		error (_("Internal error: failed to create format string to display program interpreter\n"));
5483 
5484 	      filedata->program_interpreter[0] = 0;
5485 	      if (fscanf (filedata->handle, fmt,
5486 			  filedata->program_interpreter) <= 0)
5487 		error (_("Unable to read program interpreter name\n"));
5488 
5489 	      if (do_segments)
5490 		printf (_("      [Requesting program interpreter: %s]\n"),
5491 		    filedata->program_interpreter);
5492 	    }
5493 	  break;
5494 	}
5495     }
5496 
5497   if (do_segments
5498       && filedata->section_headers != NULL
5499       && filedata->string_table != NULL)
5500     {
5501       printf (_("\n Section to Segment mapping:\n"));
5502       printf (_("  Segment Sections...\n"));
5503 
5504       for (i = 0; i < filedata->file_header.e_phnum; i++)
5505 	{
5506 	  unsigned int j;
5507 	  Elf_Internal_Shdr * section;
5508 
5509 	  segment = filedata->program_headers + i;
5510 	  section = filedata->section_headers + 1;
5511 
5512 	  printf ("   %2.2d     ", i);
5513 
5514 	  for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
5515 	    {
5516 	      if (!ELF_TBSS_SPECIAL (section, segment)
5517 		  && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5518 		printf ("%s ", printable_section_name (filedata, section));
5519 	    }
5520 
5521 	  putc ('\n',stdout);
5522 	}
5523     }
5524 
5525   return TRUE;
5526 }
5527 
5528 
5529 /* Find the file offset corresponding to VMA by using the program headers.  */
5530 
5531 static long
offset_from_vma(Filedata * filedata,bfd_vma vma,bfd_size_type size)5532 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
5533 {
5534   Elf_Internal_Phdr * seg;
5535 
5536   if (! get_program_headers (filedata))
5537     {
5538       warn (_("Cannot interpret virtual addresses without program headers.\n"));
5539       return (long) vma;
5540     }
5541 
5542   for (seg = filedata->program_headers;
5543        seg < filedata->program_headers + filedata->file_header.e_phnum;
5544        ++seg)
5545     {
5546       if (seg->p_type != PT_LOAD)
5547 	continue;
5548 
5549       if (vma >= (seg->p_vaddr & -seg->p_align)
5550 	  && vma + size <= seg->p_vaddr + seg->p_filesz)
5551 	return vma - seg->p_vaddr + seg->p_offset;
5552     }
5553 
5554   warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5555 	(unsigned long) vma);
5556   return (long) vma;
5557 }
5558 
5559 
5560 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5561    If PROBE is true, this is just a probe and we do not generate any error
5562    messages if the load fails.  */
5563 
5564 static bfd_boolean
get_32bit_section_headers(Filedata * filedata,bfd_boolean probe)5565 get_32bit_section_headers (Filedata * filedata, bfd_boolean probe)
5566 {
5567   Elf32_External_Shdr * shdrs;
5568   Elf_Internal_Shdr *   internal;
5569   unsigned int          i;
5570   unsigned int          size = filedata->file_header.e_shentsize;
5571   unsigned int          num = probe ? 1 : filedata->file_header.e_shnum;
5572 
5573   /* PR binutils/17531: Cope with unexpected section header sizes.  */
5574   if (size == 0 || num == 0)
5575     return FALSE;
5576   if (size < sizeof * shdrs)
5577     {
5578       if (! probe)
5579 	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5580       return FALSE;
5581     }
5582   if (!probe && size > sizeof * shdrs)
5583     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5584 
5585   shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
5586                                             size, num,
5587 					    probe ? NULL : _("section headers"));
5588   if (shdrs == NULL)
5589     return FALSE;
5590 
5591   free (filedata->section_headers);
5592   filedata->section_headers = (Elf_Internal_Shdr *)
5593     cmalloc (num, sizeof (Elf_Internal_Shdr));
5594   if (filedata->section_headers == NULL)
5595     {
5596       if (!probe)
5597 	error (_("Out of memory reading %u section headers\n"), num);
5598       free (shdrs);
5599       return FALSE;
5600     }
5601 
5602   for (i = 0, internal = filedata->section_headers;
5603        i < num;
5604        i++, internal++)
5605     {
5606       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
5607       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
5608       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
5609       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
5610       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
5611       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
5612       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
5613       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
5614       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5615       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
5616       if (!probe && internal->sh_link > num)
5617 	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5618       if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5619 	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5620     }
5621 
5622   free (shdrs);
5623   return TRUE;
5624 }
5625 
5626 /* Like get_32bit_section_headers, except that it fetches 64-bit headers.  */
5627 
5628 static bfd_boolean
get_64bit_section_headers(Filedata * filedata,bfd_boolean probe)5629 get_64bit_section_headers (Filedata * filedata, bfd_boolean probe)
5630 {
5631   Elf64_External_Shdr *  shdrs;
5632   Elf_Internal_Shdr *    internal;
5633   unsigned int           i;
5634   unsigned int           size = filedata->file_header.e_shentsize;
5635   unsigned int           num = probe ? 1 : filedata->file_header.e_shnum;
5636 
5637   /* PR binutils/17531: Cope with unexpected section header sizes.  */
5638   if (size == 0 || num == 0)
5639     return FALSE;
5640 
5641   if (size < sizeof * shdrs)
5642     {
5643       if (! probe)
5644 	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5645       return FALSE;
5646     }
5647 
5648   if (! probe && size > sizeof * shdrs)
5649     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5650 
5651   shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5652 					    filedata->file_header.e_shoff,
5653                                             size, num,
5654 					    probe ? NULL : _("section headers"));
5655   if (shdrs == NULL)
5656     return FALSE;
5657 
5658   free (filedata->section_headers);
5659   filedata->section_headers = (Elf_Internal_Shdr *)
5660     cmalloc (num, sizeof (Elf_Internal_Shdr));
5661   if (filedata->section_headers == NULL)
5662     {
5663       if (! probe)
5664 	error (_("Out of memory reading %u section headers\n"), num);
5665       free (shdrs);
5666       return FALSE;
5667     }
5668 
5669   for (i = 0, internal = filedata->section_headers;
5670        i < num;
5671        i++, internal++)
5672     {
5673       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
5674       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
5675       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
5676       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
5677       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
5678       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
5679       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
5680       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
5681       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
5682       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5683       if (!probe && internal->sh_link > num)
5684 	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5685       if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5686 	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5687     }
5688 
5689   free (shdrs);
5690   return TRUE;
5691 }
5692 
5693 static Elf_Internal_Sym *
get_32bit_elf_symbols(Filedata * filedata,Elf_Internal_Shdr * section,unsigned long * num_syms_return)5694 get_32bit_elf_symbols (Filedata *           filedata,
5695 		       Elf_Internal_Shdr *  section,
5696 		       unsigned long *      num_syms_return)
5697 {
5698   unsigned long number = 0;
5699   Elf32_External_Sym * esyms = NULL;
5700   Elf_External_Sym_Shndx * shndx = NULL;
5701   Elf_Internal_Sym * isyms = NULL;
5702   Elf_Internal_Sym * psym;
5703   unsigned int j;
5704   elf_section_list * entry;
5705 
5706   if (section->sh_size == 0)
5707     {
5708       if (num_syms_return != NULL)
5709 	* num_syms_return = 0;
5710       return NULL;
5711     }
5712 
5713   /* Run some sanity checks first.  */
5714   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5715     {
5716       error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5717 	     printable_section_name (filedata, section),
5718 	     (unsigned long) section->sh_entsize);
5719       goto exit_point;
5720     }
5721 
5722   if (section->sh_size > filedata->file_size)
5723     {
5724       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5725 	     printable_section_name (filedata, section),
5726 	     (unsigned long) section->sh_size);
5727       goto exit_point;
5728     }
5729 
5730   number = section->sh_size / section->sh_entsize;
5731 
5732   if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5733     {
5734       error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5735 	     (unsigned long) section->sh_size,
5736 	     printable_section_name (filedata, section),
5737 	     (unsigned long) section->sh_entsize);
5738       goto exit_point;
5739     }
5740 
5741   esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5742                                            section->sh_size, _("symbols"));
5743   if (esyms == NULL)
5744     goto exit_point;
5745 
5746   shndx = NULL;
5747   for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
5748     {
5749       if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5750 	continue;
5751 
5752       if (shndx != NULL)
5753 	{
5754 	  error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5755 	  free (shndx);
5756 	}
5757 
5758       shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5759 						   entry->hdr->sh_offset,
5760 						   1, entry->hdr->sh_size,
5761 						   _("symbol table section indices"));
5762       if (shndx == NULL)
5763 	goto exit_point;
5764 
5765       /* PR17531: file: heap-buffer-overflow */
5766       if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5767 	{
5768 	  error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5769 		 printable_section_name (filedata, entry->hdr),
5770 		 (unsigned long) entry->hdr->sh_size,
5771 		 (unsigned long) section->sh_size);
5772 	  goto exit_point;
5773 	}
5774     }
5775 
5776   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5777 
5778   if (isyms == NULL)
5779     {
5780       error (_("Out of memory reading %lu symbols\n"),
5781 	     (unsigned long) number);
5782       goto exit_point;
5783     }
5784 
5785   for (j = 0, psym = isyms; j < number; j++, psym++)
5786     {
5787       psym->st_name  = BYTE_GET (esyms[j].st_name);
5788       psym->st_value = BYTE_GET (esyms[j].st_value);
5789       psym->st_size  = BYTE_GET (esyms[j].st_size);
5790       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5791       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5792 	psym->st_shndx
5793 	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5794       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5795 	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5796       psym->st_info  = BYTE_GET (esyms[j].st_info);
5797       psym->st_other = BYTE_GET (esyms[j].st_other);
5798     }
5799 
5800  exit_point:
5801   free (shndx);
5802   free (esyms);
5803 
5804   if (num_syms_return != NULL)
5805     * num_syms_return = isyms == NULL ? 0 : number;
5806 
5807   return isyms;
5808 }
5809 
5810 static Elf_Internal_Sym *
get_64bit_elf_symbols(Filedata * filedata,Elf_Internal_Shdr * section,unsigned long * num_syms_return)5811 get_64bit_elf_symbols (Filedata *           filedata,
5812 		       Elf_Internal_Shdr *  section,
5813 		       unsigned long *      num_syms_return)
5814 {
5815   unsigned long number = 0;
5816   Elf64_External_Sym * esyms = NULL;
5817   Elf_External_Sym_Shndx * shndx = NULL;
5818   Elf_Internal_Sym * isyms = NULL;
5819   Elf_Internal_Sym * psym;
5820   unsigned int j;
5821   elf_section_list * entry;
5822 
5823   if (section->sh_size == 0)
5824     {
5825       if (num_syms_return != NULL)
5826 	* num_syms_return = 0;
5827       return NULL;
5828     }
5829 
5830   /* Run some sanity checks first.  */
5831   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5832     {
5833       error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5834 	     printable_section_name (filedata, section),
5835 	     (unsigned long) section->sh_entsize);
5836       goto exit_point;
5837     }
5838 
5839   if (section->sh_size > filedata->file_size)
5840     {
5841       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5842 	     printable_section_name (filedata, section),
5843 	     (unsigned long) section->sh_size);
5844       goto exit_point;
5845     }
5846 
5847   number = section->sh_size / section->sh_entsize;
5848 
5849   if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
5850     {
5851       error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5852 	     (unsigned long) section->sh_size,
5853 	     printable_section_name (filedata, section),
5854 	     (unsigned long) section->sh_entsize);
5855       goto exit_point;
5856     }
5857 
5858   esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5859                                            section->sh_size, _("symbols"));
5860   if (!esyms)
5861     goto exit_point;
5862 
5863   shndx = NULL;
5864   for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
5865     {
5866       if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5867 	continue;
5868 
5869       if (shndx != NULL)
5870 	{
5871 	  error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5872 	  free (shndx);
5873 	}
5874 
5875       shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5876 						   entry->hdr->sh_offset,
5877 						   1, entry->hdr->sh_size,
5878 						   _("symbol table section indices"));
5879       if (shndx == NULL)
5880 	goto exit_point;
5881 
5882       /* PR17531: file: heap-buffer-overflow */
5883       if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5884 	{
5885 	  error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5886 		 printable_section_name (filedata, entry->hdr),
5887 		 (unsigned long) entry->hdr->sh_size,
5888 		 (unsigned long) section->sh_size);
5889 	  goto exit_point;
5890 	}
5891     }
5892 
5893   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5894 
5895   if (isyms == NULL)
5896     {
5897       error (_("Out of memory reading %lu symbols\n"),
5898 	     (unsigned long) number);
5899       goto exit_point;
5900     }
5901 
5902   for (j = 0, psym = isyms; j < number; j++, psym++)
5903     {
5904       psym->st_name  = BYTE_GET (esyms[j].st_name);
5905       psym->st_info  = BYTE_GET (esyms[j].st_info);
5906       psym->st_other = BYTE_GET (esyms[j].st_other);
5907       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5908 
5909       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5910 	psym->st_shndx
5911 	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5912       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5913 	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5914 
5915       psym->st_value = BYTE_GET (esyms[j].st_value);
5916       psym->st_size  = BYTE_GET (esyms[j].st_size);
5917     }
5918 
5919  exit_point:
5920   free (shndx);
5921   free (esyms);
5922 
5923   if (num_syms_return != NULL)
5924     * num_syms_return = isyms == NULL ? 0 : number;
5925 
5926   return isyms;
5927 }
5928 
5929 static const char *
get_elf_section_flags(Filedata * filedata,bfd_vma sh_flags)5930 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
5931 {
5932   static char buff[1024];
5933   char * p = buff;
5934   unsigned int field_size = is_32bit_elf ? 8 : 16;
5935   signed int sindex;
5936   unsigned int size = sizeof (buff) - (field_size + 4 + 1);
5937   bfd_vma os_flags = 0;
5938   bfd_vma proc_flags = 0;
5939   bfd_vma unknown_flags = 0;
5940   static const struct
5941     {
5942       const char * str;
5943       unsigned int len;
5944     }
5945   flags [] =
5946     {
5947       /*  0 */ { STRING_COMMA_LEN ("WRITE") },
5948       /*  1 */ { STRING_COMMA_LEN ("ALLOC") },
5949       /*  2 */ { STRING_COMMA_LEN ("EXEC") },
5950       /*  3 */ { STRING_COMMA_LEN ("MERGE") },
5951       /*  4 */ { STRING_COMMA_LEN ("STRINGS") },
5952       /*  5 */ { STRING_COMMA_LEN ("INFO LINK") },
5953       /*  6 */ { STRING_COMMA_LEN ("LINK ORDER") },
5954       /*  7 */ { STRING_COMMA_LEN ("OS NONCONF") },
5955       /*  8 */ { STRING_COMMA_LEN ("GROUP") },
5956       /*  9 */ { STRING_COMMA_LEN ("TLS") },
5957       /* IA-64 specific.  */
5958       /* 10 */ { STRING_COMMA_LEN ("SHORT") },
5959       /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
5960       /* IA-64 OpenVMS specific.  */
5961       /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
5962       /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
5963       /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
5964       /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
5965       /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
5966       /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
5967       /* Generic.  */
5968       /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
5969       /* SPARC specific.  */
5970       /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
5971       /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
5972       /* ARM specific.  */
5973       /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
5974       /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
5975       /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
5976       /* GNU specific.  */
5977       /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
5978       /* VLE specific.  */
5979       /* 25 */ { STRING_COMMA_LEN ("VLE") },
5980     };
5981 
5982   if (do_section_details)
5983     {
5984       sprintf (buff, "[%*.*lx]: ",
5985 	       field_size, field_size, (unsigned long) sh_flags);
5986       p += field_size + 4;
5987     }
5988 
5989   while (sh_flags)
5990     {
5991       bfd_vma flag;
5992 
5993       flag = sh_flags & - sh_flags;
5994       sh_flags &= ~ flag;
5995 
5996       if (do_section_details)
5997 	{
5998 	  switch (flag)
5999 	    {
6000 	    case SHF_WRITE:		sindex = 0; break;
6001 	    case SHF_ALLOC:		sindex = 1; break;
6002 	    case SHF_EXECINSTR:		sindex = 2; break;
6003 	    case SHF_MERGE:		sindex = 3; break;
6004 	    case SHF_STRINGS:		sindex = 4; break;
6005 	    case SHF_INFO_LINK:		sindex = 5; break;
6006 	    case SHF_LINK_ORDER:	sindex = 6; break;
6007 	    case SHF_OS_NONCONFORMING:	sindex = 7; break;
6008 	    case SHF_GROUP:		sindex = 8; break;
6009 	    case SHF_TLS:		sindex = 9; break;
6010 	    case SHF_EXCLUDE:		sindex = 18; break;
6011 	    case SHF_COMPRESSED:	sindex = 20; break;
6012 	    case SHF_GNU_MBIND:		sindex = 24; break;
6013 
6014 	    default:
6015 	      sindex = -1;
6016 	      switch (filedata->file_header.e_machine)
6017 		{
6018 		case EM_IA_64:
6019 		  if (flag == SHF_IA_64_SHORT)
6020 		    sindex = 10;
6021 		  else if (flag == SHF_IA_64_NORECOV)
6022 		    sindex = 11;
6023 #ifdef BFD64
6024 		  else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6025 		    switch (flag)
6026 		      {
6027 		      case SHF_IA_64_VMS_GLOBAL:      sindex = 12; break;
6028 		      case SHF_IA_64_VMS_OVERLAID:    sindex = 13; break;
6029 		      case SHF_IA_64_VMS_SHARED:      sindex = 14; break;
6030 		      case SHF_IA_64_VMS_VECTOR:      sindex = 15; break;
6031 		      case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6032 		      case SHF_IA_64_VMS_PROTECTED:   sindex = 17; break;
6033 		      default:                        break;
6034 		      }
6035 #endif
6036 		  break;
6037 
6038 		case EM_386:
6039 		case EM_IAMCU:
6040 		case EM_X86_64:
6041 		case EM_L1OM:
6042 		case EM_K1OM:
6043 		case EM_OLD_SPARCV9:
6044 		case EM_SPARC32PLUS:
6045 		case EM_SPARCV9:
6046 		case EM_SPARC:
6047 		  if (flag == SHF_ORDERED)
6048 		    sindex = 19;
6049 		  break;
6050 
6051 		case EM_ARM:
6052 		  switch (flag)
6053 		    {
6054 		    case SHF_ENTRYSECT: sindex = 21; break;
6055 		    case SHF_ARM_PURECODE: sindex = 22; break;
6056 		    case SHF_COMDEF: sindex = 23; break;
6057 		    default: break;
6058 		    }
6059 		  break;
6060 		case EM_PPC:
6061 		  if (flag == SHF_PPC_VLE)
6062 		    sindex = 25;
6063 		  break;
6064 
6065 		default:
6066 		  break;
6067 		}
6068 	    }
6069 
6070 	  if (sindex != -1)
6071 	    {
6072 	      if (p != buff + field_size + 4)
6073 		{
6074 		  if (size < (10 + 2))
6075 		    {
6076 		      warn (_("Internal error: not enough buffer room for section flag info"));
6077 		      return _("<unknown>");
6078 		    }
6079 		  size -= 2;
6080 		  *p++ = ',';
6081 		  *p++ = ' ';
6082 		}
6083 
6084 	      size -= flags [sindex].len;
6085 	      p = stpcpy (p, flags [sindex].str);
6086 	    }
6087 	  else if (flag & SHF_MASKOS)
6088 	    os_flags |= flag;
6089 	  else if (flag & SHF_MASKPROC)
6090 	    proc_flags |= flag;
6091 	  else
6092 	    unknown_flags |= flag;
6093 	}
6094       else
6095 	{
6096 	  switch (flag)
6097 	    {
6098 	    case SHF_WRITE:		*p = 'W'; break;
6099 	    case SHF_ALLOC:		*p = 'A'; break;
6100 	    case SHF_EXECINSTR:		*p = 'X'; break;
6101 	    case SHF_MERGE:		*p = 'M'; break;
6102 	    case SHF_STRINGS:		*p = 'S'; break;
6103 	    case SHF_INFO_LINK:		*p = 'I'; break;
6104 	    case SHF_LINK_ORDER:	*p = 'L'; break;
6105 	    case SHF_OS_NONCONFORMING:	*p = 'O'; break;
6106 	    case SHF_GROUP:		*p = 'G'; break;
6107 	    case SHF_TLS:		*p = 'T'; break;
6108 	    case SHF_EXCLUDE:		*p = 'E'; break;
6109 	    case SHF_COMPRESSED:	*p = 'C'; break;
6110 	    case SHF_GNU_MBIND:		*p = 'D'; break;
6111 
6112 	    default:
6113 	      if ((filedata->file_header.e_machine == EM_X86_64
6114 		   || filedata->file_header.e_machine == EM_L1OM
6115 		   || filedata->file_header.e_machine == EM_K1OM)
6116 		  && flag == SHF_X86_64_LARGE)
6117 		*p = 'l';
6118 	      else if (filedata->file_header.e_machine == EM_ARM
6119 		       && flag == SHF_ARM_PURECODE)
6120 		  *p = 'y';
6121 	      else if (filedata->file_header.e_machine == EM_PPC
6122 		       && flag == SHF_PPC_VLE)
6123 		  *p = 'v';
6124 	      else if (flag & SHF_MASKOS)
6125 		{
6126 		  *p = 'o';
6127 		  sh_flags &= ~ SHF_MASKOS;
6128 		}
6129 	      else if (flag & SHF_MASKPROC)
6130 		{
6131 		  *p = 'p';
6132 		  sh_flags &= ~ SHF_MASKPROC;
6133 		}
6134 	      else
6135 		*p = 'x';
6136 	      break;
6137 	    }
6138 	  p++;
6139 	}
6140     }
6141 
6142   if (do_section_details)
6143     {
6144       if (os_flags)
6145 	{
6146 	  size -= 5 + field_size;
6147 	  if (p != buff + field_size + 4)
6148 	    {
6149 	      if (size < (2 + 1))
6150 		{
6151 		  warn (_("Internal error: not enough buffer room for section flag info"));
6152 		  return _("<unknown>");
6153 		}
6154 	      size -= 2;
6155 	      *p++ = ',';
6156 	      *p++ = ' ';
6157 	    }
6158 	  sprintf (p, "OS (%*.*lx)", field_size, field_size,
6159 		   (unsigned long) os_flags);
6160 	  p += 5 + field_size;
6161 	}
6162       if (proc_flags)
6163 	{
6164 	  size -= 7 + field_size;
6165 	  if (p != buff + field_size + 4)
6166 	    {
6167 	      if (size < (2 + 1))
6168 		{
6169 		  warn (_("Internal error: not enough buffer room for section flag info"));
6170 		  return _("<unknown>");
6171 		}
6172 	      size -= 2;
6173 	      *p++ = ',';
6174 	      *p++ = ' ';
6175 	    }
6176 	  sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6177 		   (unsigned long) proc_flags);
6178 	  p += 7 + field_size;
6179 	}
6180       if (unknown_flags)
6181 	{
6182 	  size -= 10 + field_size;
6183 	  if (p != buff + field_size + 4)
6184 	    {
6185 	      if (size < (2 + 1))
6186 		{
6187 		  warn (_("Internal error: not enough buffer room for section flag info"));
6188 		  return _("<unknown>");
6189 		}
6190 	      size -= 2;
6191 	      *p++ = ',';
6192 	      *p++ = ' ';
6193 	    }
6194 	  sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
6195 		   (unsigned long) unknown_flags);
6196 	  p += 10 + field_size;
6197 	}
6198     }
6199 
6200   *p = '\0';
6201   return buff;
6202 }
6203 
6204 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
get_compression_header(Elf_Internal_Chdr * chdr,unsigned char * buf,bfd_size_type size)6205 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
6206 {
6207   if (is_32bit_elf)
6208     {
6209       Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
6210 
6211       if (size < sizeof (* echdr))
6212 	{
6213 	  error (_("Compressed section is too small even for a compression header\n"));
6214 	  return 0;
6215 	}
6216 
6217       chdr->ch_type = BYTE_GET (echdr->ch_type);
6218       chdr->ch_size = BYTE_GET (echdr->ch_size);
6219       chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6220       return sizeof (*echdr);
6221     }
6222   else
6223     {
6224       Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
6225 
6226       if (size < sizeof (* echdr))
6227 	{
6228 	  error (_("Compressed section is too small even for a compression header\n"));
6229 	  return 0;
6230 	}
6231 
6232       chdr->ch_type = BYTE_GET (echdr->ch_type);
6233       chdr->ch_size = BYTE_GET (echdr->ch_size);
6234       chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6235       return sizeof (*echdr);
6236     }
6237 }
6238 
6239 static bfd_boolean
process_section_headers(Filedata * filedata)6240 process_section_headers (Filedata * filedata)
6241 {
6242   Elf_Internal_Shdr * section;
6243   unsigned int i;
6244 
6245   free (filedata->section_headers);
6246   filedata->section_headers = NULL;
6247   free (filedata->dynamic_symbols);
6248   filedata->dynamic_symbols = NULL;
6249   filedata->num_dynamic_syms = 0;
6250   free (filedata->dynamic_strings);
6251   filedata->dynamic_strings = NULL;
6252   filedata->dynamic_strings_length = 0;
6253   free (filedata->dynamic_syminfo);
6254   filedata->dynamic_syminfo = NULL;
6255   while (filedata->symtab_shndx_list != NULL)
6256     {
6257       elf_section_list *next = filedata->symtab_shndx_list->next;
6258       free (filedata->symtab_shndx_list);
6259       filedata->symtab_shndx_list = next;
6260     }
6261 
6262   if (filedata->file_header.e_shnum == 0)
6263     {
6264       /* PR binutils/12467.  */
6265       if (filedata->file_header.e_shoff != 0)
6266 	{
6267 	  warn (_("possibly corrupt ELF file header - it has a non-zero"
6268 		  " section header offset, but no section headers\n"));
6269 	  return FALSE;
6270 	}
6271       else if (do_sections)
6272 	printf (_("\nThere are no sections in this file.\n"));
6273 
6274       return TRUE;
6275     }
6276 
6277   if (do_sections && !do_header)
6278     printf (ngettext ("There is %d section header, "
6279 		      "starting at offset 0x%lx:\n",
6280 		      "There are %d section headers, "
6281 		      "starting at offset 0x%lx:\n",
6282 		      filedata->file_header.e_shnum),
6283 	    filedata->file_header.e_shnum,
6284 	    (unsigned long) filedata->file_header.e_shoff);
6285 
6286   if (is_32bit_elf)
6287     {
6288       if (! get_32bit_section_headers (filedata, FALSE))
6289 	return FALSE;
6290     }
6291   else
6292     {
6293       if (! get_64bit_section_headers (filedata, FALSE))
6294 	return FALSE;
6295     }
6296 
6297   /* Read in the string table, so that we have names to display.  */
6298   if (filedata->file_header.e_shstrndx != SHN_UNDEF
6299        && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6300     {
6301       section = filedata->section_headers + filedata->file_header.e_shstrndx;
6302 
6303       if (section->sh_size != 0)
6304 	{
6305 	  filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6306 						      1, section->sh_size,
6307 						      _("string table"));
6308 
6309 	  filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6310 	}
6311     }
6312 
6313   /* Scan the sections for the dynamic symbol table
6314      and dynamic string table and debug sections.  */
6315   eh_addr_size = is_32bit_elf ? 4 : 8;
6316   switch (filedata->file_header.e_machine)
6317     {
6318     case EM_MIPS:
6319     case EM_MIPS_RS3_LE:
6320       /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6321 	 FDE addresses.  However, the ABI also has a semi-official ILP32
6322 	 variant for which the normal FDE address size rules apply.
6323 
6324 	 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6325 	 section, where XX is the size of longs in bits.  Unfortunately,
6326 	 earlier compilers provided no way of distinguishing ILP32 objects
6327 	 from LP64 objects, so if there's any doubt, we should assume that
6328 	 the official LP64 form is being used.  */
6329       if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6330 	  && find_section (filedata, ".gcc_compiled_long32") == NULL)
6331 	eh_addr_size = 8;
6332       break;
6333 
6334     case EM_H8_300:
6335     case EM_H8_300H:
6336       switch (filedata->file_header.e_flags & EF_H8_MACH)
6337 	{
6338 	case E_H8_MACH_H8300:
6339 	case E_H8_MACH_H8300HN:
6340 	case E_H8_MACH_H8300SN:
6341 	case E_H8_MACH_H8300SXN:
6342 	  eh_addr_size = 2;
6343 	  break;
6344 	case E_H8_MACH_H8300H:
6345 	case E_H8_MACH_H8300S:
6346 	case E_H8_MACH_H8300SX:
6347 	  eh_addr_size = 4;
6348 	  break;
6349 	}
6350       break;
6351 
6352     case EM_M32C_OLD:
6353     case EM_M32C:
6354       switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6355 	{
6356 	case EF_M32C_CPU_M16C:
6357 	  eh_addr_size = 2;
6358 	  break;
6359 	}
6360       break;
6361     }
6362 
6363 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64)		\
6364   do									\
6365     {									\
6366       bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64;	\
6367       if (section->sh_entsize != expected_entsize)			\
6368 	{								\
6369 	  char buf[40];							\
6370 	  sprintf_vma (buf, section->sh_entsize);			\
6371 	  /* Note: coded this way so that there is a single string for  \
6372 	     translation.  */ \
6373 	  error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6374 	  error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6375 		   (unsigned) expected_entsize);			\
6376 	  section->sh_entsize = expected_entsize;			\
6377 	}								\
6378     }									\
6379   while (0)
6380 
6381 #define CHECK_ENTSIZE(section, i, type)					\
6382   CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type),	\
6383 			sizeof (Elf64_External_##type))
6384 
6385   for (i = 0, section = filedata->section_headers;
6386        i < filedata->file_header.e_shnum;
6387        i++, section++)
6388     {
6389       char * name = SECTION_NAME (section);
6390 
6391       /* Run some sanity checks on the headers and
6392 	 possibly fill in some file data as well.  */
6393       switch (section->sh_type)
6394 	{
6395 	case SHT_DYNSYM:
6396 	  if (filedata->dynamic_symbols != NULL)
6397 	    {
6398 	      error (_("File contains multiple dynamic symbol tables\n"));
6399 	      continue;
6400 	    }
6401 
6402 	  CHECK_ENTSIZE (section, i, Sym);
6403 	  filedata->dynamic_symbols
6404 	    = GET_ELF_SYMBOLS (filedata, section, &filedata->num_dynamic_syms);
6405 	  filedata->dynamic_symtab_section = section;
6406 	  break;
6407 
6408 	case SHT_STRTAB:
6409 	  if (streq (name, ".dynstr"))
6410 	    {
6411 	      if (filedata->dynamic_strings != NULL)
6412 		{
6413 		  error (_("File contains multiple dynamic string tables\n"));
6414 		  continue;
6415 		}
6416 
6417 	      filedata->dynamic_strings
6418 		= (char *) get_data (NULL, filedata, section->sh_offset,
6419 				     1, section->sh_size, _("dynamic strings"));
6420 	      filedata->dynamic_strings_length
6421 		= filedata->dynamic_strings == NULL ? 0 : section->sh_size;
6422 	      filedata->dynamic_strtab_section = section;
6423 	    }
6424 	  break;
6425 
6426 	case SHT_SYMTAB_SHNDX:
6427 	  {
6428 	    elf_section_list * entry = xmalloc (sizeof * entry);
6429 
6430 	    entry->hdr = section;
6431 	    entry->next = filedata->symtab_shndx_list;
6432 	    filedata->symtab_shndx_list = entry;
6433 	  }
6434 	  break;
6435 
6436 	case SHT_SYMTAB:
6437 	  CHECK_ENTSIZE (section, i, Sym);
6438 	  break;
6439 
6440 	case SHT_GROUP:
6441 	  CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6442 	  break;
6443 
6444 	case SHT_REL:
6445 	  CHECK_ENTSIZE (section, i, Rel);
6446 	  if (do_checks && section->sh_size == 0)
6447 	    warn (_("Section '%s': zero-sized relocation section\n"), name);
6448 	  break;
6449 
6450 	case SHT_RELA:
6451 	  CHECK_ENTSIZE (section, i, Rela);
6452 	  if (do_checks && section->sh_size == 0)
6453 	    warn (_("Section '%s': zero-sized relocation section\n"), name);
6454 	  break;
6455 
6456 	case SHT_NOTE:
6457 	case SHT_PROGBITS:
6458 	  /* Having a zero sized section is not illegal according to the
6459 	     ELF standard, but it might be an indication that something
6460 	     is wrong.  So issue a warning if we are running in lint mode.  */
6461 	  if (do_checks && section->sh_size == 0)
6462 	    warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
6463 	  break;
6464 
6465 	default:
6466 	  break;
6467 	}
6468 
6469       if ((do_debugging || do_debug_info || do_debug_abbrevs
6470 	   || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6471 	   || do_debug_aranges || do_debug_frames || do_debug_macinfo
6472 	   || do_debug_str || do_debug_str_offsets || do_debug_loc || do_debug_ranges
6473 	   || do_debug_addr || do_debug_cu_index || do_debug_links)
6474 	  && (const_strneq (name, ".debug_")
6475 	      || const_strneq (name, ".zdebug_")))
6476 	{
6477           if (name[1] == 'z')
6478             name += sizeof (".zdebug_") - 1;
6479           else
6480             name += sizeof (".debug_") - 1;
6481 
6482 	  if (do_debugging
6483 	      || (do_debug_info     && const_strneq (name, "info"))
6484 	      || (do_debug_info     && const_strneq (name, "types"))
6485 	      || (do_debug_abbrevs  && const_strneq (name, "abbrev"))
6486 	      || (do_debug_lines    && strcmp (name, "line") == 0)
6487 	      || (do_debug_lines    && const_strneq (name, "line."))
6488 	      || (do_debug_pubnames && const_strneq (name, "pubnames"))
6489 	      || (do_debug_pubtypes && const_strneq (name, "pubtypes"))
6490 	      || (do_debug_pubnames && const_strneq (name, "gnu_pubnames"))
6491 	      || (do_debug_pubtypes && const_strneq (name, "gnu_pubtypes"))
6492 	      || (do_debug_aranges  && const_strneq (name, "aranges"))
6493 	      || (do_debug_ranges   && const_strneq (name, "ranges"))
6494 	      || (do_debug_ranges   && const_strneq (name, "rnglists"))
6495 	      || (do_debug_frames   && const_strneq (name, "frame"))
6496 	      || (do_debug_macinfo  && const_strneq (name, "macinfo"))
6497 	      || (do_debug_macinfo  && const_strneq (name, "macro"))
6498 	      || (do_debug_str      && const_strneq (name, "str"))
6499 	      || (do_debug_str_offsets && const_strneq (name, "str_offsets"))
6500 	      || (do_debug_loc      && const_strneq (name, "loc"))
6501 	      || (do_debug_loc      && const_strneq (name, "loclists"))
6502 	      || (do_debug_addr     && const_strneq (name, "addr"))
6503 	      || (do_debug_cu_index && const_strneq (name, "cu_index"))
6504 	      || (do_debug_cu_index && const_strneq (name, "tu_index"))
6505 	      )
6506 	    request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6507 	}
6508       /* Linkonce section to be combined with .debug_info at link time.  */
6509       else if ((do_debugging || do_debug_info)
6510 	       && const_strneq (name, ".gnu.linkonce.wi."))
6511 	request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6512       else if (do_debug_frames && streq (name, ".eh_frame"))
6513 	request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6514       else if (do_gdb_index && (streq (name, ".gdb_index")
6515 				|| streq (name, ".debug_names")))
6516 	request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6517       /* Trace sections for Itanium VMS.  */
6518       else if ((do_debugging || do_trace_info || do_trace_abbrevs
6519                 || do_trace_aranges)
6520 	       && const_strneq (name, ".trace_"))
6521 	{
6522           name += sizeof (".trace_") - 1;
6523 
6524 	  if (do_debugging
6525 	      || (do_trace_info     && streq (name, "info"))
6526 	      || (do_trace_abbrevs  && streq (name, "abbrev"))
6527 	      || (do_trace_aranges  && streq (name, "aranges"))
6528 	      )
6529 	    request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6530 	}
6531       else if ((do_debugging || do_debug_links)
6532 	       && (const_strneq (name, ".gnu_debuglink")
6533 		   || const_strneq (name, ".gnu_debugaltlink")))
6534 	request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6535     }
6536 
6537   if (! do_sections)
6538     return TRUE;
6539 
6540   if (filedata->file_header.e_shnum > 1)
6541     printf (_("\nSection Headers:\n"));
6542   else
6543     printf (_("\nSection Header:\n"));
6544 
6545   if (is_32bit_elf)
6546     {
6547       if (do_section_details)
6548 	{
6549 	  printf (_("  [Nr] Name\n"));
6550 	  printf (_("       Type            Addr     Off    Size   ES   Lk Inf Al\n"));
6551 	}
6552       else
6553 	printf
6554 	  (_("  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al\n"));
6555     }
6556   else if (do_wide)
6557     {
6558       if (do_section_details)
6559 	{
6560 	  printf (_("  [Nr] Name\n"));
6561 	  printf (_("       Type            Address          Off    Size   ES   Lk Inf Al\n"));
6562 	}
6563       else
6564 	printf
6565 	  (_("  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al\n"));
6566     }
6567   else
6568     {
6569       if (do_section_details)
6570 	{
6571 	  printf (_("  [Nr] Name\n"));
6572 	  printf (_("       Type              Address          Offset            Link\n"));
6573 	  printf (_("       Size              EntSize          Info              Align\n"));
6574 	}
6575       else
6576 	{
6577 	  printf (_("  [Nr] Name              Type             Address           Offset\n"));
6578 	  printf (_("       Size              EntSize          Flags  Link  Info  Align\n"));
6579 	}
6580     }
6581 
6582   if (do_section_details)
6583     printf (_("       Flags\n"));
6584 
6585   for (i = 0, section = filedata->section_headers;
6586        i < filedata->file_header.e_shnum;
6587        i++, section++)
6588     {
6589       /* Run some sanity checks on the section header.  */
6590 
6591       /* Check the sh_link field.  */
6592       switch (section->sh_type)
6593 	{
6594 	case SHT_REL:
6595 	case SHT_RELA:
6596 	  if (section->sh_link == 0
6597 	      && (filedata->file_header.e_type == ET_EXEC
6598 		  || filedata->file_header.e_type == ET_DYN))
6599 	    /* A dynamic relocation section where all entries use a
6600 	       zero symbol index need not specify a symtab section.  */
6601 	    break;
6602 	  /* Fall through.  */
6603 	case SHT_SYMTAB_SHNDX:
6604 	case SHT_GROUP:
6605 	case SHT_HASH:
6606 	case SHT_GNU_HASH:
6607 	case SHT_GNU_versym:
6608 	  if (section->sh_link == 0
6609 	      || section->sh_link >= filedata->file_header.e_shnum
6610 	      || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6611 		  && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
6612 	    warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6613 		  i, section->sh_link);
6614 	  break;
6615 
6616 	case SHT_DYNAMIC:
6617 	case SHT_SYMTAB:
6618 	case SHT_DYNSYM:
6619 	case SHT_GNU_verneed:
6620 	case SHT_GNU_verdef:
6621 	case SHT_GNU_LIBLIST:
6622 	  if (section->sh_link == 0
6623 	      || section->sh_link >= filedata->file_header.e_shnum
6624 	      || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
6625 	    warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6626 		  i, section->sh_link);
6627 	  break;
6628 
6629 	case SHT_INIT_ARRAY:
6630 	case SHT_FINI_ARRAY:
6631 	case SHT_PREINIT_ARRAY:
6632 	  if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6633 	    warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6634 		  i, section->sh_link);
6635 	  break;
6636 
6637 	default:
6638 	  /* FIXME: Add support for target specific section types.  */
6639 #if 0 	  /* Currently we do not check other section types as there are too
6640 	     many special cases.  Stab sections for example have a type
6641 	     of SHT_PROGBITS but an sh_link field that links to the .stabstr
6642 	     section.  */
6643 	  if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6644 	    warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6645 		  i, section->sh_link);
6646 #endif
6647 	  break;
6648 	}
6649 
6650       /* Check the sh_info field.  */
6651       switch (section->sh_type)
6652 	{
6653 	case SHT_REL:
6654 	case SHT_RELA:
6655 	  if (section->sh_info == 0
6656 	      && (filedata->file_header.e_type == ET_EXEC
6657 		  || filedata->file_header.e_type == ET_DYN))
6658 	    /* Dynamic relocations apply to segments, so they do not
6659 	       need to specify the section they relocate.  */
6660 	    break;
6661 	  if (section->sh_info == 0
6662 	      || section->sh_info >= filedata->file_header.e_shnum
6663 	      || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
6664 		  && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
6665 		  && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
6666 		  && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
6667 		  && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
6668 		  && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
6669 		  /* FIXME: Are other section types valid ?  */
6670 		  && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
6671 	    warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
6672 		  i, section->sh_info);
6673 	  break;
6674 
6675 	case SHT_DYNAMIC:
6676 	case SHT_HASH:
6677 	case SHT_SYMTAB_SHNDX:
6678 	case SHT_INIT_ARRAY:
6679 	case SHT_FINI_ARRAY:
6680 	case SHT_PREINIT_ARRAY:
6681 	  if (section->sh_info != 0)
6682 	    warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6683 		  i, section->sh_info);
6684 	  break;
6685 
6686 	case SHT_GROUP:
6687 	case SHT_SYMTAB:
6688 	case SHT_DYNSYM:
6689 	  /* A symbol index - we assume that it is valid.  */
6690 	  break;
6691 
6692 	default:
6693 	  /* FIXME: Add support for target specific section types.  */
6694 	  if (section->sh_type == SHT_NOBITS)
6695 	    /* NOBITS section headers with non-zero sh_info fields can be
6696 	       created when a binary is stripped of everything but its debug
6697 	       information.  The stripped sections have their headers
6698 	       preserved but their types set to SHT_NOBITS.  So do not check
6699 	       this type of section.  */
6700 	    ;
6701 	  else if (section->sh_flags & SHF_INFO_LINK)
6702 	    {
6703 	      if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
6704 		warn (_("[%2u]: Expected link to another section in info field"), i);
6705 	    }
6706 	  else if (section->sh_type < SHT_LOOS
6707 		   && (section->sh_flags & SHF_GNU_MBIND) == 0
6708 		   && section->sh_info != 0)
6709 	    warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6710 		  i, section->sh_info);
6711 	  break;
6712 	}
6713 
6714       /* Check the sh_size field.  */
6715       if (section->sh_size > filedata->file_size
6716 	  && section->sh_type != SHT_NOBITS
6717 	  && section->sh_type != SHT_NULL
6718 	  && section->sh_type < SHT_LOOS)
6719 	warn (_("Size of section %u is larger than the entire file!\n"), i);
6720 
6721       printf ("  [%2u] ", i);
6722       if (do_section_details)
6723 	printf ("%s\n      ", printable_section_name (filedata, section));
6724       else
6725 	print_symbol (-17, SECTION_NAME (section));
6726 
6727       printf (do_wide ? " %-15s " : " %-15.15s ",
6728 	      get_section_type_name (filedata, section->sh_type));
6729 
6730       if (is_32bit_elf)
6731 	{
6732 	  const char * link_too_big = NULL;
6733 
6734 	  print_vma (section->sh_addr, LONG_HEX);
6735 
6736 	  printf ( " %6.6lx %6.6lx %2.2lx",
6737 		   (unsigned long) section->sh_offset,
6738 		   (unsigned long) section->sh_size,
6739 		   (unsigned long) section->sh_entsize);
6740 
6741 	  if (do_section_details)
6742 	    fputs ("  ", stdout);
6743 	  else
6744 	    printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6745 
6746 	  if (section->sh_link >= filedata->file_header.e_shnum)
6747 	    {
6748 	      link_too_big = "";
6749 	      /* The sh_link value is out of range.  Normally this indicates
6750 		 an error but it can have special values in Solaris binaries.  */
6751 	      switch (filedata->file_header.e_machine)
6752 		{
6753 		case EM_386:
6754 		case EM_IAMCU:
6755 		case EM_X86_64:
6756 		case EM_L1OM:
6757 		case EM_K1OM:
6758 		case EM_OLD_SPARCV9:
6759 		case EM_SPARC32PLUS:
6760 		case EM_SPARCV9:
6761 		case EM_SPARC:
6762 		  if (section->sh_link == (SHN_BEFORE & 0xffff))
6763 		    link_too_big = "BEFORE";
6764 		  else if (section->sh_link == (SHN_AFTER & 0xffff))
6765 		    link_too_big = "AFTER";
6766 		  break;
6767 		default:
6768 		  break;
6769 		}
6770 	    }
6771 
6772 	  if (do_section_details)
6773 	    {
6774 	      if (link_too_big != NULL && * link_too_big)
6775 		printf ("<%s> ", link_too_big);
6776 	      else
6777 		printf ("%2u ", section->sh_link);
6778 	      printf ("%3u %2lu\n", section->sh_info,
6779 		      (unsigned long) section->sh_addralign);
6780 	    }
6781 	  else
6782 	    printf ("%2u %3u %2lu\n",
6783 		    section->sh_link,
6784 		    section->sh_info,
6785 		    (unsigned long) section->sh_addralign);
6786 
6787 	  if (link_too_big && ! * link_too_big)
6788 	    warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
6789 		  i, section->sh_link);
6790 	}
6791       else if (do_wide)
6792 	{
6793 	  print_vma (section->sh_addr, LONG_HEX);
6794 
6795 	  if ((long) section->sh_offset == section->sh_offset)
6796 	    printf (" %6.6lx", (unsigned long) section->sh_offset);
6797 	  else
6798 	    {
6799 	      putchar (' ');
6800 	      print_vma (section->sh_offset, LONG_HEX);
6801 	    }
6802 
6803 	  if ((unsigned long) section->sh_size == section->sh_size)
6804 	    printf (" %6.6lx", (unsigned long) section->sh_size);
6805 	  else
6806 	    {
6807 	      putchar (' ');
6808 	      print_vma (section->sh_size, LONG_HEX);
6809 	    }
6810 
6811 	  if ((unsigned long) section->sh_entsize == section->sh_entsize)
6812 	    printf (" %2.2lx", (unsigned long) section->sh_entsize);
6813 	  else
6814 	    {
6815 	      putchar (' ');
6816 	      print_vma (section->sh_entsize, LONG_HEX);
6817 	    }
6818 
6819 	  if (do_section_details)
6820 	    fputs ("  ", stdout);
6821 	  else
6822 	    printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6823 
6824 	  printf ("%2u %3u ", section->sh_link, section->sh_info);
6825 
6826 	  if ((unsigned long) section->sh_addralign == section->sh_addralign)
6827 	    printf ("%2lu\n", (unsigned long) section->sh_addralign);
6828 	  else
6829 	    {
6830 	      print_vma (section->sh_addralign, DEC);
6831 	      putchar ('\n');
6832 	    }
6833 	}
6834       else if (do_section_details)
6835 	{
6836 	  putchar (' ');
6837 	  print_vma (section->sh_addr, LONG_HEX);
6838 	  if ((long) section->sh_offset == section->sh_offset)
6839 	    printf ("  %16.16lx", (unsigned long) section->sh_offset);
6840 	  else
6841 	    {
6842 	      printf ("  ");
6843 	      print_vma (section->sh_offset, LONG_HEX);
6844 	    }
6845 	  printf ("  %u\n       ", section->sh_link);
6846 	  print_vma (section->sh_size, LONG_HEX);
6847 	  putchar (' ');
6848 	  print_vma (section->sh_entsize, LONG_HEX);
6849 
6850 	  printf ("  %-16u  %lu\n",
6851 		  section->sh_info,
6852 		  (unsigned long) section->sh_addralign);
6853 	}
6854       else
6855 	{
6856 	  putchar (' ');
6857 	  print_vma (section->sh_addr, LONG_HEX);
6858 	  if ((long) section->sh_offset == section->sh_offset)
6859 	    printf ("  %8.8lx", (unsigned long) section->sh_offset);
6860 	  else
6861 	    {
6862 	      printf ("  ");
6863 	      print_vma (section->sh_offset, LONG_HEX);
6864 	    }
6865 	  printf ("\n       ");
6866 	  print_vma (section->sh_size, LONG_HEX);
6867 	  printf ("  ");
6868 	  print_vma (section->sh_entsize, LONG_HEX);
6869 
6870 	  printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6871 
6872 	  printf ("     %2u   %3u     %lu\n",
6873 		  section->sh_link,
6874 		  section->sh_info,
6875 		  (unsigned long) section->sh_addralign);
6876 	}
6877 
6878       if (do_section_details)
6879 	{
6880 	  printf ("       %s\n", get_elf_section_flags (filedata, section->sh_flags));
6881 	  if ((section->sh_flags & SHF_COMPRESSED) != 0)
6882 	    {
6883 	      /* Minimum section size is 12 bytes for 32-bit compression
6884 		 header + 12 bytes for compressed data header.  */
6885 	      unsigned char buf[24];
6886 
6887 	      assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
6888 	      if (get_data (&buf, filedata, section->sh_offset, 1,
6889 			    sizeof (buf), _("compression header")))
6890 		{
6891 		  Elf_Internal_Chdr chdr;
6892 
6893 		  if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
6894 		    printf (_("       [<corrupt>]\n"));
6895 		  else
6896 		    {
6897 		      if (chdr.ch_type == ELFCOMPRESS_ZLIB)
6898 			printf ("       ZLIB, ");
6899 		      else
6900 			printf (_("       [<unknown>: 0x%x], "),
6901 				chdr.ch_type);
6902 		      print_vma (chdr.ch_size, LONG_HEX);
6903 		      printf (", %lu\n", (unsigned long) chdr.ch_addralign);
6904 		    }
6905 		}
6906 	    }
6907 	}
6908     }
6909 
6910   if (!do_section_details)
6911     {
6912       /* The ordering of the letters shown here matches the ordering of the
6913 	 corresponding SHF_xxx values, and hence the order in which these
6914 	 letters will be displayed to the user.  */
6915       printf (_("Key to Flags:\n\
6916   W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
6917   L (link order), O (extra OS processing required), G (group), T (TLS),\n\
6918   C (compressed), x (unknown), o (OS specific), E (exclude),\n  "));
6919       if (filedata->file_header.e_machine == EM_X86_64
6920 	  || filedata->file_header.e_machine == EM_L1OM
6921 	  || filedata->file_header.e_machine == EM_K1OM)
6922 	printf (_("l (large), "));
6923       else if (filedata->file_header.e_machine == EM_ARM)
6924 	printf (_("y (purecode), "));
6925       else if (filedata->file_header.e_machine == EM_PPC)
6926 	printf (_("v (VLE), "));
6927       printf ("p (processor specific)\n");
6928     }
6929 
6930   return TRUE;
6931 }
6932 
6933 static bfd_boolean
get_symtab(Filedata * filedata,Elf_Internal_Shdr * symsec,Elf_Internal_Sym ** symtab,unsigned long * nsyms,char ** strtab,unsigned long * strtablen)6934 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
6935 	    Elf_Internal_Sym **symtab, unsigned long *nsyms,
6936 	    char **strtab, unsigned long *strtablen)
6937 {
6938   *strtab = NULL;
6939   *strtablen = 0;
6940   *symtab = GET_ELF_SYMBOLS (filedata, symsec, nsyms);
6941 
6942   if (*symtab == NULL)
6943     return FALSE;
6944 
6945   if (symsec->sh_link != 0)
6946     {
6947       Elf_Internal_Shdr *strsec;
6948 
6949       if (symsec->sh_link >= filedata->file_header.e_shnum)
6950 	{
6951 	  error (_("Bad sh_link in symbol table section\n"));
6952 	  free (*symtab);
6953 	  *symtab = NULL;
6954 	  *nsyms = 0;
6955 	  return FALSE;
6956 	}
6957 
6958       strsec = filedata->section_headers + symsec->sh_link;
6959 
6960       *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
6961 				   1, strsec->sh_size, _("string table"));
6962       if (*strtab == NULL)
6963 	{
6964 	  free (*symtab);
6965 	  *symtab = NULL;
6966 	  *nsyms = 0;
6967 	  return FALSE;
6968 	}
6969       *strtablen = strsec->sh_size;
6970     }
6971   return TRUE;
6972 }
6973 
6974 static const char *
get_group_flags(unsigned int flags)6975 get_group_flags (unsigned int flags)
6976 {
6977   static char buff[128];
6978 
6979   if (flags == 0)
6980     return "";
6981   else if (flags == GRP_COMDAT)
6982     return "COMDAT ";
6983 
6984   snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
6985 	    flags,
6986 	    flags & GRP_MASKOS ? _("<OS specific>") : "",
6987 	    flags & GRP_MASKPROC ? _("<PROC specific>") : "",
6988 	    (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
6989 	     ? _("<unknown>") : ""));
6990 
6991   return buff;
6992 }
6993 
6994 static bfd_boolean
process_section_groups(Filedata * filedata)6995 process_section_groups (Filedata * filedata)
6996 {
6997   Elf_Internal_Shdr * section;
6998   unsigned int i;
6999   struct group * group;
7000   Elf_Internal_Shdr * symtab_sec;
7001   Elf_Internal_Shdr * strtab_sec;
7002   Elf_Internal_Sym * symtab;
7003   unsigned long num_syms;
7004   char * strtab;
7005   size_t strtab_size;
7006 
7007   /* Don't process section groups unless needed.  */
7008   if (!do_unwind && !do_section_groups)
7009     return TRUE;
7010 
7011   if (filedata->file_header.e_shnum == 0)
7012     {
7013       if (do_section_groups)
7014 	printf (_("\nThere are no sections to group in this file.\n"));
7015 
7016       return TRUE;
7017     }
7018 
7019   if (filedata->section_headers == NULL)
7020     {
7021       error (_("Section headers are not available!\n"));
7022       /* PR 13622: This can happen with a corrupt ELF header.  */
7023       return FALSE;
7024     }
7025 
7026   filedata->section_headers_groups
7027     = (struct group **) calloc (filedata->file_header.e_shnum,
7028 				sizeof (struct group *));
7029 
7030   if (filedata->section_headers_groups == NULL)
7031     {
7032       error (_("Out of memory reading %u section group headers\n"),
7033 	     filedata->file_header.e_shnum);
7034       return FALSE;
7035     }
7036 
7037   /* Scan the sections for the group section.  */
7038   filedata->group_count = 0;
7039   for (i = 0, section = filedata->section_headers;
7040        i < filedata->file_header.e_shnum;
7041        i++, section++)
7042     if (section->sh_type == SHT_GROUP)
7043       filedata->group_count++;
7044 
7045   if (filedata->group_count == 0)
7046     {
7047       if (do_section_groups)
7048 	printf (_("\nThere are no section groups in this file.\n"));
7049 
7050       return TRUE;
7051     }
7052 
7053   filedata->section_groups = (struct group *) calloc (filedata->group_count,
7054 						      sizeof (struct group));
7055 
7056   if (filedata->section_groups == NULL)
7057     {
7058       error (_("Out of memory reading %lu groups\n"),
7059 	     (unsigned long) filedata->group_count);
7060       return FALSE;
7061     }
7062 
7063   symtab_sec = NULL;
7064   strtab_sec = NULL;
7065   symtab = NULL;
7066   num_syms = 0;
7067   strtab = NULL;
7068   strtab_size = 0;
7069   for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7070        i < filedata->file_header.e_shnum;
7071        i++, section++)
7072     {
7073       if (section->sh_type == SHT_GROUP)
7074 	{
7075 	  const char * name = printable_section_name (filedata, section);
7076 	  const char * group_name;
7077 	  unsigned char * start;
7078 	  unsigned char * indices;
7079 	  unsigned int entry, j, size;
7080 	  Elf_Internal_Shdr * sec;
7081 	  Elf_Internal_Sym * sym;
7082 
7083 	  /* Get the symbol table.  */
7084 	  if (section->sh_link >= filedata->file_header.e_shnum
7085 	      || ((sec = filedata->section_headers + section->sh_link)->sh_type
7086 		  != SHT_SYMTAB))
7087 	    {
7088 	      error (_("Bad sh_link in group section `%s'\n"), name);
7089 	      continue;
7090 	    }
7091 
7092 	  if (symtab_sec != sec)
7093 	    {
7094 	      symtab_sec = sec;
7095 	      free (symtab);
7096 	      symtab = GET_ELF_SYMBOLS (filedata, symtab_sec, & num_syms);
7097 	    }
7098 
7099 	  if (symtab == NULL)
7100 	    {
7101 	      error (_("Corrupt header in group section `%s'\n"), name);
7102 	      continue;
7103 	    }
7104 
7105 	  if (section->sh_info >= num_syms)
7106 	    {
7107 	      error (_("Bad sh_info in group section `%s'\n"), name);
7108 	      continue;
7109 	    }
7110 
7111 	  sym = symtab + section->sh_info;
7112 
7113 	  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7114 	    {
7115 	      if (sym->st_shndx == 0
7116 		  || sym->st_shndx >= filedata->file_header.e_shnum)
7117 		{
7118 		  error (_("Bad sh_info in group section `%s'\n"), name);
7119 		  continue;
7120 		}
7121 
7122 	      group_name = SECTION_NAME (filedata->section_headers + sym->st_shndx);
7123 	      strtab_sec = NULL;
7124 	      free (strtab);
7125 	      strtab = NULL;
7126 	      strtab_size = 0;
7127 	    }
7128 	  else
7129 	    {
7130 	      /* Get the string table.  */
7131 	      if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7132 		{
7133 		  strtab_sec = NULL;
7134 		  free (strtab);
7135 		  strtab = NULL;
7136 		  strtab_size = 0;
7137 		}
7138 	      else if (strtab_sec
7139 		       != (sec = filedata->section_headers + symtab_sec->sh_link))
7140 		{
7141 		  strtab_sec = sec;
7142 		  free (strtab);
7143 
7144 		  strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
7145 					      1, strtab_sec->sh_size,
7146 					      _("string table"));
7147 		  strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
7148 		}
7149 	      group_name = sym->st_name < strtab_size
7150 		? strtab + sym->st_name : _("<corrupt>");
7151 	    }
7152 
7153 	  /* PR 17531: file: loop.  */
7154 	  if (section->sh_entsize > section->sh_size)
7155 	    {
7156 	      error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
7157 		     printable_section_name (filedata, section),
7158 		     (unsigned long) section->sh_entsize,
7159 		     (unsigned long) section->sh_size);
7160 	      continue;
7161 	    }
7162 
7163 	  start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
7164                                               1, section->sh_size,
7165                                               _("section data"));
7166 	  if (start == NULL)
7167 	    continue;
7168 
7169 	  indices = start;
7170 	  size = (section->sh_size / section->sh_entsize) - 1;
7171 	  entry = byte_get (indices, 4);
7172 	  indices += 4;
7173 
7174 	  if (do_section_groups)
7175 	    {
7176 	      printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
7177 		      get_group_flags (entry), i, name, group_name, size);
7178 
7179 	      printf (_("   [Index]    Name\n"));
7180 	    }
7181 
7182 	  group->group_index = i;
7183 
7184 	  for (j = 0; j < size; j++)
7185 	    {
7186 	      struct group_list * g;
7187 
7188 	      entry = byte_get (indices, 4);
7189 	      indices += 4;
7190 
7191 	      if (entry >= filedata->file_header.e_shnum)
7192 		{
7193 		  static unsigned num_group_errors = 0;
7194 
7195 		  if (num_group_errors ++ < 10)
7196 		    {
7197 		      error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
7198 			     entry, i, filedata->file_header.e_shnum - 1);
7199 		      if (num_group_errors == 10)
7200 			warn (_("Further error messages about overlarge group section indices suppressed\n"));
7201 		    }
7202 		  continue;
7203 		}
7204 
7205 	      if (filedata->section_headers_groups [entry] != NULL)
7206 		{
7207 		  if (entry)
7208 		    {
7209 		      static unsigned num_errs = 0;
7210 
7211 		      if (num_errs ++ < 10)
7212 			{
7213 			  error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7214 				 entry, i,
7215 				 filedata->section_headers_groups [entry]->group_index);
7216 			  if (num_errs == 10)
7217 			    warn (_("Further error messages about already contained group sections suppressed\n"));
7218 			}
7219 		      continue;
7220 		    }
7221 		  else
7222 		    {
7223 		      /* Intel C/C++ compiler may put section 0 in a
7224 			 section group.  We just warn it the first time
7225 			 and ignore it afterwards.  */
7226 		      static bfd_boolean warned = FALSE;
7227 		      if (!warned)
7228 			{
7229 			  error (_("section 0 in group section [%5u]\n"),
7230 				 filedata->section_headers_groups [entry]->group_index);
7231 			  warned = TRUE;
7232 			}
7233 		    }
7234 		}
7235 
7236 	      filedata->section_headers_groups [entry] = group;
7237 
7238 	      if (do_section_groups)
7239 		{
7240 		  sec = filedata->section_headers + entry;
7241 		  printf ("   [%5u]   %s\n", entry, printable_section_name (filedata, sec));
7242 		}
7243 
7244 	      g = (struct group_list *) xmalloc (sizeof (struct group_list));
7245 	      g->section_index = entry;
7246 	      g->next = group->root;
7247 	      group->root = g;
7248 	    }
7249 
7250 	  free (start);
7251 
7252 	  group++;
7253 	}
7254     }
7255 
7256   free (symtab);
7257   free (strtab);
7258   return TRUE;
7259 }
7260 
7261 /* Data used to display dynamic fixups.  */
7262 
7263 struct ia64_vms_dynfixup
7264 {
7265   bfd_vma needed_ident;		/* Library ident number.  */
7266   bfd_vma needed;		/* Index in the dstrtab of the library name.  */
7267   bfd_vma fixup_needed;		/* Index of the library.  */
7268   bfd_vma fixup_rela_cnt;	/* Number of fixups.  */
7269   bfd_vma fixup_rela_off;	/* Fixups offset in the dynamic segment.  */
7270 };
7271 
7272 /* Data used to display dynamic relocations.  */
7273 
7274 struct ia64_vms_dynimgrela
7275 {
7276   bfd_vma img_rela_cnt;		/* Number of relocations.  */
7277   bfd_vma img_rela_off;		/* Reloc offset in the dynamic segment.  */
7278 };
7279 
7280 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7281    library).  */
7282 
7283 static bfd_boolean
dump_ia64_vms_dynamic_fixups(Filedata * filedata,struct ia64_vms_dynfixup * fixup,const char * strtab,unsigned int strtab_sz)7284 dump_ia64_vms_dynamic_fixups (Filedata *                  filedata,
7285 			      struct ia64_vms_dynfixup *  fixup,
7286                               const char *                strtab,
7287 			      unsigned int                strtab_sz)
7288 {
7289   Elf64_External_VMS_IMAGE_FIXUP * imfs;
7290   long i;
7291   const char * lib_name;
7292 
7293   imfs = get_data (NULL, filedata,
7294 		   filedata->dynamic_addr + fixup->fixup_rela_off,
7295 		   sizeof (*imfs), fixup->fixup_rela_cnt,
7296 		   _("dynamic section image fixups"));
7297   if (!imfs)
7298     return FALSE;
7299 
7300   if (fixup->needed < strtab_sz)
7301     lib_name = strtab + fixup->needed;
7302   else
7303     {
7304       warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7305             (unsigned long) fixup->needed);
7306       lib_name = "???";
7307     }
7308 
7309   printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7310 	  (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7311   printf
7312     (_("Seg Offset           Type                             SymVec DataType\n"));
7313 
7314   for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7315     {
7316       unsigned int type;
7317       const char *rtype;
7318 
7319       printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7320       printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7321       type = BYTE_GET (imfs [i].type);
7322       rtype = elf_ia64_reloc_type (type);
7323       if (rtype == NULL)
7324         printf (" 0x%08x                       ", type);
7325       else
7326         printf (" %-32s ", rtype);
7327       printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7328       printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7329     }
7330 
7331   free (imfs);
7332   return TRUE;
7333 }
7334 
7335 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image).  */
7336 
7337 static bfd_boolean
dump_ia64_vms_dynamic_relocs(Filedata * filedata,struct ia64_vms_dynimgrela * imgrela)7338 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
7339 {
7340   Elf64_External_VMS_IMAGE_RELA *imrs;
7341   long i;
7342 
7343   imrs = get_data (NULL, filedata,
7344 		   filedata->dynamic_addr + imgrela->img_rela_off,
7345 		   sizeof (*imrs), imgrela->img_rela_cnt,
7346 		   _("dynamic section image relocations"));
7347   if (!imrs)
7348     return FALSE;
7349 
7350   printf (_("\nImage relocs\n"));
7351   printf
7352     (_("Seg Offset   Type                            Addend            Seg Sym Off\n"));
7353 
7354   for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7355     {
7356       unsigned int type;
7357       const char *rtype;
7358 
7359       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7360       printf ("%08" BFD_VMA_FMT "x ",
7361               (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7362       type = BYTE_GET (imrs [i].type);
7363       rtype = elf_ia64_reloc_type (type);
7364       if (rtype == NULL)
7365         printf ("0x%08x                      ", type);
7366       else
7367         printf ("%-31s ", rtype);
7368       print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7369       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7370       printf ("%08" BFD_VMA_FMT "x\n",
7371               (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7372     }
7373 
7374   free (imrs);
7375   return TRUE;
7376 }
7377 
7378 /* Display IA-64 OpenVMS dynamic relocations and fixups.  */
7379 
7380 static bfd_boolean
process_ia64_vms_dynamic_relocs(Filedata * filedata)7381 process_ia64_vms_dynamic_relocs (Filedata * filedata)
7382 {
7383   struct ia64_vms_dynfixup fixup;
7384   struct ia64_vms_dynimgrela imgrela;
7385   Elf_Internal_Dyn *entry;
7386   bfd_vma strtab_off = 0;
7387   bfd_vma strtab_sz = 0;
7388   char *strtab = NULL;
7389   bfd_boolean res = TRUE;
7390 
7391   memset (&fixup, 0, sizeof (fixup));
7392   memset (&imgrela, 0, sizeof (imgrela));
7393 
7394   /* Note: the order of the entries is specified by the OpenVMS specs.  */
7395   for (entry = filedata->dynamic_section;
7396        entry < filedata->dynamic_section + filedata->dynamic_nent;
7397        entry++)
7398     {
7399       switch (entry->d_tag)
7400         {
7401         case DT_IA_64_VMS_STRTAB_OFFSET:
7402           strtab_off = entry->d_un.d_val;
7403           break;
7404         case DT_STRSZ:
7405           strtab_sz = entry->d_un.d_val;
7406           if (strtab == NULL)
7407 	    strtab = get_data (NULL, filedata,
7408 			       filedata->dynamic_addr + strtab_off,
7409                                1, strtab_sz, _("dynamic string section"));
7410 	  if (strtab == NULL)
7411 	    strtab_sz = 0;
7412           break;
7413 
7414         case DT_IA_64_VMS_NEEDED_IDENT:
7415           fixup.needed_ident = entry->d_un.d_val;
7416           break;
7417         case DT_NEEDED:
7418           fixup.needed = entry->d_un.d_val;
7419           break;
7420         case DT_IA_64_VMS_FIXUP_NEEDED:
7421           fixup.fixup_needed = entry->d_un.d_val;
7422           break;
7423         case DT_IA_64_VMS_FIXUP_RELA_CNT:
7424           fixup.fixup_rela_cnt = entry->d_un.d_val;
7425           break;
7426         case DT_IA_64_VMS_FIXUP_RELA_OFF:
7427           fixup.fixup_rela_off = entry->d_un.d_val;
7428           if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
7429 	    res = FALSE;
7430           break;
7431         case DT_IA_64_VMS_IMG_RELA_CNT:
7432 	  imgrela.img_rela_cnt = entry->d_un.d_val;
7433           break;
7434         case DT_IA_64_VMS_IMG_RELA_OFF:
7435 	  imgrela.img_rela_off = entry->d_un.d_val;
7436           if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
7437 	    res = FALSE;
7438           break;
7439 
7440         default:
7441           break;
7442 	}
7443     }
7444 
7445   free (strtab);
7446 
7447   return res;
7448 }
7449 
7450 static struct
7451 {
7452   const char * name;
7453   int reloc;
7454   int size;
7455   int rela;
7456 }
7457   dynamic_relocations [] =
7458 {
7459   { "REL", DT_REL, DT_RELSZ, FALSE },
7460   { "RELA", DT_RELA, DT_RELASZ, TRUE },
7461   { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
7462 };
7463 
7464 /* Process the reloc section.  */
7465 
7466 static bfd_boolean
process_relocs(Filedata * filedata)7467 process_relocs (Filedata * filedata)
7468 {
7469   unsigned long rel_size;
7470   unsigned long rel_offset;
7471 
7472   if (!do_reloc)
7473     return TRUE;
7474 
7475   if (do_using_dynamic)
7476     {
7477       int          is_rela;
7478       const char * name;
7479       bfd_boolean  has_dynamic_reloc;
7480       unsigned int i;
7481 
7482       has_dynamic_reloc = FALSE;
7483 
7484       for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7485 	{
7486 	  is_rela = dynamic_relocations [i].rela;
7487 	  name = dynamic_relocations [i].name;
7488 	  rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
7489 	  rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
7490 
7491 	  if (rel_size)
7492 	    has_dynamic_reloc = TRUE;
7493 
7494 	  if (is_rela == UNKNOWN)
7495 	    {
7496 	      if (dynamic_relocations [i].reloc == DT_JMPREL)
7497 		switch (filedata->dynamic_info[DT_PLTREL])
7498 		  {
7499 		  case DT_REL:
7500 		    is_rela = FALSE;
7501 		    break;
7502 		  case DT_RELA:
7503 		    is_rela = TRUE;
7504 		    break;
7505 		  }
7506 	    }
7507 
7508 	  if (rel_size)
7509 	    {
7510 	      printf
7511 		(_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7512 		 name, rel_offset, rel_size);
7513 
7514 	      dump_relocations (filedata,
7515 				offset_from_vma (filedata, rel_offset, rel_size),
7516 				rel_size,
7517 				filedata->dynamic_symbols,
7518 				filedata->num_dynamic_syms,
7519 				filedata->dynamic_strings,
7520 				filedata->dynamic_strings_length,
7521 				is_rela, TRUE /* is_dynamic */);
7522 	    }
7523 	}
7524 
7525       if (is_ia64_vms (filedata))
7526         if (process_ia64_vms_dynamic_relocs (filedata))
7527 	  has_dynamic_reloc = TRUE;
7528 
7529       if (! has_dynamic_reloc)
7530 	printf (_("\nThere are no dynamic relocations in this file.\n"));
7531     }
7532   else
7533     {
7534       Elf_Internal_Shdr * section;
7535       unsigned long i;
7536       bfd_boolean found = FALSE;
7537 
7538       for (i = 0, section = filedata->section_headers;
7539 	   i < filedata->file_header.e_shnum;
7540 	   i++, section++)
7541 	{
7542 	  if (   section->sh_type != SHT_RELA
7543 	      && section->sh_type != SHT_REL)
7544 	    continue;
7545 
7546 	  rel_offset = section->sh_offset;
7547 	  rel_size   = section->sh_size;
7548 
7549 	  if (rel_size)
7550 	    {
7551 	      int is_rela;
7552 	      unsigned long num_rela;
7553 
7554 	      printf (_("\nRelocation section "));
7555 
7556 	      if (filedata->string_table == NULL)
7557 		printf ("%d", section->sh_name);
7558 	      else
7559 		printf ("'%s'", printable_section_name (filedata, section));
7560 
7561 	      num_rela = rel_size / section->sh_entsize;
7562 	      printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7563 				" at offset 0x%lx contains %lu entries:\n",
7564 				num_rela),
7565 		      rel_offset, num_rela);
7566 
7567 	      is_rela = section->sh_type == SHT_RELA;
7568 
7569 	      if (section->sh_link != 0
7570 		  && section->sh_link < filedata->file_header.e_shnum)
7571 		{
7572 		  Elf_Internal_Shdr * symsec;
7573 		  Elf_Internal_Sym *  symtab;
7574 		  unsigned long nsyms;
7575 		  unsigned long strtablen = 0;
7576 		  char * strtab = NULL;
7577 
7578 		  symsec = filedata->section_headers + section->sh_link;
7579 		  if (symsec->sh_type != SHT_SYMTAB
7580 		      && symsec->sh_type != SHT_DYNSYM)
7581                     continue;
7582 
7583 		  if (!get_symtab (filedata, symsec,
7584 				   &symtab, &nsyms, &strtab, &strtablen))
7585 		    continue;
7586 
7587 		  dump_relocations (filedata, rel_offset, rel_size,
7588 				    symtab, nsyms, strtab, strtablen,
7589 				    is_rela,
7590 				    symsec->sh_type == SHT_DYNSYM);
7591 		  free (strtab);
7592 		  free (symtab);
7593 		}
7594 	      else
7595 		dump_relocations (filedata, rel_offset, rel_size,
7596 				  NULL, 0, NULL, 0, is_rela,
7597 				  FALSE /* is_dynamic */);
7598 
7599 	      found = TRUE;
7600 	    }
7601 	}
7602 
7603       if (! found)
7604 	{
7605 	  /* Users sometimes forget the -D option, so try to be helpful.  */
7606 	  for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7607 	    {
7608 	      if (filedata->dynamic_info[dynamic_relocations [i].size])
7609 		{
7610 		  printf (_("\nThere are no static relocations in this file."));
7611 		  printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
7612 
7613 		  break;
7614 		}
7615 	    }
7616 	  if (i == ARRAY_SIZE (dynamic_relocations))
7617 	    printf (_("\nThere are no relocations in this file.\n"));
7618 	}
7619     }
7620 
7621   return TRUE;
7622 }
7623 
7624 /* An absolute address consists of a section and an offset.  If the
7625    section is NULL, the offset itself is the address, otherwise, the
7626    address equals to LOAD_ADDRESS(section) + offset.  */
7627 
7628 struct absaddr
7629 {
7630   unsigned short section;
7631   bfd_vma offset;
7632 };
7633 
7634 /* Find the nearest symbol at or below ADDR.  Returns the symbol
7635    name, if found, and the offset from the symbol to ADDR.  */
7636 
7637 static void
find_symbol_for_address(Filedata * filedata,Elf_Internal_Sym * symtab,unsigned long nsyms,const char * strtab,unsigned long strtab_size,struct absaddr addr,const char ** symname,bfd_vma * offset)7638 find_symbol_for_address (Filedata *          filedata,
7639 			 Elf_Internal_Sym *  symtab,
7640 			 unsigned long       nsyms,
7641 			 const char *        strtab,
7642 			 unsigned long       strtab_size,
7643 			 struct absaddr      addr,
7644 			 const char **       symname,
7645 			 bfd_vma *           offset)
7646 {
7647   bfd_vma dist = 0x100000;
7648   Elf_Internal_Sym * sym;
7649   Elf_Internal_Sym * beg;
7650   Elf_Internal_Sym * end;
7651   Elf_Internal_Sym * best = NULL;
7652 
7653   REMOVE_ARCH_BITS (addr.offset);
7654   beg = symtab;
7655   end = symtab + nsyms;
7656 
7657   while (beg < end)
7658     {
7659       bfd_vma value;
7660 
7661       sym = beg + (end - beg) / 2;
7662 
7663       value = sym->st_value;
7664       REMOVE_ARCH_BITS (value);
7665 
7666       if (sym->st_name != 0
7667 	  && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
7668 	  && addr.offset >= value
7669 	  && addr.offset - value < dist)
7670 	{
7671 	  best = sym;
7672 	  dist = addr.offset - value;
7673 	  if (!dist)
7674 	    break;
7675 	}
7676 
7677       if (addr.offset < value)
7678 	end = sym;
7679       else
7680 	beg = sym + 1;
7681     }
7682 
7683   if (best)
7684     {
7685       *symname = (best->st_name >= strtab_size
7686 		  ? _("<corrupt>") : strtab + best->st_name);
7687       *offset = dist;
7688       return;
7689     }
7690 
7691   *symname = NULL;
7692   *offset = addr.offset;
7693 }
7694 
7695 static /* signed */ int
symcmp(const void * p,const void * q)7696 symcmp (const void *p, const void *q)
7697 {
7698   Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
7699   Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
7700 
7701   return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
7702 }
7703 
7704 /* Process the unwind section.  */
7705 
7706 #include "unwind-ia64.h"
7707 
7708 struct ia64_unw_table_entry
7709 {
7710   struct absaddr start;
7711   struct absaddr end;
7712   struct absaddr info;
7713 };
7714 
7715 struct ia64_unw_aux_info
7716 {
7717   struct ia64_unw_table_entry * table;		/* Unwind table.  */
7718   unsigned long                 table_len;	/* Length of unwind table.  */
7719   unsigned char *               info;		/* Unwind info.  */
7720   unsigned long                 info_size;	/* Size of unwind info.  */
7721   bfd_vma                       info_addr;	/* Starting address of unwind info.  */
7722   bfd_vma                       seg_base;	/* Starting address of segment.  */
7723   Elf_Internal_Sym *            symtab;		/* The symbol table.  */
7724   unsigned long                 nsyms;		/* Number of symbols.  */
7725   Elf_Internal_Sym *            funtab;		/* Sorted table of STT_FUNC symbols.  */
7726   unsigned long                 nfuns;		/* Number of entries in funtab.  */
7727   char *                        strtab;		/* The string table.  */
7728   unsigned long                 strtab_size;	/* Size of string table.  */
7729 };
7730 
7731 static bfd_boolean
dump_ia64_unwind(Filedata * filedata,struct ia64_unw_aux_info * aux)7732 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
7733 {
7734   struct ia64_unw_table_entry * tp;
7735   unsigned long j, nfuns;
7736   int in_body;
7737   bfd_boolean res = TRUE;
7738 
7739   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7740   for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7741     if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7742       aux->funtab[nfuns++] = aux->symtab[j];
7743   aux->nfuns = nfuns;
7744   qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7745 
7746   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7747     {
7748       bfd_vma stamp;
7749       bfd_vma offset;
7750       const unsigned char * dp;
7751       const unsigned char * head;
7752       const unsigned char * end;
7753       const char * procname;
7754 
7755       find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
7756 			       aux->strtab_size, tp->start, &procname, &offset);
7757 
7758       fputs ("\n<", stdout);
7759 
7760       if (procname)
7761 	{
7762 	  fputs (procname, stdout);
7763 
7764 	  if (offset)
7765 	    printf ("+%lx", (unsigned long) offset);
7766 	}
7767 
7768       fputs (">: [", stdout);
7769       print_vma (tp->start.offset, PREFIX_HEX);
7770       fputc ('-', stdout);
7771       print_vma (tp->end.offset, PREFIX_HEX);
7772       printf ("], info at +0x%lx\n",
7773 	      (unsigned long) (tp->info.offset - aux->seg_base));
7774 
7775       /* PR 17531: file: 86232b32.  */
7776       if (aux->info == NULL)
7777 	continue;
7778 
7779       offset = tp->info.offset;
7780       if (tp->info.section)
7781 	{
7782 	  if (tp->info.section >= filedata->file_header.e_shnum)
7783 	    {
7784 	      warn (_("Invalid section %u in table entry %ld\n"),
7785 		    tp->info.section, (long) (tp - aux->table));
7786 	      res = FALSE;
7787 	      continue;
7788 	    }
7789 	  offset += filedata->section_headers[tp->info.section].sh_addr;
7790 	}
7791       offset -= aux->info_addr;
7792       /* PR 17531: file: 0997b4d1.  */
7793       if (offset >= aux->info_size
7794 	  || aux->info_size - offset < 8)
7795 	{
7796 	  warn (_("Invalid offset %lx in table entry %ld\n"),
7797 		(long) tp->info.offset, (long) (tp - aux->table));
7798 	  res = FALSE;
7799 	  continue;
7800 	}
7801 
7802       head = aux->info + offset;
7803       stamp = byte_get ((unsigned char *) head, sizeof (stamp));
7804 
7805       printf ("  v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
7806 	      (unsigned) UNW_VER (stamp),
7807 	      (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
7808 	      UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
7809 	      UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
7810 	      (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
7811 
7812       if (UNW_VER (stamp) != 1)
7813 	{
7814 	  printf (_("\tUnknown version.\n"));
7815 	  continue;
7816 	}
7817 
7818       in_body = 0;
7819       end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
7820       /* PR 17531: file: 16ceda89.  */
7821       if (end > aux->info + aux->info_size)
7822 	end = aux->info + aux->info_size;
7823       for (dp = head + 8; dp < end;)
7824 	dp = unw_decode (dp, in_body, & in_body, end);
7825     }
7826 
7827   free (aux->funtab);
7828 
7829   return res;
7830 }
7831 
7832 static bfd_boolean
slurp_ia64_unwind_table(Filedata * filedata,struct ia64_unw_aux_info * aux,Elf_Internal_Shdr * sec)7833 slurp_ia64_unwind_table (Filedata *                  filedata,
7834 			 struct ia64_unw_aux_info *  aux,
7835 			 Elf_Internal_Shdr *         sec)
7836 {
7837   unsigned long size, nrelas, i;
7838   Elf_Internal_Phdr * seg;
7839   struct ia64_unw_table_entry * tep;
7840   Elf_Internal_Shdr * relsec;
7841   Elf_Internal_Rela * rela;
7842   Elf_Internal_Rela * rp;
7843   unsigned char * table;
7844   unsigned char * tp;
7845   Elf_Internal_Sym * sym;
7846   const char * relname;
7847 
7848   aux->table_len = 0;
7849 
7850   /* First, find the starting address of the segment that includes
7851      this section: */
7852 
7853   if (filedata->file_header.e_phnum)
7854     {
7855       if (! get_program_headers (filedata))
7856 	  return FALSE;
7857 
7858       for (seg = filedata->program_headers;
7859 	   seg < filedata->program_headers + filedata->file_header.e_phnum;
7860 	   ++seg)
7861 	{
7862 	  if (seg->p_type != PT_LOAD)
7863 	    continue;
7864 
7865 	  if (sec->sh_addr >= seg->p_vaddr
7866 	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
7867 	    {
7868 	      aux->seg_base = seg->p_vaddr;
7869 	      break;
7870 	    }
7871 	}
7872     }
7873 
7874   /* Second, build the unwind table from the contents of the unwind section:  */
7875   size = sec->sh_size;
7876   table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
7877                                       _("unwind table"));
7878   if (!table)
7879     return FALSE;
7880 
7881   aux->table_len = size / (3 * eh_addr_size);
7882   aux->table = (struct ia64_unw_table_entry *)
7883     xcmalloc (aux->table_len, sizeof (aux->table[0]));
7884   tep = aux->table;
7885 
7886   for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
7887     {
7888       tep->start.section = SHN_UNDEF;
7889       tep->end.section   = SHN_UNDEF;
7890       tep->info.section  = SHN_UNDEF;
7891       tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7892       tep->end.offset   = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7893       tep->info.offset  = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7894       tep->start.offset += aux->seg_base;
7895       tep->end.offset   += aux->seg_base;
7896       tep->info.offset  += aux->seg_base;
7897     }
7898   free (table);
7899 
7900   /* Third, apply any relocations to the unwind table:  */
7901   for (relsec = filedata->section_headers;
7902        relsec < filedata->section_headers + filedata->file_header.e_shnum;
7903        ++relsec)
7904     {
7905       if (relsec->sh_type != SHT_RELA
7906 	  || relsec->sh_info >= filedata->file_header.e_shnum
7907 	  || filedata->section_headers + relsec->sh_info != sec)
7908 	continue;
7909 
7910       if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
7911 			      & rela, & nrelas))
7912 	{
7913 	  free (aux->table);
7914 	  aux->table = NULL;
7915 	  aux->table_len = 0;
7916 	  return FALSE;
7917 	}
7918 
7919       for (rp = rela; rp < rela + nrelas; ++rp)
7920 	{
7921 	  unsigned int sym_ndx;
7922 	  unsigned int r_type = get_reloc_type (filedata, rp->r_info);
7923 	  relname = elf_ia64_reloc_type (r_type);
7924 
7925 	  /* PR 17531: file: 9fa67536.  */
7926 	  if (relname == NULL)
7927 	    {
7928 	      warn (_("Skipping unknown relocation type: %u\n"), r_type);
7929 	      continue;
7930 	    }
7931 
7932 	  if (! const_strneq (relname, "R_IA64_SEGREL"))
7933 	    {
7934 	      warn (_("Skipping unexpected relocation type: %s\n"), relname);
7935 	      continue;
7936 	    }
7937 
7938 	  i = rp->r_offset / (3 * eh_addr_size);
7939 
7940 	  /* PR 17531: file: 5bc8d9bf.  */
7941 	  if (i >= aux->table_len)
7942 	    {
7943 	      warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
7944 	      continue;
7945 	    }
7946 
7947 	  sym_ndx = get_reloc_symindex (rp->r_info);
7948 	  if (sym_ndx >= aux->nsyms)
7949 	    {
7950 	      warn (_("Skipping reloc with invalid symbol index: %u\n"),
7951 		    sym_ndx);
7952 	      continue;
7953 	    }
7954 	  sym = aux->symtab + sym_ndx;
7955 
7956 	  switch (rp->r_offset / eh_addr_size % 3)
7957 	    {
7958 	    case 0:
7959 	      aux->table[i].start.section = sym->st_shndx;
7960 	      aux->table[i].start.offset  = rp->r_addend + sym->st_value;
7961 	      break;
7962 	    case 1:
7963 	      aux->table[i].end.section   = sym->st_shndx;
7964 	      aux->table[i].end.offset    = rp->r_addend + sym->st_value;
7965 	      break;
7966 	    case 2:
7967 	      aux->table[i].info.section  = sym->st_shndx;
7968 	      aux->table[i].info.offset   = rp->r_addend + sym->st_value;
7969 	      break;
7970 	    default:
7971 	      break;
7972 	    }
7973 	}
7974 
7975       free (rela);
7976     }
7977 
7978   return TRUE;
7979 }
7980 
7981 static bfd_boolean
ia64_process_unwind(Filedata * filedata)7982 ia64_process_unwind (Filedata * filedata)
7983 {
7984   Elf_Internal_Shdr * sec;
7985   Elf_Internal_Shdr * unwsec = NULL;
7986   unsigned long i, unwcount = 0, unwstart = 0;
7987   struct ia64_unw_aux_info aux;
7988   bfd_boolean res = TRUE;
7989 
7990   memset (& aux, 0, sizeof (aux));
7991 
7992   for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
7993     {
7994       if (sec->sh_type == SHT_SYMTAB)
7995 	{
7996 	  if (aux.symtab)
7997 	    {
7998 	      error (_("Multiple symbol tables encountered\n"));
7999 	      free (aux.symtab);
8000 	      aux.symtab = NULL;
8001 	      free (aux.strtab);
8002 	      aux.strtab = NULL;
8003 	    }
8004 	  if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8005 			   &aux.strtab, &aux.strtab_size))
8006 	    return FALSE;
8007 	}
8008       else if (sec->sh_type == SHT_IA_64_UNWIND)
8009 	unwcount++;
8010     }
8011 
8012   if (!unwcount)
8013     printf (_("\nThere are no unwind sections in this file.\n"));
8014 
8015   while (unwcount-- > 0)
8016     {
8017       char * suffix;
8018       size_t len, len2;
8019 
8020       for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8021 	   i < filedata->file_header.e_shnum; ++i, ++sec)
8022 	if (sec->sh_type == SHT_IA_64_UNWIND)
8023 	  {
8024 	    unwsec = sec;
8025 	    break;
8026 	  }
8027       /* We have already counted the number of SHT_IA64_UNWIND
8028 	 sections so the loop above should never fail.  */
8029       assert (unwsec != NULL);
8030 
8031       unwstart = i + 1;
8032       len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8033 
8034       if ((unwsec->sh_flags & SHF_GROUP) != 0)
8035 	{
8036 	  /* We need to find which section group it is in.  */
8037 	  struct group_list * g;
8038 
8039 	  if (filedata->section_headers_groups == NULL
8040 	      || filedata->section_headers_groups[i] == NULL)
8041 	    i = filedata->file_header.e_shnum;
8042 	  else
8043 	    {
8044 	      g = filedata->section_headers_groups[i]->root;
8045 
8046 	      for (; g != NULL; g = g->next)
8047 		{
8048 		  sec = filedata->section_headers + g->section_index;
8049 
8050 		  if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
8051 		    break;
8052 		}
8053 
8054 	      if (g == NULL)
8055 		i = filedata->file_header.e_shnum;
8056 	    }
8057 	}
8058       else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
8059 	{
8060 	  /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO.  */
8061 	  len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8062 	  suffix = SECTION_NAME (unwsec) + len;
8063 	  for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum;
8064 	       ++i, ++sec)
8065 	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
8066 		&& streq (SECTION_NAME (sec) + len2, suffix))
8067 	      break;
8068 	}
8069       else
8070 	{
8071 	  /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8072 	     .IA_64.unwind or BAR -> .IA_64.unwind_info.  */
8073 	  len = sizeof (ELF_STRING_ia64_unwind) - 1;
8074 	  len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8075 	  suffix = "";
8076 	  if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
8077 	    suffix = SECTION_NAME (unwsec) + len;
8078 	  for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum;
8079 	       ++i, ++sec)
8080 	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
8081 		&& streq (SECTION_NAME (sec) + len2, suffix))
8082 	      break;
8083 	}
8084 
8085       if (i == filedata->file_header.e_shnum)
8086 	{
8087 	  printf (_("\nCould not find unwind info section for "));
8088 
8089 	  if (filedata->string_table == NULL)
8090 	    printf ("%d", unwsec->sh_name);
8091 	  else
8092 	    printf ("'%s'", printable_section_name (filedata, unwsec));
8093 	}
8094       else
8095 	{
8096 	  aux.info_addr = sec->sh_addr;
8097 	  aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
8098 						 sec->sh_size,
8099 						 _("unwind info"));
8100 	  aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
8101 
8102 	  printf (_("\nUnwind section "));
8103 
8104 	  if (filedata->string_table == NULL)
8105 	    printf ("%d", unwsec->sh_name);
8106 	  else
8107 	    printf ("'%s'", printable_section_name (filedata, unwsec));
8108 
8109 	  printf (_(" at offset 0x%lx contains %lu entries:\n"),
8110 		  (unsigned long) unwsec->sh_offset,
8111 		  (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
8112 
8113 	  if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
8114 	      && aux.table_len > 0)
8115 	    dump_ia64_unwind (filedata, & aux);
8116 
8117 	  free ((char *) aux.table);
8118 	  free ((char *) aux.info);
8119 	  aux.table = NULL;
8120 	  aux.info = NULL;
8121 	}
8122     }
8123 
8124   free (aux.symtab);
8125   free ((char *) aux.strtab);
8126 
8127   return res;
8128 }
8129 
8130 struct hppa_unw_table_entry
8131 {
8132   struct absaddr start;
8133   struct absaddr end;
8134   unsigned int Cannot_unwind:1;			/* 0 */
8135   unsigned int Millicode:1;			/* 1 */
8136   unsigned int Millicode_save_sr0:1;		/* 2 */
8137   unsigned int Region_description:2;		/* 3..4 */
8138   unsigned int reserved1:1;			/* 5 */
8139   unsigned int Entry_SR:1;			/* 6 */
8140   unsigned int Entry_FR:4;     /* Number saved     7..10 */
8141   unsigned int Entry_GR:5;     /* Number saved     11..15 */
8142   unsigned int Args_stored:1;			/* 16 */
8143   unsigned int Variable_Frame:1;		/* 17 */
8144   unsigned int Separate_Package_Body:1;		/* 18 */
8145   unsigned int Frame_Extension_Millicode:1;	/* 19 */
8146   unsigned int Stack_Overflow_Check:1;		/* 20 */
8147   unsigned int Two_Instruction_SP_Increment:1;	/* 21 */
8148   unsigned int Ada_Region:1;			/* 22 */
8149   unsigned int cxx_info:1;			/* 23 */
8150   unsigned int cxx_try_catch:1;			/* 24 */
8151   unsigned int sched_entry_seq:1;		/* 25 */
8152   unsigned int reserved2:1;			/* 26 */
8153   unsigned int Save_SP:1;			/* 27 */
8154   unsigned int Save_RP:1;			/* 28 */
8155   unsigned int Save_MRP_in_frame:1;		/* 29 */
8156   unsigned int extn_ptr_defined:1;		/* 30 */
8157   unsigned int Cleanup_defined:1;		/* 31 */
8158 
8159   unsigned int MPE_XL_interrupt_marker:1;	/* 0 */
8160   unsigned int HP_UX_interrupt_marker:1;	/* 1 */
8161   unsigned int Large_frame:1;			/* 2 */
8162   unsigned int Pseudo_SP_Set:1;			/* 3 */
8163   unsigned int reserved4:1;			/* 4 */
8164   unsigned int Total_frame_size:27;		/* 5..31 */
8165 };
8166 
8167 struct hppa_unw_aux_info
8168 {
8169   struct hppa_unw_table_entry *  table;		/* Unwind table.  */
8170   unsigned long                  table_len;	/* Length of unwind table.  */
8171   bfd_vma                        seg_base;	/* Starting address of segment.  */
8172   Elf_Internal_Sym *             symtab;	/* The symbol table.  */
8173   unsigned long                  nsyms;		/* Number of symbols.  */
8174   Elf_Internal_Sym *             funtab;	/* Sorted table of STT_FUNC symbols.  */
8175   unsigned long                  nfuns;		/* Number of entries in funtab.  */
8176   char *                         strtab;	/* The string table.  */
8177   unsigned long                  strtab_size;	/* Size of string table.  */
8178 };
8179 
8180 static bfd_boolean
dump_hppa_unwind(Filedata * filedata,struct hppa_unw_aux_info * aux)8181 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
8182 {
8183   struct hppa_unw_table_entry * tp;
8184   unsigned long j, nfuns;
8185   bfd_boolean res = TRUE;
8186 
8187   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8188   for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8189     if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8190       aux->funtab[nfuns++] = aux->symtab[j];
8191   aux->nfuns = nfuns;
8192   qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8193 
8194   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8195     {
8196       bfd_vma offset;
8197       const char * procname;
8198 
8199       find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8200 			       aux->strtab_size, tp->start, &procname,
8201 			       &offset);
8202 
8203       fputs ("\n<", stdout);
8204 
8205       if (procname)
8206 	{
8207 	  fputs (procname, stdout);
8208 
8209 	  if (offset)
8210 	    printf ("+%lx", (unsigned long) offset);
8211 	}
8212 
8213       fputs (">: [", stdout);
8214       print_vma (tp->start.offset, PREFIX_HEX);
8215       fputc ('-', stdout);
8216       print_vma (tp->end.offset, PREFIX_HEX);
8217       printf ("]\n\t");
8218 
8219 #define PF(_m) if (tp->_m) printf (#_m " ");
8220 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
8221       PF(Cannot_unwind);
8222       PF(Millicode);
8223       PF(Millicode_save_sr0);
8224       /* PV(Region_description);  */
8225       PF(Entry_SR);
8226       PV(Entry_FR);
8227       PV(Entry_GR);
8228       PF(Args_stored);
8229       PF(Variable_Frame);
8230       PF(Separate_Package_Body);
8231       PF(Frame_Extension_Millicode);
8232       PF(Stack_Overflow_Check);
8233       PF(Two_Instruction_SP_Increment);
8234       PF(Ada_Region);
8235       PF(cxx_info);
8236       PF(cxx_try_catch);
8237       PF(sched_entry_seq);
8238       PF(Save_SP);
8239       PF(Save_RP);
8240       PF(Save_MRP_in_frame);
8241       PF(extn_ptr_defined);
8242       PF(Cleanup_defined);
8243       PF(MPE_XL_interrupt_marker);
8244       PF(HP_UX_interrupt_marker);
8245       PF(Large_frame);
8246       PF(Pseudo_SP_Set);
8247       PV(Total_frame_size);
8248 #undef PF
8249 #undef PV
8250     }
8251 
8252   printf ("\n");
8253 
8254   free (aux->funtab);
8255 
8256   return res;
8257 }
8258 
8259 static bfd_boolean
slurp_hppa_unwind_table(Filedata * filedata,struct hppa_unw_aux_info * aux,Elf_Internal_Shdr * sec)8260 slurp_hppa_unwind_table (Filedata *                  filedata,
8261 			 struct hppa_unw_aux_info *  aux,
8262 			 Elf_Internal_Shdr *         sec)
8263 {
8264   unsigned long size, unw_ent_size, nentries, nrelas, i;
8265   Elf_Internal_Phdr * seg;
8266   struct hppa_unw_table_entry * tep;
8267   Elf_Internal_Shdr * relsec;
8268   Elf_Internal_Rela * rela;
8269   Elf_Internal_Rela * rp;
8270   unsigned char * table;
8271   unsigned char * tp;
8272   Elf_Internal_Sym * sym;
8273   const char * relname;
8274 
8275   /* First, find the starting address of the segment that includes
8276      this section.  */
8277   if (filedata->file_header.e_phnum)
8278     {
8279       if (! get_program_headers (filedata))
8280 	return FALSE;
8281 
8282       for (seg = filedata->program_headers;
8283 	   seg < filedata->program_headers + filedata->file_header.e_phnum;
8284 	   ++seg)
8285 	{
8286 	  if (seg->p_type != PT_LOAD)
8287 	    continue;
8288 
8289 	  if (sec->sh_addr >= seg->p_vaddr
8290 	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8291 	    {
8292 	      aux->seg_base = seg->p_vaddr;
8293 	      break;
8294 	    }
8295 	}
8296     }
8297 
8298   /* Second, build the unwind table from the contents of the unwind
8299      section.  */
8300   size = sec->sh_size;
8301   table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8302                                       _("unwind table"));
8303   if (!table)
8304     return FALSE;
8305 
8306   unw_ent_size = 16;
8307   nentries = size / unw_ent_size;
8308   size = unw_ent_size * nentries;
8309 
8310   aux->table_len = nentries;
8311   tep = aux->table = (struct hppa_unw_table_entry *)
8312       xcmalloc (nentries, sizeof (aux->table[0]));
8313 
8314   for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
8315     {
8316       unsigned int tmp1, tmp2;
8317 
8318       tep->start.section = SHN_UNDEF;
8319       tep->end.section   = SHN_UNDEF;
8320 
8321       tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8322       tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8323       tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8324       tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8325 
8326       tep->start.offset += aux->seg_base;
8327       tep->end.offset   += aux->seg_base;
8328 
8329       tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8330       tep->Millicode = (tmp1 >> 30) & 0x1;
8331       tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8332       tep->Region_description = (tmp1 >> 27) & 0x3;
8333       tep->reserved1 = (tmp1 >> 26) & 0x1;
8334       tep->Entry_SR = (tmp1 >> 25) & 0x1;
8335       tep->Entry_FR = (tmp1 >> 21) & 0xf;
8336       tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8337       tep->Args_stored = (tmp1 >> 15) & 0x1;
8338       tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8339       tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8340       tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8341       tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8342       tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8343       tep->Ada_Region = (tmp1 >> 9) & 0x1;
8344       tep->cxx_info = (tmp1 >> 8) & 0x1;
8345       tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8346       tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8347       tep->reserved2 = (tmp1 >> 5) & 0x1;
8348       tep->Save_SP = (tmp1 >> 4) & 0x1;
8349       tep->Save_RP = (tmp1 >> 3) & 0x1;
8350       tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8351       tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8352       tep->Cleanup_defined = tmp1 & 0x1;
8353 
8354       tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8355       tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8356       tep->Large_frame = (tmp2 >> 29) & 0x1;
8357       tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8358       tep->reserved4 = (tmp2 >> 27) & 0x1;
8359       tep->Total_frame_size = tmp2 & 0x7ffffff;
8360     }
8361   free (table);
8362 
8363   /* Third, apply any relocations to the unwind table.  */
8364   for (relsec = filedata->section_headers;
8365        relsec < filedata->section_headers + filedata->file_header.e_shnum;
8366        ++relsec)
8367     {
8368       if (relsec->sh_type != SHT_RELA
8369 	  || relsec->sh_info >= filedata->file_header.e_shnum
8370 	  || filedata->section_headers + relsec->sh_info != sec)
8371 	continue;
8372 
8373       if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8374 			      & rela, & nrelas))
8375 	return FALSE;
8376 
8377       for (rp = rela; rp < rela + nrelas; ++rp)
8378 	{
8379 	  unsigned int sym_ndx;
8380 	  unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8381 	  relname = elf_hppa_reloc_type (r_type);
8382 
8383 	  if (relname == NULL)
8384 	    {
8385 	      warn (_("Skipping unknown relocation type: %u\n"), r_type);
8386 	      continue;
8387 	    }
8388 
8389 	  /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64.  */
8390 	  if (! const_strneq (relname, "R_PARISC_SEGREL"))
8391 	    {
8392 	      warn (_("Skipping unexpected relocation type: %s\n"), relname);
8393 	      continue;
8394 	    }
8395 
8396 	  i = rp->r_offset / unw_ent_size;
8397 	  if (i >= aux->table_len)
8398 	    {
8399 	      warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8400 	      continue;
8401 	    }
8402 
8403 	  sym_ndx = get_reloc_symindex (rp->r_info);
8404 	  if (sym_ndx >= aux->nsyms)
8405 	    {
8406 	      warn (_("Skipping reloc with invalid symbol index: %u\n"),
8407 		    sym_ndx);
8408 	      continue;
8409 	    }
8410 	  sym = aux->symtab + sym_ndx;
8411 
8412 	  switch ((rp->r_offset % unw_ent_size) / 4)
8413 	    {
8414 	    case 0:
8415 	      aux->table[i].start.section = sym->st_shndx;
8416 	      aux->table[i].start.offset  = sym->st_value + rp->r_addend;
8417 	      break;
8418 	    case 1:
8419 	      aux->table[i].end.section   = sym->st_shndx;
8420 	      aux->table[i].end.offset    = sym->st_value + rp->r_addend;
8421 	      break;
8422 	    default:
8423 	      break;
8424 	    }
8425 	}
8426 
8427       free (rela);
8428     }
8429 
8430   return TRUE;
8431 }
8432 
8433 static bfd_boolean
hppa_process_unwind(Filedata * filedata)8434 hppa_process_unwind (Filedata * filedata)
8435 {
8436   struct hppa_unw_aux_info aux;
8437   Elf_Internal_Shdr * unwsec = NULL;
8438   Elf_Internal_Shdr * sec;
8439   unsigned long i;
8440   bfd_boolean res = TRUE;
8441 
8442   if (filedata->string_table == NULL)
8443     return FALSE;
8444 
8445   memset (& aux, 0, sizeof (aux));
8446 
8447   for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8448     {
8449       if (sec->sh_type == SHT_SYMTAB)
8450 	{
8451 	  if (aux.symtab)
8452 	    {
8453 	      error (_("Multiple symbol tables encountered\n"));
8454 	      free (aux.symtab);
8455 	      aux.symtab = NULL;
8456 	      free (aux.strtab);
8457 	      aux.strtab = NULL;
8458 	    }
8459 	  if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8460 			   &aux.strtab, &aux.strtab_size))
8461 	    return FALSE;
8462 	}
8463       else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
8464 	unwsec = sec;
8465     }
8466 
8467   if (!unwsec)
8468     printf (_("\nThere are no unwind sections in this file.\n"));
8469 
8470   for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8471     {
8472       if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
8473 	{
8474 	  unsigned long num_unwind = sec->sh_size / 16;
8475 
8476 	  printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8477 			    "contains %lu entry:\n",
8478 			    "\nUnwind section '%s' at offset 0x%lx "
8479 			    "contains %lu entries:\n",
8480 			    num_unwind),
8481 		  printable_section_name (filedata, sec),
8482 		  (unsigned long) sec->sh_offset,
8483 		  num_unwind);
8484 
8485           if (! slurp_hppa_unwind_table (filedata, &aux, sec))
8486 	    res = FALSE;
8487 
8488 	  if (res && aux.table_len > 0)
8489 	    {
8490 	      if (! dump_hppa_unwind (filedata, &aux))
8491 		res = FALSE;
8492 	    }
8493 
8494 	  free ((char *) aux.table);
8495 	  aux.table = NULL;
8496 	}
8497     }
8498 
8499   free (aux.symtab);
8500   free ((char *) aux.strtab);
8501 
8502   return res;
8503 }
8504 
8505 struct arm_section
8506 {
8507   unsigned char *      data;		/* The unwind data.  */
8508   Elf_Internal_Shdr *  sec;		/* The cached unwind section header.  */
8509   Elf_Internal_Rela *  rela;		/* The cached relocations for this section.  */
8510   unsigned long        nrelas;		/* The number of relocations.  */
8511   unsigned int         rel_type;	/* REL or RELA ?  */
8512   Elf_Internal_Rela *  next_rela;	/* Cyclic pointer to the next reloc to process.  */
8513 };
8514 
8515 struct arm_unw_aux_info
8516 {
8517   Filedata *          filedata;		/* The file containing the unwind sections.  */
8518   Elf_Internal_Sym *  symtab;		/* The file's symbol table.  */
8519   unsigned long       nsyms;		/* Number of symbols.  */
8520   Elf_Internal_Sym *  funtab;		/* Sorted table of STT_FUNC symbols.  */
8521   unsigned long       nfuns;		/* Number of these symbols.  */
8522   char *              strtab;		/* The file's string table.  */
8523   unsigned long       strtab_size;	/* Size of string table.  */
8524 };
8525 
8526 static const char *
arm_print_vma_and_name(Filedata * filedata,struct arm_unw_aux_info * aux,bfd_vma fn,struct absaddr addr)8527 arm_print_vma_and_name (Filedata *                 filedata,
8528 			struct arm_unw_aux_info *  aux,
8529 			bfd_vma                    fn,
8530 			struct absaddr             addr)
8531 {
8532   const char *procname;
8533   bfd_vma sym_offset;
8534 
8535   if (addr.section == SHN_UNDEF)
8536     addr.offset = fn;
8537 
8538   find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8539 			   aux->strtab_size, addr, &procname,
8540 			   &sym_offset);
8541 
8542   print_vma (fn, PREFIX_HEX);
8543 
8544   if (procname)
8545     {
8546       fputs (" <", stdout);
8547       fputs (procname, stdout);
8548 
8549       if (sym_offset)
8550 	printf ("+0x%lx", (unsigned long) sym_offset);
8551       fputc ('>', stdout);
8552     }
8553 
8554   return procname;
8555 }
8556 
8557 static void
arm_free_section(struct arm_section * arm_sec)8558 arm_free_section (struct arm_section *arm_sec)
8559 {
8560   free (arm_sec->data);
8561   free (arm_sec->rela);
8562 }
8563 
8564 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
8565       cached section and install SEC instead.
8566    2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
8567       and return its valued in * WORDP, relocating if necessary.
8568    3) Update the NEXT_RELA field in ARM_SEC and store the section index and
8569       relocation's offset in ADDR.
8570    4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
8571       into the string table of the symbol associated with the reloc.  If no
8572       reloc was applied store -1 there.
8573    5) Return TRUE upon success, FALSE otherwise.  */
8574 
8575 static bfd_boolean
get_unwind_section_word(Filedata * filedata,struct arm_unw_aux_info * aux,struct arm_section * arm_sec,Elf_Internal_Shdr * sec,bfd_vma word_offset,unsigned int * wordp,struct absaddr * addr,bfd_vma * sym_name)8576 get_unwind_section_word (Filedata *                 filedata,
8577 			 struct arm_unw_aux_info *  aux,
8578 			 struct arm_section *       arm_sec,
8579 			 Elf_Internal_Shdr *        sec,
8580 			 bfd_vma 		    word_offset,
8581 			 unsigned int *             wordp,
8582 			 struct absaddr *           addr,
8583 			 bfd_vma *		    sym_name)
8584 {
8585   Elf_Internal_Rela *rp;
8586   Elf_Internal_Sym *sym;
8587   const char * relname;
8588   unsigned int word;
8589   bfd_boolean wrapped;
8590 
8591   if (sec == NULL || arm_sec == NULL)
8592     return FALSE;
8593 
8594   addr->section = SHN_UNDEF;
8595   addr->offset = 0;
8596 
8597   if (sym_name != NULL)
8598     *sym_name = (bfd_vma) -1;
8599 
8600   /* If necessary, update the section cache.  */
8601   if (sec != arm_sec->sec)
8602     {
8603       Elf_Internal_Shdr *relsec;
8604 
8605       arm_free_section (arm_sec);
8606 
8607       arm_sec->sec = sec;
8608       arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
8609 				sec->sh_size, _("unwind data"));
8610       arm_sec->rela = NULL;
8611       arm_sec->nrelas = 0;
8612 
8613       for (relsec = filedata->section_headers;
8614 	   relsec < filedata->section_headers + filedata->file_header.e_shnum;
8615 	   ++relsec)
8616 	{
8617 	  if (relsec->sh_info >= filedata->file_header.e_shnum
8618 	      || filedata->section_headers + relsec->sh_info != sec
8619 	      /* PR 15745: Check the section type as well.  */
8620 	      || (relsec->sh_type != SHT_REL
8621 		  && relsec->sh_type != SHT_RELA))
8622 	    continue;
8623 
8624 	  arm_sec->rel_type = relsec->sh_type;
8625 	  if (relsec->sh_type == SHT_REL)
8626 	    {
8627 	      if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
8628 				     relsec->sh_size,
8629 				     & arm_sec->rela, & arm_sec->nrelas))
8630 		return FALSE;
8631 	    }
8632 	  else /* relsec->sh_type == SHT_RELA */
8633 	    {
8634 	      if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
8635 				      relsec->sh_size,
8636 				      & arm_sec->rela, & arm_sec->nrelas))
8637 		return FALSE;
8638 	    }
8639 	  break;
8640 	}
8641 
8642       arm_sec->next_rela = arm_sec->rela;
8643     }
8644 
8645   /* If there is no unwind data we can do nothing.  */
8646   if (arm_sec->data == NULL)
8647     return FALSE;
8648 
8649   /* If the offset is invalid then fail.  */
8650   if (/* PR 21343 *//* PR 18879 */
8651       sec->sh_size < 4
8652       || word_offset > (sec->sh_size - 4)
8653       || ((bfd_signed_vma) word_offset) < 0)
8654     return FALSE;
8655 
8656   /* Get the word at the required offset.  */
8657   word = byte_get (arm_sec->data + word_offset, 4);
8658 
8659   /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128.  */
8660   if (arm_sec->rela == NULL)
8661     {
8662       * wordp = word;
8663       return TRUE;
8664     }
8665 
8666   /* Look through the relocs to find the one that applies to the provided offset.  */
8667   wrapped = FALSE;
8668   for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
8669     {
8670       bfd_vma prelval, offset;
8671 
8672       if (rp->r_offset > word_offset && !wrapped)
8673 	{
8674 	  rp = arm_sec->rela;
8675 	  wrapped = TRUE;
8676 	}
8677       if (rp->r_offset > word_offset)
8678 	break;
8679 
8680       if (rp->r_offset & 3)
8681 	{
8682 	  warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
8683 		(unsigned long) rp->r_offset);
8684 	  continue;
8685 	}
8686 
8687       if (rp->r_offset < word_offset)
8688 	continue;
8689 
8690       /* PR 17531: file: 027-161405-0.004  */
8691       if (aux->symtab == NULL)
8692 	continue;
8693 
8694       if (arm_sec->rel_type == SHT_REL)
8695 	{
8696 	  offset = word & 0x7fffffff;
8697 	  if (offset & 0x40000000)
8698 	    offset |= ~ (bfd_vma) 0x7fffffff;
8699 	}
8700       else if (arm_sec->rel_type == SHT_RELA)
8701 	offset = rp->r_addend;
8702       else
8703 	{
8704 	  error (_("Unknown section relocation type %d encountered\n"),
8705 		 arm_sec->rel_type);
8706 	  break;
8707 	}
8708 
8709       /* PR 17531 file: 027-1241568-0.004.  */
8710       if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
8711 	{
8712 	  error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
8713 		 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
8714 	  break;
8715 	}
8716 
8717       sym = aux->symtab + ELF32_R_SYM (rp->r_info);
8718       offset += sym->st_value;
8719       prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
8720 
8721       /* Check that we are processing the expected reloc type.  */
8722       if (filedata->file_header.e_machine == EM_ARM)
8723 	{
8724 	  relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
8725 	  if (relname == NULL)
8726 	    {
8727 	      warn (_("Skipping unknown ARM relocation type: %d\n"),
8728 		    (int) ELF32_R_TYPE (rp->r_info));
8729 	      continue;
8730 	    }
8731 
8732 	  if (streq (relname, "R_ARM_NONE"))
8733 	      continue;
8734 
8735 	  if (! streq (relname, "R_ARM_PREL31"))
8736 	    {
8737 	      warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
8738 	      continue;
8739 	    }
8740 	}
8741       else if (filedata->file_header.e_machine == EM_TI_C6000)
8742 	{
8743 	  relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
8744 	  if (relname == NULL)
8745 	    {
8746 	      warn (_("Skipping unknown C6000 relocation type: %d\n"),
8747 		    (int) ELF32_R_TYPE (rp->r_info));
8748 	      continue;
8749 	    }
8750 
8751 	  if (streq (relname, "R_C6000_NONE"))
8752 	    continue;
8753 
8754 	  if (! streq (relname, "R_C6000_PREL31"))
8755 	    {
8756 	      warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
8757 	      continue;
8758 	    }
8759 
8760 	  prelval >>= 1;
8761 	}
8762       else
8763 	{
8764 	  /* This function currently only supports ARM and TI unwinders.  */
8765 	  warn (_("Only TI and ARM unwinders are currently supported\n"));
8766 	  break;
8767 	}
8768 
8769       word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
8770       addr->section = sym->st_shndx;
8771       addr->offset = offset;
8772 
8773       if (sym_name)
8774 	* sym_name = sym->st_name;
8775       break;
8776     }
8777 
8778   *wordp = word;
8779   arm_sec->next_rela = rp;
8780 
8781   return TRUE;
8782 }
8783 
8784 static const char *tic6x_unwind_regnames[16] =
8785 {
8786   "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
8787   "A14", "A13", "A12", "A11", "A10",
8788   "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
8789 };
8790 
8791 static void
decode_tic6x_unwind_regmask(unsigned int mask)8792 decode_tic6x_unwind_regmask (unsigned int mask)
8793 {
8794   int i;
8795 
8796   for (i = 12; mask; mask >>= 1, i--)
8797     {
8798       if (mask & 1)
8799 	{
8800 	  fputs (tic6x_unwind_regnames[i], stdout);
8801 	  if (mask > 1)
8802 	    fputs (", ", stdout);
8803 	}
8804     }
8805 }
8806 
8807 #define ADVANCE							\
8808   if (remaining == 0 && more_words)				\
8809     {								\
8810       data_offset += 4;						\
8811       if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec,	\
8812 				     data_offset, & word, & addr, NULL))	\
8813 	return FALSE;						\
8814       remaining = 4;						\
8815       more_words--;						\
8816     }								\
8817 
8818 #define GET_OP(OP)			\
8819   ADVANCE;				\
8820   if (remaining)			\
8821     {					\
8822       remaining--;			\
8823       (OP) = word >> 24;		\
8824       word <<= 8;			\
8825     }					\
8826   else					\
8827     {					\
8828       printf (_("[Truncated opcode]\n"));	\
8829       return FALSE;			\
8830     }					\
8831   printf ("0x%02x ", OP)
8832 
8833 static bfd_boolean
decode_arm_unwind_bytecode(Filedata * filedata,struct arm_unw_aux_info * aux,unsigned int word,unsigned int remaining,unsigned int more_words,bfd_vma data_offset,Elf_Internal_Shdr * data_sec,struct arm_section * data_arm_sec)8834 decode_arm_unwind_bytecode (Filedata *                 filedata,
8835 			    struct arm_unw_aux_info *  aux,
8836 			    unsigned int               word,
8837 			    unsigned int               remaining,
8838 			    unsigned int               more_words,
8839 			    bfd_vma                    data_offset,
8840 			    Elf_Internal_Shdr *        data_sec,
8841 			    struct arm_section *       data_arm_sec)
8842 {
8843   struct absaddr addr;
8844   bfd_boolean res = TRUE;
8845 
8846   /* Decode the unwinding instructions.  */
8847   while (1)
8848     {
8849       unsigned int op, op2;
8850 
8851       ADVANCE;
8852       if (remaining == 0)
8853 	break;
8854       remaining--;
8855       op = word >> 24;
8856       word <<= 8;
8857 
8858       printf ("  0x%02x ", op);
8859 
8860       if ((op & 0xc0) == 0x00)
8861 	{
8862 	  int offset = ((op & 0x3f) << 2) + 4;
8863 
8864 	  printf ("     vsp = vsp + %d", offset);
8865 	}
8866       else if ((op & 0xc0) == 0x40)
8867 	{
8868 	  int offset = ((op & 0x3f) << 2) + 4;
8869 
8870 	  printf ("     vsp = vsp - %d", offset);
8871 	}
8872       else if ((op & 0xf0) == 0x80)
8873 	{
8874 	  GET_OP (op2);
8875 	  if (op == 0x80 && op2 == 0)
8876 	    printf (_("Refuse to unwind"));
8877 	  else
8878 	    {
8879 	      unsigned int mask = ((op & 0x0f) << 8) | op2;
8880 	      bfd_boolean first = TRUE;
8881 	      int i;
8882 
8883 	      printf ("pop {");
8884 	      for (i = 0; i < 12; i++)
8885 		if (mask & (1 << i))
8886 		  {
8887 		    if (first)
8888 		      first = FALSE;
8889 		    else
8890 		      printf (", ");
8891 		    printf ("r%d", 4 + i);
8892 		  }
8893 	      printf ("}");
8894 	    }
8895 	}
8896       else if ((op & 0xf0) == 0x90)
8897 	{
8898 	  if (op == 0x9d || op == 0x9f)
8899 	    printf (_("     [Reserved]"));
8900 	  else
8901 	    printf ("     vsp = r%d", op & 0x0f);
8902 	}
8903       else if ((op & 0xf0) == 0xa0)
8904 	{
8905 	  int end = 4 + (op & 0x07);
8906 	  bfd_boolean first = TRUE;
8907 	  int i;
8908 
8909 	  printf ("     pop {");
8910 	  for (i = 4; i <= end; i++)
8911 	    {
8912 	      if (first)
8913 		first = FALSE;
8914 	      else
8915 		printf (", ");
8916 	      printf ("r%d", i);
8917 	    }
8918 	  if (op & 0x08)
8919 	    {
8920 	      if (!first)
8921 		printf (", ");
8922 	      printf ("r14");
8923 	    }
8924 	  printf ("}");
8925 	}
8926       else if (op == 0xb0)
8927 	printf (_("     finish"));
8928       else if (op == 0xb1)
8929 	{
8930 	  GET_OP (op2);
8931 	  if (op2 == 0 || (op2 & 0xf0) != 0)
8932 	    printf (_("[Spare]"));
8933 	  else
8934 	    {
8935 	      unsigned int mask = op2 & 0x0f;
8936 	      bfd_boolean first = TRUE;
8937 	      int i;
8938 
8939 	      printf ("pop {");
8940 	      for (i = 0; i < 12; i++)
8941 		if (mask & (1 << i))
8942 		  {
8943 		    if (first)
8944 		      first = FALSE;
8945 		    else
8946 		      printf (", ");
8947 		    printf ("r%d", i);
8948 		  }
8949 	      printf ("}");
8950 	    }
8951 	}
8952       else if (op == 0xb2)
8953 	{
8954 	  unsigned char buf[9];
8955 	  unsigned int i, len;
8956 	  unsigned long offset;
8957 
8958 	  for (i = 0; i < sizeof (buf); i++)
8959 	    {
8960 	      GET_OP (buf[i]);
8961 	      if ((buf[i] & 0x80) == 0)
8962 		break;
8963 	    }
8964 	  if (i == sizeof (buf))
8965 	    {
8966 	      error (_("corrupt change to vsp\n"));
8967 	      res = FALSE;
8968 	    }
8969 	  else
8970 	    {
8971 	      offset = read_leb128 (buf, buf + i + 1, FALSE, &len, NULL);
8972 	      assert (len == i + 1);
8973 	      offset = offset * 4 + 0x204;
8974 	      printf ("vsp = vsp + %ld", offset);
8975 	    }
8976 	}
8977       else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
8978 	{
8979 	  unsigned int first, last;
8980 
8981 	  GET_OP (op2);
8982 	  first = op2 >> 4;
8983 	  last = op2 & 0x0f;
8984 	  if (op == 0xc8)
8985 	    first = first + 16;
8986 	  printf ("pop {D%d", first);
8987 	  if (last)
8988 	    printf ("-D%d", first + last);
8989 	  printf ("}");
8990 	}
8991       else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
8992 	{
8993 	  unsigned int count = op & 0x07;
8994 
8995 	  printf ("pop {D8");
8996 	  if (count)
8997 	    printf ("-D%d", 8 + count);
8998 	  printf ("}");
8999 	}
9000       else if (op >= 0xc0 && op <= 0xc5)
9001 	{
9002 	  unsigned int count = op & 0x07;
9003 
9004 	  printf ("     pop {wR10");
9005 	  if (count)
9006 	    printf ("-wR%d", 10 + count);
9007 	  printf ("}");
9008 	}
9009       else if (op == 0xc6)
9010 	{
9011 	  unsigned int first, last;
9012 
9013 	  GET_OP (op2);
9014 	  first = op2 >> 4;
9015 	  last = op2 & 0x0f;
9016 	  printf ("pop {wR%d", first);
9017 	  if (last)
9018 	    printf ("-wR%d", first + last);
9019 	  printf ("}");
9020 	}
9021       else if (op == 0xc7)
9022 	{
9023 	  GET_OP (op2);
9024 	  if (op2 == 0 || (op2 & 0xf0) != 0)
9025 	    printf (_("[Spare]"));
9026 	  else
9027 	    {
9028 	      unsigned int mask = op2 & 0x0f;
9029 	      bfd_boolean first = TRUE;
9030 	      int i;
9031 
9032 	      printf ("pop {");
9033 	      for (i = 0; i < 4; i++)
9034 		if (mask & (1 << i))
9035 		  {
9036 		    if (first)
9037 		      first = FALSE;
9038 		    else
9039 		      printf (", ");
9040 		    printf ("wCGR%d", i);
9041 		  }
9042 	      printf ("}");
9043 	    }
9044 	}
9045       else
9046 	{
9047 	  printf (_("     [unsupported opcode]"));
9048 	  res = FALSE;
9049 	}
9050 
9051       printf ("\n");
9052     }
9053 
9054   return res;
9055 }
9056 
9057 static bfd_boolean
decode_tic6x_unwind_bytecode(Filedata * filedata,struct arm_unw_aux_info * aux,unsigned int word,unsigned int remaining,unsigned int more_words,bfd_vma data_offset,Elf_Internal_Shdr * data_sec,struct arm_section * data_arm_sec)9058 decode_tic6x_unwind_bytecode (Filedata *                 filedata,
9059 			      struct arm_unw_aux_info *  aux,
9060 			      unsigned int               word,
9061 			      unsigned int               remaining,
9062 			      unsigned int               more_words,
9063 			      bfd_vma                    data_offset,
9064 			      Elf_Internal_Shdr *        data_sec,
9065 			      struct arm_section *       data_arm_sec)
9066 {
9067   struct absaddr addr;
9068 
9069   /* Decode the unwinding instructions.  */
9070   while (1)
9071     {
9072       unsigned int op, op2;
9073 
9074       ADVANCE;
9075       if (remaining == 0)
9076 	break;
9077       remaining--;
9078       op = word >> 24;
9079       word <<= 8;
9080 
9081       printf ("  0x%02x ", op);
9082 
9083       if ((op & 0xc0) == 0x00)
9084 	{
9085 	  int offset = ((op & 0x3f) << 3) + 8;
9086 	  printf ("     sp = sp + %d", offset);
9087 	}
9088       else if ((op & 0xc0) == 0x80)
9089 	{
9090 	  GET_OP (op2);
9091 	  if (op == 0x80 && op2 == 0)
9092 	    printf (_("Refuse to unwind"));
9093 	  else
9094 	    {
9095 	      unsigned int mask = ((op & 0x1f) << 8) | op2;
9096 	      if (op & 0x20)
9097 		printf ("pop compact {");
9098 	      else
9099 		printf ("pop {");
9100 
9101 	      decode_tic6x_unwind_regmask (mask);
9102 	      printf("}");
9103 	    }
9104 	}
9105       else if ((op & 0xf0) == 0xc0)
9106 	{
9107 	  unsigned int reg;
9108 	  unsigned int nregs;
9109 	  unsigned int i;
9110 	  const char *name;
9111 	  struct
9112 	  {
9113 	    unsigned int offset;
9114 	    unsigned int reg;
9115 	  } regpos[16];
9116 
9117 	  /* Scan entire instruction first so that GET_OP output is not
9118 	     interleaved with disassembly.  */
9119 	  nregs = 0;
9120 	  for (i = 0; nregs < (op & 0xf); i++)
9121 	    {
9122 	      GET_OP (op2);
9123 	      reg = op2 >> 4;
9124 	      if (reg != 0xf)
9125 		{
9126 		  regpos[nregs].offset = i * 2;
9127 		  regpos[nregs].reg = reg;
9128 		  nregs++;
9129 		}
9130 
9131 	      reg = op2 & 0xf;
9132 	      if (reg != 0xf)
9133 		{
9134 		  regpos[nregs].offset = i * 2 + 1;
9135 		  regpos[nregs].reg = reg;
9136 		  nregs++;
9137 		}
9138 	    }
9139 
9140 	  printf (_("pop frame {"));
9141 	  if (nregs == 0)
9142 	    {
9143 	      printf (_("*corrupt* - no registers specified"));
9144 	    }
9145 	  else
9146 	    {
9147 	      reg = nregs - 1;
9148 	      for (i = i * 2; i > 0; i--)
9149 		{
9150 		  if (regpos[reg].offset == i - 1)
9151 		    {
9152 		      name = tic6x_unwind_regnames[regpos[reg].reg];
9153 		      if (reg > 0)
9154 			reg--;
9155 		    }
9156 		  else
9157 		    name = _("[pad]");
9158 
9159 		  fputs (name, stdout);
9160 		  if (i > 1)
9161 		    printf (", ");
9162 		}
9163 	    }
9164 
9165 	  printf ("}");
9166 	}
9167       else if (op == 0xd0)
9168 	printf ("     MOV FP, SP");
9169       else if (op == 0xd1)
9170 	printf ("     __c6xabi_pop_rts");
9171       else if (op == 0xd2)
9172 	{
9173 	  unsigned char buf[9];
9174 	  unsigned int i, len;
9175 	  unsigned long offset;
9176 
9177 	  for (i = 0; i < sizeof (buf); i++)
9178 	    {
9179 	      GET_OP (buf[i]);
9180 	      if ((buf[i] & 0x80) == 0)
9181 		break;
9182 	    }
9183 	  /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2.  */
9184 	  if (i == sizeof (buf))
9185 	    {
9186 	      warn (_("Corrupt stack pointer adjustment detected\n"));
9187 	      return FALSE;
9188 	    }
9189 
9190 	  offset = read_leb128 (buf, buf + i + 1, FALSE, &len, NULL);
9191 	  assert (len == i + 1);
9192 	  offset = offset * 8 + 0x408;
9193 	  printf (_("sp = sp + %ld"), offset);
9194 	}
9195       else if ((op & 0xf0) == 0xe0)
9196 	{
9197 	  if ((op & 0x0f) == 7)
9198 	    printf ("     RETURN");
9199 	  else
9200 	    printf ("     MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9201 	}
9202       else
9203 	{
9204 	  printf (_("     [unsupported opcode]"));
9205 	}
9206       putchar ('\n');
9207     }
9208 
9209   return TRUE;
9210 }
9211 
9212 static bfd_vma
arm_expand_prel31(Filedata * filedata,bfd_vma word,bfd_vma where)9213 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
9214 {
9215   bfd_vma offset;
9216 
9217   offset = word & 0x7fffffff;
9218   if (offset & 0x40000000)
9219     offset |= ~ (bfd_vma) 0x7fffffff;
9220 
9221   if (filedata->file_header.e_machine == EM_TI_C6000)
9222     offset <<= 1;
9223 
9224   return offset + where;
9225 }
9226 
9227 static bfd_boolean
decode_arm_unwind(Filedata * filedata,struct arm_unw_aux_info * aux,unsigned int word,unsigned int remaining,bfd_vma data_offset,Elf_Internal_Shdr * data_sec,struct arm_section * data_arm_sec)9228 decode_arm_unwind (Filedata *                 filedata,
9229 		   struct arm_unw_aux_info *  aux,
9230 		   unsigned int               word,
9231 		   unsigned int               remaining,
9232 		   bfd_vma                    data_offset,
9233 		   Elf_Internal_Shdr *        data_sec,
9234 		   struct arm_section *       data_arm_sec)
9235 {
9236   int per_index;
9237   unsigned int more_words = 0;
9238   struct absaddr addr;
9239   bfd_vma sym_name = (bfd_vma) -1;
9240   bfd_boolean res = TRUE;
9241 
9242   if (remaining == 0)
9243     {
9244       /* Fetch the first word.
9245 	 Note - when decoding an object file the address extracted
9246 	 here will always be 0.  So we also pass in the sym_name
9247 	 parameter so that we can find the symbol associated with
9248 	 the personality routine.  */
9249       if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
9250 				     & word, & addr, & sym_name))
9251 	return FALSE;
9252 
9253       remaining = 4;
9254     }
9255   else
9256     {
9257       addr.section = SHN_UNDEF;
9258       addr.offset = 0;
9259     }
9260 
9261   if ((word & 0x80000000) == 0)
9262     {
9263       /* Expand prel31 for personality routine.  */
9264       bfd_vma fn;
9265       const char *procname;
9266 
9267       fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
9268       printf (_("  Personality routine: "));
9269       if (fn == 0
9270 	  && addr.section == SHN_UNDEF && addr.offset == 0
9271 	  && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9272 	{
9273 	  procname = aux->strtab + sym_name;
9274 	  print_vma (fn, PREFIX_HEX);
9275 	  if (procname)
9276 	    {
9277 	      fputs (" <", stdout);
9278 	      fputs (procname, stdout);
9279 	      fputc ('>', stdout);
9280 	    }
9281 	}
9282       else
9283 	procname = arm_print_vma_and_name (filedata, aux, fn, addr);
9284       fputc ('\n', stdout);
9285 
9286       /* The GCC personality routines use the standard compact
9287 	 encoding, starting with one byte giving the number of
9288 	 words.  */
9289       if (procname != NULL
9290 	  && (const_strneq (procname, "__gcc_personality_v0")
9291 	      || const_strneq (procname, "__gxx_personality_v0")
9292 	      || const_strneq (procname, "__gcj_personality_v0")
9293 	      || const_strneq (procname, "__gnu_objc_personality_v0")))
9294 	{
9295 	  remaining = 0;
9296 	  more_words = 1;
9297 	  ADVANCE;
9298 	  if (!remaining)
9299 	    {
9300 	      printf (_("  [Truncated data]\n"));
9301 	      return FALSE;
9302 	    }
9303 	  more_words = word >> 24;
9304 	  word <<= 8;
9305 	  remaining--;
9306 	  per_index = -1;
9307 	}
9308       else
9309 	return TRUE;
9310     }
9311   else
9312     {
9313       /* ARM EHABI Section 6.3:
9314 
9315 	 An exception-handling table entry for the compact model looks like:
9316 
9317            31 30-28 27-24 23-0
9318 	   -- ----- ----- ----
9319             1   0   index Data for personalityRoutine[index]    */
9320 
9321       if (filedata->file_header.e_machine == EM_ARM
9322 	  && (word & 0x70000000))
9323 	{
9324 	  warn (_("Corrupt ARM compact model table entry: %x \n"), word);
9325 	  res = FALSE;
9326 	}
9327 
9328       per_index = (word >> 24) & 0x7f;
9329       printf (_("  Compact model index: %d\n"), per_index);
9330       if (per_index == 0)
9331 	{
9332 	  more_words = 0;
9333 	  word <<= 8;
9334 	  remaining--;
9335 	}
9336       else if (per_index < 3)
9337 	{
9338 	  more_words = (word >> 16) & 0xff;
9339 	  word <<= 16;
9340 	  remaining -= 2;
9341 	}
9342     }
9343 
9344   switch (filedata->file_header.e_machine)
9345     {
9346     case EM_ARM:
9347       if (per_index < 3)
9348 	{
9349 	  if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
9350 					    data_offset, data_sec, data_arm_sec))
9351 	    res = FALSE;
9352 	}
9353       else
9354 	{
9355 	  warn (_("Unknown ARM compact model index encountered\n"));
9356 	  printf (_("  [reserved]\n"));
9357 	  res = FALSE;
9358 	}
9359       break;
9360 
9361     case EM_TI_C6000:
9362       if (per_index < 3)
9363 	{
9364 	  if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
9365 					      data_offset, data_sec, data_arm_sec))
9366 	    res = FALSE;
9367 	}
9368       else if (per_index < 5)
9369 	{
9370 	  if (((word >> 17) & 0x7f) == 0x7f)
9371 	    printf (_("  Restore stack from frame pointer\n"));
9372 	  else
9373 	    printf (_("  Stack increment %d\n"), (word >> 14) & 0x1fc);
9374 	  printf (_("  Registers restored: "));
9375 	  if (per_index == 4)
9376 	    printf (" (compact) ");
9377 	  decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9378 	  putchar ('\n');
9379 	  printf (_("  Return register: %s\n"),
9380 		  tic6x_unwind_regnames[word & 0xf]);
9381 	}
9382       else
9383 	printf (_("  [reserved (%d)]\n"), per_index);
9384       break;
9385 
9386     default:
9387       error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
9388 	     filedata->file_header.e_machine);
9389       res = FALSE;
9390     }
9391 
9392   /* Decode the descriptors.  Not implemented.  */
9393 
9394   return res;
9395 }
9396 
9397 static bfd_boolean
dump_arm_unwind(Filedata * filedata,struct arm_unw_aux_info * aux,Elf_Internal_Shdr * exidx_sec)9398 dump_arm_unwind (Filedata *                 filedata,
9399 		 struct arm_unw_aux_info *  aux,
9400 		 Elf_Internal_Shdr *        exidx_sec)
9401 {
9402   struct arm_section exidx_arm_sec, extab_arm_sec;
9403   unsigned int i, exidx_len;
9404   unsigned long j, nfuns;
9405   bfd_boolean res = TRUE;
9406 
9407   memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9408   memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9409   exidx_len = exidx_sec->sh_size / 8;
9410 
9411   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9412   for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9413     if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9414       aux->funtab[nfuns++] = aux->symtab[j];
9415   aux->nfuns = nfuns;
9416   qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9417 
9418   for (i = 0; i < exidx_len; i++)
9419     {
9420       unsigned int exidx_fn, exidx_entry;
9421       struct absaddr fn_addr, entry_addr;
9422       bfd_vma fn;
9423 
9424       fputc ('\n', stdout);
9425 
9426       if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9427 				     8 * i, & exidx_fn, & fn_addr, NULL)
9428 	  || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9429 					8 * i + 4, & exidx_entry, & entry_addr, NULL))
9430 	{
9431 	  free (aux->funtab);
9432 	  arm_free_section (& exidx_arm_sec);
9433 	  arm_free_section (& extab_arm_sec);
9434 	  return FALSE;
9435 	}
9436 
9437       /* ARM EHABI, Section 5:
9438 	 An index table entry consists of 2 words.
9439          The first word contains a prel31 offset to the start of a function, with bit 31 clear.  */
9440       if (exidx_fn & 0x80000000)
9441 	{
9442 	  warn (_("corrupt index table entry: %x\n"), exidx_fn);
9443 	  res = FALSE;
9444 	}
9445 
9446       fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
9447 
9448       arm_print_vma_and_name (filedata, aux, fn, fn_addr);
9449       fputs (": ", stdout);
9450 
9451       if (exidx_entry == 1)
9452 	{
9453 	  print_vma (exidx_entry, PREFIX_HEX);
9454 	  fputs (" [cantunwind]\n", stdout);
9455 	}
9456       else if (exidx_entry & 0x80000000)
9457 	{
9458 	  print_vma (exidx_entry, PREFIX_HEX);
9459 	  fputc ('\n', stdout);
9460 	  decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
9461 	}
9462       else
9463 	{
9464 	  bfd_vma table, table_offset = 0;
9465 	  Elf_Internal_Shdr *table_sec;
9466 
9467 	  fputs ("@", stdout);
9468 	  table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
9469 	  print_vma (table, PREFIX_HEX);
9470 	  printf ("\n");
9471 
9472 	  /* Locate the matching .ARM.extab.  */
9473 	  if (entry_addr.section != SHN_UNDEF
9474 	      && entry_addr.section < filedata->file_header.e_shnum)
9475 	    {
9476 	      table_sec = filedata->section_headers + entry_addr.section;
9477 	      table_offset = entry_addr.offset;
9478 	      /* PR 18879 */
9479 	      if (table_offset > table_sec->sh_size
9480 		  || ((bfd_signed_vma) table_offset) < 0)
9481 		{
9482 		  warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9483 			(unsigned long) table_offset,
9484 			printable_section_name (filedata, table_sec));
9485 		  res = FALSE;
9486 		  continue;
9487 		}
9488 	    }
9489 	  else
9490 	    {
9491 	      table_sec = find_section_by_address (filedata, table);
9492 	      if (table_sec != NULL)
9493 		table_offset = table - table_sec->sh_addr;
9494 	    }
9495 
9496 	  if (table_sec == NULL)
9497 	    {
9498 	      warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9499 		    (unsigned long) table);
9500 	      res = FALSE;
9501 	      continue;
9502 	    }
9503 
9504 	  if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
9505 				   &extab_arm_sec))
9506 	    res = FALSE;
9507 	}
9508     }
9509 
9510   printf ("\n");
9511 
9512   free (aux->funtab);
9513   arm_free_section (&exidx_arm_sec);
9514   arm_free_section (&extab_arm_sec);
9515 
9516   return res;
9517 }
9518 
9519 /* Used for both ARM and C6X unwinding tables.  */
9520 
9521 static bfd_boolean
arm_process_unwind(Filedata * filedata)9522 arm_process_unwind (Filedata * filedata)
9523 {
9524   struct arm_unw_aux_info aux;
9525   Elf_Internal_Shdr *unwsec = NULL;
9526   Elf_Internal_Shdr *sec;
9527   unsigned long i;
9528   unsigned int sec_type;
9529   bfd_boolean res = TRUE;
9530 
9531   switch (filedata->file_header.e_machine)
9532     {
9533     case EM_ARM:
9534       sec_type = SHT_ARM_EXIDX;
9535       break;
9536 
9537     case EM_TI_C6000:
9538       sec_type = SHT_C6000_UNWIND;
9539       break;
9540 
9541     default:
9542       error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
9543 	     filedata->file_header.e_machine);
9544       return FALSE;
9545     }
9546 
9547   if (filedata->string_table == NULL)
9548     return FALSE;
9549 
9550   memset (& aux, 0, sizeof (aux));
9551   aux.filedata = filedata;
9552 
9553   for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9554     {
9555       if (sec->sh_type == SHT_SYMTAB)
9556 	{
9557 	  if (aux.symtab)
9558 	    {
9559 	      error (_("Multiple symbol tables encountered\n"));
9560 	      free (aux.symtab);
9561 	      aux.symtab = NULL;
9562 	      free (aux.strtab);
9563 	      aux.strtab = NULL;
9564 	    }
9565 	  if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9566 			   &aux.strtab, &aux.strtab_size))
9567 	    return FALSE;
9568 	}
9569       else if (sec->sh_type == sec_type)
9570 	unwsec = sec;
9571     }
9572 
9573   if (unwsec == NULL)
9574     printf (_("\nThere are no unwind sections in this file.\n"));
9575   else
9576     for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9577       {
9578 	if (sec->sh_type == sec_type)
9579 	  {
9580 	    unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
9581 	    printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9582 			      "contains %lu entry:\n",
9583 			      "\nUnwind section '%s' at offset 0x%lx "
9584 			      "contains %lu entries:\n",
9585 			      num_unwind),
9586 		    printable_section_name (filedata, sec),
9587 		    (unsigned long) sec->sh_offset,
9588 		    num_unwind);
9589 
9590 	    if (! dump_arm_unwind (filedata, &aux, sec))
9591 	      res = FALSE;
9592 	  }
9593       }
9594 
9595   free (aux.symtab);
9596   free ((char *) aux.strtab);
9597 
9598   return res;
9599 }
9600 
9601 static bfd_boolean
process_unwind(Filedata * filedata)9602 process_unwind (Filedata * filedata)
9603 {
9604   struct unwind_handler
9605   {
9606     unsigned int machtype;
9607     bfd_boolean (* handler)(Filedata *);
9608   } handlers[] =
9609   {
9610     { EM_ARM, arm_process_unwind },
9611     { EM_IA_64, ia64_process_unwind },
9612     { EM_PARISC, hppa_process_unwind },
9613     { EM_TI_C6000, arm_process_unwind },
9614     { 0, NULL }
9615   };
9616   int i;
9617 
9618   if (!do_unwind)
9619     return TRUE;
9620 
9621   for (i = 0; handlers[i].handler != NULL; i++)
9622     if (filedata->file_header.e_machine == handlers[i].machtype)
9623       return handlers[i].handler (filedata);
9624 
9625   printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
9626 	  get_machine_name (filedata->file_header.e_machine));
9627   return TRUE;
9628 }
9629 
9630 static void
dynamic_section_aarch64_val(Elf_Internal_Dyn * entry)9631 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
9632 {
9633   switch (entry->d_tag)
9634     {
9635     case DT_AARCH64_BTI_PLT:
9636     case DT_AARCH64_PAC_PLT:
9637       break;
9638     default:
9639       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9640       break;
9641     }
9642   putchar ('\n');
9643 }
9644 
9645 static void
dynamic_section_mips_val(Filedata * filedata,Elf_Internal_Dyn * entry)9646 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
9647 {
9648   switch (entry->d_tag)
9649     {
9650     case DT_MIPS_FLAGS:
9651       if (entry->d_un.d_val == 0)
9652 	printf (_("NONE"));
9653       else
9654 	{
9655 	  static const char * opts[] =
9656 	  {
9657 	    "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
9658 	    "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
9659 	    "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
9660 	    "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
9661 	    "RLD_ORDER_SAFE"
9662 	  };
9663 	  unsigned int cnt;
9664 	  bfd_boolean first = TRUE;
9665 
9666 	  for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
9667 	    if (entry->d_un.d_val & (1 << cnt))
9668 	      {
9669 		printf ("%s%s", first ? "" : " ", opts[cnt]);
9670 		first = FALSE;
9671 	      }
9672 	}
9673       break;
9674 
9675     case DT_MIPS_IVERSION:
9676       if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
9677 	printf (_("Interface Version: %s"),
9678 		GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
9679       else
9680 	{
9681 	  char buf[40];
9682 	  sprintf_vma (buf, entry->d_un.d_ptr);
9683 	  /* Note: coded this way so that there is a single string for translation.  */
9684 	  printf (_("<corrupt: %s>"), buf);
9685 	}
9686       break;
9687 
9688     case DT_MIPS_TIME_STAMP:
9689       {
9690 	char timebuf[128];
9691 	struct tm * tmp;
9692 	time_t atime = entry->d_un.d_val;
9693 
9694 	tmp = gmtime (&atime);
9695 	/* PR 17531: file: 6accc532.  */
9696 	if (tmp == NULL)
9697 	  snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
9698 	else
9699 	  snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
9700 		    tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9701 		    tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9702 	printf (_("Time Stamp: %s"), timebuf);
9703       }
9704       break;
9705 
9706     case DT_MIPS_RLD_VERSION:
9707     case DT_MIPS_LOCAL_GOTNO:
9708     case DT_MIPS_CONFLICTNO:
9709     case DT_MIPS_LIBLISTNO:
9710     case DT_MIPS_SYMTABNO:
9711     case DT_MIPS_UNREFEXTNO:
9712     case DT_MIPS_HIPAGENO:
9713     case DT_MIPS_DELTA_CLASS_NO:
9714     case DT_MIPS_DELTA_INSTANCE_NO:
9715     case DT_MIPS_DELTA_RELOC_NO:
9716     case DT_MIPS_DELTA_SYM_NO:
9717     case DT_MIPS_DELTA_CLASSSYM_NO:
9718     case DT_MIPS_COMPACT_SIZE:
9719       print_vma (entry->d_un.d_val, DEC);
9720       break;
9721 
9722     case DT_MIPS_XHASH:
9723       filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
9724       filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
9725       /* Falls through.  */
9726 
9727     default:
9728       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9729     }
9730     putchar ('\n');
9731 }
9732 
9733 static void
dynamic_section_parisc_val(Elf_Internal_Dyn * entry)9734 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
9735 {
9736   switch (entry->d_tag)
9737     {
9738     case DT_HP_DLD_FLAGS:
9739       {
9740 	static struct
9741 	{
9742 	  long int bit;
9743 	  const char * str;
9744 	}
9745 	flags[] =
9746 	{
9747 	  { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
9748 	  { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
9749 	  { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
9750 	  { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
9751 	  { DT_HP_BIND_NOW, "HP_BIND_NOW" },
9752 	  { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
9753 	  { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
9754 	  { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
9755 	  { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
9756 	  { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
9757 	  { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
9758 	  { DT_HP_GST, "HP_GST" },
9759 	  { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
9760 	  { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
9761 	  { DT_HP_NODELETE, "HP_NODELETE" },
9762 	  { DT_HP_GROUP, "HP_GROUP" },
9763 	  { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
9764 	};
9765 	bfd_boolean first = TRUE;
9766 	size_t cnt;
9767 	bfd_vma val = entry->d_un.d_val;
9768 
9769 	for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
9770 	  if (val & flags[cnt].bit)
9771 	    {
9772 	      if (! first)
9773 		putchar (' ');
9774 	      fputs (flags[cnt].str, stdout);
9775 	      first = FALSE;
9776 	      val ^= flags[cnt].bit;
9777 	    }
9778 
9779 	if (val != 0 || first)
9780 	  {
9781 	    if (! first)
9782 	      putchar (' ');
9783 	    print_vma (val, HEX);
9784 	  }
9785       }
9786       break;
9787 
9788     default:
9789       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9790       break;
9791     }
9792   putchar ('\n');
9793 }
9794 
9795 #ifdef BFD64
9796 
9797 /* VMS vs Unix time offset and factor.  */
9798 
9799 #define VMS_EPOCH_OFFSET 35067168000000000LL
9800 #define VMS_GRANULARITY_FACTOR 10000000
9801 
9802 /* Display a VMS time in a human readable format.  */
9803 
9804 static void
print_vms_time(bfd_int64_t vmstime)9805 print_vms_time (bfd_int64_t vmstime)
9806 {
9807   struct tm *tm;
9808   time_t unxtime;
9809 
9810   unxtime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
9811   tm = gmtime (&unxtime);
9812   printf ("%04u-%02u-%02uT%02u:%02u:%02u",
9813           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
9814           tm->tm_hour, tm->tm_min, tm->tm_sec);
9815 }
9816 #endif /* BFD64 */
9817 
9818 static void
dynamic_section_ia64_val(Elf_Internal_Dyn * entry)9819 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
9820 {
9821   switch (entry->d_tag)
9822     {
9823     case DT_IA_64_PLT_RESERVE:
9824       /* First 3 slots reserved.  */
9825       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9826       printf (" -- ");
9827       print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
9828       break;
9829 
9830     case DT_IA_64_VMS_LINKTIME:
9831 #ifdef BFD64
9832       print_vms_time (entry->d_un.d_val);
9833 #endif
9834       break;
9835 
9836     case DT_IA_64_VMS_LNKFLAGS:
9837       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9838       if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
9839         printf (" CALL_DEBUG");
9840       if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
9841         printf (" NOP0BUFS");
9842       if (entry->d_un.d_val & VMS_LF_P0IMAGE)
9843         printf (" P0IMAGE");
9844       if (entry->d_un.d_val & VMS_LF_MKTHREADS)
9845         printf (" MKTHREADS");
9846       if (entry->d_un.d_val & VMS_LF_UPCALLS)
9847         printf (" UPCALLS");
9848       if (entry->d_un.d_val & VMS_LF_IMGSTA)
9849         printf (" IMGSTA");
9850       if (entry->d_un.d_val & VMS_LF_INITIALIZE)
9851         printf (" INITIALIZE");
9852       if (entry->d_un.d_val & VMS_LF_MAIN)
9853         printf (" MAIN");
9854       if (entry->d_un.d_val & VMS_LF_EXE_INIT)
9855         printf (" EXE_INIT");
9856       if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
9857         printf (" TBK_IN_IMG");
9858       if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
9859         printf (" DBG_IN_IMG");
9860       if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
9861         printf (" TBK_IN_DSF");
9862       if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
9863         printf (" DBG_IN_DSF");
9864       if (entry->d_un.d_val & VMS_LF_SIGNATURES)
9865         printf (" SIGNATURES");
9866       if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
9867         printf (" REL_SEG_OFF");
9868       break;
9869 
9870     default:
9871       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9872       break;
9873     }
9874   putchar ('\n');
9875 }
9876 
9877 static bfd_boolean
get_32bit_dynamic_section(Filedata * filedata)9878 get_32bit_dynamic_section (Filedata * filedata)
9879 {
9880   Elf32_External_Dyn * edyn;
9881   Elf32_External_Dyn * ext;
9882   Elf_Internal_Dyn * entry;
9883 
9884   edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
9885 					  filedata->dynamic_addr, 1,
9886 					  filedata->dynamic_size,
9887 					  _("dynamic section"));
9888   if (!edyn)
9889     return FALSE;
9890 
9891   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
9892      might not have the luxury of section headers.  Look for the DT_NULL
9893      terminator to determine the number of entries.  */
9894   for (ext = edyn, filedata->dynamic_nent = 0;
9895        (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
9896        ext++)
9897     {
9898       filedata->dynamic_nent++;
9899       if (BYTE_GET (ext->d_tag) == DT_NULL)
9900 	break;
9901     }
9902 
9903   filedata->dynamic_section
9904     = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
9905   if (filedata->dynamic_section == NULL)
9906     {
9907       error (_("Out of memory allocating space for %lu dynamic entries\n"),
9908 	     (unsigned long) filedata->dynamic_nent);
9909       free (edyn);
9910       return FALSE;
9911     }
9912 
9913   for (ext = edyn, entry = filedata->dynamic_section;
9914        entry < filedata->dynamic_section + filedata->dynamic_nent;
9915        ext++, entry++)
9916     {
9917       entry->d_tag      = BYTE_GET (ext->d_tag);
9918       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
9919     }
9920 
9921   free (edyn);
9922 
9923   return TRUE;
9924 }
9925 
9926 static bfd_boolean
get_64bit_dynamic_section(Filedata * filedata)9927 get_64bit_dynamic_section (Filedata * filedata)
9928 {
9929   Elf64_External_Dyn * edyn;
9930   Elf64_External_Dyn * ext;
9931   Elf_Internal_Dyn * entry;
9932 
9933   /* Read in the data.  */
9934   edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
9935 					  filedata->dynamic_addr, 1,
9936 					  filedata->dynamic_size,
9937 					  _("dynamic section"));
9938   if (!edyn)
9939     return FALSE;
9940 
9941   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
9942      might not have the luxury of section headers.  Look for the DT_NULL
9943      terminator to determine the number of entries.  */
9944   for (ext = edyn, filedata->dynamic_nent = 0;
9945        /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer.  */
9946        (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
9947        ext++)
9948     {
9949       filedata->dynamic_nent++;
9950       if (BYTE_GET (ext->d_tag) == DT_NULL)
9951 	break;
9952     }
9953 
9954   filedata->dynamic_section
9955     = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
9956   if (filedata->dynamic_section == NULL)
9957     {
9958       error (_("Out of memory allocating space for %lu dynamic entries\n"),
9959 	     (unsigned long) filedata->dynamic_nent);
9960       free (edyn);
9961       return FALSE;
9962     }
9963 
9964   /* Convert from external to internal formats.  */
9965   for (ext = edyn, entry = filedata->dynamic_section;
9966        entry < filedata->dynamic_section + filedata->dynamic_nent;
9967        ext++, entry++)
9968     {
9969       entry->d_tag      = BYTE_GET (ext->d_tag);
9970       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
9971     }
9972 
9973   free (edyn);
9974 
9975   return TRUE;
9976 }
9977 
9978 static void
print_dynamic_flags(bfd_vma flags)9979 print_dynamic_flags (bfd_vma flags)
9980 {
9981   bfd_boolean first = TRUE;
9982 
9983   while (flags)
9984     {
9985       bfd_vma flag;
9986 
9987       flag = flags & - flags;
9988       flags &= ~ flag;
9989 
9990       if (first)
9991 	first = FALSE;
9992       else
9993 	putc (' ', stdout);
9994 
9995       switch (flag)
9996 	{
9997 	case DF_ORIGIN:		fputs ("ORIGIN", stdout); break;
9998 	case DF_SYMBOLIC:	fputs ("SYMBOLIC", stdout); break;
9999 	case DF_TEXTREL:	fputs ("TEXTREL", stdout); break;
10000 	case DF_BIND_NOW:	fputs ("BIND_NOW", stdout); break;
10001 	case DF_STATIC_TLS:	fputs ("STATIC_TLS", stdout); break;
10002 	default:		fputs (_("unknown"), stdout); break;
10003 	}
10004     }
10005   puts ("");
10006 }
10007 
10008 static bfd_vma *
get_dynamic_data(Filedata * filedata,bfd_size_type number,unsigned int ent_size)10009 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10010 {
10011   unsigned char * e_data;
10012   bfd_vma * i_data;
10013 
10014   /* If the size_t type is smaller than the bfd_size_type, eg because
10015      you are building a 32-bit tool on a 64-bit host, then make sure
10016      that when (number) is cast to (size_t) no information is lost.  */
10017   if (sizeof (size_t) < sizeof (bfd_size_type)
10018       && (bfd_size_type) ((size_t) number) != number)
10019     {
10020       error (_("Size truncation prevents reading %s elements of size %u\n"),
10021 	     bfd_vmatoa ("u", number), ent_size);
10022       return NULL;
10023     }
10024 
10025   /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10026      attempting to allocate memory when the read is bound to fail.  */
10027   if (ent_size * number > filedata->file_size)
10028     {
10029       error (_("Invalid number of dynamic entries: %s\n"),
10030 	     bfd_vmatoa ("u", number));
10031       return NULL;
10032     }
10033 
10034   e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10035   if (e_data == NULL)
10036     {
10037       error (_("Out of memory reading %s dynamic entries\n"),
10038 	     bfd_vmatoa ("u", number));
10039       return NULL;
10040     }
10041 
10042   if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10043     {
10044       error (_("Unable to read in %s bytes of dynamic data\n"),
10045 	     bfd_vmatoa ("u", number * ent_size));
10046       free (e_data);
10047       return NULL;
10048     }
10049 
10050   i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10051   if (i_data == NULL)
10052     {
10053       error (_("Out of memory allocating space for %s dynamic entries\n"),
10054 	     bfd_vmatoa ("u", number));
10055       free (e_data);
10056       return NULL;
10057     }
10058 
10059   while (number--)
10060     i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10061 
10062   free (e_data);
10063 
10064   return i_data;
10065 }
10066 
10067 static unsigned long
get_num_dynamic_syms(Filedata * filedata)10068 get_num_dynamic_syms (Filedata * filedata)
10069 {
10070   unsigned long num_of_syms = 0;
10071 
10072   if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10073     return num_of_syms;
10074 
10075   if (filedata->dynamic_info[DT_HASH])
10076     {
10077       unsigned char nb[8];
10078       unsigned char nc[8];
10079       unsigned int hash_ent_size = 4;
10080 
10081       if ((filedata->file_header.e_machine == EM_ALPHA
10082 	   || filedata->file_header.e_machine == EM_S390
10083 	   || filedata->file_header.e_machine == EM_S390_OLD)
10084 	  && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10085 	hash_ent_size = 8;
10086 
10087       if (fseek (filedata->handle,
10088 		 (filedata->archive_file_offset
10089 		  + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10090 				     sizeof nb + sizeof nc)),
10091 		 SEEK_SET))
10092 	{
10093 	  error (_("Unable to seek to start of dynamic information\n"));
10094 	  goto no_hash;
10095 	}
10096 
10097       if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10098 	{
10099 	  error (_("Failed to read in number of buckets\n"));
10100 	  goto no_hash;
10101 	}
10102 
10103       if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10104 	{
10105 	  error (_("Failed to read in number of chains\n"));
10106 	  goto no_hash;
10107 	}
10108 
10109       filedata->nbuckets = byte_get (nb, hash_ent_size);
10110       filedata->nchains = byte_get (nc, hash_ent_size);
10111 
10112       if (filedata->nbuckets != 0 && filedata->nchains != 0)
10113 	{
10114 	  filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10115 						hash_ent_size);
10116 	  filedata->chains  = get_dynamic_data (filedata, filedata->nchains,
10117 						hash_ent_size);
10118 
10119 	  if (filedata->buckets != NULL && filedata->chains != NULL)
10120 	    num_of_syms = filedata->nchains;
10121 	}
10122     no_hash:
10123       if (num_of_syms == 0)
10124 	{
10125 	  free (filedata->buckets);
10126 	  filedata->buckets = NULL;
10127 	  free (filedata->chains);
10128 	  filedata->chains = NULL;
10129 	  filedata->nbuckets = 0;
10130 	}
10131     }
10132 
10133   if (filedata->dynamic_info_DT_GNU_HASH)
10134     {
10135       unsigned char nb[16];
10136       bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10137       bfd_vma buckets_vma;
10138       unsigned long hn;
10139 
10140       if (fseek (filedata->handle,
10141 		 (filedata->archive_file_offset
10142 		  + offset_from_vma (filedata,
10143 				     filedata->dynamic_info_DT_GNU_HASH,
10144 				     sizeof nb)),
10145 		 SEEK_SET))
10146 	{
10147 	  error (_("Unable to seek to start of dynamic information\n"));
10148 	  goto no_gnu_hash;
10149 	}
10150 
10151       if (fread (nb, 16, 1, filedata->handle) != 1)
10152 	{
10153 	  error (_("Failed to read in number of buckets\n"));
10154 	  goto no_gnu_hash;
10155 	}
10156 
10157       filedata->ngnubuckets = byte_get (nb, 4);
10158       filedata->gnusymidx = byte_get (nb + 4, 4);
10159       bitmaskwords = byte_get (nb + 8, 4);
10160       buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10161       if (is_32bit_elf)
10162 	buckets_vma += bitmaskwords * 4;
10163       else
10164 	buckets_vma += bitmaskwords * 8;
10165 
10166       if (fseek (filedata->handle,
10167 		 (filedata->archive_file_offset
10168 		  + offset_from_vma (filedata, buckets_vma, 4)),
10169 		 SEEK_SET))
10170 	{
10171 	  error (_("Unable to seek to start of dynamic information\n"));
10172 	  goto no_gnu_hash;
10173 	}
10174 
10175       filedata->gnubuckets
10176 	= get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10177 
10178       if (filedata->gnubuckets == NULL)
10179 	goto no_gnu_hash;
10180 
10181       for (i = 0; i < filedata->ngnubuckets; i++)
10182 	if (filedata->gnubuckets[i] != 0)
10183 	  {
10184 	    if (filedata->gnubuckets[i] < filedata->gnusymidx)
10185 	      goto no_gnu_hash;
10186 
10187 	    if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10188 	      maxchain = filedata->gnubuckets[i];
10189 	  }
10190 
10191       if (maxchain == 0xffffffff)
10192 	goto no_gnu_hash;
10193 
10194       maxchain -= filedata->gnusymidx;
10195 
10196       if (fseek (filedata->handle,
10197 		 (filedata->archive_file_offset
10198 		  + offset_from_vma (filedata,
10199 				     buckets_vma + 4 * (filedata->ngnubuckets
10200 							+ maxchain),
10201 				      4)),
10202 		 SEEK_SET))
10203 	{
10204 	  error (_("Unable to seek to start of dynamic information\n"));
10205 	  goto no_gnu_hash;
10206 	}
10207 
10208       do
10209 	{
10210 	  if (fread (nb, 4, 1, filedata->handle) != 1)
10211 	    {
10212 	      error (_("Failed to determine last chain length\n"));
10213 	      goto no_gnu_hash;
10214 	    }
10215 
10216 	  if (maxchain + 1 == 0)
10217 	    goto no_gnu_hash;
10218 
10219 	  ++maxchain;
10220 	}
10221       while ((byte_get (nb, 4) & 1) == 0);
10222 
10223       if (fseek (filedata->handle,
10224 		 (filedata->archive_file_offset
10225 		  + offset_from_vma (filedata, (buckets_vma
10226 						+ 4 * filedata->ngnubuckets),
10227 				     4)),
10228 		 SEEK_SET))
10229 	{
10230 	  error (_("Unable to seek to start of dynamic information\n"));
10231 	  goto no_gnu_hash;
10232 	}
10233 
10234       filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10235       filedata->ngnuchains = maxchain;
10236 
10237       if (filedata->gnuchains == NULL)
10238 	goto no_gnu_hash;
10239 
10240       if (filedata->dynamic_info_DT_MIPS_XHASH)
10241 	{
10242 	  if (fseek (filedata->handle,
10243 		     (filedata->archive_file_offset
10244 		      + offset_from_vma (filedata, (buckets_vma
10245 						    + 4 * (filedata->ngnubuckets
10246 							   + maxchain)), 4)),
10247 		     SEEK_SET))
10248 	    {
10249 	      error (_("Unable to seek to start of dynamic information\n"));
10250 	      goto no_gnu_hash;
10251 	    }
10252 
10253 	  filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
10254 	  if (filedata->mipsxlat == NULL)
10255 	    goto no_gnu_hash;
10256 	}
10257 
10258       for (hn = 0; hn < filedata->ngnubuckets; ++hn)
10259 	if (filedata->gnubuckets[hn] != 0)
10260 	  {
10261 	    bfd_vma si = filedata->gnubuckets[hn];
10262 	    bfd_vma off = si - filedata->gnusymidx;
10263 
10264 	    do
10265 	      {
10266 		if (filedata->dynamic_info_DT_MIPS_XHASH)
10267 		  {
10268 		    if (off < filedata->ngnuchains
10269 			&& filedata->mipsxlat[off] >= num_of_syms)
10270 		      num_of_syms = filedata->mipsxlat[off] + 1;
10271 		  }
10272 		else
10273 		  {
10274 		    if (si >= num_of_syms)
10275 		      num_of_syms = si + 1;
10276 		  }
10277 		si++;
10278 	      }
10279 	    while (off < filedata->ngnuchains
10280 		   && (filedata->gnuchains[off++] & 1) == 0);
10281 	  }
10282 
10283       if (num_of_syms == 0)
10284 	{
10285 	no_gnu_hash:
10286 	  free (filedata->mipsxlat);
10287 	  filedata->mipsxlat = NULL;
10288 	  free (filedata->gnuchains);
10289 	  filedata->gnuchains = NULL;
10290 	  free (filedata->gnubuckets);
10291 	  filedata->gnubuckets = NULL;
10292 	  filedata->ngnubuckets = 0;
10293 	  filedata->ngnuchains = 0;
10294 	}
10295     }
10296 
10297   return num_of_syms;
10298 }
10299 
10300 /* Parse and display the contents of the dynamic section.  */
10301 
10302 static bfd_boolean
process_dynamic_section(Filedata * filedata)10303 process_dynamic_section (Filedata * filedata)
10304 {
10305   Elf_Internal_Dyn * entry;
10306 
10307   if (filedata->dynamic_size == 0)
10308     {
10309       if (do_dynamic)
10310 	printf (_("\nThere is no dynamic section in this file.\n"));
10311 
10312       return TRUE;
10313     }
10314 
10315   if (is_32bit_elf)
10316     {
10317       if (! get_32bit_dynamic_section (filedata))
10318 	return FALSE;
10319     }
10320   else
10321     {
10322       if (! get_64bit_dynamic_section (filedata))
10323 	return FALSE;
10324     }
10325 
10326   /* Find the appropriate symbol table.  */
10327   if (filedata->dynamic_symbols == NULL || do_histogram)
10328     {
10329       unsigned long num_of_syms;
10330 
10331       for (entry = filedata->dynamic_section;
10332 	   entry < filedata->dynamic_section + filedata->dynamic_nent;
10333 	   ++entry)
10334 	if (entry->d_tag == DT_SYMTAB)
10335 	  filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10336 	else if (entry->d_tag == DT_SYMENT)
10337 	  filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10338 	else if (entry->d_tag == DT_HASH)
10339 	  filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10340 	else if (entry->d_tag == DT_GNU_HASH)
10341 	  filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10342 	else if ((filedata->file_header.e_machine == EM_MIPS
10343 		  || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
10344 		 && entry->d_tag == DT_MIPS_XHASH)
10345 	  {
10346 	    filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10347 	    filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10348 	  }
10349 
10350       num_of_syms = get_num_dynamic_syms (filedata);
10351 
10352       if (num_of_syms != 0
10353 	  && filedata->dynamic_symbols == NULL
10354 	  && filedata->dynamic_info[DT_SYMTAB]
10355 	  && filedata->dynamic_info[DT_SYMENT])
10356 	{
10357 	  Elf_Internal_Phdr *seg;
10358 	  bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
10359 
10360 	  if (! get_program_headers (filedata))
10361 	    {
10362 	      error (_("Cannot interpret virtual addresses "
10363 		       "without program headers.\n"));
10364 	      return FALSE;
10365 	    }
10366 
10367 	  for (seg = filedata->program_headers;
10368 	       seg < filedata->program_headers + filedata->file_header.e_phnum;
10369 	       ++seg)
10370 	    {
10371 	      if (seg->p_type != PT_LOAD)
10372 		continue;
10373 
10374 	      if (seg->p_offset + seg->p_filesz > filedata->file_size)
10375 		{
10376 		  /* See PR 21379 for a reproducer.  */
10377 		  error (_("Invalid PT_LOAD entry\n"));
10378 		  return FALSE;
10379 		}
10380 
10381 	      if (vma >= (seg->p_vaddr & -seg->p_align)
10382 		  && vma < seg->p_vaddr + seg->p_filesz)
10383 		{
10384 		  /* Since we do not know how big the symbol table is,
10385 		     we default to reading in up to the end of PT_LOAD
10386 		     segment and processing that.  This is overkill, I
10387 		     know, but it should work.  */
10388 		  Elf_Internal_Shdr section;
10389 		  section.sh_offset = (vma - seg->p_vaddr
10390 				       + seg->p_offset);
10391 		  section.sh_size = (num_of_syms
10392 				     * filedata->dynamic_info[DT_SYMENT]);
10393 		  section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
10394 
10395 		  if (do_checks
10396 		      && filedata->dynamic_symtab_section != NULL
10397 		      && ((filedata->dynamic_symtab_section->sh_offset
10398 			   != section.sh_offset)
10399 			  || (filedata->dynamic_symtab_section->sh_size
10400 			      != section.sh_size)
10401 			  || (filedata->dynamic_symtab_section->sh_entsize
10402 			      != section.sh_entsize)))
10403 		    warn (_("\
10404 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
10405 
10406 		  section.sh_name = filedata->string_table_length;
10407 		  filedata->dynamic_symbols
10408 		    = GET_ELF_SYMBOLS (filedata, &section,
10409 				       &filedata->num_dynamic_syms);
10410 		  if (filedata->dynamic_symbols == NULL
10411 		      || filedata->num_dynamic_syms != num_of_syms)
10412 		    {
10413 		      error (_("Corrupt DT_SYMTAB dynamic entry\n"));
10414 		      return FALSE;
10415 		    }
10416 		  break;
10417 		}
10418 	    }
10419 	}
10420     }
10421 
10422   /* Similarly find a string table.  */
10423   if (filedata->dynamic_strings == NULL)
10424     for (entry = filedata->dynamic_section;
10425 	 entry < filedata->dynamic_section + filedata->dynamic_nent;
10426 	 ++entry)
10427       {
10428 	if (entry->d_tag == DT_STRTAB)
10429 	  filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
10430 
10431 	if (entry->d_tag == DT_STRSZ)
10432 	  filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
10433 
10434 	if (filedata->dynamic_info[DT_STRTAB]
10435 	    && filedata->dynamic_info[DT_STRSZ])
10436 	  {
10437 	    unsigned long offset;
10438 	    bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
10439 
10440 	    offset = offset_from_vma (filedata,
10441 				      filedata->dynamic_info[DT_STRTAB],
10442 				      str_tab_len);
10443 	    if (do_checks
10444 		&& filedata->dynamic_strtab_section
10445 		&& ((filedata->dynamic_strtab_section->sh_offset
10446 		     != (file_ptr) offset)
10447 		    || (filedata->dynamic_strtab_section->sh_size
10448 			!= str_tab_len)))
10449 	      warn (_("\
10450 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
10451 
10452 	    filedata->dynamic_strings
10453 	      = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
10454 				   _("dynamic string table"));
10455 	    if (filedata->dynamic_strings == NULL)
10456 	      {
10457 		error (_("Corrupt DT_STRTAB dynamic entry\n"));
10458 		break;
10459 	      }
10460 
10461 	    filedata->dynamic_strings_length = str_tab_len;
10462 	    break;
10463 	  }
10464       }
10465 
10466   /* And find the syminfo section if available.  */
10467   if (filedata->dynamic_syminfo == NULL)
10468     {
10469       unsigned long syminsz = 0;
10470 
10471       for (entry = filedata->dynamic_section;
10472 	   entry < filedata->dynamic_section + filedata->dynamic_nent;
10473 	   ++entry)
10474 	{
10475 	  if (entry->d_tag == DT_SYMINENT)
10476 	    {
10477 	      /* Note: these braces are necessary to avoid a syntax
10478 		 error from the SunOS4 C compiler.  */
10479 	      /* PR binutils/17531: A corrupt file can trigger this test.
10480 		 So do not use an assert, instead generate an error message.  */
10481 	      if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
10482 		error (_("Bad value (%d) for SYMINENT entry\n"),
10483 		       (int) entry->d_un.d_val);
10484 	    }
10485 	  else if (entry->d_tag == DT_SYMINSZ)
10486 	    syminsz = entry->d_un.d_val;
10487 	  else if (entry->d_tag == DT_SYMINFO)
10488 	    filedata->dynamic_syminfo_offset
10489 	      = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
10490 	}
10491 
10492       if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
10493 	{
10494 	  Elf_External_Syminfo * extsyminfo;
10495 	  Elf_External_Syminfo * extsym;
10496 	  Elf_Internal_Syminfo * syminfo;
10497 
10498 	  /* There is a syminfo section.  Read the data.  */
10499 	  extsyminfo = (Elf_External_Syminfo *)
10500 	    get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
10501 		      1, syminsz, _("symbol information"));
10502 	  if (!extsyminfo)
10503 	    return FALSE;
10504 
10505 	  if (filedata->dynamic_syminfo != NULL)
10506 	    {
10507 	      error (_("Multiple dynamic symbol information sections found\n"));
10508 	      free (filedata->dynamic_syminfo);
10509 	    }
10510 	  filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
10511 	  if (filedata->dynamic_syminfo == NULL)
10512 	    {
10513 	      error (_("Out of memory allocating %lu bytes "
10514 		       "for dynamic symbol info\n"),
10515 		     (unsigned long) syminsz);
10516 	      return FALSE;
10517 	    }
10518 
10519 	  filedata->dynamic_syminfo_nent
10520 	    = syminsz / sizeof (Elf_External_Syminfo);
10521 	  for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
10522 	       syminfo < (filedata->dynamic_syminfo
10523 			  + filedata->dynamic_syminfo_nent);
10524 	       ++syminfo, ++extsym)
10525 	    {
10526 	      syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
10527 	      syminfo->si_flags = BYTE_GET (extsym->si_flags);
10528 	    }
10529 
10530 	  free (extsyminfo);
10531 	}
10532     }
10533 
10534   if (do_dynamic && filedata->dynamic_addr)
10535     printf (ngettext ("\nDynamic section at offset 0x%lx "
10536 		      "contains %lu entry:\n",
10537 		      "\nDynamic section at offset 0x%lx "
10538 		      "contains %lu entries:\n",
10539 		      filedata->dynamic_nent),
10540 	    filedata->dynamic_addr, (unsigned long) filedata->dynamic_nent);
10541   if (do_dynamic)
10542     printf (_("  Tag        Type                         Name/Value\n"));
10543 
10544   for (entry = filedata->dynamic_section;
10545        entry < filedata->dynamic_section + filedata->dynamic_nent;
10546        entry++)
10547     {
10548       if (do_dynamic)
10549 	{
10550 	  const char * dtype;
10551 
10552 	  putchar (' ');
10553 	  print_vma (entry->d_tag, FULL_HEX);
10554 	  dtype = get_dynamic_type (filedata, entry->d_tag);
10555 	  printf (" (%s)%*s", dtype,
10556 		  ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
10557 	}
10558 
10559       switch (entry->d_tag)
10560 	{
10561 	case DT_FLAGS:
10562 	  if (do_dynamic)
10563 	    print_dynamic_flags (entry->d_un.d_val);
10564 	  break;
10565 
10566 	case DT_AUXILIARY:
10567 	case DT_FILTER:
10568 	case DT_CONFIG:
10569 	case DT_DEPAUDIT:
10570 	case DT_AUDIT:
10571 	  if (do_dynamic)
10572 	    {
10573 	      switch (entry->d_tag)
10574 		{
10575 		case DT_AUXILIARY:
10576 		  printf (_("Auxiliary library"));
10577 		  break;
10578 
10579 		case DT_FILTER:
10580 		  printf (_("Filter library"));
10581 		  break;
10582 
10583 		case DT_CONFIG:
10584 		  printf (_("Configuration file"));
10585 		  break;
10586 
10587 		case DT_DEPAUDIT:
10588 		  printf (_("Dependency audit library"));
10589 		  break;
10590 
10591 		case DT_AUDIT:
10592 		  printf (_("Audit library"));
10593 		  break;
10594 		}
10595 
10596 	      if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
10597 		printf (": [%s]\n",
10598 			GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
10599 	      else
10600 		{
10601 		  printf (": ");
10602 		  print_vma (entry->d_un.d_val, PREFIX_HEX);
10603 		  putchar ('\n');
10604 		}
10605 	    }
10606 	  break;
10607 
10608 	case DT_FEATURE:
10609 	  if (do_dynamic)
10610 	    {
10611 	      printf (_("Flags:"));
10612 
10613 	      if (entry->d_un.d_val == 0)
10614 		printf (_(" None\n"));
10615 	      else
10616 		{
10617 		  unsigned long int val = entry->d_un.d_val;
10618 
10619 		  if (val & DTF_1_PARINIT)
10620 		    {
10621 		      printf (" PARINIT");
10622 		      val ^= DTF_1_PARINIT;
10623 		    }
10624 		  if (val & DTF_1_CONFEXP)
10625 		    {
10626 		      printf (" CONFEXP");
10627 		      val ^= DTF_1_CONFEXP;
10628 		    }
10629 		  if (val != 0)
10630 		    printf (" %lx", val);
10631 		  puts ("");
10632 		}
10633 	    }
10634 	  break;
10635 
10636 	case DT_POSFLAG_1:
10637 	  if (do_dynamic)
10638 	    {
10639 	      printf (_("Flags:"));
10640 
10641 	      if (entry->d_un.d_val == 0)
10642 		printf (_(" None\n"));
10643 	      else
10644 		{
10645 		  unsigned long int val = entry->d_un.d_val;
10646 
10647 		  if (val & DF_P1_LAZYLOAD)
10648 		    {
10649 		      printf (" LAZYLOAD");
10650 		      val ^= DF_P1_LAZYLOAD;
10651 		    }
10652 		  if (val & DF_P1_GROUPPERM)
10653 		    {
10654 		      printf (" GROUPPERM");
10655 		      val ^= DF_P1_GROUPPERM;
10656 		    }
10657 		  if (val != 0)
10658 		    printf (" %lx", val);
10659 		  puts ("");
10660 		}
10661 	    }
10662 	  break;
10663 
10664 	case DT_FLAGS_1:
10665 	  if (do_dynamic)
10666 	    {
10667 	      printf (_("Flags:"));
10668 	      if (entry->d_un.d_val == 0)
10669 		printf (_(" None\n"));
10670 	      else
10671 		{
10672 		  unsigned long int val = entry->d_un.d_val;
10673 
10674 		  if (val & DF_1_NOW)
10675 		    {
10676 		      printf (" NOW");
10677 		      val ^= DF_1_NOW;
10678 		    }
10679 		  if (val & DF_1_GLOBAL)
10680 		    {
10681 		      printf (" GLOBAL");
10682 		      val ^= DF_1_GLOBAL;
10683 		    }
10684 		  if (val & DF_1_GROUP)
10685 		    {
10686 		      printf (" GROUP");
10687 		      val ^= DF_1_GROUP;
10688 		    }
10689 		  if (val & DF_1_NODELETE)
10690 		    {
10691 		      printf (" NODELETE");
10692 		      val ^= DF_1_NODELETE;
10693 		    }
10694 		  if (val & DF_1_LOADFLTR)
10695 		    {
10696 		      printf (" LOADFLTR");
10697 		      val ^= DF_1_LOADFLTR;
10698 		    }
10699 		  if (val & DF_1_INITFIRST)
10700 		    {
10701 		      printf (" INITFIRST");
10702 		      val ^= DF_1_INITFIRST;
10703 		    }
10704 		  if (val & DF_1_NOOPEN)
10705 		    {
10706 		      printf (" NOOPEN");
10707 		      val ^= DF_1_NOOPEN;
10708 		    }
10709 		  if (val & DF_1_ORIGIN)
10710 		    {
10711 		      printf (" ORIGIN");
10712 		      val ^= DF_1_ORIGIN;
10713 		    }
10714 		  if (val & DF_1_DIRECT)
10715 		    {
10716 		      printf (" DIRECT");
10717 		      val ^= DF_1_DIRECT;
10718 		    }
10719 		  if (val & DF_1_TRANS)
10720 		    {
10721 		      printf (" TRANS");
10722 		      val ^= DF_1_TRANS;
10723 		    }
10724 		  if (val & DF_1_INTERPOSE)
10725 		    {
10726 		      printf (" INTERPOSE");
10727 		      val ^= DF_1_INTERPOSE;
10728 		    }
10729 		  if (val & DF_1_NODEFLIB)
10730 		    {
10731 		      printf (" NODEFLIB");
10732 		      val ^= DF_1_NODEFLIB;
10733 		    }
10734 		  if (val & DF_1_NODUMP)
10735 		    {
10736 		      printf (" NODUMP");
10737 		      val ^= DF_1_NODUMP;
10738 		    }
10739 		  if (val & DF_1_CONFALT)
10740 		    {
10741 		      printf (" CONFALT");
10742 		      val ^= DF_1_CONFALT;
10743 		    }
10744 		  if (val & DF_1_ENDFILTEE)
10745 		    {
10746 		      printf (" ENDFILTEE");
10747 		      val ^= DF_1_ENDFILTEE;
10748 		    }
10749 		  if (val & DF_1_DISPRELDNE)
10750 		    {
10751 		      printf (" DISPRELDNE");
10752 		      val ^= DF_1_DISPRELDNE;
10753 		    }
10754 		  if (val & DF_1_DISPRELPND)
10755 		    {
10756 		      printf (" DISPRELPND");
10757 		      val ^= DF_1_DISPRELPND;
10758 		    }
10759 		  if (val & DF_1_NODIRECT)
10760 		    {
10761 		      printf (" NODIRECT");
10762 		      val ^= DF_1_NODIRECT;
10763 		    }
10764 		  if (val & DF_1_IGNMULDEF)
10765 		    {
10766 		      printf (" IGNMULDEF");
10767 		      val ^= DF_1_IGNMULDEF;
10768 		    }
10769 		  if (val & DF_1_NOKSYMS)
10770 		    {
10771 		      printf (" NOKSYMS");
10772 		      val ^= DF_1_NOKSYMS;
10773 		    }
10774 		  if (val & DF_1_NOHDR)
10775 		    {
10776 		      printf (" NOHDR");
10777 		      val ^= DF_1_NOHDR;
10778 		    }
10779 		  if (val & DF_1_EDITED)
10780 		    {
10781 		      printf (" EDITED");
10782 		      val ^= DF_1_EDITED;
10783 		    }
10784 		  if (val & DF_1_NORELOC)
10785 		    {
10786 		      printf (" NORELOC");
10787 		      val ^= DF_1_NORELOC;
10788 		    }
10789 		  if (val & DF_1_SYMINTPOSE)
10790 		    {
10791 		      printf (" SYMINTPOSE");
10792 		      val ^= DF_1_SYMINTPOSE;
10793 		    }
10794 		  if (val & DF_1_GLOBAUDIT)
10795 		    {
10796 		      printf (" GLOBAUDIT");
10797 		      val ^= DF_1_GLOBAUDIT;
10798 		    }
10799 		  if (val & DF_1_SINGLETON)
10800 		    {
10801 		      printf (" SINGLETON");
10802 		      val ^= DF_1_SINGLETON;
10803 		    }
10804 		  if (val & DF_1_STUB)
10805 		    {
10806 		      printf (" STUB");
10807 		      val ^= DF_1_STUB;
10808 		    }
10809 		  if (val & DF_1_PIE)
10810 		    {
10811 		      printf (" PIE");
10812 		      val ^= DF_1_PIE;
10813 		    }
10814 		  if (val & DF_1_KMOD)
10815 		    {
10816 		      printf (" KMOD");
10817 		      val ^= DF_1_KMOD;
10818 		    }
10819 		  if (val & DF_1_WEAKFILTER)
10820 		    {
10821 		      printf (" WEAKFILTER");
10822 		      val ^= DF_1_WEAKFILTER;
10823 		    }
10824 		  if (val & DF_1_NOCOMMON)
10825 		    {
10826 		      printf (" NOCOMMON");
10827 		      val ^= DF_1_NOCOMMON;
10828 		    }
10829 		  if (val != 0)
10830 		    printf (" %lx", val);
10831 		  puts ("");
10832 		}
10833 	    }
10834 	  break;
10835 
10836 	case DT_PLTREL:
10837 	  filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
10838 	  if (do_dynamic)
10839 	    puts (get_dynamic_type (filedata, entry->d_un.d_val));
10840 	  break;
10841 
10842 	case DT_NULL	:
10843 	case DT_NEEDED	:
10844 	case DT_PLTGOT	:
10845 	case DT_HASH	:
10846 	case DT_STRTAB	:
10847 	case DT_SYMTAB	:
10848 	case DT_RELA	:
10849 	case DT_INIT	:
10850 	case DT_FINI	:
10851 	case DT_SONAME	:
10852 	case DT_RPATH	:
10853 	case DT_SYMBOLIC:
10854 	case DT_REL	:
10855 	case DT_DEBUG	:
10856 	case DT_TEXTREL	:
10857 	case DT_JMPREL	:
10858 	case DT_RUNPATH	:
10859 	  filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
10860 
10861 	  if (do_dynamic)
10862 	    {
10863 	      char * name;
10864 
10865 	      if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
10866 		name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
10867 	      else
10868 		name = NULL;
10869 
10870 	      if (name)
10871 		{
10872 		  switch (entry->d_tag)
10873 		    {
10874 		    case DT_NEEDED:
10875 		      printf (_("Shared library: [%s]"), name);
10876 
10877 		      if (streq (name, filedata->program_interpreter))
10878 			printf (_(" program interpreter"));
10879 		      break;
10880 
10881 		    case DT_SONAME:
10882 		      printf (_("Library soname: [%s]"), name);
10883 		      break;
10884 
10885 		    case DT_RPATH:
10886 		      printf (_("Library rpath: [%s]"), name);
10887 		      break;
10888 
10889 		    case DT_RUNPATH:
10890 		      printf (_("Library runpath: [%s]"), name);
10891 		      break;
10892 
10893 		    default:
10894 		      print_vma (entry->d_un.d_val, PREFIX_HEX);
10895 		      break;
10896 		    }
10897 		}
10898 	      else
10899 		print_vma (entry->d_un.d_val, PREFIX_HEX);
10900 
10901 	      putchar ('\n');
10902 	    }
10903 	  break;
10904 
10905 	case DT_PLTRELSZ:
10906 	case DT_RELASZ	:
10907 	case DT_STRSZ	:
10908 	case DT_RELSZ	:
10909 	case DT_RELAENT	:
10910 	case DT_SYMENT	:
10911 	case DT_RELENT	:
10912 	  filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
10913 	  /* Fall through.  */
10914 	case DT_PLTPADSZ:
10915 	case DT_MOVEENT	:
10916 	case DT_MOVESZ	:
10917 	case DT_INIT_ARRAYSZ:
10918 	case DT_FINI_ARRAYSZ:
10919 	case DT_GNU_CONFLICTSZ:
10920 	case DT_GNU_LIBLISTSZ:
10921 	  if (do_dynamic)
10922 	    {
10923 	      print_vma (entry->d_un.d_val, UNSIGNED);
10924 	      printf (_(" (bytes)\n"));
10925 	    }
10926 	  break;
10927 
10928 	case DT_VERDEFNUM:
10929 	case DT_VERNEEDNUM:
10930 	case DT_RELACOUNT:
10931 	case DT_RELCOUNT:
10932 	  if (do_dynamic)
10933 	    {
10934 	      print_vma (entry->d_un.d_val, UNSIGNED);
10935 	      putchar ('\n');
10936 	    }
10937 	  break;
10938 
10939 	case DT_SYMINSZ:
10940 	case DT_SYMINENT:
10941 	case DT_SYMINFO:
10942 	case DT_USED:
10943 	case DT_INIT_ARRAY:
10944 	case DT_FINI_ARRAY:
10945 	  if (do_dynamic)
10946 	    {
10947 	      if (entry->d_tag == DT_USED
10948 		  && VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
10949 		{
10950 		  char * name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
10951 
10952 		  if (*name)
10953 		    {
10954 		      printf (_("Not needed object: [%s]\n"), name);
10955 		      break;
10956 		    }
10957 		}
10958 
10959 	      print_vma (entry->d_un.d_val, PREFIX_HEX);
10960 	      putchar ('\n');
10961 	    }
10962 	  break;
10963 
10964 	case DT_BIND_NOW:
10965 	  /* The value of this entry is ignored.  */
10966 	  if (do_dynamic)
10967 	    putchar ('\n');
10968 	  break;
10969 
10970 	case DT_GNU_PRELINKED:
10971 	  if (do_dynamic)
10972 	    {
10973 	      struct tm * tmp;
10974 	      time_t atime = entry->d_un.d_val;
10975 
10976 	      tmp = gmtime (&atime);
10977 	      /* PR 17533 file: 041-1244816-0.004.  */
10978 	      if (tmp == NULL)
10979 		printf (_("<corrupt time val: %lx"),
10980 			(unsigned long) atime);
10981 	      else
10982 		printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
10983 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10984 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10985 
10986 	    }
10987 	  break;
10988 
10989 	case DT_GNU_HASH:
10990 	  filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10991 	  if (do_dynamic)
10992 	    {
10993 	      print_vma (entry->d_un.d_val, PREFIX_HEX);
10994 	      putchar ('\n');
10995 	    }
10996 	  break;
10997 
10998 	default:
10999 	  if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11000 	    filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11001 	      = entry->d_un.d_val;
11002 
11003 	  if (do_dynamic)
11004 	    {
11005 	      switch (filedata->file_header.e_machine)
11006 		{
11007 		case EM_AARCH64:
11008 		  dynamic_section_aarch64_val (entry);
11009 		  break;
11010 		case EM_MIPS:
11011 		case EM_MIPS_RS3_LE:
11012 		  dynamic_section_mips_val (filedata, entry);
11013 		  break;
11014 		case EM_PARISC:
11015 		  dynamic_section_parisc_val (entry);
11016 		  break;
11017 		case EM_IA_64:
11018 		  dynamic_section_ia64_val (entry);
11019 		  break;
11020 		default:
11021 		  print_vma (entry->d_un.d_val, PREFIX_HEX);
11022 		  putchar ('\n');
11023 		}
11024 	    }
11025 	  break;
11026 	}
11027     }
11028 
11029   return TRUE;
11030 }
11031 
11032 static char *
get_ver_flags(unsigned int flags)11033 get_ver_flags (unsigned int flags)
11034 {
11035   static char buff[128];
11036 
11037   buff[0] = 0;
11038 
11039   if (flags == 0)
11040     return _("none");
11041 
11042   if (flags & VER_FLG_BASE)
11043     strcat (buff, "BASE");
11044 
11045   if (flags & VER_FLG_WEAK)
11046     {
11047       if (flags & VER_FLG_BASE)
11048 	strcat (buff, " | ");
11049 
11050       strcat (buff, "WEAK");
11051     }
11052 
11053   if (flags & VER_FLG_INFO)
11054     {
11055       if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
11056 	strcat (buff, " | ");
11057 
11058       strcat (buff, "INFO");
11059     }
11060 
11061   if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11062     {
11063       if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11064 	strcat (buff, " | ");
11065 
11066       strcat (buff, _("<unknown>"));
11067     }
11068 
11069   return buff;
11070 }
11071 
11072 /* Display the contents of the version sections.  */
11073 
11074 static bfd_boolean
process_version_sections(Filedata * filedata)11075 process_version_sections (Filedata * filedata)
11076 {
11077   Elf_Internal_Shdr * section;
11078   unsigned i;
11079   bfd_boolean found = FALSE;
11080 
11081   if (! do_version)
11082     return TRUE;
11083 
11084   for (i = 0, section = filedata->section_headers;
11085        i < filedata->file_header.e_shnum;
11086        i++, section++)
11087     {
11088       switch (section->sh_type)
11089 	{
11090 	case SHT_GNU_verdef:
11091 	  {
11092 	    Elf_External_Verdef * edefs;
11093 	    unsigned long idx;
11094 	    unsigned long cnt;
11095 	    char * endbuf;
11096 
11097 	    found = TRUE;
11098 
11099 	    printf (ngettext ("\nVersion definition section '%s' "
11100 			      "contains %u entry:\n",
11101 			      "\nVersion definition section '%s' "
11102 			      "contains %u entries:\n",
11103 			      section->sh_info),
11104 		    printable_section_name (filedata, section),
11105 		    section->sh_info);
11106 
11107 	    printf (_(" Addr: 0x"));
11108 	    printf_vma (section->sh_addr);
11109 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
11110 		    (unsigned long) section->sh_offset, section->sh_link,
11111 		    printable_section_name_from_index (filedata, section->sh_link));
11112 
11113 	    edefs = (Elf_External_Verdef *)
11114                 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
11115                           _("version definition section"));
11116 	    if (!edefs)
11117 	      break;
11118 	    endbuf = (char *) edefs + section->sh_size;
11119 
11120 	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11121 	      {
11122 		char * vstart;
11123 		Elf_External_Verdef * edef;
11124 		Elf_Internal_Verdef ent;
11125 		Elf_External_Verdaux * eaux;
11126 		Elf_Internal_Verdaux aux;
11127 		unsigned long isum;
11128 		int j;
11129 
11130 		vstart = ((char *) edefs) + idx;
11131 		if (vstart + sizeof (*edef) > endbuf)
11132 		  break;
11133 
11134 		edef = (Elf_External_Verdef *) vstart;
11135 
11136 		ent.vd_version = BYTE_GET (edef->vd_version);
11137 		ent.vd_flags   = BYTE_GET (edef->vd_flags);
11138 		ent.vd_ndx     = BYTE_GET (edef->vd_ndx);
11139 		ent.vd_cnt     = BYTE_GET (edef->vd_cnt);
11140 		ent.vd_hash    = BYTE_GET (edef->vd_hash);
11141 		ent.vd_aux     = BYTE_GET (edef->vd_aux);
11142 		ent.vd_next    = BYTE_GET (edef->vd_next);
11143 
11144 		printf (_("  %#06lx: Rev: %d  Flags: %s"),
11145 			idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11146 
11147 		printf (_("  Index: %d  Cnt: %d  "),
11148 			ent.vd_ndx, ent.vd_cnt);
11149 
11150 		/* Check for overflow.  */
11151 		if (ent.vd_aux > (size_t) (endbuf - vstart))
11152 		  break;
11153 
11154 		vstart += ent.vd_aux;
11155 
11156 		if (vstart + sizeof (*eaux) > endbuf)
11157 		  break;
11158 		eaux = (Elf_External_Verdaux *) vstart;
11159 
11160 		aux.vda_name = BYTE_GET (eaux->vda_name);
11161 		aux.vda_next = BYTE_GET (eaux->vda_next);
11162 
11163 		if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11164 		  printf (_("Name: %s\n"),
11165 			  GET_DYNAMIC_NAME (filedata, aux.vda_name));
11166 		else
11167 		  printf (_("Name index: %ld\n"), aux.vda_name);
11168 
11169 		isum = idx + ent.vd_aux;
11170 
11171 		for (j = 1; j < ent.vd_cnt; j++)
11172 		  {
11173 		    if (aux.vda_next < sizeof (*eaux)
11174 			&& !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11175 		      {
11176 			warn (_("Invalid vda_next field of %lx\n"),
11177 			      aux.vda_next);
11178 			j = ent.vd_cnt;
11179 			break;
11180 		      }
11181 		    /* Check for overflow.  */
11182 		    if (aux.vda_next > (size_t) (endbuf - vstart))
11183 		      break;
11184 
11185 		    isum   += aux.vda_next;
11186 		    vstart += aux.vda_next;
11187 
11188 		    if (vstart + sizeof (*eaux) > endbuf)
11189 		      break;
11190 		    eaux = (Elf_External_Verdaux *) vstart;
11191 
11192 		    aux.vda_name = BYTE_GET (eaux->vda_name);
11193 		    aux.vda_next = BYTE_GET (eaux->vda_next);
11194 
11195 		    if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11196 		      printf (_("  %#06lx: Parent %d: %s\n"),
11197 			      isum, j,
11198 			      GET_DYNAMIC_NAME (filedata, aux.vda_name));
11199 		    else
11200 		      printf (_("  %#06lx: Parent %d, name index: %ld\n"),
11201 			      isum, j, aux.vda_name);
11202 		  }
11203 
11204 		if (j < ent.vd_cnt)
11205 		  printf (_("  Version def aux past end of section\n"));
11206 
11207 		/* PR 17531:
11208 		   file: id:000001,src:000172+005151,op:splice,rep:2.  */
11209 		if (ent.vd_next < sizeof (*edef)
11210 		    && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
11211 		  {
11212 		    warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
11213 		    cnt = section->sh_info;
11214 		    break;
11215 		  }
11216 		if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
11217 		  break;
11218 
11219 		idx += ent.vd_next;
11220 	      }
11221 
11222 	    if (cnt < section->sh_info)
11223 	      printf (_("  Version definition past end of section\n"));
11224 
11225 	    free (edefs);
11226 	  }
11227 	  break;
11228 
11229 	case SHT_GNU_verneed:
11230 	  {
11231 	    Elf_External_Verneed * eneed;
11232 	    unsigned long idx;
11233 	    unsigned long cnt;
11234 	    char * endbuf;
11235 
11236 	    found = TRUE;
11237 
11238 	    printf (ngettext ("\nVersion needs section '%s' "
11239 			      "contains %u entry:\n",
11240 			      "\nVersion needs section '%s' "
11241 			      "contains %u entries:\n",
11242 			      section->sh_info),
11243 		    printable_section_name (filedata, section), section->sh_info);
11244 
11245 	    printf (_(" Addr: 0x"));
11246 	    printf_vma (section->sh_addr);
11247 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
11248 		    (unsigned long) section->sh_offset, section->sh_link,
11249 		    printable_section_name_from_index (filedata, section->sh_link));
11250 
11251 	    eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
11252                                                        section->sh_offset, 1,
11253                                                        section->sh_size,
11254                                                        _("Version Needs section"));
11255 	    if (!eneed)
11256 	      break;
11257 	    endbuf = (char *) eneed + section->sh_size;
11258 
11259 	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11260 	      {
11261 		Elf_External_Verneed * entry;
11262 		Elf_Internal_Verneed ent;
11263 		unsigned long isum;
11264 		int j;
11265 		char * vstart;
11266 
11267 		vstart = ((char *) eneed) + idx;
11268 		if (vstart + sizeof (*entry) > endbuf)
11269 		  break;
11270 
11271 		entry = (Elf_External_Verneed *) vstart;
11272 
11273 		ent.vn_version = BYTE_GET (entry->vn_version);
11274 		ent.vn_cnt     = BYTE_GET (entry->vn_cnt);
11275 		ent.vn_file    = BYTE_GET (entry->vn_file);
11276 		ent.vn_aux     = BYTE_GET (entry->vn_aux);
11277 		ent.vn_next    = BYTE_GET (entry->vn_next);
11278 
11279 		printf (_("  %#06lx: Version: %d"), idx, ent.vn_version);
11280 
11281 		if (VALID_DYNAMIC_NAME (filedata, ent.vn_file))
11282 		  printf (_("  File: %s"),
11283 			  GET_DYNAMIC_NAME (filedata, ent.vn_file));
11284 		else
11285 		  printf (_("  File: %lx"), ent.vn_file);
11286 
11287 		printf (_("  Cnt: %d\n"), ent.vn_cnt);
11288 
11289 		/* Check for overflow.  */
11290 		if (ent.vn_aux > (size_t) (endbuf - vstart))
11291 		  break;
11292 		vstart += ent.vn_aux;
11293 
11294 		for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
11295 		  {
11296 		    Elf_External_Vernaux * eaux;
11297 		    Elf_Internal_Vernaux aux;
11298 
11299 		    if (vstart + sizeof (*eaux) > endbuf)
11300 		      break;
11301 		    eaux = (Elf_External_Vernaux *) vstart;
11302 
11303 		    aux.vna_hash  = BYTE_GET (eaux->vna_hash);
11304 		    aux.vna_flags = BYTE_GET (eaux->vna_flags);
11305 		    aux.vna_other = BYTE_GET (eaux->vna_other);
11306 		    aux.vna_name  = BYTE_GET (eaux->vna_name);
11307 		    aux.vna_next  = BYTE_GET (eaux->vna_next);
11308 
11309 		    if (VALID_DYNAMIC_NAME (filedata, aux.vna_name))
11310 		      printf (_("  %#06lx:   Name: %s"),
11311 			      isum, GET_DYNAMIC_NAME (filedata, aux.vna_name));
11312 		    else
11313 		      printf (_("  %#06lx:   Name index: %lx"),
11314 			      isum, aux.vna_name);
11315 
11316 		    printf (_("  Flags: %s  Version: %d\n"),
11317 			    get_ver_flags (aux.vna_flags), aux.vna_other);
11318 
11319 		    if (aux.vna_next < sizeof (*eaux)
11320 			&& !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
11321 		      {
11322 			warn (_("Invalid vna_next field of %lx\n"),
11323 			      aux.vna_next);
11324 			j = ent.vn_cnt;
11325 			break;
11326 		      }
11327 		    /* Check for overflow.  */
11328 		    if (aux.vna_next > (size_t) (endbuf - vstart))
11329 		      break;
11330 		    isum   += aux.vna_next;
11331 		    vstart += aux.vna_next;
11332 		  }
11333 
11334 		if (j < ent.vn_cnt)
11335 		  warn (_("Missing Version Needs auxillary information\n"));
11336 
11337 		if (ent.vn_next < sizeof (*entry)
11338 		    && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
11339 		  {
11340 		    warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
11341 		    cnt = section->sh_info;
11342 		    break;
11343 		  }
11344 		if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
11345 		  break;
11346 		idx += ent.vn_next;
11347 	      }
11348 
11349 	    if (cnt < section->sh_info)
11350 	      warn (_("Missing Version Needs information\n"));
11351 
11352 	    free (eneed);
11353 	  }
11354 	  break;
11355 
11356 	case SHT_GNU_versym:
11357 	  {
11358 	    Elf_Internal_Shdr * link_section;
11359 	    size_t total;
11360 	    unsigned int cnt;
11361 	    unsigned char * edata;
11362 	    unsigned short * data;
11363 	    char * strtab;
11364 	    Elf_Internal_Sym * symbols;
11365 	    Elf_Internal_Shdr * string_sec;
11366 	    unsigned long num_syms;
11367 	    long off;
11368 
11369 	    if (section->sh_link >= filedata->file_header.e_shnum)
11370 	      break;
11371 
11372 	    link_section = filedata->section_headers + section->sh_link;
11373 	    total = section->sh_size / sizeof (Elf_External_Versym);
11374 
11375 	    if (link_section->sh_link >= filedata->file_header.e_shnum)
11376 	      break;
11377 
11378 	    found = TRUE;
11379 
11380 	    symbols = GET_ELF_SYMBOLS (filedata, link_section, & num_syms);
11381 	    if (symbols == NULL)
11382 	      break;
11383 
11384 	    string_sec = filedata->section_headers + link_section->sh_link;
11385 
11386 	    strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
11387                                         string_sec->sh_size,
11388                                         _("version string table"));
11389 	    if (!strtab)
11390 	      {
11391 		free (symbols);
11392 		break;
11393 	      }
11394 
11395 	    printf (ngettext ("\nVersion symbols section '%s' "
11396 			      "contains %lu entry:\n",
11397 			      "\nVersion symbols section '%s' "
11398 			      "contains %lu entries:\n",
11399 			      total),
11400 		    printable_section_name (filedata, section), (unsigned long) total);
11401 
11402 	    printf (_(" Addr: 0x"));
11403 	    printf_vma (section->sh_addr);
11404 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
11405 		    (unsigned long) section->sh_offset, section->sh_link,
11406 		    printable_section_name (filedata, link_section));
11407 
11408 	    off = offset_from_vma (filedata,
11409 				   filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11410 				   total * sizeof (short));
11411 	    edata = (unsigned char *) get_data (NULL, filedata, off,
11412 						sizeof (short), total,
11413 						_("version symbol data"));
11414 	    if (!edata)
11415 	      {
11416 		free (strtab);
11417 		free (symbols);
11418 		break;
11419 	      }
11420 
11421 	    data = (short unsigned int *) cmalloc (total, sizeof (short));
11422 
11423 	    for (cnt = total; cnt --;)
11424 	      data[cnt] = byte_get (edata + cnt * sizeof (short),
11425 				    sizeof (short));
11426 
11427 	    free (edata);
11428 
11429 	    for (cnt = 0; cnt < total; cnt += 4)
11430 	      {
11431 		int j, nn;
11432 		char *name;
11433 		char *invalid = _("*invalid*");
11434 
11435 		printf ("  %03x:", cnt);
11436 
11437 		for (j = 0; (j < 4) && (cnt + j) < total; ++j)
11438 		  switch (data[cnt + j])
11439 		    {
11440 		    case 0:
11441 		      fputs (_("   0 (*local*)    "), stdout);
11442 		      break;
11443 
11444 		    case 1:
11445 		      fputs (_("   1 (*global*)   "), stdout);
11446 		      break;
11447 
11448 		    default:
11449 		      nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
11450 				   data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
11451 
11452 		      /* If this index value is greater than the size of the symbols
11453 		         array, break to avoid an out-of-bounds read.  */
11454 		      if ((unsigned long)(cnt + j) >= num_syms)
11455 		        {
11456 		          warn (_("invalid index into symbol array\n"));
11457 		          break;
11458 			}
11459 
11460 		      name = NULL;
11461 		      if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
11462 			{
11463 			  Elf_Internal_Verneed ivn;
11464 			  unsigned long offset;
11465 
11466 			  offset = offset_from_vma
11467 			    (filedata,
11468 			     filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
11469 			     sizeof (Elf_External_Verneed));
11470 
11471 			  do
11472 			    {
11473 			      Elf_Internal_Vernaux ivna;
11474 			      Elf_External_Verneed evn;
11475 			      Elf_External_Vernaux evna;
11476 			      unsigned long a_off;
11477 
11478 			      if (get_data (&evn, filedata, offset, sizeof (evn), 1,
11479 					    _("version need")) == NULL)
11480 				break;
11481 
11482 			      ivn.vn_aux  = BYTE_GET (evn.vn_aux);
11483 			      ivn.vn_next = BYTE_GET (evn.vn_next);
11484 
11485 			      a_off = offset + ivn.vn_aux;
11486 
11487 			      do
11488 				{
11489 				  if (get_data (&evna, filedata, a_off, sizeof (evna),
11490 						1, _("version need aux (2)")) == NULL)
11491 				    {
11492 				      ivna.vna_next  = 0;
11493 				      ivna.vna_other = 0;
11494 				    }
11495 				  else
11496 				    {
11497 				      ivna.vna_next  = BYTE_GET (evna.vna_next);
11498 				      ivna.vna_other = BYTE_GET (evna.vna_other);
11499 				    }
11500 
11501 				  a_off += ivna.vna_next;
11502 				}
11503 			      while (ivna.vna_other != data[cnt + j]
11504 				     && ivna.vna_next != 0);
11505 
11506 			      if (ivna.vna_other == data[cnt + j])
11507 				{
11508 				  ivna.vna_name = BYTE_GET (evna.vna_name);
11509 
11510 				  if (ivna.vna_name >= string_sec->sh_size)
11511 				    name = invalid;
11512 				  else
11513 				    name = strtab + ivna.vna_name;
11514 				  break;
11515 				}
11516 
11517 			      offset += ivn.vn_next;
11518 			    }
11519 			  while (ivn.vn_next);
11520 			}
11521 
11522 		      if (data[cnt + j] != 0x8001
11523 			  && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
11524 			{
11525 			  Elf_Internal_Verdef ivd;
11526 			  Elf_External_Verdef evd;
11527 			  unsigned long offset;
11528 
11529 			  offset = offset_from_vma
11530 			    (filedata,
11531 			     filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
11532 			     sizeof evd);
11533 
11534 			  do
11535 			    {
11536 			      if (get_data (&evd, filedata, offset, sizeof (evd), 1,
11537 					    _("version def")) == NULL)
11538 				{
11539 				  ivd.vd_next = 0;
11540 				  /* PR 17531: file: 046-1082287-0.004.  */
11541 				  ivd.vd_ndx  = (data[cnt + j] & VERSYM_VERSION) + 1;
11542 				  break;
11543 				}
11544 			      else
11545 				{
11546 				  ivd.vd_next = BYTE_GET (evd.vd_next);
11547 				  ivd.vd_ndx  = BYTE_GET (evd.vd_ndx);
11548 				}
11549 
11550 			      offset += ivd.vd_next;
11551 			    }
11552 			  while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
11553 				 && ivd.vd_next != 0);
11554 
11555 			  if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
11556 			    {
11557 			      Elf_External_Verdaux evda;
11558 			      Elf_Internal_Verdaux ivda;
11559 
11560 			      ivd.vd_aux = BYTE_GET (evd.vd_aux);
11561 
11562 			      if (get_data (&evda, filedata,
11563 					    offset - ivd.vd_next + ivd.vd_aux,
11564 					    sizeof (evda), 1,
11565 					    _("version def aux")) == NULL)
11566 				break;
11567 
11568 			      ivda.vda_name = BYTE_GET (evda.vda_name);
11569 
11570 			      if (ivda.vda_name >= string_sec->sh_size)
11571 				name = invalid;
11572 			      else if (name != NULL && name != invalid)
11573 				name = _("*both*");
11574 			      else
11575 				name = strtab + ivda.vda_name;
11576 			    }
11577 			}
11578 		      if (name != NULL)
11579 			nn += printf ("(%s%-*s",
11580 				      name,
11581 				      12 - (int) strlen (name),
11582 				      ")");
11583 
11584 		      if (nn < 18)
11585 			printf ("%*c", 18 - nn, ' ');
11586 		    }
11587 
11588 		putchar ('\n');
11589 	      }
11590 
11591 	    free (data);
11592 	    free (strtab);
11593 	    free (symbols);
11594 	  }
11595 	  break;
11596 
11597 	default:
11598 	  break;
11599 	}
11600     }
11601 
11602   if (! found)
11603     printf (_("\nNo version information found in this file.\n"));
11604 
11605   return TRUE;
11606 }
11607 
11608 static const char *
get_symbol_binding(Filedata * filedata,unsigned int binding)11609 get_symbol_binding (Filedata * filedata, unsigned int binding)
11610 {
11611   static char buff[64];
11612 
11613   switch (binding)
11614     {
11615     case STB_LOCAL:	return "LOCAL";
11616     case STB_GLOBAL:	return "GLOBAL";
11617     case STB_WEAK:	return "WEAK";
11618     default:
11619       if (binding >= STB_LOPROC && binding <= STB_HIPROC)
11620 	snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
11621 		  binding);
11622       else if (binding >= STB_LOOS && binding <= STB_HIOS)
11623 	{
11624 	  if (binding == STB_GNU_UNIQUE
11625 	      && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
11626 	    return "UNIQUE";
11627 	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
11628 	}
11629       else
11630 	snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
11631       return buff;
11632     }
11633 }
11634 
11635 static const char *
get_symbol_type(Filedata * filedata,unsigned int type)11636 get_symbol_type (Filedata * filedata, unsigned int type)
11637 {
11638   static char buff[64];
11639 
11640   switch (type)
11641     {
11642     case STT_NOTYPE:	return "NOTYPE";
11643     case STT_OBJECT:	return "OBJECT";
11644     case STT_FUNC:	return "FUNC";
11645     case STT_SECTION:	return "SECTION";
11646     case STT_FILE:	return "FILE";
11647     case STT_COMMON:	return "COMMON";
11648     case STT_TLS:	return "TLS";
11649     case STT_RELC:      return "RELC";
11650     case STT_SRELC:     return "SRELC";
11651     default:
11652       if (type >= STT_LOPROC && type <= STT_HIPROC)
11653 	{
11654 	  if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
11655 	    return "THUMB_FUNC";
11656 
11657 	  if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
11658 	    return "REGISTER";
11659 
11660 	  if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
11661 	    return "PARISC_MILLI";
11662 
11663 	  snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
11664 	}
11665       else if (type >= STT_LOOS && type <= STT_HIOS)
11666 	{
11667 	  if (filedata->file_header.e_machine == EM_PARISC)
11668 	    {
11669 	      if (type == STT_HP_OPAQUE)
11670 		return "HP_OPAQUE";
11671 	      if (type == STT_HP_STUB)
11672 		return "HP_STUB";
11673 	    }
11674 
11675 	  if (type == STT_GNU_IFUNC
11676 	      && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
11677 		  || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
11678 	    return "IFUNC";
11679 
11680 	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
11681 	}
11682       else
11683 	snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
11684       return buff;
11685     }
11686 }
11687 
11688 static const char *
get_symbol_visibility(unsigned int visibility)11689 get_symbol_visibility (unsigned int visibility)
11690 {
11691   switch (visibility)
11692     {
11693     case STV_DEFAULT:	return "DEFAULT";
11694     case STV_INTERNAL:	return "INTERNAL";
11695     case STV_HIDDEN:	return "HIDDEN";
11696     case STV_PROTECTED: return "PROTECTED";
11697     default:
11698       error (_("Unrecognized visibility value: %u\n"), visibility);
11699       return _("<unknown>");
11700     }
11701 }
11702 
11703 static const char *
get_alpha_symbol_other(unsigned int other)11704 get_alpha_symbol_other (unsigned int other)
11705 {
11706   switch (other)
11707     {
11708     case STO_ALPHA_NOPV:       return "NOPV";
11709     case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
11710     default:
11711       error (_("Unrecognized alpha specific other value: %u\n"), other);
11712       return _("<unknown>");
11713     }
11714 }
11715 
11716 static const char *
get_solaris_symbol_visibility(unsigned int visibility)11717 get_solaris_symbol_visibility (unsigned int visibility)
11718 {
11719   switch (visibility)
11720     {
11721     case 4: return "EXPORTED";
11722     case 5: return "SINGLETON";
11723     case 6: return "ELIMINATE";
11724     default: return get_symbol_visibility (visibility);
11725     }
11726 }
11727 
11728 static const char *
get_aarch64_symbol_other(unsigned int other)11729 get_aarch64_symbol_other (unsigned int other)
11730 {
11731   static char buf[32];
11732 
11733   if (other & STO_AARCH64_VARIANT_PCS)
11734     {
11735       other &= ~STO_AARCH64_VARIANT_PCS;
11736       if (other == 0)
11737 	return "VARIANT_PCS";
11738       snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
11739       return buf;
11740     }
11741   return NULL;
11742 }
11743 
11744 static const char *
get_mips_symbol_other(unsigned int other)11745 get_mips_symbol_other (unsigned int other)
11746 {
11747   switch (other)
11748     {
11749     case STO_OPTIONAL:      return "OPTIONAL";
11750     case STO_MIPS_PLT:      return "MIPS PLT";
11751     case STO_MIPS_PIC:      return "MIPS PIC";
11752     case STO_MICROMIPS:     return "MICROMIPS";
11753     case STO_MICROMIPS | STO_MIPS_PIC:      return "MICROMIPS, MIPS PIC";
11754     case STO_MIPS16:        return "MIPS16";
11755     default:	            return NULL;
11756     }
11757 }
11758 
11759 static const char *
get_ia64_symbol_other(Filedata * filedata,unsigned int other)11760 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
11761 {
11762   if (is_ia64_vms (filedata))
11763     {
11764       static char res[32];
11765 
11766       res[0] = 0;
11767 
11768       /* Function types is for images and .STB files only.  */
11769       switch (filedata->file_header.e_type)
11770         {
11771         case ET_DYN:
11772         case ET_EXEC:
11773           switch (VMS_ST_FUNC_TYPE (other))
11774             {
11775             case VMS_SFT_CODE_ADDR:
11776               strcat (res, " CA");
11777               break;
11778             case VMS_SFT_SYMV_IDX:
11779               strcat (res, " VEC");
11780               break;
11781             case VMS_SFT_FD:
11782               strcat (res, " FD");
11783               break;
11784             case VMS_SFT_RESERVE:
11785               strcat (res, " RSV");
11786               break;
11787             default:
11788 	      warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
11789 		    VMS_ST_FUNC_TYPE (other));
11790 	      strcat (res, " <unknown>");
11791 	      break;
11792             }
11793           break;
11794         default:
11795           break;
11796         }
11797       switch (VMS_ST_LINKAGE (other))
11798         {
11799         case VMS_STL_IGNORE:
11800           strcat (res, " IGN");
11801           break;
11802         case VMS_STL_RESERVE:
11803           strcat (res, " RSV");
11804           break;
11805         case VMS_STL_STD:
11806           strcat (res, " STD");
11807           break;
11808         case VMS_STL_LNK:
11809           strcat (res, " LNK");
11810           break;
11811         default:
11812 	  warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
11813 		VMS_ST_LINKAGE (other));
11814 	  strcat (res, " <unknown>");
11815 	  break;
11816         }
11817 
11818       if (res[0] != 0)
11819         return res + 1;
11820       else
11821         return res;
11822     }
11823   return NULL;
11824 }
11825 
11826 static const char *
get_ppc64_symbol_other(unsigned int other)11827 get_ppc64_symbol_other (unsigned int other)
11828 {
11829   if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
11830     return NULL;
11831 
11832   other >>= STO_PPC64_LOCAL_BIT;
11833   if (other <= 6)
11834     {
11835       static char buf[64];
11836       if (other >= 2)
11837 	other = ppc64_decode_local_entry (other);
11838       snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
11839       return buf;
11840     }
11841   return NULL;
11842 }
11843 
11844 static const char *
get_symbol_other(Filedata * filedata,unsigned int other)11845 get_symbol_other (Filedata * filedata, unsigned int other)
11846 {
11847   const char * result = NULL;
11848   static char buff [64];
11849 
11850   if (other == 0)
11851     return "";
11852 
11853   switch (filedata->file_header.e_machine)
11854     {
11855     case EM_ALPHA:
11856       result = get_alpha_symbol_other (other);
11857       break;
11858     case EM_AARCH64:
11859       result = get_aarch64_symbol_other (other);
11860       break;
11861     case EM_MIPS:
11862       result = get_mips_symbol_other (other);
11863       break;
11864     case EM_IA_64:
11865       result = get_ia64_symbol_other (filedata, other);
11866       break;
11867     case EM_PPC64:
11868       result = get_ppc64_symbol_other (other);
11869       break;
11870     default:
11871       result = NULL;
11872       break;
11873     }
11874 
11875   if (result)
11876     return result;
11877 
11878   snprintf (buff, sizeof buff, _("<other>: %x"), other);
11879   return buff;
11880 }
11881 
11882 static const char *
get_symbol_index_type(Filedata * filedata,unsigned int type)11883 get_symbol_index_type (Filedata * filedata, unsigned int type)
11884 {
11885   static char buff[32];
11886 
11887   switch (type)
11888     {
11889     case SHN_UNDEF:	return "UND";
11890     case SHN_ABS:	return "ABS";
11891     case SHN_COMMON:	return "COM";
11892     default:
11893       if (type == SHN_IA_64_ANSI_COMMON
11894 	  && filedata->file_header.e_machine == EM_IA_64
11895 	  && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
11896 	return "ANSI_COM";
11897       else if ((filedata->file_header.e_machine == EM_X86_64
11898 		|| filedata->file_header.e_machine == EM_L1OM
11899 		|| filedata->file_header.e_machine == EM_K1OM)
11900 	       && type == SHN_X86_64_LCOMMON)
11901 	return "LARGE_COM";
11902       else if ((type == SHN_MIPS_SCOMMON
11903 		&& filedata->file_header.e_machine == EM_MIPS)
11904 	       || (type == SHN_TIC6X_SCOMMON
11905 		   && filedata->file_header.e_machine == EM_TI_C6000))
11906 	return "SCOM";
11907       else if (type == SHN_MIPS_SUNDEFINED
11908 	       && filedata->file_header.e_machine == EM_MIPS)
11909 	return "SUND";
11910       else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
11911 	sprintf (buff, "PRC[0x%04x]", type & 0xffff);
11912       else if (type >= SHN_LOOS && type <= SHN_HIOS)
11913 	sprintf (buff, "OS [0x%04x]", type & 0xffff);
11914       else if (type >= SHN_LORESERVE)
11915 	sprintf (buff, "RSV[0x%04x]", type & 0xffff);
11916       else if (filedata->file_header.e_shnum != 0
11917 	       && type >= filedata->file_header.e_shnum)
11918 	sprintf (buff, _("bad section index[%3d]"), type);
11919       else
11920 	sprintf (buff, "%3d", type);
11921       break;
11922     }
11923 
11924   return buff;
11925 }
11926 
11927 static const char *
get_symbol_version_string(Filedata * filedata,bfd_boolean is_dynsym,const char * strtab,unsigned long int strtab_size,unsigned int si,Elf_Internal_Sym * psym,enum versioned_symbol_info * sym_info,unsigned short * vna_other)11928 get_symbol_version_string (Filedata *                   filedata,
11929 			   bfd_boolean                  is_dynsym,
11930 			   const char *                 strtab,
11931 			   unsigned long int            strtab_size,
11932 			   unsigned int                 si,
11933 			   Elf_Internal_Sym *           psym,
11934 			   enum versioned_symbol_info * sym_info,
11935 			   unsigned short *             vna_other)
11936 {
11937   unsigned char data[2];
11938   unsigned short vers_data;
11939   unsigned long offset;
11940   unsigned short max_vd_ndx;
11941 
11942   if (!is_dynsym
11943       || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
11944     return NULL;
11945 
11946   offset = offset_from_vma (filedata,
11947 			    filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11948 			    sizeof data + si * sizeof (vers_data));
11949 
11950   if (get_data (&data, filedata, offset + si * sizeof (vers_data),
11951 		sizeof (data), 1, _("version data")) == NULL)
11952     return NULL;
11953 
11954   vers_data = byte_get (data, 2);
11955 
11956   if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
11957     return NULL;
11958 
11959   *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
11960   max_vd_ndx = 0;
11961 
11962   /* Usually we'd only see verdef for defined symbols, and verneed for
11963      undefined symbols.  However, symbols defined by the linker in
11964      .dynbss for variables copied from a shared library in order to
11965      avoid text relocations are defined yet have verneed.  We could
11966      use a heuristic to detect the special case, for example, check
11967      for verneed first on symbols defined in SHT_NOBITS sections, but
11968      it is simpler and more reliable to just look for both verdef and
11969      verneed.  .dynbss might not be mapped to a SHT_NOBITS section.  */
11970 
11971   if (psym->st_shndx != SHN_UNDEF
11972       && vers_data != 0x8001
11973       && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
11974     {
11975       Elf_Internal_Verdef ivd;
11976       Elf_Internal_Verdaux ivda;
11977       Elf_External_Verdaux evda;
11978       unsigned long off;
11979 
11980       off = offset_from_vma (filedata,
11981 			     filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
11982 			     sizeof (Elf_External_Verdef));
11983 
11984       do
11985 	{
11986 	  Elf_External_Verdef evd;
11987 
11988 	  if (get_data (&evd, filedata, off, sizeof (evd), 1,
11989 			_("version def")) == NULL)
11990 	    {
11991 	      ivd.vd_ndx = 0;
11992 	      ivd.vd_aux = 0;
11993 	      ivd.vd_next = 0;
11994 	      ivd.vd_flags = 0;
11995 	    }
11996 	  else
11997 	    {
11998 	      ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
11999 	      ivd.vd_aux = BYTE_GET (evd.vd_aux);
12000 	      ivd.vd_next = BYTE_GET (evd.vd_next);
12001 	      ivd.vd_flags = BYTE_GET (evd.vd_flags);
12002 	    }
12003 
12004 	  if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12005 	    max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12006 
12007 	  off += ivd.vd_next;
12008 	}
12009       while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
12010 
12011       if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12012 	{
12013 	  if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
12014 	    return NULL;
12015 
12016 	  off -= ivd.vd_next;
12017 	  off += ivd.vd_aux;
12018 
12019 	  if (get_data (&evda, filedata, off, sizeof (evda), 1,
12020 			_("version def aux")) != NULL)
12021 	    {
12022 	      ivda.vda_name = BYTE_GET (evda.vda_name);
12023 
12024 	      if (psym->st_name != ivda.vda_name)
12025 		return (ivda.vda_name < strtab_size
12026 			? strtab + ivda.vda_name : _("<corrupt>"));
12027 	    }
12028 	}
12029     }
12030 
12031   if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12032     {
12033       Elf_External_Verneed evn;
12034       Elf_Internal_Verneed ivn;
12035       Elf_Internal_Vernaux ivna;
12036 
12037       offset = offset_from_vma (filedata,
12038 				filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12039 				sizeof evn);
12040       do
12041 	{
12042 	  unsigned long vna_off;
12043 
12044 	  if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12045 			_("version need")) == NULL)
12046 	    {
12047 	      ivna.vna_next = 0;
12048 	      ivna.vna_other = 0;
12049 	      ivna.vna_name = 0;
12050 	      break;
12051 	    }
12052 
12053 	  ivn.vn_aux  = BYTE_GET (evn.vn_aux);
12054 	  ivn.vn_next = BYTE_GET (evn.vn_next);
12055 
12056 	  vna_off = offset + ivn.vn_aux;
12057 
12058 	  do
12059 	    {
12060 	      Elf_External_Vernaux evna;
12061 
12062 	      if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
12063 			    _("version need aux (3)")) == NULL)
12064 		{
12065 		  ivna.vna_next = 0;
12066 		  ivna.vna_other = 0;
12067 		  ivna.vna_name = 0;
12068 		}
12069 	      else
12070 		{
12071 		  ivna.vna_other = BYTE_GET (evna.vna_other);
12072 		  ivna.vna_next  = BYTE_GET (evna.vna_next);
12073 		  ivna.vna_name  = BYTE_GET (evna.vna_name);
12074 		}
12075 
12076 	      vna_off += ivna.vna_next;
12077 	    }
12078 	  while (ivna.vna_other != vers_data && ivna.vna_next != 0);
12079 
12080 	  if (ivna.vna_other == vers_data)
12081 	    break;
12082 
12083 	  offset += ivn.vn_next;
12084 	}
12085       while (ivn.vn_next != 0);
12086 
12087       if (ivna.vna_other == vers_data)
12088 	{
12089 	  *sym_info = symbol_undefined;
12090 	  *vna_other = ivna.vna_other;
12091 	  return (ivna.vna_name < strtab_size
12092 		  ? strtab + ivna.vna_name : _("<corrupt>"));
12093 	}
12094       else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12095 	       && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12096 	return _("<corrupt>");
12097     }
12098   return NULL;
12099 }
12100 
12101 static void
print_dynamic_symbol(Filedata * filedata,unsigned long si,Elf_Internal_Sym * symtab,Elf_Internal_Shdr * section,char * strtab,size_t strtab_size)12102 print_dynamic_symbol (Filedata *filedata, unsigned long si,
12103 		      Elf_Internal_Sym *symtab,
12104 		      Elf_Internal_Shdr *section,
12105 		      char *strtab, size_t strtab_size)
12106 {
12107   const char *version_string;
12108   enum versioned_symbol_info sym_info;
12109   unsigned short vna_other;
12110   Elf_Internal_Sym *psym = symtab + si;
12111 
12112   printf ("%6ld: ", si);
12113   print_vma (psym->st_value, LONG_HEX);
12114   putchar (' ');
12115   print_vma (psym->st_size, DEC_5);
12116   printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12117   printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12118   if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12119     printf (" %-7s",  get_solaris_symbol_visibility (psym->st_other));
12120   else
12121     {
12122       unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
12123 
12124       printf (" %-7s", get_symbol_visibility (vis));
12125       /* Check to see if any other bits in the st_other field are set.
12126 	 Note - displaying this information disrupts the layout of the
12127 	 table being generated, but for the moment this case is very rare.  */
12128       if (psym->st_other ^ vis)
12129 	printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
12130     }
12131   printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
12132 
12133   bfd_boolean is_valid = VALID_SYMBOL_NAME (strtab, strtab_size,
12134 					    psym->st_name);
12135   const char * sstr = is_valid  ? strtab + psym->st_name : _("<corrupt>");
12136 
12137   version_string
12138     = get_symbol_version_string (filedata,
12139 				 (section == NULL
12140 				  || section->sh_type == SHT_DYNSYM),
12141 				 strtab, strtab_size, si,
12142 				 psym, &sym_info, &vna_other);
12143 
12144   int len_avail = 21;
12145   if (! do_wide && version_string != NULL)
12146     {
12147       char buffer[16];
12148 
12149       len_avail -= 1 + strlen (version_string);
12150 
12151       if (sym_info == symbol_undefined)
12152 	len_avail -= sprintf (buffer," (%d)", vna_other);
12153       else if (sym_info != symbol_hidden)
12154 	len_avail -= 1;
12155     }
12156 
12157   print_symbol (len_avail, sstr);
12158 
12159   if (version_string)
12160     {
12161       if (sym_info == symbol_undefined)
12162 	printf ("@%s (%d)", version_string, vna_other);
12163       else
12164 	printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
12165 		version_string);
12166     }
12167 
12168   putchar ('\n');
12169 
12170   if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
12171       && section != NULL
12172       && si >= section->sh_info
12173       /* Irix 5 and 6 MIPS binaries are known to ignore this requirement.  */
12174       && filedata->file_header.e_machine != EM_MIPS
12175       /* Solaris binaries have been found to violate this requirement as
12176 	 well.  Not sure if this is a bug or an ABI requirement.  */
12177       && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12178     warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
12179 	  si, printable_section_name (filedata, section), section->sh_info);
12180 }
12181 
12182 /* Dump the symbol table.  */
12183 static bfd_boolean
process_symbol_table(Filedata * filedata)12184 process_symbol_table (Filedata * filedata)
12185 {
12186   Elf_Internal_Shdr * section;
12187 
12188   if (!do_syms && !do_dyn_syms && !do_histogram)
12189     return TRUE;
12190 
12191   if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
12192       && do_syms
12193       && do_using_dynamic
12194       && filedata->dynamic_strings != NULL
12195       && filedata->dynamic_symbols != NULL)
12196     {
12197       unsigned long si;
12198 
12199       printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
12200 			"\nSymbol table for image contains %lu entries:\n",
12201 			filedata->num_dynamic_syms),
12202 	      filedata->num_dynamic_syms);
12203       if (is_32bit_elf)
12204 	printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
12205       else
12206 	printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
12207 
12208       for (si = 0; si < filedata->num_dynamic_syms; si++)
12209 	print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
12210 			      filedata->dynamic_strings,
12211 			      filedata->dynamic_strings_length);
12212     }
12213   else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
12214 	   && filedata->section_headers != NULL)
12215     {
12216       unsigned int i;
12217 
12218       for (i = 0, section = filedata->section_headers;
12219 	   i < filedata->file_header.e_shnum;
12220 	   i++, section++)
12221 	{
12222 	  char * strtab = NULL;
12223 	  unsigned long int strtab_size = 0;
12224 	  Elf_Internal_Sym * symtab;
12225 	  unsigned long si, num_syms;
12226 
12227 	  if ((section->sh_type != SHT_SYMTAB
12228 	       && section->sh_type != SHT_DYNSYM)
12229 	      || (!do_syms
12230 		  && section->sh_type == SHT_SYMTAB))
12231 	    continue;
12232 
12233 	  if (section->sh_entsize == 0)
12234 	    {
12235 	      printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
12236 		      printable_section_name (filedata, section));
12237 	      continue;
12238 	    }
12239 
12240 	  num_syms = section->sh_size / section->sh_entsize;
12241 	  printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
12242 			    "\nSymbol table '%s' contains %lu entries:\n",
12243 			    num_syms),
12244 		  printable_section_name (filedata, section),
12245 		  num_syms);
12246 
12247 	  if (is_32bit_elf)
12248 	    printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
12249 	  else
12250 	    printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
12251 
12252 	  symtab = GET_ELF_SYMBOLS (filedata, section, & num_syms);
12253 	  if (symtab == NULL)
12254 	    continue;
12255 
12256 	  if (section->sh_link == filedata->file_header.e_shstrndx)
12257 	    {
12258 	      strtab = filedata->string_table;
12259 	      strtab_size = filedata->string_table_length;
12260 	    }
12261 	  else if (section->sh_link < filedata->file_header.e_shnum)
12262 	    {
12263 	      Elf_Internal_Shdr * string_sec;
12264 
12265 	      string_sec = filedata->section_headers + section->sh_link;
12266 
12267 	      strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
12268                                           1, string_sec->sh_size,
12269                                           _("string table"));
12270 	      strtab_size = strtab != NULL ? string_sec->sh_size : 0;
12271 	    }
12272 
12273 	  for (si = 0; si < num_syms; si++)
12274 	    print_dynamic_symbol (filedata, si, symtab, section,
12275 				  strtab, strtab_size);
12276 
12277 	  free (symtab);
12278 	  if (strtab != filedata->string_table)
12279 	    free (strtab);
12280 	}
12281     }
12282   else if (do_syms)
12283     printf
12284       (_("\nDynamic symbol information is not available for displaying symbols.\n"));
12285 
12286   if (do_histogram && filedata->buckets != NULL)
12287     {
12288       unsigned long * lengths;
12289       unsigned long * counts;
12290       unsigned long hn;
12291       bfd_vma si;
12292       unsigned long maxlength = 0;
12293       unsigned long nzero_counts = 0;
12294       unsigned long nsyms = 0;
12295       char *visited;
12296 
12297       printf (ngettext ("\nHistogram for bucket list length "
12298 			"(total of %lu bucket):\n",
12299 			"\nHistogram for bucket list length "
12300 			"(total of %lu buckets):\n",
12301 			(unsigned long) filedata->nbuckets),
12302 	      (unsigned long) filedata->nbuckets);
12303 
12304       lengths = (unsigned long *) calloc (filedata->nbuckets,
12305 					  sizeof (*lengths));
12306       if (lengths == NULL)
12307 	{
12308 	  error (_("Out of memory allocating space for histogram buckets\n"));
12309 	  goto err_out;
12310 	}
12311       visited = xcmalloc (filedata->nchains, 1);
12312       memset (visited, 0, filedata->nchains);
12313 
12314       printf (_(" Length  Number     %% of total  Coverage\n"));
12315       for (hn = 0; hn < filedata->nbuckets; ++hn)
12316 	{
12317 	  for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
12318 	    {
12319 	      ++nsyms;
12320 	      if (maxlength < ++lengths[hn])
12321 		++maxlength;
12322 	      if (si >= filedata->nchains || visited[si])
12323 		{
12324 		  error (_("histogram chain is corrupt\n"));
12325 		  break;
12326 		}
12327 	      visited[si] = 1;
12328 	    }
12329 	}
12330       free (visited);
12331 
12332       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
12333       if (counts == NULL)
12334 	{
12335 	  free (lengths);
12336 	  error (_("Out of memory allocating space for histogram counts\n"));
12337 	  goto err_out;
12338 	}
12339 
12340       for (hn = 0; hn < filedata->nbuckets; ++hn)
12341 	++counts[lengths[hn]];
12342 
12343       if (filedata->nbuckets > 0)
12344 	{
12345 	  unsigned long i;
12346 	  printf ("      0  %-10lu (%5.1f%%)\n",
12347 		  counts[0], (counts[0] * 100.0) / filedata->nbuckets);
12348 	  for (i = 1; i <= maxlength; ++i)
12349 	    {
12350 	      nzero_counts += counts[i] * i;
12351 	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
12352 		      i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
12353 		      (nzero_counts * 100.0) / nsyms);
12354 	    }
12355 	}
12356 
12357       free (counts);
12358       free (lengths);
12359     }
12360 
12361   free (filedata->buckets);
12362   filedata->buckets = NULL;
12363   filedata->nbuckets = 0;
12364   free (filedata->chains);
12365   filedata->chains = NULL;
12366 
12367   if (do_histogram && filedata->gnubuckets != NULL)
12368     {
12369       unsigned long * lengths;
12370       unsigned long * counts;
12371       unsigned long hn;
12372       unsigned long maxlength = 0;
12373       unsigned long nzero_counts = 0;
12374       unsigned long nsyms = 0;
12375 
12376       printf (ngettext ("\nHistogram for `%s' bucket list length "
12377 			"(total of %lu bucket):\n",
12378 			"\nHistogram for `%s' bucket list length "
12379 			"(total of %lu buckets):\n",
12380 			(unsigned long) filedata->ngnubuckets),
12381 	      GNU_HASH_SECTION_NAME (filedata),
12382 	      (unsigned long) filedata->ngnubuckets);
12383 
12384       lengths = (unsigned long *) calloc (filedata->ngnubuckets,
12385 					  sizeof (*lengths));
12386       if (lengths == NULL)
12387 	{
12388 	  error (_("Out of memory allocating space for gnu histogram buckets\n"));
12389 	  goto err_out;
12390 	}
12391 
12392       printf (_(" Length  Number     %% of total  Coverage\n"));
12393 
12394       for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12395 	if (filedata->gnubuckets[hn] != 0)
12396 	  {
12397 	    bfd_vma off, length = 1;
12398 
12399 	    for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
12400 		 /* PR 17531 file: 010-77222-0.004.  */
12401 		 off < filedata->ngnuchains
12402 		   && (filedata->gnuchains[off] & 1) == 0;
12403 		 ++off)
12404 	      ++length;
12405 	    lengths[hn] = length;
12406 	    if (length > maxlength)
12407 	      maxlength = length;
12408 	    nsyms += length;
12409 	  }
12410 
12411       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
12412       if (counts == NULL)
12413 	{
12414 	  free (lengths);
12415 	  error (_("Out of memory allocating space for gnu histogram counts\n"));
12416 	  goto err_out;
12417 	}
12418 
12419       for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12420 	++counts[lengths[hn]];
12421 
12422       if (filedata->ngnubuckets > 0)
12423 	{
12424 	  unsigned long j;
12425 	  printf ("      0  %-10lu (%5.1f%%)\n",
12426 		  counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
12427 	  for (j = 1; j <= maxlength; ++j)
12428 	    {
12429 	      nzero_counts += counts[j] * j;
12430 	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
12431 		      j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
12432 		      (nzero_counts * 100.0) / nsyms);
12433 	    }
12434 	}
12435 
12436       free (counts);
12437       free (lengths);
12438     }
12439   free (filedata->gnubuckets);
12440   filedata->gnubuckets = NULL;
12441   filedata->ngnubuckets = 0;
12442   free (filedata->gnuchains);
12443   filedata->gnuchains = NULL;
12444   filedata->ngnuchains = 0;
12445   free (filedata->mipsxlat);
12446   filedata->mipsxlat = NULL;
12447   return TRUE;
12448 
12449  err_out:
12450   free (filedata->gnubuckets);
12451   filedata->gnubuckets = NULL;
12452   filedata->ngnubuckets = 0;
12453   free (filedata->gnuchains);
12454   filedata->gnuchains = NULL;
12455   filedata->ngnuchains = 0;
12456   free (filedata->mipsxlat);
12457   filedata->mipsxlat = NULL;
12458   free (filedata->buckets);
12459   filedata->buckets = NULL;
12460   filedata->nbuckets = 0;
12461   free (filedata->chains);
12462   filedata->chains = NULL;
12463   return FALSE;
12464 }
12465 
12466 static bfd_boolean
process_syminfo(Filedata * filedata ATTRIBUTE_UNUSED)12467 process_syminfo (Filedata * filedata ATTRIBUTE_UNUSED)
12468 {
12469   unsigned int i;
12470 
12471   if (filedata->dynamic_syminfo == NULL
12472       || !do_dynamic)
12473     /* No syminfo, this is ok.  */
12474     return TRUE;
12475 
12476   /* There better should be a dynamic symbol section.  */
12477   if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
12478     return FALSE;
12479 
12480   if (filedata->dynamic_addr)
12481     printf (ngettext ("\nDynamic info segment at offset 0x%lx "
12482 		      "contains %d entry:\n",
12483 		      "\nDynamic info segment at offset 0x%lx "
12484 		      "contains %d entries:\n",
12485 		      filedata->dynamic_syminfo_nent),
12486 	    filedata->dynamic_syminfo_offset, filedata->dynamic_syminfo_nent);
12487 
12488   printf (_(" Num: Name                           BoundTo     Flags\n"));
12489   for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
12490     {
12491       unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
12492 
12493       printf ("%4d: ", i);
12494       if (i >= filedata->num_dynamic_syms)
12495 	printf (_("<corrupt index>"));
12496       else if (VALID_DYNAMIC_NAME (filedata, filedata->dynamic_symbols[i].st_name))
12497 	print_symbol (30, GET_DYNAMIC_NAME (filedata,
12498 					    filedata->dynamic_symbols[i].st_name));
12499       else
12500 	printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
12501       putchar (' ');
12502 
12503       switch (filedata->dynamic_syminfo[i].si_boundto)
12504 	{
12505 	case SYMINFO_BT_SELF:
12506 	  fputs ("SELF       ", stdout);
12507 	  break;
12508 	case SYMINFO_BT_PARENT:
12509 	  fputs ("PARENT     ", stdout);
12510 	  break;
12511 	default:
12512 	  if (filedata->dynamic_syminfo[i].si_boundto > 0
12513 	      && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
12514 	      && VALID_DYNAMIC_NAME (filedata,
12515 				     filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
12516 	    {
12517 	      print_symbol (10, GET_DYNAMIC_NAME (filedata,
12518 						  filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
12519 	      putchar (' ' );
12520 	    }
12521 	  else
12522 	    printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
12523 	  break;
12524 	}
12525 
12526       if (flags & SYMINFO_FLG_DIRECT)
12527 	printf (" DIRECT");
12528       if (flags & SYMINFO_FLG_PASSTHRU)
12529 	printf (" PASSTHRU");
12530       if (flags & SYMINFO_FLG_COPY)
12531 	printf (" COPY");
12532       if (flags & SYMINFO_FLG_LAZYLOAD)
12533 	printf (" LAZYLOAD");
12534 
12535       puts ("");
12536     }
12537 
12538   return TRUE;
12539 }
12540 
12541 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
12542    is contained by the region START .. END.  The types of ADDR, START
12543    and END should all be the same.  Note both ADDR + NELEM and END
12544    point to just beyond the end of the regions that are being tested.  */
12545 #define IN_RANGE(START,END,ADDR,NELEM)		\
12546   (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
12547 
12548 /* Check to see if the given reloc needs to be handled in a target specific
12549    manner.  If so then process the reloc and return TRUE otherwise return
12550    FALSE.
12551 
12552    If called with reloc == NULL, then this is a signal that reloc processing
12553    for the current section has finished, and any saved state should be
12554    discarded.  */
12555 
12556 static bfd_boolean
target_specific_reloc_handling(Filedata * filedata,Elf_Internal_Rela * reloc,unsigned char * start,unsigned char * end,Elf_Internal_Sym * symtab,unsigned long num_syms)12557 target_specific_reloc_handling (Filedata *           filedata,
12558 				Elf_Internal_Rela *  reloc,
12559 				unsigned char *      start,
12560 				unsigned char *      end,
12561 				Elf_Internal_Sym *   symtab,
12562 				unsigned long        num_syms)
12563 {
12564   unsigned int reloc_type = 0;
12565   unsigned long sym_index = 0;
12566 
12567   if (reloc)
12568     {
12569       reloc_type = get_reloc_type (filedata, reloc->r_info);
12570       sym_index = get_reloc_symindex (reloc->r_info);
12571     }
12572 
12573   switch (filedata->file_header.e_machine)
12574     {
12575     case EM_MSP430:
12576     case EM_MSP430_OLD:
12577       {
12578 	static Elf_Internal_Sym * saved_sym = NULL;
12579 
12580 	if (reloc == NULL)
12581 	  {
12582 	    saved_sym = NULL;
12583 	    return TRUE;
12584 	  }
12585 
12586 	switch (reloc_type)
12587 	  {
12588 	  case 10: /* R_MSP430_SYM_DIFF */
12589 	  case 12: /* R_MSP430_GNU_SUB_ULEB128 */
12590 	    if (uses_msp430x_relocs (filedata))
12591 	      break;
12592 	    /* Fall through.  */
12593 	  case 21: /* R_MSP430X_SYM_DIFF */
12594 	  case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
12595 	    /* PR 21139.  */
12596 	    if (sym_index >= num_syms)
12597 	      error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
12598 		     sym_index);
12599 	    else
12600 	      saved_sym = symtab + sym_index;
12601 	    return TRUE;
12602 
12603 	  case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
12604 	  case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
12605 	    goto handle_sym_diff;
12606 
12607 	  case 5: /* R_MSP430_16_BYTE */
12608 	  case 9: /* R_MSP430_8 */
12609 	  case 11: /* R_MSP430_GNU_SET_ULEB128 */
12610 	    if (uses_msp430x_relocs (filedata))
12611 	      break;
12612 	    goto handle_sym_diff;
12613 
12614 	  case 2: /* R_MSP430_ABS16 */
12615 	  case 15: /* R_MSP430X_ABS16 */
12616 	  case 22: /* R_MSP430X_GNU_SET_ULEB128 */
12617 	    if (! uses_msp430x_relocs (filedata))
12618 	      break;
12619 	    goto handle_sym_diff;
12620 
12621 	  handle_sym_diff:
12622 	    if (saved_sym != NULL)
12623 	      {
12624 		bfd_vma value;
12625 		unsigned int reloc_size;
12626 		int leb_ret = 0;
12627 		switch (reloc_type)
12628 		  {
12629 		  case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
12630 		    reloc_size = 4;
12631 		    break;
12632 		  case 11: /* R_MSP430_GNU_SET_ULEB128 */
12633 		  case 22: /* R_MSP430X_GNU_SET_ULEB128 */
12634 		    read_leb128 (start + reloc->r_offset, end, FALSE,
12635 				 &reloc_size, &leb_ret);
12636 		    break;
12637 		  default:
12638 		    reloc_size = 2;
12639 		    break;
12640 		  }
12641 
12642 		if (leb_ret != 0)
12643 		  error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
12644 			   "ULEB128 value\n"),
12645 			 (long) reloc->r_offset);
12646 		else if (sym_index >= num_syms)
12647 		  error (_("MSP430 reloc contains invalid symbol index %lu\n"),
12648 			 sym_index);
12649 		else
12650 		  {
12651 		    value = reloc->r_addend + (symtab[sym_index].st_value
12652 					       - saved_sym->st_value);
12653 
12654 		    if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
12655 		      byte_put (start + reloc->r_offset, value, reloc_size);
12656 		    else
12657 		      /* PR 21137 */
12658 		      error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
12659 			     (long) reloc->r_offset);
12660 		  }
12661 
12662 		saved_sym = NULL;
12663 		return TRUE;
12664 	      }
12665 	    break;
12666 
12667 	  default:
12668 	    if (saved_sym != NULL)
12669 	      error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
12670 	    break;
12671 	  }
12672 	break;
12673       }
12674 
12675     case EM_MN10300:
12676     case EM_CYGNUS_MN10300:
12677       {
12678 	static Elf_Internal_Sym * saved_sym = NULL;
12679 
12680 	if (reloc == NULL)
12681 	  {
12682 	    saved_sym = NULL;
12683 	    return TRUE;
12684 	  }
12685 
12686 	switch (reloc_type)
12687 	  {
12688 	  case 34: /* R_MN10300_ALIGN */
12689 	    return TRUE;
12690 	  case 33: /* R_MN10300_SYM_DIFF */
12691 	    if (sym_index >= num_syms)
12692 	      error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
12693 		     sym_index);
12694 	    else
12695 	      saved_sym = symtab + sym_index;
12696 	    return TRUE;
12697 
12698 	  case 1: /* R_MN10300_32 */
12699 	  case 2: /* R_MN10300_16 */
12700 	    if (saved_sym != NULL)
12701 	      {
12702 		int reloc_size = reloc_type == 1 ? 4 : 2;
12703 		bfd_vma value;
12704 
12705 		if (sym_index >= num_syms)
12706 		  error (_("MN10300 reloc contains invalid symbol index %lu\n"),
12707 			 sym_index);
12708 		else
12709 		  {
12710 		    value = reloc->r_addend + (symtab[sym_index].st_value
12711 					       - saved_sym->st_value);
12712 
12713 		    if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
12714 		      byte_put (start + reloc->r_offset, value, reloc_size);
12715 		    else
12716 		      error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
12717 			     (long) reloc->r_offset);
12718 		  }
12719 
12720 		saved_sym = NULL;
12721 		return TRUE;
12722 	      }
12723 	    break;
12724 	  default:
12725 	    if (saved_sym != NULL)
12726 	      error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
12727 	    break;
12728 	  }
12729 	break;
12730       }
12731 
12732     case EM_RL78:
12733       {
12734 	static bfd_vma saved_sym1 = 0;
12735 	static bfd_vma saved_sym2 = 0;
12736 	static bfd_vma value;
12737 
12738 	if (reloc == NULL)
12739 	  {
12740 	    saved_sym1 = saved_sym2 = 0;
12741 	    return TRUE;
12742 	  }
12743 
12744 	switch (reloc_type)
12745 	  {
12746 	  case 0x80: /* R_RL78_SYM.  */
12747 	    saved_sym1 = saved_sym2;
12748 	    if (sym_index >= num_syms)
12749 	      error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
12750 		     sym_index);
12751 	    else
12752 	      {
12753 		saved_sym2 = symtab[sym_index].st_value;
12754 		saved_sym2 += reloc->r_addend;
12755 	      }
12756 	    return TRUE;
12757 
12758 	  case 0x83: /* R_RL78_OPsub.  */
12759 	    value = saved_sym1 - saved_sym2;
12760 	    saved_sym2 = saved_sym1 = 0;
12761 	    return TRUE;
12762 	    break;
12763 
12764 	  case 0x41: /* R_RL78_ABS32.  */
12765 	    if (IN_RANGE (start, end, start + reloc->r_offset, 4))
12766 	      byte_put (start + reloc->r_offset, value, 4);
12767 	    else
12768 	      error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
12769 		     (long) reloc->r_offset);
12770 	    value = 0;
12771 	    return TRUE;
12772 
12773 	  case 0x43: /* R_RL78_ABS16.  */
12774 	    if (IN_RANGE (start, end, start + reloc->r_offset, 2))
12775 	      byte_put (start + reloc->r_offset, value, 2);
12776 	    else
12777 	      error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
12778 		     (long) reloc->r_offset);
12779 	    value = 0;
12780 	    return TRUE;
12781 
12782 	  default:
12783 	    break;
12784 	  }
12785 	break;
12786       }
12787     }
12788 
12789   return FALSE;
12790 }
12791 
12792 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
12793    DWARF debug sections.  This is a target specific test.  Note - we do not
12794    go through the whole including-target-headers-multiple-times route, (as
12795    we have already done with <elf/h8.h>) because this would become very
12796    messy and even then this function would have to contain target specific
12797    information (the names of the relocs instead of their numeric values).
12798    FIXME: This is not the correct way to solve this problem.  The proper way
12799    is to have target specific reloc sizing and typing functions created by
12800    the reloc-macros.h header, in the same way that it already creates the
12801    reloc naming functions.  */
12802 
12803 static bfd_boolean
is_32bit_abs_reloc(Filedata * filedata,unsigned int reloc_type)12804 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12805 {
12806   /* Please keep this table alpha-sorted for ease of visual lookup.  */
12807   switch (filedata->file_header.e_machine)
12808     {
12809     case EM_386:
12810     case EM_IAMCU:
12811       return reloc_type == 1; /* R_386_32.  */
12812     case EM_68K:
12813       return reloc_type == 1; /* R_68K_32.  */
12814     case EM_860:
12815       return reloc_type == 1; /* R_860_32.  */
12816     case EM_960:
12817       return reloc_type == 2; /* R_960_32.  */
12818     case EM_AARCH64:
12819       return (reloc_type == 258
12820 	      || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
12821     case EM_BPF:
12822       return reloc_type == 11; /* R_BPF_DATA_32 */
12823     case EM_ADAPTEVA_EPIPHANY:
12824       return reloc_type == 3;
12825     case EM_ALPHA:
12826       return reloc_type == 1; /* R_ALPHA_REFLONG.  */
12827     case EM_ARC:
12828       return reloc_type == 1; /* R_ARC_32.  */
12829     case EM_ARC_COMPACT:
12830     case EM_ARC_COMPACT2:
12831       return reloc_type == 4; /* R_ARC_32.  */
12832     case EM_ARM:
12833       return reloc_type == 2; /* R_ARM_ABS32 */
12834     case EM_AVR_OLD:
12835     case EM_AVR:
12836       return reloc_type == 1;
12837     case EM_BLACKFIN:
12838       return reloc_type == 0x12; /* R_byte4_data.  */
12839     case EM_CRIS:
12840       return reloc_type == 3; /* R_CRIS_32.  */
12841     case EM_CR16:
12842       return reloc_type == 3; /* R_CR16_NUM32.  */
12843     case EM_CRX:
12844       return reloc_type == 15; /* R_CRX_NUM32.  */
12845     case EM_CSKY:
12846       return reloc_type == 1; /* R_CKCORE_ADDR32.  */
12847     case EM_CYGNUS_FRV:
12848       return reloc_type == 1;
12849     case EM_CYGNUS_D10V:
12850     case EM_D10V:
12851       return reloc_type == 6; /* R_D10V_32.  */
12852     case EM_CYGNUS_D30V:
12853     case EM_D30V:
12854       return reloc_type == 12; /* R_D30V_32_NORMAL.  */
12855     case EM_DLX:
12856       return reloc_type == 3; /* R_DLX_RELOC_32.  */
12857     case EM_CYGNUS_FR30:
12858     case EM_FR30:
12859       return reloc_type == 3; /* R_FR30_32.  */
12860     case EM_FT32:
12861       return reloc_type == 1; /* R_FT32_32.  */
12862     case EM_H8S:
12863     case EM_H8_300:
12864     case EM_H8_300H:
12865       return reloc_type == 1; /* R_H8_DIR32.  */
12866     case EM_IA_64:
12867       return (reloc_type == 0x64    /* R_IA64_SECREL32MSB.  */
12868 	      || reloc_type == 0x65 /* R_IA64_SECREL32LSB.  */
12869 	      || reloc_type == 0x24 /* R_IA64_DIR32MSB.  */
12870 	      || reloc_type == 0x25 /* R_IA64_DIR32LSB.  */);
12871     case EM_IP2K_OLD:
12872     case EM_IP2K:
12873       return reloc_type == 2; /* R_IP2K_32.  */
12874     case EM_IQ2000:
12875       return reloc_type == 2; /* R_IQ2000_32.  */
12876     case EM_LATTICEMICO32:
12877       return reloc_type == 3; /* R_LM32_32.  */
12878     case EM_M32C_OLD:
12879     case EM_M32C:
12880       return reloc_type == 3; /* R_M32C_32.  */
12881     case EM_M32R:
12882       return reloc_type == 34; /* R_M32R_32_RELA.  */
12883     case EM_68HC11:
12884     case EM_68HC12:
12885       return reloc_type == 6; /* R_M68HC11_32.  */
12886     case EM_S12Z:
12887       return reloc_type == 7 || /* R_S12Z_EXT32 */
12888 	reloc_type == 6;        /* R_S12Z_CW32.  */
12889     case EM_MCORE:
12890       return reloc_type == 1; /* R_MCORE_ADDR32.  */
12891     case EM_CYGNUS_MEP:
12892       return reloc_type == 4; /* R_MEP_32.  */
12893     case EM_METAG:
12894       return reloc_type == 2; /* R_METAG_ADDR32.  */
12895     case EM_MICROBLAZE:
12896       return reloc_type == 1; /* R_MICROBLAZE_32.  */
12897     case EM_MIPS:
12898       return reloc_type == 2; /* R_MIPS_32.  */
12899     case EM_MMIX:
12900       return reloc_type == 4; /* R_MMIX_32.  */
12901     case EM_CYGNUS_MN10200:
12902     case EM_MN10200:
12903       return reloc_type == 1; /* R_MN10200_32.  */
12904     case EM_CYGNUS_MN10300:
12905     case EM_MN10300:
12906       return reloc_type == 1; /* R_MN10300_32.  */
12907     case EM_MOXIE:
12908       return reloc_type == 1; /* R_MOXIE_32.  */
12909     case EM_MSP430_OLD:
12910     case EM_MSP430:
12911       return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32.  */
12912     case EM_MT:
12913       return reloc_type == 2; /* R_MT_32.  */
12914     case EM_NDS32:
12915       return reloc_type == 20; /* R_NDS32_RELA.  */
12916     case EM_ALTERA_NIOS2:
12917       return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32.  */
12918     case EM_NIOS32:
12919       return reloc_type == 1; /* R_NIOS_32.  */
12920     case EM_OR1K:
12921       return reloc_type == 1; /* R_OR1K_32.  */
12922     case EM_PARISC:
12923       return (reloc_type == 1 /* R_PARISC_DIR32.  */
12924 	      || reloc_type == 2 /* R_PARISC_DIR21L.  */
12925 	      || reloc_type == 41); /* R_PARISC_SECREL32.  */
12926     case EM_PJ:
12927     case EM_PJ_OLD:
12928       return reloc_type == 1; /* R_PJ_DATA_DIR32.  */
12929     case EM_PPC64:
12930       return reloc_type == 1; /* R_PPC64_ADDR32.  */
12931     case EM_PPC:
12932       return reloc_type == 1; /* R_PPC_ADDR32.  */
12933     case EM_TI_PRU:
12934       return reloc_type == 11; /* R_PRU_BFD_RELOC_32.  */
12935     case EM_RISCV:
12936       return reloc_type == 1; /* R_RISCV_32.  */
12937     case EM_RL78:
12938       return reloc_type == 1; /* R_RL78_DIR32.  */
12939     case EM_RX:
12940       return reloc_type == 1; /* R_RX_DIR32.  */
12941     case EM_S370:
12942       return reloc_type == 1; /* R_I370_ADDR31.  */
12943     case EM_S390_OLD:
12944     case EM_S390:
12945       return reloc_type == 4; /* R_S390_32.  */
12946     case EM_SCORE:
12947       return reloc_type == 8; /* R_SCORE_ABS32.  */
12948     case EM_SH:
12949       return reloc_type == 1; /* R_SH_DIR32.  */
12950     case EM_SPARC32PLUS:
12951     case EM_SPARCV9:
12952     case EM_SPARC:
12953       return reloc_type == 3 /* R_SPARC_32.  */
12954 	|| reloc_type == 23; /* R_SPARC_UA32.  */
12955     case EM_SPU:
12956       return reloc_type == 6; /* R_SPU_ADDR32 */
12957     case EM_TI_C6000:
12958       return reloc_type == 1; /* R_C6000_ABS32.  */
12959     case EM_TILEGX:
12960       return reloc_type == 2; /* R_TILEGX_32.  */
12961     case EM_TILEPRO:
12962       return reloc_type == 1; /* R_TILEPRO_32.  */
12963     case EM_CYGNUS_V850:
12964     case EM_V850:
12965       return reloc_type == 6; /* R_V850_ABS32.  */
12966     case EM_V800:
12967       return reloc_type == 0x33; /* R_V810_WORD.  */
12968     case EM_VAX:
12969       return reloc_type == 1; /* R_VAX_32.  */
12970     case EM_VISIUM:
12971       return reloc_type == 3;  /* R_VISIUM_32. */
12972     case EM_WEBASSEMBLY:
12973       return reloc_type == 1;  /* R_WASM32_32.  */
12974     case EM_X86_64:
12975     case EM_L1OM:
12976     case EM_K1OM:
12977       return reloc_type == 10; /* R_X86_64_32.  */
12978     case EM_XC16X:
12979     case EM_C166:
12980       return reloc_type == 3; /* R_XC16C_ABS_32.  */
12981     case EM_XGATE:
12982       return reloc_type == 4; /* R_XGATE_32.  */
12983     case EM_XSTORMY16:
12984       return reloc_type == 1; /* R_XSTROMY16_32.  */
12985     case EM_XTENSA_OLD:
12986     case EM_XTENSA:
12987       return reloc_type == 1; /* R_XTENSA_32.  */
12988     case EM_Z80:
12989       return reloc_type == 6; /* R_Z80_32.  */
12990     default:
12991       {
12992 	static unsigned int prev_warn = 0;
12993 
12994 	/* Avoid repeating the same warning multiple times.  */
12995 	if (prev_warn != filedata->file_header.e_machine)
12996 	  error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
12997 		 filedata->file_header.e_machine);
12998 	prev_warn = filedata->file_header.e_machine;
12999 	return FALSE;
13000       }
13001     }
13002 }
13003 
13004 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13005    a 32-bit pc-relative RELA relocation used in DWARF debug sections.  */
13006 
13007 static bfd_boolean
is_32bit_pcrel_reloc(Filedata * filedata,unsigned int reloc_type)13008 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13009 {
13010   switch (filedata->file_header.e_machine)
13011   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13012     {
13013     case EM_386:
13014     case EM_IAMCU:
13015       return reloc_type == 2;  /* R_386_PC32.  */
13016     case EM_68K:
13017       return reloc_type == 4;  /* R_68K_PC32.  */
13018     case EM_AARCH64:
13019       return reloc_type == 261; /* R_AARCH64_PREL32 */
13020     case EM_ADAPTEVA_EPIPHANY:
13021       return reloc_type == 6;
13022     case EM_ALPHA:
13023       return reloc_type == 10; /* R_ALPHA_SREL32.  */
13024     case EM_ARC_COMPACT:
13025     case EM_ARC_COMPACT2:
13026       return reloc_type == 49; /* R_ARC_32_PCREL.  */
13027     case EM_ARM:
13028       return reloc_type == 3;  /* R_ARM_REL32 */
13029     case EM_AVR_OLD:
13030     case EM_AVR:
13031       return reloc_type == 36; /* R_AVR_32_PCREL.  */
13032     case EM_MICROBLAZE:
13033       return reloc_type == 2;  /* R_MICROBLAZE_32_PCREL.  */
13034     case EM_OR1K:
13035       return reloc_type == 9; /* R_OR1K_32_PCREL.  */
13036     case EM_PARISC:
13037       return reloc_type == 9;  /* R_PARISC_PCREL32.  */
13038     case EM_PPC:
13039       return reloc_type == 26; /* R_PPC_REL32.  */
13040     case EM_PPC64:
13041       return reloc_type == 26; /* R_PPC64_REL32.  */
13042     case EM_RISCV:
13043       return reloc_type == 57;	/* R_RISCV_32_PCREL.  */
13044     case EM_S390_OLD:
13045     case EM_S390:
13046       return reloc_type == 5;  /* R_390_PC32.  */
13047     case EM_SH:
13048       return reloc_type == 2;  /* R_SH_REL32.  */
13049     case EM_SPARC32PLUS:
13050     case EM_SPARCV9:
13051     case EM_SPARC:
13052       return reloc_type == 6;  /* R_SPARC_DISP32.  */
13053     case EM_SPU:
13054       return reloc_type == 13; /* R_SPU_REL32.  */
13055     case EM_TILEGX:
13056       return reloc_type == 6; /* R_TILEGX_32_PCREL.  */
13057     case EM_TILEPRO:
13058       return reloc_type == 4; /* R_TILEPRO_32_PCREL.  */
13059     case EM_VISIUM:
13060       return reloc_type == 6;  /* R_VISIUM_32_PCREL */
13061     case EM_X86_64:
13062     case EM_L1OM:
13063     case EM_K1OM:
13064       return reloc_type == 2;  /* R_X86_64_PC32.  */
13065     case EM_VAX:
13066       return reloc_type == 4;  /* R_VAX_PCREL32.  */
13067     case EM_XTENSA_OLD:
13068     case EM_XTENSA:
13069       return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
13070     default:
13071       /* Do not abort or issue an error message here.  Not all targets use
13072 	 pc-relative 32-bit relocs in their DWARF debug information and we
13073 	 have already tested for target coverage in is_32bit_abs_reloc.  A
13074 	 more helpful warning message will be generated by apply_relocations
13075 	 anyway, so just return.  */
13076       return FALSE;
13077     }
13078 }
13079 
13080 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13081    a 64-bit absolute RELA relocation used in DWARF debug sections.  */
13082 
13083 static bfd_boolean
is_64bit_abs_reloc(Filedata * filedata,unsigned int reloc_type)13084 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13085 {
13086   switch (filedata->file_header.e_machine)
13087     {
13088     case EM_AARCH64:
13089       return reloc_type == 257;	/* R_AARCH64_ABS64.  */
13090     case EM_ALPHA:
13091       return reloc_type == 2; /* R_ALPHA_REFQUAD.  */
13092     case EM_IA_64:
13093       return (reloc_type == 0x26    /* R_IA64_DIR64MSB.  */
13094 	      || reloc_type == 0x27 /* R_IA64_DIR64LSB.  */);
13095     case EM_PARISC:
13096       return reloc_type == 80; /* R_PARISC_DIR64.  */
13097     case EM_PPC64:
13098       return reloc_type == 38; /* R_PPC64_ADDR64.  */
13099     case EM_RISCV:
13100       return reloc_type == 2; /* R_RISCV_64.  */
13101     case EM_SPARC32PLUS:
13102     case EM_SPARCV9:
13103     case EM_SPARC:
13104       return reloc_type == 32 /* R_SPARC_64.  */
13105 	|| reloc_type == 54; /* R_SPARC_UA64.  */
13106     case EM_X86_64:
13107     case EM_L1OM:
13108     case EM_K1OM:
13109       return reloc_type == 1; /* R_X86_64_64.  */
13110     case EM_S390_OLD:
13111     case EM_S390:
13112       return reloc_type == 22;	/* R_S390_64.  */
13113     case EM_TILEGX:
13114       return reloc_type == 1; /* R_TILEGX_64.  */
13115     case EM_MIPS:
13116       return reloc_type == 18;	/* R_MIPS_64.  */
13117     default:
13118       return FALSE;
13119     }
13120 }
13121 
13122 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
13123    a 64-bit pc-relative RELA relocation used in DWARF debug sections.  */
13124 
13125 static bfd_boolean
is_64bit_pcrel_reloc(Filedata * filedata,unsigned int reloc_type)13126 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13127 {
13128   switch (filedata->file_header.e_machine)
13129     {
13130     case EM_AARCH64:
13131       return reloc_type == 260;	/* R_AARCH64_PREL64.  */
13132     case EM_ALPHA:
13133       return reloc_type == 11; /* R_ALPHA_SREL64.  */
13134     case EM_IA_64:
13135       return (reloc_type == 0x4e    /* R_IA64_PCREL64MSB.  */
13136 	      || reloc_type == 0x4f /* R_IA64_PCREL64LSB.  */);
13137     case EM_PARISC:
13138       return reloc_type == 72; /* R_PARISC_PCREL64.  */
13139     case EM_PPC64:
13140       return reloc_type == 44; /* R_PPC64_REL64.  */
13141     case EM_SPARC32PLUS:
13142     case EM_SPARCV9:
13143     case EM_SPARC:
13144       return reloc_type == 46; /* R_SPARC_DISP64.  */
13145     case EM_X86_64:
13146     case EM_L1OM:
13147     case EM_K1OM:
13148       return reloc_type == 24; /* R_X86_64_PC64.  */
13149     case EM_S390_OLD:
13150     case EM_S390:
13151       return reloc_type == 23;	/* R_S390_PC64.  */
13152     case EM_TILEGX:
13153       return reloc_type == 5;  /* R_TILEGX_64_PCREL.  */
13154     default:
13155       return FALSE;
13156     }
13157 }
13158 
13159 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13160    a 24-bit absolute RELA relocation used in DWARF debug sections.  */
13161 
13162 static bfd_boolean
is_24bit_abs_reloc(Filedata * filedata,unsigned int reloc_type)13163 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13164 {
13165   switch (filedata->file_header.e_machine)
13166     {
13167     case EM_CYGNUS_MN10200:
13168     case EM_MN10200:
13169       return reloc_type == 4; /* R_MN10200_24.  */
13170     case EM_FT32:
13171       return reloc_type == 5; /* R_FT32_20.  */
13172     case EM_Z80:
13173       return reloc_type == 5; /* R_Z80_24. */
13174     default:
13175       return FALSE;
13176     }
13177 }
13178 
13179 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13180    a 16-bit absolute RELA relocation used in DWARF debug sections.  */
13181 
13182 static bfd_boolean
is_16bit_abs_reloc(Filedata * filedata,unsigned int reloc_type)13183 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13184 {
13185   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13186   switch (filedata->file_header.e_machine)
13187     {
13188     case EM_ARC:
13189     case EM_ARC_COMPACT:
13190     case EM_ARC_COMPACT2:
13191       return reloc_type == 2; /* R_ARC_16.  */
13192     case EM_ADAPTEVA_EPIPHANY:
13193       return reloc_type == 5;
13194     case EM_AVR_OLD:
13195     case EM_AVR:
13196       return reloc_type == 4; /* R_AVR_16.  */
13197     case EM_CYGNUS_D10V:
13198     case EM_D10V:
13199       return reloc_type == 3; /* R_D10V_16.  */
13200     case EM_FT32:
13201       return reloc_type == 2; /* R_FT32_16.  */
13202     case EM_H8S:
13203     case EM_H8_300:
13204     case EM_H8_300H:
13205       return reloc_type == R_H8_DIR16;
13206     case EM_IP2K_OLD:
13207     case EM_IP2K:
13208       return reloc_type == 1; /* R_IP2K_16.  */
13209     case EM_M32C_OLD:
13210     case EM_M32C:
13211       return reloc_type == 1; /* R_M32C_16 */
13212     case EM_CYGNUS_MN10200:
13213     case EM_MN10200:
13214       return reloc_type == 2; /* R_MN10200_16.  */
13215     case EM_CYGNUS_MN10300:
13216     case EM_MN10300:
13217       return reloc_type == 2; /* R_MN10300_16.  */
13218     case EM_MSP430:
13219       if (uses_msp430x_relocs (filedata))
13220 	return reloc_type == 2; /* R_MSP430_ABS16.  */
13221       /* Fall through.  */
13222     case EM_MSP430_OLD:
13223       return reloc_type == 5; /* R_MSP430_16_BYTE.  */
13224     case EM_NDS32:
13225       return reloc_type == 19; /* R_NDS32_RELA.  */
13226     case EM_ALTERA_NIOS2:
13227       return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16.  */
13228     case EM_NIOS32:
13229       return reloc_type == 9; /* R_NIOS_16.  */
13230     case EM_OR1K:
13231       return reloc_type == 2; /* R_OR1K_16.  */
13232     case EM_RISCV:
13233       return reloc_type == 55; /* R_RISCV_SET16.  */
13234     case EM_TI_PRU:
13235       return reloc_type == 8; /* R_PRU_BFD_RELOC_16.  */
13236     case EM_TI_C6000:
13237       return reloc_type == 2; /* R_C6000_ABS16.  */
13238     case EM_VISIUM:
13239       return reloc_type == 2; /* R_VISIUM_16. */
13240     case EM_XC16X:
13241     case EM_C166:
13242       return reloc_type == 2; /* R_XC16C_ABS_16.  */
13243     case EM_XGATE:
13244       return reloc_type == 3; /* R_XGATE_16.  */
13245     case EM_Z80:
13246       return reloc_type == 4; /* R_Z80_16.  */
13247     default:
13248       return FALSE;
13249     }
13250 }
13251 
13252 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13253    a 8-bit absolute RELA relocation used in DWARF debug sections.  */
13254 
13255 static bfd_boolean
is_8bit_abs_reloc(Filedata * filedata,unsigned int reloc_type)13256 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13257 {
13258   switch (filedata->file_header.e_machine)
13259     {
13260     case EM_RISCV:
13261       return reloc_type == 54; /* R_RISCV_SET8.  */
13262     case EM_Z80:
13263       return reloc_type == 1;  /* R_Z80_8.  */
13264     default:
13265       return FALSE;
13266     }
13267 }
13268 
13269 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13270    a 6-bit absolute RELA relocation used in DWARF debug sections.  */
13271 
13272 static bfd_boolean
is_6bit_abs_reloc(Filedata * filedata,unsigned int reloc_type)13273 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13274 {
13275   switch (filedata->file_header.e_machine)
13276     {
13277     case EM_RISCV:
13278       return reloc_type == 53; /* R_RISCV_SET6.  */
13279     default:
13280       return FALSE;
13281     }
13282 }
13283 
13284 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13285    a 32-bit inplace add RELA relocation used in DWARF debug sections.  */
13286 
13287 static bfd_boolean
is_32bit_inplace_add_reloc(Filedata * filedata,unsigned int reloc_type)13288 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13289 {
13290   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13291   switch (filedata->file_header.e_machine)
13292     {
13293     case EM_RISCV:
13294       return reloc_type == 35; /* R_RISCV_ADD32.  */
13295     default:
13296       return FALSE;
13297     }
13298 }
13299 
13300 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13301    a 32-bit inplace sub RELA relocation used in DWARF debug sections.  */
13302 
13303 static bfd_boolean
is_32bit_inplace_sub_reloc(Filedata * filedata,unsigned int reloc_type)13304 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13305 {
13306   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13307   switch (filedata->file_header.e_machine)
13308     {
13309     case EM_RISCV:
13310       return reloc_type == 39; /* R_RISCV_SUB32.  */
13311     default:
13312       return FALSE;
13313     }
13314 }
13315 
13316 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13317    a 64-bit inplace add RELA relocation used in DWARF debug sections.  */
13318 
13319 static bfd_boolean
is_64bit_inplace_add_reloc(Filedata * filedata,unsigned int reloc_type)13320 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13321 {
13322   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13323   switch (filedata->file_header.e_machine)
13324     {
13325     case EM_RISCV:
13326       return reloc_type == 36; /* R_RISCV_ADD64.  */
13327     default:
13328       return FALSE;
13329     }
13330 }
13331 
13332 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13333    a 64-bit inplace sub RELA relocation used in DWARF debug sections.  */
13334 
13335 static bfd_boolean
is_64bit_inplace_sub_reloc(Filedata * filedata,unsigned int reloc_type)13336 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13337 {
13338   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13339   switch (filedata->file_header.e_machine)
13340     {
13341     case EM_RISCV:
13342       return reloc_type == 40; /* R_RISCV_SUB64.  */
13343     default:
13344       return FALSE;
13345     }
13346 }
13347 
13348 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13349    a 16-bit inplace add RELA relocation used in DWARF debug sections.  */
13350 
13351 static bfd_boolean
is_16bit_inplace_add_reloc(Filedata * filedata,unsigned int reloc_type)13352 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13353 {
13354   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13355   switch (filedata->file_header.e_machine)
13356     {
13357     case EM_RISCV:
13358       return reloc_type == 34; /* R_RISCV_ADD16.  */
13359     default:
13360       return FALSE;
13361     }
13362 }
13363 
13364 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13365    a 16-bit inplace sub RELA relocation used in DWARF debug sections.  */
13366 
13367 static bfd_boolean
is_16bit_inplace_sub_reloc(Filedata * filedata,unsigned int reloc_type)13368 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13369 {
13370   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13371   switch (filedata->file_header.e_machine)
13372     {
13373     case EM_RISCV:
13374       return reloc_type == 38; /* R_RISCV_SUB16.  */
13375     default:
13376       return FALSE;
13377     }
13378 }
13379 
13380 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13381    a 8-bit inplace add RELA relocation used in DWARF debug sections.  */
13382 
13383 static bfd_boolean
is_8bit_inplace_add_reloc(Filedata * filedata,unsigned int reloc_type)13384 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13385 {
13386   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13387   switch (filedata->file_header.e_machine)
13388     {
13389     case EM_RISCV:
13390       return reloc_type == 33; /* R_RISCV_ADD8.  */
13391     default:
13392       return FALSE;
13393     }
13394 }
13395 
13396 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13397    a 8-bit inplace sub RELA relocation used in DWARF debug sections.  */
13398 
13399 static bfd_boolean
is_8bit_inplace_sub_reloc(Filedata * filedata,unsigned int reloc_type)13400 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13401 {
13402   /* Please keep this table alpha-sorted for ease of visual lookup.  */
13403   switch (filedata->file_header.e_machine)
13404     {
13405     case EM_RISCV:
13406       return reloc_type == 37; /* R_RISCV_SUB8.  */
13407     default:
13408       return FALSE;
13409     }
13410 }
13411 
13412 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13413    a 6-bit inplace sub RELA relocation used in DWARF debug sections.  */
13414 
13415 static bfd_boolean
is_6bit_inplace_sub_reloc(Filedata * filedata,unsigned int reloc_type)13416 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13417 {
13418   switch (filedata->file_header.e_machine)
13419     {
13420     case EM_RISCV:
13421       return reloc_type == 52; /* R_RISCV_SUB6.  */
13422     default:
13423       return FALSE;
13424     }
13425 }
13426 
13427 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
13428    relocation entries (possibly formerly used for SHT_GROUP sections).  */
13429 
13430 static bfd_boolean
is_none_reloc(Filedata * filedata,unsigned int reloc_type)13431 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
13432 {
13433   switch (filedata->file_header.e_machine)
13434     {
13435     case EM_386:     /* R_386_NONE.  */
13436     case EM_68K:     /* R_68K_NONE.  */
13437     case EM_ADAPTEVA_EPIPHANY:
13438     case EM_ALPHA:   /* R_ALPHA_NONE.  */
13439     case EM_ALTERA_NIOS2: /* R_NIOS2_NONE.  */
13440     case EM_ARC:     /* R_ARC_NONE.  */
13441     case EM_ARC_COMPACT2: /* R_ARC_NONE.  */
13442     case EM_ARC_COMPACT: /* R_ARC_NONE.  */
13443     case EM_ARM:     /* R_ARM_NONE.  */
13444     case EM_C166:    /* R_XC16X_NONE.  */
13445     case EM_CRIS:    /* R_CRIS_NONE.  */
13446     case EM_FT32:    /* R_FT32_NONE.  */
13447     case EM_IA_64:   /* R_IA64_NONE.  */
13448     case EM_K1OM:    /* R_X86_64_NONE.  */
13449     case EM_L1OM:    /* R_X86_64_NONE.  */
13450     case EM_M32R:    /* R_M32R_NONE.  */
13451     case EM_MIPS:    /* R_MIPS_NONE.  */
13452     case EM_MN10300: /* R_MN10300_NONE.  */
13453     case EM_MOXIE:   /* R_MOXIE_NONE.  */
13454     case EM_NIOS32:  /* R_NIOS_NONE.  */
13455     case EM_OR1K:    /* R_OR1K_NONE. */
13456     case EM_PARISC:  /* R_PARISC_NONE.  */
13457     case EM_PPC64:   /* R_PPC64_NONE.  */
13458     case EM_PPC:     /* R_PPC_NONE.  */
13459     case EM_RISCV:   /* R_RISCV_NONE.  */
13460     case EM_S390:    /* R_390_NONE.  */
13461     case EM_S390_OLD:
13462     case EM_SH:      /* R_SH_NONE.  */
13463     case EM_SPARC32PLUS:
13464     case EM_SPARC:   /* R_SPARC_NONE.  */
13465     case EM_SPARCV9:
13466     case EM_TILEGX:  /* R_TILEGX_NONE.  */
13467     case EM_TILEPRO: /* R_TILEPRO_NONE.  */
13468     case EM_TI_C6000:/* R_C6000_NONE.  */
13469     case EM_X86_64:  /* R_X86_64_NONE.  */
13470     case EM_XC16X:
13471     case EM_Z80:     /* R_Z80_NONE. */
13472     case EM_WEBASSEMBLY: /* R_WASM32_NONE.  */
13473       return reloc_type == 0;
13474 
13475     case EM_AARCH64:
13476       return reloc_type == 0 || reloc_type == 256;
13477     case EM_AVR_OLD:
13478     case EM_AVR:
13479       return (reloc_type == 0 /* R_AVR_NONE.  */
13480 	      || reloc_type == 30 /* R_AVR_DIFF8.  */
13481 	      || reloc_type == 31 /* R_AVR_DIFF16.  */
13482 	      || reloc_type == 32 /* R_AVR_DIFF32.  */);
13483     case EM_METAG:
13484       return reloc_type == 3; /* R_METAG_NONE.  */
13485     case EM_NDS32:
13486       return (reloc_type == 0       /* R_XTENSA_NONE.  */
13487 	      || reloc_type == 204  /* R_NDS32_DIFF8.  */
13488 	      || reloc_type == 205  /* R_NDS32_DIFF16.  */
13489 	      || reloc_type == 206  /* R_NDS32_DIFF32.  */
13490 	      || reloc_type == 207  /* R_NDS32_ULEB128.  */);
13491     case EM_TI_PRU:
13492       return (reloc_type == 0       /* R_PRU_NONE.  */
13493 	      || reloc_type == 65   /* R_PRU_DIFF8.  */
13494 	      || reloc_type == 66   /* R_PRU_DIFF16.  */
13495 	      || reloc_type == 67   /* R_PRU_DIFF32.  */);
13496     case EM_XTENSA_OLD:
13497     case EM_XTENSA:
13498       return (reloc_type == 0      /* R_XTENSA_NONE.  */
13499 	      || reloc_type == 17  /* R_XTENSA_DIFF8.  */
13500 	      || reloc_type == 18  /* R_XTENSA_DIFF16.  */
13501 	      || reloc_type == 19  /* R_XTENSA_DIFF32.  */
13502 	      || reloc_type == 57  /* R_XTENSA_PDIFF8.  */
13503 	      || reloc_type == 58  /* R_XTENSA_PDIFF16.  */
13504 	      || reloc_type == 59  /* R_XTENSA_PDIFF32.  */
13505 	      || reloc_type == 60  /* R_XTENSA_NDIFF8.  */
13506 	      || reloc_type == 61  /* R_XTENSA_NDIFF16.  */
13507 	      || reloc_type == 62  /* R_XTENSA_NDIFF32.  */);
13508     }
13509   return FALSE;
13510 }
13511 
13512 /* Returns TRUE if there is a relocation against
13513    section NAME at OFFSET bytes.  */
13514 
13515 bfd_boolean
reloc_at(struct dwarf_section * dsec,dwarf_vma offset)13516 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
13517 {
13518   Elf_Internal_Rela * relocs;
13519   Elf_Internal_Rela * rp;
13520 
13521   if (dsec == NULL || dsec->reloc_info == NULL)
13522     return FALSE;
13523 
13524   relocs = (Elf_Internal_Rela *) dsec->reloc_info;
13525 
13526   for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
13527     if (rp->r_offset == offset)
13528       return TRUE;
13529 
13530    return FALSE;
13531 }
13532 
13533 /* Apply relocations to a section.
13534    Returns TRUE upon success, FALSE otherwise.
13535    If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
13536    It is then the caller's responsibility to free them.  NUM_RELOCS_RETURN
13537    will be set to the number of relocs loaded.
13538 
13539    Note: So far support has been added only for those relocations
13540    which can be found in debug sections. FIXME: Add support for
13541    more relocations ?  */
13542 
13543 static bfd_boolean
apply_relocations(Filedata * filedata,const Elf_Internal_Shdr * section,unsigned char * start,bfd_size_type size,void ** relocs_return,unsigned long * num_relocs_return)13544 apply_relocations (Filedata *                 filedata,
13545 		   const Elf_Internal_Shdr *  section,
13546 		   unsigned char *            start,
13547 		   bfd_size_type              size,
13548 		   void **                    relocs_return,
13549 		   unsigned long *            num_relocs_return)
13550 {
13551   Elf_Internal_Shdr * relsec;
13552   unsigned char * end = start + size;
13553 
13554   if (relocs_return != NULL)
13555     {
13556       * (Elf_Internal_Rela **) relocs_return = NULL;
13557       * num_relocs_return = 0;
13558     }
13559 
13560   if (filedata->file_header.e_type != ET_REL)
13561     /* No relocs to apply.  */
13562     return TRUE;
13563 
13564   /* Find the reloc section associated with the section.  */
13565   for (relsec = filedata->section_headers;
13566        relsec < filedata->section_headers + filedata->file_header.e_shnum;
13567        ++relsec)
13568     {
13569       bfd_boolean is_rela;
13570       unsigned long num_relocs;
13571       Elf_Internal_Rela * relocs;
13572       Elf_Internal_Rela * rp;
13573       Elf_Internal_Shdr * symsec;
13574       Elf_Internal_Sym * symtab;
13575       unsigned long num_syms;
13576       Elf_Internal_Sym * sym;
13577 
13578       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
13579 	  || relsec->sh_info >= filedata->file_header.e_shnum
13580 	  || filedata->section_headers + relsec->sh_info != section
13581 	  || relsec->sh_size == 0
13582 	  || relsec->sh_link >= filedata->file_header.e_shnum)
13583 	continue;
13584 
13585       symsec = filedata->section_headers + relsec->sh_link;
13586       if (symsec->sh_type != SHT_SYMTAB
13587 	  && symsec->sh_type != SHT_DYNSYM)
13588 	return FALSE;
13589 
13590       is_rela = relsec->sh_type == SHT_RELA;
13591 
13592       if (is_rela)
13593 	{
13594 	  if (!slurp_rela_relocs (filedata, relsec->sh_offset,
13595                                   relsec->sh_size, & relocs, & num_relocs))
13596 	    return FALSE;
13597 	}
13598       else
13599 	{
13600 	  if (!slurp_rel_relocs (filedata, relsec->sh_offset,
13601                                  relsec->sh_size, & relocs, & num_relocs))
13602 	    return FALSE;
13603 	}
13604 
13605       /* SH uses RELA but uses in place value instead of the addend field.  */
13606       if (filedata->file_header.e_machine == EM_SH)
13607 	is_rela = FALSE;
13608 
13609       symtab = GET_ELF_SYMBOLS (filedata, symsec, & num_syms);
13610 
13611       for (rp = relocs; rp < relocs + num_relocs; ++rp)
13612 	{
13613 	  bfd_vma         addend;
13614 	  unsigned int    reloc_type;
13615 	  unsigned int    reloc_size;
13616 	  bfd_boolean     reloc_inplace = FALSE;
13617 	  bfd_boolean     reloc_subtract = FALSE;
13618 	  unsigned char * rloc;
13619 	  unsigned long   sym_index;
13620 
13621 	  reloc_type = get_reloc_type (filedata, rp->r_info);
13622 
13623 	  if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
13624 	    continue;
13625 	  else if (is_none_reloc (filedata, reloc_type))
13626 	    continue;
13627 	  else if (is_32bit_abs_reloc (filedata, reloc_type)
13628 		   || is_32bit_pcrel_reloc (filedata, reloc_type))
13629 	    reloc_size = 4;
13630 	  else if (is_64bit_abs_reloc (filedata, reloc_type)
13631 		   || is_64bit_pcrel_reloc (filedata, reloc_type))
13632 	    reloc_size = 8;
13633 	  else if (is_24bit_abs_reloc (filedata, reloc_type))
13634 	    reloc_size = 3;
13635 	  else if (is_16bit_abs_reloc (filedata, reloc_type))
13636 	    reloc_size = 2;
13637 	  else if (is_8bit_abs_reloc (filedata, reloc_type)
13638 		   || is_6bit_abs_reloc (filedata, reloc_type))
13639 	    reloc_size = 1;
13640 	  else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
13641 								 reloc_type))
13642 		   || is_32bit_inplace_add_reloc (filedata, reloc_type))
13643 	    {
13644 	      reloc_size = 4;
13645 	      reloc_inplace = TRUE;
13646 	    }
13647 	  else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
13648 								 reloc_type))
13649 		   || is_64bit_inplace_add_reloc (filedata, reloc_type))
13650 	    {
13651 	      reloc_size = 8;
13652 	      reloc_inplace = TRUE;
13653 	    }
13654 	  else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
13655 								 reloc_type))
13656 		   || is_16bit_inplace_add_reloc (filedata, reloc_type))
13657 	    {
13658 	      reloc_size = 2;
13659 	      reloc_inplace = TRUE;
13660 	    }
13661 	  else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
13662 								reloc_type))
13663 		   || is_8bit_inplace_add_reloc (filedata, reloc_type))
13664 	    {
13665 	      reloc_size = 1;
13666 	      reloc_inplace = TRUE;
13667 	    }
13668 	  else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
13669 								reloc_type)))
13670 	    {
13671 	      reloc_size = 1;
13672 	      reloc_inplace = TRUE;
13673 	    }
13674 	  else
13675 	    {
13676 	      static unsigned int prev_reloc = 0;
13677 
13678 	      if (reloc_type != prev_reloc)
13679 		warn (_("unable to apply unsupported reloc type %d to section %s\n"),
13680 		      reloc_type, printable_section_name (filedata, section));
13681 	      prev_reloc = reloc_type;
13682 	      continue;
13683 	    }
13684 
13685 	  rloc = start + rp->r_offset;
13686 	  if (!IN_RANGE (start, end, rloc, reloc_size))
13687 	    {
13688 	      warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
13689 		    (unsigned long) rp->r_offset,
13690 		    printable_section_name (filedata, section));
13691 	      continue;
13692 	    }
13693 
13694 	  sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
13695 	  if (sym_index >= num_syms)
13696 	    {
13697 	      warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
13698 		    sym_index, printable_section_name (filedata, section));
13699 	      continue;
13700 	    }
13701 	  sym = symtab + sym_index;
13702 
13703 	  /* If the reloc has a symbol associated with it,
13704 	     make sure that it is of an appropriate type.
13705 
13706 	     Relocations against symbols without type can happen.
13707 	     Gcc -feliminate-dwarf2-dups may generate symbols
13708 	     without type for debug info.
13709 
13710 	     Icc generates relocations against function symbols
13711 	     instead of local labels.
13712 
13713 	     Relocations against object symbols can happen, eg when
13714 	     referencing a global array.  For an example of this see
13715 	     the _clz.o binary in libgcc.a.  */
13716 	  if (sym != symtab
13717 	      && ELF_ST_TYPE (sym->st_info) != STT_COMMON
13718 	      && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
13719 	    {
13720 	      warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
13721 		    get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
13722 		    printable_section_name (filedata, relsec),
13723 		    (long int)(rp - relocs));
13724 	      continue;
13725 	    }
13726 
13727 	  addend = 0;
13728 	  if (is_rela)
13729 	    addend += rp->r_addend;
13730 	  /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
13731 	     partial_inplace.  */
13732 	  if (!is_rela
13733 	      || (filedata->file_header.e_machine == EM_XTENSA
13734 		  && reloc_type == 1)
13735 	      || ((filedata->file_header.e_machine == EM_PJ
13736 		   || filedata->file_header.e_machine == EM_PJ_OLD)
13737 		  && reloc_type == 1)
13738 	      || ((filedata->file_header.e_machine == EM_D30V
13739 		   || filedata->file_header.e_machine == EM_CYGNUS_D30V)
13740 		  && reloc_type == 12)
13741 	      || reloc_inplace)
13742 	    {
13743 	      if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
13744 		addend += byte_get (rloc, reloc_size) & 0x3f;
13745 	      else
13746 		addend += byte_get (rloc, reloc_size);
13747 	    }
13748 
13749 	  if (is_32bit_pcrel_reloc (filedata, reloc_type)
13750 	      || is_64bit_pcrel_reloc (filedata, reloc_type))
13751 	    {
13752 	      /* On HPPA, all pc-relative relocations are biased by 8.  */
13753 	      if (filedata->file_header.e_machine == EM_PARISC)
13754 		addend -= 8;
13755 	      byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
13756 		        reloc_size);
13757 	    }
13758 	  else if (is_6bit_abs_reloc (filedata, reloc_type)
13759 		   || is_6bit_inplace_sub_reloc (filedata, reloc_type))
13760 	    {
13761 	      if (reloc_subtract)
13762 		addend -= sym->st_value;
13763 	      else
13764 		addend += sym->st_value;
13765 	      addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
13766 	      byte_put (rloc, addend, reloc_size);
13767 	    }
13768 	  else if (reloc_subtract)
13769 	    byte_put (rloc, addend - sym->st_value, reloc_size);
13770 	  else
13771 	    byte_put (rloc, addend + sym->st_value, reloc_size);
13772 	}
13773 
13774       free (symtab);
13775       /* Let the target specific reloc processing code know that
13776 	 we have finished with these relocs.  */
13777       target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
13778 
13779       if (relocs_return)
13780 	{
13781 	  * (Elf_Internal_Rela **) relocs_return = relocs;
13782 	  * num_relocs_return = num_relocs;
13783 	}
13784       else
13785 	free (relocs);
13786 
13787       break;
13788     }
13789 
13790   return TRUE;
13791 }
13792 
13793 #ifdef SUPPORT_DISASSEMBLY
13794 static bfd_boolean
disassemble_section(Elf_Internal_Shdr * section,Filedata * filedata)13795 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
13796 {
13797   printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
13798 
13799   /* FIXME: XXX -- to be done --- XXX */
13800 
13801   return TRUE;
13802 }
13803 #endif
13804 
13805 /* Reads in the contents of SECTION from FILE, returning a pointer
13806    to a malloc'ed buffer or NULL if something went wrong.  */
13807 
13808 static char *
get_section_contents(Elf_Internal_Shdr * section,Filedata * filedata)13809 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
13810 {
13811   bfd_size_type num_bytes = section->sh_size;
13812 
13813   if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
13814     {
13815       printf (_("Section '%s' has no data to dump.\n"),
13816 	      printable_section_name (filedata, section));
13817       return NULL;
13818     }
13819 
13820   return  (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
13821                              _("section contents"));
13822 }
13823 
13824 /* Uncompresses a section that was compressed using zlib, in place.  */
13825 
13826 static bfd_boolean
uncompress_section_contents(unsigned char ** buffer,dwarf_size_type uncompressed_size,dwarf_size_type * size)13827 uncompress_section_contents (unsigned char **   buffer,
13828 			     dwarf_size_type    uncompressed_size,
13829 			     dwarf_size_type *  size)
13830 {
13831   dwarf_size_type compressed_size = *size;
13832   unsigned char * compressed_buffer = *buffer;
13833   unsigned char * uncompressed_buffer;
13834   z_stream strm;
13835   int rc;
13836 
13837   /* It is possible the section consists of several compressed
13838      buffers concatenated together, so we uncompress in a loop.  */
13839   /* PR 18313: The state field in the z_stream structure is supposed
13840      to be invisible to the user (ie us), but some compilers will
13841      still complain about it being used without initialisation.  So
13842      we first zero the entire z_stream structure and then set the fields
13843      that we need.  */
13844   memset (& strm, 0, sizeof strm);
13845   strm.avail_in = compressed_size;
13846   strm.next_in = (Bytef *) compressed_buffer;
13847   strm.avail_out = uncompressed_size;
13848   uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
13849 
13850   rc = inflateInit (& strm);
13851   while (strm.avail_in > 0)
13852     {
13853       if (rc != Z_OK)
13854         goto fail;
13855       strm.next_out = ((Bytef *) uncompressed_buffer
13856                        + (uncompressed_size - strm.avail_out));
13857       rc = inflate (&strm, Z_FINISH);
13858       if (rc != Z_STREAM_END)
13859         goto fail;
13860       rc = inflateReset (& strm);
13861     }
13862   rc = inflateEnd (& strm);
13863   if (rc != Z_OK
13864       || strm.avail_out != 0)
13865     goto fail;
13866 
13867   *buffer = uncompressed_buffer;
13868   *size = uncompressed_size;
13869   return TRUE;
13870 
13871  fail:
13872   free (uncompressed_buffer);
13873   /* Indicate decompression failure.  */
13874   *buffer = NULL;
13875   return FALSE;
13876 }
13877 
13878 static bfd_boolean
dump_section_as_strings(Elf_Internal_Shdr * section,Filedata * filedata)13879 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
13880 {
13881   Elf_Internal_Shdr *  relsec;
13882   bfd_size_type        num_bytes;
13883   unsigned char *      data;
13884   unsigned char *      end;
13885   unsigned char *      real_start;
13886   unsigned char *      start;
13887   bfd_boolean          some_strings_shown;
13888 
13889   real_start = start = (unsigned char *) get_section_contents (section, filedata);
13890   if (start == NULL)
13891     /* PR 21820: Do not fail if the section was empty.  */
13892     return (section->sh_size == 0 || section->sh_type == SHT_NOBITS) ? TRUE : FALSE;
13893 
13894   num_bytes = section->sh_size;
13895 
13896   printf (_("\nString dump of section '%s':\n"), printable_section_name (filedata, section));
13897 
13898   if (decompress_dumps)
13899     {
13900       dwarf_size_type new_size = num_bytes;
13901       dwarf_size_type uncompressed_size = 0;
13902 
13903       if ((section->sh_flags & SHF_COMPRESSED) != 0)
13904 	{
13905 	  Elf_Internal_Chdr chdr;
13906 	  unsigned int compression_header_size
13907 	    = get_compression_header (& chdr, (unsigned char *) start,
13908 				      num_bytes);
13909 	  if (compression_header_size == 0)
13910 	    /* An error message will have already been generated
13911 	       by get_compression_header.  */
13912 	    goto error_out;
13913 
13914 	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
13915 	    {
13916 	      warn (_("section '%s' has unsupported compress type: %d\n"),
13917 		    printable_section_name (filedata, section), chdr.ch_type);
13918 	      goto error_out;
13919 	    }
13920 	  uncompressed_size = chdr.ch_size;
13921 	  start += compression_header_size;
13922 	  new_size -= compression_header_size;
13923 	}
13924       else if (new_size > 12 && streq ((char *) start, "ZLIB"))
13925 	{
13926 	  /* Read the zlib header.  In this case, it should be "ZLIB"
13927 	     followed by the uncompressed section size, 8 bytes in
13928 	     big-endian order.  */
13929 	  uncompressed_size = start[4]; uncompressed_size <<= 8;
13930 	  uncompressed_size += start[5]; uncompressed_size <<= 8;
13931 	  uncompressed_size += start[6]; uncompressed_size <<= 8;
13932 	  uncompressed_size += start[7]; uncompressed_size <<= 8;
13933 	  uncompressed_size += start[8]; uncompressed_size <<= 8;
13934 	  uncompressed_size += start[9]; uncompressed_size <<= 8;
13935 	  uncompressed_size += start[10]; uncompressed_size <<= 8;
13936 	  uncompressed_size += start[11];
13937 	  start += 12;
13938 	  new_size -= 12;
13939 	}
13940 
13941       if (uncompressed_size)
13942 	{
13943 	  if (uncompress_section_contents (& start,
13944 					   uncompressed_size, & new_size))
13945 	    num_bytes = new_size;
13946 	  else
13947 	    {
13948 	      error (_("Unable to decompress section %s\n"),
13949 		     printable_section_name (filedata, section));
13950 	      goto error_out;
13951 	    }
13952 	}
13953       else
13954 	start = real_start;
13955     }
13956 
13957   /* If the section being dumped has relocations against it the user might
13958      be expecting these relocations to have been applied.  Check for this
13959      case and issue a warning message in order to avoid confusion.
13960      FIXME: Maybe we ought to have an option that dumps a section with
13961      relocs applied ?  */
13962   for (relsec = filedata->section_headers;
13963        relsec < filedata->section_headers + filedata->file_header.e_shnum;
13964        ++relsec)
13965     {
13966       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
13967 	  || relsec->sh_info >= filedata->file_header.e_shnum
13968 	  || filedata->section_headers + relsec->sh_info != section
13969 	  || relsec->sh_size == 0
13970 	  || relsec->sh_link >= filedata->file_header.e_shnum)
13971 	continue;
13972 
13973       printf (_("  Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
13974       break;
13975     }
13976 
13977   data = start;
13978   end  = start + num_bytes;
13979   some_strings_shown = FALSE;
13980 
13981 #ifdef HAVE_MBSTATE_T
13982   mbstate_t state;
13983   /* Initialise the multibyte conversion state.  */
13984   memset (& state, 0, sizeof (state));
13985 #endif
13986 
13987   bfd_boolean continuing = FALSE;
13988 
13989   while (data < end)
13990     {
13991       while (!ISPRINT (* data))
13992 	if (++ data >= end)
13993 	  break;
13994 
13995       if (data < end)
13996 	{
13997 	  size_t maxlen = end - data;
13998 
13999 	  if (continuing)
14000 	    {
14001 	      printf ("            ");
14002 	      continuing = FALSE;
14003 	    }
14004 	  else
14005 	    {
14006 	      printf ("  [%6lx]  ", (unsigned long) (data - start));
14007 	    }
14008 
14009 	  if (maxlen > 0)
14010 	    {
14011 	      char c = 0;
14012 
14013 	      while (maxlen)
14014 		{
14015 		  c = *data++;
14016 
14017 		  if (c == 0)
14018 		    break;
14019 
14020 		  /* PR 25543: Treat new-lines as string-ending characters.  */
14021 		  if (c == '\n')
14022 		    {
14023 		      printf ("\\n\n");
14024 		      if (*data != 0)
14025 			continuing = TRUE;
14026 		      break;
14027 		    }
14028 
14029 		  /* Do not print control characters directly as they can affect terminal
14030 		     settings.  Such characters usually appear in the names generated
14031 		     by the assembler for local labels.  */
14032 		  if (ISCNTRL (c))
14033 		    {
14034 		      printf ("^%c", c + 0x40);
14035 		    }
14036 		  else if (ISPRINT (c))
14037 		    {
14038 		      putchar (c);
14039 		    }
14040 		  else
14041 		    {
14042 		      size_t  n;
14043 #ifdef HAVE_MBSTATE_T
14044 		      wchar_t w;
14045 #endif
14046 		      /* Let printf do the hard work of displaying multibyte characters.  */
14047 		      printf ("%.1s", data - 1);
14048 #ifdef HAVE_MBSTATE_T
14049 		      /* Try to find out how many bytes made up the character that was
14050 			 just printed.  Advance the symbol pointer past the bytes that
14051 			 were displayed.  */
14052 		      n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
14053 #else
14054 		      n = 1;
14055 #endif
14056 		      if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
14057 			data += (n - 1);
14058 		    }
14059 		}
14060 
14061 	      if (c != '\n')
14062 		putchar ('\n');
14063 	    }
14064 	  else
14065 	    {
14066 	      printf (_("<corrupt>\n"));
14067 	      data = end;
14068 	    }
14069 	  some_strings_shown = TRUE;
14070 	}
14071     }
14072 
14073   if (! some_strings_shown)
14074     printf (_("  No strings found in this section."));
14075 
14076   free (real_start);
14077 
14078   putchar ('\n');
14079   return TRUE;
14080 
14081 error_out:
14082   free (real_start);
14083   return FALSE;
14084 }
14085 
14086 static bfd_boolean
dump_section_as_bytes(Elf_Internal_Shdr * section,Filedata * filedata,bfd_boolean relocate)14087 dump_section_as_bytes (Elf_Internal_Shdr *  section,
14088 		       Filedata *           filedata,
14089 		       bfd_boolean          relocate)
14090 {
14091   Elf_Internal_Shdr * relsec;
14092   bfd_size_type       bytes;
14093   bfd_size_type       section_size;
14094   bfd_vma             addr;
14095   unsigned char *     data;
14096   unsigned char *     real_start;
14097   unsigned char *     start;
14098 
14099   real_start = start = (unsigned char *) get_section_contents (section, filedata);
14100   if (start == NULL)
14101     /* PR 21820: Do not fail if the section was empty.  */
14102     return (section->sh_size == 0 || section->sh_type == SHT_NOBITS) ? TRUE : FALSE;
14103 
14104   section_size = section->sh_size;
14105 
14106   printf (_("\nHex dump of section '%s':\n"), printable_section_name (filedata, section));
14107 
14108   if (decompress_dumps)
14109     {
14110       dwarf_size_type new_size = section_size;
14111       dwarf_size_type uncompressed_size = 0;
14112 
14113       if ((section->sh_flags & SHF_COMPRESSED) != 0)
14114 	{
14115 	  Elf_Internal_Chdr chdr;
14116 	  unsigned int compression_header_size
14117 	    = get_compression_header (& chdr, start, section_size);
14118 
14119 	  if (compression_header_size == 0)
14120 	    /* An error message will have already been generated
14121 	       by get_compression_header.  */
14122 	    goto error_out;
14123 
14124 	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14125 	    {
14126 	      warn (_("section '%s' has unsupported compress type: %d\n"),
14127 		    printable_section_name (filedata, section), chdr.ch_type);
14128 	      goto error_out;
14129 	    }
14130 	  uncompressed_size = chdr.ch_size;
14131 	  start += compression_header_size;
14132 	  new_size -= compression_header_size;
14133 	}
14134       else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14135 	{
14136 	  /* Read the zlib header.  In this case, it should be "ZLIB"
14137 	     followed by the uncompressed section size, 8 bytes in
14138 	     big-endian order.  */
14139 	  uncompressed_size = start[4]; uncompressed_size <<= 8;
14140 	  uncompressed_size += start[5]; uncompressed_size <<= 8;
14141 	  uncompressed_size += start[6]; uncompressed_size <<= 8;
14142 	  uncompressed_size += start[7]; uncompressed_size <<= 8;
14143 	  uncompressed_size += start[8]; uncompressed_size <<= 8;
14144 	  uncompressed_size += start[9]; uncompressed_size <<= 8;
14145 	  uncompressed_size += start[10]; uncompressed_size <<= 8;
14146 	  uncompressed_size += start[11];
14147 	  start += 12;
14148 	  new_size -= 12;
14149 	}
14150 
14151       if (uncompressed_size)
14152 	{
14153 	  if (uncompress_section_contents (& start, uncompressed_size,
14154 					   & new_size))
14155 	    {
14156 	      section_size = new_size;
14157 	    }
14158 	  else
14159 	    {
14160 	      error (_("Unable to decompress section %s\n"),
14161 		     printable_section_name (filedata, section));
14162 	      /* FIXME: Print the section anyway ?  */
14163 	      goto error_out;
14164 	    }
14165 	}
14166       else
14167 	start = real_start;
14168     }
14169 
14170   if (relocate)
14171     {
14172       if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
14173 	goto error_out;
14174     }
14175   else
14176     {
14177       /* If the section being dumped has relocations against it the user might
14178 	 be expecting these relocations to have been applied.  Check for this
14179 	 case and issue a warning message in order to avoid confusion.
14180 	 FIXME: Maybe we ought to have an option that dumps a section with
14181 	 relocs applied ?  */
14182       for (relsec = filedata->section_headers;
14183 	   relsec < filedata->section_headers + filedata->file_header.e_shnum;
14184 	   ++relsec)
14185 	{
14186 	  if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14187 	      || relsec->sh_info >= filedata->file_header.e_shnum
14188 	      || filedata->section_headers + relsec->sh_info != section
14189 	      || relsec->sh_size == 0
14190 	      || relsec->sh_link >= filedata->file_header.e_shnum)
14191 	    continue;
14192 
14193 	  printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14194 	  break;
14195 	}
14196     }
14197 
14198   addr = section->sh_addr;
14199   bytes = section_size;
14200   data = start;
14201 
14202   while (bytes)
14203     {
14204       int j;
14205       int k;
14206       int lbytes;
14207 
14208       lbytes = (bytes > 16 ? 16 : bytes);
14209 
14210       printf ("  0x%8.8lx ", (unsigned long) addr);
14211 
14212       for (j = 0; j < 16; j++)
14213 	{
14214 	  if (j < lbytes)
14215 	    printf ("%2.2x", data[j]);
14216 	  else
14217 	    printf ("  ");
14218 
14219 	  if ((j & 3) == 3)
14220 	    printf (" ");
14221 	}
14222 
14223       for (j = 0; j < lbytes; j++)
14224 	{
14225 	  k = data[j];
14226 	  if (k >= ' ' && k < 0x7f)
14227 	    printf ("%c", k);
14228 	  else
14229 	    printf (".");
14230 	}
14231 
14232       putchar ('\n');
14233 
14234       data  += lbytes;
14235       addr  += lbytes;
14236       bytes -= lbytes;
14237     }
14238 
14239   free (real_start);
14240 
14241   putchar ('\n');
14242   return TRUE;
14243 
14244  error_out:
14245   free (real_start);
14246   return FALSE;
14247 }
14248 
14249 #ifdef ENABLE_LIBCTF
14250 static ctf_sect_t *
shdr_to_ctf_sect(ctf_sect_t * buf,Elf_Internal_Shdr * shdr,Filedata * filedata)14251 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
14252 {
14253   buf->cts_name = SECTION_NAME (shdr);
14254   buf->cts_size = shdr->sh_size;
14255   buf->cts_entsize = shdr->sh_entsize;
14256 
14257   return buf;
14258 }
14259 
14260 /* Formatting callback function passed to ctf_dump.  Returns either the pointer
14261    it is passed, or a pointer to newly-allocated storage, in which case
14262    dump_ctf() will free it when it no longer needs it.  */
14263 
14264 static char *
dump_ctf_indent_lines(ctf_sect_names_t sect ATTRIBUTE_UNUSED,char * s,void * arg)14265 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
14266 		       char *s, void *arg)
14267 {
14268   const char *blanks = arg;
14269   char *new_s;
14270 
14271   if (asprintf (&new_s, "%s%s", blanks, s) < 0)
14272     return s;
14273   return new_s;
14274 }
14275 
14276 /* Dump CTF errors/warnings.  */
14277 static void
dump_ctf_errs(ctf_file_t * fp)14278 dump_ctf_errs (ctf_file_t *fp)
14279 {
14280   ctf_next_t *it = NULL;
14281   char *errtext;
14282   int is_warning;
14283   int err;
14284 
14285   /* Dump accumulated errors and warnings.  */
14286   while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
14287     {
14288       error (_("%s: %s"), is_warning ? _("warning"): _("error"),
14289 	     errtext);
14290       free (errtext);
14291     }
14292   if (err != ECTF_NEXT_END)
14293     error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
14294 }
14295 
14296 /* Dump one CTF archive member.  */
14297 
14298 static int
dump_ctf_archive_member(ctf_file_t * ctf,const char * name,void * arg)14299 dump_ctf_archive_member (ctf_file_t *ctf, const char *name, void *arg)
14300 {
14301   ctf_file_t *parent = (ctf_file_t *) arg;
14302   const char *things[] = {"Header", "Labels", "Data objects",
14303 			  "Function objects", "Variables", "Types", "Strings",
14304 			  ""};
14305   const char **thing;
14306   size_t i;
14307   int err = 0;
14308 
14309   /* Only print out the name of non-default-named archive members.
14310      The name .ctf appears everywhere, even for things that aren't
14311      really archives, so printing it out is liable to be confusing.
14312 
14313      The parent, if there is one, is the default-owned archive member:
14314      avoid importing it into itself.  (This does no harm, but looks
14315      confusing.)  */
14316 
14317   if (strcmp (name, ".ctf") != 0)
14318     {
14319       printf (_("\nCTF archive member: %s:\n"), name);
14320       ctf_import (ctf, parent);
14321     }
14322 
14323   for (i = 0, thing = things; *thing[0]; thing++, i++)
14324     {
14325       ctf_dump_state_t *s = NULL;
14326       char *item;
14327 
14328       printf ("\n  %s:\n", *thing);
14329       while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
14330 			       (void *) "    ")) != NULL)
14331 	{
14332 	  printf ("%s\n", item);
14333 	  free (item);
14334 	}
14335 
14336       if (ctf_errno (ctf))
14337 	{
14338 	  error (_("Iteration failed: %s, %s\n"), *thing,
14339 		 ctf_errmsg (ctf_errno (ctf)));
14340 	  err = 1;
14341 	  goto out;
14342 	}
14343     }
14344 
14345  out:
14346   dump_ctf_errs (ctf);
14347   return err;
14348 }
14349 
14350 static bfd_boolean
dump_section_as_ctf(Elf_Internal_Shdr * section,Filedata * filedata)14351 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
14352 {
14353   Elf_Internal_Shdr *  parent_sec = NULL;
14354   Elf_Internal_Shdr *  symtab_sec = NULL;
14355   Elf_Internal_Shdr *  strtab_sec = NULL;
14356   void *	       data = NULL;
14357   void *	       symdata = NULL;
14358   void *	       strdata = NULL;
14359   void *	       parentdata = NULL;
14360   ctf_sect_t	       ctfsect, symsect, strsect, parentsect;
14361   ctf_sect_t *	       symsectp = NULL;
14362   ctf_sect_t *	       strsectp = NULL;
14363   ctf_archive_t *      ctfa = NULL;
14364   ctf_archive_t *      parenta = NULL, *lookparent;
14365   ctf_file_t *         parent = NULL;
14366 
14367   int err;
14368   bfd_boolean ret = FALSE;
14369 
14370   shdr_to_ctf_sect (&ctfsect, section, filedata);
14371   data = get_section_contents (section, filedata);
14372   ctfsect.cts_data = data;
14373 
14374   if (!dump_ctf_symtab_name)
14375     dump_ctf_symtab_name = strdup (".symtab");
14376 
14377   if (!dump_ctf_strtab_name)
14378     dump_ctf_strtab_name = strdup (".strtab");
14379 
14380   if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
14381     {
14382       if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
14383 	{
14384 	  error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
14385 	  goto fail;
14386 	}
14387       if ((symdata = (void *) get_data (NULL, filedata,
14388 					symtab_sec->sh_offset, 1,
14389 					symtab_sec->sh_size,
14390 					_("symbols"))) == NULL)
14391 	goto fail;
14392       symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
14393       symsect.cts_data = symdata;
14394     }
14395   if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
14396     {
14397       if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
14398 	{
14399 	  error (_("No string table section named %s\n"),
14400 		 dump_ctf_strtab_name);
14401 	  goto fail;
14402 	}
14403       if ((strdata = (void *) get_data (NULL, filedata,
14404 					strtab_sec->sh_offset, 1,
14405 					strtab_sec->sh_size,
14406 					_("strings"))) == NULL)
14407 	goto fail;
14408       strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
14409       strsect.cts_data = strdata;
14410     }
14411   if (dump_ctf_parent_name)
14412     {
14413       if ((parent_sec = find_section (filedata, dump_ctf_parent_name)) == NULL)
14414 	{
14415 	  error (_("No CTF parent section named %s\n"), dump_ctf_parent_name);
14416 	  goto fail;
14417 	}
14418       if ((parentdata = (void *) get_data (NULL, filedata,
14419 					   parent_sec->sh_offset, 1,
14420 					   parent_sec->sh_size,
14421 					   _("CTF parent"))) == NULL)
14422 	goto fail;
14423       shdr_to_ctf_sect (&parentsect, parent_sec, filedata);
14424       parentsect.cts_data = parentdata;
14425     }
14426 
14427   /* Load the CTF file and dump it.  It may be a raw CTF section, or an archive:
14428      libctf papers over the difference, so we can pretend it is always an
14429      archive.  Possibly open the parent as well, if one was specified.  */
14430 
14431   if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
14432     {
14433       dump_ctf_errs (NULL);
14434       error (_("CTF open failure: %s\n"), ctf_errmsg (err));
14435       goto fail;
14436     }
14437 
14438   if (parentdata)
14439     {
14440       if ((parenta = ctf_arc_bufopen (&parentsect, symsectp, strsectp,
14441 				      &err)) == NULL)
14442 	{
14443 	  dump_ctf_errs (NULL);
14444 	  error (_("CTF open failure: %s\n"), ctf_errmsg (err));
14445 	  goto fail;
14446 	}
14447       lookparent = parenta;
14448     }
14449   else
14450     lookparent = ctfa;
14451 
14452   /* Assume that the applicable parent archive member is the default one.
14453      (This is what all known implementations are expected to do, if they
14454      put CTFs and their parents in archives together.)  */
14455   if ((parent = ctf_arc_open_by_name (lookparent, NULL, &err)) == NULL)
14456     {
14457       dump_ctf_errs (NULL);
14458       error (_("CTF open failure: %s\n"), ctf_errmsg (err));
14459       goto fail;
14460     }
14461 
14462   ret = TRUE;
14463 
14464   printf (_("\nDump of CTF section '%s':\n"),
14465 	  printable_section_name (filedata, section));
14466 
14467   if (ctf_archive_iter (ctfa, dump_ctf_archive_member, parent) != 0)
14468     ret = FALSE;
14469 
14470  fail:
14471   ctf_file_close (parent);
14472   ctf_close (ctfa);
14473   ctf_close (parenta);
14474   free (parentdata);
14475   free (data);
14476   free (symdata);
14477   free (strdata);
14478   return ret;
14479 }
14480 #endif
14481 
14482 static bfd_boolean
load_specific_debug_section(enum dwarf_section_display_enum debug,const Elf_Internal_Shdr * sec,void * data)14483 load_specific_debug_section (enum dwarf_section_display_enum  debug,
14484 			     const Elf_Internal_Shdr *        sec,
14485 			     void *                           data)
14486 {
14487   struct dwarf_section * section = &debug_displays [debug].section;
14488   char buf [64];
14489   Filedata * filedata = (Filedata *) data;
14490 
14491   if (section->start != NULL)
14492     {
14493       /* If it is already loaded, do nothing.  */
14494       if (streq (section->filename, filedata->file_name))
14495 	return TRUE;
14496       free (section->start);
14497     }
14498 
14499   snprintf (buf, sizeof (buf), _("%s section data"), section->name);
14500   section->address = sec->sh_addr;
14501   section->user_data = NULL;
14502   section->filename = filedata->file_name;
14503   section->start = (unsigned char *) get_data (NULL, filedata,
14504                                                sec->sh_offset, 1,
14505                                                sec->sh_size, buf);
14506   if (section->start == NULL)
14507     section->size = 0;
14508   else
14509     {
14510       unsigned char *start = section->start;
14511       dwarf_size_type size = sec->sh_size;
14512       dwarf_size_type uncompressed_size = 0;
14513 
14514       if ((sec->sh_flags & SHF_COMPRESSED) != 0)
14515 	{
14516 	  Elf_Internal_Chdr chdr;
14517 	  unsigned int compression_header_size;
14518 
14519 	  if (size < (is_32bit_elf
14520 		      ? sizeof (Elf32_External_Chdr)
14521 		      : sizeof (Elf64_External_Chdr)))
14522 	    {
14523 	      warn (_("compressed section %s is too small to contain a compression header\n"),
14524 		    section->name);
14525 	      return FALSE;
14526 	    }
14527 
14528 	  compression_header_size = get_compression_header (&chdr, start, size);
14529 	  if (compression_header_size == 0)
14530 	    /* An error message will have already been generated
14531 	       by get_compression_header.  */
14532 	    return FALSE;
14533 
14534 	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14535 	    {
14536 	      warn (_("section '%s' has unsupported compress type: %d\n"),
14537 		    section->name, chdr.ch_type);
14538 	      return FALSE;
14539 	    }
14540 	  uncompressed_size = chdr.ch_size;
14541 	  start += compression_header_size;
14542 	  size -= compression_header_size;
14543 	}
14544       else if (size > 12 && streq ((char *) start, "ZLIB"))
14545 	{
14546 	  /* Read the zlib header.  In this case, it should be "ZLIB"
14547 	     followed by the uncompressed section size, 8 bytes in
14548 	     big-endian order.  */
14549 	  uncompressed_size = start[4]; uncompressed_size <<= 8;
14550 	  uncompressed_size += start[5]; uncompressed_size <<= 8;
14551 	  uncompressed_size += start[6]; uncompressed_size <<= 8;
14552 	  uncompressed_size += start[7]; uncompressed_size <<= 8;
14553 	  uncompressed_size += start[8]; uncompressed_size <<= 8;
14554 	  uncompressed_size += start[9]; uncompressed_size <<= 8;
14555 	  uncompressed_size += start[10]; uncompressed_size <<= 8;
14556 	  uncompressed_size += start[11];
14557 	  start += 12;
14558 	  size -= 12;
14559 	}
14560 
14561       if (uncompressed_size)
14562 	{
14563 	  if (uncompress_section_contents (&start, uncompressed_size,
14564 					   &size))
14565 	    {
14566 	      /* Free the compressed buffer, update the section buffer
14567 		 and the section size if uncompress is successful.  */
14568 	      free (section->start);
14569 	      section->start = start;
14570 	    }
14571 	  else
14572 	    {
14573 	      error (_("Unable to decompress section %s\n"),
14574 		     printable_section_name (filedata, sec));
14575 	      return FALSE;
14576 	    }
14577 	}
14578 
14579       section->size = size;
14580     }
14581 
14582   if (section->start == NULL)
14583     return FALSE;
14584 
14585   if (debug_displays [debug].relocate)
14586     {
14587       if (! apply_relocations (filedata, sec, section->start, section->size,
14588 			       & section->reloc_info, & section->num_relocs))
14589 	return FALSE;
14590     }
14591   else
14592     {
14593       section->reloc_info = NULL;
14594       section->num_relocs = 0;
14595     }
14596 
14597   return TRUE;
14598 }
14599 
14600 #if HAVE_LIBDEBUGINFOD
14601 /* Return a hex string representation of the build-id.  */
14602 unsigned char *
get_build_id(void * data)14603 get_build_id (void * data)
14604 {
14605   Filedata * filedata = (Filedata *)data;
14606   Elf_Internal_Shdr * shdr;
14607   unsigned long i;
14608 
14609   /* Iterate through notes to find note.gnu.build-id.
14610      FIXME: Only the first note in any note section is examined.  */
14611   for (i = 0, shdr = filedata->section_headers;
14612        i < filedata->file_header.e_shnum && shdr != NULL;
14613        i++, shdr++)
14614     {
14615       if (shdr->sh_type != SHT_NOTE)
14616         continue;
14617 
14618       char * next;
14619       char * end;
14620       size_t data_remaining;
14621       size_t min_notesz;
14622       Elf_External_Note * enote;
14623       Elf_Internal_Note inote;
14624 
14625       bfd_vma offset = shdr->sh_offset;
14626       bfd_vma align = shdr->sh_addralign;
14627       bfd_vma length = shdr->sh_size;
14628 
14629       enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
14630       if (enote == NULL)
14631         continue;
14632 
14633       if (align < 4)
14634         align = 4;
14635       else if (align != 4 && align != 8)
14636 	{
14637 	  free (enote);
14638 	  continue;
14639 	}
14640 
14641       end = (char *) enote + length;
14642       data_remaining = end - (char *) enote;
14643 
14644       if (!is_ia64_vms (filedata))
14645         {
14646           min_notesz = offsetof (Elf_External_Note, name);
14647           if (data_remaining < min_notesz)
14648             {
14649 	      warn (_("\
14650 malformed note encountered in section %s whilst scanning for build-id note\n"),
14651 		    printable_section_name (filedata, shdr));
14652 	      free (enote);
14653               continue;
14654             }
14655           data_remaining -= min_notesz;
14656 
14657           inote.type     = BYTE_GET (enote->type);
14658           inote.namesz   = BYTE_GET (enote->namesz);
14659           inote.namedata = enote->name;
14660           inote.descsz   = BYTE_GET (enote->descsz);
14661           inote.descdata = ((char *) enote
14662                             + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
14663           inote.descpos  = offset + (inote.descdata - (char *) enote);
14664           next = ((char *) enote
14665                   + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
14666         }
14667       else
14668         {
14669           Elf64_External_VMS_Note *vms_enote;
14670 
14671           /* PR binutils/15191
14672              Make sure that there is enough data to read.  */
14673           min_notesz = offsetof (Elf64_External_VMS_Note, name);
14674           if (data_remaining < min_notesz)
14675             {
14676 	      warn (_("\
14677 malformed note encountered in section %s whilst scanning for build-id note\n"),
14678 		    printable_section_name (filedata, shdr));
14679 	      free (enote);
14680               continue;
14681             }
14682           data_remaining -= min_notesz;
14683 
14684           vms_enote = (Elf64_External_VMS_Note *) enote;
14685           inote.type     = BYTE_GET (vms_enote->type);
14686           inote.namesz   = BYTE_GET (vms_enote->namesz);
14687           inote.namedata = vms_enote->name;
14688           inote.descsz   = BYTE_GET (vms_enote->descsz);
14689           inote.descdata = inote.namedata + align_power (inote.namesz, 3);
14690           inote.descpos  = offset + (inote.descdata - (char *) enote);
14691           next = inote.descdata + align_power (inote.descsz, 3);
14692         }
14693 
14694       /* Skip malformed notes.  */
14695       if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
14696           || (size_t) (inote.descdata - inote.namedata) > data_remaining
14697           || (size_t) (next - inote.descdata) < inote.descsz
14698           || ((size_t) (next - inote.descdata)
14699               > data_remaining - (size_t) (inote.descdata - inote.namedata)))
14700         {
14701 	  warn (_("\
14702 malformed note encountered in section %s whilst scanning for build-id note\n"),
14703 		printable_section_name (filedata, shdr));
14704 	  free (enote);
14705           continue;
14706         }
14707 
14708       /* Check if this is the build-id note. If so then convert the build-id
14709          bytes to a hex string.  */
14710       if (inote.namesz > 0
14711           && const_strneq (inote.namedata, "GNU")
14712           && inote.type == NT_GNU_BUILD_ID)
14713         {
14714           unsigned long j;
14715           char * build_id;
14716 
14717           build_id = malloc (inote.descsz * 2 + 1);
14718           if (build_id == NULL)
14719 	    {
14720 	      free (enote);
14721 	      return NULL;
14722 	    }
14723 
14724           for (j = 0; j < inote.descsz; ++j)
14725             sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
14726           build_id[inote.descsz * 2] = '\0';
14727 	  free (enote);
14728 
14729           return (unsigned char *) build_id;
14730         }
14731       free (enote);
14732     }
14733 
14734   return NULL;
14735 }
14736 #endif /* HAVE_LIBDEBUGINFOD */
14737 
14738 /* If this is not NULL, load_debug_section will only look for sections
14739    within the list of sections given here.  */
14740 static unsigned int * section_subset = NULL;
14741 
14742 bfd_boolean
load_debug_section(enum dwarf_section_display_enum debug,void * data)14743 load_debug_section (enum dwarf_section_display_enum debug, void * data)
14744 {
14745   struct dwarf_section * section = &debug_displays [debug].section;
14746   Elf_Internal_Shdr * sec;
14747   Filedata * filedata = (Filedata *) data;
14748 
14749   /* Without section headers we cannot find any sections.  */
14750   if (filedata->section_headers == NULL)
14751     return FALSE;
14752 
14753   if (filedata->string_table == NULL
14754       && filedata->file_header.e_shstrndx != SHN_UNDEF
14755       && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
14756     {
14757       Elf_Internal_Shdr * strs;
14758 
14759       /* Read in the string table, so that we have section names to scan.  */
14760       strs = filedata->section_headers + filedata->file_header.e_shstrndx;
14761 
14762       if (strs != NULL && strs->sh_size != 0)
14763 	{
14764 	  filedata->string_table
14765 	    = (char *) get_data (NULL, filedata, strs->sh_offset,
14766 				 1, strs->sh_size, _("string table"));
14767 
14768 	  filedata->string_table_length
14769 	    = filedata->string_table != NULL ? strs->sh_size : 0;
14770 	}
14771     }
14772 
14773   /* Locate the debug section.  */
14774   sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
14775   if (sec != NULL)
14776     section->name = section->uncompressed_name;
14777   else
14778     {
14779       sec = find_section_in_set (filedata, section->compressed_name, section_subset);
14780       if (sec != NULL)
14781 	section->name = section->compressed_name;
14782     }
14783   if (sec == NULL)
14784     return FALSE;
14785 
14786   /* If we're loading from a subset of sections, and we've loaded
14787      a section matching this name before, it's likely that it's a
14788      different one.  */
14789   if (section_subset != NULL)
14790     free_debug_section (debug);
14791 
14792   return load_specific_debug_section (debug, sec, data);
14793 }
14794 
14795 void
free_debug_section(enum dwarf_section_display_enum debug)14796 free_debug_section (enum dwarf_section_display_enum debug)
14797 {
14798   struct dwarf_section * section = &debug_displays [debug].section;
14799 
14800   if (section->start == NULL)
14801     return;
14802 
14803   free ((char *) section->start);
14804   section->start = NULL;
14805   section->address = 0;
14806   section->size = 0;
14807 
14808   free (section->reloc_info);
14809   section->reloc_info = NULL;
14810   section->num_relocs = 0;
14811 }
14812 
14813 static bfd_boolean
display_debug_section(int shndx,Elf_Internal_Shdr * section,Filedata * filedata)14814 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
14815 {
14816   char * name = SECTION_NAME (section);
14817   const char * print_name = printable_section_name (filedata, section);
14818   bfd_size_type length;
14819   bfd_boolean result = TRUE;
14820   int i;
14821 
14822   length = section->sh_size;
14823   if (length == 0)
14824     {
14825       printf (_("\nSection '%s' has no debugging data.\n"), print_name);
14826       return TRUE;
14827     }
14828   if (section->sh_type == SHT_NOBITS)
14829     {
14830       /* There is no point in dumping the contents of a debugging section
14831 	 which has the NOBITS type - the bits in the file will be random.
14832 	 This can happen when a file containing a .eh_frame section is
14833 	 stripped with the --only-keep-debug command line option.  */
14834       printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
14835 	      print_name);
14836       return FALSE;
14837     }
14838 
14839   if (const_strneq (name, ".gnu.linkonce.wi."))
14840     name = ".debug_info";
14841 
14842   /* See if we know how to display the contents of this section.  */
14843   for (i = 0; i < max; i++)
14844     {
14845       enum dwarf_section_display_enum  id = (enum dwarf_section_display_enum) i;
14846       struct dwarf_section_display *   display = debug_displays + i;
14847       struct dwarf_section *           sec = & display->section;
14848 
14849       if (streq (sec->uncompressed_name, name)
14850 	  || (id == line && const_strneq (name, ".debug_line."))
14851 	  || streq (sec->compressed_name, name))
14852 	{
14853 	  bfd_boolean secondary = (section != find_section (filedata, name));
14854 
14855 	  if (secondary)
14856 	    free_debug_section (id);
14857 
14858 	  if (i == line && const_strneq (name, ".debug_line."))
14859 	    sec->name = name;
14860 	  else if (streq (sec->uncompressed_name, name))
14861 	    sec->name = sec->uncompressed_name;
14862 	  else
14863 	    sec->name = sec->compressed_name;
14864 
14865 	  if (load_specific_debug_section (id, section, filedata))
14866 	    {
14867 	      /* If this debug section is part of a CU/TU set in a .dwp file,
14868 		 restrict load_debug_section to the sections in that set.  */
14869 	      section_subset = find_cu_tu_set (filedata, shndx);
14870 
14871 	      result &= display->display (sec, filedata);
14872 
14873 	      section_subset = NULL;
14874 
14875 	      if (secondary || (id != info && id != abbrev))
14876 		free_debug_section (id);
14877 	    }
14878 	  break;
14879 	}
14880     }
14881 
14882   if (i == max)
14883     {
14884       printf (_("Unrecognized debug section: %s\n"), print_name);
14885       result = FALSE;
14886     }
14887 
14888   return result;
14889 }
14890 
14891 /* Set DUMP_SECTS for all sections where dumps were requested
14892    based on section name.  */
14893 
14894 static void
initialise_dumps_byname(Filedata * filedata)14895 initialise_dumps_byname (Filedata * filedata)
14896 {
14897   struct dump_list_entry * cur;
14898 
14899   for (cur = dump_sects_byname; cur; cur = cur->next)
14900     {
14901       unsigned int i;
14902       bfd_boolean any = FALSE;
14903 
14904       for (i = 0; i < filedata->file_header.e_shnum; i++)
14905 	if (streq (SECTION_NAME (filedata->section_headers + i), cur->name))
14906 	  {
14907 	    request_dump_bynumber (&filedata->dump, i, cur->type);
14908 	    any = TRUE;
14909 	  }
14910 
14911       if (!any)
14912 	warn (_("Section '%s' was not dumped because it does not exist!\n"),
14913 	      cur->name);
14914     }
14915 }
14916 
14917 static bfd_boolean
process_section_contents(Filedata * filedata)14918 process_section_contents (Filedata * filedata)
14919 {
14920   Elf_Internal_Shdr * section;
14921   unsigned int i;
14922   bfd_boolean res = TRUE;
14923 
14924   if (! do_dump)
14925     return TRUE;
14926 
14927   initialise_dumps_byname (filedata);
14928 
14929   for (i = 0, section = filedata->section_headers;
14930        i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
14931        i++, section++)
14932     {
14933       dump_type dump = filedata->dump.dump_sects[i];
14934 
14935 #ifdef SUPPORT_DISASSEMBLY
14936       if (dump & DISASS_DUMP)
14937 	{
14938 	  if (! disassemble_section (section, filedata))
14939 	    res = FALSE;
14940 	}
14941 #endif
14942       if (dump & HEX_DUMP)
14943 	{
14944 	  if (! dump_section_as_bytes (section, filedata, FALSE))
14945 	    res = FALSE;
14946 	}
14947 
14948       if (dump & RELOC_DUMP)
14949 	{
14950 	  if (! dump_section_as_bytes (section, filedata, TRUE))
14951 	    res = FALSE;
14952 	}
14953 
14954       if (dump & STRING_DUMP)
14955 	{
14956 	  if (! dump_section_as_strings (section, filedata))
14957 	    res = FALSE;
14958 	}
14959 
14960       if (dump & DEBUG_DUMP)
14961 	{
14962 	  if (! display_debug_section (i, section, filedata))
14963 	    res = FALSE;
14964 	}
14965 
14966 #ifdef ENABLE_LIBCTF
14967       if (dump & CTF_DUMP)
14968 	{
14969 	  if (! dump_section_as_ctf (section, filedata))
14970 	    res = FALSE;
14971 	}
14972 #endif
14973     }
14974 
14975   /* Check to see if the user requested a
14976      dump of a section that does not exist.  */
14977   while (i < filedata->dump.num_dump_sects)
14978     {
14979       if (filedata->dump.dump_sects[i])
14980 	{
14981 	  warn (_("Section %d was not dumped because it does not exist!\n"), i);
14982 	  res = FALSE;
14983 	}
14984       i++;
14985     }
14986 
14987   return res;
14988 }
14989 
14990 static void
process_mips_fpe_exception(int mask)14991 process_mips_fpe_exception (int mask)
14992 {
14993   if (mask)
14994     {
14995       bfd_boolean first = TRUE;
14996 
14997       if (mask & OEX_FPU_INEX)
14998 	fputs ("INEX", stdout), first = FALSE;
14999       if (mask & OEX_FPU_UFLO)
15000 	printf ("%sUFLO", first ? "" : "|"), first = FALSE;
15001       if (mask & OEX_FPU_OFLO)
15002 	printf ("%sOFLO", first ? "" : "|"), first = FALSE;
15003       if (mask & OEX_FPU_DIV0)
15004 	printf ("%sDIV0", first ? "" : "|"), first = FALSE;
15005       if (mask & OEX_FPU_INVAL)
15006 	printf ("%sINVAL", first ? "" : "|");
15007     }
15008   else
15009     fputs ("0", stdout);
15010 }
15011 
15012 /* Display's the value of TAG at location P.  If TAG is
15013    greater than 0 it is assumed to be an unknown tag, and
15014    a message is printed to this effect.  Otherwise it is
15015    assumed that a message has already been printed.
15016 
15017    If the bottom bit of TAG is set it assumed to have a
15018    string value, otherwise it is assumed to have an integer
15019    value.
15020 
15021    Returns an updated P pointing to the first unread byte
15022    beyond the end of TAG's value.
15023 
15024    Reads at or beyond END will not be made.  */
15025 
15026 static unsigned char *
display_tag_value(signed int tag,unsigned char * p,const unsigned char * const end)15027 display_tag_value (signed int tag,
15028 		   unsigned char * p,
15029 		   const unsigned char * const end)
15030 {
15031   unsigned long val;
15032 
15033   if (tag > 0)
15034     printf ("  Tag_unknown_%d: ", tag);
15035 
15036   if (p >= end)
15037     {
15038       warn (_("<corrupt tag>\n"));
15039     }
15040   else if (tag & 1)
15041     {
15042       /* PR 17531 file: 027-19978-0.004.  */
15043       size_t maxlen = (end - p) - 1;
15044 
15045       putchar ('"');
15046       if (maxlen > 0)
15047 	{
15048 	  print_symbol ((int) maxlen, (const char *) p);
15049 	  p += strnlen ((char *) p, maxlen) + 1;
15050 	}
15051       else
15052 	{
15053 	  printf (_("<corrupt string tag>"));
15054 	  p = (unsigned char *) end;
15055 	}
15056       printf ("\"\n");
15057     }
15058   else
15059     {
15060       READ_ULEB (val, p, end);
15061       printf ("%ld (0x%lx)\n", val, val);
15062     }
15063 
15064   assert (p <= end);
15065   return p;
15066 }
15067 
15068 /* ARC ABI attributes section.  */
15069 
15070 static unsigned char *
display_arc_attribute(unsigned char * p,const unsigned char * const end)15071 display_arc_attribute (unsigned char * p,
15072 		       const unsigned char * const end)
15073 {
15074   unsigned int tag;
15075   unsigned int val;
15076 
15077   READ_ULEB (tag, p, end);
15078 
15079   switch (tag)
15080     {
15081     case Tag_ARC_PCS_config:
15082       READ_ULEB (val, p, end);
15083       printf ("  Tag_ARC_PCS_config: ");
15084       switch (val)
15085 	{
15086 	case 0:
15087 	  printf (_("Absent/Non standard\n"));
15088 	  break;
15089 	case 1:
15090 	  printf (_("Bare metal/mwdt\n"));
15091 	  break;
15092 	case 2:
15093 	  printf (_("Bare metal/newlib\n"));
15094 	  break;
15095 	case 3:
15096 	  printf (_("Linux/uclibc\n"));
15097 	  break;
15098 	case 4:
15099 	  printf (_("Linux/glibc\n"));
15100 	  break;
15101 	default:
15102 	  printf (_("Unknown\n"));
15103 	  break;
15104 	}
15105       break;
15106 
15107     case Tag_ARC_CPU_base:
15108       READ_ULEB (val, p, end);
15109       printf ("  Tag_ARC_CPU_base: ");
15110       switch (val)
15111 	{
15112 	default:
15113 	case TAG_CPU_NONE:
15114 	  printf (_("Absent\n"));
15115 	  break;
15116 	case TAG_CPU_ARC6xx:
15117 	  printf ("ARC6xx\n");
15118 	  break;
15119 	case TAG_CPU_ARC7xx:
15120 	  printf ("ARC7xx\n");
15121 	  break;
15122 	case TAG_CPU_ARCEM:
15123 	  printf ("ARCEM\n");
15124 	  break;
15125 	case TAG_CPU_ARCHS:
15126 	  printf ("ARCHS\n");
15127 	  break;
15128 	}
15129       break;
15130 
15131     case Tag_ARC_CPU_variation:
15132       READ_ULEB (val, p, end);
15133       printf ("  Tag_ARC_CPU_variation: ");
15134       switch (val)
15135 	{
15136 	default:
15137 	  if (val > 0 && val < 16)
15138 	      printf ("Core%d\n", val);
15139 	  else
15140 	      printf ("Unknown\n");
15141 	  break;
15142 
15143 	case 0:
15144 	  printf (_("Absent\n"));
15145 	  break;
15146 	}
15147       break;
15148 
15149     case Tag_ARC_CPU_name:
15150       printf ("  Tag_ARC_CPU_name: ");
15151       p = display_tag_value (-1, p, end);
15152       break;
15153 
15154     case Tag_ARC_ABI_rf16:
15155       READ_ULEB (val, p, end);
15156       printf ("  Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
15157       break;
15158 
15159     case Tag_ARC_ABI_osver:
15160       READ_ULEB (val, p, end);
15161       printf ("  Tag_ARC_ABI_osver: v%d\n", val);
15162       break;
15163 
15164     case Tag_ARC_ABI_pic:
15165     case Tag_ARC_ABI_sda:
15166       READ_ULEB (val, p, end);
15167       printf (tag == Tag_ARC_ABI_sda ? "  Tag_ARC_ABI_sda: "
15168 	      : "  Tag_ARC_ABI_pic: ");
15169       switch (val)
15170 	{
15171 	case 0:
15172 	  printf (_("Absent\n"));
15173 	  break;
15174 	case 1:
15175 	  printf ("MWDT\n");
15176 	  break;
15177 	case 2:
15178 	  printf ("GNU\n");
15179 	  break;
15180 	default:
15181 	  printf (_("Unknown\n"));
15182 	  break;
15183 	}
15184       break;
15185 
15186     case Tag_ARC_ABI_tls:
15187       READ_ULEB (val, p, end);
15188       printf ("  Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
15189       break;
15190 
15191     case Tag_ARC_ABI_enumsize:
15192       READ_ULEB (val, p, end);
15193       printf ("  Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
15194 	      _("smallest"));
15195       break;
15196 
15197     case Tag_ARC_ABI_exceptions:
15198       READ_ULEB (val, p, end);
15199       printf ("  Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
15200 	      : _("default"));
15201       break;
15202 
15203     case Tag_ARC_ABI_double_size:
15204       READ_ULEB (val, p, end);
15205       printf ("  Tag_ARC_ABI_double_size: %d\n", val);
15206       break;
15207 
15208     case Tag_ARC_ISA_config:
15209       printf ("  Tag_ARC_ISA_config: ");
15210       p = display_tag_value (-1, p, end);
15211       break;
15212 
15213     case Tag_ARC_ISA_apex:
15214       printf ("  Tag_ARC_ISA_apex: ");
15215       p = display_tag_value (-1, p, end);
15216       break;
15217 
15218     case Tag_ARC_ISA_mpy_option:
15219       READ_ULEB (val, p, end);
15220       printf ("  Tag_ARC_ISA_mpy_option: %d\n", val);
15221       break;
15222 
15223     case Tag_ARC_ATR_version:
15224       READ_ULEB (val, p, end);
15225       printf ("  Tag_ARC_ATR_version: %d\n", val);
15226       break;
15227 
15228     default:
15229       return display_tag_value (tag & 1, p, end);
15230     }
15231 
15232   return p;
15233 }
15234 
15235 /* ARM EABI attributes section.  */
15236 typedef struct
15237 {
15238   unsigned int tag;
15239   const char * name;
15240   /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup.  */
15241   unsigned int type;
15242   const char ** table;
15243 } arm_attr_public_tag;
15244 
15245 static const char * arm_attr_tag_CPU_arch[] =
15246   {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
15247    "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
15248    "v8-M.mainline", "", "", "", "v8.1-M.mainline"};
15249 static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
15250 static const char * arm_attr_tag_THUMB_ISA_use[] =
15251   {"No", "Thumb-1", "Thumb-2", "Yes"};
15252 static const char * arm_attr_tag_FP_arch[] =
15253   {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
15254    "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
15255 static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
15256 static const char * arm_attr_tag_Advanced_SIMD_arch[] =
15257   {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
15258    "NEON for ARMv8.1"};
15259 static const char * arm_attr_tag_PCS_config[] =
15260   {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
15261    "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
15262 static const char * arm_attr_tag_ABI_PCS_R9_use[] =
15263   {"V6", "SB", "TLS", "Unused"};
15264 static const char * arm_attr_tag_ABI_PCS_RW_data[] =
15265   {"Absolute", "PC-relative", "SB-relative", "None"};
15266 static const char * arm_attr_tag_ABI_PCS_RO_data[] =
15267   {"Absolute", "PC-relative", "None"};
15268 static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
15269   {"None", "direct", "GOT-indirect"};
15270 static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
15271   {"None", "??? 1", "2", "??? 3", "4"};
15272 static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
15273 static const char * arm_attr_tag_ABI_FP_denormal[] =
15274   {"Unused", "Needed", "Sign only"};
15275 static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
15276 static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
15277 static const char * arm_attr_tag_ABI_FP_number_model[] =
15278   {"Unused", "Finite", "RTABI", "IEEE 754"};
15279 static const char * arm_attr_tag_ABI_enum_size[] =
15280   {"Unused", "small", "int", "forced to int"};
15281 static const char * arm_attr_tag_ABI_HardFP_use[] =
15282   {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
15283 static const char * arm_attr_tag_ABI_VFP_args[] =
15284   {"AAPCS", "VFP registers", "custom", "compatible"};
15285 static const char * arm_attr_tag_ABI_WMMX_args[] =
15286   {"AAPCS", "WMMX registers", "custom"};
15287 static const char * arm_attr_tag_ABI_optimization_goals[] =
15288   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
15289     "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
15290 static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
15291   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
15292     "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
15293 static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
15294 static const char * arm_attr_tag_FP_HP_extension[] =
15295   {"Not Allowed", "Allowed"};
15296 static const char * arm_attr_tag_ABI_FP_16bit_format[] =
15297   {"None", "IEEE 754", "Alternative Format"};
15298 static const char * arm_attr_tag_DSP_extension[] =
15299   {"Follow architecture", "Allowed"};
15300 static const char * arm_attr_tag_MPextension_use[] =
15301   {"Not Allowed", "Allowed"};
15302 static const char * arm_attr_tag_DIV_use[] =
15303   {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
15304     "Allowed in v7-A with integer division extension"};
15305 static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
15306 static const char * arm_attr_tag_Virtualization_use[] =
15307   {"Not Allowed", "TrustZone", "Virtualization Extensions",
15308     "TrustZone and Virtualization Extensions"};
15309 static const char * arm_attr_tag_MPextension_use_legacy[] =
15310   {"Not Allowed", "Allowed"};
15311 
15312 static const char * arm_attr_tag_MVE_arch[] =
15313   {"No MVE", "MVE Integer only", "MVE Integer and FP"};
15314 
15315 #define LOOKUP(id, name) \
15316   {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
15317 static arm_attr_public_tag arm_attr_public_tags[] =
15318 {
15319   {4, "CPU_raw_name", 1, NULL},
15320   {5, "CPU_name", 1, NULL},
15321   LOOKUP(6, CPU_arch),
15322   {7, "CPU_arch_profile", 0, NULL},
15323   LOOKUP(8, ARM_ISA_use),
15324   LOOKUP(9, THUMB_ISA_use),
15325   LOOKUP(10, FP_arch),
15326   LOOKUP(11, WMMX_arch),
15327   LOOKUP(12, Advanced_SIMD_arch),
15328   LOOKUP(13, PCS_config),
15329   LOOKUP(14, ABI_PCS_R9_use),
15330   LOOKUP(15, ABI_PCS_RW_data),
15331   LOOKUP(16, ABI_PCS_RO_data),
15332   LOOKUP(17, ABI_PCS_GOT_use),
15333   LOOKUP(18, ABI_PCS_wchar_t),
15334   LOOKUP(19, ABI_FP_rounding),
15335   LOOKUP(20, ABI_FP_denormal),
15336   LOOKUP(21, ABI_FP_exceptions),
15337   LOOKUP(22, ABI_FP_user_exceptions),
15338   LOOKUP(23, ABI_FP_number_model),
15339   {24, "ABI_align_needed", 0, NULL},
15340   {25, "ABI_align_preserved", 0, NULL},
15341   LOOKUP(26, ABI_enum_size),
15342   LOOKUP(27, ABI_HardFP_use),
15343   LOOKUP(28, ABI_VFP_args),
15344   LOOKUP(29, ABI_WMMX_args),
15345   LOOKUP(30, ABI_optimization_goals),
15346   LOOKUP(31, ABI_FP_optimization_goals),
15347   {32, "compatibility", 0, NULL},
15348   LOOKUP(34, CPU_unaligned_access),
15349   LOOKUP(36, FP_HP_extension),
15350   LOOKUP(38, ABI_FP_16bit_format),
15351   LOOKUP(42, MPextension_use),
15352   LOOKUP(44, DIV_use),
15353   LOOKUP(46, DSP_extension),
15354   LOOKUP(48, MVE_arch),
15355   {64, "nodefaults", 0, NULL},
15356   {65, "also_compatible_with", 0, NULL},
15357   LOOKUP(66, T2EE_use),
15358   {67, "conformance", 1, NULL},
15359   LOOKUP(68, Virtualization_use),
15360   LOOKUP(70, MPextension_use_legacy)
15361 };
15362 #undef LOOKUP
15363 
15364 static unsigned char *
display_arm_attribute(unsigned char * p,const unsigned char * const end)15365 display_arm_attribute (unsigned char * p,
15366 		       const unsigned char * const end)
15367 {
15368   unsigned int tag;
15369   unsigned int val;
15370   arm_attr_public_tag * attr;
15371   unsigned i;
15372   unsigned int type;
15373 
15374   READ_ULEB (tag, p, end);
15375   attr = NULL;
15376   for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
15377     {
15378       if (arm_attr_public_tags[i].tag == tag)
15379 	{
15380 	  attr = &arm_attr_public_tags[i];
15381 	  break;
15382 	}
15383     }
15384 
15385   if (attr)
15386     {
15387       printf ("  Tag_%s: ", attr->name);
15388       switch (attr->type)
15389 	{
15390 	case 0:
15391 	  switch (tag)
15392 	    {
15393 	    case 7: /* Tag_CPU_arch_profile.  */
15394 	      READ_ULEB (val, p, end);
15395 	      switch (val)
15396 		{
15397 		case 0: printf (_("None\n")); break;
15398 		case 'A': printf (_("Application\n")); break;
15399 		case 'R': printf (_("Realtime\n")); break;
15400 		case 'M': printf (_("Microcontroller\n")); break;
15401 		case 'S': printf (_("Application or Realtime\n")); break;
15402 		default: printf ("??? (%d)\n", val); break;
15403 		}
15404 	      break;
15405 
15406 	    case 24: /* Tag_align_needed.  */
15407 	      READ_ULEB (val, p, end);
15408 	      switch (val)
15409 		{
15410 		case 0: printf (_("None\n")); break;
15411 		case 1: printf (_("8-byte\n")); break;
15412 		case 2: printf (_("4-byte\n")); break;
15413 		case 3: printf ("??? 3\n"); break;
15414 		default:
15415 		  if (val <= 12)
15416 		    printf (_("8-byte and up to %d-byte extended\n"),
15417 			    1 << val);
15418 		  else
15419 		    printf ("??? (%d)\n", val);
15420 		  break;
15421 		}
15422 	      break;
15423 
15424 	    case 25: /* Tag_align_preserved.  */
15425 	      READ_ULEB (val, p, end);
15426 	      switch (val)
15427 		{
15428 		case 0: printf (_("None\n")); break;
15429 		case 1: printf (_("8-byte, except leaf SP\n")); break;
15430 		case 2: printf (_("8-byte\n")); break;
15431 		case 3: printf ("??? 3\n"); break;
15432 		default:
15433 		  if (val <= 12)
15434 		    printf (_("8-byte and up to %d-byte extended\n"),
15435 			    1 << val);
15436 		  else
15437 		    printf ("??? (%d)\n", val);
15438 		  break;
15439 		}
15440 	      break;
15441 
15442 	    case 32: /* Tag_compatibility.  */
15443 	      {
15444 		READ_ULEB (val, p, end);
15445 		printf (_("flag = %d, vendor = "), val);
15446 		if (p < end - 1)
15447 		  {
15448 		    size_t maxlen = (end - p) - 1;
15449 
15450 		    print_symbol ((int) maxlen, (const char *) p);
15451 		    p += strnlen ((char *) p, maxlen) + 1;
15452 		  }
15453 		else
15454 		  {
15455 		    printf (_("<corrupt>"));
15456 		    p = (unsigned char *) end;
15457 		  }
15458 		putchar ('\n');
15459 	      }
15460 	      break;
15461 
15462 	    case 64: /* Tag_nodefaults.  */
15463 	      /* PR 17531: file: 001-505008-0.01.  */
15464 	      if (p < end)
15465 		p++;
15466 	      printf (_("True\n"));
15467 	      break;
15468 
15469 	    case 65: /* Tag_also_compatible_with.  */
15470 	      READ_ULEB (val, p, end);
15471 	      if (val == 6 /* Tag_CPU_arch.  */)
15472 		{
15473 		  READ_ULEB (val, p, end);
15474 		  if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
15475 		    printf ("??? (%d)\n", val);
15476 		  else
15477 		    printf ("%s\n", arm_attr_tag_CPU_arch[val]);
15478 		}
15479 	      else
15480 		printf ("???\n");
15481 	      while (p < end && *(p++) != '\0' /* NUL terminator.  */)
15482 		;
15483 	      break;
15484 
15485 	    default:
15486 	      printf (_("<unknown: %d>\n"), tag);
15487 	      break;
15488 	    }
15489 	  return p;
15490 
15491 	case 1:
15492 	  return display_tag_value (-1, p, end);
15493 	case 2:
15494 	  return display_tag_value (0, p, end);
15495 
15496 	default:
15497 	  assert (attr->type & 0x80);
15498 	  READ_ULEB (val, p, end);
15499 	  type = attr->type & 0x7f;
15500 	  if (val >= type)
15501 	    printf ("??? (%d)\n", val);
15502 	  else
15503 	    printf ("%s\n", attr->table[val]);
15504 	  return p;
15505 	}
15506     }
15507 
15508   return display_tag_value (tag, p, end);
15509 }
15510 
15511 static unsigned char *
display_gnu_attribute(unsigned char * p,unsigned char * (* display_proc_gnu_attribute)(unsigned char *,unsigned int,const unsigned char * const),const unsigned char * const end)15512 display_gnu_attribute (unsigned char * p,
15513 		       unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
15514 		       const unsigned char * const end)
15515 {
15516   unsigned int tag;
15517   unsigned int val;
15518 
15519   READ_ULEB (tag, p, end);
15520 
15521   /* Tag_compatibility is the only generic GNU attribute defined at
15522      present.  */
15523   if (tag == 32)
15524     {
15525       READ_ULEB (val, p, end);
15526 
15527       printf (_("flag = %d, vendor = "), val);
15528       if (p == end)
15529 	{
15530 	  printf (_("<corrupt>\n"));
15531 	  warn (_("corrupt vendor attribute\n"));
15532 	}
15533       else
15534 	{
15535 	  if (p < end - 1)
15536 	    {
15537 	      size_t maxlen = (end - p) - 1;
15538 
15539 	      print_symbol ((int) maxlen, (const char *) p);
15540 	      p += strnlen ((char *) p, maxlen) + 1;
15541 	    }
15542 	  else
15543 	    {
15544 	      printf (_("<corrupt>"));
15545 	      p = (unsigned char *) end;
15546 	    }
15547 	  putchar ('\n');
15548 	}
15549       return p;
15550     }
15551 
15552   if ((tag & 2) == 0 && display_proc_gnu_attribute)
15553     return display_proc_gnu_attribute (p, tag, end);
15554 
15555   return display_tag_value (tag, p, end);
15556 }
15557 
15558 static unsigned char *
display_m68k_gnu_attribute(unsigned char * p,unsigned int tag,const unsigned char * const end)15559 display_m68k_gnu_attribute (unsigned char * p,
15560 			    unsigned int tag,
15561 			    const unsigned char * const end)
15562 {
15563   unsigned int val;
15564 
15565   if (tag == Tag_GNU_M68K_ABI_FP)
15566     {
15567       printf ("  Tag_GNU_M68K_ABI_FP: ");
15568       if (p == end)
15569 	{
15570 	  printf (_("<corrupt>\n"));
15571 	  return p;
15572 	}
15573       READ_ULEB (val, p, end);
15574 
15575       if (val > 3)
15576 	printf ("(%#x), ", val);
15577 
15578       switch (val & 3)
15579 	{
15580 	case 0:
15581 	  printf (_("unspecified hard/soft float\n"));
15582 	  break;
15583 	case 1:
15584 	  printf (_("hard float\n"));
15585 	  break;
15586 	case 2:
15587 	  printf (_("soft float\n"));
15588 	  break;
15589 	}
15590       return p;
15591     }
15592 
15593   return display_tag_value (tag & 1, p, end);
15594 }
15595 
15596 static unsigned char *
display_power_gnu_attribute(unsigned char * p,unsigned int tag,const unsigned char * const end)15597 display_power_gnu_attribute (unsigned char * p,
15598 			     unsigned int tag,
15599 			     const unsigned char * const end)
15600 {
15601   unsigned int val;
15602 
15603   if (tag == Tag_GNU_Power_ABI_FP)
15604     {
15605       printf ("  Tag_GNU_Power_ABI_FP: ");
15606       if (p == end)
15607 	{
15608 	  printf (_("<corrupt>\n"));
15609 	  return p;
15610 	}
15611       READ_ULEB (val, p, end);
15612 
15613       if (val > 15)
15614 	printf ("(%#x), ", val);
15615 
15616       switch (val & 3)
15617 	{
15618 	case 0:
15619 	  printf (_("unspecified hard/soft float, "));
15620 	  break;
15621 	case 1:
15622 	  printf (_("hard float, "));
15623 	  break;
15624 	case 2:
15625 	  printf (_("soft float, "));
15626 	  break;
15627 	case 3:
15628 	  printf (_("single-precision hard float, "));
15629 	  break;
15630 	}
15631 
15632       switch (val & 0xC)
15633 	{
15634 	case 0:
15635 	  printf (_("unspecified long double\n"));
15636 	  break;
15637 	case 4:
15638 	  printf (_("128-bit IBM long double\n"));
15639 	  break;
15640 	case 8:
15641 	  printf (_("64-bit long double\n"));
15642 	  break;
15643 	case 12:
15644 	  printf (_("128-bit IEEE long double\n"));
15645 	  break;
15646 	}
15647       return p;
15648     }
15649 
15650   if (tag == Tag_GNU_Power_ABI_Vector)
15651     {
15652       printf ("  Tag_GNU_Power_ABI_Vector: ");
15653       if (p == end)
15654 	{
15655 	  printf (_("<corrupt>\n"));
15656 	  return p;
15657 	}
15658       READ_ULEB (val, p, end);
15659 
15660       if (val > 3)
15661 	printf ("(%#x), ", val);
15662 
15663       switch (val & 3)
15664 	{
15665 	case 0:
15666 	  printf (_("unspecified\n"));
15667 	  break;
15668 	case 1:
15669 	  printf (_("generic\n"));
15670 	  break;
15671 	case 2:
15672 	  printf ("AltiVec\n");
15673 	  break;
15674 	case 3:
15675 	  printf ("SPE\n");
15676 	  break;
15677 	}
15678       return p;
15679     }
15680 
15681   if (tag == Tag_GNU_Power_ABI_Struct_Return)
15682     {
15683       printf ("  Tag_GNU_Power_ABI_Struct_Return: ");
15684       if (p == end)
15685 	{
15686 	  printf (_("<corrupt>\n"));
15687 	  return p;
15688 	}
15689       READ_ULEB (val, p, end);
15690 
15691       if (val > 2)
15692 	printf ("(%#x), ", val);
15693 
15694       switch (val & 3)
15695 	{
15696 	case 0:
15697 	  printf (_("unspecified\n"));
15698 	  break;
15699 	case 1:
15700 	  printf ("r3/r4\n");
15701 	  break;
15702 	case 2:
15703 	  printf (_("memory\n"));
15704 	  break;
15705 	case 3:
15706 	  printf ("???\n");
15707 	  break;
15708 	}
15709       return p;
15710     }
15711 
15712   return display_tag_value (tag & 1, p, end);
15713 }
15714 
15715 static unsigned char *
display_s390_gnu_attribute(unsigned char * p,unsigned int tag,const unsigned char * const end)15716 display_s390_gnu_attribute (unsigned char * p,
15717 			    unsigned int tag,
15718 			    const unsigned char * const end)
15719 {
15720   unsigned int val;
15721 
15722   if (tag == Tag_GNU_S390_ABI_Vector)
15723     {
15724       printf ("  Tag_GNU_S390_ABI_Vector: ");
15725       READ_ULEB (val, p, end);
15726 
15727       switch (val)
15728 	{
15729 	case 0:
15730 	  printf (_("any\n"));
15731 	  break;
15732 	case 1:
15733 	  printf (_("software\n"));
15734 	  break;
15735 	case 2:
15736 	  printf (_("hardware\n"));
15737 	  break;
15738 	default:
15739 	  printf ("??? (%d)\n", val);
15740 	  break;
15741 	}
15742       return p;
15743    }
15744 
15745   return display_tag_value (tag & 1, p, end);
15746 }
15747 
15748 static void
display_sparc_hwcaps(unsigned int mask)15749 display_sparc_hwcaps (unsigned int mask)
15750 {
15751   if (mask)
15752     {
15753       bfd_boolean first = TRUE;
15754 
15755       if (mask & ELF_SPARC_HWCAP_MUL32)
15756 	fputs ("mul32", stdout), first = FALSE;
15757       if (mask & ELF_SPARC_HWCAP_DIV32)
15758 	printf ("%sdiv32", first ? "" : "|"), first = FALSE;
15759       if (mask & ELF_SPARC_HWCAP_FSMULD)
15760 	printf ("%sfsmuld", first ? "" : "|"), first = FALSE;
15761       if (mask & ELF_SPARC_HWCAP_V8PLUS)
15762 	printf ("%sv8plus", first ? "" : "|"), first = FALSE;
15763       if (mask & ELF_SPARC_HWCAP_POPC)
15764 	printf ("%spopc", first ? "" : "|"), first = FALSE;
15765       if (mask & ELF_SPARC_HWCAP_VIS)
15766 	printf ("%svis", first ? "" : "|"), first = FALSE;
15767       if (mask & ELF_SPARC_HWCAP_VIS2)
15768 	printf ("%svis2", first ? "" : "|"), first = FALSE;
15769       if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
15770 	printf ("%sASIBlkInit", first ? "" : "|"), first = FALSE;
15771       if (mask & ELF_SPARC_HWCAP_FMAF)
15772 	printf ("%sfmaf", first ? "" : "|"), first = FALSE;
15773       if (mask & ELF_SPARC_HWCAP_VIS3)
15774 	printf ("%svis3", first ? "" : "|"), first = FALSE;
15775       if (mask & ELF_SPARC_HWCAP_HPC)
15776 	printf ("%shpc", first ? "" : "|"), first = FALSE;
15777       if (mask & ELF_SPARC_HWCAP_RANDOM)
15778 	printf ("%srandom", first ? "" : "|"), first = FALSE;
15779       if (mask & ELF_SPARC_HWCAP_TRANS)
15780 	printf ("%strans", first ? "" : "|"), first = FALSE;
15781       if (mask & ELF_SPARC_HWCAP_FJFMAU)
15782 	printf ("%sfjfmau", first ? "" : "|"), first = FALSE;
15783       if (mask & ELF_SPARC_HWCAP_IMA)
15784 	printf ("%sima", first ? "" : "|"), first = FALSE;
15785       if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
15786 	printf ("%scspare", first ? "" : "|"), first = FALSE;
15787     }
15788   else
15789     fputc ('0', stdout);
15790   fputc ('\n', stdout);
15791 }
15792 
15793 static void
display_sparc_hwcaps2(unsigned int mask)15794 display_sparc_hwcaps2 (unsigned int mask)
15795 {
15796   if (mask)
15797     {
15798       bfd_boolean first = TRUE;
15799 
15800       if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
15801 	fputs ("fjathplus", stdout), first = FALSE;
15802       if (mask & ELF_SPARC_HWCAP2_VIS3B)
15803 	printf ("%svis3b", first ? "" : "|"), first = FALSE;
15804       if (mask & ELF_SPARC_HWCAP2_ADP)
15805 	printf ("%sadp", first ? "" : "|"), first = FALSE;
15806       if (mask & ELF_SPARC_HWCAP2_SPARC5)
15807 	printf ("%ssparc5", first ? "" : "|"), first = FALSE;
15808       if (mask & ELF_SPARC_HWCAP2_MWAIT)
15809 	printf ("%smwait", first ? "" : "|"), first = FALSE;
15810       if (mask & ELF_SPARC_HWCAP2_XMPMUL)
15811 	printf ("%sxmpmul", first ? "" : "|"), first = FALSE;
15812       if (mask & ELF_SPARC_HWCAP2_XMONT)
15813 	printf ("%sxmont2", first ? "" : "|"), first = FALSE;
15814       if (mask & ELF_SPARC_HWCAP2_NSEC)
15815 	printf ("%snsec", first ? "" : "|"), first = FALSE;
15816       if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
15817 	printf ("%sfjathhpc", first ? "" : "|"), first = FALSE;
15818       if (mask & ELF_SPARC_HWCAP2_FJDES)
15819 	printf ("%sfjdes", first ? "" : "|"), first = FALSE;
15820       if (mask & ELF_SPARC_HWCAP2_FJAES)
15821 	printf ("%sfjaes", first ? "" : "|"), first = FALSE;
15822     }
15823   else
15824     fputc ('0', stdout);
15825   fputc ('\n', stdout);
15826 }
15827 
15828 static unsigned char *
display_sparc_gnu_attribute(unsigned char * p,unsigned int tag,const unsigned char * const end)15829 display_sparc_gnu_attribute (unsigned char * p,
15830 			     unsigned int tag,
15831 			     const unsigned char * const end)
15832 {
15833   unsigned int val;
15834 
15835   if (tag == Tag_GNU_Sparc_HWCAPS)
15836     {
15837       READ_ULEB (val, p, end);
15838       printf ("  Tag_GNU_Sparc_HWCAPS: ");
15839       display_sparc_hwcaps (val);
15840       return p;
15841     }
15842   if (tag == Tag_GNU_Sparc_HWCAPS2)
15843     {
15844       READ_ULEB (val, p, end);
15845       printf ("  Tag_GNU_Sparc_HWCAPS2: ");
15846       display_sparc_hwcaps2 (val);
15847       return p;
15848     }
15849 
15850   return display_tag_value (tag, p, end);
15851 }
15852 
15853 static void
print_mips_fp_abi_value(unsigned int val)15854 print_mips_fp_abi_value (unsigned int val)
15855 {
15856   switch (val)
15857     {
15858     case Val_GNU_MIPS_ABI_FP_ANY:
15859       printf (_("Hard or soft float\n"));
15860       break;
15861     case Val_GNU_MIPS_ABI_FP_DOUBLE:
15862       printf (_("Hard float (double precision)\n"));
15863       break;
15864     case Val_GNU_MIPS_ABI_FP_SINGLE:
15865       printf (_("Hard float (single precision)\n"));
15866       break;
15867     case Val_GNU_MIPS_ABI_FP_SOFT:
15868       printf (_("Soft float\n"));
15869       break;
15870     case Val_GNU_MIPS_ABI_FP_OLD_64:
15871       printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
15872       break;
15873     case Val_GNU_MIPS_ABI_FP_XX:
15874       printf (_("Hard float (32-bit CPU, Any FPU)\n"));
15875       break;
15876     case Val_GNU_MIPS_ABI_FP_64:
15877       printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
15878       break;
15879     case Val_GNU_MIPS_ABI_FP_64A:
15880       printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
15881       break;
15882     case Val_GNU_MIPS_ABI_FP_NAN2008:
15883       printf (_("NaN 2008 compatibility\n"));
15884       break;
15885     default:
15886       printf ("??? (%d)\n", val);
15887       break;
15888     }
15889 }
15890 
15891 static unsigned char *
display_mips_gnu_attribute(unsigned char * p,unsigned int tag,const unsigned char * const end)15892 display_mips_gnu_attribute (unsigned char * p,
15893 			    unsigned int tag,
15894 			    const unsigned char * const end)
15895 {
15896   if (tag == Tag_GNU_MIPS_ABI_FP)
15897     {
15898       unsigned int val;
15899 
15900       printf ("  Tag_GNU_MIPS_ABI_FP: ");
15901       READ_ULEB (val, p, end);
15902       print_mips_fp_abi_value (val);
15903       return p;
15904    }
15905 
15906   if (tag == Tag_GNU_MIPS_ABI_MSA)
15907     {
15908       unsigned int val;
15909 
15910       printf ("  Tag_GNU_MIPS_ABI_MSA: ");
15911       READ_ULEB (val, p, end);
15912 
15913       switch (val)
15914 	{
15915 	case Val_GNU_MIPS_ABI_MSA_ANY:
15916 	  printf (_("Any MSA or not\n"));
15917 	  break;
15918 	case Val_GNU_MIPS_ABI_MSA_128:
15919 	  printf (_("128-bit MSA\n"));
15920 	  break;
15921 	default:
15922 	  printf ("??? (%d)\n", val);
15923 	  break;
15924 	}
15925       return p;
15926     }
15927 
15928   return display_tag_value (tag & 1, p, end);
15929 }
15930 
15931 static unsigned char *
display_tic6x_attribute(unsigned char * p,const unsigned char * const end)15932 display_tic6x_attribute (unsigned char * p,
15933 			 const unsigned char * const end)
15934 {
15935   unsigned int tag;
15936   unsigned int val;
15937 
15938   READ_ULEB (tag, p, end);
15939 
15940   switch (tag)
15941     {
15942     case Tag_ISA:
15943       printf ("  Tag_ISA: ");
15944       READ_ULEB (val, p, end);
15945 
15946       switch (val)
15947 	{
15948 	case C6XABI_Tag_ISA_none:
15949 	  printf (_("None\n"));
15950 	  break;
15951 	case C6XABI_Tag_ISA_C62X:
15952 	  printf ("C62x\n");
15953 	  break;
15954 	case C6XABI_Tag_ISA_C67X:
15955 	  printf ("C67x\n");
15956 	  break;
15957 	case C6XABI_Tag_ISA_C67XP:
15958 	  printf ("C67x+\n");
15959 	  break;
15960 	case C6XABI_Tag_ISA_C64X:
15961 	  printf ("C64x\n");
15962 	  break;
15963 	case C6XABI_Tag_ISA_C64XP:
15964 	  printf ("C64x+\n");
15965 	  break;
15966 	case C6XABI_Tag_ISA_C674X:
15967 	  printf ("C674x\n");
15968 	  break;
15969 	default:
15970 	  printf ("??? (%d)\n", val);
15971 	  break;
15972 	}
15973       return p;
15974 
15975     case Tag_ABI_wchar_t:
15976       printf ("  Tag_ABI_wchar_t: ");
15977       READ_ULEB (val, p, end);
15978       switch (val)
15979 	{
15980 	case 0:
15981 	  printf (_("Not used\n"));
15982 	  break;
15983 	case 1:
15984 	  printf (_("2 bytes\n"));
15985 	  break;
15986 	case 2:
15987 	  printf (_("4 bytes\n"));
15988 	  break;
15989 	default:
15990 	  printf ("??? (%d)\n", val);
15991 	  break;
15992 	}
15993       return p;
15994 
15995     case Tag_ABI_stack_align_needed:
15996       printf ("  Tag_ABI_stack_align_needed: ");
15997       READ_ULEB (val, p, end);
15998       switch (val)
15999 	{
16000 	case 0:
16001 	  printf (_("8-byte\n"));
16002 	  break;
16003 	case 1:
16004 	  printf (_("16-byte\n"));
16005 	  break;
16006 	default:
16007 	  printf ("??? (%d)\n", val);
16008 	  break;
16009 	}
16010       return p;
16011 
16012     case Tag_ABI_stack_align_preserved:
16013       READ_ULEB (val, p, end);
16014       printf ("  Tag_ABI_stack_align_preserved: ");
16015       switch (val)
16016 	{
16017 	case 0:
16018 	  printf (_("8-byte\n"));
16019 	  break;
16020 	case 1:
16021 	  printf (_("16-byte\n"));
16022 	  break;
16023 	default:
16024 	  printf ("??? (%d)\n", val);
16025 	  break;
16026 	}
16027       return p;
16028 
16029     case Tag_ABI_DSBT:
16030       READ_ULEB (val, p, end);
16031       printf ("  Tag_ABI_DSBT: ");
16032       switch (val)
16033 	{
16034 	case 0:
16035 	  printf (_("DSBT addressing not used\n"));
16036 	  break;
16037 	case 1:
16038 	  printf (_("DSBT addressing used\n"));
16039 	  break;
16040 	default:
16041 	  printf ("??? (%d)\n", val);
16042 	  break;
16043 	}
16044       return p;
16045 
16046     case Tag_ABI_PID:
16047       READ_ULEB (val, p, end);
16048       printf ("  Tag_ABI_PID: ");
16049       switch (val)
16050 	{
16051 	case 0:
16052 	  printf (_("Data addressing position-dependent\n"));
16053 	  break;
16054 	case 1:
16055 	  printf (_("Data addressing position-independent, GOT near DP\n"));
16056 	  break;
16057 	case 2:
16058 	  printf (_("Data addressing position-independent, GOT far from DP\n"));
16059 	  break;
16060 	default:
16061 	  printf ("??? (%d)\n", val);
16062 	  break;
16063 	}
16064       return p;
16065 
16066     case Tag_ABI_PIC:
16067       READ_ULEB (val, p, end);
16068       printf ("  Tag_ABI_PIC: ");
16069       switch (val)
16070 	{
16071 	case 0:
16072 	  printf (_("Code addressing position-dependent\n"));
16073 	  break;
16074 	case 1:
16075 	  printf (_("Code addressing position-independent\n"));
16076 	  break;
16077 	default:
16078 	  printf ("??? (%d)\n", val);
16079 	  break;
16080 	}
16081       return p;
16082 
16083     case Tag_ABI_array_object_alignment:
16084       READ_ULEB (val, p, end);
16085       printf ("  Tag_ABI_array_object_alignment: ");
16086       switch (val)
16087 	{
16088 	case 0:
16089 	  printf (_("8-byte\n"));
16090 	  break;
16091 	case 1:
16092 	  printf (_("4-byte\n"));
16093 	  break;
16094 	case 2:
16095 	  printf (_("16-byte\n"));
16096 	  break;
16097 	default:
16098 	  printf ("??? (%d)\n", val);
16099 	  break;
16100 	}
16101       return p;
16102 
16103     case Tag_ABI_array_object_align_expected:
16104       READ_ULEB (val, p, end);
16105       printf ("  Tag_ABI_array_object_align_expected: ");
16106       switch (val)
16107 	{
16108 	case 0:
16109 	  printf (_("8-byte\n"));
16110 	  break;
16111 	case 1:
16112 	  printf (_("4-byte\n"));
16113 	  break;
16114 	case 2:
16115 	  printf (_("16-byte\n"));
16116 	  break;
16117 	default:
16118 	  printf ("??? (%d)\n", val);
16119 	  break;
16120 	}
16121       return p;
16122 
16123     case Tag_ABI_compatibility:
16124       {
16125 	READ_ULEB (val, p, end);
16126 	printf ("  Tag_ABI_compatibility: ");
16127 	printf (_("flag = %d, vendor = "), val);
16128 	if (p < end - 1)
16129 	  {
16130 	    size_t maxlen = (end - p) - 1;
16131 
16132 	    print_symbol ((int) maxlen, (const char *) p);
16133 	    p += strnlen ((char *) p, maxlen) + 1;
16134 	  }
16135 	else
16136 	  {
16137 	    printf (_("<corrupt>"));
16138 	    p = (unsigned char *) end;
16139 	  }
16140 	putchar ('\n');
16141 	return p;
16142       }
16143 
16144     case Tag_ABI_conformance:
16145       {
16146 	printf ("  Tag_ABI_conformance: \"");
16147 	if (p < end - 1)
16148 	  {
16149 	    size_t maxlen = (end - p) - 1;
16150 
16151 	    print_symbol ((int) maxlen, (const char *) p);
16152 	    p += strnlen ((char *) p, maxlen) + 1;
16153 	  }
16154 	else
16155 	  {
16156 	    printf (_("<corrupt>"));
16157 	    p = (unsigned char *) end;
16158 	  }
16159 	printf ("\"\n");
16160 	return p;
16161       }
16162     }
16163 
16164   return display_tag_value (tag, p, end);
16165 }
16166 
16167 static void
display_raw_attribute(unsigned char * p,unsigned char const * const end)16168 display_raw_attribute (unsigned char * p, unsigned char const * const end)
16169 {
16170   unsigned long addr = 0;
16171   size_t bytes = end - p;
16172 
16173   assert (end >= p);
16174   while (bytes)
16175     {
16176       int j;
16177       int k;
16178       int lbytes = (bytes > 16 ? 16 : bytes);
16179 
16180       printf ("  0x%8.8lx ", addr);
16181 
16182       for (j = 0; j < 16; j++)
16183 	{
16184 	  if (j < lbytes)
16185 	    printf ("%2.2x", p[j]);
16186 	  else
16187 	    printf ("  ");
16188 
16189 	  if ((j & 3) == 3)
16190 	    printf (" ");
16191 	}
16192 
16193       for (j = 0; j < lbytes; j++)
16194 	{
16195 	  k = p[j];
16196 	  if (k >= ' ' && k < 0x7f)
16197 	    printf ("%c", k);
16198 	  else
16199 	    printf (".");
16200 	}
16201 
16202       putchar ('\n');
16203 
16204       p  += lbytes;
16205       bytes -= lbytes;
16206       addr += lbytes;
16207     }
16208 
16209   putchar ('\n');
16210 }
16211 
16212 static unsigned char *
display_msp430_attribute(unsigned char * p,const unsigned char * const end)16213 display_msp430_attribute (unsigned char * p,
16214 			   const unsigned char * const end)
16215 {
16216   unsigned int val;
16217   unsigned int tag;
16218 
16219   READ_ULEB (tag, p, end);
16220 
16221   switch (tag)
16222     {
16223     case OFBA_MSPABI_Tag_ISA:
16224       printf ("  Tag_ISA: ");
16225       READ_ULEB (val, p, end);
16226       switch (val)
16227 	{
16228 	case 0: printf (_("None\n")); break;
16229 	case 1: printf (_("MSP430\n")); break;
16230 	case 2: printf (_("MSP430X\n")); break;
16231 	default: printf ("??? (%d)\n", val); break;
16232 	}
16233       break;
16234 
16235     case OFBA_MSPABI_Tag_Code_Model:
16236       printf ("  Tag_Code_Model: ");
16237       READ_ULEB (val, p, end);
16238       switch (val)
16239 	{
16240 	case 0: printf (_("None\n")); break;
16241 	case 1: printf (_("Small\n")); break;
16242 	case 2: printf (_("Large\n")); break;
16243 	default: printf ("??? (%d)\n", val); break;
16244 	}
16245       break;
16246 
16247     case OFBA_MSPABI_Tag_Data_Model:
16248       printf ("  Tag_Data_Model: ");
16249       READ_ULEB (val, p, end);
16250       switch (val)
16251 	{
16252 	case 0: printf (_("None\n")); break;
16253 	case 1: printf (_("Small\n")); break;
16254 	case 2: printf (_("Large\n")); break;
16255 	case 3: printf (_("Restricted Large\n")); break;
16256 	default: printf ("??? (%d)\n", val); break;
16257 	}
16258       break;
16259 
16260     default:
16261       printf (_("  <unknown tag %d>: "), tag);
16262 
16263       if (tag & 1)
16264 	{
16265 	  putchar ('"');
16266 	  if (p < end - 1)
16267 	    {
16268 	      size_t maxlen = (end - p) - 1;
16269 
16270 	      print_symbol ((int) maxlen, (const char *) p);
16271 	      p += strnlen ((char *) p, maxlen) + 1;
16272 	    }
16273 	  else
16274 	    {
16275 	      printf (_("<corrupt>"));
16276 	      p = (unsigned char *) end;
16277 	    }
16278 	  printf ("\"\n");
16279 	}
16280       else
16281 	{
16282 	  READ_ULEB (val, p, end);
16283 	  printf ("%d (0x%x)\n", val, val);
16284 	}
16285       break;
16286    }
16287 
16288   assert (p <= end);
16289   return p;
16290 }
16291 
16292 static unsigned char *
display_msp430_gnu_attribute(unsigned char * p,unsigned int tag,const unsigned char * const end)16293 display_msp430_gnu_attribute (unsigned char * p,
16294 			      unsigned int tag,
16295 			      const unsigned char * const end)
16296 {
16297   if (tag == Tag_GNU_MSP430_Data_Region)
16298     {
16299       unsigned int val;
16300 
16301       printf ("  Tag_GNU_MSP430_Data_Region: ");
16302       READ_ULEB (val, p, end);
16303 
16304       switch (val)
16305 	{
16306 	case Val_GNU_MSP430_Data_Region_Any:
16307 	  printf (_("Any Region\n"));
16308 	  break;
16309 	case Val_GNU_MSP430_Data_Region_Lower:
16310 	  printf (_("Lower Region Only\n"));
16311 	  break;
16312 	default:
16313 	  printf ("??? (%u)\n", val);
16314 	}
16315       return p;
16316     }
16317   return display_tag_value (tag & 1, p, end);
16318 }
16319 
16320 struct riscv_attr_tag_t {
16321   const char *name;
16322   unsigned int tag;
16323 };
16324 
16325 static struct riscv_attr_tag_t riscv_attr_tag[] =
16326 {
16327 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
16328   T(arch),
16329   T(priv_spec),
16330   T(priv_spec_minor),
16331   T(priv_spec_revision),
16332   T(unaligned_access),
16333   T(stack_align),
16334 #undef T
16335 };
16336 
16337 static unsigned char *
display_riscv_attribute(unsigned char * p,const unsigned char * const end)16338 display_riscv_attribute (unsigned char *p,
16339 			 const unsigned char * const end)
16340 {
16341   unsigned int val;
16342   unsigned int tag;
16343   struct riscv_attr_tag_t *attr = NULL;
16344   unsigned i;
16345 
16346   READ_ULEB (tag, p, end);
16347 
16348   /* Find the name of attribute. */
16349   for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
16350     {
16351       if (riscv_attr_tag[i].tag == tag)
16352 	{
16353 	  attr = &riscv_attr_tag[i];
16354 	  break;
16355 	}
16356     }
16357 
16358   if (attr)
16359     printf ("  %s: ", attr->name);
16360   else
16361     return display_tag_value (tag, p, end);
16362 
16363   switch (tag)
16364     {
16365     case Tag_RISCV_priv_spec:
16366     case Tag_RISCV_priv_spec_minor:
16367     case Tag_RISCV_priv_spec_revision:
16368       READ_ULEB (val, p, end);
16369       printf (_("%u\n"), val);
16370       break;
16371     case Tag_RISCV_unaligned_access:
16372       READ_ULEB (val, p, end);
16373       switch (val)
16374 	{
16375 	case 0:
16376 	  printf (_("No unaligned access\n"));
16377 	  break;
16378 	case 1:
16379 	  printf (_("Unaligned access\n"));
16380 	  break;
16381 	}
16382       break;
16383     case Tag_RISCV_stack_align:
16384       READ_ULEB (val, p, end);
16385       printf (_("%u-bytes\n"), val);
16386       break;
16387     case Tag_RISCV_arch:
16388       p = display_tag_value (-1, p, end);
16389       break;
16390     default:
16391       return display_tag_value (tag, p, end);
16392     }
16393 
16394   return p;
16395 }
16396 
16397 static unsigned char *
display_csky_attribute(unsigned char * p,const unsigned char * const end)16398 display_csky_attribute (unsigned char * p,
16399 			const unsigned char * const end)
16400 {
16401   unsigned int tag;
16402   unsigned int val;
16403   READ_ULEB (tag, p, end);
16404 
16405   if (tag >= Tag_CSKY_MAX)
16406     {
16407       return display_tag_value (-1, p, end);
16408     }
16409 
16410   switch (tag)
16411     {
16412     case Tag_CSKY_ARCH_NAME:
16413       printf ("  Tag_CSKY_ARCH_NAME:\t\t");
16414       return display_tag_value (-1, p, end);
16415     case Tag_CSKY_CPU_NAME:
16416       printf ("  Tag_CSKY_CPU_NAME:\t\t");
16417       return display_tag_value (-1, p, end);
16418 
16419     case Tag_CSKY_ISA_FLAGS:
16420       printf ("  Tag_CSKY_ISA_FLAGS:\t\t");
16421       return display_tag_value (0, p, end);
16422     case Tag_CSKY_ISA_EXT_FLAGS:
16423       printf ("  Tag_CSKY_ISA_EXT_FLAGS:\t");
16424       return display_tag_value (0, p, end);
16425 
16426     case Tag_CSKY_DSP_VERSION:
16427       printf ("  Tag_CSKY_DSP_VERSION:\t\t");
16428       READ_ULEB (val, p, end);
16429       if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
16430 	printf ("DSP Extension\n");
16431       else if (val == VAL_CSKY_DSP_VERSION_2)
16432 	printf ("DSP 2.0\n");
16433       break;
16434 
16435     case Tag_CSKY_VDSP_VERSION:
16436       printf ("  Tag_CSKY_VDSP_VERSION:\t");
16437       READ_ULEB (val, p, end);
16438       printf ("VDSP Version %d\n", val);
16439       break;
16440 
16441     case Tag_CSKY_FPU_VERSION:
16442       printf ("  Tag_CSKY_FPU_VERSION:\t\t");
16443       READ_ULEB (val, p, end);
16444       if (val == VAL_CSKY_FPU_VERSION_1)
16445 	printf ("ABIV1 FPU Version 1\n");
16446       else if (val == VAL_CSKY_FPU_VERSION_2)
16447 	printf ("FPU Version 2\n");
16448       break;
16449 
16450     case Tag_CSKY_FPU_ABI:
16451       printf ("  Tag_CSKY_FPU_ABI:\t\t");
16452       READ_ULEB (val, p, end);
16453       if (val == VAL_CSKY_FPU_ABI_HARD)
16454 	printf ("Hard\n");
16455       else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
16456 	printf ("SoftFP\n");
16457       else if (val == VAL_CSKY_FPU_ABI_SOFT)
16458 	printf ("Soft\n");
16459       break;
16460     case Tag_CSKY_FPU_ROUNDING:
16461       READ_ULEB (val, p, end);
16462       if (val == 1) {
16463 	printf ("  Tag_CSKY_FPU_ROUNDING:\t");
16464 	printf ("Needed\n");
16465       }
16466       break;
16467     case Tag_CSKY_FPU_DENORMAL:
16468       READ_ULEB (val, p, end);
16469       if (val == 1) {
16470 	printf ("  Tag_CSKY_FPU_DENORMAL:\t");
16471 	printf ("Needed\n");
16472       }
16473       break;
16474     case Tag_CSKY_FPU_Exception:
16475       READ_ULEB (val, p, end);
16476       if (val == 1) {
16477 	printf ("  Tag_CSKY_FPU_Exception:\t");
16478 	printf ("Needed\n");
16479       }
16480       break;
16481     case Tag_CSKY_FPU_NUMBER_MODULE:
16482       printf ("  Tag_CSKY_FPU_NUMBER_MODULE:\t");
16483       return display_tag_value (-1, p, end);
16484     case Tag_CSKY_FPU_HARDFP:
16485       printf ("  Tag_CSKY_FPU_HARDFP:\t\t");
16486       READ_ULEB (val, p, end);
16487       if (val & VAL_CSKY_FPU_HARDFP_HALF)
16488 	printf (" Half");
16489       if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
16490 	printf (" Single");
16491       if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
16492 	printf (" Double");
16493       printf ("\n");
16494       break;
16495     default:
16496       return display_tag_value (tag, p, end);
16497      }
16498   return p;
16499 }
16500 
16501 static bfd_boolean
process_attributes(Filedata * filedata,const char * public_name,unsigned int proc_type,unsigned char * (* display_pub_attribute)(unsigned char *,const unsigned char * const),unsigned char * (* display_proc_gnu_attribute)(unsigned char *,unsigned int,const unsigned char * const))16502 process_attributes (Filedata * filedata,
16503 		    const char * public_name,
16504 		    unsigned int proc_type,
16505 		    unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
16506 		    unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
16507 {
16508   Elf_Internal_Shdr * sect;
16509   unsigned i;
16510   bfd_boolean res = TRUE;
16511 
16512   /* Find the section header so that we get the size.  */
16513   for (i = 0, sect = filedata->section_headers;
16514        i < filedata->file_header.e_shnum;
16515        i++, sect++)
16516     {
16517       unsigned char * contents;
16518       unsigned char * p;
16519 
16520       if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
16521 	continue;
16522 
16523       contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
16524                                              sect->sh_size, _("attributes"));
16525       if (contents == NULL)
16526 	{
16527 	  res = FALSE;
16528 	  continue;
16529 	}
16530 
16531       p = contents;
16532       /* The first character is the version of the attributes.
16533 	 Currently only version 1, (aka 'A') is recognised here.  */
16534       if (*p != 'A')
16535 	{
16536 	  printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
16537 	  res = FALSE;
16538 	}
16539       else
16540 	{
16541 	  bfd_vma section_len;
16542 
16543 	  section_len = sect->sh_size - 1;
16544 	  p++;
16545 
16546 	  while (section_len > 0)
16547 	    {
16548 	      bfd_vma attr_len;
16549 	      unsigned int namelen;
16550 	      bfd_boolean public_section;
16551 	      bfd_boolean gnu_section;
16552 
16553 	      if (section_len <= 4)
16554 		{
16555 		  error (_("Tag section ends prematurely\n"));
16556 		  res = FALSE;
16557 		  break;
16558 		}
16559 	      attr_len = byte_get (p, 4);
16560 	      p += 4;
16561 
16562 	      if (attr_len > section_len)
16563 		{
16564 		  error (_("Bad attribute length (%u > %u)\n"),
16565 			  (unsigned) attr_len, (unsigned) section_len);
16566 		  attr_len = section_len;
16567 		  res = FALSE;
16568 		}
16569 	      /* PR 17531: file: 001-101425-0.004  */
16570 	      else if (attr_len < 5)
16571 		{
16572 		  error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
16573 		  res = FALSE;
16574 		  break;
16575 		}
16576 
16577 	      section_len -= attr_len;
16578 	      attr_len -= 4;
16579 
16580 	      namelen = strnlen ((char *) p, attr_len) + 1;
16581 	      if (namelen == 0 || namelen >= attr_len)
16582 		{
16583 		  error (_("Corrupt attribute section name\n"));
16584 		  res = FALSE;
16585 		  break;
16586 		}
16587 
16588 	      printf (_("Attribute Section: "));
16589 	      print_symbol (INT_MAX, (const char *) p);
16590 	      putchar ('\n');
16591 
16592 	      if (public_name && streq ((char *) p, public_name))
16593 		public_section = TRUE;
16594 	      else
16595 		public_section = FALSE;
16596 
16597 	      if (streq ((char *) p, "gnu"))
16598 		gnu_section = TRUE;
16599 	      else
16600 		gnu_section = FALSE;
16601 
16602 	      p += namelen;
16603 	      attr_len -= namelen;
16604 
16605 	      while (attr_len > 0 && p < contents + sect->sh_size)
16606 		{
16607 		  int tag;
16608 		  unsigned int val;
16609 		  bfd_vma size;
16610 		  unsigned char * end;
16611 
16612 		  /* PR binutils/17531: Safe handling of corrupt files.  */
16613 		  if (attr_len < 6)
16614 		    {
16615 		      error (_("Unused bytes at end of section\n"));
16616 		      res = FALSE;
16617 		      section_len = 0;
16618 		      break;
16619 		    }
16620 
16621 		  tag = *(p++);
16622 		  size = byte_get (p, 4);
16623 		  if (size > attr_len)
16624 		    {
16625 		      error (_("Bad subsection length (%u > %u)\n"),
16626 			      (unsigned) size, (unsigned) attr_len);
16627 		      res = FALSE;
16628 		      size = attr_len;
16629 		    }
16630 		  /* PR binutils/17531: Safe handling of corrupt files.  */
16631 		  if (size < 6)
16632 		    {
16633 		      error (_("Bad subsection length (%u < 6)\n"),
16634 			      (unsigned) size);
16635 		      res = FALSE;
16636 		      section_len = 0;
16637 		      break;
16638 		    }
16639 
16640 		  attr_len -= size;
16641 		  end = p + size - 1;
16642 		  assert (end <= contents + sect->sh_size);
16643 		  p += 4;
16644 
16645 		  switch (tag)
16646 		    {
16647 		    case 1:
16648 		      printf (_("File Attributes\n"));
16649 		      break;
16650 		    case 2:
16651 		      printf (_("Section Attributes:"));
16652 		      goto do_numlist;
16653 		    case 3:
16654 		      printf (_("Symbol Attributes:"));
16655 		      /* Fall through.  */
16656 		    do_numlist:
16657 		      for (;;)
16658 			{
16659 			  READ_ULEB (val, p, end);
16660 			  if (val == 0)
16661 			    break;
16662 			  printf (" %d", val);
16663 			}
16664 		      printf ("\n");
16665 		      break;
16666 		    default:
16667 		      printf (_("Unknown tag: %d\n"), tag);
16668 		      public_section = FALSE;
16669 		      break;
16670 		    }
16671 
16672 		  if (public_section && display_pub_attribute != NULL)
16673 		    {
16674 		      while (p < end)
16675 			p = display_pub_attribute (p, end);
16676 		      assert (p == end);
16677 		    }
16678 		  else if (gnu_section && display_proc_gnu_attribute != NULL)
16679 		    {
16680 		      while (p < end)
16681 			p = display_gnu_attribute (p,
16682 						   display_proc_gnu_attribute,
16683 						   end);
16684 		      assert (p == end);
16685 		    }
16686 		  else if (p < end)
16687 		    {
16688 		      printf (_("  Unknown attribute:\n"));
16689 		      display_raw_attribute (p, end);
16690 		      p = end;
16691 		    }
16692 		  else
16693 		    attr_len = 0;
16694 		}
16695 	    }
16696 	}
16697 
16698       free (contents);
16699     }
16700 
16701   return res;
16702 }
16703 
16704 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
16705    Print the Address, Access and Initial fields of an entry at VMA ADDR
16706    and return the VMA of the next entry, or -1 if there was a problem.
16707    Does not read from DATA_END or beyond.  */
16708 
16709 static bfd_vma
print_mips_got_entry(unsigned char * data,bfd_vma pltgot,bfd_vma addr,unsigned char * data_end)16710 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
16711 		      unsigned char * data_end)
16712 {
16713   printf ("  ");
16714   print_vma (addr, LONG_HEX);
16715   printf (" ");
16716   if (addr < pltgot + 0xfff0)
16717     printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
16718   else
16719     printf ("%10s", "");
16720   printf (" ");
16721   if (data == NULL)
16722     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
16723   else
16724     {
16725       bfd_vma entry;
16726       unsigned char * from = data + addr - pltgot;
16727 
16728       if (from + (is_32bit_elf ? 4 : 8) > data_end)
16729 	{
16730 	  warn (_("MIPS GOT entry extends beyond the end of available data\n"));
16731 	  printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
16732 	  return (bfd_vma) -1;
16733 	}
16734       else
16735 	{
16736 	  entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
16737 	  print_vma (entry, LONG_HEX);
16738 	}
16739     }
16740   return addr + (is_32bit_elf ? 4 : 8);
16741 }
16742 
16743 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
16744    PLTGOT.  Print the Address and Initial fields of an entry at VMA
16745    ADDR and return the VMA of the next entry.  */
16746 
16747 static bfd_vma
print_mips_pltgot_entry(unsigned char * data,bfd_vma pltgot,bfd_vma addr)16748 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
16749 {
16750   printf ("  ");
16751   print_vma (addr, LONG_HEX);
16752   printf (" ");
16753   if (data == NULL)
16754     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
16755   else
16756     {
16757       bfd_vma entry;
16758 
16759       entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
16760       print_vma (entry, LONG_HEX);
16761     }
16762   return addr + (is_32bit_elf ? 4 : 8);
16763 }
16764 
16765 static void
print_mips_ases(unsigned int mask)16766 print_mips_ases (unsigned int mask)
16767 {
16768   if (mask & AFL_ASE_DSP)
16769     fputs ("\n\tDSP ASE", stdout);
16770   if (mask & AFL_ASE_DSPR2)
16771     fputs ("\n\tDSP R2 ASE", stdout);
16772   if (mask & AFL_ASE_DSPR3)
16773     fputs ("\n\tDSP R3 ASE", stdout);
16774   if (mask & AFL_ASE_EVA)
16775     fputs ("\n\tEnhanced VA Scheme", stdout);
16776   if (mask & AFL_ASE_MCU)
16777     fputs ("\n\tMCU (MicroController) ASE", stdout);
16778   if (mask & AFL_ASE_MDMX)
16779     fputs ("\n\tMDMX ASE", stdout);
16780   if (mask & AFL_ASE_MIPS3D)
16781     fputs ("\n\tMIPS-3D ASE", stdout);
16782   if (mask & AFL_ASE_MT)
16783     fputs ("\n\tMT ASE", stdout);
16784   if (mask & AFL_ASE_SMARTMIPS)
16785     fputs ("\n\tSmartMIPS ASE", stdout);
16786   if (mask & AFL_ASE_VIRT)
16787     fputs ("\n\tVZ ASE", stdout);
16788   if (mask & AFL_ASE_MSA)
16789     fputs ("\n\tMSA ASE", stdout);
16790   if (mask & AFL_ASE_MIPS16)
16791     fputs ("\n\tMIPS16 ASE", stdout);
16792   if (mask & AFL_ASE_MICROMIPS)
16793     fputs ("\n\tMICROMIPS ASE", stdout);
16794   if (mask & AFL_ASE_XPA)
16795     fputs ("\n\tXPA ASE", stdout);
16796   if (mask & AFL_ASE_MIPS16E2)
16797     fputs ("\n\tMIPS16e2 ASE", stdout);
16798   if (mask & AFL_ASE_CRC)
16799     fputs ("\n\tCRC ASE", stdout);
16800   if (mask & AFL_ASE_GINV)
16801     fputs ("\n\tGINV ASE", stdout);
16802   if (mask & AFL_ASE_LOONGSON_MMI)
16803     fputs ("\n\tLoongson MMI ASE", stdout);
16804   if (mask & AFL_ASE_LOONGSON_CAM)
16805     fputs ("\n\tLoongson CAM ASE", stdout);
16806   if (mask & AFL_ASE_LOONGSON_EXT)
16807     fputs ("\n\tLoongson EXT ASE", stdout);
16808   if (mask & AFL_ASE_LOONGSON_EXT2)
16809     fputs ("\n\tLoongson EXT2 ASE", stdout);
16810   if (mask == 0)
16811     fprintf (stdout, "\n\t%s", _("None"));
16812   else if ((mask & ~AFL_ASE_MASK) != 0)
16813     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16814 }
16815 
16816 static void
print_mips_isa_ext(unsigned int isa_ext)16817 print_mips_isa_ext (unsigned int isa_ext)
16818 {
16819   switch (isa_ext)
16820     {
16821     case 0:
16822       fputs (_("None"), stdout);
16823       break;
16824     case AFL_EXT_XLR:
16825       fputs ("RMI XLR", stdout);
16826       break;
16827     case AFL_EXT_OCTEON3:
16828       fputs ("Cavium Networks Octeon3", stdout);
16829       break;
16830     case AFL_EXT_OCTEON2:
16831       fputs ("Cavium Networks Octeon2", stdout);
16832       break;
16833     case AFL_EXT_OCTEONP:
16834       fputs ("Cavium Networks OcteonP", stdout);
16835       break;
16836     case AFL_EXT_OCTEON:
16837       fputs ("Cavium Networks Octeon", stdout);
16838       break;
16839     case AFL_EXT_5900:
16840       fputs ("Toshiba R5900", stdout);
16841       break;
16842     case AFL_EXT_4650:
16843       fputs ("MIPS R4650", stdout);
16844       break;
16845     case AFL_EXT_4010:
16846       fputs ("LSI R4010", stdout);
16847       break;
16848     case AFL_EXT_4100:
16849       fputs ("NEC VR4100", stdout);
16850       break;
16851     case AFL_EXT_3900:
16852       fputs ("Toshiba R3900", stdout);
16853       break;
16854     case AFL_EXT_10000:
16855       fputs ("MIPS R10000", stdout);
16856       break;
16857     case AFL_EXT_SB1:
16858       fputs ("Broadcom SB-1", stdout);
16859       break;
16860     case AFL_EXT_4111:
16861       fputs ("NEC VR4111/VR4181", stdout);
16862       break;
16863     case AFL_EXT_4120:
16864       fputs ("NEC VR4120", stdout);
16865       break;
16866     case AFL_EXT_5400:
16867       fputs ("NEC VR5400", stdout);
16868       break;
16869     case AFL_EXT_5500:
16870       fputs ("NEC VR5500", stdout);
16871       break;
16872     case AFL_EXT_LOONGSON_2E:
16873       fputs ("ST Microelectronics Loongson 2E", stdout);
16874       break;
16875     case AFL_EXT_LOONGSON_2F:
16876       fputs ("ST Microelectronics Loongson 2F", stdout);
16877       break;
16878     case AFL_EXT_INTERAPTIV_MR2:
16879       fputs ("Imagination interAptiv MR2", stdout);
16880       break;
16881     default:
16882       fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
16883     }
16884 }
16885 
16886 static signed int
get_mips_reg_size(int reg_size)16887 get_mips_reg_size (int reg_size)
16888 {
16889   return (reg_size == AFL_REG_NONE) ? 0
16890 	 : (reg_size == AFL_REG_32) ? 32
16891 	 : (reg_size == AFL_REG_64) ? 64
16892 	 : (reg_size == AFL_REG_128) ? 128
16893 	 : -1;
16894 }
16895 
16896 static bfd_boolean
process_mips_specific(Filedata * filedata)16897 process_mips_specific (Filedata * filedata)
16898 {
16899   Elf_Internal_Dyn * entry;
16900   Elf_Internal_Shdr *sect = NULL;
16901   size_t liblist_offset = 0;
16902   size_t liblistno = 0;
16903   size_t conflictsno = 0;
16904   size_t options_offset = 0;
16905   size_t conflicts_offset = 0;
16906   size_t pltrelsz = 0;
16907   size_t pltrel = 0;
16908   bfd_vma pltgot = 0;
16909   bfd_vma mips_pltgot = 0;
16910   bfd_vma jmprel = 0;
16911   bfd_vma local_gotno = 0;
16912   bfd_vma gotsym = 0;
16913   bfd_vma symtabno = 0;
16914   bfd_boolean res = TRUE;
16915 
16916   if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
16917 			    display_mips_gnu_attribute))
16918     res = FALSE;
16919 
16920   sect = find_section (filedata, ".MIPS.abiflags");
16921 
16922   if (sect != NULL)
16923     {
16924       Elf_External_ABIFlags_v0 *abiflags_ext;
16925       Elf_Internal_ABIFlags_v0 abiflags_in;
16926 
16927       if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
16928 	{
16929 	  error (_("Corrupt MIPS ABI Flags section.\n"));
16930 	  res = FALSE;
16931 	}
16932       else
16933 	{
16934 	  abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
16935 				   sect->sh_size, _("MIPS ABI Flags section"));
16936 	  if (abiflags_ext)
16937 	    {
16938 	      abiflags_in.version = BYTE_GET (abiflags_ext->version);
16939 	      abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
16940 	      abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
16941 	      abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
16942 	      abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
16943 	      abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
16944 	      abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
16945 	      abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
16946 	      abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
16947 	      abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
16948 	      abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
16949 
16950 	      printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
16951 	      printf ("\nISA: MIPS%d", abiflags_in.isa_level);
16952 	      if (abiflags_in.isa_rev > 1)
16953 		printf ("r%d", abiflags_in.isa_rev);
16954 	      printf ("\nGPR size: %d",
16955 		      get_mips_reg_size (abiflags_in.gpr_size));
16956 	      printf ("\nCPR1 size: %d",
16957 		      get_mips_reg_size (abiflags_in.cpr1_size));
16958 	      printf ("\nCPR2 size: %d",
16959 		      get_mips_reg_size (abiflags_in.cpr2_size));
16960 	      fputs ("\nFP ABI: ", stdout);
16961 	      print_mips_fp_abi_value (abiflags_in.fp_abi);
16962 	      fputs ("ISA Extension: ", stdout);
16963 	      print_mips_isa_ext (abiflags_in.isa_ext);
16964 	      fputs ("\nASEs:", stdout);
16965 	      print_mips_ases (abiflags_in.ases);
16966 	      printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
16967 	      printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
16968 	      fputc ('\n', stdout);
16969 	      free (abiflags_ext);
16970 	    }
16971 	}
16972     }
16973 
16974   /* We have a lot of special sections.  Thanks SGI!  */
16975   if (filedata->dynamic_section == NULL)
16976     {
16977       /* No dynamic information available.  See if there is static GOT.  */
16978       sect = find_section (filedata, ".got");
16979       if (sect != NULL)
16980 	{
16981 	  unsigned char *data_end;
16982 	  unsigned char *data;
16983 	  bfd_vma ent, end;
16984 	  int addr_size;
16985 
16986 	  pltgot = sect->sh_addr;
16987 
16988 	  ent = pltgot;
16989 	  addr_size = (is_32bit_elf ? 4 : 8);
16990 	  end = pltgot + sect->sh_size;
16991 
16992 	  data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
16993 					     end - pltgot, 1,
16994 					     _("Global Offset Table data"));
16995 	  /* PR 12855: Null data is handled gracefully throughout.  */
16996 	  data_end = data + (end - pltgot);
16997 
16998 	  printf (_("\nStatic GOT:\n"));
16999 	  printf (_(" Canonical gp value: "));
17000 	  print_vma (ent + 0x7ff0, LONG_HEX);
17001 	  printf ("\n\n");
17002 
17003 	  /* In a dynamic binary GOT[0] is reserved for the dynamic
17004 	     loader to store the lazy resolver pointer, however in
17005 	     a static binary it may well have been omitted and GOT
17006 	     reduced to a table of addresses.
17007 	     PR 21344: Check for the entry being fully available
17008 	     before fetching it.  */
17009 	  if (data
17010 	      && data + ent - pltgot + addr_size <= data_end
17011 	      && byte_get (data + ent - pltgot, addr_size) == 0)
17012 	    {
17013 	      printf (_(" Reserved entries:\n"));
17014 	      printf (_("  %*s %10s %*s\n"),
17015 		      addr_size * 2, _("Address"), _("Access"),
17016 		      addr_size * 2, _("Value"));
17017 	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
17018 	      printf ("\n");
17019 	      if (ent == (bfd_vma) -1)
17020 		goto sgot_print_fail;
17021 
17022 	      /* Check for the MSB of GOT[1] being set, identifying a
17023 		 GNU object.  This entry will be used by some runtime
17024 		 loaders, to store the module pointer.  Otherwise this
17025 		 is an ordinary local entry.
17026 		 PR 21344: Check for the entry being fully available
17027 		 before fetching it.  */
17028 	      if (data
17029 		  && data + ent - pltgot + addr_size <= data_end
17030 		  && (byte_get (data + ent - pltgot, addr_size)
17031 		      >> (addr_size * 8 - 1)) != 0)
17032 		{
17033 		  ent = print_mips_got_entry (data, pltgot, ent, data_end);
17034 		  printf ("\n");
17035 		  if (ent == (bfd_vma) -1)
17036 		    goto sgot_print_fail;
17037 		}
17038 	      printf ("\n");
17039 	    }
17040 
17041 	  if (data != NULL && ent < end)
17042 	    {
17043 	      printf (_(" Local entries:\n"));
17044 	      printf ("  %*s %10s %*s\n",
17045 		      addr_size * 2, _("Address"), _("Access"),
17046 		      addr_size * 2, _("Value"));
17047 	      while (ent < end)
17048 		{
17049 		  ent = print_mips_got_entry (data, pltgot, ent, data_end);
17050 		  printf ("\n");
17051 		  if (ent == (bfd_vma) -1)
17052 		    goto sgot_print_fail;
17053 		}
17054 	      printf ("\n");
17055 	    }
17056 
17057 	sgot_print_fail:
17058 	  free (data);
17059 	}
17060       return res;
17061     }
17062 
17063   for (entry = filedata->dynamic_section;
17064        /* PR 17531 file: 012-50589-0.004.  */
17065        (entry < filedata->dynamic_section + filedata->dynamic_nent
17066 	&& entry->d_tag != DT_NULL);
17067        ++entry)
17068     switch (entry->d_tag)
17069       {
17070       case DT_MIPS_LIBLIST:
17071 	liblist_offset
17072 	  = offset_from_vma (filedata, entry->d_un.d_val,
17073 			     liblistno * sizeof (Elf32_External_Lib));
17074 	break;
17075       case DT_MIPS_LIBLISTNO:
17076 	liblistno = entry->d_un.d_val;
17077 	break;
17078       case DT_MIPS_OPTIONS:
17079 	options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
17080 	break;
17081       case DT_MIPS_CONFLICT:
17082 	conflicts_offset
17083 	  = offset_from_vma (filedata, entry->d_un.d_val,
17084 			     conflictsno * sizeof (Elf32_External_Conflict));
17085 	break;
17086       case DT_MIPS_CONFLICTNO:
17087 	conflictsno = entry->d_un.d_val;
17088 	break;
17089       case DT_PLTGOT:
17090 	pltgot = entry->d_un.d_ptr;
17091 	break;
17092       case DT_MIPS_LOCAL_GOTNO:
17093 	local_gotno = entry->d_un.d_val;
17094 	break;
17095       case DT_MIPS_GOTSYM:
17096 	gotsym = entry->d_un.d_val;
17097 	break;
17098       case DT_MIPS_SYMTABNO:
17099 	symtabno = entry->d_un.d_val;
17100 	break;
17101       case DT_MIPS_PLTGOT:
17102 	mips_pltgot = entry->d_un.d_ptr;
17103 	break;
17104       case DT_PLTREL:
17105 	pltrel = entry->d_un.d_val;
17106 	break;
17107       case DT_PLTRELSZ:
17108 	pltrelsz = entry->d_un.d_val;
17109 	break;
17110       case DT_JMPREL:
17111 	jmprel = entry->d_un.d_ptr;
17112 	break;
17113       default:
17114 	break;
17115       }
17116 
17117   if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
17118     {
17119       Elf32_External_Lib * elib;
17120       size_t cnt;
17121 
17122       elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
17123 					      sizeof (Elf32_External_Lib),
17124 					      liblistno,
17125 					      _("liblist section data"));
17126       if (elib)
17127 	{
17128 	  printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
17129 			    "\nSection '.liblist' contains %lu entries:\n",
17130 			    (unsigned long) liblistno),
17131 		  (unsigned long) liblistno);
17132 	  fputs (_("     Library              Time Stamp          Checksum   Version Flags\n"),
17133 		 stdout);
17134 
17135 	  for (cnt = 0; cnt < liblistno; ++cnt)
17136 	    {
17137 	      Elf32_Lib liblist;
17138 	      time_t atime;
17139 	      char timebuf[128];
17140 	      struct tm * tmp;
17141 
17142 	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
17143 	      atime = BYTE_GET (elib[cnt].l_time_stamp);
17144 	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
17145 	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
17146 	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
17147 
17148 	      tmp = gmtime (&atime);
17149 	      snprintf (timebuf, sizeof (timebuf),
17150 			"%04u-%02u-%02uT%02u:%02u:%02u",
17151 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
17152 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
17153 
17154 	      printf ("%3lu: ", (unsigned long) cnt);
17155 	      if (VALID_DYNAMIC_NAME (filedata, liblist.l_name))
17156 		print_symbol (20, GET_DYNAMIC_NAME (filedata, liblist.l_name));
17157 	      else
17158 		printf (_("<corrupt: %9ld>"), liblist.l_name);
17159 	      printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
17160 		      liblist.l_version);
17161 
17162 	      if (liblist.l_flags == 0)
17163 		puts (_(" NONE"));
17164 	      else
17165 		{
17166 		  static const struct
17167 		  {
17168 		    const char * name;
17169 		    int bit;
17170 		  }
17171 		  l_flags_vals[] =
17172 		  {
17173 		    { " EXACT_MATCH", LL_EXACT_MATCH },
17174 		    { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
17175 		    { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
17176 		    { " EXPORTS", LL_EXPORTS },
17177 		    { " DELAY_LOAD", LL_DELAY_LOAD },
17178 		    { " DELTA", LL_DELTA }
17179 		  };
17180 		  int flags = liblist.l_flags;
17181 		  size_t fcnt;
17182 
17183 		  for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
17184 		    if ((flags & l_flags_vals[fcnt].bit) != 0)
17185 		      {
17186 			fputs (l_flags_vals[fcnt].name, stdout);
17187 			flags ^= l_flags_vals[fcnt].bit;
17188 		      }
17189 		  if (flags != 0)
17190 		    printf (" %#x", (unsigned int) flags);
17191 
17192 		  puts ("");
17193 		}
17194 	    }
17195 
17196 	  free (elib);
17197 	}
17198       else
17199 	res = FALSE;
17200     }
17201 
17202   if (options_offset != 0)
17203     {
17204       Elf_External_Options * eopt;
17205       size_t offset;
17206       int cnt;
17207       sect = filedata->section_headers;
17208 
17209       /* Find the section header so that we get the size.  */
17210       sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
17211       /* PR 17533 file: 012-277276-0.004.  */
17212       if (sect == NULL)
17213 	{
17214 	  error (_("No MIPS_OPTIONS header found\n"));
17215 	  return FALSE;
17216 	}
17217       /* PR 24243  */
17218       if (sect->sh_size < sizeof (* eopt))
17219 	{
17220 	  error (_("The MIPS options section is too small.\n"));
17221 	  return FALSE;
17222 	}
17223 
17224       eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
17225                                                 sect->sh_size, _("options"));
17226       if (eopt)
17227 	{
17228 	  Elf_Internal_Options option;
17229 
17230 	  offset = cnt = 0;
17231 	  while (offset <= sect->sh_size - sizeof (* eopt))
17232 	    {
17233 	      Elf_External_Options * eoption;
17234 	      unsigned int optsize;
17235 
17236 	      eoption = (Elf_External_Options *) ((char *) eopt + offset);
17237 
17238 	      optsize = BYTE_GET (eoption->size);
17239 
17240 	      /* PR 17531: file: ffa0fa3b.  */
17241 	      if (optsize < sizeof (* eopt)
17242 		  || optsize > sect->sh_size - offset)
17243 		{
17244 		  error (_("Invalid size (%u) for MIPS option\n"),
17245 			 optsize);
17246 		  free (eopt);
17247 		  return FALSE;
17248 		}
17249 	      offset += optsize;
17250 	      ++cnt;
17251 	    }
17252 
17253 	  printf (ngettext ("\nSection '%s' contains %d entry:\n",
17254 			    "\nSection '%s' contains %d entries:\n",
17255 			    cnt),
17256 		  printable_section_name (filedata, sect), cnt);
17257 
17258 	  offset = 0;
17259 	  while (cnt-- > 0)
17260 	    {
17261 	      size_t len;
17262 	      Elf_External_Options * eoption;
17263 
17264 	      eoption = (Elf_External_Options *) ((char *) eopt + offset);
17265 
17266 	      option.kind = BYTE_GET (eoption->kind);
17267 	      option.size = BYTE_GET (eoption->size);
17268 	      option.section = BYTE_GET (eoption->section);
17269 	      option.info = BYTE_GET (eoption->info);
17270 
17271 	      switch (option.kind)
17272 		{
17273 		case ODK_NULL:
17274 		  /* This shouldn't happen.  */
17275 		  printf (" NULL       %" PRId16 " %" PRIx32,
17276 			  option.section, option.info);
17277 		  break;
17278 
17279 		case ODK_REGINFO:
17280 		  printf (" REGINFO    ");
17281 		  if (filedata->file_header.e_machine == EM_MIPS)
17282 		    {
17283 		      Elf32_External_RegInfo * ereg;
17284 		      Elf32_RegInfo reginfo;
17285 
17286 		      /* 32bit form.  */
17287 		      if (option.size < (sizeof (Elf_External_Options)
17288 					 + sizeof (Elf32_External_RegInfo)))
17289 			{
17290 			  printf (_("<corrupt>\n"));
17291 			  error (_("Truncated MIPS REGINFO option\n"));
17292 			  cnt = 0;
17293 			  break;
17294 			}
17295 
17296 		      ereg = (Elf32_External_RegInfo *) (eoption + 1);
17297 
17298 		      reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
17299 		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
17300 		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
17301 		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
17302 		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
17303 		      reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
17304 
17305 		      printf ("GPR %08" PRIx32 "  GP 0x%" PRIx32 "\n",
17306 			      reginfo.ri_gprmask, reginfo.ri_gp_value);
17307 		      printf ("          "
17308 			      "  CPR0 %08" PRIx32 "  CPR1 %08" PRIx32
17309 			      "  CPR2 %08" PRIx32 "  CPR3 %08" PRIx32 "\n",
17310 			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
17311 			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
17312 		    }
17313 		  else
17314 		    {
17315 		      /* 64 bit form.  */
17316 		      Elf64_External_RegInfo * ereg;
17317 		      Elf64_Internal_RegInfo reginfo;
17318 
17319 		      if (option.size < (sizeof (Elf_External_Options)
17320 					 + sizeof (Elf64_External_RegInfo)))
17321 			{
17322 			  printf (_("<corrupt>\n"));
17323 			  error (_("Truncated MIPS REGINFO option\n"));
17324 			  cnt = 0;
17325 			  break;
17326 			}
17327 
17328 		      ereg = (Elf64_External_RegInfo *) (eoption + 1);
17329 		      reginfo.ri_gprmask    = BYTE_GET (ereg->ri_gprmask);
17330 		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
17331 		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
17332 		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
17333 		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
17334 		      reginfo.ri_gp_value   = BYTE_GET (ereg->ri_gp_value);
17335 
17336 		      printf ("GPR %08" PRIx32 "  GP 0x%" PRIx64 "\n",
17337 			      reginfo.ri_gprmask, reginfo.ri_gp_value);
17338 		      printf ("          "
17339 			      "  CPR0 %08" PRIx32 "  CPR1 %08" PRIx32
17340 			      "  CPR2 %08" PRIx32 "  CPR3 %08" PRIx32 "\n",
17341 			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
17342 			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
17343 		    }
17344 		  offset += option.size;
17345 		  continue;
17346 
17347 		case ODK_EXCEPTIONS:
17348 		  fputs (" EXCEPTIONS fpe_min(", stdout);
17349 		  process_mips_fpe_exception (option.info & OEX_FPU_MIN);
17350 		  fputs (") fpe_max(", stdout);
17351 		  process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
17352 		  fputs (")", stdout);
17353 
17354 		  if (option.info & OEX_PAGE0)
17355 		    fputs (" PAGE0", stdout);
17356 		  if (option.info & OEX_SMM)
17357 		    fputs (" SMM", stdout);
17358 		  if (option.info & OEX_FPDBUG)
17359 		    fputs (" FPDBUG", stdout);
17360 		  if (option.info & OEX_DISMISS)
17361 		    fputs (" DISMISS", stdout);
17362 		  break;
17363 
17364 		case ODK_PAD:
17365 		  fputs (" PAD       ", stdout);
17366 		  if (option.info & OPAD_PREFIX)
17367 		    fputs (" PREFIX", stdout);
17368 		  if (option.info & OPAD_POSTFIX)
17369 		    fputs (" POSTFIX", stdout);
17370 		  if (option.info & OPAD_SYMBOL)
17371 		    fputs (" SYMBOL", stdout);
17372 		  break;
17373 
17374 		case ODK_HWPATCH:
17375 		  fputs (" HWPATCH   ", stdout);
17376 		  if (option.info & OHW_R4KEOP)
17377 		    fputs (" R4KEOP", stdout);
17378 		  if (option.info & OHW_R8KPFETCH)
17379 		    fputs (" R8KPFETCH", stdout);
17380 		  if (option.info & OHW_R5KEOP)
17381 		    fputs (" R5KEOP", stdout);
17382 		  if (option.info & OHW_R5KCVTL)
17383 		    fputs (" R5KCVTL", stdout);
17384 		  break;
17385 
17386 		case ODK_FILL:
17387 		  fputs (" FILL       ", stdout);
17388 		  /* XXX Print content of info word?  */
17389 		  break;
17390 
17391 		case ODK_TAGS:
17392 		  fputs (" TAGS       ", stdout);
17393 		  /* XXX Print content of info word?  */
17394 		  break;
17395 
17396 		case ODK_HWAND:
17397 		  fputs (" HWAND     ", stdout);
17398 		  if (option.info & OHWA0_R4KEOP_CHECKED)
17399 		    fputs (" R4KEOP_CHECKED", stdout);
17400 		  if (option.info & OHWA0_R4KEOP_CLEAN)
17401 		    fputs (" R4KEOP_CLEAN", stdout);
17402 		  break;
17403 
17404 		case ODK_HWOR:
17405 		  fputs (" HWOR      ", stdout);
17406 		  if (option.info & OHWA0_R4KEOP_CHECKED)
17407 		    fputs (" R4KEOP_CHECKED", stdout);
17408 		  if (option.info & OHWA0_R4KEOP_CLEAN)
17409 		    fputs (" R4KEOP_CLEAN", stdout);
17410 		  break;
17411 
17412 		case ODK_GP_GROUP:
17413 		  printf (" GP_GROUP  %#06x  self-contained %#06x",
17414 			  option.info & OGP_GROUP,
17415 			  (option.info & OGP_SELF) >> 16);
17416 		  break;
17417 
17418 		case ODK_IDENT:
17419 		  printf (" IDENT     %#06x  self-contained %#06x",
17420 			  option.info & OGP_GROUP,
17421 			  (option.info & OGP_SELF) >> 16);
17422 		  break;
17423 
17424 		default:
17425 		  /* This shouldn't happen.  */
17426 		  printf (" %3d ???     %" PRId16 " %" PRIx32,
17427 			  option.kind, option.section, option.info);
17428 		  break;
17429 		}
17430 
17431 	      len = sizeof (* eopt);
17432 	      while (len < option.size)
17433 		{
17434 		  unsigned char datum = *((unsigned char *) eoption + len);
17435 
17436 		  if (ISPRINT (datum))
17437 		    printf ("%c", datum);
17438 		  else
17439 		    printf ("\\%03o", datum);
17440 		  len ++;
17441 		}
17442 	      fputs ("\n", stdout);
17443 
17444 	      offset += option.size;
17445 	    }
17446 	  free (eopt);
17447 	}
17448       else
17449 	res = FALSE;
17450     }
17451 
17452   if (conflicts_offset != 0 && conflictsno != 0)
17453     {
17454       Elf32_Conflict * iconf;
17455       size_t cnt;
17456 
17457       if (filedata->dynamic_symbols == NULL)
17458 	{
17459 	  error (_("conflict list found without a dynamic symbol table\n"));
17460 	  return FALSE;
17461 	}
17462 
17463       /* PR 21345 - print a slightly more helpful error message
17464 	 if we are sure that the cmalloc will fail.  */
17465       if (conflictsno > filedata->file_size / sizeof (* iconf))
17466 	{
17467 	  error (_("Overlarge number of conflicts detected: %lx\n"),
17468 		 (long) conflictsno);
17469 	  return FALSE;
17470 	}
17471 
17472       iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
17473       if (iconf == NULL)
17474 	{
17475 	  error (_("Out of memory allocating space for dynamic conflicts\n"));
17476 	  return FALSE;
17477 	}
17478 
17479       if (is_32bit_elf)
17480 	{
17481 	  Elf32_External_Conflict * econf32;
17482 
17483 	  econf32 = (Elf32_External_Conflict *)
17484 	    get_data (NULL, filedata, conflicts_offset,
17485 		      sizeof (*econf32), conflictsno, _("conflict"));
17486 	  if (!econf32)
17487 	    {
17488 	      free (iconf);
17489 	      return FALSE;
17490 	    }
17491 
17492 	  for (cnt = 0; cnt < conflictsno; ++cnt)
17493 	    iconf[cnt] = BYTE_GET (econf32[cnt]);
17494 
17495 	  free (econf32);
17496 	}
17497       else
17498 	{
17499 	  Elf64_External_Conflict * econf64;
17500 
17501 	  econf64 = (Elf64_External_Conflict *)
17502 	    get_data (NULL, filedata, conflicts_offset,
17503 		      sizeof (*econf64), conflictsno, _("conflict"));
17504 	  if (!econf64)
17505 	    {
17506 	      free (iconf);
17507 	      return FALSE;
17508 	    }
17509 
17510 	  for (cnt = 0; cnt < conflictsno; ++cnt)
17511 	    iconf[cnt] = BYTE_GET (econf64[cnt]);
17512 
17513 	  free (econf64);
17514 	}
17515 
17516       printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
17517 			"\nSection '.conflict' contains %lu entries:\n",
17518 			(unsigned long) conflictsno),
17519 	      (unsigned long) conflictsno);
17520       puts (_("  Num:    Index       Value  Name"));
17521 
17522       for (cnt = 0; cnt < conflictsno; ++cnt)
17523 	{
17524 	  printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
17525 
17526 	  if (iconf[cnt] >= filedata->num_dynamic_syms)
17527 	    printf (_("<corrupt symbol index>"));
17528 	  else
17529 	    {
17530 	      Elf_Internal_Sym * psym;
17531 
17532 	      psym = & filedata->dynamic_symbols[iconf[cnt]];
17533 	      print_vma (psym->st_value, FULL_HEX);
17534 	      putchar (' ');
17535 	      if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
17536 		print_symbol (25, GET_DYNAMIC_NAME (filedata, psym->st_name));
17537 	      else
17538 		printf (_("<corrupt: %14ld>"), psym->st_name);
17539 	    }
17540 	  putchar ('\n');
17541 	}
17542 
17543       free (iconf);
17544     }
17545 
17546   if (pltgot != 0 && local_gotno != 0)
17547     {
17548       bfd_vma ent, local_end, global_end;
17549       size_t i, offset;
17550       unsigned char * data;
17551       unsigned char * data_end;
17552       int addr_size;
17553 
17554       ent = pltgot;
17555       addr_size = (is_32bit_elf ? 4 : 8);
17556       local_end = pltgot + local_gotno * addr_size;
17557 
17558       /* PR binutils/17533 file: 012-111227-0.004  */
17559       if (symtabno < gotsym)
17560 	{
17561 	  error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
17562 		 (unsigned long) gotsym, (unsigned long) symtabno);
17563 	  return FALSE;
17564 	}
17565 
17566       global_end = local_end + (symtabno - gotsym) * addr_size;
17567       /* PR 17531: file: 54c91a34.  */
17568       if (global_end < local_end)
17569 	{
17570 	  error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
17571 	  return FALSE;
17572 	}
17573 
17574       offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
17575       data = (unsigned char *) get_data (NULL, filedata, offset,
17576                                          global_end - pltgot, 1,
17577 					 _("Global Offset Table data"));
17578       /* PR 12855: Null data is handled gracefully throughout.  */
17579       data_end = data + (global_end - pltgot);
17580 
17581       printf (_("\nPrimary GOT:\n"));
17582       printf (_(" Canonical gp value: "));
17583       print_vma (pltgot + 0x7ff0, LONG_HEX);
17584       printf ("\n\n");
17585 
17586       printf (_(" Reserved entries:\n"));
17587       printf (_("  %*s %10s %*s Purpose\n"),
17588 	      addr_size * 2, _("Address"), _("Access"),
17589 	      addr_size * 2, _("Initial"));
17590       ent = print_mips_got_entry (data, pltgot, ent, data_end);
17591       printf (_(" Lazy resolver\n"));
17592       if (ent == (bfd_vma) -1)
17593 	goto got_print_fail;
17594 
17595       /* Check for the MSB of GOT[1] being set, denoting a GNU object.
17596 	 This entry will be used by some runtime loaders, to store the
17597 	 module pointer.  Otherwise this is an ordinary local entry.
17598 	 PR 21344: Check for the entry being fully available before
17599 	 fetching it.  */
17600       if (data
17601 	  && data + ent - pltgot + addr_size <= data_end
17602 	  && (byte_get (data + ent - pltgot, addr_size)
17603 	      >> (addr_size * 8 - 1)) != 0)
17604 	{
17605 	  ent = print_mips_got_entry (data, pltgot, ent, data_end);
17606 	  printf (_(" Module pointer (GNU extension)\n"));
17607 	  if (ent == (bfd_vma) -1)
17608 	    goto got_print_fail;
17609 	}
17610       printf ("\n");
17611 
17612       if (data != NULL && ent < local_end)
17613 	{
17614 	  printf (_(" Local entries:\n"));
17615 	  printf ("  %*s %10s %*s\n",
17616 		  addr_size * 2, _("Address"), _("Access"),
17617 		  addr_size * 2, _("Initial"));
17618 	  while (ent < local_end)
17619 	    {
17620 	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
17621 	      printf ("\n");
17622 	      if (ent == (bfd_vma) -1)
17623 		goto got_print_fail;
17624 	    }
17625 	  printf ("\n");
17626 	}
17627 
17628       if (data != NULL && gotsym < symtabno)
17629 	{
17630 	  int sym_width;
17631 
17632 	  printf (_(" Global entries:\n"));
17633 	  printf ("  %*s %10s %*s %*s %-7s %3s %s\n",
17634 		  addr_size * 2, _("Address"),
17635 		  _("Access"),
17636 		  addr_size * 2, _("Initial"),
17637 		  addr_size * 2, _("Sym.Val."),
17638 		  _("Type"),
17639 		  /* Note for translators: "Ndx" = abbreviated form of "Index".  */
17640 		  _("Ndx"), _("Name"));
17641 
17642 	  sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
17643 
17644 	  for (i = gotsym; i < symtabno; i++)
17645 	    {
17646 	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
17647 	      printf (" ");
17648 
17649 	      if (filedata->dynamic_symbols == NULL)
17650 		printf (_("<no dynamic symbols>"));
17651 	      else if (i < filedata->num_dynamic_syms)
17652 		{
17653 		  Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
17654 
17655 		  print_vma (psym->st_value, LONG_HEX);
17656 		  printf (" %-7s %3s ",
17657 			  get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
17658 			  get_symbol_index_type (filedata, psym->st_shndx));
17659 
17660 		  if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
17661 		    print_symbol (sym_width,
17662 				  GET_DYNAMIC_NAME (filedata, psym->st_name));
17663 		  else
17664 		    printf (_("<corrupt: %14ld>"), psym->st_name);
17665 		}
17666 	      else
17667 		printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
17668 			(unsigned long) i);
17669 
17670 	      printf ("\n");
17671 	      if (ent == (bfd_vma) -1)
17672 		break;
17673 	    }
17674 	  printf ("\n");
17675 	}
17676 
17677     got_print_fail:
17678       free (data);
17679     }
17680 
17681   if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
17682     {
17683       bfd_vma ent, end;
17684       size_t offset, rel_offset;
17685       unsigned long count, i;
17686       unsigned char * data;
17687       int addr_size, sym_width;
17688       Elf_Internal_Rela * rels;
17689 
17690       rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
17691       if (pltrel == DT_RELA)
17692 	{
17693 	  if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
17694 	    return FALSE;
17695 	}
17696       else
17697 	{
17698 	  if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
17699 	    return FALSE;
17700 	}
17701 
17702       ent = mips_pltgot;
17703       addr_size = (is_32bit_elf ? 4 : 8);
17704       end = mips_pltgot + (2 + count) * addr_size;
17705 
17706       offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
17707       data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
17708                                          1, _("Procedure Linkage Table data"));
17709       if (data == NULL)
17710 	return FALSE;
17711 
17712       printf ("\nPLT GOT:\n\n");
17713       printf (_(" Reserved entries:\n"));
17714       printf (_("  %*s %*s Purpose\n"),
17715 	      addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
17716       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
17717       printf (_(" PLT lazy resolver\n"));
17718       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
17719       printf (_(" Module pointer\n"));
17720       printf ("\n");
17721 
17722       printf (_(" Entries:\n"));
17723       printf ("  %*s %*s %*s %-7s %3s %s\n",
17724 	      addr_size * 2, _("Address"),
17725 	      addr_size * 2, _("Initial"),
17726 	      addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
17727       sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
17728       for (i = 0; i < count; i++)
17729 	{
17730 	  unsigned long idx = get_reloc_symindex (rels[i].r_info);
17731 
17732 	  ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
17733 	  printf (" ");
17734 
17735 	  if (idx >= filedata->num_dynamic_syms)
17736 	    printf (_("<corrupt symbol index: %lu>"), idx);
17737 	  else
17738 	    {
17739 	      Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
17740 
17741 	      print_vma (psym->st_value, LONG_HEX);
17742 	      printf (" %-7s %3s ",
17743 		      get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
17744 		      get_symbol_index_type (filedata, psym->st_shndx));
17745 	      if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
17746 		print_symbol (sym_width,
17747 			      GET_DYNAMIC_NAME (filedata, psym->st_name));
17748 	      else
17749 		printf (_("<corrupt: %14ld>"), psym->st_name);
17750 	    }
17751 	  printf ("\n");
17752 	}
17753       printf ("\n");
17754 
17755       free (data);
17756       free (rels);
17757     }
17758 
17759   return res;
17760 }
17761 
17762 static bfd_boolean
process_nds32_specific(Filedata * filedata)17763 process_nds32_specific (Filedata * filedata)
17764 {
17765   Elf_Internal_Shdr *sect = NULL;
17766 
17767   sect = find_section (filedata, ".nds32_e_flags");
17768   if (sect != NULL && sect->sh_size >= 4)
17769     {
17770       unsigned char *buf;
17771       unsigned int flag;
17772 
17773       printf ("\nNDS32 elf flags section:\n");
17774       buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
17775 		      _("NDS32 elf flags section"));
17776 
17777       if (buf == NULL)
17778 	return FALSE;
17779 
17780       flag = byte_get (buf, 4);
17781       free (buf);
17782       switch (flag & 0x3)
17783 	{
17784 	case 0:
17785 	  printf ("(VEC_SIZE):\tNo entry.\n");
17786 	  break;
17787 	case 1:
17788 	  printf ("(VEC_SIZE):\t4 bytes\n");
17789 	  break;
17790 	case 2:
17791 	  printf ("(VEC_SIZE):\t16 bytes\n");
17792 	  break;
17793 	case 3:
17794 	  printf ("(VEC_SIZE):\treserved\n");
17795 	  break;
17796 	}
17797     }
17798 
17799   return TRUE;
17800 }
17801 
17802 static bfd_boolean
process_gnu_liblist(Filedata * filedata)17803 process_gnu_liblist (Filedata * filedata)
17804 {
17805   Elf_Internal_Shdr * section;
17806   Elf_Internal_Shdr * string_sec;
17807   Elf32_External_Lib * elib;
17808   char * strtab;
17809   size_t strtab_size;
17810   size_t cnt;
17811   unsigned long num_liblist;
17812   unsigned i;
17813   bfd_boolean res = TRUE;
17814 
17815   if (! do_arch)
17816     return TRUE;
17817 
17818   for (i = 0, section = filedata->section_headers;
17819        i < filedata->file_header.e_shnum;
17820        i++, section++)
17821     {
17822       switch (section->sh_type)
17823 	{
17824 	case SHT_GNU_LIBLIST:
17825 	  if (section->sh_link >= filedata->file_header.e_shnum)
17826 	    break;
17827 
17828 	  elib = (Elf32_External_Lib *)
17829               get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
17830                         _("liblist section data"));
17831 
17832 	  if (elib == NULL)
17833 	    {
17834 	      res = FALSE;
17835 	      break;
17836 	    }
17837 
17838 	  string_sec = filedata->section_headers + section->sh_link;
17839 	  strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
17840                                       string_sec->sh_size,
17841                                       _("liblist string table"));
17842 	  if (strtab == NULL
17843 	      || section->sh_entsize != sizeof (Elf32_External_Lib))
17844 	    {
17845 	      free (elib);
17846 	      free (strtab);
17847 	      res = FALSE;
17848 	      break;
17849 	    }
17850 	  strtab_size = string_sec->sh_size;
17851 
17852 	  num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
17853 	  printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
17854 			    "\nLibrary list section '%s' contains %lu entries:\n",
17855 			    num_liblist),
17856 		  printable_section_name (filedata, section),
17857 		  num_liblist);
17858 
17859 	  puts (_("     Library              Time Stamp          Checksum   Version Flags"));
17860 
17861 	  for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
17862 	       ++cnt)
17863 	    {
17864 	      Elf32_Lib liblist;
17865 	      time_t atime;
17866 	      char timebuf[128];
17867 	      struct tm * tmp;
17868 
17869 	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
17870 	      atime = BYTE_GET (elib[cnt].l_time_stamp);
17871 	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
17872 	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
17873 	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
17874 
17875 	      tmp = gmtime (&atime);
17876 	      snprintf (timebuf, sizeof (timebuf),
17877 			"%04u-%02u-%02uT%02u:%02u:%02u",
17878 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
17879 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
17880 
17881 	      printf ("%3lu: ", (unsigned long) cnt);
17882 	      if (do_wide)
17883 		printf ("%-20s", liblist.l_name < strtab_size
17884 			? strtab + liblist.l_name : _("<corrupt>"));
17885 	      else
17886 		printf ("%-20.20s", liblist.l_name < strtab_size
17887 			? strtab + liblist.l_name : _("<corrupt>"));
17888 	      printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
17889 		      liblist.l_version, liblist.l_flags);
17890 	    }
17891 
17892 	  free (elib);
17893 	  free (strtab);
17894 	}
17895     }
17896 
17897   return res;
17898 }
17899 
17900 static const char *
get_note_type(Filedata * filedata,unsigned e_type)17901 get_note_type (Filedata * filedata, unsigned e_type)
17902 {
17903   static char buff[64];
17904 
17905   if (filedata->file_header.e_type == ET_CORE)
17906     switch (e_type)
17907       {
17908       case NT_AUXV:
17909 	return _("NT_AUXV (auxiliary vector)");
17910       case NT_PRSTATUS:
17911 	return _("NT_PRSTATUS (prstatus structure)");
17912       case NT_FPREGSET:
17913 	return _("NT_FPREGSET (floating point registers)");
17914       case NT_PRPSINFO:
17915 	return _("NT_PRPSINFO (prpsinfo structure)");
17916       case NT_TASKSTRUCT:
17917 	return _("NT_TASKSTRUCT (task structure)");
17918       case NT_PRXFPREG:
17919 	return _("NT_PRXFPREG (user_xfpregs structure)");
17920       case NT_PPC_VMX:
17921 	return _("NT_PPC_VMX (ppc Altivec registers)");
17922       case NT_PPC_VSX:
17923 	return _("NT_PPC_VSX (ppc VSX registers)");
17924       case NT_PPC_TAR:
17925 	return _("NT_PPC_TAR (ppc TAR register)");
17926       case NT_PPC_PPR:
17927 	return _("NT_PPC_PPR (ppc PPR register)");
17928       case NT_PPC_DSCR:
17929 	return _("NT_PPC_DSCR (ppc DSCR register)");
17930       case NT_PPC_EBB:
17931 	return _("NT_PPC_EBB (ppc EBB registers)");
17932       case NT_PPC_PMU:
17933 	return _("NT_PPC_PMU (ppc PMU registers)");
17934       case NT_PPC_TM_CGPR:
17935 	return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
17936       case NT_PPC_TM_CFPR:
17937 	return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
17938       case NT_PPC_TM_CVMX:
17939 	return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
17940       case NT_PPC_TM_CVSX:
17941 	return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
17942       case NT_PPC_TM_SPR:
17943 	return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
17944       case NT_PPC_TM_CTAR:
17945 	return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
17946       case NT_PPC_TM_CPPR:
17947 	return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
17948       case NT_PPC_TM_CDSCR:
17949 	return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
17950       case NT_386_TLS:
17951 	return _("NT_386_TLS (x86 TLS information)");
17952       case NT_386_IOPERM:
17953 	return _("NT_386_IOPERM (x86 I/O permissions)");
17954       case NT_X86_XSTATE:
17955 	return _("NT_X86_XSTATE (x86 XSAVE extended state)");
17956       case NT_X86_CET:
17957 	return _("NT_X86_CET (x86 CET state)");
17958       case NT_S390_HIGH_GPRS:
17959 	return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
17960       case NT_S390_TIMER:
17961 	return _("NT_S390_TIMER (s390 timer register)");
17962       case NT_S390_TODCMP:
17963 	return _("NT_S390_TODCMP (s390 TOD comparator register)");
17964       case NT_S390_TODPREG:
17965 	return _("NT_S390_TODPREG (s390 TOD programmable register)");
17966       case NT_S390_CTRS:
17967 	return _("NT_S390_CTRS (s390 control registers)");
17968       case NT_S390_PREFIX:
17969 	return _("NT_S390_PREFIX (s390 prefix register)");
17970       case NT_S390_LAST_BREAK:
17971 	return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
17972       case NT_S390_SYSTEM_CALL:
17973 	return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
17974       case NT_S390_TDB:
17975 	return _("NT_S390_TDB (s390 transaction diagnostic block)");
17976       case NT_S390_VXRS_LOW:
17977 	return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
17978       case NT_S390_VXRS_HIGH:
17979 	return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
17980       case NT_S390_GS_CB:
17981 	return _("NT_S390_GS_CB (s390 guarded-storage registers)");
17982       case NT_S390_GS_BC:
17983 	return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
17984       case NT_ARM_VFP:
17985 	return _("NT_ARM_VFP (arm VFP registers)");
17986       case NT_ARM_TLS:
17987 	return _("NT_ARM_TLS (AArch TLS registers)");
17988       case NT_ARM_HW_BREAK:
17989 	return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
17990       case NT_ARM_HW_WATCH:
17991 	return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
17992       case NT_ARC_V2:
17993 	return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
17994       case NT_PSTATUS:
17995 	return _("NT_PSTATUS (pstatus structure)");
17996       case NT_FPREGS:
17997 	return _("NT_FPREGS (floating point registers)");
17998       case NT_PSINFO:
17999 	return _("NT_PSINFO (psinfo structure)");
18000       case NT_LWPSTATUS:
18001 	return _("NT_LWPSTATUS (lwpstatus_t structure)");
18002       case NT_LWPSINFO:
18003 	return _("NT_LWPSINFO (lwpsinfo_t structure)");
18004       case NT_WIN32PSTATUS:
18005 	return _("NT_WIN32PSTATUS (win32_pstatus structure)");
18006       case NT_SIGINFO:
18007 	return _("NT_SIGINFO (siginfo_t data)");
18008       case NT_FILE:
18009 	return _("NT_FILE (mapped files)");
18010       default:
18011 	break;
18012       }
18013   else
18014     switch (e_type)
18015       {
18016       case NT_VERSION:
18017 	return _("NT_VERSION (version)");
18018       case NT_ARCH:
18019 	return _("NT_ARCH (architecture)");
18020       case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18021 	return _("OPEN");
18022       case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18023 	return _("func");
18024       default:
18025 	break;
18026       }
18027 
18028   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18029   return buff;
18030 }
18031 
18032 static bfd_boolean
print_core_note(Elf_Internal_Note * pnote)18033 print_core_note (Elf_Internal_Note *pnote)
18034 {
18035   unsigned int addr_size = is_32bit_elf ? 4 : 8;
18036   bfd_vma count, page_size;
18037   unsigned char *descdata, *filenames, *descend;
18038 
18039   if (pnote->type != NT_FILE)
18040     {
18041       if (do_wide)
18042 	printf ("\n");
18043       return TRUE;
18044     }
18045 
18046 #ifndef BFD64
18047   if (!is_32bit_elf)
18048     {
18049       printf (_("    Cannot decode 64-bit note in 32-bit build\n"));
18050       /* Still "successful".  */
18051       return TRUE;
18052     }
18053 #endif
18054 
18055   if (pnote->descsz < 2 * addr_size)
18056     {
18057       error (_("    Malformed note - too short for header\n"));
18058       return FALSE;
18059     }
18060 
18061   descdata = (unsigned char *) pnote->descdata;
18062   descend = descdata + pnote->descsz;
18063 
18064   if (descdata[pnote->descsz - 1] != '\0')
18065     {
18066       error (_("    Malformed note - does not end with \\0\n"));
18067       return FALSE;
18068     }
18069 
18070   count = byte_get (descdata, addr_size);
18071   descdata += addr_size;
18072 
18073   page_size = byte_get (descdata, addr_size);
18074   descdata += addr_size;
18075 
18076   if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
18077       || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
18078     {
18079       error (_("    Malformed note - too short for supplied file count\n"));
18080       return FALSE;
18081     }
18082 
18083   printf (_("    Page size: "));
18084   print_vma (page_size, DEC);
18085   printf ("\n");
18086 
18087   printf (_("    %*s%*s%*s\n"),
18088 	  (int) (2 + 2 * addr_size), _("Start"),
18089 	  (int) (4 + 2 * addr_size), _("End"),
18090 	  (int) (4 + 2 * addr_size), _("Page Offset"));
18091   filenames = descdata + count * 3 * addr_size;
18092   while (count-- > 0)
18093     {
18094       bfd_vma start, end, file_ofs;
18095 
18096       if (filenames == descend)
18097 	{
18098 	  error (_("    Malformed note - filenames end too early\n"));
18099 	  return FALSE;
18100 	}
18101 
18102       start = byte_get (descdata, addr_size);
18103       descdata += addr_size;
18104       end = byte_get (descdata, addr_size);
18105       descdata += addr_size;
18106       file_ofs = byte_get (descdata, addr_size);
18107       descdata += addr_size;
18108 
18109       printf ("    ");
18110       print_vma (start, FULL_HEX);
18111       printf ("  ");
18112       print_vma (end, FULL_HEX);
18113       printf ("  ");
18114       print_vma (file_ofs, FULL_HEX);
18115       printf ("\n        %s\n", filenames);
18116 
18117       filenames += 1 + strlen ((char *) filenames);
18118     }
18119 
18120   return TRUE;
18121 }
18122 
18123 static const char *
get_gnu_elf_note_type(unsigned e_type)18124 get_gnu_elf_note_type (unsigned e_type)
18125 {
18126   /* NB/ Keep this switch statement in sync with print_gnu_note ().  */
18127   switch (e_type)
18128     {
18129     case NT_GNU_ABI_TAG:
18130       return _("NT_GNU_ABI_TAG (ABI version tag)");
18131     case NT_GNU_HWCAP:
18132       return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
18133     case NT_GNU_BUILD_ID:
18134       return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
18135     case NT_GNU_GOLD_VERSION:
18136       return _("NT_GNU_GOLD_VERSION (gold version)");
18137     case NT_GNU_PROPERTY_TYPE_0:
18138       return _("NT_GNU_PROPERTY_TYPE_0");
18139     case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18140       return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
18141     case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18142       return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
18143     default:
18144       {
18145 	static char buff[64];
18146 
18147 	snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18148 	return buff;
18149       }
18150     }
18151 }
18152 
18153 static void
decode_x86_compat_isa(unsigned int bitmask)18154 decode_x86_compat_isa (unsigned int bitmask)
18155 {
18156   while (bitmask)
18157     {
18158       unsigned int bit = bitmask & (- bitmask);
18159 
18160       bitmask &= ~ bit;
18161       switch (bit)
18162 	{
18163 	case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
18164 	  printf ("i486");
18165 	  break;
18166 	case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
18167 	  printf ("586");
18168 	  break;
18169 	case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
18170 	  printf ("686");
18171 	  break;
18172 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
18173 	  printf ("SSE");
18174 	  break;
18175 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
18176 	  printf ("SSE2");
18177 	  break;
18178 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
18179 	  printf ("SSE3");
18180 	  break;
18181 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
18182 	  printf ("SSSE3");
18183 	  break;
18184 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
18185 	  printf ("SSE4_1");
18186 	  break;
18187 	case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
18188 	  printf ("SSE4_2");
18189 	  break;
18190 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
18191 	  printf ("AVX");
18192 	  break;
18193 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
18194 	  printf ("AVX2");
18195 	  break;
18196 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
18197 	  printf ("AVX512F");
18198 	  break;
18199 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
18200 	  printf ("AVX512CD");
18201 	  break;
18202 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
18203 	  printf ("AVX512ER");
18204 	  break;
18205 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
18206 	  printf ("AVX512PF");
18207 	  break;
18208 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
18209 	  printf ("AVX512VL");
18210 	  break;
18211 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
18212 	  printf ("AVX512DQ");
18213 	  break;
18214 	case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
18215 	  printf ("AVX512BW");
18216 	  break;
18217 	default:
18218 	  printf (_("<unknown: %x>"), bit);
18219 	  break;
18220 	}
18221       if (bitmask)
18222 	printf (", ");
18223     }
18224 }
18225 
18226 static void
decode_x86_isa(unsigned int bitmask)18227 decode_x86_isa (unsigned int bitmask)
18228 {
18229   if (!bitmask)
18230     {
18231       printf (_("<None>"));
18232       return;
18233     }
18234 
18235   while (bitmask)
18236     {
18237       unsigned int bit = bitmask & (- bitmask);
18238 
18239       bitmask &= ~ bit;
18240       switch (bit)
18241 	{
18242 	case GNU_PROPERTY_X86_ISA_1_CMOV:
18243 	  printf ("CMOV");
18244 	  break;
18245 	case GNU_PROPERTY_X86_ISA_1_SSE:
18246 	  printf ("SSE");
18247 	  break;
18248 	case GNU_PROPERTY_X86_ISA_1_SSE2:
18249 	  printf ("SSE2");
18250 	  break;
18251 	case GNU_PROPERTY_X86_ISA_1_SSE3:
18252 	  printf ("SSE3");
18253 	  break;
18254 	case GNU_PROPERTY_X86_ISA_1_SSSE3:
18255 	  printf ("SSSE3");
18256 	  break;
18257 	case GNU_PROPERTY_X86_ISA_1_SSE4_1:
18258 	  printf ("SSE4_1");
18259 	  break;
18260 	case GNU_PROPERTY_X86_ISA_1_SSE4_2:
18261 	  printf ("SSE4_2");
18262 	  break;
18263 	case GNU_PROPERTY_X86_ISA_1_AVX:
18264 	  printf ("AVX");
18265 	  break;
18266 	case GNU_PROPERTY_X86_ISA_1_AVX2:
18267 	  printf ("AVX2");
18268 	  break;
18269 	case GNU_PROPERTY_X86_ISA_1_FMA:
18270 	  printf ("FMA");
18271 	  break;
18272 	case GNU_PROPERTY_X86_ISA_1_AVX512F:
18273 	  printf ("AVX512F");
18274 	  break;
18275 	case GNU_PROPERTY_X86_ISA_1_AVX512CD:
18276 	  printf ("AVX512CD");
18277 	  break;
18278 	case GNU_PROPERTY_X86_ISA_1_AVX512ER:
18279 	  printf ("AVX512ER");
18280 	  break;
18281 	case GNU_PROPERTY_X86_ISA_1_AVX512PF:
18282 	  printf ("AVX512PF");
18283 	  break;
18284 	case GNU_PROPERTY_X86_ISA_1_AVX512VL:
18285 	  printf ("AVX512VL");
18286 	  break;
18287 	case GNU_PROPERTY_X86_ISA_1_AVX512DQ:
18288 	  printf ("AVX512DQ");
18289 	  break;
18290 	case GNU_PROPERTY_X86_ISA_1_AVX512BW:
18291 	  printf ("AVX512BW");
18292 	  break;
18293 	case GNU_PROPERTY_X86_ISA_1_AVX512_4FMAPS:
18294 	  printf ("AVX512_4FMAPS");
18295 	  break;
18296 	case GNU_PROPERTY_X86_ISA_1_AVX512_4VNNIW:
18297 	  printf ("AVX512_4VNNIW");
18298 	  break;
18299 	case GNU_PROPERTY_X86_ISA_1_AVX512_BITALG:
18300 	  printf ("AVX512_BITALG");
18301 	  break;
18302 	case GNU_PROPERTY_X86_ISA_1_AVX512_IFMA:
18303 	  printf ("AVX512_IFMA");
18304 	  break;
18305 	case GNU_PROPERTY_X86_ISA_1_AVX512_VBMI:
18306 	  printf ("AVX512_VBMI");
18307 	  break;
18308 	case GNU_PROPERTY_X86_ISA_1_AVX512_VBMI2:
18309 	  printf ("AVX512_VBMI2");
18310 	  break;
18311 	case GNU_PROPERTY_X86_ISA_1_AVX512_VNNI:
18312 	  printf ("AVX512_VNNI");
18313 	  break;
18314 	case GNU_PROPERTY_X86_ISA_1_AVX512_BF16:
18315 	  printf ("AVX512_BF16");
18316 	  break;
18317 	default:
18318 	  printf (_("<unknown: %x>"), bit);
18319 	  break;
18320 	}
18321       if (bitmask)
18322 	printf (", ");
18323     }
18324 }
18325 
18326 static void
decode_x86_feature_1(unsigned int bitmask)18327 decode_x86_feature_1 (unsigned int bitmask)
18328 {
18329   if (!bitmask)
18330     {
18331       printf (_("<None>"));
18332       return;
18333     }
18334 
18335   while (bitmask)
18336     {
18337       unsigned int bit = bitmask & (- bitmask);
18338 
18339       bitmask &= ~ bit;
18340       switch (bit)
18341 	{
18342 	case GNU_PROPERTY_X86_FEATURE_1_IBT:
18343 	  printf ("IBT");
18344 	  break;
18345 	case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
18346 	  printf ("SHSTK");
18347 	  break;
18348 	default:
18349 	  printf (_("<unknown: %x>"), bit);
18350 	  break;
18351 	}
18352       if (bitmask)
18353 	printf (", ");
18354     }
18355 }
18356 
18357 static void
decode_x86_feature_2(unsigned int bitmask)18358 decode_x86_feature_2 (unsigned int bitmask)
18359 {
18360   if (!bitmask)
18361     {
18362       printf (_("<None>"));
18363       return;
18364     }
18365 
18366   while (bitmask)
18367     {
18368       unsigned int bit = bitmask & (- bitmask);
18369 
18370       bitmask &= ~ bit;
18371       switch (bit)
18372 	{
18373 	case GNU_PROPERTY_X86_FEATURE_2_X86:
18374 	  printf ("x86");
18375 	  break;
18376 	case GNU_PROPERTY_X86_FEATURE_2_X87:
18377 	  printf ("x87");
18378 	  break;
18379 	case GNU_PROPERTY_X86_FEATURE_2_MMX:
18380 	  printf ("MMX");
18381 	  break;
18382 	case GNU_PROPERTY_X86_FEATURE_2_XMM:
18383 	  printf ("XMM");
18384 	  break;
18385 	case GNU_PROPERTY_X86_FEATURE_2_YMM:
18386 	  printf ("YMM");
18387 	  break;
18388 	case GNU_PROPERTY_X86_FEATURE_2_ZMM:
18389 	  printf ("ZMM");
18390 	  break;
18391 	case GNU_PROPERTY_X86_FEATURE_2_TMM:
18392 	  printf ("TMM");
18393 	  break;
18394 	case GNU_PROPERTY_X86_FEATURE_2_FXSR:
18395 	  printf ("FXSR");
18396 	  break;
18397 	case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
18398 	  printf ("XSAVE");
18399 	  break;
18400 	case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
18401 	  printf ("XSAVEOPT");
18402 	  break;
18403 	case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
18404 	  printf ("XSAVEC");
18405 	  break;
18406 	default:
18407 	  printf (_("<unknown: %x>"), bit);
18408 	  break;
18409 	}
18410       if (bitmask)
18411 	printf (", ");
18412     }
18413 }
18414 
18415 static void
decode_aarch64_feature_1_and(unsigned int bitmask)18416 decode_aarch64_feature_1_and (unsigned int bitmask)
18417 {
18418   while (bitmask)
18419     {
18420       unsigned int bit = bitmask & (- bitmask);
18421 
18422       bitmask &= ~ bit;
18423       switch (bit)
18424 	{
18425 	case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
18426 	  printf ("BTI");
18427 	  break;
18428 
18429 	case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
18430 	  printf ("PAC");
18431 	  break;
18432 
18433 	default:
18434 	  printf (_("<unknown: %x>"), bit);
18435 	  break;
18436 	}
18437       if (bitmask)
18438 	printf (", ");
18439     }
18440 }
18441 
18442 static void
print_gnu_property_note(Filedata * filedata,Elf_Internal_Note * pnote)18443 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
18444 {
18445   unsigned char * ptr = (unsigned char *) pnote->descdata;
18446   unsigned char * ptr_end = ptr + pnote->descsz;
18447   unsigned int    size = is_32bit_elf ? 4 : 8;
18448 
18449   printf (_("      Properties: "));
18450 
18451   if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
18452     {
18453       printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
18454       return;
18455     }
18456 
18457   while (ptr < ptr_end)
18458     {
18459       unsigned int j;
18460       unsigned int type;
18461       unsigned int datasz;
18462 
18463       if ((size_t) (ptr_end - ptr) < 8)
18464 	{
18465 	  printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
18466 	  break;
18467 	}
18468 
18469       type = byte_get (ptr, 4);
18470       datasz = byte_get (ptr + 4, 4);
18471 
18472       ptr += 8;
18473 
18474       if (datasz > (size_t) (ptr_end - ptr))
18475 	{
18476 	  printf (_("<corrupt type (%#x) datasz: %#x>\n"),
18477 		  type, datasz);
18478 	  break;
18479 	}
18480 
18481       if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
18482 	{
18483 	  if (filedata->file_header.e_machine == EM_X86_64
18484 	      || filedata->file_header.e_machine == EM_IAMCU
18485 	      || filedata->file_header.e_machine == EM_386)
18486 	    {
18487 	      unsigned int bitmask;
18488 
18489 	      if (datasz == 4)
18490 		bitmask = byte_get (ptr, 4);
18491 	      else
18492 		bitmask = 0;
18493 
18494 	      switch (type)
18495 		{
18496 		case GNU_PROPERTY_X86_ISA_1_USED:
18497 		  if (datasz != 4)
18498 		    printf (_("x86 ISA used: <corrupt length: %#x> "),
18499 			    datasz);
18500 		  else
18501 		    {
18502 		      printf ("x86 ISA used: ");
18503 		      decode_x86_isa (bitmask);
18504 		    }
18505 		  goto next;
18506 
18507 		case GNU_PROPERTY_X86_ISA_1_NEEDED:
18508 		  if (datasz != 4)
18509 		    printf (_("x86 ISA needed: <corrupt length: %#x> "),
18510 			    datasz);
18511 		  else
18512 		    {
18513 		      printf ("x86 ISA needed: ");
18514 		      decode_x86_isa (bitmask);
18515 		    }
18516 		  goto next;
18517 
18518 		case GNU_PROPERTY_X86_FEATURE_1_AND:
18519 		  if (datasz != 4)
18520 		    printf (_("x86 feature: <corrupt length: %#x> "),
18521 			    datasz);
18522 		  else
18523 		    {
18524 		      printf ("x86 feature: ");
18525 		      decode_x86_feature_1 (bitmask);
18526 		    }
18527 		  goto next;
18528 
18529 		case GNU_PROPERTY_X86_FEATURE_2_USED:
18530 		  if (datasz != 4)
18531 		    printf (_("x86 feature used: <corrupt length: %#x> "),
18532 			    datasz);
18533 		  else
18534 		    {
18535 		      printf ("x86 feature used: ");
18536 		      decode_x86_feature_2 (bitmask);
18537 		    }
18538 		  goto next;
18539 
18540 		case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
18541 		  if (datasz != 4)
18542 		    printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
18543 		  else
18544 		    {
18545 		      printf ("x86 feature needed: ");
18546 		      decode_x86_feature_2 (bitmask);
18547 		    }
18548 		  goto next;
18549 
18550 		case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
18551 		  if (datasz != 4)
18552 		    printf (_("x86 ISA used: <corrupt length: %#x> "),
18553 			    datasz);
18554 		  else
18555 		    {
18556 		      printf ("x86 ISA used: ");
18557 		      decode_x86_compat_isa (bitmask);
18558 		    }
18559 		  goto next;
18560 
18561 		case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
18562 		  if (datasz != 4)
18563 		    printf (_("x86 ISA needed: <corrupt length: %#x> "),
18564 			    datasz);
18565 		  else
18566 		    {
18567 		      printf ("x86 ISA needed: ");
18568 		      decode_x86_compat_isa (bitmask);
18569 		    }
18570 		  goto next;
18571 
18572 		default:
18573 		  break;
18574 		}
18575 	    }
18576 	  else if (filedata->file_header.e_machine == EM_AARCH64)
18577 	    {
18578 	      if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
18579 		{
18580 		  printf ("AArch64 feature: ");
18581 		  if (datasz != 4)
18582 		    printf (_("<corrupt length: %#x> "), datasz);
18583 		  else
18584 		    decode_aarch64_feature_1_and (byte_get (ptr, 4));
18585 		  goto next;
18586 		}
18587 	    }
18588 	}
18589       else
18590 	{
18591 	  switch (type)
18592 	    {
18593 	    case GNU_PROPERTY_STACK_SIZE:
18594 	      printf (_("stack size: "));
18595 	      if (datasz != size)
18596 		printf (_("<corrupt length: %#x> "), datasz);
18597 	      else
18598 		printf ("%#lx", (unsigned long) byte_get (ptr, size));
18599 	      goto next;
18600 
18601 	    case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
18602 	      printf ("no copy on protected ");
18603 	      if (datasz)
18604 		printf (_("<corrupt length: %#x> "), datasz);
18605 	      goto next;
18606 
18607 	    default:
18608 	      break;
18609 	    }
18610 	}
18611 
18612       if (type < GNU_PROPERTY_LOPROC)
18613 	printf (_("<unknown type %#x data: "), type);
18614       else if (type < GNU_PROPERTY_LOUSER)
18615 	printf (_("<procesor-specific type %#x data: "), type);
18616       else
18617 	printf (_("<application-specific type %#x data: "), type);
18618       for (j = 0; j < datasz; ++j)
18619 	printf ("%02x ", ptr[j] & 0xff);
18620       printf (">");
18621 
18622     next:
18623       ptr += ((datasz + (size - 1)) & ~ (size - 1));
18624       if (ptr == ptr_end)
18625 	break;
18626 
18627       if (do_wide)
18628 	printf (", ");
18629       else
18630 	printf ("\n\t");
18631     }
18632 
18633   printf ("\n");
18634 }
18635 
18636 static bfd_boolean
print_gnu_note(Filedata * filedata,Elf_Internal_Note * pnote)18637 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
18638 {
18639   /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type ().  */
18640   switch (pnote->type)
18641     {
18642     case NT_GNU_BUILD_ID:
18643       {
18644 	unsigned long i;
18645 
18646 	printf (_("    Build ID: "));
18647 	for (i = 0; i < pnote->descsz; ++i)
18648 	  printf ("%02x", pnote->descdata[i] & 0xff);
18649 	printf ("\n");
18650       }
18651       break;
18652 
18653     case NT_GNU_ABI_TAG:
18654       {
18655 	unsigned long os, major, minor, subminor;
18656 	const char *osname;
18657 
18658 	/* PR 17531: file: 030-599401-0.004.  */
18659 	if (pnote->descsz < 16)
18660 	  {
18661 	    printf (_("    <corrupt GNU_ABI_TAG>\n"));
18662 	    break;
18663 	  }
18664 
18665 	os = byte_get ((unsigned char *) pnote->descdata, 4);
18666 	major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
18667 	minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
18668 	subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
18669 
18670 	switch (os)
18671 	  {
18672 	  case GNU_ABI_TAG_LINUX:
18673 	    osname = "Linux";
18674 	    break;
18675 	  case GNU_ABI_TAG_HURD:
18676 	    osname = "Hurd";
18677 	    break;
18678 	  case GNU_ABI_TAG_SOLARIS:
18679 	    osname = "Solaris";
18680 	    break;
18681 	  case GNU_ABI_TAG_FREEBSD:
18682 	    osname = "FreeBSD";
18683 	    break;
18684 	  case GNU_ABI_TAG_NETBSD:
18685 	    osname = "NetBSD";
18686 	    break;
18687 	  case GNU_ABI_TAG_SYLLABLE:
18688 	    osname = "Syllable";
18689 	    break;
18690 	  case GNU_ABI_TAG_NACL:
18691 	    osname = "NaCl";
18692 	    break;
18693 	  default:
18694 	    osname = "Unknown";
18695 	    break;
18696 	  }
18697 
18698 	printf (_("    OS: %s, ABI: %ld.%ld.%ld\n"), osname,
18699 		major, minor, subminor);
18700       }
18701       break;
18702 
18703     case NT_GNU_GOLD_VERSION:
18704       {
18705 	unsigned long i;
18706 
18707 	printf (_("    Version: "));
18708 	for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
18709 	  printf ("%c", pnote->descdata[i]);
18710 	printf ("\n");
18711       }
18712       break;
18713 
18714     case NT_GNU_HWCAP:
18715       {
18716 	unsigned long num_entries, mask;
18717 
18718 	/* Hardware capabilities information.  Word 0 is the number of entries.
18719 	   Word 1 is a bitmask of enabled entries.  The rest of the descriptor
18720 	   is a series of entries, where each entry is a single byte followed
18721 	   by a nul terminated string.  The byte gives the bit number to test
18722 	   if enabled in the bitmask.  */
18723 	printf (_("      Hardware Capabilities: "));
18724 	if (pnote->descsz < 8)
18725 	  {
18726 	    error (_("<corrupt GNU_HWCAP>\n"));
18727 	    return FALSE;
18728 	  }
18729 	num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
18730 	mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
18731 	printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
18732 	/* FIXME: Add code to display the entries... */
18733       }
18734       break;
18735 
18736     case NT_GNU_PROPERTY_TYPE_0:
18737       print_gnu_property_note (filedata, pnote);
18738       break;
18739 
18740     default:
18741       /* Handle unrecognised types.  An error message should have already been
18742 	 created by get_gnu_elf_note_type(), so all that we need to do is to
18743 	 display the data.  */
18744       {
18745 	unsigned long i;
18746 
18747 	printf (_("    Description data: "));
18748 	for (i = 0; i < pnote->descsz; ++i)
18749 	  printf ("%02x ", pnote->descdata[i] & 0xff);
18750 	printf ("\n");
18751       }
18752       break;
18753     }
18754 
18755   return TRUE;
18756 }
18757 
18758 static const char *
get_v850_elf_note_type(enum v850_notes n_type)18759 get_v850_elf_note_type (enum v850_notes n_type)
18760 {
18761   static char buff[64];
18762 
18763   switch (n_type)
18764     {
18765     case V850_NOTE_ALIGNMENT:  return _("Alignment of 8-byte objects");
18766     case V850_NOTE_DATA_SIZE:  return _("Sizeof double and long double");
18767     case V850_NOTE_FPU_INFO:   return _("Type of FPU support needed");
18768     case V850_NOTE_SIMD_INFO:  return _("Use of SIMD instructions");
18769     case V850_NOTE_CACHE_INFO: return _("Use of cache");
18770     case V850_NOTE_MMU_INFO:   return _("Use of MMU");
18771     default:
18772       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
18773       return buff;
18774     }
18775 }
18776 
18777 static bfd_boolean
print_v850_note(Elf_Internal_Note * pnote)18778 print_v850_note (Elf_Internal_Note * pnote)
18779 {
18780   unsigned int val;
18781 
18782   if (pnote->descsz != 4)
18783     return FALSE;
18784 
18785   val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
18786 
18787   if (val == 0)
18788     {
18789       printf (_("not set\n"));
18790       return TRUE;
18791     }
18792 
18793   switch (pnote->type)
18794     {
18795     case V850_NOTE_ALIGNMENT:
18796       switch (val)
18797 	{
18798 	case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return TRUE;
18799 	case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return TRUE;
18800 	}
18801       break;
18802 
18803     case V850_NOTE_DATA_SIZE:
18804       switch (val)
18805 	{
18806 	case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return TRUE;
18807 	case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return TRUE;
18808 	}
18809       break;
18810 
18811     case V850_NOTE_FPU_INFO:
18812       switch (val)
18813 	{
18814 	case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return TRUE;
18815 	case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return TRUE;
18816 	}
18817       break;
18818 
18819     case V850_NOTE_MMU_INFO:
18820     case V850_NOTE_CACHE_INFO:
18821     case V850_NOTE_SIMD_INFO:
18822       if (val == EF_RH850_SIMD)
18823 	{
18824 	  printf (_("yes\n"));
18825 	  return TRUE;
18826 	}
18827       break;
18828 
18829     default:
18830       /* An 'unknown note type' message will already have been displayed.  */
18831       break;
18832     }
18833 
18834   printf (_("unknown value: %x\n"), val);
18835   return FALSE;
18836 }
18837 
18838 static bfd_boolean
process_netbsd_elf_note(Elf_Internal_Note * pnote)18839 process_netbsd_elf_note (Elf_Internal_Note * pnote)
18840 {
18841   unsigned int version;
18842 
18843   switch (pnote->type)
18844     {
18845     case NT_NETBSD_IDENT:
18846       if (pnote->descsz < 1)
18847 	break;
18848       version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
18849       if ((version / 10000) % 100)
18850 	printf ("  NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
18851 		version, version / 100000000, (version / 1000000) % 100,
18852 		(version / 10000) % 100 > 26 ? "Z" : "",
18853 		'A' + (version / 10000) % 26);
18854       else
18855 	printf ("  NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
18856 		version, version / 100000000, (version / 1000000) % 100,
18857 		(version / 100) % 100);
18858       return TRUE;
18859 
18860     case NT_NETBSD_MARCH:
18861       printf ("  NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
18862 	      pnote->descdata);
18863       return TRUE;
18864 
18865 #ifdef   NT_NETBSD_PAX
18866     case NT_NETBSD_PAX:
18867       if (pnote->descsz < 1)
18868 	break;
18869       version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
18870       printf ("  NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
18871 	      ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
18872 	      ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
18873 	      ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
18874 	      ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
18875 	      ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
18876 	      ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
18877       return TRUE;
18878 #endif
18879     }
18880 
18881   printf ("  NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
18882 	  pnote->descsz, pnote->type);
18883   return FALSE;
18884 }
18885 
18886 static const char *
get_freebsd_elfcore_note_type(Filedata * filedata,unsigned e_type)18887 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
18888 {
18889   switch (e_type)
18890     {
18891     case NT_FREEBSD_THRMISC:
18892       return _("NT_THRMISC (thrmisc structure)");
18893     case NT_FREEBSD_PROCSTAT_PROC:
18894       return _("NT_PROCSTAT_PROC (proc data)");
18895     case NT_FREEBSD_PROCSTAT_FILES:
18896       return _("NT_PROCSTAT_FILES (files data)");
18897     case NT_FREEBSD_PROCSTAT_VMMAP:
18898       return _("NT_PROCSTAT_VMMAP (vmmap data)");
18899     case NT_FREEBSD_PROCSTAT_GROUPS:
18900       return _("NT_PROCSTAT_GROUPS (groups data)");
18901     case NT_FREEBSD_PROCSTAT_UMASK:
18902       return _("NT_PROCSTAT_UMASK (umask data)");
18903     case NT_FREEBSD_PROCSTAT_RLIMIT:
18904       return _("NT_PROCSTAT_RLIMIT (rlimit data)");
18905     case NT_FREEBSD_PROCSTAT_OSREL:
18906       return _("NT_PROCSTAT_OSREL (osreldate data)");
18907     case NT_FREEBSD_PROCSTAT_PSSTRINGS:
18908       return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
18909     case NT_FREEBSD_PROCSTAT_AUXV:
18910       return _("NT_PROCSTAT_AUXV (auxv data)");
18911     case NT_FREEBSD_PTLWPINFO:
18912       return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
18913     }
18914   return get_note_type (filedata, e_type);
18915 }
18916 
18917 static const char *
get_netbsd_elfcore_note_type(Filedata * filedata,unsigned e_type)18918 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
18919 {
18920   static char buff[64];
18921 
18922   switch (e_type)
18923     {
18924     case NT_NETBSDCORE_PROCINFO:
18925       /* NetBSD core "procinfo" structure.  */
18926       return _("NetBSD procinfo structure");
18927 
18928 #ifdef NT_NETBSDCORE_AUXV
18929     case NT_NETBSDCORE_AUXV:
18930       return _("NetBSD ELF auxiliary vector data");
18931 #endif
18932 
18933 #ifdef NT_NETBSDCORE_LWPSTATUS
18934     case NT_NETBSDCORE_LWPSTATUS:
18935       return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
18936 #endif
18937 
18938     default:
18939       /* As of Jan 2020 there are no other machine-independent notes
18940 	 defined for NetBSD core files.  If the note type is less
18941 	 than the start of the machine-dependent note types, we don't
18942 	 understand it.  */
18943 
18944       if (e_type < NT_NETBSDCORE_FIRSTMACH)
18945 	{
18946 	  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18947 	  return buff;
18948 	}
18949       break;
18950     }
18951 
18952   switch (filedata->file_header.e_machine)
18953     {
18954     /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
18955        and PT_GETFPREGS == mach+2.  */
18956 
18957     case EM_OLD_ALPHA:
18958     case EM_ALPHA:
18959     case EM_SPARC:
18960     case EM_SPARC32PLUS:
18961     case EM_SPARCV9:
18962       switch (e_type)
18963 	{
18964 	case NT_NETBSDCORE_FIRSTMACH + 0:
18965 	  return _("PT_GETREGS (reg structure)");
18966 	case NT_NETBSDCORE_FIRSTMACH + 2:
18967 	  return _("PT_GETFPREGS (fpreg structure)");
18968 	default:
18969 	  break;
18970 	}
18971       break;
18972 
18973     /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
18974        There's also old PT___GETREGS40 == mach + 1 for old reg
18975        structure which lacks GBR.  */
18976     case EM_SH:
18977       switch (e_type)
18978 	{
18979 	case NT_NETBSDCORE_FIRSTMACH + 1:
18980 	  return _("PT___GETREGS40 (old reg structure)");
18981 	case NT_NETBSDCORE_FIRSTMACH + 3:
18982 	  return _("PT_GETREGS (reg structure)");
18983 	case NT_NETBSDCORE_FIRSTMACH + 5:
18984 	  return _("PT_GETFPREGS (fpreg structure)");
18985 	default:
18986 	  break;
18987 	}
18988       break;
18989 
18990     /* On all other arch's, PT_GETREGS == mach+1 and
18991        PT_GETFPREGS == mach+3.  */
18992     default:
18993       switch (e_type)
18994 	{
18995 	case NT_NETBSDCORE_FIRSTMACH + 1:
18996 	  return _("PT_GETREGS (reg structure)");
18997 	case NT_NETBSDCORE_FIRSTMACH + 3:
18998 	  return _("PT_GETFPREGS (fpreg structure)");
18999 	default:
19000 	  break;
19001 	}
19002     }
19003 
19004   snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
19005 	    e_type - NT_NETBSDCORE_FIRSTMACH);
19006   return buff;
19007 }
19008 
19009 static const char *
get_stapsdt_note_type(unsigned e_type)19010 get_stapsdt_note_type (unsigned e_type)
19011 {
19012   static char buff[64];
19013 
19014   switch (e_type)
19015     {
19016     case NT_STAPSDT:
19017       return _("NT_STAPSDT (SystemTap probe descriptors)");
19018 
19019     default:
19020       break;
19021     }
19022 
19023   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19024   return buff;
19025 }
19026 
19027 static bfd_boolean
print_stapsdt_note(Elf_Internal_Note * pnote)19028 print_stapsdt_note (Elf_Internal_Note *pnote)
19029 {
19030   size_t len, maxlen;
19031   unsigned long addr_size = is_32bit_elf ? 4 : 8;
19032   char *data = pnote->descdata;
19033   char *data_end = pnote->descdata + pnote->descsz;
19034   bfd_vma pc, base_addr, semaphore;
19035   char *provider, *probe, *arg_fmt;
19036 
19037   if (pnote->descsz < (addr_size * 3))
19038     goto stapdt_note_too_small;
19039 
19040   pc = byte_get ((unsigned char *) data, addr_size);
19041   data += addr_size;
19042 
19043   base_addr = byte_get ((unsigned char *) data, addr_size);
19044   data += addr_size;
19045 
19046   semaphore = byte_get ((unsigned char *) data, addr_size);
19047   data += addr_size;
19048 
19049   if (data >= data_end)
19050     goto stapdt_note_too_small;
19051   maxlen = data_end - data;
19052   len = strnlen (data, maxlen);
19053   if (len < maxlen)
19054     {
19055       provider = data;
19056       data += len + 1;
19057     }
19058   else
19059     goto stapdt_note_too_small;
19060 
19061   if (data >= data_end)
19062     goto stapdt_note_too_small;
19063   maxlen = data_end - data;
19064   len = strnlen (data, maxlen);
19065   if (len < maxlen)
19066     {
19067       probe = data;
19068       data += len + 1;
19069     }
19070   else
19071     goto stapdt_note_too_small;
19072 
19073   if (data >= data_end)
19074     goto stapdt_note_too_small;
19075   maxlen = data_end - data;
19076   len = strnlen (data, maxlen);
19077   if (len < maxlen)
19078     {
19079       arg_fmt = data;
19080       data += len + 1;
19081     }
19082   else
19083     goto stapdt_note_too_small;
19084 
19085   printf (_("    Provider: %s\n"), provider);
19086   printf (_("    Name: %s\n"), probe);
19087   printf (_("    Location: "));
19088   print_vma (pc, FULL_HEX);
19089   printf (_(", Base: "));
19090   print_vma (base_addr, FULL_HEX);
19091   printf (_(", Semaphore: "));
19092   print_vma (semaphore, FULL_HEX);
19093   printf ("\n");
19094   printf (_("    Arguments: %s\n"), arg_fmt);
19095 
19096   return data == data_end;
19097 
19098  stapdt_note_too_small:
19099   printf (_("  <corrupt - note is too small>\n"));
19100   error (_("corrupt stapdt note - the data size is too small\n"));
19101   return FALSE;
19102 }
19103 
19104 static const char *
get_ia64_vms_note_type(unsigned e_type)19105 get_ia64_vms_note_type (unsigned e_type)
19106 {
19107   static char buff[64];
19108 
19109   switch (e_type)
19110     {
19111     case NT_VMS_MHD:
19112       return _("NT_VMS_MHD (module header)");
19113     case NT_VMS_LNM:
19114       return _("NT_VMS_LNM (language name)");
19115     case NT_VMS_SRC:
19116       return _("NT_VMS_SRC (source files)");
19117     case NT_VMS_TITLE:
19118       return "NT_VMS_TITLE";
19119     case NT_VMS_EIDC:
19120       return _("NT_VMS_EIDC (consistency check)");
19121     case NT_VMS_FPMODE:
19122       return _("NT_VMS_FPMODE (FP mode)");
19123     case NT_VMS_LINKTIME:
19124       return "NT_VMS_LINKTIME";
19125     case NT_VMS_IMGNAM:
19126       return _("NT_VMS_IMGNAM (image name)");
19127     case NT_VMS_IMGID:
19128       return _("NT_VMS_IMGID (image id)");
19129     case NT_VMS_LINKID:
19130       return _("NT_VMS_LINKID (link id)");
19131     case NT_VMS_IMGBID:
19132       return _("NT_VMS_IMGBID (build id)");
19133     case NT_VMS_GSTNAM:
19134       return _("NT_VMS_GSTNAM (sym table name)");
19135     case NT_VMS_ORIG_DYN:
19136       return "NT_VMS_ORIG_DYN";
19137     case NT_VMS_PATCHTIME:
19138       return "NT_VMS_PATCHTIME";
19139     default:
19140       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19141       return buff;
19142     }
19143 }
19144 
19145 static bfd_boolean
print_ia64_vms_note(Elf_Internal_Note * pnote)19146 print_ia64_vms_note (Elf_Internal_Note * pnote)
19147 {
19148   int maxlen = pnote->descsz;
19149 
19150   if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
19151     goto desc_size_fail;
19152 
19153   switch (pnote->type)
19154     {
19155     case NT_VMS_MHD:
19156       if (maxlen <= 36)
19157 	goto desc_size_fail;
19158 
19159       int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
19160 
19161       printf (_("    Creation date  : %.17s\n"), pnote->descdata);
19162       printf (_("    Last patch date: %.17s\n"), pnote->descdata + 17);
19163       if (l + 34 < maxlen)
19164 	{
19165 	  printf (_("    Module name    : %s\n"), pnote->descdata + 34);
19166 	  if (l + 35 < maxlen)
19167 	    printf (_("    Module version : %s\n"), pnote->descdata + 34 + l + 1);
19168 	  else
19169 	    printf (_("    Module version : <missing>\n"));
19170 	}
19171       else
19172 	{
19173 	  printf (_("    Module name    : <missing>\n"));
19174 	  printf (_("    Module version : <missing>\n"));
19175 	}
19176       break;
19177 
19178     case NT_VMS_LNM:
19179       printf (_("   Language: %.*s\n"), maxlen, pnote->descdata);
19180       break;
19181 
19182 #ifdef BFD64
19183     case NT_VMS_FPMODE:
19184       printf (_("   Floating Point mode: "));
19185       if (maxlen < 8)
19186 	goto desc_size_fail;
19187       /* FIXME: Generate an error if descsz > 8 ?  */
19188 
19189       printf ("0x%016" BFD_VMA_FMT "x\n",
19190 	      (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
19191       break;
19192 
19193     case NT_VMS_LINKTIME:
19194       printf (_("   Link time: "));
19195       if (maxlen < 8)
19196 	goto desc_size_fail;
19197       /* FIXME: Generate an error if descsz > 8 ?  */
19198 
19199       print_vms_time
19200 	((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
19201       printf ("\n");
19202       break;
19203 
19204     case NT_VMS_PATCHTIME:
19205       printf (_("   Patch time: "));
19206       if (maxlen < 8)
19207 	goto desc_size_fail;
19208       /* FIXME: Generate an error if descsz > 8 ?  */
19209 
19210       print_vms_time
19211 	((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
19212       printf ("\n");
19213       break;
19214 
19215     case NT_VMS_ORIG_DYN:
19216       if (maxlen < 34)
19217 	goto desc_size_fail;
19218 
19219       printf (_("   Major id: %u,  minor id: %u\n"),
19220               (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
19221               (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
19222       printf (_("   Last modified  : "));
19223       print_vms_time
19224         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
19225       printf (_("\n   Link flags  : "));
19226       printf ("0x%016" BFD_VMA_FMT "x\n",
19227               (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
19228       printf (_("   Header flags: 0x%08x\n"),
19229               (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
19230       printf (_("   Image id    : %.*s\n"), maxlen - 32, pnote->descdata + 32);
19231       break;
19232 #endif
19233 
19234     case NT_VMS_IMGNAM:
19235       printf (_("    Image name: %.*s\n"), maxlen, pnote->descdata);
19236       break;
19237 
19238     case NT_VMS_GSTNAM:
19239       printf (_("    Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
19240       break;
19241 
19242     case NT_VMS_IMGID:
19243       printf (_("    Image id: %.*s\n"), maxlen, pnote->descdata);
19244       break;
19245 
19246     case NT_VMS_LINKID:
19247       printf (_("    Linker id: %.*s\n"), maxlen, pnote->descdata);
19248       break;
19249 
19250     default:
19251       return FALSE;
19252     }
19253 
19254   return TRUE;
19255 
19256  desc_size_fail:
19257   printf (_("  <corrupt - data size is too small>\n"));
19258   error (_("corrupt IA64 note: data size is too small\n"));
19259   return FALSE;
19260 }
19261 
19262 struct build_attr_cache {
19263   Filedata *filedata;
19264   char *strtab;
19265   unsigned long strtablen;
19266   Elf_Internal_Sym *symtab;
19267   unsigned long nsyms;
19268 } ba_cache;
19269 
19270 /* Find the symbol associated with a build attribute that is attached
19271    to address OFFSET.  If PNAME is non-NULL then store the name of
19272    the symbol (if found) in the provided pointer,  Returns NULL if a
19273    symbol could not be found.  */
19274 
19275 static Elf_Internal_Sym *
get_symbol_for_build_attribute(Filedata * filedata,unsigned long offset,bfd_boolean is_open_attr,const char ** pname)19276 get_symbol_for_build_attribute (Filedata *       filedata,
19277 				unsigned long    offset,
19278 				bfd_boolean      is_open_attr,
19279 				const char **    pname)
19280 {
19281   Elf_Internal_Sym *saved_sym = NULL;
19282   Elf_Internal_Sym *sym;
19283 
19284   if (filedata->section_headers != NULL
19285       && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
19286     {
19287       Elf_Internal_Shdr * symsec;
19288 
19289       free (ba_cache.strtab);
19290       ba_cache.strtab = NULL;
19291       free (ba_cache.symtab);
19292       ba_cache.symtab = NULL;
19293 
19294       /* Load the symbol and string sections.  */
19295       for (symsec = filedata->section_headers;
19296 	   symsec < filedata->section_headers + filedata->file_header.e_shnum;
19297 	   symsec ++)
19298 	{
19299 	  if (symsec->sh_type == SHT_SYMTAB
19300 	      && get_symtab (filedata, symsec,
19301 			     &ba_cache.symtab, &ba_cache.nsyms,
19302 			     &ba_cache.strtab, &ba_cache.strtablen))
19303 	    break;
19304 	}
19305       ba_cache.filedata = filedata;
19306     }
19307 
19308   if (ba_cache.symtab == NULL)
19309     return NULL;
19310 
19311   /* Find a symbol whose value matches offset.  */
19312   for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
19313     if (sym->st_value == offset)
19314       {
19315 	if (sym->st_name >= ba_cache.strtablen)
19316 	  /* Huh ?  This should not happen.  */
19317 	  continue;
19318 
19319 	if (ba_cache.strtab[sym->st_name] == 0)
19320 	  continue;
19321 
19322 	/* The AArch64 and ARM architectures define mapping symbols
19323 	   (eg $d, $x, $t) which we want to ignore.  */
19324 	if (ba_cache.strtab[sym->st_name] == '$'
19325 	    && ba_cache.strtab[sym->st_name + 1] != 0
19326 	    && ba_cache.strtab[sym->st_name + 2] == 0)
19327 	  continue;
19328 
19329 	if (is_open_attr)
19330 	  {
19331 	    /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
19332 	       and FILE or OBJECT symbols over NOTYPE symbols.  We skip
19333 	       FUNC symbols entirely.  */
19334 	    switch (ELF_ST_TYPE (sym->st_info))
19335 	      {
19336 	      case STT_OBJECT:
19337 	      case STT_FILE:
19338 		saved_sym = sym;
19339 		if (sym->st_size)
19340 		  {
19341 		    /* If the symbol has a size associated
19342 		       with it then we can stop searching.  */
19343 		    sym = ba_cache.symtab + ba_cache.nsyms;
19344 		  }
19345 		continue;
19346 
19347 	      case STT_FUNC:
19348 		/* Ignore function symbols.  */
19349 		continue;
19350 
19351 	      default:
19352 		break;
19353 	      }
19354 
19355 	    switch (ELF_ST_BIND (sym->st_info))
19356 	      {
19357 	      case STB_GLOBAL:
19358 		if (saved_sym == NULL
19359 		    || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
19360 		  saved_sym = sym;
19361 		break;
19362 
19363 	      case STB_LOCAL:
19364 		if (saved_sym == NULL)
19365 		  saved_sym = sym;
19366 		break;
19367 
19368 	      default:
19369 		break;
19370 	      }
19371 	  }
19372 	else
19373 	  {
19374 	    if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
19375 	      continue;
19376 
19377 	    saved_sym = sym;
19378 	    break;
19379 	  }
19380       }
19381 
19382   if (saved_sym && pname)
19383     * pname = ba_cache.strtab + saved_sym->st_name;
19384 
19385   return saved_sym;
19386 }
19387 
19388 /* Returns true iff addr1 and addr2 are in the same section.  */
19389 
19390 static bfd_boolean
same_section(Filedata * filedata,unsigned long addr1,unsigned long addr2)19391 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
19392 {
19393   Elf_Internal_Shdr * a1;
19394   Elf_Internal_Shdr * a2;
19395 
19396   a1 = find_section_by_address (filedata, addr1);
19397   a2 = find_section_by_address (filedata, addr2);
19398 
19399   return a1 == a2 && a1 != NULL;
19400 }
19401 
19402 static bfd_boolean
print_gnu_build_attribute_description(Elf_Internal_Note * pnote,Filedata * filedata)19403 print_gnu_build_attribute_description (Elf_Internal_Note *  pnote,
19404 				       Filedata *           filedata)
19405 {
19406   static unsigned long  global_offset = 0;
19407   static unsigned long  global_end = 0;
19408   static unsigned long  func_offset = 0;
19409   static unsigned long  func_end = 0;
19410 
19411   Elf_Internal_Sym *    sym;
19412   const char *          name;
19413   unsigned long         start;
19414   unsigned long         end;
19415   bfd_boolean           is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
19416 
19417   switch (pnote->descsz)
19418     {
19419     case 0:
19420       /* A zero-length description means that the range of
19421 	 the previous note of the same type should be used.  */
19422       if (is_open_attr)
19423 	{
19424 	  if (global_end > global_offset)
19425 	    printf (_("    Applies to region from %#lx to %#lx\n"),
19426 		    global_offset, global_end);
19427 	  else
19428 	    printf (_("    Applies to region from %#lx\n"), global_offset);
19429 	}
19430       else
19431 	{
19432 	  if (func_end > func_offset)
19433 	    printf (_("    Applies to region from %#lx to %#lx\n"), func_offset, func_end);
19434 	  else
19435 	    printf (_("    Applies to region from %#lx\n"), func_offset);
19436 	}
19437       return TRUE;
19438 
19439     case 4:
19440       start = byte_get ((unsigned char *) pnote->descdata, 4);
19441       end = 0;
19442       break;
19443 
19444     case 8:
19445       if (is_32bit_elf)
19446 	{
19447 	  /* FIXME: We should check that version 3+ notes are being used here...  */
19448 	  start = byte_get ((unsigned char *) pnote->descdata, 4);
19449 	  end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19450 	}
19451       else
19452 	{
19453 	  start = byte_get ((unsigned char *) pnote->descdata, 8);
19454 	  end = 0;
19455 	}
19456       break;
19457 
19458     case 16:
19459       start = byte_get ((unsigned char *) pnote->descdata, 8);
19460       end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
19461       break;
19462 
19463     default:
19464       error (_("    <invalid description size: %lx>\n"), pnote->descsz);
19465       printf (_("    <invalid descsz>"));
19466       return FALSE;
19467     }
19468 
19469   name = NULL;
19470   sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
19471   /* As of version 5 of the annobin plugin, filename symbols are biased by 2
19472      in order to avoid them being confused with the start address of the
19473      first function in the file...  */
19474   if (sym == NULL && is_open_attr)
19475     sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
19476 					  & name);
19477 
19478   if (end == 0 && sym != NULL && sym->st_size > 0)
19479     end = start + sym->st_size;
19480 
19481   if (is_open_attr)
19482     {
19483       /* FIXME: Need to properly allow for section alignment.
19484 	 16 is just the alignment used on x86_64.  */
19485       if (global_end > 0
19486 	  && start > BFD_ALIGN (global_end, 16)
19487 	  /* Build notes are not guaranteed to be organised in order of
19488 	     increasing address, but we should find the all of the notes
19489 	     for one section in the same place.  */
19490 	  && same_section (filedata, start, global_end))
19491 	warn (_("Gap in build notes detected from %#lx to %#lx\n"),
19492 	      global_end + 1, start - 1);
19493 
19494       printf (_("    Applies to region from %#lx"), start);
19495       global_offset = start;
19496 
19497       if (end)
19498 	{
19499 	  printf (_(" to %#lx"), end);
19500 	  global_end = end;
19501 	}
19502     }
19503   else
19504     {
19505       printf (_("    Applies to region from %#lx"), start);
19506       func_offset = start;
19507 
19508       if (end)
19509 	{
19510 	  printf (_(" to %#lx"), end);
19511 	  func_end = end;
19512 	}
19513     }
19514 
19515   if (sym && name)
19516     printf (_(" (%s)"), name);
19517 
19518   printf ("\n");
19519   return TRUE;
19520 }
19521 
19522 static bfd_boolean
print_gnu_build_attribute_name(Elf_Internal_Note * pnote)19523 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
19524 {
19525   static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
19526   static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
19527   static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
19528   char         name_type;
19529   char         name_attribute;
19530   const char * expected_types;
19531   const char * name = pnote->namedata;
19532   const char * text;
19533   signed int   left;
19534 
19535   if (name == NULL || pnote->namesz < 2)
19536     {
19537       error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
19538       print_symbol (-20, _("  <corrupt name>"));
19539       return FALSE;
19540     }
19541 
19542   if (do_wide)
19543     left = 28;
19544   else
19545     left = 20;
19546 
19547   /* Version 2 of the spec adds a "GA" prefix to the name field.  */
19548   if (name[0] == 'G' && name[1] == 'A')
19549     {
19550       if (pnote->namesz < 4)
19551 	{
19552 	  error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
19553 	  print_symbol (-20, _("  <corrupt name>"));
19554 	  return FALSE;
19555 	}
19556 
19557       printf ("GA");
19558       name += 2;
19559       left -= 2;
19560     }
19561 
19562   switch ((name_type = * name))
19563     {
19564     case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
19565     case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
19566     case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
19567     case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
19568       printf ("%c", * name);
19569       left --;
19570       break;
19571     default:
19572       error (_("unrecognised attribute type in name field: %d\n"), name_type);
19573       print_symbol (-20, _("<unknown name type>"));
19574       return FALSE;
19575     }
19576 
19577   ++ name;
19578   text = NULL;
19579 
19580   switch ((name_attribute = * name))
19581     {
19582     case GNU_BUILD_ATTRIBUTE_VERSION:
19583       text = _("<version>");
19584       expected_types = string_expected;
19585       ++ name;
19586       break;
19587     case GNU_BUILD_ATTRIBUTE_STACK_PROT:
19588       text = _("<stack prot>");
19589       expected_types = "!+*";
19590       ++ name;
19591       break;
19592     case GNU_BUILD_ATTRIBUTE_RELRO:
19593       text = _("<relro>");
19594       expected_types = bool_expected;
19595       ++ name;
19596       break;
19597     case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
19598       text = _("<stack size>");
19599       expected_types = number_expected;
19600       ++ name;
19601       break;
19602     case GNU_BUILD_ATTRIBUTE_TOOL:
19603       text = _("<tool>");
19604       expected_types = string_expected;
19605       ++ name;
19606       break;
19607     case GNU_BUILD_ATTRIBUTE_ABI:
19608       text = _("<ABI>");
19609       expected_types = "$*";
19610       ++ name;
19611       break;
19612     case GNU_BUILD_ATTRIBUTE_PIC:
19613       text = _("<PIC>");
19614       expected_types = number_expected;
19615       ++ name;
19616       break;
19617     case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
19618       text = _("<short enum>");
19619       expected_types = bool_expected;
19620       ++ name;
19621       break;
19622     default:
19623       if (ISPRINT (* name))
19624 	{
19625 	  int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
19626 
19627 	  if (len > left && ! do_wide)
19628 	    len = left;
19629 	  printf ("%.*s:", len, name);
19630 	  left -= len;
19631 	  name += len;
19632 	}
19633       else
19634 	{
19635 	  static char tmpbuf [128];
19636 
19637 	  error (_("unrecognised byte in name field: %d\n"), * name);
19638 	  sprintf (tmpbuf, _("<unknown:_%d>"), * name);
19639 	  text = tmpbuf;
19640 	  name ++;
19641 	}
19642       expected_types = "*$!+";
19643       break;
19644     }
19645 
19646   if (text)
19647     left -= printf ("%s", text);
19648 
19649   if (strchr (expected_types, name_type) == NULL)
19650     warn (_("attribute does not have an expected type (%c)\n"), name_type);
19651 
19652   if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
19653     {
19654       error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
19655 	     (unsigned long) pnote->namesz,
19656 	     (long) (name - pnote->namedata));
19657       return FALSE;
19658     }
19659 
19660   if (left < 1 && ! do_wide)
19661     return TRUE;
19662 
19663   switch (name_type)
19664     {
19665     case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
19666       {
19667 	unsigned int        bytes;
19668 	unsigned long long  val = 0;
19669 	unsigned int        shift = 0;
19670 	char *              decoded = NULL;
19671 
19672 	bytes = pnote->namesz - (name - pnote->namedata);
19673 	if (bytes > 0)
19674 	  /* The -1 is because the name field is always 0 terminated, and we
19675 	     want to be able to ensure that the shift in the while loop below
19676 	     will not overflow.  */
19677 	  -- bytes;
19678 
19679 	if (bytes > sizeof (val))
19680 	  {
19681 	    error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
19682 		   bytes);
19683 	    bytes = sizeof (val);
19684 	  }
19685 	/* We do not bother to warn if bytes == 0 as this can
19686 	   happen with some early versions of the gcc plugin.  */
19687 
19688 	while (bytes --)
19689 	  {
19690 	    unsigned long long byte = *name++ & 0xff;
19691 
19692 	    val |= byte << shift;
19693 	    shift += 8;
19694 	  }
19695 
19696 	switch (name_attribute)
19697 	  {
19698 	  case GNU_BUILD_ATTRIBUTE_PIC:
19699 	    switch (val)
19700 	      {
19701 	      case 0: decoded = "static"; break;
19702 	      case 1: decoded = "pic"; break;
19703 	      case 2: decoded = "PIC"; break;
19704 	      case 3: decoded = "pie"; break;
19705 	      case 4: decoded = "PIE"; break;
19706 	      default: break;
19707 	      }
19708 	    break;
19709 	  case GNU_BUILD_ATTRIBUTE_STACK_PROT:
19710 	    switch (val)
19711 	      {
19712 		/* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c.  */
19713 	      case 0: decoded = "off"; break;
19714 	      case 1: decoded = "on"; break;
19715 	      case 2: decoded = "all"; break;
19716 	      case 3: decoded = "strong"; break;
19717 	      case 4: decoded = "explicit"; break;
19718 	      default: break;
19719 	      }
19720 	    break;
19721 	  default:
19722 	    break;
19723 	  }
19724 
19725 	if (decoded != NULL)
19726 	  {
19727 	    print_symbol (-left, decoded);
19728 	    left = 0;
19729 	  }
19730 	else if (val == 0)
19731 	  {
19732 	    printf ("0x0");
19733 	    left -= 3;
19734 	  }
19735 	else
19736 	  {
19737 	    if (do_wide)
19738 	      left -= printf ("0x%llx", val);
19739 	    else
19740 	      left -= printf ("0x%-.*llx", left, val);
19741 	  }
19742       }
19743       break;
19744     case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
19745       left -= print_symbol (- left, name);
19746       break;
19747     case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
19748       left -= print_symbol (- left, "true");
19749       break;
19750     case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
19751       left -= print_symbol (- left, "false");
19752       break;
19753     }
19754 
19755   if (do_wide && left > 0)
19756     printf ("%-*s", left, " ");
19757 
19758   return TRUE;
19759 }
19760 
19761 /* Note that by the ELF standard, the name field is already null byte
19762    terminated, and namesz includes the terminating null byte.
19763    I.E. the value of namesz for the name "FSF" is 4.
19764 
19765    If the value of namesz is zero, there is no name present.  */
19766 
19767 static bfd_boolean
process_note(Elf_Internal_Note * pnote,Filedata * filedata)19768 process_note (Elf_Internal_Note *  pnote,
19769 	      Filedata *           filedata)
19770 {
19771   const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
19772   const char * nt;
19773 
19774   if (pnote->namesz == 0)
19775     /* If there is no note name, then use the default set of
19776        note type strings.  */
19777     nt = get_note_type (filedata, pnote->type);
19778 
19779   else if (const_strneq (pnote->namedata, "GNU"))
19780     /* GNU-specific object file notes.  */
19781     nt = get_gnu_elf_note_type (pnote->type);
19782 
19783   else if (const_strneq (pnote->namedata, "FreeBSD"))
19784     /* FreeBSD-specific core file notes.  */
19785     nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
19786 
19787   else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
19788     /* NetBSD-specific core file notes.  */
19789     nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
19790 
19791   else if (const_strneq (pnote->namedata, "NetBSD"))
19792     /* NetBSD-specific core file notes.  */
19793     return process_netbsd_elf_note (pnote);
19794 
19795   else if (const_strneq (pnote->namedata, "PaX"))
19796     /* NetBSD-specific core file notes.  */
19797     return process_netbsd_elf_note (pnote);
19798 
19799   else if (strneq (pnote->namedata, "SPU/", 4))
19800     {
19801       /* SPU-specific core file notes.  */
19802       nt = pnote->namedata + 4;
19803       name = "SPU";
19804     }
19805 
19806   else if (const_strneq (pnote->namedata, "IPF/VMS"))
19807     /* VMS/ia64-specific file notes.  */
19808     nt = get_ia64_vms_note_type (pnote->type);
19809 
19810   else if (const_strneq (pnote->namedata, "stapsdt"))
19811     nt = get_stapsdt_note_type (pnote->type);
19812 
19813   else
19814     /* Don't recognize this note name; just use the default set of
19815        note type strings.  */
19816     nt = get_note_type (filedata, pnote->type);
19817 
19818   printf ("  ");
19819 
19820   if (((const_strneq (pnote->namedata, "GA")
19821 	&& strchr ("*$!+", pnote->namedata[2]) != NULL)
19822        || strchr ("*$!+", pnote->namedata[0]) != NULL)
19823       && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
19824 	  || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
19825     print_gnu_build_attribute_name (pnote);
19826   else
19827     print_symbol (-20, name);
19828 
19829   if (do_wide)
19830     printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
19831   else
19832     printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
19833 
19834   if (const_strneq (pnote->namedata, "IPF/VMS"))
19835     return print_ia64_vms_note (pnote);
19836   else if (const_strneq (pnote->namedata, "GNU"))
19837     return print_gnu_note (filedata, pnote);
19838   else if (const_strneq (pnote->namedata, "stapsdt"))
19839     return print_stapsdt_note (pnote);
19840   else if (const_strneq (pnote->namedata, "CORE"))
19841     return print_core_note (pnote);
19842   else if (((const_strneq (pnote->namedata, "GA")
19843 	     && strchr ("*$!+", pnote->namedata[2]) != NULL)
19844 	    || strchr ("*$!+", pnote->namedata[0]) != NULL)
19845 	   && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
19846 	       || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
19847     return print_gnu_build_attribute_description (pnote, filedata);
19848 
19849   if (pnote->descsz)
19850     {
19851       unsigned long i;
19852 
19853       printf (_("   description data: "));
19854       for (i = 0; i < pnote->descsz; i++)
19855 	printf ("%02x ", pnote->descdata[i] & 0xff);
19856       if (!do_wide)
19857 	printf ("\n");
19858     }
19859 
19860   if (do_wide)
19861     printf ("\n");
19862 
19863   return TRUE;
19864 }
19865 
19866 static bfd_boolean
process_notes_at(Filedata * filedata,Elf_Internal_Shdr * section,bfd_vma offset,bfd_vma length,bfd_vma align)19867 process_notes_at (Filedata *           filedata,
19868 		  Elf_Internal_Shdr *  section,
19869 		  bfd_vma              offset,
19870 		  bfd_vma              length,
19871 		  bfd_vma              align)
19872 {
19873   Elf_External_Note * pnotes;
19874   Elf_External_Note * external;
19875   char *              end;
19876   bfd_boolean         res = TRUE;
19877 
19878   if (length <= 0)
19879     return FALSE;
19880 
19881   if (section)
19882     {
19883       pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
19884       if (pnotes)
19885 	{
19886 	  if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
19887 	    {
19888 	      free (pnotes);
19889 	      return FALSE;
19890 	    }
19891 	}
19892     }
19893   else
19894     pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
19895 					     _("notes"));
19896 
19897   if (pnotes == NULL)
19898     return FALSE;
19899 
19900   external = pnotes;
19901 
19902   if (section)
19903     printf (_("\nDisplaying notes found in: %s\n"), printable_section_name (filedata, section));
19904   else
19905     printf (_("\nDisplaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
19906 	    (unsigned long) offset, (unsigned long) length);
19907 
19908   /* NB: Some note sections may have alignment value of 0 or 1.  gABI
19909      specifies that notes should be aligned to 4 bytes in 32-bit
19910      objects and to 8 bytes in 64-bit objects.  As a Linux extension,
19911      we also support 4 byte alignment in 64-bit objects.  If section
19912      alignment is less than 4, we treate alignment as 4 bytes.   */
19913   if (align < 4)
19914     align = 4;
19915   else if (align != 4 && align != 8)
19916     {
19917       warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
19918 	    (long) align);
19919       free (pnotes);
19920       return FALSE;
19921     }
19922 
19923   printf (_("  %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
19924 
19925   end = (char *) pnotes + length;
19926   while ((char *) external < end)
19927     {
19928       Elf_Internal_Note inote;
19929       size_t min_notesz;
19930       char * next;
19931       char * temp = NULL;
19932       size_t data_remaining = end - (char *) external;
19933 
19934       if (!is_ia64_vms (filedata))
19935 	{
19936 	  /* PR binutils/15191
19937 	     Make sure that there is enough data to read.  */
19938 	  min_notesz = offsetof (Elf_External_Note, name);
19939 	  if (data_remaining < min_notesz)
19940 	    {
19941 	      warn (ngettext ("Corrupt note: only %ld byte remains, "
19942 			      "not enough for a full note\n",
19943 			      "Corrupt note: only %ld bytes remain, "
19944 			      "not enough for a full note\n",
19945 			      data_remaining),
19946 		    (long) data_remaining);
19947 	      break;
19948 	    }
19949 	  data_remaining -= min_notesz;
19950 
19951 	  inote.type     = BYTE_GET (external->type);
19952 	  inote.namesz   = BYTE_GET (external->namesz);
19953 	  inote.namedata = external->name;
19954 	  inote.descsz   = BYTE_GET (external->descsz);
19955 	  inote.descdata = ((char *) external
19956 			    + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
19957 	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
19958 	  next = ((char *) external
19959 		  + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
19960 	}
19961       else
19962 	{
19963 	  Elf64_External_VMS_Note *vms_external;
19964 
19965 	  /* PR binutils/15191
19966 	     Make sure that there is enough data to read.  */
19967 	  min_notesz = offsetof (Elf64_External_VMS_Note, name);
19968 	  if (data_remaining < min_notesz)
19969 	    {
19970 	      warn (ngettext ("Corrupt note: only %ld byte remains, "
19971 			      "not enough for a full note\n",
19972 			      "Corrupt note: only %ld bytes remain, "
19973 			      "not enough for a full note\n",
19974 			      data_remaining),
19975 		    (long) data_remaining);
19976 	      break;
19977 	    }
19978 	  data_remaining -= min_notesz;
19979 
19980 	  vms_external = (Elf64_External_VMS_Note *) external;
19981 	  inote.type     = BYTE_GET (vms_external->type);
19982 	  inote.namesz   = BYTE_GET (vms_external->namesz);
19983 	  inote.namedata = vms_external->name;
19984 	  inote.descsz   = BYTE_GET (vms_external->descsz);
19985 	  inote.descdata = inote.namedata + align_power (inote.namesz, 3);
19986 	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
19987 	  next = inote.descdata + align_power (inote.descsz, 3);
19988 	}
19989 
19990       /* PR 17531: file: 3443835e.  */
19991       /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4.  */
19992       if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
19993 	  || (size_t) (inote.descdata - inote.namedata) > data_remaining
19994 	  || (size_t) (next - inote.descdata) < inote.descsz
19995 	  || ((size_t) (next - inote.descdata)
19996 	      > data_remaining - (size_t) (inote.descdata - inote.namedata)))
19997 	{
19998 	  warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
19999 		(unsigned long) ((char *) external - (char *) pnotes));
20000 	  warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
20001 		inote.type, inote.namesz, inote.descsz, (int) align);
20002 	  break;
20003 	}
20004 
20005       external = (Elf_External_Note *) next;
20006 
20007       /* Verify that name is null terminated.  It appears that at least
20008 	 one version of Linux (RedHat 6.0) generates corefiles that don't
20009 	 comply with the ELF spec by failing to include the null byte in
20010 	 namesz.  */
20011       if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
20012 	{
20013 	  if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
20014 	    {
20015 	      temp = (char *) malloc (inote.namesz + 1);
20016 	      if (temp == NULL)
20017 		{
20018 		  error (_("Out of memory allocating space for inote name\n"));
20019 		  res = FALSE;
20020 		  break;
20021 		}
20022 
20023 	      memcpy (temp, inote.namedata, inote.namesz);
20024 	      inote.namedata = temp;
20025 	    }
20026 	  inote.namedata[inote.namesz] = 0;
20027 	}
20028 
20029       if (! process_note (& inote, filedata))
20030 	res = FALSE;
20031 
20032       free (temp);
20033       temp = NULL;
20034     }
20035 
20036   free (pnotes);
20037 
20038   return res;
20039 }
20040 
20041 static bfd_boolean
process_corefile_note_segments(Filedata * filedata)20042 process_corefile_note_segments (Filedata * filedata)
20043 {
20044   Elf_Internal_Phdr * segment;
20045   unsigned int i;
20046   bfd_boolean res = TRUE;
20047 
20048   if (! get_program_headers (filedata))
20049     return TRUE;
20050 
20051   for (i = 0, segment = filedata->program_headers;
20052        i < filedata->file_header.e_phnum;
20053        i++, segment++)
20054     {
20055       if (segment->p_type == PT_NOTE)
20056 	if (! process_notes_at (filedata, NULL,
20057 				(bfd_vma) segment->p_offset,
20058 				(bfd_vma) segment->p_filesz,
20059 				(bfd_vma) segment->p_align))
20060 	  res = FALSE;
20061     }
20062 
20063   return res;
20064 }
20065 
20066 static bfd_boolean
process_v850_notes(Filedata * filedata,bfd_vma offset,bfd_vma length)20067 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
20068 {
20069   Elf_External_Note * pnotes;
20070   Elf_External_Note * external;
20071   char * end;
20072   bfd_boolean res = TRUE;
20073 
20074   if (length <= 0)
20075     return FALSE;
20076 
20077   pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20078                                            _("v850 notes"));
20079   if (pnotes == NULL)
20080     return FALSE;
20081 
20082   external = pnotes;
20083   end = (char*) pnotes + length;
20084 
20085   printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
20086 	  (unsigned long) offset, (unsigned long) length);
20087 
20088   while ((char *) external + sizeof (Elf_External_Note) < end)
20089     {
20090       Elf_External_Note * next;
20091       Elf_Internal_Note inote;
20092 
20093       inote.type     = BYTE_GET (external->type);
20094       inote.namesz   = BYTE_GET (external->namesz);
20095       inote.namedata = external->name;
20096       inote.descsz   = BYTE_GET (external->descsz);
20097       inote.descdata = inote.namedata + align_power (inote.namesz, 2);
20098       inote.descpos  = offset + (inote.descdata - (char *) pnotes);
20099 
20100       if (inote.descdata < (char *) pnotes || inote.descdata >= end)
20101 	{
20102 	  warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
20103 	  inote.descdata = inote.namedata;
20104 	  inote.namesz   = 0;
20105 	}
20106 
20107       next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
20108 
20109       if (   ((char *) next > end)
20110 	  || ((char *) next <  (char *) pnotes))
20111 	{
20112 	  warn (_("corrupt descsz found in note at offset 0x%lx\n"),
20113 		(unsigned long) ((char *) external - (char *) pnotes));
20114 	  warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20115 		inote.type, inote.namesz, inote.descsz);
20116 	  break;
20117 	}
20118 
20119       external = next;
20120 
20121       /* Prevent out-of-bounds indexing.  */
20122       if (   inote.namedata + inote.namesz > end
20123 	  || inote.namedata + inote.namesz < inote.namedata)
20124         {
20125           warn (_("corrupt namesz found in note at offset 0x%lx\n"),
20126                 (unsigned long) ((char *) external - (char *) pnotes));
20127           warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20128                 inote.type, inote.namesz, inote.descsz);
20129           break;
20130         }
20131 
20132       printf ("  %s: ", get_v850_elf_note_type (inote.type));
20133 
20134       if (! print_v850_note (& inote))
20135 	{
20136 	  res = FALSE;
20137 	  printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
20138 		  inote.namesz, inote.descsz);
20139 	}
20140     }
20141 
20142   free (pnotes);
20143 
20144   return res;
20145 }
20146 
20147 static bfd_boolean
process_note_sections(Filedata * filedata)20148 process_note_sections (Filedata * filedata)
20149 {
20150   Elf_Internal_Shdr * section;
20151   unsigned long i;
20152   unsigned int n = 0;
20153   bfd_boolean res = TRUE;
20154 
20155   for (i = 0, section = filedata->section_headers;
20156        i < filedata->file_header.e_shnum && section != NULL;
20157        i++, section++)
20158     {
20159       if (section->sh_type == SHT_NOTE)
20160 	{
20161 	  if (! process_notes_at (filedata, section,
20162 				  (bfd_vma) section->sh_offset,
20163 				  (bfd_vma) section->sh_size,
20164 				  (bfd_vma) section->sh_addralign))
20165 	    res = FALSE;
20166 	  n++;
20167 	}
20168 
20169       if ((   filedata->file_header.e_machine == EM_V800
20170 	   || filedata->file_header.e_machine == EM_V850
20171 	   || filedata->file_header.e_machine == EM_CYGNUS_V850)
20172 	  && section->sh_type == SHT_RENESAS_INFO)
20173 	{
20174 	  if (! process_v850_notes (filedata,
20175 				    (bfd_vma) section->sh_offset,
20176 				    (bfd_vma) section->sh_size))
20177 	    res = FALSE;
20178 	  n++;
20179 	}
20180     }
20181 
20182   if (n == 0)
20183     /* Try processing NOTE segments instead.  */
20184     return process_corefile_note_segments (filedata);
20185 
20186   return res;
20187 }
20188 
20189 static bfd_boolean
process_notes(Filedata * filedata)20190 process_notes (Filedata * filedata)
20191 {
20192   /* If we have not been asked to display the notes then do nothing.  */
20193   if (! do_notes)
20194     return TRUE;
20195 
20196   if (filedata->file_header.e_type != ET_CORE)
20197     return process_note_sections (filedata);
20198 
20199   /* No program headers means no NOTE segment.  */
20200   if (filedata->file_header.e_phnum > 0)
20201     return process_corefile_note_segments (filedata);
20202 
20203   printf (_("No note segments present in the core file.\n"));
20204   return TRUE;
20205 }
20206 
20207 static unsigned char *
display_public_gnu_attributes(unsigned char * start,const unsigned char * const end)20208 display_public_gnu_attributes (unsigned char * start,
20209 			       const unsigned char * const end)
20210 {
20211   printf (_("  Unknown GNU attribute: %s\n"), start);
20212 
20213   start += strnlen ((char *) start, end - start);
20214   display_raw_attribute (start, end);
20215 
20216   return (unsigned char *) end;
20217 }
20218 
20219 static unsigned char *
display_generic_attribute(unsigned char * start,unsigned int tag,const unsigned char * const end)20220 display_generic_attribute (unsigned char * start,
20221 			   unsigned int tag,
20222 			   const unsigned char * const end)
20223 {
20224   if (tag == 0)
20225     return (unsigned char *) end;
20226 
20227   return display_tag_value (tag, start, end);
20228 }
20229 
20230 static bfd_boolean
process_arch_specific(Filedata * filedata)20231 process_arch_specific (Filedata * filedata)
20232 {
20233   if (! do_arch)
20234     return TRUE;
20235 
20236   switch (filedata->file_header.e_machine)
20237     {
20238     case EM_ARC:
20239     case EM_ARC_COMPACT:
20240     case EM_ARC_COMPACT2:
20241       return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
20242 				 display_arc_attribute,
20243 				 display_generic_attribute);
20244     case EM_ARM:
20245       return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
20246 				 display_arm_attribute,
20247 				 display_generic_attribute);
20248 
20249     case EM_MIPS:
20250     case EM_MIPS_RS3_LE:
20251       return process_mips_specific (filedata);
20252 
20253     case EM_MSP430:
20254      return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
20255 				display_msp430_attribute,
20256 				display_msp430_gnu_attribute);
20257 
20258     case EM_RISCV:
20259      return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
20260 				display_riscv_attribute,
20261 				display_generic_attribute);
20262 
20263     case EM_NDS32:
20264       return process_nds32_specific (filedata);
20265 
20266     case EM_68K:
20267       return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20268 				 display_m68k_gnu_attribute);
20269 
20270     case EM_PPC:
20271     case EM_PPC64:
20272       return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20273 				 display_power_gnu_attribute);
20274 
20275     case EM_S390:
20276     case EM_S390_OLD:
20277       return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20278 				 display_s390_gnu_attribute);
20279 
20280     case EM_SPARC:
20281     case EM_SPARC32PLUS:
20282     case EM_SPARCV9:
20283       return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20284 				 display_sparc_gnu_attribute);
20285 
20286     case EM_TI_C6000:
20287       return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
20288 				 display_tic6x_attribute,
20289 				 display_generic_attribute);
20290 
20291     case EM_CSKY:
20292       return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
20293 				 display_csky_attribute, NULL);
20294 
20295     default:
20296       return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
20297 				 display_public_gnu_attributes,
20298 				 display_generic_attribute);
20299     }
20300 }
20301 
20302 static bfd_boolean
get_file_header(Filedata * filedata)20303 get_file_header (Filedata * filedata)
20304 {
20305   /* Read in the identity array.  */
20306   if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
20307     return FALSE;
20308 
20309   /* Determine how to read the rest of the header.  */
20310   switch (filedata->file_header.e_ident[EI_DATA])
20311     {
20312     default:
20313     case ELFDATANONE:
20314     case ELFDATA2LSB:
20315       byte_get = byte_get_little_endian;
20316       byte_put = byte_put_little_endian;
20317       break;
20318     case ELFDATA2MSB:
20319       byte_get = byte_get_big_endian;
20320       byte_put = byte_put_big_endian;
20321       break;
20322     }
20323 
20324   /* For now we only support 32 bit and 64 bit ELF files.  */
20325   is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
20326 
20327   /* Read in the rest of the header.  */
20328   if (is_32bit_elf)
20329     {
20330       Elf32_External_Ehdr ehdr32;
20331 
20332       if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
20333 	return FALSE;
20334 
20335       filedata->file_header.e_type      = BYTE_GET (ehdr32.e_type);
20336       filedata->file_header.e_machine   = BYTE_GET (ehdr32.e_machine);
20337       filedata->file_header.e_version   = BYTE_GET (ehdr32.e_version);
20338       filedata->file_header.e_entry     = BYTE_GET (ehdr32.e_entry);
20339       filedata->file_header.e_phoff     = BYTE_GET (ehdr32.e_phoff);
20340       filedata->file_header.e_shoff     = BYTE_GET (ehdr32.e_shoff);
20341       filedata->file_header.e_flags     = BYTE_GET (ehdr32.e_flags);
20342       filedata->file_header.e_ehsize    = BYTE_GET (ehdr32.e_ehsize);
20343       filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
20344       filedata->file_header.e_phnum     = BYTE_GET (ehdr32.e_phnum);
20345       filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
20346       filedata->file_header.e_shnum     = BYTE_GET (ehdr32.e_shnum);
20347       filedata->file_header.e_shstrndx  = BYTE_GET (ehdr32.e_shstrndx);
20348     }
20349   else
20350     {
20351       Elf64_External_Ehdr ehdr64;
20352 
20353       /* If we have been compiled with sizeof (bfd_vma) == 4, then
20354 	 we will not be able to cope with the 64bit data found in
20355 	 64 ELF files.  Detect this now and abort before we start
20356 	 overwriting things.  */
20357       if (sizeof (bfd_vma) < 8)
20358 	{
20359 	  error (_("This instance of readelf has been built without support for a\n\
20360 64 bit data type and so it cannot read 64 bit ELF files.\n"));
20361 	  return FALSE;
20362 	}
20363 
20364       if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
20365 	return FALSE;
20366 
20367       filedata->file_header.e_type      = BYTE_GET (ehdr64.e_type);
20368       filedata->file_header.e_machine   = BYTE_GET (ehdr64.e_machine);
20369       filedata->file_header.e_version   = BYTE_GET (ehdr64.e_version);
20370       filedata->file_header.e_entry     = BYTE_GET (ehdr64.e_entry);
20371       filedata->file_header.e_phoff     = BYTE_GET (ehdr64.e_phoff);
20372       filedata->file_header.e_shoff     = BYTE_GET (ehdr64.e_shoff);
20373       filedata->file_header.e_flags     = BYTE_GET (ehdr64.e_flags);
20374       filedata->file_header.e_ehsize    = BYTE_GET (ehdr64.e_ehsize);
20375       filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
20376       filedata->file_header.e_phnum     = BYTE_GET (ehdr64.e_phnum);
20377       filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
20378       filedata->file_header.e_shnum     = BYTE_GET (ehdr64.e_shnum);
20379       filedata->file_header.e_shstrndx  = BYTE_GET (ehdr64.e_shstrndx);
20380     }
20381 
20382   if (filedata->file_header.e_shoff)
20383     {
20384       /* There may be some extensions in the first section header.  Don't
20385 	 bomb if we can't read it.  */
20386       if (is_32bit_elf)
20387 	get_32bit_section_headers (filedata, TRUE);
20388       else
20389 	get_64bit_section_headers (filedata, TRUE);
20390     }
20391 
20392   return TRUE;
20393 }
20394 
20395 static void
close_file(Filedata * filedata)20396 close_file (Filedata * filedata)
20397 {
20398   if (filedata)
20399     {
20400       if (filedata->handle)
20401 	fclose (filedata->handle);
20402       free (filedata);
20403     }
20404 }
20405 
20406 void
close_debug_file(void * data)20407 close_debug_file (void * data)
20408 {
20409   close_file ((Filedata *) data);
20410 }
20411 
20412 static Filedata *
open_file(const char * pathname)20413 open_file (const char * pathname)
20414 {
20415   struct stat  statbuf;
20416   Filedata *   filedata = NULL;
20417 
20418   if (stat (pathname, & statbuf) < 0
20419       || ! S_ISREG (statbuf.st_mode))
20420     goto fail;
20421 
20422   filedata = calloc (1, sizeof * filedata);
20423   if (filedata == NULL)
20424     goto fail;
20425 
20426   filedata->handle = fopen (pathname, "rb");
20427   if (filedata->handle == NULL)
20428     goto fail;
20429 
20430   filedata->file_size = (bfd_size_type) statbuf.st_size;
20431   filedata->file_name = pathname;
20432 
20433   if (! get_file_header (filedata))
20434     goto fail;
20435 
20436   if (filedata->file_header.e_shoff)
20437     {
20438       bfd_boolean res;
20439 
20440       /* Read the section headers again, this time for real.  */
20441       if (is_32bit_elf)
20442 	res = get_32bit_section_headers (filedata, FALSE);
20443       else
20444 	res = get_64bit_section_headers (filedata, FALSE);
20445 
20446       if (!res)
20447 	goto fail;
20448     }
20449 
20450   return filedata;
20451 
20452  fail:
20453   if (filedata)
20454     {
20455       if (filedata->handle)
20456         fclose (filedata->handle);
20457       free (filedata);
20458     }
20459   return NULL;
20460 }
20461 
20462 void *
open_debug_file(const char * pathname)20463 open_debug_file (const char * pathname)
20464 {
20465   return open_file (pathname);
20466 }
20467 
20468 /* Process one ELF object file according to the command line options.
20469    This file may actually be stored in an archive.  The file is
20470    positioned at the start of the ELF object.  Returns TRUE if no
20471    problems were encountered, FALSE otherwise.  */
20472 
20473 static bfd_boolean
process_object(Filedata * filedata)20474 process_object (Filedata * filedata)
20475 {
20476   bfd_boolean  have_separate_files;
20477   unsigned int i;
20478   bfd_boolean res;
20479 
20480   if (! get_file_header (filedata))
20481     {
20482       error (_("%s: Failed to read file header\n"), filedata->file_name);
20483       return FALSE;
20484     }
20485 
20486   /* Initialise per file variables.  */
20487   for (i = ARRAY_SIZE (filedata->version_info); i--;)
20488     filedata->version_info[i] = 0;
20489 
20490   for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
20491     filedata->dynamic_info[i] = 0;
20492   filedata->dynamic_info_DT_GNU_HASH = 0;
20493   filedata->dynamic_info_DT_MIPS_XHASH = 0;
20494 
20495   /* Process the file.  */
20496   if (show_name)
20497     printf (_("\nFile: %s\n"), filedata->file_name);
20498 
20499   /* Initialise the dump_sects array from the cmdline_dump_sects array.
20500      Note we do this even if cmdline_dump_sects is empty because we
20501      must make sure that the dump_sets array is zeroed out before each
20502      object file is processed.  */
20503   if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
20504     memset (filedata->dump.dump_sects, 0,
20505 	    filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
20506 
20507   if (cmdline.num_dump_sects > 0)
20508     {
20509       if (filedata->dump.num_dump_sects == 0)
20510 	/* A sneaky way of allocating the dump_sects array.  */
20511 	request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
20512 
20513       assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
20514       memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
20515 	      cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
20516     }
20517 
20518   if (! process_file_header (filedata))
20519     return FALSE;
20520 
20521   if (! process_section_headers (filedata))
20522     {
20523       /* Without loaded section headers we cannot process lots of things.  */
20524       do_unwind = do_version = do_dump = do_arch = FALSE;
20525 
20526       if (! do_using_dynamic)
20527 	do_syms = do_dyn_syms = do_reloc = FALSE;
20528     }
20529 
20530   if (! process_section_groups (filedata))
20531     /* Without loaded section groups we cannot process unwind.  */
20532     do_unwind = FALSE;
20533 
20534   res = process_program_headers (filedata);
20535   if (res)
20536     res = process_dynamic_section (filedata);
20537 
20538   if (! process_relocs (filedata))
20539     res = FALSE;
20540 
20541   if (! process_unwind (filedata))
20542     res = FALSE;
20543 
20544   if (! process_symbol_table (filedata))
20545     res = FALSE;
20546 
20547   if (! process_syminfo (filedata))
20548     res = FALSE;
20549 
20550   if (! process_version_sections (filedata))
20551     res = FALSE;
20552 
20553   if (filedata->file_header.e_shstrndx != SHN_UNDEF)
20554     have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
20555   else
20556     have_separate_files = FALSE;
20557 
20558   if (! process_section_contents (filedata))
20559     res = FALSE;
20560 
20561   if (have_separate_files)
20562     {
20563       separate_info * d;
20564 
20565       for (d = first_separate_info; d != NULL; d = d->next)
20566 	{
20567 	  if (! process_section_headers (d->handle))
20568 	    res = FALSE;
20569 	  else if (! process_section_contents (d->handle))
20570 	    res = FALSE;
20571 	}
20572 
20573       /* The file handles are closed by the call to free_debug_memory() below.  */
20574     }
20575 
20576   if (! process_notes (filedata))
20577     res = FALSE;
20578 
20579   if (! process_gnu_liblist (filedata))
20580     res = FALSE;
20581 
20582   if (! process_arch_specific (filedata))
20583     res = FALSE;
20584 
20585   free (filedata->program_headers);
20586   filedata->program_headers = NULL;
20587 
20588   free (filedata->section_headers);
20589   filedata->section_headers = NULL;
20590 
20591   free (filedata->string_table);
20592   filedata->string_table = NULL;
20593   filedata->string_table_length = 0;
20594 
20595   free (filedata->dump.dump_sects);
20596   filedata->dump.dump_sects = NULL;
20597   filedata->dump.num_dump_sects = 0;
20598 
20599   free (filedata->dynamic_strings);
20600   filedata->dynamic_strings = NULL;
20601   filedata->dynamic_strings_length = 0;
20602 
20603   free (filedata->dynamic_symbols);
20604   filedata->dynamic_symbols = NULL;
20605   filedata->num_dynamic_syms = 0;
20606 
20607   free (filedata->dynamic_syminfo);
20608   filedata->dynamic_syminfo = NULL;
20609 
20610   free (filedata->dynamic_section);
20611   filedata->dynamic_section = NULL;
20612 
20613   while (filedata->symtab_shndx_list != NULL)
20614     {
20615       elf_section_list *next = filedata->symtab_shndx_list->next;
20616       free (filedata->symtab_shndx_list);
20617       filedata->symtab_shndx_list = next;
20618     }
20619 
20620   free (filedata->section_headers_groups);
20621   filedata->section_headers_groups = NULL;
20622 
20623   if (filedata->section_groups)
20624     {
20625       struct group_list * g;
20626       struct group_list * next;
20627 
20628       for (i = 0; i < filedata->group_count; i++)
20629 	{
20630 	  for (g = filedata->section_groups [i].root; g != NULL; g = next)
20631 	    {
20632 	      next = g->next;
20633 	      free (g);
20634 	    }
20635 	}
20636 
20637       free (filedata->section_groups);
20638       filedata->section_groups = NULL;
20639     }
20640 
20641   free_debug_memory ();
20642 
20643   return res;
20644 }
20645 
20646 /* Process an ELF archive.
20647    On entry the file is positioned just after the ARMAG string.
20648    Returns TRUE upon success, FALSE otherwise.  */
20649 
20650 static bfd_boolean
process_archive(Filedata * filedata,bfd_boolean is_thin_archive)20651 process_archive (Filedata * filedata, bfd_boolean is_thin_archive)
20652 {
20653   struct archive_info arch;
20654   struct archive_info nested_arch;
20655   size_t got;
20656   bfd_boolean ret = TRUE;
20657 
20658   show_name = TRUE;
20659 
20660   /* The ARCH structure is used to hold information about this archive.  */
20661   arch.file_name = NULL;
20662   arch.file = NULL;
20663   arch.index_array = NULL;
20664   arch.sym_table = NULL;
20665   arch.longnames = NULL;
20666 
20667   /* The NESTED_ARCH structure is used as a single-item cache of information
20668      about a nested archive (when members of a thin archive reside within
20669      another regular archive file).  */
20670   nested_arch.file_name = NULL;
20671   nested_arch.file = NULL;
20672   nested_arch.index_array = NULL;
20673   nested_arch.sym_table = NULL;
20674   nested_arch.longnames = NULL;
20675 
20676   if (setup_archive (&arch, filedata->file_name, filedata->handle,
20677 		     filedata->file_size, is_thin_archive,
20678 		     do_archive_index) != 0)
20679     {
20680       ret = FALSE;
20681       goto out;
20682     }
20683 
20684   if (do_archive_index)
20685     {
20686       if (arch.sym_table == NULL)
20687 	error (_("%s: unable to dump the index as none was found\n"),
20688 	       filedata->file_name);
20689       else
20690 	{
20691 	  unsigned long i, l;
20692 	  unsigned long current_pos;
20693 
20694 	  printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
20695 		    "in the symbol table)\n"),
20696 		  filedata->file_name, (unsigned long) arch.index_num,
20697 		  arch.sym_size);
20698 
20699 	  current_pos = ftell (filedata->handle);
20700 
20701 	  for (i = l = 0; i < arch.index_num; i++)
20702 	    {
20703 	      if (i == 0
20704 		  || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
20705 		{
20706 		  char * member_name
20707 		    = get_archive_member_name_at (&arch, arch.index_array[i],
20708 						  &nested_arch);
20709 
20710 		  if (member_name != NULL)
20711 		    {
20712 		      char * qualified_name
20713 			= make_qualified_name (&arch, &nested_arch,
20714 					       member_name);
20715 
20716 		      if (qualified_name != NULL)
20717 			{
20718 			  printf (_("Contents of binary %s at offset "),
20719 				  qualified_name);
20720 			  (void) print_vma (arch.index_array[i], PREFIX_HEX);
20721 			  putchar ('\n');
20722 			  free (qualified_name);
20723 			}
20724 		      free (member_name);
20725 		    }
20726 		}
20727 
20728 	      if (l >= arch.sym_size)
20729 		{
20730 		  error (_("%s: end of the symbol table reached "
20731 			   "before the end of the index\n"),
20732 			 filedata->file_name);
20733 		  ret = FALSE;
20734 		  break;
20735 		}
20736 	      /* PR 17531: file: 0b6630b2.  */
20737 	      printf ("\t%.*s\n",
20738 		      (int) (arch.sym_size - l), arch.sym_table + l);
20739 	      l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
20740 	    }
20741 
20742 	  if (arch.uses_64bit_indices)
20743 	    l = (l + 7) & ~ 7;
20744 	  else
20745 	    l += l & 1;
20746 
20747 	  if (l < arch.sym_size)
20748 	    {
20749 	      error (ngettext ("%s: %ld byte remains in the symbol table, "
20750 			       "but without corresponding entries in "
20751 			       "the index table\n",
20752 			       "%s: %ld bytes remain in the symbol table, "
20753 			       "but without corresponding entries in "
20754 			       "the index table\n",
20755 			       arch.sym_size - l),
20756 		     filedata->file_name, arch.sym_size - l);
20757 	      ret = FALSE;
20758 	    }
20759 
20760 	  if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
20761 	    {
20762 	      error (_("%s: failed to seek back to start of object files "
20763 		       "in the archive\n"),
20764 		     filedata->file_name);
20765 	      ret = FALSE;
20766 	      goto out;
20767 	    }
20768 	}
20769 
20770       if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
20771 	  && !do_segments && !do_header && !do_dump && !do_version
20772 	  && !do_histogram && !do_debugging && !do_arch && !do_notes
20773 	  && !do_section_groups && !do_dyn_syms)
20774 	{
20775 	  ret = TRUE; /* Archive index only.  */
20776 	  goto out;
20777 	}
20778     }
20779 
20780   while (1)
20781     {
20782       char * name;
20783       size_t namelen;
20784       char * qualified_name;
20785 
20786       /* Read the next archive header.  */
20787       if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
20788 	{
20789 	  error (_("%s: failed to seek to next archive header\n"),
20790 		 arch.file_name);
20791 	  ret = FALSE;
20792 	  break;
20793 	}
20794       got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
20795       if (got != sizeof arch.arhdr)
20796 	{
20797 	  if (got == 0)
20798 	    break;
20799 	  /* PR 24049 - we cannot use filedata->file_name as this will
20800 	     have already been freed.  */
20801 	  error (_("%s: failed to read archive header\n"), arch.file_name);
20802 
20803 	  ret = FALSE;
20804 	  break;
20805 	}
20806       if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
20807 	{
20808 	  error (_("%s: did not find a valid archive header\n"),
20809 		 arch.file_name);
20810 	  ret = FALSE;
20811 	  break;
20812 	}
20813 
20814       arch.next_arhdr_offset += sizeof arch.arhdr;
20815 
20816       filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
20817       if (filedata->archive_file_size & 01)
20818 	++filedata->archive_file_size;
20819 
20820       name = get_archive_member_name (&arch, &nested_arch);
20821       if (name == NULL)
20822 	{
20823 	  error (_("%s: bad archive file name\n"), arch.file_name);
20824 	  ret = FALSE;
20825 	  break;
20826 	}
20827       namelen = strlen (name);
20828 
20829       qualified_name = make_qualified_name (&arch, &nested_arch, name);
20830       if (qualified_name == NULL)
20831 	{
20832 	  error (_("%s: bad archive file name\n"), arch.file_name);
20833 	  free (name);
20834 	  ret = FALSE;
20835 	  break;
20836 	}
20837 
20838       if (is_thin_archive && arch.nested_member_origin == 0)
20839 	{
20840 	  /* This is a proxy for an external member of a thin archive.  */
20841 	  Filedata * member_filedata;
20842 	  char * member_file_name = adjust_relative_path
20843 	    (filedata->file_name, name, namelen);
20844 
20845 	  free (name);
20846 	  if (member_file_name == NULL)
20847 	    {
20848 	      free (qualified_name);
20849 	      ret = FALSE;
20850 	      break;
20851 	    }
20852 
20853 	  member_filedata = open_file (member_file_name);
20854 	  if (member_filedata == NULL)
20855 	    {
20856 	      error (_("Input file '%s' is not readable.\n"), member_file_name);
20857 	      free (member_file_name);
20858 	      free (qualified_name);
20859 	      ret = FALSE;
20860 	      break;
20861 	    }
20862 
20863 	  filedata->archive_file_offset = arch.nested_member_origin;
20864 	  member_filedata->file_name = qualified_name;
20865 
20866 	  if (! process_object (member_filedata))
20867 	    ret = FALSE;
20868 
20869 	  close_file (member_filedata);
20870 	  free (member_file_name);
20871 	}
20872       else if (is_thin_archive)
20873 	{
20874 	  Filedata thin_filedata;
20875 
20876 	  memset (&thin_filedata, 0, sizeof (thin_filedata));
20877 
20878 	  /* PR 15140: Allow for corrupt thin archives.  */
20879 	  if (nested_arch.file == NULL)
20880 	    {
20881 	      error (_("%s: contains corrupt thin archive: %s\n"),
20882 		     qualified_name, name);
20883 	      free (qualified_name);
20884 	      free (name);
20885 	      ret = FALSE;
20886 	      break;
20887 	    }
20888 	  free (name);
20889 
20890 	  /* This is a proxy for a member of a nested archive.  */
20891 	  filedata->archive_file_offset
20892 	    = arch.nested_member_origin + sizeof arch.arhdr;
20893 
20894 	  /* The nested archive file will have been opened and setup by
20895 	     get_archive_member_name.  */
20896 	  if (fseek (nested_arch.file, filedata->archive_file_offset,
20897 		     SEEK_SET) != 0)
20898 	    {
20899 	      error (_("%s: failed to seek to archive member.\n"),
20900 		     nested_arch.file_name);
20901 	      free (qualified_name);
20902 	      ret = FALSE;
20903 	      break;
20904 	    }
20905 
20906 	  thin_filedata.handle = nested_arch.file;
20907 	  thin_filedata.file_name = qualified_name;
20908 
20909 	  if (! process_object (& thin_filedata))
20910 	    ret = FALSE;
20911 	}
20912       else
20913 	{
20914 	  free (name);
20915 	  filedata->archive_file_offset = arch.next_arhdr_offset;
20916 	  filedata->file_name = qualified_name;
20917 	  if (! process_object (filedata))
20918 	    ret = FALSE;
20919 	  arch.next_arhdr_offset += filedata->archive_file_size;
20920 	  /* Stop looping with "negative" archive_file_size.  */
20921 	  if (arch.next_arhdr_offset < filedata->archive_file_size)
20922 	    arch.next_arhdr_offset = -1ul;
20923 	}
20924 
20925       free (qualified_name);
20926     }
20927 
20928  out:
20929   if (nested_arch.file != NULL)
20930     fclose (nested_arch.file);
20931   release_archive (&nested_arch);
20932   release_archive (&arch);
20933 
20934   return ret;
20935 }
20936 
20937 static bfd_boolean
process_file(char * file_name)20938 process_file (char * file_name)
20939 {
20940   Filedata * filedata = NULL;
20941   struct stat statbuf;
20942   char armag[SARMAG];
20943   bfd_boolean ret = TRUE;
20944 
20945   if (stat (file_name, &statbuf) < 0)
20946     {
20947       if (errno == ENOENT)
20948 	error (_("'%s': No such file\n"), file_name);
20949       else
20950 	error (_("Could not locate '%s'.  System error message: %s\n"),
20951 	       file_name, strerror (errno));
20952       return FALSE;
20953     }
20954 
20955   if (! S_ISREG (statbuf.st_mode))
20956     {
20957       error (_("'%s' is not an ordinary file\n"), file_name);
20958       return FALSE;
20959     }
20960 
20961   filedata = calloc (1, sizeof * filedata);
20962   if (filedata == NULL)
20963     {
20964       error (_("Out of memory allocating file data structure\n"));
20965       return FALSE;
20966     }
20967 
20968   filedata->file_name = file_name;
20969   filedata->handle = fopen (file_name, "rb");
20970   if (filedata->handle == NULL)
20971     {
20972       error (_("Input file '%s' is not readable.\n"), file_name);
20973       free (filedata);
20974       return FALSE;
20975     }
20976 
20977   if (fread (armag, SARMAG, 1, filedata->handle) != 1)
20978     {
20979       error (_("%s: Failed to read file's magic number\n"), file_name);
20980       fclose (filedata->handle);
20981       free (filedata);
20982       return FALSE;
20983     }
20984 
20985   filedata->file_size = (bfd_size_type) statbuf.st_size;
20986 
20987   if (memcmp (armag, ARMAG, SARMAG) == 0)
20988     {
20989       if (! process_archive (filedata, FALSE))
20990 	ret = FALSE;
20991     }
20992   else if (memcmp (armag, ARMAGT, SARMAG) == 0)
20993     {
20994       if ( ! process_archive (filedata, TRUE))
20995 	ret = FALSE;
20996     }
20997   else
20998     {
20999       if (do_archive_index && !check_all)
21000 	error (_("File %s is not an archive so its index cannot be displayed.\n"),
21001 	       file_name);
21002 
21003       rewind (filedata->handle);
21004       filedata->archive_file_size = filedata->archive_file_offset = 0;
21005 
21006       if (! process_object (filedata))
21007 	ret = FALSE;
21008     }
21009 
21010   fclose (filedata->handle);
21011   free (filedata->section_headers);
21012   free (filedata->program_headers);
21013   free (filedata->string_table);
21014   free (filedata->dump.dump_sects);
21015   free (filedata);
21016 
21017   free (ba_cache.strtab);
21018   ba_cache.strtab = NULL;
21019   free (ba_cache.symtab);
21020   ba_cache.symtab = NULL;
21021   ba_cache.filedata = NULL;
21022 
21023   return ret;
21024 }
21025 
21026 #ifdef SUPPORT_DISASSEMBLY
21027 /* Needed by the i386 disassembler.  For extra credit, someone could
21028    fix this so that we insert symbolic addresses here, esp for GOT/PLT
21029    symbols.  */
21030 
21031 void
print_address(unsigned int addr,FILE * outfile)21032 print_address (unsigned int addr, FILE * outfile)
21033 {
21034   fprintf (outfile,"0x%8.8x", addr);
21035 }
21036 
21037 /* Needed by the i386 disassembler.  */
21038 
21039 void
db_task_printsym(unsigned int addr)21040 db_task_printsym (unsigned int addr)
21041 {
21042   print_address (addr, stderr);
21043 }
21044 #endif
21045 
21046 int
main(int argc,char ** argv)21047 main (int argc, char ** argv)
21048 {
21049   int err;
21050 
21051 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
21052   setlocale (LC_MESSAGES, "");
21053 #endif
21054 #if defined (HAVE_SETLOCALE)
21055   setlocale (LC_CTYPE, "");
21056 #endif
21057   bindtextdomain (PACKAGE, LOCALEDIR);
21058   textdomain (PACKAGE);
21059 
21060   expandargv (&argc, &argv);
21061 
21062   parse_args (& cmdline, argc, argv);
21063 
21064   if (optind < (argc - 1))
21065     /* When displaying information for more than one file,
21066        prefix the information with the file name.  */
21067     show_name = TRUE;
21068   else if (optind >= argc)
21069     {
21070       /* Ensure that the warning is always displayed.  */
21071       do_checks = TRUE;
21072 
21073       warn (_("Nothing to do.\n"));
21074       usage (stderr);
21075     }
21076 
21077   err = FALSE;
21078   while (optind < argc)
21079     if (! process_file (argv[optind++]))
21080       err = TRUE;
21081 
21082   free (cmdline.dump_sects);
21083 
21084   free (dump_ctf_symtab_name);
21085   free (dump_ctf_strtab_name);
21086   free (dump_ctf_parent_name);
21087 
21088   return err ? EXIT_FAILURE : EXIT_SUCCESS;
21089 }
21090