1 /* readelf.c -- display contents of an ELF format file
2    Copyright (C) 1998-2014 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 #ifdef HAVE_ZLIB_H
47 #include <zlib.h>
48 #endif
49 #ifdef HAVE_WCHAR_H
50 #include <wchar.h>
51 #endif
52 
53 #if __GNUC__ >= 2
54 /* Define BFD64 here, even if our default architecture is 32 bit ELF
55    as this will allow us to read in and parse 64bit and 32bit ELF files.
56    Only do this if we believe that the compiler can support a 64 bit
57    data type.  For now we only rely on GCC being able to do this.  */
58 #define BFD64
59 #endif
60 
61 #include "bfd.h"
62 #include "bucomm.h"
63 #include "elfcomm.h"
64 #include "dwarf.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/d10v.h"
104 #include "elf/d30v.h"
105 #include "elf/dlx.h"
106 #include "elf/epiphany.h"
107 #include "elf/fr30.h"
108 #include "elf/frv.h"
109 #include "elf/h8.h"
110 #include "elf/hppa.h"
111 #include "elf/i386.h"
112 #include "elf/i370.h"
113 #include "elf/i860.h"
114 #include "elf/i960.h"
115 #include "elf/ia64.h"
116 #include "elf/ip2k.h"
117 #include "elf/lm32.h"
118 #include "elf/iq2000.h"
119 #include "elf/m32c.h"
120 #include "elf/m32r.h"
121 #include "elf/m68k.h"
122 #include "elf/m68hc11.h"
123 #include "elf/mcore.h"
124 #include "elf/mep.h"
125 #include "elf/metag.h"
126 #include "elf/microblaze.h"
127 #include "elf/mips.h"
128 #include "elf/mmix.h"
129 #include "elf/mn10200.h"
130 #include "elf/mn10300.h"
131 #include "elf/moxie.h"
132 #include "elf/mt.h"
133 #include "elf/msp430.h"
134 #include "elf/nds32.h"
135 #include "elf/nios2.h"
136 #include "elf/or1k.h"
137 #include "elf/pj.h"
138 #include "elf/ppc.h"
139 #include "elf/ppc64.h"
140 #include "elf/rl78.h"
141 #include "elf/rx.h"
142 #include "elf/s390.h"
143 #include "elf/score.h"
144 #include "elf/sh.h"
145 #include "elf/sparc.h"
146 #include "elf/spu.h"
147 #include "elf/tic6x.h"
148 #include "elf/tilegx.h"
149 #include "elf/tilepro.h"
150 #include "elf/v850.h"
151 #include "elf/vax.h"
152 #include "elf/x86-64.h"
153 #include "elf/xc16x.h"
154 #include "elf/xgate.h"
155 #include "elf/xstormy16.h"
156 #include "elf/xtensa.h"
157 
158 #include "getopt.h"
159 #include "libiberty.h"
160 #include "safe-ctype.h"
161 #include "filenames.h"
162 
163 #ifndef offsetof
164 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
165 #endif
166 
167 char * program_name = "readelf";
168 static long archive_file_offset;
169 static unsigned long archive_file_size;
170 static bfd_size_type current_file_size;
171 static unsigned long dynamic_addr;
172 static bfd_size_type dynamic_size;
173 static size_t dynamic_nent;
174 static char * dynamic_strings;
175 static unsigned long dynamic_strings_length;
176 static char * string_table;
177 static unsigned long string_table_length;
178 static unsigned long num_dynamic_syms;
179 static Elf_Internal_Sym * dynamic_symbols;
180 static Elf_Internal_Syminfo * dynamic_syminfo;
181 static unsigned long dynamic_syminfo_offset;
182 static unsigned int dynamic_syminfo_nent;
183 static char program_interpreter[PATH_MAX];
184 static bfd_vma dynamic_info[DT_ENCODING];
185 static bfd_vma dynamic_info_DT_GNU_HASH;
186 static bfd_vma version_info[16];
187 static Elf_Internal_Ehdr elf_header;
188 static Elf_Internal_Shdr * section_headers;
189 static Elf_Internal_Phdr * program_headers;
190 static Elf_Internal_Dyn *  dynamic_section;
191 static Elf_Internal_Shdr * symtab_shndx_hdr;
192 static int show_name;
193 static int do_dynamic;
194 static int do_syms;
195 static int do_dyn_syms;
196 static int do_reloc;
197 static int do_sections;
198 static int do_section_groups;
199 static int do_section_details;
200 static int do_segments;
201 static int do_unwind;
202 static int do_using_dynamic;
203 static int do_header;
204 static int do_dump;
205 static int do_version;
206 static int do_histogram;
207 static int do_debugging;
208 static int do_arch;
209 static int do_notes;
210 static int do_archive_index;
211 static int is_32bit_elf;
212 
213 struct group_list
214 {
215   struct group_list * next;
216   unsigned int section_index;
217 };
218 
219 struct group
220 {
221   struct group_list * root;
222   unsigned int group_index;
223 };
224 
225 static size_t group_count;
226 static struct group * section_groups;
227 static struct group ** section_headers_groups;
228 
229 
230 /* Flag bits indicating particular types of dump.  */
231 #define HEX_DUMP	(1 << 0)	/* The -x command line switch.  */
232 #define DISASS_DUMP	(1 << 1)	/* The -i command line switch.  */
233 #define DEBUG_DUMP	(1 << 2)	/* The -w command line switch.  */
234 #define STRING_DUMP     (1 << 3)	/* The -p command line switch.  */
235 #define RELOC_DUMP      (1 << 4)	/* The -R command line switch.  */
236 
237 typedef unsigned char dump_type;
238 
239 /* A linked list of the section names for which dumps were requested.  */
240 struct dump_list_entry
241 {
242   char * name;
243   dump_type type;
244   struct dump_list_entry * next;
245 };
246 static struct dump_list_entry * dump_sects_byname;
247 
248 /* A dynamic array of flags indicating for which sections a dump
249    has been requested via command line switches.  */
250 static dump_type *   cmdline_dump_sects = NULL;
251 static unsigned int  num_cmdline_dump_sects = 0;
252 
253 /* A dynamic array of flags indicating for which sections a dump of
254    some kind has been requested.  It is reset on a per-object file
255    basis and then initialised from the cmdline_dump_sects array,
256    the results of interpreting the -w switch, and the
257    dump_sects_byname list.  */
258 static dump_type *   dump_sects = NULL;
259 static unsigned int  num_dump_sects = 0;
260 
261 
262 /* How to print a vma value.  */
263 typedef enum print_mode
264 {
265   HEX,
266   DEC,
267   DEC_5,
268   UNSIGNED,
269   PREFIX_HEX,
270   FULL_HEX,
271   LONG_HEX
272 }
273 print_mode;
274 
275 #define UNKNOWN -1
276 
277 #define SECTION_NAME(X)						\
278   ((X) == NULL ? _("<none>")					\
279    : string_table == NULL ? _("<no-name>")			\
280    : ((X)->sh_name >= string_table_length ? _("<corrupt>")	\
281   : string_table + (X)->sh_name))
282 
283 #define DT_VERSIONTAGIDX(tag)	(DT_VERNEEDNUM - (tag))	/* Reverse order!  */
284 
285 #define GET_ELF_SYMBOLS(file, section, sym_count)			\
286   (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count)	\
287    : get_64bit_elf_symbols (file, section, sym_count))
288 
289 #define VALID_DYNAMIC_NAME(offset)	((dynamic_strings != NULL) && (offset < dynamic_strings_length))
290 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
291    already been called and verified that the string exists.  */
292 #define GET_DYNAMIC_NAME(offset)	(dynamic_strings + offset)
293 
294 #define REMOVE_ARCH_BITS(ADDR)			\
295   do						\
296     {						\
297       if (elf_header.e_machine == EM_ARM)	\
298 	(ADDR) &= ~1;				\
299     }						\
300   while (0)
301 
302 /* Retrieve NMEMB structures, each SIZE bytes long from FILE starting at OFFSET.
303    Put the retrieved data into VAR, if it is not NULL.  Otherwise allocate a buffer
304    using malloc and fill that.  In either case return the pointer to the start of
305    the retrieved data or NULL if something went wrong.  If something does go wrong
306    emit an error message using REASON as part of the context.  */
307 
308 static void *
get_data(void * var,FILE * file,long offset,size_t size,size_t nmemb,const char * reason)309 get_data (void * var, FILE * file, long offset, size_t size, size_t nmemb,
310 	  const char * reason)
311 {
312   void * mvar;
313 
314   if (size == 0 || nmemb == 0)
315     return NULL;
316 
317   if (fseek (file, archive_file_offset + offset, SEEK_SET))
318     {
319       if (reason)
320 	error (_("Unable to seek to 0x%lx for %s\n"),
321 	       (unsigned long) archive_file_offset + offset, reason);
322       return NULL;
323     }
324 
325   /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
326      attempting to allocate memory when the read is bound to fail.  */
327   if (offset + archive_file_offset + size * nmemb > current_file_size)
328     {
329       if (reason)
330 	error (_("Reading 0x%lx bytes extends past end of file for %s\n"),
331 	       (unsigned long) (size * nmemb), reason);
332       return NULL;
333     }
334 
335   mvar = var;
336   if (mvar == NULL)
337     {
338       /* Check for overflow.  */
339       if (nmemb < (~(size_t) 0 - 1) / size)
340 	/* + 1 so that we can '\0' terminate invalid string table sections.  */
341 	mvar = malloc (size * nmemb + 1);
342 
343       if (mvar == NULL)
344 	{
345 	  if (reason)
346 	    error (_("Out of memory allocating 0x%lx bytes for %s\n"),
347 		   (unsigned long)(size * nmemb), reason);
348 	  return NULL;
349 	}
350 
351       ((char *) mvar)[size * nmemb] = '\0';
352     }
353 
354   if (fread (mvar, size, nmemb, file) != nmemb)
355     {
356       if (reason)
357 	error (_("Unable to read in 0x%lx bytes of %s\n"),
358 	       (unsigned long)(size * nmemb), reason);
359       if (mvar != var)
360 	free (mvar);
361       return NULL;
362     }
363 
364   return mvar;
365 }
366 
367 /* Print a VMA value.  */
368 
369 static int
print_vma(bfd_vma vma,print_mode mode)370 print_vma (bfd_vma vma, print_mode mode)
371 {
372   int nc = 0;
373 
374   switch (mode)
375     {
376     case FULL_HEX:
377       nc = printf ("0x");
378       /* Drop through.  */
379 
380     case LONG_HEX:
381 #ifdef BFD64
382       if (is_32bit_elf)
383 	return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
384 #endif
385       printf_vma (vma);
386       return nc + 16;
387 
388     case DEC_5:
389       if (vma <= 99999)
390 	return printf ("%5" BFD_VMA_FMT "d", vma);
391       /* Drop through.  */
392 
393     case PREFIX_HEX:
394       nc = printf ("0x");
395       /* Drop through.  */
396 
397     case HEX:
398       return nc + printf ("%" BFD_VMA_FMT "x", vma);
399 
400     case DEC:
401       return printf ("%" BFD_VMA_FMT "d", vma);
402 
403     case UNSIGNED:
404       return printf ("%" BFD_VMA_FMT "u", vma);
405     }
406   return 0;
407 }
408 
409 /* Display a symbol on stdout.  Handles the display of control characters and
410    multibye characters (assuming the host environment supports them).
411 
412    Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
413 
414    If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
415    padding as necessary.
416 
417    Returns the number of emitted characters.  */
418 
419 static unsigned int
print_symbol(int width,const char * symbol)420 print_symbol (int width, const char *symbol)
421 {
422   bfd_boolean extra_padding = FALSE;
423   int num_printed = 0;
424 #ifdef HAVE_MBSTATE_T
425   mbstate_t state;
426 #endif
427   int width_remaining;
428 
429   if (width < 0)
430     {
431       /* Keep the width positive.  This also helps.  */
432       width = - width;
433       extra_padding = TRUE;
434     }
435   assert (width != 0);
436 
437   if (do_wide)
438     /* Set the remaining width to a very large value.
439        This simplifies the code below.  */
440     width_remaining = INT_MAX;
441   else
442     width_remaining = width;
443 
444 #ifdef HAVE_MBSTATE_T
445   /* Initialise the multibyte conversion state.  */
446   memset (& state, 0, sizeof (state));
447 #endif
448 
449   while (width_remaining)
450     {
451       size_t  n;
452       const char c = *symbol++;
453 
454       if (c == 0)
455 	break;
456 
457       /* Do not print control characters directly as they can affect terminal
458 	 settings.  Such characters usually appear in the names generated
459 	 by the assembler for local labels.  */
460       if (ISCNTRL (c))
461 	{
462 	  if (width_remaining < 2)
463 	    break;
464 
465 	  printf ("^%c", c + 0x40);
466 	  width_remaining -= 2;
467 	  num_printed += 2;
468 	}
469       else if (ISPRINT (c))
470 	{
471 	  putchar (c);
472 	  width_remaining --;
473 	  num_printed ++;
474 	}
475       else
476 	{
477 #ifdef HAVE_MBSTATE_T
478 	  wchar_t w;
479 #endif
480 	  /* Let printf do the hard work of displaying multibyte characters.  */
481 	  printf ("%.1s", symbol - 1);
482 	  width_remaining --;
483 	  num_printed ++;
484 
485 #ifdef HAVE_MBSTATE_T
486 	  /* Try to find out how many bytes made up the character that was
487 	     just printed.  Advance the symbol pointer past the bytes that
488 	     were displayed.  */
489 	  n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
490 #else
491 	  n = 1;
492 #endif
493 	  if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
494 	    symbol += (n - 1);
495 	}
496     }
497 
498   if (extra_padding && num_printed < width)
499     {
500       /* Fill in the remaining spaces.  */
501       printf ("%-*s", width - num_printed, " ");
502       num_printed = width;
503     }
504 
505   return num_printed;
506 }
507 
508 /* Returns a pointer to a static buffer containing a  printable version of
509    the given section's name.  Like print_symbol, except that it does not try
510    to print multibyte characters, it just interprets them as hex values.  */
511 
512 static const char *
printable_section_name(Elf_Internal_Shdr * sec)513 printable_section_name (Elf_Internal_Shdr * sec)
514 {
515 #define MAX_PRINT_SEC_NAME_LEN 128
516   static char  sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
517   const char * name = SECTION_NAME (sec);
518   char *       buf = sec_name_buf;
519   char         c;
520   unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
521 
522   while ((c = * name ++) != 0)
523     {
524       if (ISCNTRL (c))
525 	{
526 	  if (remaining < 2)
527 	    break;
528 
529 	  * buf ++ = '^';
530 	  * buf ++ = c + 0x40;
531 	  remaining -= 2;
532 	}
533       else if (ISPRINT (c))
534 	{
535 	  * buf ++ = c;
536 	  remaining -= 1;
537 	}
538       else
539 	{
540 	  static char hex[17] = "0123456789ABCDEF";
541 
542 	  if (remaining < 4)
543 	    break;
544 	  * buf ++ = '<';
545 	  * buf ++ = hex[(c & 0xf0) >> 4];
546 	  * buf ++ = hex[c & 0x0f];
547 	  * buf ++ = '>';
548 	  remaining -= 4;
549 	}
550 
551       if (remaining == 0)
552 	break;
553     }
554 
555   * buf = 0;
556   return sec_name_buf;
557 }
558 
559 static const char *
printable_section_name_from_index(unsigned long ndx)560 printable_section_name_from_index (unsigned long ndx)
561 {
562   if (ndx >= elf_header.e_shnum)
563     return _("<corrupt>");
564 
565   return printable_section_name (section_headers + ndx);
566 }
567 
568 /* Return a pointer to section NAME, or NULL if no such section exists.  */
569 
570 static Elf_Internal_Shdr *
find_section(const char * name)571 find_section (const char * name)
572 {
573   unsigned int i;
574 
575   for (i = 0; i < elf_header.e_shnum; i++)
576     if (streq (SECTION_NAME (section_headers + i), name))
577       return section_headers + i;
578 
579   return NULL;
580 }
581 
582 /* Return a pointer to a section containing ADDR, or NULL if no such
583    section exists.  */
584 
585 static Elf_Internal_Shdr *
find_section_by_address(bfd_vma addr)586 find_section_by_address (bfd_vma addr)
587 {
588   unsigned int i;
589 
590   for (i = 0; i < elf_header.e_shnum; i++)
591     {
592       Elf_Internal_Shdr *sec = section_headers + i;
593       if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
594 	return sec;
595     }
596 
597   return NULL;
598 }
599 
600 static Elf_Internal_Shdr *
find_section_by_type(unsigned int type)601 find_section_by_type (unsigned int type)
602 {
603   unsigned int i;
604 
605   for (i = 0; i < elf_header.e_shnum; i++)
606     {
607       Elf_Internal_Shdr *sec = section_headers + i;
608       if (sec->sh_type == type)
609 	return sec;
610     }
611 
612   return NULL;
613 }
614 
615 /* Return a pointer to section NAME, or NULL if no such section exists,
616    restricted to the list of sections given in SET.  */
617 
618 static Elf_Internal_Shdr *
find_section_in_set(const char * name,unsigned int * set)619 find_section_in_set (const char * name, unsigned int * set)
620 {
621   unsigned int i;
622 
623   if (set != NULL)
624     {
625       while ((i = *set++) > 0)
626 	if (streq (SECTION_NAME (section_headers + i), name))
627 	  return section_headers + i;
628     }
629 
630   return find_section (name);
631 }
632 
633 /* Read an unsigned LEB128 encoded value from p.  Set *PLEN to the number of
634    bytes read.  */
635 
636 static inline unsigned long
read_uleb128(unsigned char * data,unsigned int * length_return,const unsigned char * const end)637 read_uleb128 (unsigned char *data,
638 	      unsigned int *length_return,
639 	      const unsigned char * const end)
640 {
641   return read_leb128 (data, length_return, FALSE, end);
642 }
643 
644 /* Return true if the current file is for IA-64 machine and OpenVMS ABI.
645    This OS has so many departures from the ELF standard that we test it at
646    many places.  */
647 
648 static inline int
is_ia64_vms(void)649 is_ia64_vms (void)
650 {
651   return elf_header.e_machine == EM_IA_64
652     && elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
653 }
654 
655 /* Guess the relocation size commonly used by the specific machines.  */
656 
657 static int
guess_is_rela(unsigned int e_machine)658 guess_is_rela (unsigned int e_machine)
659 {
660   switch (e_machine)
661     {
662       /* Targets that use REL relocations.  */
663     case EM_386:
664     case EM_486:
665     case EM_960:
666     case EM_ARM:
667     case EM_D10V:
668     case EM_CYGNUS_D10V:
669     case EM_DLX:
670     case EM_MIPS:
671     case EM_MIPS_RS3_LE:
672     case EM_CYGNUS_M32R:
673     case EM_SCORE:
674     case EM_XGATE:
675       return FALSE;
676 
677       /* Targets that use RELA relocations.  */
678     case EM_68K:
679     case EM_860:
680     case EM_AARCH64:
681     case EM_ADAPTEVA_EPIPHANY:
682     case EM_ALPHA:
683     case EM_ALTERA_NIOS2:
684     case EM_AVR:
685     case EM_AVR_OLD:
686     case EM_BLACKFIN:
687     case EM_CR16:
688     case EM_CRIS:
689     case EM_CRX:
690     case EM_D30V:
691     case EM_CYGNUS_D30V:
692     case EM_FR30:
693     case EM_CYGNUS_FR30:
694     case EM_CYGNUS_FRV:
695     case EM_H8S:
696     case EM_H8_300:
697     case EM_H8_300H:
698     case EM_IA_64:
699     case EM_IP2K:
700     case EM_IP2K_OLD:
701     case EM_IQ2000:
702     case EM_LATTICEMICO32:
703     case EM_M32C_OLD:
704     case EM_M32C:
705     case EM_M32R:
706     case EM_MCORE:
707     case EM_CYGNUS_MEP:
708     case EM_METAG:
709     case EM_MMIX:
710     case EM_MN10200:
711     case EM_CYGNUS_MN10200:
712     case EM_MN10300:
713     case EM_CYGNUS_MN10300:
714     case EM_MOXIE:
715     case EM_MSP430:
716     case EM_MSP430_OLD:
717     case EM_MT:
718     case EM_NDS32:
719     case EM_NIOS32:
720     case EM_OR1K:
721     case EM_PPC64:
722     case EM_PPC:
723     case EM_RL78:
724     case EM_RX:
725     case EM_S390:
726     case EM_S390_OLD:
727     case EM_SH:
728     case EM_SPARC:
729     case EM_SPARC32PLUS:
730     case EM_SPARCV9:
731     case EM_SPU:
732     case EM_TI_C6000:
733     case EM_TILEGX:
734     case EM_TILEPRO:
735     case EM_V800:
736     case EM_V850:
737     case EM_CYGNUS_V850:
738     case EM_VAX:
739     case EM_X86_64:
740     case EM_L1OM:
741     case EM_K1OM:
742     case EM_XSTORMY16:
743     case EM_XTENSA:
744     case EM_XTENSA_OLD:
745     case EM_MICROBLAZE:
746     case EM_MICROBLAZE_OLD:
747       return TRUE;
748 
749     case EM_68HC05:
750     case EM_68HC08:
751     case EM_68HC11:
752     case EM_68HC16:
753     case EM_FX66:
754     case EM_ME16:
755     case EM_MMA:
756     case EM_NCPU:
757     case EM_NDR1:
758     case EM_PCP:
759     case EM_ST100:
760     case EM_ST19:
761     case EM_ST7:
762     case EM_ST9PLUS:
763     case EM_STARCORE:
764     case EM_SVX:
765     case EM_TINYJ:
766     default:
767       warn (_("Don't know about relocations on this machine architecture\n"));
768       return FALSE;
769     }
770 }
771 
772 static int
slurp_rela_relocs(FILE * file,unsigned long rel_offset,unsigned long rel_size,Elf_Internal_Rela ** relasp,unsigned long * nrelasp)773 slurp_rela_relocs (FILE * file,
774 		   unsigned long rel_offset,
775 		   unsigned long rel_size,
776 		   Elf_Internal_Rela ** relasp,
777 		   unsigned long * nrelasp)
778 {
779   Elf_Internal_Rela * relas;
780   size_t nrelas;
781   unsigned int i;
782 
783   if (is_32bit_elf)
784     {
785       Elf32_External_Rela * erelas;
786 
787       erelas = (Elf32_External_Rela *) get_data (NULL, file, rel_offset, 1,
788                                                  rel_size, _("32-bit relocation data"));
789       if (!erelas)
790 	return 0;
791 
792       nrelas = rel_size / sizeof (Elf32_External_Rela);
793 
794       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
795                                              sizeof (Elf_Internal_Rela));
796 
797       if (relas == NULL)
798 	{
799 	  free (erelas);
800 	  error (_("out of memory parsing relocs\n"));
801 	  return 0;
802 	}
803 
804       for (i = 0; i < nrelas; i++)
805 	{
806 	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
807 	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
808 	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
809 	}
810 
811       free (erelas);
812     }
813   else
814     {
815       Elf64_External_Rela * erelas;
816 
817       erelas = (Elf64_External_Rela *) get_data (NULL, file, rel_offset, 1,
818                                                  rel_size, _("64-bit relocation data"));
819       if (!erelas)
820 	return 0;
821 
822       nrelas = rel_size / sizeof (Elf64_External_Rela);
823 
824       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
825                                              sizeof (Elf_Internal_Rela));
826 
827       if (relas == NULL)
828 	{
829 	  free (erelas);
830 	  error (_("out of memory parsing relocs\n"));
831 	  return 0;
832 	}
833 
834       for (i = 0; i < nrelas; i++)
835 	{
836 	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
837 	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
838 	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
839 
840 	  /* The #ifdef BFD64 below is to prevent a compile time
841 	     warning.  We know that if we do not have a 64 bit data
842 	     type that we will never execute this code anyway.  */
843 #ifdef BFD64
844 	  if (elf_header.e_machine == EM_MIPS
845 	      && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
846 	    {
847 	      /* In little-endian objects, r_info isn't really a
848 		 64-bit little-endian value: it has a 32-bit
849 		 little-endian symbol index followed by four
850 		 individual byte fields.  Reorder INFO
851 		 accordingly.  */
852 	      bfd_vma inf = relas[i].r_info;
853 	      inf = (((inf & 0xffffffff) << 32)
854 		      | ((inf >> 56) & 0xff)
855 		      | ((inf >> 40) & 0xff00)
856 		      | ((inf >> 24) & 0xff0000)
857 		      | ((inf >> 8) & 0xff000000));
858 	      relas[i].r_info = inf;
859 	    }
860 #endif /* BFD64 */
861 	}
862 
863       free (erelas);
864     }
865   *relasp = relas;
866   *nrelasp = nrelas;
867   return 1;
868 }
869 
870 static int
slurp_rel_relocs(FILE * file,unsigned long rel_offset,unsigned long rel_size,Elf_Internal_Rela ** relsp,unsigned long * nrelsp)871 slurp_rel_relocs (FILE * file,
872 		  unsigned long rel_offset,
873 		  unsigned long rel_size,
874 		  Elf_Internal_Rela ** relsp,
875 		  unsigned long * nrelsp)
876 {
877   Elf_Internal_Rela * rels;
878   size_t nrels;
879   unsigned int i;
880 
881   if (is_32bit_elf)
882     {
883       Elf32_External_Rel * erels;
884 
885       erels = (Elf32_External_Rel *) get_data (NULL, file, rel_offset, 1,
886                                                rel_size, _("32-bit relocation data"));
887       if (!erels)
888 	return 0;
889 
890       nrels = rel_size / sizeof (Elf32_External_Rel);
891 
892       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
893 
894       if (rels == NULL)
895 	{
896 	  free (erels);
897 	  error (_("out of memory parsing relocs\n"));
898 	  return 0;
899 	}
900 
901       for (i = 0; i < nrels; i++)
902 	{
903 	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
904 	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
905 	  rels[i].r_addend = 0;
906 	}
907 
908       free (erels);
909     }
910   else
911     {
912       Elf64_External_Rel * erels;
913 
914       erels = (Elf64_External_Rel *) get_data (NULL, file, rel_offset, 1,
915                                                rel_size, _("64-bit relocation data"));
916       if (!erels)
917 	return 0;
918 
919       nrels = rel_size / sizeof (Elf64_External_Rel);
920 
921       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
922 
923       if (rels == NULL)
924 	{
925 	  free (erels);
926 	  error (_("out of memory parsing relocs\n"));
927 	  return 0;
928 	}
929 
930       for (i = 0; i < nrels; i++)
931 	{
932 	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
933 	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
934 	  rels[i].r_addend = 0;
935 
936 	  /* The #ifdef BFD64 below is to prevent a compile time
937 	     warning.  We know that if we do not have a 64 bit data
938 	     type that we will never execute this code anyway.  */
939 #ifdef BFD64
940 	  if (elf_header.e_machine == EM_MIPS
941 	      && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
942 	    {
943 	      /* In little-endian objects, r_info isn't really a
944 		 64-bit little-endian value: it has a 32-bit
945 		 little-endian symbol index followed by four
946 		 individual byte fields.  Reorder INFO
947 		 accordingly.  */
948 	      bfd_vma inf = rels[i].r_info;
949 	      inf = (((inf & 0xffffffff) << 32)
950 		     | ((inf >> 56) & 0xff)
951 		     | ((inf >> 40) & 0xff00)
952 		     | ((inf >> 24) & 0xff0000)
953 		     | ((inf >> 8) & 0xff000000));
954 	      rels[i].r_info = inf;
955 	    }
956 #endif /* BFD64 */
957 	}
958 
959       free (erels);
960     }
961   *relsp = rels;
962   *nrelsp = nrels;
963   return 1;
964 }
965 
966 /* Returns the reloc type extracted from the reloc info field.  */
967 
968 static unsigned int
get_reloc_type(bfd_vma reloc_info)969 get_reloc_type (bfd_vma reloc_info)
970 {
971   if (is_32bit_elf)
972     return ELF32_R_TYPE (reloc_info);
973 
974   switch (elf_header.e_machine)
975     {
976     case EM_MIPS:
977       /* Note: We assume that reloc_info has already been adjusted for us.  */
978       return ELF64_MIPS_R_TYPE (reloc_info);
979 
980     case EM_SPARCV9:
981       return ELF64_R_TYPE_ID (reloc_info);
982 
983     default:
984       return ELF64_R_TYPE (reloc_info);
985     }
986 }
987 
988 /* Return the symbol index extracted from the reloc info field.  */
989 
990 static bfd_vma
get_reloc_symindex(bfd_vma reloc_info)991 get_reloc_symindex (bfd_vma reloc_info)
992 {
993   return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
994 }
995 
996 static inline bfd_boolean
uses_msp430x_relocs(void)997 uses_msp430x_relocs (void)
998 {
999   return
1000     elf_header.e_machine == EM_MSP430 /* Paranoia.  */
1001     /* GCC uses osabi == ELFOSBI_STANDALONE.  */
1002     && (((elf_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1003 	/* TI compiler uses ELFOSABI_NONE.  */
1004 	|| (elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1005 }
1006 
1007 /* Display the contents of the relocation data found at the specified
1008    offset.  */
1009 
1010 static void
dump_relocations(FILE * file,unsigned long rel_offset,unsigned long rel_size,Elf_Internal_Sym * symtab,unsigned long nsyms,char * strtab,unsigned long strtablen,int is_rela)1011 dump_relocations (FILE * file,
1012 		  unsigned long rel_offset,
1013 		  unsigned long rel_size,
1014 		  Elf_Internal_Sym * symtab,
1015 		  unsigned long nsyms,
1016 		  char * strtab,
1017 		  unsigned long strtablen,
1018 		  int is_rela)
1019 {
1020   unsigned int i;
1021   Elf_Internal_Rela * rels;
1022 
1023   if (is_rela == UNKNOWN)
1024     is_rela = guess_is_rela (elf_header.e_machine);
1025 
1026   if (is_rela)
1027     {
1028       if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
1029 	return;
1030     }
1031   else
1032     {
1033       if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
1034 	return;
1035     }
1036 
1037   if (is_32bit_elf)
1038     {
1039       if (is_rela)
1040 	{
1041 	  if (do_wide)
1042 	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name + Addend\n"));
1043 	  else
1044 	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name + Addend\n"));
1045 	}
1046       else
1047 	{
1048 	  if (do_wide)
1049 	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name\n"));
1050 	  else
1051 	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name\n"));
1052 	}
1053     }
1054   else
1055     {
1056       if (is_rela)
1057 	{
1058 	  if (do_wide)
1059 	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend\n"));
1060 	  else
1061 	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name + Addend\n"));
1062 	}
1063       else
1064 	{
1065 	  if (do_wide)
1066 	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name\n"));
1067 	  else
1068 	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name\n"));
1069 	}
1070     }
1071 
1072   for (i = 0; i < rel_size; i++)
1073     {
1074       const char * rtype;
1075       bfd_vma offset;
1076       bfd_vma inf;
1077       bfd_vma symtab_index;
1078       bfd_vma type;
1079 
1080       offset = rels[i].r_offset;
1081       inf    = rels[i].r_info;
1082 
1083       type = get_reloc_type (inf);
1084       symtab_index = get_reloc_symindex  (inf);
1085 
1086       if (is_32bit_elf)
1087 	{
1088 	  printf ("%8.8lx  %8.8lx ",
1089 		  (unsigned long) offset & 0xffffffff,
1090 		  (unsigned long) inf & 0xffffffff);
1091 	}
1092       else
1093 	{
1094 #if BFD_HOST_64BIT_LONG
1095 	  printf (do_wide
1096 		  ? "%16.16lx  %16.16lx "
1097 		  : "%12.12lx  %12.12lx ",
1098 		  offset, inf);
1099 #elif BFD_HOST_64BIT_LONG_LONG
1100 #ifndef __MSVCRT__
1101 	  printf (do_wide
1102 		  ? "%16.16llx  %16.16llx "
1103 		  : "%12.12llx  %12.12llx ",
1104 		  offset, inf);
1105 #else
1106 	  printf (do_wide
1107 		  ? "%16.16I64x  %16.16I64x "
1108 		  : "%12.12I64x  %12.12I64x ",
1109 		  offset, inf);
1110 #endif
1111 #else
1112 	  printf (do_wide
1113 		  ? "%8.8lx%8.8lx  %8.8lx%8.8lx "
1114 		  : "%4.4lx%8.8lx  %4.4lx%8.8lx ",
1115 		  _bfd_int64_high (offset),
1116 		  _bfd_int64_low (offset),
1117 		  _bfd_int64_high (inf),
1118 		  _bfd_int64_low (inf));
1119 #endif
1120 	}
1121 
1122       switch (elf_header.e_machine)
1123 	{
1124 	default:
1125 	  rtype = NULL;
1126 	  break;
1127 
1128 	case EM_AARCH64:
1129 	  rtype = elf_aarch64_reloc_type (type);
1130 	  break;
1131 
1132 	case EM_M32R:
1133 	case EM_CYGNUS_M32R:
1134 	  rtype = elf_m32r_reloc_type (type);
1135 	  break;
1136 
1137 	case EM_386:
1138 	case EM_486:
1139 	  rtype = elf_i386_reloc_type (type);
1140 	  break;
1141 
1142 	case EM_68HC11:
1143 	case EM_68HC12:
1144 	  rtype = elf_m68hc11_reloc_type (type);
1145 	  break;
1146 
1147 	case EM_68K:
1148 	  rtype = elf_m68k_reloc_type (type);
1149 	  break;
1150 
1151 	case EM_960:
1152 	  rtype = elf_i960_reloc_type (type);
1153 	  break;
1154 
1155 	case EM_AVR:
1156 	case EM_AVR_OLD:
1157 	  rtype = elf_avr_reloc_type (type);
1158 	  break;
1159 
1160 	case EM_OLD_SPARCV9:
1161 	case EM_SPARC32PLUS:
1162 	case EM_SPARCV9:
1163 	case EM_SPARC:
1164 	  rtype = elf_sparc_reloc_type (type);
1165 	  break;
1166 
1167 	case EM_SPU:
1168 	  rtype = elf_spu_reloc_type (type);
1169 	  break;
1170 
1171 	case EM_V800:
1172 	  rtype = v800_reloc_type (type);
1173 	  break;
1174 	case EM_V850:
1175 	case EM_CYGNUS_V850:
1176 	  rtype = v850_reloc_type (type);
1177 	  break;
1178 
1179 	case EM_D10V:
1180 	case EM_CYGNUS_D10V:
1181 	  rtype = elf_d10v_reloc_type (type);
1182 	  break;
1183 
1184 	case EM_D30V:
1185 	case EM_CYGNUS_D30V:
1186 	  rtype = elf_d30v_reloc_type (type);
1187 	  break;
1188 
1189 	case EM_DLX:
1190 	  rtype = elf_dlx_reloc_type (type);
1191 	  break;
1192 
1193 	case EM_SH:
1194 	  rtype = elf_sh_reloc_type (type);
1195 	  break;
1196 
1197 	case EM_MN10300:
1198 	case EM_CYGNUS_MN10300:
1199 	  rtype = elf_mn10300_reloc_type (type);
1200 	  break;
1201 
1202 	case EM_MN10200:
1203 	case EM_CYGNUS_MN10200:
1204 	  rtype = elf_mn10200_reloc_type (type);
1205 	  break;
1206 
1207 	case EM_FR30:
1208 	case EM_CYGNUS_FR30:
1209 	  rtype = elf_fr30_reloc_type (type);
1210 	  break;
1211 
1212 	case EM_CYGNUS_FRV:
1213 	  rtype = elf_frv_reloc_type (type);
1214 	  break;
1215 
1216 	case EM_MCORE:
1217 	  rtype = elf_mcore_reloc_type (type);
1218 	  break;
1219 
1220 	case EM_MMIX:
1221 	  rtype = elf_mmix_reloc_type (type);
1222 	  break;
1223 
1224 	case EM_MOXIE:
1225 	  rtype = elf_moxie_reloc_type (type);
1226 	  break;
1227 
1228 	case EM_MSP430:
1229 	  if (uses_msp430x_relocs ())
1230 	    {
1231 	      rtype = elf_msp430x_reloc_type (type);
1232 	      break;
1233 	    }
1234 	case EM_MSP430_OLD:
1235 	  rtype = elf_msp430_reloc_type (type);
1236 	  break;
1237 
1238 	case EM_NDS32:
1239 	  rtype = elf_nds32_reloc_type (type);
1240 	  break;
1241 
1242 	case EM_PPC:
1243 	  rtype = elf_ppc_reloc_type (type);
1244 	  break;
1245 
1246 	case EM_PPC64:
1247 	  rtype = elf_ppc64_reloc_type (type);
1248 	  break;
1249 
1250 	case EM_MIPS:
1251 	case EM_MIPS_RS3_LE:
1252 	  rtype = elf_mips_reloc_type (type);
1253 	  break;
1254 
1255 	case EM_ALPHA:
1256 	  rtype = elf_alpha_reloc_type (type);
1257 	  break;
1258 
1259 	case EM_ARM:
1260 	  rtype = elf_arm_reloc_type (type);
1261 	  break;
1262 
1263 	case EM_ARC:
1264 	  rtype = elf_arc_reloc_type (type);
1265 	  break;
1266 
1267 	case EM_PARISC:
1268 	  rtype = elf_hppa_reloc_type (type);
1269 	  break;
1270 
1271 	case EM_H8_300:
1272 	case EM_H8_300H:
1273 	case EM_H8S:
1274 	  rtype = elf_h8_reloc_type (type);
1275 	  break;
1276 
1277 	case EM_OR1K:
1278 	  rtype = elf_or1k_reloc_type (type);
1279 	  break;
1280 
1281 	case EM_PJ:
1282 	case EM_PJ_OLD:
1283 	  rtype = elf_pj_reloc_type (type);
1284 	  break;
1285 	case EM_IA_64:
1286 	  rtype = elf_ia64_reloc_type (type);
1287 	  break;
1288 
1289 	case EM_CRIS:
1290 	  rtype = elf_cris_reloc_type (type);
1291 	  break;
1292 
1293 	case EM_860:
1294 	  rtype = elf_i860_reloc_type (type);
1295 	  break;
1296 
1297 	case EM_X86_64:
1298 	case EM_L1OM:
1299 	case EM_K1OM:
1300 	  rtype = elf_x86_64_reloc_type (type);
1301 	  break;
1302 
1303 	case EM_S370:
1304 	  rtype = i370_reloc_type (type);
1305 	  break;
1306 
1307 	case EM_S390_OLD:
1308 	case EM_S390:
1309 	  rtype = elf_s390_reloc_type (type);
1310 	  break;
1311 
1312 	case EM_SCORE:
1313 	  rtype = elf_score_reloc_type (type);
1314 	  break;
1315 
1316 	case EM_XSTORMY16:
1317 	  rtype = elf_xstormy16_reloc_type (type);
1318 	  break;
1319 
1320 	case EM_CRX:
1321 	  rtype = elf_crx_reloc_type (type);
1322 	  break;
1323 
1324 	case EM_VAX:
1325 	  rtype = elf_vax_reloc_type (type);
1326 	  break;
1327 
1328 	case EM_ADAPTEVA_EPIPHANY:
1329 	  rtype = elf_epiphany_reloc_type (type);
1330 	  break;
1331 
1332 	case EM_IP2K:
1333 	case EM_IP2K_OLD:
1334 	  rtype = elf_ip2k_reloc_type (type);
1335 	  break;
1336 
1337 	case EM_IQ2000:
1338 	  rtype = elf_iq2000_reloc_type (type);
1339 	  break;
1340 
1341 	case EM_XTENSA_OLD:
1342 	case EM_XTENSA:
1343 	  rtype = elf_xtensa_reloc_type (type);
1344 	  break;
1345 
1346 	case EM_LATTICEMICO32:
1347 	  rtype = elf_lm32_reloc_type (type);
1348 	  break;
1349 
1350 	case EM_M32C_OLD:
1351 	case EM_M32C:
1352 	  rtype = elf_m32c_reloc_type (type);
1353 	  break;
1354 
1355 	case EM_MT:
1356 	  rtype = elf_mt_reloc_type (type);
1357 	  break;
1358 
1359 	case EM_BLACKFIN:
1360 	  rtype = elf_bfin_reloc_type (type);
1361 	  break;
1362 
1363 	case EM_CYGNUS_MEP:
1364 	  rtype = elf_mep_reloc_type (type);
1365 	  break;
1366 
1367 	case EM_CR16:
1368 	  rtype = elf_cr16_reloc_type (type);
1369 	  break;
1370 
1371 	case EM_MICROBLAZE:
1372 	case EM_MICROBLAZE_OLD:
1373 	  rtype = elf_microblaze_reloc_type (type);
1374 	  break;
1375 
1376 	case EM_RL78:
1377 	  rtype = elf_rl78_reloc_type (type);
1378 	  break;
1379 
1380 	case EM_RX:
1381 	  rtype = elf_rx_reloc_type (type);
1382 	  break;
1383 
1384 	case EM_METAG:
1385 	  rtype = elf_metag_reloc_type (type);
1386 	  break;
1387 
1388 	case EM_XC16X:
1389 	case EM_C166:
1390 	  rtype = elf_xc16x_reloc_type (type);
1391 	  break;
1392 
1393 	case EM_TI_C6000:
1394 	  rtype = elf_tic6x_reloc_type (type);
1395 	  break;
1396 
1397 	case EM_TILEGX:
1398 	  rtype = elf_tilegx_reloc_type (type);
1399 	  break;
1400 
1401 	case EM_TILEPRO:
1402 	  rtype = elf_tilepro_reloc_type (type);
1403 	  break;
1404 
1405 	case EM_XGATE:
1406 	  rtype = elf_xgate_reloc_type (type);
1407 	  break;
1408 
1409 	case EM_ALTERA_NIOS2:
1410 	  rtype = elf_nios2_reloc_type (type);
1411 	  break;
1412 	}
1413 
1414       if (rtype == NULL)
1415 	printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1416       else
1417 	printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1418 
1419       if (elf_header.e_machine == EM_ALPHA
1420 	  && rtype != NULL
1421 	  && streq (rtype, "R_ALPHA_LITUSE")
1422 	  && is_rela)
1423 	{
1424 	  switch (rels[i].r_addend)
1425 	    {
1426 	    case LITUSE_ALPHA_ADDR:   rtype = "ADDR";   break;
1427 	    case LITUSE_ALPHA_BASE:   rtype = "BASE";   break;
1428 	    case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1429 	    case LITUSE_ALPHA_JSR:    rtype = "JSR";    break;
1430 	    case LITUSE_ALPHA_TLSGD:  rtype = "TLSGD";  break;
1431 	    case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1432 	    case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1433 	    default: rtype = NULL;
1434 	    }
1435 	  if (rtype)
1436 	    printf (" (%s)", rtype);
1437 	  else
1438 	    {
1439 	      putchar (' ');
1440 	      printf (_("<unknown addend: %lx>"),
1441 		      (unsigned long) rels[i].r_addend);
1442 	    }
1443 	}
1444       else if (symtab_index)
1445 	{
1446 	  if (symtab == NULL || symtab_index >= nsyms)
1447 	    printf (_(" bad symbol index: %08lx"), (unsigned long) symtab_index);
1448 	  else
1449 	    {
1450 	      Elf_Internal_Sym * psym;
1451 
1452 	      psym = symtab + symtab_index;
1453 
1454 	      printf (" ");
1455 
1456 	      if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1457 		{
1458 		  const char * name;
1459 		  unsigned int len;
1460 		  unsigned int width = is_32bit_elf ? 8 : 14;
1461 
1462 		  /* Relocations against GNU_IFUNC symbols do not use the value
1463 		     of the symbol as the address to relocate against.  Instead
1464 		     they invoke the function named by the symbol and use its
1465 		     result as the address for relocation.
1466 
1467 		     To indicate this to the user, do not display the value of
1468 		     the symbol in the "Symbols's Value" field.  Instead show
1469 		     its name followed by () as a hint that the symbol is
1470 		     invoked.  */
1471 
1472 		  if (strtab == NULL
1473 		      || psym->st_name == 0
1474 		      || psym->st_name >= strtablen)
1475 		    name = "??";
1476 		  else
1477 		    name = strtab + psym->st_name;
1478 
1479 		  len = print_symbol (width, name);
1480 		  printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1481 		}
1482 	      else
1483 		{
1484 		  print_vma (psym->st_value, LONG_HEX);
1485 
1486 		  printf (is_32bit_elf ? "   " : " ");
1487 		}
1488 
1489 	      if (psym->st_name == 0)
1490 		{
1491 		  const char * sec_name = "<null>";
1492 		  char name_buf[40];
1493 
1494 		  if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1495 		    {
1496 		      if (psym->st_shndx < elf_header.e_shnum)
1497 			sec_name = SECTION_NAME (section_headers + psym->st_shndx);
1498 		      else if (psym->st_shndx == SHN_ABS)
1499 			sec_name = "ABS";
1500 		      else if (psym->st_shndx == SHN_COMMON)
1501 			sec_name = "COMMON";
1502 		      else if ((elf_header.e_machine == EM_MIPS
1503 				&& psym->st_shndx == SHN_MIPS_SCOMMON)
1504 			       || (elf_header.e_machine == EM_TI_C6000
1505 				   && psym->st_shndx == SHN_TIC6X_SCOMMON))
1506 			sec_name = "SCOMMON";
1507 		      else if (elf_header.e_machine == EM_MIPS
1508 			       && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1509 			sec_name = "SUNDEF";
1510 		      else if ((elf_header.e_machine == EM_X86_64
1511 				|| elf_header.e_machine == EM_L1OM
1512 				|| elf_header.e_machine == EM_K1OM)
1513 			       && psym->st_shndx == SHN_X86_64_LCOMMON)
1514 			sec_name = "LARGE_COMMON";
1515 		      else if (elf_header.e_machine == EM_IA_64
1516 			       && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1517 			       && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1518 			sec_name = "ANSI_COM";
1519 		      else if (is_ia64_vms ()
1520 			       && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1521 			sec_name = "VMS_SYMVEC";
1522 		      else
1523 			{
1524 			  sprintf (name_buf, "<section 0x%x>",
1525 				   (unsigned int) psym->st_shndx);
1526 			  sec_name = name_buf;
1527 			}
1528 		    }
1529 		  print_symbol (22, sec_name);
1530 		}
1531 	      else if (strtab == NULL)
1532 		printf (_("<string table index: %3ld>"), psym->st_name);
1533 	      else if (psym->st_name >= strtablen)
1534 		printf (_("<corrupt string table index: %3ld>"), psym->st_name);
1535 	      else
1536 		print_symbol (22, strtab + psym->st_name);
1537 
1538 	      if (is_rela)
1539 		{
1540 		  bfd_signed_vma off = rels[i].r_addend;
1541 
1542 		  if (off < 0)
1543 		    printf (" - %" BFD_VMA_FMT "x", - off);
1544 		  else
1545 		    printf (" + %" BFD_VMA_FMT "x", off);
1546 		}
1547 	    }
1548 	}
1549       else if (is_rela)
1550 	{
1551 	  bfd_signed_vma off = rels[i].r_addend;
1552 
1553 	  printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1554 	  if (off < 0)
1555 	    printf ("-%" BFD_VMA_FMT "x", - off);
1556 	  else
1557 	    printf ("%" BFD_VMA_FMT "x", off);
1558 	}
1559 
1560       if (elf_header.e_machine == EM_SPARCV9
1561 	  && rtype != NULL
1562 	  && streq (rtype, "R_SPARC_OLO10"))
1563 	printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1564 
1565       putchar ('\n');
1566 
1567 #ifdef BFD64
1568       if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1569 	{
1570 	  bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1571 	  bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1572 	  const char * rtype2 = elf_mips_reloc_type (type2);
1573 	  const char * rtype3 = elf_mips_reloc_type (type3);
1574 
1575 	  printf ("                    Type2: ");
1576 
1577 	  if (rtype2 == NULL)
1578 	    printf (_("unrecognized: %-7lx"),
1579 		    (unsigned long) type2 & 0xffffffff);
1580 	  else
1581 	    printf ("%-17.17s", rtype2);
1582 
1583 	  printf ("\n                    Type3: ");
1584 
1585 	  if (rtype3 == NULL)
1586 	    printf (_("unrecognized: %-7lx"),
1587 		    (unsigned long) type3 & 0xffffffff);
1588 	  else
1589 	    printf ("%-17.17s", rtype3);
1590 
1591 	  putchar ('\n');
1592 	}
1593 #endif /* BFD64 */
1594     }
1595 
1596   free (rels);
1597 }
1598 
1599 static const char *
get_mips_dynamic_type(unsigned long type)1600 get_mips_dynamic_type (unsigned long type)
1601 {
1602   switch (type)
1603     {
1604     case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1605     case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1606     case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1607     case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1608     case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1609     case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1610     case DT_MIPS_MSYM: return "MIPS_MSYM";
1611     case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1612     case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1613     case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1614     case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1615     case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1616     case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1617     case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1618     case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1619     case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1620     case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1621     case DT_MIPS_RLD_MAP2: return "MIPS_RLD_MAP2";
1622     case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1623     case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1624     case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1625     case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1626     case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1627     case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1628     case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1629     case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1630     case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1631     case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1632     case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1633     case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1634     case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1635     case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1636     case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1637     case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1638     case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1639     case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1640     case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1641     case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1642     case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1643     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1644     case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1645     case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1646     case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1647     case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1648     case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1649     case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1650     default:
1651       return NULL;
1652     }
1653 }
1654 
1655 static const char *
get_sparc64_dynamic_type(unsigned long type)1656 get_sparc64_dynamic_type (unsigned long type)
1657 {
1658   switch (type)
1659     {
1660     case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1661     default:
1662       return NULL;
1663     }
1664 }
1665 
1666 static const char *
get_ppc_dynamic_type(unsigned long type)1667 get_ppc_dynamic_type (unsigned long type)
1668 {
1669   switch (type)
1670     {
1671     case DT_PPC_GOT:    return "PPC_GOT";
1672     case DT_PPC_OPT:    return "PPC_OPT";
1673     default:
1674       return NULL;
1675     }
1676 }
1677 
1678 static const char *
get_ppc64_dynamic_type(unsigned long type)1679 get_ppc64_dynamic_type (unsigned long type)
1680 {
1681   switch (type)
1682     {
1683     case DT_PPC64_GLINK:  return "PPC64_GLINK";
1684     case DT_PPC64_OPD:    return "PPC64_OPD";
1685     case DT_PPC64_OPDSZ:  return "PPC64_OPDSZ";
1686     case DT_PPC64_OPT:    return "PPC64_OPT";
1687     default:
1688       return NULL;
1689     }
1690 }
1691 
1692 static const char *
get_parisc_dynamic_type(unsigned long type)1693 get_parisc_dynamic_type (unsigned long type)
1694 {
1695   switch (type)
1696     {
1697     case DT_HP_LOAD_MAP:	return "HP_LOAD_MAP";
1698     case DT_HP_DLD_FLAGS:	return "HP_DLD_FLAGS";
1699     case DT_HP_DLD_HOOK:	return "HP_DLD_HOOK";
1700     case DT_HP_UX10_INIT:	return "HP_UX10_INIT";
1701     case DT_HP_UX10_INITSZ:	return "HP_UX10_INITSZ";
1702     case DT_HP_PREINIT:		return "HP_PREINIT";
1703     case DT_HP_PREINITSZ:	return "HP_PREINITSZ";
1704     case DT_HP_NEEDED:		return "HP_NEEDED";
1705     case DT_HP_TIME_STAMP:	return "HP_TIME_STAMP";
1706     case DT_HP_CHECKSUM:	return "HP_CHECKSUM";
1707     case DT_HP_GST_SIZE:	return "HP_GST_SIZE";
1708     case DT_HP_GST_VERSION:	return "HP_GST_VERSION";
1709     case DT_HP_GST_HASHVAL:	return "HP_GST_HASHVAL";
1710     case DT_HP_EPLTREL:		return "HP_GST_EPLTREL";
1711     case DT_HP_EPLTRELSZ:	return "HP_GST_EPLTRELSZ";
1712     case DT_HP_FILTERED:	return "HP_FILTERED";
1713     case DT_HP_FILTER_TLS:	return "HP_FILTER_TLS";
1714     case DT_HP_COMPAT_FILTERED:	return "HP_COMPAT_FILTERED";
1715     case DT_HP_LAZYLOAD:	return "HP_LAZYLOAD";
1716     case DT_HP_BIND_NOW_COUNT:	return "HP_BIND_NOW_COUNT";
1717     case DT_PLT:		return "PLT";
1718     case DT_PLT_SIZE:		return "PLT_SIZE";
1719     case DT_DLT:		return "DLT";
1720     case DT_DLT_SIZE:		return "DLT_SIZE";
1721     default:
1722       return NULL;
1723     }
1724 }
1725 
1726 static const char *
get_ia64_dynamic_type(unsigned long type)1727 get_ia64_dynamic_type (unsigned long type)
1728 {
1729   switch (type)
1730     {
1731     case DT_IA_64_PLT_RESERVE:         return "IA_64_PLT_RESERVE";
1732     case DT_IA_64_VMS_SUBTYPE:         return "VMS_SUBTYPE";
1733     case DT_IA_64_VMS_IMGIOCNT:        return "VMS_IMGIOCNT";
1734     case DT_IA_64_VMS_LNKFLAGS:        return "VMS_LNKFLAGS";
1735     case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1736     case DT_IA_64_VMS_IDENT:           return "VMS_IDENT";
1737     case DT_IA_64_VMS_NEEDED_IDENT:    return "VMS_NEEDED_IDENT";
1738     case DT_IA_64_VMS_IMG_RELA_CNT:    return "VMS_IMG_RELA_CNT";
1739     case DT_IA_64_VMS_SEG_RELA_CNT:    return "VMS_SEG_RELA_CNT";
1740     case DT_IA_64_VMS_FIXUP_RELA_CNT:  return "VMS_FIXUP_RELA_CNT";
1741     case DT_IA_64_VMS_FIXUP_NEEDED:    return "VMS_FIXUP_NEEDED";
1742     case DT_IA_64_VMS_SYMVEC_CNT:      return "VMS_SYMVEC_CNT";
1743     case DT_IA_64_VMS_XLATED:          return "VMS_XLATED";
1744     case DT_IA_64_VMS_STACKSIZE:       return "VMS_STACKSIZE";
1745     case DT_IA_64_VMS_UNWINDSZ:        return "VMS_UNWINDSZ";
1746     case DT_IA_64_VMS_UNWIND_CODSEG:   return "VMS_UNWIND_CODSEG";
1747     case DT_IA_64_VMS_UNWIND_INFOSEG:  return "VMS_UNWIND_INFOSEG";
1748     case DT_IA_64_VMS_LINKTIME:        return "VMS_LINKTIME";
1749     case DT_IA_64_VMS_SEG_NO:          return "VMS_SEG_NO";
1750     case DT_IA_64_VMS_SYMVEC_OFFSET:   return "VMS_SYMVEC_OFFSET";
1751     case DT_IA_64_VMS_SYMVEC_SEG:      return "VMS_SYMVEC_SEG";
1752     case DT_IA_64_VMS_UNWIND_OFFSET:   return "VMS_UNWIND_OFFSET";
1753     case DT_IA_64_VMS_UNWIND_SEG:      return "VMS_UNWIND_SEG";
1754     case DT_IA_64_VMS_STRTAB_OFFSET:   return "VMS_STRTAB_OFFSET";
1755     case DT_IA_64_VMS_SYSVER_OFFSET:   return "VMS_SYSVER_OFFSET";
1756     case DT_IA_64_VMS_IMG_RELA_OFF:    return "VMS_IMG_RELA_OFF";
1757     case DT_IA_64_VMS_SEG_RELA_OFF:    return "VMS_SEG_RELA_OFF";
1758     case DT_IA_64_VMS_FIXUP_RELA_OFF:  return "VMS_FIXUP_RELA_OFF";
1759     case DT_IA_64_VMS_PLTGOT_OFFSET:   return "VMS_PLTGOT_OFFSET";
1760     case DT_IA_64_VMS_PLTGOT_SEG:      return "VMS_PLTGOT_SEG";
1761     case DT_IA_64_VMS_FPMODE:          return "VMS_FPMODE";
1762     default:
1763       return NULL;
1764     }
1765 }
1766 
1767 static const char *
get_alpha_dynamic_type(unsigned long type)1768 get_alpha_dynamic_type (unsigned long type)
1769 {
1770   switch (type)
1771     {
1772     case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1773     default:
1774       return NULL;
1775     }
1776 }
1777 
1778 static const char *
get_score_dynamic_type(unsigned long type)1779 get_score_dynamic_type (unsigned long type)
1780 {
1781   switch (type)
1782     {
1783     case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
1784     case DT_SCORE_LOCAL_GOTNO:  return "SCORE_LOCAL_GOTNO";
1785     case DT_SCORE_SYMTABNO:     return "SCORE_SYMTABNO";
1786     case DT_SCORE_GOTSYM:       return "SCORE_GOTSYM";
1787     case DT_SCORE_UNREFEXTNO:   return "SCORE_UNREFEXTNO";
1788     case DT_SCORE_HIPAGENO:     return "SCORE_HIPAGENO";
1789     default:
1790       return NULL;
1791     }
1792 }
1793 
1794 static const char *
get_tic6x_dynamic_type(unsigned long type)1795 get_tic6x_dynamic_type (unsigned long type)
1796 {
1797   switch (type)
1798     {
1799     case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
1800     case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
1801     case DT_C6000_DSBT_BASE:   return "C6000_DSBT_BASE";
1802     case DT_C6000_DSBT_SIZE:   return "C6000_DSBT_SIZE";
1803     case DT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
1804     case DT_C6000_DSBT_INDEX:  return "C6000_DSBT_INDEX";
1805     default:
1806       return NULL;
1807     }
1808 }
1809 
1810 static const char *
get_nios2_dynamic_type(unsigned long type)1811 get_nios2_dynamic_type (unsigned long type)
1812 {
1813   switch (type)
1814     {
1815     case DT_NIOS2_GP: return "NIOS2_GP";
1816     default:
1817       return NULL;
1818     }
1819 }
1820 
1821 static const char *
get_dynamic_type(unsigned long type)1822 get_dynamic_type (unsigned long type)
1823 {
1824   static char buff[64];
1825 
1826   switch (type)
1827     {
1828     case DT_NULL:	return "NULL";
1829     case DT_NEEDED:	return "NEEDED";
1830     case DT_PLTRELSZ:	return "PLTRELSZ";
1831     case DT_PLTGOT:	return "PLTGOT";
1832     case DT_HASH:	return "HASH";
1833     case DT_STRTAB:	return "STRTAB";
1834     case DT_SYMTAB:	return "SYMTAB";
1835     case DT_RELA:	return "RELA";
1836     case DT_RELASZ:	return "RELASZ";
1837     case DT_RELAENT:	return "RELAENT";
1838     case DT_STRSZ:	return "STRSZ";
1839     case DT_SYMENT:	return "SYMENT";
1840     case DT_INIT:	return "INIT";
1841     case DT_FINI:	return "FINI";
1842     case DT_SONAME:	return "SONAME";
1843     case DT_RPATH:	return "RPATH";
1844     case DT_SYMBOLIC:	return "SYMBOLIC";
1845     case DT_REL:	return "REL";
1846     case DT_RELSZ:	return "RELSZ";
1847     case DT_RELENT:	return "RELENT";
1848     case DT_PLTREL:	return "PLTREL";
1849     case DT_DEBUG:	return "DEBUG";
1850     case DT_TEXTREL:	return "TEXTREL";
1851     case DT_JMPREL:	return "JMPREL";
1852     case DT_BIND_NOW:   return "BIND_NOW";
1853     case DT_INIT_ARRAY: return "INIT_ARRAY";
1854     case DT_FINI_ARRAY: return "FINI_ARRAY";
1855     case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
1856     case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
1857     case DT_RUNPATH:    return "RUNPATH";
1858     case DT_FLAGS:      return "FLAGS";
1859 
1860     case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
1861     case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
1862 
1863     case DT_CHECKSUM:	return "CHECKSUM";
1864     case DT_PLTPADSZ:	return "PLTPADSZ";
1865     case DT_MOVEENT:	return "MOVEENT";
1866     case DT_MOVESZ:	return "MOVESZ";
1867     case DT_FEATURE:	return "FEATURE";
1868     case DT_POSFLAG_1:	return "POSFLAG_1";
1869     case DT_SYMINSZ:	return "SYMINSZ";
1870     case DT_SYMINENT:	return "SYMINENT"; /* aka VALRNGHI */
1871 
1872     case DT_ADDRRNGLO:  return "ADDRRNGLO";
1873     case DT_CONFIG:	return "CONFIG";
1874     case DT_DEPAUDIT:	return "DEPAUDIT";
1875     case DT_AUDIT:	return "AUDIT";
1876     case DT_PLTPAD:	return "PLTPAD";
1877     case DT_MOVETAB:	return "MOVETAB";
1878     case DT_SYMINFO:	return "SYMINFO"; /* aka ADDRRNGHI */
1879 
1880     case DT_VERSYM:	return "VERSYM";
1881 
1882     case DT_TLSDESC_GOT: return "TLSDESC_GOT";
1883     case DT_TLSDESC_PLT: return "TLSDESC_PLT";
1884     case DT_RELACOUNT:	return "RELACOUNT";
1885     case DT_RELCOUNT:	return "RELCOUNT";
1886     case DT_FLAGS_1:	return "FLAGS_1";
1887     case DT_VERDEF:	return "VERDEF";
1888     case DT_VERDEFNUM:	return "VERDEFNUM";
1889     case DT_VERNEED:	return "VERNEED";
1890     case DT_VERNEEDNUM:	return "VERNEEDNUM";
1891 
1892     case DT_AUXILIARY:	return "AUXILIARY";
1893     case DT_USED:	return "USED";
1894     case DT_FILTER:	return "FILTER";
1895 
1896     case DT_GNU_PRELINKED: return "GNU_PRELINKED";
1897     case DT_GNU_CONFLICT: return "GNU_CONFLICT";
1898     case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
1899     case DT_GNU_LIBLIST: return "GNU_LIBLIST";
1900     case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
1901     case DT_GNU_HASH:	return "GNU_HASH";
1902 
1903     default:
1904       if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
1905 	{
1906 	  const char * result;
1907 
1908 	  switch (elf_header.e_machine)
1909 	    {
1910 	    case EM_MIPS:
1911 	    case EM_MIPS_RS3_LE:
1912 	      result = get_mips_dynamic_type (type);
1913 	      break;
1914 	    case EM_SPARCV9:
1915 	      result = get_sparc64_dynamic_type (type);
1916 	      break;
1917 	    case EM_PPC:
1918 	      result = get_ppc_dynamic_type (type);
1919 	      break;
1920 	    case EM_PPC64:
1921 	      result = get_ppc64_dynamic_type (type);
1922 	      break;
1923 	    case EM_IA_64:
1924 	      result = get_ia64_dynamic_type (type);
1925 	      break;
1926 	    case EM_ALPHA:
1927 	      result = get_alpha_dynamic_type (type);
1928 	      break;
1929 	    case EM_SCORE:
1930 	      result = get_score_dynamic_type (type);
1931 	      break;
1932 	    case EM_TI_C6000:
1933 	      result = get_tic6x_dynamic_type (type);
1934 	      break;
1935 	    case EM_ALTERA_NIOS2:
1936 	      result = get_nios2_dynamic_type (type);
1937 	      break;
1938 	    default:
1939 	      result = NULL;
1940 	      break;
1941 	    }
1942 
1943 	  if (result != NULL)
1944 	    return result;
1945 
1946 	  snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
1947 	}
1948       else if (((type >= DT_LOOS) && (type <= DT_HIOS))
1949 	       || (elf_header.e_machine == EM_PARISC
1950 		   && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
1951 	{
1952 	  const char * result;
1953 
1954 	  switch (elf_header.e_machine)
1955 	    {
1956 	    case EM_PARISC:
1957 	      result = get_parisc_dynamic_type (type);
1958 	      break;
1959 	    case EM_IA_64:
1960 	      result = get_ia64_dynamic_type (type);
1961 	      break;
1962 	    default:
1963 	      result = NULL;
1964 	      break;
1965 	    }
1966 
1967 	  if (result != NULL)
1968 	    return result;
1969 
1970 	  snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
1971 		    type);
1972 	}
1973       else
1974 	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
1975 
1976       return buff;
1977     }
1978 }
1979 
1980 static char *
get_file_type(unsigned e_type)1981 get_file_type (unsigned e_type)
1982 {
1983   static char buff[32];
1984 
1985   switch (e_type)
1986     {
1987     case ET_NONE:	return _("NONE (None)");
1988     case ET_REL:	return _("REL (Relocatable file)");
1989     case ET_EXEC:	return _("EXEC (Executable file)");
1990     case ET_DYN:	return _("DYN (Shared object file)");
1991     case ET_CORE:	return _("CORE (Core file)");
1992 
1993     default:
1994       if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
1995 	snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
1996       else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
1997 	snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
1998       else
1999 	snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2000       return buff;
2001     }
2002 }
2003 
2004 static char *
get_machine_name(unsigned e_machine)2005 get_machine_name (unsigned e_machine)
2006 {
2007   static char buff[64]; /* XXX */
2008 
2009   switch (e_machine)
2010     {
2011     case EM_NONE:		return _("None");
2012     case EM_AARCH64:		return "AArch64";
2013     case EM_M32:		return "WE32100";
2014     case EM_SPARC:		return "Sparc";
2015     case EM_SPU:		return "SPU";
2016     case EM_386:		return "Intel 80386";
2017     case EM_68K:		return "MC68000";
2018     case EM_88K:		return "MC88000";
2019     case EM_486:		return "Intel 80486";
2020     case EM_860:		return "Intel 80860";
2021     case EM_MIPS:		return "MIPS R3000";
2022     case EM_S370:		return "IBM System/370";
2023     case EM_MIPS_RS3_LE:	return "MIPS R4000 big-endian";
2024     case EM_OLD_SPARCV9:	return "Sparc v9 (old)";
2025     case EM_PARISC:		return "HPPA";
2026     case EM_PPC_OLD:		return "Power PC (old)";
2027     case EM_SPARC32PLUS:	return "Sparc v8+" ;
2028     case EM_960:		return "Intel 90860";
2029     case EM_PPC:		return "PowerPC";
2030     case EM_PPC64:		return "PowerPC64";
2031     case EM_FR20:		return "Fujitsu FR20";
2032     case EM_RH32:		return "TRW RH32";
2033     case EM_MCORE:		return "MCORE";
2034     case EM_ARM:		return "ARM";
2035     case EM_OLD_ALPHA:		return "Digital Alpha (old)";
2036     case EM_SH:			return "Renesas / SuperH SH";
2037     case EM_SPARCV9:		return "Sparc v9";
2038     case EM_TRICORE:		return "Siemens Tricore";
2039     case EM_ARC:		return "ARC";
2040     case EM_H8_300:		return "Renesas H8/300";
2041     case EM_H8_300H:		return "Renesas H8/300H";
2042     case EM_H8S:		return "Renesas H8S";
2043     case EM_H8_500:		return "Renesas H8/500";
2044     case EM_IA_64:		return "Intel IA-64";
2045     case EM_MIPS_X:		return "Stanford MIPS-X";
2046     case EM_COLDFIRE:		return "Motorola Coldfire";
2047     case EM_ALPHA:		return "Alpha";
2048     case EM_CYGNUS_D10V:
2049     case EM_D10V:		return "d10v";
2050     case EM_CYGNUS_D30V:
2051     case EM_D30V:		return "d30v";
2052     case EM_CYGNUS_M32R:
2053     case EM_M32R:		return "Renesas M32R (formerly Mitsubishi M32r)";
2054     case EM_CYGNUS_V850:
2055     case EM_V800:		return "Renesas V850 (using RH850 ABI)";
2056     case EM_V850:		return "Renesas V850";
2057     case EM_CYGNUS_MN10300:
2058     case EM_MN10300:		return "mn10300";
2059     case EM_CYGNUS_MN10200:
2060     case EM_MN10200:		return "mn10200";
2061     case EM_MOXIE:		return "Moxie";
2062     case EM_CYGNUS_FR30:
2063     case EM_FR30:		return "Fujitsu FR30";
2064     case EM_CYGNUS_FRV:		return "Fujitsu FR-V";
2065     case EM_PJ_OLD:
2066     case EM_PJ:			return "picoJava";
2067     case EM_MMA:		return "Fujitsu Multimedia Accelerator";
2068     case EM_PCP:		return "Siemens PCP";
2069     case EM_NCPU:		return "Sony nCPU embedded RISC processor";
2070     case EM_NDR1:		return "Denso NDR1 microprocesspr";
2071     case EM_STARCORE:		return "Motorola Star*Core processor";
2072     case EM_ME16:		return "Toyota ME16 processor";
2073     case EM_ST100:		return "STMicroelectronics ST100 processor";
2074     case EM_TINYJ:		return "Advanced Logic Corp. TinyJ embedded processor";
2075     case EM_PDSP:		return "Sony DSP processor";
2076     case EM_PDP10:		return "Digital Equipment Corp. PDP-10";
2077     case EM_PDP11:		return "Digital Equipment Corp. PDP-11";
2078     case EM_FX66:		return "Siemens FX66 microcontroller";
2079     case EM_ST9PLUS:		return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2080     case EM_ST7:		return "STMicroelectronics ST7 8-bit microcontroller";
2081     case EM_68HC16:		return "Motorola MC68HC16 Microcontroller";
2082     case EM_68HC12:		return "Motorola MC68HC12 Microcontroller";
2083     case EM_68HC11:		return "Motorola MC68HC11 Microcontroller";
2084     case EM_68HC08:		return "Motorola MC68HC08 Microcontroller";
2085     case EM_68HC05:		return "Motorola MC68HC05 Microcontroller";
2086     case EM_SVX:		return "Silicon Graphics SVx";
2087     case EM_ST19:		return "STMicroelectronics ST19 8-bit microcontroller";
2088     case EM_VAX:		return "Digital VAX";
2089     case EM_AVR_OLD:
2090     case EM_AVR:		return "Atmel AVR 8-bit microcontroller";
2091     case EM_CRIS:		return "Axis Communications 32-bit embedded processor";
2092     case EM_JAVELIN:		return "Infineon Technologies 32-bit embedded cpu";
2093     case EM_FIREPATH:		return "Element 14 64-bit DSP processor";
2094     case EM_ZSP:		return "LSI Logic's 16-bit DSP processor";
2095     case EM_MMIX:		return "Donald Knuth's educational 64-bit processor";
2096     case EM_HUANY:		return "Harvard Universitys's machine-independent object format";
2097     case EM_PRISM:		return "Vitesse Prism";
2098     case EM_X86_64:		return "Advanced Micro Devices X86-64";
2099     case EM_L1OM:		return "Intel L1OM";
2100     case EM_K1OM:		return "Intel K1OM";
2101     case EM_S390_OLD:
2102     case EM_S390:		return "IBM S/390";
2103     case EM_SCORE:		return "SUNPLUS S+Core";
2104     case EM_XSTORMY16:		return "Sanyo XStormy16 CPU core";
2105     case EM_OR1K:		return "OpenRISC 1000";
2106     case EM_ARC_A5:		return "ARC International ARCompact processor";
2107     case EM_CRX:		return "National Semiconductor CRX microprocessor";
2108     case EM_ADAPTEVA_EPIPHANY:	return "Adapteva EPIPHANY";
2109     case EM_DLX:		return "OpenDLX";
2110     case EM_IP2K_OLD:
2111     case EM_IP2K:		return "Ubicom IP2xxx 8-bit microcontrollers";
2112     case EM_IQ2000:       	return "Vitesse IQ2000";
2113     case EM_XTENSA_OLD:
2114     case EM_XTENSA:		return "Tensilica Xtensa Processor";
2115     case EM_VIDEOCORE:		return "Alphamosaic VideoCore processor";
2116     case EM_TMM_GPP:		return "Thompson Multimedia General Purpose Processor";
2117     case EM_NS32K:		return "National Semiconductor 32000 series";
2118     case EM_TPC:		return "Tenor Network TPC processor";
2119     case EM_ST200:		return "STMicroelectronics ST200 microcontroller";
2120     case EM_MAX:		return "MAX Processor";
2121     case EM_CR:			return "National Semiconductor CompactRISC";
2122     case EM_F2MC16:		return "Fujitsu F2MC16";
2123     case EM_MSP430:		return "Texas Instruments msp430 microcontroller";
2124     case EM_LATTICEMICO32:	return "Lattice Mico32";
2125     case EM_M32C_OLD:
2126     case EM_M32C:	        return "Renesas M32c";
2127     case EM_MT:                 return "Morpho Techologies MT processor";
2128     case EM_BLACKFIN:		return "Analog Devices Blackfin";
2129     case EM_SE_C33:		return "S1C33 Family of Seiko Epson processors";
2130     case EM_SEP:		return "Sharp embedded microprocessor";
2131     case EM_ARCA:		return "Arca RISC microprocessor";
2132     case EM_UNICORE:		return "Unicore";
2133     case EM_EXCESS:		return "eXcess 16/32/64-bit configurable embedded CPU";
2134     case EM_DXP:		return "Icera Semiconductor Inc. Deep Execution Processor";
2135     case EM_NIOS32:		return "Altera Nios";
2136     case EM_ALTERA_NIOS2:	return "Altera Nios II";
2137     case EM_C166:
2138     case EM_XC16X:		return "Infineon Technologies xc16x";
2139     case EM_M16C:		return "Renesas M16C series microprocessors";
2140     case EM_DSPIC30F:		return "Microchip Technology dsPIC30F Digital Signal Controller";
2141     case EM_CE:			return "Freescale Communication Engine RISC core";
2142     case EM_TSK3000:		return "Altium TSK3000 core";
2143     case EM_RS08:		return "Freescale RS08 embedded processor";
2144     case EM_ECOG2:		return "Cyan Technology eCOG2 microprocessor";
2145     case EM_DSP24:		return "New Japan Radio (NJR) 24-bit DSP Processor";
2146     case EM_VIDEOCORE3:		return "Broadcom VideoCore III processor";
2147     case EM_SE_C17:		return "Seiko Epson C17 family";
2148     case EM_TI_C6000:		return "Texas Instruments TMS320C6000 DSP family";
2149     case EM_TI_C2000:		return "Texas Instruments TMS320C2000 DSP family";
2150     case EM_TI_C5500:		return "Texas Instruments TMS320C55x DSP family";
2151     case EM_MMDSP_PLUS:		return "STMicroelectronics 64bit VLIW Data Signal Processor";
2152     case EM_CYPRESS_M8C:	return "Cypress M8C microprocessor";
2153     case EM_R32C:		return "Renesas R32C series microprocessors";
2154     case EM_TRIMEDIA:		return "NXP Semiconductors TriMedia architecture family";
2155     case EM_QDSP6:		return "QUALCOMM DSP6 Processor";
2156     case EM_8051:		return "Intel 8051 and variants";
2157     case EM_STXP7X:		return "STMicroelectronics STxP7x family";
2158     case EM_NDS32:		return "Andes Technology compact code size embedded RISC processor family";
2159     case EM_ECOG1X:		return "Cyan Technology eCOG1X family";
2160     case EM_MAXQ30:		return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2161     case EM_XIMO16:		return "New Japan Radio (NJR) 16-bit DSP Processor";
2162     case EM_MANIK:		return "M2000 Reconfigurable RISC Microprocessor";
2163     case EM_CRAYNV2:		return "Cray Inc. NV2 vector architecture";
2164     case EM_CYGNUS_MEP:         return "Toshiba MeP Media Engine";
2165     case EM_CR16:
2166     case EM_MICROBLAZE:
2167     case EM_MICROBLAZE_OLD:	return "Xilinx MicroBlaze";
2168     case EM_RL78:		return "Renesas RL78";
2169     case EM_RX:			return "Renesas RX";
2170     case EM_METAG:		return "Imagination Technologies Meta processor architecture";
2171     case EM_MCST_ELBRUS:	return "MCST Elbrus general purpose hardware architecture";
2172     case EM_ECOG16:		return "Cyan Technology eCOG16 family";
2173     case EM_ETPU:		return "Freescale Extended Time Processing Unit";
2174     case EM_SLE9X:		return "Infineon Technologies SLE9X core";
2175     case EM_AVR32:		return "Atmel Corporation 32-bit microprocessor family";
2176     case EM_STM8:		return "STMicroeletronics STM8 8-bit microcontroller";
2177     case EM_TILE64:		return "Tilera TILE64 multicore architecture family";
2178     case EM_TILEPRO:		return "Tilera TILEPro multicore architecture family";
2179     case EM_TILEGX:		return "Tilera TILE-Gx multicore architecture family";
2180     case EM_CUDA:		return "NVIDIA CUDA architecture";
2181     case EM_XGATE:		return "Motorola XGATE embedded processor";
2182     default:
2183       snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2184       return buff;
2185     }
2186 }
2187 
2188 static void
decode_ARM_machine_flags(unsigned e_flags,char buf[])2189 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2190 {
2191   unsigned eabi;
2192   int unknown = 0;
2193 
2194   eabi = EF_ARM_EABI_VERSION (e_flags);
2195   e_flags &= ~ EF_ARM_EABIMASK;
2196 
2197   /* Handle "generic" ARM flags.  */
2198   if (e_flags & EF_ARM_RELEXEC)
2199     {
2200       strcat (buf, ", relocatable executable");
2201       e_flags &= ~ EF_ARM_RELEXEC;
2202     }
2203 
2204   if (e_flags & EF_ARM_HASENTRY)
2205     {
2206       strcat (buf, ", has entry point");
2207       e_flags &= ~ EF_ARM_HASENTRY;
2208     }
2209 
2210   /* Now handle EABI specific flags.  */
2211   switch (eabi)
2212     {
2213     default:
2214       strcat (buf, ", <unrecognized EABI>");
2215       if (e_flags)
2216 	unknown = 1;
2217       break;
2218 
2219     case EF_ARM_EABI_VER1:
2220       strcat (buf, ", Version1 EABI");
2221       while (e_flags)
2222 	{
2223 	  unsigned flag;
2224 
2225 	  /* Process flags one bit at a time.  */
2226 	  flag = e_flags & - e_flags;
2227 	  e_flags &= ~ flag;
2228 
2229 	  switch (flag)
2230 	    {
2231 	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2232 	      strcat (buf, ", sorted symbol tables");
2233 	      break;
2234 
2235 	    default:
2236 	      unknown = 1;
2237 	      break;
2238 	    }
2239 	}
2240       break;
2241 
2242     case EF_ARM_EABI_VER2:
2243       strcat (buf, ", Version2 EABI");
2244       while (e_flags)
2245 	{
2246 	  unsigned flag;
2247 
2248 	  /* Process flags one bit at a time.  */
2249 	  flag = e_flags & - e_flags;
2250 	  e_flags &= ~ flag;
2251 
2252 	  switch (flag)
2253 	    {
2254 	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2255 	      strcat (buf, ", sorted symbol tables");
2256 	      break;
2257 
2258 	    case EF_ARM_DYNSYMSUSESEGIDX:
2259 	      strcat (buf, ", dynamic symbols use segment index");
2260 	      break;
2261 
2262 	    case EF_ARM_MAPSYMSFIRST:
2263 	      strcat (buf, ", mapping symbols precede others");
2264 	      break;
2265 
2266 	    default:
2267 	      unknown = 1;
2268 	      break;
2269 	    }
2270 	}
2271       break;
2272 
2273     case EF_ARM_EABI_VER3:
2274       strcat (buf, ", Version3 EABI");
2275       break;
2276 
2277     case EF_ARM_EABI_VER4:
2278       strcat (buf, ", Version4 EABI");
2279       while (e_flags)
2280 	{
2281 	  unsigned flag;
2282 
2283 	  /* Process flags one bit at a time.  */
2284 	  flag = e_flags & - e_flags;
2285 	  e_flags &= ~ flag;
2286 
2287 	  switch (flag)
2288 	    {
2289 	    case EF_ARM_BE8:
2290 	      strcat (buf, ", BE8");
2291 	      break;
2292 
2293 	    case EF_ARM_LE8:
2294 	      strcat (buf, ", LE8");
2295 	      break;
2296 
2297 	    default:
2298 	      unknown = 1;
2299 	      break;
2300 	    }
2301       break;
2302 	}
2303       break;
2304 
2305     case EF_ARM_EABI_VER5:
2306       strcat (buf, ", Version5 EABI");
2307       while (e_flags)
2308 	{
2309 	  unsigned flag;
2310 
2311 	  /* Process flags one bit at a time.  */
2312 	  flag = e_flags & - e_flags;
2313 	  e_flags &= ~ flag;
2314 
2315 	  switch (flag)
2316 	    {
2317 	    case EF_ARM_BE8:
2318 	      strcat (buf, ", BE8");
2319 	      break;
2320 
2321 	    case EF_ARM_LE8:
2322 	      strcat (buf, ", LE8");
2323 	      break;
2324 
2325 	    case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT.  */
2326 	      strcat (buf, ", soft-float ABI");
2327 	      break;
2328 
2329 	    case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT.  */
2330 	      strcat (buf, ", hard-float ABI");
2331 	      break;
2332 
2333 	    default:
2334 	      unknown = 1;
2335 	      break;
2336 	    }
2337 	}
2338       break;
2339 
2340     case EF_ARM_EABI_UNKNOWN:
2341       strcat (buf, ", GNU EABI");
2342       while (e_flags)
2343 	{
2344 	  unsigned flag;
2345 
2346 	  /* Process flags one bit at a time.  */
2347 	  flag = e_flags & - e_flags;
2348 	  e_flags &= ~ flag;
2349 
2350 	  switch (flag)
2351 	    {
2352 	    case EF_ARM_INTERWORK:
2353 	      strcat (buf, ", interworking enabled");
2354 	      break;
2355 
2356 	    case EF_ARM_APCS_26:
2357 	      strcat (buf, ", uses APCS/26");
2358 	      break;
2359 
2360 	    case EF_ARM_APCS_FLOAT:
2361 	      strcat (buf, ", uses APCS/float");
2362 	      break;
2363 
2364 	    case EF_ARM_PIC:
2365 	      strcat (buf, ", position independent");
2366 	      break;
2367 
2368 	    case EF_ARM_ALIGN8:
2369 	      strcat (buf, ", 8 bit structure alignment");
2370 	      break;
2371 
2372 	    case EF_ARM_NEW_ABI:
2373 	      strcat (buf, ", uses new ABI");
2374 	      break;
2375 
2376 	    case EF_ARM_OLD_ABI:
2377 	      strcat (buf, ", uses old ABI");
2378 	      break;
2379 
2380 	    case EF_ARM_SOFT_FLOAT:
2381 	      strcat (buf, ", software FP");
2382 	      break;
2383 
2384 	    case EF_ARM_VFP_FLOAT:
2385 	      strcat (buf, ", VFP");
2386 	      break;
2387 
2388 	    case EF_ARM_MAVERICK_FLOAT:
2389 	      strcat (buf, ", Maverick FP");
2390 	      break;
2391 
2392 	    default:
2393 	      unknown = 1;
2394 	      break;
2395 	    }
2396 	}
2397     }
2398 
2399   if (unknown)
2400     strcat (buf,_(", <unknown>"));
2401 }
2402 
2403 static void
decode_NDS32_machine_flags(unsigned e_flags,char buf[],size_t size)2404 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2405 {
2406   unsigned abi;
2407   unsigned arch;
2408   unsigned config;
2409   unsigned version;
2410   int has_fpu = 0;
2411   int r = 0;
2412 
2413   static const char *ABI_STRINGS[] =
2414   {
2415     "ABI v0", /* use r5 as return register; only used in N1213HC */
2416     "ABI v1", /* use r0 as return register */
2417     "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2418     "ABI v2fp", /* for FPU */
2419     "AABI",
2420     "ABI2 FP+"
2421   };
2422   static const char *VER_STRINGS[] =
2423   {
2424     "Andes ELF V1.3 or older",
2425     "Andes ELF V1.3.1",
2426     "Andes ELF V1.4"
2427   };
2428   static const char *ARCH_STRINGS[] =
2429   {
2430     "",
2431     "Andes Star v1.0",
2432     "Andes Star v2.0",
2433     "Andes Star v3.0",
2434     "Andes Star v3.0m"
2435   };
2436 
2437   abi = EF_NDS_ABI & e_flags;
2438   arch = EF_NDS_ARCH & e_flags;
2439   config = EF_NDS_INST & e_flags;
2440   version = EF_NDS32_ELF_VERSION & e_flags;
2441 
2442   memset (buf, 0, size);
2443 
2444   switch (abi)
2445     {
2446     case E_NDS_ABI_V0:
2447     case E_NDS_ABI_V1:
2448     case E_NDS_ABI_V2:
2449     case E_NDS_ABI_V2FP:
2450     case E_NDS_ABI_AABI:
2451     case E_NDS_ABI_V2FP_PLUS:
2452       /* In case there are holes in the array.  */
2453       r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
2454       break;
2455 
2456     default:
2457       r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
2458       break;
2459     }
2460 
2461   switch (version)
2462     {
2463     case E_NDS32_ELF_VER_1_2:
2464     case E_NDS32_ELF_VER_1_3:
2465     case E_NDS32_ELF_VER_1_4:
2466       r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
2467       break;
2468 
2469     default:
2470       r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
2471       break;
2472     }
2473 
2474   if (E_NDS_ABI_V0 == abi)
2475     {
2476       /* OLD ABI; only used in N1213HC, has performance extension 1.  */
2477       r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
2478       if (arch == E_NDS_ARCH_STAR_V1_0)
2479 	r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
2480       return;
2481     }
2482 
2483   switch (arch)
2484     {
2485     case E_NDS_ARCH_STAR_V1_0:
2486     case E_NDS_ARCH_STAR_V2_0:
2487     case E_NDS_ARCH_STAR_V3_0:
2488     case E_NDS_ARCH_STAR_V3_M:
2489       r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
2490       break;
2491 
2492     default:
2493       r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
2494       /* ARCH version determines how the e_flags are interpreted.
2495 	 If it is unknown, we cannot proceed.  */
2496       return;
2497     }
2498 
2499   /* Newer ABI; Now handle architecture specific flags.  */
2500   if (arch == E_NDS_ARCH_STAR_V1_0)
2501     {
2502       if (config & E_NDS32_HAS_MFUSR_PC_INST)
2503 	r += snprintf (buf + r, size -r, ", MFUSR_PC");
2504 
2505       if (!(config & E_NDS32_HAS_NO_MAC_INST))
2506 	r += snprintf (buf + r, size -r, ", MAC");
2507 
2508       if (config & E_NDS32_HAS_DIV_INST)
2509 	r += snprintf (buf + r, size -r, ", DIV");
2510 
2511       if (config & E_NDS32_HAS_16BIT_INST)
2512 	r += snprintf (buf + r, size -r, ", 16b");
2513     }
2514   else
2515     {
2516       if (config & E_NDS32_HAS_MFUSR_PC_INST)
2517 	{
2518 	  if (version <= E_NDS32_ELF_VER_1_3)
2519 	    r += snprintf (buf + r, size -r, ", [B8]");
2520 	  else
2521 	    r += snprintf (buf + r, size -r, ", EX9");
2522 	}
2523 
2524       if (config & E_NDS32_HAS_MAC_DX_INST)
2525 	r += snprintf (buf + r, size -r, ", MAC_DX");
2526 
2527       if (config & E_NDS32_HAS_DIV_DX_INST)
2528 	r += snprintf (buf + r, size -r, ", DIV_DX");
2529 
2530       if (config & E_NDS32_HAS_16BIT_INST)
2531 	{
2532 	  if (version <= E_NDS32_ELF_VER_1_3)
2533 	    r += snprintf (buf + r, size -r, ", 16b");
2534 	  else
2535 	    r += snprintf (buf + r, size -r, ", IFC");
2536 	}
2537     }
2538 
2539   if (config & E_NDS32_HAS_EXT_INST)
2540     r += snprintf (buf + r, size -r, ", PERF1");
2541 
2542   if (config & E_NDS32_HAS_EXT2_INST)
2543     r += snprintf (buf + r, size -r, ", PERF2");
2544 
2545   if (config & E_NDS32_HAS_FPU_INST)
2546     {
2547       has_fpu = 1;
2548       r += snprintf (buf + r, size -r, ", FPU_SP");
2549     }
2550 
2551   if (config & E_NDS32_HAS_FPU_DP_INST)
2552     {
2553       has_fpu = 1;
2554       r += snprintf (buf + r, size -r, ", FPU_DP");
2555     }
2556 
2557   if (config & E_NDS32_HAS_FPU_MAC_INST)
2558     {
2559       has_fpu = 1;
2560       r += snprintf (buf + r, size -r, ", FPU_MAC");
2561     }
2562 
2563   if (has_fpu)
2564     {
2565       switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
2566 	{
2567 	case E_NDS32_FPU_REG_8SP_4DP:
2568 	  r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
2569 	  break;
2570 	case E_NDS32_FPU_REG_16SP_8DP:
2571 	  r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
2572 	  break;
2573 	case E_NDS32_FPU_REG_32SP_16DP:
2574 	  r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
2575 	  break;
2576 	case E_NDS32_FPU_REG_32SP_32DP:
2577 	  r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
2578 	  break;
2579 	}
2580     }
2581 
2582   if (config & E_NDS32_HAS_AUDIO_INST)
2583     r += snprintf (buf + r, size -r, ", AUDIO");
2584 
2585   if (config & E_NDS32_HAS_STRING_INST)
2586     r += snprintf (buf + r, size -r, ", STR");
2587 
2588   if (config & E_NDS32_HAS_REDUCED_REGS)
2589     r += snprintf (buf + r, size -r, ", 16REG");
2590 
2591   if (config & E_NDS32_HAS_VIDEO_INST)
2592     {
2593       if (version <= E_NDS32_ELF_VER_1_3)
2594 	r += snprintf (buf + r, size -r, ", VIDEO");
2595       else
2596 	r += snprintf (buf + r, size -r, ", SATURATION");
2597     }
2598 
2599   if (config & E_NDS32_HAS_ENCRIPT_INST)
2600     r += snprintf (buf + r, size -r, ", ENCRP");
2601 
2602   if (config & E_NDS32_HAS_L2C_INST)
2603     r += snprintf (buf + r, size -r, ", L2C");
2604 }
2605 
2606 static char *
get_machine_flags(unsigned e_flags,unsigned e_machine)2607 get_machine_flags (unsigned e_flags, unsigned e_machine)
2608 {
2609   static char buf[1024];
2610 
2611   buf[0] = '\0';
2612 
2613   if (e_flags)
2614     {
2615       switch (e_machine)
2616 	{
2617 	default:
2618 	  break;
2619 
2620 	case EM_ARM:
2621 	  decode_ARM_machine_flags (e_flags, buf);
2622 	  break;
2623 
2624 	case EM_BLACKFIN:
2625 	  if (e_flags & EF_BFIN_PIC)
2626 	    strcat (buf, ", PIC");
2627 
2628 	  if (e_flags & EF_BFIN_FDPIC)
2629 	    strcat (buf, ", FDPIC");
2630 
2631 	  if (e_flags & EF_BFIN_CODE_IN_L1)
2632 	    strcat (buf, ", code in L1");
2633 
2634 	  if (e_flags & EF_BFIN_DATA_IN_L1)
2635 	    strcat (buf, ", data in L1");
2636 
2637 	  break;
2638 
2639 	case EM_CYGNUS_FRV:
2640 	  switch (e_flags & EF_FRV_CPU_MASK)
2641 	    {
2642 	    case EF_FRV_CPU_GENERIC:
2643 	      break;
2644 
2645 	    default:
2646 	      strcat (buf, ", fr???");
2647 	      break;
2648 
2649 	    case EF_FRV_CPU_FR300:
2650 	      strcat (buf, ", fr300");
2651 	      break;
2652 
2653 	    case EF_FRV_CPU_FR400:
2654 	      strcat (buf, ", fr400");
2655 	      break;
2656 	    case EF_FRV_CPU_FR405:
2657 	      strcat (buf, ", fr405");
2658 	      break;
2659 
2660 	    case EF_FRV_CPU_FR450:
2661 	      strcat (buf, ", fr450");
2662 	      break;
2663 
2664 	    case EF_FRV_CPU_FR500:
2665 	      strcat (buf, ", fr500");
2666 	      break;
2667 	    case EF_FRV_CPU_FR550:
2668 	      strcat (buf, ", fr550");
2669 	      break;
2670 
2671 	    case EF_FRV_CPU_SIMPLE:
2672 	      strcat (buf, ", simple");
2673 	      break;
2674 	    case EF_FRV_CPU_TOMCAT:
2675 	      strcat (buf, ", tomcat");
2676 	      break;
2677 	    }
2678 	  break;
2679 
2680 	case EM_68K:
2681 	  if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
2682 	    strcat (buf, ", m68000");
2683 	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
2684 	    strcat (buf, ", cpu32");
2685 	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
2686 	    strcat (buf, ", fido_a");
2687 	  else
2688 	    {
2689 	      char const * isa = _("unknown");
2690 	      char const * mac = _("unknown mac");
2691 	      char const * additional = NULL;
2692 
2693 	      switch (e_flags & EF_M68K_CF_ISA_MASK)
2694 		{
2695 		case EF_M68K_CF_ISA_A_NODIV:
2696 		  isa = "A";
2697 		  additional = ", nodiv";
2698 		  break;
2699 		case EF_M68K_CF_ISA_A:
2700 		  isa = "A";
2701 		  break;
2702 		case EF_M68K_CF_ISA_A_PLUS:
2703 		  isa = "A+";
2704 		  break;
2705 		case EF_M68K_CF_ISA_B_NOUSP:
2706 		  isa = "B";
2707 		  additional = ", nousp";
2708 		  break;
2709 		case EF_M68K_CF_ISA_B:
2710 		  isa = "B";
2711 		  break;
2712 		case EF_M68K_CF_ISA_C:
2713 		  isa = "C";
2714 		  break;
2715 		case EF_M68K_CF_ISA_C_NODIV:
2716 		  isa = "C";
2717 		  additional = ", nodiv";
2718 		  break;
2719 		}
2720 	      strcat (buf, ", cf, isa ");
2721 	      strcat (buf, isa);
2722 	      if (additional)
2723 		strcat (buf, additional);
2724 	      if (e_flags & EF_M68K_CF_FLOAT)
2725 		strcat (buf, ", float");
2726 	      switch (e_flags & EF_M68K_CF_MAC_MASK)
2727 		{
2728 		case 0:
2729 		  mac = NULL;
2730 		  break;
2731 		case EF_M68K_CF_MAC:
2732 		  mac = "mac";
2733 		  break;
2734 		case EF_M68K_CF_EMAC:
2735 		  mac = "emac";
2736 		  break;
2737 		case EF_M68K_CF_EMAC_B:
2738 		  mac = "emac_b";
2739 		  break;
2740 		}
2741 	      if (mac)
2742 		{
2743 		  strcat (buf, ", ");
2744 		  strcat (buf, mac);
2745 		}
2746 	    }
2747 	  break;
2748 
2749 	case EM_PPC:
2750 	  if (e_flags & EF_PPC_EMB)
2751 	    strcat (buf, ", emb");
2752 
2753 	  if (e_flags & EF_PPC_RELOCATABLE)
2754 	    strcat (buf, _(", relocatable"));
2755 
2756 	  if (e_flags & EF_PPC_RELOCATABLE_LIB)
2757 	    strcat (buf, _(", relocatable-lib"));
2758 	  break;
2759 
2760 	case EM_PPC64:
2761 	  if (e_flags & EF_PPC64_ABI)
2762 	    {
2763 	      char abi[] = ", abiv0";
2764 
2765 	      abi[6] += e_flags & EF_PPC64_ABI;
2766 	      strcat (buf, abi);
2767 	    }
2768 	  break;
2769 
2770 	case EM_V800:
2771 	  if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
2772 	    strcat (buf, ", RH850 ABI");
2773 
2774 	  if (e_flags & EF_V800_850E3)
2775 	    strcat (buf, ", V3 architecture");
2776 
2777 	  if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
2778 	    strcat (buf, ", FPU not used");
2779 
2780 	  if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
2781 	    strcat (buf, ", regmode: COMMON");
2782 
2783 	  if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
2784 	    strcat (buf, ", r4 not used");
2785 
2786 	  if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
2787 	    strcat (buf, ", r30 not used");
2788 
2789 	  if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
2790 	    strcat (buf, ", r5 not used");
2791 
2792 	  if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
2793 	    strcat (buf, ", r2 not used");
2794 
2795 	  for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
2796 	    {
2797 	      switch (e_flags & - e_flags)
2798 		{
2799 		case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
2800 		case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
2801 		case EF_RH850_SIMD: strcat (buf, ", SIMD"); break;
2802 		case EF_RH850_CACHE: strcat (buf, ", CACHE"); break;
2803 		case EF_RH850_MMU: strcat (buf, ", MMU"); break;
2804 		case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
2805 		case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
2806 		case EF_RH850_DATA_ALIGN8: strcat (buf, ", 8-byte alignment"); break;
2807 		case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
2808 		case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
2809 		case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
2810 		case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
2811 		case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
2812 		case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
2813 		case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
2814 		case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
2815 		default: break;
2816 		}
2817 	    }
2818 	  break;
2819 
2820 	case EM_V850:
2821 	case EM_CYGNUS_V850:
2822 	  switch (e_flags & EF_V850_ARCH)
2823 	    {
2824 	    case E_V850E3V5_ARCH:
2825 	      strcat (buf, ", v850e3v5");
2826 	      break;
2827 	    case E_V850E2V3_ARCH:
2828 	      strcat (buf, ", v850e2v3");
2829 	      break;
2830 	    case E_V850E2_ARCH:
2831 	      strcat (buf, ", v850e2");
2832 	      break;
2833             case E_V850E1_ARCH:
2834               strcat (buf, ", v850e1");
2835 	      break;
2836 	    case E_V850E_ARCH:
2837 	      strcat (buf, ", v850e");
2838 	      break;
2839 	    case E_V850_ARCH:
2840 	      strcat (buf, ", v850");
2841 	      break;
2842 	    default:
2843 	      strcat (buf, _(", unknown v850 architecture variant"));
2844 	      break;
2845 	    }
2846 	  break;
2847 
2848 	case EM_M32R:
2849 	case EM_CYGNUS_M32R:
2850 	  if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
2851 	    strcat (buf, ", m32r");
2852 	  break;
2853 
2854 	case EM_MIPS:
2855 	case EM_MIPS_RS3_LE:
2856 	  if (e_flags & EF_MIPS_NOREORDER)
2857 	    strcat (buf, ", noreorder");
2858 
2859 	  if (e_flags & EF_MIPS_PIC)
2860 	    strcat (buf, ", pic");
2861 
2862 	  if (e_flags & EF_MIPS_CPIC)
2863 	    strcat (buf, ", cpic");
2864 
2865 	  if (e_flags & EF_MIPS_UCODE)
2866 	    strcat (buf, ", ugen_reserved");
2867 
2868 	  if (e_flags & EF_MIPS_ABI2)
2869 	    strcat (buf, ", abi2");
2870 
2871 	  if (e_flags & EF_MIPS_OPTIONS_FIRST)
2872 	    strcat (buf, ", odk first");
2873 
2874 	  if (e_flags & EF_MIPS_32BITMODE)
2875 	    strcat (buf, ", 32bitmode");
2876 
2877 	  if (e_flags & EF_MIPS_NAN2008)
2878 	    strcat (buf, ", nan2008");
2879 
2880 	  if (e_flags & EF_MIPS_FP64)
2881 	    strcat (buf, ", fp64");
2882 
2883 	  switch ((e_flags & EF_MIPS_MACH))
2884 	    {
2885 	    case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
2886 	    case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
2887 	    case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
2888 	    case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
2889 	    case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
2890 	    case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
2891 	    case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
2892 	    case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
2893 	    case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
2894 	    case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
2895   	    case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
2896   	    case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
2897   	    case E_MIPS_MACH_LS3A: strcat (buf, ", loongson-3a"); break;
2898 	    case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
2899 	    case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
2900 	    case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
2901 	    case E_MIPS_MACH_XLR:  strcat (buf, ", xlr"); break;
2902 	    case 0:
2903 	    /* We simply ignore the field in this case to avoid confusion:
2904 	       MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
2905 	       extension.  */
2906 	      break;
2907 	    default: strcat (buf, _(", unknown CPU")); break;
2908 	    }
2909 
2910 	  switch ((e_flags & EF_MIPS_ABI))
2911 	    {
2912 	    case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
2913 	    case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
2914 	    case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
2915 	    case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
2916 	    case 0:
2917 	    /* We simply ignore the field in this case to avoid confusion:
2918 	       MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
2919 	       This means it is likely to be an o32 file, but not for
2920 	       sure.  */
2921 	      break;
2922 	    default: strcat (buf, _(", unknown ABI")); break;
2923 	    }
2924 
2925 	  if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
2926 	    strcat (buf, ", mdmx");
2927 
2928 	  if (e_flags & EF_MIPS_ARCH_ASE_M16)
2929 	    strcat (buf, ", mips16");
2930 
2931 	  if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
2932 	    strcat (buf, ", micromips");
2933 
2934 	  switch ((e_flags & EF_MIPS_ARCH))
2935 	    {
2936 	    case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
2937 	    case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
2938 	    case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
2939 	    case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
2940 	    case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
2941 	    case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
2942 	    case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
2943 	    case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
2944 	    case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
2945 	    case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
2946 	    case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
2947 	    default: strcat (buf, _(", unknown ISA")); break;
2948 	    }
2949 	  break;
2950 
2951 	case EM_NDS32:
2952 	  decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
2953 	  break;
2954 
2955 	case EM_SH:
2956 	  switch ((e_flags & EF_SH_MACH_MASK))
2957 	    {
2958 	    case EF_SH1: strcat (buf, ", sh1"); break;
2959 	    case EF_SH2: strcat (buf, ", sh2"); break;
2960 	    case EF_SH3: strcat (buf, ", sh3"); break;
2961 	    case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
2962 	    case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
2963 	    case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
2964 	    case EF_SH3E: strcat (buf, ", sh3e"); break;
2965 	    case EF_SH4: strcat (buf, ", sh4"); break;
2966 	    case EF_SH5: strcat (buf, ", sh5"); break;
2967 	    case EF_SH2E: strcat (buf, ", sh2e"); break;
2968 	    case EF_SH4A: strcat (buf, ", sh4a"); break;
2969 	    case EF_SH2A: strcat (buf, ", sh2a"); break;
2970 	    case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
2971 	    case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
2972 	    case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
2973 	    case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
2974 	    case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
2975 	    case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
2976 	    case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
2977 	    case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
2978 	    case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
2979 	    default: strcat (buf, _(", unknown ISA")); break;
2980 	    }
2981 
2982 	  if (e_flags & EF_SH_PIC)
2983 	    strcat (buf, ", pic");
2984 
2985 	  if (e_flags & EF_SH_FDPIC)
2986 	    strcat (buf, ", fdpic");
2987 	  break;
2988 
2989         case EM_OR1K:
2990           if (e_flags & EF_OR1K_NODELAY)
2991             strcat (buf, ", no delay");
2992           break;
2993 
2994 	case EM_SPARCV9:
2995 	  if (e_flags & EF_SPARC_32PLUS)
2996 	    strcat (buf, ", v8+");
2997 
2998 	  if (e_flags & EF_SPARC_SUN_US1)
2999 	    strcat (buf, ", ultrasparcI");
3000 
3001 	  if (e_flags & EF_SPARC_SUN_US3)
3002 	    strcat (buf, ", ultrasparcIII");
3003 
3004 	  if (e_flags & EF_SPARC_HAL_R1)
3005 	    strcat (buf, ", halr1");
3006 
3007 	  if (e_flags & EF_SPARC_LEDATA)
3008 	    strcat (buf, ", ledata");
3009 
3010 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3011 	    strcat (buf, ", tso");
3012 
3013 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3014 	    strcat (buf, ", pso");
3015 
3016 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3017 	    strcat (buf, ", rmo");
3018 	  break;
3019 
3020 	case EM_PARISC:
3021 	  switch (e_flags & EF_PARISC_ARCH)
3022 	    {
3023 	    case EFA_PARISC_1_0:
3024 	      strcpy (buf, ", PA-RISC 1.0");
3025 	      break;
3026 	    case EFA_PARISC_1_1:
3027 	      strcpy (buf, ", PA-RISC 1.1");
3028 	      break;
3029 	    case EFA_PARISC_2_0:
3030 	      strcpy (buf, ", PA-RISC 2.0");
3031 	      break;
3032 	    default:
3033 	      break;
3034 	    }
3035 	  if (e_flags & EF_PARISC_TRAPNIL)
3036 	    strcat (buf, ", trapnil");
3037 	  if (e_flags & EF_PARISC_EXT)
3038 	    strcat (buf, ", ext");
3039 	  if (e_flags & EF_PARISC_LSB)
3040 	    strcat (buf, ", lsb");
3041 	  if (e_flags & EF_PARISC_WIDE)
3042 	    strcat (buf, ", wide");
3043 	  if (e_flags & EF_PARISC_NO_KABP)
3044 	    strcat (buf, ", no kabp");
3045 	  if (e_flags & EF_PARISC_LAZYSWAP)
3046 	    strcat (buf, ", lazyswap");
3047 	  break;
3048 
3049 	case EM_PJ:
3050 	case EM_PJ_OLD:
3051 	  if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3052 	    strcat (buf, ", new calling convention");
3053 
3054 	  if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3055 	    strcat (buf, ", gnu calling convention");
3056 	  break;
3057 
3058 	case EM_IA_64:
3059 	  if ((e_flags & EF_IA_64_ABI64))
3060 	    strcat (buf, ", 64-bit");
3061 	  else
3062 	    strcat (buf, ", 32-bit");
3063 	  if ((e_flags & EF_IA_64_REDUCEDFP))
3064 	    strcat (buf, ", reduced fp model");
3065 	  if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3066 	    strcat (buf, ", no function descriptors, constant gp");
3067 	  else if ((e_flags & EF_IA_64_CONS_GP))
3068 	    strcat (buf, ", constant gp");
3069 	  if ((e_flags & EF_IA_64_ABSOLUTE))
3070 	    strcat (buf, ", absolute");
3071           if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3072             {
3073               if ((e_flags & EF_IA_64_VMS_LINKAGES))
3074                 strcat (buf, ", vms_linkages");
3075               switch ((e_flags & EF_IA_64_VMS_COMCOD))
3076                 {
3077                 case EF_IA_64_VMS_COMCOD_SUCCESS:
3078                   break;
3079                 case EF_IA_64_VMS_COMCOD_WARNING:
3080                   strcat (buf, ", warning");
3081                   break;
3082                 case EF_IA_64_VMS_COMCOD_ERROR:
3083                   strcat (buf, ", error");
3084                   break;
3085                 case EF_IA_64_VMS_COMCOD_ABORT:
3086                   strcat (buf, ", abort");
3087                   break;
3088                 default:
3089                   abort ();
3090                 }
3091             }
3092 	  break;
3093 
3094 	case EM_VAX:
3095 	  if ((e_flags & EF_VAX_NONPIC))
3096 	    strcat (buf, ", non-PIC");
3097 	  if ((e_flags & EF_VAX_DFLOAT))
3098 	    strcat (buf, ", D-Float");
3099 	  if ((e_flags & EF_VAX_GFLOAT))
3100 	    strcat (buf, ", G-Float");
3101 	  break;
3102 
3103 	case EM_RL78:
3104 	  if (e_flags & E_FLAG_RL78_G10)
3105 	    strcat (buf, ", G10");
3106 	  if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3107 	    strcat (buf, ", 64-bit doubles");
3108 	  break;
3109 
3110 	case EM_RX:
3111 	  if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3112 	    strcat (buf, ", 64-bit doubles");
3113 	  if (e_flags & E_FLAG_RX_DSP)
3114 	    strcat (buf, ", dsp");
3115 	  if (e_flags & E_FLAG_RX_PID)
3116 	    strcat (buf, ", pid");
3117 	  if (e_flags & E_FLAG_RX_ABI)
3118 	    strcat (buf, ", RX ABI");
3119 	  break;
3120 
3121 	case EM_S390:
3122 	  if (e_flags & EF_S390_HIGH_GPRS)
3123 	    strcat (buf, ", highgprs");
3124 	  break;
3125 
3126 	case EM_TI_C6000:
3127 	  if ((e_flags & EF_C6000_REL))
3128 	    strcat (buf, ", relocatable module");
3129 	  break;
3130 
3131 	case EM_MSP430:
3132 	  strcat (buf, _(": architecture variant: "));
3133 	  switch (e_flags & EF_MSP430_MACH)
3134 	    {
3135 	    case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3136 	    case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3137 	    case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3138 	    case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3139 	    case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3140 	    case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3141 	    case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3142 	    case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3143 	    case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3144 	    case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3145 	    case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3146 	    case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3147 	    case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3148 	    case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3149 	    case E_MSP430_MACH_MSP430X  : strcat (buf, "MSP430X"); break;
3150 	    default:
3151 	      strcat (buf, _(": unknown")); break;
3152 	    }
3153 
3154 	  if (e_flags & ~ EF_MSP430_MACH)
3155 	    strcat (buf, _(": unknown extra flag bits also present"));
3156 	}
3157     }
3158 
3159   return buf;
3160 }
3161 
3162 static const char *
get_osabi_name(unsigned int osabi)3163 get_osabi_name (unsigned int osabi)
3164 {
3165   static char buff[32];
3166 
3167   switch (osabi)
3168     {
3169     case ELFOSABI_NONE:		return "UNIX - System V";
3170     case ELFOSABI_HPUX:		return "UNIX - HP-UX";
3171     case ELFOSABI_NETBSD:	return "UNIX - NetBSD";
3172     case ELFOSABI_GNU:		return "UNIX - GNU";
3173     case ELFOSABI_SOLARIS:	return "UNIX - Solaris";
3174     case ELFOSABI_AIX:		return "UNIX - AIX";
3175     case ELFOSABI_IRIX:		return "UNIX - IRIX";
3176     case ELFOSABI_FREEBSD:	return "UNIX - FreeBSD";
3177     case ELFOSABI_TRU64:	return "UNIX - TRU64";
3178     case ELFOSABI_MODESTO:	return "Novell - Modesto";
3179     case ELFOSABI_OPENBSD:	return "UNIX - OpenBSD";
3180     case ELFOSABI_OPENVMS:	return "VMS - OpenVMS";
3181     case ELFOSABI_NSK:		return "HP - Non-Stop Kernel";
3182     case ELFOSABI_AROS:		return "AROS";
3183     case ELFOSABI_FENIXOS:	return "FenixOS";
3184     default:
3185       if (osabi >= 64)
3186 	switch (elf_header.e_machine)
3187 	  {
3188 	  case EM_ARM:
3189 	    switch (osabi)
3190 	      {
3191 	      case ELFOSABI_ARM:	return "ARM";
3192 	      default:
3193 		break;
3194 	      }
3195 	    break;
3196 
3197 	  case EM_MSP430:
3198 	  case EM_MSP430_OLD:
3199 	    switch (osabi)
3200 	      {
3201 	      case ELFOSABI_STANDALONE:	return _("Standalone App");
3202 	      default:
3203 		break;
3204 	      }
3205 	    break;
3206 
3207 	  case EM_TI_C6000:
3208 	    switch (osabi)
3209 	      {
3210 	      case ELFOSABI_C6000_ELFABI:	return _("Bare-metal C6000");
3211 	      case ELFOSABI_C6000_LINUX:	return "Linux C6000";
3212 	      default:
3213 		break;
3214 	      }
3215 	    break;
3216 
3217 	  default:
3218 	    break;
3219 	  }
3220       snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3221       return buff;
3222     }
3223 }
3224 
3225 static const char *
get_aarch64_segment_type(unsigned long type)3226 get_aarch64_segment_type (unsigned long type)
3227 {
3228   switch (type)
3229     {
3230     case PT_AARCH64_ARCHEXT:
3231       return "AARCH64_ARCHEXT";
3232     default:
3233       break;
3234     }
3235 
3236   return NULL;
3237 }
3238 
3239 static const char *
get_arm_segment_type(unsigned long type)3240 get_arm_segment_type (unsigned long type)
3241 {
3242   switch (type)
3243     {
3244     case PT_ARM_EXIDX:
3245       return "EXIDX";
3246     default:
3247       break;
3248     }
3249 
3250   return NULL;
3251 }
3252 
3253 static const char *
get_mips_segment_type(unsigned long type)3254 get_mips_segment_type (unsigned long type)
3255 {
3256   switch (type)
3257     {
3258     case PT_MIPS_REGINFO:
3259       return "REGINFO";
3260     case PT_MIPS_RTPROC:
3261       return "RTPROC";
3262     case PT_MIPS_OPTIONS:
3263       return "OPTIONS";
3264     case PT_MIPS_ABIFLAGS:
3265       return "ABIFLAGS";
3266     default:
3267       break;
3268     }
3269 
3270   return NULL;
3271 }
3272 
3273 static const char *
get_parisc_segment_type(unsigned long type)3274 get_parisc_segment_type (unsigned long type)
3275 {
3276   switch (type)
3277     {
3278     case PT_HP_TLS:		return "HP_TLS";
3279     case PT_HP_CORE_NONE:	return "HP_CORE_NONE";
3280     case PT_HP_CORE_VERSION:	return "HP_CORE_VERSION";
3281     case PT_HP_CORE_KERNEL:	return "HP_CORE_KERNEL";
3282     case PT_HP_CORE_COMM:	return "HP_CORE_COMM";
3283     case PT_HP_CORE_PROC:	return "HP_CORE_PROC";
3284     case PT_HP_CORE_LOADABLE:	return "HP_CORE_LOADABLE";
3285     case PT_HP_CORE_STACK:	return "HP_CORE_STACK";
3286     case PT_HP_CORE_SHM:	return "HP_CORE_SHM";
3287     case PT_HP_CORE_MMF:	return "HP_CORE_MMF";
3288     case PT_HP_PARALLEL:	return "HP_PARALLEL";
3289     case PT_HP_FASTBIND:	return "HP_FASTBIND";
3290     case PT_HP_OPT_ANNOT:	return "HP_OPT_ANNOT";
3291     case PT_HP_HSL_ANNOT:	return "HP_HSL_ANNOT";
3292     case PT_HP_STACK:		return "HP_STACK";
3293     case PT_HP_CORE_UTSNAME:	return "HP_CORE_UTSNAME";
3294     case PT_PARISC_ARCHEXT:	return "PARISC_ARCHEXT";
3295     case PT_PARISC_UNWIND:	return "PARISC_UNWIND";
3296     case PT_PARISC_WEAKORDER:	return "PARISC_WEAKORDER";
3297     default:
3298       break;
3299     }
3300 
3301   return NULL;
3302 }
3303 
3304 static const char *
get_ia64_segment_type(unsigned long type)3305 get_ia64_segment_type (unsigned long type)
3306 {
3307   switch (type)
3308     {
3309     case PT_IA_64_ARCHEXT:	return "IA_64_ARCHEXT";
3310     case PT_IA_64_UNWIND:	return "IA_64_UNWIND";
3311     case PT_HP_TLS:		return "HP_TLS";
3312     case PT_IA_64_HP_OPT_ANOT:	return "HP_OPT_ANNOT";
3313     case PT_IA_64_HP_HSL_ANOT:	return "HP_HSL_ANNOT";
3314     case PT_IA_64_HP_STACK:	return "HP_STACK";
3315     default:
3316       break;
3317     }
3318 
3319   return NULL;
3320 }
3321 
3322 static const char *
get_tic6x_segment_type(unsigned long type)3323 get_tic6x_segment_type (unsigned long type)
3324 {
3325   switch (type)
3326     {
3327     case PT_C6000_PHATTR:	return "C6000_PHATTR";
3328     default:
3329       break;
3330     }
3331 
3332   return NULL;
3333 }
3334 
3335 static const char *
get_segment_type(unsigned long p_type)3336 get_segment_type (unsigned long p_type)
3337 {
3338   static char buff[32];
3339 
3340   switch (p_type)
3341     {
3342     case PT_NULL:	return "NULL";
3343     case PT_LOAD:	return "LOAD";
3344     case PT_DYNAMIC:	return "DYNAMIC";
3345     case PT_INTERP:	return "INTERP";
3346     case PT_NOTE:	return "NOTE";
3347     case PT_SHLIB:	return "SHLIB";
3348     case PT_PHDR:	return "PHDR";
3349     case PT_TLS:	return "TLS";
3350 
3351     case PT_GNU_EH_FRAME:
3352 			return "GNU_EH_FRAME";
3353     case PT_GNU_STACK:	return "GNU_STACK";
3354     case PT_GNU_RELRO:  return "GNU_RELRO";
3355 
3356     default:
3357       if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
3358 	{
3359 	  const char * result;
3360 
3361 	  switch (elf_header.e_machine)
3362 	    {
3363 	    case EM_AARCH64:
3364 	      result = get_aarch64_segment_type (p_type);
3365 	      break;
3366 	    case EM_ARM:
3367 	      result = get_arm_segment_type (p_type);
3368 	      break;
3369 	    case EM_MIPS:
3370 	    case EM_MIPS_RS3_LE:
3371 	      result = get_mips_segment_type (p_type);
3372 	      break;
3373 	    case EM_PARISC:
3374 	      result = get_parisc_segment_type (p_type);
3375 	      break;
3376 	    case EM_IA_64:
3377 	      result = get_ia64_segment_type (p_type);
3378 	      break;
3379 	    case EM_TI_C6000:
3380 	      result = get_tic6x_segment_type (p_type);
3381 	      break;
3382 	    default:
3383 	      result = NULL;
3384 	      break;
3385 	    }
3386 
3387 	  if (result != NULL)
3388 	    return result;
3389 
3390 	  sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
3391 	}
3392       else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
3393 	{
3394 	  const char * result;
3395 
3396 	  switch (elf_header.e_machine)
3397 	    {
3398 	    case EM_PARISC:
3399 	      result = get_parisc_segment_type (p_type);
3400 	      break;
3401 	    case EM_IA_64:
3402 	      result = get_ia64_segment_type (p_type);
3403 	      break;
3404 	    default:
3405 	      result = NULL;
3406 	      break;
3407 	    }
3408 
3409 	  if (result != NULL)
3410 	    return result;
3411 
3412 	  sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
3413 	}
3414       else
3415 	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
3416 
3417       return buff;
3418     }
3419 }
3420 
3421 static const char *
get_mips_section_type_name(unsigned int sh_type)3422 get_mips_section_type_name (unsigned int sh_type)
3423 {
3424   switch (sh_type)
3425     {
3426     case SHT_MIPS_LIBLIST:	 return "MIPS_LIBLIST";
3427     case SHT_MIPS_MSYM:		 return "MIPS_MSYM";
3428     case SHT_MIPS_CONFLICT:	 return "MIPS_CONFLICT";
3429     case SHT_MIPS_GPTAB:	 return "MIPS_GPTAB";
3430     case SHT_MIPS_UCODE:	 return "MIPS_UCODE";
3431     case SHT_MIPS_DEBUG:	 return "MIPS_DEBUG";
3432     case SHT_MIPS_REGINFO:	 return "MIPS_REGINFO";
3433     case SHT_MIPS_PACKAGE:	 return "MIPS_PACKAGE";
3434     case SHT_MIPS_PACKSYM:	 return "MIPS_PACKSYM";
3435     case SHT_MIPS_RELD:		 return "MIPS_RELD";
3436     case SHT_MIPS_IFACE:	 return "MIPS_IFACE";
3437     case SHT_MIPS_CONTENT:	 return "MIPS_CONTENT";
3438     case SHT_MIPS_OPTIONS:	 return "MIPS_OPTIONS";
3439     case SHT_MIPS_SHDR:		 return "MIPS_SHDR";
3440     case SHT_MIPS_FDESC:	 return "MIPS_FDESC";
3441     case SHT_MIPS_EXTSYM:	 return "MIPS_EXTSYM";
3442     case SHT_MIPS_DENSE:	 return "MIPS_DENSE";
3443     case SHT_MIPS_PDESC:	 return "MIPS_PDESC";
3444     case SHT_MIPS_LOCSYM:	 return "MIPS_LOCSYM";
3445     case SHT_MIPS_AUXSYM:	 return "MIPS_AUXSYM";
3446     case SHT_MIPS_OPTSYM:	 return "MIPS_OPTSYM";
3447     case SHT_MIPS_LOCSTR:	 return "MIPS_LOCSTR";
3448     case SHT_MIPS_LINE:		 return "MIPS_LINE";
3449     case SHT_MIPS_RFDESC:	 return "MIPS_RFDESC";
3450     case SHT_MIPS_DELTASYM:	 return "MIPS_DELTASYM";
3451     case SHT_MIPS_DELTAINST:	 return "MIPS_DELTAINST";
3452     case SHT_MIPS_DELTACLASS:	 return "MIPS_DELTACLASS";
3453     case SHT_MIPS_DWARF:	 return "MIPS_DWARF";
3454     case SHT_MIPS_DELTADECL:	 return "MIPS_DELTADECL";
3455     case SHT_MIPS_SYMBOL_LIB:	 return "MIPS_SYMBOL_LIB";
3456     case SHT_MIPS_EVENTS:	 return "MIPS_EVENTS";
3457     case SHT_MIPS_TRANSLATE:	 return "MIPS_TRANSLATE";
3458     case SHT_MIPS_PIXIE:	 return "MIPS_PIXIE";
3459     case SHT_MIPS_XLATE:	 return "MIPS_XLATE";
3460     case SHT_MIPS_XLATE_DEBUG:	 return "MIPS_XLATE_DEBUG";
3461     case SHT_MIPS_WHIRL:	 return "MIPS_WHIRL";
3462     case SHT_MIPS_EH_REGION:	 return "MIPS_EH_REGION";
3463     case SHT_MIPS_XLATE_OLD:	 return "MIPS_XLATE_OLD";
3464     case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
3465     case SHT_MIPS_ABIFLAGS:	 return "MIPS_ABIFLAGS";
3466     default:
3467       break;
3468     }
3469   return NULL;
3470 }
3471 
3472 static const char *
get_parisc_section_type_name(unsigned int sh_type)3473 get_parisc_section_type_name (unsigned int sh_type)
3474 {
3475   switch (sh_type)
3476     {
3477     case SHT_PARISC_EXT:	return "PARISC_EXT";
3478     case SHT_PARISC_UNWIND:	return "PARISC_UNWIND";
3479     case SHT_PARISC_DOC:	return "PARISC_DOC";
3480     case SHT_PARISC_ANNOT:	return "PARISC_ANNOT";
3481     case SHT_PARISC_SYMEXTN:	return "PARISC_SYMEXTN";
3482     case SHT_PARISC_STUBS:	return "PARISC_STUBS";
3483     case SHT_PARISC_DLKM:	return "PARISC_DLKM";
3484     default:
3485       break;
3486     }
3487   return NULL;
3488 }
3489 
3490 static const char *
get_ia64_section_type_name(unsigned int sh_type)3491 get_ia64_section_type_name (unsigned int sh_type)
3492 {
3493   /* If the top 8 bits are 0x78 the next 8 are the os/abi ID.  */
3494   if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
3495     return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
3496 
3497   switch (sh_type)
3498     {
3499     case SHT_IA_64_EXT:		       return "IA_64_EXT";
3500     case SHT_IA_64_UNWIND:	       return "IA_64_UNWIND";
3501     case SHT_IA_64_PRIORITY_INIT:      return "IA_64_PRIORITY_INIT";
3502     case SHT_IA_64_VMS_TRACE:          return "VMS_TRACE";
3503     case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
3504     case SHT_IA_64_VMS_DEBUG:          return "VMS_DEBUG";
3505     case SHT_IA_64_VMS_DEBUG_STR:      return "VMS_DEBUG_STR";
3506     case SHT_IA_64_VMS_LINKAGES:       return "VMS_LINKAGES";
3507     case SHT_IA_64_VMS_SYMBOL_VECTOR:  return "VMS_SYMBOL_VECTOR";
3508     case SHT_IA_64_VMS_FIXUP:          return "VMS_FIXUP";
3509     default:
3510       break;
3511     }
3512   return NULL;
3513 }
3514 
3515 static const char *
get_x86_64_section_type_name(unsigned int sh_type)3516 get_x86_64_section_type_name (unsigned int sh_type)
3517 {
3518   switch (sh_type)
3519     {
3520     case SHT_X86_64_UNWIND:	return "X86_64_UNWIND";
3521     default:
3522       break;
3523     }
3524   return NULL;
3525 }
3526 
3527 static const char *
get_aarch64_section_type_name(unsigned int sh_type)3528 get_aarch64_section_type_name (unsigned int sh_type)
3529 {
3530   switch (sh_type)
3531     {
3532     case SHT_AARCH64_ATTRIBUTES:
3533       return "AARCH64_ATTRIBUTES";
3534     default:
3535       break;
3536     }
3537   return NULL;
3538 }
3539 
3540 static const char *
get_arm_section_type_name(unsigned int sh_type)3541 get_arm_section_type_name (unsigned int sh_type)
3542 {
3543   switch (sh_type)
3544     {
3545     case SHT_ARM_EXIDX:           return "ARM_EXIDX";
3546     case SHT_ARM_PREEMPTMAP:      return "ARM_PREEMPTMAP";
3547     case SHT_ARM_ATTRIBUTES:      return "ARM_ATTRIBUTES";
3548     case SHT_ARM_DEBUGOVERLAY:    return "ARM_DEBUGOVERLAY";
3549     case SHT_ARM_OVERLAYSECTION:  return "ARM_OVERLAYSECTION";
3550     default:
3551       break;
3552     }
3553   return NULL;
3554 }
3555 
3556 static const char *
get_tic6x_section_type_name(unsigned int sh_type)3557 get_tic6x_section_type_name (unsigned int sh_type)
3558 {
3559   switch (sh_type)
3560     {
3561     case SHT_C6000_UNWIND:
3562       return "C6000_UNWIND";
3563     case SHT_C6000_PREEMPTMAP:
3564       return "C6000_PREEMPTMAP";
3565     case SHT_C6000_ATTRIBUTES:
3566       return "C6000_ATTRIBUTES";
3567     case SHT_TI_ICODE:
3568       return "TI_ICODE";
3569     case SHT_TI_XREF:
3570       return "TI_XREF";
3571     case SHT_TI_HANDLER:
3572       return "TI_HANDLER";
3573     case SHT_TI_INITINFO:
3574       return "TI_INITINFO";
3575     case SHT_TI_PHATTRS:
3576       return "TI_PHATTRS";
3577     default:
3578       break;
3579     }
3580   return NULL;
3581 }
3582 
3583 static const char *
get_msp430x_section_type_name(unsigned int sh_type)3584 get_msp430x_section_type_name (unsigned int sh_type)
3585 {
3586   switch (sh_type)
3587     {
3588     case SHT_MSP430_SEC_FLAGS:   return "MSP430_SEC_FLAGS";
3589     case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
3590     case SHT_MSP430_ATTRIBUTES:  return "MSP430_ATTRIBUTES";
3591     default: return NULL;
3592     }
3593 }
3594 
3595 static const char *
get_section_type_name(unsigned int sh_type)3596 get_section_type_name (unsigned int sh_type)
3597 {
3598   static char buff[32];
3599 
3600   switch (sh_type)
3601     {
3602     case SHT_NULL:		return "NULL";
3603     case SHT_PROGBITS:		return "PROGBITS";
3604     case SHT_SYMTAB:		return "SYMTAB";
3605     case SHT_STRTAB:		return "STRTAB";
3606     case SHT_RELA:		return "RELA";
3607     case SHT_HASH:		return "HASH";
3608     case SHT_DYNAMIC:		return "DYNAMIC";
3609     case SHT_NOTE:		return "NOTE";
3610     case SHT_NOBITS:		return "NOBITS";
3611     case SHT_REL:		return "REL";
3612     case SHT_SHLIB:		return "SHLIB";
3613     case SHT_DYNSYM:		return "DYNSYM";
3614     case SHT_INIT_ARRAY:	return "INIT_ARRAY";
3615     case SHT_FINI_ARRAY:	return "FINI_ARRAY";
3616     case SHT_PREINIT_ARRAY:	return "PREINIT_ARRAY";
3617     case SHT_GNU_HASH:		return "GNU_HASH";
3618     case SHT_GROUP:		return "GROUP";
3619     case SHT_SYMTAB_SHNDX:	return "SYMTAB SECTION INDICIES";
3620     case SHT_GNU_verdef:	return "VERDEF";
3621     case SHT_GNU_verneed:	return "VERNEED";
3622     case SHT_GNU_versym:	return "VERSYM";
3623     case 0x6ffffff0:		return "VERSYM";
3624     case 0x6ffffffc:		return "VERDEF";
3625     case 0x7ffffffd:		return "AUXILIARY";
3626     case 0x7fffffff:		return "FILTER";
3627     case SHT_GNU_LIBLIST:	return "GNU_LIBLIST";
3628 
3629     default:
3630       if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
3631 	{
3632 	  const char * result;
3633 
3634 	  switch (elf_header.e_machine)
3635 	    {
3636 	    case EM_MIPS:
3637 	    case EM_MIPS_RS3_LE:
3638 	      result = get_mips_section_type_name (sh_type);
3639 	      break;
3640 	    case EM_PARISC:
3641 	      result = get_parisc_section_type_name (sh_type);
3642 	      break;
3643 	    case EM_IA_64:
3644 	      result = get_ia64_section_type_name (sh_type);
3645 	      break;
3646 	    case EM_X86_64:
3647 	    case EM_L1OM:
3648 	    case EM_K1OM:
3649 	      result = get_x86_64_section_type_name (sh_type);
3650 	      break;
3651 	    case EM_AARCH64:
3652 	      result = get_aarch64_section_type_name (sh_type);
3653 	      break;
3654 	    case EM_ARM:
3655 	      result = get_arm_section_type_name (sh_type);
3656 	      break;
3657 	    case EM_TI_C6000:
3658 	      result = get_tic6x_section_type_name (sh_type);
3659 	      break;
3660 	    case EM_MSP430:
3661 	      result = get_msp430x_section_type_name (sh_type);
3662 	      break;
3663 	    default:
3664 	      result = NULL;
3665 	      break;
3666 	    }
3667 
3668 	  if (result != NULL)
3669 	    return result;
3670 
3671 	  sprintf (buff, "LOPROC+%x", sh_type - SHT_LOPROC);
3672 	}
3673       else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
3674 	{
3675 	  const char * result;
3676 
3677 	  switch (elf_header.e_machine)
3678 	    {
3679 	    case EM_IA_64:
3680 	      result = get_ia64_section_type_name (sh_type);
3681 	      break;
3682 	    default:
3683 	      result = NULL;
3684 	      break;
3685 	    }
3686 
3687 	  if (result != NULL)
3688 	    return result;
3689 
3690 	  sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
3691 	}
3692       else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
3693 	sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
3694       else
3695 	/* This message is probably going to be displayed in a 15
3696 	   character wide field, so put the hex value first.  */
3697 	snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
3698 
3699       return buff;
3700     }
3701 }
3702 
3703 #define OPTION_DEBUG_DUMP	512
3704 #define OPTION_DYN_SYMS		513
3705 #define OPTION_DWARF_DEPTH	514
3706 #define OPTION_DWARF_START	515
3707 #define OPTION_DWARF_CHECK	516
3708 
3709 static struct option options[] =
3710 {
3711   {"all",	       no_argument, 0, 'a'},
3712   {"file-header",      no_argument, 0, 'h'},
3713   {"program-headers",  no_argument, 0, 'l'},
3714   {"headers",	       no_argument, 0, 'e'},
3715   {"histogram",	       no_argument, 0, 'I'},
3716   {"segments",	       no_argument, 0, 'l'},
3717   {"sections",	       no_argument, 0, 'S'},
3718   {"section-headers",  no_argument, 0, 'S'},
3719   {"section-groups",   no_argument, 0, 'g'},
3720   {"section-details",  no_argument, 0, 't'},
3721   {"full-section-name",no_argument, 0, 'N'},
3722   {"symbols",	       no_argument, 0, 's'},
3723   {"syms",	       no_argument, 0, 's'},
3724   {"dyn-syms",	       no_argument, 0, OPTION_DYN_SYMS},
3725   {"relocs",	       no_argument, 0, 'r'},
3726   {"notes",	       no_argument, 0, 'n'},
3727   {"dynamic",	       no_argument, 0, 'd'},
3728   {"arch-specific",    no_argument, 0, 'A'},
3729   {"version-info",     no_argument, 0, 'V'},
3730   {"use-dynamic",      no_argument, 0, 'D'},
3731   {"unwind",	       no_argument, 0, 'u'},
3732   {"archive-index",    no_argument, 0, 'c'},
3733   {"hex-dump",	       required_argument, 0, 'x'},
3734   {"relocated-dump",   required_argument, 0, 'R'},
3735   {"string-dump",      required_argument, 0, 'p'},
3736 #ifdef SUPPORT_DISASSEMBLY
3737   {"instruction-dump", required_argument, 0, 'i'},
3738 #endif
3739   {"debug-dump",       optional_argument, 0, OPTION_DEBUG_DUMP},
3740 
3741   {"dwarf-depth",      required_argument, 0, OPTION_DWARF_DEPTH},
3742   {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
3743   {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
3744 
3745   {"version",	       no_argument, 0, 'v'},
3746   {"wide",	       no_argument, 0, 'W'},
3747   {"help",	       no_argument, 0, 'H'},
3748   {0,		       no_argument, 0, 0}
3749 };
3750 
3751 static void
usage(FILE * stream)3752 usage (FILE * stream)
3753 {
3754   fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
3755   fprintf (stream, _(" Display information about the contents of ELF format files\n"));
3756   fprintf (stream, _(" Options are:\n\
3757   -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n\
3758   -h --file-header       Display the ELF file header\n\
3759   -l --program-headers   Display the program headers\n\
3760      --segments          An alias for --program-headers\n\
3761   -S --section-headers   Display the sections' header\n\
3762      --sections          An alias for --section-headers\n\
3763   -g --section-groups    Display the section groups\n\
3764   -t --section-details   Display the section details\n\
3765   -e --headers           Equivalent to: -h -l -S\n\
3766   -s --syms              Display the symbol table\n\
3767      --symbols           An alias for --syms\n\
3768   --dyn-syms             Display the dynamic symbol table\n\
3769   -n --notes             Display the core notes (if present)\n\
3770   -r --relocs            Display the relocations (if present)\n\
3771   -u --unwind            Display the unwind info (if present)\n\
3772   -d --dynamic           Display the dynamic section (if present)\n\
3773   -V --version-info      Display the version sections (if present)\n\
3774   -A --arch-specific     Display architecture specific information (if any)\n\
3775   -c --archive-index     Display the symbol/file index in an archive\n\
3776   -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
3777   -x --hex-dump=<number|name>\n\
3778                          Dump the contents of section <number|name> as bytes\n\
3779   -p --string-dump=<number|name>\n\
3780                          Dump the contents of section <number|name> as strings\n\
3781   -R --relocated-dump=<number|name>\n\
3782                          Dump the contents of section <number|name> as relocated bytes\n\
3783   -w[lLiaprmfFsoRt] or\n\
3784   --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
3785                =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
3786                =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
3787                =addr,=cu_index]\n\
3788                          Display the contents of DWARF2 debug sections\n"));
3789   fprintf (stream, _("\
3790   --dwarf-depth=N        Do not display DIEs at depth N or greater\n\
3791   --dwarf-start=N        Display DIEs starting with N, at the same depth\n\
3792                          or deeper\n"));
3793 #ifdef SUPPORT_DISASSEMBLY
3794   fprintf (stream, _("\
3795   -i --instruction-dump=<number|name>\n\
3796                          Disassemble the contents of section <number|name>\n"));
3797 #endif
3798   fprintf (stream, _("\
3799   -I --histogram         Display histogram of bucket list lengths\n\
3800   -W --wide              Allow output width to exceed 80 characters\n\
3801   @<file>                Read options from <file>\n\
3802   -H --help              Display this information\n\
3803   -v --version           Display the version number of readelf\n"));
3804 
3805   if (REPORT_BUGS_TO[0] && stream == stdout)
3806     fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
3807 
3808   exit (stream == stdout ? 0 : 1);
3809 }
3810 
3811 /* Record the fact that the user wants the contents of section number
3812    SECTION to be displayed using the method(s) encoded as flags bits
3813    in TYPE.  Note, TYPE can be zero if we are creating the array for
3814    the first time.  */
3815 
3816 static void
request_dump_bynumber(unsigned int section,dump_type type)3817 request_dump_bynumber (unsigned int section, dump_type type)
3818 {
3819   if (section >= num_dump_sects)
3820     {
3821       dump_type * new_dump_sects;
3822 
3823       new_dump_sects = (dump_type *) calloc (section + 1,
3824                                              sizeof (* dump_sects));
3825 
3826       if (new_dump_sects == NULL)
3827 	error (_("Out of memory allocating dump request table.\n"));
3828       else
3829 	{
3830 	  /* Copy current flag settings.  */
3831 	  memcpy (new_dump_sects, dump_sects, num_dump_sects * sizeof (* dump_sects));
3832 
3833 	  free (dump_sects);
3834 
3835 	  dump_sects = new_dump_sects;
3836 	  num_dump_sects = section + 1;
3837 	}
3838     }
3839 
3840   if (dump_sects)
3841     dump_sects[section] |= type;
3842 
3843   return;
3844 }
3845 
3846 /* Request a dump by section name.  */
3847 
3848 static void
request_dump_byname(const char * section,dump_type type)3849 request_dump_byname (const char * section, dump_type type)
3850 {
3851   struct dump_list_entry * new_request;
3852 
3853   new_request = (struct dump_list_entry *)
3854       malloc (sizeof (struct dump_list_entry));
3855   if (!new_request)
3856     error (_("Out of memory allocating dump request table.\n"));
3857 
3858   new_request->name = strdup (section);
3859   if (!new_request->name)
3860     error (_("Out of memory allocating dump request table.\n"));
3861 
3862   new_request->type = type;
3863 
3864   new_request->next = dump_sects_byname;
3865   dump_sects_byname = new_request;
3866 }
3867 
3868 static inline void
request_dump(dump_type type)3869 request_dump (dump_type type)
3870 {
3871   int section;
3872   char * cp;
3873 
3874   do_dump++;
3875   section = strtoul (optarg, & cp, 0);
3876 
3877   if (! *cp && section >= 0)
3878     request_dump_bynumber (section, type);
3879   else
3880     request_dump_byname (optarg, type);
3881 }
3882 
3883 
3884 static void
parse_args(int argc,char ** argv)3885 parse_args (int argc, char ** argv)
3886 {
3887   int c;
3888 
3889   if (argc < 2)
3890     usage (stderr);
3891 
3892   while ((c = getopt_long
3893 	  (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:", options, NULL)) != EOF)
3894     {
3895       switch (c)
3896 	{
3897 	case 0:
3898 	  /* Long options.  */
3899 	  break;
3900 	case 'H':
3901 	  usage (stdout);
3902 	  break;
3903 
3904 	case 'a':
3905 	  do_syms++;
3906 	  do_reloc++;
3907 	  do_unwind++;
3908 	  do_dynamic++;
3909 	  do_header++;
3910 	  do_sections++;
3911 	  do_section_groups++;
3912 	  do_segments++;
3913 	  do_version++;
3914 	  do_histogram++;
3915 	  do_arch++;
3916 	  do_notes++;
3917 	  break;
3918 	case 'g':
3919 	  do_section_groups++;
3920 	  break;
3921 	case 't':
3922 	case 'N':
3923 	  do_sections++;
3924 	  do_section_details++;
3925 	  break;
3926 	case 'e':
3927 	  do_header++;
3928 	  do_sections++;
3929 	  do_segments++;
3930 	  break;
3931 	case 'A':
3932 	  do_arch++;
3933 	  break;
3934 	case 'D':
3935 	  do_using_dynamic++;
3936 	  break;
3937 	case 'r':
3938 	  do_reloc++;
3939 	  break;
3940 	case 'u':
3941 	  do_unwind++;
3942 	  break;
3943 	case 'h':
3944 	  do_header++;
3945 	  break;
3946 	case 'l':
3947 	  do_segments++;
3948 	  break;
3949 	case 's':
3950 	  do_syms++;
3951 	  break;
3952 	case 'S':
3953 	  do_sections++;
3954 	  break;
3955 	case 'd':
3956 	  do_dynamic++;
3957 	  break;
3958 	case 'I':
3959 	  do_histogram++;
3960 	  break;
3961 	case 'n':
3962 	  do_notes++;
3963 	  break;
3964 	case 'c':
3965 	  do_archive_index++;
3966 	  break;
3967 	case 'x':
3968 	  request_dump (HEX_DUMP);
3969 	  break;
3970 	case 'p':
3971 	  request_dump (STRING_DUMP);
3972 	  break;
3973 	case 'R':
3974 	  request_dump (RELOC_DUMP);
3975 	  break;
3976 	case 'w':
3977 	  do_dump++;
3978 	  if (optarg == 0)
3979 	    {
3980 	      do_debugging = 1;
3981 	      dwarf_select_sections_all ();
3982 	    }
3983 	  else
3984 	    {
3985 	      do_debugging = 0;
3986 	      dwarf_select_sections_by_letters (optarg);
3987 	    }
3988 	  break;
3989 	case OPTION_DEBUG_DUMP:
3990 	  do_dump++;
3991 	  if (optarg == 0)
3992 	    do_debugging = 1;
3993 	  else
3994 	    {
3995 	      do_debugging = 0;
3996 	      dwarf_select_sections_by_names (optarg);
3997 	    }
3998 	  break;
3999 	case OPTION_DWARF_DEPTH:
4000 	  {
4001 	    char *cp;
4002 
4003 	    dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4004 	  }
4005 	  break;
4006 	case OPTION_DWARF_START:
4007 	  {
4008 	    char *cp;
4009 
4010 	    dwarf_start_die = strtoul (optarg, & cp, 0);
4011 	  }
4012 	  break;
4013 	case OPTION_DWARF_CHECK:
4014 	  dwarf_check = 1;
4015 	  break;
4016 	case OPTION_DYN_SYMS:
4017 	  do_dyn_syms++;
4018 	  break;
4019 #ifdef SUPPORT_DISASSEMBLY
4020 	case 'i':
4021 	  request_dump (DISASS_DUMP);
4022 	  break;
4023 #endif
4024 	case 'v':
4025 	  print_version (program_name);
4026 	  break;
4027 	case 'V':
4028 	  do_version++;
4029 	  break;
4030 	case 'W':
4031 	  do_wide++;
4032 	  break;
4033 	default:
4034 	  /* xgettext:c-format */
4035 	  error (_("Invalid option '-%c'\n"), c);
4036 	  /* Drop through.  */
4037 	case '?':
4038 	  usage (stderr);
4039 	}
4040     }
4041 
4042   if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
4043       && !do_segments && !do_header && !do_dump && !do_version
4044       && !do_histogram && !do_debugging && !do_arch && !do_notes
4045       && !do_section_groups && !do_archive_index
4046       && !do_dyn_syms)
4047     usage (stderr);
4048   else if (argc < 3)
4049     {
4050       warn (_("Nothing to do.\n"));
4051       usage (stderr);
4052     }
4053 }
4054 
4055 static const char *
get_elf_class(unsigned int elf_class)4056 get_elf_class (unsigned int elf_class)
4057 {
4058   static char buff[32];
4059 
4060   switch (elf_class)
4061     {
4062     case ELFCLASSNONE: return _("none");
4063     case ELFCLASS32:   return "ELF32";
4064     case ELFCLASS64:   return "ELF64";
4065     default:
4066       snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
4067       return buff;
4068     }
4069 }
4070 
4071 static const char *
get_data_encoding(unsigned int encoding)4072 get_data_encoding (unsigned int encoding)
4073 {
4074   static char buff[32];
4075 
4076   switch (encoding)
4077     {
4078     case ELFDATANONE: return _("none");
4079     case ELFDATA2LSB: return _("2's complement, little endian");
4080     case ELFDATA2MSB: return _("2's complement, big endian");
4081     default:
4082       snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
4083       return buff;
4084     }
4085 }
4086 
4087 /* Decode the data held in 'elf_header'.  */
4088 
4089 static int
process_file_header(void)4090 process_file_header (void)
4091 {
4092   if (   elf_header.e_ident[EI_MAG0] != ELFMAG0
4093       || elf_header.e_ident[EI_MAG1] != ELFMAG1
4094       || elf_header.e_ident[EI_MAG2] != ELFMAG2
4095       || elf_header.e_ident[EI_MAG3] != ELFMAG3)
4096     {
4097       error
4098 	(_("Not an ELF file - it has the wrong magic bytes at the start\n"));
4099       return 0;
4100     }
4101 
4102   init_dwarf_regnames (elf_header.e_machine);
4103 
4104   if (do_header)
4105     {
4106       int i;
4107 
4108       printf (_("ELF Header:\n"));
4109       printf (_("  Magic:   "));
4110       for (i = 0; i < EI_NIDENT; i++)
4111 	printf ("%2.2x ", elf_header.e_ident[i]);
4112       printf ("\n");
4113       printf (_("  Class:                             %s\n"),
4114 	      get_elf_class (elf_header.e_ident[EI_CLASS]));
4115       printf (_("  Data:                              %s\n"),
4116 	      get_data_encoding (elf_header.e_ident[EI_DATA]));
4117       printf (_("  Version:                           %d %s\n"),
4118 	      elf_header.e_ident[EI_VERSION],
4119 	      (elf_header.e_ident[EI_VERSION] == EV_CURRENT
4120 	       ? "(current)"
4121 	       : (elf_header.e_ident[EI_VERSION] != EV_NONE
4122 		  ? _("<unknown: %lx>")
4123 		  : "")));
4124       printf (_("  OS/ABI:                            %s\n"),
4125 	      get_osabi_name (elf_header.e_ident[EI_OSABI]));
4126       printf (_("  ABI Version:                       %d\n"),
4127 	      elf_header.e_ident[EI_ABIVERSION]);
4128       printf (_("  Type:                              %s\n"),
4129 	      get_file_type (elf_header.e_type));
4130       printf (_("  Machine:                           %s\n"),
4131 	      get_machine_name (elf_header.e_machine));
4132       printf (_("  Version:                           0x%lx\n"),
4133 	      (unsigned long) elf_header.e_version);
4134 
4135       printf (_("  Entry point address:               "));
4136       print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
4137       printf (_("\n  Start of program headers:          "));
4138       print_vma ((bfd_vma) elf_header.e_phoff, DEC);
4139       printf (_(" (bytes into file)\n  Start of section headers:          "));
4140       print_vma ((bfd_vma) elf_header.e_shoff, DEC);
4141       printf (_(" (bytes into file)\n"));
4142 
4143       printf (_("  Flags:                             0x%lx%s\n"),
4144 	      (unsigned long) elf_header.e_flags,
4145 	      get_machine_flags (elf_header.e_flags, elf_header.e_machine));
4146       printf (_("  Size of this header:               %ld (bytes)\n"),
4147 	      (long) elf_header.e_ehsize);
4148       printf (_("  Size of program headers:           %ld (bytes)\n"),
4149 	      (long) elf_header.e_phentsize);
4150       printf (_("  Number of program headers:         %ld"),
4151 	      (long) elf_header.e_phnum);
4152       if (section_headers != NULL
4153 	  && elf_header.e_phnum == PN_XNUM
4154 	  && section_headers[0].sh_info != 0)
4155 	printf (" (%ld)", (long) section_headers[0].sh_info);
4156       putc ('\n', stdout);
4157       printf (_("  Size of section headers:           %ld (bytes)\n"),
4158 	      (long) elf_header.e_shentsize);
4159       printf (_("  Number of section headers:         %ld"),
4160 	      (long) elf_header.e_shnum);
4161       if (section_headers != NULL && elf_header.e_shnum == SHN_UNDEF)
4162 	printf (" (%ld)", (long) section_headers[0].sh_size);
4163       putc ('\n', stdout);
4164       printf (_("  Section header string table index: %ld"),
4165 	      (long) elf_header.e_shstrndx);
4166       if (section_headers != NULL
4167 	  && elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
4168 	printf (" (%u)", section_headers[0].sh_link);
4169       else if (elf_header.e_shstrndx != SHN_UNDEF
4170 	       && elf_header.e_shstrndx >= elf_header.e_shnum)
4171 	printf (_(" <corrupt: out of range>"));
4172       putc ('\n', stdout);
4173     }
4174 
4175   if (section_headers != NULL)
4176     {
4177       if (elf_header.e_phnum == PN_XNUM
4178 	  && section_headers[0].sh_info != 0)
4179 	elf_header.e_phnum = section_headers[0].sh_info;
4180       if (elf_header.e_shnum == SHN_UNDEF)
4181 	elf_header.e_shnum = section_headers[0].sh_size;
4182       if (elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
4183 	elf_header.e_shstrndx = section_headers[0].sh_link;
4184       else if (elf_header.e_shstrndx >= elf_header.e_shnum)
4185 	elf_header.e_shstrndx = SHN_UNDEF;
4186       free (section_headers);
4187       section_headers = NULL;
4188     }
4189 
4190   return 1;
4191 }
4192 
4193 static bfd_boolean
get_32bit_program_headers(FILE * file,Elf_Internal_Phdr * pheaders)4194 get_32bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
4195 {
4196   Elf32_External_Phdr * phdrs;
4197   Elf32_External_Phdr * external;
4198   Elf_Internal_Phdr *   internal;
4199   unsigned int i;
4200   unsigned int size = elf_header.e_phentsize;
4201   unsigned int num  = elf_header.e_phnum;
4202 
4203   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4204   if (size == 0 || num == 0)
4205     return FALSE;
4206   if (size < sizeof * phdrs)
4207     {
4208       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4209       return FALSE;
4210     }
4211   if (size > sizeof * phdrs)
4212     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4213 
4214   phdrs = (Elf32_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
4215                                             size, num, _("program headers"));
4216   if (phdrs == NULL)
4217     return FALSE;
4218 
4219   for (i = 0, internal = pheaders, external = phdrs;
4220        i < elf_header.e_phnum;
4221        i++, internal++, external++)
4222     {
4223       internal->p_type   = BYTE_GET (external->p_type);
4224       internal->p_offset = BYTE_GET (external->p_offset);
4225       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4226       internal->p_paddr  = BYTE_GET (external->p_paddr);
4227       internal->p_filesz = BYTE_GET (external->p_filesz);
4228       internal->p_memsz  = BYTE_GET (external->p_memsz);
4229       internal->p_flags  = BYTE_GET (external->p_flags);
4230       internal->p_align  = BYTE_GET (external->p_align);
4231     }
4232 
4233   free (phdrs);
4234   return TRUE;
4235 }
4236 
4237 static bfd_boolean
get_64bit_program_headers(FILE * file,Elf_Internal_Phdr * pheaders)4238 get_64bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
4239 {
4240   Elf64_External_Phdr * phdrs;
4241   Elf64_External_Phdr * external;
4242   Elf_Internal_Phdr *   internal;
4243   unsigned int i;
4244   unsigned int size = elf_header.e_phentsize;
4245   unsigned int num  = elf_header.e_phnum;
4246 
4247   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4248   if (size == 0 || num == 0)
4249     return FALSE;
4250   if (size < sizeof * phdrs)
4251     {
4252       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4253       return FALSE;
4254     }
4255   if (size > sizeof * phdrs)
4256     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4257 
4258   phdrs = (Elf64_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
4259                                             size, num, _("program headers"));
4260   if (!phdrs)
4261     return FALSE;
4262 
4263   for (i = 0, internal = pheaders, external = phdrs;
4264        i < elf_header.e_phnum;
4265        i++, internal++, external++)
4266     {
4267       internal->p_type   = BYTE_GET (external->p_type);
4268       internal->p_flags  = BYTE_GET (external->p_flags);
4269       internal->p_offset = BYTE_GET (external->p_offset);
4270       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4271       internal->p_paddr  = BYTE_GET (external->p_paddr);
4272       internal->p_filesz = BYTE_GET (external->p_filesz);
4273       internal->p_memsz  = BYTE_GET (external->p_memsz);
4274       internal->p_align  = BYTE_GET (external->p_align);
4275     }
4276 
4277   free (phdrs);
4278   return TRUE;
4279 }
4280 
4281 /* Returns 1 if the program headers were read into `program_headers'.  */
4282 
4283 static int
get_program_headers(FILE * file)4284 get_program_headers (FILE * file)
4285 {
4286   Elf_Internal_Phdr * phdrs;
4287 
4288   /* Check cache of prior read.  */
4289   if (program_headers != NULL)
4290     return 1;
4291 
4292   phdrs = (Elf_Internal_Phdr *) cmalloc (elf_header.e_phnum,
4293                                          sizeof (Elf_Internal_Phdr));
4294 
4295   if (phdrs == NULL)
4296     {
4297       error (_("Out of memory reading %u program headers\n"),
4298 	     elf_header.e_phnum);
4299       return 0;
4300     }
4301 
4302   if (is_32bit_elf
4303       ? get_32bit_program_headers (file, phdrs)
4304       : get_64bit_program_headers (file, phdrs))
4305     {
4306       program_headers = phdrs;
4307       return 1;
4308     }
4309 
4310   free (phdrs);
4311   return 0;
4312 }
4313 
4314 /* Returns 1 if the program headers were loaded.  */
4315 
4316 static int
process_program_headers(FILE * file)4317 process_program_headers (FILE * file)
4318 {
4319   Elf_Internal_Phdr * segment;
4320   unsigned int i;
4321 
4322   if (elf_header.e_phnum == 0)
4323     {
4324       /* PR binutils/12467.  */
4325       if (elf_header.e_phoff != 0)
4326 	warn (_("possibly corrupt ELF header - it has a non-zero program"
4327 		" header offset, but no program headers"));
4328       else if (do_segments)
4329 	printf (_("\nThere are no program headers in this file.\n"));
4330       return 0;
4331     }
4332 
4333   if (do_segments && !do_header)
4334     {
4335       printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
4336       printf (_("Entry point "));
4337       print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
4338       printf (_("\nThere are %d program headers, starting at offset "),
4339 	      elf_header.e_phnum);
4340       print_vma ((bfd_vma) elf_header.e_phoff, DEC);
4341       printf ("\n");
4342     }
4343 
4344   if (! get_program_headers (file))
4345       return 0;
4346 
4347   if (do_segments)
4348     {
4349       if (elf_header.e_phnum > 1)
4350 	printf (_("\nProgram Headers:\n"));
4351       else
4352 	printf (_("\nProgram Headers:\n"));
4353 
4354       if (is_32bit_elf)
4355 	printf
4356 	  (_("  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n"));
4357       else if (do_wide)
4358 	printf
4359 	  (_("  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align\n"));
4360       else
4361 	{
4362 	  printf
4363 	    (_("  Type           Offset             VirtAddr           PhysAddr\n"));
4364 	  printf
4365 	    (_("                 FileSiz            MemSiz              Flags  Align\n"));
4366 	}
4367     }
4368 
4369   dynamic_addr = 0;
4370   dynamic_size = 0;
4371 
4372   for (i = 0, segment = program_headers;
4373        i < elf_header.e_phnum;
4374        i++, segment++)
4375     {
4376       if (do_segments)
4377 	{
4378 	  printf ("  %-14.14s ", get_segment_type (segment->p_type));
4379 
4380 	  if (is_32bit_elf)
4381 	    {
4382 	      printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
4383 	      printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
4384 	      printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
4385 	      printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
4386 	      printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
4387 	      printf ("%c%c%c ",
4388 		      (segment->p_flags & PF_R ? 'R' : ' '),
4389 		      (segment->p_flags & PF_W ? 'W' : ' '),
4390 		      (segment->p_flags & PF_X ? 'E' : ' '));
4391 	      printf ("%#lx", (unsigned long) segment->p_align);
4392 	    }
4393 	  else if (do_wide)
4394 	    {
4395 	      if ((unsigned long) segment->p_offset == segment->p_offset)
4396 		printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
4397 	      else
4398 		{
4399 		  print_vma (segment->p_offset, FULL_HEX);
4400 		  putchar (' ');
4401 		}
4402 
4403 	      print_vma (segment->p_vaddr, FULL_HEX);
4404 	      putchar (' ');
4405 	      print_vma (segment->p_paddr, FULL_HEX);
4406 	      putchar (' ');
4407 
4408 	      if ((unsigned long) segment->p_filesz == segment->p_filesz)
4409 		printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
4410 	      else
4411 		{
4412 		  print_vma (segment->p_filesz, FULL_HEX);
4413 		  putchar (' ');
4414 		}
4415 
4416 	      if ((unsigned long) segment->p_memsz == segment->p_memsz)
4417 		printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
4418 	      else
4419 		{
4420 		  print_vma (segment->p_memsz, FULL_HEX);
4421 		}
4422 
4423 	      printf (" %c%c%c ",
4424 		      (segment->p_flags & PF_R ? 'R' : ' '),
4425 		      (segment->p_flags & PF_W ? 'W' : ' '),
4426 		      (segment->p_flags & PF_X ? 'E' : ' '));
4427 
4428 	      if ((unsigned long) segment->p_align == segment->p_align)
4429 		printf ("%#lx", (unsigned long) segment->p_align);
4430 	      else
4431 		{
4432 		  print_vma (segment->p_align, PREFIX_HEX);
4433 		}
4434 	    }
4435 	  else
4436 	    {
4437 	      print_vma (segment->p_offset, FULL_HEX);
4438 	      putchar (' ');
4439 	      print_vma (segment->p_vaddr, FULL_HEX);
4440 	      putchar (' ');
4441 	      print_vma (segment->p_paddr, FULL_HEX);
4442 	      printf ("\n                 ");
4443 	      print_vma (segment->p_filesz, FULL_HEX);
4444 	      putchar (' ');
4445 	      print_vma (segment->p_memsz, FULL_HEX);
4446 	      printf ("  %c%c%c    ",
4447 		      (segment->p_flags & PF_R ? 'R' : ' '),
4448 		      (segment->p_flags & PF_W ? 'W' : ' '),
4449 		      (segment->p_flags & PF_X ? 'E' : ' '));
4450 	      print_vma (segment->p_align, HEX);
4451 	    }
4452 	}
4453 
4454       if (do_segments)
4455 	putc ('\n', stdout);
4456 
4457       switch (segment->p_type)
4458 	{
4459 	case PT_DYNAMIC:
4460 	  if (dynamic_addr)
4461 	    error (_("more than one dynamic segment\n"));
4462 
4463 	  /* By default, assume that the .dynamic section is the first
4464 	     section in the DYNAMIC segment.  */
4465 	  dynamic_addr = segment->p_offset;
4466 	  dynamic_size = segment->p_filesz;
4467 	  /* PR binutils/17512: Avoid corrupt dynamic section info in the segment.  */
4468 	  if (dynamic_addr + dynamic_size >= current_file_size)
4469 	    {
4470 	      error (_("the dynamic segment offset + size exceeds the size of the file\n"));
4471 	      dynamic_addr = dynamic_size = 0;
4472 	    }
4473 
4474 	  /* Try to locate the .dynamic section. If there is
4475 	     a section header table, we can easily locate it.  */
4476 	  if (section_headers != NULL)
4477 	    {
4478 	      Elf_Internal_Shdr * sec;
4479 
4480 	      sec = find_section (".dynamic");
4481 	      if (sec == NULL || sec->sh_size == 0)
4482 		{
4483                   /* A corresponding .dynamic section is expected, but on
4484                      IA-64/OpenVMS it is OK for it to be missing.  */
4485                   if (!is_ia64_vms ())
4486                     error (_("no .dynamic section in the dynamic segment\n"));
4487 		  break;
4488 		}
4489 
4490 	      if (sec->sh_type == SHT_NOBITS)
4491 		{
4492 		  dynamic_size = 0;
4493 		  break;
4494 		}
4495 
4496 	      dynamic_addr = sec->sh_offset;
4497 	      dynamic_size = sec->sh_size;
4498 
4499 	      if (dynamic_addr < segment->p_offset
4500 		  || dynamic_addr > segment->p_offset + segment->p_filesz)
4501 		warn (_("the .dynamic section is not contained"
4502 			" within the dynamic segment\n"));
4503 	      else if (dynamic_addr > segment->p_offset)
4504 		warn (_("the .dynamic section is not the first section"
4505 			" in the dynamic segment.\n"));
4506 	    }
4507 	  break;
4508 
4509 	case PT_INTERP:
4510 	  if (fseek (file, archive_file_offset + (long) segment->p_offset,
4511 		     SEEK_SET))
4512 	    error (_("Unable to find program interpreter name\n"));
4513 	  else
4514 	    {
4515 	      char fmt [32];
4516 	      int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
4517 
4518 	      if (ret >= (int) sizeof (fmt) || ret < 0)
4519 		error (_("Internal error: failed to create format string to display program interpreter\n"));
4520 
4521 	      program_interpreter[0] = 0;
4522 	      if (fscanf (file, fmt, program_interpreter) <= 0)
4523 		error (_("Unable to read program interpreter name\n"));
4524 
4525 	      if (do_segments)
4526 		printf (_("      [Requesting program interpreter: %s]\n"),
4527 		    program_interpreter);
4528 	    }
4529 	  break;
4530 	}
4531     }
4532 
4533   if (do_segments && section_headers != NULL && string_table != NULL)
4534     {
4535       printf (_("\n Section to Segment mapping:\n"));
4536       printf (_("  Segment Sections...\n"));
4537 
4538       for (i = 0; i < elf_header.e_phnum; i++)
4539 	{
4540 	  unsigned int j;
4541 	  Elf_Internal_Shdr * section;
4542 
4543 	  segment = program_headers + i;
4544 	  section = section_headers + 1;
4545 
4546 	  printf ("   %2.2d     ", i);
4547 
4548 	  for (j = 1; j < elf_header.e_shnum; j++, section++)
4549 	    {
4550 	      if (!ELF_TBSS_SPECIAL (section, segment)
4551 		  && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
4552 		printf ("%s ", printable_section_name (section));
4553 	    }
4554 
4555 	  putc ('\n',stdout);
4556 	}
4557     }
4558 
4559   return 1;
4560 }
4561 
4562 
4563 /* Find the file offset corresponding to VMA by using the program headers.  */
4564 
4565 static long
offset_from_vma(FILE * file,bfd_vma vma,bfd_size_type size)4566 offset_from_vma (FILE * file, bfd_vma vma, bfd_size_type size)
4567 {
4568   Elf_Internal_Phdr * seg;
4569 
4570   if (! get_program_headers (file))
4571     {
4572       warn (_("Cannot interpret virtual addresses without program headers.\n"));
4573       return (long) vma;
4574     }
4575 
4576   for (seg = program_headers;
4577        seg < program_headers + elf_header.e_phnum;
4578        ++seg)
4579     {
4580       if (seg->p_type != PT_LOAD)
4581 	continue;
4582 
4583       if (vma >= (seg->p_vaddr & -seg->p_align)
4584 	  && vma + size <= seg->p_vaddr + seg->p_filesz)
4585 	return vma - seg->p_vaddr + seg->p_offset;
4586     }
4587 
4588   warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
4589 	(unsigned long) vma);
4590   return (long) vma;
4591 }
4592 
4593 
4594 /* Allocate memory and load the sections headers into the global pointer
4595    SECTION_HEADERS.  If PROBE is true, this is just a probe and we do not
4596    generate any error messages if the load fails.  */
4597 
4598 static bfd_boolean
get_32bit_section_headers(FILE * file,bfd_boolean probe)4599 get_32bit_section_headers (FILE * file, bfd_boolean probe)
4600 {
4601   Elf32_External_Shdr * shdrs;
4602   Elf_Internal_Shdr *   internal;
4603   unsigned int i;
4604   unsigned int size = elf_header.e_shentsize;
4605   unsigned int num = probe ? 1 : elf_header.e_shnum;
4606 
4607   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4608   if (size == 0 || num == 0)
4609     return FALSE;
4610   if (size < sizeof * shdrs)
4611     {
4612       if (! probe)
4613 	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
4614       return FALSE;
4615     }
4616   if (!probe && size > sizeof * shdrs)
4617     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
4618 
4619   shdrs = (Elf32_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
4620                                             size, num,
4621 					    probe ? NULL : _("section headers"));
4622   if (shdrs == NULL)
4623     return FALSE;
4624 
4625   if (section_headers != NULL)
4626     free (section_headers);
4627   section_headers = (Elf_Internal_Shdr *) cmalloc (num,
4628                                                    sizeof (Elf_Internal_Shdr));
4629   if (section_headers == NULL)
4630     {
4631       if (!probe)
4632 	error (_("Out of memory reading %u section headers\n"), num);
4633       return FALSE;
4634     }
4635 
4636   for (i = 0, internal = section_headers;
4637        i < num;
4638        i++, internal++)
4639     {
4640       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
4641       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
4642       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
4643       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
4644       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
4645       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
4646       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
4647       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
4648       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
4649       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
4650     }
4651 
4652   free (shdrs);
4653   return TRUE;
4654 }
4655 
4656 static bfd_boolean
get_64bit_section_headers(FILE * file,bfd_boolean probe)4657 get_64bit_section_headers (FILE * file, bfd_boolean probe)
4658 {
4659   Elf64_External_Shdr * shdrs;
4660   Elf_Internal_Shdr *   internal;
4661   unsigned int i;
4662   unsigned int size = elf_header.e_shentsize;
4663   unsigned int num = probe ? 1 : elf_header.e_shnum;
4664 
4665   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4666   if (size == 0 || num == 0)
4667     return FALSE;
4668   if (size < sizeof * shdrs)
4669     {
4670       if (! probe)
4671 	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
4672       return FALSE;
4673     }
4674   if (! probe && size > sizeof * shdrs)
4675     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
4676 
4677   shdrs = (Elf64_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
4678                                             size, num,
4679 					    probe ? NULL : _("section headers"));
4680   if (shdrs == NULL)
4681     return FALSE;
4682 
4683   if (section_headers != NULL)
4684     free (section_headers);
4685   section_headers = (Elf_Internal_Shdr *) cmalloc (num,
4686                                                    sizeof (Elf_Internal_Shdr));
4687   if (section_headers == NULL)
4688     {
4689       if (! probe)
4690 	error (_("Out of memory reading %u section headers\n"), num);
4691       return FALSE;
4692     }
4693 
4694   for (i = 0, internal = section_headers;
4695        i < num;
4696        i++, internal++)
4697     {
4698       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
4699       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
4700       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
4701       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
4702       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
4703       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
4704       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
4705       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
4706       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
4707       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
4708     }
4709 
4710   free (shdrs);
4711   return TRUE;
4712 }
4713 
4714 static Elf_Internal_Sym *
get_32bit_elf_symbols(FILE * file,Elf_Internal_Shdr * section,unsigned long * num_syms_return)4715 get_32bit_elf_symbols (FILE * file,
4716 		       Elf_Internal_Shdr * section,
4717 		       unsigned long * num_syms_return)
4718 {
4719   unsigned long number = 0;
4720   Elf32_External_Sym * esyms = NULL;
4721   Elf_External_Sym_Shndx * shndx = NULL;
4722   Elf_Internal_Sym * isyms = NULL;
4723   Elf_Internal_Sym * psym;
4724   unsigned int j;
4725 
4726   /* Run some sanity checks first.  */
4727   if (section->sh_entsize == 0)
4728     {
4729       error (_("sh_entsize is zero\n"));
4730       goto exit_point;
4731     }
4732 
4733   if (section->sh_size > current_file_size)
4734     {
4735       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
4736 	     printable_section_name (section), (unsigned long) section->sh_size);
4737       goto exit_point;
4738     }
4739 
4740   number = section->sh_size / section->sh_entsize;
4741 
4742   if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
4743     {
4744       error (_("Invalid sh_entsize\n"));
4745       goto exit_point;
4746     }
4747 
4748   esyms = (Elf32_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
4749                                            section->sh_size, _("symbols"));
4750   if (esyms == NULL)
4751     goto exit_point;
4752 
4753   shndx = NULL;
4754   if (symtab_shndx_hdr != NULL
4755       && (symtab_shndx_hdr->sh_link
4756 	  == (unsigned long) (section - section_headers)))
4757     {
4758       shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
4759                                                    symtab_shndx_hdr->sh_offset,
4760                                                    1, symtab_shndx_hdr->sh_size,
4761                                                    _("symbol table section indicies"));
4762       if (shndx == NULL)
4763 	goto exit_point;
4764     }
4765 
4766   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
4767 
4768   if (isyms == NULL)
4769     {
4770       error (_("Out of memory reading %lu symbols\n"),
4771 	     (unsigned long) number);
4772       goto exit_point;
4773     }
4774 
4775   for (j = 0, psym = isyms; j < number; j++, psym++)
4776     {
4777       psym->st_name  = BYTE_GET (esyms[j].st_name);
4778       psym->st_value = BYTE_GET (esyms[j].st_value);
4779       psym->st_size  = BYTE_GET (esyms[j].st_size);
4780       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
4781       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
4782 	psym->st_shndx
4783 	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4784       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
4785 	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
4786       psym->st_info  = BYTE_GET (esyms[j].st_info);
4787       psym->st_other = BYTE_GET (esyms[j].st_other);
4788     }
4789 
4790  exit_point:
4791   if (shndx != NULL)
4792     free (shndx);
4793   if (esyms != NULL)
4794     free (esyms);
4795 
4796   if (num_syms_return != NULL)
4797     * num_syms_return = isyms == NULL ? 0 : number;
4798 
4799   return isyms;
4800 }
4801 
4802 static Elf_Internal_Sym *
get_64bit_elf_symbols(FILE * file,Elf_Internal_Shdr * section,unsigned long * num_syms_return)4803 get_64bit_elf_symbols (FILE * file,
4804 		       Elf_Internal_Shdr * section,
4805 		       unsigned long * num_syms_return)
4806 {
4807   unsigned long number = 0;
4808   Elf64_External_Sym * esyms = NULL;
4809   Elf_External_Sym_Shndx * shndx = NULL;
4810   Elf_Internal_Sym * isyms = NULL;
4811   Elf_Internal_Sym * psym;
4812   unsigned int j;
4813 
4814   /* Run some sanity checks first.  */
4815   if (section->sh_entsize == 0)
4816     {
4817       error (_("sh_entsize is zero\n"));
4818       goto exit_point;
4819     }
4820 
4821   if (section->sh_size > current_file_size)
4822     {
4823       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
4824 	     printable_section_name (section), (unsigned long) section->sh_size);
4825       goto exit_point;
4826     }
4827 
4828   number = section->sh_size / section->sh_entsize;
4829 
4830   if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
4831     {
4832       error (_("Invalid sh_entsize\n"));
4833       goto exit_point;
4834     }
4835 
4836   esyms = (Elf64_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
4837                                            section->sh_size, _("symbols"));
4838   if (!esyms)
4839     goto exit_point;
4840 
4841   if (symtab_shndx_hdr != NULL
4842       && (symtab_shndx_hdr->sh_link
4843 	  == (unsigned long) (section - section_headers)))
4844     {
4845       shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
4846                                                    symtab_shndx_hdr->sh_offset,
4847                                                    1, symtab_shndx_hdr->sh_size,
4848                                                    _("symbol table section indicies"));
4849       if (shndx == NULL)
4850 	goto exit_point;
4851     }
4852 
4853   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
4854 
4855   if (isyms == NULL)
4856     {
4857       error (_("Out of memory reading %lu symbols\n"),
4858 	     (unsigned long) number);
4859       goto exit_point;
4860     }
4861 
4862   for (j = 0, psym = isyms; j < number; j++, psym++)
4863     {
4864       psym->st_name  = BYTE_GET (esyms[j].st_name);
4865       psym->st_info  = BYTE_GET (esyms[j].st_info);
4866       psym->st_other = BYTE_GET (esyms[j].st_other);
4867       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
4868 
4869       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
4870 	psym->st_shndx
4871 	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4872       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
4873 	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
4874 
4875       psym->st_value = BYTE_GET (esyms[j].st_value);
4876       psym->st_size  = BYTE_GET (esyms[j].st_size);
4877     }
4878 
4879  exit_point:
4880   if (shndx != NULL)
4881     free (shndx);
4882   if (esyms != NULL)
4883     free (esyms);
4884 
4885   if (num_syms_return != NULL)
4886     * num_syms_return = isyms == NULL ? 0 : number;
4887 
4888   return isyms;
4889 }
4890 
4891 static const char *
get_elf_section_flags(bfd_vma sh_flags)4892 get_elf_section_flags (bfd_vma sh_flags)
4893 {
4894   static char buff[1024];
4895   char * p = buff;
4896   int field_size = is_32bit_elf ? 8 : 16;
4897   int sindex;
4898   int size = sizeof (buff) - (field_size + 4 + 1);
4899   bfd_vma os_flags = 0;
4900   bfd_vma proc_flags = 0;
4901   bfd_vma unknown_flags = 0;
4902   static const struct
4903     {
4904       const char * str;
4905       int len;
4906     }
4907   flags [] =
4908     {
4909       /*  0 */ { STRING_COMMA_LEN ("WRITE") },
4910       /*  1 */ { STRING_COMMA_LEN ("ALLOC") },
4911       /*  2 */ { STRING_COMMA_LEN ("EXEC") },
4912       /*  3 */ { STRING_COMMA_LEN ("MERGE") },
4913       /*  4 */ { STRING_COMMA_LEN ("STRINGS") },
4914       /*  5 */ { STRING_COMMA_LEN ("INFO LINK") },
4915       /*  6 */ { STRING_COMMA_LEN ("LINK ORDER") },
4916       /*  7 */ { STRING_COMMA_LEN ("OS NONCONF") },
4917       /*  8 */ { STRING_COMMA_LEN ("GROUP") },
4918       /*  9 */ { STRING_COMMA_LEN ("TLS") },
4919       /* IA-64 specific.  */
4920       /* 10 */ { STRING_COMMA_LEN ("SHORT") },
4921       /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
4922       /* IA-64 OpenVMS specific.  */
4923       /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
4924       /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
4925       /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
4926       /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
4927       /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
4928       /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
4929       /* Generic.  */
4930       /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
4931       /* SPARC specific.  */
4932       /* 19 */ { STRING_COMMA_LEN ("ORDERED") }
4933     };
4934 
4935   if (do_section_details)
4936     {
4937       sprintf (buff, "[%*.*lx]: ",
4938 	       field_size, field_size, (unsigned long) sh_flags);
4939       p += field_size + 4;
4940     }
4941 
4942   while (sh_flags)
4943     {
4944       bfd_vma flag;
4945 
4946       flag = sh_flags & - sh_flags;
4947       sh_flags &= ~ flag;
4948 
4949       if (do_section_details)
4950 	{
4951 	  switch (flag)
4952 	    {
4953 	    case SHF_WRITE:		sindex = 0; break;
4954 	    case SHF_ALLOC:		sindex = 1; break;
4955 	    case SHF_EXECINSTR:		sindex = 2; break;
4956 	    case SHF_MERGE:		sindex = 3; break;
4957 	    case SHF_STRINGS:		sindex = 4; break;
4958 	    case SHF_INFO_LINK:		sindex = 5; break;
4959 	    case SHF_LINK_ORDER:	sindex = 6; break;
4960 	    case SHF_OS_NONCONFORMING:	sindex = 7; break;
4961 	    case SHF_GROUP:		sindex = 8; break;
4962 	    case SHF_TLS:		sindex = 9; break;
4963 	    case SHF_EXCLUDE:		sindex = 18; break;
4964 
4965 	    default:
4966 	      sindex = -1;
4967 	      switch (elf_header.e_machine)
4968 		{
4969 		case EM_IA_64:
4970 		  if (flag == SHF_IA_64_SHORT)
4971 		    sindex = 10;
4972 		  else if (flag == SHF_IA_64_NORECOV)
4973 		    sindex = 11;
4974 #ifdef BFD64
4975 		  else if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4976 		    switch (flag)
4977 		      {
4978 		      case SHF_IA_64_VMS_GLOBAL:      sindex = 12; break;
4979 		      case SHF_IA_64_VMS_OVERLAID:    sindex = 13; break;
4980 		      case SHF_IA_64_VMS_SHARED:      sindex = 14; break;
4981 		      case SHF_IA_64_VMS_VECTOR:      sindex = 15; break;
4982 		      case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
4983 		      case SHF_IA_64_VMS_PROTECTED:   sindex = 17; break;
4984 		      default:                        break;
4985 		      }
4986 #endif
4987 		  break;
4988 
4989 		case EM_386:
4990 		case EM_486:
4991 		case EM_X86_64:
4992 		case EM_L1OM:
4993 		case EM_K1OM:
4994 		case EM_OLD_SPARCV9:
4995 		case EM_SPARC32PLUS:
4996 		case EM_SPARCV9:
4997 		case EM_SPARC:
4998 		  if (flag == SHF_ORDERED)
4999 		    sindex = 19;
5000 		  break;
5001 		default:
5002 		  break;
5003 		}
5004 	    }
5005 
5006 	  if (sindex != -1)
5007 	    {
5008 	      if (p != buff + field_size + 4)
5009 		{
5010 		  if (size < (10 + 2))
5011 		    abort ();
5012 		  size -= 2;
5013 		  *p++ = ',';
5014 		  *p++ = ' ';
5015 		}
5016 
5017 	      size -= flags [sindex].len;
5018 	      p = stpcpy (p, flags [sindex].str);
5019 	    }
5020 	  else if (flag & SHF_MASKOS)
5021 	    os_flags |= flag;
5022 	  else if (flag & SHF_MASKPROC)
5023 	    proc_flags |= flag;
5024 	  else
5025 	    unknown_flags |= flag;
5026 	}
5027       else
5028 	{
5029 	  switch (flag)
5030 	    {
5031 	    case SHF_WRITE:		*p = 'W'; break;
5032 	    case SHF_ALLOC:		*p = 'A'; break;
5033 	    case SHF_EXECINSTR:		*p = 'X'; break;
5034 	    case SHF_MERGE:		*p = 'M'; break;
5035 	    case SHF_STRINGS:		*p = 'S'; break;
5036 	    case SHF_INFO_LINK:		*p = 'I'; break;
5037 	    case SHF_LINK_ORDER:	*p = 'L'; break;
5038 	    case SHF_OS_NONCONFORMING:	*p = 'O'; break;
5039 	    case SHF_GROUP:		*p = 'G'; break;
5040 	    case SHF_TLS:		*p = 'T'; break;
5041 	    case SHF_EXCLUDE:		*p = 'E'; break;
5042 
5043 	    default:
5044 	      if ((elf_header.e_machine == EM_X86_64
5045 		   || elf_header.e_machine == EM_L1OM
5046 		   || elf_header.e_machine == EM_K1OM)
5047 		  && flag == SHF_X86_64_LARGE)
5048 		*p = 'l';
5049 	      else if (flag & SHF_MASKOS)
5050 		{
5051 		  *p = 'o';
5052 		  sh_flags &= ~ SHF_MASKOS;
5053 		}
5054 	      else if (flag & SHF_MASKPROC)
5055 		{
5056 		  *p = 'p';
5057 		  sh_flags &= ~ SHF_MASKPROC;
5058 		}
5059 	      else
5060 		*p = 'x';
5061 	      break;
5062 	    }
5063 	  p++;
5064 	}
5065     }
5066 
5067   if (do_section_details)
5068     {
5069       if (os_flags)
5070 	{
5071 	  size -= 5 + field_size;
5072 	  if (p != buff + field_size + 4)
5073 	    {
5074 	      if (size < (2 + 1))
5075 		abort ();
5076 	      size -= 2;
5077 	      *p++ = ',';
5078 	      *p++ = ' ';
5079 	    }
5080 	  sprintf (p, "OS (%*.*lx)", field_size, field_size,
5081 		   (unsigned long) os_flags);
5082 	  p += 5 + field_size;
5083 	}
5084       if (proc_flags)
5085 	{
5086 	  size -= 7 + field_size;
5087 	  if (p != buff + field_size + 4)
5088 	    {
5089 	      if (size < (2 + 1))
5090 		abort ();
5091 	      size -= 2;
5092 	      *p++ = ',';
5093 	      *p++ = ' ';
5094 	    }
5095 	  sprintf (p, "PROC (%*.*lx)", field_size, field_size,
5096 		   (unsigned long) proc_flags);
5097 	  p += 7 + field_size;
5098 	}
5099       if (unknown_flags)
5100 	{
5101 	  size -= 10 + field_size;
5102 	  if (p != buff + field_size + 4)
5103 	    {
5104 	      if (size < (2 + 1))
5105 		abort ();
5106 	      size -= 2;
5107 	      *p++ = ',';
5108 	      *p++ = ' ';
5109 	    }
5110 	  sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
5111 		   (unsigned long) unknown_flags);
5112 	  p += 10 + field_size;
5113 	}
5114     }
5115 
5116   *p = '\0';
5117   return buff;
5118 }
5119 
5120 static int
process_section_headers(FILE * file)5121 process_section_headers (FILE * file)
5122 {
5123   Elf_Internal_Shdr * section;
5124   unsigned int i;
5125 
5126   section_headers = NULL;
5127 
5128   if (elf_header.e_shnum == 0)
5129     {
5130       /* PR binutils/12467.  */
5131       if (elf_header.e_shoff != 0)
5132 	warn (_("possibly corrupt ELF file header - it has a non-zero"
5133 		" section header offset, but no section headers\n"));
5134       else if (do_sections)
5135 	printf (_("\nThere are no sections in this file.\n"));
5136 
5137       return 1;
5138     }
5139 
5140   if (do_sections && !do_header)
5141     printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
5142 	    elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
5143 
5144   if (is_32bit_elf)
5145     {
5146       if (! get_32bit_section_headers (file, FALSE))
5147 	return 0;
5148     }
5149   else if (! get_64bit_section_headers (file, FALSE))
5150     return 0;
5151 
5152   /* Read in the string table, so that we have names to display.  */
5153   if (elf_header.e_shstrndx != SHN_UNDEF
5154        && elf_header.e_shstrndx < elf_header.e_shnum)
5155     {
5156       section = section_headers + elf_header.e_shstrndx;
5157 
5158       if (section->sh_size != 0)
5159 	{
5160 	  string_table = (char *) get_data (NULL, file, section->sh_offset,
5161                                             1, section->sh_size,
5162                                             _("string table"));
5163 
5164 	  string_table_length = string_table != NULL ? section->sh_size : 0;
5165 	}
5166     }
5167 
5168   /* Scan the sections for the dynamic symbol table
5169      and dynamic string table and debug sections.  */
5170   dynamic_symbols = NULL;
5171   dynamic_strings = NULL;
5172   dynamic_syminfo = NULL;
5173   symtab_shndx_hdr = NULL;
5174 
5175   eh_addr_size = is_32bit_elf ? 4 : 8;
5176   switch (elf_header.e_machine)
5177     {
5178     case EM_MIPS:
5179     case EM_MIPS_RS3_LE:
5180       /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
5181 	 FDE addresses.  However, the ABI also has a semi-official ILP32
5182 	 variant for which the normal FDE address size rules apply.
5183 
5184 	 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
5185 	 section, where XX is the size of longs in bits.  Unfortunately,
5186 	 earlier compilers provided no way of distinguishing ILP32 objects
5187 	 from LP64 objects, so if there's any doubt, we should assume that
5188 	 the official LP64 form is being used.  */
5189       if ((elf_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
5190 	  && find_section (".gcc_compiled_long32") == NULL)
5191 	eh_addr_size = 8;
5192       break;
5193 
5194     case EM_H8_300:
5195     case EM_H8_300H:
5196       switch (elf_header.e_flags & EF_H8_MACH)
5197 	{
5198 	case E_H8_MACH_H8300:
5199 	case E_H8_MACH_H8300HN:
5200 	case E_H8_MACH_H8300SN:
5201 	case E_H8_MACH_H8300SXN:
5202 	  eh_addr_size = 2;
5203 	  break;
5204 	case E_H8_MACH_H8300H:
5205 	case E_H8_MACH_H8300S:
5206 	case E_H8_MACH_H8300SX:
5207 	  eh_addr_size = 4;
5208 	  break;
5209 	}
5210       break;
5211 
5212     case EM_M32C_OLD:
5213     case EM_M32C:
5214       switch (elf_header.e_flags & EF_M32C_CPU_MASK)
5215 	{
5216 	case EF_M32C_CPU_M16C:
5217 	  eh_addr_size = 2;
5218 	  break;
5219 	}
5220       break;
5221     }
5222 
5223 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64)		\
5224   do									\
5225     {									\
5226       bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64;	\
5227       if (section->sh_entsize != expected_entsize)			\
5228 	{								\
5229 	  char buf[40];							\
5230 	  sprintf_vma (buf, section->sh_entsize);			\
5231 	  /* Note: coded this way so that there is a single string for  \
5232 	     translation.  */ \
5233 	  error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
5234 	  error (_("(Using the expected size of %u for the rest of this dump)\n"), \
5235 		   (unsigned) expected_entsize);			\
5236 	  section->sh_entsize = expected_entsize;			\
5237 	}								\
5238     }									\
5239   while (0)
5240 
5241 #define CHECK_ENTSIZE(section, i, type)					\
5242   CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type),	    \
5243 			sizeof (Elf64_External_##type))
5244 
5245   for (i = 0, section = section_headers;
5246        i < elf_header.e_shnum;
5247        i++, section++)
5248     {
5249       char * name = SECTION_NAME (section);
5250 
5251       if (section->sh_type == SHT_DYNSYM)
5252 	{
5253 	  if (dynamic_symbols != NULL)
5254 	    {
5255 	      error (_("File contains multiple dynamic symbol tables\n"));
5256 	      continue;
5257 	    }
5258 
5259 	  CHECK_ENTSIZE (section, i, Sym);
5260 	  dynamic_symbols = GET_ELF_SYMBOLS (file, section, & num_dynamic_syms);
5261 	}
5262       else if (section->sh_type == SHT_STRTAB
5263 	       && streq (name, ".dynstr"))
5264 	{
5265 	  if (dynamic_strings != NULL)
5266 	    {
5267 	      error (_("File contains multiple dynamic string tables\n"));
5268 	      continue;
5269 	    }
5270 
5271 	  dynamic_strings = (char *) get_data (NULL, file, section->sh_offset,
5272                                                1, section->sh_size,
5273                                                _("dynamic strings"));
5274 	  dynamic_strings_length = dynamic_strings == NULL ? 0 : section->sh_size;
5275 	}
5276       else if (section->sh_type == SHT_SYMTAB_SHNDX)
5277 	{
5278 	  if (symtab_shndx_hdr != NULL)
5279 	    {
5280 	      error (_("File contains multiple symtab shndx tables\n"));
5281 	      continue;
5282 	    }
5283 	  symtab_shndx_hdr = section;
5284 	}
5285       else if (section->sh_type == SHT_SYMTAB)
5286 	CHECK_ENTSIZE (section, i, Sym);
5287       else if (section->sh_type == SHT_GROUP)
5288 	CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
5289       else if (section->sh_type == SHT_REL)
5290 	CHECK_ENTSIZE (section, i, Rel);
5291       else if (section->sh_type == SHT_RELA)
5292 	CHECK_ENTSIZE (section, i, Rela);
5293       else if ((do_debugging || do_debug_info || do_debug_abbrevs
5294 		|| do_debug_lines || do_debug_pubnames || do_debug_pubtypes
5295 		|| do_debug_aranges || do_debug_frames || do_debug_macinfo
5296 		|| do_debug_str || do_debug_loc || do_debug_ranges
5297 		|| do_debug_addr || do_debug_cu_index)
5298 	       && (const_strneq (name, ".debug_")
5299                    || const_strneq (name, ".zdebug_")))
5300 	{
5301           if (name[1] == 'z')
5302             name += sizeof (".zdebug_") - 1;
5303           else
5304             name += sizeof (".debug_") - 1;
5305 
5306 	  if (do_debugging
5307 	      || (do_debug_info     && const_strneq (name, "info"))
5308 	      || (do_debug_info     && const_strneq (name, "types"))
5309 	      || (do_debug_abbrevs  && const_strneq (name, "abbrev"))
5310 	      || (do_debug_lines    && strcmp (name, "line") == 0)
5311 	      || (do_debug_lines    && const_strneq (name, "line."))
5312 	      || (do_debug_pubnames && const_strneq (name, "pubnames"))
5313 	      || (do_debug_pubtypes && const_strneq (name, "pubtypes"))
5314 	      || (do_debug_pubnames && const_strneq (name, "gnu_pubnames"))
5315 	      || (do_debug_pubtypes && const_strneq (name, "gnu_pubtypes"))
5316 	      || (do_debug_aranges  && const_strneq (name, "aranges"))
5317 	      || (do_debug_ranges   && const_strneq (name, "ranges"))
5318 	      || (do_debug_frames   && const_strneq (name, "frame"))
5319 	      || (do_debug_macinfo  && const_strneq (name, "macinfo"))
5320 	      || (do_debug_macinfo  && const_strneq (name, "macro"))
5321 	      || (do_debug_str      && const_strneq (name, "str"))
5322 	      || (do_debug_str      && const_strneq (name, "line_str"))
5323 	      || (do_debug_loc      && const_strneq (name, "loc"))
5324 	      || (do_debug_addr     && const_strneq (name, "addr"))
5325 	      || (do_debug_cu_index && const_strneq (name, "cu_index"))
5326 	      || (do_debug_cu_index && const_strneq (name, "tu_index"))
5327 	      )
5328 	    request_dump_bynumber (i, DEBUG_DUMP);
5329 	}
5330       /* Linkonce section to be combined with .debug_info at link time.  */
5331       else if ((do_debugging || do_debug_info)
5332 	       && const_strneq (name, ".gnu.linkonce.wi."))
5333 	request_dump_bynumber (i, DEBUG_DUMP);
5334       else if (do_debug_frames && streq (name, ".eh_frame"))
5335 	request_dump_bynumber (i, DEBUG_DUMP);
5336       else if (do_gdb_index && streq (name, ".gdb_index"))
5337 	request_dump_bynumber (i, DEBUG_DUMP);
5338       /* Trace sections for Itanium VMS.  */
5339       else if ((do_debugging || do_trace_info || do_trace_abbrevs
5340                 || do_trace_aranges)
5341 	       && const_strneq (name, ".trace_"))
5342 	{
5343           name += sizeof (".trace_") - 1;
5344 
5345 	  if (do_debugging
5346 	      || (do_trace_info     && streq (name, "info"))
5347 	      || (do_trace_abbrevs  && streq (name, "abbrev"))
5348 	      || (do_trace_aranges  && streq (name, "aranges"))
5349 	      )
5350 	    request_dump_bynumber (i, DEBUG_DUMP);
5351 	}
5352     }
5353 
5354   if (! do_sections)
5355     return 1;
5356 
5357   if (elf_header.e_shnum > 1)
5358     printf (_("\nSection Headers:\n"));
5359   else
5360     printf (_("\nSection Header:\n"));
5361 
5362   if (is_32bit_elf)
5363     {
5364       if (do_section_details)
5365 	{
5366 	  printf (_("  [Nr] Name\n"));
5367 	  printf (_("       Type            Addr     Off    Size   ES   Lk Inf Al\n"));
5368 	}
5369       else
5370 	printf
5371 	  (_("  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al\n"));
5372     }
5373   else if (do_wide)
5374     {
5375       if (do_section_details)
5376 	{
5377 	  printf (_("  [Nr] Name\n"));
5378 	  printf (_("       Type            Address          Off    Size   ES   Lk Inf Al\n"));
5379 	}
5380       else
5381 	printf
5382 	  (_("  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al\n"));
5383     }
5384   else
5385     {
5386       if (do_section_details)
5387 	{
5388 	  printf (_("  [Nr] Name\n"));
5389 	  printf (_("       Type              Address          Offset            Link\n"));
5390 	  printf (_("       Size              EntSize          Info              Align\n"));
5391 	}
5392       else
5393 	{
5394 	  printf (_("  [Nr] Name              Type             Address           Offset\n"));
5395 	  printf (_("       Size              EntSize          Flags  Link  Info  Align\n"));
5396 	}
5397     }
5398 
5399   if (do_section_details)
5400     printf (_("       Flags\n"));
5401 
5402   for (i = 0, section = section_headers;
5403        i < elf_header.e_shnum;
5404        i++, section++)
5405     {
5406       printf ("  [%2u] ", i);
5407       if (do_section_details)
5408 	printf ("%s\n      ", printable_section_name (section));
5409       else
5410 	print_symbol (-17, SECTION_NAME (section));
5411 
5412       printf (do_wide ? " %-15s " : " %-15.15s ",
5413 	      get_section_type_name (section->sh_type));
5414 
5415       if (is_32bit_elf)
5416 	{
5417 	  const char * link_too_big = NULL;
5418 
5419 	  print_vma (section->sh_addr, LONG_HEX);
5420 
5421 	  printf ( " %6.6lx %6.6lx %2.2lx",
5422 		   (unsigned long) section->sh_offset,
5423 		   (unsigned long) section->sh_size,
5424 		   (unsigned long) section->sh_entsize);
5425 
5426 	  if (do_section_details)
5427 	    fputs ("  ", stdout);
5428 	  else
5429 	    printf (" %3s ", get_elf_section_flags (section->sh_flags));
5430 
5431 	  if (section->sh_link >= elf_header.e_shnum)
5432 	    {
5433 	      link_too_big = "";
5434 	      /* The sh_link value is out of range.  Normally this indicates
5435 		 an error but it can have special values in Solaris binaries.  */
5436 	      switch (elf_header.e_machine)
5437 		{
5438 		case EM_386:
5439 		case EM_486:
5440 		case EM_X86_64:
5441 		case EM_L1OM:
5442 		case EM_K1OM:
5443 		case EM_OLD_SPARCV9:
5444 		case EM_SPARC32PLUS:
5445 		case EM_SPARCV9:
5446 		case EM_SPARC:
5447 		  if (section->sh_link == (SHN_BEFORE & 0xffff))
5448 		    link_too_big = "BEFORE";
5449 		  else if (section->sh_link == (SHN_AFTER & 0xffff))
5450 		    link_too_big = "AFTER";
5451 		  break;
5452 		default:
5453 		  break;
5454 		}
5455 	    }
5456 
5457 	  if (do_section_details)
5458 	    {
5459 	      if (link_too_big != NULL && * link_too_big)
5460 		printf ("<%s> ", link_too_big);
5461 	      else
5462 		printf ("%2u ", section->sh_link);
5463 	      printf ("%3u %2lu\n", section->sh_info,
5464 		      (unsigned long) section->sh_addralign);
5465 	    }
5466 	  else
5467 	    printf ("%2u %3u %2lu\n",
5468 		    section->sh_link,
5469 		    section->sh_info,
5470 		    (unsigned long) section->sh_addralign);
5471 
5472 	  if (link_too_big && ! * link_too_big)
5473 	    warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
5474 		  i, section->sh_link);
5475 	}
5476       else if (do_wide)
5477 	{
5478 	  print_vma (section->sh_addr, LONG_HEX);
5479 
5480 	  if ((long) section->sh_offset == section->sh_offset)
5481 	    printf (" %6.6lx", (unsigned long) section->sh_offset);
5482 	  else
5483 	    {
5484 	      putchar (' ');
5485 	      print_vma (section->sh_offset, LONG_HEX);
5486 	    }
5487 
5488 	  if ((unsigned long) section->sh_size == section->sh_size)
5489 	    printf (" %6.6lx", (unsigned long) section->sh_size);
5490 	  else
5491 	    {
5492 	      putchar (' ');
5493 	      print_vma (section->sh_size, LONG_HEX);
5494 	    }
5495 
5496 	  if ((unsigned long) section->sh_entsize == section->sh_entsize)
5497 	    printf (" %2.2lx", (unsigned long) section->sh_entsize);
5498 	  else
5499 	    {
5500 	      putchar (' ');
5501 	      print_vma (section->sh_entsize, LONG_HEX);
5502 	    }
5503 
5504 	  if (do_section_details)
5505 	    fputs ("  ", stdout);
5506 	  else
5507 	    printf (" %3s ", get_elf_section_flags (section->sh_flags));
5508 
5509 	  printf ("%2u %3u ", section->sh_link, section->sh_info);
5510 
5511 	  if ((unsigned long) section->sh_addralign == section->sh_addralign)
5512 	    printf ("%2lu\n", (unsigned long) section->sh_addralign);
5513 	  else
5514 	    {
5515 	      print_vma (section->sh_addralign, DEC);
5516 	      putchar ('\n');
5517 	    }
5518 	}
5519       else if (do_section_details)
5520 	{
5521 	  printf ("       %-15.15s  ",
5522 		  get_section_type_name (section->sh_type));
5523 	  print_vma (section->sh_addr, LONG_HEX);
5524 	  if ((long) section->sh_offset == section->sh_offset)
5525 	    printf ("  %16.16lx", (unsigned long) section->sh_offset);
5526 	  else
5527 	    {
5528 	      printf ("  ");
5529 	      print_vma (section->sh_offset, LONG_HEX);
5530 	    }
5531 	  printf ("  %u\n       ", section->sh_link);
5532 	  print_vma (section->sh_size, LONG_HEX);
5533 	  putchar (' ');
5534 	  print_vma (section->sh_entsize, LONG_HEX);
5535 
5536 	  printf ("  %-16u  %lu\n",
5537 		  section->sh_info,
5538 		  (unsigned long) section->sh_addralign);
5539 	}
5540       else
5541 	{
5542 	  putchar (' ');
5543 	  print_vma (section->sh_addr, LONG_HEX);
5544 	  if ((long) section->sh_offset == section->sh_offset)
5545 	    printf ("  %8.8lx", (unsigned long) section->sh_offset);
5546 	  else
5547 	    {
5548 	      printf ("  ");
5549 	      print_vma (section->sh_offset, LONG_HEX);
5550 	    }
5551 	  printf ("\n       ");
5552 	  print_vma (section->sh_size, LONG_HEX);
5553 	  printf ("  ");
5554 	  print_vma (section->sh_entsize, LONG_HEX);
5555 
5556 	  printf (" %3s ", get_elf_section_flags (section->sh_flags));
5557 
5558 	  printf ("     %2u   %3u     %lu\n",
5559 		  section->sh_link,
5560 		  section->sh_info,
5561 		  (unsigned long) section->sh_addralign);
5562 	}
5563 
5564       if (do_section_details)
5565 	printf ("       %s\n", get_elf_section_flags (section->sh_flags));
5566     }
5567 
5568   if (!do_section_details)
5569     {
5570       if (elf_header.e_machine == EM_X86_64
5571 	  || elf_header.e_machine == EM_L1OM
5572 	  || elf_header.e_machine == EM_K1OM)
5573 	printf (_("Key to Flags:\n\
5574   W (write), A (alloc), X (execute), M (merge), S (strings), l (large)\n\
5575   I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\n\
5576   O (extra OS processing required) o (OS specific), p (processor specific)\n"));
5577       else
5578 	printf (_("Key to Flags:\n\
5579   W (write), A (alloc), X (execute), M (merge), S (strings)\n\
5580   I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\n\
5581   O (extra OS processing required) o (OS specific), p (processor specific)\n"));
5582     }
5583 
5584   return 1;
5585 }
5586 
5587 static const char *
get_group_flags(unsigned int flags)5588 get_group_flags (unsigned int flags)
5589 {
5590   static char buff[32];
5591   switch (flags)
5592     {
5593     case 0:
5594       return "";
5595 
5596     case GRP_COMDAT:
5597       return "COMDAT ";
5598 
5599    default:
5600       snprintf (buff, sizeof (buff), _("[<unknown>: 0x%x] "), flags);
5601       break;
5602     }
5603   return buff;
5604 }
5605 
5606 static int
process_section_groups(FILE * file)5607 process_section_groups (FILE * file)
5608 {
5609   Elf_Internal_Shdr * section;
5610   unsigned int i;
5611   struct group * group;
5612   Elf_Internal_Shdr * symtab_sec;
5613   Elf_Internal_Shdr * strtab_sec;
5614   Elf_Internal_Sym * symtab;
5615   unsigned long num_syms;
5616   char * strtab;
5617   size_t strtab_size;
5618 
5619   /* Don't process section groups unless needed.  */
5620   if (!do_unwind && !do_section_groups)
5621     return 1;
5622 
5623   if (elf_header.e_shnum == 0)
5624     {
5625       if (do_section_groups)
5626 	printf (_("\nThere are no sections to group in this file.\n"));
5627 
5628       return 1;
5629     }
5630 
5631   if (section_headers == NULL)
5632     {
5633       error (_("Section headers are not available!\n"));
5634       /* PR 13622: This can happen with a corrupt ELF header.  */
5635       return 0;
5636     }
5637 
5638   section_headers_groups = (struct group **) calloc (elf_header.e_shnum,
5639                                                      sizeof (struct group *));
5640 
5641   if (section_headers_groups == NULL)
5642     {
5643       error (_("Out of memory reading %u section group headers\n"),
5644 	     elf_header.e_shnum);
5645       return 0;
5646     }
5647 
5648   /* Scan the sections for the group section.  */
5649   group_count = 0;
5650   for (i = 0, section = section_headers;
5651        i < elf_header.e_shnum;
5652        i++, section++)
5653     if (section->sh_type == SHT_GROUP)
5654       group_count++;
5655 
5656   if (group_count == 0)
5657     {
5658       if (do_section_groups)
5659 	printf (_("\nThere are no section groups in this file.\n"));
5660 
5661       return 1;
5662     }
5663 
5664   section_groups = (struct group *) calloc (group_count, sizeof (struct group));
5665 
5666   if (section_groups == NULL)
5667     {
5668       error (_("Out of memory reading %lu groups\n"),
5669 	     (unsigned long) group_count);
5670       return 0;
5671     }
5672 
5673   symtab_sec = NULL;
5674   strtab_sec = NULL;
5675   symtab = NULL;
5676   num_syms = 0;
5677   strtab = NULL;
5678   strtab_size = 0;
5679   for (i = 0, section = section_headers, group = section_groups;
5680        i < elf_header.e_shnum;
5681        i++, section++)
5682     {
5683       if (section->sh_type == SHT_GROUP)
5684 	{
5685 	  const char * name = printable_section_name (section);
5686 	  const char * group_name;
5687 	  unsigned char * start;
5688 	  unsigned char * indices;
5689 	  unsigned int entry, j, size;
5690 	  Elf_Internal_Shdr * sec;
5691 	  Elf_Internal_Sym * sym;
5692 
5693 	  /* Get the symbol table.  */
5694 	  if (section->sh_link >= elf_header.e_shnum
5695 	      || ((sec = section_headers + section->sh_link)->sh_type
5696 		  != SHT_SYMTAB))
5697 	    {
5698 	      error (_("Bad sh_link in group section `%s'\n"), name);
5699 	      continue;
5700 	    }
5701 
5702 	  if (symtab_sec != sec)
5703 	    {
5704 	      symtab_sec = sec;
5705 	      if (symtab)
5706 		free (symtab);
5707 	      symtab = GET_ELF_SYMBOLS (file, symtab_sec, & num_syms);
5708 	    }
5709 
5710 	  if (symtab == NULL)
5711 	    {
5712 	      error (_("Corrupt header in group section `%s'\n"), name);
5713 	      continue;
5714 	    }
5715 
5716 	  if (section->sh_info >= num_syms)
5717 	    {
5718 	      error (_("Bad sh_info in group section `%s'\n"), name);
5719 	      continue;
5720 	    }
5721 
5722 	  sym = symtab + section->sh_info;
5723 
5724 	  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5725 	    {
5726 	      if (sym->st_shndx == 0
5727 		  || sym->st_shndx >= elf_header.e_shnum)
5728 		{
5729 		  error (_("Bad sh_info in group section `%s'\n"), name);
5730 		  continue;
5731 		}
5732 
5733 	      group_name = SECTION_NAME (section_headers + sym->st_shndx);
5734 	      strtab_sec = NULL;
5735 	      if (strtab)
5736 		free (strtab);
5737 	      strtab = NULL;
5738 	      strtab_size = 0;
5739 	    }
5740 	  else
5741 	    {
5742 	      /* Get the string table.  */
5743 	      if (symtab_sec->sh_link >= elf_header.e_shnum)
5744 		{
5745 		  strtab_sec = NULL;
5746 		  if (strtab)
5747 		    free (strtab);
5748 		  strtab = NULL;
5749 		  strtab_size = 0;
5750 		}
5751 	      else if (strtab_sec
5752 		       != (sec = section_headers + symtab_sec->sh_link))
5753 		{
5754 		  strtab_sec = sec;
5755 		  if (strtab)
5756 		    free (strtab);
5757 
5758 		  strtab = (char *) get_data (NULL, file, strtab_sec->sh_offset,
5759 					      1, strtab_sec->sh_size,
5760 					      _("string table"));
5761 		  strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
5762 		}
5763 	      group_name = sym->st_name < strtab_size
5764 		? strtab + sym->st_name : _("<corrupt>");
5765 	    }
5766 
5767 	  start = (unsigned char *) get_data (NULL, file, section->sh_offset,
5768                                               1, section->sh_size,
5769                                               _("section data"));
5770 	  if (start == NULL)
5771 	    continue;
5772 
5773 	  indices = start;
5774 	  size = (section->sh_size / section->sh_entsize) - 1;
5775 	  entry = byte_get (indices, 4);
5776 	  indices += 4;
5777 
5778 	  if (do_section_groups)
5779 	    {
5780 	      printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
5781 		      get_group_flags (entry), i, name, group_name, size);
5782 
5783 	      printf (_("   [Index]    Name\n"));
5784 	    }
5785 
5786 	  group->group_index = i;
5787 
5788 	  for (j = 0; j < size; j++)
5789 	    {
5790 	      struct group_list * g;
5791 
5792 	      entry = byte_get (indices, 4);
5793 	      indices += 4;
5794 
5795 	      if (entry >= elf_header.e_shnum)
5796 		{
5797 		  error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
5798 			 entry, i, elf_header.e_shnum - 1);
5799 		  continue;
5800 		}
5801 
5802 	      if (section_headers_groups [entry] != NULL)
5803 		{
5804 		  if (entry)
5805 		    {
5806 		      error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
5807 			     entry, i,
5808 			     section_headers_groups [entry]->group_index);
5809 		      continue;
5810 		    }
5811 		  else
5812 		    {
5813 		      /* Intel C/C++ compiler may put section 0 in a
5814 			 section group. We just warn it the first time
5815 			 and ignore it afterwards.  */
5816 		      static int warned = 0;
5817 		      if (!warned)
5818 			{
5819 			  error (_("section 0 in group section [%5u]\n"),
5820 				 section_headers_groups [entry]->group_index);
5821 			  warned++;
5822 			}
5823 		    }
5824 		}
5825 
5826 	      section_headers_groups [entry] = group;
5827 
5828 	      if (do_section_groups)
5829 		{
5830 		  sec = section_headers + entry;
5831 		  printf ("   [%5u]   %s\n", entry, printable_section_name (sec));
5832 		}
5833 
5834 	      g = (struct group_list *) xmalloc (sizeof (struct group_list));
5835 	      g->section_index = entry;
5836 	      g->next = group->root;
5837 	      group->root = g;
5838 	    }
5839 
5840 	  if (start)
5841 	    free (start);
5842 
5843 	  group++;
5844 	}
5845     }
5846 
5847   if (symtab)
5848     free (symtab);
5849   if (strtab)
5850     free (strtab);
5851   return 1;
5852 }
5853 
5854 /* Data used to display dynamic fixups.  */
5855 
5856 struct ia64_vms_dynfixup
5857 {
5858   bfd_vma needed_ident;		/* Library ident number.  */
5859   bfd_vma needed;		/* Index in the dstrtab of the library name.  */
5860   bfd_vma fixup_needed;		/* Index of the library.  */
5861   bfd_vma fixup_rela_cnt;	/* Number of fixups.  */
5862   bfd_vma fixup_rela_off;	/* Fixups offset in the dynamic segment.  */
5863 };
5864 
5865 /* Data used to display dynamic relocations.  */
5866 
5867 struct ia64_vms_dynimgrela
5868 {
5869   bfd_vma img_rela_cnt;		/* Number of relocations.  */
5870   bfd_vma img_rela_off;		/* Reloc offset in the dynamic segment.  */
5871 };
5872 
5873 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
5874    library).  */
5875 
5876 static void
dump_ia64_vms_dynamic_fixups(FILE * file,struct ia64_vms_dynfixup * fixup,const char * strtab,unsigned int strtab_sz)5877 dump_ia64_vms_dynamic_fixups (FILE *file, struct ia64_vms_dynfixup *fixup,
5878                               const char *strtab, unsigned int strtab_sz)
5879 {
5880   Elf64_External_VMS_IMAGE_FIXUP *imfs;
5881   long i;
5882   const char *lib_name;
5883 
5884   imfs = get_data (NULL, file, dynamic_addr + fixup->fixup_rela_off,
5885 		   1, fixup->fixup_rela_cnt * sizeof (*imfs),
5886 		   _("dynamic section image fixups"));
5887   if (!imfs)
5888     return;
5889 
5890   if (fixup->needed < strtab_sz)
5891     lib_name = strtab + fixup->needed;
5892   else
5893     {
5894       warn ("corrupt library name index of 0x%lx found in dynamic entry",
5895             (unsigned long) fixup->needed);
5896       lib_name = "???";
5897     }
5898   printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
5899 	  (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
5900   printf
5901     (_("Seg Offset           Type                             SymVec DataType\n"));
5902 
5903   for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
5904     {
5905       unsigned int type;
5906       const char *rtype;
5907 
5908       printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
5909       printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
5910       type = BYTE_GET (imfs [i].type);
5911       rtype = elf_ia64_reloc_type (type);
5912       if (rtype == NULL)
5913         printf (" 0x%08x                       ", type);
5914       else
5915         printf (" %-32s ", rtype);
5916       printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
5917       printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
5918     }
5919 
5920   free (imfs);
5921 }
5922 
5923 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image).  */
5924 
5925 static void
dump_ia64_vms_dynamic_relocs(FILE * file,struct ia64_vms_dynimgrela * imgrela)5926 dump_ia64_vms_dynamic_relocs (FILE *file, struct ia64_vms_dynimgrela *imgrela)
5927 {
5928   Elf64_External_VMS_IMAGE_RELA *imrs;
5929   long i;
5930 
5931   imrs = get_data (NULL, file, dynamic_addr + imgrela->img_rela_off,
5932 		   1, imgrela->img_rela_cnt * sizeof (*imrs),
5933 		   _("dynamic section image relocations"));
5934   if (!imrs)
5935     return;
5936 
5937   printf (_("\nImage relocs\n"));
5938   printf
5939     (_("Seg Offset   Type                            Addend            Seg Sym Off\n"));
5940 
5941   for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
5942     {
5943       unsigned int type;
5944       const char *rtype;
5945 
5946       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
5947       printf ("%08" BFD_VMA_FMT "x ",
5948               (bfd_vma) BYTE_GET (imrs [i].rela_offset));
5949       type = BYTE_GET (imrs [i].type);
5950       rtype = elf_ia64_reloc_type (type);
5951       if (rtype == NULL)
5952         printf ("0x%08x                      ", type);
5953       else
5954         printf ("%-31s ", rtype);
5955       print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
5956       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
5957       printf ("%08" BFD_VMA_FMT "x\n",
5958               (bfd_vma) BYTE_GET (imrs [i].sym_offset));
5959     }
5960 
5961   free (imrs);
5962 }
5963 
5964 /* Display IA-64 OpenVMS dynamic relocations and fixups.  */
5965 
5966 static int
process_ia64_vms_dynamic_relocs(FILE * file)5967 process_ia64_vms_dynamic_relocs (FILE *file)
5968 {
5969   struct ia64_vms_dynfixup fixup;
5970   struct ia64_vms_dynimgrela imgrela;
5971   Elf_Internal_Dyn *entry;
5972   int res = 0;
5973   bfd_vma strtab_off = 0;
5974   bfd_vma strtab_sz = 0;
5975   char *strtab = NULL;
5976 
5977   memset (&fixup, 0, sizeof (fixup));
5978   memset (&imgrela, 0, sizeof (imgrela));
5979 
5980   /* Note: the order of the entries is specified by the OpenVMS specs.  */
5981   for (entry = dynamic_section;
5982        entry < dynamic_section + dynamic_nent;
5983        entry++)
5984     {
5985       switch (entry->d_tag)
5986         {
5987         case DT_IA_64_VMS_STRTAB_OFFSET:
5988           strtab_off = entry->d_un.d_val;
5989           break;
5990         case DT_STRSZ:
5991           strtab_sz = entry->d_un.d_val;
5992           if (strtab == NULL)
5993             strtab = get_data (NULL, file, dynamic_addr + strtab_off,
5994                                1, strtab_sz, _("dynamic string section"));
5995           break;
5996 
5997         case DT_IA_64_VMS_NEEDED_IDENT:
5998           fixup.needed_ident = entry->d_un.d_val;
5999           break;
6000         case DT_NEEDED:
6001           fixup.needed = entry->d_un.d_val;
6002           break;
6003         case DT_IA_64_VMS_FIXUP_NEEDED:
6004           fixup.fixup_needed = entry->d_un.d_val;
6005           break;
6006         case DT_IA_64_VMS_FIXUP_RELA_CNT:
6007           fixup.fixup_rela_cnt = entry->d_un.d_val;
6008           break;
6009         case DT_IA_64_VMS_FIXUP_RELA_OFF:
6010           fixup.fixup_rela_off = entry->d_un.d_val;
6011           res++;
6012           dump_ia64_vms_dynamic_fixups (file, &fixup, strtab, strtab_sz);
6013           break;
6014 
6015         case DT_IA_64_VMS_IMG_RELA_CNT:
6016 	  imgrela.img_rela_cnt = entry->d_un.d_val;
6017           break;
6018         case DT_IA_64_VMS_IMG_RELA_OFF:
6019 	  imgrela.img_rela_off = entry->d_un.d_val;
6020           res++;
6021           dump_ia64_vms_dynamic_relocs (file, &imgrela);
6022           break;
6023 
6024         default:
6025           break;
6026 	}
6027     }
6028 
6029   if (strtab != NULL)
6030     free (strtab);
6031 
6032   return res;
6033 }
6034 
6035 static struct
6036 {
6037   const char * name;
6038   int reloc;
6039   int size;
6040   int rela;
6041 } dynamic_relocations [] =
6042 {
6043     { "REL", DT_REL, DT_RELSZ, FALSE },
6044     { "RELA", DT_RELA, DT_RELASZ, TRUE },
6045     { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
6046 };
6047 
6048 /* Process the reloc section.  */
6049 
6050 static int
process_relocs(FILE * file)6051 process_relocs (FILE * file)
6052 {
6053   unsigned long rel_size;
6054   unsigned long rel_offset;
6055 
6056 
6057   if (!do_reloc)
6058     return 1;
6059 
6060   if (do_using_dynamic)
6061     {
6062       int is_rela;
6063       const char * name;
6064       int has_dynamic_reloc;
6065       unsigned int i;
6066 
6067       has_dynamic_reloc = 0;
6068 
6069       for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
6070 	{
6071 	  is_rela = dynamic_relocations [i].rela;
6072 	  name = dynamic_relocations [i].name;
6073 	  rel_size = dynamic_info [dynamic_relocations [i].size];
6074 	  rel_offset = dynamic_info [dynamic_relocations [i].reloc];
6075 
6076 	  has_dynamic_reloc |= rel_size;
6077 
6078 	  if (is_rela == UNKNOWN)
6079 	    {
6080 	      if (dynamic_relocations [i].reloc == DT_JMPREL)
6081 		switch (dynamic_info[DT_PLTREL])
6082 		  {
6083 		  case DT_REL:
6084 		    is_rela = FALSE;
6085 		    break;
6086 		  case DT_RELA:
6087 		    is_rela = TRUE;
6088 		    break;
6089 		  }
6090 	    }
6091 
6092 	  if (rel_size)
6093 	    {
6094 	      printf
6095 		(_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
6096 		 name, rel_offset, rel_size);
6097 
6098 	      dump_relocations (file,
6099 				offset_from_vma (file, rel_offset, rel_size),
6100 				rel_size,
6101 				dynamic_symbols, num_dynamic_syms,
6102 				dynamic_strings, dynamic_strings_length, is_rela);
6103 	    }
6104 	}
6105 
6106       if (is_ia64_vms ())
6107         has_dynamic_reloc |= process_ia64_vms_dynamic_relocs (file);
6108 
6109       if (! has_dynamic_reloc)
6110 	printf (_("\nThere are no dynamic relocations in this file.\n"));
6111     }
6112   else
6113     {
6114       Elf_Internal_Shdr * section;
6115       unsigned long i;
6116       int found = 0;
6117 
6118       for (i = 0, section = section_headers;
6119 	   i < elf_header.e_shnum;
6120 	   i++, section++)
6121 	{
6122 	  if (   section->sh_type != SHT_RELA
6123 	      && section->sh_type != SHT_REL)
6124 	    continue;
6125 
6126 	  rel_offset = section->sh_offset;
6127 	  rel_size   = section->sh_size;
6128 
6129 	  if (rel_size)
6130 	    {
6131 	      Elf_Internal_Shdr * strsec;
6132 	      int is_rela;
6133 
6134 	      printf (_("\nRelocation section "));
6135 
6136 	      if (string_table == NULL)
6137 		printf ("%d", section->sh_name);
6138 	      else
6139 		printf ("'%s'", printable_section_name (section));
6140 
6141 	      printf (_(" at offset 0x%lx contains %lu entries:\n"),
6142 		 rel_offset, (unsigned long) (rel_size / section->sh_entsize));
6143 
6144 	      is_rela = section->sh_type == SHT_RELA;
6145 
6146 	      if (section->sh_link != 0
6147 		  && section->sh_link < elf_header.e_shnum)
6148 		{
6149 		  Elf_Internal_Shdr * symsec;
6150 		  Elf_Internal_Sym *  symtab;
6151 		  unsigned long nsyms;
6152 		  unsigned long strtablen = 0;
6153 		  char * strtab = NULL;
6154 
6155 		  symsec = section_headers + section->sh_link;
6156 		  if (symsec->sh_type != SHT_SYMTAB
6157 		      && symsec->sh_type != SHT_DYNSYM)
6158                     continue;
6159 
6160 		  symtab = GET_ELF_SYMBOLS (file, symsec, & nsyms);
6161 
6162 		  if (symtab == NULL)
6163 		    continue;
6164 
6165 		  if (symsec->sh_link != 0
6166 		      && symsec->sh_link < elf_header.e_shnum)
6167 		    {
6168 		      strsec = section_headers + symsec->sh_link;
6169 
6170 		      strtab = (char *) get_data (NULL, file, strsec->sh_offset,
6171 						  1, strsec->sh_size,
6172 						  _("string table"));
6173 		      strtablen = strtab == NULL ? 0 : strsec->sh_size;
6174 		    }
6175 
6176 		  dump_relocations (file, rel_offset, rel_size,
6177 				    symtab, nsyms, strtab, strtablen, is_rela);
6178 		  if (strtab)
6179 		    free (strtab);
6180 		  free (symtab);
6181 		}
6182 	      else
6183 		dump_relocations (file, rel_offset, rel_size,
6184 				  NULL, 0, NULL, 0, is_rela);
6185 
6186 	      found = 1;
6187 	    }
6188 	}
6189 
6190       if (! found)
6191 	printf (_("\nThere are no relocations in this file.\n"));
6192     }
6193 
6194   return 1;
6195 }
6196 
6197 /* Process the unwind section.  */
6198 
6199 #include "unwind-ia64.h"
6200 
6201 /* An absolute address consists of a section and an offset.  If the
6202    section is NULL, the offset itself is the address, otherwise, the
6203    address equals to LOAD_ADDRESS(section) + offset.  */
6204 
6205 struct absaddr
6206   {
6207     unsigned short section;
6208     bfd_vma offset;
6209   };
6210 
6211 #define ABSADDR(a) \
6212   ((a).section \
6213    ? section_headers [(a).section].sh_addr + (a).offset \
6214    : (a).offset)
6215 
6216 struct ia64_unw_table_entry
6217   {
6218     struct absaddr start;
6219     struct absaddr end;
6220     struct absaddr info;
6221   };
6222 
6223 struct ia64_unw_aux_info
6224   {
6225 
6226     struct ia64_unw_table_entry *table;	/* Unwind table.  */
6227     unsigned long table_len;	/* Length of unwind table.  */
6228     unsigned char * info;	/* Unwind info.  */
6229     unsigned long info_size;	/* Size of unwind info.  */
6230     bfd_vma info_addr;		/* starting address of unwind info.  */
6231     bfd_vma seg_base;		/* Starting address of segment.  */
6232     Elf_Internal_Sym * symtab;	/* The symbol table.  */
6233     unsigned long nsyms;	/* Number of symbols.  */
6234     char * strtab;		/* The string table.  */
6235     unsigned long strtab_size;	/* Size of string table.  */
6236   };
6237 
6238 static void
find_symbol_for_address(Elf_Internal_Sym * symtab,unsigned long nsyms,const char * strtab,unsigned long strtab_size,struct absaddr addr,const char ** symname,bfd_vma * offset)6239 find_symbol_for_address (Elf_Internal_Sym * symtab,
6240 			 unsigned long nsyms,
6241 			 const char * strtab,
6242 			 unsigned long strtab_size,
6243 			 struct absaddr addr,
6244 			 const char ** symname,
6245 			 bfd_vma * offset)
6246 {
6247   bfd_vma dist = 0x100000;
6248   Elf_Internal_Sym * sym;
6249   Elf_Internal_Sym * best = NULL;
6250   unsigned long i;
6251 
6252   REMOVE_ARCH_BITS (addr.offset);
6253 
6254   for (i = 0, sym = symtab; i < nsyms; ++i, ++sym)
6255     {
6256       bfd_vma value = sym->st_value;
6257 
6258       REMOVE_ARCH_BITS (value);
6259 
6260       if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
6261 	  && sym->st_name != 0
6262 	  && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
6263 	  && addr.offset >= value
6264 	  && addr.offset - value < dist)
6265 	{
6266 	  best = sym;
6267 	  dist = addr.offset - value;
6268 	  if (!dist)
6269 	    break;
6270 	}
6271     }
6272 
6273   if (best)
6274     {
6275       *symname = (best->st_name >= strtab_size
6276 		  ? _("<corrupt>") : strtab + best->st_name);
6277       *offset = dist;
6278       return;
6279     }
6280 
6281   *symname = NULL;
6282   *offset = addr.offset;
6283 }
6284 
6285 static void
dump_ia64_unwind(struct ia64_unw_aux_info * aux)6286 dump_ia64_unwind (struct ia64_unw_aux_info * aux)
6287 {
6288   struct ia64_unw_table_entry * tp;
6289   int in_body;
6290 
6291   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
6292     {
6293       bfd_vma stamp;
6294       bfd_vma offset;
6295       const unsigned char * dp;
6296       const unsigned char * head;
6297       const char * procname;
6298 
6299       find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
6300 			       aux->strtab_size, tp->start, &procname, &offset);
6301 
6302       fputs ("\n<", stdout);
6303 
6304       if (procname)
6305 	{
6306 	  fputs (procname, stdout);
6307 
6308 	  if (offset)
6309 	    printf ("+%lx", (unsigned long) offset);
6310 	}
6311 
6312       fputs (">: [", stdout);
6313       print_vma (tp->start.offset, PREFIX_HEX);
6314       fputc ('-', stdout);
6315       print_vma (tp->end.offset, PREFIX_HEX);
6316       printf ("], info at +0x%lx\n",
6317 	      (unsigned long) (tp->info.offset - aux->seg_base));
6318 
6319       head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
6320       stamp = byte_get ((unsigned char *) head, sizeof (stamp));
6321 
6322       printf ("  v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
6323 	      (unsigned) UNW_VER (stamp),
6324 	      (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
6325 	      UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
6326 	      UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
6327 	      (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
6328 
6329       if (UNW_VER (stamp) != 1)
6330 	{
6331 	  printf (_("\tUnknown version.\n"));
6332 	  continue;
6333 	}
6334 
6335       in_body = 0;
6336       for (dp = head + 8; dp < head + 8 + eh_addr_size * UNW_LENGTH (stamp);)
6337 	dp = unw_decode (dp, in_body, & in_body);
6338     }
6339 }
6340 
6341 static int
slurp_ia64_unwind_table(FILE * file,struct ia64_unw_aux_info * aux,Elf_Internal_Shdr * sec)6342 slurp_ia64_unwind_table (FILE * file,
6343 			 struct ia64_unw_aux_info * aux,
6344 			 Elf_Internal_Shdr * sec)
6345 {
6346   unsigned long size, nrelas, i;
6347   Elf_Internal_Phdr * seg;
6348   struct ia64_unw_table_entry * tep;
6349   Elf_Internal_Shdr * relsec;
6350   Elf_Internal_Rela * rela;
6351   Elf_Internal_Rela * rp;
6352   unsigned char * table;
6353   unsigned char * tp;
6354   Elf_Internal_Sym * sym;
6355   const char * relname;
6356 
6357   /* First, find the starting address of the segment that includes
6358      this section: */
6359 
6360   if (elf_header.e_phnum)
6361     {
6362       if (! get_program_headers (file))
6363 	  return 0;
6364 
6365       for (seg = program_headers;
6366 	   seg < program_headers + elf_header.e_phnum;
6367 	   ++seg)
6368 	{
6369 	  if (seg->p_type != PT_LOAD)
6370 	    continue;
6371 
6372 	  if (sec->sh_addr >= seg->p_vaddr
6373 	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
6374 	    {
6375 	      aux->seg_base = seg->p_vaddr;
6376 	      break;
6377 	    }
6378 	}
6379     }
6380 
6381   /* Second, build the unwind table from the contents of the unwind section:  */
6382   size = sec->sh_size;
6383   table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
6384                                       _("unwind table"));
6385   if (!table)
6386     return 0;
6387 
6388   aux->table = (struct ia64_unw_table_entry *)
6389       xcmalloc (size / (3 * eh_addr_size), sizeof (aux->table[0]));
6390   tep = aux->table;
6391   for (tp = table; tp < table + size; ++tep)
6392     {
6393       tep->start.section = SHN_UNDEF;
6394       tep->end.section   = SHN_UNDEF;
6395       tep->info.section  = SHN_UNDEF;
6396       tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
6397       tep->end.offset   = byte_get (tp, eh_addr_size); tp += eh_addr_size;
6398       tep->info.offset  = byte_get (tp, eh_addr_size); tp += eh_addr_size;
6399       tep->start.offset += aux->seg_base;
6400       tep->end.offset   += aux->seg_base;
6401       tep->info.offset  += aux->seg_base;
6402     }
6403   free (table);
6404 
6405   /* Third, apply any relocations to the unwind table:  */
6406   for (relsec = section_headers;
6407        relsec < section_headers + elf_header.e_shnum;
6408        ++relsec)
6409     {
6410       if (relsec->sh_type != SHT_RELA
6411 	  || relsec->sh_info >= elf_header.e_shnum
6412 	  || section_headers + relsec->sh_info != sec)
6413 	continue;
6414 
6415       if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
6416 			      & rela, & nrelas))
6417 	return 0;
6418 
6419       for (rp = rela; rp < rela + nrelas; ++rp)
6420 	{
6421 	  relname = elf_ia64_reloc_type (get_reloc_type (rp->r_info));
6422 	  sym = aux->symtab + get_reloc_symindex (rp->r_info);
6423 
6424 	  if (! const_strneq (relname, "R_IA64_SEGREL"))
6425 	    {
6426 	      warn (_("Skipping unexpected relocation type %s\n"), relname);
6427 	      continue;
6428 	    }
6429 
6430 	  i = rp->r_offset / (3 * eh_addr_size);
6431 
6432 	  switch (rp->r_offset/eh_addr_size % 3)
6433 	    {
6434 	    case 0:
6435 	      aux->table[i].start.section = sym->st_shndx;
6436 	      aux->table[i].start.offset  = rp->r_addend + sym->st_value;
6437 	      break;
6438 	    case 1:
6439 	      aux->table[i].end.section   = sym->st_shndx;
6440 	      aux->table[i].end.offset    = rp->r_addend + sym->st_value;
6441 	      break;
6442 	    case 2:
6443 	      aux->table[i].info.section  = sym->st_shndx;
6444 	      aux->table[i].info.offset   = rp->r_addend + sym->st_value;
6445 	      break;
6446 	    default:
6447 	      break;
6448 	    }
6449 	}
6450 
6451       free (rela);
6452     }
6453 
6454   aux->table_len = size / (3 * eh_addr_size);
6455   return 1;
6456 }
6457 
6458 static void
ia64_process_unwind(FILE * file)6459 ia64_process_unwind (FILE * file)
6460 {
6461   Elf_Internal_Shdr * sec;
6462   Elf_Internal_Shdr * unwsec = NULL;
6463   Elf_Internal_Shdr * strsec;
6464   unsigned long i, unwcount = 0, unwstart = 0;
6465   struct ia64_unw_aux_info aux;
6466 
6467   memset (& aux, 0, sizeof (aux));
6468 
6469   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
6470     {
6471       if (sec->sh_type == SHT_SYMTAB
6472 	  && sec->sh_link < elf_header.e_shnum)
6473 	{
6474 	  aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
6475 
6476 	  strsec = section_headers + sec->sh_link;
6477 	  if (aux.strtab != NULL)
6478 	    {
6479 	      error (_("Multiple auxillary string tables encountered\n"));
6480 	      free (aux.strtab);
6481 	    }
6482 	  aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
6483                                           1, strsec->sh_size,
6484                                           _("string table"));
6485 	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
6486 	}
6487       else if (sec->sh_type == SHT_IA_64_UNWIND)
6488 	unwcount++;
6489     }
6490 
6491   if (!unwcount)
6492     printf (_("\nThere are no unwind sections in this file.\n"));
6493 
6494   while (unwcount-- > 0)
6495     {
6496       char * suffix;
6497       size_t len, len2;
6498 
6499       for (i = unwstart, sec = section_headers + unwstart, unwsec = NULL;
6500 	   i < elf_header.e_shnum; ++i, ++sec)
6501 	if (sec->sh_type == SHT_IA_64_UNWIND)
6502 	  {
6503 	    unwsec = sec;
6504 	    break;
6505 	  }
6506       /* We have already counted the number of SHT_IA64_UNWIND
6507 	 sections so the loop above should never fail.  */
6508       assert (unwsec != NULL);
6509 
6510       unwstart = i + 1;
6511       len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
6512 
6513       if ((unwsec->sh_flags & SHF_GROUP) != 0)
6514 	{
6515 	  /* We need to find which section group it is in.  */
6516 	  struct group_list * g;
6517 
6518 	  if (section_headers_groups == NULL
6519 	      || section_headers_groups [i] == NULL)
6520 	    i = elf_header.e_shnum;
6521 	  else
6522 	    {
6523 	      g = section_headers_groups [i]->root;
6524 
6525 	      for (; g != NULL; g = g->next)
6526 		{
6527 		  sec = section_headers + g->section_index;
6528 
6529 		  if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
6530 		    break;
6531 		}
6532 
6533 	      if (g == NULL)
6534 		i = elf_header.e_shnum;
6535 	    }
6536 	}
6537       else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
6538 	{
6539 	  /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO.  */
6540 	  len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
6541 	  suffix = SECTION_NAME (unwsec) + len;
6542 	  for (i = 0, sec = section_headers; i < elf_header.e_shnum;
6543 	       ++i, ++sec)
6544 	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
6545 		&& streq (SECTION_NAME (sec) + len2, suffix))
6546 	      break;
6547 	}
6548       else
6549 	{
6550 	  /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
6551 	     .IA_64.unwind or BAR -> .IA_64.unwind_info.  */
6552 	  len = sizeof (ELF_STRING_ia64_unwind) - 1;
6553 	  len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
6554 	  suffix = "";
6555 	  if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
6556 	    suffix = SECTION_NAME (unwsec) + len;
6557 	  for (i = 0, sec = section_headers; i < elf_header.e_shnum;
6558 	       ++i, ++sec)
6559 	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
6560 		&& streq (SECTION_NAME (sec) + len2, suffix))
6561 	      break;
6562 	}
6563 
6564       if (i == elf_header.e_shnum)
6565 	{
6566 	  printf (_("\nCould not find unwind info section for "));
6567 
6568 	  if (string_table == NULL)
6569 	    printf ("%d", unwsec->sh_name);
6570 	  else
6571 	    printf ("'%s'", printable_section_name (unwsec));
6572 	}
6573       else
6574 	{
6575 	  aux.info_addr = sec->sh_addr;
6576 	  aux.info = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1,
6577 						 sec->sh_size,
6578 						 _("unwind info"));
6579 	  aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
6580 
6581 	  printf (_("\nUnwind section "));
6582 
6583 	  if (string_table == NULL)
6584 	    printf ("%d", unwsec->sh_name);
6585 	  else
6586 	    printf ("'%s'", printable_section_name (unwsec));
6587 
6588 	  printf (_(" at offset 0x%lx contains %lu entries:\n"),
6589 		  (unsigned long) unwsec->sh_offset,
6590 		  (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
6591 
6592 	  (void) slurp_ia64_unwind_table (file, & aux, unwsec);
6593 
6594 	  if (aux.table_len > 0)
6595 	    dump_ia64_unwind (& aux);
6596 
6597 	  if (aux.table)
6598 	    free ((char *) aux.table);
6599 	  if (aux.info)
6600 	    free ((char *) aux.info);
6601 	  aux.table = NULL;
6602 	  aux.info = NULL;
6603 	}
6604     }
6605 
6606   if (aux.symtab)
6607     free (aux.symtab);
6608   if (aux.strtab)
6609     free ((char *) aux.strtab);
6610 }
6611 
6612 struct hppa_unw_table_entry
6613   {
6614     struct absaddr start;
6615     struct absaddr end;
6616     unsigned int Cannot_unwind:1;			/* 0 */
6617     unsigned int Millicode:1;			/* 1 */
6618     unsigned int Millicode_save_sr0:1;		/* 2 */
6619     unsigned int Region_description:2;		/* 3..4 */
6620     unsigned int reserved1:1;			/* 5 */
6621     unsigned int Entry_SR:1;			/* 6 */
6622     unsigned int Entry_FR:4;     /* number saved */	/* 7..10 */
6623     unsigned int Entry_GR:5;     /* number saved */	/* 11..15 */
6624     unsigned int Args_stored:1;			/* 16 */
6625     unsigned int Variable_Frame:1;			/* 17 */
6626     unsigned int Separate_Package_Body:1;		/* 18 */
6627     unsigned int Frame_Extension_Millicode:1;	/* 19 */
6628     unsigned int Stack_Overflow_Check:1;		/* 20 */
6629     unsigned int Two_Instruction_SP_Increment:1;	/* 21 */
6630     unsigned int Ada_Region:1;			/* 22 */
6631     unsigned int cxx_info:1;			/* 23 */
6632     unsigned int cxx_try_catch:1;			/* 24 */
6633     unsigned int sched_entry_seq:1;			/* 25 */
6634     unsigned int reserved2:1;			/* 26 */
6635     unsigned int Save_SP:1;				/* 27 */
6636     unsigned int Save_RP:1;				/* 28 */
6637     unsigned int Save_MRP_in_frame:1;		/* 29 */
6638     unsigned int extn_ptr_defined:1;		/* 30 */
6639     unsigned int Cleanup_defined:1;			/* 31 */
6640 
6641     unsigned int MPE_XL_interrupt_marker:1;		/* 0 */
6642     unsigned int HP_UX_interrupt_marker:1;		/* 1 */
6643     unsigned int Large_frame:1;			/* 2 */
6644     unsigned int Pseudo_SP_Set:1;			/* 3 */
6645     unsigned int reserved4:1;			/* 4 */
6646     unsigned int Total_frame_size:27;		/* 5..31 */
6647   };
6648 
6649 struct hppa_unw_aux_info
6650   {
6651     struct hppa_unw_table_entry *table;	/* Unwind table.  */
6652     unsigned long table_len;	/* Length of unwind table.  */
6653     bfd_vma seg_base;		/* Starting address of segment.  */
6654     Elf_Internal_Sym * symtab;	/* The symbol table.  */
6655     unsigned long nsyms;	/* Number of symbols.  */
6656     char * strtab;		/* The string table.  */
6657     unsigned long strtab_size;	/* Size of string table.  */
6658   };
6659 
6660 static void
dump_hppa_unwind(struct hppa_unw_aux_info * aux)6661 dump_hppa_unwind (struct hppa_unw_aux_info * aux)
6662 {
6663   struct hppa_unw_table_entry * tp;
6664 
6665   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
6666     {
6667       bfd_vma offset;
6668       const char * procname;
6669 
6670       find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
6671 			       aux->strtab_size, tp->start, &procname,
6672 			       &offset);
6673 
6674       fputs ("\n<", stdout);
6675 
6676       if (procname)
6677 	{
6678 	  fputs (procname, stdout);
6679 
6680 	  if (offset)
6681 	    printf ("+%lx", (unsigned long) offset);
6682 	}
6683 
6684       fputs (">: [", stdout);
6685       print_vma (tp->start.offset, PREFIX_HEX);
6686       fputc ('-', stdout);
6687       print_vma (tp->end.offset, PREFIX_HEX);
6688       printf ("]\n\t");
6689 
6690 #define PF(_m) if (tp->_m) printf (#_m " ");
6691 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
6692       PF(Cannot_unwind);
6693       PF(Millicode);
6694       PF(Millicode_save_sr0);
6695       /* PV(Region_description);  */
6696       PF(Entry_SR);
6697       PV(Entry_FR);
6698       PV(Entry_GR);
6699       PF(Args_stored);
6700       PF(Variable_Frame);
6701       PF(Separate_Package_Body);
6702       PF(Frame_Extension_Millicode);
6703       PF(Stack_Overflow_Check);
6704       PF(Two_Instruction_SP_Increment);
6705       PF(Ada_Region);
6706       PF(cxx_info);
6707       PF(cxx_try_catch);
6708       PF(sched_entry_seq);
6709       PF(Save_SP);
6710       PF(Save_RP);
6711       PF(Save_MRP_in_frame);
6712       PF(extn_ptr_defined);
6713       PF(Cleanup_defined);
6714       PF(MPE_XL_interrupt_marker);
6715       PF(HP_UX_interrupt_marker);
6716       PF(Large_frame);
6717       PF(Pseudo_SP_Set);
6718       PV(Total_frame_size);
6719 #undef PF
6720 #undef PV
6721     }
6722 
6723   printf ("\n");
6724 }
6725 
6726 static int
slurp_hppa_unwind_table(FILE * file,struct hppa_unw_aux_info * aux,Elf_Internal_Shdr * sec)6727 slurp_hppa_unwind_table (FILE * file,
6728 			 struct hppa_unw_aux_info * aux,
6729 			 Elf_Internal_Shdr * sec)
6730 {
6731   unsigned long size, unw_ent_size, nentries, nrelas, i;
6732   Elf_Internal_Phdr * seg;
6733   struct hppa_unw_table_entry * tep;
6734   Elf_Internal_Shdr * relsec;
6735   Elf_Internal_Rela * rela;
6736   Elf_Internal_Rela * rp;
6737   unsigned char * table;
6738   unsigned char * tp;
6739   Elf_Internal_Sym * sym;
6740   const char * relname;
6741 
6742   /* First, find the starting address of the segment that includes
6743      this section.  */
6744 
6745   if (elf_header.e_phnum)
6746     {
6747       if (! get_program_headers (file))
6748 	return 0;
6749 
6750       for (seg = program_headers;
6751 	   seg < program_headers + elf_header.e_phnum;
6752 	   ++seg)
6753 	{
6754 	  if (seg->p_type != PT_LOAD)
6755 	    continue;
6756 
6757 	  if (sec->sh_addr >= seg->p_vaddr
6758 	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
6759 	    {
6760 	      aux->seg_base = seg->p_vaddr;
6761 	      break;
6762 	    }
6763 	}
6764     }
6765 
6766   /* Second, build the unwind table from the contents of the unwind
6767      section.  */
6768   size = sec->sh_size;
6769   table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
6770                                       _("unwind table"));
6771   if (!table)
6772     return 0;
6773 
6774   unw_ent_size = 16;
6775   nentries = size / unw_ent_size;
6776   size = unw_ent_size * nentries;
6777 
6778   tep = aux->table = (struct hppa_unw_table_entry *)
6779       xcmalloc (nentries, sizeof (aux->table[0]));
6780 
6781   for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
6782     {
6783       unsigned int tmp1, tmp2;
6784 
6785       tep->start.section = SHN_UNDEF;
6786       tep->end.section   = SHN_UNDEF;
6787 
6788       tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
6789       tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
6790       tmp1 = byte_get ((unsigned char *) tp + 8, 4);
6791       tmp2 = byte_get ((unsigned char *) tp + 12, 4);
6792 
6793       tep->start.offset += aux->seg_base;
6794       tep->end.offset   += aux->seg_base;
6795 
6796       tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
6797       tep->Millicode = (tmp1 >> 30) & 0x1;
6798       tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
6799       tep->Region_description = (tmp1 >> 27) & 0x3;
6800       tep->reserved1 = (tmp1 >> 26) & 0x1;
6801       tep->Entry_SR = (tmp1 >> 25) & 0x1;
6802       tep->Entry_FR = (tmp1 >> 21) & 0xf;
6803       tep->Entry_GR = (tmp1 >> 16) & 0x1f;
6804       tep->Args_stored = (tmp1 >> 15) & 0x1;
6805       tep->Variable_Frame = (tmp1 >> 14) & 0x1;
6806       tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
6807       tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
6808       tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
6809       tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
6810       tep->Ada_Region = (tmp1 >> 9) & 0x1;
6811       tep->cxx_info = (tmp1 >> 8) & 0x1;
6812       tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
6813       tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
6814       tep->reserved2 = (tmp1 >> 5) & 0x1;
6815       tep->Save_SP = (tmp1 >> 4) & 0x1;
6816       tep->Save_RP = (tmp1 >> 3) & 0x1;
6817       tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
6818       tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
6819       tep->Cleanup_defined = tmp1 & 0x1;
6820 
6821       tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
6822       tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
6823       tep->Large_frame = (tmp2 >> 29) & 0x1;
6824       tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
6825       tep->reserved4 = (tmp2 >> 27) & 0x1;
6826       tep->Total_frame_size = tmp2 & 0x7ffffff;
6827     }
6828   free (table);
6829 
6830   /* Third, apply any relocations to the unwind table.  */
6831   for (relsec = section_headers;
6832        relsec < section_headers + elf_header.e_shnum;
6833        ++relsec)
6834     {
6835       if (relsec->sh_type != SHT_RELA
6836 	  || relsec->sh_info >= elf_header.e_shnum
6837 	  || section_headers + relsec->sh_info != sec)
6838 	continue;
6839 
6840       if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
6841 			      & rela, & nrelas))
6842 	return 0;
6843 
6844       for (rp = rela; rp < rela + nrelas; ++rp)
6845 	{
6846 	  relname = elf_hppa_reloc_type (get_reloc_type (rp->r_info));
6847 	  sym = aux->symtab + get_reloc_symindex (rp->r_info);
6848 
6849 	  /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64.  */
6850 	  if (! const_strneq (relname, "R_PARISC_SEGREL"))
6851 	    {
6852 	      warn (_("Skipping unexpected relocation type %s\n"), relname);
6853 	      continue;
6854 	    }
6855 
6856 	  i = rp->r_offset / unw_ent_size;
6857 
6858 	  switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
6859 	    {
6860 	    case 0:
6861 	      aux->table[i].start.section = sym->st_shndx;
6862 	      aux->table[i].start.offset  = sym->st_value + rp->r_addend;
6863 	      break;
6864 	    case 1:
6865 	      aux->table[i].end.section   = sym->st_shndx;
6866 	      aux->table[i].end.offset    = sym->st_value + rp->r_addend;
6867 	      break;
6868 	    default:
6869 	      break;
6870 	    }
6871 	}
6872 
6873       free (rela);
6874     }
6875 
6876   aux->table_len = nentries;
6877 
6878   return 1;
6879 }
6880 
6881 static void
hppa_process_unwind(FILE * file)6882 hppa_process_unwind (FILE * file)
6883 {
6884   struct hppa_unw_aux_info aux;
6885   Elf_Internal_Shdr * unwsec = NULL;
6886   Elf_Internal_Shdr * strsec;
6887   Elf_Internal_Shdr * sec;
6888   unsigned long i;
6889 
6890   if (string_table == NULL)
6891     return;
6892 
6893   memset (& aux, 0, sizeof (aux));
6894 
6895   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
6896     {
6897       if (sec->sh_type == SHT_SYMTAB
6898 	  && sec->sh_link < elf_header.e_shnum)
6899 	{
6900 	  aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
6901 
6902 	  strsec = section_headers + sec->sh_link;
6903 	  if (aux.strtab != NULL)
6904 	    {
6905 	      error (_("Multiple auxillary string tables encountered\n"));
6906 	      free (aux.strtab);
6907 	    }
6908 	  aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
6909                                           1, strsec->sh_size,
6910                                           _("string table"));
6911 	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
6912 	}
6913       else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
6914 	unwsec = sec;
6915     }
6916 
6917   if (!unwsec)
6918     printf (_("\nThere are no unwind sections in this file.\n"));
6919 
6920   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
6921     {
6922       if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
6923 	{
6924 	  printf (_("\nUnwind section '%s' at offset 0x%lx contains %lu entries:\n"),
6925 		  printable_section_name (sec),
6926 		  (unsigned long) sec->sh_offset,
6927 		  (unsigned long) (sec->sh_size / (2 * eh_addr_size + 8)));
6928 
6929           slurp_hppa_unwind_table (file, &aux, sec);
6930 	  if (aux.table_len > 0)
6931 	    dump_hppa_unwind (&aux);
6932 
6933 	  if (aux.table)
6934 	    free ((char *) aux.table);
6935 	  aux.table = NULL;
6936 	}
6937     }
6938 
6939   if (aux.symtab)
6940     free (aux.symtab);
6941   if (aux.strtab)
6942     free ((char *) aux.strtab);
6943 }
6944 
6945 struct arm_section
6946 {
6947   unsigned char *      data;		/* The unwind data.  */
6948   Elf_Internal_Shdr *  sec;		/* The cached unwind section header.  */
6949   Elf_Internal_Rela *  rela;		/* The cached relocations for this section.  */
6950   unsigned long        nrelas;		/* The number of relocations.  */
6951   unsigned int         rel_type;	/* REL or RELA ?  */
6952   Elf_Internal_Rela *  next_rela;	/* Cyclic pointer to the next reloc to process.  */
6953 };
6954 
6955 struct arm_unw_aux_info
6956 {
6957   FILE *              file;		/* The file containing the unwind sections.  */
6958   Elf_Internal_Sym *  symtab;		/* The file's symbol table.  */
6959   unsigned long       nsyms;		/* Number of symbols.  */
6960   char *              strtab;		/* The file's string table.  */
6961   unsigned long       strtab_size;	/* Size of string table.  */
6962 };
6963 
6964 static const char *
arm_print_vma_and_name(struct arm_unw_aux_info * aux,bfd_vma fn,struct absaddr addr)6965 arm_print_vma_and_name (struct arm_unw_aux_info *aux,
6966 			bfd_vma fn, struct absaddr addr)
6967 {
6968   const char *procname;
6969   bfd_vma sym_offset;
6970 
6971   if (addr.section == SHN_UNDEF)
6972     addr.offset = fn;
6973 
6974   find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
6975 			   aux->strtab_size, addr, &procname,
6976 			   &sym_offset);
6977 
6978   print_vma (fn, PREFIX_HEX);
6979 
6980   if (procname)
6981     {
6982       fputs (" <", stdout);
6983       fputs (procname, stdout);
6984 
6985       if (sym_offset)
6986 	printf ("+0x%lx", (unsigned long) sym_offset);
6987       fputc ('>', stdout);
6988     }
6989 
6990   return procname;
6991 }
6992 
6993 static void
arm_free_section(struct arm_section * arm_sec)6994 arm_free_section (struct arm_section *arm_sec)
6995 {
6996   if (arm_sec->data != NULL)
6997     free (arm_sec->data);
6998 
6999   if (arm_sec->rela != NULL)
7000     free (arm_sec->rela);
7001 }
7002 
7003 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
7004       cached section and install SEC instead.
7005    2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
7006       and return its valued in * WORDP, relocating if necessary.
7007    3) Update the NEXT_RELA field in ARM_SEC and store the section index and
7008       relocation's offset in ADDR.
7009    4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
7010       into the string table of the symbol associated with the reloc.  If no
7011       reloc was applied store -1 there.
7012    5) Return TRUE upon success, FALSE otherwise.  */
7013 
7014 static bfd_boolean
get_unwind_section_word(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)7015 get_unwind_section_word (struct arm_unw_aux_info *  aux,
7016 			 struct arm_section *       arm_sec,
7017 			 Elf_Internal_Shdr *        sec,
7018 			 bfd_vma 		    word_offset,
7019 			 unsigned int *             wordp,
7020 			 struct absaddr *           addr,
7021 			 bfd_vma *		    sym_name)
7022 {
7023   Elf_Internal_Rela *rp;
7024   Elf_Internal_Sym *sym;
7025   const char * relname;
7026   unsigned int word;
7027   bfd_boolean wrapped;
7028 
7029   if (sec == NULL || arm_sec == NULL)
7030     return FALSE;
7031 
7032   addr->section = SHN_UNDEF;
7033   addr->offset = 0;
7034 
7035   if (sym_name != NULL)
7036     *sym_name = (bfd_vma) -1;
7037 
7038   /* If necessary, update the section cache.  */
7039   if (sec != arm_sec->sec)
7040     {
7041       Elf_Internal_Shdr *relsec;
7042 
7043       arm_free_section (arm_sec);
7044 
7045       arm_sec->sec = sec;
7046       arm_sec->data = get_data (NULL, aux->file, sec->sh_offset, 1,
7047 				sec->sh_size, _("unwind data"));
7048       arm_sec->rela = NULL;
7049       arm_sec->nrelas = 0;
7050 
7051       for (relsec = section_headers;
7052 	   relsec < section_headers + elf_header.e_shnum;
7053 	   ++relsec)
7054 	{
7055 	  if (relsec->sh_info >= elf_header.e_shnum
7056 	      || section_headers + relsec->sh_info != sec
7057 	      /* PR 15745: Check the section type as well.  */
7058 	      || (relsec->sh_type != SHT_REL
7059 		  && relsec->sh_type != SHT_RELA))
7060 	    continue;
7061 
7062 	  arm_sec->rel_type = relsec->sh_type;
7063 	  if (relsec->sh_type == SHT_REL)
7064 	    {
7065 	      if (!slurp_rel_relocs (aux->file, relsec->sh_offset,
7066 				     relsec->sh_size,
7067 				     & arm_sec->rela, & arm_sec->nrelas))
7068 		return FALSE;
7069 	    }
7070 	  else /* relsec->sh_type == SHT_RELA */
7071 	    {
7072 	      if (!slurp_rela_relocs (aux->file, relsec->sh_offset,
7073 				      relsec->sh_size,
7074 				      & arm_sec->rela, & arm_sec->nrelas))
7075 		return FALSE;
7076 	    }
7077 	  break;
7078 	}
7079 
7080       arm_sec->next_rela = arm_sec->rela;
7081     }
7082 
7083   /* If there is no unwind data we can do nothing.  */
7084   if (arm_sec->data == NULL)
7085     return FALSE;
7086 
7087   /* If the offset is invalid then fail.  */
7088   if (word_offset > sec->sh_size - 4)
7089     return FALSE;
7090 
7091   /* Get the word at the required offset.  */
7092   word = byte_get (arm_sec->data + word_offset, 4);
7093 
7094   /* Look through the relocs to find the one that applies to the provided offset.  */
7095   wrapped = FALSE;
7096   for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
7097     {
7098       bfd_vma prelval, offset;
7099 
7100       if (rp->r_offset > word_offset && !wrapped)
7101 	{
7102 	  rp = arm_sec->rela;
7103 	  wrapped = TRUE;
7104 	}
7105       if (rp->r_offset > word_offset)
7106 	break;
7107 
7108       if (rp->r_offset & 3)
7109 	{
7110 	  warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
7111 		(unsigned long) rp->r_offset);
7112 	  continue;
7113 	}
7114 
7115       if (rp->r_offset < word_offset)
7116 	continue;
7117 
7118       /* PR 17531: file: 027-161405-0.004  */
7119       if (aux->symtab == NULL)
7120 	continue;
7121 
7122       if (arm_sec->rel_type == SHT_REL)
7123 	{
7124 	  offset = word & 0x7fffffff;
7125 	  if (offset & 0x40000000)
7126 	    offset |= ~ (bfd_vma) 0x7fffffff;
7127 	}
7128       else if (arm_sec->rel_type == SHT_RELA)
7129 	offset = rp->r_addend;
7130       else
7131 	{
7132 	  error (_("Unknown section relocation type %d encountered\n"),
7133 		 arm_sec->rel_type);
7134 	  break;
7135 	}
7136 
7137       /* PR 17531 file: 027-1241568-0.004.  */
7138       if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
7139 	{
7140 	  error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
7141 		 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
7142 	  break;
7143 	}
7144 
7145       sym = aux->symtab + ELF32_R_SYM (rp->r_info);
7146       offset += sym->st_value;
7147       prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
7148 
7149       /* Check that we are processing the expected reloc type.  */
7150       if (elf_header.e_machine == EM_ARM)
7151 	{
7152 	  relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
7153 	  if (relname == NULL)
7154 	    {
7155 	      warn (_("Skipping unknown ARM relocation type: %d\n"),
7156 		    (int) ELF32_R_TYPE (rp->r_info));
7157 	      continue;
7158 	    }
7159 
7160 	  if (streq (relname, "R_ARM_NONE"))
7161 	      continue;
7162 
7163 	  if (! streq (relname, "R_ARM_PREL31"))
7164 	    {
7165 	      warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
7166 	      continue;
7167 	    }
7168 	}
7169       else if (elf_header.e_machine == EM_TI_C6000)
7170 	{
7171 	  relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
7172 	  if (relname == NULL)
7173 	    {
7174 	      warn (_("Skipping unknown C6000 relocation type: %d\n"),
7175 		    (int) ELF32_R_TYPE (rp->r_info));
7176 	      continue;
7177 	    }
7178 
7179 	  if (streq (relname, "R_C6000_NONE"))
7180 	    continue;
7181 
7182 	  if (! streq (relname, "R_C6000_PREL31"))
7183 	    {
7184 	      warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
7185 	      continue;
7186 	    }
7187 
7188 	  prelval >>= 1;
7189 	}
7190       else
7191 	{
7192 	  /* This function currently only supports ARM and TI unwinders.  */
7193 	  warn (_("Only TI and ARM unwinders are currently supported\n"));
7194 	  break;
7195 	}
7196 
7197       word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
7198       addr->section = sym->st_shndx;
7199       addr->offset = offset;
7200 
7201       if (sym_name)
7202 	* sym_name = sym->st_name;
7203       break;
7204     }
7205 
7206   *wordp = word;
7207   arm_sec->next_rela = rp;
7208 
7209   return TRUE;
7210 }
7211 
7212 static const char *tic6x_unwind_regnames[16] =
7213 {
7214   "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
7215   "A14", "A13", "A12", "A11", "A10",
7216   "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
7217 };
7218 
7219 static void
decode_tic6x_unwind_regmask(unsigned int mask)7220 decode_tic6x_unwind_regmask (unsigned int mask)
7221 {
7222   int i;
7223 
7224   for (i = 12; mask; mask >>= 1, i--)
7225     {
7226       if (mask & 1)
7227 	{
7228 	  fputs (tic6x_unwind_regnames[i], stdout);
7229 	  if (mask > 1)
7230 	    fputs (", ", stdout);
7231 	}
7232     }
7233 }
7234 
7235 #define ADVANCE							\
7236   if (remaining == 0 && more_words)				\
7237     {								\
7238       data_offset += 4;						\
7239       if (! get_unwind_section_word (aux, data_arm_sec, data_sec,	\
7240 				     data_offset, & word, & addr, NULL))	\
7241 	return;							\
7242       remaining = 4;						\
7243       more_words--;						\
7244     }								\
7245 
7246 #define GET_OP(OP)			\
7247   ADVANCE;				\
7248   if (remaining)			\
7249     {					\
7250       remaining--;			\
7251       (OP) = word >> 24;		\
7252       word <<= 8;			\
7253     }					\
7254   else					\
7255     {					\
7256       printf (_("[Truncated opcode]\n"));	\
7257       return;				\
7258     }					\
7259   printf ("0x%02x ", OP)
7260 
7261 static void
decode_arm_unwind_bytecode(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)7262 decode_arm_unwind_bytecode (struct arm_unw_aux_info *aux,
7263 			    unsigned int word, unsigned int remaining,
7264 			    unsigned int more_words,
7265 			    bfd_vma data_offset, Elf_Internal_Shdr *data_sec,
7266 			    struct arm_section *data_arm_sec)
7267 {
7268   struct absaddr addr;
7269 
7270   /* Decode the unwinding instructions.  */
7271   while (1)
7272     {
7273       unsigned int op, op2;
7274 
7275       ADVANCE;
7276       if (remaining == 0)
7277 	break;
7278       remaining--;
7279       op = word >> 24;
7280       word <<= 8;
7281 
7282       printf ("  0x%02x ", op);
7283 
7284       if ((op & 0xc0) == 0x00)
7285 	{
7286 	  int offset = ((op & 0x3f) << 2) + 4;
7287 
7288 	  printf ("     vsp = vsp + %d", offset);
7289 	}
7290       else if ((op & 0xc0) == 0x40)
7291 	{
7292 	  int offset = ((op & 0x3f) << 2) + 4;
7293 
7294 	  printf ("     vsp = vsp - %d", offset);
7295 	}
7296       else if ((op & 0xf0) == 0x80)
7297 	{
7298 	  GET_OP (op2);
7299 	  if (op == 0x80 && op2 == 0)
7300 	    printf (_("Refuse to unwind"));
7301 	  else
7302 	    {
7303 	      unsigned int mask = ((op & 0x0f) << 8) | op2;
7304 	      int first = 1;
7305 	      int i;
7306 
7307 	      printf ("pop {");
7308 	      for (i = 0; i < 12; i++)
7309 		if (mask & (1 << i))
7310 		  {
7311 		    if (first)
7312 		      first = 0;
7313 		    else
7314 		      printf (", ");
7315 		    printf ("r%d", 4 + i);
7316 		  }
7317 	      printf ("}");
7318 	    }
7319 	}
7320       else if ((op & 0xf0) == 0x90)
7321 	{
7322 	  if (op == 0x9d || op == 0x9f)
7323 	    printf (_("     [Reserved]"));
7324 	  else
7325 	    printf ("     vsp = r%d", op & 0x0f);
7326 	}
7327       else if ((op & 0xf0) == 0xa0)
7328 	{
7329 	  int end = 4 + (op & 0x07);
7330 	  int first = 1;
7331 	  int i;
7332 
7333 	  printf ("     pop {");
7334 	  for (i = 4; i <= end; i++)
7335 	    {
7336 	      if (first)
7337 		first = 0;
7338 	      else
7339 		printf (", ");
7340 	      printf ("r%d", i);
7341 	    }
7342 	  if (op & 0x08)
7343 	    {
7344 	      if (!first)
7345 		printf (", ");
7346 	      printf ("r14");
7347 	    }
7348 	  printf ("}");
7349 	}
7350       else if (op == 0xb0)
7351 	printf (_("     finish"));
7352       else if (op == 0xb1)
7353 	{
7354 	  GET_OP (op2);
7355 	  if (op2 == 0 || (op2 & 0xf0) != 0)
7356 	    printf (_("[Spare]"));
7357 	  else
7358 	    {
7359 	      unsigned int mask = op2 & 0x0f;
7360 	      int first = 1;
7361 	      int i;
7362 
7363 	      printf ("pop {");
7364 	      for (i = 0; i < 12; i++)
7365 		if (mask & (1 << i))
7366 		  {
7367 		    if (first)
7368 		      first = 0;
7369 		    else
7370 		      printf (", ");
7371 		    printf ("r%d", i);
7372 		  }
7373 	      printf ("}");
7374 	    }
7375 	}
7376       else if (op == 0xb2)
7377 	{
7378 	  unsigned char buf[9];
7379 	  unsigned int i, len;
7380 	  unsigned long offset;
7381 
7382 	  for (i = 0; i < sizeof (buf); i++)
7383 	    {
7384 	      GET_OP (buf[i]);
7385 	      if ((buf[i] & 0x80) == 0)
7386 		break;
7387 	    }
7388 	  if (i == sizeof (buf))
7389 	    printf (_("corrupt change to vsp"));
7390 	  else
7391 	    {
7392 	      offset = read_uleb128 (buf, &len, buf + i + 1);
7393 	      assert (len == i + 1);
7394 	      offset = offset * 4 + 0x204;
7395 	      printf ("vsp = vsp + %ld", offset);
7396 	    }
7397 	}
7398       else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
7399 	{
7400 	  unsigned int first, last;
7401 
7402 	  GET_OP (op2);
7403 	  first = op2 >> 4;
7404 	  last = op2 & 0x0f;
7405 	  if (op == 0xc8)
7406 	    first = first + 16;
7407 	  printf ("pop {D%d", first);
7408 	  if (last)
7409 	    printf ("-D%d", first + last);
7410 	  printf ("}");
7411 	}
7412       else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
7413 	{
7414 	  unsigned int count = op & 0x07;
7415 
7416 	  printf ("pop {D8");
7417 	  if (count)
7418 	    printf ("-D%d", 8 + count);
7419 	  printf ("}");
7420 	}
7421       else if (op >= 0xc0 && op <= 0xc5)
7422 	{
7423 	  unsigned int count = op & 0x07;
7424 
7425 	  printf ("     pop {wR10");
7426 	  if (count)
7427 	    printf ("-wR%d", 10 + count);
7428 	  printf ("}");
7429 	}
7430       else if (op == 0xc6)
7431 	{
7432 	  unsigned int first, last;
7433 
7434 	  GET_OP (op2);
7435 	  first = op2 >> 4;
7436 	  last = op2 & 0x0f;
7437 	  printf ("pop {wR%d", first);
7438 	  if (last)
7439 	    printf ("-wR%d", first + last);
7440 	  printf ("}");
7441 	}
7442       else if (op == 0xc7)
7443 	{
7444 	  GET_OP (op2);
7445 	  if (op2 == 0 || (op2 & 0xf0) != 0)
7446 	    printf (_("[Spare]"));
7447 	  else
7448 	    {
7449 	      unsigned int mask = op2 & 0x0f;
7450 	      int first = 1;
7451 	      int i;
7452 
7453 	      printf ("pop {");
7454 	      for (i = 0; i < 4; i++)
7455 		if (mask & (1 << i))
7456 		  {
7457 		    if (first)
7458 		      first = 0;
7459 		    else
7460 		      printf (", ");
7461 		    printf ("wCGR%d", i);
7462 		  }
7463 	      printf ("}");
7464 	    }
7465 	}
7466       else
7467 	printf (_("     [unsupported opcode]"));
7468       printf ("\n");
7469     }
7470 }
7471 
7472 static void
decode_tic6x_unwind_bytecode(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)7473 decode_tic6x_unwind_bytecode (struct arm_unw_aux_info *aux,
7474 			    unsigned int word, unsigned int remaining,
7475 			    unsigned int more_words,
7476 			    bfd_vma data_offset, Elf_Internal_Shdr *data_sec,
7477 			    struct arm_section *data_arm_sec)
7478 {
7479   struct absaddr addr;
7480 
7481   /* Decode the unwinding instructions.  */
7482   while (1)
7483     {
7484       unsigned int op, op2;
7485 
7486       ADVANCE;
7487       if (remaining == 0)
7488 	break;
7489       remaining--;
7490       op = word >> 24;
7491       word <<= 8;
7492 
7493       printf ("  0x%02x ", op);
7494 
7495       if ((op & 0xc0) == 0x00)
7496 	{
7497 	  int offset = ((op & 0x3f) << 3) + 8;
7498 	  printf ("     sp = sp + %d", offset);
7499 	}
7500       else if ((op & 0xc0) == 0x80)
7501 	{
7502 	  GET_OP (op2);
7503 	  if (op == 0x80 && op2 == 0)
7504 	    printf (_("Refuse to unwind"));
7505 	  else
7506 	    {
7507 	      unsigned int mask = ((op & 0x1f) << 8) | op2;
7508 	      if (op & 0x20)
7509 		printf ("pop compact {");
7510 	      else
7511 		printf ("pop {");
7512 
7513 	      decode_tic6x_unwind_regmask (mask);
7514 	      printf("}");
7515 	    }
7516 	}
7517       else if ((op & 0xf0) == 0xc0)
7518 	{
7519 	  unsigned int reg;
7520 	  unsigned int nregs;
7521 	  unsigned int i;
7522 	  const char *name;
7523 	  struct
7524 	  {
7525 	      unsigned int offset;
7526 	      unsigned int reg;
7527 	  } regpos[16];
7528 
7529 	  /* Scan entire instruction first so that GET_OP output is not
7530 	     interleaved with disassembly.  */
7531 	  nregs = 0;
7532 	  for (i = 0; nregs < (op & 0xf); i++)
7533 	    {
7534 	      GET_OP (op2);
7535 	      reg = op2 >> 4;
7536 	      if (reg != 0xf)
7537 		{
7538 		  regpos[nregs].offset = i * 2;
7539 		  regpos[nregs].reg = reg;
7540 		  nregs++;
7541 		}
7542 
7543 	      reg = op2 & 0xf;
7544 	      if (reg != 0xf)
7545 		{
7546 		  regpos[nregs].offset = i * 2 + 1;
7547 		  regpos[nregs].reg = reg;
7548 		  nregs++;
7549 		}
7550 	    }
7551 
7552 	  printf (_("pop frame {"));
7553 	  reg = nregs - 1;
7554 	  for (i = i * 2; i > 0; i--)
7555 	    {
7556 	      if (regpos[reg].offset == i - 1)
7557 		{
7558 		  name = tic6x_unwind_regnames[regpos[reg].reg];
7559 		  if (reg > 0)
7560 		    reg--;
7561 		}
7562 	      else
7563 		name = _("[pad]");
7564 
7565 	      fputs (name, stdout);
7566 	      if (i > 1)
7567 		printf (", ");
7568 	    }
7569 
7570 	  printf ("}");
7571 	}
7572       else if (op == 0xd0)
7573 	printf ("     MOV FP, SP");
7574       else if (op == 0xd1)
7575 	printf ("     __c6xabi_pop_rts");
7576       else if (op == 0xd2)
7577 	{
7578 	  unsigned char buf[9];
7579 	  unsigned int i, len;
7580 	  unsigned long offset;
7581 
7582 	  for (i = 0; i < sizeof (buf); i++)
7583 	    {
7584 	      GET_OP (buf[i]);
7585 	      if ((buf[i] & 0x80) == 0)
7586 		break;
7587 	    }
7588 	  assert (i < sizeof (buf));
7589 	  offset = read_uleb128 (buf, &len, buf + i + 1);
7590 	  assert (len == i + 1);
7591 	  offset = offset * 8 + 0x408;
7592 	  printf (_("sp = sp + %ld"), offset);
7593 	}
7594       else if ((op & 0xf0) == 0xe0)
7595 	{
7596 	  if ((op & 0x0f) == 7)
7597 	    printf ("     RETURN");
7598 	  else
7599 	    printf ("     MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
7600 	}
7601       else
7602 	{
7603 	  printf (_("     [unsupported opcode]"));
7604 	}
7605       putchar ('\n');
7606     }
7607 }
7608 
7609 static bfd_vma
arm_expand_prel31(bfd_vma word,bfd_vma where)7610 arm_expand_prel31 (bfd_vma word, bfd_vma where)
7611 {
7612   bfd_vma offset;
7613 
7614   offset = word & 0x7fffffff;
7615   if (offset & 0x40000000)
7616     offset |= ~ (bfd_vma) 0x7fffffff;
7617 
7618   if (elf_header.e_machine == EM_TI_C6000)
7619     offset <<= 1;
7620 
7621   return offset + where;
7622 }
7623 
7624 static void
decode_arm_unwind(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)7625 decode_arm_unwind (struct arm_unw_aux_info *  aux,
7626 		   unsigned int               word,
7627 		   unsigned int               remaining,
7628 		   bfd_vma                    data_offset,
7629 		   Elf_Internal_Shdr *        data_sec,
7630 		   struct arm_section *       data_arm_sec)
7631 {
7632   int per_index;
7633   unsigned int more_words = 0;
7634   struct absaddr addr;
7635   bfd_vma sym_name = (bfd_vma) -1;
7636 
7637   if (remaining == 0)
7638     {
7639       /* Fetch the first word.
7640 	 Note - when decoding an object file the address extracted
7641 	 here will always be 0.  So we also pass in the sym_name
7642 	 parameter so that we can find the symbol associated with
7643 	 the personality routine.  */
7644       if (! get_unwind_section_word (aux, data_arm_sec, data_sec, data_offset,
7645 				     & word, & addr, & sym_name))
7646 	return;
7647 
7648       remaining = 4;
7649     }
7650 
7651   if ((word & 0x80000000) == 0)
7652     {
7653       /* Expand prel31 for personality routine.  */
7654       bfd_vma fn;
7655       const char *procname;
7656 
7657       fn = arm_expand_prel31 (word, data_sec->sh_addr + data_offset);
7658       printf (_("  Personality routine: "));
7659       if (fn == 0
7660 	  && addr.section == SHN_UNDEF && addr.offset == 0
7661 	  && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
7662 	{
7663 	  procname = aux->strtab + sym_name;
7664 	  print_vma (fn, PREFIX_HEX);
7665 	  if (procname)
7666 	    {
7667 	      fputs (" <", stdout);
7668 	      fputs (procname, stdout);
7669 	      fputc ('>', stdout);
7670 	    }
7671 	}
7672       else
7673 	procname = arm_print_vma_and_name (aux, fn, addr);
7674       fputc ('\n', stdout);
7675 
7676       /* The GCC personality routines use the standard compact
7677 	 encoding, starting with one byte giving the number of
7678 	 words.  */
7679       if (procname != NULL
7680 	  && (const_strneq (procname, "__gcc_personality_v0")
7681 	      || const_strneq (procname, "__gxx_personality_v0")
7682 	      || const_strneq (procname, "__gcj_personality_v0")
7683 	      || const_strneq (procname, "__gnu_objc_personality_v0")))
7684 	{
7685 	  remaining = 0;
7686 	  more_words = 1;
7687 	  ADVANCE;
7688 	  if (!remaining)
7689 	    {
7690 	      printf (_("  [Truncated data]\n"));
7691 	      return;
7692 	    }
7693 	  more_words = word >> 24;
7694 	  word <<= 8;
7695 	  remaining--;
7696 	  per_index = -1;
7697 	}
7698       else
7699 	return;
7700     }
7701   else
7702     {
7703       /* ARM EHABI Section 6.3:
7704 
7705 	 An exception-handling table entry for the compact model looks like:
7706 
7707            31 30-28 27-24 23-0
7708 	   -- ----- ----- ----
7709             1   0   index Data for personalityRoutine[index]    */
7710 
7711       if (elf_header.e_machine == EM_ARM
7712 	  && (word & 0x70000000))
7713 	warn (_("Corrupt ARM compact model table entry: %x \n"), word);
7714 
7715       per_index = (word >> 24) & 0x7f;
7716       printf (_("  Compact model index: %d\n"), per_index);
7717       if (per_index == 0)
7718 	{
7719 	  more_words = 0;
7720 	  word <<= 8;
7721 	  remaining--;
7722 	}
7723       else if (per_index < 3)
7724 	{
7725 	  more_words = (word >> 16) & 0xff;
7726 	  word <<= 16;
7727 	  remaining -= 2;
7728 	}
7729     }
7730 
7731   switch (elf_header.e_machine)
7732     {
7733     case EM_ARM:
7734       if (per_index < 3)
7735 	{
7736 	  decode_arm_unwind_bytecode (aux, word, remaining, more_words,
7737 				      data_offset, data_sec, data_arm_sec);
7738 	}
7739       else
7740 	{
7741 	  warn (_("Unknown ARM compact model index encountered\n"));
7742 	  printf (_("  [reserved]\n"));
7743 	}
7744       break;
7745 
7746     case EM_TI_C6000:
7747       if (per_index < 3)
7748 	{
7749 	  decode_tic6x_unwind_bytecode (aux, word, remaining, more_words,
7750 					data_offset, data_sec, data_arm_sec);
7751 	}
7752       else if (per_index < 5)
7753 	{
7754 	  if (((word >> 17) & 0x7f) == 0x7f)
7755 	    printf (_("  Restore stack from frame pointer\n"));
7756 	  else
7757 	    printf (_("  Stack increment %d\n"), (word >> 14) & 0x1fc);
7758 	  printf (_("  Registers restored: "));
7759 	  if (per_index == 4)
7760 	    printf (" (compact) ");
7761 	  decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
7762 	  putchar ('\n');
7763 	  printf (_("  Return register: %s\n"),
7764 		  tic6x_unwind_regnames[word & 0xf]);
7765 	}
7766       else
7767 	printf (_("  [reserved (%d)]\n"), per_index);
7768       break;
7769 
7770     default:
7771       error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
7772 	     elf_header.e_machine);
7773     }
7774 
7775   /* Decode the descriptors.  Not implemented.  */
7776 }
7777 
7778 static void
dump_arm_unwind(struct arm_unw_aux_info * aux,Elf_Internal_Shdr * exidx_sec)7779 dump_arm_unwind (struct arm_unw_aux_info *aux, Elf_Internal_Shdr *exidx_sec)
7780 {
7781   struct arm_section exidx_arm_sec, extab_arm_sec;
7782   unsigned int i, exidx_len;
7783 
7784   memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
7785   memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
7786   exidx_len = exidx_sec->sh_size / 8;
7787 
7788   for (i = 0; i < exidx_len; i++)
7789     {
7790       unsigned int exidx_fn, exidx_entry;
7791       struct absaddr fn_addr, entry_addr;
7792       bfd_vma fn;
7793 
7794       fputc ('\n', stdout);
7795 
7796       if (! get_unwind_section_word (aux, & exidx_arm_sec, exidx_sec,
7797 				     8 * i, & exidx_fn, & fn_addr, NULL)
7798 	  || ! get_unwind_section_word (aux, & exidx_arm_sec, exidx_sec,
7799 					8 * i + 4, & exidx_entry, & entry_addr, NULL))
7800 	{
7801 	  arm_free_section (& exidx_arm_sec);
7802 	  arm_free_section (& extab_arm_sec);
7803 	  return;
7804 	}
7805 
7806       /* ARM EHABI, Section 5:
7807 	 An index table entry consists of 2 words.
7808          The first word contains a prel31 offset to the start of a function, with bit 31 clear.  */
7809       if (exidx_fn & 0x80000000)
7810 	warn (_("corrupt index table entry: %x\n"), exidx_fn);
7811 
7812       fn = arm_expand_prel31 (exidx_fn, exidx_sec->sh_addr + 8 * i);
7813 
7814       arm_print_vma_and_name (aux, fn, fn_addr);
7815       fputs (": ", stdout);
7816 
7817       if (exidx_entry == 1)
7818 	{
7819 	  print_vma (exidx_entry, PREFIX_HEX);
7820 	  fputs (" [cantunwind]\n", stdout);
7821 	}
7822       else if (exidx_entry & 0x80000000)
7823 	{
7824 	  print_vma (exidx_entry, PREFIX_HEX);
7825 	  fputc ('\n', stdout);
7826 	  decode_arm_unwind (aux, exidx_entry, 4, 0, NULL, NULL);
7827 	}
7828       else
7829 	{
7830 	  bfd_vma table, table_offset = 0;
7831 	  Elf_Internal_Shdr *table_sec;
7832 
7833 	  fputs ("@", stdout);
7834 	  table = arm_expand_prel31 (exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
7835 	  print_vma (table, PREFIX_HEX);
7836 	  printf ("\n");
7837 
7838 	  /* Locate the matching .ARM.extab.  */
7839 	  if (entry_addr.section != SHN_UNDEF
7840 	      && entry_addr.section < elf_header.e_shnum)
7841 	    {
7842 	      table_sec = section_headers + entry_addr.section;
7843 	      table_offset = entry_addr.offset;
7844 	    }
7845 	  else
7846 	    {
7847 	      table_sec = find_section_by_address (table);
7848 	      if (table_sec != NULL)
7849 		table_offset = table - table_sec->sh_addr;
7850 	    }
7851 	  if (table_sec == NULL)
7852 	    {
7853 	      warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
7854 		    (unsigned long) table);
7855 	      continue;
7856 	    }
7857 	  decode_arm_unwind (aux, 0, 0, table_offset, table_sec,
7858 			     &extab_arm_sec);
7859 	}
7860     }
7861 
7862   printf ("\n");
7863 
7864   arm_free_section (&exidx_arm_sec);
7865   arm_free_section (&extab_arm_sec);
7866 }
7867 
7868 /* Used for both ARM and C6X unwinding tables.  */
7869 
7870 static void
arm_process_unwind(FILE * file)7871 arm_process_unwind (FILE *file)
7872 {
7873   struct arm_unw_aux_info aux;
7874   Elf_Internal_Shdr *unwsec = NULL;
7875   Elf_Internal_Shdr *strsec;
7876   Elf_Internal_Shdr *sec;
7877   unsigned long i;
7878   unsigned int sec_type;
7879 
7880   switch (elf_header.e_machine)
7881     {
7882     case EM_ARM:
7883       sec_type = SHT_ARM_EXIDX;
7884       break;
7885 
7886     case EM_TI_C6000:
7887       sec_type = SHT_C6000_UNWIND;
7888       break;
7889 
7890     default:
7891       error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
7892 	     elf_header.e_machine);
7893       return;
7894     }
7895 
7896   if (string_table == NULL)
7897     return;
7898 
7899   memset (& aux, 0, sizeof (aux));
7900   aux.file = file;
7901 
7902   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7903     {
7904       if (sec->sh_type == SHT_SYMTAB && sec->sh_link < elf_header.e_shnum)
7905 	{
7906 	  aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
7907 
7908 	  strsec = section_headers + sec->sh_link;
7909 
7910 	  /* PR binutils/17531 file: 011-12666-0.004.  */
7911 	  if (aux.strtab != NULL)
7912 	    {
7913 	      error (_("Multiple string tables found in file.\n"));
7914 	      free (aux.strtab);
7915 	    }
7916 	  aux.strtab = get_data (NULL, file, strsec->sh_offset,
7917 				 1, strsec->sh_size, _("string table"));
7918 	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
7919 	}
7920       else if (sec->sh_type == sec_type)
7921 	unwsec = sec;
7922     }
7923 
7924   if (unwsec == NULL)
7925     printf (_("\nThere are no unwind sections in this file.\n"));
7926   else
7927     for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7928       {
7929 	if (sec->sh_type == sec_type)
7930 	  {
7931 	    printf (_("\nUnwind table index '%s' at offset 0x%lx contains %lu entries:\n"),
7932 		    printable_section_name (sec),
7933 		    (unsigned long) sec->sh_offset,
7934 		    (unsigned long) (sec->sh_size / (2 * eh_addr_size)));
7935 
7936 	    dump_arm_unwind (&aux, sec);
7937 	  }
7938       }
7939 
7940   if (aux.symtab)
7941     free (aux.symtab);
7942   if (aux.strtab)
7943     free ((char *) aux.strtab);
7944 }
7945 
7946 static void
process_unwind(FILE * file)7947 process_unwind (FILE * file)
7948 {
7949   struct unwind_handler
7950   {
7951     int machtype;
7952     void (* handler)(FILE *);
7953   } handlers[] =
7954   {
7955     { EM_ARM, arm_process_unwind },
7956     { EM_IA_64, ia64_process_unwind },
7957     { EM_PARISC, hppa_process_unwind },
7958     { EM_TI_C6000, arm_process_unwind },
7959     { 0, 0 }
7960   };
7961   int i;
7962 
7963   if (!do_unwind)
7964     return;
7965 
7966   for (i = 0; handlers[i].handler != NULL; i++)
7967     if (elf_header.e_machine == handlers[i].machtype)
7968       {
7969 	handlers[i].handler (file);
7970 	return;
7971       }
7972 
7973   printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
7974 	  get_machine_name (elf_header.e_machine));
7975 }
7976 
7977 static void
dynamic_section_mips_val(Elf_Internal_Dyn * entry)7978 dynamic_section_mips_val (Elf_Internal_Dyn * entry)
7979 {
7980   switch (entry->d_tag)
7981     {
7982     case DT_MIPS_FLAGS:
7983       if (entry->d_un.d_val == 0)
7984 	printf (_("NONE"));
7985       else
7986 	{
7987 	  static const char * opts[] =
7988 	  {
7989 	    "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
7990 	    "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
7991 	    "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
7992 	    "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
7993 	    "RLD_ORDER_SAFE"
7994 	  };
7995 	  unsigned int cnt;
7996 	  int first = 1;
7997 
7998 	  for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
7999 	    if (entry->d_un.d_val & (1 << cnt))
8000 	      {
8001 		printf ("%s%s", first ? "" : " ", opts[cnt]);
8002 		first = 0;
8003 	      }
8004 	}
8005       break;
8006 
8007     case DT_MIPS_IVERSION:
8008       if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
8009 	printf (_("Interface Version: %s"), GET_DYNAMIC_NAME (entry->d_un.d_val));
8010       else
8011 	{
8012 	  char buf[40];
8013 	  sprintf_vma (buf, entry->d_un.d_ptr);
8014 	  /* Note: coded this way so that there is a single string for translation.  */
8015 	  printf (_("<corrupt: %s>"), buf);
8016 	}
8017       break;
8018 
8019     case DT_MIPS_TIME_STAMP:
8020       {
8021 	char timebuf[20];
8022 	struct tm * tmp;
8023 
8024 	time_t atime = entry->d_un.d_val;
8025 	tmp = gmtime (&atime);
8026 	snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
8027 		  tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
8028 		  tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
8029 	printf (_("Time Stamp: %s"), timebuf);
8030       }
8031       break;
8032 
8033     case DT_MIPS_RLD_VERSION:
8034     case DT_MIPS_LOCAL_GOTNO:
8035     case DT_MIPS_CONFLICTNO:
8036     case DT_MIPS_LIBLISTNO:
8037     case DT_MIPS_SYMTABNO:
8038     case DT_MIPS_UNREFEXTNO:
8039     case DT_MIPS_HIPAGENO:
8040     case DT_MIPS_DELTA_CLASS_NO:
8041     case DT_MIPS_DELTA_INSTANCE_NO:
8042     case DT_MIPS_DELTA_RELOC_NO:
8043     case DT_MIPS_DELTA_SYM_NO:
8044     case DT_MIPS_DELTA_CLASSSYM_NO:
8045     case DT_MIPS_COMPACT_SIZE:
8046       print_vma (entry->d_un.d_ptr, DEC);
8047       break;
8048 
8049     default:
8050       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8051     }
8052     putchar ('\n');
8053 }
8054 
8055 static void
dynamic_section_parisc_val(Elf_Internal_Dyn * entry)8056 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
8057 {
8058   switch (entry->d_tag)
8059     {
8060     case DT_HP_DLD_FLAGS:
8061       {
8062 	static struct
8063 	{
8064 	  long int bit;
8065 	  const char * str;
8066 	}
8067 	flags[] =
8068 	{
8069 	  { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
8070 	  { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
8071 	  { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
8072 	  { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
8073 	  { DT_HP_BIND_NOW, "HP_BIND_NOW" },
8074 	  { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
8075 	  { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
8076 	  { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
8077 	  { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
8078 	  { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
8079 	  { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
8080 	  { DT_HP_GST, "HP_GST" },
8081 	  { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
8082 	  { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
8083 	  { DT_HP_NODELETE, "HP_NODELETE" },
8084 	  { DT_HP_GROUP, "HP_GROUP" },
8085 	  { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
8086 	};
8087 	int first = 1;
8088 	size_t cnt;
8089 	bfd_vma val = entry->d_un.d_val;
8090 
8091 	for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
8092 	  if (val & flags[cnt].bit)
8093 	    {
8094 	      if (! first)
8095 		putchar (' ');
8096 	      fputs (flags[cnt].str, stdout);
8097 	      first = 0;
8098 	      val ^= flags[cnt].bit;
8099 	    }
8100 
8101 	if (val != 0 || first)
8102 	  {
8103 	    if (! first)
8104 	      putchar (' ');
8105 	    print_vma (val, HEX);
8106 	  }
8107       }
8108       break;
8109 
8110     default:
8111       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8112       break;
8113     }
8114   putchar ('\n');
8115 }
8116 
8117 #ifdef BFD64
8118 
8119 /* VMS vs Unix time offset and factor.  */
8120 
8121 #define VMS_EPOCH_OFFSET 35067168000000000LL
8122 #define VMS_GRANULARITY_FACTOR 10000000
8123 
8124 /* Display a VMS time in a human readable format.  */
8125 
8126 static void
print_vms_time(bfd_int64_t vmstime)8127 print_vms_time (bfd_int64_t vmstime)
8128 {
8129   struct tm *tm;
8130   time_t unxtime;
8131 
8132   unxtime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
8133   tm = gmtime (&unxtime);
8134   printf ("%04u-%02u-%02uT%02u:%02u:%02u",
8135           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
8136           tm->tm_hour, tm->tm_min, tm->tm_sec);
8137 }
8138 #endif /* BFD64 */
8139 
8140 static void
dynamic_section_ia64_val(Elf_Internal_Dyn * entry)8141 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
8142 {
8143   switch (entry->d_tag)
8144     {
8145     case DT_IA_64_PLT_RESERVE:
8146       /* First 3 slots reserved.  */
8147       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8148       printf (" -- ");
8149       print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
8150       break;
8151 
8152     case DT_IA_64_VMS_LINKTIME:
8153 #ifdef BFD64
8154       print_vms_time (entry->d_un.d_val);
8155 #endif
8156       break;
8157 
8158     case DT_IA_64_VMS_LNKFLAGS:
8159       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8160       if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
8161         printf (" CALL_DEBUG");
8162       if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
8163         printf (" NOP0BUFS");
8164       if (entry->d_un.d_val & VMS_LF_P0IMAGE)
8165         printf (" P0IMAGE");
8166       if (entry->d_un.d_val & VMS_LF_MKTHREADS)
8167         printf (" MKTHREADS");
8168       if (entry->d_un.d_val & VMS_LF_UPCALLS)
8169         printf (" UPCALLS");
8170       if (entry->d_un.d_val & VMS_LF_IMGSTA)
8171         printf (" IMGSTA");
8172       if (entry->d_un.d_val & VMS_LF_INITIALIZE)
8173         printf (" INITIALIZE");
8174       if (entry->d_un.d_val & VMS_LF_MAIN)
8175         printf (" MAIN");
8176       if (entry->d_un.d_val & VMS_LF_EXE_INIT)
8177         printf (" EXE_INIT");
8178       if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
8179         printf (" TBK_IN_IMG");
8180       if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
8181         printf (" DBG_IN_IMG");
8182       if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
8183         printf (" TBK_IN_DSF");
8184       if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
8185         printf (" DBG_IN_DSF");
8186       if (entry->d_un.d_val & VMS_LF_SIGNATURES)
8187         printf (" SIGNATURES");
8188       if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
8189         printf (" REL_SEG_OFF");
8190       break;
8191 
8192     default:
8193       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8194       break;
8195     }
8196   putchar ('\n');
8197 }
8198 
8199 static int
get_32bit_dynamic_section(FILE * file)8200 get_32bit_dynamic_section (FILE * file)
8201 {
8202   Elf32_External_Dyn * edyn;
8203   Elf32_External_Dyn * ext;
8204   Elf_Internal_Dyn * entry;
8205 
8206   edyn = (Elf32_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
8207                                           dynamic_size, _("dynamic section"));
8208   if (!edyn)
8209     return 0;
8210 
8211   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
8212      might not have the luxury of section headers.  Look for the DT_NULL
8213      terminator to determine the number of entries.  */
8214   for (ext = edyn, dynamic_nent = 0;
8215        (char *) ext < (char *) edyn + dynamic_size - sizeof (* entry);
8216        ext++)
8217     {
8218       dynamic_nent++;
8219       if (BYTE_GET (ext->d_tag) == DT_NULL)
8220 	break;
8221     }
8222 
8223   dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
8224                                                   sizeof (* entry));
8225   if (dynamic_section == NULL)
8226     {
8227       error (_("Out of memory allocating space for %lu dynamic entries\n"),
8228 	     (unsigned long) dynamic_nent);
8229       free (edyn);
8230       return 0;
8231     }
8232 
8233   for (ext = edyn, entry = dynamic_section;
8234        entry < dynamic_section + dynamic_nent;
8235        ext++, entry++)
8236     {
8237       entry->d_tag      = BYTE_GET (ext->d_tag);
8238       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
8239     }
8240 
8241   free (edyn);
8242 
8243   return 1;
8244 }
8245 
8246 static int
get_64bit_dynamic_section(FILE * file)8247 get_64bit_dynamic_section (FILE * file)
8248 {
8249   Elf64_External_Dyn * edyn;
8250   Elf64_External_Dyn * ext;
8251   Elf_Internal_Dyn * entry;
8252 
8253   /* Read in the data.  */
8254   edyn = (Elf64_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
8255                                           dynamic_size, _("dynamic section"));
8256   if (!edyn)
8257     return 0;
8258 
8259   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
8260      might not have the luxury of section headers.  Look for the DT_NULL
8261      terminator to determine the number of entries.  */
8262   for (ext = edyn, dynamic_nent = 0;
8263        /* PR 17533 file: 033-67080-0.004 - do not read off the end of the buffer.  */
8264        (char *) ext < ((char *) edyn) + dynamic_size - sizeof (* ext);
8265        ext++)
8266     {
8267       dynamic_nent++;
8268       if (BYTE_GET (ext->d_tag) == DT_NULL)
8269 	break;
8270     }
8271 
8272   dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
8273                                                   sizeof (* entry));
8274   if (dynamic_section == NULL)
8275     {
8276       error (_("Out of memory allocating space for %lu dynamic entries\n"),
8277 	     (unsigned long) dynamic_nent);
8278       free (edyn);
8279       return 0;
8280     }
8281 
8282   /* Convert from external to internal formats.  */
8283   for (ext = edyn, entry = dynamic_section;
8284        entry < dynamic_section + dynamic_nent;
8285        ext++, entry++)
8286     {
8287       entry->d_tag      = BYTE_GET (ext->d_tag);
8288       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
8289     }
8290 
8291   free (edyn);
8292 
8293   return 1;
8294 }
8295 
8296 static void
print_dynamic_flags(bfd_vma flags)8297 print_dynamic_flags (bfd_vma flags)
8298 {
8299   int first = 1;
8300 
8301   while (flags)
8302     {
8303       bfd_vma flag;
8304 
8305       flag = flags & - flags;
8306       flags &= ~ flag;
8307 
8308       if (first)
8309 	first = 0;
8310       else
8311 	putc (' ', stdout);
8312 
8313       switch (flag)
8314 	{
8315 	case DF_ORIGIN:		fputs ("ORIGIN", stdout); break;
8316 	case DF_SYMBOLIC:	fputs ("SYMBOLIC", stdout); break;
8317 	case DF_TEXTREL:	fputs ("TEXTREL", stdout); break;
8318 	case DF_BIND_NOW:	fputs ("BIND_NOW", stdout); break;
8319 	case DF_STATIC_TLS:	fputs ("STATIC_TLS", stdout); break;
8320 	default:		fputs (_("unknown"), stdout); break;
8321 	}
8322     }
8323   puts ("");
8324 }
8325 
8326 /* Parse and display the contents of the dynamic section.  */
8327 
8328 static int
process_dynamic_section(FILE * file)8329 process_dynamic_section (FILE * file)
8330 {
8331   Elf_Internal_Dyn * entry;
8332 
8333   if (dynamic_size == 0)
8334     {
8335       if (do_dynamic)
8336 	printf (_("\nThere is no dynamic section in this file.\n"));
8337 
8338       return 1;
8339     }
8340 
8341   if (is_32bit_elf)
8342     {
8343       if (! get_32bit_dynamic_section (file))
8344 	return 0;
8345     }
8346   else if (! get_64bit_dynamic_section (file))
8347     return 0;
8348 
8349   /* Find the appropriate symbol table.  */
8350   if (dynamic_symbols == NULL)
8351     {
8352       for (entry = dynamic_section;
8353 	   entry < dynamic_section + dynamic_nent;
8354 	   ++entry)
8355 	{
8356 	  Elf_Internal_Shdr section;
8357 
8358 	  if (entry->d_tag != DT_SYMTAB)
8359 	    continue;
8360 
8361 	  dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
8362 
8363 	  /* Since we do not know how big the symbol table is,
8364 	     we default to reading in the entire file (!) and
8365 	     processing that.  This is overkill, I know, but it
8366 	     should work.  */
8367 	  section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
8368 
8369 	  if (archive_file_offset != 0)
8370 	    section.sh_size = archive_file_size - section.sh_offset;
8371 	  else
8372 	    {
8373 	      if (fseek (file, 0, SEEK_END))
8374 		error (_("Unable to seek to end of file!\n"));
8375 
8376 	      section.sh_size = ftell (file) - section.sh_offset;
8377 	    }
8378 
8379 	  if (is_32bit_elf)
8380 	    section.sh_entsize = sizeof (Elf32_External_Sym);
8381 	  else
8382 	    section.sh_entsize = sizeof (Elf64_External_Sym);
8383 	  section.sh_name = string_table_length;
8384 
8385 	  dynamic_symbols = GET_ELF_SYMBOLS (file, &section, & num_dynamic_syms);
8386 	  if (num_dynamic_syms < 1)
8387 	    {
8388 	      error (_("Unable to determine the number of symbols to load\n"));
8389 	      continue;
8390 	    }
8391 	}
8392     }
8393 
8394   /* Similarly find a string table.  */
8395   if (dynamic_strings == NULL)
8396     {
8397       for (entry = dynamic_section;
8398 	   entry < dynamic_section + dynamic_nent;
8399 	   ++entry)
8400 	{
8401 	  unsigned long offset;
8402 	  long str_tab_len;
8403 
8404 	  if (entry->d_tag != DT_STRTAB)
8405 	    continue;
8406 
8407 	  dynamic_info[DT_STRTAB] = entry->d_un.d_val;
8408 
8409 	  /* Since we do not know how big the string table is,
8410 	     we default to reading in the entire file (!) and
8411 	     processing that.  This is overkill, I know, but it
8412 	     should work.  */
8413 
8414 	  offset = offset_from_vma (file, entry->d_un.d_val, 0);
8415 
8416 	  if (archive_file_offset != 0)
8417 	    str_tab_len = archive_file_size - offset;
8418 	  else
8419 	    {
8420 	      if (fseek (file, 0, SEEK_END))
8421 		error (_("Unable to seek to end of file\n"));
8422 	      str_tab_len = ftell (file) - offset;
8423 	    }
8424 
8425 	  if (str_tab_len < 1)
8426 	    {
8427 	      error
8428 		(_("Unable to determine the length of the dynamic string table\n"));
8429 	      continue;
8430 	    }
8431 
8432 	  dynamic_strings = (char *) get_data (NULL, file, offset, 1,
8433                                                str_tab_len,
8434                                                _("dynamic string table"));
8435 	  dynamic_strings_length = dynamic_strings == NULL ? 0 : str_tab_len;
8436 	  break;
8437 	}
8438     }
8439 
8440   /* And find the syminfo section if available.  */
8441   if (dynamic_syminfo == NULL)
8442     {
8443       unsigned long syminsz = 0;
8444 
8445       for (entry = dynamic_section;
8446 	   entry < dynamic_section + dynamic_nent;
8447 	   ++entry)
8448 	{
8449 	  if (entry->d_tag == DT_SYMINENT)
8450 	    {
8451 	      /* Note: these braces are necessary to avoid a syntax
8452 		 error from the SunOS4 C compiler.  */
8453 	      /* PR binutils/17531: A corrupt file can trigger this test.
8454 		 So do not use an assert, instead generate an error message.  */
8455 	      if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
8456 		error (_("Bad value (%d) for SYMINENT entry\n"),
8457 		       (int) entry->d_un.d_val);
8458 	    }
8459 	  else if (entry->d_tag == DT_SYMINSZ)
8460 	    syminsz = entry->d_un.d_val;
8461 	  else if (entry->d_tag == DT_SYMINFO)
8462 	    dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
8463 						      syminsz);
8464 	}
8465 
8466       if (dynamic_syminfo_offset != 0 && syminsz != 0)
8467 	{
8468 	  Elf_External_Syminfo * extsyminfo;
8469 	  Elf_External_Syminfo * extsym;
8470 	  Elf_Internal_Syminfo * syminfo;
8471 
8472 	  /* There is a syminfo section.  Read the data.  */
8473 	  extsyminfo = (Elf_External_Syminfo *)
8474               get_data (NULL, file, dynamic_syminfo_offset, 1, syminsz,
8475                         _("symbol information"));
8476 	  if (!extsyminfo)
8477 	    return 0;
8478 
8479 	  dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
8480 	  if (dynamic_syminfo == NULL)
8481 	    {
8482 	      error (_("Out of memory allocating %lu byte for dynamic symbol info\n"),
8483 		     (unsigned long) syminsz);
8484 	      return 0;
8485 	    }
8486 
8487 	  dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
8488 	  for (syminfo = dynamic_syminfo, extsym = extsyminfo;
8489 	       syminfo < dynamic_syminfo + dynamic_syminfo_nent;
8490 	       ++syminfo, ++extsym)
8491 	    {
8492 	      syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
8493 	      syminfo->si_flags = BYTE_GET (extsym->si_flags);
8494 	    }
8495 
8496 	  free (extsyminfo);
8497 	}
8498     }
8499 
8500   if (do_dynamic && dynamic_addr)
8501     printf (_("\nDynamic section at offset 0x%lx contains %lu entries:\n"),
8502 	    dynamic_addr, (unsigned long) dynamic_nent);
8503   if (do_dynamic)
8504     printf (_("  Tag        Type                         Name/Value\n"));
8505 
8506   for (entry = dynamic_section;
8507        entry < dynamic_section + dynamic_nent;
8508        entry++)
8509     {
8510       if (do_dynamic)
8511 	{
8512 	  const char * dtype;
8513 
8514 	  putchar (' ');
8515 	  print_vma (entry->d_tag, FULL_HEX);
8516 	  dtype = get_dynamic_type (entry->d_tag);
8517 	  printf (" (%s)%*s", dtype,
8518 		  ((is_32bit_elf ? 27 : 19)
8519 		   - (int) strlen (dtype)),
8520 		  " ");
8521 	}
8522 
8523       switch (entry->d_tag)
8524 	{
8525 	case DT_FLAGS:
8526 	  if (do_dynamic)
8527 	    print_dynamic_flags (entry->d_un.d_val);
8528 	  break;
8529 
8530 	case DT_AUXILIARY:
8531 	case DT_FILTER:
8532 	case DT_CONFIG:
8533 	case DT_DEPAUDIT:
8534 	case DT_AUDIT:
8535 	  if (do_dynamic)
8536 	    {
8537 	      switch (entry->d_tag)
8538 		{
8539 		case DT_AUXILIARY:
8540 		  printf (_("Auxiliary library"));
8541 		  break;
8542 
8543 		case DT_FILTER:
8544 		  printf (_("Filter library"));
8545 		  break;
8546 
8547 		case DT_CONFIG:
8548 		  printf (_("Configuration file"));
8549 		  break;
8550 
8551 		case DT_DEPAUDIT:
8552 		  printf (_("Dependency audit library"));
8553 		  break;
8554 
8555 		case DT_AUDIT:
8556 		  printf (_("Audit library"));
8557 		  break;
8558 		}
8559 
8560 	      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
8561 		printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
8562 	      else
8563 		{
8564 		  printf (": ");
8565 		  print_vma (entry->d_un.d_val, PREFIX_HEX);
8566 		  putchar ('\n');
8567 		}
8568 	    }
8569 	  break;
8570 
8571 	case DT_FEATURE:
8572 	  if (do_dynamic)
8573 	    {
8574 	      printf (_("Flags:"));
8575 
8576 	      if (entry->d_un.d_val == 0)
8577 		printf (_(" None\n"));
8578 	      else
8579 		{
8580 		  unsigned long int val = entry->d_un.d_val;
8581 
8582 		  if (val & DTF_1_PARINIT)
8583 		    {
8584 		      printf (" PARINIT");
8585 		      val ^= DTF_1_PARINIT;
8586 		    }
8587 		  if (val & DTF_1_CONFEXP)
8588 		    {
8589 		      printf (" CONFEXP");
8590 		      val ^= DTF_1_CONFEXP;
8591 		    }
8592 		  if (val != 0)
8593 		    printf (" %lx", val);
8594 		  puts ("");
8595 		}
8596 	    }
8597 	  break;
8598 
8599 	case DT_POSFLAG_1:
8600 	  if (do_dynamic)
8601 	    {
8602 	      printf (_("Flags:"));
8603 
8604 	      if (entry->d_un.d_val == 0)
8605 		printf (_(" None\n"));
8606 	      else
8607 		{
8608 		  unsigned long int val = entry->d_un.d_val;
8609 
8610 		  if (val & DF_P1_LAZYLOAD)
8611 		    {
8612 		      printf (" LAZYLOAD");
8613 		      val ^= DF_P1_LAZYLOAD;
8614 		    }
8615 		  if (val & DF_P1_GROUPPERM)
8616 		    {
8617 		      printf (" GROUPPERM");
8618 		      val ^= DF_P1_GROUPPERM;
8619 		    }
8620 		  if (val != 0)
8621 		    printf (" %lx", val);
8622 		  puts ("");
8623 		}
8624 	    }
8625 	  break;
8626 
8627 	case DT_FLAGS_1:
8628 	  if (do_dynamic)
8629 	    {
8630 	      printf (_("Flags:"));
8631 	      if (entry->d_un.d_val == 0)
8632 		printf (_(" None\n"));
8633 	      else
8634 		{
8635 		  unsigned long int val = entry->d_un.d_val;
8636 
8637 		  if (val & DF_1_NOW)
8638 		    {
8639 		      printf (" NOW");
8640 		      val ^= DF_1_NOW;
8641 		    }
8642 		  if (val & DF_1_GLOBAL)
8643 		    {
8644 		      printf (" GLOBAL");
8645 		      val ^= DF_1_GLOBAL;
8646 		    }
8647 		  if (val & DF_1_GROUP)
8648 		    {
8649 		      printf (" GROUP");
8650 		      val ^= DF_1_GROUP;
8651 		    }
8652 		  if (val & DF_1_NODELETE)
8653 		    {
8654 		      printf (" NODELETE");
8655 		      val ^= DF_1_NODELETE;
8656 		    }
8657 		  if (val & DF_1_LOADFLTR)
8658 		    {
8659 		      printf (" LOADFLTR");
8660 		      val ^= DF_1_LOADFLTR;
8661 		    }
8662 		  if (val & DF_1_INITFIRST)
8663 		    {
8664 		      printf (" INITFIRST");
8665 		      val ^= DF_1_INITFIRST;
8666 		    }
8667 		  if (val & DF_1_NOOPEN)
8668 		    {
8669 		      printf (" NOOPEN");
8670 		      val ^= DF_1_NOOPEN;
8671 		    }
8672 		  if (val & DF_1_ORIGIN)
8673 		    {
8674 		      printf (" ORIGIN");
8675 		      val ^= DF_1_ORIGIN;
8676 		    }
8677 		  if (val & DF_1_DIRECT)
8678 		    {
8679 		      printf (" DIRECT");
8680 		      val ^= DF_1_DIRECT;
8681 		    }
8682 		  if (val & DF_1_TRANS)
8683 		    {
8684 		      printf (" TRANS");
8685 		      val ^= DF_1_TRANS;
8686 		    }
8687 		  if (val & DF_1_INTERPOSE)
8688 		    {
8689 		      printf (" INTERPOSE");
8690 		      val ^= DF_1_INTERPOSE;
8691 		    }
8692 		  if (val & DF_1_NODEFLIB)
8693 		    {
8694 		      printf (" NODEFLIB");
8695 		      val ^= DF_1_NODEFLIB;
8696 		    }
8697 		  if (val & DF_1_NODUMP)
8698 		    {
8699 		      printf (" NODUMP");
8700 		      val ^= DF_1_NODUMP;
8701 		    }
8702 		  if (val & DF_1_CONFALT)
8703 		    {
8704 		      printf (" CONFALT");
8705 		      val ^= DF_1_CONFALT;
8706 		    }
8707 		  if (val & DF_1_ENDFILTEE)
8708 		    {
8709 		      printf (" ENDFILTEE");
8710 		      val ^= DF_1_ENDFILTEE;
8711 		    }
8712 		  if (val & DF_1_DISPRELDNE)
8713 		    {
8714 		      printf (" DISPRELDNE");
8715 		      val ^= DF_1_DISPRELDNE;
8716 		    }
8717 		  if (val & DF_1_DISPRELPND)
8718 		    {
8719 		      printf (" DISPRELPND");
8720 		      val ^= DF_1_DISPRELPND;
8721 		    }
8722 		  if (val & DF_1_NODIRECT)
8723 		    {
8724 		      printf (" NODIRECT");
8725 		      val ^= DF_1_NODIRECT;
8726 		    }
8727 		  if (val & DF_1_IGNMULDEF)
8728 		    {
8729 		      printf (" IGNMULDEF");
8730 		      val ^= DF_1_IGNMULDEF;
8731 		    }
8732 		  if (val & DF_1_NOKSYMS)
8733 		    {
8734 		      printf (" NOKSYMS");
8735 		      val ^= DF_1_NOKSYMS;
8736 		    }
8737 		  if (val & DF_1_NOHDR)
8738 		    {
8739 		      printf (" NOHDR");
8740 		      val ^= DF_1_NOHDR;
8741 		    }
8742 		  if (val & DF_1_EDITED)
8743 		    {
8744 		      printf (" EDITED");
8745 		      val ^= DF_1_EDITED;
8746 		    }
8747 		  if (val & DF_1_NORELOC)
8748 		    {
8749 		      printf (" NORELOC");
8750 		      val ^= DF_1_NORELOC;
8751 		    }
8752 		  if (val & DF_1_SYMINTPOSE)
8753 		    {
8754 		      printf (" SYMINTPOSE");
8755 		      val ^= DF_1_SYMINTPOSE;
8756 		    }
8757 		  if (val & DF_1_GLOBAUDIT)
8758 		    {
8759 		      printf (" GLOBAUDIT");
8760 		      val ^= DF_1_GLOBAUDIT;
8761 		    }
8762 		  if (val & DF_1_SINGLETON)
8763 		    {
8764 		      printf (" SINGLETON");
8765 		      val ^= DF_1_SINGLETON;
8766 		    }
8767 		  if (val != 0)
8768 		    printf (" %lx", val);
8769 		  puts ("");
8770 		}
8771 	    }
8772 	  break;
8773 
8774 	case DT_PLTREL:
8775 	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
8776 	  if (do_dynamic)
8777 	    puts (get_dynamic_type (entry->d_un.d_val));
8778 	  break;
8779 
8780 	case DT_NULL	:
8781 	case DT_NEEDED	:
8782 	case DT_PLTGOT	:
8783 	case DT_HASH	:
8784 	case DT_STRTAB	:
8785 	case DT_SYMTAB	:
8786 	case DT_RELA	:
8787 	case DT_INIT	:
8788 	case DT_FINI	:
8789 	case DT_SONAME	:
8790 	case DT_RPATH	:
8791 	case DT_SYMBOLIC:
8792 	case DT_REL	:
8793 	case DT_DEBUG	:
8794 	case DT_TEXTREL	:
8795 	case DT_JMPREL	:
8796 	case DT_RUNPATH	:
8797 	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
8798 
8799 	  if (do_dynamic)
8800 	    {
8801 	      char * name;
8802 
8803 	      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
8804 		name = GET_DYNAMIC_NAME (entry->d_un.d_val);
8805 	      else
8806 		name = NULL;
8807 
8808 	      if (name)
8809 		{
8810 		  switch (entry->d_tag)
8811 		    {
8812 		    case DT_NEEDED:
8813 		      printf (_("Shared library: [%s]"), name);
8814 
8815 		      if (streq (name, program_interpreter))
8816 			printf (_(" program interpreter"));
8817 		      break;
8818 
8819 		    case DT_SONAME:
8820 		      printf (_("Library soname: [%s]"), name);
8821 		      break;
8822 
8823 		    case DT_RPATH:
8824 		      printf (_("Library rpath: [%s]"), name);
8825 		      break;
8826 
8827 		    case DT_RUNPATH:
8828 		      printf (_("Library runpath: [%s]"), name);
8829 		      break;
8830 
8831 		    default:
8832 		      print_vma (entry->d_un.d_val, PREFIX_HEX);
8833 		      break;
8834 		    }
8835 		}
8836 	      else
8837 		print_vma (entry->d_un.d_val, PREFIX_HEX);
8838 
8839 	      putchar ('\n');
8840 	    }
8841 	  break;
8842 
8843 	case DT_PLTRELSZ:
8844 	case DT_RELASZ	:
8845 	case DT_STRSZ	:
8846 	case DT_RELSZ	:
8847 	case DT_RELAENT	:
8848 	case DT_SYMENT	:
8849 	case DT_RELENT	:
8850 	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
8851 	case DT_PLTPADSZ:
8852 	case DT_MOVEENT	:
8853 	case DT_MOVESZ	:
8854 	case DT_INIT_ARRAYSZ:
8855 	case DT_FINI_ARRAYSZ:
8856 	case DT_GNU_CONFLICTSZ:
8857 	case DT_GNU_LIBLISTSZ:
8858 	  if (do_dynamic)
8859 	    {
8860 	      print_vma (entry->d_un.d_val, UNSIGNED);
8861 	      printf (_(" (bytes)\n"));
8862 	    }
8863 	  break;
8864 
8865 	case DT_VERDEFNUM:
8866 	case DT_VERNEEDNUM:
8867 	case DT_RELACOUNT:
8868 	case DT_RELCOUNT:
8869 	  if (do_dynamic)
8870 	    {
8871 	      print_vma (entry->d_un.d_val, UNSIGNED);
8872 	      putchar ('\n');
8873 	    }
8874 	  break;
8875 
8876 	case DT_SYMINSZ:
8877 	case DT_SYMINENT:
8878 	case DT_SYMINFO:
8879 	case DT_USED:
8880 	case DT_INIT_ARRAY:
8881 	case DT_FINI_ARRAY:
8882 	  if (do_dynamic)
8883 	    {
8884 	      if (entry->d_tag == DT_USED
8885 		  && VALID_DYNAMIC_NAME (entry->d_un.d_val))
8886 		{
8887 		  char * name = GET_DYNAMIC_NAME (entry->d_un.d_val);
8888 
8889 		  if (*name)
8890 		    {
8891 		      printf (_("Not needed object: [%s]\n"), name);
8892 		      break;
8893 		    }
8894 		}
8895 
8896 	      print_vma (entry->d_un.d_val, PREFIX_HEX);
8897 	      putchar ('\n');
8898 	    }
8899 	  break;
8900 
8901 	case DT_BIND_NOW:
8902 	  /* The value of this entry is ignored.  */
8903 	  if (do_dynamic)
8904 	    putchar ('\n');
8905 	  break;
8906 
8907 	case DT_GNU_PRELINKED:
8908 	  if (do_dynamic)
8909 	    {
8910 	      struct tm * tmp;
8911 	      time_t atime = entry->d_un.d_val;
8912 
8913 	      tmp = gmtime (&atime);
8914 	      /* PR 17533 file: 041-1244816-0.004.  */
8915 	      if (tmp == NULL)
8916 		printf (_("<corrupt time val: %lx"),
8917 			(unsigned long) atime);
8918 	      else
8919 		printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
8920 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
8921 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
8922 
8923 	    }
8924 	  break;
8925 
8926 	case DT_GNU_HASH:
8927 	  dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
8928 	  if (do_dynamic)
8929 	    {
8930 	      print_vma (entry->d_un.d_val, PREFIX_HEX);
8931 	      putchar ('\n');
8932 	    }
8933 	  break;
8934 
8935 	default:
8936 	  if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
8937 	    version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
8938 	      entry->d_un.d_val;
8939 
8940 	  if (do_dynamic)
8941 	    {
8942 	      switch (elf_header.e_machine)
8943 		{
8944 		case EM_MIPS:
8945 		case EM_MIPS_RS3_LE:
8946 		  dynamic_section_mips_val (entry);
8947 		  break;
8948 		case EM_PARISC:
8949 		  dynamic_section_parisc_val (entry);
8950 		  break;
8951 		case EM_IA_64:
8952 		  dynamic_section_ia64_val (entry);
8953 		  break;
8954 		default:
8955 		  print_vma (entry->d_un.d_val, PREFIX_HEX);
8956 		  putchar ('\n');
8957 		}
8958 	    }
8959 	  break;
8960 	}
8961     }
8962 
8963   return 1;
8964 }
8965 
8966 static char *
get_ver_flags(unsigned int flags)8967 get_ver_flags (unsigned int flags)
8968 {
8969   static char buff[32];
8970 
8971   buff[0] = 0;
8972 
8973   if (flags == 0)
8974     return _("none");
8975 
8976   if (flags & VER_FLG_BASE)
8977     strcat (buff, "BASE ");
8978 
8979   if (flags & VER_FLG_WEAK)
8980     {
8981       if (flags & VER_FLG_BASE)
8982 	strcat (buff, "| ");
8983 
8984       strcat (buff, "WEAK ");
8985     }
8986 
8987   if (flags & VER_FLG_INFO)
8988     {
8989       if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
8990 	strcat (buff, "| ");
8991 
8992       strcat (buff, "INFO ");
8993     }
8994 
8995   if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
8996     strcat (buff, _("| <unknown>"));
8997 
8998   return buff;
8999 }
9000 
9001 /* Display the contents of the version sections.  */
9002 
9003 static int
process_version_sections(FILE * file)9004 process_version_sections (FILE * file)
9005 {
9006   Elf_Internal_Shdr * section;
9007   unsigned i;
9008   int found = 0;
9009 
9010   if (! do_version)
9011     return 1;
9012 
9013   for (i = 0, section = section_headers;
9014        i < elf_header.e_shnum;
9015        i++, section++)
9016     {
9017       switch (section->sh_type)
9018 	{
9019 	case SHT_GNU_verdef:
9020 	  {
9021 	    Elf_External_Verdef * edefs;
9022 	    unsigned int idx;
9023 	    unsigned int cnt;
9024 	    char * endbuf;
9025 
9026 	    found = 1;
9027 
9028 	    printf (_("\nVersion definition section '%s' contains %u entries:\n"),
9029 		    printable_section_name (section),
9030 		    section->sh_info);
9031 
9032 	    printf (_("  Addr: 0x"));
9033 	    printf_vma (section->sh_addr);
9034 	    printf (_("  Offset: %#08lx  Link: %u (%s)"),
9035 		    (unsigned long) section->sh_offset, section->sh_link,
9036 		    printable_section_name_from_index (section->sh_link));
9037 
9038 	    edefs = (Elf_External_Verdef *)
9039                 get_data (NULL, file, section->sh_offset, 1,section->sh_size,
9040                           _("version definition section"));
9041 	    if (!edefs)
9042 	      break;
9043 	    endbuf = (char *) edefs + section->sh_size;
9044 
9045 	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
9046 	      {
9047 		char * vstart;
9048 		Elf_External_Verdef * edef;
9049 		Elf_Internal_Verdef ent;
9050 		Elf_External_Verdaux * eaux;
9051 		Elf_Internal_Verdaux aux;
9052 		int j;
9053 		int isum;
9054 
9055 		/* Check for very large indicies.  */
9056 		if (idx > (size_t) (endbuf - (char *) edefs))
9057 		  break;
9058 
9059 		vstart = ((char *) edefs) + idx;
9060 		if (vstart + sizeof (*edef) > endbuf)
9061 		  break;
9062 
9063 		edef = (Elf_External_Verdef *) vstart;
9064 
9065 		ent.vd_version = BYTE_GET (edef->vd_version);
9066 		ent.vd_flags   = BYTE_GET (edef->vd_flags);
9067 		ent.vd_ndx     = BYTE_GET (edef->vd_ndx);
9068 		ent.vd_cnt     = BYTE_GET (edef->vd_cnt);
9069 		ent.vd_hash    = BYTE_GET (edef->vd_hash);
9070 		ent.vd_aux     = BYTE_GET (edef->vd_aux);
9071 		ent.vd_next    = BYTE_GET (edef->vd_next);
9072 
9073 		printf (_("  %#06x: Rev: %d  Flags: %s"),
9074 			idx, ent.vd_version, get_ver_flags (ent.vd_flags));
9075 
9076 		printf (_("  Index: %d  Cnt: %d  "),
9077 			ent.vd_ndx, ent.vd_cnt);
9078 
9079 		/* Check for overflow.  */
9080 		if (ent.vd_aux > (size_t) (endbuf - vstart))
9081 		  break;
9082 
9083 		vstart += ent.vd_aux;
9084 
9085 		eaux = (Elf_External_Verdaux *) vstart;
9086 
9087 		aux.vda_name = BYTE_GET (eaux->vda_name);
9088 		aux.vda_next = BYTE_GET (eaux->vda_next);
9089 
9090 		if (VALID_DYNAMIC_NAME (aux.vda_name))
9091 		  printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
9092 		else
9093 		  printf (_("Name index: %ld\n"), aux.vda_name);
9094 
9095 		isum = idx + ent.vd_aux;
9096 
9097 		for (j = 1; j < ent.vd_cnt; j++)
9098 		  {
9099 		    /* Check for overflow.  */
9100 		    if (aux.vda_next > (size_t) (endbuf - vstart))
9101 		      break;
9102 
9103 		    isum   += aux.vda_next;
9104 		    vstart += aux.vda_next;
9105 
9106 		    eaux = (Elf_External_Verdaux *) vstart;
9107 		    if (vstart + sizeof (*eaux) > endbuf)
9108 		      break;
9109 
9110 		    aux.vda_name = BYTE_GET (eaux->vda_name);
9111 		    aux.vda_next = BYTE_GET (eaux->vda_next);
9112 
9113 		    if (VALID_DYNAMIC_NAME (aux.vda_name))
9114 		      printf (_("  %#06x: Parent %d: %s\n"),
9115 			      isum, j, GET_DYNAMIC_NAME (aux.vda_name));
9116 		    else
9117 		      printf (_("  %#06x: Parent %d, name index: %ld\n"),
9118 			      isum, j, aux.vda_name);
9119 		  }
9120 
9121 		if (j < ent.vd_cnt)
9122 		  printf (_("  Version def aux past end of section\n"));
9123 
9124 		idx += ent.vd_next;
9125 	      }
9126 
9127 	    if (cnt < section->sh_info)
9128 	      printf (_("  Version definition past end of section\n"));
9129 
9130 	    free (edefs);
9131 	  }
9132 	  break;
9133 
9134 	case SHT_GNU_verneed:
9135 	  {
9136 	    Elf_External_Verneed * eneed;
9137 	    unsigned int idx;
9138 	    unsigned int cnt;
9139 	    char * endbuf;
9140 
9141 	    found = 1;
9142 
9143 	    printf (_("\nVersion needs section '%s' contains %u entries:\n"),
9144 		    printable_section_name (section), section->sh_info);
9145 
9146 	    printf (_(" Addr: 0x"));
9147 	    printf_vma (section->sh_addr);
9148 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
9149 		    (unsigned long) section->sh_offset, section->sh_link,
9150 		    printable_section_name_from_index (section->sh_link));
9151 
9152 	    eneed = (Elf_External_Verneed *) get_data (NULL, file,
9153                                                        section->sh_offset, 1,
9154                                                        section->sh_size,
9155                                                        _("Version Needs section"));
9156 	    if (!eneed)
9157 	      break;
9158 	    endbuf = (char *) eneed + section->sh_size;
9159 
9160 	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
9161 	      {
9162 		Elf_External_Verneed * entry;
9163 		Elf_Internal_Verneed ent;
9164 		int j;
9165 		int isum;
9166 		char * vstart;
9167 
9168 		if (idx > (size_t) (endbuf - (char *) eneed))
9169 		  break;
9170 
9171 		vstart = ((char *) eneed) + idx;
9172 		if (vstart + sizeof (*entry) > endbuf)
9173 		  break;
9174 
9175 		entry = (Elf_External_Verneed *) vstart;
9176 
9177 		ent.vn_version = BYTE_GET (entry->vn_version);
9178 		ent.vn_cnt     = BYTE_GET (entry->vn_cnt);
9179 		ent.vn_file    = BYTE_GET (entry->vn_file);
9180 		ent.vn_aux     = BYTE_GET (entry->vn_aux);
9181 		ent.vn_next    = BYTE_GET (entry->vn_next);
9182 
9183 		printf (_("  %#06x: Version: %d"), idx, ent.vn_version);
9184 
9185 		if (VALID_DYNAMIC_NAME (ent.vn_file))
9186 		  printf (_("  File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
9187 		else
9188 		  printf (_("  File: %lx"), ent.vn_file);
9189 
9190 		printf (_("  Cnt: %d\n"), ent.vn_cnt);
9191 
9192 		/* Check for overflow.  */
9193 		if (ent.vn_aux > (size_t) (endbuf - vstart))
9194 		  break;
9195 
9196 		vstart += ent.vn_aux;
9197 
9198 		for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
9199 		  {
9200 		    Elf_External_Vernaux * eaux;
9201 		    Elf_Internal_Vernaux aux;
9202 
9203 		    if (vstart + sizeof (*eaux) > endbuf)
9204 		      break;
9205 		    eaux = (Elf_External_Vernaux *) vstart;
9206 
9207 		    aux.vna_hash  = BYTE_GET (eaux->vna_hash);
9208 		    aux.vna_flags = BYTE_GET (eaux->vna_flags);
9209 		    aux.vna_other = BYTE_GET (eaux->vna_other);
9210 		    aux.vna_name  = BYTE_GET (eaux->vna_name);
9211 		    aux.vna_next  = BYTE_GET (eaux->vna_next);
9212 
9213 		    if (VALID_DYNAMIC_NAME (aux.vna_name))
9214 		      printf (_("  %#06x:   Name: %s"),
9215 			      isum, GET_DYNAMIC_NAME (aux.vna_name));
9216 		    else
9217 		      printf (_("  %#06x:   Name index: %lx"),
9218 			      isum, aux.vna_name);
9219 
9220 		    printf (_("  Flags: %s  Version: %d\n"),
9221 			    get_ver_flags (aux.vna_flags), aux.vna_other);
9222 
9223 		    /* Check for overflow.  */
9224 		    if (aux.vna_next > (size_t) (endbuf - vstart))
9225 		      break;
9226 
9227 		    isum   += aux.vna_next;
9228 		    vstart += aux.vna_next;
9229 		  }
9230 
9231 		if (j < ent.vn_cnt)
9232 		  warn (_("Missing Version Needs auxillary information\n"));
9233 
9234 		if (ent.vn_next == 0 && cnt < section->sh_info - 1)
9235 		  {
9236 		    warn (_("Corrupt Version Needs structure - offset to next structure is zero with entries still left to be processed\n"));
9237 		    cnt = section->sh_info;
9238 		    break;
9239 		  }
9240 		idx += ent.vn_next;
9241 	      }
9242 
9243 	    if (cnt < section->sh_info)
9244 	      warn (_("Missing Version Needs information\n"));
9245 
9246 	    free (eneed);
9247 	  }
9248 	  break;
9249 
9250 	case SHT_GNU_versym:
9251 	  {
9252 	    Elf_Internal_Shdr * link_section;
9253 	    size_t total;
9254 	    unsigned int cnt;
9255 	    unsigned char * edata;
9256 	    unsigned short * data;
9257 	    char * strtab;
9258 	    Elf_Internal_Sym * symbols;
9259 	    Elf_Internal_Shdr * string_sec;
9260 	    unsigned long num_syms;
9261 	    long off;
9262 
9263 	    if (section->sh_link >= elf_header.e_shnum)
9264 	      break;
9265 
9266 	    link_section = section_headers + section->sh_link;
9267 	    total = section->sh_size / sizeof (Elf_External_Versym);
9268 
9269 	    if (link_section->sh_link >= elf_header.e_shnum)
9270 	      break;
9271 
9272 	    found = 1;
9273 
9274 	    symbols = GET_ELF_SYMBOLS (file, link_section, & num_syms);
9275 	    if (symbols == NULL)
9276 	      break;
9277 
9278 	    string_sec = section_headers + link_section->sh_link;
9279 
9280 	    strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
9281                                         string_sec->sh_size,
9282                                         _("version string table"));
9283 	    if (!strtab)
9284 	      {
9285 		free (symbols);
9286 		break;
9287 	      }
9288 
9289 	    printf (_("\nVersion symbols section '%s' contains %lu entries:\n"),
9290 		    printable_section_name (section), (unsigned long) total);
9291 
9292 	    printf (_(" Addr: "));
9293 	    printf_vma (section->sh_addr);
9294 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
9295 		    (unsigned long) section->sh_offset, section->sh_link,
9296 		    printable_section_name (link_section));
9297 
9298 	    off = offset_from_vma (file,
9299 				   version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
9300 				   total * sizeof (short));
9301 	    edata = (unsigned char *) get_data (NULL, file, off, total,
9302                                                 sizeof (short),
9303                                                 _("version symbol data"));
9304 	    if (!edata)
9305 	      {
9306 		free (strtab);
9307 		free (symbols);
9308 		break;
9309 	      }
9310 
9311 	    data = (short unsigned int *) cmalloc (total, sizeof (short));
9312 
9313 	    for (cnt = total; cnt --;)
9314 	      data[cnt] = byte_get (edata + cnt * sizeof (short),
9315 				    sizeof (short));
9316 
9317 	    free (edata);
9318 
9319 	    for (cnt = 0; cnt < total; cnt += 4)
9320 	      {
9321 		int j, nn;
9322 		int check_def, check_need;
9323 		char * name;
9324 
9325 		printf ("  %03x:", cnt);
9326 
9327 		for (j = 0; (j < 4) && (cnt + j) < total; ++j)
9328 		  switch (data[cnt + j])
9329 		    {
9330 		    case 0:
9331 		      fputs (_("   0 (*local*)    "), stdout);
9332 		      break;
9333 
9334 		    case 1:
9335 		      fputs (_("   1 (*global*)   "), stdout);
9336 		      break;
9337 
9338 		    default:
9339 		      nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
9340 				   data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
9341 
9342 		      /* If this index value is greater than the size of the symbols
9343 		         array, break to avoid an out-of-bounds read.  */
9344 		      if ((unsigned long)(cnt + j) >= num_syms)
9345 		        {
9346 		          warn (_("invalid index into symbol array\n"));
9347 		          break;
9348 			}
9349 
9350 		      check_def = 1;
9351 		      check_need = 1;
9352 		      if (symbols[cnt + j].st_shndx >= elf_header.e_shnum
9353 			  || section_headers[symbols[cnt + j].st_shndx].sh_type
9354 			     != SHT_NOBITS)
9355 			{
9356 			  if (symbols[cnt + j].st_shndx == SHN_UNDEF)
9357 			    check_def = 0;
9358 			  else
9359 			    check_need = 0;
9360 			}
9361 
9362 		      if (check_need
9363 			  && version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
9364 			{
9365 			  Elf_Internal_Verneed ivn;
9366 			  unsigned long offset;
9367 
9368 			  offset = offset_from_vma
9369 			    (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
9370 			     sizeof (Elf_External_Verneed));
9371 
9372 			  do
9373 			    {
9374 			      Elf_Internal_Vernaux ivna;
9375 			      Elf_External_Verneed evn;
9376 			      Elf_External_Vernaux evna;
9377 			      unsigned long a_off;
9378 
9379 			      if (get_data (&evn, file, offset, sizeof (evn), 1,
9380 					    _("version need")) == NULL)
9381 				break;
9382 
9383 			      ivn.vn_aux  = BYTE_GET (evn.vn_aux);
9384 			      ivn.vn_next = BYTE_GET (evn.vn_next);
9385 
9386 			      a_off = offset + ivn.vn_aux;
9387 
9388 			      do
9389 				{
9390 				  if (get_data (&evna, file, a_off, sizeof (evna),
9391 						1, _("version need aux (2)")) == NULL)
9392 				    {
9393 				      ivna.vna_next  = 0;
9394 				      ivna.vna_other = 0;
9395 				    }
9396 				  else
9397 				    {
9398 				      ivna.vna_next  = BYTE_GET (evna.vna_next);
9399 				      ivna.vna_other = BYTE_GET (evna.vna_other);
9400 				    }
9401 
9402 				  a_off += ivna.vna_next;
9403 				}
9404 			      while (ivna.vna_other != data[cnt + j]
9405 				     && ivna.vna_next != 0);
9406 
9407 			      if (ivna.vna_other == data[cnt + j])
9408 				{
9409 				  ivna.vna_name = BYTE_GET (evna.vna_name);
9410 
9411 				  if (ivna.vna_name >= string_sec->sh_size)
9412 				    name = _("*invalid*");
9413 				  else
9414 				    name = strtab + ivna.vna_name;
9415 				  nn += printf ("(%s%-*s",
9416 						name,
9417 						12 - (int) strlen (name),
9418 						")");
9419 				  check_def = 0;
9420 				  break;
9421 				}
9422 
9423 			      offset += ivn.vn_next;
9424 			    }
9425 			  while (ivn.vn_next);
9426 			}
9427 
9428 		      if (check_def && data[cnt + j] != 0x8001
9429 			  && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
9430 			{
9431 			  Elf_Internal_Verdef ivd;
9432 			  Elf_External_Verdef evd;
9433 			  unsigned long offset;
9434 
9435 			  offset = offset_from_vma
9436 			    (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
9437 			     sizeof evd);
9438 
9439 			  do
9440 			    {
9441 			      if (get_data (&evd, file, offset, sizeof (evd), 1,
9442 					    _("version def")) == NULL)
9443 				{
9444 				  ivd.vd_next = 0;
9445 				  /* PR 17531: file: 046-1082287-0.004.  */
9446 				  ivd.vd_ndx  = (data[cnt + j] & VERSYM_VERSION) + 1;
9447 				  break;
9448 				}
9449 			      else
9450 				{
9451 				  ivd.vd_next = BYTE_GET (evd.vd_next);
9452 				  ivd.vd_ndx  = BYTE_GET (evd.vd_ndx);
9453 				}
9454 
9455 			      offset += ivd.vd_next;
9456 			    }
9457 			  while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
9458 				 && ivd.vd_next != 0);
9459 
9460 			  if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
9461 			    {
9462 			      Elf_External_Verdaux evda;
9463 			      Elf_Internal_Verdaux ivda;
9464 
9465 			      ivd.vd_aux = BYTE_GET (evd.vd_aux);
9466 
9467 			      if (get_data (&evda, file,
9468 					    offset - ivd.vd_next + ivd.vd_aux,
9469 					    sizeof (evda), 1,
9470 					    _("version def aux")) == NULL)
9471 				break;
9472 
9473 			      ivda.vda_name = BYTE_GET (evda.vda_name);
9474 
9475 			      if (ivda.vda_name >= string_sec->sh_size)
9476 				name = _("*invalid*");
9477 			      else
9478 				name = strtab + ivda.vda_name;
9479 			      nn += printf ("(%s%-*s",
9480 					    name,
9481 					    12 - (int) strlen (name),
9482 					    ")");
9483 			    }
9484 			}
9485 
9486 		      if (nn < 18)
9487 			printf ("%*c", 18 - nn, ' ');
9488 		    }
9489 
9490 		putchar ('\n');
9491 	      }
9492 
9493 	    free (data);
9494 	    free (strtab);
9495 	    free (symbols);
9496 	  }
9497 	  break;
9498 
9499 	default:
9500 	  break;
9501 	}
9502     }
9503 
9504   if (! found)
9505     printf (_("\nNo version information found in this file.\n"));
9506 
9507   return 1;
9508 }
9509 
9510 static const char *
get_symbol_binding(unsigned int binding)9511 get_symbol_binding (unsigned int binding)
9512 {
9513   static char buff[32];
9514 
9515   switch (binding)
9516     {
9517     case STB_LOCAL:	return "LOCAL";
9518     case STB_GLOBAL:	return "GLOBAL";
9519     case STB_WEAK:	return "WEAK";
9520     default:
9521       if (binding >= STB_LOPROC && binding <= STB_HIPROC)
9522 	snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
9523 		  binding);
9524       else if (binding >= STB_LOOS && binding <= STB_HIOS)
9525 	{
9526 	  if (binding == STB_GNU_UNIQUE
9527 	      && (elf_header.e_ident[EI_OSABI] == ELFOSABI_GNU
9528 		  /* GNU is still using the default value 0.  */
9529 		  || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
9530 	    return "UNIQUE";
9531 	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
9532 	}
9533       else
9534 	snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
9535       return buff;
9536     }
9537 }
9538 
9539 static const char *
get_symbol_type(unsigned int type)9540 get_symbol_type (unsigned int type)
9541 {
9542   static char buff[32];
9543 
9544   switch (type)
9545     {
9546     case STT_NOTYPE:	return "NOTYPE";
9547     case STT_OBJECT:	return "OBJECT";
9548     case STT_FUNC:	return "FUNC";
9549     case STT_SECTION:	return "SECTION";
9550     case STT_FILE:	return "FILE";
9551     case STT_COMMON:	return "COMMON";
9552     case STT_TLS:	return "TLS";
9553     case STT_RELC:      return "RELC";
9554     case STT_SRELC:     return "SRELC";
9555     default:
9556       if (type >= STT_LOPROC && type <= STT_HIPROC)
9557 	{
9558 	  if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
9559 	    return "THUMB_FUNC";
9560 
9561 	  if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
9562 	    return "REGISTER";
9563 
9564 	  if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
9565 	    return "PARISC_MILLI";
9566 
9567 	  snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
9568 	}
9569       else if (type >= STT_LOOS && type <= STT_HIOS)
9570 	{
9571 	  if (elf_header.e_machine == EM_PARISC)
9572 	    {
9573 	      if (type == STT_HP_OPAQUE)
9574 		return "HP_OPAQUE";
9575 	      if (type == STT_HP_STUB)
9576 		return "HP_STUB";
9577 	    }
9578 
9579 	  if (type == STT_GNU_IFUNC
9580 	      && (elf_header.e_ident[EI_OSABI] == ELFOSABI_GNU
9581 		  || elf_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD
9582 		  /* GNU is still using the default value 0.  */
9583 		  || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
9584 	    return "IFUNC";
9585 
9586 	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
9587 	}
9588       else
9589 	snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
9590       return buff;
9591     }
9592 }
9593 
9594 static const char *
get_symbol_visibility(unsigned int visibility)9595 get_symbol_visibility (unsigned int visibility)
9596 {
9597   switch (visibility)
9598     {
9599     case STV_DEFAULT:	return "DEFAULT";
9600     case STV_INTERNAL:	return "INTERNAL";
9601     case STV_HIDDEN:	return "HIDDEN";
9602     case STV_PROTECTED: return "PROTECTED";
9603     default: abort ();
9604     }
9605 }
9606 
9607 static const char *
get_mips_symbol_other(unsigned int other)9608 get_mips_symbol_other (unsigned int other)
9609 {
9610   switch (other)
9611     {
9612     case STO_OPTIONAL:
9613       return "OPTIONAL";
9614     case STO_MIPS_PLT:
9615       return "MIPS PLT";
9616     case STO_MIPS_PIC:
9617       return "MIPS PIC";
9618     case STO_MICROMIPS:
9619       return "MICROMIPS";
9620     case STO_MICROMIPS | STO_MIPS_PIC:
9621       return "MICROMIPS, MIPS PIC";
9622     case STO_MIPS16:
9623       return "MIPS16";
9624     default:
9625       return NULL;
9626     }
9627 }
9628 
9629 static const char *
get_ia64_symbol_other(unsigned int other)9630 get_ia64_symbol_other (unsigned int other)
9631 {
9632   if (is_ia64_vms ())
9633     {
9634       static char res[32];
9635 
9636       res[0] = 0;
9637 
9638       /* Function types is for images and .STB files only.  */
9639       switch (elf_header.e_type)
9640         {
9641         case ET_DYN:
9642         case ET_EXEC:
9643           switch (VMS_ST_FUNC_TYPE (other))
9644             {
9645             case VMS_SFT_CODE_ADDR:
9646               strcat (res, " CA");
9647               break;
9648             case VMS_SFT_SYMV_IDX:
9649               strcat (res, " VEC");
9650               break;
9651             case VMS_SFT_FD:
9652               strcat (res, " FD");
9653               break;
9654             case VMS_SFT_RESERVE:
9655               strcat (res, " RSV");
9656               break;
9657             default:
9658               abort ();
9659             }
9660           break;
9661         default:
9662           break;
9663         }
9664       switch (VMS_ST_LINKAGE (other))
9665         {
9666         case VMS_STL_IGNORE:
9667           strcat (res, " IGN");
9668           break;
9669         case VMS_STL_RESERVE:
9670           strcat (res, " RSV");
9671           break;
9672         case VMS_STL_STD:
9673           strcat (res, " STD");
9674           break;
9675         case VMS_STL_LNK:
9676           strcat (res, " LNK");
9677           break;
9678         default:
9679           abort ();
9680         }
9681 
9682       if (res[0] != 0)
9683         return res + 1;
9684       else
9685         return res;
9686     }
9687   return NULL;
9688 }
9689 
9690 static const char *
get_ppc64_symbol_other(unsigned int other)9691 get_ppc64_symbol_other (unsigned int other)
9692 {
9693   if (PPC64_LOCAL_ENTRY_OFFSET (other) != 0)
9694     {
9695       static char buf[32];
9696       snprintf (buf, sizeof buf, _("<localentry>: %d"),
9697 		PPC64_LOCAL_ENTRY_OFFSET (other));
9698       return buf;
9699     }
9700   return NULL;
9701 }
9702 
9703 static const char *
get_symbol_other(unsigned int other)9704 get_symbol_other (unsigned int other)
9705 {
9706   const char * result = NULL;
9707   static char buff [32];
9708 
9709   if (other == 0)
9710     return "";
9711 
9712   switch (elf_header.e_machine)
9713     {
9714     case EM_MIPS:
9715       result = get_mips_symbol_other (other);
9716       break;
9717     case EM_IA_64:
9718       result = get_ia64_symbol_other (other);
9719       break;
9720     case EM_PPC64:
9721       result = get_ppc64_symbol_other (other);
9722       break;
9723     default:
9724       break;
9725     }
9726 
9727   if (result)
9728     return result;
9729 
9730   snprintf (buff, sizeof buff, _("<other>: %x"), other);
9731   return buff;
9732 }
9733 
9734 static const char *
get_symbol_index_type(unsigned int type)9735 get_symbol_index_type (unsigned int type)
9736 {
9737   static char buff[32];
9738 
9739   switch (type)
9740     {
9741     case SHN_UNDEF:	return "UND";
9742     case SHN_ABS:	return "ABS";
9743     case SHN_COMMON:	return "COM";
9744     default:
9745       if (type == SHN_IA_64_ANSI_COMMON
9746 	  && elf_header.e_machine == EM_IA_64
9747 	  && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
9748 	return "ANSI_COM";
9749       else if ((elf_header.e_machine == EM_X86_64
9750 		|| elf_header.e_machine == EM_L1OM
9751 		|| elf_header.e_machine == EM_K1OM)
9752 	       && type == SHN_X86_64_LCOMMON)
9753 	return "LARGE_COM";
9754       else if ((type == SHN_MIPS_SCOMMON
9755 		&& elf_header.e_machine == EM_MIPS)
9756 	       || (type == SHN_TIC6X_SCOMMON
9757 		   && elf_header.e_machine == EM_TI_C6000))
9758 	return "SCOM";
9759       else if (type == SHN_MIPS_SUNDEFINED
9760 	       && elf_header.e_machine == EM_MIPS)
9761 	return "SUND";
9762       else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
9763 	sprintf (buff, "PRC[0x%04x]", type & 0xffff);
9764       else if (type >= SHN_LOOS && type <= SHN_HIOS)
9765 	sprintf (buff, "OS [0x%04x]", type & 0xffff);
9766       else if (type >= SHN_LORESERVE)
9767 	sprintf (buff, "RSV[0x%04x]", type & 0xffff);
9768       else if (type >= elf_header.e_shnum)
9769 	sprintf (buff, _("bad section index[%3d]"), type);
9770       else
9771 	sprintf (buff, "%3d", type);
9772       break;
9773     }
9774 
9775   return buff;
9776 }
9777 
9778 static bfd_vma *
get_dynamic_data(FILE * file,size_t number,unsigned int ent_size)9779 get_dynamic_data (FILE * file, size_t number, unsigned int ent_size)
9780 {
9781   unsigned char * e_data;
9782   bfd_vma * i_data;
9783 
9784   /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
9785      attempting to allocate memory when the read is bound to fail.  */
9786   if (ent_size * number > current_file_size)
9787     {
9788       error (_("Invalid number of dynamic entries: %lu\n"),
9789 	     (unsigned long) number);
9790       return NULL;
9791     }
9792 
9793   e_data = (unsigned char *) cmalloc (number, ent_size);
9794   if (e_data == NULL)
9795     {
9796       error (_("Out of memory reading %lu dynamic entries\n"),
9797 	     (unsigned long) number);
9798       return NULL;
9799     }
9800 
9801   if (fread (e_data, ent_size, number, file) != number)
9802     {
9803       error (_("Unable to read in %lu bytes of dynamic data\n"),
9804 	     (unsigned long) (number * ent_size));
9805       free (e_data);
9806       return NULL;
9807     }
9808 
9809   i_data = (bfd_vma *) cmalloc (number, sizeof (*i_data));
9810   if (i_data == NULL)
9811     {
9812       error (_("Out of memory allocating space for %lu dynamic entries\n"),
9813 	     (unsigned long) number);
9814       free (e_data);
9815       return NULL;
9816     }
9817 
9818   while (number--)
9819     i_data[number] = byte_get (e_data + number * ent_size, ent_size);
9820 
9821   free (e_data);
9822 
9823   return i_data;
9824 }
9825 
9826 static void
print_dynamic_symbol(bfd_vma si,unsigned long hn)9827 print_dynamic_symbol (bfd_vma si, unsigned long hn)
9828 {
9829   Elf_Internal_Sym * psym;
9830   int n;
9831 
9832   n = print_vma (si, DEC_5);
9833   if (n < 5)
9834     fputs (&"     "[n], stdout);
9835   printf (" %3lu: ", hn);
9836 
9837   if (dynamic_symbols == NULL || si >= num_dynamic_syms)
9838     {
9839       printf (_("<No info available for dynamic symbol number %lu>\n"),
9840 	      (unsigned long) si);
9841       return;
9842     }
9843 
9844   psym = dynamic_symbols + si;
9845   print_vma (psym->st_value, LONG_HEX);
9846   putchar (' ');
9847   print_vma (psym->st_size, DEC_5);
9848 
9849   printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
9850   printf (" %-6s",  get_symbol_binding (ELF_ST_BIND (psym->st_info)));
9851   printf (" %-7s",  get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
9852   /* Check to see if any other bits in the st_other field are set.
9853      Note - displaying this information disrupts the layout of the
9854      table being generated, but for the moment this case is very
9855      rare.  */
9856   if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
9857     printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
9858   printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
9859   if (VALID_DYNAMIC_NAME (psym->st_name))
9860     print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
9861   else
9862     printf (_(" <corrupt: %14ld>"), psym->st_name);
9863   putchar ('\n');
9864 }
9865 
9866 /* Dump the symbol table.  */
9867 static int
process_symbol_table(FILE * file)9868 process_symbol_table (FILE * file)
9869 {
9870   Elf_Internal_Shdr * section;
9871   bfd_size_type nbuckets = 0;
9872   bfd_size_type nchains = 0;
9873   bfd_vma * buckets = NULL;
9874   bfd_vma * chains = NULL;
9875   bfd_vma ngnubuckets = 0;
9876   bfd_vma * gnubuckets = NULL;
9877   bfd_vma * gnuchains = NULL;
9878   bfd_vma gnusymidx = 0;
9879   bfd_size_type ngnuchains = 0;
9880 
9881   if (!do_syms && !do_dyn_syms && !do_histogram)
9882     return 1;
9883 
9884   if (dynamic_info[DT_HASH]
9885       && (do_histogram
9886 	  || (do_using_dynamic
9887 	      && !do_dyn_syms
9888 	      && dynamic_strings != NULL)))
9889     {
9890       unsigned char nb[8];
9891       unsigned char nc[8];
9892       unsigned int hash_ent_size = 4;
9893 
9894       if ((elf_header.e_machine == EM_ALPHA
9895 	   || elf_header.e_machine == EM_S390
9896 	   || elf_header.e_machine == EM_S390_OLD)
9897 	  && elf_header.e_ident[EI_CLASS] == ELFCLASS64)
9898 	hash_ent_size = 8;
9899 
9900       if (fseek (file,
9901 		 (archive_file_offset
9902 		  + offset_from_vma (file, dynamic_info[DT_HASH],
9903 				     sizeof nb + sizeof nc)),
9904 		 SEEK_SET))
9905 	{
9906 	  error (_("Unable to seek to start of dynamic information\n"));
9907 	  goto no_hash;
9908 	}
9909 
9910       if (fread (nb, hash_ent_size, 1, file) != 1)
9911 	{
9912 	  error (_("Failed to read in number of buckets\n"));
9913 	  goto no_hash;
9914 	}
9915 
9916       if (fread (nc, hash_ent_size, 1, file) != 1)
9917 	{
9918 	  error (_("Failed to read in number of chains\n"));
9919 	  goto no_hash;
9920 	}
9921 
9922       nbuckets = byte_get (nb, hash_ent_size);
9923       nchains  = byte_get (nc, hash_ent_size);
9924 
9925       buckets = get_dynamic_data (file, nbuckets, hash_ent_size);
9926       chains  = get_dynamic_data (file, nchains, hash_ent_size);
9927 
9928     no_hash:
9929       if (buckets == NULL || chains == NULL)
9930 	{
9931 	  if (do_using_dynamic)
9932 	    return 0;
9933 	  free (buckets);
9934 	  free (chains);
9935 	  buckets = NULL;
9936 	  chains = NULL;
9937 	  nbuckets = 0;
9938 	  nchains = 0;
9939 	}
9940     }
9941 
9942   if (dynamic_info_DT_GNU_HASH
9943       && (do_histogram
9944 	  || (do_using_dynamic
9945 	      && !do_dyn_syms
9946 	      && dynamic_strings != NULL)))
9947     {
9948       unsigned char nb[16];
9949       bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
9950       bfd_vma buckets_vma;
9951 
9952       if (fseek (file,
9953 		 (archive_file_offset
9954 		  + offset_from_vma (file, dynamic_info_DT_GNU_HASH,
9955 				     sizeof nb)),
9956 		 SEEK_SET))
9957 	{
9958 	  error (_("Unable to seek to start of dynamic information\n"));
9959 	  goto no_gnu_hash;
9960 	}
9961 
9962       if (fread (nb, 16, 1, file) != 1)
9963 	{
9964 	  error (_("Failed to read in number of buckets\n"));
9965 	  goto no_gnu_hash;
9966 	}
9967 
9968       ngnubuckets = byte_get (nb, 4);
9969       gnusymidx = byte_get (nb + 4, 4);
9970       bitmaskwords = byte_get (nb + 8, 4);
9971       buckets_vma = dynamic_info_DT_GNU_HASH + 16;
9972       if (is_32bit_elf)
9973 	buckets_vma += bitmaskwords * 4;
9974       else
9975 	buckets_vma += bitmaskwords * 8;
9976 
9977       if (fseek (file,
9978 		 (archive_file_offset
9979 		  + offset_from_vma (file, buckets_vma, 4)),
9980 		 SEEK_SET))
9981 	{
9982 	  error (_("Unable to seek to start of dynamic information\n"));
9983 	  goto no_gnu_hash;
9984 	}
9985 
9986       gnubuckets = get_dynamic_data (file, ngnubuckets, 4);
9987 
9988       if (gnubuckets == NULL)
9989 	goto no_gnu_hash;
9990 
9991       for (i = 0; i < ngnubuckets; i++)
9992 	if (gnubuckets[i] != 0)
9993 	  {
9994 	    if (gnubuckets[i] < gnusymidx)
9995 	      return 0;
9996 
9997 	    if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
9998 	      maxchain = gnubuckets[i];
9999 	  }
10000 
10001       if (maxchain == 0xffffffff)
10002 	goto no_gnu_hash;
10003 
10004       maxchain -= gnusymidx;
10005 
10006       if (fseek (file,
10007 		 (archive_file_offset
10008 		  + offset_from_vma (file, buckets_vma
10009 					   + 4 * (ngnubuckets + maxchain), 4)),
10010 		 SEEK_SET))
10011 	{
10012 	  error (_("Unable to seek to start of dynamic information\n"));
10013 	  goto no_gnu_hash;
10014 	}
10015 
10016       do
10017 	{
10018 	  if (fread (nb, 4, 1, file) != 1)
10019 	    {
10020 	      error (_("Failed to determine last chain length\n"));
10021 	      goto no_gnu_hash;
10022 	    }
10023 
10024 	  if (maxchain + 1 == 0)
10025 	    goto no_gnu_hash;
10026 
10027 	  ++maxchain;
10028 	}
10029       while ((byte_get (nb, 4) & 1) == 0);
10030 
10031       if (fseek (file,
10032 		 (archive_file_offset
10033 		  + offset_from_vma (file, buckets_vma + 4 * ngnubuckets, 4)),
10034 		 SEEK_SET))
10035 	{
10036 	  error (_("Unable to seek to start of dynamic information\n"));
10037 	  goto no_gnu_hash;
10038 	}
10039 
10040       gnuchains = get_dynamic_data (file, maxchain, 4);
10041       ngnuchains = maxchain;
10042 
10043     no_gnu_hash:
10044       if (gnuchains == NULL)
10045 	{
10046 	  free (gnubuckets);
10047 	  gnubuckets = NULL;
10048 	  ngnubuckets = 0;
10049 	  if (do_using_dynamic)
10050 	    return 0;
10051 	}
10052     }
10053 
10054   if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
10055       && do_syms
10056       && do_using_dynamic
10057       && dynamic_strings != NULL
10058       && dynamic_symbols != NULL)
10059     {
10060       unsigned long hn;
10061 
10062       if (dynamic_info[DT_HASH])
10063 	{
10064 	  bfd_vma si;
10065 
10066 	  printf (_("\nSymbol table for image:\n"));
10067 	  if (is_32bit_elf)
10068 	    printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
10069 	  else
10070 	    printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
10071 
10072 	  for (hn = 0; hn < nbuckets; hn++)
10073 	    {
10074 	      if (! buckets[hn])
10075 		continue;
10076 
10077 	      for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
10078 		print_dynamic_symbol (si, hn);
10079 	    }
10080 	}
10081 
10082       if (dynamic_info_DT_GNU_HASH)
10083 	{
10084 	  printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
10085 	  if (is_32bit_elf)
10086 	    printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
10087 	  else
10088 	    printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
10089 
10090 	  for (hn = 0; hn < ngnubuckets; ++hn)
10091 	    if (gnubuckets[hn] != 0)
10092 	      {
10093 		bfd_vma si = gnubuckets[hn];
10094 		bfd_vma off = si - gnusymidx;
10095 
10096 		do
10097 		  {
10098 		    print_dynamic_symbol (si, hn);
10099 		    si++;
10100 		  }
10101 		while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
10102 	      }
10103 	}
10104     }
10105   else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
10106 	   && section_headers != NULL)
10107     {
10108       unsigned int i;
10109 
10110       for (i = 0, section = section_headers;
10111 	   i < elf_header.e_shnum;
10112 	   i++, section++)
10113 	{
10114 	  unsigned int si;
10115 	  char * strtab = NULL;
10116 	  unsigned long int strtab_size = 0;
10117 	  Elf_Internal_Sym * symtab;
10118 	  Elf_Internal_Sym * psym;
10119 	  unsigned long num_syms;
10120 
10121 	  if ((section->sh_type != SHT_SYMTAB
10122 	       && section->sh_type != SHT_DYNSYM)
10123 	      || (!do_syms
10124 		  && section->sh_type == SHT_SYMTAB))
10125 	    continue;
10126 
10127 	  if (section->sh_entsize == 0)
10128 	    {
10129 	      printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
10130 		      printable_section_name (section));
10131 	      continue;
10132 	    }
10133 
10134 	  printf (_("\nSymbol table '%s' contains %lu entries:\n"),
10135 		  printable_section_name (section),
10136 		  (unsigned long) (section->sh_size / section->sh_entsize));
10137 
10138 	  if (is_32bit_elf)
10139 	    printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
10140 	  else
10141 	    printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
10142 
10143 	  symtab = GET_ELF_SYMBOLS (file, section, & num_syms);
10144 	  if (symtab == NULL)
10145 	    continue;
10146 
10147 	  if (section->sh_link == elf_header.e_shstrndx)
10148 	    {
10149 	      strtab = string_table;
10150 	      strtab_size = string_table_length;
10151 	    }
10152 	  else if (section->sh_link < elf_header.e_shnum)
10153 	    {
10154 	      Elf_Internal_Shdr * string_sec;
10155 
10156 	      string_sec = section_headers + section->sh_link;
10157 
10158 	      strtab = (char *) get_data (NULL, file, string_sec->sh_offset,
10159                                           1, string_sec->sh_size,
10160                                           _("string table"));
10161 	      strtab_size = strtab != NULL ? string_sec->sh_size : 0;
10162 	    }
10163 
10164 	  for (si = 0, psym = symtab; si < num_syms; si++, psym++)
10165 	    {
10166 	      printf ("%6d: ", si);
10167 	      print_vma (psym->st_value, LONG_HEX);
10168 	      putchar (' ');
10169 	      print_vma (psym->st_size, DEC_5);
10170 	      printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
10171 	      printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
10172 	      printf (" %-7s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
10173 	      /* Check to see if any other bits in the st_other field are set.
10174 	         Note - displaying this information disrupts the layout of the
10175 	         table being generated, but for the moment this case is very rare.  */
10176 	      if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
10177 		printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
10178 	      printf (" %4s ", get_symbol_index_type (psym->st_shndx));
10179 	      print_symbol (25, psym->st_name < strtab_size
10180 			    ? strtab + psym->st_name : _("<corrupt>"));
10181 
10182 	      if (section->sh_type == SHT_DYNSYM
10183 		  && version_info[DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
10184 		{
10185 		  unsigned char data[2];
10186 		  unsigned short vers_data;
10187 		  unsigned long offset;
10188 		  int is_nobits;
10189 		  int check_def;
10190 
10191 		  offset = offset_from_vma
10192 		    (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
10193 		     sizeof data + si * sizeof (vers_data));
10194 
10195 		  if (get_data (&data, file, offset + si * sizeof (vers_data),
10196 				sizeof (data), 1, _("version data")) == NULL)
10197 		    break;
10198 
10199 		  vers_data = byte_get (data, 2);
10200 
10201 		  is_nobits = (psym->st_shndx < elf_header.e_shnum
10202 			       && section_headers[psym->st_shndx].sh_type
10203 				  == SHT_NOBITS);
10204 
10205 		  check_def = (psym->st_shndx != SHN_UNDEF);
10206 
10207 		  if ((vers_data & VERSYM_HIDDEN) || vers_data > 1)
10208 		    {
10209 		      if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
10210 			  && (is_nobits || ! check_def))
10211 			{
10212 			  Elf_External_Verneed evn;
10213 			  Elf_Internal_Verneed ivn;
10214 			  Elf_Internal_Vernaux ivna;
10215 
10216 			  /* We must test both.  */
10217 			  offset = offset_from_vma
10218 			    (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
10219 			     sizeof evn);
10220 
10221 			  do
10222 			    {
10223 			      unsigned long vna_off;
10224 
10225 			      if (get_data (&evn, file, offset, sizeof (evn), 1,
10226 					    _("version need")) == NULL)
10227 				{
10228 				  ivna.vna_next = 0;
10229 				  ivna.vna_other = 0;
10230 				  ivna.vna_name = 0;
10231 				  break;
10232 				}
10233 
10234 			      ivn.vn_aux  = BYTE_GET (evn.vn_aux);
10235 			      ivn.vn_next = BYTE_GET (evn.vn_next);
10236 
10237 			      vna_off = offset + ivn.vn_aux;
10238 
10239 			      do
10240 				{
10241 				  Elf_External_Vernaux evna;
10242 
10243 				  if (get_data (&evna, file, vna_off,
10244 						sizeof (evna), 1,
10245 						_("version need aux (3)")) == NULL)
10246 				    {
10247 				      ivna.vna_next = 0;
10248 				      ivna.vna_other = 0;
10249 				      ivna.vna_name = 0;
10250 				    }
10251 				  else
10252 				    {
10253 				      ivna.vna_other = BYTE_GET (evna.vna_other);
10254 				      ivna.vna_next  = BYTE_GET (evna.vna_next);
10255 				      ivna.vna_name  = BYTE_GET (evna.vna_name);
10256 				    }
10257 
10258 				  vna_off += ivna.vna_next;
10259 				}
10260 			      while (ivna.vna_other != vers_data
10261 				     && ivna.vna_next != 0);
10262 
10263 			      if (ivna.vna_other == vers_data)
10264 				break;
10265 
10266 			      offset += ivn.vn_next;
10267 			    }
10268 			  while (ivn.vn_next != 0);
10269 
10270 			  if (ivna.vna_other == vers_data)
10271 			    {
10272 			      printf ("@%s (%d)",
10273 				      ivna.vna_name < strtab_size
10274 				      ? strtab + ivna.vna_name : _("<corrupt>"),
10275 				      ivna.vna_other);
10276 			      check_def = 0;
10277 			    }
10278 			  else if (! is_nobits)
10279 			    error (_("bad dynamic symbol\n"));
10280 			  else
10281 			    check_def = 1;
10282 			}
10283 
10284 		      if (check_def)
10285 			{
10286 			  if (vers_data != 0x8001
10287 			      && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
10288 			    {
10289 			      Elf_Internal_Verdef ivd;
10290 			      Elf_Internal_Verdaux ivda;
10291 			      Elf_External_Verdaux evda;
10292 			      unsigned long off;
10293 
10294 			      off = offset_from_vma
10295 				(file,
10296 				 version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
10297 				 sizeof (Elf_External_Verdef));
10298 
10299 			      do
10300 				{
10301 				  Elf_External_Verdef evd;
10302 
10303 				  if (get_data (&evd, file, off, sizeof (evd),
10304 						1, _("version def")) == NULL)
10305 				    {
10306 				      ivd.vd_ndx = 0;
10307 				      ivd.vd_aux = 0;
10308 				      ivd.vd_next = 0;
10309 				    }
10310 				  else
10311 				    {
10312 				      ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
10313 				      ivd.vd_aux = BYTE_GET (evd.vd_aux);
10314 				      ivd.vd_next = BYTE_GET (evd.vd_next);
10315 				    }
10316 
10317 				  off += ivd.vd_next;
10318 				}
10319 			      while (ivd.vd_ndx != (vers_data & VERSYM_VERSION)
10320 				     && ivd.vd_next != 0);
10321 
10322 			      off -= ivd.vd_next;
10323 			      off += ivd.vd_aux;
10324 
10325 			      if (get_data (&evda, file, off, sizeof (evda),
10326 					    1, _("version def aux")) == NULL)
10327 				break;
10328 
10329 			      ivda.vda_name = BYTE_GET (evda.vda_name);
10330 
10331 			      if (psym->st_name != ivda.vda_name)
10332 				printf ((vers_data & VERSYM_HIDDEN)
10333 					? "@%s" : "@@%s",
10334 					ivda.vda_name < strtab_size
10335 					? strtab + ivda.vda_name : _("<corrupt>"));
10336 			    }
10337 			}
10338 		    }
10339 		}
10340 
10341 	      putchar ('\n');
10342 	    }
10343 
10344 	  free (symtab);
10345 	  if (strtab != string_table)
10346 	    free (strtab);
10347 	}
10348     }
10349   else if (do_syms)
10350     printf
10351       (_("\nDynamic symbol information is not available for displaying symbols.\n"));
10352 
10353   if (do_histogram && buckets != NULL)
10354     {
10355       unsigned long * lengths;
10356       unsigned long * counts;
10357       unsigned long hn;
10358       bfd_vma si;
10359       unsigned long maxlength = 0;
10360       unsigned long nzero_counts = 0;
10361       unsigned long nsyms = 0;
10362 
10363       printf (_("\nHistogram for bucket list length (total of %lu buckets):\n"),
10364 	      (unsigned long) nbuckets);
10365 
10366       lengths = (unsigned long *) calloc (nbuckets, sizeof (*lengths));
10367       if (lengths == NULL)
10368 	{
10369 	  error (_("Out of memory allocating space for histogram buckets\n"));
10370 	  return 0;
10371 	}
10372 
10373       printf (_(" Length  Number     %% of total  Coverage\n"));
10374       for (hn = 0; hn < nbuckets; ++hn)
10375 	{
10376 	  for (si = buckets[hn]; si > 0 && si < nchains; si = chains[si])
10377 	    {
10378 	      ++nsyms;
10379 	      if (maxlength < ++lengths[hn])
10380 		++maxlength;
10381 
10382 	      /* PR binutils/17531: A corrupt binary could contain broken
10383 		 histogram data.  Do not go into an infinite loop trying
10384 		 to process it.  */
10385 	      if (chains[si] == si)
10386 		{
10387 		  error (_("histogram chain links to itself\n"));
10388 		  break;
10389 		}
10390 	    }
10391 	}
10392 
10393       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
10394       if (counts == NULL)
10395 	{
10396 	  free (lengths);
10397 	  error (_("Out of memory allocating space for histogram counts\n"));
10398 	  return 0;
10399 	}
10400 
10401       for (hn = 0; hn < nbuckets; ++hn)
10402 	++counts[lengths[hn]];
10403 
10404       if (nbuckets > 0)
10405 	{
10406 	  unsigned long i;
10407 	  printf ("      0  %-10lu (%5.1f%%)\n",
10408 		  counts[0], (counts[0] * 100.0) / nbuckets);
10409 	  for (i = 1; i <= maxlength; ++i)
10410 	    {
10411 	      nzero_counts += counts[i] * i;
10412 	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
10413 		      i, counts[i], (counts[i] * 100.0) / nbuckets,
10414 		      (nzero_counts * 100.0) / nsyms);
10415 	    }
10416 	}
10417 
10418       free (counts);
10419       free (lengths);
10420     }
10421 
10422   if (buckets != NULL)
10423     {
10424       free (buckets);
10425       free (chains);
10426     }
10427 
10428   if (do_histogram && gnubuckets != NULL)
10429     {
10430       unsigned long * lengths;
10431       unsigned long * counts;
10432       unsigned long hn;
10433       unsigned long maxlength = 0;
10434       unsigned long nzero_counts = 0;
10435       unsigned long nsyms = 0;
10436 
10437       printf (_("\nHistogram for `.gnu.hash' bucket list length (total of %lu buckets):\n"),
10438 	      (unsigned long) ngnubuckets);
10439 
10440       lengths = (unsigned long *) calloc (ngnubuckets, sizeof (*lengths));
10441       if (lengths == NULL)
10442 	{
10443 	  error (_("Out of memory allocating space for gnu histogram buckets\n"));
10444 	  return 0;
10445 	}
10446 
10447       printf (_(" Length  Number     %% of total  Coverage\n"));
10448 
10449       for (hn = 0; hn < ngnubuckets; ++hn)
10450 	if (gnubuckets[hn] != 0)
10451 	  {
10452 	    bfd_vma off, length = 1;
10453 
10454 	    for (off = gnubuckets[hn] - gnusymidx;
10455 		 /* PR 17531 file: 010-77222-0.004.  */
10456 		 off < ngnuchains && (gnuchains[off] & 1) == 0;
10457 		 ++off)
10458 	      ++length;
10459 	    lengths[hn] = length;
10460 	    if (length > maxlength)
10461 	      maxlength = length;
10462 	    nsyms += length;
10463 	  }
10464 
10465       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
10466       if (counts == NULL)
10467 	{
10468 	  free (lengths);
10469 	  error (_("Out of memory allocating space for gnu histogram counts\n"));
10470 	  return 0;
10471 	}
10472 
10473       for (hn = 0; hn < ngnubuckets; ++hn)
10474 	++counts[lengths[hn]];
10475 
10476       if (ngnubuckets > 0)
10477 	{
10478 	  unsigned long j;
10479 	  printf ("      0  %-10lu (%5.1f%%)\n",
10480 		  counts[0], (counts[0] * 100.0) / ngnubuckets);
10481 	  for (j = 1; j <= maxlength; ++j)
10482 	    {
10483 	      nzero_counts += counts[j] * j;
10484 	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
10485 		      j, counts[j], (counts[j] * 100.0) / ngnubuckets,
10486 		      (nzero_counts * 100.0) / nsyms);
10487 	    }
10488 	}
10489 
10490       free (counts);
10491       free (lengths);
10492       free (gnubuckets);
10493       free (gnuchains);
10494     }
10495 
10496   return 1;
10497 }
10498 
10499 static int
process_syminfo(FILE * file ATTRIBUTE_UNUSED)10500 process_syminfo (FILE * file ATTRIBUTE_UNUSED)
10501 {
10502   unsigned int i;
10503 
10504   if (dynamic_syminfo == NULL
10505       || !do_dynamic)
10506     /* No syminfo, this is ok.  */
10507     return 1;
10508 
10509   /* There better should be a dynamic symbol section.  */
10510   if (dynamic_symbols == NULL || dynamic_strings == NULL)
10511     return 0;
10512 
10513   if (dynamic_addr)
10514     printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
10515 	    dynamic_syminfo_offset, dynamic_syminfo_nent);
10516 
10517   printf (_(" Num: Name                           BoundTo     Flags\n"));
10518   for (i = 0; i < dynamic_syminfo_nent; ++i)
10519     {
10520       unsigned short int flags = dynamic_syminfo[i].si_flags;
10521 
10522       printf ("%4d: ", i);
10523       if (i >= num_dynamic_syms)
10524 	printf (_("<corrupt index>"));
10525       else if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
10526 	print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
10527       else
10528 	printf (_("<corrupt: %19ld>"), dynamic_symbols[i].st_name);
10529       putchar (' ');
10530 
10531       switch (dynamic_syminfo[i].si_boundto)
10532 	{
10533 	case SYMINFO_BT_SELF:
10534 	  fputs ("SELF       ", stdout);
10535 	  break;
10536 	case SYMINFO_BT_PARENT:
10537 	  fputs ("PARENT     ", stdout);
10538 	  break;
10539 	default:
10540 	  if (dynamic_syminfo[i].si_boundto > 0
10541 	      && dynamic_syminfo[i].si_boundto < dynamic_nent
10542 	      && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
10543 	    {
10544 	      print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
10545 	      putchar (' ' );
10546 	    }
10547 	  else
10548 	    printf ("%-10d ", dynamic_syminfo[i].si_boundto);
10549 	  break;
10550 	}
10551 
10552       if (flags & SYMINFO_FLG_DIRECT)
10553 	printf (" DIRECT");
10554       if (flags & SYMINFO_FLG_PASSTHRU)
10555 	printf (" PASSTHRU");
10556       if (flags & SYMINFO_FLG_COPY)
10557 	printf (" COPY");
10558       if (flags & SYMINFO_FLG_LAZYLOAD)
10559 	printf (" LAZYLOAD");
10560 
10561       puts ("");
10562     }
10563 
10564   return 1;
10565 }
10566 
10567 /* Check to see if the given reloc needs to be handled in a target specific
10568    manner.  If so then process the reloc and return TRUE otherwise return
10569    FALSE.  */
10570 
10571 static bfd_boolean
target_specific_reloc_handling(Elf_Internal_Rela * reloc,unsigned char * start,Elf_Internal_Sym * symtab)10572 target_specific_reloc_handling (Elf_Internal_Rela * reloc,
10573 				unsigned char *     start,
10574 				Elf_Internal_Sym *  symtab)
10575 {
10576   unsigned int reloc_type = get_reloc_type (reloc->r_info);
10577 
10578   switch (elf_header.e_machine)
10579     {
10580     case EM_MSP430:
10581     case EM_MSP430_OLD:
10582       {
10583 	static Elf_Internal_Sym * saved_sym = NULL;
10584 
10585 	switch (reloc_type)
10586 	  {
10587 	  case 10: /* R_MSP430_SYM_DIFF */
10588 	    if (uses_msp430x_relocs ())
10589 	      break;
10590 	  case 21: /* R_MSP430X_SYM_DIFF */
10591 	    saved_sym = symtab + get_reloc_symindex (reloc->r_info);
10592 	    return TRUE;
10593 
10594 	  case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
10595 	  case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
10596 	    goto handle_sym_diff;
10597 
10598 	  case 5: /* R_MSP430_16_BYTE */
10599 	  case 9: /* R_MSP430_8 */
10600 	    if (uses_msp430x_relocs ())
10601 	      break;
10602 	    goto handle_sym_diff;
10603 
10604 	  case 2: /* R_MSP430_ABS16 */
10605 	  case 15: /* R_MSP430X_ABS16 */
10606 	    if (! uses_msp430x_relocs ())
10607 	      break;
10608 	    goto handle_sym_diff;
10609 
10610 	  handle_sym_diff:
10611 	    if (saved_sym != NULL)
10612 	      {
10613 		bfd_vma value;
10614 
10615 		value = reloc->r_addend
10616 		  + (symtab[get_reloc_symindex (reloc->r_info)].st_value
10617 		     - saved_sym->st_value);
10618 
10619 		byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
10620 
10621 		saved_sym = NULL;
10622 		return TRUE;
10623 	      }
10624 	    break;
10625 
10626 	  default:
10627 	    if (saved_sym != NULL)
10628 	      error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
10629 	    break;
10630 	  }
10631 	break;
10632       }
10633 
10634     case EM_MN10300:
10635     case EM_CYGNUS_MN10300:
10636       {
10637 	static Elf_Internal_Sym * saved_sym = NULL;
10638 
10639 	switch (reloc_type)
10640 	  {
10641 	  case 34: /* R_MN10300_ALIGN */
10642 	    return TRUE;
10643 	  case 33: /* R_MN10300_SYM_DIFF */
10644 	    saved_sym = symtab + get_reloc_symindex (reloc->r_info);
10645 	    return TRUE;
10646 	  case 1: /* R_MN10300_32 */
10647 	  case 2: /* R_MN10300_16 */
10648 	    if (saved_sym != NULL)
10649 	      {
10650 		bfd_vma value;
10651 
10652 		value = reloc->r_addend
10653 		  + (symtab[get_reloc_symindex (reloc->r_info)].st_value
10654 		     - saved_sym->st_value);
10655 
10656 		byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
10657 
10658 		saved_sym = NULL;
10659 		return TRUE;
10660 	      }
10661 	    break;
10662 	  default:
10663 	    if (saved_sym != NULL)
10664 	      error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
10665 	    break;
10666 	  }
10667 	break;
10668       }
10669     }
10670 
10671   return FALSE;
10672 }
10673 
10674 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
10675    DWARF debug sections.  This is a target specific test.  Note - we do not
10676    go through the whole including-target-headers-multiple-times route, (as
10677    we have already done with <elf/h8.h>) because this would become very
10678    messy and even then this function would have to contain target specific
10679    information (the names of the relocs instead of their numeric values).
10680    FIXME: This is not the correct way to solve this problem.  The proper way
10681    is to have target specific reloc sizing and typing functions created by
10682    the reloc-macros.h header, in the same way that it already creates the
10683    reloc naming functions.  */
10684 
10685 static bfd_boolean
is_32bit_abs_reloc(unsigned int reloc_type)10686 is_32bit_abs_reloc (unsigned int reloc_type)
10687 {
10688   switch (elf_header.e_machine)
10689     {
10690     case EM_386:
10691     case EM_486:
10692       return reloc_type == 1; /* R_386_32.  */
10693     case EM_68K:
10694       return reloc_type == 1; /* R_68K_32.  */
10695     case EM_860:
10696       return reloc_type == 1; /* R_860_32.  */
10697     case EM_960:
10698       return reloc_type == 2; /* R_960_32.  */
10699     case EM_AARCH64:
10700       return reloc_type == 258; /* R_AARCH64_ABS32 */
10701     case EM_ALPHA:
10702       return reloc_type == 1; /* R_ALPHA_REFLONG.  */
10703     case EM_ARC:
10704       return reloc_type == 1; /* R_ARC_32.  */
10705     case EM_ARM:
10706       return reloc_type == 2; /* R_ARM_ABS32 */
10707     case EM_AVR_OLD:
10708     case EM_AVR:
10709       return reloc_type == 1;
10710     case EM_ADAPTEVA_EPIPHANY:
10711       return reloc_type == 3;
10712     case EM_BLACKFIN:
10713       return reloc_type == 0x12; /* R_byte4_data.  */
10714     case EM_CRIS:
10715       return reloc_type == 3; /* R_CRIS_32.  */
10716     case EM_CR16:
10717       return reloc_type == 3; /* R_CR16_NUM32.  */
10718     case EM_CRX:
10719       return reloc_type == 15; /* R_CRX_NUM32.  */
10720     case EM_CYGNUS_FRV:
10721       return reloc_type == 1;
10722     case EM_CYGNUS_D10V:
10723     case EM_D10V:
10724       return reloc_type == 6; /* R_D10V_32.  */
10725     case EM_CYGNUS_D30V:
10726     case EM_D30V:
10727       return reloc_type == 12; /* R_D30V_32_NORMAL.  */
10728     case EM_DLX:
10729       return reloc_type == 3; /* R_DLX_RELOC_32.  */
10730     case EM_CYGNUS_FR30:
10731     case EM_FR30:
10732       return reloc_type == 3; /* R_FR30_32.  */
10733     case EM_H8S:
10734     case EM_H8_300:
10735     case EM_H8_300H:
10736       return reloc_type == 1; /* R_H8_DIR32.  */
10737     case EM_IA_64:
10738       return reloc_type == 0x65; /* R_IA64_SECREL32LSB.  */
10739     case EM_IP2K_OLD:
10740     case EM_IP2K:
10741       return reloc_type == 2; /* R_IP2K_32.  */
10742     case EM_IQ2000:
10743       return reloc_type == 2; /* R_IQ2000_32.  */
10744     case EM_LATTICEMICO32:
10745       return reloc_type == 3; /* R_LM32_32.  */
10746     case EM_M32C_OLD:
10747     case EM_M32C:
10748       return reloc_type == 3; /* R_M32C_32.  */
10749     case EM_M32R:
10750       return reloc_type == 34; /* R_M32R_32_RELA.  */
10751     case EM_MCORE:
10752       return reloc_type == 1; /* R_MCORE_ADDR32.  */
10753     case EM_CYGNUS_MEP:
10754       return reloc_type == 4; /* R_MEP_32.  */
10755     case EM_METAG:
10756       return reloc_type == 2; /* R_METAG_ADDR32.  */
10757     case EM_MICROBLAZE:
10758       return reloc_type == 1; /* R_MICROBLAZE_32.  */
10759     case EM_MIPS:
10760       return reloc_type == 2; /* R_MIPS_32.  */
10761     case EM_MMIX:
10762       return reloc_type == 4; /* R_MMIX_32.  */
10763     case EM_CYGNUS_MN10200:
10764     case EM_MN10200:
10765       return reloc_type == 1; /* R_MN10200_32.  */
10766     case EM_CYGNUS_MN10300:
10767     case EM_MN10300:
10768       return reloc_type == 1; /* R_MN10300_32.  */
10769     case EM_MOXIE:
10770       return reloc_type == 1; /* R_MOXIE_32.  */
10771     case EM_MSP430_OLD:
10772     case EM_MSP430:
10773       return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32.  */
10774     case EM_MT:
10775       return reloc_type == 2; /* R_MT_32.  */
10776     case EM_NDS32:
10777       return reloc_type == 20; /* R_NDS32_RELA.  */
10778     case EM_ALTERA_NIOS2:
10779       return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32.  */
10780     case EM_NIOS32:
10781       return reloc_type == 1; /* R_NIOS_32.  */
10782     case EM_OR1K:
10783       return reloc_type == 1; /* R_OR1K_32.  */
10784     case EM_PARISC:
10785       return (reloc_type == 1 /* R_PARISC_DIR32.  */
10786 	      || reloc_type == 41); /* R_PARISC_SECREL32.  */
10787     case EM_PJ:
10788     case EM_PJ_OLD:
10789       return reloc_type == 1; /* R_PJ_DATA_DIR32.  */
10790     case EM_PPC64:
10791       return reloc_type == 1; /* R_PPC64_ADDR32.  */
10792     case EM_PPC:
10793       return reloc_type == 1; /* R_PPC_ADDR32.  */
10794     case EM_RL78:
10795       return reloc_type == 1; /* R_RL78_DIR32.  */
10796     case EM_RX:
10797       return reloc_type == 1; /* R_RX_DIR32.  */
10798     case EM_S370:
10799       return reloc_type == 1; /* R_I370_ADDR31.  */
10800     case EM_S390_OLD:
10801     case EM_S390:
10802       return reloc_type == 4; /* R_S390_32.  */
10803     case EM_SCORE:
10804       return reloc_type == 8; /* R_SCORE_ABS32.  */
10805     case EM_SH:
10806       return reloc_type == 1; /* R_SH_DIR32.  */
10807     case EM_SPARC32PLUS:
10808     case EM_SPARCV9:
10809     case EM_SPARC:
10810       return reloc_type == 3 /* R_SPARC_32.  */
10811 	|| reloc_type == 23; /* R_SPARC_UA32.  */
10812     case EM_SPU:
10813       return reloc_type == 6; /* R_SPU_ADDR32 */
10814     case EM_TI_C6000:
10815       return reloc_type == 1; /* R_C6000_ABS32.  */
10816     case EM_TILEGX:
10817       return reloc_type == 2; /* R_TILEGX_32.  */
10818     case EM_TILEPRO:
10819       return reloc_type == 1; /* R_TILEPRO_32.  */
10820     case EM_CYGNUS_V850:
10821     case EM_V850:
10822       return reloc_type == 6; /* R_V850_ABS32.  */
10823     case EM_V800:
10824       return reloc_type == 0x33; /* R_V810_WORD.  */
10825     case EM_VAX:
10826       return reloc_type == 1; /* R_VAX_32.  */
10827     case EM_X86_64:
10828     case EM_L1OM:
10829     case EM_K1OM:
10830       return reloc_type == 10; /* R_X86_64_32.  */
10831     case EM_XC16X:
10832     case EM_C166:
10833       return reloc_type == 3; /* R_XC16C_ABS_32.  */
10834     case EM_XGATE:
10835       return reloc_type == 4; /* R_XGATE_32.  */
10836     case EM_XSTORMY16:
10837       return reloc_type == 1; /* R_XSTROMY16_32.  */
10838     case EM_XTENSA_OLD:
10839     case EM_XTENSA:
10840       return reloc_type == 1; /* R_XTENSA_32.  */
10841     default:
10842       error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
10843 	     elf_header.e_machine);
10844       abort ();
10845     }
10846 }
10847 
10848 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
10849    a 32-bit pc-relative RELA relocation used in DWARF debug sections.  */
10850 
10851 static bfd_boolean
is_32bit_pcrel_reloc(unsigned int reloc_type)10852 is_32bit_pcrel_reloc (unsigned int reloc_type)
10853 {
10854   switch (elf_header.e_machine)
10855     {
10856     case EM_386:
10857     case EM_486:
10858       return reloc_type == 2;  /* R_386_PC32.  */
10859     case EM_68K:
10860       return reloc_type == 4;  /* R_68K_PC32.  */
10861     case EM_AARCH64:
10862       return reloc_type == 261; /* R_AARCH64_PREL32 */
10863     case EM_ADAPTEVA_EPIPHANY:
10864       return reloc_type == 6;
10865     case EM_ALPHA:
10866       return reloc_type == 10; /* R_ALPHA_SREL32.  */
10867     case EM_ARM:
10868       return reloc_type == 3;  /* R_ARM_REL32 */
10869     case EM_MICROBLAZE:
10870       return reloc_type == 2;  /* R_MICROBLAZE_32_PCREL.  */
10871     case EM_OR1K:
10872       return reloc_type == 9; /* R_OR1K_32_PCREL.  */
10873     case EM_PARISC:
10874       return reloc_type == 9;  /* R_PARISC_PCREL32.  */
10875     case EM_PPC:
10876       return reloc_type == 26; /* R_PPC_REL32.  */
10877     case EM_PPC64:
10878       return reloc_type == 26; /* R_PPC64_REL32.  */
10879     case EM_S390_OLD:
10880     case EM_S390:
10881       return reloc_type == 5;  /* R_390_PC32.  */
10882     case EM_SH:
10883       return reloc_type == 2;  /* R_SH_REL32.  */
10884     case EM_SPARC32PLUS:
10885     case EM_SPARCV9:
10886     case EM_SPARC:
10887       return reloc_type == 6;  /* R_SPARC_DISP32.  */
10888     case EM_SPU:
10889       return reloc_type == 13; /* R_SPU_REL32.  */
10890     case EM_TILEGX:
10891       return reloc_type == 6; /* R_TILEGX_32_PCREL.  */
10892     case EM_TILEPRO:
10893       return reloc_type == 4; /* R_TILEPRO_32_PCREL.  */
10894     case EM_X86_64:
10895     case EM_L1OM:
10896     case EM_K1OM:
10897       return reloc_type == 2;  /* R_X86_64_PC32.  */
10898     case EM_XTENSA_OLD:
10899     case EM_XTENSA:
10900       return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
10901     default:
10902       /* Do not abort or issue an error message here.  Not all targets use
10903 	 pc-relative 32-bit relocs in their DWARF debug information and we
10904 	 have already tested for target coverage in is_32bit_abs_reloc.  A
10905 	 more helpful warning message will be generated by apply_relocations
10906 	 anyway, so just return.  */
10907       return FALSE;
10908     }
10909 }
10910 
10911 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
10912    a 64-bit absolute RELA relocation used in DWARF debug sections.  */
10913 
10914 static bfd_boolean
is_64bit_abs_reloc(unsigned int reloc_type)10915 is_64bit_abs_reloc (unsigned int reloc_type)
10916 {
10917   switch (elf_header.e_machine)
10918     {
10919     case EM_AARCH64:
10920       return reloc_type == 257;	/* R_AARCH64_ABS64.  */
10921     case EM_ALPHA:
10922       return reloc_type == 2; /* R_ALPHA_REFQUAD.  */
10923     case EM_IA_64:
10924       return reloc_type == 0x27; /* R_IA64_DIR64LSB.  */
10925     case EM_PARISC:
10926       return reloc_type == 80; /* R_PARISC_DIR64.  */
10927     case EM_PPC64:
10928       return reloc_type == 38; /* R_PPC64_ADDR64.  */
10929     case EM_SPARC32PLUS:
10930     case EM_SPARCV9:
10931     case EM_SPARC:
10932       return reloc_type == 54; /* R_SPARC_UA64.  */
10933     case EM_X86_64:
10934     case EM_L1OM:
10935     case EM_K1OM:
10936       return reloc_type == 1; /* R_X86_64_64.  */
10937     case EM_S390_OLD:
10938     case EM_S390:
10939       return reloc_type == 22;	/* R_S390_64.  */
10940     case EM_TILEGX:
10941       return reloc_type == 1; /* R_TILEGX_64.  */
10942     case EM_MIPS:
10943       return reloc_type == 18;	/* R_MIPS_64.  */
10944     default:
10945       return FALSE;
10946     }
10947 }
10948 
10949 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
10950    a 64-bit pc-relative RELA relocation used in DWARF debug sections.  */
10951 
10952 static bfd_boolean
is_64bit_pcrel_reloc(unsigned int reloc_type)10953 is_64bit_pcrel_reloc (unsigned int reloc_type)
10954 {
10955   switch (elf_header.e_machine)
10956     {
10957     case EM_AARCH64:
10958       return reloc_type == 260;	/* R_AARCH64_PREL64.  */
10959     case EM_ALPHA:
10960       return reloc_type == 11; /* R_ALPHA_SREL64.  */
10961     case EM_IA_64:
10962       return reloc_type == 0x4f; /* R_IA64_PCREL64LSB.  */
10963     case EM_PARISC:
10964       return reloc_type == 72; /* R_PARISC_PCREL64.  */
10965     case EM_PPC64:
10966       return reloc_type == 44; /* R_PPC64_REL64.  */
10967     case EM_SPARC32PLUS:
10968     case EM_SPARCV9:
10969     case EM_SPARC:
10970       return reloc_type == 46; /* R_SPARC_DISP64.  */
10971     case EM_X86_64:
10972     case EM_L1OM:
10973     case EM_K1OM:
10974       return reloc_type == 24; /* R_X86_64_PC64.  */
10975     case EM_S390_OLD:
10976     case EM_S390:
10977       return reloc_type == 23;	/* R_S390_PC64.  */
10978     case EM_TILEGX:
10979       return reloc_type == 5;  /* R_TILEGX_64_PCREL.  */
10980     default:
10981       return FALSE;
10982     }
10983 }
10984 
10985 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
10986    a 24-bit absolute RELA relocation used in DWARF debug sections.  */
10987 
10988 static bfd_boolean
is_24bit_abs_reloc(unsigned int reloc_type)10989 is_24bit_abs_reloc (unsigned int reloc_type)
10990 {
10991   switch (elf_header.e_machine)
10992     {
10993     case EM_CYGNUS_MN10200:
10994     case EM_MN10200:
10995       return reloc_type == 4; /* R_MN10200_24.  */
10996     default:
10997       return FALSE;
10998     }
10999 }
11000 
11001 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11002    a 16-bit absolute RELA relocation used in DWARF debug sections.  */
11003 
11004 static bfd_boolean
is_16bit_abs_reloc(unsigned int reloc_type)11005 is_16bit_abs_reloc (unsigned int reloc_type)
11006 {
11007   switch (elf_header.e_machine)
11008     {
11009     case EM_AVR_OLD:
11010     case EM_AVR:
11011       return reloc_type == 4; /* R_AVR_16.  */
11012     case EM_ADAPTEVA_EPIPHANY:
11013       return reloc_type == 5;
11014     case EM_CYGNUS_D10V:
11015     case EM_D10V:
11016       return reloc_type == 3; /* R_D10V_16.  */
11017     case EM_H8S:
11018     case EM_H8_300:
11019     case EM_H8_300H:
11020       return reloc_type == R_H8_DIR16;
11021     case EM_IP2K_OLD:
11022     case EM_IP2K:
11023       return reloc_type == 1; /* R_IP2K_16.  */
11024     case EM_M32C_OLD:
11025     case EM_M32C:
11026       return reloc_type == 1; /* R_M32C_16 */
11027     case EM_MSP430:
11028       if (uses_msp430x_relocs ())
11029 	return reloc_type == 2; /* R_MSP430_ABS16.  */
11030     case EM_MSP430_OLD:
11031       return reloc_type == 5; /* R_MSP430_16_BYTE.  */
11032     case EM_NDS32:
11033       return reloc_type == 19; /* R_NDS32_RELA.  */
11034     case EM_ALTERA_NIOS2:
11035       return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16.  */
11036     case EM_NIOS32:
11037       return reloc_type == 9; /* R_NIOS_16.  */
11038     case EM_OR1K:
11039       return reloc_type == 2; /* R_OR1K_16.  */
11040     case EM_TI_C6000:
11041       return reloc_type == 2; /* R_C6000_ABS16.  */
11042     case EM_XC16X:
11043     case EM_C166:
11044       return reloc_type == 2; /* R_XC16C_ABS_16.  */
11045     case EM_CYGNUS_MN10200:
11046     case EM_MN10200:
11047       return reloc_type == 2; /* R_MN10200_16.  */
11048     case EM_CYGNUS_MN10300:
11049     case EM_MN10300:
11050       return reloc_type == 2; /* R_MN10300_16.  */
11051     case EM_XGATE:
11052       return reloc_type == 3; /* R_XGATE_16.  */
11053     default:
11054       return FALSE;
11055     }
11056 }
11057 
11058 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
11059    relocation entries (possibly formerly used for SHT_GROUP sections).  */
11060 
11061 static bfd_boolean
is_none_reloc(unsigned int reloc_type)11062 is_none_reloc (unsigned int reloc_type)
11063 {
11064   switch (elf_header.e_machine)
11065     {
11066     case EM_68K:     /* R_68K_NONE.  */
11067     case EM_386:     /* R_386_NONE.  */
11068     case EM_SPARC32PLUS:
11069     case EM_SPARCV9:
11070     case EM_SPARC:   /* R_SPARC_NONE.  */
11071     case EM_MIPS:    /* R_MIPS_NONE.  */
11072     case EM_PARISC:  /* R_PARISC_NONE.  */
11073     case EM_ALPHA:   /* R_ALPHA_NONE.  */
11074     case EM_ADAPTEVA_EPIPHANY:
11075     case EM_PPC:     /* R_PPC_NONE.  */
11076     case EM_PPC64:   /* R_PPC64_NONE.  */
11077     case EM_ARM:     /* R_ARM_NONE.  */
11078     case EM_IA_64:   /* R_IA64_NONE.  */
11079     case EM_SH:      /* R_SH_NONE.  */
11080     case EM_S390_OLD:
11081     case EM_S390:    /* R_390_NONE.  */
11082     case EM_CRIS:    /* R_CRIS_NONE.  */
11083     case EM_X86_64:  /* R_X86_64_NONE.  */
11084     case EM_L1OM:    /* R_X86_64_NONE.  */
11085     case EM_K1OM:    /* R_X86_64_NONE.  */
11086     case EM_MN10300: /* R_MN10300_NONE.  */
11087     case EM_MOXIE:   /* R_MOXIE_NONE.  */
11088     case EM_M32R:    /* R_M32R_NONE.  */
11089     case EM_TI_C6000:/* R_C6000_NONE.  */
11090     case EM_TILEGX:  /* R_TILEGX_NONE.  */
11091     case EM_TILEPRO: /* R_TILEPRO_NONE.  */
11092     case EM_XC16X:
11093     case EM_C166:    /* R_XC16X_NONE.  */
11094     case EM_ALTERA_NIOS2: /* R_NIOS2_NONE.  */
11095     case EM_NIOS32:  /* R_NIOS_NONE.  */
11096     case EM_OR1K:    /* R_OR1K_NONE. */
11097       return reloc_type == 0;
11098     case EM_AARCH64:
11099       return reloc_type == 0 || reloc_type == 256;
11100     case EM_NDS32:
11101       return (reloc_type == 0       /* R_XTENSA_NONE.  */
11102 	      || reloc_type == 204  /* R_NDS32_DIFF8.  */
11103 	      || reloc_type == 205  /* R_NDS32_DIFF16.  */
11104 	      || reloc_type == 206  /* R_NDS32_DIFF32.  */
11105 	      || reloc_type == 207  /* R_NDS32_ULEB128.  */);
11106     case EM_XTENSA_OLD:
11107     case EM_XTENSA:
11108       return (reloc_type == 0      /* R_XTENSA_NONE.  */
11109 	      || reloc_type == 17  /* R_XTENSA_DIFF8.  */
11110 	      || reloc_type == 18  /* R_XTENSA_DIFF16.  */
11111 	      || reloc_type == 19  /* R_XTENSA_DIFF32.  */);
11112     case EM_METAG:
11113       return reloc_type == 3; /* R_METAG_NONE.  */
11114     }
11115   return FALSE;
11116 }
11117 
11118 /* Apply relocations to a section.
11119    Note: So far support has been added only for those relocations
11120    which can be found in debug sections.
11121    FIXME: Add support for more relocations ?  */
11122 
11123 static void
apply_relocations(void * file,Elf_Internal_Shdr * section,unsigned char * start)11124 apply_relocations (void * file,
11125 		   Elf_Internal_Shdr * section,
11126 		   unsigned char * start)
11127 {
11128   Elf_Internal_Shdr * relsec;
11129   unsigned char * end = start + section->sh_size;
11130 
11131   if (elf_header.e_type != ET_REL)
11132     return;
11133 
11134   /* Find the reloc section associated with the section.  */
11135   for (relsec = section_headers;
11136        relsec < section_headers + elf_header.e_shnum;
11137        ++relsec)
11138     {
11139       bfd_boolean is_rela;
11140       unsigned long num_relocs;
11141       Elf_Internal_Rela * relocs;
11142       Elf_Internal_Rela * rp;
11143       Elf_Internal_Shdr * symsec;
11144       Elf_Internal_Sym * symtab;
11145       unsigned long num_syms;
11146       Elf_Internal_Sym * sym;
11147 
11148       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
11149 	  || relsec->sh_info >= elf_header.e_shnum
11150 	  || section_headers + relsec->sh_info != section
11151 	  || relsec->sh_size == 0
11152 	  || relsec->sh_link >= elf_header.e_shnum)
11153 	continue;
11154 
11155       is_rela = relsec->sh_type == SHT_RELA;
11156 
11157       if (is_rela)
11158 	{
11159 	  if (!slurp_rela_relocs ((FILE *) file, relsec->sh_offset,
11160                                   relsec->sh_size, & relocs, & num_relocs))
11161 	    return;
11162 	}
11163       else
11164 	{
11165 	  if (!slurp_rel_relocs ((FILE *) file, relsec->sh_offset,
11166                                  relsec->sh_size, & relocs, & num_relocs))
11167 	    return;
11168 	}
11169 
11170       /* SH uses RELA but uses in place value instead of the addend field.  */
11171       if (elf_header.e_machine == EM_SH)
11172 	is_rela = FALSE;
11173 
11174       symsec = section_headers + relsec->sh_link;
11175       symtab = GET_ELF_SYMBOLS ((FILE *) file, symsec, & num_syms);
11176 
11177       for (rp = relocs; rp < relocs + num_relocs; ++rp)
11178 	{
11179 	  bfd_vma         addend;
11180 	  unsigned int    reloc_type;
11181 	  unsigned int    reloc_size;
11182 	  unsigned char * rloc;
11183 	  unsigned long   sym_index;
11184 
11185 	  reloc_type = get_reloc_type (rp->r_info);
11186 
11187 	  if (target_specific_reloc_handling (rp, start, symtab))
11188 	    continue;
11189 	  else if (is_none_reloc (reloc_type))
11190 	    continue;
11191 	  else if (is_32bit_abs_reloc (reloc_type)
11192 		   || is_32bit_pcrel_reloc (reloc_type))
11193 	    reloc_size = 4;
11194 	  else if (is_64bit_abs_reloc (reloc_type)
11195 		   || is_64bit_pcrel_reloc (reloc_type))
11196 	    reloc_size = 8;
11197 	  else if (is_24bit_abs_reloc (reloc_type))
11198 	    reloc_size = 3;
11199 	  else if (is_16bit_abs_reloc (reloc_type))
11200 	    reloc_size = 2;
11201 	  else
11202 	    {
11203 	      warn (_("unable to apply unsupported reloc type %d to section %s\n"),
11204 		    reloc_type, printable_section_name (section));
11205 	      continue;
11206 	    }
11207 
11208 	  rloc = start + rp->r_offset;
11209 	  if ((rloc + reloc_size) > end || (rloc < start))
11210 	    {
11211 	      warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
11212 		    (unsigned long) rp->r_offset,
11213 		    printable_section_name (section));
11214 	      continue;
11215 	    }
11216 
11217 	  sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
11218 	  if (sym_index >= num_syms)
11219 	    {
11220 	      warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
11221 		    sym_index, printable_section_name (section));
11222 	      continue;
11223 	    }
11224 	  sym = symtab + sym_index;
11225 
11226 	  /* If the reloc has a symbol associated with it,
11227 	     make sure that it is of an appropriate type.
11228 
11229 	     Relocations against symbols without type can happen.
11230 	     Gcc -feliminate-dwarf2-dups may generate symbols
11231 	     without type for debug info.
11232 
11233 	     Icc generates relocations against function symbols
11234 	     instead of local labels.
11235 
11236 	     Relocations against object symbols can happen, eg when
11237 	     referencing a global array.  For an example of this see
11238 	     the _clz.o binary in libgcc.a.  */
11239 	  if (sym != symtab
11240 	      && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
11241 	    {
11242 	      warn (_("skipping unexpected symbol type %s in %ld'th relocation in section %s\n"),
11243 		    get_symbol_type (ELF_ST_TYPE (sym->st_info)),
11244 		    (long int)(rp - relocs),
11245 		    printable_section_name (relsec));
11246 	      continue;
11247 	    }
11248 
11249 	  addend = 0;
11250 	  if (is_rela)
11251 	    addend += rp->r_addend;
11252 	  /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
11253 	     partial_inplace.  */
11254 	  if (!is_rela
11255 	      || (elf_header.e_machine == EM_XTENSA
11256 		  && reloc_type == 1)
11257 	      || ((elf_header.e_machine == EM_PJ
11258 		   || elf_header.e_machine == EM_PJ_OLD)
11259 		  && reloc_type == 1)
11260 	      || ((elf_header.e_machine == EM_D30V
11261 		   || elf_header.e_machine == EM_CYGNUS_D30V)
11262 		  && reloc_type == 12))
11263 	    addend += byte_get (rloc, reloc_size);
11264 
11265 	  if (is_32bit_pcrel_reloc (reloc_type)
11266 	      || is_64bit_pcrel_reloc (reloc_type))
11267 	    {
11268 	      /* On HPPA, all pc-relative relocations are biased by 8.  */
11269 	      if (elf_header.e_machine == EM_PARISC)
11270 		addend -= 8;
11271 	      byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
11272 		        reloc_size);
11273 	    }
11274 	  else
11275 	    byte_put (rloc, addend + sym->st_value, reloc_size);
11276 	}
11277 
11278       free (symtab);
11279       free (relocs);
11280       break;
11281     }
11282 }
11283 
11284 #ifdef SUPPORT_DISASSEMBLY
11285 static int
disassemble_section(Elf_Internal_Shdr * section,FILE * file)11286 disassemble_section (Elf_Internal_Shdr * section, FILE * file)
11287 {
11288   printf (_("\nAssembly dump of section %s\n"), printable_section_name (section));
11289 
11290   /* FIXME: XXX -- to be done --- XXX */
11291 
11292   return 1;
11293 }
11294 #endif
11295 
11296 /* Reads in the contents of SECTION from FILE, returning a pointer
11297    to a malloc'ed buffer or NULL if something went wrong.  */
11298 
11299 static char *
get_section_contents(Elf_Internal_Shdr * section,FILE * file)11300 get_section_contents (Elf_Internal_Shdr * section, FILE * file)
11301 {
11302   bfd_size_type num_bytes;
11303 
11304   num_bytes = section->sh_size;
11305 
11306   if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
11307     {
11308       printf (_("\nSection '%s' has no data to dump.\n"),
11309 	      printable_section_name (section));
11310       return NULL;
11311     }
11312 
11313   return  (char *) get_data (NULL, file, section->sh_offset, 1, num_bytes,
11314                              _("section contents"));
11315 }
11316 
11317 
11318 static void
dump_section_as_strings(Elf_Internal_Shdr * section,FILE * file)11319 dump_section_as_strings (Elf_Internal_Shdr * section, FILE * file)
11320 {
11321   Elf_Internal_Shdr * relsec;
11322   bfd_size_type num_bytes;
11323   char * data;
11324   char * end;
11325   char * start;
11326   bfd_boolean some_strings_shown;
11327 
11328   start = get_section_contents (section, file);
11329   if (start == NULL)
11330     return;
11331 
11332   printf (_("\nString dump of section '%s':\n"), printable_section_name (section));
11333 
11334   /* If the section being dumped has relocations against it the user might
11335      be expecting these relocations to have been applied.  Check for this
11336      case and issue a warning message in order to avoid confusion.
11337      FIXME: Maybe we ought to have an option that dumps a section with
11338      relocs applied ?  */
11339   for (relsec = section_headers;
11340        relsec < section_headers + elf_header.e_shnum;
11341        ++relsec)
11342     {
11343       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
11344 	  || relsec->sh_info >= elf_header.e_shnum
11345 	  || section_headers + relsec->sh_info != section
11346 	  || relsec->sh_size == 0
11347 	  || relsec->sh_link >= elf_header.e_shnum)
11348 	continue;
11349 
11350       printf (_("  Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
11351       break;
11352     }
11353 
11354   num_bytes = section->sh_size;
11355   data = start;
11356   end  = start + num_bytes;
11357   some_strings_shown = FALSE;
11358 
11359   while (data < end)
11360     {
11361       while (!ISPRINT (* data))
11362 	if (++ data >= end)
11363 	  break;
11364 
11365       if (data < end)
11366 	{
11367 	  size_t maxlen = end - data;
11368 
11369 #ifndef __MSVCRT__
11370 	  /* PR 11128: Use two separate invocations in order to work
11371              around bugs in the Solaris 8 implementation of printf.  */
11372 	  printf ("  [%6tx]  ", data - start);
11373 #else
11374 	  printf ("  [%6Ix]  ", (size_t) (data - start));
11375 #endif
11376 	  if (maxlen > 0)
11377 	    {
11378 	      print_symbol ((int) maxlen, data);
11379 	      putchar ('\n');
11380 	      data += strnlen (data, maxlen);
11381 	    }
11382 	  else
11383 	    {
11384 	      printf (_("<corrupt>\n"));
11385 	      data = end;
11386 	    }
11387 	  some_strings_shown = TRUE;
11388 	}
11389     }
11390 
11391   if (! some_strings_shown)
11392     printf (_("  No strings found in this section."));
11393 
11394   free (start);
11395 
11396   putchar ('\n');
11397 }
11398 
11399 static void
dump_section_as_bytes(Elf_Internal_Shdr * section,FILE * file,bfd_boolean relocate)11400 dump_section_as_bytes (Elf_Internal_Shdr * section,
11401 		       FILE * file,
11402 		       bfd_boolean relocate)
11403 {
11404   Elf_Internal_Shdr * relsec;
11405   bfd_size_type bytes;
11406   bfd_vma addr;
11407   unsigned char * data;
11408   unsigned char * start;
11409 
11410   start = (unsigned char *) get_section_contents (section, file);
11411   if (start == NULL)
11412     return;
11413 
11414   printf (_("\nHex dump of section '%s':\n"), printable_section_name (section));
11415 
11416   if (relocate)
11417     {
11418       apply_relocations (file, section, start);
11419     }
11420   else
11421     {
11422       /* If the section being dumped has relocations against it the user might
11423 	 be expecting these relocations to have been applied.  Check for this
11424 	 case and issue a warning message in order to avoid confusion.
11425 	 FIXME: Maybe we ought to have an option that dumps a section with
11426 	 relocs applied ?  */
11427       for (relsec = section_headers;
11428 	   relsec < section_headers + elf_header.e_shnum;
11429 	   ++relsec)
11430 	{
11431 	  if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
11432 	      || relsec->sh_info >= elf_header.e_shnum
11433 	      || section_headers + relsec->sh_info != section
11434 	      || relsec->sh_size == 0
11435 	      || relsec->sh_link >= elf_header.e_shnum)
11436 	    continue;
11437 
11438 	  printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
11439 	  break;
11440 	}
11441     }
11442 
11443   addr = section->sh_addr;
11444   bytes = section->sh_size;
11445   data = start;
11446 
11447   while (bytes)
11448     {
11449       int j;
11450       int k;
11451       int lbytes;
11452 
11453       lbytes = (bytes > 16 ? 16 : bytes);
11454 
11455       printf ("  0x%8.8lx ", (unsigned long) addr);
11456 
11457       for (j = 0; j < 16; j++)
11458 	{
11459 	  if (j < lbytes)
11460 	    printf ("%2.2x", data[j]);
11461 	  else
11462 	    printf ("  ");
11463 
11464 	  if ((j & 3) == 3)
11465 	    printf (" ");
11466 	}
11467 
11468       for (j = 0; j < lbytes; j++)
11469 	{
11470 	  k = data[j];
11471 	  if (k >= ' ' && k < 0x7f)
11472 	    printf ("%c", k);
11473 	  else
11474 	    printf (".");
11475 	}
11476 
11477       putchar ('\n');
11478 
11479       data  += lbytes;
11480       addr  += lbytes;
11481       bytes -= lbytes;
11482     }
11483 
11484   free (start);
11485 
11486   putchar ('\n');
11487 }
11488 
11489 /* Uncompresses a section that was compressed using zlib, in place.  */
11490 
11491 static int
uncompress_section_contents(unsigned char ** buffer ATTRIBUTE_UNUSED,dwarf_size_type * size ATTRIBUTE_UNUSED)11492 uncompress_section_contents (unsigned char **buffer ATTRIBUTE_UNUSED,
11493 			     dwarf_size_type *size ATTRIBUTE_UNUSED)
11494 {
11495 #ifndef HAVE_ZLIB_H
11496   return FALSE;
11497 #else
11498   dwarf_size_type compressed_size = *size;
11499   unsigned char * compressed_buffer = *buffer;
11500   dwarf_size_type uncompressed_size;
11501   unsigned char * uncompressed_buffer;
11502   z_stream strm;
11503   int rc;
11504   dwarf_size_type header_size = 12;
11505 
11506   /* Read the zlib header.  In this case, it should be "ZLIB" followed
11507      by the uncompressed section size, 8 bytes in big-endian order.  */
11508   if (compressed_size < header_size
11509       || ! streq ((char *) compressed_buffer, "ZLIB"))
11510     return 0;
11511 
11512   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
11513   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
11514   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
11515   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
11516   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
11517   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
11518   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
11519   uncompressed_size += compressed_buffer[11];
11520 
11521   /* It is possible the section consists of several compressed
11522      buffers concatenated together, so we uncompress in a loop.  */
11523   strm.zalloc = NULL;
11524   strm.zfree = NULL;
11525   strm.opaque = NULL;
11526   strm.avail_in = compressed_size - header_size;
11527   strm.next_in = (Bytef *) compressed_buffer + header_size;
11528   strm.avail_out = uncompressed_size;
11529   uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
11530 
11531   rc = inflateInit (& strm);
11532   while (strm.avail_in > 0)
11533     {
11534       if (rc != Z_OK)
11535         goto fail;
11536       strm.next_out = ((Bytef *) uncompressed_buffer
11537                        + (uncompressed_size - strm.avail_out));
11538       rc = inflate (&strm, Z_FINISH);
11539       if (rc != Z_STREAM_END)
11540         goto fail;
11541       rc = inflateReset (& strm);
11542     }
11543   rc = inflateEnd (& strm);
11544   if (rc != Z_OK
11545       || strm.avail_out != 0)
11546     goto fail;
11547 
11548   free (compressed_buffer);
11549   *buffer = uncompressed_buffer;
11550   *size = uncompressed_size;
11551   return 1;
11552 
11553  fail:
11554   free (uncompressed_buffer);
11555   /* Indicate decompression failure.  */
11556   *buffer = NULL;
11557   return 0;
11558 #endif  /* HAVE_ZLIB_H */
11559 }
11560 
11561 static int
load_specific_debug_section(enum dwarf_section_display_enum debug,Elf_Internal_Shdr * sec,void * file)11562 load_specific_debug_section (enum dwarf_section_display_enum debug,
11563 			     Elf_Internal_Shdr * sec, void * file)
11564 {
11565   struct dwarf_section * section = &debug_displays [debug].section;
11566   char buf [64];
11567 
11568   /* If it is already loaded, do nothing.  */
11569   if (section->start != NULL)
11570     return 1;
11571 
11572   snprintf (buf, sizeof (buf), _("%s section data"), section->name);
11573   section->address = sec->sh_addr;
11574   section->start = (unsigned char *) get_data (NULL, (FILE *) file,
11575                                                sec->sh_offset, 1,
11576                                                sec->sh_size, buf);
11577   if (section->start == NULL)
11578     section->size = 0;
11579   else
11580     {
11581       section->size = sec->sh_size;
11582       if (uncompress_section_contents (&section->start, &section->size))
11583 	sec->sh_size = section->size;
11584     }
11585 
11586   if (section->start == NULL)
11587     return 0;
11588 
11589   if (debug_displays [debug].relocate)
11590     apply_relocations ((FILE *) file, sec, section->start);
11591 
11592   return 1;
11593 }
11594 
11595 /* If this is not NULL, load_debug_section will only look for sections
11596    within the list of sections given here.  */
11597 unsigned int *section_subset = NULL;
11598 
11599 int
load_debug_section(enum dwarf_section_display_enum debug,void * file)11600 load_debug_section (enum dwarf_section_display_enum debug, void * file)
11601 {
11602   struct dwarf_section * section = &debug_displays [debug].section;
11603   Elf_Internal_Shdr * sec;
11604 
11605   /* Locate the debug section.  */
11606   sec = find_section_in_set (section->uncompressed_name, section_subset);
11607   if (sec != NULL)
11608     section->name = section->uncompressed_name;
11609   else
11610     {
11611       sec = find_section_in_set (section->compressed_name, section_subset);
11612       if (sec != NULL)
11613 	section->name = section->compressed_name;
11614     }
11615   if (sec == NULL)
11616     return 0;
11617 
11618   /* If we're loading from a subset of sections, and we've loaded
11619      a section matching this name before, it's likely that it's a
11620      different one.  */
11621   if (section_subset != NULL)
11622     free_debug_section (debug);
11623 
11624   return load_specific_debug_section (debug, sec, (FILE *) file);
11625 }
11626 
11627 void
free_debug_section(enum dwarf_section_display_enum debug)11628 free_debug_section (enum dwarf_section_display_enum debug)
11629 {
11630   struct dwarf_section * section = &debug_displays [debug].section;
11631 
11632   if (section->start == NULL)
11633     return;
11634 
11635   free ((char *) section->start);
11636   section->start = NULL;
11637   section->address = 0;
11638   section->size = 0;
11639 }
11640 
11641 static int
display_debug_section(int shndx,Elf_Internal_Shdr * section,FILE * file)11642 display_debug_section (int shndx, Elf_Internal_Shdr * section, FILE * file)
11643 {
11644   char * name = SECTION_NAME (section);
11645   const char * print_name = printable_section_name (section);
11646   bfd_size_type length;
11647   int result = 1;
11648   int i;
11649 
11650   length = section->sh_size;
11651   if (length == 0)
11652     {
11653       printf (_("\nSection '%s' has no debugging data.\n"), print_name);
11654       return 0;
11655     }
11656   if (section->sh_type == SHT_NOBITS)
11657     {
11658       /* There is no point in dumping the contents of a debugging section
11659 	 which has the NOBITS type - the bits in the file will be random.
11660 	 This can happen when a file containing a .eh_frame section is
11661 	 stripped with the --only-keep-debug command line option.  */
11662       printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
11663 	      print_name);
11664       return 0;
11665     }
11666 
11667   if (const_strneq (name, ".gnu.linkonce.wi."))
11668     name = ".debug_info";
11669 
11670   /* See if we know how to display the contents of this section.  */
11671   for (i = 0; i < max; i++)
11672     if (streq (debug_displays[i].section.uncompressed_name, name)
11673 	|| (i == line && const_strneq (name, ".debug_line."))
11674         || streq (debug_displays[i].section.compressed_name, name))
11675       {
11676 	struct dwarf_section * sec = &debug_displays [i].section;
11677 	int secondary = (section != find_section (name));
11678 
11679 	if (secondary)
11680 	  free_debug_section ((enum dwarf_section_display_enum) i);
11681 
11682 	if (i == line && const_strneq (name, ".debug_line."))
11683 	  sec->name = name;
11684 	else if (streq (sec->uncompressed_name, name))
11685 	  sec->name = sec->uncompressed_name;
11686 	else
11687 	  sec->name = sec->compressed_name;
11688 	if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
11689                                          section, file))
11690 	  {
11691 	    /* If this debug section is part of a CU/TU set in a .dwp file,
11692 	       restrict load_debug_section to the sections in that set.  */
11693 	    section_subset = find_cu_tu_set (file, shndx);
11694 
11695 	    result &= debug_displays[i].display (sec, file);
11696 
11697 	    section_subset = NULL;
11698 
11699 	    if (secondary || (i != info && i != abbrev))
11700 	      free_debug_section ((enum dwarf_section_display_enum) i);
11701 	  }
11702 
11703 	break;
11704       }
11705 
11706   if (i == max)
11707     {
11708       printf (_("Unrecognized debug section: %s\n"), print_name);
11709       result = 0;
11710     }
11711 
11712   return result;
11713 }
11714 
11715 /* Set DUMP_SECTS for all sections where dumps were requested
11716    based on section name.  */
11717 
11718 static void
initialise_dumps_byname(void)11719 initialise_dumps_byname (void)
11720 {
11721   struct dump_list_entry * cur;
11722 
11723   for (cur = dump_sects_byname; cur; cur = cur->next)
11724     {
11725       unsigned int i;
11726       int any;
11727 
11728       for (i = 0, any = 0; i < elf_header.e_shnum; i++)
11729 	if (streq (SECTION_NAME (section_headers + i), cur->name))
11730 	  {
11731 	    request_dump_bynumber (i, cur->type);
11732 	    any = 1;
11733 	  }
11734 
11735       if (!any)
11736 	warn (_("Section '%s' was not dumped because it does not exist!\n"),
11737 	      cur->name);
11738     }
11739 }
11740 
11741 static void
process_section_contents(FILE * file)11742 process_section_contents (FILE * file)
11743 {
11744   Elf_Internal_Shdr * section;
11745   unsigned int i;
11746 
11747   if (! do_dump)
11748     return;
11749 
11750   initialise_dumps_byname ();
11751 
11752   for (i = 0, section = section_headers;
11753        i < elf_header.e_shnum && i < num_dump_sects;
11754        i++, section++)
11755     {
11756 #ifdef SUPPORT_DISASSEMBLY
11757       if (dump_sects[i] & DISASS_DUMP)
11758 	disassemble_section (section, file);
11759 #endif
11760       if (dump_sects[i] & HEX_DUMP)
11761 	dump_section_as_bytes (section, file, FALSE);
11762 
11763       if (dump_sects[i] & RELOC_DUMP)
11764 	dump_section_as_bytes (section, file, TRUE);
11765 
11766       if (dump_sects[i] & STRING_DUMP)
11767 	dump_section_as_strings (section, file);
11768 
11769       if (dump_sects[i] & DEBUG_DUMP)
11770 	display_debug_section (i, section, file);
11771     }
11772 
11773   /* Check to see if the user requested a
11774      dump of a section that does not exist.  */
11775   while (i++ < num_dump_sects)
11776     if (dump_sects[i])
11777       warn (_("Section %d was not dumped because it does not exist!\n"), i);
11778 }
11779 
11780 static void
process_mips_fpe_exception(int mask)11781 process_mips_fpe_exception (int mask)
11782 {
11783   if (mask)
11784     {
11785       int first = 1;
11786       if (mask & OEX_FPU_INEX)
11787 	fputs ("INEX", stdout), first = 0;
11788       if (mask & OEX_FPU_UFLO)
11789 	printf ("%sUFLO", first ? "" : "|"), first = 0;
11790       if (mask & OEX_FPU_OFLO)
11791 	printf ("%sOFLO", first ? "" : "|"), first = 0;
11792       if (mask & OEX_FPU_DIV0)
11793 	printf ("%sDIV0", first ? "" : "|"), first = 0;
11794       if (mask & OEX_FPU_INVAL)
11795 	printf ("%sINVAL", first ? "" : "|");
11796     }
11797   else
11798     fputs ("0", stdout);
11799 }
11800 
11801 /* Display's the value of TAG at location P.  If TAG is
11802    greater than 0 it is assumed to be an unknown tag, and
11803    a message is printed to this effect.  Otherwise it is
11804    assumed that a message has already been printed.
11805 
11806    If the bottom bit of TAG is set it assumed to have a
11807    string value, otherwise it is assumed to have an integer
11808    value.
11809 
11810    Returns an updated P pointing to the first unread byte
11811    beyond the end of TAG's value.
11812 
11813    Reads at or beyond END will not be made.  */
11814 
11815 static unsigned char *
display_tag_value(int tag,unsigned char * p,const unsigned char * const end)11816 display_tag_value (int tag,
11817 		   unsigned char * p,
11818 		   const unsigned char * const end)
11819 {
11820   unsigned long val;
11821 
11822   if (tag > 0)
11823     printf ("  Tag_unknown_%d: ", tag);
11824 
11825   if (p >= end)
11826     {
11827       warn (_("<corrupt tag>\n"));
11828     }
11829   else if (tag & 1)
11830     {
11831       /* PR 17531 file: 027-19978-0.004.  */
11832       size_t maxlen = (end - p) - 1;
11833 
11834       putchar ('"');
11835       if (maxlen > 0)
11836 	{
11837 	  print_symbol ((int) maxlen, (const char *) p);
11838 	  p += strnlen ((char *) p, maxlen) + 1;
11839 	}
11840       else
11841 	{
11842 	  printf (_("<corrupt string tag>"));
11843 	  p = (unsigned char *) end;
11844 	}
11845       printf ("\"\n");
11846     }
11847   else
11848     {
11849       unsigned int len;
11850 
11851       val = read_uleb128 (p, &len, end);
11852       p += len;
11853       printf ("%ld (0x%lx)\n", val, val);
11854     }
11855 
11856   assert (p <= end);
11857   return p;
11858 }
11859 
11860 /* ARM EABI attributes section.  */
11861 typedef struct
11862 {
11863   unsigned int tag;
11864   const char * name;
11865   /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup.  */
11866   unsigned int type;
11867   const char ** table;
11868 } arm_attr_public_tag;
11869 
11870 static const char * arm_attr_tag_CPU_arch[] =
11871   {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
11872    "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8"};
11873 static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
11874 static const char * arm_attr_tag_THUMB_ISA_use[] =
11875   {"No", "Thumb-1", "Thumb-2"};
11876 static const char * arm_attr_tag_FP_arch[] =
11877   {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
11878    "FP for ARMv8"};
11879 static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
11880 static const char * arm_attr_tag_Advanced_SIMD_arch[] =
11881   {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8"};
11882 static const char * arm_attr_tag_PCS_config[] =
11883   {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
11884    "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
11885 static const char * arm_attr_tag_ABI_PCS_R9_use[] =
11886   {"V6", "SB", "TLS", "Unused"};
11887 static const char * arm_attr_tag_ABI_PCS_RW_data[] =
11888   {"Absolute", "PC-relative", "SB-relative", "None"};
11889 static const char * arm_attr_tag_ABI_PCS_RO_data[] =
11890   {"Absolute", "PC-relative", "None"};
11891 static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
11892   {"None", "direct", "GOT-indirect"};
11893 static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
11894   {"None", "??? 1", "2", "??? 3", "4"};
11895 static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
11896 static const char * arm_attr_tag_ABI_FP_denormal[] =
11897   {"Unused", "Needed", "Sign only"};
11898 static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
11899 static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
11900 static const char * arm_attr_tag_ABI_FP_number_model[] =
11901   {"Unused", "Finite", "RTABI", "IEEE 754"};
11902 static const char * arm_attr_tag_ABI_enum_size[] =
11903   {"Unused", "small", "int", "forced to int"};
11904 static const char * arm_attr_tag_ABI_HardFP_use[] =
11905   {"As Tag_FP_arch", "SP only", "DP only", "SP and DP"};
11906 static const char * arm_attr_tag_ABI_VFP_args[] =
11907   {"AAPCS", "VFP registers", "custom", "compatible"};
11908 static const char * arm_attr_tag_ABI_WMMX_args[] =
11909   {"AAPCS", "WMMX registers", "custom"};
11910 static const char * arm_attr_tag_ABI_optimization_goals[] =
11911   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
11912     "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
11913 static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
11914   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
11915     "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
11916 static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
11917 static const char * arm_attr_tag_FP_HP_extension[] =
11918   {"Not Allowed", "Allowed"};
11919 static const char * arm_attr_tag_ABI_FP_16bit_format[] =
11920   {"None", "IEEE 754", "Alternative Format"};
11921 static const char * arm_attr_tag_MPextension_use[] =
11922   {"Not Allowed", "Allowed"};
11923 static const char * arm_attr_tag_DIV_use[] =
11924   {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
11925     "Allowed in v7-A with integer division extension"};
11926 static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
11927 static const char * arm_attr_tag_Virtualization_use[] =
11928   {"Not Allowed", "TrustZone", "Virtualization Extensions",
11929     "TrustZone and Virtualization Extensions"};
11930 static const char * arm_attr_tag_MPextension_use_legacy[] =
11931   {"Not Allowed", "Allowed"};
11932 
11933 #define LOOKUP(id, name) \
11934   {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
11935 static arm_attr_public_tag arm_attr_public_tags[] =
11936 {
11937   {4, "CPU_raw_name", 1, NULL},
11938   {5, "CPU_name", 1, NULL},
11939   LOOKUP(6, CPU_arch),
11940   {7, "CPU_arch_profile", 0, NULL},
11941   LOOKUP(8, ARM_ISA_use),
11942   LOOKUP(9, THUMB_ISA_use),
11943   LOOKUP(10, FP_arch),
11944   LOOKUP(11, WMMX_arch),
11945   LOOKUP(12, Advanced_SIMD_arch),
11946   LOOKUP(13, PCS_config),
11947   LOOKUP(14, ABI_PCS_R9_use),
11948   LOOKUP(15, ABI_PCS_RW_data),
11949   LOOKUP(16, ABI_PCS_RO_data),
11950   LOOKUP(17, ABI_PCS_GOT_use),
11951   LOOKUP(18, ABI_PCS_wchar_t),
11952   LOOKUP(19, ABI_FP_rounding),
11953   LOOKUP(20, ABI_FP_denormal),
11954   LOOKUP(21, ABI_FP_exceptions),
11955   LOOKUP(22, ABI_FP_user_exceptions),
11956   LOOKUP(23, ABI_FP_number_model),
11957   {24, "ABI_align_needed", 0, NULL},
11958   {25, "ABI_align_preserved", 0, NULL},
11959   LOOKUP(26, ABI_enum_size),
11960   LOOKUP(27, ABI_HardFP_use),
11961   LOOKUP(28, ABI_VFP_args),
11962   LOOKUP(29, ABI_WMMX_args),
11963   LOOKUP(30, ABI_optimization_goals),
11964   LOOKUP(31, ABI_FP_optimization_goals),
11965   {32, "compatibility", 0, NULL},
11966   LOOKUP(34, CPU_unaligned_access),
11967   LOOKUP(36, FP_HP_extension),
11968   LOOKUP(38, ABI_FP_16bit_format),
11969   LOOKUP(42, MPextension_use),
11970   LOOKUP(44, DIV_use),
11971   {64, "nodefaults", 0, NULL},
11972   {65, "also_compatible_with", 0, NULL},
11973   LOOKUP(66, T2EE_use),
11974   {67, "conformance", 1, NULL},
11975   LOOKUP(68, Virtualization_use),
11976   LOOKUP(70, MPextension_use_legacy)
11977 };
11978 #undef LOOKUP
11979 
11980 static unsigned char *
display_arm_attribute(unsigned char * p,const unsigned char * const end)11981 display_arm_attribute (unsigned char * p,
11982 		       const unsigned char * const end)
11983 {
11984   unsigned int tag;
11985   unsigned int len;
11986   unsigned int val;
11987   arm_attr_public_tag * attr;
11988   unsigned i;
11989   unsigned int type;
11990 
11991   tag = read_uleb128 (p, &len, end);
11992   p += len;
11993   attr = NULL;
11994   for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
11995     {
11996       if (arm_attr_public_tags[i].tag == tag)
11997 	{
11998 	  attr = &arm_attr_public_tags[i];
11999 	  break;
12000 	}
12001     }
12002 
12003   if (attr)
12004     {
12005       printf ("  Tag_%s: ", attr->name);
12006       switch (attr->type)
12007 	{
12008 	case 0:
12009 	  switch (tag)
12010 	    {
12011 	    case 7: /* Tag_CPU_arch_profile.  */
12012 	      val = read_uleb128 (p, &len, end);
12013 	      p += len;
12014 	      switch (val)
12015 		{
12016 		case 0: printf (_("None\n")); break;
12017 		case 'A': printf (_("Application\n")); break;
12018 		case 'R': printf (_("Realtime\n")); break;
12019 		case 'M': printf (_("Microcontroller\n")); break;
12020 		case 'S': printf (_("Application or Realtime\n")); break;
12021 		default: printf ("??? (%d)\n", val); break;
12022 		}
12023 	      break;
12024 
12025 	    case 24: /* Tag_align_needed.  */
12026 	      val = read_uleb128 (p, &len, end);
12027 	      p += len;
12028 	      switch (val)
12029 		{
12030 		case 0: printf (_("None\n")); break;
12031 		case 1: printf (_("8-byte\n")); break;
12032 		case 2: printf (_("4-byte\n")); break;
12033 		case 3: printf ("??? 3\n"); break;
12034 		default:
12035 		  if (val <= 12)
12036 		    printf (_("8-byte and up to %d-byte extended\n"),
12037 			    1 << val);
12038 		  else
12039 		    printf ("??? (%d)\n", val);
12040 		  break;
12041 		}
12042 	      break;
12043 
12044 	    case 25: /* Tag_align_preserved.  */
12045 	      val = read_uleb128 (p, &len, end);
12046 	      p += len;
12047 	      switch (val)
12048 		{
12049 		case 0: printf (_("None\n")); break;
12050 		case 1: printf (_("8-byte, except leaf SP\n")); break;
12051 		case 2: printf (_("8-byte\n")); break;
12052 		case 3: printf ("??? 3\n"); break;
12053 		default:
12054 		  if (val <= 12)
12055 		    printf (_("8-byte and up to %d-byte extended\n"),
12056 			    1 << val);
12057 		  else
12058 		    printf ("??? (%d)\n", val);
12059 		  break;
12060 		}
12061 	      break;
12062 
12063 	    case 32: /* Tag_compatibility.  */
12064 	      {
12065 		val = read_uleb128 (p, &len, end);
12066 		p += len;
12067 		printf (_("flag = %d, vendor = "), val);
12068 		if (p < end - 1)
12069 		  {
12070 		    size_t maxlen = (end - p) - 1;
12071 
12072 		    print_symbol ((int) maxlen, (const char *) p);
12073 		    p += strnlen ((char *) p, maxlen) + 1;
12074 		  }
12075 		else
12076 		  {
12077 		    printf (_("<corrupt>"));
12078 		    p = (unsigned char *) end;
12079 		  }
12080 		putchar ('\n');
12081 	      }
12082 	      break;
12083 
12084 	    case 64: /* Tag_nodefaults.  */
12085 	      /* PR 17531: file: 001-505008-0.01.  */
12086 	      if (p < end)
12087 		p++;
12088 	      printf (_("True\n"));
12089 	      break;
12090 
12091 	    case 65: /* Tag_also_compatible_with.  */
12092 	      val = read_uleb128 (p, &len, end);
12093 	      p += len;
12094 	      if (val == 6 /* Tag_CPU_arch.  */)
12095 		{
12096 		  val = read_uleb128 (p, &len, end);
12097 		  p += len;
12098 		  if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
12099 		    printf ("??? (%d)\n", val);
12100 		  else
12101 		    printf ("%s\n", arm_attr_tag_CPU_arch[val]);
12102 		}
12103 	      else
12104 		printf ("???\n");
12105 	      while (p < end && *(p++) != '\0' /* NUL terminator.  */)
12106 		;
12107 	      break;
12108 
12109 	    default:
12110 	      abort ();
12111 	    }
12112 	  return p;
12113 
12114 	case 1:
12115 	  return display_tag_value (-1, p, end);
12116 	case 2:
12117 	  return display_tag_value (0, p, end);
12118 
12119 	default:
12120 	  assert (attr->type & 0x80);
12121 	  val = read_uleb128 (p, &len, end);
12122 	  p += len;
12123 	  type = attr->type & 0x7f;
12124 	  if (val >= type)
12125 	    printf ("??? (%d)\n", val);
12126 	  else
12127 	    printf ("%s\n", attr->table[val]);
12128 	  return p;
12129 	}
12130     }
12131 
12132   return display_tag_value (tag, p, end);
12133 }
12134 
12135 static unsigned char *
display_gnu_attribute(unsigned char * p,unsigned char * (* display_proc_gnu_attribute)(unsigned char *,int,const unsigned char * const),const unsigned char * const end)12136 display_gnu_attribute (unsigned char * p,
12137 		       unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int, const unsigned char * const),
12138 		       const unsigned char * const end)
12139 {
12140   int tag;
12141   unsigned int len;
12142   int val;
12143 
12144   tag = read_uleb128 (p, &len, end);
12145   p += len;
12146 
12147   /* Tag_compatibility is the only generic GNU attribute defined at
12148      present.  */
12149   if (tag == 32)
12150     {
12151       val = read_uleb128 (p, &len, end);
12152       p += len;
12153 
12154       printf (_("flag = %d, vendor = "), val);
12155       if (p == end)
12156 	{
12157 	  printf (_("<corrupt>\n"));
12158 	  warn (_("corrupt vendor attribute\n"));
12159 	}
12160       else
12161 	{
12162 	  if (p < end - 1)
12163 	    {
12164 	      size_t maxlen = (end - p) - 1;
12165 
12166 	      print_symbol ((int) maxlen, (const char *) p);
12167 	      p += strnlen ((char *) p, maxlen) + 1;
12168 	    }
12169 	  else
12170 	    {
12171 	      printf (_("<corrupt>"));
12172 	      p = (unsigned char *) end;
12173 	    }
12174 	  putchar ('\n');
12175 	}
12176       return p;
12177     }
12178 
12179   if ((tag & 2) == 0 && display_proc_gnu_attribute)
12180     return display_proc_gnu_attribute (p, tag, end);
12181 
12182   return display_tag_value (tag, p, end);
12183 }
12184 
12185 static unsigned char *
display_power_gnu_attribute(unsigned char * p,int tag,const unsigned char * const end)12186 display_power_gnu_attribute (unsigned char * p,
12187 			     int tag,
12188 			     const unsigned char * const end)
12189 {
12190   unsigned int len;
12191   int val;
12192 
12193   if (tag == Tag_GNU_Power_ABI_FP)
12194     {
12195       val = read_uleb128 (p, &len, end);
12196       p += len;
12197       printf ("  Tag_GNU_Power_ABI_FP: ");
12198 
12199       switch (val)
12200 	{
12201 	case 0:
12202 	  printf (_("Hard or soft float\n"));
12203 	  break;
12204 	case 1:
12205 	  printf (_("Hard float\n"));
12206 	  break;
12207 	case 2:
12208 	  printf (_("Soft float\n"));
12209 	  break;
12210 	case 3:
12211 	  printf (_("Single-precision hard float\n"));
12212 	  break;
12213 	default:
12214 	  printf ("??? (%d)\n", val);
12215 	  break;
12216 	}
12217       return p;
12218    }
12219 
12220   if (tag == Tag_GNU_Power_ABI_Vector)
12221     {
12222       val = read_uleb128 (p, &len, end);
12223       p += len;
12224       printf ("  Tag_GNU_Power_ABI_Vector: ");
12225       switch (val)
12226 	{
12227 	case 0:
12228 	  printf (_("Any\n"));
12229 	  break;
12230 	case 1:
12231 	  printf (_("Generic\n"));
12232 	  break;
12233 	case 2:
12234 	  printf ("AltiVec\n");
12235 	  break;
12236 	case 3:
12237 	  printf ("SPE\n");
12238 	  break;
12239 	default:
12240 	  printf ("??? (%d)\n", val);
12241 	  break;
12242 	}
12243       return p;
12244    }
12245 
12246   if (tag == Tag_GNU_Power_ABI_Struct_Return)
12247     {
12248       if (p == end)
12249 	{
12250 	  warn (_("corrupt Tag_GNU_Power_ABI_Struct_Return\n"));
12251 	  return p;
12252 	}
12253 
12254       val = read_uleb128 (p, &len, end);
12255       p += len;
12256       printf ("  Tag_GNU_Power_ABI_Struct_Return: ");
12257       switch (val)
12258        {
12259        case 0:
12260          printf (_("Any\n"));
12261          break;
12262        case 1:
12263          printf ("r3/r4\n");
12264          break;
12265        case 2:
12266          printf (_("Memory\n"));
12267          break;
12268        default:
12269          printf ("??? (%d)\n", val);
12270          break;
12271        }
12272       return p;
12273     }
12274 
12275   return display_tag_value (tag & 1, p, end);
12276 }
12277 
12278 static void
display_sparc_hwcaps(int mask)12279 display_sparc_hwcaps (int mask)
12280 {
12281   if (mask)
12282     {
12283       int first = 1;
12284 
12285       if (mask & ELF_SPARC_HWCAP_MUL32)
12286 	fputs ("mul32", stdout), first = 0;
12287       if (mask & ELF_SPARC_HWCAP_DIV32)
12288 	printf ("%sdiv32", first ? "" : "|"), first = 0;
12289       if (mask & ELF_SPARC_HWCAP_FSMULD)
12290 	printf ("%sfsmuld", first ? "" : "|"), first = 0;
12291       if (mask & ELF_SPARC_HWCAP_V8PLUS)
12292 	printf ("%sv8plus", first ? "" : "|"), first = 0;
12293       if (mask & ELF_SPARC_HWCAP_POPC)
12294 	printf ("%spopc", first ? "" : "|"), first = 0;
12295       if (mask & ELF_SPARC_HWCAP_VIS)
12296 	printf ("%svis", first ? "" : "|"), first = 0;
12297       if (mask & ELF_SPARC_HWCAP_VIS2)
12298 	printf ("%svis2", first ? "" : "|"), first = 0;
12299       if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
12300 	printf ("%sASIBlkInit", first ? "" : "|"), first = 0;
12301       if (mask & ELF_SPARC_HWCAP_FMAF)
12302 	printf ("%sfmaf", first ? "" : "|"), first = 0;
12303       if (mask & ELF_SPARC_HWCAP_VIS3)
12304 	printf ("%svis3", first ? "" : "|"), first = 0;
12305       if (mask & ELF_SPARC_HWCAP_HPC)
12306 	printf ("%shpc", first ? "" : "|"), first = 0;
12307       if (mask & ELF_SPARC_HWCAP_RANDOM)
12308 	printf ("%srandom", first ? "" : "|"), first = 0;
12309       if (mask & ELF_SPARC_HWCAP_TRANS)
12310 	printf ("%strans", first ? "" : "|"), first = 0;
12311       if (mask & ELF_SPARC_HWCAP_FJFMAU)
12312 	printf ("%sfjfmau", first ? "" : "|"), first = 0;
12313       if (mask & ELF_SPARC_HWCAP_IMA)
12314 	printf ("%sima", first ? "" : "|"), first = 0;
12315       if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
12316 	printf ("%scspare", first ? "" : "|"), first = 0;
12317     }
12318   else
12319     fputc ('0', stdout);
12320   fputc ('\n', stdout);
12321 }
12322 
12323 static void
display_sparc_hwcaps2(int mask)12324 display_sparc_hwcaps2 (int mask)
12325 {
12326   if (mask)
12327     {
12328       int first = 1;
12329 
12330       if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
12331 	fputs ("fjathplus", stdout), first = 0;
12332       if (mask & ELF_SPARC_HWCAP2_VIS3B)
12333 	printf ("%svis3b", first ? "" : "|"), first = 0;
12334       if (mask & ELF_SPARC_HWCAP2_ADP)
12335 	printf ("%sadp", first ? "" : "|"), first = 0;
12336       if (mask & ELF_SPARC_HWCAP2_SPARC5)
12337 	printf ("%ssparc5", first ? "" : "|"), first = 0;
12338       if (mask & ELF_SPARC_HWCAP2_MWAIT)
12339 	printf ("%smwait", first ? "" : "|"), first = 0;
12340       if (mask & ELF_SPARC_HWCAP2_XMPMUL)
12341 	printf ("%sxmpmul", first ? "" : "|"), first = 0;
12342       if (mask & ELF_SPARC_HWCAP2_XMONT)
12343 	printf ("%sxmont2", first ? "" : "|"), first = 0;
12344       if (mask & ELF_SPARC_HWCAP2_NSEC)
12345 	printf ("%snsec", first ? "" : "|"), first = 0;
12346       if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
12347 	printf ("%sfjathhpc", first ? "" : "|"), first = 0;
12348       if (mask & ELF_SPARC_HWCAP2_FJDES)
12349 	printf ("%sfjdes", first ? "" : "|"), first = 0;
12350       if (mask & ELF_SPARC_HWCAP2_FJAES)
12351 	printf ("%sfjaes", first ? "" : "|"), first = 0;
12352     }
12353   else
12354     fputc ('0', stdout);
12355   fputc ('\n', stdout);
12356 }
12357 
12358 static unsigned char *
display_sparc_gnu_attribute(unsigned char * p,int tag,const unsigned char * const end)12359 display_sparc_gnu_attribute (unsigned char * p,
12360 			     int tag,
12361 			     const unsigned char * const end)
12362 {
12363   unsigned int len;
12364   int val;
12365 
12366   if (tag == Tag_GNU_Sparc_HWCAPS)
12367     {
12368       val = read_uleb128 (p, &len, end);
12369       p += len;
12370       printf ("  Tag_GNU_Sparc_HWCAPS: ");
12371       display_sparc_hwcaps (val);
12372       return p;
12373     }
12374   if (tag == Tag_GNU_Sparc_HWCAPS2)
12375     {
12376       val = read_uleb128 (p, &len, end);
12377       p += len;
12378       printf ("  Tag_GNU_Sparc_HWCAPS2: ");
12379       display_sparc_hwcaps2 (val);
12380       return p;
12381     }
12382 
12383   return display_tag_value (tag, p, end);
12384 }
12385 
12386 static void
print_mips_fp_abi_value(int val)12387 print_mips_fp_abi_value (int val)
12388 {
12389   switch (val)
12390     {
12391     case Val_GNU_MIPS_ABI_FP_ANY:
12392       printf (_("Hard or soft float\n"));
12393       break;
12394     case Val_GNU_MIPS_ABI_FP_DOUBLE:
12395       printf (_("Hard float (double precision)\n"));
12396       break;
12397     case Val_GNU_MIPS_ABI_FP_SINGLE:
12398       printf (_("Hard float (single precision)\n"));
12399       break;
12400     case Val_GNU_MIPS_ABI_FP_SOFT:
12401       printf (_("Soft float\n"));
12402       break;
12403     case Val_GNU_MIPS_ABI_FP_OLD_64:
12404       printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
12405       break;
12406     case Val_GNU_MIPS_ABI_FP_XX:
12407       printf (_("Hard float (32-bit CPU, Any FPU)\n"));
12408       break;
12409     case Val_GNU_MIPS_ABI_FP_64:
12410       printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
12411       break;
12412     case Val_GNU_MIPS_ABI_FP_64A:
12413       printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
12414       break;
12415     default:
12416       printf ("??? (%d)\n", val);
12417       break;
12418     }
12419 }
12420 
12421 static unsigned char *
display_mips_gnu_attribute(unsigned char * p,int tag,const unsigned char * const end)12422 display_mips_gnu_attribute (unsigned char * p,
12423 			    int tag,
12424 			    const unsigned char * const end)
12425 {
12426   if (tag == Tag_GNU_MIPS_ABI_FP)
12427     {
12428       unsigned int len;
12429       int val;
12430 
12431       val = read_uleb128 (p, &len, end);
12432       p += len;
12433       printf ("  Tag_GNU_MIPS_ABI_FP: ");
12434 
12435       print_mips_fp_abi_value (val);
12436 
12437       return p;
12438    }
12439 
12440   if (tag == Tag_GNU_MIPS_ABI_MSA)
12441     {
12442       unsigned int len;
12443       int val;
12444 
12445       val = read_uleb128 (p, &len, end);
12446       p += len;
12447       printf ("  Tag_GNU_MIPS_ABI_MSA: ");
12448 
12449       switch (val)
12450 	{
12451 	case Val_GNU_MIPS_ABI_MSA_ANY:
12452 	  printf (_("Any MSA or not\n"));
12453 	  break;
12454 	case Val_GNU_MIPS_ABI_MSA_128:
12455 	  printf (_("128-bit MSA\n"));
12456 	  break;
12457 	default:
12458 	  printf ("??? (%d)\n", val);
12459 	  break;
12460 	}
12461       return p;
12462     }
12463 
12464   return display_tag_value (tag & 1, p, end);
12465 }
12466 
12467 static unsigned char *
display_tic6x_attribute(unsigned char * p,const unsigned char * const end)12468 display_tic6x_attribute (unsigned char * p,
12469 			 const unsigned char * const end)
12470 {
12471   int tag;
12472   unsigned int len;
12473   int val;
12474 
12475   tag = read_uleb128 (p, &len, end);
12476   p += len;
12477 
12478   switch (tag)
12479     {
12480     case Tag_ISA:
12481       val = read_uleb128 (p, &len, end);
12482       p += len;
12483       printf ("  Tag_ISA: ");
12484 
12485       switch (val)
12486 	{
12487 	case C6XABI_Tag_ISA_none:
12488 	  printf (_("None\n"));
12489 	  break;
12490 	case C6XABI_Tag_ISA_C62X:
12491 	  printf ("C62x\n");
12492 	  break;
12493 	case C6XABI_Tag_ISA_C67X:
12494 	  printf ("C67x\n");
12495 	  break;
12496 	case C6XABI_Tag_ISA_C67XP:
12497 	  printf ("C67x+\n");
12498 	  break;
12499 	case C6XABI_Tag_ISA_C64X:
12500 	  printf ("C64x\n");
12501 	  break;
12502 	case C6XABI_Tag_ISA_C64XP:
12503 	  printf ("C64x+\n");
12504 	  break;
12505 	case C6XABI_Tag_ISA_C674X:
12506 	  printf ("C674x\n");
12507 	  break;
12508 	default:
12509 	  printf ("??? (%d)\n", val);
12510 	  break;
12511 	}
12512       return p;
12513 
12514     case Tag_ABI_wchar_t:
12515       val = read_uleb128 (p, &len, end);
12516       p += len;
12517       printf ("  Tag_ABI_wchar_t: ");
12518       switch (val)
12519 	{
12520 	case 0:
12521 	  printf (_("Not used\n"));
12522 	  break;
12523 	case 1:
12524 	  printf (_("2 bytes\n"));
12525 	  break;
12526 	case 2:
12527 	  printf (_("4 bytes\n"));
12528 	  break;
12529 	default:
12530 	  printf ("??? (%d)\n", val);
12531 	  break;
12532 	}
12533       return p;
12534 
12535     case Tag_ABI_stack_align_needed:
12536       val = read_uleb128 (p, &len, end);
12537       p += len;
12538       printf ("  Tag_ABI_stack_align_needed: ");
12539       switch (val)
12540 	{
12541 	case 0:
12542 	  printf (_("8-byte\n"));
12543 	  break;
12544 	case 1:
12545 	  printf (_("16-byte\n"));
12546 	  break;
12547 	default:
12548 	  printf ("??? (%d)\n", val);
12549 	  break;
12550 	}
12551       return p;
12552 
12553     case Tag_ABI_stack_align_preserved:
12554       val = read_uleb128 (p, &len, end);
12555       p += len;
12556       printf ("  Tag_ABI_stack_align_preserved: ");
12557       switch (val)
12558 	{
12559 	case 0:
12560 	  printf (_("8-byte\n"));
12561 	  break;
12562 	case 1:
12563 	  printf (_("16-byte\n"));
12564 	  break;
12565 	default:
12566 	  printf ("??? (%d)\n", val);
12567 	  break;
12568 	}
12569       return p;
12570 
12571     case Tag_ABI_DSBT:
12572       val = read_uleb128 (p, &len, end);
12573       p += len;
12574       printf ("  Tag_ABI_DSBT: ");
12575       switch (val)
12576 	{
12577 	case 0:
12578 	  printf (_("DSBT addressing not used\n"));
12579 	  break;
12580 	case 1:
12581 	  printf (_("DSBT addressing used\n"));
12582 	  break;
12583 	default:
12584 	  printf ("??? (%d)\n", val);
12585 	  break;
12586 	}
12587       return p;
12588 
12589     case Tag_ABI_PID:
12590       val = read_uleb128 (p, &len, end);
12591       p += len;
12592       printf ("  Tag_ABI_PID: ");
12593       switch (val)
12594 	{
12595 	case 0:
12596 	  printf (_("Data addressing position-dependent\n"));
12597 	  break;
12598 	case 1:
12599 	  printf (_("Data addressing position-independent, GOT near DP\n"));
12600 	  break;
12601 	case 2:
12602 	  printf (_("Data addressing position-independent, GOT far from DP\n"));
12603 	  break;
12604 	default:
12605 	  printf ("??? (%d)\n", val);
12606 	  break;
12607 	}
12608       return p;
12609 
12610     case Tag_ABI_PIC:
12611       val = read_uleb128 (p, &len, end);
12612       p += len;
12613       printf ("  Tag_ABI_PIC: ");
12614       switch (val)
12615 	{
12616 	case 0:
12617 	  printf (_("Code addressing position-dependent\n"));
12618 	  break;
12619 	case 1:
12620 	  printf (_("Code addressing position-independent\n"));
12621 	  break;
12622 	default:
12623 	  printf ("??? (%d)\n", val);
12624 	  break;
12625 	}
12626       return p;
12627 
12628     case Tag_ABI_array_object_alignment:
12629       val = read_uleb128 (p, &len, end);
12630       p += len;
12631       printf ("  Tag_ABI_array_object_alignment: ");
12632       switch (val)
12633 	{
12634 	case 0:
12635 	  printf (_("8-byte\n"));
12636 	  break;
12637 	case 1:
12638 	  printf (_("4-byte\n"));
12639 	  break;
12640 	case 2:
12641 	  printf (_("16-byte\n"));
12642 	  break;
12643 	default:
12644 	  printf ("??? (%d)\n", val);
12645 	  break;
12646 	}
12647       return p;
12648 
12649     case Tag_ABI_array_object_align_expected:
12650       val = read_uleb128 (p, &len, end);
12651       p += len;
12652       printf ("  Tag_ABI_array_object_align_expected: ");
12653       switch (val)
12654 	{
12655 	case 0:
12656 	  printf (_("8-byte\n"));
12657 	  break;
12658 	case 1:
12659 	  printf (_("4-byte\n"));
12660 	  break;
12661 	case 2:
12662 	  printf (_("16-byte\n"));
12663 	  break;
12664 	default:
12665 	  printf ("??? (%d)\n", val);
12666 	  break;
12667 	}
12668       return p;
12669 
12670     case Tag_ABI_compatibility:
12671       {
12672 	val = read_uleb128 (p, &len, end);
12673 	p += len;
12674 	printf ("  Tag_ABI_compatibility: ");
12675 	printf (_("flag = %d, vendor = "), val);
12676 	if (p < end - 1)
12677 	  {
12678 	    size_t maxlen = (end - p) - 1;
12679 
12680 	    print_symbol ((int) maxlen, (const char *) p);
12681 	    p += strnlen ((char *) p, maxlen) + 1;
12682 	  }
12683 	else
12684 	  {
12685 	    printf (_("<corrupt>"));
12686 	    p = (unsigned char *) end;
12687 	  }
12688 	putchar ('\n');
12689 	return p;
12690       }
12691 
12692     case Tag_ABI_conformance:
12693       {
12694 	printf ("  Tag_ABI_conformance: \"");
12695 	if (p < end - 1)
12696 	  {
12697 	    size_t maxlen = (end - p) - 1;
12698 
12699 	    print_symbol ((int) maxlen, (const char *) p);
12700 	    p += strnlen ((char *) p, maxlen) + 1;
12701 	  }
12702 	else
12703 	  {
12704 	    printf (_("<corrupt>"));
12705 	    p = (unsigned char *) end;
12706 	  }
12707 	printf ("\"\n");
12708 	return p;
12709       }
12710     }
12711 
12712   return display_tag_value (tag, p, end);
12713 }
12714 
12715 static void
display_raw_attribute(unsigned char * p,unsigned char * end)12716 display_raw_attribute (unsigned char * p, unsigned char * end)
12717 {
12718   unsigned long addr = 0;
12719   size_t bytes = end - p;
12720 
12721   assert (end > p);
12722   while (bytes)
12723     {
12724       int j;
12725       int k;
12726       int lbytes = (bytes > 16 ? 16 : bytes);
12727 
12728       printf ("  0x%8.8lx ", addr);
12729 
12730       for (j = 0; j < 16; j++)
12731 	{
12732 	  if (j < lbytes)
12733 	    printf ("%2.2x", p[j]);
12734 	  else
12735 	    printf ("  ");
12736 
12737 	  if ((j & 3) == 3)
12738 	    printf (" ");
12739 	}
12740 
12741       for (j = 0; j < lbytes; j++)
12742 	{
12743 	  k = p[j];
12744 	  if (k >= ' ' && k < 0x7f)
12745 	    printf ("%c", k);
12746 	  else
12747 	    printf (".");
12748 	}
12749 
12750       putchar ('\n');
12751 
12752       p  += lbytes;
12753       bytes -= lbytes;
12754       addr += lbytes;
12755     }
12756 
12757   putchar ('\n');
12758 }
12759 
12760 static unsigned char *
display_msp430x_attribute(unsigned char * p,const unsigned char * const end)12761 display_msp430x_attribute (unsigned char * p,
12762 			   const unsigned char * const end)
12763 {
12764   unsigned int len;
12765   int val;
12766   int tag;
12767 
12768   tag = read_uleb128 (p, & len, end);
12769   p += len;
12770 
12771   switch (tag)
12772     {
12773     case OFBA_MSPABI_Tag_ISA:
12774       val = read_uleb128 (p, &len, end);
12775       p += len;
12776       printf ("  Tag_ISA: ");
12777       switch (val)
12778 	{
12779 	case 0: printf (_("None\n")); break;
12780 	case 1: printf (_("MSP430\n")); break;
12781 	case 2: printf (_("MSP430X\n")); break;
12782 	default: printf ("??? (%d)\n", val); break;
12783 	}
12784       break;
12785 
12786     case OFBA_MSPABI_Tag_Code_Model:
12787       val = read_uleb128 (p, &len, end);
12788       p += len;
12789       printf ("  Tag_Code_Model: ");
12790       switch (val)
12791 	{
12792 	case 0: printf (_("None\n")); break;
12793 	case 1: printf (_("Small\n")); break;
12794 	case 2: printf (_("Large\n")); break;
12795 	default: printf ("??? (%d)\n", val); break;
12796 	}
12797       break;
12798 
12799     case OFBA_MSPABI_Tag_Data_Model:
12800       val = read_uleb128 (p, &len, end);
12801       p += len;
12802       printf ("  Tag_Data_Model: ");
12803       switch (val)
12804 	{
12805 	case 0: printf (_("None\n")); break;
12806 	case 1: printf (_("Small\n")); break;
12807 	case 2: printf (_("Large\n")); break;
12808 	case 3: printf (_("Restricted Large\n")); break;
12809 	default: printf ("??? (%d)\n", val); break;
12810 	}
12811       break;
12812 
12813     default:
12814       printf (_("  <unknown tag %d>: "), tag);
12815 
12816       if (tag & 1)
12817 	{
12818 	  putchar ('"');
12819 	  if (p < end - 1)
12820 	    {
12821 	      size_t maxlen = (end - p) - 1;
12822 
12823 	      print_symbol ((int) maxlen, (const char *) p);
12824 	      p += strnlen ((char *) p, maxlen) + 1;
12825 	    }
12826 	  else
12827 	    {
12828 	      printf (_("<corrupt>"));
12829 	      p = (unsigned char *) end;
12830 	    }
12831 	  printf ("\"\n");
12832 	}
12833       else
12834 	{
12835 	  val = read_uleb128 (p, &len, end);
12836 	  p += len;
12837 	  printf ("%d (0x%x)\n", val, val);
12838 	}
12839       break;
12840    }
12841 
12842   assert (p <= end);
12843   return p;
12844 }
12845 
12846 static int
process_attributes(FILE * file,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 *,int,const unsigned char * const))12847 process_attributes (FILE * file,
12848 		    const char * public_name,
12849 		    unsigned int proc_type,
12850 		    unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
12851 		    unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int, const unsigned char * const))
12852 {
12853   Elf_Internal_Shdr * sect;
12854   unsigned i;
12855 
12856   /* Find the section header so that we get the size.  */
12857   for (i = 0, sect = section_headers;
12858        i < elf_header.e_shnum;
12859        i++, sect++)
12860     {
12861       unsigned char * contents;
12862       unsigned char * p;
12863 
12864       if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
12865 	continue;
12866 
12867       contents = (unsigned char *) get_data (NULL, file, sect->sh_offset, 1,
12868                                              sect->sh_size, _("attributes"));
12869       if (contents == NULL)
12870 	continue;
12871 
12872       p = contents;
12873       if (*p == 'A')
12874 	{
12875 	  bfd_vma section_len;
12876 
12877 	  section_len = sect->sh_size - 1;
12878 	  p++;
12879 
12880 	  while (section_len > 0)
12881 	    {
12882 	      bfd_vma attr_len;
12883 	      unsigned int namelen;
12884 	      bfd_boolean public_section;
12885 	      bfd_boolean gnu_section;
12886 
12887 	      if (section_len <= 4)
12888 		{
12889 		  error (_("Tag section ends prematurely\n"));
12890 		  break;
12891 		}
12892 	      attr_len = byte_get (p, 4);
12893 	      p += 4;
12894 
12895 	      if (attr_len > section_len)
12896 		{
12897 		  error (_("Bad attribute length (%u > %u)\n"),
12898 			  (unsigned) attr_len, (unsigned) section_len);
12899 		  attr_len = section_len;
12900 		}
12901 	      /* PR 17531: file: 001-101425-0.004  */
12902 	      else if (attr_len < 5)
12903 		{
12904 		  error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
12905 		  break;
12906 		}
12907 
12908 	      section_len -= attr_len;
12909 	      attr_len -= 4;
12910 
12911 	      namelen = strnlen ((char *) p, attr_len) + 1;
12912 	      if (namelen == 0 || namelen >= attr_len)
12913 		{
12914 		  error (_("Corrupt attribute section name\n"));
12915 		  break;
12916 		}
12917 
12918 	      printf (_("Attribute Section: "));
12919 	      print_symbol (INT_MAX, (const char *) p);
12920 	      putchar ('\n');
12921 
12922 	      if (public_name && streq ((char *) p, public_name))
12923 		public_section = TRUE;
12924 	      else
12925 		public_section = FALSE;
12926 
12927 	      if (streq ((char *) p, "gnu"))
12928 		gnu_section = TRUE;
12929 	      else
12930 		gnu_section = FALSE;
12931 
12932 	      p += namelen;
12933 	      attr_len -= namelen;
12934 
12935 	      while (attr_len > 0 && p < contents + sect->sh_size)
12936 		{
12937 		  int tag;
12938 		  int val;
12939 		  bfd_vma size;
12940 		  unsigned char * end;
12941 
12942 		  /* PR binutils/17531: Safe handling of corrupt files.  */
12943 		  if (attr_len < 6)
12944 		    {
12945 		      error (_("Unused bytes at end of section\n"));
12946 		      section_len = 0;
12947 		      break;
12948 		    }
12949 
12950 		  tag = *(p++);
12951 		  size = byte_get (p, 4);
12952 		  if (size > attr_len)
12953 		    {
12954 		      error (_("Bad subsection length (%u > %u)\n"),
12955 			      (unsigned) size, (unsigned) attr_len);
12956 		      size = attr_len;
12957 		    }
12958 		  /* PR binutils/17531: Safe handling of corrupt files.  */
12959 		  if (size < 6)
12960 		    {
12961 		      error (_("Bad subsection length (%u < 6)\n"),
12962 			      (unsigned) size);
12963 		      section_len = 0;
12964 		      break;
12965 		    }
12966 
12967 		  attr_len -= size;
12968 		  end = p + size - 1;
12969 		  assert (end <= contents + sect->sh_size);
12970 		  p += 4;
12971 
12972 		  switch (tag)
12973 		    {
12974 		    case 1:
12975 		      printf (_("File Attributes\n"));
12976 		      break;
12977 		    case 2:
12978 		      printf (_("Section Attributes:"));
12979 		      goto do_numlist;
12980 		    case 3:
12981 		      printf (_("Symbol Attributes:"));
12982 		    do_numlist:
12983 		      for (;;)
12984 			{
12985 			  unsigned int j;
12986 
12987 			  val = read_uleb128 (p, &j, end);
12988 			  p += j;
12989 			  if (val == 0)
12990 			    break;
12991 			  printf (" %d", val);
12992 			}
12993 		      printf ("\n");
12994 		      break;
12995 		    default:
12996 		      printf (_("Unknown tag: %d\n"), tag);
12997 		      public_section = FALSE;
12998 		      break;
12999 		    }
13000 
13001 		  if (public_section && display_pub_attribute != NULL)
13002 		    {
13003 		      while (p < end)
13004 			p = display_pub_attribute (p, end);
13005 		      assert (p <= end);
13006 		    }
13007 		  else if (gnu_section && display_proc_gnu_attribute != NULL)
13008 		    {
13009 		      while (p < end)
13010 			p = display_gnu_attribute (p,
13011 						   display_proc_gnu_attribute,
13012 						   end);
13013 		      assert (p <= end);
13014 		    }
13015 		  else if (p < end)
13016 		    {
13017 		      printf (_("  Unknown attribute:\n"));
13018 		      display_raw_attribute (p, end);
13019 		      p = end;
13020 		    }
13021 		  else
13022 		    attr_len = 0;
13023 		}
13024 	    }
13025 	}
13026       else
13027 	printf (_("Unknown format '%c' (%d)\n"), *p, *p);
13028 
13029       free (contents);
13030     }
13031   return 1;
13032 }
13033 
13034 static int
process_arm_specific(FILE * file)13035 process_arm_specific (FILE * file)
13036 {
13037   return process_attributes (file, "aeabi", SHT_ARM_ATTRIBUTES,
13038 			     display_arm_attribute, NULL);
13039 }
13040 
13041 static int
process_power_specific(FILE * file)13042 process_power_specific (FILE * file)
13043 {
13044   return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
13045 			     display_power_gnu_attribute);
13046 }
13047 
13048 static int
process_sparc_specific(FILE * file)13049 process_sparc_specific (FILE * file)
13050 {
13051   return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
13052 			     display_sparc_gnu_attribute);
13053 }
13054 
13055 static int
process_tic6x_specific(FILE * file)13056 process_tic6x_specific (FILE * file)
13057 {
13058   return process_attributes (file, "c6xabi", SHT_C6000_ATTRIBUTES,
13059 			     display_tic6x_attribute, NULL);
13060 }
13061 
13062 static int
process_msp430x_specific(FILE * file)13063 process_msp430x_specific (FILE * file)
13064 {
13065   return process_attributes (file, "mspabi", SHT_MSP430_ATTRIBUTES,
13066 			     display_msp430x_attribute, NULL);
13067 }
13068 
13069 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
13070    Print the Address, Access and Initial fields of an entry at VMA ADDR
13071    and return the VMA of the next entry.  */
13072 
13073 static bfd_vma
print_mips_got_entry(unsigned char * data,bfd_vma pltgot,bfd_vma addr)13074 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
13075 {
13076   printf ("  ");
13077   print_vma (addr, LONG_HEX);
13078   printf (" ");
13079   if (addr < pltgot + 0xfff0)
13080     printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
13081   else
13082     printf ("%10s", "");
13083   printf (" ");
13084   if (data == NULL)
13085     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
13086   else
13087     {
13088       bfd_vma entry;
13089 
13090       entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
13091       print_vma (entry, LONG_HEX);
13092     }
13093   return addr + (is_32bit_elf ? 4 : 8);
13094 }
13095 
13096 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
13097    PLTGOT.  Print the Address and Initial fields of an entry at VMA
13098    ADDR and return the VMA of the next entry.  */
13099 
13100 static bfd_vma
print_mips_pltgot_entry(unsigned char * data,bfd_vma pltgot,bfd_vma addr)13101 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
13102 {
13103   printf ("  ");
13104   print_vma (addr, LONG_HEX);
13105   printf (" ");
13106   if (data == NULL)
13107     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
13108   else
13109     {
13110       bfd_vma entry;
13111 
13112       entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
13113       print_vma (entry, LONG_HEX);
13114     }
13115   return addr + (is_32bit_elf ? 4 : 8);
13116 }
13117 
13118 static void
print_mips_ases(unsigned int mask)13119 print_mips_ases (unsigned int mask)
13120 {
13121   if (mask & AFL_ASE_DSP)
13122     fputs ("\n\tDSP ASE", stdout);
13123   if (mask & AFL_ASE_DSPR2)
13124     fputs ("\n\tDSP R2 ASE", stdout);
13125   if (mask & AFL_ASE_DSPR6)
13126     fputs ("\n\tDSP R6 ASE", stdout);
13127   if (mask & AFL_ASE_EVA)
13128     fputs ("\n\tEnhanced VA Scheme", stdout);
13129   if (mask & AFL_ASE_MCU)
13130     fputs ("\n\tMCU (MicroController) ASE", stdout);
13131   if (mask & AFL_ASE_MDMX)
13132     fputs ("\n\tMDMX ASE", stdout);
13133   if (mask & AFL_ASE_MIPS3D)
13134     fputs ("\n\tMIPS-3D ASE", stdout);
13135   if (mask & AFL_ASE_MT)
13136     fputs ("\n\tMT ASE", stdout);
13137   if (mask & AFL_ASE_SMARTMIPS)
13138     fputs ("\n\tSmartMIPS ASE", stdout);
13139   if (mask & AFL_ASE_VIRT)
13140     fputs ("\n\tVZ ASE", stdout);
13141   if (mask & AFL_ASE_MSA)
13142     fputs ("\n\tMSA ASE", stdout);
13143   if (mask & AFL_ASE_MIPS16)
13144     fputs ("\n\tMIPS16 ASE", stdout);
13145   if (mask & AFL_ASE_MICROMIPS)
13146     fputs ("\n\tMICROMIPS ASE", stdout);
13147   if (mask & AFL_ASE_XPA)
13148     fputs ("\n\tXPA ASE", stdout);
13149   if (mask == 0)
13150     fprintf (stdout, "\n\t%s", _("None"));
13151   else if ((mask & ~AFL_ASE_MASK) != 0)
13152     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
13153 }
13154 
13155 static void
print_mips_isa_ext(unsigned int isa_ext)13156 print_mips_isa_ext (unsigned int isa_ext)
13157 {
13158   switch (isa_ext)
13159     {
13160     case 0:
13161       fputs (_("None"), stdout);
13162       break;
13163     case AFL_EXT_XLR:
13164       fputs ("RMI XLR", stdout);
13165       break;
13166     case AFL_EXT_OCTEON3:
13167       fputs ("Cavium Networks Octeon3", stdout);
13168       break;
13169     case AFL_EXT_OCTEON2:
13170       fputs ("Cavium Networks Octeon2", stdout);
13171       break;
13172     case AFL_EXT_OCTEONP:
13173       fputs ("Cavium Networks OcteonP", stdout);
13174       break;
13175     case AFL_EXT_LOONGSON_3A:
13176       fputs ("Loongson 3A", stdout);
13177       break;
13178     case AFL_EXT_OCTEON:
13179       fputs ("Cavium Networks Octeon", stdout);
13180       break;
13181     case AFL_EXT_5900:
13182       fputs ("Toshiba R5900", stdout);
13183       break;
13184     case AFL_EXT_4650:
13185       fputs ("MIPS R4650", stdout);
13186       break;
13187     case AFL_EXT_4010:
13188       fputs ("LSI R4010", stdout);
13189       break;
13190     case AFL_EXT_4100:
13191       fputs ("NEC VR4100", stdout);
13192       break;
13193     case AFL_EXT_3900:
13194       fputs ("Toshiba R3900", stdout);
13195       break;
13196     case AFL_EXT_10000:
13197       fputs ("MIPS R10000", stdout);
13198       break;
13199     case AFL_EXT_SB1:
13200       fputs ("Broadcom SB-1", stdout);
13201       break;
13202     case AFL_EXT_4111:
13203       fputs ("NEC VR4111/VR4181", stdout);
13204       break;
13205     case AFL_EXT_4120:
13206       fputs ("NEC VR4120", stdout);
13207       break;
13208     case AFL_EXT_5400:
13209       fputs ("NEC VR5400", stdout);
13210       break;
13211     case AFL_EXT_5500:
13212       fputs ("NEC VR5500", stdout);
13213       break;
13214     case AFL_EXT_LOONGSON_2E:
13215       fputs ("ST Microelectronics Loongson 2E", stdout);
13216       break;
13217     case AFL_EXT_LOONGSON_2F:
13218       fputs ("ST Microelectronics Loongson 2F", stdout);
13219       break;
13220     default:
13221       fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
13222     }
13223 }
13224 
13225 static int
get_mips_reg_size(int reg_size)13226 get_mips_reg_size (int reg_size)
13227 {
13228   return (reg_size == AFL_REG_NONE) ? 0
13229 	 : (reg_size == AFL_REG_32) ? 32
13230 	 : (reg_size == AFL_REG_64) ? 64
13231 	 : (reg_size == AFL_REG_128) ? 128
13232 	 : -1;
13233 }
13234 
13235 static int
process_mips_specific(FILE * file)13236 process_mips_specific (FILE * file)
13237 {
13238   Elf_Internal_Dyn * entry;
13239   Elf_Internal_Shdr *sect = NULL;
13240   size_t liblist_offset = 0;
13241   size_t liblistno = 0;
13242   size_t conflictsno = 0;
13243   size_t options_offset = 0;
13244   size_t conflicts_offset = 0;
13245   size_t pltrelsz = 0;
13246   size_t pltrel = 0;
13247   bfd_vma pltgot = 0;
13248   bfd_vma mips_pltgot = 0;
13249   bfd_vma jmprel = 0;
13250   bfd_vma local_gotno = 0;
13251   bfd_vma gotsym = 0;
13252   bfd_vma symtabno = 0;
13253 
13254   process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
13255 		      display_mips_gnu_attribute);
13256 
13257   sect = find_section (".MIPS.abiflags");
13258 
13259   if (sect != NULL)
13260     {
13261       Elf_External_ABIFlags_v0 *abiflags_ext;
13262       Elf_Internal_ABIFlags_v0 abiflags_in;
13263 
13264       if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
13265 	fputs ("\nCorrupt ABI Flags section.\n", stdout);
13266       else
13267 	{
13268 	  abiflags_ext = get_data (NULL, file, sect->sh_offset, 1,
13269 				   sect->sh_size, _("MIPS ABI Flags section"));
13270 	  if (abiflags_ext)
13271 	    {
13272 	      abiflags_in.version = BYTE_GET (abiflags_ext->version);
13273 	      abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
13274 	      abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
13275 	      abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
13276 	      abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
13277 	      abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
13278 	      abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
13279 	      abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
13280 	      abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
13281 	      abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
13282 	      abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
13283 
13284 	      printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
13285 	      printf ("\nISA: MIPS%d", abiflags_in.isa_level);
13286 	      if (abiflags_in.isa_rev > 1)
13287 		printf ("r%d", abiflags_in.isa_rev);
13288 	      printf ("\nGPR size: %d",
13289 		      get_mips_reg_size (abiflags_in.gpr_size));
13290 	      printf ("\nCPR1 size: %d",
13291 		      get_mips_reg_size (abiflags_in.cpr1_size));
13292 	      printf ("\nCPR2 size: %d",
13293 		      get_mips_reg_size (abiflags_in.cpr2_size));
13294 	      fputs ("\nFP ABI: ", stdout);
13295 	      print_mips_fp_abi_value (abiflags_in.fp_abi);
13296 	      fputs ("ISA Extension: ", stdout);
13297 	      print_mips_isa_ext (abiflags_in.isa_ext);
13298 	      fputs ("\nASEs:", stdout);
13299 	      print_mips_ases (abiflags_in.ases);
13300 	      printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
13301 	      printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
13302 	      fputc ('\n', stdout);
13303 	      free (abiflags_ext);
13304 	    }
13305 	}
13306     }
13307 
13308   /* We have a lot of special sections.  Thanks SGI!  */
13309   if (dynamic_section == NULL)
13310     /* No information available.  */
13311     return 0;
13312 
13313   for (entry = dynamic_section;
13314        /* PR 17531 file: 012-50589-0.004.  */
13315        entry < dynamic_section + dynamic_nent && entry->d_tag != DT_NULL;
13316        ++entry)
13317     switch (entry->d_tag)
13318       {
13319       case DT_MIPS_LIBLIST:
13320 	liblist_offset
13321 	  = offset_from_vma (file, entry->d_un.d_val,
13322 			     liblistno * sizeof (Elf32_External_Lib));
13323 	break;
13324       case DT_MIPS_LIBLISTNO:
13325 	liblistno = entry->d_un.d_val;
13326 	break;
13327       case DT_MIPS_OPTIONS:
13328 	options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
13329 	break;
13330       case DT_MIPS_CONFLICT:
13331 	conflicts_offset
13332 	  = offset_from_vma (file, entry->d_un.d_val,
13333 			     conflictsno * sizeof (Elf32_External_Conflict));
13334 	break;
13335       case DT_MIPS_CONFLICTNO:
13336 	conflictsno = entry->d_un.d_val;
13337 	break;
13338       case DT_PLTGOT:
13339 	pltgot = entry->d_un.d_ptr;
13340 	break;
13341       case DT_MIPS_LOCAL_GOTNO:
13342 	local_gotno = entry->d_un.d_val;
13343 	break;
13344       case DT_MIPS_GOTSYM:
13345 	gotsym = entry->d_un.d_val;
13346 	break;
13347       case DT_MIPS_SYMTABNO:
13348 	symtabno = entry->d_un.d_val;
13349 	break;
13350       case DT_MIPS_PLTGOT:
13351 	mips_pltgot = entry->d_un.d_ptr;
13352 	break;
13353       case DT_PLTREL:
13354 	pltrel = entry->d_un.d_val;
13355 	break;
13356       case DT_PLTRELSZ:
13357 	pltrelsz = entry->d_un.d_val;
13358 	break;
13359       case DT_JMPREL:
13360 	jmprel = entry->d_un.d_ptr;
13361 	break;
13362       default:
13363 	break;
13364       }
13365 
13366   if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
13367     {
13368       Elf32_External_Lib * elib;
13369       size_t cnt;
13370 
13371       elib = (Elf32_External_Lib *) get_data (NULL, file, liblist_offset,
13372                                               liblistno,
13373                                               sizeof (Elf32_External_Lib),
13374                                               _("liblist section data"));
13375       if (elib)
13376 	{
13377 	  printf (_("\nSection '.liblist' contains %lu entries:\n"),
13378 		  (unsigned long) liblistno);
13379 	  fputs (_("     Library              Time Stamp          Checksum   Version Flags\n"),
13380 		 stdout);
13381 
13382 	  for (cnt = 0; cnt < liblistno; ++cnt)
13383 	    {
13384 	      Elf32_Lib liblist;
13385 	      time_t atime;
13386 	      char timebuf[20];
13387 	      struct tm * tmp;
13388 
13389 	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
13390 	      atime = BYTE_GET (elib[cnt].l_time_stamp);
13391 	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
13392 	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
13393 	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
13394 
13395 	      tmp = gmtime (&atime);
13396 	      snprintf (timebuf, sizeof (timebuf),
13397 			"%04u-%02u-%02uT%02u:%02u:%02u",
13398 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
13399 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
13400 
13401 	      printf ("%3lu: ", (unsigned long) cnt);
13402 	      if (VALID_DYNAMIC_NAME (liblist.l_name))
13403 		print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
13404 	      else
13405 		printf (_("<corrupt: %9ld>"), liblist.l_name);
13406 	      printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
13407 		      liblist.l_version);
13408 
13409 	      if (liblist.l_flags == 0)
13410 		puts (_(" NONE"));
13411 	      else
13412 		{
13413 		  static const struct
13414 		  {
13415 		    const char * name;
13416 		    int bit;
13417 		  }
13418 		  l_flags_vals[] =
13419 		  {
13420 		    { " EXACT_MATCH", LL_EXACT_MATCH },
13421 		    { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
13422 		    { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
13423 		    { " EXPORTS", LL_EXPORTS },
13424 		    { " DELAY_LOAD", LL_DELAY_LOAD },
13425 		    { " DELTA", LL_DELTA }
13426 		  };
13427 		  int flags = liblist.l_flags;
13428 		  size_t fcnt;
13429 
13430 		  for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
13431 		    if ((flags & l_flags_vals[fcnt].bit) != 0)
13432 		      {
13433 			fputs (l_flags_vals[fcnt].name, stdout);
13434 			flags ^= l_flags_vals[fcnt].bit;
13435 		      }
13436 		  if (flags != 0)
13437 		    printf (" %#x", (unsigned int) flags);
13438 
13439 		  puts ("");
13440 		}
13441 	    }
13442 
13443 	  free (elib);
13444 	}
13445     }
13446 
13447   if (options_offset != 0)
13448     {
13449       Elf_External_Options * eopt;
13450       Elf_Internal_Options * iopt;
13451       Elf_Internal_Options * option;
13452       size_t offset;
13453       int cnt;
13454       sect = section_headers;
13455 
13456       /* Find the section header so that we get the size.  */
13457       sect = find_section_by_type (SHT_MIPS_OPTIONS);
13458       /* PR 17533 file: 012-277276-0.004.  */
13459       if (sect == NULL)
13460 	{
13461 	  error (_("No MIPS_OPTIONS header found\n"));
13462 	  return 0;
13463 	}
13464 
13465       eopt = (Elf_External_Options *) get_data (NULL, file, options_offset, 1,
13466                                                 sect->sh_size, _("options"));
13467       if (eopt)
13468 	{
13469 	  iopt = (Elf_Internal_Options *)
13470               cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (* iopt));
13471 	  if (iopt == NULL)
13472 	    {
13473 	      error (_("Out of memory allocatinf space for MIPS options\n"));
13474 	      return 0;
13475 	    }
13476 
13477 	  offset = cnt = 0;
13478 	  option = iopt;
13479 
13480 	  while (offset < sect->sh_size)
13481 	    {
13482 	      Elf_External_Options * eoption;
13483 
13484 	      eoption = (Elf_External_Options *) ((char *) eopt + offset);
13485 
13486 	      option->kind = BYTE_GET (eoption->kind);
13487 	      option->size = BYTE_GET (eoption->size);
13488 	      option->section = BYTE_GET (eoption->section);
13489 	      option->info = BYTE_GET (eoption->info);
13490 
13491 	      offset += option->size;
13492 
13493 	      ++option;
13494 	      ++cnt;
13495 	    }
13496 
13497 	  printf (_("\nSection '%s' contains %d entries:\n"),
13498 		  printable_section_name (sect), cnt);
13499 
13500 	  option = iopt;
13501 
13502 	  while (cnt-- > 0)
13503 	    {
13504 	      size_t len;
13505 
13506 	      switch (option->kind)
13507 		{
13508 		case ODK_NULL:
13509 		  /* This shouldn't happen.  */
13510 		  printf (" NULL       %d %lx", option->section, option->info);
13511 		  break;
13512 		case ODK_REGINFO:
13513 		  printf (" REGINFO    ");
13514 		  if (elf_header.e_machine == EM_MIPS)
13515 		    {
13516 		      /* 32bit form.  */
13517 		      Elf32_External_RegInfo * ereg;
13518 		      Elf32_RegInfo reginfo;
13519 
13520 		      ereg = (Elf32_External_RegInfo *) (option + 1);
13521 		      reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
13522 		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
13523 		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
13524 		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
13525 		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
13526 		      reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
13527 
13528 		      printf ("GPR %08lx  GP 0x%lx\n",
13529 			      reginfo.ri_gprmask,
13530 			      (unsigned long) reginfo.ri_gp_value);
13531 		      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
13532 			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
13533 			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
13534 		    }
13535 		  else
13536 		    {
13537 		      /* 64 bit form.  */
13538 		      Elf64_External_RegInfo * ereg;
13539 		      Elf64_Internal_RegInfo reginfo;
13540 
13541 		      ereg = (Elf64_External_RegInfo *) (option + 1);
13542 		      reginfo.ri_gprmask    = BYTE_GET (ereg->ri_gprmask);
13543 		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
13544 		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
13545 		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
13546 		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
13547 		      reginfo.ri_gp_value   = BYTE_GET (ereg->ri_gp_value);
13548 
13549 		      printf ("GPR %08lx  GP 0x",
13550 			      reginfo.ri_gprmask);
13551 		      printf_vma (reginfo.ri_gp_value);
13552 		      printf ("\n");
13553 
13554 		      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
13555 			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
13556 			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
13557 		    }
13558 		  ++option;
13559 		  continue;
13560 		case ODK_EXCEPTIONS:
13561 		  fputs (" EXCEPTIONS fpe_min(", stdout);
13562 		  process_mips_fpe_exception (option->info & OEX_FPU_MIN);
13563 		  fputs (") fpe_max(", stdout);
13564 		  process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
13565 		  fputs (")", stdout);
13566 
13567 		  if (option->info & OEX_PAGE0)
13568 		    fputs (" PAGE0", stdout);
13569 		  if (option->info & OEX_SMM)
13570 		    fputs (" SMM", stdout);
13571 		  if (option->info & OEX_FPDBUG)
13572 		    fputs (" FPDBUG", stdout);
13573 		  if (option->info & OEX_DISMISS)
13574 		    fputs (" DISMISS", stdout);
13575 		  break;
13576 		case ODK_PAD:
13577 		  fputs (" PAD       ", stdout);
13578 		  if (option->info & OPAD_PREFIX)
13579 		    fputs (" PREFIX", stdout);
13580 		  if (option->info & OPAD_POSTFIX)
13581 		    fputs (" POSTFIX", stdout);
13582 		  if (option->info & OPAD_SYMBOL)
13583 		    fputs (" SYMBOL", stdout);
13584 		  break;
13585 		case ODK_HWPATCH:
13586 		  fputs (" HWPATCH   ", stdout);
13587 		  if (option->info & OHW_R4KEOP)
13588 		    fputs (" R4KEOP", stdout);
13589 		  if (option->info & OHW_R8KPFETCH)
13590 		    fputs (" R8KPFETCH", stdout);
13591 		  if (option->info & OHW_R5KEOP)
13592 		    fputs (" R5KEOP", stdout);
13593 		  if (option->info & OHW_R5KCVTL)
13594 		    fputs (" R5KCVTL", stdout);
13595 		  break;
13596 		case ODK_FILL:
13597 		  fputs (" FILL       ", stdout);
13598 		  /* XXX Print content of info word?  */
13599 		  break;
13600 		case ODK_TAGS:
13601 		  fputs (" TAGS       ", stdout);
13602 		  /* XXX Print content of info word?  */
13603 		  break;
13604 		case ODK_HWAND:
13605 		  fputs (" HWAND     ", stdout);
13606 		  if (option->info & OHWA0_R4KEOP_CHECKED)
13607 		    fputs (" R4KEOP_CHECKED", stdout);
13608 		  if (option->info & OHWA0_R4KEOP_CLEAN)
13609 		    fputs (" R4KEOP_CLEAN", stdout);
13610 		  break;
13611 		case ODK_HWOR:
13612 		  fputs (" HWOR      ", stdout);
13613 		  if (option->info & OHWA0_R4KEOP_CHECKED)
13614 		    fputs (" R4KEOP_CHECKED", stdout);
13615 		  if (option->info & OHWA0_R4KEOP_CLEAN)
13616 		    fputs (" R4KEOP_CLEAN", stdout);
13617 		  break;
13618 		case ODK_GP_GROUP:
13619 		  printf (" GP_GROUP  %#06lx  self-contained %#06lx",
13620 			  option->info & OGP_GROUP,
13621 			  (option->info & OGP_SELF) >> 16);
13622 		  break;
13623 		case ODK_IDENT:
13624 		  printf (" IDENT     %#06lx  self-contained %#06lx",
13625 			  option->info & OGP_GROUP,
13626 			  (option->info & OGP_SELF) >> 16);
13627 		  break;
13628 		default:
13629 		  /* This shouldn't happen.  */
13630 		  printf (" %3d ???     %d %lx",
13631 			  option->kind, option->section, option->info);
13632 		  break;
13633 		}
13634 
13635 	      len = sizeof (* eopt);
13636 	      while (len < option->size)
13637 		if (((char *) option)[len] >= ' '
13638 		    && ((char *) option)[len] < 0x7f)
13639 		  printf ("%c", ((char *) option)[len++]);
13640 		else
13641 		  printf ("\\%03o", ((char *) option)[len++]);
13642 
13643 	      fputs ("\n", stdout);
13644 	      ++option;
13645 	    }
13646 
13647 	  free (eopt);
13648 	}
13649     }
13650 
13651   if (conflicts_offset != 0 && conflictsno != 0)
13652     {
13653       Elf32_Conflict * iconf;
13654       size_t cnt;
13655 
13656       if (dynamic_symbols == NULL)
13657 	{
13658 	  error (_("conflict list found without a dynamic symbol table\n"));
13659 	  return 0;
13660 	}
13661 
13662       iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
13663       if (iconf == NULL)
13664 	{
13665 	  error (_("Out of memory allocating space for dynamic conflicts\n"));
13666 	  return 0;
13667 	}
13668 
13669       if (is_32bit_elf)
13670 	{
13671 	  Elf32_External_Conflict * econf32;
13672 
13673 	  econf32 = (Elf32_External_Conflict *)
13674               get_data (NULL, file, conflicts_offset, conflictsno,
13675                         sizeof (* econf32), _("conflict"));
13676 	  if (!econf32)
13677 	    return 0;
13678 
13679 	  for (cnt = 0; cnt < conflictsno; ++cnt)
13680 	    iconf[cnt] = BYTE_GET (econf32[cnt]);
13681 
13682 	  free (econf32);
13683 	}
13684       else
13685 	{
13686 	  Elf64_External_Conflict * econf64;
13687 
13688 	  econf64 = (Elf64_External_Conflict *)
13689               get_data (NULL, file, conflicts_offset, conflictsno,
13690                         sizeof (* econf64), _("conflict"));
13691 	  if (!econf64)
13692 	    return 0;
13693 
13694 	  for (cnt = 0; cnt < conflictsno; ++cnt)
13695 	    iconf[cnt] = BYTE_GET (econf64[cnt]);
13696 
13697 	  free (econf64);
13698 	}
13699 
13700       printf (_("\nSection '.conflict' contains %lu entries:\n"),
13701 	      (unsigned long) conflictsno);
13702       puts (_("  Num:    Index       Value  Name"));
13703 
13704       for (cnt = 0; cnt < conflictsno; ++cnt)
13705 	{
13706 	  printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
13707 
13708 	  if (iconf[cnt] >= num_dynamic_syms)
13709 	    printf (_("<corrupt symbol index>"));
13710 	  else
13711 	    {
13712 	      Elf_Internal_Sym * psym;
13713 
13714 	      psym = & dynamic_symbols[iconf[cnt]];
13715 	      print_vma (psym->st_value, FULL_HEX);
13716 	      putchar (' ');
13717 	      if (VALID_DYNAMIC_NAME (psym->st_name))
13718 		print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
13719 	      else
13720 		printf (_("<corrupt: %14ld>"), psym->st_name);
13721 	    }
13722 	  putchar ('\n');
13723 	}
13724 
13725       free (iconf);
13726     }
13727 
13728   if (pltgot != 0 && local_gotno != 0)
13729     {
13730       bfd_vma ent, local_end, global_end;
13731       size_t i, offset;
13732       unsigned char * data;
13733       int addr_size;
13734 
13735       ent = pltgot;
13736       addr_size = (is_32bit_elf ? 4 : 8);
13737       local_end = pltgot + local_gotno * addr_size;
13738 
13739       /* PR binutils/17533 file: 012-111227-0.004  */
13740       if (symtabno < gotsym)
13741 	{
13742 	  error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
13743 		 (long) gotsym, (long) symtabno);
13744 	  return 0;
13745 	}
13746 
13747       global_end = local_end + (symtabno - gotsym) * addr_size;
13748       assert (global_end >= local_end);
13749       offset = offset_from_vma (file, pltgot, global_end - pltgot);
13750       data = (unsigned char *) get_data (NULL, file, offset,
13751                                          global_end - pltgot, 1,
13752 					 _("Global Offset Table data"));
13753       if (data == NULL)
13754 	return 0;
13755 
13756       printf (_("\nPrimary GOT:\n"));
13757       printf (_(" Canonical gp value: "));
13758       print_vma (pltgot + 0x7ff0, LONG_HEX);
13759       printf ("\n\n");
13760 
13761       printf (_(" Reserved entries:\n"));
13762       printf (_("  %*s %10s %*s Purpose\n"),
13763 	      addr_size * 2, _("Address"), _("Access"),
13764 	      addr_size * 2, _("Initial"));
13765       ent = print_mips_got_entry (data, pltgot, ent);
13766       printf (_(" Lazy resolver\n"));
13767       if (data
13768 	  && (byte_get (data + ent - pltgot, addr_size)
13769 	      >> (addr_size * 8 - 1)) != 0)
13770 	{
13771 	  ent = print_mips_got_entry (data, pltgot, ent);
13772 	  printf (_(" Module pointer (GNU extension)\n"));
13773 	}
13774       printf ("\n");
13775 
13776       if (ent < local_end)
13777 	{
13778 	  printf (_(" Local entries:\n"));
13779 	  printf ("  %*s %10s %*s\n",
13780 		  addr_size * 2, _("Address"), _("Access"),
13781 		  addr_size * 2, _("Initial"));
13782 	  while (ent < local_end)
13783 	    {
13784 	      ent = print_mips_got_entry (data, pltgot, ent);
13785 	      printf ("\n");
13786 	    }
13787 	  printf ("\n");
13788 	}
13789 
13790       if (gotsym < symtabno)
13791 	{
13792 	  int sym_width;
13793 
13794 	  printf (_(" Global entries:\n"));
13795 	  printf ("  %*s %10s %*s %*s %-7s %3s %s\n",
13796 		  addr_size * 2, _("Address"),
13797 		  _("Access"),
13798 		  addr_size * 2, _("Initial"),
13799 		  addr_size * 2, _("Sym.Val."),
13800 		  _("Type"),
13801 		  /* Note for translators: "Ndx" = abbreviated form of "Index".  */
13802 		  _("Ndx"), _("Name"));
13803 
13804 	  sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
13805 
13806 	  for (i = gotsym; i < symtabno; i++)
13807 	    {
13808 	      ent = print_mips_got_entry (data, pltgot, ent);
13809 	      printf (" ");
13810 
13811 	      if (dynamic_symbols == NULL)
13812 		printf (_("<no dynamic symbols>"));
13813 	      else if (i < num_dynamic_syms)
13814 		{
13815 		  Elf_Internal_Sym * psym = dynamic_symbols + i;
13816 
13817 		  print_vma (psym->st_value, LONG_HEX);
13818 		  printf (" %-7s %3s ",
13819 			  get_symbol_type (ELF_ST_TYPE (psym->st_info)),
13820 			  get_symbol_index_type (psym->st_shndx));
13821 
13822 		  if (VALID_DYNAMIC_NAME (psym->st_name))
13823 		    print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
13824 		  else
13825 		    printf (_("<corrupt: %14ld>"), psym->st_name);
13826 		}
13827 	      else
13828 		printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
13829 			(unsigned long) i);
13830 
13831 	      printf ("\n");
13832 	    }
13833 	  printf ("\n");
13834 	}
13835 
13836       if (data)
13837 	free (data);
13838     }
13839 
13840   if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
13841     {
13842       bfd_vma ent, end;
13843       size_t offset, rel_offset;
13844       unsigned long count, i;
13845       unsigned char * data;
13846       int addr_size, sym_width;
13847       Elf_Internal_Rela * rels;
13848 
13849       rel_offset = offset_from_vma (file, jmprel, pltrelsz);
13850       if (pltrel == DT_RELA)
13851 	{
13852 	  if (!slurp_rela_relocs (file, rel_offset, pltrelsz, &rels, &count))
13853 	    return 0;
13854 	}
13855       else
13856 	{
13857 	  if (!slurp_rel_relocs (file, rel_offset, pltrelsz, &rels, &count))
13858 	    return 0;
13859 	}
13860 
13861       ent = mips_pltgot;
13862       addr_size = (is_32bit_elf ? 4 : 8);
13863       end = mips_pltgot + (2 + count) * addr_size;
13864 
13865       offset = offset_from_vma (file, mips_pltgot, end - mips_pltgot);
13866       data = (unsigned char *) get_data (NULL, file, offset, end - mips_pltgot,
13867                                          1, _("Procedure Linkage Table data"));
13868       if (data == NULL)
13869 	return 0;
13870 
13871       printf ("\nPLT GOT:\n\n");
13872       printf (_(" Reserved entries:\n"));
13873       printf (_("  %*s %*s Purpose\n"),
13874 	      addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
13875       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
13876       printf (_(" PLT lazy resolver\n"));
13877       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
13878       printf (_(" Module pointer\n"));
13879       printf ("\n");
13880 
13881       printf (_(" Entries:\n"));
13882       printf ("  %*s %*s %*s %-7s %3s %s\n",
13883 	      addr_size * 2, _("Address"),
13884 	      addr_size * 2, _("Initial"),
13885 	      addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
13886       sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
13887       for (i = 0; i < count; i++)
13888 	{
13889 	  unsigned long idx = get_reloc_symindex (rels[i].r_info);
13890 
13891 	  ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
13892 	  printf (" ");
13893 
13894 	  if (idx >= num_dynamic_syms)
13895 	    printf (_("<corrupt symbol index: %lu>"), idx);
13896 	  else
13897 	    {
13898 	      Elf_Internal_Sym * psym = dynamic_symbols + idx;
13899 
13900 	      print_vma (psym->st_value, LONG_HEX);
13901 	      printf (" %-7s %3s ",
13902 		      get_symbol_type (ELF_ST_TYPE (psym->st_info)),
13903 		      get_symbol_index_type (psym->st_shndx));
13904 	      if (VALID_DYNAMIC_NAME (psym->st_name))
13905 		print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
13906 	      else
13907 		printf (_("<corrupt: %14ld>"), psym->st_name);
13908 	    }
13909 	  printf ("\n");
13910 	}
13911       printf ("\n");
13912 
13913       if (data)
13914 	free (data);
13915       free (rels);
13916     }
13917 
13918   return 1;
13919 }
13920 
13921 static int
process_nds32_specific(FILE * file)13922 process_nds32_specific (FILE * file)
13923 {
13924   Elf_Internal_Shdr *sect = NULL;
13925 
13926   sect = find_section (".nds32_e_flags");
13927   if (sect != NULL)
13928     {
13929       unsigned int *flag;
13930 
13931       printf ("\nNDS32 elf flags section:\n");
13932       flag = get_data (NULL, file, sect->sh_offset, 1,
13933 		       sect->sh_size, _("NDS32 elf flags section"));
13934 
13935       switch ((*flag) & 0x3)
13936 	{
13937 	case 0:
13938 	  printf ("(VEC_SIZE):\tNo entry.\n");
13939 	  break;
13940 	case 1:
13941 	  printf ("(VEC_SIZE):\t4 bytes\n");
13942 	  break;
13943 	case 2:
13944 	  printf ("(VEC_SIZE):\t16 bytes\n");
13945 	  break;
13946 	case 3:
13947 	  printf ("(VEC_SIZE):\treserved\n");
13948 	  break;
13949 	}
13950     }
13951 
13952   return TRUE;
13953 }
13954 
13955 static int
process_gnu_liblist(FILE * file)13956 process_gnu_liblist (FILE * file)
13957 {
13958   Elf_Internal_Shdr * section;
13959   Elf_Internal_Shdr * string_sec;
13960   Elf32_External_Lib * elib;
13961   char * strtab;
13962   size_t strtab_size;
13963   size_t cnt;
13964   unsigned i;
13965 
13966   if (! do_arch)
13967     return 0;
13968 
13969   for (i = 0, section = section_headers;
13970        i < elf_header.e_shnum;
13971        i++, section++)
13972     {
13973       switch (section->sh_type)
13974 	{
13975 	case SHT_GNU_LIBLIST:
13976 	  if (section->sh_link >= elf_header.e_shnum)
13977 	    break;
13978 
13979 	  elib = (Elf32_External_Lib *)
13980               get_data (NULL, file, section->sh_offset, 1, section->sh_size,
13981                         _("liblist section data"));
13982 
13983 	  if (elib == NULL)
13984 	    break;
13985 	  string_sec = section_headers + section->sh_link;
13986 
13987 	  strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
13988                                       string_sec->sh_size,
13989                                       _("liblist string table"));
13990 	  if (strtab == NULL
13991 	      || section->sh_entsize != sizeof (Elf32_External_Lib))
13992 	    {
13993 	      free (elib);
13994 	      free (strtab);
13995 	      break;
13996 	    }
13997 	  strtab_size = string_sec->sh_size;
13998 
13999 	  printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
14000 		  printable_section_name (section),
14001 		  (unsigned long) (section->sh_size / sizeof (Elf32_External_Lib)));
14002 
14003 	  puts (_("     Library              Time Stamp          Checksum   Version Flags"));
14004 
14005 	  for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
14006 	       ++cnt)
14007 	    {
14008 	      Elf32_Lib liblist;
14009 	      time_t atime;
14010 	      char timebuf[20];
14011 	      struct tm * tmp;
14012 
14013 	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
14014 	      atime = BYTE_GET (elib[cnt].l_time_stamp);
14015 	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
14016 	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
14017 	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
14018 
14019 	      tmp = gmtime (&atime);
14020 	      snprintf (timebuf, sizeof (timebuf),
14021 			"%04u-%02u-%02uT%02u:%02u:%02u",
14022 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
14023 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
14024 
14025 	      printf ("%3lu: ", (unsigned long) cnt);
14026 	      if (do_wide)
14027 		printf ("%-20s", liblist.l_name < strtab_size
14028 			? strtab + liblist.l_name : _("<corrupt>"));
14029 	      else
14030 		printf ("%-20.20s", liblist.l_name < strtab_size
14031 			? strtab + liblist.l_name : _("<corrupt>"));
14032 	      printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
14033 		      liblist.l_version, liblist.l_flags);
14034 	    }
14035 
14036 	  free (elib);
14037 	  free (strtab);
14038 	}
14039     }
14040 
14041   return 1;
14042 }
14043 
14044 static const char *
get_note_type(unsigned e_type)14045 get_note_type (unsigned e_type)
14046 {
14047   static char buff[64];
14048 
14049   if (elf_header.e_type == ET_CORE)
14050     switch (e_type)
14051       {
14052       case NT_AUXV:
14053 	return _("NT_AUXV (auxiliary vector)");
14054       case NT_PRSTATUS:
14055 	return _("NT_PRSTATUS (prstatus structure)");
14056       case NT_FPREGSET:
14057 	return _("NT_FPREGSET (floating point registers)");
14058       case NT_PRPSINFO:
14059 	return _("NT_PRPSINFO (prpsinfo structure)");
14060       case NT_TASKSTRUCT:
14061 	return _("NT_TASKSTRUCT (task structure)");
14062       case NT_PRXFPREG:
14063 	return _("NT_PRXFPREG (user_xfpregs structure)");
14064       case NT_PPC_VMX:
14065 	return _("NT_PPC_VMX (ppc Altivec registers)");
14066       case NT_PPC_VSX:
14067 	return _("NT_PPC_VSX (ppc VSX registers)");
14068       case NT_386_TLS:
14069 	return _("NT_386_TLS (x86 TLS information)");
14070       case NT_386_IOPERM:
14071 	return _("NT_386_IOPERM (x86 I/O permissions)");
14072       case NT_X86_XSTATE:
14073 	return _("NT_X86_XSTATE (x86 XSAVE extended state)");
14074       case NT_S390_HIGH_GPRS:
14075 	return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
14076       case NT_S390_TIMER:
14077 	return _("NT_S390_TIMER (s390 timer register)");
14078       case NT_S390_TODCMP:
14079 	return _("NT_S390_TODCMP (s390 TOD comparator register)");
14080       case NT_S390_TODPREG:
14081 	return _("NT_S390_TODPREG (s390 TOD programmable register)");
14082       case NT_S390_CTRS:
14083 	return _("NT_S390_CTRS (s390 control registers)");
14084       case NT_S390_PREFIX:
14085 	return _("NT_S390_PREFIX (s390 prefix register)");
14086       case NT_S390_LAST_BREAK:
14087 	return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
14088       case NT_S390_SYSTEM_CALL:
14089 	return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
14090       case NT_S390_TDB:
14091 	return _("NT_S390_TDB (s390 transaction diagnostic block)");
14092       case NT_ARM_VFP:
14093 	return _("NT_ARM_VFP (arm VFP registers)");
14094       case NT_ARM_TLS:
14095 	return _("NT_ARM_TLS (AArch TLS registers)");
14096       case NT_ARM_HW_BREAK:
14097 	return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
14098       case NT_ARM_HW_WATCH:
14099 	return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
14100       case NT_PSTATUS:
14101 	return _("NT_PSTATUS (pstatus structure)");
14102       case NT_FPREGS:
14103 	return _("NT_FPREGS (floating point registers)");
14104       case NT_PSINFO:
14105 	return _("NT_PSINFO (psinfo structure)");
14106       case NT_LWPSTATUS:
14107 	return _("NT_LWPSTATUS (lwpstatus_t structure)");
14108       case NT_LWPSINFO:
14109 	return _("NT_LWPSINFO (lwpsinfo_t structure)");
14110       case NT_WIN32PSTATUS:
14111 	return _("NT_WIN32PSTATUS (win32_pstatus structure)");
14112       case NT_SIGINFO:
14113 	return _("NT_SIGINFO (siginfo_t data)");
14114       case NT_FILE:
14115 	return _("NT_FILE (mapped files)");
14116       default:
14117 	break;
14118       }
14119   else
14120     switch (e_type)
14121       {
14122       case NT_VERSION:
14123 	return _("NT_VERSION (version)");
14124       case NT_ARCH:
14125 	return _("NT_ARCH (architecture)");
14126       default:
14127 	break;
14128       }
14129 
14130   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14131   return buff;
14132 }
14133 
14134 static int
print_core_note(Elf_Internal_Note * pnote)14135 print_core_note (Elf_Internal_Note *pnote)
14136 {
14137   unsigned int addr_size = is_32bit_elf ? 4 : 8;
14138   bfd_vma count, page_size;
14139   unsigned char *descdata, *filenames, *descend;
14140 
14141   if (pnote->type != NT_FILE)
14142     return 1;
14143 
14144 #ifndef BFD64
14145   if (!is_32bit_elf)
14146     {
14147       printf (_("    Cannot decode 64-bit note in 32-bit build\n"));
14148       /* Still "successful".  */
14149       return 1;
14150     }
14151 #endif
14152 
14153   if (pnote->descsz < 2 * addr_size)
14154     {
14155       printf (_("    Malformed note - too short for header\n"));
14156       return 0;
14157     }
14158 
14159   descdata = (unsigned char *) pnote->descdata;
14160   descend = descdata + pnote->descsz;
14161 
14162   if (descdata[pnote->descsz - 1] != '\0')
14163     {
14164       printf (_("    Malformed note - does not end with \\0\n"));
14165       return 0;
14166     }
14167 
14168   count = byte_get (descdata, addr_size);
14169   descdata += addr_size;
14170 
14171   page_size = byte_get (descdata, addr_size);
14172   descdata += addr_size;
14173 
14174   if (pnote->descsz < 2 * addr_size + count * 3 * addr_size)
14175     {
14176       printf (_("    Malformed note - too short for supplied file count\n"));
14177       return 0;
14178     }
14179 
14180   printf (_("    Page size: "));
14181   print_vma (page_size, DEC);
14182   printf ("\n");
14183 
14184   printf (_("    %*s%*s%*s\n"),
14185 	  (int) (2 + 2 * addr_size), _("Start"),
14186 	  (int) (4 + 2 * addr_size), _("End"),
14187 	  (int) (4 + 2 * addr_size), _("Page Offset"));
14188   filenames = descdata + count * 3 * addr_size;
14189   while (--count > 0)
14190     {
14191       bfd_vma start, end, file_ofs;
14192 
14193       if (filenames == descend)
14194 	{
14195 	  printf (_("    Malformed note - filenames end too early\n"));
14196 	  return 0;
14197 	}
14198 
14199       start = byte_get (descdata, addr_size);
14200       descdata += addr_size;
14201       end = byte_get (descdata, addr_size);
14202       descdata += addr_size;
14203       file_ofs = byte_get (descdata, addr_size);
14204       descdata += addr_size;
14205 
14206       printf ("    ");
14207       print_vma (start, FULL_HEX);
14208       printf ("  ");
14209       print_vma (end, FULL_HEX);
14210       printf ("  ");
14211       print_vma (file_ofs, FULL_HEX);
14212       printf ("\n        %s\n", filenames);
14213 
14214       filenames += 1 + strlen ((char *) filenames);
14215     }
14216 
14217   return 1;
14218 }
14219 
14220 static const char *
get_gnu_elf_note_type(unsigned e_type)14221 get_gnu_elf_note_type (unsigned e_type)
14222 {
14223   static char buff[64];
14224 
14225   switch (e_type)
14226     {
14227     case NT_GNU_ABI_TAG:
14228       return _("NT_GNU_ABI_TAG (ABI version tag)");
14229     case NT_GNU_HWCAP:
14230       return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
14231     case NT_GNU_BUILD_ID:
14232       return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
14233     case NT_GNU_GOLD_VERSION:
14234       return _("NT_GNU_GOLD_VERSION (gold version)");
14235     default:
14236       break;
14237     }
14238 
14239   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14240   return buff;
14241 }
14242 
14243 static int
print_gnu_note(Elf_Internal_Note * pnote)14244 print_gnu_note (Elf_Internal_Note *pnote)
14245 {
14246   switch (pnote->type)
14247     {
14248     case NT_GNU_BUILD_ID:
14249       {
14250 	unsigned long i;
14251 
14252 	printf (_("    Build ID: "));
14253 	for (i = 0; i < pnote->descsz; ++i)
14254 	  printf ("%02x", pnote->descdata[i] & 0xff);
14255 	printf ("\n");
14256       }
14257       break;
14258 
14259     case NT_GNU_ABI_TAG:
14260       {
14261 	unsigned long os, major, minor, subminor;
14262 	const char *osname;
14263 
14264 	/* PR 17531: file: 030-599401-0.004.  */
14265 	if (pnote->descsz < 16)
14266 	  {
14267 	    printf (_("    <corrupt GNU_ABI_TAG>\n"));
14268 	    break;
14269 	  }
14270 
14271 	os = byte_get ((unsigned char *) pnote->descdata, 4);
14272 	major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
14273 	minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
14274 	subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
14275 
14276 	switch (os)
14277 	  {
14278 	  case GNU_ABI_TAG_LINUX:
14279 	    osname = "Linux";
14280 	    break;
14281 	  case GNU_ABI_TAG_HURD:
14282 	    osname = "Hurd";
14283 	    break;
14284 	  case GNU_ABI_TAG_SOLARIS:
14285 	    osname = "Solaris";
14286 	    break;
14287 	  case GNU_ABI_TAG_FREEBSD:
14288 	    osname = "FreeBSD";
14289 	    break;
14290 	  case GNU_ABI_TAG_NETBSD:
14291 	    osname = "NetBSD";
14292 	    break;
14293 	  default:
14294 	    osname = "Unknown";
14295 	    break;
14296 	  }
14297 
14298 	printf (_("    OS: %s, ABI: %ld.%ld.%ld\n"), osname,
14299 		major, minor, subminor);
14300       }
14301       break;
14302 
14303     case NT_GNU_GOLD_VERSION:
14304       {
14305 	unsigned long i;
14306 
14307 	printf (_("    Version: "));
14308 	for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
14309 	  printf ("%c", pnote->descdata[i]);
14310 	printf ("\n");
14311       }
14312       break;
14313     }
14314 
14315   return 1;
14316 }
14317 
14318 static const char *
get_netbsd_elfcore_note_type(unsigned e_type)14319 get_netbsd_elfcore_note_type (unsigned e_type)
14320 {
14321   static char buff[64];
14322 
14323   if (e_type == NT_NETBSDCORE_PROCINFO)
14324     {
14325       /* NetBSD core "procinfo" structure.  */
14326       return _("NetBSD procinfo structure");
14327     }
14328 
14329   /* As of Jan 2002 there are no other machine-independent notes
14330      defined for NetBSD core files.  If the note type is less
14331      than the start of the machine-dependent note types, we don't
14332      understand it.  */
14333 
14334   if (e_type < NT_NETBSDCORE_FIRSTMACH)
14335     {
14336       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14337       return buff;
14338     }
14339 
14340   switch (elf_header.e_machine)
14341     {
14342     /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
14343        and PT_GETFPREGS == mach+2.  */
14344 
14345     case EM_OLD_ALPHA:
14346     case EM_ALPHA:
14347     case EM_SPARC:
14348     case EM_SPARC32PLUS:
14349     case EM_SPARCV9:
14350       switch (e_type)
14351 	{
14352 	case NT_NETBSDCORE_FIRSTMACH + 0:
14353 	  return _("PT_GETREGS (reg structure)");
14354 	case NT_NETBSDCORE_FIRSTMACH + 2:
14355 	  return _("PT_GETFPREGS (fpreg structure)");
14356 	default:
14357 	  break;
14358 	}
14359       break;
14360 
14361     /* On all other arch's, PT_GETREGS == mach+1 and
14362        PT_GETFPREGS == mach+3.  */
14363     default:
14364       switch (e_type)
14365 	{
14366 	case NT_NETBSDCORE_FIRSTMACH + 1:
14367 	  return _("PT_GETREGS (reg structure)");
14368 	case NT_NETBSDCORE_FIRSTMACH + 3:
14369 	  return _("PT_GETFPREGS (fpreg structure)");
14370 	default:
14371 	  break;
14372 	}
14373     }
14374 
14375   snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
14376 	    e_type - NT_NETBSDCORE_FIRSTMACH);
14377   return buff;
14378 }
14379 
14380 static const char *
get_stapsdt_note_type(unsigned e_type)14381 get_stapsdt_note_type (unsigned e_type)
14382 {
14383   static char buff[64];
14384 
14385   switch (e_type)
14386     {
14387     case NT_STAPSDT:
14388       return _("NT_STAPSDT (SystemTap probe descriptors)");
14389 
14390     default:
14391       break;
14392     }
14393 
14394   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14395   return buff;
14396 }
14397 
14398 static int
print_stapsdt_note(Elf_Internal_Note * pnote)14399 print_stapsdt_note (Elf_Internal_Note *pnote)
14400 {
14401   int addr_size = is_32bit_elf ? 4 : 8;
14402   char *data = pnote->descdata;
14403   char *data_end = pnote->descdata + pnote->descsz;
14404   bfd_vma pc, base_addr, semaphore;
14405   char *provider, *probe, *arg_fmt;
14406 
14407   pc = byte_get ((unsigned char *) data, addr_size);
14408   data += addr_size;
14409   base_addr = byte_get ((unsigned char *) data, addr_size);
14410   data += addr_size;
14411   semaphore = byte_get ((unsigned char *) data, addr_size);
14412   data += addr_size;
14413 
14414   provider = data;
14415   data += strlen (data) + 1;
14416   probe = data;
14417   data += strlen (data) + 1;
14418   arg_fmt = data;
14419   data += strlen (data) + 1;
14420 
14421   printf (_("    Provider: %s\n"), provider);
14422   printf (_("    Name: %s\n"), probe);
14423   printf (_("    Location: "));
14424   print_vma (pc, FULL_HEX);
14425   printf (_(", Base: "));
14426   print_vma (base_addr, FULL_HEX);
14427   printf (_(", Semaphore: "));
14428   print_vma (semaphore, FULL_HEX);
14429   printf ("\n");
14430   printf (_("    Arguments: %s\n"), arg_fmt);
14431 
14432   return data == data_end;
14433 }
14434 
14435 static const char *
get_ia64_vms_note_type(unsigned e_type)14436 get_ia64_vms_note_type (unsigned e_type)
14437 {
14438   static char buff[64];
14439 
14440   switch (e_type)
14441     {
14442     case NT_VMS_MHD:
14443       return _("NT_VMS_MHD (module header)");
14444     case NT_VMS_LNM:
14445       return _("NT_VMS_LNM (language name)");
14446     case NT_VMS_SRC:
14447       return _("NT_VMS_SRC (source files)");
14448     case NT_VMS_TITLE:
14449       return "NT_VMS_TITLE";
14450     case NT_VMS_EIDC:
14451       return _("NT_VMS_EIDC (consistency check)");
14452     case NT_VMS_FPMODE:
14453       return _("NT_VMS_FPMODE (FP mode)");
14454     case NT_VMS_LINKTIME:
14455       return "NT_VMS_LINKTIME";
14456     case NT_VMS_IMGNAM:
14457       return _("NT_VMS_IMGNAM (image name)");
14458     case NT_VMS_IMGID:
14459       return _("NT_VMS_IMGID (image id)");
14460     case NT_VMS_LINKID:
14461       return _("NT_VMS_LINKID (link id)");
14462     case NT_VMS_IMGBID:
14463       return _("NT_VMS_IMGBID (build id)");
14464     case NT_VMS_GSTNAM:
14465       return _("NT_VMS_GSTNAM (sym table name)");
14466     case NT_VMS_ORIG_DYN:
14467       return "NT_VMS_ORIG_DYN";
14468     case NT_VMS_PATCHTIME:
14469       return "NT_VMS_PATCHTIME";
14470     default:
14471       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
14472       return buff;
14473     }
14474 }
14475 
14476 static int
print_ia64_vms_note(Elf_Internal_Note * pnote)14477 print_ia64_vms_note (Elf_Internal_Note * pnote)
14478 {
14479   switch (pnote->type)
14480     {
14481     case NT_VMS_MHD:
14482       if (pnote->descsz > 36)
14483         {
14484           size_t l = strlen (pnote->descdata + 34);
14485           printf (_("    Creation date  : %.17s\n"), pnote->descdata);
14486           printf (_("    Last patch date: %.17s\n"), pnote->descdata + 17);
14487           printf (_("    Module name    : %s\n"), pnote->descdata + 34);
14488           printf (_("    Module version : %s\n"), pnote->descdata + 34 + l + 1);
14489         }
14490       else
14491         printf (_("    Invalid size\n"));
14492       break;
14493     case NT_VMS_LNM:
14494       printf (_("   Language: %s\n"), pnote->descdata);
14495       break;
14496 #ifdef BFD64
14497     case NT_VMS_FPMODE:
14498       printf (_("   Floating Point mode: "));
14499       printf ("0x%016" BFD_VMA_FMT "x\n",
14500               (bfd_vma)byte_get ((unsigned char *)pnote->descdata, 8));
14501       break;
14502     case NT_VMS_LINKTIME:
14503       printf (_("   Link time: "));
14504       print_vms_time
14505         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
14506       printf ("\n");
14507       break;
14508     case NT_VMS_PATCHTIME:
14509       printf (_("   Patch time: "));
14510       print_vms_time
14511         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
14512       printf ("\n");
14513       break;
14514     case NT_VMS_ORIG_DYN:
14515       printf (_("   Major id: %u,  minor id: %u\n"),
14516               (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
14517               (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
14518       printf (_("   Last modified  : "));
14519       print_vms_time
14520         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
14521       printf (_("\n   Link flags  : "));
14522       printf ("0x%016" BFD_VMA_FMT "x\n",
14523               (bfd_vma)byte_get ((unsigned char *)pnote->descdata + 16, 8));
14524       printf (_("   Header flags: 0x%08x\n"),
14525               (unsigned)byte_get ((unsigned char *)pnote->descdata + 24, 4));
14526       printf (_("   Image id    : %s\n"), pnote->descdata + 32);
14527       break;
14528 #endif
14529     case NT_VMS_IMGNAM:
14530       printf (_("    Image name: %s\n"), pnote->descdata);
14531       break;
14532     case NT_VMS_GSTNAM:
14533       printf (_("    Global symbol table name: %s\n"), pnote->descdata);
14534       break;
14535     case NT_VMS_IMGID:
14536       printf (_("    Image id: %s\n"), pnote->descdata);
14537       break;
14538     case NT_VMS_LINKID:
14539       printf (_("    Linker id: %s\n"), pnote->descdata);
14540       break;
14541     default:
14542       break;
14543     }
14544   return 1;
14545 }
14546 
14547 /* Note that by the ELF standard, the name field is already null byte
14548    terminated, and namesz includes the terminating null byte.
14549    I.E. the value of namesz for the name "FSF" is 4.
14550 
14551    If the value of namesz is zero, there is no name present.  */
14552 static int
process_note(Elf_Internal_Note * pnote)14553 process_note (Elf_Internal_Note * pnote)
14554 {
14555   const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
14556   const char * nt;
14557 
14558   if (pnote->namesz == 0)
14559     /* If there is no note name, then use the default set of
14560        note type strings.  */
14561     nt = get_note_type (pnote->type);
14562 
14563   else if (const_strneq (pnote->namedata, "GNU"))
14564     /* GNU-specific object file notes.  */
14565     nt = get_gnu_elf_note_type (pnote->type);
14566 
14567   else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
14568     /* NetBSD-specific core file notes.  */
14569     nt = get_netbsd_elfcore_note_type (pnote->type);
14570 
14571   else if (strneq (pnote->namedata, "SPU/", 4))
14572     {
14573       /* SPU-specific core file notes.  */
14574       nt = pnote->namedata + 4;
14575       name = "SPU";
14576     }
14577 
14578   else if (const_strneq (pnote->namedata, "IPF/VMS"))
14579     /* VMS/ia64-specific file notes.  */
14580     nt = get_ia64_vms_note_type (pnote->type);
14581 
14582   else if (const_strneq (pnote->namedata, "stapsdt"))
14583     nt = get_stapsdt_note_type (pnote->type);
14584 
14585   else
14586     /* Don't recognize this note name; just use the default set of
14587        note type strings.  */
14588     nt = get_note_type (pnote->type);
14589 
14590   printf ("  %-20s 0x%08lx\t%s\n", name, pnote->descsz, nt);
14591 
14592   if (const_strneq (pnote->namedata, "IPF/VMS"))
14593     return print_ia64_vms_note (pnote);
14594   else if (const_strneq (pnote->namedata, "GNU"))
14595     return print_gnu_note (pnote);
14596   else if (const_strneq (pnote->namedata, "stapsdt"))
14597     return print_stapsdt_note (pnote);
14598   else if (const_strneq (pnote->namedata, "CORE"))
14599     return print_core_note (pnote);
14600   else
14601     return 1;
14602 }
14603 
14604 
14605 static int
process_corefile_note_segment(FILE * file,bfd_vma offset,bfd_vma length)14606 process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
14607 {
14608   Elf_External_Note * pnotes;
14609   Elf_External_Note * external;
14610   int res = 1;
14611 
14612   if (length <= 0)
14613     return 0;
14614 
14615   pnotes = (Elf_External_Note *) get_data (NULL, file, offset, 1, length,
14616 					   _("notes"));
14617   if (pnotes == NULL)
14618     return 0;
14619 
14620   external = pnotes;
14621 
14622   printf (_("\nDisplaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
14623 	  (unsigned long) offset, (unsigned long) length);
14624   printf (_("  %-20s %10s\tDescription\n"), _("Owner"), _("Data size"));
14625 
14626   while ((char *) external < (char *) pnotes + length)
14627     {
14628       Elf_Internal_Note inote;
14629       size_t min_notesz;
14630       char *next;
14631       char * temp = NULL;
14632       size_t data_remaining = ((char *) pnotes + length) - (char *) external;
14633 
14634       if (!is_ia64_vms ())
14635 	{
14636 	  /* PR binutils/15191
14637 	     Make sure that there is enough data to read.  */
14638 	  min_notesz = offsetof (Elf_External_Note, name);
14639 	  if (data_remaining < min_notesz)
14640 	    {
14641 	      warn (_("Corrupt note: only %d bytes remain, not enough for a full note\n"),
14642 		    (int) data_remaining);
14643 	      break;
14644 	    }
14645 	  inote.type     = BYTE_GET (external->type);
14646 	  inote.namesz   = BYTE_GET (external->namesz);
14647 	  inote.namedata = external->name;
14648 	  inote.descsz   = BYTE_GET (external->descsz);
14649 	  inote.descdata = inote.namedata + align_power (inote.namesz, 2);
14650 	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
14651 	  next = inote.descdata + align_power (inote.descsz, 2);
14652 	}
14653       else
14654 	{
14655 	  Elf64_External_VMS_Note *vms_external;
14656 
14657 	  /* PR binutils/15191
14658 	     Make sure that there is enough data to read.  */
14659 	  min_notesz = offsetof (Elf64_External_VMS_Note, name);
14660 	  if (data_remaining < min_notesz)
14661 	    {
14662 	      warn (_("Corrupt note: only %d bytes remain, not enough for a full note\n"),
14663 		    (int) data_remaining);
14664 	      break;
14665 	    }
14666 
14667 	  vms_external = (Elf64_External_VMS_Note *) external;
14668 	  inote.type     = BYTE_GET (vms_external->type);
14669 	  inote.namesz   = BYTE_GET (vms_external->namesz);
14670 	  inote.namedata = vms_external->name;
14671 	  inote.descsz   = BYTE_GET (vms_external->descsz);
14672 	  inote.descdata = inote.namedata + align_power (inote.namesz, 3);
14673 	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
14674 	  next = inote.descdata + align_power (inote.descsz, 3);
14675 	}
14676 
14677       if (inote.descdata < (char *) external + min_notesz
14678 	  || next < (char *) external + min_notesz
14679 	  || data_remaining < (size_t)(next - (char *) external))
14680 	{
14681 	  warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
14682 		(unsigned long) ((char *) external - (char *) pnotes));
14683 	  warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx\n"),
14684 		inote.type, inote.namesz, inote.descsz);
14685 	  break;
14686 	}
14687 
14688       external = (Elf_External_Note *) next;
14689 
14690       /* Verify that name is null terminated.  It appears that at least
14691 	 one version of Linux (RedHat 6.0) generates corefiles that don't
14692 	 comply with the ELF spec by failing to include the null byte in
14693 	 namesz.  */
14694       if (inote.namedata[inote.namesz - 1] != '\0')
14695 	{
14696 	  temp = (char *) malloc (inote.namesz + 1);
14697 
14698 	  if (temp == NULL)
14699 	    {
14700 	      error (_("Out of memory allocating space for inote name\n"));
14701 	      res = 0;
14702 	      break;
14703 	    }
14704 
14705 	  strncpy (temp, inote.namedata, inote.namesz);
14706 	  temp[inote.namesz] = 0;
14707 
14708 	  /* warn (_("'%s' NOTE name not properly null terminated\n"), temp);  */
14709 	  inote.namedata = temp;
14710 	}
14711 
14712       res &= process_note (& inote);
14713 
14714       if (temp != NULL)
14715 	{
14716 	  free (temp);
14717 	  temp = NULL;
14718 	}
14719     }
14720 
14721   free (pnotes);
14722 
14723   return res;
14724 }
14725 
14726 static int
process_corefile_note_segments(FILE * file)14727 process_corefile_note_segments (FILE * file)
14728 {
14729   Elf_Internal_Phdr * segment;
14730   unsigned int i;
14731   int res = 1;
14732 
14733   if (! get_program_headers (file))
14734       return 0;
14735 
14736   for (i = 0, segment = program_headers;
14737        i < elf_header.e_phnum;
14738        i++, segment++)
14739     {
14740       if (segment->p_type == PT_NOTE)
14741 	res &= process_corefile_note_segment (file,
14742 					      (bfd_vma) segment->p_offset,
14743 					      (bfd_vma) segment->p_filesz);
14744     }
14745 
14746   return res;
14747 }
14748 
14749 static int
process_note_sections(FILE * file)14750 process_note_sections (FILE * file)
14751 {
14752   Elf_Internal_Shdr * section;
14753   unsigned long i;
14754   int n = 0;
14755   int res = 1;
14756 
14757   for (i = 0, section = section_headers;
14758        i < elf_header.e_shnum && section != NULL;
14759        i++, section++)
14760     if (section->sh_type == SHT_NOTE)
14761       {
14762 	res &= process_corefile_note_segment (file,
14763 					      (bfd_vma) section->sh_offset,
14764 					      (bfd_vma) section->sh_size);
14765 	n++;
14766       }
14767 
14768   if (n == 0)
14769     /* Try processing NOTE segments instead.  */
14770     return process_corefile_note_segments (file);
14771 
14772   return res;
14773 }
14774 
14775 static int
process_notes(FILE * file)14776 process_notes (FILE * file)
14777 {
14778   /* If we have not been asked to display the notes then do nothing.  */
14779   if (! do_notes)
14780     return 1;
14781 
14782   if (elf_header.e_type != ET_CORE)
14783     return process_note_sections (file);
14784 
14785   /* No program headers means no NOTE segment.  */
14786   if (elf_header.e_phnum > 0)
14787     return process_corefile_note_segments (file);
14788 
14789   printf (_("No note segments present in the core file.\n"));
14790   return 1;
14791 }
14792 
14793 static int
process_arch_specific(FILE * file)14794 process_arch_specific (FILE * file)
14795 {
14796   if (! do_arch)
14797     return 1;
14798 
14799   switch (elf_header.e_machine)
14800     {
14801     case EM_ARM:
14802       return process_arm_specific (file);
14803     case EM_MIPS:
14804     case EM_MIPS_RS3_LE:
14805       return process_mips_specific (file);
14806       break;
14807     case EM_NDS32:
14808       return process_nds32_specific (file);
14809       break;
14810     case EM_PPC:
14811       return process_power_specific (file);
14812       break;
14813     case EM_SPARC:
14814     case EM_SPARC32PLUS:
14815     case EM_SPARCV9:
14816       return process_sparc_specific (file);
14817       break;
14818     case EM_TI_C6000:
14819       return process_tic6x_specific (file);
14820       break;
14821     case EM_MSP430:
14822       return process_msp430x_specific (file);
14823     default:
14824       break;
14825     }
14826   return 1;
14827 }
14828 
14829 static int
get_file_header(FILE * file)14830 get_file_header (FILE * file)
14831 {
14832   /* Read in the identity array.  */
14833   if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
14834     return 0;
14835 
14836   /* Determine how to read the rest of the header.  */
14837   switch (elf_header.e_ident[EI_DATA])
14838     {
14839     default: /* fall through */
14840     case ELFDATANONE: /* fall through */
14841     case ELFDATA2LSB:
14842       byte_get = byte_get_little_endian;
14843       byte_put = byte_put_little_endian;
14844       break;
14845     case ELFDATA2MSB:
14846       byte_get = byte_get_big_endian;
14847       byte_put = byte_put_big_endian;
14848       break;
14849     }
14850 
14851   /* For now we only support 32 bit and 64 bit ELF files.  */
14852   is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
14853 
14854   /* Read in the rest of the header.  */
14855   if (is_32bit_elf)
14856     {
14857       Elf32_External_Ehdr ehdr32;
14858 
14859       if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
14860 	return 0;
14861 
14862       elf_header.e_type      = BYTE_GET (ehdr32.e_type);
14863       elf_header.e_machine   = BYTE_GET (ehdr32.e_machine);
14864       elf_header.e_version   = BYTE_GET (ehdr32.e_version);
14865       elf_header.e_entry     = BYTE_GET (ehdr32.e_entry);
14866       elf_header.e_phoff     = BYTE_GET (ehdr32.e_phoff);
14867       elf_header.e_shoff     = BYTE_GET (ehdr32.e_shoff);
14868       elf_header.e_flags     = BYTE_GET (ehdr32.e_flags);
14869       elf_header.e_ehsize    = BYTE_GET (ehdr32.e_ehsize);
14870       elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
14871       elf_header.e_phnum     = BYTE_GET (ehdr32.e_phnum);
14872       elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
14873       elf_header.e_shnum     = BYTE_GET (ehdr32.e_shnum);
14874       elf_header.e_shstrndx  = BYTE_GET (ehdr32.e_shstrndx);
14875     }
14876   else
14877     {
14878       Elf64_External_Ehdr ehdr64;
14879 
14880       /* If we have been compiled with sizeof (bfd_vma) == 4, then
14881 	 we will not be able to cope with the 64bit data found in
14882 	 64 ELF files.  Detect this now and abort before we start
14883 	 overwriting things.  */
14884       if (sizeof (bfd_vma) < 8)
14885 	{
14886 	  error (_("This instance of readelf has been built without support for a\n\
14887 64 bit data type and so it cannot read 64 bit ELF files.\n"));
14888 	  return 0;
14889 	}
14890 
14891       if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
14892 	return 0;
14893 
14894       elf_header.e_type      = BYTE_GET (ehdr64.e_type);
14895       elf_header.e_machine   = BYTE_GET (ehdr64.e_machine);
14896       elf_header.e_version   = BYTE_GET (ehdr64.e_version);
14897       elf_header.e_entry     = BYTE_GET (ehdr64.e_entry);
14898       elf_header.e_phoff     = BYTE_GET (ehdr64.e_phoff);
14899       elf_header.e_shoff     = BYTE_GET (ehdr64.e_shoff);
14900       elf_header.e_flags     = BYTE_GET (ehdr64.e_flags);
14901       elf_header.e_ehsize    = BYTE_GET (ehdr64.e_ehsize);
14902       elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
14903       elf_header.e_phnum     = BYTE_GET (ehdr64.e_phnum);
14904       elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
14905       elf_header.e_shnum     = BYTE_GET (ehdr64.e_shnum);
14906       elf_header.e_shstrndx  = BYTE_GET (ehdr64.e_shstrndx);
14907     }
14908 
14909   if (elf_header.e_shoff)
14910     {
14911       /* There may be some extensions in the first section header.  Don't
14912 	 bomb if we can't read it.  */
14913       if (is_32bit_elf)
14914 	get_32bit_section_headers (file, TRUE);
14915       else
14916 	get_64bit_section_headers (file, TRUE);
14917     }
14918 
14919   return 1;
14920 }
14921 
14922 /* Process one ELF object file according to the command line options.
14923    This file may actually be stored in an archive.  The file is
14924    positioned at the start of the ELF object.  */
14925 
14926 static int
process_object(char * file_name,FILE * file)14927 process_object (char * file_name, FILE * file)
14928 {
14929   unsigned int i;
14930 
14931   if (! get_file_header (file))
14932     {
14933       error (_("%s: Failed to read file header\n"), file_name);
14934       return 1;
14935     }
14936 
14937   /* Initialise per file variables.  */
14938   for (i = ARRAY_SIZE (version_info); i--;)
14939     version_info[i] = 0;
14940 
14941   for (i = ARRAY_SIZE (dynamic_info); i--;)
14942     dynamic_info[i] = 0;
14943   dynamic_info_DT_GNU_HASH = 0;
14944 
14945   /* Process the file.  */
14946   if (show_name)
14947     printf (_("\nFile: %s\n"), file_name);
14948 
14949   /* Initialise the dump_sects array from the cmdline_dump_sects array.
14950      Note we do this even if cmdline_dump_sects is empty because we
14951      must make sure that the dump_sets array is zeroed out before each
14952      object file is processed.  */
14953   if (num_dump_sects > num_cmdline_dump_sects)
14954     memset (dump_sects, 0, num_dump_sects * sizeof (* dump_sects));
14955 
14956   if (num_cmdline_dump_sects > 0)
14957     {
14958       if (num_dump_sects == 0)
14959 	/* A sneaky way of allocating the dump_sects array.  */
14960 	request_dump_bynumber (num_cmdline_dump_sects, 0);
14961 
14962       assert (num_dump_sects >= num_cmdline_dump_sects);
14963       memcpy (dump_sects, cmdline_dump_sects,
14964 	      num_cmdline_dump_sects * sizeof (* dump_sects));
14965     }
14966 
14967   if (! process_file_header ())
14968     return 1;
14969 
14970   if (! process_section_headers (file))
14971     {
14972       /* Without loaded section headers we cannot process lots of
14973 	 things.  */
14974       do_unwind = do_version = do_dump = do_arch = 0;
14975 
14976       if (! do_using_dynamic)
14977 	do_syms = do_dyn_syms = do_reloc = 0;
14978     }
14979 
14980   if (! process_section_groups (file))
14981     {
14982       /* Without loaded section groups we cannot process unwind.  */
14983       do_unwind = 0;
14984     }
14985 
14986   if (process_program_headers (file))
14987     process_dynamic_section (file);
14988 
14989   process_relocs (file);
14990 
14991   process_unwind (file);
14992 
14993   process_symbol_table (file);
14994 
14995   process_syminfo (file);
14996 
14997   process_version_sections (file);
14998 
14999   process_section_contents (file);
15000 
15001   process_notes (file);
15002 
15003   process_gnu_liblist (file);
15004 
15005   process_arch_specific (file);
15006 
15007   if (program_headers)
15008     {
15009       free (program_headers);
15010       program_headers = NULL;
15011     }
15012 
15013   if (section_headers)
15014     {
15015       free (section_headers);
15016       section_headers = NULL;
15017     }
15018 
15019   if (string_table)
15020     {
15021       free (string_table);
15022       string_table = NULL;
15023       string_table_length = 0;
15024     }
15025 
15026   if (dynamic_strings)
15027     {
15028       free (dynamic_strings);
15029       dynamic_strings = NULL;
15030       dynamic_strings_length = 0;
15031     }
15032 
15033   if (dynamic_symbols)
15034     {
15035       free (dynamic_symbols);
15036       dynamic_symbols = NULL;
15037       num_dynamic_syms = 0;
15038     }
15039 
15040   if (dynamic_syminfo)
15041     {
15042       free (dynamic_syminfo);
15043       dynamic_syminfo = NULL;
15044     }
15045 
15046   if (dynamic_section)
15047     {
15048       free (dynamic_section);
15049       dynamic_section = NULL;
15050     }
15051 
15052   if (section_headers_groups)
15053     {
15054       free (section_headers_groups);
15055       section_headers_groups = NULL;
15056     }
15057 
15058   if (section_groups)
15059     {
15060       struct group_list * g;
15061       struct group_list * next;
15062 
15063       for (i = 0; i < group_count; i++)
15064 	{
15065 	  for (g = section_groups [i].root; g != NULL; g = next)
15066 	    {
15067 	      next = g->next;
15068 	      free (g);
15069 	    }
15070 	}
15071 
15072       free (section_groups);
15073       section_groups = NULL;
15074     }
15075 
15076   free_debug_memory ();
15077 
15078   return 0;
15079 }
15080 
15081 /* Process an ELF archive.
15082    On entry the file is positioned just after the ARMAG string.  */
15083 
15084 static int
process_archive(char * file_name,FILE * file,bfd_boolean is_thin_archive)15085 process_archive (char * file_name, FILE * file, bfd_boolean is_thin_archive)
15086 {
15087   struct archive_info arch;
15088   struct archive_info nested_arch;
15089   size_t got;
15090   int ret;
15091 
15092   show_name = 1;
15093 
15094   /* The ARCH structure is used to hold information about this archive.  */
15095   arch.file_name = NULL;
15096   arch.file = NULL;
15097   arch.index_array = NULL;
15098   arch.sym_table = NULL;
15099   arch.longnames = NULL;
15100 
15101   /* The NESTED_ARCH structure is used as a single-item cache of information
15102      about a nested archive (when members of a thin archive reside within
15103      another regular archive file).  */
15104   nested_arch.file_name = NULL;
15105   nested_arch.file = NULL;
15106   nested_arch.index_array = NULL;
15107   nested_arch.sym_table = NULL;
15108   nested_arch.longnames = NULL;
15109 
15110   if (setup_archive (&arch, file_name, file, is_thin_archive, do_archive_index) != 0)
15111     {
15112       ret = 1;
15113       goto out;
15114     }
15115 
15116   if (do_archive_index)
15117     {
15118       if (arch.sym_table == NULL)
15119 	error (_("%s: unable to dump the index as none was found\n"), file_name);
15120       else
15121 	{
15122 	  unsigned int i, l;
15123 	  unsigned long current_pos;
15124 
15125 	  printf (_("Index of archive %s: (%ld entries, 0x%lx bytes in the symbol table)\n"),
15126 		  file_name, (long) arch.index_num, arch.sym_size);
15127 	  current_pos = ftell (file);
15128 
15129 	  for (i = l = 0; i < arch.index_num; i++)
15130 	    {
15131 	      if ((i == 0) || ((i > 0) && (arch.index_array[i] != arch.index_array[i - 1])))
15132 	        {
15133 	          char * member_name;
15134 
15135 		  member_name = get_archive_member_name_at (&arch, arch.index_array[i], &nested_arch);
15136 
15137                   if (member_name != NULL)
15138                     {
15139 	              char * qualified_name = make_qualified_name (&arch, &nested_arch, member_name);
15140 
15141                       if (qualified_name != NULL)
15142                         {
15143 		          printf (_("Contents of binary %s at offset "), qualified_name);
15144 			  (void) print_vma (arch.index_array[i], PREFIX_HEX);
15145 			  putchar ('\n');
15146 		          free (qualified_name);
15147 		        }
15148 		    }
15149 		}
15150 
15151 	      if (l >= arch.sym_size)
15152 		{
15153 		  error (_("%s: end of the symbol table reached before the end of the index\n"),
15154 			 file_name);
15155 		  break;
15156 		}
15157 	      printf ("\t%s\n", arch.sym_table + l);
15158 	      l += strlen (arch.sym_table + l) + 1;
15159 	    }
15160 
15161 	  if (arch.uses_64bit_indicies)
15162 	    l = (l + 7) & ~ 7;
15163 	  else
15164 	    l += l & 1;
15165 
15166 	  if (l < arch.sym_size)
15167 	    error (_("%s: %ld bytes remain in the symbol table, but without corresponding entries in the index table\n"),
15168 		   file_name, arch.sym_size - l);
15169 
15170 	  if (fseek (file, current_pos, SEEK_SET) != 0)
15171 	    {
15172 	      error (_("%s: failed to seek back to start of object files in the archive\n"), file_name);
15173 	      ret = 1;
15174 	      goto out;
15175 	    }
15176 	}
15177 
15178       if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
15179 	  && !do_segments && !do_header && !do_dump && !do_version
15180 	  && !do_histogram && !do_debugging && !do_arch && !do_notes
15181 	  && !do_section_groups && !do_dyn_syms)
15182 	{
15183 	  ret = 0; /* Archive index only.  */
15184 	  goto out;
15185 	}
15186     }
15187 
15188   ret = 0;
15189 
15190   while (1)
15191     {
15192       char * name;
15193       size_t namelen;
15194       char * qualified_name;
15195 
15196       /* Read the next archive header.  */
15197       if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
15198         {
15199           error (_("%s: failed to seek to next archive header\n"), file_name);
15200           return 1;
15201         }
15202       got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
15203       if (got != sizeof arch.arhdr)
15204         {
15205           if (got == 0)
15206 	    break;
15207           error (_("%s: failed to read archive header\n"), file_name);
15208           ret = 1;
15209           break;
15210         }
15211       if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
15212         {
15213           error (_("%s: did not find a valid archive header\n"), arch.file_name);
15214           ret = 1;
15215           break;
15216         }
15217 
15218       arch.next_arhdr_offset += sizeof arch.arhdr;
15219 
15220       archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
15221       if (archive_file_size & 01)
15222         ++archive_file_size;
15223 
15224       name = get_archive_member_name (&arch, &nested_arch);
15225       if (name == NULL)
15226 	{
15227 	  error (_("%s: bad archive file name\n"), file_name);
15228 	  ret = 1;
15229 	  break;
15230 	}
15231       namelen = strlen (name);
15232 
15233       qualified_name = make_qualified_name (&arch, &nested_arch, name);
15234       if (qualified_name == NULL)
15235 	{
15236 	  error (_("%s: bad archive file name\n"), file_name);
15237 	  ret = 1;
15238 	  break;
15239 	}
15240 
15241       if (is_thin_archive && arch.nested_member_origin == 0)
15242         {
15243           /* This is a proxy for an external member of a thin archive.  */
15244           FILE * member_file;
15245           char * member_file_name = adjust_relative_path (file_name, name, namelen);
15246           if (member_file_name == NULL)
15247             {
15248               ret = 1;
15249               break;
15250             }
15251 
15252           member_file = fopen (member_file_name, "rb");
15253           if (member_file == NULL)
15254             {
15255               error (_("Input file '%s' is not readable.\n"), member_file_name);
15256               free (member_file_name);
15257               ret = 1;
15258               break;
15259             }
15260 
15261           archive_file_offset = arch.nested_member_origin;
15262 
15263           ret |= process_object (qualified_name, member_file);
15264 
15265           fclose (member_file);
15266           free (member_file_name);
15267         }
15268       else if (is_thin_archive)
15269         {
15270 	  /* PR 15140: Allow for corrupt thin archives.  */
15271 	  if (nested_arch.file == NULL)
15272 	    {
15273 	      error (_("%s: contains corrupt thin archive: %s\n"),
15274 		     file_name, name);
15275 	      ret = 1;
15276 	      break;
15277 	    }
15278 
15279           /* This is a proxy for a member of a nested archive.  */
15280           archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
15281 
15282           /* The nested archive file will have been opened and setup by
15283              get_archive_member_name.  */
15284           if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
15285             {
15286               error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
15287               ret = 1;
15288               break;
15289             }
15290 
15291           ret |= process_object (qualified_name, nested_arch.file);
15292         }
15293       else
15294         {
15295           archive_file_offset = arch.next_arhdr_offset;
15296           arch.next_arhdr_offset += archive_file_size;
15297 
15298           ret |= process_object (qualified_name, file);
15299         }
15300 
15301       if (dump_sects != NULL)
15302 	{
15303 	  free (dump_sects);
15304 	  dump_sects = NULL;
15305 	  num_dump_sects = 0;
15306 	}
15307 
15308       free (qualified_name);
15309     }
15310 
15311  out:
15312   if (nested_arch.file != NULL)
15313     fclose (nested_arch.file);
15314   release_archive (&nested_arch);
15315   release_archive (&arch);
15316 
15317   return ret;
15318 }
15319 
15320 static int
process_file(char * file_name)15321 process_file (char * file_name)
15322 {
15323   FILE * file;
15324   struct stat statbuf;
15325   char armag[SARMAG];
15326   int ret;
15327 
15328   if (stat (file_name, &statbuf) < 0)
15329     {
15330       if (errno == ENOENT)
15331 	error (_("'%s': No such file\n"), file_name);
15332       else
15333 	error (_("Could not locate '%s'.  System error message: %s\n"),
15334 	       file_name, strerror (errno));
15335       return 1;
15336     }
15337 
15338   if (! S_ISREG (statbuf.st_mode))
15339     {
15340       error (_("'%s' is not an ordinary file\n"), file_name);
15341       return 1;
15342     }
15343 
15344   file = fopen (file_name, "rb");
15345   if (file == NULL)
15346     {
15347       error (_("Input file '%s' is not readable.\n"), file_name);
15348       return 1;
15349     }
15350 
15351   if (fread (armag, SARMAG, 1, file) != 1)
15352     {
15353       error (_("%s: Failed to read file's magic number\n"), file_name);
15354       fclose (file);
15355       return 1;
15356     }
15357 
15358   current_file_size = (bfd_size_type) statbuf.st_size;
15359 
15360   if (memcmp (armag, ARMAG, SARMAG) == 0)
15361     ret = process_archive (file_name, file, FALSE);
15362   else if (memcmp (armag, ARMAGT, SARMAG) == 0)
15363     ret = process_archive (file_name, file, TRUE);
15364   else
15365     {
15366       if (do_archive_index)
15367 	error (_("File %s is not an archive so its index cannot be displayed.\n"),
15368 	       file_name);
15369 
15370       rewind (file);
15371       archive_file_size = archive_file_offset = 0;
15372       ret = process_object (file_name, file);
15373     }
15374 
15375   fclose (file);
15376 
15377   current_file_size = 0;
15378   return ret;
15379 }
15380 
15381 #ifdef SUPPORT_DISASSEMBLY
15382 /* Needed by the i386 disassembler.  For extra credit, someone could
15383    fix this so that we insert symbolic addresses here, esp for GOT/PLT
15384    symbols.  */
15385 
15386 void
print_address(unsigned int addr,FILE * outfile)15387 print_address (unsigned int addr, FILE * outfile)
15388 {
15389   fprintf (outfile,"0x%8.8x", addr);
15390 }
15391 
15392 /* Needed by the i386 disassembler.  */
15393 void
db_task_printsym(unsigned int addr)15394 db_task_printsym (unsigned int addr)
15395 {
15396   print_address (addr, stderr);
15397 }
15398 #endif
15399 
15400 int
main(int argc,char ** argv)15401 main (int argc, char ** argv)
15402 {
15403   int err;
15404 
15405 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
15406   setlocale (LC_MESSAGES, "");
15407 #endif
15408 #if defined (HAVE_SETLOCALE)
15409   setlocale (LC_CTYPE, "");
15410 #endif
15411   bindtextdomain (PACKAGE, LOCALEDIR);
15412   textdomain (PACKAGE);
15413 
15414   expandargv (&argc, &argv);
15415 
15416   parse_args (argc, argv);
15417 
15418   if (num_dump_sects > 0)
15419     {
15420       /* Make a copy of the dump_sects array.  */
15421       cmdline_dump_sects = (dump_type *)
15422           malloc (num_dump_sects * sizeof (* dump_sects));
15423       if (cmdline_dump_sects == NULL)
15424 	error (_("Out of memory allocating dump request table.\n"));
15425       else
15426 	{
15427 	  memcpy (cmdline_dump_sects, dump_sects,
15428 		  num_dump_sects * sizeof (* dump_sects));
15429 	  num_cmdline_dump_sects = num_dump_sects;
15430 	}
15431     }
15432 
15433   if (optind < (argc - 1))
15434     show_name = 1;
15435 
15436   err = 0;
15437   while (optind < argc)
15438     err |= process_file (argv[optind++]);
15439 
15440   if (dump_sects != NULL)
15441     free (dump_sects);
15442   if (cmdline_dump_sects != NULL)
15443     free (cmdline_dump_sects);
15444 
15445   return err;
15446 }
15447