1 /* readelf.c -- display contents of an ELF format file
2    Copyright (C) 1998-2016 Free Software Foundation, Inc.
3 
4    Originally developed by Eric Youngdale <eric@andante.jic.com>
5    Modifications by Nick Clifton <nickc@redhat.com>
6 
7    This file is part of GNU Binutils.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22    02110-1301, USA.  */
23 
24 /* The difference between readelf and objdump:
25 
26   Both programs are capable of displaying the contents of ELF format files,
27   so why does the binutils project have two file dumpers ?
28 
29   The reason is that objdump sees an ELF file through a BFD filter of the
30   world; if BFD has a bug where, say, it disagrees about a machine constant
31   in e_flags, then the odds are good that it will remain internally
32   consistent.  The linker sees it the BFD way, objdump sees it the BFD way,
33   GAS sees it the BFD way.  There was need for a tool to go find out what
34   the file actually says.
35 
36   This is why the readelf program does not link against the BFD library - it
37   exists as an independent program to help verify the correct working of BFD.
38 
39   There is also the case that readelf can provide more information about an
40   ELF file than is provided by objdump.  In particular it can display DWARF
41   debugging information which (at the moment) objdump cannot.  */
42 
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #ifdef HAVE_WCHAR_H
48 #include <wchar.h>
49 #endif
50 
51 #if __GNUC__ >= 2
52 /* Define BFD64 here, even if our default architecture is 32 bit ELF
53    as this will allow us to read in and parse 64bit and 32bit ELF files.
54    Only do this if we believe that the compiler can support a 64 bit
55    data type.  For now we only rely on GCC being able to do this.  */
56 #define BFD64
57 #endif
58 
59 #include "bfd.h"
60 #include "bucomm.h"
61 #include "elfcomm.h"
62 #include "dwarf.h"
63 
64 #include "elf/common.h"
65 #include "elf/external.h"
66 #include "elf/internal.h"
67 
68 
69 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
70    we can obtain the H8 reloc numbers.  We need these for the
71    get_reloc_size() function.  We include h8.h again after defining
72    RELOC_MACROS_GEN_FUNC so that we get the naming function as well.  */
73 
74 #include "elf/h8.h"
75 #undef _ELF_H8_H
76 
77 /* Undo the effects of #including reloc-macros.h.  */
78 
79 #undef START_RELOC_NUMBERS
80 #undef RELOC_NUMBER
81 #undef FAKE_RELOC
82 #undef EMPTY_RELOC
83 #undef END_RELOC_NUMBERS
84 #undef _RELOC_MACROS_H
85 
86 /* The following headers use the elf/reloc-macros.h file to
87    automatically generate relocation recognition functions
88    such as elf_mips_reloc_type()  */
89 
90 #define RELOC_MACROS_GEN_FUNC
91 
92 #include "elf/aarch64.h"
93 #include "elf/alpha.h"
94 #include "elf/arc.h"
95 #include "elf/arm.h"
96 #include "elf/avr.h"
97 #include "elf/bfin.h"
98 #include "elf/cr16.h"
99 #include "elf/cris.h"
100 #include "elf/crx.h"
101 #include "elf/d10v.h"
102 #include "elf/d30v.h"
103 #include "elf/dlx.h"
104 #include "elf/epiphany.h"
105 #include "elf/fr30.h"
106 #include "elf/frv.h"
107 #include "elf/ft32.h"
108 #include "elf/h8.h"
109 #include "elf/hppa.h"
110 #include "elf/i386.h"
111 #include "elf/i370.h"
112 #include "elf/i860.h"
113 #include "elf/i960.h"
114 #include "elf/ia64.h"
115 #include "elf/ip2k.h"
116 #include "elf/lm32.h"
117 #include "elf/iq2000.h"
118 #include "elf/m32c.h"
119 #include "elf/m32r.h"
120 #include "elf/m68k.h"
121 #include "elf/m68hc11.h"
122 #include "elf/mcore.h"
123 #include "elf/mep.h"
124 #include "elf/metag.h"
125 #include "elf/microblaze.h"
126 #include "elf/mips.h"
127 #include "elf/mmix.h"
128 #include "elf/mn10200.h"
129 #include "elf/mn10300.h"
130 #include "elf/moxie.h"
131 #include "elf/mt.h"
132 #include "elf/msp430.h"
133 #include "elf/nds32.h"
134 #include "elf/nios2.h"
135 #include "elf/or1k.h"
136 #include "elf/pj.h"
137 #include "elf/ppc.h"
138 #include "elf/ppc64.h"
139 #include "elf/rl78.h"
140 #include "elf/rx.h"
141 #include "elf/s390.h"
142 #include "elf/score.h"
143 #include "elf/sh.h"
144 #include "elf/sparc.h"
145 #include "elf/spu.h"
146 #include "elf/tic6x.h"
147 #include "elf/tilegx.h"
148 #include "elf/tilepro.h"
149 #include "elf/v850.h"
150 #include "elf/vax.h"
151 #include "elf/visium.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 typedef struct elf_section_list
168 {
169   Elf_Internal_Shdr * hdr;
170   struct elf_section_list * next;
171 } elf_section_list;
172 
173 char * program_name = "readelf";
174 static unsigned long archive_file_offset;
175 static unsigned long archive_file_size;
176 static bfd_size_type current_file_size;
177 static unsigned long dynamic_addr;
178 static bfd_size_type dynamic_size;
179 static size_t dynamic_nent;
180 static char * dynamic_strings;
181 static unsigned long dynamic_strings_length;
182 static char * string_table;
183 static unsigned long string_table_length;
184 static unsigned long num_dynamic_syms;
185 static Elf_Internal_Sym * dynamic_symbols;
186 static Elf_Internal_Syminfo * dynamic_syminfo;
187 static unsigned long dynamic_syminfo_offset;
188 static unsigned int dynamic_syminfo_nent;
189 static char program_interpreter[PATH_MAX];
190 static bfd_vma dynamic_info[DT_ENCODING];
191 static bfd_vma dynamic_info_DT_GNU_HASH;
192 static bfd_vma version_info[16];
193 static Elf_Internal_Ehdr elf_header;
194 static Elf_Internal_Shdr * section_headers;
195 static Elf_Internal_Phdr * program_headers;
196 static Elf_Internal_Dyn *  dynamic_section;
197 static elf_section_list * symtab_shndx_list;
198 static int show_name;
199 static int do_dynamic;
200 static int do_syms;
201 static int do_dyn_syms;
202 static int do_reloc;
203 static int do_sections;
204 static int do_section_groups;
205 static int do_section_details;
206 static int do_segments;
207 static int do_unwind;
208 static int do_using_dynamic;
209 static int do_header;
210 static int do_dump;
211 static int do_version;
212 static int do_histogram;
213 static int do_debugging;
214 static int do_arch;
215 static int do_notes;
216 static int do_archive_index;
217 static int is_32bit_elf;
218 static int decompress_dumps;
219 
220 struct group_list
221 {
222   struct group_list * next;
223   unsigned int section_index;
224 };
225 
226 struct group
227 {
228   struct group_list * root;
229   unsigned int group_index;
230 };
231 
232 static size_t group_count;
233 static struct group * section_groups;
234 static struct group ** section_headers_groups;
235 
236 
237 /* Flag bits indicating particular types of dump.  */
238 #define HEX_DUMP	(1 << 0)	/* The -x command line switch.  */
239 #define DISASS_DUMP	(1 << 1)	/* The -i command line switch.  */
240 #define DEBUG_DUMP	(1 << 2)	/* The -w command line switch.  */
241 #define STRING_DUMP     (1 << 3)	/* The -p command line switch.  */
242 #define RELOC_DUMP      (1 << 4)	/* The -R command line switch.  */
243 
244 typedef unsigned char dump_type;
245 
246 /* A linked list of the section names for which dumps were requested.  */
247 struct dump_list_entry
248 {
249   char * name;
250   dump_type type;
251   struct dump_list_entry * next;
252 };
253 static struct dump_list_entry * dump_sects_byname;
254 
255 /* A dynamic array of flags indicating for which sections a dump
256    has been requested via command line switches.  */
257 static dump_type *   cmdline_dump_sects = NULL;
258 static unsigned int  num_cmdline_dump_sects = 0;
259 
260 /* A dynamic array of flags indicating for which sections a dump of
261    some kind has been requested.  It is reset on a per-object file
262    basis and then initialised from the cmdline_dump_sects array,
263    the results of interpreting the -w switch, and the
264    dump_sects_byname list.  */
265 static dump_type *   dump_sects = NULL;
266 static unsigned int  num_dump_sects = 0;
267 
268 
269 /* How to print a vma value.  */
270 typedef enum print_mode
271 {
272   HEX,
273   DEC,
274   DEC_5,
275   UNSIGNED,
276   PREFIX_HEX,
277   FULL_HEX,
278   LONG_HEX
279 }
280 print_mode;
281 
282 /* Versioned symbol info.  */
283 enum versioned_symbol_info
284 {
285   symbol_undefined,
286   symbol_hidden,
287   symbol_public
288 };
289 
290 static const char *get_symbol_version_string
291   (FILE *file, int is_dynsym, const char *strtab,
292    unsigned long int strtab_size, unsigned int si,
293    Elf_Internal_Sym *psym, enum versioned_symbol_info *sym_info,
294    unsigned short *vna_other);
295 
296 #define UNKNOWN -1
297 
298 #define SECTION_NAME(X)						\
299   ((X) == NULL ? _("<none>")					\
300    : string_table == NULL ? _("<no-name>")			\
301    : ((X)->sh_name >= string_table_length ? _("<corrupt>")	\
302   : string_table + (X)->sh_name))
303 
304 #define DT_VERSIONTAGIDX(tag)	(DT_VERNEEDNUM - (tag))	/* Reverse order!  */
305 
306 #define GET_ELF_SYMBOLS(file, section, sym_count)			\
307   (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count)	\
308    : get_64bit_elf_symbols (file, section, sym_count))
309 
310 #define VALID_DYNAMIC_NAME(offset)	((dynamic_strings != NULL) && (offset < dynamic_strings_length))
311 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
312    already been called and verified that the string exists.  */
313 #define GET_DYNAMIC_NAME(offset)	(dynamic_strings + offset)
314 
315 #define REMOVE_ARCH_BITS(ADDR)			\
316   do						\
317     {						\
318       if (elf_header.e_machine == EM_ARM)	\
319 	(ADDR) &= ~1;				\
320     }						\
321   while (0)
322 
323 /* Retrieve NMEMB structures, each SIZE bytes long from FILE starting at OFFSET +
324    the offset of the current archive member, if we are examining an archive.
325    Put the retrieved data into VAR, if it is not NULL.  Otherwise allocate a buffer
326    using malloc and fill that.  In either case return the pointer to the start of
327    the retrieved data or NULL if something went wrong.  If something does go wrong
328    and REASON is not NULL then emit an error message using REASON as part of the
329    context.  */
330 
331 static void *
get_data(void * var,FILE * file,unsigned long offset,bfd_size_type size,bfd_size_type nmemb,const char * reason)332 get_data (void * var, FILE * file, unsigned long offset, bfd_size_type size,
333 	  bfd_size_type nmemb, const char * reason)
334 {
335   void * mvar;
336   bfd_size_type amt = size * nmemb;
337 
338   if (size == 0 || nmemb == 0)
339     return NULL;
340 
341   /* If the size_t type is smaller than the bfd_size_type, eg because
342      you are building a 32-bit tool on a 64-bit host, then make sure
343      that when the sizes are cast to (size_t) no information is lost.  */
344   if (sizeof (size_t) < sizeof (bfd_size_type)
345       && (   (bfd_size_type) ((size_t) size) != size
346 	  || (bfd_size_type) ((size_t) nmemb) != nmemb))
347     {
348       if (reason)
349 	error (_("Size truncation prevents reading 0x%" BFD_VMA_FMT "x"
350 		 " elements of size 0x%" BFD_VMA_FMT "x for %s\n"),
351 	       nmemb, size, reason);
352       return NULL;
353     }
354 
355   /* Check for size overflow.  */
356   if (amt < nmemb)
357     {
358       if (reason)
359 	error (_("Size overflow prevents reading 0x%" BFD_VMA_FMT "x"
360 		 " elements of size 0x%" BFD_VMA_FMT "x for %s\n"),
361 	       nmemb, size, reason);
362       return NULL;
363     }
364 
365   /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
366      attempting to allocate memory when the read is bound to fail.  */
367   if (amt > current_file_size
368       || offset + archive_file_offset + amt > current_file_size)
369     {
370       if (reason)
371 	error (_("Reading 0x%" BFD_VMA_FMT "x"
372 		 " bytes extends past end of file for %s\n"),
373 	       amt, reason);
374       return NULL;
375     }
376 
377   if (fseek (file, archive_file_offset + offset, SEEK_SET))
378     {
379       if (reason)
380 	error (_("Unable to seek to 0x%lx for %s\n"),
381 	       archive_file_offset + offset, reason);
382       return NULL;
383     }
384 
385   mvar = var;
386   if (mvar == NULL)
387     {
388       /* Check for overflow.  */
389       if (nmemb < (~(bfd_size_type) 0 - 1) / size)
390 	/* + 1 so that we can '\0' terminate invalid string table sections.  */
391 	mvar = malloc ((size_t) amt + 1);
392 
393       if (mvar == NULL)
394 	{
395 	  if (reason)
396 	    error (_("Out of memory allocating 0x%" BFD_VMA_FMT "x"
397 		     " bytes for %s\n"),
398 		   amt, reason);
399 	  return NULL;
400 	}
401 
402       ((char *) mvar)[amt] = '\0';
403     }
404 
405   if (fread (mvar, (size_t) size, (size_t) nmemb, file) != nmemb)
406     {
407       if (reason)
408 	error (_("Unable to read in 0x%" BFD_VMA_FMT "x bytes of %s\n"),
409 	       amt, reason);
410       if (mvar != var)
411 	free (mvar);
412       return NULL;
413     }
414 
415   return mvar;
416 }
417 
418 /* Print a VMA value.  */
419 
420 static int
print_vma(bfd_vma vma,print_mode mode)421 print_vma (bfd_vma vma, print_mode mode)
422 {
423   int nc = 0;
424 
425   switch (mode)
426     {
427     case FULL_HEX:
428       nc = printf ("0x");
429       /* Drop through.  */
430 
431     case LONG_HEX:
432 #ifdef BFD64
433       if (is_32bit_elf)
434 	return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
435 #endif
436       printf_vma (vma);
437       return nc + 16;
438 
439     case DEC_5:
440       if (vma <= 99999)
441 	return printf ("%5" BFD_VMA_FMT "d", vma);
442       /* Drop through.  */
443 
444     case PREFIX_HEX:
445       nc = printf ("0x");
446       /* Drop through.  */
447 
448     case HEX:
449       return nc + printf ("%" BFD_VMA_FMT "x", vma);
450 
451     case DEC:
452       return printf ("%" BFD_VMA_FMT "d", vma);
453 
454     case UNSIGNED:
455       return printf ("%" BFD_VMA_FMT "u", vma);
456     }
457   return 0;
458 }
459 
460 /* Display a symbol on stdout.  Handles the display of control characters and
461    multibye characters (assuming the host environment supports them).
462 
463    Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
464 
465    If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
466    padding as necessary.
467 
468    Returns the number of emitted characters.  */
469 
470 static unsigned int
print_symbol(int width,const char * symbol)471 print_symbol (int width, const char *symbol)
472 {
473   bfd_boolean extra_padding = FALSE;
474   int num_printed = 0;
475 #ifdef HAVE_MBSTATE_T
476   mbstate_t state;
477 #endif
478   int width_remaining;
479 
480   if (width < 0)
481     {
482       /* Keep the width positive.  This also helps.  */
483       width = - width;
484       extra_padding = TRUE;
485     }
486   assert (width != 0);
487 
488   if (do_wide)
489     /* Set the remaining width to a very large value.
490        This simplifies the code below.  */
491     width_remaining = INT_MAX;
492   else
493     width_remaining = width;
494 
495 #ifdef HAVE_MBSTATE_T
496   /* Initialise the multibyte conversion state.  */
497   memset (& state, 0, sizeof (state));
498 #endif
499 
500   while (width_remaining)
501     {
502       size_t  n;
503       const char c = *symbol++;
504 
505       if (c == 0)
506 	break;
507 
508       /* Do not print control characters directly as they can affect terminal
509 	 settings.  Such characters usually appear in the names generated
510 	 by the assembler for local labels.  */
511       if (ISCNTRL (c))
512 	{
513 	  if (width_remaining < 2)
514 	    break;
515 
516 	  printf ("^%c", c + 0x40);
517 	  width_remaining -= 2;
518 	  num_printed += 2;
519 	}
520       else if (ISPRINT (c))
521 	{
522 	  putchar (c);
523 	  width_remaining --;
524 	  num_printed ++;
525 	}
526       else
527 	{
528 #ifdef HAVE_MBSTATE_T
529 	  wchar_t w;
530 #endif
531 	  /* Let printf do the hard work of displaying multibyte characters.  */
532 	  printf ("%.1s", symbol - 1);
533 	  width_remaining --;
534 	  num_printed ++;
535 
536 #ifdef HAVE_MBSTATE_T
537 	  /* Try to find out how many bytes made up the character that was
538 	     just printed.  Advance the symbol pointer past the bytes that
539 	     were displayed.  */
540 	  n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
541 #else
542 	  n = 1;
543 #endif
544 	  if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
545 	    symbol += (n - 1);
546 	}
547     }
548 
549   if (extra_padding && num_printed < width)
550     {
551       /* Fill in the remaining spaces.  */
552       printf ("%-*s", width - num_printed, " ");
553       num_printed = width;
554     }
555 
556   return num_printed;
557 }
558 
559 /* Returns a pointer to a static buffer containing a  printable version of
560    the given section's name.  Like print_symbol, except that it does not try
561    to print multibyte characters, it just interprets them as hex values.  */
562 
563 static const char *
printable_section_name(const Elf_Internal_Shdr * sec)564 printable_section_name (const Elf_Internal_Shdr * sec)
565 {
566 #define MAX_PRINT_SEC_NAME_LEN 128
567   static char  sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
568   const char * name = SECTION_NAME (sec);
569   char *       buf = sec_name_buf;
570   char         c;
571   unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
572 
573   while ((c = * name ++) != 0)
574     {
575       if (ISCNTRL (c))
576 	{
577 	  if (remaining < 2)
578 	    break;
579 
580 	  * buf ++ = '^';
581 	  * buf ++ = c + 0x40;
582 	  remaining -= 2;
583 	}
584       else if (ISPRINT (c))
585 	{
586 	  * buf ++ = c;
587 	  remaining -= 1;
588 	}
589       else
590 	{
591 	  static char hex[17] = "0123456789ABCDEF";
592 
593 	  if (remaining < 4)
594 	    break;
595 	  * buf ++ = '<';
596 	  * buf ++ = hex[(c & 0xf0) >> 4];
597 	  * buf ++ = hex[c & 0x0f];
598 	  * buf ++ = '>';
599 	  remaining -= 4;
600 	}
601 
602       if (remaining == 0)
603 	break;
604     }
605 
606   * buf = 0;
607   return sec_name_buf;
608 }
609 
610 static const char *
printable_section_name_from_index(unsigned long ndx)611 printable_section_name_from_index (unsigned long ndx)
612 {
613   if (ndx >= elf_header.e_shnum)
614     return _("<corrupt>");
615 
616   return printable_section_name (section_headers + ndx);
617 }
618 
619 /* Return a pointer to section NAME, or NULL if no such section exists.  */
620 
621 static Elf_Internal_Shdr *
find_section(const char * name)622 find_section (const char * name)
623 {
624   unsigned int i;
625 
626   for (i = 0; i < elf_header.e_shnum; i++)
627     if (streq (SECTION_NAME (section_headers + i), name))
628       return section_headers + i;
629 
630   return NULL;
631 }
632 
633 /* Return a pointer to a section containing ADDR, or NULL if no such
634    section exists.  */
635 
636 static Elf_Internal_Shdr *
find_section_by_address(bfd_vma addr)637 find_section_by_address (bfd_vma addr)
638 {
639   unsigned int i;
640 
641   for (i = 0; i < elf_header.e_shnum; i++)
642     {
643       Elf_Internal_Shdr *sec = section_headers + i;
644       if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
645 	return sec;
646     }
647 
648   return NULL;
649 }
650 
651 static Elf_Internal_Shdr *
find_section_by_type(unsigned int type)652 find_section_by_type (unsigned int type)
653 {
654   unsigned int i;
655 
656   for (i = 0; i < elf_header.e_shnum; i++)
657     {
658       Elf_Internal_Shdr *sec = section_headers + i;
659       if (sec->sh_type == type)
660 	return sec;
661     }
662 
663   return NULL;
664 }
665 
666 /* Return a pointer to section NAME, or NULL if no such section exists,
667    restricted to the list of sections given in SET.  */
668 
669 static Elf_Internal_Shdr *
find_section_in_set(const char * name,unsigned int * set)670 find_section_in_set (const char * name, unsigned int * set)
671 {
672   unsigned int i;
673 
674   if (set != NULL)
675     {
676       while ((i = *set++) > 0)
677 	if (streq (SECTION_NAME (section_headers + i), name))
678 	  return section_headers + i;
679     }
680 
681   return find_section (name);
682 }
683 
684 /* Read an unsigned LEB128 encoded value from p.  Set *PLEN to the number of
685    bytes read.  */
686 
687 static inline unsigned long
read_uleb128(unsigned char * data,unsigned int * length_return,const unsigned char * const end)688 read_uleb128 (unsigned char *data,
689 	      unsigned int *length_return,
690 	      const unsigned char * const end)
691 {
692   return read_leb128 (data, length_return, FALSE, end);
693 }
694 
695 /* Return true if the current file is for IA-64 machine and OpenVMS ABI.
696    This OS has so many departures from the ELF standard that we test it at
697    many places.  */
698 
699 static inline int
is_ia64_vms(void)700 is_ia64_vms (void)
701 {
702   return elf_header.e_machine == EM_IA_64
703     && elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
704 }
705 
706 /* Guess the relocation size commonly used by the specific machines.  */
707 
708 static int
guess_is_rela(unsigned int e_machine)709 guess_is_rela (unsigned int e_machine)
710 {
711   switch (e_machine)
712     {
713       /* Targets that use REL relocations.  */
714     case EM_386:
715     case EM_IAMCU:
716     case EM_960:
717     case EM_ARM:
718     case EM_D10V:
719     case EM_CYGNUS_D10V:
720     case EM_DLX:
721     case EM_MIPS:
722     case EM_MIPS_RS3_LE:
723     case EM_CYGNUS_M32R:
724     case EM_SCORE:
725     case EM_XGATE:
726       return FALSE;
727 
728       /* Targets that use RELA relocations.  */
729     case EM_68K:
730     case EM_860:
731     case EM_AARCH64:
732     case EM_ADAPTEVA_EPIPHANY:
733     case EM_ALPHA:
734     case EM_ALTERA_NIOS2:
735     case EM_ARC:
736     case EM_ARC_COMPACT:
737     case EM_ARC_COMPACT2:
738     case EM_AVR:
739     case EM_AVR_OLD:
740     case EM_BLACKFIN:
741     case EM_CR16:
742     case EM_CRIS:
743     case EM_CRX:
744     case EM_D30V:
745     case EM_CYGNUS_D30V:
746     case EM_FR30:
747     case EM_FT32:
748     case EM_CYGNUS_FR30:
749     case EM_CYGNUS_FRV:
750     case EM_H8S:
751     case EM_H8_300:
752     case EM_H8_300H:
753     case EM_IA_64:
754     case EM_IP2K:
755     case EM_IP2K_OLD:
756     case EM_IQ2000:
757     case EM_LATTICEMICO32:
758     case EM_M32C_OLD:
759     case EM_M32C:
760     case EM_M32R:
761     case EM_MCORE:
762     case EM_CYGNUS_MEP:
763     case EM_METAG:
764     case EM_MMIX:
765     case EM_MN10200:
766     case EM_CYGNUS_MN10200:
767     case EM_MN10300:
768     case EM_CYGNUS_MN10300:
769     case EM_MOXIE:
770     case EM_MSP430:
771     case EM_MSP430_OLD:
772     case EM_MT:
773     case EM_NDS32:
774     case EM_NIOS32:
775     case EM_OR1K:
776     case EM_PPC64:
777     case EM_PPC:
778     case EM_RL78:
779     case EM_RX:
780     case EM_S390:
781     case EM_S390_OLD:
782     case EM_SH:
783     case EM_SPARC:
784     case EM_SPARC32PLUS:
785     case EM_SPARCV9:
786     case EM_SPU:
787     case EM_TI_C6000:
788     case EM_TILEGX:
789     case EM_TILEPRO:
790     case EM_V800:
791     case EM_V850:
792     case EM_CYGNUS_V850:
793     case EM_VAX:
794     case EM_VISIUM:
795     case EM_X86_64:
796     case EM_L1OM:
797     case EM_K1OM:
798     case EM_XSTORMY16:
799     case EM_XTENSA:
800     case EM_XTENSA_OLD:
801     case EM_MICROBLAZE:
802     case EM_MICROBLAZE_OLD:
803       return TRUE;
804 
805     case EM_68HC05:
806     case EM_68HC08:
807     case EM_68HC11:
808     case EM_68HC16:
809     case EM_FX66:
810     case EM_ME16:
811     case EM_MMA:
812     case EM_NCPU:
813     case EM_NDR1:
814     case EM_PCP:
815     case EM_ST100:
816     case EM_ST19:
817     case EM_ST7:
818     case EM_ST9PLUS:
819     case EM_STARCORE:
820     case EM_SVX:
821     case EM_TINYJ:
822     default:
823       warn (_("Don't know about relocations on this machine architecture\n"));
824       return FALSE;
825     }
826 }
827 
828 static int
slurp_rela_relocs(FILE * file,unsigned long rel_offset,unsigned long rel_size,Elf_Internal_Rela ** relasp,unsigned long * nrelasp)829 slurp_rela_relocs (FILE * file,
830 		   unsigned long rel_offset,
831 		   unsigned long rel_size,
832 		   Elf_Internal_Rela ** relasp,
833 		   unsigned long * nrelasp)
834 {
835   Elf_Internal_Rela * relas;
836   size_t nrelas;
837   unsigned int i;
838 
839   if (is_32bit_elf)
840     {
841       Elf32_External_Rela * erelas;
842 
843       erelas = (Elf32_External_Rela *) get_data (NULL, file, rel_offset, 1,
844                                                  rel_size, _("32-bit relocation data"));
845       if (!erelas)
846 	return 0;
847 
848       nrelas = rel_size / sizeof (Elf32_External_Rela);
849 
850       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
851                                              sizeof (Elf_Internal_Rela));
852 
853       if (relas == NULL)
854 	{
855 	  free (erelas);
856 	  error (_("out of memory parsing relocs\n"));
857 	  return 0;
858 	}
859 
860       for (i = 0; i < nrelas; i++)
861 	{
862 	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
863 	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
864 	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
865 	}
866 
867       free (erelas);
868     }
869   else
870     {
871       Elf64_External_Rela * erelas;
872 
873       erelas = (Elf64_External_Rela *) get_data (NULL, file, rel_offset, 1,
874                                                  rel_size, _("64-bit relocation data"));
875       if (!erelas)
876 	return 0;
877 
878       nrelas = rel_size / sizeof (Elf64_External_Rela);
879 
880       relas = (Elf_Internal_Rela *) cmalloc (nrelas,
881                                              sizeof (Elf_Internal_Rela));
882 
883       if (relas == NULL)
884 	{
885 	  free (erelas);
886 	  error (_("out of memory parsing relocs\n"));
887 	  return 0;
888 	}
889 
890       for (i = 0; i < nrelas; i++)
891 	{
892 	  relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
893 	  relas[i].r_info   = BYTE_GET (erelas[i].r_info);
894 	  relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
895 
896 	  /* The #ifdef BFD64 below is to prevent a compile time
897 	     warning.  We know that if we do not have a 64 bit data
898 	     type that we will never execute this code anyway.  */
899 #ifdef BFD64
900 	  if (elf_header.e_machine == EM_MIPS
901 	      && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
902 	    {
903 	      /* In little-endian objects, r_info isn't really a
904 		 64-bit little-endian value: it has a 32-bit
905 		 little-endian symbol index followed by four
906 		 individual byte fields.  Reorder INFO
907 		 accordingly.  */
908 	      bfd_vma inf = relas[i].r_info;
909 	      inf = (((inf & 0xffffffff) << 32)
910 		      | ((inf >> 56) & 0xff)
911 		      | ((inf >> 40) & 0xff00)
912 		      | ((inf >> 24) & 0xff0000)
913 		      | ((inf >> 8) & 0xff000000));
914 	      relas[i].r_info = inf;
915 	    }
916 #endif /* BFD64 */
917 	}
918 
919       free (erelas);
920     }
921   *relasp = relas;
922   *nrelasp = nrelas;
923   return 1;
924 }
925 
926 static int
slurp_rel_relocs(FILE * file,unsigned long rel_offset,unsigned long rel_size,Elf_Internal_Rela ** relsp,unsigned long * nrelsp)927 slurp_rel_relocs (FILE * file,
928 		  unsigned long rel_offset,
929 		  unsigned long rel_size,
930 		  Elf_Internal_Rela ** relsp,
931 		  unsigned long * nrelsp)
932 {
933   Elf_Internal_Rela * rels;
934   size_t nrels;
935   unsigned int i;
936 
937   if (is_32bit_elf)
938     {
939       Elf32_External_Rel * erels;
940 
941       erels = (Elf32_External_Rel *) get_data (NULL, file, rel_offset, 1,
942                                                rel_size, _("32-bit relocation data"));
943       if (!erels)
944 	return 0;
945 
946       nrels = rel_size / sizeof (Elf32_External_Rel);
947 
948       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
949 
950       if (rels == NULL)
951 	{
952 	  free (erels);
953 	  error (_("out of memory parsing relocs\n"));
954 	  return 0;
955 	}
956 
957       for (i = 0; i < nrels; i++)
958 	{
959 	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
960 	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
961 	  rels[i].r_addend = 0;
962 	}
963 
964       free (erels);
965     }
966   else
967     {
968       Elf64_External_Rel * erels;
969 
970       erels = (Elf64_External_Rel *) get_data (NULL, file, rel_offset, 1,
971                                                rel_size, _("64-bit relocation data"));
972       if (!erels)
973 	return 0;
974 
975       nrels = rel_size / sizeof (Elf64_External_Rel);
976 
977       rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
978 
979       if (rels == NULL)
980 	{
981 	  free (erels);
982 	  error (_("out of memory parsing relocs\n"));
983 	  return 0;
984 	}
985 
986       for (i = 0; i < nrels; i++)
987 	{
988 	  rels[i].r_offset = BYTE_GET (erels[i].r_offset);
989 	  rels[i].r_info   = BYTE_GET (erels[i].r_info);
990 	  rels[i].r_addend = 0;
991 
992 	  /* The #ifdef BFD64 below is to prevent a compile time
993 	     warning.  We know that if we do not have a 64 bit data
994 	     type that we will never execute this code anyway.  */
995 #ifdef BFD64
996 	  if (elf_header.e_machine == EM_MIPS
997 	      && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
998 	    {
999 	      /* In little-endian objects, r_info isn't really a
1000 		 64-bit little-endian value: it has a 32-bit
1001 		 little-endian symbol index followed by four
1002 		 individual byte fields.  Reorder INFO
1003 		 accordingly.  */
1004 	      bfd_vma inf = rels[i].r_info;
1005 	      inf = (((inf & 0xffffffff) << 32)
1006 		     | ((inf >> 56) & 0xff)
1007 		     | ((inf >> 40) & 0xff00)
1008 		     | ((inf >> 24) & 0xff0000)
1009 		     | ((inf >> 8) & 0xff000000));
1010 	      rels[i].r_info = inf;
1011 	    }
1012 #endif /* BFD64 */
1013 	}
1014 
1015       free (erels);
1016     }
1017   *relsp = rels;
1018   *nrelsp = nrels;
1019   return 1;
1020 }
1021 
1022 static bfd_boolean
slurp_relr_relocs(FILE * file,unsigned long relr_offset,unsigned long relr_size,Elf_Internal_Rela ** relrsp,unsigned long * nrelrsp)1023 slurp_relr_relocs (FILE * file,
1024 		   unsigned long relr_offset,
1025 		   unsigned long relr_size,
1026 		   Elf_Internal_Rela ** relrsp,
1027 		   unsigned long * nrelrsp)
1028 {
1029   Elf_Internal_Rela * relrs;
1030   size_t nrelrs;
1031   unsigned int i;
1032 
1033   if (is_32bit_elf)
1034     {
1035       Elf32_External_Relr * erelrs;
1036 
1037       erelrs = (Elf32_External_Relr *) get_data (NULL, file, relr_offset, 1,
1038                                                  relr_size, _("32-bit relocation data"));
1039       if (!erelrs)
1040 	return FALSE;
1041 
1042       nrelrs = relr_size / sizeof (Elf32_External_Relr);
1043 
1044       relrs = (Elf_Internal_Rela *) cmalloc (nrelrs, sizeof (Elf_Internal_Rela));
1045 
1046       if (relrs == NULL)
1047 	{
1048 	  free (erelrs);
1049 	  error (_("out of memory parsing relocs\n"));
1050 	  return FALSE;
1051 	}
1052 
1053       for (i = 0; i < nrelrs; i++)
1054 	{
1055 	  relrs[i].r_offset = BYTE_GET (erelrs[i].r_data);
1056 	  relrs[i].r_info   = 0;
1057 	  relrs[i].r_addend = 0;
1058 	}
1059 
1060       free (erelrs);
1061     }
1062   else
1063     {
1064       Elf64_External_Relr * erelrs;
1065 
1066       erelrs = (Elf64_External_Relr *) get_data (NULL, file, relr_offset, 1,
1067                                                  relr_size, _("64-bit relocation data"));
1068       if (!erelrs)
1069 	return FALSE;
1070 
1071       nrelrs = relr_size / sizeof (Elf64_External_Relr);
1072 
1073       relrs = (Elf_Internal_Rela *) cmalloc (nrelrs, sizeof (Elf_Internal_Rela));
1074 
1075       if (relrs == NULL)
1076 	{
1077 	  free (erelrs);
1078 	  error (_("out of memory parsing relocs\n"));
1079 	  return FALSE;
1080 	}
1081 
1082       for (i = 0; i < nrelrs; i++)
1083 	{
1084 	  relrs[i].r_offset = BYTE_GET (erelrs[i].r_data);
1085 	  relrs[i].r_info   = 0;
1086 	  relrs[i].r_addend = 0;
1087 	}
1088 
1089       free (erelrs);
1090     }
1091   *relrsp = relrs;
1092   *nrelrsp = nrelrs;
1093   return 1;
1094 }
1095 
1096 /* Returns the reloc type extracted from the reloc info field.  */
1097 
1098 static unsigned int
get_reloc_type(bfd_vma reloc_info)1099 get_reloc_type (bfd_vma reloc_info)
1100 {
1101   if (is_32bit_elf)
1102     return ELF32_R_TYPE (reloc_info);
1103 
1104   switch (elf_header.e_machine)
1105     {
1106     case EM_MIPS:
1107       /* Note: We assume that reloc_info has already been adjusted for us.  */
1108       return ELF64_MIPS_R_TYPE (reloc_info);
1109 
1110     case EM_SPARCV9:
1111       return ELF64_R_TYPE_ID (reloc_info);
1112 
1113     default:
1114       return ELF64_R_TYPE (reloc_info);
1115     }
1116 }
1117 
1118 /* Return the symbol index extracted from the reloc info field.  */
1119 
1120 static bfd_vma
get_reloc_symindex(bfd_vma reloc_info)1121 get_reloc_symindex (bfd_vma reloc_info)
1122 {
1123   return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1124 }
1125 
1126 static inline bfd_boolean
uses_msp430x_relocs(void)1127 uses_msp430x_relocs (void)
1128 {
1129   return
1130     elf_header.e_machine == EM_MSP430 /* Paranoia.  */
1131     /* GCC uses osabi == ELFOSBI_STANDALONE.  */
1132     && (((elf_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1133 	/* TI compiler uses ELFOSABI_NONE.  */
1134 	|| (elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1135 }
1136 
1137 /* Display the contents of the relocation data found at the specified
1138    offset.  */
1139 
1140 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,int is_relr,int is_dynsym)1141 dump_relocations (FILE * file,
1142 		  unsigned long rel_offset,
1143 		  unsigned long rel_size,
1144 		  Elf_Internal_Sym * symtab,
1145 		  unsigned long nsyms,
1146 		  char * strtab,
1147 		  unsigned long strtablen,
1148 		  int is_rela,
1149 		  int is_relr,
1150 		  int is_dynsym)
1151 {
1152   unsigned int i;
1153   Elf_Internal_Rela * rels;
1154 
1155   if (is_rela == UNKNOWN)
1156     is_rela = guess_is_rela (elf_header.e_machine);
1157 
1158   if (is_rela)
1159     {
1160       if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
1161 	return;
1162     }
1163   else if (is_relr)
1164     {
1165       if (!slurp_relr_relocs (file, rel_offset, rel_size, &rels, &rel_size))
1166 	return;
1167     }
1168   else
1169     {
1170       if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
1171 	return;
1172     }
1173 
1174   if (is_32bit_elf)
1175     {
1176       if (is_rela)
1177 	{
1178 	  if (do_wide)
1179 	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name + Addend\n"));
1180 	  else
1181 	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name + Addend\n"));
1182 	}
1183       else if (is_relr)
1184 	{
1185 	  if (do_wide)
1186 	    printf (_(" Data       Info    Type\n"));
1187 	  else
1188 	    printf (_(" Data       Info    Type\n"));
1189 	}
1190       else
1191 	{
1192 	  if (do_wide)
1193 	    printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name\n"));
1194 	  else
1195 	    printf (_(" Offset     Info    Type            Sym.Value  Sym. Name\n"));
1196 	}
1197     }
1198   else
1199     {
1200       if (is_rela)
1201 	{
1202 	  if (do_wide)
1203 	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend\n"));
1204 	  else
1205 	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name + Addend\n"));
1206 	}
1207       else if (is_relr)
1208 	{
1209 	  if (do_wide)
1210 	    printf (_("    Data               Info             Type\n"));
1211 	  else
1212 	    printf (_("  Data            Info           Type\n"));
1213 	}
1214       else
1215 	{
1216 	  if (do_wide)
1217 	    printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name\n"));
1218 	  else
1219 	    printf (_("  Offset          Info           Type           Sym. Value    Sym. Name\n"));
1220 	}
1221     }
1222 
1223   for (i = 0; i < rel_size; i++)
1224     {
1225       const char * rtype;
1226       bfd_vma offset;
1227       bfd_vma inf;
1228       bfd_vma symtab_index;
1229       bfd_vma type;
1230 
1231       offset = rels[i].r_offset;
1232       inf    = rels[i].r_info;
1233 
1234       type = get_reloc_type (inf);
1235       symtab_index = get_reloc_symindex  (inf);
1236 
1237       if (is_32bit_elf)
1238 	{
1239 	  printf ("%8.8lx  %8.8lx ",
1240 		  (unsigned long) offset & 0xffffffff,
1241 		  (unsigned long) inf & 0xffffffff);
1242 	}
1243       else
1244 	{
1245 #if BFD_HOST_64BIT_LONG
1246 	  printf (do_wide
1247 		  ? "%16.16lx  %16.16lx "
1248 		  : "%12.12lx  %12.12lx ",
1249 		  offset, inf);
1250 #elif BFD_HOST_64BIT_LONG_LONG
1251 #ifndef __MSVCRT__
1252 	  printf (do_wide
1253 		  ? "%16.16llx  %16.16llx "
1254 		  : "%12.12llx  %12.12llx ",
1255 		  offset, inf);
1256 #else
1257 	  printf (do_wide
1258 		  ? "%16.16I64x  %16.16I64x "
1259 		  : "%12.12I64x  %12.12I64x ",
1260 		  offset, inf);
1261 #endif
1262 #else
1263 	  printf (do_wide
1264 		  ? "%8.8lx%8.8lx  %8.8lx%8.8lx "
1265 		  : "%4.4lx%8.8lx  %4.4lx%8.8lx ",
1266 		  _bfd_int64_high (offset),
1267 		  _bfd_int64_low (offset),
1268 		  _bfd_int64_high (inf),
1269 		  _bfd_int64_low (inf));
1270 #endif
1271 	}
1272 
1273       switch (elf_header.e_machine)
1274 	{
1275 	default:
1276 	  rtype = NULL;
1277 	  break;
1278 
1279 	case EM_AARCH64:
1280 	  rtype = elf_aarch64_reloc_type (type);
1281 	  break;
1282 
1283 	case EM_M32R:
1284 	case EM_CYGNUS_M32R:
1285 	  rtype = elf_m32r_reloc_type (type);
1286 	  break;
1287 
1288 	case EM_386:
1289 	case EM_IAMCU:
1290 	  rtype = elf_i386_reloc_type (type);
1291 	  break;
1292 
1293 	case EM_68HC11:
1294 	case EM_68HC12:
1295 	  rtype = elf_m68hc11_reloc_type (type);
1296 	  break;
1297 
1298 	case EM_68K:
1299 	  rtype = elf_m68k_reloc_type (type);
1300 	  break;
1301 
1302 	case EM_960:
1303 	  rtype = elf_i960_reloc_type (type);
1304 	  break;
1305 
1306 	case EM_AVR:
1307 	case EM_AVR_OLD:
1308 	  rtype = elf_avr_reloc_type (type);
1309 	  break;
1310 
1311 	case EM_OLD_SPARCV9:
1312 	case EM_SPARC32PLUS:
1313 	case EM_SPARCV9:
1314 	case EM_SPARC:
1315 	  rtype = elf_sparc_reloc_type (type);
1316 	  break;
1317 
1318 	case EM_SPU:
1319 	  rtype = elf_spu_reloc_type (type);
1320 	  break;
1321 
1322 	case EM_V800:
1323 	  rtype = v800_reloc_type (type);
1324 	  break;
1325 	case EM_V850:
1326 	case EM_CYGNUS_V850:
1327 	  rtype = v850_reloc_type (type);
1328 	  break;
1329 
1330 	case EM_D10V:
1331 	case EM_CYGNUS_D10V:
1332 	  rtype = elf_d10v_reloc_type (type);
1333 	  break;
1334 
1335 	case EM_D30V:
1336 	case EM_CYGNUS_D30V:
1337 	  rtype = elf_d30v_reloc_type (type);
1338 	  break;
1339 
1340 	case EM_DLX:
1341 	  rtype = elf_dlx_reloc_type (type);
1342 	  break;
1343 
1344 	case EM_SH:
1345 	  rtype = elf_sh_reloc_type (type);
1346 	  break;
1347 
1348 	case EM_MN10300:
1349 	case EM_CYGNUS_MN10300:
1350 	  rtype = elf_mn10300_reloc_type (type);
1351 	  break;
1352 
1353 	case EM_MN10200:
1354 	case EM_CYGNUS_MN10200:
1355 	  rtype = elf_mn10200_reloc_type (type);
1356 	  break;
1357 
1358 	case EM_FR30:
1359 	case EM_CYGNUS_FR30:
1360 	  rtype = elf_fr30_reloc_type (type);
1361 	  break;
1362 
1363 	case EM_CYGNUS_FRV:
1364 	  rtype = elf_frv_reloc_type (type);
1365 	  break;
1366 
1367 	case EM_FT32:
1368 	  rtype = elf_ft32_reloc_type (type);
1369 	  break;
1370 
1371 	case EM_MCORE:
1372 	  rtype = elf_mcore_reloc_type (type);
1373 	  break;
1374 
1375 	case EM_MMIX:
1376 	  rtype = elf_mmix_reloc_type (type);
1377 	  break;
1378 
1379 	case EM_MOXIE:
1380 	  rtype = elf_moxie_reloc_type (type);
1381 	  break;
1382 
1383 	case EM_MSP430:
1384 	  if (uses_msp430x_relocs ())
1385 	    {
1386 	      rtype = elf_msp430x_reloc_type (type);
1387 	      break;
1388 	    }
1389 	case EM_MSP430_OLD:
1390 	  rtype = elf_msp430_reloc_type (type);
1391 	  break;
1392 
1393 	case EM_NDS32:
1394 	  rtype = elf_nds32_reloc_type (type);
1395 	  break;
1396 
1397 	case EM_PPC:
1398 	  rtype = elf_ppc_reloc_type (type);
1399 	  break;
1400 
1401 	case EM_PPC64:
1402 	  rtype = elf_ppc64_reloc_type (type);
1403 	  break;
1404 
1405 	case EM_MIPS:
1406 	case EM_MIPS_RS3_LE:
1407 	  rtype = elf_mips_reloc_type (type);
1408 	  break;
1409 
1410 	case EM_ALPHA:
1411 	  rtype = elf_alpha_reloc_type (type);
1412 	  break;
1413 
1414 	case EM_ARM:
1415 	  rtype = elf_arm_reloc_type (type);
1416 	  break;
1417 
1418 	case EM_ARC:
1419 	case EM_ARC_COMPACT:
1420 	case EM_ARC_COMPACT2:
1421 	  rtype = elf_arc_reloc_type (type);
1422 	  break;
1423 
1424 	case EM_PARISC:
1425 	  rtype = elf_hppa_reloc_type (type);
1426 	  break;
1427 
1428 	case EM_H8_300:
1429 	case EM_H8_300H:
1430 	case EM_H8S:
1431 	  rtype = elf_h8_reloc_type (type);
1432 	  break;
1433 
1434 	case EM_OR1K:
1435 	  rtype = elf_or1k_reloc_type (type);
1436 	  break;
1437 
1438 	case EM_PJ:
1439 	case EM_PJ_OLD:
1440 	  rtype = elf_pj_reloc_type (type);
1441 	  break;
1442 	case EM_IA_64:
1443 	  rtype = elf_ia64_reloc_type (type);
1444 	  break;
1445 
1446 	case EM_CRIS:
1447 	  rtype = elf_cris_reloc_type (type);
1448 	  break;
1449 
1450 	case EM_860:
1451 	  rtype = elf_i860_reloc_type (type);
1452 	  break;
1453 
1454 	case EM_X86_64:
1455 	case EM_L1OM:
1456 	case EM_K1OM:
1457 	  rtype = elf_x86_64_reloc_type (type);
1458 	  break;
1459 
1460 	case EM_S370:
1461 	  rtype = i370_reloc_type (type);
1462 	  break;
1463 
1464 	case EM_S390_OLD:
1465 	case EM_S390:
1466 	  rtype = elf_s390_reloc_type (type);
1467 	  break;
1468 
1469 	case EM_SCORE:
1470 	  rtype = elf_score_reloc_type (type);
1471 	  break;
1472 
1473 	case EM_XSTORMY16:
1474 	  rtype = elf_xstormy16_reloc_type (type);
1475 	  break;
1476 
1477 	case EM_CRX:
1478 	  rtype = elf_crx_reloc_type (type);
1479 	  break;
1480 
1481 	case EM_VAX:
1482 	  rtype = elf_vax_reloc_type (type);
1483 	  break;
1484 
1485 	case EM_VISIUM:
1486 	  rtype = elf_visium_reloc_type (type);
1487 	  break;
1488 
1489 	case EM_ADAPTEVA_EPIPHANY:
1490 	  rtype = elf_epiphany_reloc_type (type);
1491 	  break;
1492 
1493 	case EM_IP2K:
1494 	case EM_IP2K_OLD:
1495 	  rtype = elf_ip2k_reloc_type (type);
1496 	  break;
1497 
1498 	case EM_IQ2000:
1499 	  rtype = elf_iq2000_reloc_type (type);
1500 	  break;
1501 
1502 	case EM_XTENSA_OLD:
1503 	case EM_XTENSA:
1504 	  rtype = elf_xtensa_reloc_type (type);
1505 	  break;
1506 
1507 	case EM_LATTICEMICO32:
1508 	  rtype = elf_lm32_reloc_type (type);
1509 	  break;
1510 
1511 	case EM_M32C_OLD:
1512 	case EM_M32C:
1513 	  rtype = elf_m32c_reloc_type (type);
1514 	  break;
1515 
1516 	case EM_MT:
1517 	  rtype = elf_mt_reloc_type (type);
1518 	  break;
1519 
1520 	case EM_BLACKFIN:
1521 	  rtype = elf_bfin_reloc_type (type);
1522 	  break;
1523 
1524 	case EM_CYGNUS_MEP:
1525 	  rtype = elf_mep_reloc_type (type);
1526 	  break;
1527 
1528 	case EM_CR16:
1529 	  rtype = elf_cr16_reloc_type (type);
1530 	  break;
1531 
1532 	case EM_MICROBLAZE:
1533 	case EM_MICROBLAZE_OLD:
1534 	  rtype = elf_microblaze_reloc_type (type);
1535 	  break;
1536 
1537 	case EM_RL78:
1538 	  rtype = elf_rl78_reloc_type (type);
1539 	  break;
1540 
1541 	case EM_RX:
1542 	  rtype = elf_rx_reloc_type (type);
1543 	  break;
1544 
1545 	case EM_METAG:
1546 	  rtype = elf_metag_reloc_type (type);
1547 	  break;
1548 
1549 	case EM_XC16X:
1550 	case EM_C166:
1551 	  rtype = elf_xc16x_reloc_type (type);
1552 	  break;
1553 
1554 	case EM_TI_C6000:
1555 	  rtype = elf_tic6x_reloc_type (type);
1556 	  break;
1557 
1558 	case EM_TILEGX:
1559 	  rtype = elf_tilegx_reloc_type (type);
1560 	  break;
1561 
1562 	case EM_TILEPRO:
1563 	  rtype = elf_tilepro_reloc_type (type);
1564 	  break;
1565 
1566 	case EM_XGATE:
1567 	  rtype = elf_xgate_reloc_type (type);
1568 	  break;
1569 
1570 	case EM_ALTERA_NIOS2:
1571 	  rtype = elf_nios2_reloc_type (type);
1572 	  break;
1573 	}
1574 
1575       if (is_relr)
1576         printf (do_wide ? "RELATIVE COMPRESSED" : "RELR");
1577       else if (rtype == NULL)
1578 	printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1579       else
1580 	printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1581 
1582       if (elf_header.e_machine == EM_ALPHA
1583 	  && rtype != NULL
1584 	  && streq (rtype, "R_ALPHA_LITUSE")
1585 	  && is_rela)
1586 	{
1587 	  switch (rels[i].r_addend)
1588 	    {
1589 	    case LITUSE_ALPHA_ADDR:   rtype = "ADDR";   break;
1590 	    case LITUSE_ALPHA_BASE:   rtype = "BASE";   break;
1591 	    case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1592 	    case LITUSE_ALPHA_JSR:    rtype = "JSR";    break;
1593 	    case LITUSE_ALPHA_TLSGD:  rtype = "TLSGD";  break;
1594 	    case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1595 	    case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1596 	    default: rtype = NULL;
1597 	    }
1598 	  if (rtype)
1599 	    printf (" (%s)", rtype);
1600 	  else
1601 	    {
1602 	      putchar (' ');
1603 	      printf (_("<unknown addend: %lx>"),
1604 		      (unsigned long) rels[i].r_addend);
1605 	    }
1606 	}
1607       else if (symtab_index)
1608 	{
1609 	  if (symtab == NULL || symtab_index >= nsyms)
1610 	    printf (_(" bad symbol index: %08lx"), (unsigned long) symtab_index);
1611 	  else
1612 	    {
1613 	      Elf_Internal_Sym * psym;
1614 	      const char * version_string;
1615 	      enum versioned_symbol_info sym_info;
1616 	      unsigned short vna_other;
1617 
1618 	      psym = symtab + symtab_index;
1619 
1620 	      version_string
1621 		= get_symbol_version_string (file, is_dynsym,
1622 					     strtab, strtablen,
1623 					     symtab_index,
1624 					     psym,
1625 					     &sym_info,
1626 					     &vna_other);
1627 
1628 	      printf (" ");
1629 
1630 	      if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1631 		{
1632 		  const char * name;
1633 		  unsigned int len;
1634 		  unsigned int width = is_32bit_elf ? 8 : 14;
1635 
1636 		  /* Relocations against GNU_IFUNC symbols do not use the value
1637 		     of the symbol as the address to relocate against.  Instead
1638 		     they invoke the function named by the symbol and use its
1639 		     result as the address for relocation.
1640 
1641 		     To indicate this to the user, do not display the value of
1642 		     the symbol in the "Symbols's Value" field.  Instead show
1643 		     its name followed by () as a hint that the symbol is
1644 		     invoked.  */
1645 
1646 		  if (strtab == NULL
1647 		      || psym->st_name == 0
1648 		      || psym->st_name >= strtablen)
1649 		    name = "??";
1650 		  else
1651 		    name = strtab + psym->st_name;
1652 
1653 		  len = print_symbol (width, name);
1654 		  if (version_string)
1655 		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
1656 			    version_string);
1657 		  printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1658 		}
1659 	      else
1660 		{
1661 		  print_vma (psym->st_value, LONG_HEX);
1662 
1663 		  printf (is_32bit_elf ? "   " : " ");
1664 		}
1665 
1666 	      if (psym->st_name == 0)
1667 		{
1668 		  const char * sec_name = "<null>";
1669 		  char name_buf[40];
1670 
1671 		  if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1672 		    {
1673 		      if (psym->st_shndx < elf_header.e_shnum)
1674 			sec_name = SECTION_NAME (section_headers + psym->st_shndx);
1675 		      else if (psym->st_shndx == SHN_ABS)
1676 			sec_name = "ABS";
1677 		      else if (psym->st_shndx == SHN_COMMON)
1678 			sec_name = "COMMON";
1679 		      else if ((elf_header.e_machine == EM_MIPS
1680 				&& psym->st_shndx == SHN_MIPS_SCOMMON)
1681 			       || (elf_header.e_machine == EM_TI_C6000
1682 				   && psym->st_shndx == SHN_TIC6X_SCOMMON))
1683 			sec_name = "SCOMMON";
1684 		      else if (elf_header.e_machine == EM_MIPS
1685 			       && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1686 			sec_name = "SUNDEF";
1687 		      else if ((elf_header.e_machine == EM_X86_64
1688 				|| elf_header.e_machine == EM_L1OM
1689 				|| elf_header.e_machine == EM_K1OM)
1690 			       && psym->st_shndx == SHN_X86_64_LCOMMON)
1691 			sec_name = "LARGE_COMMON";
1692 		      else if (elf_header.e_machine == EM_IA_64
1693 			       && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1694 			       && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1695 			sec_name = "ANSI_COM";
1696 		      else if (is_ia64_vms ()
1697 			       && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1698 			sec_name = "VMS_SYMVEC";
1699 		      else
1700 			{
1701 			  sprintf (name_buf, "<section 0x%x>",
1702 				   (unsigned int) psym->st_shndx);
1703 			  sec_name = name_buf;
1704 			}
1705 		    }
1706 		  print_symbol (22, sec_name);
1707 		}
1708 	      else if (strtab == NULL)
1709 		printf (_("<string table index: %3ld>"), psym->st_name);
1710 	      else if (psym->st_name >= strtablen)
1711 		printf (_("<corrupt string table index: %3ld>"), psym->st_name);
1712 	      else
1713 		{
1714 		  print_symbol (22, strtab + psym->st_name);
1715 		  if (version_string)
1716 		    printf (sym_info == symbol_public ? "@@%s" : "@%s",
1717 			    version_string);
1718 		}
1719 
1720 	      if (is_rela)
1721 		{
1722 		  bfd_vma off = rels[i].r_addend;
1723 
1724 		  if ((bfd_signed_vma) off < 0)
1725 		    printf (" - %" BFD_VMA_FMT "x", - off);
1726 		  else
1727 		    printf (" + %" BFD_VMA_FMT "x", off);
1728 		}
1729 	    }
1730 	}
1731       else if (is_rela)
1732 	{
1733 	  bfd_vma off = rels[i].r_addend;
1734 
1735 	  printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1736 	  if ((bfd_signed_vma) off < 0)
1737 	    printf ("-%" BFD_VMA_FMT "x", - off);
1738 	  else
1739 	    printf ("%" BFD_VMA_FMT "x", off);
1740 	}
1741 
1742       if (elf_header.e_machine == EM_SPARCV9
1743 	  && rtype != NULL
1744 	  && streq (rtype, "R_SPARC_OLO10"))
1745 	printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1746 
1747       putchar ('\n');
1748 
1749 #ifdef BFD64
1750       if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1751 	{
1752 	  bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1753 	  bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1754 	  const char * rtype2 = elf_mips_reloc_type (type2);
1755 	  const char * rtype3 = elf_mips_reloc_type (type3);
1756 
1757 	  printf ("                    Type2: ");
1758 
1759 	  if (rtype2 == NULL)
1760 	    printf (_("unrecognized: %-7lx"),
1761 		    (unsigned long) type2 & 0xffffffff);
1762 	  else
1763 	    printf ("%-17.17s", rtype2);
1764 
1765 	  printf ("\n                    Type3: ");
1766 
1767 	  if (rtype3 == NULL)
1768 	    printf (_("unrecognized: %-7lx"),
1769 		    (unsigned long) type3 & 0xffffffff);
1770 	  else
1771 	    printf ("%-17.17s", rtype3);
1772 
1773 	  putchar ('\n');
1774 	}
1775 #endif /* BFD64 */
1776     }
1777 
1778   free (rels);
1779 }
1780 
1781 static const char *
get_mips_dynamic_type(unsigned long type)1782 get_mips_dynamic_type (unsigned long type)
1783 {
1784   switch (type)
1785     {
1786     case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1787     case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1788     case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1789     case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1790     case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1791     case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1792     case DT_MIPS_MSYM: return "MIPS_MSYM";
1793     case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1794     case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1795     case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1796     case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1797     case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1798     case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1799     case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1800     case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1801     case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1802     case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1803     case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1804     case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1805     case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1806     case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1807     case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1808     case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1809     case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1810     case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1811     case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1812     case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1813     case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1814     case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1815     case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1816     case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1817     case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1818     case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1819     case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1820     case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1821     case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1822     case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1823     case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1824     case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1825     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1826     case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1827     case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1828     case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1829     case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1830     case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1831     case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1832     default:
1833       return NULL;
1834     }
1835 }
1836 
1837 static const char *
get_sparc64_dynamic_type(unsigned long type)1838 get_sparc64_dynamic_type (unsigned long type)
1839 {
1840   switch (type)
1841     {
1842     case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1843     default:
1844       return NULL;
1845     }
1846 }
1847 
1848 static const char *
get_ppc_dynamic_type(unsigned long type)1849 get_ppc_dynamic_type (unsigned long type)
1850 {
1851   switch (type)
1852     {
1853     case DT_PPC_GOT:    return "PPC_GOT";
1854     case DT_PPC_OPT:    return "PPC_OPT";
1855     default:
1856       return NULL;
1857     }
1858 }
1859 
1860 static const char *
get_ppc64_dynamic_type(unsigned long type)1861 get_ppc64_dynamic_type (unsigned long type)
1862 {
1863   switch (type)
1864     {
1865     case DT_PPC64_GLINK:  return "PPC64_GLINK";
1866     case DT_PPC64_OPD:    return "PPC64_OPD";
1867     case DT_PPC64_OPDSZ:  return "PPC64_OPDSZ";
1868     case DT_PPC64_OPT:    return "PPC64_OPT";
1869     default:
1870       return NULL;
1871     }
1872 }
1873 
1874 static const char *
get_parisc_dynamic_type(unsigned long type)1875 get_parisc_dynamic_type (unsigned long type)
1876 {
1877   switch (type)
1878     {
1879     case DT_HP_LOAD_MAP:	return "HP_LOAD_MAP";
1880     case DT_HP_DLD_FLAGS:	return "HP_DLD_FLAGS";
1881     case DT_HP_DLD_HOOK:	return "HP_DLD_HOOK";
1882     case DT_HP_UX10_INIT:	return "HP_UX10_INIT";
1883     case DT_HP_UX10_INITSZ:	return "HP_UX10_INITSZ";
1884     case DT_HP_PREINIT:		return "HP_PREINIT";
1885     case DT_HP_PREINITSZ:	return "HP_PREINITSZ";
1886     case DT_HP_NEEDED:		return "HP_NEEDED";
1887     case DT_HP_TIME_STAMP:	return "HP_TIME_STAMP";
1888     case DT_HP_CHECKSUM:	return "HP_CHECKSUM";
1889     case DT_HP_GST_SIZE:	return "HP_GST_SIZE";
1890     case DT_HP_GST_VERSION:	return "HP_GST_VERSION";
1891     case DT_HP_GST_HASHVAL:	return "HP_GST_HASHVAL";
1892     case DT_HP_EPLTREL:		return "HP_GST_EPLTREL";
1893     case DT_HP_EPLTRELSZ:	return "HP_GST_EPLTRELSZ";
1894     case DT_HP_FILTERED:	return "HP_FILTERED";
1895     case DT_HP_FILTER_TLS:	return "HP_FILTER_TLS";
1896     case DT_HP_COMPAT_FILTERED:	return "HP_COMPAT_FILTERED";
1897     case DT_HP_LAZYLOAD:	return "HP_LAZYLOAD";
1898     case DT_HP_BIND_NOW_COUNT:	return "HP_BIND_NOW_COUNT";
1899     case DT_PLT:		return "PLT";
1900     case DT_PLT_SIZE:		return "PLT_SIZE";
1901     case DT_DLT:		return "DLT";
1902     case DT_DLT_SIZE:		return "DLT_SIZE";
1903     default:
1904       return NULL;
1905     }
1906 }
1907 
1908 static const char *
get_ia64_dynamic_type(unsigned long type)1909 get_ia64_dynamic_type (unsigned long type)
1910 {
1911   switch (type)
1912     {
1913     case DT_IA_64_PLT_RESERVE:         return "IA_64_PLT_RESERVE";
1914     case DT_IA_64_VMS_SUBTYPE:         return "VMS_SUBTYPE";
1915     case DT_IA_64_VMS_IMGIOCNT:        return "VMS_IMGIOCNT";
1916     case DT_IA_64_VMS_LNKFLAGS:        return "VMS_LNKFLAGS";
1917     case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1918     case DT_IA_64_VMS_IDENT:           return "VMS_IDENT";
1919     case DT_IA_64_VMS_NEEDED_IDENT:    return "VMS_NEEDED_IDENT";
1920     case DT_IA_64_VMS_IMG_RELA_CNT:    return "VMS_IMG_RELA_CNT";
1921     case DT_IA_64_VMS_SEG_RELA_CNT:    return "VMS_SEG_RELA_CNT";
1922     case DT_IA_64_VMS_FIXUP_RELA_CNT:  return "VMS_FIXUP_RELA_CNT";
1923     case DT_IA_64_VMS_FIXUP_NEEDED:    return "VMS_FIXUP_NEEDED";
1924     case DT_IA_64_VMS_SYMVEC_CNT:      return "VMS_SYMVEC_CNT";
1925     case DT_IA_64_VMS_XLATED:          return "VMS_XLATED";
1926     case DT_IA_64_VMS_STACKSIZE:       return "VMS_STACKSIZE";
1927     case DT_IA_64_VMS_UNWINDSZ:        return "VMS_UNWINDSZ";
1928     case DT_IA_64_VMS_UNWIND_CODSEG:   return "VMS_UNWIND_CODSEG";
1929     case DT_IA_64_VMS_UNWIND_INFOSEG:  return "VMS_UNWIND_INFOSEG";
1930     case DT_IA_64_VMS_LINKTIME:        return "VMS_LINKTIME";
1931     case DT_IA_64_VMS_SEG_NO:          return "VMS_SEG_NO";
1932     case DT_IA_64_VMS_SYMVEC_OFFSET:   return "VMS_SYMVEC_OFFSET";
1933     case DT_IA_64_VMS_SYMVEC_SEG:      return "VMS_SYMVEC_SEG";
1934     case DT_IA_64_VMS_UNWIND_OFFSET:   return "VMS_UNWIND_OFFSET";
1935     case DT_IA_64_VMS_UNWIND_SEG:      return "VMS_UNWIND_SEG";
1936     case DT_IA_64_VMS_STRTAB_OFFSET:   return "VMS_STRTAB_OFFSET";
1937     case DT_IA_64_VMS_SYSVER_OFFSET:   return "VMS_SYSVER_OFFSET";
1938     case DT_IA_64_VMS_IMG_RELA_OFF:    return "VMS_IMG_RELA_OFF";
1939     case DT_IA_64_VMS_SEG_RELA_OFF:    return "VMS_SEG_RELA_OFF";
1940     case DT_IA_64_VMS_FIXUP_RELA_OFF:  return "VMS_FIXUP_RELA_OFF";
1941     case DT_IA_64_VMS_PLTGOT_OFFSET:   return "VMS_PLTGOT_OFFSET";
1942     case DT_IA_64_VMS_PLTGOT_SEG:      return "VMS_PLTGOT_SEG";
1943     case DT_IA_64_VMS_FPMODE:          return "VMS_FPMODE";
1944     default:
1945       return NULL;
1946     }
1947 }
1948 
1949 static const char *
get_solaris_section_type(unsigned long type)1950 get_solaris_section_type (unsigned long type)
1951 {
1952   switch (type)
1953     {
1954     case 0x6fffffee: return "SUNW_ancillary";
1955     case 0x6fffffef: return "SUNW_capchain";
1956     case 0x6ffffff0: return "SUNW_capinfo";
1957     case 0x6ffffff1: return "SUNW_symsort";
1958     case 0x6ffffff2: return "SUNW_tlssort";
1959     case 0x6ffffff3: return "SUNW_LDYNSYM";
1960     case 0x6ffffff4: return "SUNW_dof";
1961     case 0x6ffffff5: return "SUNW_cap";
1962     case 0x6ffffff6: return "SUNW_SIGNATURE";
1963     case 0x6ffffff7: return "SUNW_ANNOTATE";
1964     case 0x6ffffff8: return "SUNW_DEBUGSTR";
1965     case 0x6ffffff9: return "SUNW_DEBUG";
1966     case 0x6ffffffa: return "SUNW_move";
1967     case 0x6ffffffb: return "SUNW_COMDAT";
1968     case 0x6ffffffc: return "SUNW_syminfo";
1969     case 0x6ffffffd: return "SUNW_verdef";
1970     case 0x6ffffffe: return "SUNW_verneed";
1971     case 0x6fffffff: return "SUNW_versym";
1972     case 0x70000000: return "SPARC_GOTDATA";
1973     default: return NULL;
1974     }
1975 }
1976 
1977 static const char *
get_alpha_dynamic_type(unsigned long type)1978 get_alpha_dynamic_type (unsigned long type)
1979 {
1980   switch (type)
1981     {
1982     case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1983     default:
1984       return NULL;
1985     }
1986 }
1987 
1988 static const char *
get_score_dynamic_type(unsigned long type)1989 get_score_dynamic_type (unsigned long type)
1990 {
1991   switch (type)
1992     {
1993     case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
1994     case DT_SCORE_LOCAL_GOTNO:  return "SCORE_LOCAL_GOTNO";
1995     case DT_SCORE_SYMTABNO:     return "SCORE_SYMTABNO";
1996     case DT_SCORE_GOTSYM:       return "SCORE_GOTSYM";
1997     case DT_SCORE_UNREFEXTNO:   return "SCORE_UNREFEXTNO";
1998     case DT_SCORE_HIPAGENO:     return "SCORE_HIPAGENO";
1999     default:
2000       return NULL;
2001     }
2002 }
2003 
2004 static const char *
get_tic6x_dynamic_type(unsigned long type)2005 get_tic6x_dynamic_type (unsigned long type)
2006 {
2007   switch (type)
2008     {
2009     case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2010     case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2011     case DT_C6000_DSBT_BASE:   return "C6000_DSBT_BASE";
2012     case DT_C6000_DSBT_SIZE:   return "C6000_DSBT_SIZE";
2013     case DT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
2014     case DT_C6000_DSBT_INDEX:  return "C6000_DSBT_INDEX";
2015     default:
2016       return NULL;
2017     }
2018 }
2019 
2020 static const char *
get_nios2_dynamic_type(unsigned long type)2021 get_nios2_dynamic_type (unsigned long type)
2022 {
2023   switch (type)
2024     {
2025     case DT_NIOS2_GP: return "NIOS2_GP";
2026     default:
2027       return NULL;
2028     }
2029 }
2030 
2031 static const char *
get_solaris_dynamic_type(unsigned long type)2032 get_solaris_dynamic_type (unsigned long type)
2033 {
2034   switch (type)
2035     {
2036     case 0x6000000d: return "SUNW_AUXILIARY";
2037     case 0x6000000e: return "SUNW_RTLDINF";
2038     case 0x6000000f: return "SUNW_FILTER";
2039     case 0x60000010: return "SUNW_CAP";
2040     case 0x60000011: return "SUNW_SYMTAB";
2041     case 0x60000012: return "SUNW_SYMSZ";
2042     case 0x60000013: return "SUNW_SORTENT";
2043     case 0x60000014: return "SUNW_SYMSORT";
2044     case 0x60000015: return "SUNW_SYMSORTSZ";
2045     case 0x60000016: return "SUNW_TLSSORT";
2046     case 0x60000017: return "SUNW_TLSSORTSZ";
2047     case 0x60000018: return "SUNW_CAPINFO";
2048     case 0x60000019: return "SUNW_STRPAD";
2049     case 0x6000001a: return "SUNW_CAPCHAIN";
2050     case 0x6000001b: return "SUNW_LDMACH";
2051     case 0x6000001d: return "SUNW_CAPCHAINENT";
2052     case 0x6000001f: return "SUNW_CAPCHAINSZ";
2053     case 0x60000021: return "SUNW_PARENT";
2054     case 0x60000023: return "SUNW_ASLR";
2055     case 0x60000025: return "SUNW_RELAX";
2056     case 0x60000029: return "SUNW_NXHEAP";
2057     case 0x6000002b: return "SUNW_NXSTACK";
2058 
2059     case 0x70000001: return "SPARC_REGISTER";
2060     case 0x7ffffffd: return "AUXILIARY";
2061     case 0x7ffffffe: return "USED";
2062     case 0x7fffffff: return "FILTER";
2063 
2064     default: return NULL;
2065     }
2066 }
2067 
2068 static const char *
get_dynamic_type(unsigned long type)2069 get_dynamic_type (unsigned long type)
2070 {
2071   static char buff[64];
2072 
2073   switch (type)
2074     {
2075     case DT_NULL:	return "NULL";
2076     case DT_NEEDED:	return "NEEDED";
2077     case DT_PLTRELSZ:	return "PLTRELSZ";
2078     case DT_PLTGOT:	return "PLTGOT";
2079     case DT_HASH:	return "HASH";
2080     case DT_STRTAB:	return "STRTAB";
2081     case DT_SYMTAB:	return "SYMTAB";
2082     case DT_RELA:	return "RELA";
2083     case DT_RELASZ:	return "RELASZ";
2084     case DT_RELAENT:	return "RELAENT";
2085     case DT_STRSZ:	return "STRSZ";
2086     case DT_SYMENT:	return "SYMENT";
2087     case DT_INIT:	return "INIT";
2088     case DT_FINI:	return "FINI";
2089     case DT_SONAME:	return "SONAME";
2090     case DT_RPATH:	return "RPATH";
2091     case DT_SYMBOLIC:	return "SYMBOLIC";
2092     case DT_REL:	return "REL";
2093     case DT_RELSZ:	return "RELSZ";
2094     case DT_RELENT:	return "RELENT";
2095     case DT_RELR:	return "RELR";
2096     case DT_RELRSZ:	return "RELRSZ";
2097     case DT_RELRENT:	return "RELRENT";
2098     case DT_PLTREL:	return "PLTREL";
2099     case DT_DEBUG:	return "DEBUG";
2100     case DT_TEXTREL:	return "TEXTREL";
2101     case DT_JMPREL:	return "JMPREL";
2102     case DT_BIND_NOW:   return "BIND_NOW";
2103     case DT_INIT_ARRAY: return "INIT_ARRAY";
2104     case DT_FINI_ARRAY: return "FINI_ARRAY";
2105     case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2106     case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2107     case DT_RUNPATH:    return "RUNPATH";
2108     case DT_FLAGS:      return "FLAGS";
2109 
2110     case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2111     case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2112 
2113     case DT_CHECKSUM:	return "CHECKSUM";
2114     case DT_PLTPADSZ:	return "PLTPADSZ";
2115     case DT_MOVEENT:	return "MOVEENT";
2116     case DT_MOVESZ:	return "MOVESZ";
2117     case DT_FEATURE:	return "FEATURE";
2118     case DT_POSFLAG_1:	return "POSFLAG_1";
2119     case DT_SYMINSZ:	return "SYMINSZ";
2120     case DT_SYMINENT:	return "SYMINENT"; /* aka VALRNGHI */
2121 
2122     case DT_ADDRRNGLO:  return "ADDRRNGLO";
2123     case DT_CONFIG:	return "CONFIG";
2124     case DT_DEPAUDIT:	return "DEPAUDIT";
2125     case DT_AUDIT:	return "AUDIT";
2126     case DT_PLTPAD:	return "PLTPAD";
2127     case DT_MOVETAB:	return "MOVETAB";
2128     case DT_SYMINFO:	return "SYMINFO"; /* aka ADDRRNGHI */
2129 
2130     case DT_VERSYM:	return "VERSYM";
2131 
2132     case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2133     case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2134     case DT_RELRCOUNT:	return "RELRCOUNT";
2135     case DT_RELACOUNT:	return "RELACOUNT";
2136     case DT_RELCOUNT:	return "RELCOUNT";
2137     case DT_FLAGS_1:	return "FLAGS_1";
2138     case DT_VERDEF:	return "VERDEF";
2139     case DT_VERDEFNUM:	return "VERDEFNUM";
2140     case DT_VERNEED:	return "VERNEED";
2141     case DT_VERNEEDNUM:	return "VERNEEDNUM";
2142 
2143     case DT_AUXILIARY:	return "AUXILIARY";
2144     case DT_USED:	return "USED";
2145     case DT_FILTER:	return "FILTER";
2146 
2147     case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2148     case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2149     case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2150     case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2151     case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2152     case DT_GNU_HASH:	return "GNU_HASH";
2153 
2154     default:
2155       if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2156 	{
2157 	  const char * result;
2158 
2159 	  switch (elf_header.e_machine)
2160 	    {
2161 	    case EM_MIPS:
2162 	    case EM_MIPS_RS3_LE:
2163 	      result = get_mips_dynamic_type (type);
2164 	      break;
2165 	    case EM_SPARCV9:
2166 	      result = get_sparc64_dynamic_type (type);
2167 	      break;
2168 	    case EM_PPC:
2169 	      result = get_ppc_dynamic_type (type);
2170 	      break;
2171 	    case EM_PPC64:
2172 	      result = get_ppc64_dynamic_type (type);
2173 	      break;
2174 	    case EM_IA_64:
2175 	      result = get_ia64_dynamic_type (type);
2176 	      break;
2177 	    case EM_ALPHA:
2178 	      result = get_alpha_dynamic_type (type);
2179 	      break;
2180 	    case EM_SCORE:
2181 	      result = get_score_dynamic_type (type);
2182 	      break;
2183 	    case EM_TI_C6000:
2184 	      result = get_tic6x_dynamic_type (type);
2185 	      break;
2186 	    case EM_ALTERA_NIOS2:
2187 	      result = get_nios2_dynamic_type (type);
2188 	      break;
2189 	    default:
2190 	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2191 		result = get_solaris_dynamic_type (type);
2192 	      else
2193 		result = NULL;
2194 	      break;
2195 	    }
2196 
2197 	  if (result != NULL)
2198 	    return result;
2199 
2200 	  snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2201 	}
2202       else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2203 	       || (elf_header.e_machine == EM_PARISC
2204 		   && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2205 	{
2206 	  const char * result;
2207 
2208 	  switch (elf_header.e_machine)
2209 	    {
2210 	    case EM_PARISC:
2211 	      result = get_parisc_dynamic_type (type);
2212 	      break;
2213 	    case EM_IA_64:
2214 	      result = get_ia64_dynamic_type (type);
2215 	      break;
2216 	    default:
2217 	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2218 		result = get_solaris_dynamic_type (type);
2219 	      else
2220 		result = NULL;
2221 	      break;
2222 	    }
2223 
2224 	  if (result != NULL)
2225 	    return result;
2226 
2227 	  snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2228 		    type);
2229 	}
2230       else
2231 	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2232 
2233       return buff;
2234     }
2235 }
2236 
2237 static char *
get_file_type(unsigned e_type)2238 get_file_type (unsigned e_type)
2239 {
2240   static char buff[32];
2241 
2242   switch (e_type)
2243     {
2244     case ET_NONE:	return _("NONE (None)");
2245     case ET_REL:	return _("REL (Relocatable file)");
2246     case ET_EXEC:	return _("EXEC (Executable file)");
2247     case ET_DYN:	return _("DYN (Shared object file)");
2248     case ET_CORE:	return _("CORE (Core file)");
2249 
2250     default:
2251       if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2252 	snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2253       else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2254 	snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2255       else
2256 	snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2257       return buff;
2258     }
2259 }
2260 
2261 static char *
get_machine_name(unsigned e_machine)2262 get_machine_name (unsigned e_machine)
2263 {
2264   static char buff[64]; /* XXX */
2265 
2266   switch (e_machine)
2267     {
2268     case EM_NONE:		return _("None");
2269     case EM_AARCH64:		return "AArch64";
2270     case EM_M32:		return "WE32100";
2271     case EM_SPARC:		return "Sparc";
2272     case EM_SPU:		return "SPU";
2273     case EM_386:		return "Intel 80386";
2274     case EM_68K:		return "MC68000";
2275     case EM_88K:		return "MC88000";
2276     case EM_IAMCU:		return "Intel MCU";
2277     case EM_860:		return "Intel 80860";
2278     case EM_MIPS:		return "MIPS R3000";
2279     case EM_S370:		return "IBM System/370";
2280     case EM_MIPS_RS3_LE:	return "MIPS R4000 big-endian";
2281     case EM_OLD_SPARCV9:	return "Sparc v9 (old)";
2282     case EM_PARISC:		return "HPPA";
2283     case EM_PPC_OLD:		return "Power PC (old)";
2284     case EM_SPARC32PLUS:	return "Sparc v8+" ;
2285     case EM_960:		return "Intel 90860";
2286     case EM_PPC:		return "PowerPC";
2287     case EM_PPC64:		return "PowerPC64";
2288     case EM_FR20:		return "Fujitsu FR20";
2289     case EM_FT32:		return "FTDI FT32";
2290     case EM_RH32:		return "TRW RH32";
2291     case EM_MCORE:		return "MCORE";
2292     case EM_ARM:		return "ARM";
2293     case EM_OLD_ALPHA:		return "Digital Alpha (old)";
2294     case EM_SH:			return "Renesas / SuperH SH";
2295     case EM_SPARCV9:		return "Sparc v9";
2296     case EM_TRICORE:		return "Siemens Tricore";
2297     case EM_ARC:		return "ARC";
2298     case EM_ARC_COMPACT:	return "ARCompact";
2299     case EM_ARC_COMPACT2:	return "ARCv2";
2300     case EM_H8_300:		return "Renesas H8/300";
2301     case EM_H8_300H:		return "Renesas H8/300H";
2302     case EM_H8S:		return "Renesas H8S";
2303     case EM_H8_500:		return "Renesas H8/500";
2304     case EM_IA_64:		return "Intel IA-64";
2305     case EM_MIPS_X:		return "Stanford MIPS-X";
2306     case EM_COLDFIRE:		return "Motorola Coldfire";
2307     case EM_ALPHA:		return "Alpha";
2308     case EM_CYGNUS_D10V:
2309     case EM_D10V:		return "d10v";
2310     case EM_CYGNUS_D30V:
2311     case EM_D30V:		return "d30v";
2312     case EM_CYGNUS_M32R:
2313     case EM_M32R:		return "Renesas M32R (formerly Mitsubishi M32r)";
2314     case EM_CYGNUS_V850:
2315     case EM_V800:		return "Renesas V850 (using RH850 ABI)";
2316     case EM_V850:		return "Renesas V850";
2317     case EM_CYGNUS_MN10300:
2318     case EM_MN10300:		return "mn10300";
2319     case EM_CYGNUS_MN10200:
2320     case EM_MN10200:		return "mn10200";
2321     case EM_MOXIE:		return "Moxie";
2322     case EM_CYGNUS_FR30:
2323     case EM_FR30:		return "Fujitsu FR30";
2324     case EM_CYGNUS_FRV:		return "Fujitsu FR-V";
2325     case EM_PJ_OLD:
2326     case EM_PJ:			return "picoJava";
2327     case EM_MMA:		return "Fujitsu Multimedia Accelerator";
2328     case EM_PCP:		return "Siemens PCP";
2329     case EM_NCPU:		return "Sony nCPU embedded RISC processor";
2330     case EM_NDR1:		return "Denso NDR1 microprocesspr";
2331     case EM_STARCORE:		return "Motorola Star*Core processor";
2332     case EM_ME16:		return "Toyota ME16 processor";
2333     case EM_ST100:		return "STMicroelectronics ST100 processor";
2334     case EM_TINYJ:		return "Advanced Logic Corp. TinyJ embedded processor";
2335     case EM_PDSP:		return "Sony DSP processor";
2336     case EM_PDP10:		return "Digital Equipment Corp. PDP-10";
2337     case EM_PDP11:		return "Digital Equipment Corp. PDP-11";
2338     case EM_FX66:		return "Siemens FX66 microcontroller";
2339     case EM_ST9PLUS:		return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2340     case EM_ST7:		return "STMicroelectronics ST7 8-bit microcontroller";
2341     case EM_68HC16:		return "Motorola MC68HC16 Microcontroller";
2342     case EM_68HC12:		return "Motorola MC68HC12 Microcontroller";
2343     case EM_68HC11:		return "Motorola MC68HC11 Microcontroller";
2344     case EM_68HC08:		return "Motorola MC68HC08 Microcontroller";
2345     case EM_68HC05:		return "Motorola MC68HC05 Microcontroller";
2346     case EM_SVX:		return "Silicon Graphics SVx";
2347     case EM_ST19:		return "STMicroelectronics ST19 8-bit microcontroller";
2348     case EM_VAX:		return "Digital VAX";
2349     case EM_VISIUM:		return "CDS VISIUMcore processor";
2350     case EM_AVR_OLD:
2351     case EM_AVR:		return "Atmel AVR 8-bit microcontroller";
2352     case EM_CRIS:		return "Axis Communications 32-bit embedded processor";
2353     case EM_JAVELIN:		return "Infineon Technologies 32-bit embedded cpu";
2354     case EM_FIREPATH:		return "Element 14 64-bit DSP processor";
2355     case EM_ZSP:		return "LSI Logic's 16-bit DSP processor";
2356     case EM_MMIX:		return "Donald Knuth's educational 64-bit processor";
2357     case EM_HUANY:		return "Harvard Universitys's machine-independent object format";
2358     case EM_PRISM:		return "Vitesse Prism";
2359     case EM_X86_64:		return "Advanced Micro Devices X86-64";
2360     case EM_L1OM:		return "Intel L1OM";
2361     case EM_K1OM:		return "Intel K1OM";
2362     case EM_S390_OLD:
2363     case EM_S390:		return "IBM S/390";
2364     case EM_SCORE:		return "SUNPLUS S+Core";
2365     case EM_XSTORMY16:		return "Sanyo XStormy16 CPU core";
2366     case EM_OR1K:		return "OpenRISC 1000";
2367     case EM_CRX:		return "National Semiconductor CRX microprocessor";
2368     case EM_ADAPTEVA_EPIPHANY:	return "Adapteva EPIPHANY";
2369     case EM_DLX:		return "OpenDLX";
2370     case EM_IP2K_OLD:
2371     case EM_IP2K:		return "Ubicom IP2xxx 8-bit microcontrollers";
2372     case EM_IQ2000:       	return "Vitesse IQ2000";
2373     case EM_XTENSA_OLD:
2374     case EM_XTENSA:		return "Tensilica Xtensa Processor";
2375     case EM_VIDEOCORE:		return "Alphamosaic VideoCore processor";
2376     case EM_TMM_GPP:		return "Thompson Multimedia General Purpose Processor";
2377     case EM_NS32K:		return "National Semiconductor 32000 series";
2378     case EM_TPC:		return "Tenor Network TPC processor";
2379     case EM_ST200:		return "STMicroelectronics ST200 microcontroller";
2380     case EM_MAX:		return "MAX Processor";
2381     case EM_CR:			return "National Semiconductor CompactRISC";
2382     case EM_F2MC16:		return "Fujitsu F2MC16";
2383     case EM_MSP430:		return "Texas Instruments msp430 microcontroller";
2384     case EM_LATTICEMICO32:	return "Lattice Mico32";
2385     case EM_M32C_OLD:
2386     case EM_M32C:	        return "Renesas M32c";
2387     case EM_MT:                 return "Morpho Techologies MT processor";
2388     case EM_BLACKFIN:		return "Analog Devices Blackfin";
2389     case EM_SE_C33:		return "S1C33 Family of Seiko Epson processors";
2390     case EM_SEP:		return "Sharp embedded microprocessor";
2391     case EM_ARCA:		return "Arca RISC microprocessor";
2392     case EM_UNICORE:		return "Unicore";
2393     case EM_EXCESS:		return "eXcess 16/32/64-bit configurable embedded CPU";
2394     case EM_DXP:		return "Icera Semiconductor Inc. Deep Execution Processor";
2395     case EM_NIOS32:		return "Altera Nios";
2396     case EM_ALTERA_NIOS2:	return "Altera Nios II";
2397     case EM_C166:
2398     case EM_XC16X:		return "Infineon Technologies xc16x";
2399     case EM_M16C:		return "Renesas M16C series microprocessors";
2400     case EM_DSPIC30F:		return "Microchip Technology dsPIC30F Digital Signal Controller";
2401     case EM_CE:			return "Freescale Communication Engine RISC core";
2402     case EM_TSK3000:		return "Altium TSK3000 core";
2403     case EM_RS08:		return "Freescale RS08 embedded processor";
2404     case EM_ECOG2:		return "Cyan Technology eCOG2 microprocessor";
2405     case EM_DSP24:		return "New Japan Radio (NJR) 24-bit DSP Processor";
2406     case EM_VIDEOCORE3:		return "Broadcom VideoCore III processor";
2407     case EM_SE_C17:		return "Seiko Epson C17 family";
2408     case EM_TI_C6000:		return "Texas Instruments TMS320C6000 DSP family";
2409     case EM_TI_C2000:		return "Texas Instruments TMS320C2000 DSP family";
2410     case EM_TI_C5500:		return "Texas Instruments TMS320C55x DSP family";
2411     case EM_MMDSP_PLUS:		return "STMicroelectronics 64bit VLIW Data Signal Processor";
2412     case EM_CYPRESS_M8C:	return "Cypress M8C microprocessor";
2413     case EM_R32C:		return "Renesas R32C series microprocessors";
2414     case EM_TRIMEDIA:		return "NXP Semiconductors TriMedia architecture family";
2415     case EM_QDSP6:		return "QUALCOMM DSP6 Processor";
2416     case EM_8051:		return "Intel 8051 and variants";
2417     case EM_STXP7X:		return "STMicroelectronics STxP7x family";
2418     case EM_NDS32:		return "Andes Technology compact code size embedded RISC processor family";
2419     case EM_ECOG1X:		return "Cyan Technology eCOG1X family";
2420     case EM_MAXQ30:		return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2421     case EM_XIMO16:		return "New Japan Radio (NJR) 16-bit DSP Processor";
2422     case EM_MANIK:		return "M2000 Reconfigurable RISC Microprocessor";
2423     case EM_CRAYNV2:		return "Cray Inc. NV2 vector architecture";
2424     case EM_CYGNUS_MEP:         return "Toshiba MeP Media Engine";
2425     case EM_CR16:
2426     case EM_MICROBLAZE:
2427     case EM_MICROBLAZE_OLD:	return "Xilinx MicroBlaze";
2428     case EM_RL78:		return "Renesas RL78";
2429     case EM_RX:			return "Renesas RX";
2430     case EM_METAG:		return "Imagination Technologies Meta processor architecture";
2431     case EM_MCST_ELBRUS:	return "MCST Elbrus general purpose hardware architecture";
2432     case EM_ECOG16:		return "Cyan Technology eCOG16 family";
2433     case EM_ETPU:		return "Freescale Extended Time Processing Unit";
2434     case EM_SLE9X:		return "Infineon Technologies SLE9X core";
2435     case EM_AVR32:		return "Atmel Corporation 32-bit microprocessor family";
2436     case EM_STM8:		return "STMicroeletronics STM8 8-bit microcontroller";
2437     case EM_TILE64:		return "Tilera TILE64 multicore architecture family";
2438     case EM_TILEPRO:		return "Tilera TILEPro multicore architecture family";
2439     case EM_TILEGX:		return "Tilera TILE-Gx multicore architecture family";
2440     case EM_CUDA:		return "NVIDIA CUDA architecture";
2441     case EM_XGATE:		return "Motorola XGATE embedded processor";
2442     default:
2443       snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2444       return buff;
2445     }
2446 }
2447 
2448 static void
decode_ARC_machine_flags(unsigned e_flags,unsigned e_machine,char buf[])2449 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2450 {
2451   /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2.  Some
2452      other compilers don't a specific architecture type in the e_flags, and
2453      instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2454      architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2455      architectures.
2456 
2457      Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2458      but also sets a specific architecture type in the e_flags field.
2459 
2460      However, when decoding the flags we don't worry if we see an
2461      unexpected pairing, for example EM_ARC_COMPACT machine type, with
2462      ARCEM architecture type.  */
2463 
2464   switch (e_flags & EF_ARC_MACH_MSK)
2465     {
2466       /* We only expect these to occur for EM_ARC_COMPACT2.  */
2467     case EF_ARC_CPU_ARCV2EM:
2468       strcat (buf, ", ARC EM");
2469       break;
2470     case EF_ARC_CPU_ARCV2HS:
2471       strcat (buf, ", ARC HS");
2472       break;
2473 
2474       /* We only expect these to occur for EM_ARC_COMPACT.  */
2475     case E_ARC_MACH_ARC600:
2476       strcat (buf, ", ARC600");
2477       break;
2478     case E_ARC_MACH_ARC601:
2479       strcat (buf, ", ARC601");
2480       break;
2481     case E_ARC_MACH_ARC700:
2482       strcat (buf, ", ARC700");
2483       break;
2484 
2485       /* The only times we should end up here are (a) A corrupt ELF, (b) A
2486          new ELF with new architecture being read by an old version of
2487          readelf, or (c) An ELF built with non-GNU compiler that does not
2488          set the architecture in the e_flags.  */
2489     default:
2490       if (e_machine == EM_ARC_COMPACT)
2491         strcat (buf, ", Unknown ARCompact");
2492       else
2493         strcat (buf, ", Unknown ARC");
2494       break;
2495     }
2496 
2497   switch (e_flags & EF_ARC_OSABI_MSK)
2498     {
2499     case E_ARC_OSABI_ORIG:
2500       strcat (buf, ", (ABI:legacy)");
2501       break;
2502     case E_ARC_OSABI_V2:
2503       strcat (buf, ", (ABI:v2)");
2504       break;
2505       /* Only upstream 3.9+ kernels will support ARCv2 ISA.  */
2506     case E_ARC_OSABI_V3:
2507       strcat (buf, ", v3 no-legacy-syscalls ABI");
2508       break;
2509     default:
2510       strcat (buf, ", unrecognised ARC OSABI flag");
2511       break;
2512     }
2513 }
2514 
2515 static void
decode_ARM_machine_flags(unsigned e_flags,char buf[])2516 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2517 {
2518   unsigned eabi;
2519   int unknown = 0;
2520 
2521   eabi = EF_ARM_EABI_VERSION (e_flags);
2522   e_flags &= ~ EF_ARM_EABIMASK;
2523 
2524   /* Handle "generic" ARM flags.  */
2525   if (e_flags & EF_ARM_RELEXEC)
2526     {
2527       strcat (buf, ", relocatable executable");
2528       e_flags &= ~ EF_ARM_RELEXEC;
2529     }
2530 
2531   /* Now handle EABI specific flags.  */
2532   switch (eabi)
2533     {
2534     default:
2535       strcat (buf, ", <unrecognized EABI>");
2536       if (e_flags)
2537 	unknown = 1;
2538       break;
2539 
2540     case EF_ARM_EABI_VER1:
2541       strcat (buf, ", Version1 EABI");
2542       while (e_flags)
2543 	{
2544 	  unsigned flag;
2545 
2546 	  /* Process flags one bit at a time.  */
2547 	  flag = e_flags & - e_flags;
2548 	  e_flags &= ~ flag;
2549 
2550 	  switch (flag)
2551 	    {
2552 	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2553 	      strcat (buf, ", sorted symbol tables");
2554 	      break;
2555 
2556 	    default:
2557 	      unknown = 1;
2558 	      break;
2559 	    }
2560 	}
2561       break;
2562 
2563     case EF_ARM_EABI_VER2:
2564       strcat (buf, ", Version2 EABI");
2565       while (e_flags)
2566 	{
2567 	  unsigned flag;
2568 
2569 	  /* Process flags one bit at a time.  */
2570 	  flag = e_flags & - e_flags;
2571 	  e_flags &= ~ flag;
2572 
2573 	  switch (flag)
2574 	    {
2575 	    case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2576 	      strcat (buf, ", sorted symbol tables");
2577 	      break;
2578 
2579 	    case EF_ARM_DYNSYMSUSESEGIDX:
2580 	      strcat (buf, ", dynamic symbols use segment index");
2581 	      break;
2582 
2583 	    case EF_ARM_MAPSYMSFIRST:
2584 	      strcat (buf, ", mapping symbols precede others");
2585 	      break;
2586 
2587 	    default:
2588 	      unknown = 1;
2589 	      break;
2590 	    }
2591 	}
2592       break;
2593 
2594     case EF_ARM_EABI_VER3:
2595       strcat (buf, ", Version3 EABI");
2596       break;
2597 
2598     case EF_ARM_EABI_VER4:
2599       strcat (buf, ", Version4 EABI");
2600       while (e_flags)
2601 	{
2602 	  unsigned flag;
2603 
2604 	  /* Process flags one bit at a time.  */
2605 	  flag = e_flags & - e_flags;
2606 	  e_flags &= ~ flag;
2607 
2608 	  switch (flag)
2609 	    {
2610 	    case EF_ARM_BE8:
2611 	      strcat (buf, ", BE8");
2612 	      break;
2613 
2614 	    case EF_ARM_LE8:
2615 	      strcat (buf, ", LE8");
2616 	      break;
2617 
2618 	    default:
2619 	      unknown = 1;
2620 	      break;
2621 	    }
2622       break;
2623 	}
2624       break;
2625 
2626     case EF_ARM_EABI_VER5:
2627       strcat (buf, ", Version5 EABI");
2628       while (e_flags)
2629 	{
2630 	  unsigned flag;
2631 
2632 	  /* Process flags one bit at a time.  */
2633 	  flag = e_flags & - e_flags;
2634 	  e_flags &= ~ flag;
2635 
2636 	  switch (flag)
2637 	    {
2638 	    case EF_ARM_BE8:
2639 	      strcat (buf, ", BE8");
2640 	      break;
2641 
2642 	    case EF_ARM_LE8:
2643 	      strcat (buf, ", LE8");
2644 	      break;
2645 
2646 	    case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT.  */
2647 	      strcat (buf, ", soft-float ABI");
2648 	      break;
2649 
2650 	    case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT.  */
2651 	      strcat (buf, ", hard-float ABI");
2652 	      break;
2653 
2654 	    default:
2655 	      unknown = 1;
2656 	      break;
2657 	    }
2658 	}
2659       break;
2660 
2661     case EF_ARM_EABI_UNKNOWN:
2662       strcat (buf, ", GNU EABI");
2663       while (e_flags)
2664 	{
2665 	  unsigned flag;
2666 
2667 	  /* Process flags one bit at a time.  */
2668 	  flag = e_flags & - e_flags;
2669 	  e_flags &= ~ flag;
2670 
2671 	  switch (flag)
2672 	    {
2673 	    case EF_ARM_INTERWORK:
2674 	      strcat (buf, ", interworking enabled");
2675 	      break;
2676 
2677 	    case EF_ARM_APCS_26:
2678 	      strcat (buf, ", uses APCS/26");
2679 	      break;
2680 
2681 	    case EF_ARM_APCS_FLOAT:
2682 	      strcat (buf, ", uses APCS/float");
2683 	      break;
2684 
2685 	    case EF_ARM_PIC:
2686 	      strcat (buf, ", position independent");
2687 	      break;
2688 
2689 	    case EF_ARM_ALIGN8:
2690 	      strcat (buf, ", 8 bit structure alignment");
2691 	      break;
2692 
2693 	    case EF_ARM_NEW_ABI:
2694 	      strcat (buf, ", uses new ABI");
2695 	      break;
2696 
2697 	    case EF_ARM_OLD_ABI:
2698 	      strcat (buf, ", uses old ABI");
2699 	      break;
2700 
2701 	    case EF_ARM_SOFT_FLOAT:
2702 	      strcat (buf, ", software FP");
2703 	      break;
2704 
2705 	    case EF_ARM_VFP_FLOAT:
2706 	      strcat (buf, ", VFP");
2707 	      break;
2708 
2709 	    case EF_ARM_MAVERICK_FLOAT:
2710 	      strcat (buf, ", Maverick FP");
2711 	      break;
2712 
2713 	    default:
2714 	      unknown = 1;
2715 	      break;
2716 	    }
2717 	}
2718     }
2719 
2720   if (unknown)
2721     strcat (buf,_(", <unknown>"));
2722 }
2723 
2724 static void
decode_AVR_machine_flags(unsigned e_flags,char buf[],size_t size)2725 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
2726 {
2727   --size; /* Leave space for null terminator.  */
2728 
2729   switch (e_flags & EF_AVR_MACH)
2730     {
2731     case E_AVR_MACH_AVR1:
2732       strncat (buf, ", avr:1", size);
2733       break;
2734     case E_AVR_MACH_AVR2:
2735       strncat (buf, ", avr:2", size);
2736       break;
2737     case E_AVR_MACH_AVR25:
2738       strncat (buf, ", avr:25", size);
2739       break;
2740     case E_AVR_MACH_AVR3:
2741       strncat (buf, ", avr:3", size);
2742       break;
2743     case E_AVR_MACH_AVR31:
2744       strncat (buf, ", avr:31", size);
2745       break;
2746     case E_AVR_MACH_AVR35:
2747       strncat (buf, ", avr:35", size);
2748       break;
2749     case E_AVR_MACH_AVR4:
2750       strncat (buf, ", avr:4", size);
2751       break;
2752     case E_AVR_MACH_AVR5:
2753       strncat (buf, ", avr:5", size);
2754       break;
2755     case E_AVR_MACH_AVR51:
2756       strncat (buf, ", avr:51", size);
2757       break;
2758     case E_AVR_MACH_AVR6:
2759       strncat (buf, ", avr:6", size);
2760       break;
2761     case E_AVR_MACH_AVRTINY:
2762       strncat (buf, ", avr:100", size);
2763       break;
2764     case E_AVR_MACH_XMEGA1:
2765       strncat (buf, ", avr:101", size);
2766       break;
2767     case E_AVR_MACH_XMEGA2:
2768       strncat (buf, ", avr:102", size);
2769       break;
2770     case E_AVR_MACH_XMEGA3:
2771       strncat (buf, ", avr:103", size);
2772       break;
2773     case E_AVR_MACH_XMEGA4:
2774       strncat (buf, ", avr:104", size);
2775       break;
2776     case E_AVR_MACH_XMEGA5:
2777       strncat (buf, ", avr:105", size);
2778       break;
2779     case E_AVR_MACH_XMEGA6:
2780       strncat (buf, ", avr:106", size);
2781       break;
2782     case E_AVR_MACH_XMEGA7:
2783       strncat (buf, ", avr:107", size);
2784       break;
2785     default:
2786       strncat (buf, ", avr:<unknown>", size);
2787       break;
2788     }
2789 
2790   size -= strlen (buf);
2791   if (e_flags & EF_AVR_LINKRELAX_PREPARED)
2792     strncat (buf, ", link-relax", size);
2793 }
2794 
2795 static void
decode_NDS32_machine_flags(unsigned e_flags,char buf[],size_t size)2796 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2797 {
2798   unsigned abi;
2799   unsigned arch;
2800   unsigned config;
2801   unsigned version;
2802   int has_fpu = 0;
2803   int r = 0;
2804 
2805   static const char *ABI_STRINGS[] =
2806   {
2807     "ABI v0", /* use r5 as return register; only used in N1213HC */
2808     "ABI v1", /* use r0 as return register */
2809     "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2810     "ABI v2fp", /* for FPU */
2811     "AABI",
2812     "ABI2 FP+"
2813   };
2814   static const char *VER_STRINGS[] =
2815   {
2816     "Andes ELF V1.3 or older",
2817     "Andes ELF V1.3.1",
2818     "Andes ELF V1.4"
2819   };
2820   static const char *ARCH_STRINGS[] =
2821   {
2822     "",
2823     "Andes Star v1.0",
2824     "Andes Star v2.0",
2825     "Andes Star v3.0",
2826     "Andes Star v3.0m"
2827   };
2828 
2829   abi = EF_NDS_ABI & e_flags;
2830   arch = EF_NDS_ARCH & e_flags;
2831   config = EF_NDS_INST & e_flags;
2832   version = EF_NDS32_ELF_VERSION & e_flags;
2833 
2834   memset (buf, 0, size);
2835 
2836   switch (abi)
2837     {
2838     case E_NDS_ABI_V0:
2839     case E_NDS_ABI_V1:
2840     case E_NDS_ABI_V2:
2841     case E_NDS_ABI_V2FP:
2842     case E_NDS_ABI_AABI:
2843     case E_NDS_ABI_V2FP_PLUS:
2844       /* In case there are holes in the array.  */
2845       r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
2846       break;
2847 
2848     default:
2849       r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
2850       break;
2851     }
2852 
2853   switch (version)
2854     {
2855     case E_NDS32_ELF_VER_1_2:
2856     case E_NDS32_ELF_VER_1_3:
2857     case E_NDS32_ELF_VER_1_4:
2858       r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
2859       break;
2860 
2861     default:
2862       r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
2863       break;
2864     }
2865 
2866   if (E_NDS_ABI_V0 == abi)
2867     {
2868       /* OLD ABI; only used in N1213HC, has performance extension 1.  */
2869       r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
2870       if (arch == E_NDS_ARCH_STAR_V1_0)
2871 	r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
2872       return;
2873     }
2874 
2875   switch (arch)
2876     {
2877     case E_NDS_ARCH_STAR_V1_0:
2878     case E_NDS_ARCH_STAR_V2_0:
2879     case E_NDS_ARCH_STAR_V3_0:
2880     case E_NDS_ARCH_STAR_V3_M:
2881       r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
2882       break;
2883 
2884     default:
2885       r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
2886       /* ARCH version determines how the e_flags are interpreted.
2887 	 If it is unknown, we cannot proceed.  */
2888       return;
2889     }
2890 
2891   /* Newer ABI; Now handle architecture specific flags.  */
2892   if (arch == E_NDS_ARCH_STAR_V1_0)
2893     {
2894       if (config & E_NDS32_HAS_MFUSR_PC_INST)
2895 	r += snprintf (buf + r, size -r, ", MFUSR_PC");
2896 
2897       if (!(config & E_NDS32_HAS_NO_MAC_INST))
2898 	r += snprintf (buf + r, size -r, ", MAC");
2899 
2900       if (config & E_NDS32_HAS_DIV_INST)
2901 	r += snprintf (buf + r, size -r, ", DIV");
2902 
2903       if (config & E_NDS32_HAS_16BIT_INST)
2904 	r += snprintf (buf + r, size -r, ", 16b");
2905     }
2906   else
2907     {
2908       if (config & E_NDS32_HAS_MFUSR_PC_INST)
2909 	{
2910 	  if (version <= E_NDS32_ELF_VER_1_3)
2911 	    r += snprintf (buf + r, size -r, ", [B8]");
2912 	  else
2913 	    r += snprintf (buf + r, size -r, ", EX9");
2914 	}
2915 
2916       if (config & E_NDS32_HAS_MAC_DX_INST)
2917 	r += snprintf (buf + r, size -r, ", MAC_DX");
2918 
2919       if (config & E_NDS32_HAS_DIV_DX_INST)
2920 	r += snprintf (buf + r, size -r, ", DIV_DX");
2921 
2922       if (config & E_NDS32_HAS_16BIT_INST)
2923 	{
2924 	  if (version <= E_NDS32_ELF_VER_1_3)
2925 	    r += snprintf (buf + r, size -r, ", 16b");
2926 	  else
2927 	    r += snprintf (buf + r, size -r, ", IFC");
2928 	}
2929     }
2930 
2931   if (config & E_NDS32_HAS_EXT_INST)
2932     r += snprintf (buf + r, size -r, ", PERF1");
2933 
2934   if (config & E_NDS32_HAS_EXT2_INST)
2935     r += snprintf (buf + r, size -r, ", PERF2");
2936 
2937   if (config & E_NDS32_HAS_FPU_INST)
2938     {
2939       has_fpu = 1;
2940       r += snprintf (buf + r, size -r, ", FPU_SP");
2941     }
2942 
2943   if (config & E_NDS32_HAS_FPU_DP_INST)
2944     {
2945       has_fpu = 1;
2946       r += snprintf (buf + r, size -r, ", FPU_DP");
2947     }
2948 
2949   if (config & E_NDS32_HAS_FPU_MAC_INST)
2950     {
2951       has_fpu = 1;
2952       r += snprintf (buf + r, size -r, ", FPU_MAC");
2953     }
2954 
2955   if (has_fpu)
2956     {
2957       switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
2958 	{
2959 	case E_NDS32_FPU_REG_8SP_4DP:
2960 	  r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
2961 	  break;
2962 	case E_NDS32_FPU_REG_16SP_8DP:
2963 	  r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
2964 	  break;
2965 	case E_NDS32_FPU_REG_32SP_16DP:
2966 	  r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
2967 	  break;
2968 	case E_NDS32_FPU_REG_32SP_32DP:
2969 	  r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
2970 	  break;
2971 	}
2972     }
2973 
2974   if (config & E_NDS32_HAS_AUDIO_INST)
2975     r += snprintf (buf + r, size -r, ", AUDIO");
2976 
2977   if (config & E_NDS32_HAS_STRING_INST)
2978     r += snprintf (buf + r, size -r, ", STR");
2979 
2980   if (config & E_NDS32_HAS_REDUCED_REGS)
2981     r += snprintf (buf + r, size -r, ", 16REG");
2982 
2983   if (config & E_NDS32_HAS_VIDEO_INST)
2984     {
2985       if (version <= E_NDS32_ELF_VER_1_3)
2986 	r += snprintf (buf + r, size -r, ", VIDEO");
2987       else
2988 	r += snprintf (buf + r, size -r, ", SATURATION");
2989     }
2990 
2991   if (config & E_NDS32_HAS_ENCRIPT_INST)
2992     r += snprintf (buf + r, size -r, ", ENCRP");
2993 
2994   if (config & E_NDS32_HAS_L2C_INST)
2995     r += snprintf (buf + r, size -r, ", L2C");
2996 }
2997 
2998 static char *
get_machine_flags(unsigned e_flags,unsigned e_machine)2999 get_machine_flags (unsigned e_flags, unsigned e_machine)
3000 {
3001   static char buf[1024];
3002 
3003   buf[0] = '\0';
3004 
3005   if (e_flags)
3006     {
3007       switch (e_machine)
3008 	{
3009 	default:
3010 	  break;
3011 
3012 	case EM_ARC_COMPACT2:
3013 	case EM_ARC_COMPACT:
3014           decode_ARC_machine_flags (e_flags, e_machine, buf);
3015           break;
3016 
3017 	case EM_ARM:
3018 	  decode_ARM_machine_flags (e_flags, buf);
3019 	  break;
3020 
3021         case EM_AVR:
3022           decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3023           break;
3024 
3025 	case EM_BLACKFIN:
3026 	  if (e_flags & EF_BFIN_PIC)
3027 	    strcat (buf, ", PIC");
3028 
3029 	  if (e_flags & EF_BFIN_FDPIC)
3030 	    strcat (buf, ", FDPIC");
3031 
3032 	  if (e_flags & EF_BFIN_CODE_IN_L1)
3033 	    strcat (buf, ", code in L1");
3034 
3035 	  if (e_flags & EF_BFIN_DATA_IN_L1)
3036 	    strcat (buf, ", data in L1");
3037 
3038 	  break;
3039 
3040 	case EM_CYGNUS_FRV:
3041 	  switch (e_flags & EF_FRV_CPU_MASK)
3042 	    {
3043 	    case EF_FRV_CPU_GENERIC:
3044 	      break;
3045 
3046 	    default:
3047 	      strcat (buf, ", fr???");
3048 	      break;
3049 
3050 	    case EF_FRV_CPU_FR300:
3051 	      strcat (buf, ", fr300");
3052 	      break;
3053 
3054 	    case EF_FRV_CPU_FR400:
3055 	      strcat (buf, ", fr400");
3056 	      break;
3057 	    case EF_FRV_CPU_FR405:
3058 	      strcat (buf, ", fr405");
3059 	      break;
3060 
3061 	    case EF_FRV_CPU_FR450:
3062 	      strcat (buf, ", fr450");
3063 	      break;
3064 
3065 	    case EF_FRV_CPU_FR500:
3066 	      strcat (buf, ", fr500");
3067 	      break;
3068 	    case EF_FRV_CPU_FR550:
3069 	      strcat (buf, ", fr550");
3070 	      break;
3071 
3072 	    case EF_FRV_CPU_SIMPLE:
3073 	      strcat (buf, ", simple");
3074 	      break;
3075 	    case EF_FRV_CPU_TOMCAT:
3076 	      strcat (buf, ", tomcat");
3077 	      break;
3078 	    }
3079 	  break;
3080 
3081 	case EM_68K:
3082 	  if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3083 	    strcat (buf, ", m68000");
3084 	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3085 	    strcat (buf, ", cpu32");
3086 	  else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3087 	    strcat (buf, ", fido_a");
3088 	  else
3089 	    {
3090 	      char const * isa = _("unknown");
3091 	      char const * mac = _("unknown mac");
3092 	      char const * additional = NULL;
3093 
3094 	      switch (e_flags & EF_M68K_CF_ISA_MASK)
3095 		{
3096 		case EF_M68K_CF_ISA_A_NODIV:
3097 		  isa = "A";
3098 		  additional = ", nodiv";
3099 		  break;
3100 		case EF_M68K_CF_ISA_A:
3101 		  isa = "A";
3102 		  break;
3103 		case EF_M68K_CF_ISA_A_PLUS:
3104 		  isa = "A+";
3105 		  break;
3106 		case EF_M68K_CF_ISA_B_NOUSP:
3107 		  isa = "B";
3108 		  additional = ", nousp";
3109 		  break;
3110 		case EF_M68K_CF_ISA_B:
3111 		  isa = "B";
3112 		  break;
3113 		case EF_M68K_CF_ISA_C:
3114 		  isa = "C";
3115 		  break;
3116 		case EF_M68K_CF_ISA_C_NODIV:
3117 		  isa = "C";
3118 		  additional = ", nodiv";
3119 		  break;
3120 		}
3121 	      strcat (buf, ", cf, isa ");
3122 	      strcat (buf, isa);
3123 	      if (additional)
3124 		strcat (buf, additional);
3125 	      if (e_flags & EF_M68K_CF_FLOAT)
3126 		strcat (buf, ", float");
3127 	      switch (e_flags & EF_M68K_CF_MAC_MASK)
3128 		{
3129 		case 0:
3130 		  mac = NULL;
3131 		  break;
3132 		case EF_M68K_CF_MAC:
3133 		  mac = "mac";
3134 		  break;
3135 		case EF_M68K_CF_EMAC:
3136 		  mac = "emac";
3137 		  break;
3138 		case EF_M68K_CF_EMAC_B:
3139 		  mac = "emac_b";
3140 		  break;
3141 		}
3142 	      if (mac)
3143 		{
3144 		  strcat (buf, ", ");
3145 		  strcat (buf, mac);
3146 		}
3147 	    }
3148 	  break;
3149 
3150 	case EM_CYGNUS_MEP:
3151 	  switch (e_flags & EF_MEP_CPU_MASK)
3152 	    {
3153 	    case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3154 	    case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3155 	    case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3156 	    case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3157 	    case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3158 	    case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3159 	    default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3160 	    }
3161 
3162 	  switch (e_flags & EF_MEP_COP_MASK)
3163 	    {
3164 	    case EF_MEP_COP_NONE: break;
3165 	    case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3166 	    case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3167 	    case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3168 	    case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3169 	    default: strcat (buf, _("<unknown MeP copro type>")); break;
3170 	    }
3171 
3172 	  if (e_flags & EF_MEP_LIBRARY)
3173 	    strcat (buf, ", Built for Library");
3174 
3175 	  if (e_flags & EF_MEP_INDEX_MASK)
3176 	    sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3177 		     e_flags & EF_MEP_INDEX_MASK);
3178 
3179 	  if (e_flags & ~ EF_MEP_ALL_FLAGS)
3180 	    sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3181 		     e_flags & ~ EF_MEP_ALL_FLAGS);
3182 	  break;
3183 
3184 	case EM_PPC:
3185 	  if (e_flags & EF_PPC_EMB)
3186 	    strcat (buf, ", emb");
3187 
3188 	  if (e_flags & EF_PPC_RELOCATABLE)
3189 	    strcat (buf, _(", relocatable"));
3190 
3191 	  if (e_flags & EF_PPC_RELOCATABLE_LIB)
3192 	    strcat (buf, _(", relocatable-lib"));
3193 	  break;
3194 
3195 	case EM_PPC64:
3196 	  if (e_flags & EF_PPC64_ABI)
3197 	    {
3198 	      char abi[] = ", abiv0";
3199 
3200 	      abi[6] += e_flags & EF_PPC64_ABI;
3201 	      strcat (buf, abi);
3202 	    }
3203 	  break;
3204 
3205 	case EM_V800:
3206 	  if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3207 	    strcat (buf, ", RH850 ABI");
3208 
3209 	  if (e_flags & EF_V800_850E3)
3210 	    strcat (buf, ", V3 architecture");
3211 
3212 	  if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3213 	    strcat (buf, ", FPU not used");
3214 
3215 	  if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3216 	    strcat (buf, ", regmode: COMMON");
3217 
3218 	  if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3219 	    strcat (buf, ", r4 not used");
3220 
3221 	  if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3222 	    strcat (buf, ", r30 not used");
3223 
3224 	  if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3225 	    strcat (buf, ", r5 not used");
3226 
3227 	  if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3228 	    strcat (buf, ", r2 not used");
3229 
3230 	  for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3231 	    {
3232 	      switch (e_flags & - e_flags)
3233 		{
3234 		case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3235 		case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3236 		case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3237 		case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3238 		case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3239 		case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3240 		case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3241 		case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3242 		case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3243 		case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3244 		case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3245 		case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3246 		default: break;
3247 		}
3248 	    }
3249 	  break;
3250 
3251 	case EM_V850:
3252 	case EM_CYGNUS_V850:
3253 	  switch (e_flags & EF_V850_ARCH)
3254 	    {
3255 	    case E_V850E3V5_ARCH:
3256 	      strcat (buf, ", v850e3v5");
3257 	      break;
3258 	    case E_V850E2V3_ARCH:
3259 	      strcat (buf, ", v850e2v3");
3260 	      break;
3261 	    case E_V850E2_ARCH:
3262 	      strcat (buf, ", v850e2");
3263 	      break;
3264             case E_V850E1_ARCH:
3265               strcat (buf, ", v850e1");
3266 	      break;
3267 	    case E_V850E_ARCH:
3268 	      strcat (buf, ", v850e");
3269 	      break;
3270 	    case E_V850_ARCH:
3271 	      strcat (buf, ", v850");
3272 	      break;
3273 	    default:
3274 	      strcat (buf, _(", unknown v850 architecture variant"));
3275 	      break;
3276 	    }
3277 	  break;
3278 
3279 	case EM_M32R:
3280 	case EM_CYGNUS_M32R:
3281 	  if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3282 	    strcat (buf, ", m32r");
3283 	  break;
3284 
3285 	case EM_MIPS:
3286 	case EM_MIPS_RS3_LE:
3287 	  if (e_flags & EF_MIPS_NOREORDER)
3288 	    strcat (buf, ", noreorder");
3289 
3290 	  if (e_flags & EF_MIPS_PIC)
3291 	    strcat (buf, ", pic");
3292 
3293 	  if (e_flags & EF_MIPS_CPIC)
3294 	    strcat (buf, ", cpic");
3295 
3296 	  if (e_flags & EF_MIPS_UCODE)
3297 	    strcat (buf, ", ugen_reserved");
3298 
3299 	  if (e_flags & EF_MIPS_ABI2)
3300 	    strcat (buf, ", abi2");
3301 
3302 	  if (e_flags & EF_MIPS_OPTIONS_FIRST)
3303 	    strcat (buf, ", odk first");
3304 
3305 	  if (e_flags & EF_MIPS_32BITMODE)
3306 	    strcat (buf, ", 32bitmode");
3307 
3308 	  if (e_flags & EF_MIPS_NAN2008)
3309 	    strcat (buf, ", nan2008");
3310 
3311 	  if (e_flags & EF_MIPS_FP64)
3312 	    strcat (buf, ", fp64");
3313 
3314 	  switch ((e_flags & EF_MIPS_MACH))
3315 	    {
3316 	    case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3317 	    case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3318 	    case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3319 	    case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3320 	    case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3321 	    case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3322 	    case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3323 	    case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3324 	    case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
3325 	    case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3326   	    case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3327   	    case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3328   	    case E_MIPS_MACH_LS3A: strcat (buf, ", loongson-3a"); break;
3329 	    case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3330 	    case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3331 	    case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3332 	    case E_MIPS_MACH_XLR:  strcat (buf, ", xlr"); break;
3333 	    case 0:
3334 	    /* We simply ignore the field in this case to avoid confusion:
3335 	       MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3336 	       extension.  */
3337 	      break;
3338 	    default: strcat (buf, _(", unknown CPU")); break;
3339 	    }
3340 
3341 	  switch ((e_flags & EF_MIPS_ABI))
3342 	    {
3343 	    case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3344 	    case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3345 	    case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3346 	    case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3347 	    case 0:
3348 	    /* We simply ignore the field in this case to avoid confusion:
3349 	       MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3350 	       This means it is likely to be an o32 file, but not for
3351 	       sure.  */
3352 	      break;
3353 	    default: strcat (buf, _(", unknown ABI")); break;
3354 	    }
3355 
3356 	  if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3357 	    strcat (buf, ", mdmx");
3358 
3359 	  if (e_flags & EF_MIPS_ARCH_ASE_M16)
3360 	    strcat (buf, ", mips16");
3361 
3362 	  if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3363 	    strcat (buf, ", micromips");
3364 
3365 	  switch ((e_flags & EF_MIPS_ARCH))
3366 	    {
3367 	    case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3368 	    case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3369 	    case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3370 	    case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3371 	    case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3372 	    case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3373 	    case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3374 	    case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3375 	    case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3376 	    case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3377 	    case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3378 	    default: strcat (buf, _(", unknown ISA")); break;
3379 	    }
3380 	  break;
3381 
3382 	case EM_NDS32:
3383 	  decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3384 	  break;
3385 
3386 	case EM_SH:
3387 	  switch ((e_flags & EF_SH_MACH_MASK))
3388 	    {
3389 	    case EF_SH1: strcat (buf, ", sh1"); break;
3390 	    case EF_SH2: strcat (buf, ", sh2"); break;
3391 	    case EF_SH3: strcat (buf, ", sh3"); break;
3392 	    case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3393 	    case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3394 	    case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3395 	    case EF_SH3E: strcat (buf, ", sh3e"); break;
3396 	    case EF_SH4: strcat (buf, ", sh4"); break;
3397 	    case EF_SH5: strcat (buf, ", sh5"); break;
3398 	    case EF_SH2E: strcat (buf, ", sh2e"); break;
3399 	    case EF_SH4A: strcat (buf, ", sh4a"); break;
3400 	    case EF_SH2A: strcat (buf, ", sh2a"); break;
3401 	    case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3402 	    case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3403 	    case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3404 	    case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3405 	    case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3406 	    case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3407 	    case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3408 	    case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3409 	    case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3410 	    default: strcat (buf, _(", unknown ISA")); break;
3411 	    }
3412 
3413 	  if (e_flags & EF_SH_PIC)
3414 	    strcat (buf, ", pic");
3415 
3416 	  if (e_flags & EF_SH_FDPIC)
3417 	    strcat (buf, ", fdpic");
3418 	  break;
3419 
3420         case EM_OR1K:
3421           if (e_flags & EF_OR1K_NODELAY)
3422             strcat (buf, ", no delay");
3423           break;
3424 
3425 	case EM_SPARCV9:
3426 	  if (e_flags & EF_SPARC_32PLUS)
3427 	    strcat (buf, ", v8+");
3428 
3429 	  if (e_flags & EF_SPARC_SUN_US1)
3430 	    strcat (buf, ", ultrasparcI");
3431 
3432 	  if (e_flags & EF_SPARC_SUN_US3)
3433 	    strcat (buf, ", ultrasparcIII");
3434 
3435 	  if (e_flags & EF_SPARC_HAL_R1)
3436 	    strcat (buf, ", halr1");
3437 
3438 	  if (e_flags & EF_SPARC_LEDATA)
3439 	    strcat (buf, ", ledata");
3440 
3441 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3442 	    strcat (buf, ", tso");
3443 
3444 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3445 	    strcat (buf, ", pso");
3446 
3447 	  if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3448 	    strcat (buf, ", rmo");
3449 	  break;
3450 
3451 	case EM_PARISC:
3452 	  switch (e_flags & EF_PARISC_ARCH)
3453 	    {
3454 	    case EFA_PARISC_1_0:
3455 	      strcpy (buf, ", PA-RISC 1.0");
3456 	      break;
3457 	    case EFA_PARISC_1_1:
3458 	      strcpy (buf, ", PA-RISC 1.1");
3459 	      break;
3460 	    case EFA_PARISC_2_0:
3461 	      strcpy (buf, ", PA-RISC 2.0");
3462 	      break;
3463 	    default:
3464 	      break;
3465 	    }
3466 	  if (e_flags & EF_PARISC_TRAPNIL)
3467 	    strcat (buf, ", trapnil");
3468 	  if (e_flags & EF_PARISC_EXT)
3469 	    strcat (buf, ", ext");
3470 	  if (e_flags & EF_PARISC_LSB)
3471 	    strcat (buf, ", lsb");
3472 	  if (e_flags & EF_PARISC_WIDE)
3473 	    strcat (buf, ", wide");
3474 	  if (e_flags & EF_PARISC_NO_KABP)
3475 	    strcat (buf, ", no kabp");
3476 	  if (e_flags & EF_PARISC_LAZYSWAP)
3477 	    strcat (buf, ", lazyswap");
3478 	  break;
3479 
3480 	case EM_PJ:
3481 	case EM_PJ_OLD:
3482 	  if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3483 	    strcat (buf, ", new calling convention");
3484 
3485 	  if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3486 	    strcat (buf, ", gnu calling convention");
3487 	  break;
3488 
3489 	case EM_IA_64:
3490 	  if ((e_flags & EF_IA_64_ABI64))
3491 	    strcat (buf, ", 64-bit");
3492 	  else
3493 	    strcat (buf, ", 32-bit");
3494 	  if ((e_flags & EF_IA_64_REDUCEDFP))
3495 	    strcat (buf, ", reduced fp model");
3496 	  if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3497 	    strcat (buf, ", no function descriptors, constant gp");
3498 	  else if ((e_flags & EF_IA_64_CONS_GP))
3499 	    strcat (buf, ", constant gp");
3500 	  if ((e_flags & EF_IA_64_ABSOLUTE))
3501 	    strcat (buf, ", absolute");
3502           if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3503             {
3504               if ((e_flags & EF_IA_64_VMS_LINKAGES))
3505                 strcat (buf, ", vms_linkages");
3506               switch ((e_flags & EF_IA_64_VMS_COMCOD))
3507                 {
3508                 case EF_IA_64_VMS_COMCOD_SUCCESS:
3509                   break;
3510                 case EF_IA_64_VMS_COMCOD_WARNING:
3511                   strcat (buf, ", warning");
3512                   break;
3513                 case EF_IA_64_VMS_COMCOD_ERROR:
3514                   strcat (buf, ", error");
3515                   break;
3516                 case EF_IA_64_VMS_COMCOD_ABORT:
3517                   strcat (buf, ", abort");
3518                   break;
3519                 default:
3520 		  warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3521 			e_flags & EF_IA_64_VMS_COMCOD);
3522 		  strcat (buf, ", <unknown>");
3523                 }
3524             }
3525 	  break;
3526 
3527 	case EM_VAX:
3528 	  if ((e_flags & EF_VAX_NONPIC))
3529 	    strcat (buf, ", non-PIC");
3530 	  if ((e_flags & EF_VAX_DFLOAT))
3531 	    strcat (buf, ", D-Float");
3532 	  if ((e_flags & EF_VAX_GFLOAT))
3533 	    strcat (buf, ", G-Float");
3534 	  break;
3535 
3536         case EM_VISIUM:
3537 	  if (e_flags & EF_VISIUM_ARCH_MCM)
3538 	    strcat (buf, ", mcm");
3539 	  else if (e_flags & EF_VISIUM_ARCH_MCM24)
3540 	    strcat (buf, ", mcm24");
3541 	  if (e_flags & EF_VISIUM_ARCH_GR6)
3542 	    strcat (buf, ", gr6");
3543 	  break;
3544 
3545 	case EM_RL78:
3546 	  switch (e_flags & E_FLAG_RL78_CPU_MASK)
3547 	    {
3548 	    case E_FLAG_RL78_ANY_CPU: break;
3549 	    case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3550 	    case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3551 	    case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3552 	    }
3553 	  if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3554 	    strcat (buf, ", 64-bit doubles");
3555 	  break;
3556 
3557 	case EM_RX:
3558 	  if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3559 	    strcat (buf, ", 64-bit doubles");
3560 	  if (e_flags & E_FLAG_RX_DSP)
3561 	    strcat (buf, ", dsp");
3562 	  if (e_flags & E_FLAG_RX_PID)
3563 	    strcat (buf, ", pid");
3564 	  if (e_flags & E_FLAG_RX_ABI)
3565 	    strcat (buf, ", RX ABI");
3566 	  if (e_flags & E_FLAG_RX_SINSNS_SET)
3567 	    strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3568 		    ? ", uses String instructions" : ", bans String instructions");
3569 	  if (e_flags & E_FLAG_RX_V2)
3570 	    strcat (buf, ", V2");
3571 	  break;
3572 
3573 	case EM_S390:
3574 	  if (e_flags & EF_S390_HIGH_GPRS)
3575 	    strcat (buf, ", highgprs");
3576 	  break;
3577 
3578 	case EM_TI_C6000:
3579 	  if ((e_flags & EF_C6000_REL))
3580 	    strcat (buf, ", relocatable module");
3581 	  break;
3582 
3583 	case EM_MSP430:
3584 	  strcat (buf, _(": architecture variant: "));
3585 	  switch (e_flags & EF_MSP430_MACH)
3586 	    {
3587 	    case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3588 	    case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3589 	    case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3590 	    case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3591 	    case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3592 	    case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3593 	    case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3594 	    case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3595 	    case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3596 	    case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3597 	    case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3598 	    case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3599 	    case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3600 	    case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3601 	    case E_MSP430_MACH_MSP430X  : strcat (buf, "MSP430X"); break;
3602 	    default:
3603 	      strcat (buf, _(": unknown")); break;
3604 	    }
3605 
3606 	  if (e_flags & ~ EF_MSP430_MACH)
3607 	    strcat (buf, _(": unknown extra flag bits also present"));
3608 	}
3609     }
3610 
3611   return buf;
3612 }
3613 
3614 static const char *
get_osabi_name(unsigned int osabi)3615 get_osabi_name (unsigned int osabi)
3616 {
3617   static char buff[32];
3618 
3619   switch (osabi)
3620     {
3621     case ELFOSABI_NONE:		return "UNIX - System V";
3622     case ELFOSABI_HPUX:		return "UNIX - HP-UX";
3623     case ELFOSABI_NETBSD:	return "UNIX - NetBSD";
3624     case ELFOSABI_GNU:		return "UNIX - GNU";
3625     case ELFOSABI_SOLARIS:	return "UNIX - Solaris";
3626     case ELFOSABI_AIX:		return "UNIX - AIX";
3627     case ELFOSABI_IRIX:		return "UNIX - IRIX";
3628     case ELFOSABI_FREEBSD:	return "UNIX - FreeBSD";
3629     case ELFOSABI_TRU64:	return "UNIX - TRU64";
3630     case ELFOSABI_MODESTO:	return "Novell - Modesto";
3631     case ELFOSABI_OPENBSD:	return "UNIX - OpenBSD";
3632     case ELFOSABI_OPENVMS:	return "VMS - OpenVMS";
3633     case ELFOSABI_NSK:		return "HP - Non-Stop Kernel";
3634     case ELFOSABI_AROS:		return "AROS";
3635     case ELFOSABI_FENIXOS:	return "FenixOS";
3636     default:
3637       if (osabi >= 64)
3638 	switch (elf_header.e_machine)
3639 	  {
3640 	  case EM_ARM:
3641 	    switch (osabi)
3642 	      {
3643 	      case ELFOSABI_ARM:	return "ARM";
3644 	      default:
3645 		break;
3646 	      }
3647 	    break;
3648 
3649 	  case EM_MSP430:
3650 	  case EM_MSP430_OLD:
3651 	  case EM_VISIUM:
3652 	    switch (osabi)
3653 	      {
3654 	      case ELFOSABI_STANDALONE:	return _("Standalone App");
3655 	      default:
3656 		break;
3657 	      }
3658 	    break;
3659 
3660 	  case EM_TI_C6000:
3661 	    switch (osabi)
3662 	      {
3663 	      case ELFOSABI_C6000_ELFABI:	return _("Bare-metal C6000");
3664 	      case ELFOSABI_C6000_LINUX:	return "Linux C6000";
3665 	      default:
3666 		break;
3667 	      }
3668 	    break;
3669 
3670 	  default:
3671 	    break;
3672 	  }
3673       snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3674       return buff;
3675     }
3676 }
3677 
3678 static const char *
get_aarch64_segment_type(unsigned long type)3679 get_aarch64_segment_type (unsigned long type)
3680 {
3681   switch (type)
3682     {
3683     case PT_AARCH64_ARCHEXT:
3684       return "AARCH64_ARCHEXT";
3685     default:
3686       break;
3687     }
3688 
3689   return NULL;
3690 }
3691 
3692 static const char *
get_arm_segment_type(unsigned long type)3693 get_arm_segment_type (unsigned long type)
3694 {
3695   switch (type)
3696     {
3697     case PT_ARM_EXIDX:
3698       return "EXIDX";
3699     default:
3700       break;
3701     }
3702 
3703   return NULL;
3704 }
3705 
3706 static const char *
get_mips_segment_type(unsigned long type)3707 get_mips_segment_type (unsigned long type)
3708 {
3709   switch (type)
3710     {
3711     case PT_MIPS_REGINFO:
3712       return "REGINFO";
3713     case PT_MIPS_RTPROC:
3714       return "RTPROC";
3715     case PT_MIPS_OPTIONS:
3716       return "OPTIONS";
3717     case PT_MIPS_ABIFLAGS:
3718       return "ABIFLAGS";
3719     default:
3720       break;
3721     }
3722 
3723   return NULL;
3724 }
3725 
3726 static const char *
get_parisc_segment_type(unsigned long type)3727 get_parisc_segment_type (unsigned long type)
3728 {
3729   switch (type)
3730     {
3731     case PT_HP_TLS:		return "HP_TLS";
3732     case PT_HP_CORE_NONE:	return "HP_CORE_NONE";
3733     case PT_HP_CORE_VERSION:	return "HP_CORE_VERSION";
3734     case PT_HP_CORE_KERNEL:	return "HP_CORE_KERNEL";
3735     case PT_HP_CORE_COMM:	return "HP_CORE_COMM";
3736     case PT_HP_CORE_PROC:	return "HP_CORE_PROC";
3737     case PT_HP_CORE_LOADABLE:	return "HP_CORE_LOADABLE";
3738     case PT_HP_CORE_STACK:	return "HP_CORE_STACK";
3739     case PT_HP_CORE_SHM:	return "HP_CORE_SHM";
3740     case PT_HP_CORE_MMF:	return "HP_CORE_MMF";
3741     case PT_HP_PARALLEL:	return "HP_PARALLEL";
3742     case PT_HP_FASTBIND:	return "HP_FASTBIND";
3743     case PT_HP_OPT_ANNOT:	return "HP_OPT_ANNOT";
3744     case PT_HP_HSL_ANNOT:	return "HP_HSL_ANNOT";
3745     case PT_HP_STACK:		return "HP_STACK";
3746     case PT_HP_CORE_UTSNAME:	return "HP_CORE_UTSNAME";
3747     case PT_PARISC_ARCHEXT:	return "PARISC_ARCHEXT";
3748     case PT_PARISC_UNWIND:	return "PARISC_UNWIND";
3749     case PT_PARISC_WEAKORDER:	return "PARISC_WEAKORDER";
3750     default:
3751       break;
3752     }
3753 
3754   return NULL;
3755 }
3756 
3757 static const char *
get_ia64_segment_type(unsigned long type)3758 get_ia64_segment_type (unsigned long type)
3759 {
3760   switch (type)
3761     {
3762     case PT_IA_64_ARCHEXT:	return "IA_64_ARCHEXT";
3763     case PT_IA_64_UNWIND:	return "IA_64_UNWIND";
3764     case PT_HP_TLS:		return "HP_TLS";
3765     case PT_IA_64_HP_OPT_ANOT:	return "HP_OPT_ANNOT";
3766     case PT_IA_64_HP_HSL_ANOT:	return "HP_HSL_ANNOT";
3767     case PT_IA_64_HP_STACK:	return "HP_STACK";
3768     default:
3769       break;
3770     }
3771 
3772   return NULL;
3773 }
3774 
3775 static const char *
get_tic6x_segment_type(unsigned long type)3776 get_tic6x_segment_type (unsigned long type)
3777 {
3778   switch (type)
3779     {
3780     case PT_C6000_PHATTR:	return "C6000_PHATTR";
3781     default:
3782       break;
3783     }
3784 
3785   return NULL;
3786 }
3787 
3788 static const char *
get_solaris_segment_type(unsigned long type)3789 get_solaris_segment_type (unsigned long type)
3790 {
3791   switch (type)
3792     {
3793     case 0x6464e550: return "PT_SUNW_UNWIND";
3794     case 0x6474e550: return "PT_SUNW_EH_FRAME";
3795     case 0x6ffffff7: return "PT_LOSUNW";
3796     case 0x6ffffffa: return "PT_SUNWBSS";
3797     case 0x6ffffffb: return "PT_SUNWSTACK";
3798     case 0x6ffffffc: return "PT_SUNWDTRACE";
3799     case 0x6ffffffd: return "PT_SUNWCAP";
3800     case 0x6fffffff: return "PT_HISUNW";
3801     default: return NULL;
3802     }
3803 }
3804 
3805 static const char *
get_segment_type(unsigned long p_type)3806 get_segment_type (unsigned long p_type)
3807 {
3808   static char buff[32];
3809 
3810   switch (p_type)
3811     {
3812     case PT_NULL:	return "NULL";
3813     case PT_LOAD:	return "LOAD";
3814     case PT_DYNAMIC:	return "DYNAMIC";
3815     case PT_INTERP:	return "INTERP";
3816     case PT_NOTE:	return "NOTE";
3817     case PT_SHLIB:	return "SHLIB";
3818     case PT_PHDR:	return "PHDR";
3819     case PT_TLS:	return "TLS";
3820 
3821     case PT_GNU_EH_FRAME:
3822 			return "GNU_EH_FRAME";
3823     case PT_GNU_STACK:	return "GNU_STACK";
3824     case PT_GNU_RELRO:  return "GNU_RELRO";
3825 
3826     default:
3827       if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
3828 	{
3829 	  const char * result;
3830 
3831 	  switch (elf_header.e_machine)
3832 	    {
3833 	    case EM_AARCH64:
3834 	      result = get_aarch64_segment_type (p_type);
3835 	      break;
3836 	    case EM_ARM:
3837 	      result = get_arm_segment_type (p_type);
3838 	      break;
3839 	    case EM_MIPS:
3840 	    case EM_MIPS_RS3_LE:
3841 	      result = get_mips_segment_type (p_type);
3842 	      break;
3843 	    case EM_PARISC:
3844 	      result = get_parisc_segment_type (p_type);
3845 	      break;
3846 	    case EM_IA_64:
3847 	      result = get_ia64_segment_type (p_type);
3848 	      break;
3849 	    case EM_TI_C6000:
3850 	      result = get_tic6x_segment_type (p_type);
3851 	      break;
3852 	    default:
3853 	      result = NULL;
3854 	      break;
3855 	    }
3856 
3857 	  if (result != NULL)
3858 	    return result;
3859 
3860 	  sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
3861 	}
3862       else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
3863 	{
3864 	  const char * result;
3865 
3866 	  switch (elf_header.e_machine)
3867 	    {
3868 	    case EM_PARISC:
3869 	      result = get_parisc_segment_type (p_type);
3870 	      break;
3871 	    case EM_IA_64:
3872 	      result = get_ia64_segment_type (p_type);
3873 	      break;
3874 	    default:
3875 	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
3876 		result = get_solaris_segment_type (p_type);
3877 	      else
3878 		result = NULL;
3879 	      break;
3880 	    }
3881 
3882 	  if (result != NULL)
3883 	    return result;
3884 
3885 	  sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
3886 	}
3887       else
3888 	snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
3889 
3890       return buff;
3891     }
3892 }
3893 
3894 static const char *
get_mips_section_type_name(unsigned int sh_type)3895 get_mips_section_type_name (unsigned int sh_type)
3896 {
3897   switch (sh_type)
3898     {
3899     case SHT_MIPS_LIBLIST:	 return "MIPS_LIBLIST";
3900     case SHT_MIPS_MSYM:		 return "MIPS_MSYM";
3901     case SHT_MIPS_CONFLICT:	 return "MIPS_CONFLICT";
3902     case SHT_MIPS_GPTAB:	 return "MIPS_GPTAB";
3903     case SHT_MIPS_UCODE:	 return "MIPS_UCODE";
3904     case SHT_MIPS_DEBUG:	 return "MIPS_DEBUG";
3905     case SHT_MIPS_REGINFO:	 return "MIPS_REGINFO";
3906     case SHT_MIPS_PACKAGE:	 return "MIPS_PACKAGE";
3907     case SHT_MIPS_PACKSYM:	 return "MIPS_PACKSYM";
3908     case SHT_MIPS_RELD:		 return "MIPS_RELD";
3909     case SHT_MIPS_IFACE:	 return "MIPS_IFACE";
3910     case SHT_MIPS_CONTENT:	 return "MIPS_CONTENT";
3911     case SHT_MIPS_OPTIONS:	 return "MIPS_OPTIONS";
3912     case SHT_MIPS_SHDR:		 return "MIPS_SHDR";
3913     case SHT_MIPS_FDESC:	 return "MIPS_FDESC";
3914     case SHT_MIPS_EXTSYM:	 return "MIPS_EXTSYM";
3915     case SHT_MIPS_DENSE:	 return "MIPS_DENSE";
3916     case SHT_MIPS_PDESC:	 return "MIPS_PDESC";
3917     case SHT_MIPS_LOCSYM:	 return "MIPS_LOCSYM";
3918     case SHT_MIPS_AUXSYM:	 return "MIPS_AUXSYM";
3919     case SHT_MIPS_OPTSYM:	 return "MIPS_OPTSYM";
3920     case SHT_MIPS_LOCSTR:	 return "MIPS_LOCSTR";
3921     case SHT_MIPS_LINE:		 return "MIPS_LINE";
3922     case SHT_MIPS_RFDESC:	 return "MIPS_RFDESC";
3923     case SHT_MIPS_DELTASYM:	 return "MIPS_DELTASYM";
3924     case SHT_MIPS_DELTAINST:	 return "MIPS_DELTAINST";
3925     case SHT_MIPS_DELTACLASS:	 return "MIPS_DELTACLASS";
3926     case SHT_MIPS_DWARF:	 return "MIPS_DWARF";
3927     case SHT_MIPS_DELTADECL:	 return "MIPS_DELTADECL";
3928     case SHT_MIPS_SYMBOL_LIB:	 return "MIPS_SYMBOL_LIB";
3929     case SHT_MIPS_EVENTS:	 return "MIPS_EVENTS";
3930     case SHT_MIPS_TRANSLATE:	 return "MIPS_TRANSLATE";
3931     case SHT_MIPS_PIXIE:	 return "MIPS_PIXIE";
3932     case SHT_MIPS_XLATE:	 return "MIPS_XLATE";
3933     case SHT_MIPS_XLATE_DEBUG:	 return "MIPS_XLATE_DEBUG";
3934     case SHT_MIPS_WHIRL:	 return "MIPS_WHIRL";
3935     case SHT_MIPS_EH_REGION:	 return "MIPS_EH_REGION";
3936     case SHT_MIPS_XLATE_OLD:	 return "MIPS_XLATE_OLD";
3937     case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
3938     case SHT_MIPS_ABIFLAGS:	 return "MIPS_ABIFLAGS";
3939     default:
3940       break;
3941     }
3942   return NULL;
3943 }
3944 
3945 static const char *
get_parisc_section_type_name(unsigned int sh_type)3946 get_parisc_section_type_name (unsigned int sh_type)
3947 {
3948   switch (sh_type)
3949     {
3950     case SHT_PARISC_EXT:	return "PARISC_EXT";
3951     case SHT_PARISC_UNWIND:	return "PARISC_UNWIND";
3952     case SHT_PARISC_DOC:	return "PARISC_DOC";
3953     case SHT_PARISC_ANNOT:	return "PARISC_ANNOT";
3954     case SHT_PARISC_SYMEXTN:	return "PARISC_SYMEXTN";
3955     case SHT_PARISC_STUBS:	return "PARISC_STUBS";
3956     case SHT_PARISC_DLKM:	return "PARISC_DLKM";
3957     default:
3958       break;
3959     }
3960   return NULL;
3961 }
3962 
3963 static const char *
get_ia64_section_type_name(unsigned int sh_type)3964 get_ia64_section_type_name (unsigned int sh_type)
3965 {
3966   /* If the top 8 bits are 0x78 the next 8 are the os/abi ID.  */
3967   if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
3968     return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
3969 
3970   switch (sh_type)
3971     {
3972     case SHT_IA_64_EXT:		       return "IA_64_EXT";
3973     case SHT_IA_64_UNWIND:	       return "IA_64_UNWIND";
3974     case SHT_IA_64_PRIORITY_INIT:      return "IA_64_PRIORITY_INIT";
3975     case SHT_IA_64_VMS_TRACE:          return "VMS_TRACE";
3976     case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
3977     case SHT_IA_64_VMS_DEBUG:          return "VMS_DEBUG";
3978     case SHT_IA_64_VMS_DEBUG_STR:      return "VMS_DEBUG_STR";
3979     case SHT_IA_64_VMS_LINKAGES:       return "VMS_LINKAGES";
3980     case SHT_IA_64_VMS_SYMBOL_VECTOR:  return "VMS_SYMBOL_VECTOR";
3981     case SHT_IA_64_VMS_FIXUP:          return "VMS_FIXUP";
3982     default:
3983       break;
3984     }
3985   return NULL;
3986 }
3987 
3988 static const char *
get_x86_64_section_type_name(unsigned int sh_type)3989 get_x86_64_section_type_name (unsigned int sh_type)
3990 {
3991   switch (sh_type)
3992     {
3993     case SHT_X86_64_UNWIND:	return "X86_64_UNWIND";
3994     default:
3995       break;
3996     }
3997   return NULL;
3998 }
3999 
4000 static const char *
get_aarch64_section_type_name(unsigned int sh_type)4001 get_aarch64_section_type_name (unsigned int sh_type)
4002 {
4003   switch (sh_type)
4004     {
4005     case SHT_AARCH64_ATTRIBUTES:
4006       return "AARCH64_ATTRIBUTES";
4007     default:
4008       break;
4009     }
4010   return NULL;
4011 }
4012 
4013 static const char *
get_arm_section_type_name(unsigned int sh_type)4014 get_arm_section_type_name (unsigned int sh_type)
4015 {
4016   switch (sh_type)
4017     {
4018     case SHT_ARM_EXIDX:           return "ARM_EXIDX";
4019     case SHT_ARM_PREEMPTMAP:      return "ARM_PREEMPTMAP";
4020     case SHT_ARM_ATTRIBUTES:      return "ARM_ATTRIBUTES";
4021     case SHT_ARM_DEBUGOVERLAY:    return "ARM_DEBUGOVERLAY";
4022     case SHT_ARM_OVERLAYSECTION:  return "ARM_OVERLAYSECTION";
4023     default:
4024       break;
4025     }
4026   return NULL;
4027 }
4028 
4029 static const char *
get_tic6x_section_type_name(unsigned int sh_type)4030 get_tic6x_section_type_name (unsigned int sh_type)
4031 {
4032   switch (sh_type)
4033     {
4034     case SHT_C6000_UNWIND:
4035       return "C6000_UNWIND";
4036     case SHT_C6000_PREEMPTMAP:
4037       return "C6000_PREEMPTMAP";
4038     case SHT_C6000_ATTRIBUTES:
4039       return "C6000_ATTRIBUTES";
4040     case SHT_TI_ICODE:
4041       return "TI_ICODE";
4042     case SHT_TI_XREF:
4043       return "TI_XREF";
4044     case SHT_TI_HANDLER:
4045       return "TI_HANDLER";
4046     case SHT_TI_INITINFO:
4047       return "TI_INITINFO";
4048     case SHT_TI_PHATTRS:
4049       return "TI_PHATTRS";
4050     default:
4051       break;
4052     }
4053   return NULL;
4054 }
4055 
4056 static const char *
get_msp430x_section_type_name(unsigned int sh_type)4057 get_msp430x_section_type_name (unsigned int sh_type)
4058 {
4059   switch (sh_type)
4060     {
4061     case SHT_MSP430_SEC_FLAGS:   return "MSP430_SEC_FLAGS";
4062     case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4063     case SHT_MSP430_ATTRIBUTES:  return "MSP430_ATTRIBUTES";
4064     default: return NULL;
4065     }
4066 }
4067 
4068 static const char *
get_v850_section_type_name(unsigned int sh_type)4069 get_v850_section_type_name (unsigned int sh_type)
4070 {
4071   switch (sh_type)
4072     {
4073     case SHT_V850_SCOMMON: return "V850 Small Common";
4074     case SHT_V850_TCOMMON: return "V850 Tiny Common";
4075     case SHT_V850_ZCOMMON: return "V850 Zero Common";
4076     case SHT_RENESAS_IOP:  return "RENESAS IOP";
4077     case SHT_RENESAS_INFO: return "RENESAS INFO";
4078     default: return NULL;
4079     }
4080 }
4081 
4082 static const char *
get_section_type_name(unsigned int sh_type)4083 get_section_type_name (unsigned int sh_type)
4084 {
4085   static char buff[32];
4086   const char * result;
4087 
4088   switch (sh_type)
4089     {
4090     case SHT_NULL:		return "NULL";
4091     case SHT_PROGBITS:		return "PROGBITS";
4092     case SHT_SYMTAB:		return "SYMTAB";
4093     case SHT_STRTAB:		return "STRTAB";
4094     case SHT_RELA:		return "RELA";
4095     case SHT_RELR:		return "RELR";
4096     case SHT_HASH:		return "HASH";
4097     case SHT_DYNAMIC:		return "DYNAMIC";
4098     case SHT_NOTE:		return "NOTE";
4099     case SHT_NOBITS:		return "NOBITS";
4100     case SHT_REL:		return "REL";
4101     case SHT_SHLIB:		return "SHLIB";
4102     case SHT_DYNSYM:		return "DYNSYM";
4103     case SHT_INIT_ARRAY:	return "INIT_ARRAY";
4104     case SHT_FINI_ARRAY:	return "FINI_ARRAY";
4105     case SHT_PREINIT_ARRAY:	return "PREINIT_ARRAY";
4106     case SHT_GNU_HASH:		return "GNU_HASH";
4107     case SHT_GROUP:		return "GROUP";
4108     case SHT_SYMTAB_SHNDX:	return "SYMTAB SECTION INDICIES";
4109     case SHT_GNU_verdef:	return "VERDEF";
4110     case SHT_GNU_verneed:	return "VERNEED";
4111     case SHT_GNU_versym:	return "VERSYM";
4112     case 0x6ffffff0:		return "VERSYM";
4113     case 0x6ffffffc:		return "VERDEF";
4114     case 0x7ffffffd:		return "AUXILIARY";
4115     case 0x7fffffff:		return "FILTER";
4116     case SHT_GNU_LIBLIST:	return "GNU_LIBLIST";
4117 
4118     default:
4119       if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4120 	{
4121 	  switch (elf_header.e_machine)
4122 	    {
4123 	    case EM_MIPS:
4124 	    case EM_MIPS_RS3_LE:
4125 	      result = get_mips_section_type_name (sh_type);
4126 	      break;
4127 	    case EM_PARISC:
4128 	      result = get_parisc_section_type_name (sh_type);
4129 	      break;
4130 	    case EM_IA_64:
4131 	      result = get_ia64_section_type_name (sh_type);
4132 	      break;
4133 	    case EM_X86_64:
4134 	    case EM_L1OM:
4135 	    case EM_K1OM:
4136 	      result = get_x86_64_section_type_name (sh_type);
4137 	      break;
4138 	    case EM_AARCH64:
4139 	      result = get_aarch64_section_type_name (sh_type);
4140 	      break;
4141 	    case EM_ARM:
4142 	      result = get_arm_section_type_name (sh_type);
4143 	      break;
4144 	    case EM_TI_C6000:
4145 	      result = get_tic6x_section_type_name (sh_type);
4146 	      break;
4147 	    case EM_MSP430:
4148 	      result = get_msp430x_section_type_name (sh_type);
4149 	      break;
4150 	    case EM_V800:
4151 	    case EM_V850:
4152 	    case EM_CYGNUS_V850:
4153 	      result = get_v850_section_type_name (sh_type);
4154 	      break;
4155 	    default:
4156 	      result = NULL;
4157 	      break;
4158 	    }
4159 
4160 	  if (result != NULL)
4161 	    return result;
4162 
4163 	  sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4164 	}
4165       else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4166 	{
4167 	  switch (elf_header.e_machine)
4168 	    {
4169 	    case EM_IA_64:
4170 	      result = get_ia64_section_type_name (sh_type);
4171 	      break;
4172 	    default:
4173 	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4174 		result = get_solaris_section_type (sh_type);
4175 	      else
4176 		result = NULL;
4177 	      break;
4178 	    }
4179 
4180 	  if (result != NULL)
4181 	    return result;
4182 
4183 	  sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4184 	}
4185       else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4186 	{
4187 	  switch (elf_header.e_machine)
4188 	    {
4189 	    case EM_V800:
4190 	    case EM_V850:
4191 	    case EM_CYGNUS_V850:
4192 	      result = get_v850_section_type_name (sh_type);
4193 	      break;
4194 	    default:
4195 	      result = NULL;
4196 	      break;
4197 	    }
4198 
4199 	  if (result != NULL)
4200 	    return result;
4201 
4202 	  sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4203 	}
4204       else
4205 	/* This message is probably going to be displayed in a 15
4206 	   character wide field, so put the hex value first.  */
4207 	snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4208 
4209       return buff;
4210     }
4211 }
4212 
4213 #define OPTION_DEBUG_DUMP	512
4214 #define OPTION_DYN_SYMS		513
4215 #define OPTION_DWARF_DEPTH	514
4216 #define OPTION_DWARF_START	515
4217 #define OPTION_DWARF_CHECK	516
4218 
4219 static struct option options[] =
4220 {
4221   {"all",	       no_argument, 0, 'a'},
4222   {"file-header",      no_argument, 0, 'h'},
4223   {"program-headers",  no_argument, 0, 'l'},
4224   {"headers",	       no_argument, 0, 'e'},
4225   {"histogram",	       no_argument, 0, 'I'},
4226   {"segments",	       no_argument, 0, 'l'},
4227   {"sections",	       no_argument, 0, 'S'},
4228   {"section-headers",  no_argument, 0, 'S'},
4229   {"section-groups",   no_argument, 0, 'g'},
4230   {"section-details",  no_argument, 0, 't'},
4231   {"full-section-name",no_argument, 0, 'N'},
4232   {"symbols",	       no_argument, 0, 's'},
4233   {"syms",	       no_argument, 0, 's'},
4234   {"dyn-syms",	       no_argument, 0, OPTION_DYN_SYMS},
4235   {"relocs",	       no_argument, 0, 'r'},
4236   {"notes",	       no_argument, 0, 'n'},
4237   {"dynamic",	       no_argument, 0, 'd'},
4238   {"arch-specific",    no_argument, 0, 'A'},
4239   {"version-info",     no_argument, 0, 'V'},
4240   {"use-dynamic",      no_argument, 0, 'D'},
4241   {"unwind",	       no_argument, 0, 'u'},
4242   {"archive-index",    no_argument, 0, 'c'},
4243   {"hex-dump",	       required_argument, 0, 'x'},
4244   {"relocated-dump",   required_argument, 0, 'R'},
4245   {"string-dump",      required_argument, 0, 'p'},
4246   {"decompress",       no_argument, 0, 'z'},
4247 #ifdef SUPPORT_DISASSEMBLY
4248   {"instruction-dump", required_argument, 0, 'i'},
4249 #endif
4250   {"debug-dump",       optional_argument, 0, OPTION_DEBUG_DUMP},
4251 
4252   {"dwarf-depth",      required_argument, 0, OPTION_DWARF_DEPTH},
4253   {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
4254   {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
4255 
4256   {"version",	       no_argument, 0, 'v'},
4257   {"wide",	       no_argument, 0, 'W'},
4258   {"help",	       no_argument, 0, 'H'},
4259   {0,		       no_argument, 0, 0}
4260 };
4261 
4262 static void
usage(FILE * stream)4263 usage (FILE * stream)
4264 {
4265   fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4266   fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4267   fprintf (stream, _(" Options are:\n\
4268   -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n\
4269   -h --file-header       Display the ELF file header\n\
4270   -l --program-headers   Display the program headers\n\
4271      --segments          An alias for --program-headers\n\
4272   -S --section-headers   Display the sections' header\n\
4273      --sections          An alias for --section-headers\n\
4274   -g --section-groups    Display the section groups\n\
4275   -t --section-details   Display the section details\n\
4276   -e --headers           Equivalent to: -h -l -S\n\
4277   -s --syms              Display the symbol table\n\
4278      --symbols           An alias for --syms\n\
4279   --dyn-syms             Display the dynamic symbol table\n\
4280   -n --notes             Display the core notes (if present)\n\
4281   -r --relocs            Display the relocations (if present)\n\
4282   -u --unwind            Display the unwind info (if present)\n\
4283   -d --dynamic           Display the dynamic section (if present)\n\
4284   -V --version-info      Display the version sections (if present)\n\
4285   -A --arch-specific     Display architecture specific information (if any)\n\
4286   -c --archive-index     Display the symbol/file index in an archive\n\
4287   -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
4288   -x --hex-dump=<number|name>\n\
4289                          Dump the contents of section <number|name> as bytes\n\
4290   -p --string-dump=<number|name>\n\
4291                          Dump the contents of section <number|name> as strings\n\
4292   -R --relocated-dump=<number|name>\n\
4293                          Dump the contents of section <number|name> as relocated bytes\n\
4294   -z --decompress        Decompress section before dumping it\n\
4295   -w[lLiaprmfFsoRt] or\n\
4296   --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
4297                =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
4298                =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
4299                =addr,=cu_index]\n\
4300                          Display the contents of DWARF2 debug sections\n"));
4301   fprintf (stream, _("\
4302   --dwarf-depth=N        Do not display DIEs at depth N or greater\n\
4303   --dwarf-start=N        Display DIEs starting with N, at the same depth\n\
4304                          or deeper\n"));
4305 #ifdef SUPPORT_DISASSEMBLY
4306   fprintf (stream, _("\
4307   -i --instruction-dump=<number|name>\n\
4308                          Disassemble the contents of section <number|name>\n"));
4309 #endif
4310   fprintf (stream, _("\
4311   -I --histogram         Display histogram of bucket list lengths\n\
4312   -W --wide              Allow output width to exceed 80 characters\n\
4313   @<file>                Read options from <file>\n\
4314   -H --help              Display this information\n\
4315   -v --version           Display the version number of readelf\n"));
4316 
4317   if (REPORT_BUGS_TO[0] && stream == stdout)
4318     fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4319 
4320   exit (stream == stdout ? 0 : 1);
4321 }
4322 
4323 /* Record the fact that the user wants the contents of section number
4324    SECTION to be displayed using the method(s) encoded as flags bits
4325    in TYPE.  Note, TYPE can be zero if we are creating the array for
4326    the first time.  */
4327 
4328 static void
request_dump_bynumber(unsigned int section,dump_type type)4329 request_dump_bynumber (unsigned int section, dump_type type)
4330 {
4331   if (section >= num_dump_sects)
4332     {
4333       dump_type * new_dump_sects;
4334 
4335       new_dump_sects = (dump_type *) calloc (section + 1,
4336                                              sizeof (* dump_sects));
4337 
4338       if (new_dump_sects == NULL)
4339 	error (_("Out of memory allocating dump request table.\n"));
4340       else
4341 	{
4342 	  /* Copy current flag settings.  */
4343 	  memcpy (new_dump_sects, dump_sects, num_dump_sects * sizeof (* dump_sects));
4344 
4345 	  free (dump_sects);
4346 
4347 	  dump_sects = new_dump_sects;
4348 	  num_dump_sects = section + 1;
4349 	}
4350     }
4351 
4352   if (dump_sects)
4353     dump_sects[section] |= type;
4354 
4355   return;
4356 }
4357 
4358 /* Request a dump by section name.  */
4359 
4360 static void
request_dump_byname(const char * section,dump_type type)4361 request_dump_byname (const char * section, dump_type type)
4362 {
4363   struct dump_list_entry * new_request;
4364 
4365   new_request = (struct dump_list_entry *)
4366       malloc (sizeof (struct dump_list_entry));
4367   if (!new_request)
4368     error (_("Out of memory allocating dump request table.\n"));
4369 
4370   new_request->name = strdup (section);
4371   if (!new_request->name)
4372     error (_("Out of memory allocating dump request table.\n"));
4373 
4374   new_request->type = type;
4375 
4376   new_request->next = dump_sects_byname;
4377   dump_sects_byname = new_request;
4378 }
4379 
4380 static inline void
request_dump(dump_type type)4381 request_dump (dump_type type)
4382 {
4383   int section;
4384   char * cp;
4385 
4386   do_dump++;
4387   section = strtoul (optarg, & cp, 0);
4388 
4389   if (! *cp && section >= 0)
4390     request_dump_bynumber (section, type);
4391   else
4392     request_dump_byname (optarg, type);
4393 }
4394 
4395 
4396 static void
parse_args(int argc,char ** argv)4397 parse_args (int argc, char ** argv)
4398 {
4399   int c;
4400 
4401   if (argc < 2)
4402     usage (stderr);
4403 
4404   while ((c = getopt_long
4405 	  (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4406     {
4407       switch (c)
4408 	{
4409 	case 0:
4410 	  /* Long options.  */
4411 	  break;
4412 	case 'H':
4413 	  usage (stdout);
4414 	  break;
4415 
4416 	case 'a':
4417 	  do_syms++;
4418 	  do_reloc++;
4419 	  do_unwind++;
4420 	  do_dynamic++;
4421 	  do_header++;
4422 	  do_sections++;
4423 	  do_section_groups++;
4424 	  do_segments++;
4425 	  do_version++;
4426 	  do_histogram++;
4427 	  do_arch++;
4428 	  do_notes++;
4429 	  break;
4430 	case 'g':
4431 	  do_section_groups++;
4432 	  break;
4433 	case 't':
4434 	case 'N':
4435 	  do_sections++;
4436 	  do_section_details++;
4437 	  break;
4438 	case 'e':
4439 	  do_header++;
4440 	  do_sections++;
4441 	  do_segments++;
4442 	  break;
4443 	case 'A':
4444 	  do_arch++;
4445 	  break;
4446 	case 'D':
4447 	  do_using_dynamic++;
4448 	  break;
4449 	case 'r':
4450 	  do_reloc++;
4451 	  break;
4452 	case 'u':
4453 	  do_unwind++;
4454 	  break;
4455 	case 'h':
4456 	  do_header++;
4457 	  break;
4458 	case 'l':
4459 	  do_segments++;
4460 	  break;
4461 	case 's':
4462 	  do_syms++;
4463 	  break;
4464 	case 'S':
4465 	  do_sections++;
4466 	  break;
4467 	case 'd':
4468 	  do_dynamic++;
4469 	  break;
4470 	case 'I':
4471 	  do_histogram++;
4472 	  break;
4473 	case 'n':
4474 	  do_notes++;
4475 	  break;
4476 	case 'c':
4477 	  do_archive_index++;
4478 	  break;
4479 	case 'x':
4480 	  request_dump (HEX_DUMP);
4481 	  break;
4482 	case 'p':
4483 	  request_dump (STRING_DUMP);
4484 	  break;
4485 	case 'R':
4486 	  request_dump (RELOC_DUMP);
4487 	  break;
4488 	case 'z':
4489 	  decompress_dumps++;
4490 	  break;
4491 	case 'w':
4492 	  do_dump++;
4493 	  if (optarg == 0)
4494 	    {
4495 	      do_debugging = 1;
4496 	      dwarf_select_sections_all ();
4497 	    }
4498 	  else
4499 	    {
4500 	      do_debugging = 0;
4501 	      dwarf_select_sections_by_letters (optarg);
4502 	    }
4503 	  break;
4504 	case OPTION_DEBUG_DUMP:
4505 	  do_dump++;
4506 	  if (optarg == 0)
4507 	    do_debugging = 1;
4508 	  else
4509 	    {
4510 	      do_debugging = 0;
4511 	      dwarf_select_sections_by_names (optarg);
4512 	    }
4513 	  break;
4514 	case OPTION_DWARF_DEPTH:
4515 	  {
4516 	    char *cp;
4517 
4518 	    dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4519 	  }
4520 	  break;
4521 	case OPTION_DWARF_START:
4522 	  {
4523 	    char *cp;
4524 
4525 	    dwarf_start_die = strtoul (optarg, & cp, 0);
4526 	  }
4527 	  break;
4528 	case OPTION_DWARF_CHECK:
4529 	  dwarf_check = 1;
4530 	  break;
4531 	case OPTION_DYN_SYMS:
4532 	  do_dyn_syms++;
4533 	  break;
4534 #ifdef SUPPORT_DISASSEMBLY
4535 	case 'i':
4536 	  request_dump (DISASS_DUMP);
4537 	  break;
4538 #endif
4539 	case 'v':
4540 	  print_version (program_name);
4541 	  break;
4542 	case 'V':
4543 	  do_version++;
4544 	  break;
4545 	case 'W':
4546 	  do_wide++;
4547 	  break;
4548 	default:
4549 	  /* xgettext:c-format */
4550 	  error (_("Invalid option '-%c'\n"), c);
4551 	  /* Drop through.  */
4552 	case '?':
4553 	  usage (stderr);
4554 	}
4555     }
4556 
4557   if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
4558       && !do_segments && !do_header && !do_dump && !do_version
4559       && !do_histogram && !do_debugging && !do_arch && !do_notes
4560       && !do_section_groups && !do_archive_index
4561       && !do_dyn_syms)
4562     usage (stderr);
4563 }
4564 
4565 static const char *
get_elf_class(unsigned int elf_class)4566 get_elf_class (unsigned int elf_class)
4567 {
4568   static char buff[32];
4569 
4570   switch (elf_class)
4571     {
4572     case ELFCLASSNONE: return _("none");
4573     case ELFCLASS32:   return "ELF32";
4574     case ELFCLASS64:   return "ELF64";
4575     default:
4576       snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
4577       return buff;
4578     }
4579 }
4580 
4581 static const char *
get_data_encoding(unsigned int encoding)4582 get_data_encoding (unsigned int encoding)
4583 {
4584   static char buff[32];
4585 
4586   switch (encoding)
4587     {
4588     case ELFDATANONE: return _("none");
4589     case ELFDATA2LSB: return _("2's complement, little endian");
4590     case ELFDATA2MSB: return _("2's complement, big endian");
4591     default:
4592       snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
4593       return buff;
4594     }
4595 }
4596 
4597 /* Decode the data held in 'elf_header'.  */
4598 
4599 static int
process_file_header(void)4600 process_file_header (void)
4601 {
4602   if (   elf_header.e_ident[EI_MAG0] != ELFMAG0
4603       || elf_header.e_ident[EI_MAG1] != ELFMAG1
4604       || elf_header.e_ident[EI_MAG2] != ELFMAG2
4605       || elf_header.e_ident[EI_MAG3] != ELFMAG3)
4606     {
4607       error
4608 	(_("Not an ELF file - it has the wrong magic bytes at the start\n"));
4609       return 0;
4610     }
4611 
4612   init_dwarf_regnames (elf_header.e_machine);
4613 
4614   if (do_header)
4615     {
4616       int i;
4617 
4618       printf (_("ELF Header:\n"));
4619       printf (_("  Magic:   "));
4620       for (i = 0; i < EI_NIDENT; i++)
4621 	printf ("%2.2x ", elf_header.e_ident[i]);
4622       printf ("\n");
4623       printf (_("  Class:                             %s\n"),
4624 	      get_elf_class (elf_header.e_ident[EI_CLASS]));
4625       printf (_("  Data:                              %s\n"),
4626 	      get_data_encoding (elf_header.e_ident[EI_DATA]));
4627       printf (_("  Version:                           %d %s\n"),
4628 	      elf_header.e_ident[EI_VERSION],
4629 	      (elf_header.e_ident[EI_VERSION] == EV_CURRENT
4630 	       ? "(current)"
4631 	       : (elf_header.e_ident[EI_VERSION] != EV_NONE
4632 		  ? _("<unknown: %lx>")
4633 		  : "")));
4634       printf (_("  OS/ABI:                            %s\n"),
4635 	      get_osabi_name (elf_header.e_ident[EI_OSABI]));
4636       printf (_("  ABI Version:                       %d\n"),
4637 	      elf_header.e_ident[EI_ABIVERSION]);
4638       printf (_("  Type:                              %s\n"),
4639 	      get_file_type (elf_header.e_type));
4640       printf (_("  Machine:                           %s\n"),
4641 	      get_machine_name (elf_header.e_machine));
4642       printf (_("  Version:                           0x%lx\n"),
4643 	      (unsigned long) elf_header.e_version);
4644 
4645       printf (_("  Entry point address:               "));
4646       print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
4647       printf (_("\n  Start of program headers:          "));
4648       print_vma ((bfd_vma) elf_header.e_phoff, DEC);
4649       printf (_(" (bytes into file)\n  Start of section headers:          "));
4650       print_vma ((bfd_vma) elf_header.e_shoff, DEC);
4651       printf (_(" (bytes into file)\n"));
4652 
4653       printf (_("  Flags:                             0x%lx%s\n"),
4654 	      (unsigned long) elf_header.e_flags,
4655 	      get_machine_flags (elf_header.e_flags, elf_header.e_machine));
4656       printf (_("  Size of this header:               %ld (bytes)\n"),
4657 	      (long) elf_header.e_ehsize);
4658       printf (_("  Size of program headers:           %ld (bytes)\n"),
4659 	      (long) elf_header.e_phentsize);
4660       printf (_("  Number of program headers:         %ld"),
4661 	      (long) elf_header.e_phnum);
4662       if (section_headers != NULL
4663 	  && elf_header.e_phnum == PN_XNUM
4664 	  && section_headers[0].sh_info != 0)
4665 	printf (" (%ld)", (long) section_headers[0].sh_info);
4666       putc ('\n', stdout);
4667       printf (_("  Size of section headers:           %ld (bytes)\n"),
4668 	      (long) elf_header.e_shentsize);
4669       printf (_("  Number of section headers:         %ld"),
4670 	      (long) elf_header.e_shnum);
4671       if (section_headers != NULL && elf_header.e_shnum == SHN_UNDEF)
4672 	printf (" (%ld)", (long) section_headers[0].sh_size);
4673       putc ('\n', stdout);
4674       printf (_("  Section header string table index: %ld"),
4675 	      (long) elf_header.e_shstrndx);
4676       if (section_headers != NULL
4677 	  && elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
4678 	printf (" (%u)", section_headers[0].sh_link);
4679       else if (elf_header.e_shstrndx != SHN_UNDEF
4680 	       && elf_header.e_shstrndx >= elf_header.e_shnum)
4681 	printf (_(" <corrupt: out of range>"));
4682       putc ('\n', stdout);
4683     }
4684 
4685   if (section_headers != NULL)
4686     {
4687       if (elf_header.e_phnum == PN_XNUM
4688 	  && section_headers[0].sh_info != 0)
4689 	elf_header.e_phnum = section_headers[0].sh_info;
4690       if (elf_header.e_shnum == SHN_UNDEF)
4691 	elf_header.e_shnum = section_headers[0].sh_size;
4692       if (elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
4693 	elf_header.e_shstrndx = section_headers[0].sh_link;
4694       else if (elf_header.e_shstrndx >= elf_header.e_shnum)
4695 	elf_header.e_shstrndx = SHN_UNDEF;
4696       free (section_headers);
4697       section_headers = NULL;
4698     }
4699 
4700   return 1;
4701 }
4702 
4703 static bfd_boolean
get_32bit_program_headers(FILE * file,Elf_Internal_Phdr * pheaders)4704 get_32bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
4705 {
4706   Elf32_External_Phdr * phdrs;
4707   Elf32_External_Phdr * external;
4708   Elf_Internal_Phdr *   internal;
4709   unsigned int i;
4710   unsigned int size = elf_header.e_phentsize;
4711   unsigned int num  = elf_header.e_phnum;
4712 
4713   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4714   if (size == 0 || num == 0)
4715     return FALSE;
4716   if (size < sizeof * phdrs)
4717     {
4718       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4719       return FALSE;
4720     }
4721   if (size > sizeof * phdrs)
4722     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4723 
4724   phdrs = (Elf32_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
4725                                             size, num, _("program headers"));
4726   if (phdrs == NULL)
4727     return FALSE;
4728 
4729   for (i = 0, internal = pheaders, external = phdrs;
4730        i < elf_header.e_phnum;
4731        i++, internal++, external++)
4732     {
4733       internal->p_type   = BYTE_GET (external->p_type);
4734       internal->p_offset = BYTE_GET (external->p_offset);
4735       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4736       internal->p_paddr  = BYTE_GET (external->p_paddr);
4737       internal->p_filesz = BYTE_GET (external->p_filesz);
4738       internal->p_memsz  = BYTE_GET (external->p_memsz);
4739       internal->p_flags  = BYTE_GET (external->p_flags);
4740       internal->p_align  = BYTE_GET (external->p_align);
4741     }
4742 
4743   free (phdrs);
4744   return TRUE;
4745 }
4746 
4747 static bfd_boolean
get_64bit_program_headers(FILE * file,Elf_Internal_Phdr * pheaders)4748 get_64bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
4749 {
4750   Elf64_External_Phdr * phdrs;
4751   Elf64_External_Phdr * external;
4752   Elf_Internal_Phdr *   internal;
4753   unsigned int i;
4754   unsigned int size = elf_header.e_phentsize;
4755   unsigned int num  = elf_header.e_phnum;
4756 
4757   /* PR binutils/17531: Cope with unexpected section header sizes.  */
4758   if (size == 0 || num == 0)
4759     return FALSE;
4760   if (size < sizeof * phdrs)
4761     {
4762       error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4763       return FALSE;
4764     }
4765   if (size > sizeof * phdrs)
4766     warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4767 
4768   phdrs = (Elf64_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
4769                                             size, num, _("program headers"));
4770   if (!phdrs)
4771     return FALSE;
4772 
4773   for (i = 0, internal = pheaders, external = phdrs;
4774        i < elf_header.e_phnum;
4775        i++, internal++, external++)
4776     {
4777       internal->p_type   = BYTE_GET (external->p_type);
4778       internal->p_flags  = BYTE_GET (external->p_flags);
4779       internal->p_offset = BYTE_GET (external->p_offset);
4780       internal->p_vaddr  = BYTE_GET (external->p_vaddr);
4781       internal->p_paddr  = BYTE_GET (external->p_paddr);
4782       internal->p_filesz = BYTE_GET (external->p_filesz);
4783       internal->p_memsz  = BYTE_GET (external->p_memsz);
4784       internal->p_align  = BYTE_GET (external->p_align);
4785     }
4786 
4787   free (phdrs);
4788   return TRUE;
4789 }
4790 
4791 /* Returns 1 if the program headers were read into `program_headers'.  */
4792 
4793 static int
get_program_headers(FILE * file)4794 get_program_headers (FILE * file)
4795 {
4796   Elf_Internal_Phdr * phdrs;
4797 
4798   /* Check cache of prior read.  */
4799   if (program_headers != NULL)
4800     return 1;
4801 
4802   phdrs = (Elf_Internal_Phdr *) cmalloc (elf_header.e_phnum,
4803                                          sizeof (Elf_Internal_Phdr));
4804 
4805   if (phdrs == NULL)
4806     {
4807       error (_("Out of memory reading %u program headers\n"),
4808 	     elf_header.e_phnum);
4809       return 0;
4810     }
4811 
4812   if (is_32bit_elf
4813       ? get_32bit_program_headers (file, phdrs)
4814       : get_64bit_program_headers (file, phdrs))
4815     {
4816       program_headers = phdrs;
4817       return 1;
4818     }
4819 
4820   free (phdrs);
4821   return 0;
4822 }
4823 
4824 /* Returns 1 if the program headers were loaded.  */
4825 
4826 static int
process_program_headers(FILE * file)4827 process_program_headers (FILE * file)
4828 {
4829   Elf_Internal_Phdr * segment;
4830   unsigned int i;
4831 
4832   if (elf_header.e_phnum == 0)
4833     {
4834       /* PR binutils/12467.  */
4835       if (elf_header.e_phoff != 0)
4836 	warn (_("possibly corrupt ELF header - it has a non-zero program"
4837 		" header offset, but no program headers\n"));
4838       else if (do_segments)
4839 	printf (_("\nThere are no program headers in this file.\n"));
4840       return 0;
4841     }
4842 
4843   if (do_segments && !do_header)
4844     {
4845       printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
4846       printf (_("Entry point "));
4847       print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
4848       printf (_("\nThere are %d program headers, starting at offset "),
4849 	      elf_header.e_phnum);
4850       print_vma ((bfd_vma) elf_header.e_phoff, DEC);
4851       printf ("\n");
4852     }
4853 
4854   if (! get_program_headers (file))
4855       return 0;
4856 
4857   if (do_segments)
4858     {
4859       if (elf_header.e_phnum > 1)
4860 	printf (_("\nProgram Headers:\n"));
4861       else
4862 	printf (_("\nProgram Headers:\n"));
4863 
4864       if (is_32bit_elf)
4865 	printf
4866 	  (_("  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n"));
4867       else if (do_wide)
4868 	printf
4869 	  (_("  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align\n"));
4870       else
4871 	{
4872 	  printf
4873 	    (_("  Type           Offset             VirtAddr           PhysAddr\n"));
4874 	  printf
4875 	    (_("                 FileSiz            MemSiz              Flags  Align\n"));
4876 	}
4877     }
4878 
4879   dynamic_addr = 0;
4880   dynamic_size = 0;
4881 
4882   for (i = 0, segment = program_headers;
4883        i < elf_header.e_phnum;
4884        i++, segment++)
4885     {
4886       if (do_segments)
4887 	{
4888 	  printf ("  %-14.14s ", get_segment_type (segment->p_type));
4889 
4890 	  if (is_32bit_elf)
4891 	    {
4892 	      printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
4893 	      printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
4894 	      printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
4895 	      printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
4896 	      printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
4897 	      printf ("%c%c%c ",
4898 		      (segment->p_flags & PF_R ? 'R' : ' '),
4899 		      (segment->p_flags & PF_W ? 'W' : ' '),
4900 		      (segment->p_flags & PF_X ? 'E' : ' '));
4901 	      printf ("%#lx", (unsigned long) segment->p_align);
4902 	    }
4903 	  else if (do_wide)
4904 	    {
4905 	      if ((unsigned long) segment->p_offset == segment->p_offset)
4906 		printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
4907 	      else
4908 		{
4909 		  print_vma (segment->p_offset, FULL_HEX);
4910 		  putchar (' ');
4911 		}
4912 
4913 	      print_vma (segment->p_vaddr, FULL_HEX);
4914 	      putchar (' ');
4915 	      print_vma (segment->p_paddr, FULL_HEX);
4916 	      putchar (' ');
4917 
4918 	      if ((unsigned long) segment->p_filesz == segment->p_filesz)
4919 		printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
4920 	      else
4921 		{
4922 		  print_vma (segment->p_filesz, FULL_HEX);
4923 		  putchar (' ');
4924 		}
4925 
4926 	      if ((unsigned long) segment->p_memsz == segment->p_memsz)
4927 		printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
4928 	      else
4929 		{
4930 		  print_vma (segment->p_memsz, FULL_HEX);
4931 		}
4932 
4933 	      printf (" %c%c%c ",
4934 		      (segment->p_flags & PF_R ? 'R' : ' '),
4935 		      (segment->p_flags & PF_W ? 'W' : ' '),
4936 		      (segment->p_flags & PF_X ? 'E' : ' '));
4937 
4938 	      if ((unsigned long) segment->p_align == segment->p_align)
4939 		printf ("%#lx", (unsigned long) segment->p_align);
4940 	      else
4941 		{
4942 		  print_vma (segment->p_align, PREFIX_HEX);
4943 		}
4944 	    }
4945 	  else
4946 	    {
4947 	      print_vma (segment->p_offset, FULL_HEX);
4948 	      putchar (' ');
4949 	      print_vma (segment->p_vaddr, FULL_HEX);
4950 	      putchar (' ');
4951 	      print_vma (segment->p_paddr, FULL_HEX);
4952 	      printf ("\n                 ");
4953 	      print_vma (segment->p_filesz, FULL_HEX);
4954 	      putchar (' ');
4955 	      print_vma (segment->p_memsz, FULL_HEX);
4956 	      printf ("  %c%c%c    ",
4957 		      (segment->p_flags & PF_R ? 'R' : ' '),
4958 		      (segment->p_flags & PF_W ? 'W' : ' '),
4959 		      (segment->p_flags & PF_X ? 'E' : ' '));
4960 	      print_vma (segment->p_align, HEX);
4961 	    }
4962 	}
4963 
4964       if (do_segments)
4965 	putc ('\n', stdout);
4966 
4967       switch (segment->p_type)
4968 	{
4969 	case PT_DYNAMIC:
4970 	  if (dynamic_addr)
4971 	    error (_("more than one dynamic segment\n"));
4972 
4973 	  /* By default, assume that the .dynamic section is the first
4974 	     section in the DYNAMIC segment.  */
4975 	  dynamic_addr = segment->p_offset;
4976 	  dynamic_size = segment->p_filesz;
4977 	  /* PR binutils/17512: Avoid corrupt dynamic section info in the segment.  */
4978 	  if (dynamic_addr + dynamic_size >= current_file_size)
4979 	    {
4980 	      error (_("the dynamic segment offset + size exceeds the size of the file\n"));
4981 	      dynamic_addr = dynamic_size = 0;
4982 	    }
4983 
4984 	  /* Try to locate the .dynamic section. If there is
4985 	     a section header table, we can easily locate it.  */
4986 	  if (section_headers != NULL)
4987 	    {
4988 	      Elf_Internal_Shdr * sec;
4989 
4990 	      sec = find_section (".dynamic");
4991 	      if (sec == NULL || sec->sh_size == 0)
4992 		{
4993                   /* A corresponding .dynamic section is expected, but on
4994                      IA-64/OpenVMS it is OK for it to be missing.  */
4995                   if (!is_ia64_vms ())
4996                     error (_("no .dynamic section in the dynamic segment\n"));
4997 		  break;
4998 		}
4999 
5000 	      if (sec->sh_type == SHT_NOBITS)
5001 		{
5002 		  dynamic_size = 0;
5003 		  break;
5004 		}
5005 
5006 	      dynamic_addr = sec->sh_offset;
5007 	      dynamic_size = sec->sh_size;
5008 
5009 	      if (dynamic_addr < segment->p_offset
5010 		  || dynamic_addr > segment->p_offset + segment->p_filesz)
5011 		warn (_("the .dynamic section is not contained"
5012 			" within the dynamic segment\n"));
5013 	      else if (dynamic_addr > segment->p_offset)
5014 		warn (_("the .dynamic section is not the first section"
5015 			" in the dynamic segment.\n"));
5016 	    }
5017 	  break;
5018 
5019 	case PT_INTERP:
5020 	  if (fseek (file, archive_file_offset + (long) segment->p_offset,
5021 		     SEEK_SET))
5022 	    error (_("Unable to find program interpreter name\n"));
5023 	  else
5024 	    {
5025 	      char fmt [32];
5026 	      int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
5027 
5028 	      if (ret >= (int) sizeof (fmt) || ret < 0)
5029 		error (_("Internal error: failed to create format string to display program interpreter\n"));
5030 
5031 	      program_interpreter[0] = 0;
5032 	      if (fscanf (file, fmt, program_interpreter) <= 0)
5033 		error (_("Unable to read program interpreter name\n"));
5034 
5035 	      if (do_segments)
5036 		printf (_("      [Requesting program interpreter: %s]\n"),
5037 		    program_interpreter);
5038 	    }
5039 	  break;
5040 	}
5041     }
5042 
5043   if (do_segments && section_headers != NULL && string_table != NULL)
5044     {
5045       printf (_("\n Section to Segment mapping:\n"));
5046       printf (_("  Segment Sections...\n"));
5047 
5048       for (i = 0; i < elf_header.e_phnum; i++)
5049 	{
5050 	  unsigned int j;
5051 	  Elf_Internal_Shdr * section;
5052 
5053 	  segment = program_headers + i;
5054 	  section = section_headers + 1;
5055 
5056 	  printf ("   %2.2d     ", i);
5057 
5058 	  for (j = 1; j < elf_header.e_shnum; j++, section++)
5059 	    {
5060 	      if (!ELF_TBSS_SPECIAL (section, segment)
5061 		  && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5062 		printf ("%s ", printable_section_name (section));
5063 	    }
5064 
5065 	  putc ('\n',stdout);
5066 	}
5067     }
5068 
5069   return 1;
5070 }
5071 
5072 
5073 /* Find the file offset corresponding to VMA by using the program headers.  */
5074 
5075 static long
offset_from_vma(FILE * file,bfd_vma vma,bfd_size_type size)5076 offset_from_vma (FILE * file, bfd_vma vma, bfd_size_type size)
5077 {
5078   Elf_Internal_Phdr * seg;
5079 
5080   if (! get_program_headers (file))
5081     {
5082       warn (_("Cannot interpret virtual addresses without program headers.\n"));
5083       return (long) vma;
5084     }
5085 
5086   for (seg = program_headers;
5087        seg < program_headers + elf_header.e_phnum;
5088        ++seg)
5089     {
5090       if (seg->p_type != PT_LOAD)
5091 	continue;
5092 
5093       if (vma >= (seg->p_vaddr & -seg->p_align)
5094 	  && vma + size <= seg->p_vaddr + seg->p_filesz)
5095 	return vma - seg->p_vaddr + seg->p_offset;
5096     }
5097 
5098   warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5099 	(unsigned long) vma);
5100   return (long) vma;
5101 }
5102 
5103 
5104 /* Allocate memory and load the sections headers into the global pointer
5105    SECTION_HEADERS.  If PROBE is true, this is just a probe and we do not
5106    generate any error messages if the load fails.  */
5107 
5108 static bfd_boolean
get_32bit_section_headers(FILE * file,bfd_boolean probe)5109 get_32bit_section_headers (FILE * file, bfd_boolean probe)
5110 {
5111   Elf32_External_Shdr * shdrs;
5112   Elf_Internal_Shdr *   internal;
5113   unsigned int i;
5114   unsigned int size = elf_header.e_shentsize;
5115   unsigned int num = probe ? 1 : elf_header.e_shnum;
5116 
5117   /* PR binutils/17531: Cope with unexpected section header sizes.  */
5118   if (size == 0 || num == 0)
5119     return FALSE;
5120   if (size < sizeof * shdrs)
5121     {
5122       if (! probe)
5123 	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5124       return FALSE;
5125     }
5126   if (!probe && size > sizeof * shdrs)
5127     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5128 
5129   shdrs = (Elf32_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
5130                                             size, num,
5131 					    probe ? NULL : _("section headers"));
5132   if (shdrs == NULL)
5133     return FALSE;
5134 
5135   if (section_headers != NULL)
5136     free (section_headers);
5137   section_headers = (Elf_Internal_Shdr *) cmalloc (num,
5138                                                    sizeof (Elf_Internal_Shdr));
5139   if (section_headers == NULL)
5140     {
5141       if (!probe)
5142 	error (_("Out of memory reading %u section headers\n"), num);
5143       return FALSE;
5144     }
5145 
5146   for (i = 0, internal = section_headers;
5147        i < num;
5148        i++, internal++)
5149     {
5150       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
5151       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
5152       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
5153       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
5154       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
5155       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
5156       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
5157       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
5158       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5159       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
5160       if (!probe && internal->sh_link > num)
5161 	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5162       if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5163 	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5164     }
5165 
5166   free (shdrs);
5167   return TRUE;
5168 }
5169 
5170 static bfd_boolean
get_64bit_section_headers(FILE * file,bfd_boolean probe)5171 get_64bit_section_headers (FILE * file, bfd_boolean probe)
5172 {
5173   Elf64_External_Shdr * shdrs;
5174   Elf_Internal_Shdr *   internal;
5175   unsigned int i;
5176   unsigned int size = elf_header.e_shentsize;
5177   unsigned int num = probe ? 1 : elf_header.e_shnum;
5178 
5179   /* PR binutils/17531: Cope with unexpected section header sizes.  */
5180   if (size == 0 || num == 0)
5181     return FALSE;
5182   if (size < sizeof * shdrs)
5183     {
5184       if (! probe)
5185 	error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5186       return FALSE;
5187     }
5188   if (! probe && size > sizeof * shdrs)
5189     warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5190 
5191   shdrs = (Elf64_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
5192                                             size, num,
5193 					    probe ? NULL : _("section headers"));
5194   if (shdrs == NULL)
5195     return FALSE;
5196 
5197   if (section_headers != NULL)
5198     free (section_headers);
5199   section_headers = (Elf_Internal_Shdr *) cmalloc (num,
5200                                                    sizeof (Elf_Internal_Shdr));
5201   if (section_headers == NULL)
5202     {
5203       if (! probe)
5204 	error (_("Out of memory reading %u section headers\n"), num);
5205       return FALSE;
5206     }
5207 
5208   for (i = 0, internal = section_headers;
5209        i < num;
5210        i++, internal++)
5211     {
5212       internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
5213       internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
5214       internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
5215       internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
5216       internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
5217       internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
5218       internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
5219       internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
5220       internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
5221       internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5222       if (!probe && internal->sh_link > num)
5223 	warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5224       if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5225 	warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5226     }
5227 
5228   free (shdrs);
5229   return TRUE;
5230 }
5231 
5232 static Elf_Internal_Sym *
get_32bit_elf_symbols(FILE * file,Elf_Internal_Shdr * section,unsigned long * num_syms_return)5233 get_32bit_elf_symbols (FILE * file,
5234 		       Elf_Internal_Shdr * section,
5235 		       unsigned long * num_syms_return)
5236 {
5237   unsigned long number = 0;
5238   Elf32_External_Sym * esyms = NULL;
5239   Elf_External_Sym_Shndx * shndx = NULL;
5240   Elf_Internal_Sym * isyms = NULL;
5241   Elf_Internal_Sym * psym;
5242   unsigned int j;
5243 
5244   if (section->sh_size == 0)
5245     {
5246       if (num_syms_return != NULL)
5247 	* num_syms_return = 0;
5248       return NULL;
5249     }
5250 
5251   /* Run some sanity checks first.  */
5252   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5253     {
5254       error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5255 	     printable_section_name (section), (unsigned long) section->sh_entsize);
5256       goto exit_point;
5257     }
5258 
5259   if (section->sh_size > current_file_size)
5260     {
5261       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5262 	     printable_section_name (section), (unsigned long) section->sh_size);
5263       goto exit_point;
5264     }
5265 
5266   number = section->sh_size / section->sh_entsize;
5267 
5268   if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5269     {
5270       error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5271 	     (unsigned long) section->sh_size,
5272 	     printable_section_name (section),
5273 	     (unsigned long) section->sh_entsize);
5274       goto exit_point;
5275     }
5276 
5277   esyms = (Elf32_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
5278                                            section->sh_size, _("symbols"));
5279   if (esyms == NULL)
5280     goto exit_point;
5281 
5282   {
5283     elf_section_list * entry;
5284 
5285     shndx = NULL;
5286     for (entry = symtab_shndx_list; entry != NULL; entry = entry->next)
5287       if (entry->hdr->sh_link == (unsigned long) (section - section_headers))
5288 	{
5289 	  shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
5290 						       entry->hdr->sh_offset,
5291 						       1, entry->hdr->sh_size,
5292 						       _("symbol table section indicies"));
5293 	  if (shndx == NULL)
5294 	    goto exit_point;
5295 	  /* PR17531: file: heap-buffer-overflow */
5296 	  else if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5297 	    {
5298 	      error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5299 		     printable_section_name (entry->hdr),
5300 		     (unsigned long) entry->hdr->sh_size,
5301 		     (unsigned long) section->sh_size);
5302 	      goto exit_point;
5303 	    }
5304 	}
5305   }
5306 
5307   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5308 
5309   if (isyms == NULL)
5310     {
5311       error (_("Out of memory reading %lu symbols\n"),
5312 	     (unsigned long) number);
5313       goto exit_point;
5314     }
5315 
5316   for (j = 0, psym = isyms; j < number; j++, psym++)
5317     {
5318       psym->st_name  = BYTE_GET (esyms[j].st_name);
5319       psym->st_value = BYTE_GET (esyms[j].st_value);
5320       psym->st_size  = BYTE_GET (esyms[j].st_size);
5321       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5322       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5323 	psym->st_shndx
5324 	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5325       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5326 	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5327       psym->st_info  = BYTE_GET (esyms[j].st_info);
5328       psym->st_other = BYTE_GET (esyms[j].st_other);
5329     }
5330 
5331  exit_point:
5332   if (shndx != NULL)
5333     free (shndx);
5334   if (esyms != NULL)
5335     free (esyms);
5336 
5337   if (num_syms_return != NULL)
5338     * num_syms_return = isyms == NULL ? 0 : number;
5339 
5340   return isyms;
5341 }
5342 
5343 static Elf_Internal_Sym *
get_64bit_elf_symbols(FILE * file,Elf_Internal_Shdr * section,unsigned long * num_syms_return)5344 get_64bit_elf_symbols (FILE * file,
5345 		       Elf_Internal_Shdr * section,
5346 		       unsigned long * num_syms_return)
5347 {
5348   unsigned long number = 0;
5349   Elf64_External_Sym * esyms = NULL;
5350   Elf_External_Sym_Shndx * shndx = NULL;
5351   Elf_Internal_Sym * isyms = NULL;
5352   Elf_Internal_Sym * psym;
5353   unsigned int j;
5354 
5355   if (section->sh_size == 0)
5356     {
5357       if (num_syms_return != NULL)
5358 	* num_syms_return = 0;
5359       return NULL;
5360     }
5361 
5362   /* Run some sanity checks first.  */
5363   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5364     {
5365       error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5366 	     printable_section_name (section),
5367 	     (unsigned long) section->sh_entsize);
5368       goto exit_point;
5369     }
5370 
5371   if (section->sh_size > current_file_size)
5372     {
5373       error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5374 	     printable_section_name (section),
5375 	     (unsigned long) section->sh_size);
5376       goto exit_point;
5377     }
5378 
5379   number = section->sh_size / section->sh_entsize;
5380 
5381   if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
5382     {
5383       error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5384 	     (unsigned long) section->sh_size,
5385 	     printable_section_name (section),
5386 	     (unsigned long) section->sh_entsize);
5387       goto exit_point;
5388     }
5389 
5390   esyms = (Elf64_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
5391                                            section->sh_size, _("symbols"));
5392   if (!esyms)
5393     goto exit_point;
5394 
5395   {
5396     elf_section_list * entry;
5397 
5398     shndx = NULL;
5399     for (entry = symtab_shndx_list; entry != NULL; entry = entry->next)
5400       if (entry->hdr->sh_link == (unsigned long) (section - section_headers))
5401 	{
5402 	  shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
5403 						       entry->hdr->sh_offset,
5404 						       1, entry->hdr->sh_size,
5405 						       _("symbol table section indicies"));
5406 	  if (shndx == NULL)
5407 	    goto exit_point;
5408 	  /* PR17531: file: heap-buffer-overflow */
5409 	  else if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5410 	    {
5411 	      error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5412 		     printable_section_name (entry->hdr),
5413 		     (unsigned long) entry->hdr->sh_size,
5414 		     (unsigned long) section->sh_size);
5415 	      goto exit_point;
5416 	    }
5417 	}
5418   }
5419 
5420   isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5421 
5422   if (isyms == NULL)
5423     {
5424       error (_("Out of memory reading %lu symbols\n"),
5425 	     (unsigned long) number);
5426       goto exit_point;
5427     }
5428 
5429   for (j = 0, psym = isyms; j < number; j++, psym++)
5430     {
5431       psym->st_name  = BYTE_GET (esyms[j].st_name);
5432       psym->st_info  = BYTE_GET (esyms[j].st_info);
5433       psym->st_other = BYTE_GET (esyms[j].st_other);
5434       psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5435 
5436       if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5437 	psym->st_shndx
5438 	  = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5439       else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5440 	psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5441 
5442       psym->st_value = BYTE_GET (esyms[j].st_value);
5443       psym->st_size  = BYTE_GET (esyms[j].st_size);
5444     }
5445 
5446  exit_point:
5447   if (shndx != NULL)
5448     free (shndx);
5449   if (esyms != NULL)
5450     free (esyms);
5451 
5452   if (num_syms_return != NULL)
5453     * num_syms_return = isyms == NULL ? 0 : number;
5454 
5455   return isyms;
5456 }
5457 
5458 static const char *
get_elf_section_flags(bfd_vma sh_flags)5459 get_elf_section_flags (bfd_vma sh_flags)
5460 {
5461   static char buff[1024];
5462   char * p = buff;
5463   int field_size = is_32bit_elf ? 8 : 16;
5464   int sindex;
5465   int size = sizeof (buff) - (field_size + 4 + 1);
5466   bfd_vma os_flags = 0;
5467   bfd_vma proc_flags = 0;
5468   bfd_vma unknown_flags = 0;
5469   static const struct
5470     {
5471       const char * str;
5472       int len;
5473     }
5474   flags [] =
5475     {
5476       /*  0 */ { STRING_COMMA_LEN ("WRITE") },
5477       /*  1 */ { STRING_COMMA_LEN ("ALLOC") },
5478       /*  2 */ { STRING_COMMA_LEN ("EXEC") },
5479       /*  3 */ { STRING_COMMA_LEN ("MERGE") },
5480       /*  4 */ { STRING_COMMA_LEN ("STRINGS") },
5481       /*  5 */ { STRING_COMMA_LEN ("INFO LINK") },
5482       /*  6 */ { STRING_COMMA_LEN ("LINK ORDER") },
5483       /*  7 */ { STRING_COMMA_LEN ("OS NONCONF") },
5484       /*  8 */ { STRING_COMMA_LEN ("GROUP") },
5485       /*  9 */ { STRING_COMMA_LEN ("TLS") },
5486       /* IA-64 specific.  */
5487       /* 10 */ { STRING_COMMA_LEN ("SHORT") },
5488       /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
5489       /* IA-64 OpenVMS specific.  */
5490       /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
5491       /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
5492       /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
5493       /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
5494       /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
5495       /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
5496       /* Generic.  */
5497       /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
5498       /* SPARC specific.  */
5499       /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
5500       /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
5501       /* ARM specific.  */
5502       /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
5503       /* 22 */ { STRING_COMMA_LEN ("ARM_NOREAD") },
5504       /* 23 */ { STRING_COMMA_LEN ("COMDEF") }
5505     };
5506 
5507   if (do_section_details)
5508     {
5509       sprintf (buff, "[%*.*lx]: ",
5510 	       field_size, field_size, (unsigned long) sh_flags);
5511       p += field_size + 4;
5512     }
5513 
5514   while (sh_flags)
5515     {
5516       bfd_vma flag;
5517 
5518       flag = sh_flags & - sh_flags;
5519       sh_flags &= ~ flag;
5520 
5521       if (do_section_details)
5522 	{
5523 	  switch (flag)
5524 	    {
5525 	    case SHF_WRITE:		sindex = 0; break;
5526 	    case SHF_ALLOC:		sindex = 1; break;
5527 	    case SHF_EXECINSTR:		sindex = 2; break;
5528 	    case SHF_MERGE:		sindex = 3; break;
5529 	    case SHF_STRINGS:		sindex = 4; break;
5530 	    case SHF_INFO_LINK:		sindex = 5; break;
5531 	    case SHF_LINK_ORDER:	sindex = 6; break;
5532 	    case SHF_OS_NONCONFORMING:	sindex = 7; break;
5533 	    case SHF_GROUP:		sindex = 8; break;
5534 	    case SHF_TLS:		sindex = 9; break;
5535 	    case SHF_EXCLUDE:		sindex = 18; break;
5536 	    case SHF_COMPRESSED:	sindex = 20; break;
5537 
5538 	    default:
5539 	      sindex = -1;
5540 	      switch (elf_header.e_machine)
5541 		{
5542 		case EM_IA_64:
5543 		  if (flag == SHF_IA_64_SHORT)
5544 		    sindex = 10;
5545 		  else if (flag == SHF_IA_64_NORECOV)
5546 		    sindex = 11;
5547 #ifdef BFD64
5548 		  else if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
5549 		    switch (flag)
5550 		      {
5551 		      case SHF_IA_64_VMS_GLOBAL:      sindex = 12; break;
5552 		      case SHF_IA_64_VMS_OVERLAID:    sindex = 13; break;
5553 		      case SHF_IA_64_VMS_SHARED:      sindex = 14; break;
5554 		      case SHF_IA_64_VMS_VECTOR:      sindex = 15; break;
5555 		      case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
5556 		      case SHF_IA_64_VMS_PROTECTED:   sindex = 17; break;
5557 		      default:                        break;
5558 		      }
5559 #endif
5560 		  break;
5561 
5562 		case EM_386:
5563 		case EM_IAMCU:
5564 		case EM_X86_64:
5565 		case EM_L1OM:
5566 		case EM_K1OM:
5567 		case EM_OLD_SPARCV9:
5568 		case EM_SPARC32PLUS:
5569 		case EM_SPARCV9:
5570 		case EM_SPARC:
5571 		  if (flag == SHF_ORDERED)
5572 		    sindex = 19;
5573 		  break;
5574 
5575 		case EM_ARM:
5576 		  switch (flag)
5577 		    {
5578 		    case SHF_ENTRYSECT: sindex = 21; break;
5579 		    case SHF_ARM_NOREAD: sindex = 22; break;
5580 		    case SHF_COMDEF: sindex = 23; break;
5581 		    default: break;
5582 		    }
5583 		  break;
5584 
5585 		default:
5586 		  break;
5587 		}
5588 	    }
5589 
5590 	  if (sindex != -1)
5591 	    {
5592 	      if (p != buff + field_size + 4)
5593 		{
5594 		  if (size < (10 + 2))
5595 		    {
5596 		      warn (_("Internal error: not enough buffer room for section flag info"));
5597 		      return _("<unknown>");
5598 		    }
5599 		  size -= 2;
5600 		  *p++ = ',';
5601 		  *p++ = ' ';
5602 		}
5603 
5604 	      size -= flags [sindex].len;
5605 	      p = stpcpy (p, flags [sindex].str);
5606 	    }
5607 	  else if (flag & SHF_MASKOS)
5608 	    os_flags |= flag;
5609 	  else if (flag & SHF_MASKPROC)
5610 	    proc_flags |= flag;
5611 	  else
5612 	    unknown_flags |= flag;
5613 	}
5614       else
5615 	{
5616 	  switch (flag)
5617 	    {
5618 	    case SHF_WRITE:		*p = 'W'; break;
5619 	    case SHF_ALLOC:		*p = 'A'; break;
5620 	    case SHF_EXECINSTR:		*p = 'X'; break;
5621 	    case SHF_MERGE:		*p = 'M'; break;
5622 	    case SHF_STRINGS:		*p = 'S'; break;
5623 	    case SHF_INFO_LINK:		*p = 'I'; break;
5624 	    case SHF_LINK_ORDER:	*p = 'L'; break;
5625 	    case SHF_OS_NONCONFORMING:	*p = 'O'; break;
5626 	    case SHF_GROUP:		*p = 'G'; break;
5627 	    case SHF_TLS:		*p = 'T'; break;
5628 	    case SHF_EXCLUDE:		*p = 'E'; break;
5629 	    case SHF_COMPRESSED:	*p = 'C'; break;
5630 
5631 	    default:
5632 	      if ((elf_header.e_machine == EM_X86_64
5633 		   || elf_header.e_machine == EM_L1OM
5634 		   || elf_header.e_machine == EM_K1OM)
5635 		  && flag == SHF_X86_64_LARGE)
5636 		*p = 'l';
5637 	      else if (elf_header.e_machine == EM_ARM
5638 		       && flag == SHF_ARM_NOREAD)
5639 		  *p = 'y';
5640 	      else if (flag & SHF_MASKOS)
5641 		{
5642 		  *p = 'o';
5643 		  sh_flags &= ~ SHF_MASKOS;
5644 		}
5645 	      else if (flag & SHF_MASKPROC)
5646 		{
5647 		  *p = 'p';
5648 		  sh_flags &= ~ SHF_MASKPROC;
5649 		}
5650 	      else
5651 		*p = 'x';
5652 	      break;
5653 	    }
5654 	  p++;
5655 	}
5656     }
5657 
5658   if (do_section_details)
5659     {
5660       if (os_flags)
5661 	{
5662 	  size -= 5 + field_size;
5663 	  if (p != buff + field_size + 4)
5664 	    {
5665 	      if (size < (2 + 1))
5666 		{
5667 		  warn (_("Internal error: not enough buffer room for section flag info"));
5668 		  return _("<unknown>");
5669 		}
5670 	      size -= 2;
5671 	      *p++ = ',';
5672 	      *p++ = ' ';
5673 	    }
5674 	  sprintf (p, "OS (%*.*lx)", field_size, field_size,
5675 		   (unsigned long) os_flags);
5676 	  p += 5 + field_size;
5677 	}
5678       if (proc_flags)
5679 	{
5680 	  size -= 7 + field_size;
5681 	  if (p != buff + field_size + 4)
5682 	    {
5683 	      if (size < (2 + 1))
5684 		{
5685 		  warn (_("Internal error: not enough buffer room for section flag info"));
5686 		  return _("<unknown>");
5687 		}
5688 	      size -= 2;
5689 	      *p++ = ',';
5690 	      *p++ = ' ';
5691 	    }
5692 	  sprintf (p, "PROC (%*.*lx)", field_size, field_size,
5693 		   (unsigned long) proc_flags);
5694 	  p += 7 + field_size;
5695 	}
5696       if (unknown_flags)
5697 	{
5698 	  size -= 10 + field_size;
5699 	  if (p != buff + field_size + 4)
5700 	    {
5701 	      if (size < (2 + 1))
5702 		{
5703 		  warn (_("Internal error: not enough buffer room for section flag info"));
5704 		  return _("<unknown>");
5705 		}
5706 	      size -= 2;
5707 	      *p++ = ',';
5708 	      *p++ = ' ';
5709 	    }
5710 	  sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
5711 		   (unsigned long) unknown_flags);
5712 	  p += 10 + field_size;
5713 	}
5714     }
5715 
5716   *p = '\0';
5717   return buff;
5718 }
5719 
5720 static unsigned int
get_compression_header(Elf_Internal_Chdr * chdr,unsigned char * buf)5721 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf)
5722 {
5723   if (is_32bit_elf)
5724     {
5725       Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
5726       chdr->ch_type = BYTE_GET (echdr->ch_type);
5727       chdr->ch_size = BYTE_GET (echdr->ch_size);
5728       chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
5729       return sizeof (*echdr);
5730     }
5731   else
5732     {
5733       Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
5734       chdr->ch_type = BYTE_GET (echdr->ch_type);
5735       chdr->ch_size = BYTE_GET (echdr->ch_size);
5736       chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
5737       return sizeof (*echdr);
5738     }
5739 }
5740 
5741 static int
process_section_headers(FILE * file)5742 process_section_headers (FILE * file)
5743 {
5744   Elf_Internal_Shdr * section;
5745   unsigned int i;
5746 
5747   section_headers = NULL;
5748 
5749   if (elf_header.e_shnum == 0)
5750     {
5751       /* PR binutils/12467.  */
5752       if (elf_header.e_shoff != 0)
5753 	warn (_("possibly corrupt ELF file header - it has a non-zero"
5754 		" section header offset, but no section headers\n"));
5755       else if (do_sections)
5756 	printf (_("\nThere are no sections in this file.\n"));
5757 
5758       return 1;
5759     }
5760 
5761   if (do_sections && !do_header)
5762     printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
5763 	    elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
5764 
5765   if (is_32bit_elf)
5766     {
5767       if (! get_32bit_section_headers (file, FALSE))
5768 	return 0;
5769     }
5770   else if (! get_64bit_section_headers (file, FALSE))
5771     return 0;
5772 
5773   /* Read in the string table, so that we have names to display.  */
5774   if (elf_header.e_shstrndx != SHN_UNDEF
5775        && elf_header.e_shstrndx < elf_header.e_shnum)
5776     {
5777       section = section_headers + elf_header.e_shstrndx;
5778 
5779       if (section->sh_size != 0)
5780 	{
5781 	  string_table = (char *) get_data (NULL, file, section->sh_offset,
5782                                             1, section->sh_size,
5783                                             _("string table"));
5784 
5785 	  string_table_length = string_table != NULL ? section->sh_size : 0;
5786 	}
5787     }
5788 
5789   /* Scan the sections for the dynamic symbol table
5790      and dynamic string table and debug sections.  */
5791   dynamic_symbols = NULL;
5792   dynamic_strings = NULL;
5793   dynamic_syminfo = NULL;
5794   symtab_shndx_list = NULL;
5795 
5796   eh_addr_size = is_32bit_elf ? 4 : 8;
5797   switch (elf_header.e_machine)
5798     {
5799     case EM_MIPS:
5800     case EM_MIPS_RS3_LE:
5801       /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
5802 	 FDE addresses.  However, the ABI also has a semi-official ILP32
5803 	 variant for which the normal FDE address size rules apply.
5804 
5805 	 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
5806 	 section, where XX is the size of longs in bits.  Unfortunately,
5807 	 earlier compilers provided no way of distinguishing ILP32 objects
5808 	 from LP64 objects, so if there's any doubt, we should assume that
5809 	 the official LP64 form is being used.  */
5810       if ((elf_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
5811 	  && find_section (".gcc_compiled_long32") == NULL)
5812 	eh_addr_size = 8;
5813       break;
5814 
5815     case EM_H8_300:
5816     case EM_H8_300H:
5817       switch (elf_header.e_flags & EF_H8_MACH)
5818 	{
5819 	case E_H8_MACH_H8300:
5820 	case E_H8_MACH_H8300HN:
5821 	case E_H8_MACH_H8300SN:
5822 	case E_H8_MACH_H8300SXN:
5823 	  eh_addr_size = 2;
5824 	  break;
5825 	case E_H8_MACH_H8300H:
5826 	case E_H8_MACH_H8300S:
5827 	case E_H8_MACH_H8300SX:
5828 	  eh_addr_size = 4;
5829 	  break;
5830 	}
5831       break;
5832 
5833     case EM_M32C_OLD:
5834     case EM_M32C:
5835       switch (elf_header.e_flags & EF_M32C_CPU_MASK)
5836 	{
5837 	case EF_M32C_CPU_M16C:
5838 	  eh_addr_size = 2;
5839 	  break;
5840 	}
5841       break;
5842     }
5843 
5844 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64)		\
5845   do									\
5846     {									\
5847       bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64;	\
5848       if (section->sh_entsize != expected_entsize)			\
5849 	{								\
5850 	  char buf[40];							\
5851 	  sprintf_vma (buf, section->sh_entsize);			\
5852 	  /* Note: coded this way so that there is a single string for  \
5853 	     translation.  */ \
5854 	  error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
5855 	  error (_("(Using the expected size of %u for the rest of this dump)\n"), \
5856 		   (unsigned) expected_entsize);			\
5857 	  section->sh_entsize = expected_entsize;			\
5858 	}								\
5859     }									\
5860   while (0)
5861 
5862 #define CHECK_ENTSIZE(section, i, type)					\
5863   CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type),	    \
5864 			sizeof (Elf64_External_##type))
5865 
5866   for (i = 0, section = section_headers;
5867        i < elf_header.e_shnum;
5868        i++, section++)
5869     {
5870       char * name = SECTION_NAME (section);
5871 
5872       if (section->sh_type == SHT_DYNSYM)
5873 	{
5874 	  if (dynamic_symbols != NULL)
5875 	    {
5876 	      error (_("File contains multiple dynamic symbol tables\n"));
5877 	      continue;
5878 	    }
5879 
5880 	  CHECK_ENTSIZE (section, i, Sym);
5881 	  dynamic_symbols = GET_ELF_SYMBOLS (file, section, & num_dynamic_syms);
5882 	}
5883       else if (section->sh_type == SHT_STRTAB
5884 	       && streq (name, ".dynstr"))
5885 	{
5886 	  if (dynamic_strings != NULL)
5887 	    {
5888 	      error (_("File contains multiple dynamic string tables\n"));
5889 	      continue;
5890 	    }
5891 
5892 	  dynamic_strings = (char *) get_data (NULL, file, section->sh_offset,
5893                                                1, section->sh_size,
5894                                                _("dynamic strings"));
5895 	  dynamic_strings_length = dynamic_strings == NULL ? 0 : section->sh_size;
5896 	}
5897       else if (section->sh_type == SHT_SYMTAB_SHNDX)
5898 	{
5899 	  elf_section_list * entry = xmalloc (sizeof * entry);
5900 	  entry->hdr = section;
5901 	  entry->next = symtab_shndx_list;
5902 	  symtab_shndx_list = entry;
5903 	}
5904       else if (section->sh_type == SHT_SYMTAB)
5905 	CHECK_ENTSIZE (section, i, Sym);
5906       else if (section->sh_type == SHT_GROUP)
5907 	CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
5908       else if (section->sh_type == SHT_REL)
5909 	CHECK_ENTSIZE (section, i, Rel);
5910       else if (section->sh_type == SHT_RELA)
5911 	CHECK_ENTSIZE (section, i, Rela);
5912       else if ((do_debugging || do_debug_info || do_debug_abbrevs
5913 		|| do_debug_lines || do_debug_pubnames || do_debug_pubtypes
5914 		|| do_debug_aranges || do_debug_frames || do_debug_macinfo
5915 		|| do_debug_str || do_debug_loc || do_debug_ranges
5916 		|| do_debug_addr || do_debug_cu_index)
5917 	       && (const_strneq (name, ".debug_")
5918                    || const_strneq (name, ".zdebug_")))
5919 	{
5920           if (name[1] == 'z')
5921             name += sizeof (".zdebug_") - 1;
5922           else
5923             name += sizeof (".debug_") - 1;
5924 
5925 	  if (do_debugging
5926 	      || (do_debug_info     && const_strneq (name, "info"))
5927 	      || (do_debug_info     && const_strneq (name, "types"))
5928 	      || (do_debug_abbrevs  && const_strneq (name, "abbrev"))
5929 	      || (do_debug_lines    && strcmp (name, "line") == 0)
5930 	      || (do_debug_lines    && const_strneq (name, "line."))
5931 	      || (do_debug_pubnames && const_strneq (name, "pubnames"))
5932 	      || (do_debug_pubtypes && const_strneq (name, "pubtypes"))
5933 	      || (do_debug_pubnames && const_strneq (name, "gnu_pubnames"))
5934 	      || (do_debug_pubtypes && const_strneq (name, "gnu_pubtypes"))
5935 	      || (do_debug_aranges  && const_strneq (name, "aranges"))
5936 	      || (do_debug_ranges   && const_strneq (name, "ranges"))
5937 	      || (do_debug_frames   && const_strneq (name, "frame"))
5938 	      || (do_debug_macinfo  && const_strneq (name, "macinfo"))
5939 	      || (do_debug_macinfo  && const_strneq (name, "macro"))
5940 	      || (do_debug_str      && const_strneq (name, "str"))
5941 	      || (do_debug_str      && const_strneq (name, "line_str"))
5942 	      || (do_debug_loc      && const_strneq (name, "loc"))
5943 	      || (do_debug_addr     && const_strneq (name, "addr"))
5944 	      || (do_debug_cu_index && const_strneq (name, "cu_index"))
5945 	      || (do_debug_cu_index && const_strneq (name, "tu_index"))
5946 	      )
5947 	    request_dump_bynumber (i, DEBUG_DUMP);
5948 	}
5949       /* Linkonce section to be combined with .debug_info at link time.  */
5950       else if ((do_debugging || do_debug_info)
5951 	       && const_strneq (name, ".gnu.linkonce.wi."))
5952 	request_dump_bynumber (i, DEBUG_DUMP);
5953       else if (do_debug_frames && streq (name, ".eh_frame"))
5954 	request_dump_bynumber (i, DEBUG_DUMP);
5955       else if (do_gdb_index && streq (name, ".gdb_index"))
5956 	request_dump_bynumber (i, DEBUG_DUMP);
5957       /* Trace sections for Itanium VMS.  */
5958       else if ((do_debugging || do_trace_info || do_trace_abbrevs
5959                 || do_trace_aranges)
5960 	       && const_strneq (name, ".trace_"))
5961 	{
5962           name += sizeof (".trace_") - 1;
5963 
5964 	  if (do_debugging
5965 	      || (do_trace_info     && streq (name, "info"))
5966 	      || (do_trace_abbrevs  && streq (name, "abbrev"))
5967 	      || (do_trace_aranges  && streq (name, "aranges"))
5968 	      )
5969 	    request_dump_bynumber (i, DEBUG_DUMP);
5970 	}
5971     }
5972 
5973   if (! do_sections)
5974     return 1;
5975 
5976   if (elf_header.e_shnum > 1)
5977     printf (_("\nSection Headers:\n"));
5978   else
5979     printf (_("\nSection Header:\n"));
5980 
5981   if (is_32bit_elf)
5982     {
5983       if (do_section_details)
5984 	{
5985 	  printf (_("  [Nr] Name\n"));
5986 	  printf (_("       Type            Addr     Off    Size   ES   Lk Inf Al\n"));
5987 	}
5988       else
5989 	printf
5990 	  (_("  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al\n"));
5991     }
5992   else if (do_wide)
5993     {
5994       if (do_section_details)
5995 	{
5996 	  printf (_("  [Nr] Name\n"));
5997 	  printf (_("       Type            Address          Off    Size   ES   Lk Inf Al\n"));
5998 	}
5999       else
6000 	printf
6001 	  (_("  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al\n"));
6002     }
6003   else
6004     {
6005       if (do_section_details)
6006 	{
6007 	  printf (_("  [Nr] Name\n"));
6008 	  printf (_("       Type              Address          Offset            Link\n"));
6009 	  printf (_("       Size              EntSize          Info              Align\n"));
6010 	}
6011       else
6012 	{
6013 	  printf (_("  [Nr] Name              Type             Address           Offset\n"));
6014 	  printf (_("       Size              EntSize          Flags  Link  Info  Align\n"));
6015 	}
6016     }
6017 
6018   if (do_section_details)
6019     printf (_("       Flags\n"));
6020 
6021   for (i = 0, section = section_headers;
6022        i < elf_header.e_shnum;
6023        i++, section++)
6024     {
6025       printf ("  [%2u] ", i);
6026       if (do_section_details)
6027 	printf ("%s\n      ", printable_section_name (section));
6028       else
6029 	print_symbol (-17, SECTION_NAME (section));
6030 
6031       printf (do_wide ? " %-15s " : " %-15.15s ",
6032 	      get_section_type_name (section->sh_type));
6033 
6034       if (is_32bit_elf)
6035 	{
6036 	  const char * link_too_big = NULL;
6037 
6038 	  print_vma (section->sh_addr, LONG_HEX);
6039 
6040 	  printf ( " %6.6lx %6.6lx %2.2lx",
6041 		   (unsigned long) section->sh_offset,
6042 		   (unsigned long) section->sh_size,
6043 		   (unsigned long) section->sh_entsize);
6044 
6045 	  if (do_section_details)
6046 	    fputs ("  ", stdout);
6047 	  else
6048 	    printf (" %3s ", get_elf_section_flags (section->sh_flags));
6049 
6050 	  if (section->sh_link >= elf_header.e_shnum)
6051 	    {
6052 	      link_too_big = "";
6053 	      /* The sh_link value is out of range.  Normally this indicates
6054 		 an error but it can have special values in Solaris binaries.  */
6055 	      switch (elf_header.e_machine)
6056 		{
6057 		case EM_386:
6058 		case EM_IAMCU:
6059 		case EM_X86_64:
6060 		case EM_L1OM:
6061 		case EM_K1OM:
6062 		case EM_OLD_SPARCV9:
6063 		case EM_SPARC32PLUS:
6064 		case EM_SPARCV9:
6065 		case EM_SPARC:
6066 		  if (section->sh_link == (SHN_BEFORE & 0xffff))
6067 		    link_too_big = "BEFORE";
6068 		  else if (section->sh_link == (SHN_AFTER & 0xffff))
6069 		    link_too_big = "AFTER";
6070 		  break;
6071 		default:
6072 		  break;
6073 		}
6074 	    }
6075 
6076 	  if (do_section_details)
6077 	    {
6078 	      if (link_too_big != NULL && * link_too_big)
6079 		printf ("<%s> ", link_too_big);
6080 	      else
6081 		printf ("%2u ", section->sh_link);
6082 	      printf ("%3u %2lu\n", section->sh_info,
6083 		      (unsigned long) section->sh_addralign);
6084 	    }
6085 	  else
6086 	    printf ("%2u %3u %2lu\n",
6087 		    section->sh_link,
6088 		    section->sh_info,
6089 		    (unsigned long) section->sh_addralign);
6090 
6091 	  if (link_too_big && ! * link_too_big)
6092 	    warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
6093 		  i, section->sh_link);
6094 	}
6095       else if (do_wide)
6096 	{
6097 	  print_vma (section->sh_addr, LONG_HEX);
6098 
6099 	  if ((long) section->sh_offset == section->sh_offset)
6100 	    printf (" %6.6lx", (unsigned long) section->sh_offset);
6101 	  else
6102 	    {
6103 	      putchar (' ');
6104 	      print_vma (section->sh_offset, LONG_HEX);
6105 	    }
6106 
6107 	  if ((unsigned long) section->sh_size == section->sh_size)
6108 	    printf (" %6.6lx", (unsigned long) section->sh_size);
6109 	  else
6110 	    {
6111 	      putchar (' ');
6112 	      print_vma (section->sh_size, LONG_HEX);
6113 	    }
6114 
6115 	  if ((unsigned long) section->sh_entsize == section->sh_entsize)
6116 	    printf (" %2.2lx", (unsigned long) section->sh_entsize);
6117 	  else
6118 	    {
6119 	      putchar (' ');
6120 	      print_vma (section->sh_entsize, LONG_HEX);
6121 	    }
6122 
6123 	  if (do_section_details)
6124 	    fputs ("  ", stdout);
6125 	  else
6126 	    printf (" %3s ", get_elf_section_flags (section->sh_flags));
6127 
6128 	  printf ("%2u %3u ", section->sh_link, section->sh_info);
6129 
6130 	  if ((unsigned long) section->sh_addralign == section->sh_addralign)
6131 	    printf ("%2lu\n", (unsigned long) section->sh_addralign);
6132 	  else
6133 	    {
6134 	      print_vma (section->sh_addralign, DEC);
6135 	      putchar ('\n');
6136 	    }
6137 	}
6138       else if (do_section_details)
6139 	{
6140 	  printf ("       %-15.15s  ",
6141 		  get_section_type_name (section->sh_type));
6142 	  print_vma (section->sh_addr, LONG_HEX);
6143 	  if ((long) section->sh_offset == section->sh_offset)
6144 	    printf ("  %16.16lx", (unsigned long) section->sh_offset);
6145 	  else
6146 	    {
6147 	      printf ("  ");
6148 	      print_vma (section->sh_offset, LONG_HEX);
6149 	    }
6150 	  printf ("  %u\n       ", section->sh_link);
6151 	  print_vma (section->sh_size, LONG_HEX);
6152 	  putchar (' ');
6153 	  print_vma (section->sh_entsize, LONG_HEX);
6154 
6155 	  printf ("  %-16u  %lu\n",
6156 		  section->sh_info,
6157 		  (unsigned long) section->sh_addralign);
6158 	}
6159       else
6160 	{
6161 	  putchar (' ');
6162 	  print_vma (section->sh_addr, LONG_HEX);
6163 	  if ((long) section->sh_offset == section->sh_offset)
6164 	    printf ("  %8.8lx", (unsigned long) section->sh_offset);
6165 	  else
6166 	    {
6167 	      printf ("  ");
6168 	      print_vma (section->sh_offset, LONG_HEX);
6169 	    }
6170 	  printf ("\n       ");
6171 	  print_vma (section->sh_size, LONG_HEX);
6172 	  printf ("  ");
6173 	  print_vma (section->sh_entsize, LONG_HEX);
6174 
6175 	  printf (" %3s ", get_elf_section_flags (section->sh_flags));
6176 
6177 	  printf ("     %2u   %3u     %lu\n",
6178 		  section->sh_link,
6179 		  section->sh_info,
6180 		  (unsigned long) section->sh_addralign);
6181 	}
6182 
6183       if (do_section_details)
6184 	{
6185 	  printf ("       %s\n", get_elf_section_flags (section->sh_flags));
6186 	  if ((section->sh_flags & SHF_COMPRESSED) != 0)
6187 	    {
6188 	      /* Minimum section size is 12 bytes for 32-bit compression
6189 		 header + 12 bytes for compressed data header.  */
6190 	      unsigned char buf[24];
6191 	      assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
6192 	      if (get_data (&buf, (FILE *) file, section->sh_offset, 1,
6193 			    sizeof (buf), _("compression header")))
6194 		{
6195 		  Elf_Internal_Chdr chdr;
6196 		  get_compression_header (&chdr, buf);
6197 		  if (chdr.ch_type == ELFCOMPRESS_ZLIB)
6198 		    printf ("       ZLIB, ");
6199 		  else
6200 		    printf (_("       [<unknown>: 0x%x], "),
6201 			    chdr.ch_type);
6202 		  print_vma (chdr.ch_size, LONG_HEX);
6203 		  printf (", %lu\n", (unsigned long) chdr.ch_addralign);
6204 		}
6205 	    }
6206 	}
6207     }
6208 
6209   if (!do_section_details)
6210     {
6211       /* The ordering of the letters shown here matches the ordering of the
6212 	 corresponding SHF_xxx values, and hence the order in which these
6213 	 letters will be displayed to the user.  */
6214       printf (_("Key to Flags:\n\
6215   W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
6216   L (link order), O (extra OS processing required), G (group), T (TLS),\n\
6217   C (compressed), x (unknown), o (OS specific), E (exclude),\n  "));
6218       if (elf_header.e_machine == EM_X86_64
6219 	  || elf_header.e_machine == EM_L1OM
6220 	  || elf_header.e_machine == EM_K1OM)
6221 	printf (_("l (large), "));
6222       else if (elf_header.e_machine == EM_ARM)
6223 	printf (_("y (noread), "));
6224       printf ("p (processor specific)\n");
6225     }
6226 
6227   return 1;
6228 }
6229 
6230 static const char *
get_group_flags(unsigned int flags)6231 get_group_flags (unsigned int flags)
6232 {
6233   static char buff[32];
6234   switch (flags)
6235     {
6236     case 0:
6237       return "";
6238 
6239     case GRP_COMDAT:
6240       return "COMDAT ";
6241 
6242    default:
6243       snprintf (buff, sizeof (buff), _("[<unknown>: 0x%x] "), flags);
6244       break;
6245     }
6246   return buff;
6247 }
6248 
6249 static int
process_section_groups(FILE * file)6250 process_section_groups (FILE * file)
6251 {
6252   Elf_Internal_Shdr * section;
6253   unsigned int i;
6254   struct group * group;
6255   Elf_Internal_Shdr * symtab_sec;
6256   Elf_Internal_Shdr * strtab_sec;
6257   Elf_Internal_Sym * symtab;
6258   unsigned long num_syms;
6259   char * strtab;
6260   size_t strtab_size;
6261 
6262   /* Don't process section groups unless needed.  */
6263   if (!do_unwind && !do_section_groups)
6264     return 1;
6265 
6266   if (elf_header.e_shnum == 0)
6267     {
6268       if (do_section_groups)
6269 	printf (_("\nThere are no sections to group in this file.\n"));
6270 
6271       return 1;
6272     }
6273 
6274   if (section_headers == NULL)
6275     {
6276       error (_("Section headers are not available!\n"));
6277       /* PR 13622: This can happen with a corrupt ELF header.  */
6278       return 0;
6279     }
6280 
6281   section_headers_groups = (struct group **) calloc (elf_header.e_shnum,
6282                                                      sizeof (struct group *));
6283 
6284   if (section_headers_groups == NULL)
6285     {
6286       error (_("Out of memory reading %u section group headers\n"),
6287 	     elf_header.e_shnum);
6288       return 0;
6289     }
6290 
6291   /* Scan the sections for the group section.  */
6292   group_count = 0;
6293   for (i = 0, section = section_headers;
6294        i < elf_header.e_shnum;
6295        i++, section++)
6296     if (section->sh_type == SHT_GROUP)
6297       group_count++;
6298 
6299   if (group_count == 0)
6300     {
6301       if (do_section_groups)
6302 	printf (_("\nThere are no section groups in this file.\n"));
6303 
6304       return 1;
6305     }
6306 
6307   section_groups = (struct group *) calloc (group_count, sizeof (struct group));
6308 
6309   if (section_groups == NULL)
6310     {
6311       error (_("Out of memory reading %lu groups\n"),
6312 	     (unsigned long) group_count);
6313       return 0;
6314     }
6315 
6316   symtab_sec = NULL;
6317   strtab_sec = NULL;
6318   symtab = NULL;
6319   num_syms = 0;
6320   strtab = NULL;
6321   strtab_size = 0;
6322   for (i = 0, section = section_headers, group = section_groups;
6323        i < elf_header.e_shnum;
6324        i++, section++)
6325     {
6326       if (section->sh_type == SHT_GROUP)
6327 	{
6328 	  const char * name = printable_section_name (section);
6329 	  const char * group_name;
6330 	  unsigned char * start;
6331 	  unsigned char * indices;
6332 	  unsigned int entry, j, size;
6333 	  Elf_Internal_Shdr * sec;
6334 	  Elf_Internal_Sym * sym;
6335 
6336 	  /* Get the symbol table.  */
6337 	  if (section->sh_link >= elf_header.e_shnum
6338 	      || ((sec = section_headers + section->sh_link)->sh_type
6339 		  != SHT_SYMTAB))
6340 	    {
6341 	      error (_("Bad sh_link in group section `%s'\n"), name);
6342 	      continue;
6343 	    }
6344 
6345 	  if (symtab_sec != sec)
6346 	    {
6347 	      symtab_sec = sec;
6348 	      if (symtab)
6349 		free (symtab);
6350 	      symtab = GET_ELF_SYMBOLS (file, symtab_sec, & num_syms);
6351 	    }
6352 
6353 	  if (symtab == NULL)
6354 	    {
6355 	      error (_("Corrupt header in group section `%s'\n"), name);
6356 	      continue;
6357 	    }
6358 
6359 	  if (section->sh_info >= num_syms)
6360 	    {
6361 	      error (_("Bad sh_info in group section `%s'\n"), name);
6362 	      continue;
6363 	    }
6364 
6365 	  sym = symtab + section->sh_info;
6366 
6367 	  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
6368 	    {
6369 	      if (sym->st_shndx == 0
6370 		  || sym->st_shndx >= elf_header.e_shnum)
6371 		{
6372 		  error (_("Bad sh_info in group section `%s'\n"), name);
6373 		  continue;
6374 		}
6375 
6376 	      group_name = SECTION_NAME (section_headers + sym->st_shndx);
6377 	      strtab_sec = NULL;
6378 	      if (strtab)
6379 		free (strtab);
6380 	      strtab = NULL;
6381 	      strtab_size = 0;
6382 	    }
6383 	  else
6384 	    {
6385 	      /* Get the string table.  */
6386 	      if (symtab_sec->sh_link >= elf_header.e_shnum)
6387 		{
6388 		  strtab_sec = NULL;
6389 		  if (strtab)
6390 		    free (strtab);
6391 		  strtab = NULL;
6392 		  strtab_size = 0;
6393 		}
6394 	      else if (strtab_sec
6395 		       != (sec = section_headers + symtab_sec->sh_link))
6396 		{
6397 		  strtab_sec = sec;
6398 		  if (strtab)
6399 		    free (strtab);
6400 
6401 		  strtab = (char *) get_data (NULL, file, strtab_sec->sh_offset,
6402 					      1, strtab_sec->sh_size,
6403 					      _("string table"));
6404 		  strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
6405 		}
6406 	      group_name = sym->st_name < strtab_size
6407 		? strtab + sym->st_name : _("<corrupt>");
6408 	    }
6409 
6410 	  /* PR 17531: file: loop.  */
6411 	  if (section->sh_entsize > section->sh_size)
6412 	    {
6413 	      error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
6414 		     printable_section_name (section),
6415 		     (unsigned long) section->sh_entsize,
6416 		     (unsigned long) section->sh_size);
6417 	      break;
6418 	    }
6419 
6420 	  start = (unsigned char *) get_data (NULL, file, section->sh_offset,
6421                                               1, section->sh_size,
6422                                               _("section data"));
6423 	  if (start == NULL)
6424 	    continue;
6425 
6426 	  indices = start;
6427 	  size = (section->sh_size / section->sh_entsize) - 1;
6428 	  entry = byte_get (indices, 4);
6429 	  indices += 4;
6430 
6431 	  if (do_section_groups)
6432 	    {
6433 	      printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
6434 		      get_group_flags (entry), i, name, group_name, size);
6435 
6436 	      printf (_("   [Index]    Name\n"));
6437 	    }
6438 
6439 	  group->group_index = i;
6440 
6441 	  for (j = 0; j < size; j++)
6442 	    {
6443 	      struct group_list * g;
6444 
6445 	      entry = byte_get (indices, 4);
6446 	      indices += 4;
6447 
6448 	      if (entry >= elf_header.e_shnum)
6449 		{
6450 		  static unsigned num_group_errors = 0;
6451 
6452 		  if (num_group_errors ++ < 10)
6453 		    {
6454 		      error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
6455 			     entry, i, elf_header.e_shnum - 1);
6456 		      if (num_group_errors == 10)
6457 			warn (_("Futher error messages about overlarge group section indicies suppressed\n"));
6458 		    }
6459 		  continue;
6460 		}
6461 
6462 	      if (section_headers_groups [entry] != NULL)
6463 		{
6464 		  if (entry)
6465 		    {
6466 		      static unsigned num_errs = 0;
6467 
6468 		      if (num_errs ++ < 10)
6469 			{
6470 			  error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
6471 				 entry, i,
6472 				 section_headers_groups [entry]->group_index);
6473 			  if (num_errs == 10)
6474 			    warn (_("Further error messages about already contained group sections suppressed\n"));
6475 			}
6476 		      continue;
6477 		    }
6478 		  else
6479 		    {
6480 		      /* Intel C/C++ compiler may put section 0 in a
6481 			 section group. We just warn it the first time
6482 			 and ignore it afterwards.  */
6483 		      static int warned = 0;
6484 		      if (!warned)
6485 			{
6486 			  error (_("section 0 in group section [%5u]\n"),
6487 				 section_headers_groups [entry]->group_index);
6488 			  warned++;
6489 			}
6490 		    }
6491 		}
6492 
6493 	      section_headers_groups [entry] = group;
6494 
6495 	      if (do_section_groups)
6496 		{
6497 		  sec = section_headers + entry;
6498 		  printf ("   [%5u]   %s\n", entry, printable_section_name (sec));
6499 		}
6500 
6501 	      g = (struct group_list *) xmalloc (sizeof (struct group_list));
6502 	      g->section_index = entry;
6503 	      g->next = group->root;
6504 	      group->root = g;
6505 	    }
6506 
6507 	  if (start)
6508 	    free (start);
6509 
6510 	  group++;
6511 	}
6512     }
6513 
6514   if (symtab)
6515     free (symtab);
6516   if (strtab)
6517     free (strtab);
6518   return 1;
6519 }
6520 
6521 /* Data used to display dynamic fixups.  */
6522 
6523 struct ia64_vms_dynfixup
6524 {
6525   bfd_vma needed_ident;		/* Library ident number.  */
6526   bfd_vma needed;		/* Index in the dstrtab of the library name.  */
6527   bfd_vma fixup_needed;		/* Index of the library.  */
6528   bfd_vma fixup_rela_cnt;	/* Number of fixups.  */
6529   bfd_vma fixup_rela_off;	/* Fixups offset in the dynamic segment.  */
6530 };
6531 
6532 /* Data used to display dynamic relocations.  */
6533 
6534 struct ia64_vms_dynimgrela
6535 {
6536   bfd_vma img_rela_cnt;		/* Number of relocations.  */
6537   bfd_vma img_rela_off;		/* Reloc offset in the dynamic segment.  */
6538 };
6539 
6540 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
6541    library).  */
6542 
6543 static void
dump_ia64_vms_dynamic_fixups(FILE * file,struct ia64_vms_dynfixup * fixup,const char * strtab,unsigned int strtab_sz)6544 dump_ia64_vms_dynamic_fixups (FILE *file, struct ia64_vms_dynfixup *fixup,
6545                               const char *strtab, unsigned int strtab_sz)
6546 {
6547   Elf64_External_VMS_IMAGE_FIXUP *imfs;
6548   long i;
6549   const char *lib_name;
6550 
6551   imfs = get_data (NULL, file, dynamic_addr + fixup->fixup_rela_off,
6552 		   1, fixup->fixup_rela_cnt * sizeof (*imfs),
6553 		   _("dynamic section image fixups"));
6554   if (!imfs)
6555     return;
6556 
6557   if (fixup->needed < strtab_sz)
6558     lib_name = strtab + fixup->needed;
6559   else
6560     {
6561       warn ("corrupt library name index of 0x%lx found in dynamic entry",
6562             (unsigned long) fixup->needed);
6563       lib_name = "???";
6564     }
6565   printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
6566 	  (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
6567   printf
6568     (_("Seg Offset           Type                             SymVec DataType\n"));
6569 
6570   for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
6571     {
6572       unsigned int type;
6573       const char *rtype;
6574 
6575       printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
6576       printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
6577       type = BYTE_GET (imfs [i].type);
6578       rtype = elf_ia64_reloc_type (type);
6579       if (rtype == NULL)
6580         printf (" 0x%08x                       ", type);
6581       else
6582         printf (" %-32s ", rtype);
6583       printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
6584       printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
6585     }
6586 
6587   free (imfs);
6588 }
6589 
6590 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image).  */
6591 
6592 static void
dump_ia64_vms_dynamic_relocs(FILE * file,struct ia64_vms_dynimgrela * imgrela)6593 dump_ia64_vms_dynamic_relocs (FILE *file, struct ia64_vms_dynimgrela *imgrela)
6594 {
6595   Elf64_External_VMS_IMAGE_RELA *imrs;
6596   long i;
6597 
6598   imrs = get_data (NULL, file, dynamic_addr + imgrela->img_rela_off,
6599 		   1, imgrela->img_rela_cnt * sizeof (*imrs),
6600 		   _("dynamic section image relocations"));
6601   if (!imrs)
6602     return;
6603 
6604   printf (_("\nImage relocs\n"));
6605   printf
6606     (_("Seg Offset   Type                            Addend            Seg Sym Off\n"));
6607 
6608   for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
6609     {
6610       unsigned int type;
6611       const char *rtype;
6612 
6613       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
6614       printf ("%08" BFD_VMA_FMT "x ",
6615               (bfd_vma) BYTE_GET (imrs [i].rela_offset));
6616       type = BYTE_GET (imrs [i].type);
6617       rtype = elf_ia64_reloc_type (type);
6618       if (rtype == NULL)
6619         printf ("0x%08x                      ", type);
6620       else
6621         printf ("%-31s ", rtype);
6622       print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
6623       printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
6624       printf ("%08" BFD_VMA_FMT "x\n",
6625               (bfd_vma) BYTE_GET (imrs [i].sym_offset));
6626     }
6627 
6628   free (imrs);
6629 }
6630 
6631 /* Display IA-64 OpenVMS dynamic relocations and fixups.  */
6632 
6633 static int
process_ia64_vms_dynamic_relocs(FILE * file)6634 process_ia64_vms_dynamic_relocs (FILE *file)
6635 {
6636   struct ia64_vms_dynfixup fixup;
6637   struct ia64_vms_dynimgrela imgrela;
6638   Elf_Internal_Dyn *entry;
6639   int res = 0;
6640   bfd_vma strtab_off = 0;
6641   bfd_vma strtab_sz = 0;
6642   char *strtab = NULL;
6643 
6644   memset (&fixup, 0, sizeof (fixup));
6645   memset (&imgrela, 0, sizeof (imgrela));
6646 
6647   /* Note: the order of the entries is specified by the OpenVMS specs.  */
6648   for (entry = dynamic_section;
6649        entry < dynamic_section + dynamic_nent;
6650        entry++)
6651     {
6652       switch (entry->d_tag)
6653         {
6654         case DT_IA_64_VMS_STRTAB_OFFSET:
6655           strtab_off = entry->d_un.d_val;
6656           break;
6657         case DT_STRSZ:
6658           strtab_sz = entry->d_un.d_val;
6659           if (strtab == NULL)
6660             strtab = get_data (NULL, file, dynamic_addr + strtab_off,
6661                                1, strtab_sz, _("dynamic string section"));
6662           break;
6663 
6664         case DT_IA_64_VMS_NEEDED_IDENT:
6665           fixup.needed_ident = entry->d_un.d_val;
6666           break;
6667         case DT_NEEDED:
6668           fixup.needed = entry->d_un.d_val;
6669           break;
6670         case DT_IA_64_VMS_FIXUP_NEEDED:
6671           fixup.fixup_needed = entry->d_un.d_val;
6672           break;
6673         case DT_IA_64_VMS_FIXUP_RELA_CNT:
6674           fixup.fixup_rela_cnt = entry->d_un.d_val;
6675           break;
6676         case DT_IA_64_VMS_FIXUP_RELA_OFF:
6677           fixup.fixup_rela_off = entry->d_un.d_val;
6678           res++;
6679           dump_ia64_vms_dynamic_fixups (file, &fixup, strtab, strtab_sz);
6680           break;
6681 
6682         case DT_IA_64_VMS_IMG_RELA_CNT:
6683 	  imgrela.img_rela_cnt = entry->d_un.d_val;
6684           break;
6685         case DT_IA_64_VMS_IMG_RELA_OFF:
6686 	  imgrela.img_rela_off = entry->d_un.d_val;
6687           res++;
6688           dump_ia64_vms_dynamic_relocs (file, &imgrela);
6689           break;
6690 
6691         default:
6692           break;
6693 	}
6694     }
6695 
6696   if (strtab != NULL)
6697     free (strtab);
6698 
6699   return res;
6700 }
6701 
6702 static struct
6703 {
6704   const char * name;
6705   int reloc;
6706   int size;
6707   int rela;
6708   int relr;
6709 } dynamic_relocations [] =
6710 {
6711     { "REL", DT_REL, DT_RELSZ, FALSE, FALSE },
6712     { "RELA", DT_RELA, DT_RELASZ, TRUE, FALSE },
6713     { "RELR", DT_RELR, DT_RELRSZ, FALSE, TRUE },
6714     { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN, FALSE }
6715 };
6716 
6717 /* Process the reloc section.  */
6718 
6719 static int
process_relocs(FILE * file)6720 process_relocs (FILE * file)
6721 {
6722   unsigned long rel_size;
6723   unsigned long rel_offset;
6724 
6725 
6726   if (!do_reloc)
6727     return 1;
6728 
6729   if (do_using_dynamic)
6730     {
6731       int is_rela, is_relr;
6732       const char * name;
6733       int has_dynamic_reloc;
6734       unsigned int i;
6735 
6736       has_dynamic_reloc = 0;
6737 
6738       for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
6739 	{
6740 	  is_rela = dynamic_relocations [i].rela;
6741 	  is_relr = dynamic_relocations [i].relr;
6742 	  name = dynamic_relocations [i].name;
6743 	  rel_size = dynamic_info [dynamic_relocations [i].size];
6744 	  rel_offset = dynamic_info [dynamic_relocations [i].reloc];
6745 
6746 	  has_dynamic_reloc |= rel_size;
6747 
6748 	  if (is_rela == UNKNOWN)
6749 	    {
6750 	      if (dynamic_relocations [i].reloc == DT_JMPREL)
6751 		switch (dynamic_info[DT_PLTREL])
6752 		  {
6753 		  case DT_REL:
6754 		    is_rela = FALSE;
6755 		    break;
6756 		  case DT_RELA:
6757 		    is_rela = TRUE;
6758 		    break;
6759 		  }
6760 	    }
6761 
6762 	  if (rel_size)
6763 	    {
6764 	      printf
6765 		(_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
6766 		 name, rel_offset, rel_size);
6767 
6768 	      dump_relocations (file,
6769 				offset_from_vma (file, rel_offset, rel_size),
6770 				rel_size,
6771 				dynamic_symbols, num_dynamic_syms,
6772 				dynamic_strings, dynamic_strings_length,
6773 				is_rela, is_relr, 1);
6774 	    }
6775 	}
6776 
6777       if (is_ia64_vms ())
6778         has_dynamic_reloc |= process_ia64_vms_dynamic_relocs (file);
6779 
6780       if (! has_dynamic_reloc)
6781 	printf (_("\nThere are no dynamic relocations in this file.\n"));
6782     }
6783   else
6784     {
6785       Elf_Internal_Shdr * section;
6786       unsigned long i;
6787       int found = 0;
6788 
6789       for (i = 0, section = section_headers;
6790 	   i < elf_header.e_shnum;
6791 	   i++, section++)
6792 	{
6793 	  if (   section->sh_type != SHT_RELA
6794 	      && section->sh_type != SHT_REL
6795 	      && section->sh_type != SHT_RELR)
6796 	    continue;
6797 
6798 	  rel_offset = section->sh_offset;
6799 	  rel_size   = section->sh_size;
6800 
6801 	  if (rel_size)
6802 	    {
6803 	      Elf_Internal_Shdr * strsec;
6804 	      int is_rela, is_relr;
6805 
6806 	      printf (_("\nRelocation section "));
6807 
6808 	      if (string_table == NULL)
6809 		printf ("%d", section->sh_name);
6810 	      else
6811 		printf ("'%s'", printable_section_name (section));
6812 
6813 	      printf (_(" at offset 0x%lx contains %lu entries:\n"),
6814 		 rel_offset, (unsigned long) (rel_size / section->sh_entsize));
6815 
6816 	      is_rela = section->sh_type == SHT_RELA;
6817 	      is_relr = section->sh_type == SHT_RELR;
6818 
6819 	      if (section->sh_link != 0
6820 		  && section->sh_link < elf_header.e_shnum)
6821 		{
6822 		  Elf_Internal_Shdr * symsec;
6823 		  Elf_Internal_Sym *  symtab;
6824 		  unsigned long nsyms;
6825 		  unsigned long strtablen = 0;
6826 		  char * strtab = NULL;
6827 
6828 		  symsec = section_headers + section->sh_link;
6829 		  if (symsec->sh_type != SHT_SYMTAB
6830 		      && symsec->sh_type != SHT_DYNSYM)
6831                     continue;
6832 
6833 		  symtab = GET_ELF_SYMBOLS (file, symsec, & nsyms);
6834 
6835 		  if (symtab == NULL)
6836 		    continue;
6837 
6838 		  if (symsec->sh_link != 0
6839 		      && symsec->sh_link < elf_header.e_shnum)
6840 		    {
6841 		      strsec = section_headers + symsec->sh_link;
6842 
6843 		      strtab = (char *) get_data (NULL, file, strsec->sh_offset,
6844 						  1, strsec->sh_size,
6845 						  _("string table"));
6846 		      strtablen = strtab == NULL ? 0 : strsec->sh_size;
6847 		    }
6848 
6849 		  dump_relocations (file, rel_offset, rel_size,
6850 				    symtab, nsyms, strtab, strtablen,
6851 				    is_rela, is_relr,
6852 				    symsec->sh_type == SHT_DYNSYM);
6853 		  if (strtab)
6854 		    free (strtab);
6855 		  free (symtab);
6856 		}
6857 	      else
6858 		dump_relocations (file, rel_offset, rel_size,
6859 				  NULL, 0, NULL, 0,
6860 				  is_rela, is_relr,
6861 				  0);
6862 
6863 	      found = 1;
6864 	    }
6865 	}
6866 
6867       if (! found)
6868 	printf (_("\nThere are no relocations in this file.\n"));
6869     }
6870 
6871   return 1;
6872 }
6873 
6874 /* An absolute address consists of a section and an offset.  If the
6875    section is NULL, the offset itself is the address, otherwise, the
6876    address equals to LOAD_ADDRESS(section) + offset.  */
6877 
6878 struct absaddr
6879 {
6880   unsigned short section;
6881   bfd_vma offset;
6882 };
6883 
6884 #define ABSADDR(a) \
6885   ((a).section \
6886    ? section_headers [(a).section].sh_addr + (a).offset \
6887    : (a).offset)
6888 
6889 /* Find the nearest symbol at or below ADDR.  Returns the symbol
6890    name, if found, and the offset from the symbol to ADDR.  */
6891 
6892 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)6893 find_symbol_for_address (Elf_Internal_Sym * symtab,
6894 			 unsigned long      nsyms,
6895 			 const char *       strtab,
6896 			 unsigned long      strtab_size,
6897 			 struct absaddr     addr,
6898 			 const char **      symname,
6899 			 bfd_vma *          offset)
6900 {
6901   bfd_vma dist = 0x100000;
6902   Elf_Internal_Sym * sym;
6903   Elf_Internal_Sym * beg;
6904   Elf_Internal_Sym * end;
6905   Elf_Internal_Sym * best = NULL;
6906 
6907   REMOVE_ARCH_BITS (addr.offset);
6908   beg = symtab;
6909   end = symtab + nsyms;
6910 
6911   while (beg < end)
6912     {
6913       bfd_vma value;
6914 
6915       sym = beg + (end - beg) / 2;
6916 
6917       value = sym->st_value;
6918       REMOVE_ARCH_BITS (value);
6919 
6920       if (sym->st_name != 0
6921 	  && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
6922 	  && addr.offset >= value
6923 	  && addr.offset - value < dist)
6924 	{
6925 	  best = sym;
6926 	  dist = addr.offset - value;
6927 	  if (!dist)
6928 	    break;
6929 	}
6930 
6931       if (addr.offset < value)
6932 	end = sym;
6933       else
6934 	beg = sym + 1;
6935     }
6936 
6937   if (best)
6938     {
6939       *symname = (best->st_name >= strtab_size
6940 		  ? _("<corrupt>") : strtab + best->st_name);
6941       *offset = dist;
6942       return;
6943     }
6944 
6945   *symname = NULL;
6946   *offset = addr.offset;
6947 }
6948 
6949 static int
symcmp(const void * p,const void * q)6950 symcmp (const void *p, const void *q)
6951 {
6952   Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
6953   Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
6954 
6955   return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
6956 }
6957 
6958 /* Process the unwind section.  */
6959 
6960 #include "unwind-ia64.h"
6961 
6962 struct ia64_unw_table_entry
6963 {
6964   struct absaddr start;
6965   struct absaddr end;
6966   struct absaddr info;
6967 };
6968 
6969 struct ia64_unw_aux_info
6970 {
6971   struct ia64_unw_table_entry *table;	/* Unwind table.  */
6972   unsigned long table_len;		/* Length of unwind table.  */
6973   unsigned char * info;			/* Unwind info.  */
6974   unsigned long info_size;		/* Size of unwind info.  */
6975   bfd_vma info_addr;			/* Starting address of unwind info.  */
6976   bfd_vma seg_base;			/* Starting address of segment.  */
6977   Elf_Internal_Sym * symtab;		/* The symbol table.  */
6978   unsigned long nsyms;			/* Number of symbols.  */
6979   Elf_Internal_Sym * funtab;		/* Sorted table of STT_FUNC symbols.  */
6980   unsigned long nfuns;			/* Number of entries in funtab.  */
6981   char * strtab;			/* The string table.  */
6982   unsigned long strtab_size;		/* Size of string table.  */
6983 };
6984 
6985 static void
dump_ia64_unwind(struct ia64_unw_aux_info * aux)6986 dump_ia64_unwind (struct ia64_unw_aux_info * aux)
6987 {
6988   struct ia64_unw_table_entry * tp;
6989   unsigned long j, nfuns;
6990   int in_body;
6991 
6992   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
6993   for (nfuns = 0, j = 0; j < aux->nsyms; j++)
6994     if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
6995       aux->funtab[nfuns++] = aux->symtab[j];
6996   aux->nfuns = nfuns;
6997   qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
6998 
6999   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7000     {
7001       bfd_vma stamp;
7002       bfd_vma offset;
7003       const unsigned char * dp;
7004       const unsigned char * head;
7005       const unsigned char * end;
7006       const char * procname;
7007 
7008       find_symbol_for_address (aux->funtab, aux->nfuns, aux->strtab,
7009 			       aux->strtab_size, tp->start, &procname, &offset);
7010 
7011       fputs ("\n<", stdout);
7012 
7013       if (procname)
7014 	{
7015 	  fputs (procname, stdout);
7016 
7017 	  if (offset)
7018 	    printf ("+%lx", (unsigned long) offset);
7019 	}
7020 
7021       fputs (">: [", stdout);
7022       print_vma (tp->start.offset, PREFIX_HEX);
7023       fputc ('-', stdout);
7024       print_vma (tp->end.offset, PREFIX_HEX);
7025       printf ("], info at +0x%lx\n",
7026 	      (unsigned long) (tp->info.offset - aux->seg_base));
7027 
7028       /* PR 17531: file: 86232b32.  */
7029       if (aux->info == NULL)
7030 	continue;
7031 
7032       /* PR 17531: file: 0997b4d1.  */
7033       if ((ABSADDR (tp->info) - aux->info_addr) >= aux->info_size)
7034 	{
7035 	  warn (_("Invalid offset %lx in table entry %ld\n"),
7036 		(long) tp->info.offset, (long) (tp - aux->table));
7037 	  continue;
7038 	}
7039 
7040       head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
7041       stamp = byte_get ((unsigned char *) head, sizeof (stamp));
7042 
7043       printf ("  v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
7044 	      (unsigned) UNW_VER (stamp),
7045 	      (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
7046 	      UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
7047 	      UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
7048 	      (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
7049 
7050       if (UNW_VER (stamp) != 1)
7051 	{
7052 	  printf (_("\tUnknown version.\n"));
7053 	  continue;
7054 	}
7055 
7056       in_body = 0;
7057       end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
7058       /* PR 17531: file: 16ceda89.  */
7059       if (end > aux->info + aux->info_size)
7060 	end = aux->info + aux->info_size;
7061       for (dp = head + 8; dp < end;)
7062 	dp = unw_decode (dp, in_body, & in_body, end);
7063     }
7064 
7065   free (aux->funtab);
7066 }
7067 
7068 static bfd_boolean
slurp_ia64_unwind_table(FILE * file,struct ia64_unw_aux_info * aux,Elf_Internal_Shdr * sec)7069 slurp_ia64_unwind_table (FILE * file,
7070 			 struct ia64_unw_aux_info * aux,
7071 			 Elf_Internal_Shdr * sec)
7072 {
7073   unsigned long size, nrelas, i;
7074   Elf_Internal_Phdr * seg;
7075   struct ia64_unw_table_entry * tep;
7076   Elf_Internal_Shdr * relsec;
7077   Elf_Internal_Rela * rela;
7078   Elf_Internal_Rela * rp;
7079   unsigned char * table;
7080   unsigned char * tp;
7081   Elf_Internal_Sym * sym;
7082   const char * relname;
7083 
7084   aux->table_len = 0;
7085 
7086   /* First, find the starting address of the segment that includes
7087      this section: */
7088 
7089   if (elf_header.e_phnum)
7090     {
7091       if (! get_program_headers (file))
7092 	  return FALSE;
7093 
7094       for (seg = program_headers;
7095 	   seg < program_headers + elf_header.e_phnum;
7096 	   ++seg)
7097 	{
7098 	  if (seg->p_type != PT_LOAD)
7099 	    continue;
7100 
7101 	  if (sec->sh_addr >= seg->p_vaddr
7102 	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
7103 	    {
7104 	      aux->seg_base = seg->p_vaddr;
7105 	      break;
7106 	    }
7107 	}
7108     }
7109 
7110   /* Second, build the unwind table from the contents of the unwind section:  */
7111   size = sec->sh_size;
7112   table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
7113                                       _("unwind table"));
7114   if (!table)
7115     return FALSE;
7116 
7117   aux->table_len = size / (3 * eh_addr_size);
7118   aux->table = (struct ia64_unw_table_entry *)
7119     xcmalloc (aux->table_len, sizeof (aux->table[0]));
7120   tep = aux->table;
7121 
7122   for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
7123     {
7124       tep->start.section = SHN_UNDEF;
7125       tep->end.section   = SHN_UNDEF;
7126       tep->info.section  = SHN_UNDEF;
7127       tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7128       tep->end.offset   = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7129       tep->info.offset  = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7130       tep->start.offset += aux->seg_base;
7131       tep->end.offset   += aux->seg_base;
7132       tep->info.offset  += aux->seg_base;
7133     }
7134   free (table);
7135 
7136   /* Third, apply any relocations to the unwind table:  */
7137   for (relsec = section_headers;
7138        relsec < section_headers + elf_header.e_shnum;
7139        ++relsec)
7140     {
7141       if (relsec->sh_type != SHT_RELA
7142 	  || relsec->sh_info >= elf_header.e_shnum
7143 	  || section_headers + relsec->sh_info != sec)
7144 	continue;
7145 
7146       if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
7147 			      & rela, & nrelas))
7148 	{
7149 	  free (aux->table);
7150 	  aux->table = NULL;
7151 	  aux->table_len = 0;
7152 	  return FALSE;
7153 	}
7154 
7155       for (rp = rela; rp < rela + nrelas; ++rp)
7156 	{
7157 	  relname = elf_ia64_reloc_type (get_reloc_type (rp->r_info));
7158 	  sym = aux->symtab + get_reloc_symindex (rp->r_info);
7159 
7160 	  /* PR 17531: file: 9fa67536.  */
7161 	  if (relname == NULL)
7162 	    {
7163 	      warn (_("Skipping unknown relocation type: %u\n"), get_reloc_type (rp->r_info));
7164 	      continue;
7165 	    }
7166 
7167 	  if (! const_strneq (relname, "R_IA64_SEGREL"))
7168 	    {
7169 	      warn (_("Skipping unexpected relocation type: %s\n"), relname);
7170 	      continue;
7171 	    }
7172 
7173 	  i = rp->r_offset / (3 * eh_addr_size);
7174 
7175 	  /* PR 17531: file: 5bc8d9bf.  */
7176 	  if (i >= aux->table_len)
7177 	    {
7178 	      warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
7179 	      continue;
7180 	    }
7181 
7182 	  switch (rp->r_offset / eh_addr_size % 3)
7183 	    {
7184 	    case 0:
7185 	      aux->table[i].start.section = sym->st_shndx;
7186 	      aux->table[i].start.offset  = rp->r_addend + sym->st_value;
7187 	      break;
7188 	    case 1:
7189 	      aux->table[i].end.section   = sym->st_shndx;
7190 	      aux->table[i].end.offset    = rp->r_addend + sym->st_value;
7191 	      break;
7192 	    case 2:
7193 	      aux->table[i].info.section  = sym->st_shndx;
7194 	      aux->table[i].info.offset   = rp->r_addend + sym->st_value;
7195 	      break;
7196 	    default:
7197 	      break;
7198 	    }
7199 	}
7200 
7201       free (rela);
7202     }
7203 
7204   return TRUE;
7205 }
7206 
7207 static void
ia64_process_unwind(FILE * file)7208 ia64_process_unwind (FILE * file)
7209 {
7210   Elf_Internal_Shdr * sec;
7211   Elf_Internal_Shdr * unwsec = NULL;
7212   Elf_Internal_Shdr * strsec;
7213   unsigned long i, unwcount = 0, unwstart = 0;
7214   struct ia64_unw_aux_info aux;
7215 
7216   memset (& aux, 0, sizeof (aux));
7217 
7218   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7219     {
7220       if (sec->sh_type == SHT_SYMTAB
7221 	  && sec->sh_link < elf_header.e_shnum)
7222 	{
7223 	  aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
7224 
7225 	  strsec = section_headers + sec->sh_link;
7226 	  if (aux.strtab != NULL)
7227 	    {
7228 	      error (_("Multiple auxillary string tables encountered\n"));
7229 	      free (aux.strtab);
7230 	    }
7231 	  aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
7232                                           1, strsec->sh_size,
7233                                           _("string table"));
7234 	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
7235 	}
7236       else if (sec->sh_type == SHT_IA_64_UNWIND)
7237 	unwcount++;
7238     }
7239 
7240   if (!unwcount)
7241     printf (_("\nThere are no unwind sections in this file.\n"));
7242 
7243   while (unwcount-- > 0)
7244     {
7245       char * suffix;
7246       size_t len, len2;
7247 
7248       for (i = unwstart, sec = section_headers + unwstart, unwsec = NULL;
7249 	   i < elf_header.e_shnum; ++i, ++sec)
7250 	if (sec->sh_type == SHT_IA_64_UNWIND)
7251 	  {
7252 	    unwsec = sec;
7253 	    break;
7254 	  }
7255       /* We have already counted the number of SHT_IA64_UNWIND
7256 	 sections so the loop above should never fail.  */
7257       assert (unwsec != NULL);
7258 
7259       unwstart = i + 1;
7260       len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
7261 
7262       if ((unwsec->sh_flags & SHF_GROUP) != 0)
7263 	{
7264 	  /* We need to find which section group it is in.  */
7265 	  struct group_list * g;
7266 
7267 	  if (section_headers_groups == NULL
7268 	      || section_headers_groups [i] == NULL)
7269 	    i = elf_header.e_shnum;
7270 	  else
7271 	    {
7272 	      g = section_headers_groups [i]->root;
7273 
7274 	      for (; g != NULL; g = g->next)
7275 		{
7276 		  sec = section_headers + g->section_index;
7277 
7278 		  if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
7279 		    break;
7280 		}
7281 
7282 	      if (g == NULL)
7283 		i = elf_header.e_shnum;
7284 	    }
7285 	}
7286       else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
7287 	{
7288 	  /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO.  */
7289 	  len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
7290 	  suffix = SECTION_NAME (unwsec) + len;
7291 	  for (i = 0, sec = section_headers; i < elf_header.e_shnum;
7292 	       ++i, ++sec)
7293 	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
7294 		&& streq (SECTION_NAME (sec) + len2, suffix))
7295 	      break;
7296 	}
7297       else
7298 	{
7299 	  /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
7300 	     .IA_64.unwind or BAR -> .IA_64.unwind_info.  */
7301 	  len = sizeof (ELF_STRING_ia64_unwind) - 1;
7302 	  len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
7303 	  suffix = "";
7304 	  if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
7305 	    suffix = SECTION_NAME (unwsec) + len;
7306 	  for (i = 0, sec = section_headers; i < elf_header.e_shnum;
7307 	       ++i, ++sec)
7308 	    if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
7309 		&& streq (SECTION_NAME (sec) + len2, suffix))
7310 	      break;
7311 	}
7312 
7313       if (i == elf_header.e_shnum)
7314 	{
7315 	  printf (_("\nCould not find unwind info section for "));
7316 
7317 	  if (string_table == NULL)
7318 	    printf ("%d", unwsec->sh_name);
7319 	  else
7320 	    printf ("'%s'", printable_section_name (unwsec));
7321 	}
7322       else
7323 	{
7324 	  aux.info_addr = sec->sh_addr;
7325 	  aux.info = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1,
7326 						 sec->sh_size,
7327 						 _("unwind info"));
7328 	  aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
7329 
7330 	  printf (_("\nUnwind section "));
7331 
7332 	  if (string_table == NULL)
7333 	    printf ("%d", unwsec->sh_name);
7334 	  else
7335 	    printf ("'%s'", printable_section_name (unwsec));
7336 
7337 	  printf (_(" at offset 0x%lx contains %lu entries:\n"),
7338 		  (unsigned long) unwsec->sh_offset,
7339 		  (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
7340 
7341 	  if (slurp_ia64_unwind_table (file, & aux, unwsec)
7342 	      && aux.table_len > 0)
7343 	    dump_ia64_unwind (& aux);
7344 
7345 	  if (aux.table)
7346 	    free ((char *) aux.table);
7347 	  if (aux.info)
7348 	    free ((char *) aux.info);
7349 	  aux.table = NULL;
7350 	  aux.info = NULL;
7351 	}
7352     }
7353 
7354   if (aux.symtab)
7355     free (aux.symtab);
7356   if (aux.strtab)
7357     free ((char *) aux.strtab);
7358 }
7359 
7360 struct hppa_unw_table_entry
7361   {
7362     struct absaddr start;
7363     struct absaddr end;
7364     unsigned int Cannot_unwind:1;		/* 0 */
7365     unsigned int Millicode:1;			/* 1 */
7366     unsigned int Millicode_save_sr0:1;		/* 2 */
7367     unsigned int Region_description:2;		/* 3..4 */
7368     unsigned int reserved1:1;			/* 5 */
7369     unsigned int Entry_SR:1;			/* 6 */
7370     unsigned int Entry_FR:4;     /* number saved */	/* 7..10 */
7371     unsigned int Entry_GR:5;     /* number saved */	/* 11..15 */
7372     unsigned int Args_stored:1;			/* 16 */
7373     unsigned int Variable_Frame:1;		/* 17 */
7374     unsigned int Separate_Package_Body:1;	/* 18 */
7375     unsigned int Frame_Extension_Millicode:1;	/* 19 */
7376     unsigned int Stack_Overflow_Check:1;	/* 20 */
7377     unsigned int Two_Instruction_SP_Increment:1;/* 21 */
7378     unsigned int Ada_Region:1;			/* 22 */
7379     unsigned int cxx_info:1;			/* 23 */
7380     unsigned int cxx_try_catch:1;		/* 24 */
7381     unsigned int sched_entry_seq:1;		/* 25 */
7382     unsigned int reserved2:1;			/* 26 */
7383     unsigned int Save_SP:1;			/* 27 */
7384     unsigned int Save_RP:1;			/* 28 */
7385     unsigned int Save_MRP_in_frame:1;		/* 29 */
7386     unsigned int extn_ptr_defined:1;		/* 30 */
7387     unsigned int Cleanup_defined:1;		/* 31 */
7388 
7389     unsigned int MPE_XL_interrupt_marker:1;	/* 0 */
7390     unsigned int HP_UX_interrupt_marker:1;	/* 1 */
7391     unsigned int Large_frame:1;			/* 2 */
7392     unsigned int Pseudo_SP_Set:1;		/* 3 */
7393     unsigned int reserved4:1;			/* 4 */
7394     unsigned int Total_frame_size:27;		/* 5..31 */
7395   };
7396 
7397 struct hppa_unw_aux_info
7398 {
7399   struct hppa_unw_table_entry * table;	/* Unwind table.  */
7400   unsigned long table_len;		/* Length of unwind table.  */
7401   bfd_vma seg_base;			/* Starting address of segment.  */
7402   Elf_Internal_Sym * symtab;		/* The symbol table.  */
7403   unsigned long nsyms;			/* Number of symbols.  */
7404   Elf_Internal_Sym * funtab;		/* Sorted table of STT_FUNC symbols.  */
7405   unsigned long nfuns;			/* Number of entries in funtab.  */
7406   char * strtab;			/* The string table.  */
7407   unsigned long strtab_size;		/* Size of string table.  */
7408 };
7409 
7410 static void
dump_hppa_unwind(struct hppa_unw_aux_info * aux)7411 dump_hppa_unwind (struct hppa_unw_aux_info * aux)
7412 {
7413   struct hppa_unw_table_entry * tp;
7414   unsigned long j, nfuns;
7415 
7416   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7417   for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7418     if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7419       aux->funtab[nfuns++] = aux->symtab[j];
7420   aux->nfuns = nfuns;
7421   qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7422 
7423   for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7424     {
7425       bfd_vma offset;
7426       const char * procname;
7427 
7428       find_symbol_for_address (aux->funtab, aux->nfuns, aux->strtab,
7429 			       aux->strtab_size, tp->start, &procname,
7430 			       &offset);
7431 
7432       fputs ("\n<", stdout);
7433 
7434       if (procname)
7435 	{
7436 	  fputs (procname, stdout);
7437 
7438 	  if (offset)
7439 	    printf ("+%lx", (unsigned long) offset);
7440 	}
7441 
7442       fputs (">: [", stdout);
7443       print_vma (tp->start.offset, PREFIX_HEX);
7444       fputc ('-', stdout);
7445       print_vma (tp->end.offset, PREFIX_HEX);
7446       printf ("]\n\t");
7447 
7448 #define PF(_m) if (tp->_m) printf (#_m " ");
7449 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
7450       PF(Cannot_unwind);
7451       PF(Millicode);
7452       PF(Millicode_save_sr0);
7453       /* PV(Region_description);  */
7454       PF(Entry_SR);
7455       PV(Entry_FR);
7456       PV(Entry_GR);
7457       PF(Args_stored);
7458       PF(Variable_Frame);
7459       PF(Separate_Package_Body);
7460       PF(Frame_Extension_Millicode);
7461       PF(Stack_Overflow_Check);
7462       PF(Two_Instruction_SP_Increment);
7463       PF(Ada_Region);
7464       PF(cxx_info);
7465       PF(cxx_try_catch);
7466       PF(sched_entry_seq);
7467       PF(Save_SP);
7468       PF(Save_RP);
7469       PF(Save_MRP_in_frame);
7470       PF(extn_ptr_defined);
7471       PF(Cleanup_defined);
7472       PF(MPE_XL_interrupt_marker);
7473       PF(HP_UX_interrupt_marker);
7474       PF(Large_frame);
7475       PF(Pseudo_SP_Set);
7476       PV(Total_frame_size);
7477 #undef PF
7478 #undef PV
7479     }
7480 
7481   printf ("\n");
7482 
7483   free (aux->funtab);
7484 }
7485 
7486 static int
slurp_hppa_unwind_table(FILE * file,struct hppa_unw_aux_info * aux,Elf_Internal_Shdr * sec)7487 slurp_hppa_unwind_table (FILE * file,
7488 			 struct hppa_unw_aux_info * aux,
7489 			 Elf_Internal_Shdr * sec)
7490 {
7491   unsigned long size, unw_ent_size, nentries, nrelas, i;
7492   Elf_Internal_Phdr * seg;
7493   struct hppa_unw_table_entry * tep;
7494   Elf_Internal_Shdr * relsec;
7495   Elf_Internal_Rela * rela;
7496   Elf_Internal_Rela * rp;
7497   unsigned char * table;
7498   unsigned char * tp;
7499   Elf_Internal_Sym * sym;
7500   const char * relname;
7501 
7502   /* First, find the starting address of the segment that includes
7503      this section.  */
7504 
7505   if (elf_header.e_phnum)
7506     {
7507       if (! get_program_headers (file))
7508 	return 0;
7509 
7510       for (seg = program_headers;
7511 	   seg < program_headers + elf_header.e_phnum;
7512 	   ++seg)
7513 	{
7514 	  if (seg->p_type != PT_LOAD)
7515 	    continue;
7516 
7517 	  if (sec->sh_addr >= seg->p_vaddr
7518 	      && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
7519 	    {
7520 	      aux->seg_base = seg->p_vaddr;
7521 	      break;
7522 	    }
7523 	}
7524     }
7525 
7526   /* Second, build the unwind table from the contents of the unwind
7527      section.  */
7528   size = sec->sh_size;
7529   table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
7530                                       _("unwind table"));
7531   if (!table)
7532     return 0;
7533 
7534   unw_ent_size = 16;
7535   nentries = size / unw_ent_size;
7536   size = unw_ent_size * nentries;
7537 
7538   tep = aux->table = (struct hppa_unw_table_entry *)
7539       xcmalloc (nentries, sizeof (aux->table[0]));
7540 
7541   for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
7542     {
7543       unsigned int tmp1, tmp2;
7544 
7545       tep->start.section = SHN_UNDEF;
7546       tep->end.section   = SHN_UNDEF;
7547 
7548       tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
7549       tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
7550       tmp1 = byte_get ((unsigned char *) tp + 8, 4);
7551       tmp2 = byte_get ((unsigned char *) tp + 12, 4);
7552 
7553       tep->start.offset += aux->seg_base;
7554       tep->end.offset   += aux->seg_base;
7555 
7556       tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
7557       tep->Millicode = (tmp1 >> 30) & 0x1;
7558       tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
7559       tep->Region_description = (tmp1 >> 27) & 0x3;
7560       tep->reserved1 = (tmp1 >> 26) & 0x1;
7561       tep->Entry_SR = (tmp1 >> 25) & 0x1;
7562       tep->Entry_FR = (tmp1 >> 21) & 0xf;
7563       tep->Entry_GR = (tmp1 >> 16) & 0x1f;
7564       tep->Args_stored = (tmp1 >> 15) & 0x1;
7565       tep->Variable_Frame = (tmp1 >> 14) & 0x1;
7566       tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
7567       tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
7568       tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
7569       tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
7570       tep->Ada_Region = (tmp1 >> 9) & 0x1;
7571       tep->cxx_info = (tmp1 >> 8) & 0x1;
7572       tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
7573       tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
7574       tep->reserved2 = (tmp1 >> 5) & 0x1;
7575       tep->Save_SP = (tmp1 >> 4) & 0x1;
7576       tep->Save_RP = (tmp1 >> 3) & 0x1;
7577       tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
7578       tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
7579       tep->Cleanup_defined = tmp1 & 0x1;
7580 
7581       tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
7582       tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
7583       tep->Large_frame = (tmp2 >> 29) & 0x1;
7584       tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
7585       tep->reserved4 = (tmp2 >> 27) & 0x1;
7586       tep->Total_frame_size = tmp2 & 0x7ffffff;
7587     }
7588   free (table);
7589 
7590   /* Third, apply any relocations to the unwind table.  */
7591   for (relsec = section_headers;
7592        relsec < section_headers + elf_header.e_shnum;
7593        ++relsec)
7594     {
7595       if (relsec->sh_type != SHT_RELA
7596 	  || relsec->sh_info >= elf_header.e_shnum
7597 	  || section_headers + relsec->sh_info != sec)
7598 	continue;
7599 
7600       if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
7601 			      & rela, & nrelas))
7602 	return 0;
7603 
7604       for (rp = rela; rp < rela + nrelas; ++rp)
7605 	{
7606 	  relname = elf_hppa_reloc_type (get_reloc_type (rp->r_info));
7607 	  sym = aux->symtab + get_reloc_symindex (rp->r_info);
7608 
7609 	  /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64.  */
7610 	  if (! const_strneq (relname, "R_PARISC_SEGREL"))
7611 	    {
7612 	      warn (_("Skipping unexpected relocation type %s\n"), relname);
7613 	      continue;
7614 	    }
7615 
7616 	  i = rp->r_offset / unw_ent_size;
7617 
7618 	  switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
7619 	    {
7620 	    case 0:
7621 	      aux->table[i].start.section = sym->st_shndx;
7622 	      aux->table[i].start.offset  = sym->st_value + rp->r_addend;
7623 	      break;
7624 	    case 1:
7625 	      aux->table[i].end.section   = sym->st_shndx;
7626 	      aux->table[i].end.offset    = sym->st_value + rp->r_addend;
7627 	      break;
7628 	    default:
7629 	      break;
7630 	    }
7631 	}
7632 
7633       free (rela);
7634     }
7635 
7636   aux->table_len = nentries;
7637 
7638   return 1;
7639 }
7640 
7641 static void
hppa_process_unwind(FILE * file)7642 hppa_process_unwind (FILE * file)
7643 {
7644   struct hppa_unw_aux_info aux;
7645   Elf_Internal_Shdr * unwsec = NULL;
7646   Elf_Internal_Shdr * strsec;
7647   Elf_Internal_Shdr * sec;
7648   unsigned long i;
7649 
7650   if (string_table == NULL)
7651     return;
7652 
7653   memset (& aux, 0, sizeof (aux));
7654 
7655   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7656     {
7657       if (sec->sh_type == SHT_SYMTAB
7658 	  && sec->sh_link < elf_header.e_shnum)
7659 	{
7660 	  aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
7661 
7662 	  strsec = section_headers + sec->sh_link;
7663 	  if (aux.strtab != NULL)
7664 	    {
7665 	      error (_("Multiple auxillary string tables encountered\n"));
7666 	      free (aux.strtab);
7667 	    }
7668 	  aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
7669                                           1, strsec->sh_size,
7670                                           _("string table"));
7671 	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
7672 	}
7673       else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
7674 	unwsec = sec;
7675     }
7676 
7677   if (!unwsec)
7678     printf (_("\nThere are no unwind sections in this file.\n"));
7679 
7680   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7681     {
7682       if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
7683 	{
7684 	  printf (_("\nUnwind section '%s' at offset 0x%lx contains %lu entries:\n"),
7685 		  printable_section_name (sec),
7686 		  (unsigned long) sec->sh_offset,
7687 		  (unsigned long) (sec->sh_size / (2 * eh_addr_size + 8)));
7688 
7689           slurp_hppa_unwind_table (file, &aux, sec);
7690 	  if (aux.table_len > 0)
7691 	    dump_hppa_unwind (&aux);
7692 
7693 	  if (aux.table)
7694 	    free ((char *) aux.table);
7695 	  aux.table = NULL;
7696 	}
7697     }
7698 
7699   if (aux.symtab)
7700     free (aux.symtab);
7701   if (aux.strtab)
7702     free ((char *) aux.strtab);
7703 }
7704 
7705 struct arm_section
7706 {
7707   unsigned char *      data;		/* The unwind data.  */
7708   Elf_Internal_Shdr *  sec;		/* The cached unwind section header.  */
7709   Elf_Internal_Rela *  rela;		/* The cached relocations for this section.  */
7710   unsigned long        nrelas;		/* The number of relocations.  */
7711   unsigned int         rel_type;	/* REL or RELA ?  */
7712   Elf_Internal_Rela *  next_rela;	/* Cyclic pointer to the next reloc to process.  */
7713 };
7714 
7715 struct arm_unw_aux_info
7716 {
7717   FILE *              file;		/* The file containing the unwind sections.  */
7718   Elf_Internal_Sym *  symtab;		/* The file's symbol table.  */
7719   unsigned long       nsyms;		/* Number of symbols.  */
7720   Elf_Internal_Sym *  funtab;		/* Sorted table of STT_FUNC symbols.  */
7721   unsigned long       nfuns;		/* Number of these symbols.  */
7722   char *              strtab;		/* The file's string table.  */
7723   unsigned long       strtab_size;	/* Size of string table.  */
7724 };
7725 
7726 static const char *
arm_print_vma_and_name(struct arm_unw_aux_info * aux,bfd_vma fn,struct absaddr addr)7727 arm_print_vma_and_name (struct arm_unw_aux_info *aux,
7728 			bfd_vma fn, struct absaddr addr)
7729 {
7730   const char *procname;
7731   bfd_vma sym_offset;
7732 
7733   if (addr.section == SHN_UNDEF)
7734     addr.offset = fn;
7735 
7736   find_symbol_for_address (aux->funtab, aux->nfuns, aux->strtab,
7737 			   aux->strtab_size, addr, &procname,
7738 			   &sym_offset);
7739 
7740   print_vma (fn, PREFIX_HEX);
7741 
7742   if (procname)
7743     {
7744       fputs (" <", stdout);
7745       fputs (procname, stdout);
7746 
7747       if (sym_offset)
7748 	printf ("+0x%lx", (unsigned long) sym_offset);
7749       fputc ('>', stdout);
7750     }
7751 
7752   return procname;
7753 }
7754 
7755 static void
arm_free_section(struct arm_section * arm_sec)7756 arm_free_section (struct arm_section *arm_sec)
7757 {
7758   if (arm_sec->data != NULL)
7759     free (arm_sec->data);
7760 
7761   if (arm_sec->rela != NULL)
7762     free (arm_sec->rela);
7763 }
7764 
7765 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
7766       cached section and install SEC instead.
7767    2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
7768       and return its valued in * WORDP, relocating if necessary.
7769    3) Update the NEXT_RELA field in ARM_SEC and store the section index and
7770       relocation's offset in ADDR.
7771    4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
7772       into the string table of the symbol associated with the reloc.  If no
7773       reloc was applied store -1 there.
7774    5) Return TRUE upon success, FALSE otherwise.  */
7775 
7776 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)7777 get_unwind_section_word (struct arm_unw_aux_info *  aux,
7778 			 struct arm_section *       arm_sec,
7779 			 Elf_Internal_Shdr *        sec,
7780 			 bfd_vma 		    word_offset,
7781 			 unsigned int *             wordp,
7782 			 struct absaddr *           addr,
7783 			 bfd_vma *		    sym_name)
7784 {
7785   Elf_Internal_Rela *rp;
7786   Elf_Internal_Sym *sym;
7787   const char * relname;
7788   unsigned int word;
7789   bfd_boolean wrapped;
7790 
7791   if (sec == NULL || arm_sec == NULL)
7792     return FALSE;
7793 
7794   addr->section = SHN_UNDEF;
7795   addr->offset = 0;
7796 
7797   if (sym_name != NULL)
7798     *sym_name = (bfd_vma) -1;
7799 
7800   /* If necessary, update the section cache.  */
7801   if (sec != arm_sec->sec)
7802     {
7803       Elf_Internal_Shdr *relsec;
7804 
7805       arm_free_section (arm_sec);
7806 
7807       arm_sec->sec = sec;
7808       arm_sec->data = get_data (NULL, aux->file, sec->sh_offset, 1,
7809 				sec->sh_size, _("unwind data"));
7810       arm_sec->rela = NULL;
7811       arm_sec->nrelas = 0;
7812 
7813       for (relsec = section_headers;
7814 	   relsec < section_headers + elf_header.e_shnum;
7815 	   ++relsec)
7816 	{
7817 	  if (relsec->sh_info >= elf_header.e_shnum
7818 	      || section_headers + relsec->sh_info != sec
7819 	      /* PR 15745: Check the section type as well.  */
7820 	      || (relsec->sh_type != SHT_REL
7821 		  && relsec->sh_type != SHT_RELA))
7822 	    continue;
7823 
7824 	  arm_sec->rel_type = relsec->sh_type;
7825 	  if (relsec->sh_type == SHT_REL)
7826 	    {
7827 	      if (!slurp_rel_relocs (aux->file, relsec->sh_offset,
7828 				     relsec->sh_size,
7829 				     & arm_sec->rela, & arm_sec->nrelas))
7830 		return FALSE;
7831 	    }
7832 	  else /* relsec->sh_type == SHT_RELA */
7833 	    {
7834 	      if (!slurp_rela_relocs (aux->file, relsec->sh_offset,
7835 				      relsec->sh_size,
7836 				      & arm_sec->rela, & arm_sec->nrelas))
7837 		return FALSE;
7838 	    }
7839 	  break;
7840 	}
7841 
7842       arm_sec->next_rela = arm_sec->rela;
7843     }
7844 
7845   /* If there is no unwind data we can do nothing.  */
7846   if (arm_sec->data == NULL)
7847     return FALSE;
7848 
7849   /* If the offset is invalid then fail.  */
7850   if (word_offset > (sec->sh_size - 4)
7851       /* PR 18879 */
7852       || (sec->sh_size < 5 && word_offset >= sec->sh_size)
7853       || ((bfd_signed_vma) word_offset) < 0)
7854     return FALSE;
7855 
7856   /* Get the word at the required offset.  */
7857   word = byte_get (arm_sec->data + word_offset, 4);
7858 
7859   /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128.  */
7860   if (arm_sec->rela == NULL)
7861     {
7862       * wordp = word;
7863       return TRUE;
7864     }
7865 
7866   /* Look through the relocs to find the one that applies to the provided offset.  */
7867   wrapped = FALSE;
7868   for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
7869     {
7870       bfd_vma prelval, offset;
7871 
7872       if (rp->r_offset > word_offset && !wrapped)
7873 	{
7874 	  rp = arm_sec->rela;
7875 	  wrapped = TRUE;
7876 	}
7877       if (rp->r_offset > word_offset)
7878 	break;
7879 
7880       if (rp->r_offset & 3)
7881 	{
7882 	  warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
7883 		(unsigned long) rp->r_offset);
7884 	  continue;
7885 	}
7886 
7887       if (rp->r_offset < word_offset)
7888 	continue;
7889 
7890       /* PR 17531: file: 027-161405-0.004  */
7891       if (aux->symtab == NULL)
7892 	continue;
7893 
7894       if (arm_sec->rel_type == SHT_REL)
7895 	{
7896 	  offset = word & 0x7fffffff;
7897 	  if (offset & 0x40000000)
7898 	    offset |= ~ (bfd_vma) 0x7fffffff;
7899 	}
7900       else if (arm_sec->rel_type == SHT_RELA)
7901 	offset = rp->r_addend;
7902       else
7903 	{
7904 	  error (_("Unknown section relocation type %d encountered\n"),
7905 		 arm_sec->rel_type);
7906 	  break;
7907 	}
7908 
7909       /* PR 17531 file: 027-1241568-0.004.  */
7910       if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
7911 	{
7912 	  error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
7913 		 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
7914 	  break;
7915 	}
7916 
7917       sym = aux->symtab + ELF32_R_SYM (rp->r_info);
7918       offset += sym->st_value;
7919       prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
7920 
7921       /* Check that we are processing the expected reloc type.  */
7922       if (elf_header.e_machine == EM_ARM)
7923 	{
7924 	  relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
7925 	  if (relname == NULL)
7926 	    {
7927 	      warn (_("Skipping unknown ARM relocation type: %d\n"),
7928 		    (int) ELF32_R_TYPE (rp->r_info));
7929 	      continue;
7930 	    }
7931 
7932 	  if (streq (relname, "R_ARM_NONE"))
7933 	      continue;
7934 
7935 	  if (! streq (relname, "R_ARM_PREL31"))
7936 	    {
7937 	      warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
7938 	      continue;
7939 	    }
7940 	}
7941       else if (elf_header.e_machine == EM_TI_C6000)
7942 	{
7943 	  relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
7944 	  if (relname == NULL)
7945 	    {
7946 	      warn (_("Skipping unknown C6000 relocation type: %d\n"),
7947 		    (int) ELF32_R_TYPE (rp->r_info));
7948 	      continue;
7949 	    }
7950 
7951 	  if (streq (relname, "R_C6000_NONE"))
7952 	    continue;
7953 
7954 	  if (! streq (relname, "R_C6000_PREL31"))
7955 	    {
7956 	      warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
7957 	      continue;
7958 	    }
7959 
7960 	  prelval >>= 1;
7961 	}
7962       else
7963 	{
7964 	  /* This function currently only supports ARM and TI unwinders.  */
7965 	  warn (_("Only TI and ARM unwinders are currently supported\n"));
7966 	  break;
7967 	}
7968 
7969       word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
7970       addr->section = sym->st_shndx;
7971       addr->offset = offset;
7972 
7973       if (sym_name)
7974 	* sym_name = sym->st_name;
7975       break;
7976     }
7977 
7978   *wordp = word;
7979   arm_sec->next_rela = rp;
7980 
7981   return TRUE;
7982 }
7983 
7984 static const char *tic6x_unwind_regnames[16] =
7985 {
7986   "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
7987   "A14", "A13", "A12", "A11", "A10",
7988   "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
7989 };
7990 
7991 static void
decode_tic6x_unwind_regmask(unsigned int mask)7992 decode_tic6x_unwind_regmask (unsigned int mask)
7993 {
7994   int i;
7995 
7996   for (i = 12; mask; mask >>= 1, i--)
7997     {
7998       if (mask & 1)
7999 	{
8000 	  fputs (tic6x_unwind_regnames[i], stdout);
8001 	  if (mask > 1)
8002 	    fputs (", ", stdout);
8003 	}
8004     }
8005 }
8006 
8007 #define ADVANCE							\
8008   if (remaining == 0 && more_words)				\
8009     {								\
8010       data_offset += 4;						\
8011       if (! get_unwind_section_word (aux, data_arm_sec, data_sec,	\
8012 				     data_offset, & word, & addr, NULL))	\
8013 	return;							\
8014       remaining = 4;						\
8015       more_words--;						\
8016     }								\
8017 
8018 #define GET_OP(OP)			\
8019   ADVANCE;				\
8020   if (remaining)			\
8021     {					\
8022       remaining--;			\
8023       (OP) = word >> 24;		\
8024       word <<= 8;			\
8025     }					\
8026   else					\
8027     {					\
8028       printf (_("[Truncated opcode]\n"));	\
8029       return;				\
8030     }					\
8031   printf ("0x%02x ", OP)
8032 
8033 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)8034 decode_arm_unwind_bytecode (struct arm_unw_aux_info *  aux,
8035 			    unsigned int               word,
8036 			    unsigned int               remaining,
8037 			    unsigned int               more_words,
8038 			    bfd_vma                    data_offset,
8039 			    Elf_Internal_Shdr *        data_sec,
8040 			    struct arm_section *       data_arm_sec)
8041 {
8042   struct absaddr addr;
8043 
8044   /* Decode the unwinding instructions.  */
8045   while (1)
8046     {
8047       unsigned int op, op2;
8048 
8049       ADVANCE;
8050       if (remaining == 0)
8051 	break;
8052       remaining--;
8053       op = word >> 24;
8054       word <<= 8;
8055 
8056       printf ("  0x%02x ", op);
8057 
8058       if ((op & 0xc0) == 0x00)
8059 	{
8060 	  int offset = ((op & 0x3f) << 2) + 4;
8061 
8062 	  printf ("     vsp = vsp + %d", offset);
8063 	}
8064       else if ((op & 0xc0) == 0x40)
8065 	{
8066 	  int offset = ((op & 0x3f) << 2) + 4;
8067 
8068 	  printf ("     vsp = vsp - %d", offset);
8069 	}
8070       else if ((op & 0xf0) == 0x80)
8071 	{
8072 	  GET_OP (op2);
8073 	  if (op == 0x80 && op2 == 0)
8074 	    printf (_("Refuse to unwind"));
8075 	  else
8076 	    {
8077 	      unsigned int mask = ((op & 0x0f) << 8) | op2;
8078 	      int first = 1;
8079 	      int i;
8080 
8081 	      printf ("pop {");
8082 	      for (i = 0; i < 12; i++)
8083 		if (mask & (1 << i))
8084 		  {
8085 		    if (first)
8086 		      first = 0;
8087 		    else
8088 		      printf (", ");
8089 		    printf ("r%d", 4 + i);
8090 		  }
8091 	      printf ("}");
8092 	    }
8093 	}
8094       else if ((op & 0xf0) == 0x90)
8095 	{
8096 	  if (op == 0x9d || op == 0x9f)
8097 	    printf (_("     [Reserved]"));
8098 	  else
8099 	    printf ("     vsp = r%d", op & 0x0f);
8100 	}
8101       else if ((op & 0xf0) == 0xa0)
8102 	{
8103 	  int end = 4 + (op & 0x07);
8104 	  int first = 1;
8105 	  int i;
8106 
8107 	  printf ("     pop {");
8108 	  for (i = 4; i <= end; i++)
8109 	    {
8110 	      if (first)
8111 		first = 0;
8112 	      else
8113 		printf (", ");
8114 	      printf ("r%d", i);
8115 	    }
8116 	  if (op & 0x08)
8117 	    {
8118 	      if (!first)
8119 		printf (", ");
8120 	      printf ("r14");
8121 	    }
8122 	  printf ("}");
8123 	}
8124       else if (op == 0xb0)
8125 	printf (_("     finish"));
8126       else if (op == 0xb1)
8127 	{
8128 	  GET_OP (op2);
8129 	  if (op2 == 0 || (op2 & 0xf0) != 0)
8130 	    printf (_("[Spare]"));
8131 	  else
8132 	    {
8133 	      unsigned int mask = op2 & 0x0f;
8134 	      int first = 1;
8135 	      int i;
8136 
8137 	      printf ("pop {");
8138 	      for (i = 0; i < 12; i++)
8139 		if (mask & (1 << i))
8140 		  {
8141 		    if (first)
8142 		      first = 0;
8143 		    else
8144 		      printf (", ");
8145 		    printf ("r%d", i);
8146 		  }
8147 	      printf ("}");
8148 	    }
8149 	}
8150       else if (op == 0xb2)
8151 	{
8152 	  unsigned char buf[9];
8153 	  unsigned int i, len;
8154 	  unsigned long offset;
8155 
8156 	  for (i = 0; i < sizeof (buf); i++)
8157 	    {
8158 	      GET_OP (buf[i]);
8159 	      if ((buf[i] & 0x80) == 0)
8160 		break;
8161 	    }
8162 	  if (i == sizeof (buf))
8163 	    printf (_("corrupt change to vsp"));
8164 	  else
8165 	    {
8166 	      offset = read_uleb128 (buf, &len, buf + i + 1);
8167 	      assert (len == i + 1);
8168 	      offset = offset * 4 + 0x204;
8169 	      printf ("vsp = vsp + %ld", offset);
8170 	    }
8171 	}
8172       else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
8173 	{
8174 	  unsigned int first, last;
8175 
8176 	  GET_OP (op2);
8177 	  first = op2 >> 4;
8178 	  last = op2 & 0x0f;
8179 	  if (op == 0xc8)
8180 	    first = first + 16;
8181 	  printf ("pop {D%d", first);
8182 	  if (last)
8183 	    printf ("-D%d", first + last);
8184 	  printf ("}");
8185 	}
8186       else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
8187 	{
8188 	  unsigned int count = op & 0x07;
8189 
8190 	  printf ("pop {D8");
8191 	  if (count)
8192 	    printf ("-D%d", 8 + count);
8193 	  printf ("}");
8194 	}
8195       else if (op >= 0xc0 && op <= 0xc5)
8196 	{
8197 	  unsigned int count = op & 0x07;
8198 
8199 	  printf ("     pop {wR10");
8200 	  if (count)
8201 	    printf ("-wR%d", 10 + count);
8202 	  printf ("}");
8203 	}
8204       else if (op == 0xc6)
8205 	{
8206 	  unsigned int first, last;
8207 
8208 	  GET_OP (op2);
8209 	  first = op2 >> 4;
8210 	  last = op2 & 0x0f;
8211 	  printf ("pop {wR%d", first);
8212 	  if (last)
8213 	    printf ("-wR%d", first + last);
8214 	  printf ("}");
8215 	}
8216       else if (op == 0xc7)
8217 	{
8218 	  GET_OP (op2);
8219 	  if (op2 == 0 || (op2 & 0xf0) != 0)
8220 	    printf (_("[Spare]"));
8221 	  else
8222 	    {
8223 	      unsigned int mask = op2 & 0x0f;
8224 	      int first = 1;
8225 	      int i;
8226 
8227 	      printf ("pop {");
8228 	      for (i = 0; i < 4; i++)
8229 		if (mask & (1 << i))
8230 		  {
8231 		    if (first)
8232 		      first = 0;
8233 		    else
8234 		      printf (", ");
8235 		    printf ("wCGR%d", i);
8236 		  }
8237 	      printf ("}");
8238 	    }
8239 	}
8240       else
8241 	printf (_("     [unsupported opcode]"));
8242       printf ("\n");
8243     }
8244 }
8245 
8246 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)8247 decode_tic6x_unwind_bytecode (struct arm_unw_aux_info *  aux,
8248 			      unsigned int               word,
8249 			      unsigned int               remaining,
8250 			      unsigned int               more_words,
8251 			      bfd_vma                    data_offset,
8252 			      Elf_Internal_Shdr *        data_sec,
8253 			      struct arm_section *       data_arm_sec)
8254 {
8255   struct absaddr addr;
8256 
8257   /* Decode the unwinding instructions.  */
8258   while (1)
8259     {
8260       unsigned int op, op2;
8261 
8262       ADVANCE;
8263       if (remaining == 0)
8264 	break;
8265       remaining--;
8266       op = word >> 24;
8267       word <<= 8;
8268 
8269       printf ("  0x%02x ", op);
8270 
8271       if ((op & 0xc0) == 0x00)
8272 	{
8273 	  int offset = ((op & 0x3f) << 3) + 8;
8274 	  printf ("     sp = sp + %d", offset);
8275 	}
8276       else if ((op & 0xc0) == 0x80)
8277 	{
8278 	  GET_OP (op2);
8279 	  if (op == 0x80 && op2 == 0)
8280 	    printf (_("Refuse to unwind"));
8281 	  else
8282 	    {
8283 	      unsigned int mask = ((op & 0x1f) << 8) | op2;
8284 	      if (op & 0x20)
8285 		printf ("pop compact {");
8286 	      else
8287 		printf ("pop {");
8288 
8289 	      decode_tic6x_unwind_regmask (mask);
8290 	      printf("}");
8291 	    }
8292 	}
8293       else if ((op & 0xf0) == 0xc0)
8294 	{
8295 	  unsigned int reg;
8296 	  unsigned int nregs;
8297 	  unsigned int i;
8298 	  const char *name;
8299 	  struct
8300 	  {
8301 	      unsigned int offset;
8302 	      unsigned int reg;
8303 	  } regpos[16];
8304 
8305 	  /* Scan entire instruction first so that GET_OP output is not
8306 	     interleaved with disassembly.  */
8307 	  nregs = 0;
8308 	  for (i = 0; nregs < (op & 0xf); i++)
8309 	    {
8310 	      GET_OP (op2);
8311 	      reg = op2 >> 4;
8312 	      if (reg != 0xf)
8313 		{
8314 		  regpos[nregs].offset = i * 2;
8315 		  regpos[nregs].reg = reg;
8316 		  nregs++;
8317 		}
8318 
8319 	      reg = op2 & 0xf;
8320 	      if (reg != 0xf)
8321 		{
8322 		  regpos[nregs].offset = i * 2 + 1;
8323 		  regpos[nregs].reg = reg;
8324 		  nregs++;
8325 		}
8326 	    }
8327 
8328 	  printf (_("pop frame {"));
8329 	  reg = nregs - 1;
8330 	  for (i = i * 2; i > 0; i--)
8331 	    {
8332 	      if (regpos[reg].offset == i - 1)
8333 		{
8334 		  name = tic6x_unwind_regnames[regpos[reg].reg];
8335 		  if (reg > 0)
8336 		    reg--;
8337 		}
8338 	      else
8339 		name = _("[pad]");
8340 
8341 	      fputs (name, stdout);
8342 	      if (i > 1)
8343 		printf (", ");
8344 	    }
8345 
8346 	  printf ("}");
8347 	}
8348       else if (op == 0xd0)
8349 	printf ("     MOV FP, SP");
8350       else if (op == 0xd1)
8351 	printf ("     __c6xabi_pop_rts");
8352       else if (op == 0xd2)
8353 	{
8354 	  unsigned char buf[9];
8355 	  unsigned int i, len;
8356 	  unsigned long offset;
8357 
8358 	  for (i = 0; i < sizeof (buf); i++)
8359 	    {
8360 	      GET_OP (buf[i]);
8361 	      if ((buf[i] & 0x80) == 0)
8362 		break;
8363 	    }
8364 	  /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2.  */
8365 	  if (i == sizeof (buf))
8366 	    {
8367 	      printf ("<corrupt sp adjust>\n");
8368 	      warn (_("Corrupt stack pointer adjustment detected\n"));
8369 	      return;
8370 	    }
8371 
8372 	  offset = read_uleb128 (buf, &len, buf + i + 1);
8373 	  assert (len == i + 1);
8374 	  offset = offset * 8 + 0x408;
8375 	  printf (_("sp = sp + %ld"), offset);
8376 	}
8377       else if ((op & 0xf0) == 0xe0)
8378 	{
8379 	  if ((op & 0x0f) == 7)
8380 	    printf ("     RETURN");
8381 	  else
8382 	    printf ("     MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
8383 	}
8384       else
8385 	{
8386 	  printf (_("     [unsupported opcode]"));
8387 	}
8388       putchar ('\n');
8389     }
8390 }
8391 
8392 static bfd_vma
arm_expand_prel31(bfd_vma word,bfd_vma where)8393 arm_expand_prel31 (bfd_vma word, bfd_vma where)
8394 {
8395   bfd_vma offset;
8396 
8397   offset = word & 0x7fffffff;
8398   if (offset & 0x40000000)
8399     offset |= ~ (bfd_vma) 0x7fffffff;
8400 
8401   if (elf_header.e_machine == EM_TI_C6000)
8402     offset <<= 1;
8403 
8404   return offset + where;
8405 }
8406 
8407 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)8408 decode_arm_unwind (struct arm_unw_aux_info *  aux,
8409 		   unsigned int               word,
8410 		   unsigned int               remaining,
8411 		   bfd_vma                    data_offset,
8412 		   Elf_Internal_Shdr *        data_sec,
8413 		   struct arm_section *       data_arm_sec)
8414 {
8415   int per_index;
8416   unsigned int more_words = 0;
8417   struct absaddr addr;
8418   bfd_vma sym_name = (bfd_vma) -1;
8419 
8420   if (remaining == 0)
8421     {
8422       /* Fetch the first word.
8423 	 Note - when decoding an object file the address extracted
8424 	 here will always be 0.  So we also pass in the sym_name
8425 	 parameter so that we can find the symbol associated with
8426 	 the personality routine.  */
8427       if (! get_unwind_section_word (aux, data_arm_sec, data_sec, data_offset,
8428 				     & word, & addr, & sym_name))
8429 	return;
8430 
8431       remaining = 4;
8432     }
8433 
8434   if ((word & 0x80000000) == 0)
8435     {
8436       /* Expand prel31 for personality routine.  */
8437       bfd_vma fn;
8438       const char *procname;
8439 
8440       fn = arm_expand_prel31 (word, data_sec->sh_addr + data_offset);
8441       printf (_("  Personality routine: "));
8442       if (fn == 0
8443 	  && addr.section == SHN_UNDEF && addr.offset == 0
8444 	  && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
8445 	{
8446 	  procname = aux->strtab + sym_name;
8447 	  print_vma (fn, PREFIX_HEX);
8448 	  if (procname)
8449 	    {
8450 	      fputs (" <", stdout);
8451 	      fputs (procname, stdout);
8452 	      fputc ('>', stdout);
8453 	    }
8454 	}
8455       else
8456 	procname = arm_print_vma_and_name (aux, fn, addr);
8457       fputc ('\n', stdout);
8458 
8459       /* The GCC personality routines use the standard compact
8460 	 encoding, starting with one byte giving the number of
8461 	 words.  */
8462       if (procname != NULL
8463 	  && (const_strneq (procname, "__gcc_personality_v0")
8464 	      || const_strneq (procname, "__gxx_personality_v0")
8465 	      || const_strneq (procname, "__gcj_personality_v0")
8466 	      || const_strneq (procname, "__gnu_objc_personality_v0")))
8467 	{
8468 	  remaining = 0;
8469 	  more_words = 1;
8470 	  ADVANCE;
8471 	  if (!remaining)
8472 	    {
8473 	      printf (_("  [Truncated data]\n"));
8474 	      return;
8475 	    }
8476 	  more_words = word >> 24;
8477 	  word <<= 8;
8478 	  remaining--;
8479 	  per_index = -1;
8480 	}
8481       else
8482 	return;
8483     }
8484   else
8485     {
8486       /* ARM EHABI Section 6.3:
8487 
8488 	 An exception-handling table entry for the compact model looks like:
8489 
8490            31 30-28 27-24 23-0
8491 	   -- ----- ----- ----
8492             1   0   index Data for personalityRoutine[index]    */
8493 
8494       if (elf_header.e_machine == EM_ARM
8495 	  && (word & 0x70000000))
8496 	warn (_("Corrupt ARM compact model table entry: %x \n"), word);
8497 
8498       per_index = (word >> 24) & 0x7f;
8499       printf (_("  Compact model index: %d\n"), per_index);
8500       if (per_index == 0)
8501 	{
8502 	  more_words = 0;
8503 	  word <<= 8;
8504 	  remaining--;
8505 	}
8506       else if (per_index < 3)
8507 	{
8508 	  more_words = (word >> 16) & 0xff;
8509 	  word <<= 16;
8510 	  remaining -= 2;
8511 	}
8512     }
8513 
8514   switch (elf_header.e_machine)
8515     {
8516     case EM_ARM:
8517       if (per_index < 3)
8518 	{
8519 	  decode_arm_unwind_bytecode (aux, word, remaining, more_words,
8520 				      data_offset, data_sec, data_arm_sec);
8521 	}
8522       else
8523 	{
8524 	  warn (_("Unknown ARM compact model index encountered\n"));
8525 	  printf (_("  [reserved]\n"));
8526 	}
8527       break;
8528 
8529     case EM_TI_C6000:
8530       if (per_index < 3)
8531 	{
8532 	  decode_tic6x_unwind_bytecode (aux, word, remaining, more_words,
8533 					data_offset, data_sec, data_arm_sec);
8534 	}
8535       else if (per_index < 5)
8536 	{
8537 	  if (((word >> 17) & 0x7f) == 0x7f)
8538 	    printf (_("  Restore stack from frame pointer\n"));
8539 	  else
8540 	    printf (_("  Stack increment %d\n"), (word >> 14) & 0x1fc);
8541 	  printf (_("  Registers restored: "));
8542 	  if (per_index == 4)
8543 	    printf (" (compact) ");
8544 	  decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
8545 	  putchar ('\n');
8546 	  printf (_("  Return register: %s\n"),
8547 		  tic6x_unwind_regnames[word & 0xf]);
8548 	}
8549       else
8550 	printf (_("  [reserved (%d)]\n"), per_index);
8551       break;
8552 
8553     default:
8554       error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
8555 	     elf_header.e_machine);
8556     }
8557 
8558   /* Decode the descriptors.  Not implemented.  */
8559 }
8560 
8561 static void
dump_arm_unwind(struct arm_unw_aux_info * aux,Elf_Internal_Shdr * exidx_sec)8562 dump_arm_unwind (struct arm_unw_aux_info *aux, Elf_Internal_Shdr *exidx_sec)
8563 {
8564   struct arm_section exidx_arm_sec, extab_arm_sec;
8565   unsigned int i, exidx_len;
8566   unsigned long j, nfuns;
8567 
8568   memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
8569   memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
8570   exidx_len = exidx_sec->sh_size / 8;
8571 
8572   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8573   for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8574     if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8575       aux->funtab[nfuns++] = aux->symtab[j];
8576   aux->nfuns = nfuns;
8577   qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8578 
8579   for (i = 0; i < exidx_len; i++)
8580     {
8581       unsigned int exidx_fn, exidx_entry;
8582       struct absaddr fn_addr, entry_addr;
8583       bfd_vma fn;
8584 
8585       fputc ('\n', stdout);
8586 
8587       if (! get_unwind_section_word (aux, & exidx_arm_sec, exidx_sec,
8588 				     8 * i, & exidx_fn, & fn_addr, NULL)
8589 	  || ! get_unwind_section_word (aux, & exidx_arm_sec, exidx_sec,
8590 					8 * i + 4, & exidx_entry, & entry_addr, NULL))
8591 	{
8592 	  free (aux->funtab);
8593 	  arm_free_section (& exidx_arm_sec);
8594 	  arm_free_section (& extab_arm_sec);
8595 	  return;
8596 	}
8597 
8598       /* ARM EHABI, Section 5:
8599 	 An index table entry consists of 2 words.
8600          The first word contains a prel31 offset to the start of a function, with bit 31 clear.  */
8601       if (exidx_fn & 0x80000000)
8602 	warn (_("corrupt index table entry: %x\n"), exidx_fn);
8603 
8604       fn = arm_expand_prel31 (exidx_fn, exidx_sec->sh_addr + 8 * i);
8605 
8606       arm_print_vma_and_name (aux, fn, fn_addr);
8607       fputs (": ", stdout);
8608 
8609       if (exidx_entry == 1)
8610 	{
8611 	  print_vma (exidx_entry, PREFIX_HEX);
8612 	  fputs (" [cantunwind]\n", stdout);
8613 	}
8614       else if (exidx_entry & 0x80000000)
8615 	{
8616 	  print_vma (exidx_entry, PREFIX_HEX);
8617 	  fputc ('\n', stdout);
8618 	  decode_arm_unwind (aux, exidx_entry, 4, 0, NULL, NULL);
8619 	}
8620       else
8621 	{
8622 	  bfd_vma table, table_offset = 0;
8623 	  Elf_Internal_Shdr *table_sec;
8624 
8625 	  fputs ("@", stdout);
8626 	  table = arm_expand_prel31 (exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
8627 	  print_vma (table, PREFIX_HEX);
8628 	  printf ("\n");
8629 
8630 	  /* Locate the matching .ARM.extab.  */
8631 	  if (entry_addr.section != SHN_UNDEF
8632 	      && entry_addr.section < elf_header.e_shnum)
8633 	    {
8634 	      table_sec = section_headers + entry_addr.section;
8635 	      table_offset = entry_addr.offset;
8636 	      /* PR 18879 */
8637 	      if (table_offset > table_sec->sh_size
8638 		  || ((bfd_signed_vma) table_offset) < 0)
8639 		{
8640 		  warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
8641 			(unsigned long) table_offset,
8642 			printable_section_name (table_sec));
8643 		  continue;
8644 		}
8645 	    }
8646 	  else
8647 	    {
8648 	      table_sec = find_section_by_address (table);
8649 	      if (table_sec != NULL)
8650 		table_offset = table - table_sec->sh_addr;
8651 	    }
8652 	  if (table_sec == NULL)
8653 	    {
8654 	      warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
8655 		    (unsigned long) table);
8656 	      continue;
8657 	    }
8658 	  decode_arm_unwind (aux, 0, 0, table_offset, table_sec,
8659 			     &extab_arm_sec);
8660 	}
8661     }
8662 
8663   printf ("\n");
8664 
8665   free (aux->funtab);
8666   arm_free_section (&exidx_arm_sec);
8667   arm_free_section (&extab_arm_sec);
8668 }
8669 
8670 /* Used for both ARM and C6X unwinding tables.  */
8671 
8672 static void
arm_process_unwind(FILE * file)8673 arm_process_unwind (FILE *file)
8674 {
8675   struct arm_unw_aux_info aux;
8676   Elf_Internal_Shdr *unwsec = NULL;
8677   Elf_Internal_Shdr *strsec;
8678   Elf_Internal_Shdr *sec;
8679   unsigned long i;
8680   unsigned int sec_type;
8681 
8682   switch (elf_header.e_machine)
8683     {
8684     case EM_ARM:
8685       sec_type = SHT_ARM_EXIDX;
8686       break;
8687 
8688     case EM_TI_C6000:
8689       sec_type = SHT_C6000_UNWIND;
8690       break;
8691 
8692     default:
8693       error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
8694 	     elf_header.e_machine);
8695       return;
8696     }
8697 
8698   if (string_table == NULL)
8699     return;
8700 
8701   memset (& aux, 0, sizeof (aux));
8702   aux.file = file;
8703 
8704   for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
8705     {
8706       if (sec->sh_type == SHT_SYMTAB && sec->sh_link < elf_header.e_shnum)
8707 	{
8708 	  aux.symtab = GET_ELF_SYMBOLS (file, sec, & aux.nsyms);
8709 
8710 	  strsec = section_headers + sec->sh_link;
8711 
8712 	  /* PR binutils/17531 file: 011-12666-0.004.  */
8713 	  if (aux.strtab != NULL)
8714 	    {
8715 	      error (_("Multiple string tables found in file.\n"));
8716 	      free (aux.strtab);
8717 	    }
8718 	  aux.strtab = get_data (NULL, file, strsec->sh_offset,
8719 				 1, strsec->sh_size, _("string table"));
8720 	  aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
8721 	}
8722       else if (sec->sh_type == sec_type)
8723 	unwsec = sec;
8724     }
8725 
8726   if (unwsec == NULL)
8727     printf (_("\nThere are no unwind sections in this file.\n"));
8728   else
8729     for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
8730       {
8731 	if (sec->sh_type == sec_type)
8732 	  {
8733 	    printf (_("\nUnwind table index '%s' at offset 0x%lx contains %lu entries:\n"),
8734 		    printable_section_name (sec),
8735 		    (unsigned long) sec->sh_offset,
8736 		    (unsigned long) (sec->sh_size / (2 * eh_addr_size)));
8737 
8738 	    dump_arm_unwind (&aux, sec);
8739 	  }
8740       }
8741 
8742   if (aux.symtab)
8743     free (aux.symtab);
8744   if (aux.strtab)
8745     free ((char *) aux.strtab);
8746 }
8747 
8748 static void
process_unwind(FILE * file)8749 process_unwind (FILE * file)
8750 {
8751   struct unwind_handler
8752   {
8753     int machtype;
8754     void (* handler)(FILE *);
8755   } handlers[] =
8756   {
8757     { EM_ARM, arm_process_unwind },
8758     { EM_IA_64, ia64_process_unwind },
8759     { EM_PARISC, hppa_process_unwind },
8760     { EM_TI_C6000, arm_process_unwind },
8761     { 0, 0 }
8762   };
8763   int i;
8764 
8765   if (!do_unwind)
8766     return;
8767 
8768   for (i = 0; handlers[i].handler != NULL; i++)
8769     if (elf_header.e_machine == handlers[i].machtype)
8770       {
8771 	handlers[i].handler (file);
8772 	return;
8773       }
8774 
8775   printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
8776 	  get_machine_name (elf_header.e_machine));
8777 }
8778 
8779 static void
dynamic_section_mips_val(Elf_Internal_Dyn * entry)8780 dynamic_section_mips_val (Elf_Internal_Dyn * entry)
8781 {
8782   switch (entry->d_tag)
8783     {
8784     case DT_MIPS_FLAGS:
8785       if (entry->d_un.d_val == 0)
8786 	printf (_("NONE"));
8787       else
8788 	{
8789 	  static const char * opts[] =
8790 	  {
8791 	    "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
8792 	    "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
8793 	    "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
8794 	    "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
8795 	    "RLD_ORDER_SAFE"
8796 	  };
8797 	  unsigned int cnt;
8798 	  int first = 1;
8799 
8800 	  for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
8801 	    if (entry->d_un.d_val & (1 << cnt))
8802 	      {
8803 		printf ("%s%s", first ? "" : " ", opts[cnt]);
8804 		first = 0;
8805 	      }
8806 	}
8807       break;
8808 
8809     case DT_MIPS_IVERSION:
8810       if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
8811 	printf (_("Interface Version: %s"), GET_DYNAMIC_NAME (entry->d_un.d_val));
8812       else
8813 	{
8814 	  char buf[40];
8815 	  sprintf_vma (buf, entry->d_un.d_ptr);
8816 	  /* Note: coded this way so that there is a single string for translation.  */
8817 	  printf (_("<corrupt: %s>"), buf);
8818 	}
8819       break;
8820 
8821     case DT_MIPS_TIME_STAMP:
8822       {
8823 	char timebuf[128];
8824 	struct tm * tmp;
8825 	time_t atime = entry->d_un.d_val;
8826 
8827 	tmp = gmtime (&atime);
8828 	/* PR 17531: file: 6accc532.  */
8829 	if (tmp == NULL)
8830 	  snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
8831 	else
8832 	  snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
8833 		    tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
8834 		    tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
8835 	printf (_("Time Stamp: %s"), timebuf);
8836       }
8837       break;
8838 
8839     case DT_MIPS_RLD_VERSION:
8840     case DT_MIPS_LOCAL_GOTNO:
8841     case DT_MIPS_CONFLICTNO:
8842     case DT_MIPS_LIBLISTNO:
8843     case DT_MIPS_SYMTABNO:
8844     case DT_MIPS_UNREFEXTNO:
8845     case DT_MIPS_HIPAGENO:
8846     case DT_MIPS_DELTA_CLASS_NO:
8847     case DT_MIPS_DELTA_INSTANCE_NO:
8848     case DT_MIPS_DELTA_RELOC_NO:
8849     case DT_MIPS_DELTA_SYM_NO:
8850     case DT_MIPS_DELTA_CLASSSYM_NO:
8851     case DT_MIPS_COMPACT_SIZE:
8852       print_vma (entry->d_un.d_val, DEC);
8853       break;
8854 
8855     default:
8856       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8857     }
8858     putchar ('\n');
8859 }
8860 
8861 static void
dynamic_section_parisc_val(Elf_Internal_Dyn * entry)8862 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
8863 {
8864   switch (entry->d_tag)
8865     {
8866     case DT_HP_DLD_FLAGS:
8867       {
8868 	static struct
8869 	{
8870 	  long int bit;
8871 	  const char * str;
8872 	}
8873 	flags[] =
8874 	{
8875 	  { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
8876 	  { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
8877 	  { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
8878 	  { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
8879 	  { DT_HP_BIND_NOW, "HP_BIND_NOW" },
8880 	  { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
8881 	  { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
8882 	  { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
8883 	  { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
8884 	  { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
8885 	  { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
8886 	  { DT_HP_GST, "HP_GST" },
8887 	  { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
8888 	  { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
8889 	  { DT_HP_NODELETE, "HP_NODELETE" },
8890 	  { DT_HP_GROUP, "HP_GROUP" },
8891 	  { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
8892 	};
8893 	int first = 1;
8894 	size_t cnt;
8895 	bfd_vma val = entry->d_un.d_val;
8896 
8897 	for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
8898 	  if (val & flags[cnt].bit)
8899 	    {
8900 	      if (! first)
8901 		putchar (' ');
8902 	      fputs (flags[cnt].str, stdout);
8903 	      first = 0;
8904 	      val ^= flags[cnt].bit;
8905 	    }
8906 
8907 	if (val != 0 || first)
8908 	  {
8909 	    if (! first)
8910 	      putchar (' ');
8911 	    print_vma (val, HEX);
8912 	  }
8913       }
8914       break;
8915 
8916     default:
8917       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8918       break;
8919     }
8920   putchar ('\n');
8921 }
8922 
8923 #ifdef BFD64
8924 
8925 /* VMS vs Unix time offset and factor.  */
8926 
8927 #define VMS_EPOCH_OFFSET 35067168000000000LL
8928 #define VMS_GRANULARITY_FACTOR 10000000
8929 
8930 /* Display a VMS time in a human readable format.  */
8931 
8932 static void
print_vms_time(bfd_int64_t vmstime)8933 print_vms_time (bfd_int64_t vmstime)
8934 {
8935   struct tm *tm;
8936   time_t unxtime;
8937 
8938   unxtime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
8939   tm = gmtime (&unxtime);
8940   printf ("%04u-%02u-%02uT%02u:%02u:%02u",
8941           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
8942           tm->tm_hour, tm->tm_min, tm->tm_sec);
8943 }
8944 #endif /* BFD64 */
8945 
8946 static void
dynamic_section_ia64_val(Elf_Internal_Dyn * entry)8947 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
8948 {
8949   switch (entry->d_tag)
8950     {
8951     case DT_IA_64_PLT_RESERVE:
8952       /* First 3 slots reserved.  */
8953       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8954       printf (" -- ");
8955       print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
8956       break;
8957 
8958     case DT_IA_64_VMS_LINKTIME:
8959 #ifdef BFD64
8960       print_vms_time (entry->d_un.d_val);
8961 #endif
8962       break;
8963 
8964     case DT_IA_64_VMS_LNKFLAGS:
8965       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
8966       if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
8967         printf (" CALL_DEBUG");
8968       if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
8969         printf (" NOP0BUFS");
8970       if (entry->d_un.d_val & VMS_LF_P0IMAGE)
8971         printf (" P0IMAGE");
8972       if (entry->d_un.d_val & VMS_LF_MKTHREADS)
8973         printf (" MKTHREADS");
8974       if (entry->d_un.d_val & VMS_LF_UPCALLS)
8975         printf (" UPCALLS");
8976       if (entry->d_un.d_val & VMS_LF_IMGSTA)
8977         printf (" IMGSTA");
8978       if (entry->d_un.d_val & VMS_LF_INITIALIZE)
8979         printf (" INITIALIZE");
8980       if (entry->d_un.d_val & VMS_LF_MAIN)
8981         printf (" MAIN");
8982       if (entry->d_un.d_val & VMS_LF_EXE_INIT)
8983         printf (" EXE_INIT");
8984       if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
8985         printf (" TBK_IN_IMG");
8986       if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
8987         printf (" DBG_IN_IMG");
8988       if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
8989         printf (" TBK_IN_DSF");
8990       if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
8991         printf (" DBG_IN_DSF");
8992       if (entry->d_un.d_val & VMS_LF_SIGNATURES)
8993         printf (" SIGNATURES");
8994       if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
8995         printf (" REL_SEG_OFF");
8996       break;
8997 
8998     default:
8999       print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9000       break;
9001     }
9002   putchar ('\n');
9003 }
9004 
9005 static int
get_32bit_dynamic_section(FILE * file)9006 get_32bit_dynamic_section (FILE * file)
9007 {
9008   Elf32_External_Dyn * edyn;
9009   Elf32_External_Dyn * ext;
9010   Elf_Internal_Dyn * entry;
9011 
9012   edyn = (Elf32_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
9013                                           dynamic_size, _("dynamic section"));
9014   if (!edyn)
9015     return 0;
9016 
9017   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
9018      might not have the luxury of section headers.  Look for the DT_NULL
9019      terminator to determine the number of entries.  */
9020   for (ext = edyn, dynamic_nent = 0;
9021        (char *) (ext + 1) <= (char *) edyn + dynamic_size;
9022        ext++)
9023     {
9024       dynamic_nent++;
9025       if (BYTE_GET (ext->d_tag) == DT_NULL)
9026 	break;
9027     }
9028 
9029   dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
9030                                                   sizeof (* entry));
9031   if (dynamic_section == NULL)
9032     {
9033       error (_("Out of memory allocating space for %lu dynamic entries\n"),
9034 	     (unsigned long) dynamic_nent);
9035       free (edyn);
9036       return 0;
9037     }
9038 
9039   for (ext = edyn, entry = dynamic_section;
9040        entry < dynamic_section + dynamic_nent;
9041        ext++, entry++)
9042     {
9043       entry->d_tag      = BYTE_GET (ext->d_tag);
9044       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
9045     }
9046 
9047   free (edyn);
9048 
9049   return 1;
9050 }
9051 
9052 static int
get_64bit_dynamic_section(FILE * file)9053 get_64bit_dynamic_section (FILE * file)
9054 {
9055   Elf64_External_Dyn * edyn;
9056   Elf64_External_Dyn * ext;
9057   Elf_Internal_Dyn * entry;
9058 
9059   /* Read in the data.  */
9060   edyn = (Elf64_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
9061                                           dynamic_size, _("dynamic section"));
9062   if (!edyn)
9063     return 0;
9064 
9065   /* SGI's ELF has more than one section in the DYNAMIC segment, and we
9066      might not have the luxury of section headers.  Look for the DT_NULL
9067      terminator to determine the number of entries.  */
9068   for (ext = edyn, dynamic_nent = 0;
9069        /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer.  */
9070        (char *) (ext + 1) <= (char *) edyn + dynamic_size;
9071        ext++)
9072     {
9073       dynamic_nent++;
9074       if (BYTE_GET (ext->d_tag) == DT_NULL)
9075 	break;
9076     }
9077 
9078   dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
9079                                                   sizeof (* entry));
9080   if (dynamic_section == NULL)
9081     {
9082       error (_("Out of memory allocating space for %lu dynamic entries\n"),
9083 	     (unsigned long) dynamic_nent);
9084       free (edyn);
9085       return 0;
9086     }
9087 
9088   /* Convert from external to internal formats.  */
9089   for (ext = edyn, entry = dynamic_section;
9090        entry < dynamic_section + dynamic_nent;
9091        ext++, entry++)
9092     {
9093       entry->d_tag      = BYTE_GET (ext->d_tag);
9094       entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
9095     }
9096 
9097   free (edyn);
9098 
9099   return 1;
9100 }
9101 
9102 static void
print_dynamic_flags(bfd_vma flags)9103 print_dynamic_flags (bfd_vma flags)
9104 {
9105   int first = 1;
9106 
9107   while (flags)
9108     {
9109       bfd_vma flag;
9110 
9111       flag = flags & - flags;
9112       flags &= ~ flag;
9113 
9114       if (first)
9115 	first = 0;
9116       else
9117 	putc (' ', stdout);
9118 
9119       switch (flag)
9120 	{
9121 	case DF_ORIGIN:		fputs ("ORIGIN", stdout); break;
9122 	case DF_SYMBOLIC:	fputs ("SYMBOLIC", stdout); break;
9123 	case DF_TEXTREL:	fputs ("TEXTREL", stdout); break;
9124 	case DF_BIND_NOW:	fputs ("BIND_NOW", stdout); break;
9125 	case DF_STATIC_TLS:	fputs ("STATIC_TLS", stdout); break;
9126 	default:		fputs (_("unknown"), stdout); break;
9127 	}
9128     }
9129   puts ("");
9130 }
9131 
9132 /* Parse and display the contents of the dynamic section.  */
9133 
9134 static int
process_dynamic_section(FILE * file)9135 process_dynamic_section (FILE * file)
9136 {
9137   Elf_Internal_Dyn * entry;
9138 
9139   if (dynamic_size == 0)
9140     {
9141       if (do_dynamic)
9142 	printf (_("\nThere is no dynamic section in this file.\n"));
9143 
9144       return 1;
9145     }
9146 
9147   if (is_32bit_elf)
9148     {
9149       if (! get_32bit_dynamic_section (file))
9150 	return 0;
9151     }
9152   else if (! get_64bit_dynamic_section (file))
9153     return 0;
9154 
9155   /* Find the appropriate symbol table.  */
9156   if (dynamic_symbols == NULL)
9157     {
9158       for (entry = dynamic_section;
9159 	   entry < dynamic_section + dynamic_nent;
9160 	   ++entry)
9161 	{
9162 	  Elf_Internal_Shdr section;
9163 
9164 	  if (entry->d_tag != DT_SYMTAB)
9165 	    continue;
9166 
9167 	  dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
9168 
9169 	  /* Since we do not know how big the symbol table is,
9170 	     we default to reading in the entire file (!) and
9171 	     processing that.  This is overkill, I know, but it
9172 	     should work.  */
9173 	  section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
9174 
9175 	  if (archive_file_offset != 0)
9176 	    section.sh_size = archive_file_size - section.sh_offset;
9177 	  else
9178 	    {
9179 	      if (fseek (file, 0, SEEK_END))
9180 		error (_("Unable to seek to end of file!\n"));
9181 
9182 	      section.sh_size = ftell (file) - section.sh_offset;
9183 	    }
9184 
9185 	  if (is_32bit_elf)
9186 	    section.sh_entsize = sizeof (Elf32_External_Sym);
9187 	  else
9188 	    section.sh_entsize = sizeof (Elf64_External_Sym);
9189 	  section.sh_name = string_table_length;
9190 
9191 	  dynamic_symbols = GET_ELF_SYMBOLS (file, &section, & num_dynamic_syms);
9192 	  if (num_dynamic_syms < 1)
9193 	    {
9194 	      error (_("Unable to determine the number of symbols to load\n"));
9195 	      continue;
9196 	    }
9197 	}
9198     }
9199 
9200   /* Similarly find a string table.  */
9201   if (dynamic_strings == NULL)
9202     {
9203       for (entry = dynamic_section;
9204 	   entry < dynamic_section + dynamic_nent;
9205 	   ++entry)
9206 	{
9207 	  unsigned long offset;
9208 	  long str_tab_len;
9209 
9210 	  if (entry->d_tag != DT_STRTAB)
9211 	    continue;
9212 
9213 	  dynamic_info[DT_STRTAB] = entry->d_un.d_val;
9214 
9215 	  /* Since we do not know how big the string table is,
9216 	     we default to reading in the entire file (!) and
9217 	     processing that.  This is overkill, I know, but it
9218 	     should work.  */
9219 
9220 	  offset = offset_from_vma (file, entry->d_un.d_val, 0);
9221 
9222 	  if (archive_file_offset != 0)
9223 	    str_tab_len = archive_file_size - offset;
9224 	  else
9225 	    {
9226 	      if (fseek (file, 0, SEEK_END))
9227 		error (_("Unable to seek to end of file\n"));
9228 	      str_tab_len = ftell (file) - offset;
9229 	    }
9230 
9231 	  if (str_tab_len < 1)
9232 	    {
9233 	      error
9234 		(_("Unable to determine the length of the dynamic string table\n"));
9235 	      continue;
9236 	    }
9237 
9238 	  dynamic_strings = (char *) get_data (NULL, file, offset, 1,
9239                                                str_tab_len,
9240                                                _("dynamic string table"));
9241 	  dynamic_strings_length = dynamic_strings == NULL ? 0 : str_tab_len;
9242 	  break;
9243 	}
9244     }
9245 
9246   /* And find the syminfo section if available.  */
9247   if (dynamic_syminfo == NULL)
9248     {
9249       unsigned long syminsz = 0;
9250 
9251       for (entry = dynamic_section;
9252 	   entry < dynamic_section + dynamic_nent;
9253 	   ++entry)
9254 	{
9255 	  if (entry->d_tag == DT_SYMINENT)
9256 	    {
9257 	      /* Note: these braces are necessary to avoid a syntax
9258 		 error from the SunOS4 C compiler.  */
9259 	      /* PR binutils/17531: A corrupt file can trigger this test.
9260 		 So do not use an assert, instead generate an error message.  */
9261 	      if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
9262 		error (_("Bad value (%d) for SYMINENT entry\n"),
9263 		       (int) entry->d_un.d_val);
9264 	    }
9265 	  else if (entry->d_tag == DT_SYMINSZ)
9266 	    syminsz = entry->d_un.d_val;
9267 	  else if (entry->d_tag == DT_SYMINFO)
9268 	    dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
9269 						      syminsz);
9270 	}
9271 
9272       if (dynamic_syminfo_offset != 0 && syminsz != 0)
9273 	{
9274 	  Elf_External_Syminfo * extsyminfo;
9275 	  Elf_External_Syminfo * extsym;
9276 	  Elf_Internal_Syminfo * syminfo;
9277 
9278 	  /* There is a syminfo section.  Read the data.  */
9279 	  extsyminfo = (Elf_External_Syminfo *)
9280               get_data (NULL, file, dynamic_syminfo_offset, 1, syminsz,
9281                         _("symbol information"));
9282 	  if (!extsyminfo)
9283 	    return 0;
9284 
9285 	  dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
9286 	  if (dynamic_syminfo == NULL)
9287 	    {
9288 	      error (_("Out of memory allocating %lu byte for dynamic symbol info\n"),
9289 		     (unsigned long) syminsz);
9290 	      return 0;
9291 	    }
9292 
9293 	  dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
9294 	  for (syminfo = dynamic_syminfo, extsym = extsyminfo;
9295 	       syminfo < dynamic_syminfo + dynamic_syminfo_nent;
9296 	       ++syminfo, ++extsym)
9297 	    {
9298 	      syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
9299 	      syminfo->si_flags = BYTE_GET (extsym->si_flags);
9300 	    }
9301 
9302 	  free (extsyminfo);
9303 	}
9304     }
9305 
9306   if (do_dynamic && dynamic_addr)
9307     printf (_("\nDynamic section at offset 0x%lx contains %lu entries:\n"),
9308 	    dynamic_addr, (unsigned long) dynamic_nent);
9309   if (do_dynamic)
9310     printf (_("  Tag        Type                         Name/Value\n"));
9311 
9312   for (entry = dynamic_section;
9313        entry < dynamic_section + dynamic_nent;
9314        entry++)
9315     {
9316       if (do_dynamic)
9317 	{
9318 	  const char * dtype;
9319 
9320 	  putchar (' ');
9321 	  print_vma (entry->d_tag, FULL_HEX);
9322 	  dtype = get_dynamic_type (entry->d_tag);
9323 	  printf (" (%s)%*s", dtype,
9324 		  ((is_32bit_elf ? 27 : 19)
9325 		   - (int) strlen (dtype)),
9326 		  " ");
9327 	}
9328 
9329       switch (entry->d_tag)
9330 	{
9331 	case DT_FLAGS:
9332 	  if (do_dynamic)
9333 	    print_dynamic_flags (entry->d_un.d_val);
9334 	  break;
9335 
9336 	case DT_AUXILIARY:
9337 	case DT_FILTER:
9338 	case DT_CONFIG:
9339 	case DT_DEPAUDIT:
9340 	case DT_AUDIT:
9341 	  if (do_dynamic)
9342 	    {
9343 	      switch (entry->d_tag)
9344 		{
9345 		case DT_AUXILIARY:
9346 		  printf (_("Auxiliary library"));
9347 		  break;
9348 
9349 		case DT_FILTER:
9350 		  printf (_("Filter library"));
9351 		  break;
9352 
9353 		case DT_CONFIG:
9354 		  printf (_("Configuration file"));
9355 		  break;
9356 
9357 		case DT_DEPAUDIT:
9358 		  printf (_("Dependency audit library"));
9359 		  break;
9360 
9361 		case DT_AUDIT:
9362 		  printf (_("Audit library"));
9363 		  break;
9364 		}
9365 
9366 	      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
9367 		printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
9368 	      else
9369 		{
9370 		  printf (": ");
9371 		  print_vma (entry->d_un.d_val, PREFIX_HEX);
9372 		  putchar ('\n');
9373 		}
9374 	    }
9375 	  break;
9376 
9377 	case DT_FEATURE:
9378 	  if (do_dynamic)
9379 	    {
9380 	      printf (_("Flags:"));
9381 
9382 	      if (entry->d_un.d_val == 0)
9383 		printf (_(" None\n"));
9384 	      else
9385 		{
9386 		  unsigned long int val = entry->d_un.d_val;
9387 
9388 		  if (val & DTF_1_PARINIT)
9389 		    {
9390 		      printf (" PARINIT");
9391 		      val ^= DTF_1_PARINIT;
9392 		    }
9393 		  if (val & DTF_1_CONFEXP)
9394 		    {
9395 		      printf (" CONFEXP");
9396 		      val ^= DTF_1_CONFEXP;
9397 		    }
9398 		  if (val != 0)
9399 		    printf (" %lx", val);
9400 		  puts ("");
9401 		}
9402 	    }
9403 	  break;
9404 
9405 	case DT_POSFLAG_1:
9406 	  if (do_dynamic)
9407 	    {
9408 	      printf (_("Flags:"));
9409 
9410 	      if (entry->d_un.d_val == 0)
9411 		printf (_(" None\n"));
9412 	      else
9413 		{
9414 		  unsigned long int val = entry->d_un.d_val;
9415 
9416 		  if (val & DF_P1_LAZYLOAD)
9417 		    {
9418 		      printf (" LAZYLOAD");
9419 		      val ^= DF_P1_LAZYLOAD;
9420 		    }
9421 		  if (val & DF_P1_GROUPPERM)
9422 		    {
9423 		      printf (" GROUPPERM");
9424 		      val ^= DF_P1_GROUPPERM;
9425 		    }
9426 		  if (val != 0)
9427 		    printf (" %lx", val);
9428 		  puts ("");
9429 		}
9430 	    }
9431 	  break;
9432 
9433 	case DT_FLAGS_1:
9434 	  if (do_dynamic)
9435 	    {
9436 	      printf (_("Flags:"));
9437 	      if (entry->d_un.d_val == 0)
9438 		printf (_(" None\n"));
9439 	      else
9440 		{
9441 		  unsigned long int val = entry->d_un.d_val;
9442 
9443 		  if (val & DF_1_NOW)
9444 		    {
9445 		      printf (" NOW");
9446 		      val ^= DF_1_NOW;
9447 		    }
9448 		  if (val & DF_1_GLOBAL)
9449 		    {
9450 		      printf (" GLOBAL");
9451 		      val ^= DF_1_GLOBAL;
9452 		    }
9453 		  if (val & DF_1_GROUP)
9454 		    {
9455 		      printf (" GROUP");
9456 		      val ^= DF_1_GROUP;
9457 		    }
9458 		  if (val & DF_1_NODELETE)
9459 		    {
9460 		      printf (" NODELETE");
9461 		      val ^= DF_1_NODELETE;
9462 		    }
9463 		  if (val & DF_1_LOADFLTR)
9464 		    {
9465 		      printf (" LOADFLTR");
9466 		      val ^= DF_1_LOADFLTR;
9467 		    }
9468 		  if (val & DF_1_INITFIRST)
9469 		    {
9470 		      printf (" INITFIRST");
9471 		      val ^= DF_1_INITFIRST;
9472 		    }
9473 		  if (val & DF_1_NOOPEN)
9474 		    {
9475 		      printf (" NOOPEN");
9476 		      val ^= DF_1_NOOPEN;
9477 		    }
9478 		  if (val & DF_1_ORIGIN)
9479 		    {
9480 		      printf (" ORIGIN");
9481 		      val ^= DF_1_ORIGIN;
9482 		    }
9483 		  if (val & DF_1_DIRECT)
9484 		    {
9485 		      printf (" DIRECT");
9486 		      val ^= DF_1_DIRECT;
9487 		    }
9488 		  if (val & DF_1_TRANS)
9489 		    {
9490 		      printf (" TRANS");
9491 		      val ^= DF_1_TRANS;
9492 		    }
9493 		  if (val & DF_1_INTERPOSE)
9494 		    {
9495 		      printf (" INTERPOSE");
9496 		      val ^= DF_1_INTERPOSE;
9497 		    }
9498 		  if (val & DF_1_NODEFLIB)
9499 		    {
9500 		      printf (" NODEFLIB");
9501 		      val ^= DF_1_NODEFLIB;
9502 		    }
9503 		  if (val & DF_1_NODUMP)
9504 		    {
9505 		      printf (" NODUMP");
9506 		      val ^= DF_1_NODUMP;
9507 		    }
9508 		  if (val & DF_1_CONFALT)
9509 		    {
9510 		      printf (" CONFALT");
9511 		      val ^= DF_1_CONFALT;
9512 		    }
9513 		  if (val & DF_1_ENDFILTEE)
9514 		    {
9515 		      printf (" ENDFILTEE");
9516 		      val ^= DF_1_ENDFILTEE;
9517 		    }
9518 		  if (val & DF_1_DISPRELDNE)
9519 		    {
9520 		      printf (" DISPRELDNE");
9521 		      val ^= DF_1_DISPRELDNE;
9522 		    }
9523 		  if (val & DF_1_DISPRELPND)
9524 		    {
9525 		      printf (" DISPRELPND");
9526 		      val ^= DF_1_DISPRELPND;
9527 		    }
9528 		  if (val & DF_1_NODIRECT)
9529 		    {
9530 		      printf (" NODIRECT");
9531 		      val ^= DF_1_NODIRECT;
9532 		    }
9533 		  if (val & DF_1_IGNMULDEF)
9534 		    {
9535 		      printf (" IGNMULDEF");
9536 		      val ^= DF_1_IGNMULDEF;
9537 		    }
9538 		  if (val & DF_1_NOKSYMS)
9539 		    {
9540 		      printf (" NOKSYMS");
9541 		      val ^= DF_1_NOKSYMS;
9542 		    }
9543 		  if (val & DF_1_NOHDR)
9544 		    {
9545 		      printf (" NOHDR");
9546 		      val ^= DF_1_NOHDR;
9547 		    }
9548 		  if (val & DF_1_EDITED)
9549 		    {
9550 		      printf (" EDITED");
9551 		      val ^= DF_1_EDITED;
9552 		    }
9553 		  if (val & DF_1_NORELOC)
9554 		    {
9555 		      printf (" NORELOC");
9556 		      val ^= DF_1_NORELOC;
9557 		    }
9558 		  if (val & DF_1_SYMINTPOSE)
9559 		    {
9560 		      printf (" SYMINTPOSE");
9561 		      val ^= DF_1_SYMINTPOSE;
9562 		    }
9563 		  if (val & DF_1_GLOBAUDIT)
9564 		    {
9565 		      printf (" GLOBAUDIT");
9566 		      val ^= DF_1_GLOBAUDIT;
9567 		    }
9568 		  if (val & DF_1_SINGLETON)
9569 		    {
9570 		      printf (" SINGLETON");
9571 		      val ^= DF_1_SINGLETON;
9572 		    }
9573 		  if (val & DF_1_STUB)
9574 		    {
9575 		      printf (" STUB");
9576 		      val ^= DF_1_STUB;
9577 		    }
9578 		  if (val & DF_1_PIE)
9579 		    {
9580 		      printf (" PIE");
9581 		      val ^= DF_1_PIE;
9582 		    }
9583 		  if (val != 0)
9584 		    printf (" %lx", val);
9585 		  puts ("");
9586 		}
9587 	    }
9588 	  break;
9589 
9590 	case DT_PLTREL:
9591 	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
9592 	  if (do_dynamic)
9593 	    puts (get_dynamic_type (entry->d_un.d_val));
9594 	  break;
9595 
9596 	case DT_NULL	:
9597 	case DT_NEEDED	:
9598 	case DT_PLTGOT	:
9599 	case DT_HASH	:
9600 	case DT_STRTAB	:
9601 	case DT_SYMTAB	:
9602 	case DT_RELA	:
9603 	case DT_INIT	:
9604 	case DT_FINI	:
9605 	case DT_SONAME	:
9606 	case DT_RPATH	:
9607 	case DT_SYMBOLIC:
9608 	case DT_REL	:
9609 	case DT_DEBUG	:
9610 	case DT_TEXTREL	:
9611 	case DT_JMPREL	:
9612 	case DT_RUNPATH	:
9613 	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
9614 
9615 	  if (do_dynamic)
9616 	    {
9617 	      char * name;
9618 
9619 	      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
9620 		name = GET_DYNAMIC_NAME (entry->d_un.d_val);
9621 	      else
9622 		name = NULL;
9623 
9624 	      if (name)
9625 		{
9626 		  switch (entry->d_tag)
9627 		    {
9628 		    case DT_NEEDED:
9629 		      printf (_("Shared library: [%s]"), name);
9630 
9631 		      if (streq (name, program_interpreter))
9632 			printf (_(" program interpreter"));
9633 		      break;
9634 
9635 		    case DT_SONAME:
9636 		      printf (_("Library soname: [%s]"), name);
9637 		      break;
9638 
9639 		    case DT_RPATH:
9640 		      printf (_("Library rpath: [%s]"), name);
9641 		      break;
9642 
9643 		    case DT_RUNPATH:
9644 		      printf (_("Library runpath: [%s]"), name);
9645 		      break;
9646 
9647 		    default:
9648 		      print_vma (entry->d_un.d_val, PREFIX_HEX);
9649 		      break;
9650 		    }
9651 		}
9652 	      else
9653 		print_vma (entry->d_un.d_val, PREFIX_HEX);
9654 
9655 	      putchar ('\n');
9656 	    }
9657 	  break;
9658 
9659 	case DT_PLTRELSZ:
9660 	case DT_RELASZ	:
9661 	case DT_STRSZ	:
9662 	case DT_RELSZ	:
9663 	case DT_RELAENT	:
9664 	case DT_SYMENT	:
9665 	case DT_RELENT	:
9666 	  dynamic_info[entry->d_tag] = entry->d_un.d_val;
9667 	case DT_PLTPADSZ:
9668 	case DT_MOVEENT	:
9669 	case DT_MOVESZ	:
9670 	case DT_INIT_ARRAYSZ:
9671 	case DT_FINI_ARRAYSZ:
9672 	case DT_GNU_CONFLICTSZ:
9673 	case DT_GNU_LIBLISTSZ:
9674 	  if (do_dynamic)
9675 	    {
9676 	      print_vma (entry->d_un.d_val, UNSIGNED);
9677 	      printf (_(" (bytes)\n"));
9678 	    }
9679 	  break;
9680 
9681 	case DT_VERDEFNUM:
9682 	case DT_VERNEEDNUM:
9683 	case DT_RELACOUNT:
9684 	case DT_RELCOUNT:
9685 	  if (do_dynamic)
9686 	    {
9687 	      print_vma (entry->d_un.d_val, UNSIGNED);
9688 	      putchar ('\n');
9689 	    }
9690 	  break;
9691 
9692 	case DT_SYMINSZ:
9693 	case DT_SYMINENT:
9694 	case DT_SYMINFO:
9695 	case DT_USED:
9696 	case DT_INIT_ARRAY:
9697 	case DT_FINI_ARRAY:
9698 	  if (do_dynamic)
9699 	    {
9700 	      if (entry->d_tag == DT_USED
9701 		  && VALID_DYNAMIC_NAME (entry->d_un.d_val))
9702 		{
9703 		  char * name = GET_DYNAMIC_NAME (entry->d_un.d_val);
9704 
9705 		  if (*name)
9706 		    {
9707 		      printf (_("Not needed object: [%s]\n"), name);
9708 		      break;
9709 		    }
9710 		}
9711 
9712 	      print_vma (entry->d_un.d_val, PREFIX_HEX);
9713 	      putchar ('\n');
9714 	    }
9715 	  break;
9716 
9717 	case DT_BIND_NOW:
9718 	  /* The value of this entry is ignored.  */
9719 	  if (do_dynamic)
9720 	    putchar ('\n');
9721 	  break;
9722 
9723 	case DT_GNU_PRELINKED:
9724 	  if (do_dynamic)
9725 	    {
9726 	      struct tm * tmp;
9727 	      time_t atime = entry->d_un.d_val;
9728 
9729 	      tmp = gmtime (&atime);
9730 	      /* PR 17533 file: 041-1244816-0.004.  */
9731 	      if (tmp == NULL)
9732 		printf (_("<corrupt time val: %lx"),
9733 			(unsigned long) atime);
9734 	      else
9735 		printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
9736 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9737 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9738 
9739 	    }
9740 	  break;
9741 
9742 	case DT_GNU_HASH:
9743 	  dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
9744 	  if (do_dynamic)
9745 	    {
9746 	      print_vma (entry->d_un.d_val, PREFIX_HEX);
9747 	      putchar ('\n');
9748 	    }
9749 	  break;
9750 
9751 	default:
9752 	  if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
9753 	    version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
9754 	      entry->d_un.d_val;
9755 
9756 	  if (do_dynamic)
9757 	    {
9758 	      switch (elf_header.e_machine)
9759 		{
9760 		case EM_MIPS:
9761 		case EM_MIPS_RS3_LE:
9762 		  dynamic_section_mips_val (entry);
9763 		  break;
9764 		case EM_PARISC:
9765 		  dynamic_section_parisc_val (entry);
9766 		  break;
9767 		case EM_IA_64:
9768 		  dynamic_section_ia64_val (entry);
9769 		  break;
9770 		default:
9771 		  print_vma (entry->d_un.d_val, PREFIX_HEX);
9772 		  putchar ('\n');
9773 		}
9774 	    }
9775 	  break;
9776 	}
9777     }
9778 
9779   return 1;
9780 }
9781 
9782 static char *
get_ver_flags(unsigned int flags)9783 get_ver_flags (unsigned int flags)
9784 {
9785   static char buff[32];
9786 
9787   buff[0] = 0;
9788 
9789   if (flags == 0)
9790     return _("none");
9791 
9792   if (flags & VER_FLG_BASE)
9793     strcat (buff, "BASE ");
9794 
9795   if (flags & VER_FLG_WEAK)
9796     {
9797       if (flags & VER_FLG_BASE)
9798 	strcat (buff, "| ");
9799 
9800       strcat (buff, "WEAK ");
9801     }
9802 
9803   if (flags & VER_FLG_INFO)
9804     {
9805       if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
9806 	strcat (buff, "| ");
9807 
9808       strcat (buff, "INFO ");
9809     }
9810 
9811   if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
9812     strcat (buff, _("| <unknown>"));
9813 
9814   return buff;
9815 }
9816 
9817 /* Display the contents of the version sections.  */
9818 
9819 static int
process_version_sections(FILE * file)9820 process_version_sections (FILE * file)
9821 {
9822   Elf_Internal_Shdr * section;
9823   unsigned i;
9824   int found = 0;
9825 
9826   if (! do_version)
9827     return 1;
9828 
9829   for (i = 0, section = section_headers;
9830        i < elf_header.e_shnum;
9831        i++, section++)
9832     {
9833       switch (section->sh_type)
9834 	{
9835 	case SHT_GNU_verdef:
9836 	  {
9837 	    Elf_External_Verdef * edefs;
9838 	    unsigned int idx;
9839 	    unsigned int cnt;
9840 	    char * endbuf;
9841 
9842 	    found = 1;
9843 
9844 	    printf (_("\nVersion definition section '%s' contains %u entries:\n"),
9845 		    printable_section_name (section),
9846 		    section->sh_info);
9847 
9848 	    printf (_("  Addr: 0x"));
9849 	    printf_vma (section->sh_addr);
9850 	    printf (_("  Offset: %#08lx  Link: %u (%s)"),
9851 		    (unsigned long) section->sh_offset, section->sh_link,
9852 		    printable_section_name_from_index (section->sh_link));
9853 
9854 	    edefs = (Elf_External_Verdef *)
9855                 get_data (NULL, file, section->sh_offset, 1,section->sh_size,
9856                           _("version definition section"));
9857 	    if (!edefs)
9858 	      break;
9859 	    endbuf = (char *) edefs + section->sh_size;
9860 
9861 	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
9862 	      {
9863 		char * vstart;
9864 		Elf_External_Verdef * edef;
9865 		Elf_Internal_Verdef ent;
9866 		Elf_External_Verdaux * eaux;
9867 		Elf_Internal_Verdaux aux;
9868 		int j;
9869 		int isum;
9870 
9871 		/* Check for very large indicies.  */
9872 		if (idx > (size_t) (endbuf - (char *) edefs))
9873 		  break;
9874 
9875 		vstart = ((char *) edefs) + idx;
9876 		if (vstart + sizeof (*edef) > endbuf)
9877 		  break;
9878 
9879 		edef = (Elf_External_Verdef *) vstart;
9880 
9881 		ent.vd_version = BYTE_GET (edef->vd_version);
9882 		ent.vd_flags   = BYTE_GET (edef->vd_flags);
9883 		ent.vd_ndx     = BYTE_GET (edef->vd_ndx);
9884 		ent.vd_cnt     = BYTE_GET (edef->vd_cnt);
9885 		ent.vd_hash    = BYTE_GET (edef->vd_hash);
9886 		ent.vd_aux     = BYTE_GET (edef->vd_aux);
9887 		ent.vd_next    = BYTE_GET (edef->vd_next);
9888 
9889 		printf (_("  %#06x: Rev: %d  Flags: %s"),
9890 			idx, ent.vd_version, get_ver_flags (ent.vd_flags));
9891 
9892 		printf (_("  Index: %d  Cnt: %d  "),
9893 			ent.vd_ndx, ent.vd_cnt);
9894 
9895 		/* Check for overflow.  */
9896 		if (ent.vd_aux > (size_t) (endbuf - vstart))
9897 		  break;
9898 
9899 		vstart += ent.vd_aux;
9900 
9901 		eaux = (Elf_External_Verdaux *) vstart;
9902 
9903 		aux.vda_name = BYTE_GET (eaux->vda_name);
9904 		aux.vda_next = BYTE_GET (eaux->vda_next);
9905 
9906 		if (VALID_DYNAMIC_NAME (aux.vda_name))
9907 		  printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
9908 		else
9909 		  printf (_("Name index: %ld\n"), aux.vda_name);
9910 
9911 		isum = idx + ent.vd_aux;
9912 
9913 		for (j = 1; j < ent.vd_cnt; j++)
9914 		  {
9915 		    /* Check for overflow.  */
9916 		    if (aux.vda_next > (size_t) (endbuf - vstart))
9917 		      break;
9918 
9919 		    isum   += aux.vda_next;
9920 		    vstart += aux.vda_next;
9921 
9922 		    eaux = (Elf_External_Verdaux *) vstart;
9923 		    if (vstart + sizeof (*eaux) > endbuf)
9924 		      break;
9925 
9926 		    aux.vda_name = BYTE_GET (eaux->vda_name);
9927 		    aux.vda_next = BYTE_GET (eaux->vda_next);
9928 
9929 		    if (VALID_DYNAMIC_NAME (aux.vda_name))
9930 		      printf (_("  %#06x: Parent %d: %s\n"),
9931 			      isum, j, GET_DYNAMIC_NAME (aux.vda_name));
9932 		    else
9933 		      printf (_("  %#06x: Parent %d, name index: %ld\n"),
9934 			      isum, j, aux.vda_name);
9935 		  }
9936 
9937 		if (j < ent.vd_cnt)
9938 		  printf (_("  Version def aux past end of section\n"));
9939 
9940 		/* PR 17531: file: id:000001,src:000172+005151,op:splice,rep:2.  */
9941 		if (idx + ent.vd_next <= idx)
9942 		  break;
9943 
9944 		idx += ent.vd_next;
9945 	      }
9946 
9947 	    if (cnt < section->sh_info)
9948 	      printf (_("  Version definition past end of section\n"));
9949 
9950 	    free (edefs);
9951 	  }
9952 	  break;
9953 
9954 	case SHT_GNU_verneed:
9955 	  {
9956 	    Elf_External_Verneed * eneed;
9957 	    unsigned int idx;
9958 	    unsigned int cnt;
9959 	    char * endbuf;
9960 
9961 	    found = 1;
9962 
9963 	    printf (_("\nVersion needs section '%s' contains %u entries:\n"),
9964 		    printable_section_name (section), section->sh_info);
9965 
9966 	    printf (_(" Addr: 0x"));
9967 	    printf_vma (section->sh_addr);
9968 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
9969 		    (unsigned long) section->sh_offset, section->sh_link,
9970 		    printable_section_name_from_index (section->sh_link));
9971 
9972 	    eneed = (Elf_External_Verneed *) get_data (NULL, file,
9973                                                        section->sh_offset, 1,
9974                                                        section->sh_size,
9975                                                        _("Version Needs section"));
9976 	    if (!eneed)
9977 	      break;
9978 	    endbuf = (char *) eneed + section->sh_size;
9979 
9980 	    for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
9981 	      {
9982 		Elf_External_Verneed * entry;
9983 		Elf_Internal_Verneed ent;
9984 		int j;
9985 		int isum;
9986 		char * vstart;
9987 
9988 		if (idx > (size_t) (endbuf - (char *) eneed))
9989 		  break;
9990 
9991 		vstart = ((char *) eneed) + idx;
9992 		if (vstart + sizeof (*entry) > endbuf)
9993 		  break;
9994 
9995 		entry = (Elf_External_Verneed *) vstart;
9996 
9997 		ent.vn_version = BYTE_GET (entry->vn_version);
9998 		ent.vn_cnt     = BYTE_GET (entry->vn_cnt);
9999 		ent.vn_file    = BYTE_GET (entry->vn_file);
10000 		ent.vn_aux     = BYTE_GET (entry->vn_aux);
10001 		ent.vn_next    = BYTE_GET (entry->vn_next);
10002 
10003 		printf (_("  %#06x: Version: %d"), idx, ent.vn_version);
10004 
10005 		if (VALID_DYNAMIC_NAME (ent.vn_file))
10006 		  printf (_("  File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
10007 		else
10008 		  printf (_("  File: %lx"), ent.vn_file);
10009 
10010 		printf (_("  Cnt: %d\n"), ent.vn_cnt);
10011 
10012 		/* Check for overflow.  */
10013 		if (ent.vn_aux > (size_t) (endbuf - vstart))
10014 		  break;
10015 		vstart += ent.vn_aux;
10016 
10017 		for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
10018 		  {
10019 		    Elf_External_Vernaux * eaux;
10020 		    Elf_Internal_Vernaux aux;
10021 
10022 		    if (vstart + sizeof (*eaux) > endbuf)
10023 		      break;
10024 		    eaux = (Elf_External_Vernaux *) vstart;
10025 
10026 		    aux.vna_hash  = BYTE_GET (eaux->vna_hash);
10027 		    aux.vna_flags = BYTE_GET (eaux->vna_flags);
10028 		    aux.vna_other = BYTE_GET (eaux->vna_other);
10029 		    aux.vna_name  = BYTE_GET (eaux->vna_name);
10030 		    aux.vna_next  = BYTE_GET (eaux->vna_next);
10031 
10032 		    if (VALID_DYNAMIC_NAME (aux.vna_name))
10033 		      printf (_("  %#06x:   Name: %s"),
10034 			      isum, GET_DYNAMIC_NAME (aux.vna_name));
10035 		    else
10036 		      printf (_("  %#06x:   Name index: %lx"),
10037 			      isum, aux.vna_name);
10038 
10039 		    printf (_("  Flags: %s  Version: %d\n"),
10040 			    get_ver_flags (aux.vna_flags), aux.vna_other);
10041 
10042 		    /* Check for overflow.  */
10043 		    if (aux.vna_next > (size_t) (endbuf - vstart)
10044 			|| (aux.vna_next == 0 && j < ent.vn_cnt - 1))
10045 		      {
10046 			warn (_("Invalid vna_next field of %lx\n"),
10047 			      aux.vna_next);
10048 			j = ent.vn_cnt;
10049 			break;
10050 		      }
10051 		    isum   += aux.vna_next;
10052 		    vstart += aux.vna_next;
10053 		  }
10054 
10055 		if (j < ent.vn_cnt)
10056 		  warn (_("Missing Version Needs auxillary information\n"));
10057 
10058 		if (ent.vn_next == 0 && cnt < section->sh_info - 1)
10059 		  {
10060 		    warn (_("Corrupt Version Needs structure - offset to next structure is zero with entries still left to be processed\n"));
10061 		    cnt = section->sh_info;
10062 		    break;
10063 		  }
10064 		idx += ent.vn_next;
10065 	      }
10066 
10067 	    if (cnt < section->sh_info)
10068 	      warn (_("Missing Version Needs information\n"));
10069 
10070 	    free (eneed);
10071 	  }
10072 	  break;
10073 
10074 	case SHT_GNU_versym:
10075 	  {
10076 	    Elf_Internal_Shdr * link_section;
10077 	    size_t total;
10078 	    unsigned int cnt;
10079 	    unsigned char * edata;
10080 	    unsigned short * data;
10081 	    char * strtab;
10082 	    Elf_Internal_Sym * symbols;
10083 	    Elf_Internal_Shdr * string_sec;
10084 	    unsigned long num_syms;
10085 	    long off;
10086 
10087 	    if (section->sh_link >= elf_header.e_shnum)
10088 	      break;
10089 
10090 	    link_section = section_headers + section->sh_link;
10091 	    total = section->sh_size / sizeof (Elf_External_Versym);
10092 
10093 	    if (link_section->sh_link >= elf_header.e_shnum)
10094 	      break;
10095 
10096 	    found = 1;
10097 
10098 	    symbols = GET_ELF_SYMBOLS (file, link_section, & num_syms);
10099 	    if (symbols == NULL)
10100 	      break;
10101 
10102 	    string_sec = section_headers + link_section->sh_link;
10103 
10104 	    strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
10105                                         string_sec->sh_size,
10106                                         _("version string table"));
10107 	    if (!strtab)
10108 	      {
10109 		free (symbols);
10110 		break;
10111 	      }
10112 
10113 	    printf (_("\nVersion symbols section '%s' contains %lu entries:\n"),
10114 		    printable_section_name (section), (unsigned long) total);
10115 
10116 	    printf (_(" Addr: "));
10117 	    printf_vma (section->sh_addr);
10118 	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
10119 		    (unsigned long) section->sh_offset, section->sh_link,
10120 		    printable_section_name (link_section));
10121 
10122 	    off = offset_from_vma (file,
10123 				   version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
10124 				   total * sizeof (short));
10125 	    edata = (unsigned char *) get_data (NULL, file, off, total,
10126                                                 sizeof (short),
10127                                                 _("version symbol data"));
10128 	    if (!edata)
10129 	      {
10130 		free (strtab);
10131 		free (symbols);
10132 		break;
10133 	      }
10134 
10135 	    data = (short unsigned int *) cmalloc (total, sizeof (short));
10136 
10137 	    for (cnt = total; cnt --;)
10138 	      data[cnt] = byte_get (edata + cnt * sizeof (short),
10139 				    sizeof (short));
10140 
10141 	    free (edata);
10142 
10143 	    for (cnt = 0; cnt < total; cnt += 4)
10144 	      {
10145 		int j, nn;
10146 		char *name;
10147 		char *invalid = _("*invalid*");
10148 
10149 		printf ("  %03x:", cnt);
10150 
10151 		for (j = 0; (j < 4) && (cnt + j) < total; ++j)
10152 		  switch (data[cnt + j])
10153 		    {
10154 		    case 0:
10155 		      fputs (_("   0 (*local*)    "), stdout);
10156 		      break;
10157 
10158 		    case 1:
10159 		      fputs (_("   1 (*global*)   "), stdout);
10160 		      break;
10161 
10162 		    default:
10163 		      nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
10164 				   data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
10165 
10166 		      /* If this index value is greater than the size of the symbols
10167 		         array, break to avoid an out-of-bounds read.  */
10168 		      if ((unsigned long)(cnt + j) >= num_syms)
10169 		        {
10170 		          warn (_("invalid index into symbol array\n"));
10171 		          break;
10172 			}
10173 
10174 		      name = NULL;
10175 		      if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
10176 			{
10177 			  Elf_Internal_Verneed ivn;
10178 			  unsigned long offset;
10179 
10180 			  offset = offset_from_vma
10181 			    (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
10182 			     sizeof (Elf_External_Verneed));
10183 
10184 			  do
10185 			    {
10186 			      Elf_Internal_Vernaux ivna;
10187 			      Elf_External_Verneed evn;
10188 			      Elf_External_Vernaux evna;
10189 			      unsigned long a_off;
10190 
10191 			      if (get_data (&evn, file, offset, sizeof (evn), 1,
10192 					    _("version need")) == NULL)
10193 				break;
10194 
10195 			      ivn.vn_aux  = BYTE_GET (evn.vn_aux);
10196 			      ivn.vn_next = BYTE_GET (evn.vn_next);
10197 
10198 			      a_off = offset + ivn.vn_aux;
10199 
10200 			      do
10201 				{
10202 				  if (get_data (&evna, file, a_off, sizeof (evna),
10203 						1, _("version need aux (2)")) == NULL)
10204 				    {
10205 				      ivna.vna_next  = 0;
10206 				      ivna.vna_other = 0;
10207 				    }
10208 				  else
10209 				    {
10210 				      ivna.vna_next  = BYTE_GET (evna.vna_next);
10211 				      ivna.vna_other = BYTE_GET (evna.vna_other);
10212 				    }
10213 
10214 				  a_off += ivna.vna_next;
10215 				}
10216 			      while (ivna.vna_other != data[cnt + j]
10217 				     && ivna.vna_next != 0);
10218 
10219 			      if (ivna.vna_other == data[cnt + j])
10220 				{
10221 				  ivna.vna_name = BYTE_GET (evna.vna_name);
10222 
10223 				  if (ivna.vna_name >= string_sec->sh_size)
10224 				    name = invalid;
10225 				  else
10226 				    name = strtab + ivna.vna_name;
10227 				  break;
10228 				}
10229 
10230 			      offset += ivn.vn_next;
10231 			    }
10232 			  while (ivn.vn_next);
10233 			}
10234 
10235 		      if (data[cnt + j] != 0x8001
10236 			  && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
10237 			{
10238 			  Elf_Internal_Verdef ivd;
10239 			  Elf_External_Verdef evd;
10240 			  unsigned long offset;
10241 
10242 			  offset = offset_from_vma
10243 			    (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
10244 			     sizeof evd);
10245 
10246 			  do
10247 			    {
10248 			      if (get_data (&evd, file, offset, sizeof (evd), 1,
10249 					    _("version def")) == NULL)
10250 				{
10251 				  ivd.vd_next = 0;
10252 				  /* PR 17531: file: 046-1082287-0.004.  */
10253 				  ivd.vd_ndx  = (data[cnt + j] & VERSYM_VERSION) + 1;
10254 				  break;
10255 				}
10256 			      else
10257 				{
10258 				  ivd.vd_next = BYTE_GET (evd.vd_next);
10259 				  ivd.vd_ndx  = BYTE_GET (evd.vd_ndx);
10260 				}
10261 
10262 			      offset += ivd.vd_next;
10263 			    }
10264 			  while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
10265 				 && ivd.vd_next != 0);
10266 
10267 			  if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
10268 			    {
10269 			      Elf_External_Verdaux evda;
10270 			      Elf_Internal_Verdaux ivda;
10271 
10272 			      ivd.vd_aux = BYTE_GET (evd.vd_aux);
10273 
10274 			      if (get_data (&evda, file,
10275 					    offset - ivd.vd_next + ivd.vd_aux,
10276 					    sizeof (evda), 1,
10277 					    _("version def aux")) == NULL)
10278 				break;
10279 
10280 			      ivda.vda_name = BYTE_GET (evda.vda_name);
10281 
10282 			      if (ivda.vda_name >= string_sec->sh_size)
10283 				name = invalid;
10284 			      else if (name != NULL && name != invalid)
10285 				name = _("*both*");
10286 			      else
10287 				name = strtab + ivda.vda_name;
10288 			    }
10289 			}
10290 		      if (name != NULL)
10291 			nn += printf ("(%s%-*s",
10292 				      name,
10293 				      12 - (int) strlen (name),
10294 				      ")");
10295 
10296 		      if (nn < 18)
10297 			printf ("%*c", 18 - nn, ' ');
10298 		    }
10299 
10300 		putchar ('\n');
10301 	      }
10302 
10303 	    free (data);
10304 	    free (strtab);
10305 	    free (symbols);
10306 	  }
10307 	  break;
10308 
10309 	default:
10310 	  break;
10311 	}
10312     }
10313 
10314   if (! found)
10315     printf (_("\nNo version information found in this file.\n"));
10316 
10317   return 1;
10318 }
10319 
10320 static const char *
get_symbol_binding(unsigned int binding)10321 get_symbol_binding (unsigned int binding)
10322 {
10323   static char buff[32];
10324 
10325   switch (binding)
10326     {
10327     case STB_LOCAL:	return "LOCAL";
10328     case STB_GLOBAL:	return "GLOBAL";
10329     case STB_WEAK:	return "WEAK";
10330     default:
10331       if (binding >= STB_LOPROC && binding <= STB_HIPROC)
10332 	snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
10333 		  binding);
10334       else if (binding >= STB_LOOS && binding <= STB_HIOS)
10335 	{
10336 	  if (binding == STB_GNU_UNIQUE
10337 	      && (elf_header.e_ident[EI_OSABI] == ELFOSABI_GNU
10338 		  /* GNU is still using the default value 0.  */
10339 		  || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
10340 	    return "UNIQUE";
10341 	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
10342 	}
10343       else
10344 	snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
10345       return buff;
10346     }
10347 }
10348 
10349 static const char *
get_symbol_type(unsigned int type)10350 get_symbol_type (unsigned int type)
10351 {
10352   static char buff[32];
10353 
10354   switch (type)
10355     {
10356     case STT_NOTYPE:	return "NOTYPE";
10357     case STT_OBJECT:	return "OBJECT";
10358     case STT_FUNC:	return "FUNC";
10359     case STT_SECTION:	return "SECTION";
10360     case STT_FILE:	return "FILE";
10361     case STT_COMMON:	return "COMMON";
10362     case STT_TLS:	return "TLS";
10363     case STT_RELC:      return "RELC";
10364     case STT_SRELC:     return "SRELC";
10365     default:
10366       if (type >= STT_LOPROC && type <= STT_HIPROC)
10367 	{
10368 	  if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
10369 	    return "THUMB_FUNC";
10370 
10371 	  if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
10372 	    return "REGISTER";
10373 
10374 	  if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
10375 	    return "PARISC_MILLI";
10376 
10377 	  snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
10378 	}
10379       else if (type >= STT_LOOS && type <= STT_HIOS)
10380 	{
10381 	  if (elf_header.e_machine == EM_PARISC)
10382 	    {
10383 	      if (type == STT_HP_OPAQUE)
10384 		return "HP_OPAQUE";
10385 	      if (type == STT_HP_STUB)
10386 		return "HP_STUB";
10387 	    }
10388 
10389 	  if (type == STT_GNU_IFUNC
10390 	      && (elf_header.e_ident[EI_OSABI] == ELFOSABI_GNU
10391 		  || elf_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD
10392 		  /* GNU is still using the default value 0.  */
10393 		  || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
10394 	    return "IFUNC";
10395 
10396 	  snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
10397 	}
10398       else
10399 	snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
10400       return buff;
10401     }
10402 }
10403 
10404 static const char *
get_symbol_visibility(unsigned int visibility)10405 get_symbol_visibility (unsigned int visibility)
10406 {
10407   switch (visibility)
10408     {
10409     case STV_DEFAULT:	return "DEFAULT";
10410     case STV_INTERNAL:	return "INTERNAL";
10411     case STV_HIDDEN:	return "HIDDEN";
10412     case STV_PROTECTED: return "PROTECTED";
10413     default:
10414       error (_("Unrecognized visibility value: %u"), visibility);
10415       return _("<unknown>");
10416     }
10417 }
10418 
10419 static const char *
get_solaris_symbol_visibility(unsigned int visibility)10420 get_solaris_symbol_visibility (unsigned int visibility)
10421 {
10422   switch (visibility)
10423     {
10424     case 4: return "EXPORTED";
10425     case 5: return "SINGLETON";
10426     case 6: return "ELIMINATE";
10427     default: return get_symbol_visibility (visibility);
10428     }
10429 }
10430 
10431 static const char *
get_mips_symbol_other(unsigned int other)10432 get_mips_symbol_other (unsigned int other)
10433 {
10434   switch (other)
10435     {
10436     case STO_OPTIONAL:
10437       return "OPTIONAL";
10438     case STO_MIPS_PLT:
10439       return "MIPS PLT";
10440     case STO_MIPS_PIC:
10441       return "MIPS PIC";
10442     case STO_MICROMIPS:
10443       return "MICROMIPS";
10444     case STO_MICROMIPS | STO_MIPS_PIC:
10445       return "MICROMIPS, MIPS PIC";
10446     case STO_MIPS16:
10447       return "MIPS16";
10448     default:
10449       return NULL;
10450     }
10451 }
10452 
10453 static const char *
get_ia64_symbol_other(unsigned int other)10454 get_ia64_symbol_other (unsigned int other)
10455 {
10456   if (is_ia64_vms ())
10457     {
10458       static char res[32];
10459 
10460       res[0] = 0;
10461 
10462       /* Function types is for images and .STB files only.  */
10463       switch (elf_header.e_type)
10464         {
10465         case ET_DYN:
10466         case ET_EXEC:
10467           switch (VMS_ST_FUNC_TYPE (other))
10468             {
10469             case VMS_SFT_CODE_ADDR:
10470               strcat (res, " CA");
10471               break;
10472             case VMS_SFT_SYMV_IDX:
10473               strcat (res, " VEC");
10474               break;
10475             case VMS_SFT_FD:
10476               strcat (res, " FD");
10477               break;
10478             case VMS_SFT_RESERVE:
10479               strcat (res, " RSV");
10480               break;
10481             default:
10482 	      warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
10483 		    VMS_ST_FUNC_TYPE (other));
10484 	      strcat (res, " <unknown>");
10485 	      break;
10486             }
10487           break;
10488         default:
10489           break;
10490         }
10491       switch (VMS_ST_LINKAGE (other))
10492         {
10493         case VMS_STL_IGNORE:
10494           strcat (res, " IGN");
10495           break;
10496         case VMS_STL_RESERVE:
10497           strcat (res, " RSV");
10498           break;
10499         case VMS_STL_STD:
10500           strcat (res, " STD");
10501           break;
10502         case VMS_STL_LNK:
10503           strcat (res, " LNK");
10504           break;
10505         default:
10506 	  warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
10507 		VMS_ST_LINKAGE (other));
10508 	  strcat (res, " <unknown>");
10509 	  break;
10510         }
10511 
10512       if (res[0] != 0)
10513         return res + 1;
10514       else
10515         return res;
10516     }
10517   return NULL;
10518 }
10519 
10520 static const char *
get_ppc64_symbol_other(unsigned int other)10521 get_ppc64_symbol_other (unsigned int other)
10522 {
10523   if (PPC64_LOCAL_ENTRY_OFFSET (other) != 0)
10524     {
10525       static char buf[32];
10526       snprintf (buf, sizeof buf, _("<localentry>: %d"),
10527 		PPC64_LOCAL_ENTRY_OFFSET (other));
10528       return buf;
10529     }
10530   return NULL;
10531 }
10532 
10533 static const char *
get_symbol_other(unsigned int other)10534 get_symbol_other (unsigned int other)
10535 {
10536   const char * result = NULL;
10537   static char buff [32];
10538 
10539   if (other == 0)
10540     return "";
10541 
10542   switch (elf_header.e_machine)
10543     {
10544     case EM_MIPS:
10545       result = get_mips_symbol_other (other);
10546       break;
10547     case EM_IA_64:
10548       result = get_ia64_symbol_other (other);
10549       break;
10550     case EM_PPC64:
10551       result = get_ppc64_symbol_other (other);
10552       break;
10553     default:
10554       result = NULL;
10555       break;
10556     }
10557 
10558   if (result)
10559     return result;
10560 
10561   snprintf (buff, sizeof buff, _("<other>: %x"), other);
10562   return buff;
10563 }
10564 
10565 static const char *
get_symbol_index_type(unsigned int type)10566 get_symbol_index_type (unsigned int type)
10567 {
10568   static char buff[32];
10569 
10570   switch (type)
10571     {
10572     case SHN_UNDEF:	return "UND";
10573     case SHN_ABS:	return "ABS";
10574     case SHN_COMMON:	return "COM";
10575     default:
10576       if (type == SHN_IA_64_ANSI_COMMON
10577 	  && elf_header.e_machine == EM_IA_64
10578 	  && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
10579 	return "ANSI_COM";
10580       else if ((elf_header.e_machine == EM_X86_64
10581 		|| elf_header.e_machine == EM_L1OM
10582 		|| elf_header.e_machine == EM_K1OM)
10583 	       && type == SHN_X86_64_LCOMMON)
10584 	return "LARGE_COM";
10585       else if ((type == SHN_MIPS_SCOMMON
10586 		&& elf_header.e_machine == EM_MIPS)
10587 	       || (type == SHN_TIC6X_SCOMMON
10588 		   && elf_header.e_machine == EM_TI_C6000))
10589 	return "SCOM";
10590       else if (type == SHN_MIPS_SUNDEFINED
10591 	       && elf_header.e_machine == EM_MIPS)
10592 	return "SUND";
10593       else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
10594 	sprintf (buff, "PRC[0x%04x]", type & 0xffff);
10595       else if (type >= SHN_LOOS && type <= SHN_HIOS)
10596 	sprintf (buff, "OS [0x%04x]", type & 0xffff);
10597       else if (type >= SHN_LORESERVE)
10598 	sprintf (buff, "RSV[0x%04x]", type & 0xffff);
10599       else if (type >= elf_header.e_shnum)
10600 	sprintf (buff, _("bad section index[%3d]"), type);
10601       else
10602 	sprintf (buff, "%3d", type);
10603       break;
10604     }
10605 
10606   return buff;
10607 }
10608 
10609 static bfd_vma *
get_dynamic_data(FILE * file,bfd_size_type number,unsigned int ent_size)10610 get_dynamic_data (FILE * file, bfd_size_type number, unsigned int ent_size)
10611 {
10612   unsigned char * e_data;
10613   bfd_vma * i_data;
10614 
10615   /* If the size_t type is smaller than the bfd_size_type, eg because
10616      you are building a 32-bit tool on a 64-bit host, then make sure
10617      that when (number) is cast to (size_t) no information is lost.  */
10618   if (sizeof (size_t) < sizeof (bfd_size_type)
10619       && (bfd_size_type) ((size_t) number) != number)
10620     {
10621       error (_("Size truncation prevents reading %" BFD_VMA_FMT "u"
10622 	       " elements of size %u\n"),
10623 	     number, ent_size);
10624       return NULL;
10625     }
10626 
10627   /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
10628      attempting to allocate memory when the read is bound to fail.  */
10629   if (ent_size * number > current_file_size)
10630     {
10631       error (_("Invalid number of dynamic entries: %" BFD_VMA_FMT "u\n"),
10632 	     number);
10633       return NULL;
10634     }
10635 
10636   e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10637   if (e_data == NULL)
10638     {
10639       error (_("Out of memory reading %" BFD_VMA_FMT "u dynamic entries\n"),
10640 	     number);
10641       return NULL;
10642     }
10643 
10644   if (fread (e_data, ent_size, (size_t) number, file) != number)
10645     {
10646       error (_("Unable to read in %" BFD_VMA_FMT "u bytes of dynamic data\n"),
10647 	     number * ent_size);
10648       free (e_data);
10649       return NULL;
10650     }
10651 
10652   i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10653   if (i_data == NULL)
10654     {
10655       error (_("Out of memory allocating space for %" BFD_VMA_FMT "u"
10656 	       " dynamic entries\n"),
10657 	     number);
10658       free (e_data);
10659       return NULL;
10660     }
10661 
10662   while (number--)
10663     i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10664 
10665   free (e_data);
10666 
10667   return i_data;
10668 }
10669 
10670 static void
print_dynamic_symbol(bfd_vma si,unsigned long hn)10671 print_dynamic_symbol (bfd_vma si, unsigned long hn)
10672 {
10673   Elf_Internal_Sym * psym;
10674   int n;
10675 
10676   n = print_vma (si, DEC_5);
10677   if (n < 5)
10678     fputs (&"     "[n], stdout);
10679   printf (" %3lu: ", hn);
10680 
10681   if (dynamic_symbols == NULL || si >= num_dynamic_syms)
10682     {
10683       printf (_("<No info available for dynamic symbol number %lu>\n"),
10684 	      (unsigned long) si);
10685       return;
10686     }
10687 
10688   psym = dynamic_symbols + si;
10689   print_vma (psym->st_value, LONG_HEX);
10690   putchar (' ');
10691   print_vma (psym->st_size, DEC_5);
10692 
10693   printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
10694   printf (" %-6s",  get_symbol_binding (ELF_ST_BIND (psym->st_info)));
10695 
10696   if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
10697     printf (" %-7s",  get_solaris_symbol_visibility (psym->st_other));
10698   else
10699     {
10700       unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
10701 
10702       printf (" %-7s",  get_symbol_visibility (vis));
10703       /* Check to see if any other bits in the st_other field are set.
10704 	 Note - displaying this information disrupts the layout of the
10705 	 table being generated, but for the moment this case is very
10706 	 rare.  */
10707       if (psym->st_other ^ vis)
10708 	printf (" [%s] ", get_symbol_other (psym->st_other ^ vis));
10709     }
10710 
10711   printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
10712   if (VALID_DYNAMIC_NAME (psym->st_name))
10713     print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
10714   else
10715     printf (_(" <corrupt: %14ld>"), psym->st_name);
10716   putchar ('\n');
10717 }
10718 
10719 static const char *
get_symbol_version_string(FILE * file,int is_dynsym,const char * strtab,unsigned long int strtab_size,unsigned int si,Elf_Internal_Sym * psym,enum versioned_symbol_info * sym_info,unsigned short * vna_other)10720 get_symbol_version_string (FILE *file, int is_dynsym,
10721 			   const char *strtab,
10722 			   unsigned long int strtab_size,
10723 			   unsigned int si, Elf_Internal_Sym *psym,
10724 			   enum versioned_symbol_info *sym_info,
10725 			   unsigned short *vna_other)
10726 {
10727   unsigned char data[2];
10728   unsigned short vers_data;
10729   unsigned long offset;
10730 
10731   if (!is_dynsym
10732       || version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
10733     return NULL;
10734 
10735   offset = offset_from_vma (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
10736 			    sizeof data + si * sizeof (vers_data));
10737 
10738   if (get_data (&data, file, offset + si * sizeof (vers_data),
10739 		sizeof (data), 1, _("version data")) == NULL)
10740     return NULL;
10741 
10742   vers_data = byte_get (data, 2);
10743 
10744   if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data <= 1)
10745     return NULL;
10746 
10747   /* Usually we'd only see verdef for defined symbols, and verneed for
10748      undefined symbols.  However, symbols defined by the linker in
10749      .dynbss for variables copied from a shared library in order to
10750      avoid text relocations are defined yet have verneed.  We could
10751      use a heuristic to detect the special case, for example, check
10752      for verneed first on symbols defined in SHT_NOBITS sections, but
10753      it is simpler and more reliable to just look for both verdef and
10754      verneed.  .dynbss might not be mapped to a SHT_NOBITS section.  */
10755 
10756   if (psym->st_shndx != SHN_UNDEF
10757       && vers_data != 0x8001
10758       && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
10759     {
10760       Elf_Internal_Verdef ivd;
10761       Elf_Internal_Verdaux ivda;
10762       Elf_External_Verdaux evda;
10763       unsigned long off;
10764 
10765       off = offset_from_vma (file,
10766 			     version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
10767 			     sizeof (Elf_External_Verdef));
10768 
10769       do
10770 	{
10771 	  Elf_External_Verdef evd;
10772 
10773 	  if (get_data (&evd, file, off, sizeof (evd), 1,
10774 			_("version def")) == NULL)
10775 	    {
10776 	      ivd.vd_ndx = 0;
10777 	      ivd.vd_aux = 0;
10778 	      ivd.vd_next = 0;
10779 	    }
10780 	  else
10781 	    {
10782 	      ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
10783 	      ivd.vd_aux = BYTE_GET (evd.vd_aux);
10784 	      ivd.vd_next = BYTE_GET (evd.vd_next);
10785 	    }
10786 
10787 	  off += ivd.vd_next;
10788 	}
10789       while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
10790 
10791       if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
10792 	{
10793 	  off -= ivd.vd_next;
10794 	  off += ivd.vd_aux;
10795 
10796 	  if (get_data (&evda, file, off, sizeof (evda), 1,
10797 			_("version def aux")) != NULL)
10798 	    {
10799 	      ivda.vda_name = BYTE_GET (evda.vda_name);
10800 
10801 	      if (psym->st_name != ivda.vda_name)
10802 		{
10803 		  *sym_info = ((vers_data & VERSYM_HIDDEN) != 0
10804 			       ? symbol_hidden : symbol_public);
10805 		  return (ivda.vda_name < strtab_size
10806 			  ? strtab + ivda.vda_name : _("<corrupt>"));
10807 		}
10808 	    }
10809 	}
10810     }
10811 
10812   if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
10813     {
10814       Elf_External_Verneed evn;
10815       Elf_Internal_Verneed ivn;
10816       Elf_Internal_Vernaux ivna;
10817 
10818       offset = offset_from_vma (file,
10819 				version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
10820 				sizeof evn);
10821       do
10822 	{
10823 	  unsigned long vna_off;
10824 
10825 	  if (get_data (&evn, file, offset, sizeof (evn), 1,
10826 			_("version need")) == NULL)
10827 	    {
10828 	      ivna.vna_next = 0;
10829 	      ivna.vna_other = 0;
10830 	      ivna.vna_name = 0;
10831 	      break;
10832 	    }
10833 
10834 	  ivn.vn_aux  = BYTE_GET (evn.vn_aux);
10835 	  ivn.vn_next = BYTE_GET (evn.vn_next);
10836 
10837 	  vna_off = offset + ivn.vn_aux;
10838 
10839 	  do
10840 	    {
10841 	      Elf_External_Vernaux evna;
10842 
10843 	      if (get_data (&evna, file, vna_off, sizeof (evna), 1,
10844 			    _("version need aux (3)")) == NULL)
10845 		{
10846 		  ivna.vna_next = 0;
10847 		  ivna.vna_other = 0;
10848 		  ivna.vna_name = 0;
10849 		}
10850 	      else
10851 		{
10852 		  ivna.vna_other = BYTE_GET (evna.vna_other);
10853 		  ivna.vna_next  = BYTE_GET (evna.vna_next);
10854 		  ivna.vna_name  = BYTE_GET (evna.vna_name);
10855 		}
10856 
10857 	      vna_off += ivna.vna_next;
10858 	    }
10859 	  while (ivna.vna_other != vers_data && ivna.vna_next != 0);
10860 
10861 	  if (ivna.vna_other == vers_data)
10862 	    break;
10863 
10864 	  offset += ivn.vn_next;
10865 	}
10866       while (ivn.vn_next != 0);
10867 
10868       if (ivna.vna_other == vers_data)
10869 	{
10870 	  *sym_info = symbol_undefined;
10871 	  *vna_other = ivna.vna_other;
10872 	  return (ivna.vna_name < strtab_size
10873 		  ? strtab + ivna.vna_name : _("<corrupt>"));
10874 	}
10875     }
10876   return NULL;
10877 }
10878 
10879 /* Dump the symbol table.  */
10880 static int
process_symbol_table(FILE * file)10881 process_symbol_table (FILE * file)
10882 {
10883   Elf_Internal_Shdr * section;
10884   bfd_size_type nbuckets = 0;
10885   bfd_size_type nchains = 0;
10886   bfd_vma * buckets = NULL;
10887   bfd_vma * chains = NULL;
10888   bfd_vma ngnubuckets = 0;
10889   bfd_vma * gnubuckets = NULL;
10890   bfd_vma * gnuchains = NULL;
10891   bfd_vma gnusymidx = 0;
10892   bfd_size_type ngnuchains = 0;
10893 
10894   if (!do_syms && !do_dyn_syms && !do_histogram)
10895     return 1;
10896 
10897   if (dynamic_info[DT_HASH]
10898       && (do_histogram
10899 	  || (do_using_dynamic
10900 	      && !do_dyn_syms
10901 	      && dynamic_strings != NULL)))
10902     {
10903       unsigned char nb[8];
10904       unsigned char nc[8];
10905       unsigned int hash_ent_size = 4;
10906 
10907       if ((elf_header.e_machine == EM_ALPHA
10908 	   || elf_header.e_machine == EM_S390
10909 	   || elf_header.e_machine == EM_S390_OLD)
10910 	  && elf_header.e_ident[EI_CLASS] == ELFCLASS64)
10911 	hash_ent_size = 8;
10912 
10913       if (fseek (file,
10914 		 (archive_file_offset
10915 		  + offset_from_vma (file, dynamic_info[DT_HASH],
10916 				     sizeof nb + sizeof nc)),
10917 		 SEEK_SET))
10918 	{
10919 	  error (_("Unable to seek to start of dynamic information\n"));
10920 	  goto no_hash;
10921 	}
10922 
10923       if (fread (nb, hash_ent_size, 1, file) != 1)
10924 	{
10925 	  error (_("Failed to read in number of buckets\n"));
10926 	  goto no_hash;
10927 	}
10928 
10929       if (fread (nc, hash_ent_size, 1, file) != 1)
10930 	{
10931 	  error (_("Failed to read in number of chains\n"));
10932 	  goto no_hash;
10933 	}
10934 
10935       nbuckets = byte_get (nb, hash_ent_size);
10936       nchains  = byte_get (nc, hash_ent_size);
10937 
10938       buckets = get_dynamic_data (file, nbuckets, hash_ent_size);
10939       chains  = get_dynamic_data (file, nchains, hash_ent_size);
10940 
10941     no_hash:
10942       if (buckets == NULL || chains == NULL)
10943 	{
10944 	  if (do_using_dynamic)
10945 	    return 0;
10946 	  free (buckets);
10947 	  free (chains);
10948 	  buckets = NULL;
10949 	  chains = NULL;
10950 	  nbuckets = 0;
10951 	  nchains = 0;
10952 	}
10953     }
10954 
10955   if (dynamic_info_DT_GNU_HASH
10956       && (do_histogram
10957 	  || (do_using_dynamic
10958 	      && !do_dyn_syms
10959 	      && dynamic_strings != NULL)))
10960     {
10961       unsigned char nb[16];
10962       bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10963       bfd_vma buckets_vma;
10964 
10965       if (fseek (file,
10966 		 (archive_file_offset
10967 		  + offset_from_vma (file, dynamic_info_DT_GNU_HASH,
10968 				     sizeof nb)),
10969 		 SEEK_SET))
10970 	{
10971 	  error (_("Unable to seek to start of dynamic information\n"));
10972 	  goto no_gnu_hash;
10973 	}
10974 
10975       if (fread (nb, 16, 1, file) != 1)
10976 	{
10977 	  error (_("Failed to read in number of buckets\n"));
10978 	  goto no_gnu_hash;
10979 	}
10980 
10981       ngnubuckets = byte_get (nb, 4);
10982       gnusymidx = byte_get (nb + 4, 4);
10983       bitmaskwords = byte_get (nb + 8, 4);
10984       buckets_vma = dynamic_info_DT_GNU_HASH + 16;
10985       if (is_32bit_elf)
10986 	buckets_vma += bitmaskwords * 4;
10987       else
10988 	buckets_vma += bitmaskwords * 8;
10989 
10990       if (fseek (file,
10991 		 (archive_file_offset
10992 		  + offset_from_vma (file, buckets_vma, 4)),
10993 		 SEEK_SET))
10994 	{
10995 	  error (_("Unable to seek to start of dynamic information\n"));
10996 	  goto no_gnu_hash;
10997 	}
10998 
10999       gnubuckets = get_dynamic_data (file, ngnubuckets, 4);
11000 
11001       if (gnubuckets == NULL)
11002 	goto no_gnu_hash;
11003 
11004       for (i = 0; i < ngnubuckets; i++)
11005 	if (gnubuckets[i] != 0)
11006 	  {
11007 	    if (gnubuckets[i] < gnusymidx)
11008 	      return 0;
11009 
11010 	    if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
11011 	      maxchain = gnubuckets[i];
11012 	  }
11013 
11014       if (maxchain == 0xffffffff)
11015 	goto no_gnu_hash;
11016 
11017       maxchain -= gnusymidx;
11018 
11019       if (fseek (file,
11020 		 (archive_file_offset
11021 		  + offset_from_vma (file, buckets_vma
11022 					   + 4 * (ngnubuckets + maxchain), 4)),
11023 		 SEEK_SET))
11024 	{
11025 	  error (_("Unable to seek to start of dynamic information\n"));
11026 	  goto no_gnu_hash;
11027 	}
11028 
11029       do
11030 	{
11031 	  if (fread (nb, 4, 1, file) != 1)
11032 	    {
11033 	      error (_("Failed to determine last chain length\n"));
11034 	      goto no_gnu_hash;
11035 	    }
11036 
11037 	  if (maxchain + 1 == 0)
11038 	    goto no_gnu_hash;
11039 
11040 	  ++maxchain;
11041 	}
11042       while ((byte_get (nb, 4) & 1) == 0);
11043 
11044       if (fseek (file,
11045 		 (archive_file_offset
11046 		  + offset_from_vma (file, buckets_vma + 4 * ngnubuckets, 4)),
11047 		 SEEK_SET))
11048 	{
11049 	  error (_("Unable to seek to start of dynamic information\n"));
11050 	  goto no_gnu_hash;
11051 	}
11052 
11053       gnuchains = get_dynamic_data (file, maxchain, 4);
11054       ngnuchains = maxchain;
11055 
11056     no_gnu_hash:
11057       if (gnuchains == NULL)
11058 	{
11059 	  free (gnubuckets);
11060 	  gnubuckets = NULL;
11061 	  ngnubuckets = 0;
11062 	  if (do_using_dynamic)
11063 	    return 0;
11064 	}
11065     }
11066 
11067   if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
11068       && do_syms
11069       && do_using_dynamic
11070       && dynamic_strings != NULL
11071       && dynamic_symbols != NULL)
11072     {
11073       unsigned long hn;
11074 
11075       if (dynamic_info[DT_HASH])
11076 	{
11077 	  bfd_vma si;
11078 
11079 	  printf (_("\nSymbol table for image:\n"));
11080 	  if (is_32bit_elf)
11081 	    printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
11082 	  else
11083 	    printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
11084 
11085 	  for (hn = 0; hn < nbuckets; hn++)
11086 	    {
11087 	      if (! buckets[hn])
11088 		continue;
11089 
11090 	      for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
11091 		print_dynamic_symbol (si, hn);
11092 	    }
11093 	}
11094 
11095       if (dynamic_info_DT_GNU_HASH)
11096 	{
11097 	  printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
11098 	  if (is_32bit_elf)
11099 	    printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
11100 	  else
11101 	    printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
11102 
11103 	  for (hn = 0; hn < ngnubuckets; ++hn)
11104 	    if (gnubuckets[hn] != 0)
11105 	      {
11106 		bfd_vma si = gnubuckets[hn];
11107 		bfd_vma off = si - gnusymidx;
11108 
11109 		do
11110 		  {
11111 		    print_dynamic_symbol (si, hn);
11112 		    si++;
11113 		  }
11114 		while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
11115 	      }
11116 	}
11117     }
11118   else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
11119 	   && section_headers != NULL)
11120     {
11121       unsigned int i;
11122 
11123       for (i = 0, section = section_headers;
11124 	   i < elf_header.e_shnum;
11125 	   i++, section++)
11126 	{
11127 	  unsigned int si;
11128 	  char * strtab = NULL;
11129 	  unsigned long int strtab_size = 0;
11130 	  Elf_Internal_Sym * symtab;
11131 	  Elf_Internal_Sym * psym;
11132 	  unsigned long num_syms;
11133 
11134 	  if ((section->sh_type != SHT_SYMTAB
11135 	       && section->sh_type != SHT_DYNSYM)
11136 	      || (!do_syms
11137 		  && section->sh_type == SHT_SYMTAB))
11138 	    continue;
11139 
11140 	  if (section->sh_entsize == 0)
11141 	    {
11142 	      printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
11143 		      printable_section_name (section));
11144 	      continue;
11145 	    }
11146 
11147 	  printf (_("\nSymbol table '%s' contains %lu entries:\n"),
11148 		  printable_section_name (section),
11149 		  (unsigned long) (section->sh_size / section->sh_entsize));
11150 
11151 	  if (is_32bit_elf)
11152 	    printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
11153 	  else
11154 	    printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
11155 
11156 	  symtab = GET_ELF_SYMBOLS (file, section, & num_syms);
11157 	  if (symtab == NULL)
11158 	    continue;
11159 
11160 	  if (section->sh_link == elf_header.e_shstrndx)
11161 	    {
11162 	      strtab = string_table;
11163 	      strtab_size = string_table_length;
11164 	    }
11165 	  else if (section->sh_link < elf_header.e_shnum)
11166 	    {
11167 	      Elf_Internal_Shdr * string_sec;
11168 
11169 	      string_sec = section_headers + section->sh_link;
11170 
11171 	      strtab = (char *) get_data (NULL, file, string_sec->sh_offset,
11172                                           1, string_sec->sh_size,
11173                                           _("string table"));
11174 	      strtab_size = strtab != NULL ? string_sec->sh_size : 0;
11175 	    }
11176 
11177 	  for (si = 0, psym = symtab; si < num_syms; si++, psym++)
11178 	    {
11179 	      const char *version_string;
11180 	      enum versioned_symbol_info sym_info;
11181 	      unsigned short vna_other;
11182 
11183 	      printf ("%6d: ", si);
11184 	      print_vma (psym->st_value, LONG_HEX);
11185 	      putchar (' ');
11186 	      print_vma (psym->st_size, DEC_5);
11187 	      printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
11188 	      printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
11189 	      if (elf_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
11190 		printf (" %-7s",  get_solaris_symbol_visibility (psym->st_other));
11191 	      else
11192 		{
11193 		  unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
11194 
11195 		  printf (" %-7s", get_symbol_visibility (vis));
11196 		  /* Check to see if any other bits in the st_other field are set.
11197 		     Note - displaying this information disrupts the layout of the
11198 		     table being generated, but for the moment this case is very rare.  */
11199 		  if (psym->st_other ^ vis)
11200 		    printf (" [%s] ", get_symbol_other (psym->st_other ^ vis));
11201 		}
11202 	      printf (" %4s ", get_symbol_index_type (psym->st_shndx));
11203 	      print_symbol (25, psym->st_name < strtab_size
11204 			    ? strtab + psym->st_name : _("<corrupt>"));
11205 
11206 	      version_string
11207 		= get_symbol_version_string (file,
11208 					     section->sh_type == SHT_DYNSYM,
11209 					     strtab, strtab_size, si,
11210 					     psym, &sym_info, &vna_other);
11211 	      if (version_string)
11212 		{
11213 		  if (sym_info == symbol_undefined)
11214 		    printf ("@%s (%d)", version_string, vna_other);
11215 		  else
11216 		    printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
11217 			    version_string);
11218 		}
11219 
11220 	      putchar ('\n');
11221 	    }
11222 
11223 	  free (symtab);
11224 	  if (strtab != string_table)
11225 	    free (strtab);
11226 	}
11227     }
11228   else if (do_syms)
11229     printf
11230       (_("\nDynamic symbol information is not available for displaying symbols.\n"));
11231 
11232   if (do_histogram && buckets != NULL)
11233     {
11234       unsigned long * lengths;
11235       unsigned long * counts;
11236       unsigned long hn;
11237       bfd_vma si;
11238       unsigned long maxlength = 0;
11239       unsigned long nzero_counts = 0;
11240       unsigned long nsyms = 0;
11241       unsigned long chained;
11242 
11243       printf (_("\nHistogram for bucket list length (total of %lu buckets):\n"),
11244 	      (unsigned long) nbuckets);
11245 
11246       lengths = (unsigned long *) calloc (nbuckets, sizeof (*lengths));
11247       if (lengths == NULL)
11248 	{
11249 	  error (_("Out of memory allocating space for histogram buckets\n"));
11250 	  return 0;
11251 	}
11252 
11253       printf (_(" Length  Number     %% of total  Coverage\n"));
11254       for (hn = 0; hn < nbuckets; ++hn)
11255 	{
11256 	  for (si = buckets[hn], chained = 0;
11257 	       si > 0 && si < nchains && si < nbuckets && chained <= nchains;
11258 	       si = chains[si], ++chained)
11259 	    {
11260 	      ++nsyms;
11261 	      if (maxlength < ++lengths[hn])
11262 		++maxlength;
11263 	    }
11264 
11265 	    /* PR binutils/17531: A corrupt binary could contain broken
11266 	       histogram data.  Do not go into an infinite loop trying
11267 	       to process it.  */
11268 	    if (chained > nchains)
11269 	      {
11270 		error (_("histogram chain is corrupt\n"));
11271 		break;
11272 	      }
11273 	}
11274 
11275       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
11276       if (counts == NULL)
11277 	{
11278 	  free (lengths);
11279 	  error (_("Out of memory allocating space for histogram counts\n"));
11280 	  return 0;
11281 	}
11282 
11283       for (hn = 0; hn < nbuckets; ++hn)
11284 	++counts[lengths[hn]];
11285 
11286       if (nbuckets > 0)
11287 	{
11288 	  unsigned long i;
11289 	  printf ("      0  %-10lu (%5.1f%%)\n",
11290 		  counts[0], (counts[0] * 100.0) / nbuckets);
11291 	  for (i = 1; i <= maxlength; ++i)
11292 	    {
11293 	      nzero_counts += counts[i] * i;
11294 	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
11295 		      i, counts[i], (counts[i] * 100.0) / nbuckets,
11296 		      (nzero_counts * 100.0) / nsyms);
11297 	    }
11298 	}
11299 
11300       free (counts);
11301       free (lengths);
11302     }
11303 
11304   if (buckets != NULL)
11305     {
11306       free (buckets);
11307       free (chains);
11308     }
11309 
11310   if (do_histogram && gnubuckets != NULL)
11311     {
11312       unsigned long * lengths;
11313       unsigned long * counts;
11314       unsigned long hn;
11315       unsigned long maxlength = 0;
11316       unsigned long nzero_counts = 0;
11317       unsigned long nsyms = 0;
11318 
11319       printf (_("\nHistogram for `.gnu.hash' bucket list length (total of %lu buckets):\n"),
11320 	      (unsigned long) ngnubuckets);
11321 
11322       lengths = (unsigned long *) calloc (ngnubuckets, sizeof (*lengths));
11323       if (lengths == NULL)
11324 	{
11325 	  error (_("Out of memory allocating space for gnu histogram buckets\n"));
11326 	  return 0;
11327 	}
11328 
11329       printf (_(" Length  Number     %% of total  Coverage\n"));
11330 
11331       for (hn = 0; hn < ngnubuckets; ++hn)
11332 	if (gnubuckets[hn] != 0)
11333 	  {
11334 	    bfd_vma off, length = 1;
11335 
11336 	    for (off = gnubuckets[hn] - gnusymidx;
11337 		 /* PR 17531 file: 010-77222-0.004.  */
11338 		 off < ngnuchains && (gnuchains[off] & 1) == 0;
11339 		 ++off)
11340 	      ++length;
11341 	    lengths[hn] = length;
11342 	    if (length > maxlength)
11343 	      maxlength = length;
11344 	    nsyms += length;
11345 	  }
11346 
11347       counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
11348       if (counts == NULL)
11349 	{
11350 	  free (lengths);
11351 	  error (_("Out of memory allocating space for gnu histogram counts\n"));
11352 	  return 0;
11353 	}
11354 
11355       for (hn = 0; hn < ngnubuckets; ++hn)
11356 	++counts[lengths[hn]];
11357 
11358       if (ngnubuckets > 0)
11359 	{
11360 	  unsigned long j;
11361 	  printf ("      0  %-10lu (%5.1f%%)\n",
11362 		  counts[0], (counts[0] * 100.0) / ngnubuckets);
11363 	  for (j = 1; j <= maxlength; ++j)
11364 	    {
11365 	      nzero_counts += counts[j] * j;
11366 	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
11367 		      j, counts[j], (counts[j] * 100.0) / ngnubuckets,
11368 		      (nzero_counts * 100.0) / nsyms);
11369 	    }
11370 	}
11371 
11372       free (counts);
11373       free (lengths);
11374       free (gnubuckets);
11375       free (gnuchains);
11376     }
11377 
11378   return 1;
11379 }
11380 
11381 static int
process_syminfo(FILE * file ATTRIBUTE_UNUSED)11382 process_syminfo (FILE * file ATTRIBUTE_UNUSED)
11383 {
11384   unsigned int i;
11385 
11386   if (dynamic_syminfo == NULL
11387       || !do_dynamic)
11388     /* No syminfo, this is ok.  */
11389     return 1;
11390 
11391   /* There better should be a dynamic symbol section.  */
11392   if (dynamic_symbols == NULL || dynamic_strings == NULL)
11393     return 0;
11394 
11395   if (dynamic_addr)
11396     printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
11397 	    dynamic_syminfo_offset, dynamic_syminfo_nent);
11398 
11399   printf (_(" Num: Name                           BoundTo     Flags\n"));
11400   for (i = 0; i < dynamic_syminfo_nent; ++i)
11401     {
11402       unsigned short int flags = dynamic_syminfo[i].si_flags;
11403 
11404       printf ("%4d: ", i);
11405       if (i >= num_dynamic_syms)
11406 	printf (_("<corrupt index>"));
11407       else if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
11408 	print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
11409       else
11410 	printf (_("<corrupt: %19ld>"), dynamic_symbols[i].st_name);
11411       putchar (' ');
11412 
11413       switch (dynamic_syminfo[i].si_boundto)
11414 	{
11415 	case SYMINFO_BT_SELF:
11416 	  fputs ("SELF       ", stdout);
11417 	  break;
11418 	case SYMINFO_BT_PARENT:
11419 	  fputs ("PARENT     ", stdout);
11420 	  break;
11421 	default:
11422 	  if (dynamic_syminfo[i].si_boundto > 0
11423 	      && dynamic_syminfo[i].si_boundto < dynamic_nent
11424 	      && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
11425 	    {
11426 	      print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
11427 	      putchar (' ' );
11428 	    }
11429 	  else
11430 	    printf ("%-10d ", dynamic_syminfo[i].si_boundto);
11431 	  break;
11432 	}
11433 
11434       if (flags & SYMINFO_FLG_DIRECT)
11435 	printf (" DIRECT");
11436       if (flags & SYMINFO_FLG_PASSTHRU)
11437 	printf (" PASSTHRU");
11438       if (flags & SYMINFO_FLG_COPY)
11439 	printf (" COPY");
11440       if (flags & SYMINFO_FLG_LAZYLOAD)
11441 	printf (" LAZYLOAD");
11442 
11443       puts ("");
11444     }
11445 
11446   return 1;
11447 }
11448 
11449 /* Check to see if the given reloc needs to be handled in a target specific
11450    manner.  If so then process the reloc and return TRUE otherwise return
11451    FALSE.  */
11452 
11453 static bfd_boolean
target_specific_reloc_handling(Elf_Internal_Rela * reloc,unsigned char * start,Elf_Internal_Sym * symtab)11454 target_specific_reloc_handling (Elf_Internal_Rela * reloc,
11455 				unsigned char *     start,
11456 				Elf_Internal_Sym *  symtab)
11457 {
11458   unsigned int reloc_type = get_reloc_type (reloc->r_info);
11459 
11460   switch (elf_header.e_machine)
11461     {
11462     case EM_MSP430:
11463     case EM_MSP430_OLD:
11464       {
11465 	static Elf_Internal_Sym * saved_sym = NULL;
11466 
11467 	switch (reloc_type)
11468 	  {
11469 	  case 10: /* R_MSP430_SYM_DIFF */
11470 	    if (uses_msp430x_relocs ())
11471 	      break;
11472 	  case 21: /* R_MSP430X_SYM_DIFF */
11473 	    saved_sym = symtab + get_reloc_symindex (reloc->r_info);
11474 	    return TRUE;
11475 
11476 	  case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
11477 	  case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
11478 	    goto handle_sym_diff;
11479 
11480 	  case 5: /* R_MSP430_16_BYTE */
11481 	  case 9: /* R_MSP430_8 */
11482 	    if (uses_msp430x_relocs ())
11483 	      break;
11484 	    goto handle_sym_diff;
11485 
11486 	  case 2: /* R_MSP430_ABS16 */
11487 	  case 15: /* R_MSP430X_ABS16 */
11488 	    if (! uses_msp430x_relocs ())
11489 	      break;
11490 	    goto handle_sym_diff;
11491 
11492 	  handle_sym_diff:
11493 	    if (saved_sym != NULL)
11494 	      {
11495 		bfd_vma value;
11496 
11497 		value = reloc->r_addend
11498 		  + (symtab[get_reloc_symindex (reloc->r_info)].st_value
11499 		     - saved_sym->st_value);
11500 
11501 		byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
11502 
11503 		saved_sym = NULL;
11504 		return TRUE;
11505 	      }
11506 	    break;
11507 
11508 	  default:
11509 	    if (saved_sym != NULL)
11510 	      error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
11511 	    break;
11512 	  }
11513 	break;
11514       }
11515 
11516     case EM_MN10300:
11517     case EM_CYGNUS_MN10300:
11518       {
11519 	static Elf_Internal_Sym * saved_sym = NULL;
11520 
11521 	switch (reloc_type)
11522 	  {
11523 	  case 34: /* R_MN10300_ALIGN */
11524 	    return TRUE;
11525 	  case 33: /* R_MN10300_SYM_DIFF */
11526 	    saved_sym = symtab + get_reloc_symindex (reloc->r_info);
11527 	    return TRUE;
11528 	  case 1: /* R_MN10300_32 */
11529 	  case 2: /* R_MN10300_16 */
11530 	    if (saved_sym != NULL)
11531 	      {
11532 		bfd_vma value;
11533 
11534 		value = reloc->r_addend
11535 		  + (symtab[get_reloc_symindex (reloc->r_info)].st_value
11536 		     - saved_sym->st_value);
11537 
11538 		byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
11539 
11540 		saved_sym = NULL;
11541 		return TRUE;
11542 	      }
11543 	    break;
11544 	  default:
11545 	    if (saved_sym != NULL)
11546 	      error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
11547 	    break;
11548 	  }
11549 	break;
11550       }
11551 
11552     case EM_RL78:
11553       {
11554 	static bfd_vma saved_sym1 = 0;
11555 	static bfd_vma saved_sym2 = 0;
11556 	static bfd_vma value;
11557 
11558 	switch (reloc_type)
11559 	  {
11560 	  case 0x80: /* R_RL78_SYM.  */
11561 	    saved_sym1 = saved_sym2;
11562 	    saved_sym2 = symtab[get_reloc_symindex (reloc->r_info)].st_value;
11563 	    saved_sym2 += reloc->r_addend;
11564 	    return TRUE;
11565 
11566 	  case 0x83: /* R_RL78_OPsub.  */
11567 	    value = saved_sym1 - saved_sym2;
11568 	    saved_sym2 = saved_sym1 = 0;
11569 	    return TRUE;
11570 	    break;
11571 
11572 	  case 0x41: /* R_RL78_ABS32.  */
11573 	    byte_put (start + reloc->r_offset, value, 4);
11574 	    value = 0;
11575 	    return TRUE;
11576 
11577 	  case 0x43: /* R_RL78_ABS16.  */
11578 	    byte_put (start + reloc->r_offset, value, 2);
11579 	    value = 0;
11580 	    return TRUE;
11581 
11582 	  default:
11583 	    break;
11584 	  }
11585 	break;
11586       }
11587     }
11588 
11589   return FALSE;
11590 }
11591 
11592 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
11593    DWARF debug sections.  This is a target specific test.  Note - we do not
11594    go through the whole including-target-headers-multiple-times route, (as
11595    we have already done with <elf/h8.h>) because this would become very
11596    messy and even then this function would have to contain target specific
11597    information (the names of the relocs instead of their numeric values).
11598    FIXME: This is not the correct way to solve this problem.  The proper way
11599    is to have target specific reloc sizing and typing functions created by
11600    the reloc-macros.h header, in the same way that it already creates the
11601    reloc naming functions.  */
11602 
11603 static bfd_boolean
is_32bit_abs_reloc(unsigned int reloc_type)11604 is_32bit_abs_reloc (unsigned int reloc_type)
11605 {
11606   /* Please keep this table alpha-sorted for ease of visual lookup.  */
11607   switch (elf_header.e_machine)
11608     {
11609     case EM_386:
11610     case EM_IAMCU:
11611       return reloc_type == 1; /* R_386_32.  */
11612     case EM_68K:
11613       return reloc_type == 1; /* R_68K_32.  */
11614     case EM_860:
11615       return reloc_type == 1; /* R_860_32.  */
11616     case EM_960:
11617       return reloc_type == 2; /* R_960_32.  */
11618     case EM_AARCH64:
11619       return reloc_type == 258; /* R_AARCH64_ABS32 */
11620     case EM_ADAPTEVA_EPIPHANY:
11621       return reloc_type == 3;
11622     case EM_ALPHA:
11623       return reloc_type == 1; /* R_ALPHA_REFLONG.  */
11624     case EM_ARC:
11625       return reloc_type == 1; /* R_ARC_32.  */
11626     case EM_ARC_COMPACT:
11627     case EM_ARC_COMPACT2:
11628       return reloc_type == 4; /* R_ARC_32.  */
11629     case EM_ARM:
11630       return reloc_type == 2; /* R_ARM_ABS32 */
11631     case EM_AVR_OLD:
11632     case EM_AVR:
11633       return reloc_type == 1;
11634     case EM_BLACKFIN:
11635       return reloc_type == 0x12; /* R_byte4_data.  */
11636     case EM_CRIS:
11637       return reloc_type == 3; /* R_CRIS_32.  */
11638     case EM_CR16:
11639       return reloc_type == 3; /* R_CR16_NUM32.  */
11640     case EM_CRX:
11641       return reloc_type == 15; /* R_CRX_NUM32.  */
11642     case EM_CYGNUS_FRV:
11643       return reloc_type == 1;
11644     case EM_CYGNUS_D10V:
11645     case EM_D10V:
11646       return reloc_type == 6; /* R_D10V_32.  */
11647     case EM_CYGNUS_D30V:
11648     case EM_D30V:
11649       return reloc_type == 12; /* R_D30V_32_NORMAL.  */
11650     case EM_DLX:
11651       return reloc_type == 3; /* R_DLX_RELOC_32.  */
11652     case EM_CYGNUS_FR30:
11653     case EM_FR30:
11654       return reloc_type == 3; /* R_FR30_32.  */
11655     case EM_FT32:
11656       return reloc_type == 1; /* R_FT32_32.  */
11657     case EM_H8S:
11658     case EM_H8_300:
11659     case EM_H8_300H:
11660       return reloc_type == 1; /* R_H8_DIR32.  */
11661     case EM_IA_64:
11662       return reloc_type == 0x65 /* R_IA64_SECREL32LSB.  */
11663 	|| reloc_type == 0x25;  /* R_IA64_DIR32LSB.  */
11664     case EM_IP2K_OLD:
11665     case EM_IP2K:
11666       return reloc_type == 2; /* R_IP2K_32.  */
11667     case EM_IQ2000:
11668       return reloc_type == 2; /* R_IQ2000_32.  */
11669     case EM_LATTICEMICO32:
11670       return reloc_type == 3; /* R_LM32_32.  */
11671     case EM_M32C_OLD:
11672     case EM_M32C:
11673       return reloc_type == 3; /* R_M32C_32.  */
11674     case EM_M32R:
11675       return reloc_type == 34; /* R_M32R_32_RELA.  */
11676     case EM_68HC11:
11677     case EM_68HC12:
11678       return reloc_type == 6; /* R_M68HC11_32.  */
11679     case EM_MCORE:
11680       return reloc_type == 1; /* R_MCORE_ADDR32.  */
11681     case EM_CYGNUS_MEP:
11682       return reloc_type == 4; /* R_MEP_32.  */
11683     case EM_METAG:
11684       return reloc_type == 2; /* R_METAG_ADDR32.  */
11685     case EM_MICROBLAZE:
11686       return reloc_type == 1; /* R_MICROBLAZE_32.  */
11687     case EM_MIPS:
11688       return reloc_type == 2; /* R_MIPS_32.  */
11689     case EM_MMIX:
11690       return reloc_type == 4; /* R_MMIX_32.  */
11691     case EM_CYGNUS_MN10200:
11692     case EM_MN10200:
11693       return reloc_type == 1; /* R_MN10200_32.  */
11694     case EM_CYGNUS_MN10300:
11695     case EM_MN10300:
11696       return reloc_type == 1; /* R_MN10300_32.  */
11697     case EM_MOXIE:
11698       return reloc_type == 1; /* R_MOXIE_32.  */
11699     case EM_MSP430_OLD:
11700     case EM_MSP430:
11701       return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32.  */
11702     case EM_MT:
11703       return reloc_type == 2; /* R_MT_32.  */
11704     case EM_NDS32:
11705       return reloc_type == 20; /* R_NDS32_RELA.  */
11706     case EM_ALTERA_NIOS2:
11707       return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32.  */
11708     case EM_NIOS32:
11709       return reloc_type == 1; /* R_NIOS_32.  */
11710     case EM_OR1K:
11711       return reloc_type == 1; /* R_OR1K_32.  */
11712     case EM_PARISC:
11713       return (reloc_type == 1 /* R_PARISC_DIR32.  */
11714 	      || reloc_type == 41); /* R_PARISC_SECREL32.  */
11715     case EM_PJ:
11716     case EM_PJ_OLD:
11717       return reloc_type == 1; /* R_PJ_DATA_DIR32.  */
11718     case EM_PPC64:
11719       return reloc_type == 1; /* R_PPC64_ADDR32.  */
11720     case EM_PPC:
11721       return reloc_type == 1; /* R_PPC_ADDR32.  */
11722     case EM_RL78:
11723       return reloc_type == 1; /* R_RL78_DIR32.  */
11724     case EM_RX:
11725       return reloc_type == 1; /* R_RX_DIR32.  */
11726     case EM_S370:
11727       return reloc_type == 1; /* R_I370_ADDR31.  */
11728     case EM_S390_OLD:
11729     case EM_S390:
11730       return reloc_type == 4; /* R_S390_32.  */
11731     case EM_SCORE:
11732       return reloc_type == 8; /* R_SCORE_ABS32.  */
11733     case EM_SH:
11734       return reloc_type == 1; /* R_SH_DIR32.  */
11735     case EM_SPARC32PLUS:
11736     case EM_SPARCV9:
11737     case EM_SPARC:
11738       return reloc_type == 3 /* R_SPARC_32.  */
11739 	|| reloc_type == 23; /* R_SPARC_UA32.  */
11740     case EM_SPU:
11741       return reloc_type == 6; /* R_SPU_ADDR32 */
11742     case EM_TI_C6000:
11743       return reloc_type == 1; /* R_C6000_ABS32.  */
11744     case EM_TILEGX:
11745       return reloc_type == 2; /* R_TILEGX_32.  */
11746     case EM_TILEPRO:
11747       return reloc_type == 1; /* R_TILEPRO_32.  */
11748     case EM_CYGNUS_V850:
11749     case EM_V850:
11750       return reloc_type == 6; /* R_V850_ABS32.  */
11751     case EM_V800:
11752       return reloc_type == 0x33; /* R_V810_WORD.  */
11753     case EM_VAX:
11754       return reloc_type == 1; /* R_VAX_32.  */
11755     case EM_VISIUM:
11756       return reloc_type == 3;  /* R_VISIUM_32. */
11757     case EM_X86_64:
11758     case EM_L1OM:
11759     case EM_K1OM:
11760       return reloc_type == 10; /* R_X86_64_32.  */
11761     case EM_XC16X:
11762     case EM_C166:
11763       return reloc_type == 3; /* R_XC16C_ABS_32.  */
11764     case EM_XGATE:
11765       return reloc_type == 4; /* R_XGATE_32.  */
11766     case EM_XSTORMY16:
11767       return reloc_type == 1; /* R_XSTROMY16_32.  */
11768     case EM_XTENSA_OLD:
11769     case EM_XTENSA:
11770       return reloc_type == 1; /* R_XTENSA_32.  */
11771     default:
11772       {
11773 	static unsigned int prev_warn = 0;
11774 
11775 	/* Avoid repeating the same warning multiple times.  */
11776 	if (prev_warn != elf_header.e_machine)
11777 	  error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
11778 		 elf_header.e_machine);
11779 	prev_warn = elf_header.e_machine;
11780 	return FALSE;
11781       }
11782     }
11783 }
11784 
11785 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11786    a 32-bit pc-relative RELA relocation used in DWARF debug sections.  */
11787 
11788 static bfd_boolean
is_32bit_pcrel_reloc(unsigned int reloc_type)11789 is_32bit_pcrel_reloc (unsigned int reloc_type)
11790 {
11791   switch (elf_header.e_machine)
11792   /* Please keep this table alpha-sorted for ease of visual lookup.  */
11793     {
11794     case EM_386:
11795     case EM_IAMCU:
11796       return reloc_type == 2;  /* R_386_PC32.  */
11797     case EM_68K:
11798       return reloc_type == 4;  /* R_68K_PC32.  */
11799     case EM_AARCH64:
11800       return reloc_type == 261; /* R_AARCH64_PREL32 */
11801     case EM_ADAPTEVA_EPIPHANY:
11802       return reloc_type == 6;
11803     case EM_ALPHA:
11804       return reloc_type == 10; /* R_ALPHA_SREL32.  */
11805     case EM_ARC_COMPACT:
11806     case EM_ARC_COMPACT2:
11807       return reloc_type == 49; /* R_ARC_32_PCREL.  */
11808     case EM_ARM:
11809       return reloc_type == 3;  /* R_ARM_REL32 */
11810     case EM_AVR_OLD:
11811     case EM_AVR:
11812       return reloc_type == 36; /* R_AVR_32_PCREL.  */
11813     case EM_MICROBLAZE:
11814       return reloc_type == 2;  /* R_MICROBLAZE_32_PCREL.  */
11815     case EM_OR1K:
11816       return reloc_type == 9; /* R_OR1K_32_PCREL.  */
11817     case EM_PARISC:
11818       return reloc_type == 9;  /* R_PARISC_PCREL32.  */
11819     case EM_PPC:
11820       return reloc_type == 26; /* R_PPC_REL32.  */
11821     case EM_PPC64:
11822       return reloc_type == 26; /* R_PPC64_REL32.  */
11823     case EM_S390_OLD:
11824     case EM_S390:
11825       return reloc_type == 5;  /* R_390_PC32.  */
11826     case EM_SH:
11827       return reloc_type == 2;  /* R_SH_REL32.  */
11828     case EM_SPARC32PLUS:
11829     case EM_SPARCV9:
11830     case EM_SPARC:
11831       return reloc_type == 6;  /* R_SPARC_DISP32.  */
11832     case EM_SPU:
11833       return reloc_type == 13; /* R_SPU_REL32.  */
11834     case EM_TILEGX:
11835       return reloc_type == 6; /* R_TILEGX_32_PCREL.  */
11836     case EM_TILEPRO:
11837       return reloc_type == 4; /* R_TILEPRO_32_PCREL.  */
11838     case EM_VISIUM:
11839       return reloc_type == 6;  /* R_VISIUM_32_PCREL */
11840     case EM_X86_64:
11841     case EM_L1OM:
11842     case EM_K1OM:
11843       return reloc_type == 2;  /* R_X86_64_PC32.  */
11844     case EM_XTENSA_OLD:
11845     case EM_XTENSA:
11846       return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
11847     default:
11848       /* Do not abort or issue an error message here.  Not all targets use
11849 	 pc-relative 32-bit relocs in their DWARF debug information and we
11850 	 have already tested for target coverage in is_32bit_abs_reloc.  A
11851 	 more helpful warning message will be generated by apply_relocations
11852 	 anyway, so just return.  */
11853       return FALSE;
11854     }
11855 }
11856 
11857 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11858    a 64-bit absolute RELA relocation used in DWARF debug sections.  */
11859 
11860 static bfd_boolean
is_64bit_abs_reloc(unsigned int reloc_type)11861 is_64bit_abs_reloc (unsigned int reloc_type)
11862 {
11863   switch (elf_header.e_machine)
11864     {
11865     case EM_AARCH64:
11866       return reloc_type == 257;	/* R_AARCH64_ABS64.  */
11867     case EM_ALPHA:
11868       return reloc_type == 2; /* R_ALPHA_REFQUAD.  */
11869     case EM_IA_64:
11870       return reloc_type == 0x27; /* R_IA64_DIR64LSB.  */
11871     case EM_PARISC:
11872       return reloc_type == 80; /* R_PARISC_DIR64.  */
11873     case EM_PPC64:
11874       return reloc_type == 38; /* R_PPC64_ADDR64.  */
11875     case EM_SPARC32PLUS:
11876     case EM_SPARCV9:
11877     case EM_SPARC:
11878       return reloc_type == 54; /* R_SPARC_UA64.  */
11879     case EM_X86_64:
11880     case EM_L1OM:
11881     case EM_K1OM:
11882       return reloc_type == 1; /* R_X86_64_64.  */
11883     case EM_S390_OLD:
11884     case EM_S390:
11885       return reloc_type == 22;	/* R_S390_64.  */
11886     case EM_TILEGX:
11887       return reloc_type == 1; /* R_TILEGX_64.  */
11888     case EM_MIPS:
11889       return reloc_type == 18;	/* R_MIPS_64.  */
11890     default:
11891       return FALSE;
11892     }
11893 }
11894 
11895 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
11896    a 64-bit pc-relative RELA relocation used in DWARF debug sections.  */
11897 
11898 static bfd_boolean
is_64bit_pcrel_reloc(unsigned int reloc_type)11899 is_64bit_pcrel_reloc (unsigned int reloc_type)
11900 {
11901   switch (elf_header.e_machine)
11902     {
11903     case EM_AARCH64:
11904       return reloc_type == 260;	/* R_AARCH64_PREL64.  */
11905     case EM_ALPHA:
11906       return reloc_type == 11; /* R_ALPHA_SREL64.  */
11907     case EM_IA_64:
11908       return reloc_type == 0x4f; /* R_IA64_PCREL64LSB.  */
11909     case EM_PARISC:
11910       return reloc_type == 72; /* R_PARISC_PCREL64.  */
11911     case EM_PPC64:
11912       return reloc_type == 44; /* R_PPC64_REL64.  */
11913     case EM_SPARC32PLUS:
11914     case EM_SPARCV9:
11915     case EM_SPARC:
11916       return reloc_type == 46; /* R_SPARC_DISP64.  */
11917     case EM_X86_64:
11918     case EM_L1OM:
11919     case EM_K1OM:
11920       return reloc_type == 24; /* R_X86_64_PC64.  */
11921     case EM_S390_OLD:
11922     case EM_S390:
11923       return reloc_type == 23;	/* R_S390_PC64.  */
11924     case EM_TILEGX:
11925       return reloc_type == 5;  /* R_TILEGX_64_PCREL.  */
11926     default:
11927       return FALSE;
11928     }
11929 }
11930 
11931 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11932    a 24-bit absolute RELA relocation used in DWARF debug sections.  */
11933 
11934 static bfd_boolean
is_24bit_abs_reloc(unsigned int reloc_type)11935 is_24bit_abs_reloc (unsigned int reloc_type)
11936 {
11937   switch (elf_header.e_machine)
11938     {
11939     case EM_CYGNUS_MN10200:
11940     case EM_MN10200:
11941       return reloc_type == 4; /* R_MN10200_24.  */
11942     case EM_FT32:
11943       return reloc_type == 5; /* R_FT32_20.  */
11944     default:
11945       return FALSE;
11946     }
11947 }
11948 
11949 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
11950    a 16-bit absolute RELA relocation used in DWARF debug sections.  */
11951 
11952 static bfd_boolean
is_16bit_abs_reloc(unsigned int reloc_type)11953 is_16bit_abs_reloc (unsigned int reloc_type)
11954 {
11955   /* Please keep this table alpha-sorted for ease of visual lookup.  */
11956   switch (elf_header.e_machine)
11957     {
11958     case EM_ARC:
11959     case EM_ARC_COMPACT:
11960     case EM_ARC_COMPACT2:
11961       return reloc_type == 2; /* R_ARC_16.  */
11962     case EM_ADAPTEVA_EPIPHANY:
11963       return reloc_type == 5;
11964     case EM_AVR_OLD:
11965     case EM_AVR:
11966       return reloc_type == 4; /* R_AVR_16.  */
11967     case EM_CYGNUS_D10V:
11968     case EM_D10V:
11969       return reloc_type == 3; /* R_D10V_16.  */
11970     case EM_H8S:
11971     case EM_H8_300:
11972     case EM_H8_300H:
11973       return reloc_type == R_H8_DIR16;
11974     case EM_IP2K_OLD:
11975     case EM_IP2K:
11976       return reloc_type == 1; /* R_IP2K_16.  */
11977     case EM_M32C_OLD:
11978     case EM_M32C:
11979       return reloc_type == 1; /* R_M32C_16 */
11980     case EM_CYGNUS_MN10200:
11981     case EM_MN10200:
11982       return reloc_type == 2; /* R_MN10200_16.  */
11983     case EM_CYGNUS_MN10300:
11984     case EM_MN10300:
11985       return reloc_type == 2; /* R_MN10300_16.  */
11986     case EM_MSP430:
11987       if (uses_msp430x_relocs ())
11988 	return reloc_type == 2; /* R_MSP430_ABS16.  */
11989     case EM_MSP430_OLD:
11990       return reloc_type == 5; /* R_MSP430_16_BYTE.  */
11991     case EM_NDS32:
11992       return reloc_type == 19; /* R_NDS32_RELA.  */
11993     case EM_ALTERA_NIOS2:
11994       return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16.  */
11995     case EM_NIOS32:
11996       return reloc_type == 9; /* R_NIOS_16.  */
11997     case EM_OR1K:
11998       return reloc_type == 2; /* R_OR1K_16.  */
11999     case EM_TI_C6000:
12000       return reloc_type == 2; /* R_C6000_ABS16.  */
12001     case EM_VISIUM:
12002       return reloc_type == 2; /* R_VISIUM_16. */
12003     case EM_XC16X:
12004     case EM_C166:
12005       return reloc_type == 2; /* R_XC16C_ABS_16.  */
12006     case EM_XGATE:
12007       return reloc_type == 3; /* R_XGATE_16.  */
12008     default:
12009       return FALSE;
12010     }
12011 }
12012 
12013 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
12014    relocation entries (possibly formerly used for SHT_GROUP sections).  */
12015 
12016 static bfd_boolean
is_none_reloc(unsigned int reloc_type)12017 is_none_reloc (unsigned int reloc_type)
12018 {
12019   switch (elf_header.e_machine)
12020     {
12021     case EM_386:     /* R_386_NONE.  */
12022     case EM_68K:     /* R_68K_NONE.  */
12023     case EM_ADAPTEVA_EPIPHANY:
12024     case EM_ALPHA:   /* R_ALPHA_NONE.  */
12025     case EM_ALTERA_NIOS2: /* R_NIOS2_NONE.  */
12026     case EM_ARC:     /* R_ARC_NONE.  */
12027     case EM_ARC_COMPACT2: /* R_ARC_NONE.  */
12028     case EM_ARC_COMPACT: /* R_ARC_NONE.  */
12029     case EM_ARM:     /* R_ARM_NONE.  */
12030     case EM_C166:    /* R_XC16X_NONE.  */
12031     case EM_CRIS:    /* R_CRIS_NONE.  */
12032     case EM_FT32:    /* R_FT32_NONE.  */
12033     case EM_IA_64:   /* R_IA64_NONE.  */
12034     case EM_K1OM:    /* R_X86_64_NONE.  */
12035     case EM_L1OM:    /* R_X86_64_NONE.  */
12036     case EM_M32R:    /* R_M32R_NONE.  */
12037     case EM_MIPS:    /* R_MIPS_NONE.  */
12038     case EM_MN10300: /* R_MN10300_NONE.  */
12039     case EM_MOXIE:   /* R_MOXIE_NONE.  */
12040     case EM_NIOS32:  /* R_NIOS_NONE.  */
12041     case EM_OR1K:    /* R_OR1K_NONE. */
12042     case EM_PARISC:  /* R_PARISC_NONE.  */
12043     case EM_PPC64:   /* R_PPC64_NONE.  */
12044     case EM_PPC:     /* R_PPC_NONE.  */
12045     case EM_S390:    /* R_390_NONE.  */
12046     case EM_S390_OLD:
12047     case EM_SH:      /* R_SH_NONE.  */
12048     case EM_SPARC32PLUS:
12049     case EM_SPARC:   /* R_SPARC_NONE.  */
12050     case EM_SPARCV9:
12051     case EM_TILEGX:  /* R_TILEGX_NONE.  */
12052     case EM_TILEPRO: /* R_TILEPRO_NONE.  */
12053     case EM_TI_C6000:/* R_C6000_NONE.  */
12054     case EM_X86_64:  /* R_X86_64_NONE.  */
12055     case EM_XC16X:
12056       return reloc_type == 0;
12057 
12058     case EM_AARCH64:
12059       return reloc_type == 0 || reloc_type == 256;
12060     case EM_AVR_OLD:
12061     case EM_AVR:
12062       return (reloc_type == 0 /* R_AVR_NONE.  */
12063 	      || reloc_type == 30 /* R_AVR_DIFF8.  */
12064 	      || reloc_type == 31 /* R_AVR_DIFF16.  */
12065 	      || reloc_type == 32 /* R_AVR_DIFF32.  */);
12066     case EM_METAG:
12067       return reloc_type == 3; /* R_METAG_NONE.  */
12068     case EM_NDS32:
12069       return (reloc_type == 0       /* R_XTENSA_NONE.  */
12070 	      || reloc_type == 204  /* R_NDS32_DIFF8.  */
12071 	      || reloc_type == 205  /* R_NDS32_DIFF16.  */
12072 	      || reloc_type == 206  /* R_NDS32_DIFF32.  */
12073 	      || reloc_type == 207  /* R_NDS32_ULEB128.  */);
12074     case EM_XTENSA_OLD:
12075     case EM_XTENSA:
12076       return (reloc_type == 0      /* R_XTENSA_NONE.  */
12077 	      || reloc_type == 17  /* R_XTENSA_DIFF8.  */
12078 	      || reloc_type == 18  /* R_XTENSA_DIFF16.  */
12079 	      || reloc_type == 19  /* R_XTENSA_DIFF32.  */);
12080     }
12081   return FALSE;
12082 }
12083 
12084 /* Returns TRUE if there is a relocation against
12085    section NAME at OFFSET bytes.  */
12086 
12087 bfd_boolean
reloc_at(struct dwarf_section * dsec,dwarf_vma offset)12088 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
12089 {
12090   Elf_Internal_Rela * relocs;
12091   Elf_Internal_Rela * rp;
12092 
12093   if (dsec == NULL || dsec->reloc_info == NULL)
12094     return FALSE;
12095 
12096   relocs = (Elf_Internal_Rela *) dsec->reloc_info;
12097 
12098   for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
12099     if (rp->r_offset == offset)
12100       return TRUE;
12101 
12102    return FALSE;
12103 }
12104 
12105 /* Apply relocations to a section.
12106    Note: So far support has been added only for those relocations
12107    which can be found in debug sections.
12108    If RELOCS_RETURN is non-NULL then returns in it a pointer to the
12109    loaded relocs.  It is then the caller's responsibility to free them.
12110    FIXME: Add support for more relocations ?  */
12111 
12112 static void
apply_relocations(void * file,const Elf_Internal_Shdr * section,unsigned char * start,bfd_size_type size,void ** relocs_return,unsigned long * num_relocs_return)12113 apply_relocations (void *                     file,
12114 		   const Elf_Internal_Shdr *  section,
12115 		   unsigned char *            start,
12116 		   bfd_size_type              size,
12117 		   void **                     relocs_return,
12118 		   unsigned long *            num_relocs_return)
12119 {
12120   Elf_Internal_Shdr * relsec;
12121   unsigned char * end = start + size;
12122 
12123   if (relocs_return != NULL)
12124     {
12125       * (Elf_Internal_Rela **) relocs_return = NULL;
12126       * num_relocs_return = 0;
12127     }
12128 
12129   if (elf_header.e_type != ET_REL)
12130     return;
12131 
12132   /* Find the reloc section associated with the section.  */
12133   for (relsec = section_headers;
12134        relsec < section_headers + elf_header.e_shnum;
12135        ++relsec)
12136     {
12137       bfd_boolean is_rela;
12138       unsigned long num_relocs;
12139       Elf_Internal_Rela * relocs;
12140       Elf_Internal_Rela * rp;
12141       Elf_Internal_Shdr * symsec;
12142       Elf_Internal_Sym * symtab;
12143       unsigned long num_syms;
12144       Elf_Internal_Sym * sym;
12145 
12146       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
12147 	  || relsec->sh_info >= elf_header.e_shnum
12148 	  || section_headers + relsec->sh_info != section
12149 	  || relsec->sh_size == 0
12150 	  || relsec->sh_link >= elf_header.e_shnum)
12151 	continue;
12152 
12153       is_rela = relsec->sh_type == SHT_RELA;
12154 
12155       if (is_rela)
12156 	{
12157 	  if (!slurp_rela_relocs ((FILE *) file, relsec->sh_offset,
12158                                   relsec->sh_size, & relocs, & num_relocs))
12159 	    return;
12160 	}
12161       else
12162 	{
12163 	  if (!slurp_rel_relocs ((FILE *) file, relsec->sh_offset,
12164                                  relsec->sh_size, & relocs, & num_relocs))
12165 	    return;
12166 	}
12167 
12168       /* SH uses RELA but uses in place value instead of the addend field.  */
12169       if (elf_header.e_machine == EM_SH)
12170 	is_rela = FALSE;
12171 
12172       symsec = section_headers + relsec->sh_link;
12173       symtab = GET_ELF_SYMBOLS ((FILE *) file, symsec, & num_syms);
12174 
12175       for (rp = relocs; rp < relocs + num_relocs; ++rp)
12176 	{
12177 	  bfd_vma         addend;
12178 	  unsigned int    reloc_type;
12179 	  unsigned int    reloc_size;
12180 	  unsigned char * rloc;
12181 	  unsigned long   sym_index;
12182 
12183 	  reloc_type = get_reloc_type (rp->r_info);
12184 
12185 	  if (target_specific_reloc_handling (rp, start, symtab))
12186 	    continue;
12187 	  else if (is_none_reloc (reloc_type))
12188 	    continue;
12189 	  else if (is_32bit_abs_reloc (reloc_type)
12190 		   || is_32bit_pcrel_reloc (reloc_type))
12191 	    reloc_size = 4;
12192 	  else if (is_64bit_abs_reloc (reloc_type)
12193 		   || is_64bit_pcrel_reloc (reloc_type))
12194 	    reloc_size = 8;
12195 	  else if (is_24bit_abs_reloc (reloc_type))
12196 	    reloc_size = 3;
12197 	  else if (is_16bit_abs_reloc (reloc_type))
12198 	    reloc_size = 2;
12199 	  else
12200 	    {
12201 	      static unsigned int prev_reloc = 0;
12202 	      if (reloc_type != prev_reloc)
12203 		warn (_("unable to apply unsupported reloc type %d to section %s\n"),
12204 		      reloc_type, printable_section_name (section));
12205 	      prev_reloc = reloc_type;
12206 	      continue;
12207 	    }
12208 
12209 	  rloc = start + rp->r_offset;
12210 	  if ((rloc + reloc_size) > end || (rloc < start))
12211 	    {
12212 	      warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
12213 		    (unsigned long) rp->r_offset,
12214 		    printable_section_name (section));
12215 	      continue;
12216 	    }
12217 
12218 	  sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
12219 	  if (sym_index >= num_syms)
12220 	    {
12221 	      warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
12222 		    sym_index, printable_section_name (section));
12223 	      continue;
12224 	    }
12225 	  sym = symtab + sym_index;
12226 
12227 	  /* If the reloc has a symbol associated with it,
12228 	     make sure that it is of an appropriate type.
12229 
12230 	     Relocations against symbols without type can happen.
12231 	     Gcc -feliminate-dwarf2-dups may generate symbols
12232 	     without type for debug info.
12233 
12234 	     Icc generates relocations against function symbols
12235 	     instead of local labels.
12236 
12237 	     Relocations against object symbols can happen, eg when
12238 	     referencing a global array.  For an example of this see
12239 	     the _clz.o binary in libgcc.a.  */
12240 	  if (sym != symtab
12241 	      && ELF_ST_TYPE (sym->st_info) != STT_COMMON
12242 	      && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
12243 	    {
12244 	      warn (_("skipping unexpected symbol type %s in %ld'th relocation in section %s\n"),
12245 		    get_symbol_type (ELF_ST_TYPE (sym->st_info)),
12246 		    (long int)(rp - relocs),
12247 		    printable_section_name (relsec));
12248 	      continue;
12249 	    }
12250 
12251 	  addend = 0;
12252 	  if (is_rela)
12253 	    addend += rp->r_addend;
12254 	  /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
12255 	     partial_inplace.  */
12256 	  if (!is_rela
12257 	      || (elf_header.e_machine == EM_XTENSA
12258 		  && reloc_type == 1)
12259 	      || ((elf_header.e_machine == EM_PJ
12260 		   || elf_header.e_machine == EM_PJ_OLD)
12261 		  && reloc_type == 1)
12262 	      || ((elf_header.e_machine == EM_D30V
12263 		   || elf_header.e_machine == EM_CYGNUS_D30V)
12264 		  && reloc_type == 12))
12265 	    addend += byte_get (rloc, reloc_size);
12266 
12267 	  if (is_32bit_pcrel_reloc (reloc_type)
12268 	      || is_64bit_pcrel_reloc (reloc_type))
12269 	    {
12270 	      /* On HPPA, all pc-relative relocations are biased by 8.  */
12271 	      if (elf_header.e_machine == EM_PARISC)
12272 		addend -= 8;
12273 	      byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
12274 		        reloc_size);
12275 	    }
12276 	  else
12277 	    byte_put (rloc, addend + sym->st_value, reloc_size);
12278 	}
12279 
12280       free (symtab);
12281 
12282       if (relocs_return)
12283 	{
12284 	  * (Elf_Internal_Rela **) relocs_return = relocs;
12285 	  * num_relocs_return = num_relocs;
12286 	}
12287       else
12288 	free (relocs);
12289 
12290       break;
12291     }
12292 }
12293 
12294 #ifdef SUPPORT_DISASSEMBLY
12295 static int
disassemble_section(Elf_Internal_Shdr * section,FILE * file)12296 disassemble_section (Elf_Internal_Shdr * section, FILE * file)
12297 {
12298   printf (_("\nAssembly dump of section %s\n"), printable_section_name (section));
12299 
12300   /* FIXME: XXX -- to be done --- XXX */
12301 
12302   return 1;
12303 }
12304 #endif
12305 
12306 /* Reads in the contents of SECTION from FILE, returning a pointer
12307    to a malloc'ed buffer or NULL if something went wrong.  */
12308 
12309 static char *
get_section_contents(Elf_Internal_Shdr * section,FILE * file)12310 get_section_contents (Elf_Internal_Shdr * section, FILE * file)
12311 {
12312   bfd_size_type num_bytes;
12313 
12314   num_bytes = section->sh_size;
12315 
12316   if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
12317     {
12318       printf (_("\nSection '%s' has no data to dump.\n"),
12319 	      printable_section_name (section));
12320       return NULL;
12321     }
12322 
12323   return  (char *) get_data (NULL, file, section->sh_offset, 1, num_bytes,
12324                              _("section contents"));
12325 }
12326 
12327 /* Uncompresses a section that was compressed using zlib, in place.  */
12328 
12329 static bfd_boolean
uncompress_section_contents(unsigned char ** buffer,dwarf_size_type uncompressed_size,dwarf_size_type * size)12330 uncompress_section_contents (unsigned char **buffer,
12331 			     dwarf_size_type uncompressed_size,
12332 			     dwarf_size_type *size)
12333 {
12334   dwarf_size_type compressed_size = *size;
12335   unsigned char * compressed_buffer = *buffer;
12336   unsigned char * uncompressed_buffer;
12337   z_stream strm;
12338   int rc;
12339 
12340   /* It is possible the section consists of several compressed
12341      buffers concatenated together, so we uncompress in a loop.  */
12342   /* PR 18313: The state field in the z_stream structure is supposed
12343      to be invisible to the user (ie us), but some compilers will
12344      still complain about it being used without initialisation.  So
12345      we first zero the entire z_stream structure and then set the fields
12346      that we need.  */
12347   memset (& strm, 0, sizeof strm);
12348   strm.avail_in = compressed_size;
12349   strm.next_in = (Bytef *) compressed_buffer;
12350   strm.avail_out = uncompressed_size;
12351   uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
12352 
12353   rc = inflateInit (& strm);
12354   while (strm.avail_in > 0)
12355     {
12356       if (rc != Z_OK)
12357         goto fail;
12358       strm.next_out = ((Bytef *) uncompressed_buffer
12359                        + (uncompressed_size - strm.avail_out));
12360       rc = inflate (&strm, Z_FINISH);
12361       if (rc != Z_STREAM_END)
12362         goto fail;
12363       rc = inflateReset (& strm);
12364     }
12365   rc = inflateEnd (& strm);
12366   if (rc != Z_OK
12367       || strm.avail_out != 0)
12368     goto fail;
12369 
12370   *buffer = uncompressed_buffer;
12371   *size = uncompressed_size;
12372   return TRUE;
12373 
12374  fail:
12375   free (uncompressed_buffer);
12376   /* Indicate decompression failure.  */
12377   *buffer = NULL;
12378   return FALSE;
12379 }
12380 
12381 static void
dump_section_as_strings(Elf_Internal_Shdr * section,FILE * file)12382 dump_section_as_strings (Elf_Internal_Shdr * section, FILE * file)
12383 {
12384   Elf_Internal_Shdr *  relsec;
12385   bfd_size_type        num_bytes;
12386   unsigned char *      data;
12387   unsigned char *      end;
12388   unsigned char *      real_start;
12389   unsigned char *      start;
12390   bfd_boolean          some_strings_shown;
12391 
12392   real_start = start = (unsigned char *) get_section_contents (section,
12393 							       file);
12394   if (start == NULL)
12395     return;
12396   num_bytes = section->sh_size;
12397 
12398   printf (_("\nString dump of section '%s':\n"), printable_section_name (section));
12399 
12400   if (decompress_dumps)
12401     {
12402       dwarf_size_type new_size = num_bytes;
12403       dwarf_size_type uncompressed_size = 0;
12404 
12405       if ((section->sh_flags & SHF_COMPRESSED) != 0)
12406 	{
12407 	  Elf_Internal_Chdr chdr;
12408 	  unsigned int compression_header_size
12409 	    = get_compression_header (& chdr, (unsigned char *) start);
12410 
12411 	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
12412 	    {
12413 	      warn (_("section '%s' has unsupported compress type: %d\n"),
12414 		    printable_section_name (section), chdr.ch_type);
12415 	      return;
12416 	    }
12417 	  else if (chdr.ch_addralign != section->sh_addralign)
12418 	    {
12419 	      warn (_("compressed section '%s' is corrupted\n"),
12420 		    printable_section_name (section));
12421 	      return;
12422 	    }
12423 	  uncompressed_size = chdr.ch_size;
12424 	  start += compression_header_size;
12425 	  new_size -= compression_header_size;
12426 	}
12427       else if (new_size > 12 && streq ((char *) start, "ZLIB"))
12428 	{
12429 	  /* Read the zlib header.  In this case, it should be "ZLIB"
12430 	     followed by the uncompressed section size, 8 bytes in
12431 	     big-endian order.  */
12432 	  uncompressed_size = start[4]; uncompressed_size <<= 8;
12433 	  uncompressed_size += start[5]; uncompressed_size <<= 8;
12434 	  uncompressed_size += start[6]; uncompressed_size <<= 8;
12435 	  uncompressed_size += start[7]; uncompressed_size <<= 8;
12436 	  uncompressed_size += start[8]; uncompressed_size <<= 8;
12437 	  uncompressed_size += start[9]; uncompressed_size <<= 8;
12438 	  uncompressed_size += start[10]; uncompressed_size <<= 8;
12439 	  uncompressed_size += start[11];
12440 	  start += 12;
12441 	  new_size -= 12;
12442 	}
12443 
12444       if (uncompressed_size
12445 	  && uncompress_section_contents (& start,
12446 					  uncompressed_size, & new_size))
12447 	num_bytes = new_size;
12448     }
12449 
12450   /* If the section being dumped has relocations against it the user might
12451      be expecting these relocations to have been applied.  Check for this
12452      case and issue a warning message in order to avoid confusion.
12453      FIXME: Maybe we ought to have an option that dumps a section with
12454      relocs applied ?  */
12455   for (relsec = section_headers;
12456        relsec < section_headers + elf_header.e_shnum;
12457        ++relsec)
12458     {
12459       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
12460 	  || relsec->sh_info >= elf_header.e_shnum
12461 	  || section_headers + relsec->sh_info != section
12462 	  || relsec->sh_size == 0
12463 	  || relsec->sh_link >= elf_header.e_shnum)
12464 	continue;
12465 
12466       printf (_("  Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
12467       break;
12468     }
12469 
12470   data = start;
12471   end  = start + num_bytes;
12472   some_strings_shown = FALSE;
12473 
12474   while (data < end)
12475     {
12476       while (!ISPRINT (* data))
12477 	if (++ data >= end)
12478 	  break;
12479 
12480       if (data < end)
12481 	{
12482 	  size_t maxlen = end - data;
12483 
12484 #ifndef __MSVCRT__
12485 	  /* PR 11128: Use two separate invocations in order to work
12486              around bugs in the Solaris 8 implementation of printf.  */
12487 	  printf ("  [%6tx]  ", data - start);
12488 #else
12489 	  printf ("  [%6Ix]  ", (size_t) (data - start));
12490 #endif
12491 	  if (maxlen > 0)
12492 	    {
12493 	      print_symbol ((int) maxlen, (const char *) data);
12494 	      putchar ('\n');
12495 	      data += strnlen ((const char *) data, maxlen);
12496 	    }
12497 	  else
12498 	    {
12499 	      printf (_("<corrupt>\n"));
12500 	      data = end;
12501 	    }
12502 	  some_strings_shown = TRUE;
12503 	}
12504     }
12505 
12506   if (! some_strings_shown)
12507     printf (_("  No strings found in this section."));
12508 
12509   free (real_start);
12510 
12511   putchar ('\n');
12512 }
12513 
12514 static void
dump_section_as_bytes(Elf_Internal_Shdr * section,FILE * file,bfd_boolean relocate)12515 dump_section_as_bytes (Elf_Internal_Shdr * section,
12516 		       FILE * file,
12517 		       bfd_boolean relocate)
12518 {
12519   Elf_Internal_Shdr * relsec;
12520   bfd_size_type       bytes;
12521   bfd_size_type       section_size;
12522   bfd_vma             addr;
12523   unsigned char *     data;
12524   unsigned char *     real_start;
12525   unsigned char *     start;
12526 
12527   real_start = start = (unsigned char *) get_section_contents (section, file);
12528   if (start == NULL)
12529     return;
12530   section_size = section->sh_size;
12531 
12532   printf (_("\nHex dump of section '%s':\n"), printable_section_name (section));
12533 
12534   if (decompress_dumps)
12535     {
12536       dwarf_size_type new_size = section_size;
12537       dwarf_size_type uncompressed_size = 0;
12538 
12539       if ((section->sh_flags & SHF_COMPRESSED) != 0)
12540 	{
12541 	  Elf_Internal_Chdr chdr;
12542 	  unsigned int compression_header_size
12543 	    = get_compression_header (& chdr, start);
12544 
12545 	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
12546 	    {
12547 	      warn (_("section '%s' has unsupported compress type: %d\n"),
12548 		    printable_section_name (section), chdr.ch_type);
12549 	      return;
12550 	    }
12551 	  else if (chdr.ch_addralign != section->sh_addralign)
12552 	    {
12553 	      warn (_("compressed section '%s' is corrupted\n"),
12554 		    printable_section_name (section));
12555 	      return;
12556 	    }
12557 	  uncompressed_size = chdr.ch_size;
12558 	  start += compression_header_size;
12559 	  new_size -= compression_header_size;
12560 	}
12561       else if (new_size > 12 && streq ((char *) start, "ZLIB"))
12562 	{
12563 	  /* Read the zlib header.  In this case, it should be "ZLIB"
12564 	     followed by the uncompressed section size, 8 bytes in
12565 	     big-endian order.  */
12566 	  uncompressed_size = start[4]; uncompressed_size <<= 8;
12567 	  uncompressed_size += start[5]; uncompressed_size <<= 8;
12568 	  uncompressed_size += start[6]; uncompressed_size <<= 8;
12569 	  uncompressed_size += start[7]; uncompressed_size <<= 8;
12570 	  uncompressed_size += start[8]; uncompressed_size <<= 8;
12571 	  uncompressed_size += start[9]; uncompressed_size <<= 8;
12572 	  uncompressed_size += start[10]; uncompressed_size <<= 8;
12573 	  uncompressed_size += start[11];
12574 	  start += 12;
12575 	  new_size -= 12;
12576 	}
12577 
12578       if (uncompressed_size
12579 	  && uncompress_section_contents (& start, uncompressed_size,
12580 					  & new_size))
12581 	section_size = new_size;
12582     }
12583 
12584   if (relocate)
12585     {
12586       apply_relocations (file, section, start, section_size, NULL, NULL);
12587     }
12588   else
12589     {
12590       /* If the section being dumped has relocations against it the user might
12591 	 be expecting these relocations to have been applied.  Check for this
12592 	 case and issue a warning message in order to avoid confusion.
12593 	 FIXME: Maybe we ought to have an option that dumps a section with
12594 	 relocs applied ?  */
12595       for (relsec = section_headers;
12596 	   relsec < section_headers + elf_header.e_shnum;
12597 	   ++relsec)
12598 	{
12599 	  if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
12600 	      || relsec->sh_info >= elf_header.e_shnum
12601 	      || section_headers + relsec->sh_info != section
12602 	      || relsec->sh_size == 0
12603 	      || relsec->sh_link >= elf_header.e_shnum)
12604 	    continue;
12605 
12606 	  printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
12607 	  break;
12608 	}
12609     }
12610 
12611   addr = section->sh_addr;
12612   bytes = section_size;
12613   data = start;
12614 
12615   while (bytes)
12616     {
12617       int j;
12618       int k;
12619       int lbytes;
12620 
12621       lbytes = (bytes > 16 ? 16 : bytes);
12622 
12623       printf ("  0x%8.8lx ", (unsigned long) addr);
12624 
12625       for (j = 0; j < 16; j++)
12626 	{
12627 	  if (j < lbytes)
12628 	    printf ("%2.2x", data[j]);
12629 	  else
12630 	    printf ("  ");
12631 
12632 	  if ((j & 3) == 3)
12633 	    printf (" ");
12634 	}
12635 
12636       for (j = 0; j < lbytes; j++)
12637 	{
12638 	  k = data[j];
12639 	  if (k >= ' ' && k < 0x7f)
12640 	    printf ("%c", k);
12641 	  else
12642 	    printf (".");
12643 	}
12644 
12645       putchar ('\n');
12646 
12647       data  += lbytes;
12648       addr  += lbytes;
12649       bytes -= lbytes;
12650     }
12651 
12652   free (real_start);
12653 
12654   putchar ('\n');
12655 }
12656 
12657 static int
load_specific_debug_section(enum dwarf_section_display_enum debug,const Elf_Internal_Shdr * sec,void * file)12658 load_specific_debug_section (enum dwarf_section_display_enum debug,
12659 			     const Elf_Internal_Shdr * sec, void * file)
12660 {
12661   struct dwarf_section * section = &debug_displays [debug].section;
12662   char buf [64];
12663 
12664   /* If it is already loaded, do nothing.  */
12665   if (section->start != NULL)
12666     return 1;
12667 
12668   snprintf (buf, sizeof (buf), _("%s section data"), section->name);
12669   section->address = sec->sh_addr;
12670   section->user_data = NULL;
12671   section->start = (unsigned char *) get_data (NULL, (FILE *) file,
12672                                                sec->sh_offset, 1,
12673                                                sec->sh_size, buf);
12674   if (section->start == NULL)
12675     section->size = 0;
12676   else
12677     {
12678       unsigned char *start = section->start;
12679       dwarf_size_type size = sec->sh_size;
12680       dwarf_size_type uncompressed_size = 0;
12681 
12682       if ((sec->sh_flags & SHF_COMPRESSED) != 0)
12683 	{
12684 	  Elf_Internal_Chdr chdr;
12685 	  unsigned int compression_header_size
12686 	    = get_compression_header (&chdr, start);
12687 	  if (chdr.ch_type != ELFCOMPRESS_ZLIB)
12688 	    {
12689 	      warn (_("section '%s' has unsupported compress type: %d\n"),
12690 		    section->name, chdr.ch_type);
12691 	      return 0;
12692 	    }
12693 	  else if (chdr.ch_addralign != sec->sh_addralign)
12694 	    {
12695 	      warn (_("compressed section '%s' is corrupted\n"),
12696 		    section->name);
12697 	      return 0;
12698 	    }
12699 	  uncompressed_size = chdr.ch_size;
12700 	  start += compression_header_size;
12701 	  size -= compression_header_size;
12702 	}
12703       else if (size > 12 && streq ((char *) start, "ZLIB"))
12704 	{
12705 	  /* Read the zlib header.  In this case, it should be "ZLIB"
12706 	     followed by the uncompressed section size, 8 bytes in
12707 	     big-endian order.  */
12708 	  uncompressed_size = start[4]; uncompressed_size <<= 8;
12709 	  uncompressed_size += start[5]; uncompressed_size <<= 8;
12710 	  uncompressed_size += start[6]; uncompressed_size <<= 8;
12711 	  uncompressed_size += start[7]; uncompressed_size <<= 8;
12712 	  uncompressed_size += start[8]; uncompressed_size <<= 8;
12713 	  uncompressed_size += start[9]; uncompressed_size <<= 8;
12714 	  uncompressed_size += start[10]; uncompressed_size <<= 8;
12715 	  uncompressed_size += start[11];
12716 	  start += 12;
12717 	  size -= 12;
12718 	}
12719 
12720       if (uncompressed_size
12721 	  && uncompress_section_contents (&start, uncompressed_size,
12722 					  &size))
12723 	{
12724 	  /* Free the compressed buffer, update the section buffer
12725 	     and the section size if uncompress is successful.  */
12726 	  free (section->start);
12727 	  section->start = start;
12728 	}
12729       section->size = size;
12730     }
12731 
12732   if (section->start == NULL)
12733     return 0;
12734 
12735   if (debug_displays [debug].relocate)
12736     apply_relocations ((FILE *) file, sec, section->start, section->size,
12737 		       & section->reloc_info, & section->num_relocs);
12738   else
12739     {
12740       section->reloc_info = NULL;
12741       section->num_relocs = 0;
12742     }
12743 
12744   return 1;
12745 }
12746 
12747 /* If this is not NULL, load_debug_section will only look for sections
12748    within the list of sections given here.  */
12749 unsigned int *section_subset = NULL;
12750 
12751 int
load_debug_section(enum dwarf_section_display_enum debug,void * file)12752 load_debug_section (enum dwarf_section_display_enum debug, void * file)
12753 {
12754   struct dwarf_section * section = &debug_displays [debug].section;
12755   Elf_Internal_Shdr * sec;
12756 
12757   /* Locate the debug section.  */
12758   sec = find_section_in_set (section->uncompressed_name, section_subset);
12759   if (sec != NULL)
12760     section->name = section->uncompressed_name;
12761   else
12762     {
12763       sec = find_section_in_set (section->compressed_name, section_subset);
12764       if (sec != NULL)
12765 	section->name = section->compressed_name;
12766     }
12767   if (sec == NULL)
12768     return 0;
12769 
12770   /* If we're loading from a subset of sections, and we've loaded
12771      a section matching this name before, it's likely that it's a
12772      different one.  */
12773   if (section_subset != NULL)
12774     free_debug_section (debug);
12775 
12776   return load_specific_debug_section (debug, sec, (FILE *) file);
12777 }
12778 
12779 void
free_debug_section(enum dwarf_section_display_enum debug)12780 free_debug_section (enum dwarf_section_display_enum debug)
12781 {
12782   struct dwarf_section * section = &debug_displays [debug].section;
12783 
12784   if (section->start == NULL)
12785     return;
12786 
12787   free ((char *) section->start);
12788   section->start = NULL;
12789   section->address = 0;
12790   section->size = 0;
12791 }
12792 
12793 static int
display_debug_section(int shndx,Elf_Internal_Shdr * section,FILE * file)12794 display_debug_section (int shndx, Elf_Internal_Shdr * section, FILE * file)
12795 {
12796   char * name = SECTION_NAME (section);
12797   const char * print_name = printable_section_name (section);
12798   bfd_size_type length;
12799   int result = 1;
12800   int i;
12801 
12802   length = section->sh_size;
12803   if (length == 0)
12804     {
12805       printf (_("\nSection '%s' has no debugging data.\n"), print_name);
12806       return 0;
12807     }
12808   if (section->sh_type == SHT_NOBITS)
12809     {
12810       /* There is no point in dumping the contents of a debugging section
12811 	 which has the NOBITS type - the bits in the file will be random.
12812 	 This can happen when a file containing a .eh_frame section is
12813 	 stripped with the --only-keep-debug command line option.  */
12814       printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
12815 	      print_name);
12816       return 0;
12817     }
12818 
12819   if (const_strneq (name, ".gnu.linkonce.wi."))
12820     name = ".debug_info";
12821 
12822   /* See if we know how to display the contents of this section.  */
12823   for (i = 0; i < max; i++)
12824     if (streq (debug_displays[i].section.uncompressed_name, name)
12825 	|| (i == line && const_strneq (name, ".debug_line."))
12826         || streq (debug_displays[i].section.compressed_name, name))
12827       {
12828 	struct dwarf_section * sec = &debug_displays [i].section;
12829 	int secondary = (section != find_section (name));
12830 
12831 	if (secondary)
12832 	  free_debug_section ((enum dwarf_section_display_enum) i);
12833 
12834 	if (i == line && const_strneq (name, ".debug_line."))
12835 	  sec->name = name;
12836 	else if (streq (sec->uncompressed_name, name))
12837 	  sec->name = sec->uncompressed_name;
12838 	else
12839 	  sec->name = sec->compressed_name;
12840 	if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
12841                                          section, file))
12842 	  {
12843 	    /* If this debug section is part of a CU/TU set in a .dwp file,
12844 	       restrict load_debug_section to the sections in that set.  */
12845 	    section_subset = find_cu_tu_set (file, shndx);
12846 
12847 	    result &= debug_displays[i].display (sec, file);
12848 
12849 	    section_subset = NULL;
12850 
12851 	    if (secondary || (i != info && i != abbrev))
12852 	      free_debug_section ((enum dwarf_section_display_enum) i);
12853 	  }
12854 
12855 	break;
12856       }
12857 
12858   if (i == max)
12859     {
12860       printf (_("Unrecognized debug section: %s\n"), print_name);
12861       result = 0;
12862     }
12863 
12864   return result;
12865 }
12866 
12867 /* Set DUMP_SECTS for all sections where dumps were requested
12868    based on section name.  */
12869 
12870 static void
initialise_dumps_byname(void)12871 initialise_dumps_byname (void)
12872 {
12873   struct dump_list_entry * cur;
12874 
12875   for (cur = dump_sects_byname; cur; cur = cur->next)
12876     {
12877       unsigned int i;
12878       int any;
12879 
12880       for (i = 0, any = 0; i < elf_header.e_shnum; i++)
12881 	if (streq (SECTION_NAME (section_headers + i), cur->name))
12882 	  {
12883 	    request_dump_bynumber (i, cur->type);
12884 	    any = 1;
12885 	  }
12886 
12887       if (!any)
12888 	warn (_("Section '%s' was not dumped because it does not exist!\n"),
12889 	      cur->name);
12890     }
12891 }
12892 
12893 static void
process_section_contents(FILE * file)12894 process_section_contents (FILE * file)
12895 {
12896   Elf_Internal_Shdr * section;
12897   unsigned int i;
12898 
12899   if (! do_dump)
12900     return;
12901 
12902   initialise_dumps_byname ();
12903 
12904   for (i = 0, section = section_headers;
12905        i < elf_header.e_shnum && i < num_dump_sects;
12906        i++, section++)
12907     {
12908 #ifdef SUPPORT_DISASSEMBLY
12909       if (dump_sects[i] & DISASS_DUMP)
12910 	disassemble_section (section, file);
12911 #endif
12912       if (dump_sects[i] & HEX_DUMP)
12913 	dump_section_as_bytes (section, file, FALSE);
12914 
12915       if (dump_sects[i] & RELOC_DUMP)
12916 	dump_section_as_bytes (section, file, TRUE);
12917 
12918       if (dump_sects[i] & STRING_DUMP)
12919 	dump_section_as_strings (section, file);
12920 
12921       if (dump_sects[i] & DEBUG_DUMP)
12922 	display_debug_section (i, section, file);
12923     }
12924 
12925   /* Check to see if the user requested a
12926      dump of a section that does not exist.  */
12927   while (i++ < num_dump_sects)
12928     if (dump_sects[i])
12929       warn (_("Section %d was not dumped because it does not exist!\n"), i);
12930 }
12931 
12932 static void
process_mips_fpe_exception(int mask)12933 process_mips_fpe_exception (int mask)
12934 {
12935   if (mask)
12936     {
12937       int first = 1;
12938       if (mask & OEX_FPU_INEX)
12939 	fputs ("INEX", stdout), first = 0;
12940       if (mask & OEX_FPU_UFLO)
12941 	printf ("%sUFLO", first ? "" : "|"), first = 0;
12942       if (mask & OEX_FPU_OFLO)
12943 	printf ("%sOFLO", first ? "" : "|"), first = 0;
12944       if (mask & OEX_FPU_DIV0)
12945 	printf ("%sDIV0", first ? "" : "|"), first = 0;
12946       if (mask & OEX_FPU_INVAL)
12947 	printf ("%sINVAL", first ? "" : "|");
12948     }
12949   else
12950     fputs ("0", stdout);
12951 }
12952 
12953 /* Display's the value of TAG at location P.  If TAG is
12954    greater than 0 it is assumed to be an unknown tag, and
12955    a message is printed to this effect.  Otherwise it is
12956    assumed that a message has already been printed.
12957 
12958    If the bottom bit of TAG is set it assumed to have a
12959    string value, otherwise it is assumed to have an integer
12960    value.
12961 
12962    Returns an updated P pointing to the first unread byte
12963    beyond the end of TAG's value.
12964 
12965    Reads at or beyond END will not be made.  */
12966 
12967 static unsigned char *
display_tag_value(int tag,unsigned char * p,const unsigned char * const end)12968 display_tag_value (int tag,
12969 		   unsigned char * p,
12970 		   const unsigned char * const end)
12971 {
12972   unsigned long val;
12973 
12974   if (tag > 0)
12975     printf ("  Tag_unknown_%d: ", tag);
12976 
12977   if (p >= end)
12978     {
12979       warn (_("<corrupt tag>\n"));
12980     }
12981   else if (tag & 1)
12982     {
12983       /* PR 17531 file: 027-19978-0.004.  */
12984       size_t maxlen = (end - p) - 1;
12985 
12986       putchar ('"');
12987       if (maxlen > 0)
12988 	{
12989 	  print_symbol ((int) maxlen, (const char *) p);
12990 	  p += strnlen ((char *) p, maxlen) + 1;
12991 	}
12992       else
12993 	{
12994 	  printf (_("<corrupt string tag>"));
12995 	  p = (unsigned char *) end;
12996 	}
12997       printf ("\"\n");
12998     }
12999   else
13000     {
13001       unsigned int len;
13002 
13003       val = read_uleb128 (p, &len, end);
13004       p += len;
13005       printf ("%ld (0x%lx)\n", val, val);
13006     }
13007 
13008   assert (p <= end);
13009   return p;
13010 }
13011 
13012 /* ARM EABI attributes section.  */
13013 typedef struct
13014 {
13015   unsigned int tag;
13016   const char * name;
13017   /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup.  */
13018   unsigned int type;
13019   const char ** table;
13020 } arm_attr_public_tag;
13021 
13022 static const char * arm_attr_tag_CPU_arch[] =
13023   {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
13024    "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "", "v8-M.baseline",
13025    "v8-M.mainline"};
13026 static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
13027 static const char * arm_attr_tag_THUMB_ISA_use[] =
13028   {"No", "Thumb-1", "Thumb-2", "Yes"};
13029 static const char * arm_attr_tag_FP_arch[] =
13030   {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
13031    "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
13032 static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
13033 static const char * arm_attr_tag_Advanced_SIMD_arch[] =
13034   {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
13035    "NEON for ARMv8.1"};
13036 static const char * arm_attr_tag_PCS_config[] =
13037   {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
13038    "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
13039 static const char * arm_attr_tag_ABI_PCS_R9_use[] =
13040   {"V6", "SB", "TLS", "Unused"};
13041 static const char * arm_attr_tag_ABI_PCS_RW_data[] =
13042   {"Absolute", "PC-relative", "SB-relative", "None"};
13043 static const char * arm_attr_tag_ABI_PCS_RO_data[] =
13044   {"Absolute", "PC-relative", "None"};
13045 static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
13046   {"None", "direct", "GOT-indirect"};
13047 static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
13048   {"None", "??? 1", "2", "??? 3", "4"};
13049 static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
13050 static const char * arm_attr_tag_ABI_FP_denormal[] =
13051   {"Unused", "Needed", "Sign only"};
13052 static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
13053 static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
13054 static const char * arm_attr_tag_ABI_FP_number_model[] =
13055   {"Unused", "Finite", "RTABI", "IEEE 754"};
13056 static const char * arm_attr_tag_ABI_enum_size[] =
13057   {"Unused", "small", "int", "forced to int"};
13058 static const char * arm_attr_tag_ABI_HardFP_use[] =
13059   {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
13060 static const char * arm_attr_tag_ABI_VFP_args[] =
13061   {"AAPCS", "VFP registers", "custom", "compatible"};
13062 static const char * arm_attr_tag_ABI_WMMX_args[] =
13063   {"AAPCS", "WMMX registers", "custom"};
13064 static const char * arm_attr_tag_ABI_optimization_goals[] =
13065   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
13066     "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
13067 static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
13068   {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
13069     "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
13070 static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
13071 static const char * arm_attr_tag_FP_HP_extension[] =
13072   {"Not Allowed", "Allowed"};
13073 static const char * arm_attr_tag_ABI_FP_16bit_format[] =
13074   {"None", "IEEE 754", "Alternative Format"};
13075 static const char * arm_attr_tag_DSP_extension[] =
13076   {"Follow architecture", "Allowed"};
13077 static const char * arm_attr_tag_MPextension_use[] =
13078   {"Not Allowed", "Allowed"};
13079 static const char * arm_attr_tag_DIV_use[] =
13080   {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
13081     "Allowed in v7-A with integer division extension"};
13082 static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
13083 static const char * arm_attr_tag_Virtualization_use[] =
13084   {"Not Allowed", "TrustZone", "Virtualization Extensions",
13085     "TrustZone and Virtualization Extensions"};
13086 static const char * arm_attr_tag_MPextension_use_legacy[] =
13087   {"Not Allowed", "Allowed"};
13088 
13089 #define LOOKUP(id, name) \
13090   {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
13091 static arm_attr_public_tag arm_attr_public_tags[] =
13092 {
13093   {4, "CPU_raw_name", 1, NULL},
13094   {5, "CPU_name", 1, NULL},
13095   LOOKUP(6, CPU_arch),
13096   {7, "CPU_arch_profile", 0, NULL},
13097   LOOKUP(8, ARM_ISA_use),
13098   LOOKUP(9, THUMB_ISA_use),
13099   LOOKUP(10, FP_arch),
13100   LOOKUP(11, WMMX_arch),
13101   LOOKUP(12, Advanced_SIMD_arch),
13102   LOOKUP(13, PCS_config),
13103   LOOKUP(14, ABI_PCS_R9_use),
13104   LOOKUP(15, ABI_PCS_RW_data),
13105   LOOKUP(16, ABI_PCS_RO_data),
13106   LOOKUP(17, ABI_PCS_GOT_use),
13107   LOOKUP(18, ABI_PCS_wchar_t),
13108   LOOKUP(19, ABI_FP_rounding),
13109   LOOKUP(20, ABI_FP_denormal),
13110   LOOKUP(21, ABI_FP_exceptions),
13111   LOOKUP(22, ABI_FP_user_exceptions),
13112   LOOKUP(23, ABI_FP_number_model),
13113   {24, "ABI_align_needed", 0, NULL},
13114   {25, "ABI_align_preserved", 0, NULL},
13115   LOOKUP(26, ABI_enum_size),
13116   LOOKUP(27, ABI_HardFP_use),
13117   LOOKUP(28, ABI_VFP_args),
13118   LOOKUP(29, ABI_WMMX_args),
13119   LOOKUP(30, ABI_optimization_goals),
13120   LOOKUP(31, ABI_FP_optimization_goals),
13121   {32, "compatibility", 0, NULL},
13122   LOOKUP(34, CPU_unaligned_access),
13123   LOOKUP(36, FP_HP_extension),
13124   LOOKUP(38, ABI_FP_16bit_format),
13125   LOOKUP(42, MPextension_use),
13126   LOOKUP(44, DIV_use),
13127   LOOKUP(46, DSP_extension),
13128   {64, "nodefaults", 0, NULL},
13129   {65, "also_compatible_with", 0, NULL},
13130   LOOKUP(66, T2EE_use),
13131   {67, "conformance", 1, NULL},
13132   LOOKUP(68, Virtualization_use),
13133   LOOKUP(70, MPextension_use_legacy)
13134 };
13135 #undef LOOKUP
13136 
13137 static unsigned char *
display_arm_attribute(unsigned char * p,const unsigned char * const end)13138 display_arm_attribute (unsigned char * p,
13139 		       const unsigned char * const end)
13140 {
13141   unsigned int tag;
13142   unsigned int len;
13143   unsigned int val;
13144   arm_attr_public_tag * attr;
13145   unsigned i;
13146   unsigned int type;
13147 
13148   tag = read_uleb128 (p, &len, end);
13149   p += len;
13150   attr = NULL;
13151   for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
13152     {
13153       if (arm_attr_public_tags[i].tag == tag)
13154 	{
13155 	  attr = &arm_attr_public_tags[i];
13156 	  break;
13157 	}
13158     }
13159 
13160   if (attr)
13161     {
13162       printf ("  Tag_%s: ", attr->name);
13163       switch (attr->type)
13164 	{
13165 	case 0:
13166 	  switch (tag)
13167 	    {
13168 	    case 7: /* Tag_CPU_arch_profile.  */
13169 	      val = read_uleb128 (p, &len, end);
13170 	      p += len;
13171 	      switch (val)
13172 		{
13173 		case 0: printf (_("None\n")); break;
13174 		case 'A': printf (_("Application\n")); break;
13175 		case 'R': printf (_("Realtime\n")); break;
13176 		case 'M': printf (_("Microcontroller\n")); break;
13177 		case 'S': printf (_("Application or Realtime\n")); break;
13178 		default: printf ("??? (%d)\n", val); break;
13179 		}
13180 	      break;
13181 
13182 	    case 24: /* Tag_align_needed.  */
13183 	      val = read_uleb128 (p, &len, end);
13184 	      p += len;
13185 	      switch (val)
13186 		{
13187 		case 0: printf (_("None\n")); break;
13188 		case 1: printf (_("8-byte\n")); break;
13189 		case 2: printf (_("4-byte\n")); break;
13190 		case 3: printf ("??? 3\n"); break;
13191 		default:
13192 		  if (val <= 12)
13193 		    printf (_("8-byte and up to %d-byte extended\n"),
13194 			    1 << val);
13195 		  else
13196 		    printf ("??? (%d)\n", val);
13197 		  break;
13198 		}
13199 	      break;
13200 
13201 	    case 25: /* Tag_align_preserved.  */
13202 	      val = read_uleb128 (p, &len, end);
13203 	      p += len;
13204 	      switch (val)
13205 		{
13206 		case 0: printf (_("None\n")); break;
13207 		case 1: printf (_("8-byte, except leaf SP\n")); break;
13208 		case 2: printf (_("8-byte\n")); break;
13209 		case 3: printf ("??? 3\n"); break;
13210 		default:
13211 		  if (val <= 12)
13212 		    printf (_("8-byte and up to %d-byte extended\n"),
13213 			    1 << val);
13214 		  else
13215 		    printf ("??? (%d)\n", val);
13216 		  break;
13217 		}
13218 	      break;
13219 
13220 	    case 32: /* Tag_compatibility.  */
13221 	      {
13222 		val = read_uleb128 (p, &len, end);
13223 		p += len;
13224 		printf (_("flag = %d, vendor = "), val);
13225 		if (p < end - 1)
13226 		  {
13227 		    size_t maxlen = (end - p) - 1;
13228 
13229 		    print_symbol ((int) maxlen, (const char *) p);
13230 		    p += strnlen ((char *) p, maxlen) + 1;
13231 		  }
13232 		else
13233 		  {
13234 		    printf (_("<corrupt>"));
13235 		    p = (unsigned char *) end;
13236 		  }
13237 		putchar ('\n');
13238 	      }
13239 	      break;
13240 
13241 	    case 64: /* Tag_nodefaults.  */
13242 	      /* PR 17531: file: 001-505008-0.01.  */
13243 	      if (p < end)
13244 		p++;
13245 	      printf (_("True\n"));
13246 	      break;
13247 
13248 	    case 65: /* Tag_also_compatible_with.  */
13249 	      val = read_uleb128 (p, &len, end);
13250 	      p += len;
13251 	      if (val == 6 /* Tag_CPU_arch.  */)
13252 		{
13253 		  val = read_uleb128 (p, &len, end);
13254 		  p += len;
13255 		  if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
13256 		    printf ("??? (%d)\n", val);
13257 		  else
13258 		    printf ("%s\n", arm_attr_tag_CPU_arch[val]);
13259 		}
13260 	      else
13261 		printf ("???\n");
13262 	      while (p < end && *(p++) != '\0' /* NUL terminator.  */)
13263 		;
13264 	      break;
13265 
13266 	    default:
13267 	      printf (_("<unknown: %d>\n"), tag);
13268 	      break;
13269 	    }
13270 	  return p;
13271 
13272 	case 1:
13273 	  return display_tag_value (-1, p, end);
13274 	case 2:
13275 	  return display_tag_value (0, p, end);
13276 
13277 	default:
13278 	  assert (attr->type & 0x80);
13279 	  val = read_uleb128 (p, &len, end);
13280 	  p += len;
13281 	  type = attr->type & 0x7f;
13282 	  if (val >= type)
13283 	    printf ("??? (%d)\n", val);
13284 	  else
13285 	    printf ("%s\n", attr->table[val]);
13286 	  return p;
13287 	}
13288     }
13289 
13290   return display_tag_value (tag, p, end);
13291 }
13292 
13293 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)13294 display_gnu_attribute (unsigned char * p,
13295 		       unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int, const unsigned char * const),
13296 		       const unsigned char * const end)
13297 {
13298   int tag;
13299   unsigned int len;
13300   int val;
13301 
13302   tag = read_uleb128 (p, &len, end);
13303   p += len;
13304 
13305   /* Tag_compatibility is the only generic GNU attribute defined at
13306      present.  */
13307   if (tag == 32)
13308     {
13309       val = read_uleb128 (p, &len, end);
13310       p += len;
13311 
13312       printf (_("flag = %d, vendor = "), val);
13313       if (p == end)
13314 	{
13315 	  printf (_("<corrupt>\n"));
13316 	  warn (_("corrupt vendor attribute\n"));
13317 	}
13318       else
13319 	{
13320 	  if (p < end - 1)
13321 	    {
13322 	      size_t maxlen = (end - p) - 1;
13323 
13324 	      print_symbol ((int) maxlen, (const char *) p);
13325 	      p += strnlen ((char *) p, maxlen) + 1;
13326 	    }
13327 	  else
13328 	    {
13329 	      printf (_("<corrupt>"));
13330 	      p = (unsigned char *) end;
13331 	    }
13332 	  putchar ('\n');
13333 	}
13334       return p;
13335     }
13336 
13337   if ((tag & 2) == 0 && display_proc_gnu_attribute)
13338     return display_proc_gnu_attribute (p, tag, end);
13339 
13340   return display_tag_value (tag, p, end);
13341 }
13342 
13343 static unsigned char *
display_power_gnu_attribute(unsigned char * p,int tag,const unsigned char * const end)13344 display_power_gnu_attribute (unsigned char * p,
13345 			     int tag,
13346 			     const unsigned char * const end)
13347 {
13348   unsigned int len;
13349   unsigned int val;
13350 
13351   if (tag == Tag_GNU_Power_ABI_FP)
13352     {
13353       val = read_uleb128 (p, &len, end);
13354       p += len;
13355       printf ("  Tag_GNU_Power_ABI_FP: ");
13356       if (len == 0)
13357 	{
13358 	  printf (_("<corrupt>\n"));
13359 	  return p;
13360 	}
13361 
13362       if (val > 15)
13363 	printf ("(%#x), ", val);
13364 
13365       switch (val & 3)
13366 	{
13367 	case 0:
13368 	  printf (_("unspecified hard/soft float, "));
13369 	  break;
13370 	case 1:
13371 	  printf (_("hard float, "));
13372 	  break;
13373 	case 2:
13374 	  printf (_("soft float, "));
13375 	  break;
13376 	case 3:
13377 	  printf (_("single-precision hard float, "));
13378 	  break;
13379 	}
13380 
13381       switch (val & 0xC)
13382 	{
13383 	case 0:
13384 	  printf (_("unspecified long double\n"));
13385 	  break;
13386 	case 4:
13387 	  printf (_("128-bit IBM long double\n"));
13388 	  break;
13389 	case 8:
13390 	  printf (_("64-bit long double\n"));
13391 	  break;
13392 	case 12:
13393 	  printf (_("128-bit IEEE long double\n"));
13394 	  break;
13395 	}
13396       return p;
13397     }
13398 
13399   if (tag == Tag_GNU_Power_ABI_Vector)
13400     {
13401       val = read_uleb128 (p, &len, end);
13402       p += len;
13403       printf ("  Tag_GNU_Power_ABI_Vector: ");
13404       if (len == 0)
13405 	{
13406 	  printf (_("<corrupt>\n"));
13407 	  return p;
13408 	}
13409 
13410       if (val > 3)
13411 	printf ("(%#x), ", val);
13412 
13413       switch (val & 3)
13414 	{
13415 	case 0:
13416 	  printf (_("unspecified\n"));
13417 	  break;
13418 	case 1:
13419 	  printf (_("generic\n"));
13420 	  break;
13421 	case 2:
13422 	  printf ("AltiVec\n");
13423 	  break;
13424 	case 3:
13425 	  printf ("SPE\n");
13426 	  break;
13427 	}
13428       return p;
13429     }
13430 
13431   if (tag == Tag_GNU_Power_ABI_Struct_Return)
13432     {
13433       val = read_uleb128 (p, &len, end);
13434       p += len;
13435       printf ("  Tag_GNU_Power_ABI_Struct_Return: ");
13436       if (len == 0)
13437 	{
13438 	  printf (_("<corrupt>\n"));
13439 	  return p;
13440 	}
13441 
13442       if (val > 2)
13443 	printf ("(%#x), ", val);
13444 
13445       switch (val & 3)
13446 	{
13447 	case 0:
13448 	  printf (_("unspecified\n"));
13449 	  break;
13450 	case 1:
13451 	  printf ("r3/r4\n");
13452 	  break;
13453 	case 2:
13454 	  printf (_("memory\n"));
13455 	  break;
13456 	case 3:
13457 	  printf ("???\n");
13458 	  break;
13459 	}
13460       return p;
13461     }
13462 
13463   return display_tag_value (tag & 1, p, end);
13464 }
13465 
13466 static unsigned char *
display_s390_gnu_attribute(unsigned char * p,int tag,const unsigned char * const end)13467 display_s390_gnu_attribute (unsigned char * p,
13468 			    int tag,
13469 			    const unsigned char * const end)
13470 {
13471   unsigned int len;
13472   int val;
13473 
13474   if (tag == Tag_GNU_S390_ABI_Vector)
13475     {
13476       val = read_uleb128 (p, &len, end);
13477       p += len;
13478       printf ("  Tag_GNU_S390_ABI_Vector: ");
13479 
13480       switch (val)
13481 	{
13482 	case 0:
13483 	  printf (_("any\n"));
13484 	  break;
13485 	case 1:
13486 	  printf (_("software\n"));
13487 	  break;
13488 	case 2:
13489 	  printf (_("hardware\n"));
13490 	  break;
13491 	default:
13492 	  printf ("??? (%d)\n", val);
13493 	  break;
13494 	}
13495       return p;
13496    }
13497 
13498   return display_tag_value (tag & 1, p, end);
13499 }
13500 
13501 static void
display_sparc_hwcaps(int mask)13502 display_sparc_hwcaps (int mask)
13503 {
13504   if (mask)
13505     {
13506       int first = 1;
13507 
13508       if (mask & ELF_SPARC_HWCAP_MUL32)
13509 	fputs ("mul32", stdout), first = 0;
13510       if (mask & ELF_SPARC_HWCAP_DIV32)
13511 	printf ("%sdiv32", first ? "" : "|"), first = 0;
13512       if (mask & ELF_SPARC_HWCAP_FSMULD)
13513 	printf ("%sfsmuld", first ? "" : "|"), first = 0;
13514       if (mask & ELF_SPARC_HWCAP_V8PLUS)
13515 	printf ("%sv8plus", first ? "" : "|"), first = 0;
13516       if (mask & ELF_SPARC_HWCAP_POPC)
13517 	printf ("%spopc", first ? "" : "|"), first = 0;
13518       if (mask & ELF_SPARC_HWCAP_VIS)
13519 	printf ("%svis", first ? "" : "|"), first = 0;
13520       if (mask & ELF_SPARC_HWCAP_VIS2)
13521 	printf ("%svis2", first ? "" : "|"), first = 0;
13522       if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
13523 	printf ("%sASIBlkInit", first ? "" : "|"), first = 0;
13524       if (mask & ELF_SPARC_HWCAP_FMAF)
13525 	printf ("%sfmaf", first ? "" : "|"), first = 0;
13526       if (mask & ELF_SPARC_HWCAP_VIS3)
13527 	printf ("%svis3", first ? "" : "|"), first = 0;
13528       if (mask & ELF_SPARC_HWCAP_HPC)
13529 	printf ("%shpc", first ? "" : "|"), first = 0;
13530       if (mask & ELF_SPARC_HWCAP_RANDOM)
13531 	printf ("%srandom", first ? "" : "|"), first = 0;
13532       if (mask & ELF_SPARC_HWCAP_TRANS)
13533 	printf ("%strans", first ? "" : "|"), first = 0;
13534       if (mask & ELF_SPARC_HWCAP_FJFMAU)
13535 	printf ("%sfjfmau", first ? "" : "|"), first = 0;
13536       if (mask & ELF_SPARC_HWCAP_IMA)
13537 	printf ("%sima", first ? "" : "|"), first = 0;
13538       if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
13539 	printf ("%scspare", first ? "" : "|"), first = 0;
13540     }
13541   else
13542     fputc ('0', stdout);
13543   fputc ('\n', stdout);
13544 }
13545 
13546 static void
display_sparc_hwcaps2(int mask)13547 display_sparc_hwcaps2 (int mask)
13548 {
13549   if (mask)
13550     {
13551       int first = 1;
13552 
13553       if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
13554 	fputs ("fjathplus", stdout), first = 0;
13555       if (mask & ELF_SPARC_HWCAP2_VIS3B)
13556 	printf ("%svis3b", first ? "" : "|"), first = 0;
13557       if (mask & ELF_SPARC_HWCAP2_ADP)
13558 	printf ("%sadp", first ? "" : "|"), first = 0;
13559       if (mask & ELF_SPARC_HWCAP2_SPARC5)
13560 	printf ("%ssparc5", first ? "" : "|"), first = 0;
13561       if (mask & ELF_SPARC_HWCAP2_MWAIT)
13562 	printf ("%smwait", first ? "" : "|"), first = 0;
13563       if (mask & ELF_SPARC_HWCAP2_XMPMUL)
13564 	printf ("%sxmpmul", first ? "" : "|"), first = 0;
13565       if (mask & ELF_SPARC_HWCAP2_XMONT)
13566 	printf ("%sxmont2", first ? "" : "|"), first = 0;
13567       if (mask & ELF_SPARC_HWCAP2_NSEC)
13568 	printf ("%snsec", first ? "" : "|"), first = 0;
13569       if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
13570 	printf ("%sfjathhpc", first ? "" : "|"), first = 0;
13571       if (mask & ELF_SPARC_HWCAP2_FJDES)
13572 	printf ("%sfjdes", first ? "" : "|"), first = 0;
13573       if (mask & ELF_SPARC_HWCAP2_FJAES)
13574 	printf ("%sfjaes", first ? "" : "|"), first = 0;
13575     }
13576   else
13577     fputc ('0', stdout);
13578   fputc ('\n', stdout);
13579 }
13580 
13581 static unsigned char *
display_sparc_gnu_attribute(unsigned char * p,int tag,const unsigned char * const end)13582 display_sparc_gnu_attribute (unsigned char * p,
13583 			     int tag,
13584 			     const unsigned char * const end)
13585 {
13586   unsigned int len;
13587   int val;
13588 
13589   if (tag == Tag_GNU_Sparc_HWCAPS)
13590     {
13591       val = read_uleb128 (p, &len, end);
13592       p += len;
13593       printf ("  Tag_GNU_Sparc_HWCAPS: ");
13594       display_sparc_hwcaps (val);
13595       return p;
13596     }
13597   if (tag == Tag_GNU_Sparc_HWCAPS2)
13598     {
13599       val = read_uleb128 (p, &len, end);
13600       p += len;
13601       printf ("  Tag_GNU_Sparc_HWCAPS2: ");
13602       display_sparc_hwcaps2 (val);
13603       return p;
13604     }
13605 
13606   return display_tag_value (tag, p, end);
13607 }
13608 
13609 static void
print_mips_fp_abi_value(int val)13610 print_mips_fp_abi_value (int val)
13611 {
13612   switch (val)
13613     {
13614     case Val_GNU_MIPS_ABI_FP_ANY:
13615       printf (_("Hard or soft float\n"));
13616       break;
13617     case Val_GNU_MIPS_ABI_FP_DOUBLE:
13618       printf (_("Hard float (double precision)\n"));
13619       break;
13620     case Val_GNU_MIPS_ABI_FP_SINGLE:
13621       printf (_("Hard float (single precision)\n"));
13622       break;
13623     case Val_GNU_MIPS_ABI_FP_SOFT:
13624       printf (_("Soft float\n"));
13625       break;
13626     case Val_GNU_MIPS_ABI_FP_OLD_64:
13627       printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
13628       break;
13629     case Val_GNU_MIPS_ABI_FP_XX:
13630       printf (_("Hard float (32-bit CPU, Any FPU)\n"));
13631       break;
13632     case Val_GNU_MIPS_ABI_FP_64:
13633       printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
13634       break;
13635     case Val_GNU_MIPS_ABI_FP_64A:
13636       printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
13637       break;
13638     case Val_GNU_MIPS_ABI_FP_NAN2008:
13639       printf (_("NaN 2008 compatibility\n"));
13640       break;
13641     default:
13642       printf ("??? (%d)\n", val);
13643       break;
13644     }
13645 }
13646 
13647 static unsigned char *
display_mips_gnu_attribute(unsigned char * p,int tag,const unsigned char * const end)13648 display_mips_gnu_attribute (unsigned char * p,
13649 			    int tag,
13650 			    const unsigned char * const end)
13651 {
13652   if (tag == Tag_GNU_MIPS_ABI_FP)
13653     {
13654       unsigned int len;
13655       int val;
13656 
13657       val = read_uleb128 (p, &len, end);
13658       p += len;
13659       printf ("  Tag_GNU_MIPS_ABI_FP: ");
13660 
13661       print_mips_fp_abi_value (val);
13662 
13663       return p;
13664    }
13665 
13666   if (tag == Tag_GNU_MIPS_ABI_MSA)
13667     {
13668       unsigned int len;
13669       int val;
13670 
13671       val = read_uleb128 (p, &len, end);
13672       p += len;
13673       printf ("  Tag_GNU_MIPS_ABI_MSA: ");
13674 
13675       switch (val)
13676 	{
13677 	case Val_GNU_MIPS_ABI_MSA_ANY:
13678 	  printf (_("Any MSA or not\n"));
13679 	  break;
13680 	case Val_GNU_MIPS_ABI_MSA_128:
13681 	  printf (_("128-bit MSA\n"));
13682 	  break;
13683 	default:
13684 	  printf ("??? (%d)\n", val);
13685 	  break;
13686 	}
13687       return p;
13688     }
13689 
13690   return display_tag_value (tag & 1, p, end);
13691 }
13692 
13693 static unsigned char *
display_tic6x_attribute(unsigned char * p,const unsigned char * const end)13694 display_tic6x_attribute (unsigned char * p,
13695 			 const unsigned char * const end)
13696 {
13697   int tag;
13698   unsigned int len;
13699   int val;
13700 
13701   tag = read_uleb128 (p, &len, end);
13702   p += len;
13703 
13704   switch (tag)
13705     {
13706     case Tag_ISA:
13707       val = read_uleb128 (p, &len, end);
13708       p += len;
13709       printf ("  Tag_ISA: ");
13710 
13711       switch (val)
13712 	{
13713 	case C6XABI_Tag_ISA_none:
13714 	  printf (_("None\n"));
13715 	  break;
13716 	case C6XABI_Tag_ISA_C62X:
13717 	  printf ("C62x\n");
13718 	  break;
13719 	case C6XABI_Tag_ISA_C67X:
13720 	  printf ("C67x\n");
13721 	  break;
13722 	case C6XABI_Tag_ISA_C67XP:
13723 	  printf ("C67x+\n");
13724 	  break;
13725 	case C6XABI_Tag_ISA_C64X:
13726 	  printf ("C64x\n");
13727 	  break;
13728 	case C6XABI_Tag_ISA_C64XP:
13729 	  printf ("C64x+\n");
13730 	  break;
13731 	case C6XABI_Tag_ISA_C674X:
13732 	  printf ("C674x\n");
13733 	  break;
13734 	default:
13735 	  printf ("??? (%d)\n", val);
13736 	  break;
13737 	}
13738       return p;
13739 
13740     case Tag_ABI_wchar_t:
13741       val = read_uleb128 (p, &len, end);
13742       p += len;
13743       printf ("  Tag_ABI_wchar_t: ");
13744       switch (val)
13745 	{
13746 	case 0:
13747 	  printf (_("Not used\n"));
13748 	  break;
13749 	case 1:
13750 	  printf (_("2 bytes\n"));
13751 	  break;
13752 	case 2:
13753 	  printf (_("4 bytes\n"));
13754 	  break;
13755 	default:
13756 	  printf ("??? (%d)\n", val);
13757 	  break;
13758 	}
13759       return p;
13760 
13761     case Tag_ABI_stack_align_needed:
13762       val = read_uleb128 (p, &len, end);
13763       p += len;
13764       printf ("  Tag_ABI_stack_align_needed: ");
13765       switch (val)
13766 	{
13767 	case 0:
13768 	  printf (_("8-byte\n"));
13769 	  break;
13770 	case 1:
13771 	  printf (_("16-byte\n"));
13772 	  break;
13773 	default:
13774 	  printf ("??? (%d)\n", val);
13775 	  break;
13776 	}
13777       return p;
13778 
13779     case Tag_ABI_stack_align_preserved:
13780       val = read_uleb128 (p, &len, end);
13781       p += len;
13782       printf ("  Tag_ABI_stack_align_preserved: ");
13783       switch (val)
13784 	{
13785 	case 0:
13786 	  printf (_("8-byte\n"));
13787 	  break;
13788 	case 1:
13789 	  printf (_("16-byte\n"));
13790 	  break;
13791 	default:
13792 	  printf ("??? (%d)\n", val);
13793 	  break;
13794 	}
13795       return p;
13796 
13797     case Tag_ABI_DSBT:
13798       val = read_uleb128 (p, &len, end);
13799       p += len;
13800       printf ("  Tag_ABI_DSBT: ");
13801       switch (val)
13802 	{
13803 	case 0:
13804 	  printf (_("DSBT addressing not used\n"));
13805 	  break;
13806 	case 1:
13807 	  printf (_("DSBT addressing used\n"));
13808 	  break;
13809 	default:
13810 	  printf ("??? (%d)\n", val);
13811 	  break;
13812 	}
13813       return p;
13814 
13815     case Tag_ABI_PID:
13816       val = read_uleb128 (p, &len, end);
13817       p += len;
13818       printf ("  Tag_ABI_PID: ");
13819       switch (val)
13820 	{
13821 	case 0:
13822 	  printf (_("Data addressing position-dependent\n"));
13823 	  break;
13824 	case 1:
13825 	  printf (_("Data addressing position-independent, GOT near DP\n"));
13826 	  break;
13827 	case 2:
13828 	  printf (_("Data addressing position-independent, GOT far from DP\n"));
13829 	  break;
13830 	default:
13831 	  printf ("??? (%d)\n", val);
13832 	  break;
13833 	}
13834       return p;
13835 
13836     case Tag_ABI_PIC:
13837       val = read_uleb128 (p, &len, end);
13838       p += len;
13839       printf ("  Tag_ABI_PIC: ");
13840       switch (val)
13841 	{
13842 	case 0:
13843 	  printf (_("Code addressing position-dependent\n"));
13844 	  break;
13845 	case 1:
13846 	  printf (_("Code addressing position-independent\n"));
13847 	  break;
13848 	default:
13849 	  printf ("??? (%d)\n", val);
13850 	  break;
13851 	}
13852       return p;
13853 
13854     case Tag_ABI_array_object_alignment:
13855       val = read_uleb128 (p, &len, end);
13856       p += len;
13857       printf ("  Tag_ABI_array_object_alignment: ");
13858       switch (val)
13859 	{
13860 	case 0:
13861 	  printf (_("8-byte\n"));
13862 	  break;
13863 	case 1:
13864 	  printf (_("4-byte\n"));
13865 	  break;
13866 	case 2:
13867 	  printf (_("16-byte\n"));
13868 	  break;
13869 	default:
13870 	  printf ("??? (%d)\n", val);
13871 	  break;
13872 	}
13873       return p;
13874 
13875     case Tag_ABI_array_object_align_expected:
13876       val = read_uleb128 (p, &len, end);
13877       p += len;
13878       printf ("  Tag_ABI_array_object_align_expected: ");
13879       switch (val)
13880 	{
13881 	case 0:
13882 	  printf (_("8-byte\n"));
13883 	  break;
13884 	case 1:
13885 	  printf (_("4-byte\n"));
13886 	  break;
13887 	case 2:
13888 	  printf (_("16-byte\n"));
13889 	  break;
13890 	default:
13891 	  printf ("??? (%d)\n", val);
13892 	  break;
13893 	}
13894       return p;
13895 
13896     case Tag_ABI_compatibility:
13897       {
13898 	val = read_uleb128 (p, &len, end);
13899 	p += len;
13900 	printf ("  Tag_ABI_compatibility: ");
13901 	printf (_("flag = %d, vendor = "), val);
13902 	if (p < end - 1)
13903 	  {
13904 	    size_t maxlen = (end - p) - 1;
13905 
13906 	    print_symbol ((int) maxlen, (const char *) p);
13907 	    p += strnlen ((char *) p, maxlen) + 1;
13908 	  }
13909 	else
13910 	  {
13911 	    printf (_("<corrupt>"));
13912 	    p = (unsigned char *) end;
13913 	  }
13914 	putchar ('\n');
13915 	return p;
13916       }
13917 
13918     case Tag_ABI_conformance:
13919       {
13920 	printf ("  Tag_ABI_conformance: \"");
13921 	if (p < end - 1)
13922 	  {
13923 	    size_t maxlen = (end - p) - 1;
13924 
13925 	    print_symbol ((int) maxlen, (const char *) p);
13926 	    p += strnlen ((char *) p, maxlen) + 1;
13927 	  }
13928 	else
13929 	  {
13930 	    printf (_("<corrupt>"));
13931 	    p = (unsigned char *) end;
13932 	  }
13933 	printf ("\"\n");
13934 	return p;
13935       }
13936     }
13937 
13938   return display_tag_value (tag, p, end);
13939 }
13940 
13941 static void
display_raw_attribute(unsigned char * p,unsigned char * end)13942 display_raw_attribute (unsigned char * p, unsigned char * end)
13943 {
13944   unsigned long addr = 0;
13945   size_t bytes = end - p;
13946 
13947   assert (end > p);
13948   while (bytes)
13949     {
13950       int j;
13951       int k;
13952       int lbytes = (bytes > 16 ? 16 : bytes);
13953 
13954       printf ("  0x%8.8lx ", addr);
13955 
13956       for (j = 0; j < 16; j++)
13957 	{
13958 	  if (j < lbytes)
13959 	    printf ("%2.2x", p[j]);
13960 	  else
13961 	    printf ("  ");
13962 
13963 	  if ((j & 3) == 3)
13964 	    printf (" ");
13965 	}
13966 
13967       for (j = 0; j < lbytes; j++)
13968 	{
13969 	  k = p[j];
13970 	  if (k >= ' ' && k < 0x7f)
13971 	    printf ("%c", k);
13972 	  else
13973 	    printf (".");
13974 	}
13975 
13976       putchar ('\n');
13977 
13978       p  += lbytes;
13979       bytes -= lbytes;
13980       addr += lbytes;
13981     }
13982 
13983   putchar ('\n');
13984 }
13985 
13986 static unsigned char *
display_msp430x_attribute(unsigned char * p,const unsigned char * const end)13987 display_msp430x_attribute (unsigned char * p,
13988 			   const unsigned char * const end)
13989 {
13990   unsigned int len;
13991   int val;
13992   int tag;
13993 
13994   tag = read_uleb128 (p, & len, end);
13995   p += len;
13996 
13997   switch (tag)
13998     {
13999     case OFBA_MSPABI_Tag_ISA:
14000       val = read_uleb128 (p, &len, end);
14001       p += len;
14002       printf ("  Tag_ISA: ");
14003       switch (val)
14004 	{
14005 	case 0: printf (_("None\n")); break;
14006 	case 1: printf (_("MSP430\n")); break;
14007 	case 2: printf (_("MSP430X\n")); break;
14008 	default: printf ("??? (%d)\n", val); break;
14009 	}
14010       break;
14011 
14012     case OFBA_MSPABI_Tag_Code_Model:
14013       val = read_uleb128 (p, &len, end);
14014       p += len;
14015       printf ("  Tag_Code_Model: ");
14016       switch (val)
14017 	{
14018 	case 0: printf (_("None\n")); break;
14019 	case 1: printf (_("Small\n")); break;
14020 	case 2: printf (_("Large\n")); break;
14021 	default: printf ("??? (%d)\n", val); break;
14022 	}
14023       break;
14024 
14025     case OFBA_MSPABI_Tag_Data_Model:
14026       val = read_uleb128 (p, &len, end);
14027       p += len;
14028       printf ("  Tag_Data_Model: ");
14029       switch (val)
14030 	{
14031 	case 0: printf (_("None\n")); break;
14032 	case 1: printf (_("Small\n")); break;
14033 	case 2: printf (_("Large\n")); break;
14034 	case 3: printf (_("Restricted Large\n")); break;
14035 	default: printf ("??? (%d)\n", val); break;
14036 	}
14037       break;
14038 
14039     default:
14040       printf (_("  <unknown tag %d>: "), tag);
14041 
14042       if (tag & 1)
14043 	{
14044 	  putchar ('"');
14045 	  if (p < end - 1)
14046 	    {
14047 	      size_t maxlen = (end - p) - 1;
14048 
14049 	      print_symbol ((int) maxlen, (const char *) p);
14050 	      p += strnlen ((char *) p, maxlen) + 1;
14051 	    }
14052 	  else
14053 	    {
14054 	      printf (_("<corrupt>"));
14055 	      p = (unsigned char *) end;
14056 	    }
14057 	  printf ("\"\n");
14058 	}
14059       else
14060 	{
14061 	  val = read_uleb128 (p, &len, end);
14062 	  p += len;
14063 	  printf ("%d (0x%x)\n", val, val);
14064 	}
14065       break;
14066    }
14067 
14068   assert (p <= end);
14069   return p;
14070 }
14071 
14072 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))14073 process_attributes (FILE * file,
14074 		    const char * public_name,
14075 		    unsigned int proc_type,
14076 		    unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
14077 		    unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int, const unsigned char * const))
14078 {
14079   Elf_Internal_Shdr * sect;
14080   unsigned i;
14081 
14082   /* Find the section header so that we get the size.  */
14083   for (i = 0, sect = section_headers;
14084        i < elf_header.e_shnum;
14085        i++, sect++)
14086     {
14087       unsigned char * contents;
14088       unsigned char * p;
14089 
14090       if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
14091 	continue;
14092 
14093       contents = (unsigned char *) get_data (NULL, file, sect->sh_offset, 1,
14094                                              sect->sh_size, _("attributes"));
14095       if (contents == NULL)
14096 	continue;
14097 
14098       p = contents;
14099       if (*p == 'A')
14100 	{
14101 	  bfd_vma section_len;
14102 
14103 	  section_len = sect->sh_size - 1;
14104 	  p++;
14105 
14106 	  while (section_len > 0)
14107 	    {
14108 	      bfd_vma attr_len;
14109 	      unsigned int namelen;
14110 	      bfd_boolean public_section;
14111 	      bfd_boolean gnu_section;
14112 
14113 	      if (section_len <= 4)
14114 		{
14115 		  error (_("Tag section ends prematurely\n"));
14116 		  break;
14117 		}
14118 	      attr_len = byte_get (p, 4);
14119 	      p += 4;
14120 
14121 	      if (attr_len > section_len)
14122 		{
14123 		  error (_("Bad attribute length (%u > %u)\n"),
14124 			  (unsigned) attr_len, (unsigned) section_len);
14125 		  attr_len = section_len;
14126 		}
14127 	      /* PR 17531: file: 001-101425-0.004  */
14128 	      else if (attr_len < 5)
14129 		{
14130 		  error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
14131 		  break;
14132 		}
14133 
14134 	      section_len -= attr_len;
14135 	      attr_len -= 4;
14136 
14137 	      namelen = strnlen ((char *) p, attr_len) + 1;
14138 	      if (namelen == 0 || namelen >= attr_len)
14139 		{
14140 		  error (_("Corrupt attribute section name\n"));
14141 		  break;
14142 		}
14143 
14144 	      printf (_("Attribute Section: "));
14145 	      print_symbol (INT_MAX, (const char *) p);
14146 	      putchar ('\n');
14147 
14148 	      if (public_name && streq ((char *) p, public_name))
14149 		public_section = TRUE;
14150 	      else
14151 		public_section = FALSE;
14152 
14153 	      if (streq ((char *) p, "gnu"))
14154 		gnu_section = TRUE;
14155 	      else
14156 		gnu_section = FALSE;
14157 
14158 	      p += namelen;
14159 	      attr_len -= namelen;
14160 
14161 	      while (attr_len > 0 && p < contents + sect->sh_size)
14162 		{
14163 		  int tag;
14164 		  int val;
14165 		  bfd_vma size;
14166 		  unsigned char * end;
14167 
14168 		  /* PR binutils/17531: Safe handling of corrupt files.  */
14169 		  if (attr_len < 6)
14170 		    {
14171 		      error (_("Unused bytes at end of section\n"));
14172 		      section_len = 0;
14173 		      break;
14174 		    }
14175 
14176 		  tag = *(p++);
14177 		  size = byte_get (p, 4);
14178 		  if (size > attr_len)
14179 		    {
14180 		      error (_("Bad subsection length (%u > %u)\n"),
14181 			      (unsigned) size, (unsigned) attr_len);
14182 		      size = attr_len;
14183 		    }
14184 		  /* PR binutils/17531: Safe handling of corrupt files.  */
14185 		  if (size < 6)
14186 		    {
14187 		      error (_("Bad subsection length (%u < 6)\n"),
14188 			      (unsigned) size);
14189 		      section_len = 0;
14190 		      break;
14191 		    }
14192 
14193 		  attr_len -= size;
14194 		  end = p + size - 1;
14195 		  assert (end <= contents + sect->sh_size);
14196 		  p += 4;
14197 
14198 		  switch (tag)
14199 		    {
14200 		    case 1:
14201 		      printf (_("File Attributes\n"));
14202 		      break;
14203 		    case 2:
14204 		      printf (_("Section Attributes:"));
14205 		      goto do_numlist;
14206 		    case 3:
14207 		      printf (_("Symbol Attributes:"));
14208 		    do_numlist:
14209 		      for (;;)
14210 			{
14211 			  unsigned int j;
14212 
14213 			  val = read_uleb128 (p, &j, end);
14214 			  p += j;
14215 			  if (val == 0)
14216 			    break;
14217 			  printf (" %d", val);
14218 			}
14219 		      printf ("\n");
14220 		      break;
14221 		    default:
14222 		      printf (_("Unknown tag: %d\n"), tag);
14223 		      public_section = FALSE;
14224 		      break;
14225 		    }
14226 
14227 		  if (public_section && display_pub_attribute != NULL)
14228 		    {
14229 		      while (p < end)
14230 			p = display_pub_attribute (p, end);
14231 		      assert (p <= end);
14232 		    }
14233 		  else if (gnu_section && display_proc_gnu_attribute != NULL)
14234 		    {
14235 		      while (p < end)
14236 			p = display_gnu_attribute (p,
14237 						   display_proc_gnu_attribute,
14238 						   end);
14239 		      assert (p <= end);
14240 		    }
14241 		  else if (p < end)
14242 		    {
14243 		      printf (_("  Unknown attribute:\n"));
14244 		      display_raw_attribute (p, end);
14245 		      p = end;
14246 		    }
14247 		  else
14248 		    attr_len = 0;
14249 		}
14250 	    }
14251 	}
14252       else
14253 	printf (_("Unknown format '%c' (%d)\n"), *p, *p);
14254 
14255       free (contents);
14256     }
14257   return 1;
14258 }
14259 
14260 static int
process_arm_specific(FILE * file)14261 process_arm_specific (FILE * file)
14262 {
14263   return process_attributes (file, "aeabi", SHT_ARM_ATTRIBUTES,
14264 			     display_arm_attribute, NULL);
14265 }
14266 
14267 static int
process_power_specific(FILE * file)14268 process_power_specific (FILE * file)
14269 {
14270   return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
14271 			     display_power_gnu_attribute);
14272 }
14273 
14274 static int
process_s390_specific(FILE * file)14275 process_s390_specific (FILE * file)
14276 {
14277   return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
14278 			     display_s390_gnu_attribute);
14279 }
14280 
14281 static int
process_sparc_specific(FILE * file)14282 process_sparc_specific (FILE * file)
14283 {
14284   return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
14285 			     display_sparc_gnu_attribute);
14286 }
14287 
14288 static int
process_tic6x_specific(FILE * file)14289 process_tic6x_specific (FILE * file)
14290 {
14291   return process_attributes (file, "c6xabi", SHT_C6000_ATTRIBUTES,
14292 			     display_tic6x_attribute, NULL);
14293 }
14294 
14295 static int
process_msp430x_specific(FILE * file)14296 process_msp430x_specific (FILE * file)
14297 {
14298   return process_attributes (file, "mspabi", SHT_MSP430_ATTRIBUTES,
14299 			     display_msp430x_attribute, NULL);
14300 }
14301 
14302 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
14303    Print the Address, Access and Initial fields of an entry at VMA ADDR
14304    and return the VMA of the next entry, or -1 if there was a problem.
14305    Does not read from DATA_END or beyond.  */
14306 
14307 static bfd_vma
print_mips_got_entry(unsigned char * data,bfd_vma pltgot,bfd_vma addr,unsigned char * data_end)14308 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
14309 		      unsigned char * data_end)
14310 {
14311   printf ("  ");
14312   print_vma (addr, LONG_HEX);
14313   printf (" ");
14314   if (addr < pltgot + 0xfff0)
14315     printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
14316   else
14317     printf ("%10s", "");
14318   printf (" ");
14319   if (data == NULL)
14320     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
14321   else
14322     {
14323       bfd_vma entry;
14324       unsigned char * from = data + addr - pltgot;
14325 
14326       if (from + (is_32bit_elf ? 4 : 8) > data_end)
14327 	{
14328 	  warn (_("MIPS GOT entry extends beyond the end of available data\n"));
14329 	  printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
14330 	  return (bfd_vma) -1;
14331 	}
14332       else
14333 	{
14334 	  entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
14335 	  print_vma (entry, LONG_HEX);
14336 	}
14337     }
14338   return addr + (is_32bit_elf ? 4 : 8);
14339 }
14340 
14341 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
14342    PLTGOT.  Print the Address and Initial fields of an entry at VMA
14343    ADDR and return the VMA of the next entry.  */
14344 
14345 static bfd_vma
print_mips_pltgot_entry(unsigned char * data,bfd_vma pltgot,bfd_vma addr)14346 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
14347 {
14348   printf ("  ");
14349   print_vma (addr, LONG_HEX);
14350   printf (" ");
14351   if (data == NULL)
14352     printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
14353   else
14354     {
14355       bfd_vma entry;
14356 
14357       entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
14358       print_vma (entry, LONG_HEX);
14359     }
14360   return addr + (is_32bit_elf ? 4 : 8);
14361 }
14362 
14363 static void
print_mips_ases(unsigned int mask)14364 print_mips_ases (unsigned int mask)
14365 {
14366   if (mask & AFL_ASE_DSP)
14367     fputs ("\n\tDSP ASE", stdout);
14368   if (mask & AFL_ASE_DSPR2)
14369     fputs ("\n\tDSP R2 ASE", stdout);
14370   if (mask & AFL_ASE_DSPR3)
14371     fputs ("\n\tDSP R3 ASE", stdout);
14372   if (mask & AFL_ASE_EVA)
14373     fputs ("\n\tEnhanced VA Scheme", stdout);
14374   if (mask & AFL_ASE_MCU)
14375     fputs ("\n\tMCU (MicroController) ASE", stdout);
14376   if (mask & AFL_ASE_MDMX)
14377     fputs ("\n\tMDMX ASE", stdout);
14378   if (mask & AFL_ASE_MIPS3D)
14379     fputs ("\n\tMIPS-3D ASE", stdout);
14380   if (mask & AFL_ASE_MT)
14381     fputs ("\n\tMT ASE", stdout);
14382   if (mask & AFL_ASE_SMARTMIPS)
14383     fputs ("\n\tSmartMIPS ASE", stdout);
14384   if (mask & AFL_ASE_VIRT)
14385     fputs ("\n\tVZ ASE", stdout);
14386   if (mask & AFL_ASE_MSA)
14387     fputs ("\n\tMSA ASE", stdout);
14388   if (mask & AFL_ASE_MIPS16)
14389     fputs ("\n\tMIPS16 ASE", stdout);
14390   if (mask & AFL_ASE_MICROMIPS)
14391     fputs ("\n\tMICROMIPS ASE", stdout);
14392   if (mask & AFL_ASE_XPA)
14393     fputs ("\n\tXPA ASE", stdout);
14394   if (mask == 0)
14395     fprintf (stdout, "\n\t%s", _("None"));
14396   else if ((mask & ~AFL_ASE_MASK) != 0)
14397     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
14398 }
14399 
14400 static void
print_mips_isa_ext(unsigned int isa_ext)14401 print_mips_isa_ext (unsigned int isa_ext)
14402 {
14403   switch (isa_ext)
14404     {
14405     case 0:
14406       fputs (_("None"), stdout);
14407       break;
14408     case AFL_EXT_XLR:
14409       fputs ("RMI XLR", stdout);
14410       break;
14411     case AFL_EXT_OCTEON3:
14412       fputs ("Cavium Networks Octeon3", stdout);
14413       break;
14414     case AFL_EXT_OCTEON2:
14415       fputs ("Cavium Networks Octeon2", stdout);
14416       break;
14417     case AFL_EXT_OCTEONP:
14418       fputs ("Cavium Networks OcteonP", stdout);
14419       break;
14420     case AFL_EXT_LOONGSON_3A:
14421       fputs ("Loongson 3A", stdout);
14422       break;
14423     case AFL_EXT_OCTEON:
14424       fputs ("Cavium Networks Octeon", stdout);
14425       break;
14426     case AFL_EXT_5900:
14427       fputs ("Toshiba R5900", stdout);
14428       break;
14429     case AFL_EXT_4650:
14430       fputs ("MIPS R4650", stdout);
14431       break;
14432     case AFL_EXT_4010:
14433       fputs ("LSI R4010", stdout);
14434       break;
14435     case AFL_EXT_4100:
14436       fputs ("NEC VR4100", stdout);
14437       break;
14438     case AFL_EXT_3900:
14439       fputs ("Toshiba R3900", stdout);
14440       break;
14441     case AFL_EXT_10000:
14442       fputs ("MIPS R10000", stdout);
14443       break;
14444     case AFL_EXT_SB1:
14445       fputs ("Broadcom SB-1", stdout);
14446       break;
14447     case AFL_EXT_4111:
14448       fputs ("NEC VR4111/VR4181", stdout);
14449       break;
14450     case AFL_EXT_4120:
14451       fputs ("NEC VR4120", stdout);
14452       break;
14453     case AFL_EXT_5400:
14454       fputs ("NEC VR5400", stdout);
14455       break;
14456     case AFL_EXT_5500:
14457       fputs ("NEC VR5500", stdout);
14458       break;
14459     case AFL_EXT_LOONGSON_2E:
14460       fputs ("ST Microelectronics Loongson 2E", stdout);
14461       break;
14462     case AFL_EXT_LOONGSON_2F:
14463       fputs ("ST Microelectronics Loongson 2F", stdout);
14464       break;
14465     default:
14466       fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
14467     }
14468 }
14469 
14470 static int
get_mips_reg_size(int reg_size)14471 get_mips_reg_size (int reg_size)
14472 {
14473   return (reg_size == AFL_REG_NONE) ? 0
14474 	 : (reg_size == AFL_REG_32) ? 32
14475 	 : (reg_size == AFL_REG_64) ? 64
14476 	 : (reg_size == AFL_REG_128) ? 128
14477 	 : -1;
14478 }
14479 
14480 static int
process_mips_specific(FILE * file)14481 process_mips_specific (FILE * file)
14482 {
14483   Elf_Internal_Dyn * entry;
14484   Elf_Internal_Shdr *sect = NULL;
14485   size_t liblist_offset = 0;
14486   size_t liblistno = 0;
14487   size_t conflictsno = 0;
14488   size_t options_offset = 0;
14489   size_t conflicts_offset = 0;
14490   size_t pltrelsz = 0;
14491   size_t pltrel = 0;
14492   bfd_vma pltgot = 0;
14493   bfd_vma mips_pltgot = 0;
14494   bfd_vma jmprel = 0;
14495   bfd_vma local_gotno = 0;
14496   bfd_vma gotsym = 0;
14497   bfd_vma symtabno = 0;
14498 
14499   process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
14500 		      display_mips_gnu_attribute);
14501 
14502   sect = find_section (".MIPS.abiflags");
14503 
14504   if (sect != NULL)
14505     {
14506       Elf_External_ABIFlags_v0 *abiflags_ext;
14507       Elf_Internal_ABIFlags_v0 abiflags_in;
14508 
14509       if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
14510 	fputs ("\nCorrupt ABI Flags section.\n", stdout);
14511       else
14512 	{
14513 	  abiflags_ext = get_data (NULL, file, sect->sh_offset, 1,
14514 				   sect->sh_size, _("MIPS ABI Flags section"));
14515 	  if (abiflags_ext)
14516 	    {
14517 	      abiflags_in.version = BYTE_GET (abiflags_ext->version);
14518 	      abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
14519 	      abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
14520 	      abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
14521 	      abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
14522 	      abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
14523 	      abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
14524 	      abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
14525 	      abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
14526 	      abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
14527 	      abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
14528 
14529 	      printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
14530 	      printf ("\nISA: MIPS%d", abiflags_in.isa_level);
14531 	      if (abiflags_in.isa_rev > 1)
14532 		printf ("r%d", abiflags_in.isa_rev);
14533 	      printf ("\nGPR size: %d",
14534 		      get_mips_reg_size (abiflags_in.gpr_size));
14535 	      printf ("\nCPR1 size: %d",
14536 		      get_mips_reg_size (abiflags_in.cpr1_size));
14537 	      printf ("\nCPR2 size: %d",
14538 		      get_mips_reg_size (abiflags_in.cpr2_size));
14539 	      fputs ("\nFP ABI: ", stdout);
14540 	      print_mips_fp_abi_value (abiflags_in.fp_abi);
14541 	      fputs ("ISA Extension: ", stdout);
14542 	      print_mips_isa_ext (abiflags_in.isa_ext);
14543 	      fputs ("\nASEs:", stdout);
14544 	      print_mips_ases (abiflags_in.ases);
14545 	      printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
14546 	      printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
14547 	      fputc ('\n', stdout);
14548 	      free (abiflags_ext);
14549 	    }
14550 	}
14551     }
14552 
14553   /* We have a lot of special sections.  Thanks SGI!  */
14554   if (dynamic_section == NULL)
14555     /* No information available.  */
14556     return 0;
14557 
14558   for (entry = dynamic_section;
14559        /* PR 17531 file: 012-50589-0.004.  */
14560        entry < dynamic_section + dynamic_nent && entry->d_tag != DT_NULL;
14561        ++entry)
14562     switch (entry->d_tag)
14563       {
14564       case DT_MIPS_LIBLIST:
14565 	liblist_offset
14566 	  = offset_from_vma (file, entry->d_un.d_val,
14567 			     liblistno * sizeof (Elf32_External_Lib));
14568 	break;
14569       case DT_MIPS_LIBLISTNO:
14570 	liblistno = entry->d_un.d_val;
14571 	break;
14572       case DT_MIPS_OPTIONS:
14573 	options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
14574 	break;
14575       case DT_MIPS_CONFLICT:
14576 	conflicts_offset
14577 	  = offset_from_vma (file, entry->d_un.d_val,
14578 			     conflictsno * sizeof (Elf32_External_Conflict));
14579 	break;
14580       case DT_MIPS_CONFLICTNO:
14581 	conflictsno = entry->d_un.d_val;
14582 	break;
14583       case DT_PLTGOT:
14584 	pltgot = entry->d_un.d_ptr;
14585 	break;
14586       case DT_MIPS_LOCAL_GOTNO:
14587 	local_gotno = entry->d_un.d_val;
14588 	break;
14589       case DT_MIPS_GOTSYM:
14590 	gotsym = entry->d_un.d_val;
14591 	break;
14592       case DT_MIPS_SYMTABNO:
14593 	symtabno = entry->d_un.d_val;
14594 	break;
14595       case DT_MIPS_PLTGOT:
14596 	mips_pltgot = entry->d_un.d_ptr;
14597 	break;
14598       case DT_PLTREL:
14599 	pltrel = entry->d_un.d_val;
14600 	break;
14601       case DT_PLTRELSZ:
14602 	pltrelsz = entry->d_un.d_val;
14603 	break;
14604       case DT_JMPREL:
14605 	jmprel = entry->d_un.d_ptr;
14606 	break;
14607       default:
14608 	break;
14609       }
14610 
14611   if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
14612     {
14613       Elf32_External_Lib * elib;
14614       size_t cnt;
14615 
14616       elib = (Elf32_External_Lib *) get_data (NULL, file, liblist_offset,
14617                                               liblistno,
14618                                               sizeof (Elf32_External_Lib),
14619                                               _("liblist section data"));
14620       if (elib)
14621 	{
14622 	  printf (_("\nSection '.liblist' contains %lu entries:\n"),
14623 		  (unsigned long) liblistno);
14624 	  fputs (_("     Library              Time Stamp          Checksum   Version Flags\n"),
14625 		 stdout);
14626 
14627 	  for (cnt = 0; cnt < liblistno; ++cnt)
14628 	    {
14629 	      Elf32_Lib liblist;
14630 	      time_t atime;
14631 	      char timebuf[128];
14632 	      struct tm * tmp;
14633 
14634 	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
14635 	      atime = BYTE_GET (elib[cnt].l_time_stamp);
14636 	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
14637 	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
14638 	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
14639 
14640 	      tmp = gmtime (&atime);
14641 	      snprintf (timebuf, sizeof (timebuf),
14642 			"%04u-%02u-%02uT%02u:%02u:%02u",
14643 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
14644 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
14645 
14646 	      printf ("%3lu: ", (unsigned long) cnt);
14647 	      if (VALID_DYNAMIC_NAME (liblist.l_name))
14648 		print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
14649 	      else
14650 		printf (_("<corrupt: %9ld>"), liblist.l_name);
14651 	      printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
14652 		      liblist.l_version);
14653 
14654 	      if (liblist.l_flags == 0)
14655 		puts (_(" NONE"));
14656 	      else
14657 		{
14658 		  static const struct
14659 		  {
14660 		    const char * name;
14661 		    int bit;
14662 		  }
14663 		  l_flags_vals[] =
14664 		  {
14665 		    { " EXACT_MATCH", LL_EXACT_MATCH },
14666 		    { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
14667 		    { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
14668 		    { " EXPORTS", LL_EXPORTS },
14669 		    { " DELAY_LOAD", LL_DELAY_LOAD },
14670 		    { " DELTA", LL_DELTA }
14671 		  };
14672 		  int flags = liblist.l_flags;
14673 		  size_t fcnt;
14674 
14675 		  for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
14676 		    if ((flags & l_flags_vals[fcnt].bit) != 0)
14677 		      {
14678 			fputs (l_flags_vals[fcnt].name, stdout);
14679 			flags ^= l_flags_vals[fcnt].bit;
14680 		      }
14681 		  if (flags != 0)
14682 		    printf (" %#x", (unsigned int) flags);
14683 
14684 		  puts ("");
14685 		}
14686 	    }
14687 
14688 	  free (elib);
14689 	}
14690     }
14691 
14692   if (options_offset != 0)
14693     {
14694       Elf_External_Options * eopt;
14695       Elf_Internal_Options * iopt;
14696       Elf_Internal_Options * option;
14697       size_t offset;
14698       int cnt;
14699       sect = section_headers;
14700 
14701       /* Find the section header so that we get the size.  */
14702       sect = find_section_by_type (SHT_MIPS_OPTIONS);
14703       /* PR 17533 file: 012-277276-0.004.  */
14704       if (sect == NULL)
14705 	{
14706 	  error (_("No MIPS_OPTIONS header found\n"));
14707 	  return 0;
14708 	}
14709 
14710       eopt = (Elf_External_Options *) get_data (NULL, file, options_offset, 1,
14711                                                 sect->sh_size, _("options"));
14712       if (eopt)
14713 	{
14714 	  iopt = (Elf_Internal_Options *)
14715               cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (* iopt));
14716 	  if (iopt == NULL)
14717 	    {
14718 	      error (_("Out of memory allocatinf space for MIPS options\n"));
14719 	      return 0;
14720 	    }
14721 
14722 	  offset = cnt = 0;
14723 	  option = iopt;
14724 
14725 	  while (offset <= sect->sh_size - sizeof (* eopt))
14726 	    {
14727 	      Elf_External_Options * eoption;
14728 
14729 	      eoption = (Elf_External_Options *) ((char *) eopt + offset);
14730 
14731 	      option->kind = BYTE_GET (eoption->kind);
14732 	      option->size = BYTE_GET (eoption->size);
14733 	      option->section = BYTE_GET (eoption->section);
14734 	      option->info = BYTE_GET (eoption->info);
14735 
14736 	      /* PR 17531: file: ffa0fa3b.  */
14737 	      if (option->size < sizeof (* eopt)
14738 		  || offset + option->size > sect->sh_size)
14739 		{
14740 		  error (_("Invalid size (%u) for MIPS option\n"), option->size);
14741 		  return 0;
14742 		}
14743 	      offset += option->size;
14744 
14745 	      ++option;
14746 	      ++cnt;
14747 	    }
14748 
14749 	  printf (_("\nSection '%s' contains %d entries:\n"),
14750 		  printable_section_name (sect), cnt);
14751 
14752 	  option = iopt;
14753 	  offset = 0;
14754 
14755 	  while (cnt-- > 0)
14756 	    {
14757 	      size_t len;
14758 
14759 	      switch (option->kind)
14760 		{
14761 		case ODK_NULL:
14762 		  /* This shouldn't happen.  */
14763 		  printf (" NULL       %d %lx", option->section, option->info);
14764 		  break;
14765 		case ODK_REGINFO:
14766 		  printf (" REGINFO    ");
14767 		  if (elf_header.e_machine == EM_MIPS)
14768 		    {
14769 		      /* 32bit form.  */
14770 		      Elf32_External_RegInfo * ereg;
14771 		      Elf32_RegInfo reginfo;
14772 
14773 		      ereg = (Elf32_External_RegInfo *) (option + 1);
14774 		      reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
14775 		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
14776 		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
14777 		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
14778 		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
14779 		      reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
14780 
14781 		      printf ("GPR %08lx  GP 0x%lx\n",
14782 			      reginfo.ri_gprmask,
14783 			      (unsigned long) reginfo.ri_gp_value);
14784 		      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
14785 			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
14786 			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
14787 		    }
14788 		  else
14789 		    {
14790 		      /* 64 bit form.  */
14791 		      Elf64_External_RegInfo * ereg;
14792 		      Elf64_Internal_RegInfo reginfo;
14793 
14794 		      ereg = (Elf64_External_RegInfo *) (option + 1);
14795 		      reginfo.ri_gprmask    = BYTE_GET (ereg->ri_gprmask);
14796 		      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
14797 		      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
14798 		      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
14799 		      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
14800 		      reginfo.ri_gp_value   = BYTE_GET (ereg->ri_gp_value);
14801 
14802 		      printf ("GPR %08lx  GP 0x",
14803 			      reginfo.ri_gprmask);
14804 		      printf_vma (reginfo.ri_gp_value);
14805 		      printf ("\n");
14806 
14807 		      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
14808 			      reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
14809 			      reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
14810 		    }
14811 		  ++option;
14812 		  continue;
14813 		case ODK_EXCEPTIONS:
14814 		  fputs (" EXCEPTIONS fpe_min(", stdout);
14815 		  process_mips_fpe_exception (option->info & OEX_FPU_MIN);
14816 		  fputs (") fpe_max(", stdout);
14817 		  process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
14818 		  fputs (")", stdout);
14819 
14820 		  if (option->info & OEX_PAGE0)
14821 		    fputs (" PAGE0", stdout);
14822 		  if (option->info & OEX_SMM)
14823 		    fputs (" SMM", stdout);
14824 		  if (option->info & OEX_FPDBUG)
14825 		    fputs (" FPDBUG", stdout);
14826 		  if (option->info & OEX_DISMISS)
14827 		    fputs (" DISMISS", stdout);
14828 		  break;
14829 		case ODK_PAD:
14830 		  fputs (" PAD       ", stdout);
14831 		  if (option->info & OPAD_PREFIX)
14832 		    fputs (" PREFIX", stdout);
14833 		  if (option->info & OPAD_POSTFIX)
14834 		    fputs (" POSTFIX", stdout);
14835 		  if (option->info & OPAD_SYMBOL)
14836 		    fputs (" SYMBOL", stdout);
14837 		  break;
14838 		case ODK_HWPATCH:
14839 		  fputs (" HWPATCH   ", stdout);
14840 		  if (option->info & OHW_R4KEOP)
14841 		    fputs (" R4KEOP", stdout);
14842 		  if (option->info & OHW_R8KPFETCH)
14843 		    fputs (" R8KPFETCH", stdout);
14844 		  if (option->info & OHW_R5KEOP)
14845 		    fputs (" R5KEOP", stdout);
14846 		  if (option->info & OHW_R5KCVTL)
14847 		    fputs (" R5KCVTL", stdout);
14848 		  break;
14849 		case ODK_FILL:
14850 		  fputs (" FILL       ", stdout);
14851 		  /* XXX Print content of info word?  */
14852 		  break;
14853 		case ODK_TAGS:
14854 		  fputs (" TAGS       ", stdout);
14855 		  /* XXX Print content of info word?  */
14856 		  break;
14857 		case ODK_HWAND:
14858 		  fputs (" HWAND     ", stdout);
14859 		  if (option->info & OHWA0_R4KEOP_CHECKED)
14860 		    fputs (" R4KEOP_CHECKED", stdout);
14861 		  if (option->info & OHWA0_R4KEOP_CLEAN)
14862 		    fputs (" R4KEOP_CLEAN", stdout);
14863 		  break;
14864 		case ODK_HWOR:
14865 		  fputs (" HWOR      ", stdout);
14866 		  if (option->info & OHWA0_R4KEOP_CHECKED)
14867 		    fputs (" R4KEOP_CHECKED", stdout);
14868 		  if (option->info & OHWA0_R4KEOP_CLEAN)
14869 		    fputs (" R4KEOP_CLEAN", stdout);
14870 		  break;
14871 		case ODK_GP_GROUP:
14872 		  printf (" GP_GROUP  %#06lx  self-contained %#06lx",
14873 			  option->info & OGP_GROUP,
14874 			  (option->info & OGP_SELF) >> 16);
14875 		  break;
14876 		case ODK_IDENT:
14877 		  printf (" IDENT     %#06lx  self-contained %#06lx",
14878 			  option->info & OGP_GROUP,
14879 			  (option->info & OGP_SELF) >> 16);
14880 		  break;
14881 		default:
14882 		  /* This shouldn't happen.  */
14883 		  printf (" %3d ???     %d %lx",
14884 			  option->kind, option->section, option->info);
14885 		  break;
14886 		}
14887 
14888 	      len = sizeof (* eopt);
14889 	      while (len < option->size)
14890 		{
14891 		  unsigned char datum = * ((unsigned char *) eopt + offset + len);
14892 
14893 		  if (ISPRINT (datum))
14894 		    printf ("%c", datum);
14895 		  else
14896 		    printf ("\\%03o", datum);
14897 		  len ++;
14898 		}
14899 	      fputs ("\n", stdout);
14900 
14901 	      offset += option->size;
14902 	      ++option;
14903 	    }
14904 
14905 	  free (eopt);
14906 	}
14907     }
14908 
14909   if (conflicts_offset != 0 && conflictsno != 0)
14910     {
14911       Elf32_Conflict * iconf;
14912       size_t cnt;
14913 
14914       if (dynamic_symbols == NULL)
14915 	{
14916 	  error (_("conflict list found without a dynamic symbol table\n"));
14917 	  return 0;
14918 	}
14919 
14920       iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
14921       if (iconf == NULL)
14922 	{
14923 	  error (_("Out of memory allocating space for dynamic conflicts\n"));
14924 	  return 0;
14925 	}
14926 
14927       if (is_32bit_elf)
14928 	{
14929 	  Elf32_External_Conflict * econf32;
14930 
14931 	  econf32 = (Elf32_External_Conflict *)
14932               get_data (NULL, file, conflicts_offset, conflictsno,
14933                         sizeof (* econf32), _("conflict"));
14934 	  if (!econf32)
14935 	    return 0;
14936 
14937 	  for (cnt = 0; cnt < conflictsno; ++cnt)
14938 	    iconf[cnt] = BYTE_GET (econf32[cnt]);
14939 
14940 	  free (econf32);
14941 	}
14942       else
14943 	{
14944 	  Elf64_External_Conflict * econf64;
14945 
14946 	  econf64 = (Elf64_External_Conflict *)
14947               get_data (NULL, file, conflicts_offset, conflictsno,
14948                         sizeof (* econf64), _("conflict"));
14949 	  if (!econf64)
14950 	    return 0;
14951 
14952 	  for (cnt = 0; cnt < conflictsno; ++cnt)
14953 	    iconf[cnt] = BYTE_GET (econf64[cnt]);
14954 
14955 	  free (econf64);
14956 	}
14957 
14958       printf (_("\nSection '.conflict' contains %lu entries:\n"),
14959 	      (unsigned long) conflictsno);
14960       puts (_("  Num:    Index       Value  Name"));
14961 
14962       for (cnt = 0; cnt < conflictsno; ++cnt)
14963 	{
14964 	  printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
14965 
14966 	  if (iconf[cnt] >= num_dynamic_syms)
14967 	    printf (_("<corrupt symbol index>"));
14968 	  else
14969 	    {
14970 	      Elf_Internal_Sym * psym;
14971 
14972 	      psym = & dynamic_symbols[iconf[cnt]];
14973 	      print_vma (psym->st_value, FULL_HEX);
14974 	      putchar (' ');
14975 	      if (VALID_DYNAMIC_NAME (psym->st_name))
14976 		print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
14977 	      else
14978 		printf (_("<corrupt: %14ld>"), psym->st_name);
14979 	    }
14980 	  putchar ('\n');
14981 	}
14982 
14983       free (iconf);
14984     }
14985 
14986   if (pltgot != 0 && local_gotno != 0)
14987     {
14988       bfd_vma ent, local_end, global_end;
14989       size_t i, offset;
14990       unsigned char * data;
14991       unsigned char * data_end;
14992       int addr_size;
14993 
14994       ent = pltgot;
14995       addr_size = (is_32bit_elf ? 4 : 8);
14996       local_end = pltgot + local_gotno * addr_size;
14997 
14998       /* PR binutils/17533 file: 012-111227-0.004  */
14999       if (symtabno < gotsym)
15000 	{
15001 	  error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
15002 		 (unsigned long) gotsym, (unsigned long) symtabno);
15003 	  return 0;
15004 	}
15005 
15006       global_end = local_end + (symtabno - gotsym) * addr_size;
15007       /* PR 17531: file: 54c91a34.  */
15008       if (global_end < local_end)
15009 	{
15010 	  error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
15011 	  return 0;
15012 	}
15013 
15014       offset = offset_from_vma (file, pltgot, global_end - pltgot);
15015       data = (unsigned char *) get_data (NULL, file, offset,
15016                                          global_end - pltgot, 1,
15017 					 _("Global Offset Table data"));
15018       if (data == NULL)
15019 	return 0;
15020       data_end = data + (global_end - pltgot);
15021 
15022       printf (_("\nPrimary GOT:\n"));
15023       printf (_(" Canonical gp value: "));
15024       print_vma (pltgot + 0x7ff0, LONG_HEX);
15025       printf ("\n\n");
15026 
15027       printf (_(" Reserved entries:\n"));
15028       printf (_("  %*s %10s %*s Purpose\n"),
15029 	      addr_size * 2, _("Address"), _("Access"),
15030 	      addr_size * 2, _("Initial"));
15031       ent = print_mips_got_entry (data, pltgot, ent, data_end);
15032       printf (_(" Lazy resolver\n"));
15033       if (ent == (bfd_vma) -1)
15034 	goto got_print_fail;
15035       if (data
15036 	  && (byte_get (data + ent - pltgot, addr_size)
15037 	      >> (addr_size * 8 - 1)) != 0)
15038 	{
15039 	  ent = print_mips_got_entry (data, pltgot, ent, data_end);
15040 	  printf (_(" Module pointer (GNU extension)\n"));
15041 	  if (ent == (bfd_vma) -1)
15042 	    goto got_print_fail;
15043 	}
15044       printf ("\n");
15045 
15046       if (ent < local_end)
15047 	{
15048 	  printf (_(" Local entries:\n"));
15049 	  printf ("  %*s %10s %*s\n",
15050 		  addr_size * 2, _("Address"), _("Access"),
15051 		  addr_size * 2, _("Initial"));
15052 	  while (ent < local_end)
15053 	    {
15054 	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
15055 	      printf ("\n");
15056 	      if (ent == (bfd_vma) -1)
15057 		goto got_print_fail;
15058 	    }
15059 	  printf ("\n");
15060 	}
15061 
15062       if (gotsym < symtabno)
15063 	{
15064 	  int sym_width;
15065 
15066 	  printf (_(" Global entries:\n"));
15067 	  printf ("  %*s %10s %*s %*s %-7s %3s %s\n",
15068 		  addr_size * 2, _("Address"),
15069 		  _("Access"),
15070 		  addr_size * 2, _("Initial"),
15071 		  addr_size * 2, _("Sym.Val."),
15072 		  _("Type"),
15073 		  /* Note for translators: "Ndx" = abbreviated form of "Index".  */
15074 		  _("Ndx"), _("Name"));
15075 
15076 	  sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
15077 
15078 	  for (i = gotsym; i < symtabno; i++)
15079 	    {
15080 	      ent = print_mips_got_entry (data, pltgot, ent, data_end);
15081 	      printf (" ");
15082 
15083 	      if (dynamic_symbols == NULL)
15084 		printf (_("<no dynamic symbols>"));
15085 	      else if (i < num_dynamic_syms)
15086 		{
15087 		  Elf_Internal_Sym * psym = dynamic_symbols + i;
15088 
15089 		  print_vma (psym->st_value, LONG_HEX);
15090 		  printf (" %-7s %3s ",
15091 			  get_symbol_type (ELF_ST_TYPE (psym->st_info)),
15092 			  get_symbol_index_type (psym->st_shndx));
15093 
15094 		  if (VALID_DYNAMIC_NAME (psym->st_name))
15095 		    print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
15096 		  else
15097 		    printf (_("<corrupt: %14ld>"), psym->st_name);
15098 		}
15099 	      else
15100 		printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
15101 			(unsigned long) i);
15102 
15103 	      printf ("\n");
15104 	      if (ent == (bfd_vma) -1)
15105 		break;
15106 	    }
15107 	  printf ("\n");
15108 	}
15109 
15110     got_print_fail:
15111       if (data)
15112 	free (data);
15113     }
15114 
15115   if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
15116     {
15117       bfd_vma ent, end;
15118       size_t offset, rel_offset;
15119       unsigned long count, i;
15120       unsigned char * data;
15121       int addr_size, sym_width;
15122       Elf_Internal_Rela * rels;
15123 
15124       rel_offset = offset_from_vma (file, jmprel, pltrelsz);
15125       if (pltrel == DT_RELA)
15126 	{
15127 	  if (!slurp_rela_relocs (file, rel_offset, pltrelsz, &rels, &count))
15128 	    return 0;
15129 	}
15130       else
15131 	{
15132 	  if (!slurp_rel_relocs (file, rel_offset, pltrelsz, &rels, &count))
15133 	    return 0;
15134 	}
15135 
15136       ent = mips_pltgot;
15137       addr_size = (is_32bit_elf ? 4 : 8);
15138       end = mips_pltgot + (2 + count) * addr_size;
15139 
15140       offset = offset_from_vma (file, mips_pltgot, end - mips_pltgot);
15141       data = (unsigned char *) get_data (NULL, file, offset, end - mips_pltgot,
15142                                          1, _("Procedure Linkage Table data"));
15143       if (data == NULL)
15144 	return 0;
15145 
15146       printf ("\nPLT GOT:\n\n");
15147       printf (_(" Reserved entries:\n"));
15148       printf (_("  %*s %*s Purpose\n"),
15149 	      addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
15150       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
15151       printf (_(" PLT lazy resolver\n"));
15152       ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
15153       printf (_(" Module pointer\n"));
15154       printf ("\n");
15155 
15156       printf (_(" Entries:\n"));
15157       printf ("  %*s %*s %*s %-7s %3s %s\n",
15158 	      addr_size * 2, _("Address"),
15159 	      addr_size * 2, _("Initial"),
15160 	      addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
15161       sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
15162       for (i = 0; i < count; i++)
15163 	{
15164 	  unsigned long idx = get_reloc_symindex (rels[i].r_info);
15165 
15166 	  ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
15167 	  printf (" ");
15168 
15169 	  if (idx >= num_dynamic_syms)
15170 	    printf (_("<corrupt symbol index: %lu>"), idx);
15171 	  else
15172 	    {
15173 	      Elf_Internal_Sym * psym = dynamic_symbols + idx;
15174 
15175 	      print_vma (psym->st_value, LONG_HEX);
15176 	      printf (" %-7s %3s ",
15177 		      get_symbol_type (ELF_ST_TYPE (psym->st_info)),
15178 		      get_symbol_index_type (psym->st_shndx));
15179 	      if (VALID_DYNAMIC_NAME (psym->st_name))
15180 		print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
15181 	      else
15182 		printf (_("<corrupt: %14ld>"), psym->st_name);
15183 	    }
15184 	  printf ("\n");
15185 	}
15186       printf ("\n");
15187 
15188       if (data)
15189 	free (data);
15190       free (rels);
15191     }
15192 
15193   return 1;
15194 }
15195 
15196 static int
process_nds32_specific(FILE * file)15197 process_nds32_specific (FILE * file)
15198 {
15199   Elf_Internal_Shdr *sect = NULL;
15200 
15201   sect = find_section (".nds32_e_flags");
15202   if (sect != NULL)
15203     {
15204       unsigned int *flag;
15205 
15206       printf ("\nNDS32 elf flags section:\n");
15207       flag = get_data (NULL, file, sect->sh_offset, 1,
15208 		       sect->sh_size, _("NDS32 elf flags section"));
15209 
15210       switch ((*flag) & 0x3)
15211 	{
15212 	case 0:
15213 	  printf ("(VEC_SIZE):\tNo entry.\n");
15214 	  break;
15215 	case 1:
15216 	  printf ("(VEC_SIZE):\t4 bytes\n");
15217 	  break;
15218 	case 2:
15219 	  printf ("(VEC_SIZE):\t16 bytes\n");
15220 	  break;
15221 	case 3:
15222 	  printf ("(VEC_SIZE):\treserved\n");
15223 	  break;
15224 	}
15225     }
15226 
15227   return TRUE;
15228 }
15229 
15230 static int
process_gnu_liblist(FILE * file)15231 process_gnu_liblist (FILE * file)
15232 {
15233   Elf_Internal_Shdr * section;
15234   Elf_Internal_Shdr * string_sec;
15235   Elf32_External_Lib * elib;
15236   char * strtab;
15237   size_t strtab_size;
15238   size_t cnt;
15239   unsigned i;
15240 
15241   if (! do_arch)
15242     return 0;
15243 
15244   for (i = 0, section = section_headers;
15245        i < elf_header.e_shnum;
15246        i++, section++)
15247     {
15248       switch (section->sh_type)
15249 	{
15250 	case SHT_GNU_LIBLIST:
15251 	  if (section->sh_link >= elf_header.e_shnum)
15252 	    break;
15253 
15254 	  elib = (Elf32_External_Lib *)
15255               get_data (NULL, file, section->sh_offset, 1, section->sh_size,
15256                         _("liblist section data"));
15257 
15258 	  if (elib == NULL)
15259 	    break;
15260 	  string_sec = section_headers + section->sh_link;
15261 
15262 	  strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
15263                                       string_sec->sh_size,
15264                                       _("liblist string table"));
15265 	  if (strtab == NULL
15266 	      || section->sh_entsize != sizeof (Elf32_External_Lib))
15267 	    {
15268 	      free (elib);
15269 	      free (strtab);
15270 	      break;
15271 	    }
15272 	  strtab_size = string_sec->sh_size;
15273 
15274 	  printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
15275 		  printable_section_name (section),
15276 		  (unsigned long) (section->sh_size / sizeof (Elf32_External_Lib)));
15277 
15278 	  puts (_("     Library              Time Stamp          Checksum   Version Flags"));
15279 
15280 	  for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
15281 	       ++cnt)
15282 	    {
15283 	      Elf32_Lib liblist;
15284 	      time_t atime;
15285 	      char timebuf[128];
15286 	      struct tm * tmp;
15287 
15288 	      liblist.l_name = BYTE_GET (elib[cnt].l_name);
15289 	      atime = BYTE_GET (elib[cnt].l_time_stamp);
15290 	      liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
15291 	      liblist.l_version = BYTE_GET (elib[cnt].l_version);
15292 	      liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
15293 
15294 	      tmp = gmtime (&atime);
15295 	      snprintf (timebuf, sizeof (timebuf),
15296 			"%04u-%02u-%02uT%02u:%02u:%02u",
15297 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
15298 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
15299 
15300 	      printf ("%3lu: ", (unsigned long) cnt);
15301 	      if (do_wide)
15302 		printf ("%-20s", liblist.l_name < strtab_size
15303 			? strtab + liblist.l_name : _("<corrupt>"));
15304 	      else
15305 		printf ("%-20.20s", liblist.l_name < strtab_size
15306 			? strtab + liblist.l_name : _("<corrupt>"));
15307 	      printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
15308 		      liblist.l_version, liblist.l_flags);
15309 	    }
15310 
15311 	  free (elib);
15312 	  free (strtab);
15313 	}
15314     }
15315 
15316   return 1;
15317 }
15318 
15319 static const char *
get_note_type(unsigned e_type)15320 get_note_type (unsigned e_type)
15321 {
15322   static char buff[64];
15323 
15324   if (elf_header.e_type == ET_CORE)
15325     switch (e_type)
15326       {
15327       case NT_AUXV:
15328 	return _("NT_AUXV (auxiliary vector)");
15329       case NT_PRSTATUS:
15330 	return _("NT_PRSTATUS (prstatus structure)");
15331       case NT_FPREGSET:
15332 	return _("NT_FPREGSET (floating point registers)");
15333       case NT_PRPSINFO:
15334 	return _("NT_PRPSINFO (prpsinfo structure)");
15335       case NT_TASKSTRUCT:
15336 	return _("NT_TASKSTRUCT (task structure)");
15337       case NT_PRXFPREG:
15338 	return _("NT_PRXFPREG (user_xfpregs structure)");
15339       case NT_PPC_VMX:
15340 	return _("NT_PPC_VMX (ppc Altivec registers)");
15341       case NT_PPC_VSX:
15342 	return _("NT_PPC_VSX (ppc VSX registers)");
15343       case NT_386_TLS:
15344 	return _("NT_386_TLS (x86 TLS information)");
15345       case NT_386_IOPERM:
15346 	return _("NT_386_IOPERM (x86 I/O permissions)");
15347       case NT_X86_XSTATE:
15348 	return _("NT_X86_XSTATE (x86 XSAVE extended state)");
15349       case NT_S390_HIGH_GPRS:
15350 	return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
15351       case NT_S390_TIMER:
15352 	return _("NT_S390_TIMER (s390 timer register)");
15353       case NT_S390_TODCMP:
15354 	return _("NT_S390_TODCMP (s390 TOD comparator register)");
15355       case NT_S390_TODPREG:
15356 	return _("NT_S390_TODPREG (s390 TOD programmable register)");
15357       case NT_S390_CTRS:
15358 	return _("NT_S390_CTRS (s390 control registers)");
15359       case NT_S390_PREFIX:
15360 	return _("NT_S390_PREFIX (s390 prefix register)");
15361       case NT_S390_LAST_BREAK:
15362 	return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
15363       case NT_S390_SYSTEM_CALL:
15364 	return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
15365       case NT_S390_TDB:
15366 	return _("NT_S390_TDB (s390 transaction diagnostic block)");
15367       case NT_S390_VXRS_LOW:
15368 	return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
15369       case NT_S390_VXRS_HIGH:
15370 	return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
15371       case NT_ARM_VFP:
15372 	return _("NT_ARM_VFP (arm VFP registers)");
15373       case NT_ARM_TLS:
15374 	return _("NT_ARM_TLS (AArch TLS registers)");
15375       case NT_ARM_HW_BREAK:
15376 	return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
15377       case NT_ARM_HW_WATCH:
15378 	return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
15379       case NT_PSTATUS:
15380 	return _("NT_PSTATUS (pstatus structure)");
15381       case NT_FPREGS:
15382 	return _("NT_FPREGS (floating point registers)");
15383       case NT_PSINFO:
15384 	return _("NT_PSINFO (psinfo structure)");
15385       case NT_LWPSTATUS:
15386 	return _("NT_LWPSTATUS (lwpstatus_t structure)");
15387       case NT_LWPSINFO:
15388 	return _("NT_LWPSINFO (lwpsinfo_t structure)");
15389       case NT_WIN32PSTATUS:
15390 	return _("NT_WIN32PSTATUS (win32_pstatus structure)");
15391       case NT_SIGINFO:
15392 	return _("NT_SIGINFO (siginfo_t data)");
15393       case NT_FILE:
15394 	return _("NT_FILE (mapped files)");
15395       default:
15396 	break;
15397       }
15398   else
15399     switch (e_type)
15400       {
15401       case NT_VERSION:
15402 	return _("NT_VERSION (version)");
15403       case NT_ARCH:
15404 	return _("NT_ARCH (architecture)");
15405       default:
15406 	break;
15407       }
15408 
15409   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15410   return buff;
15411 }
15412 
15413 static int
print_core_note(Elf_Internal_Note * pnote)15414 print_core_note (Elf_Internal_Note *pnote)
15415 {
15416   unsigned int addr_size = is_32bit_elf ? 4 : 8;
15417   bfd_vma count, page_size;
15418   unsigned char *descdata, *filenames, *descend;
15419 
15420   if (pnote->type != NT_FILE)
15421     return 1;
15422 
15423 #ifndef BFD64
15424   if (!is_32bit_elf)
15425     {
15426       printf (_("    Cannot decode 64-bit note in 32-bit build\n"));
15427       /* Still "successful".  */
15428       return 1;
15429     }
15430 #endif
15431 
15432   if (pnote->descsz < 2 * addr_size)
15433     {
15434       printf (_("    Malformed note - too short for header\n"));
15435       return 0;
15436     }
15437 
15438   descdata = (unsigned char *) pnote->descdata;
15439   descend = descdata + pnote->descsz;
15440 
15441   if (descdata[pnote->descsz - 1] != '\0')
15442     {
15443       printf (_("    Malformed note - does not end with \\0\n"));
15444       return 0;
15445     }
15446 
15447   count = byte_get (descdata, addr_size);
15448   descdata += addr_size;
15449 
15450   page_size = byte_get (descdata, addr_size);
15451   descdata += addr_size;
15452 
15453   if (pnote->descsz < 2 * addr_size + count * 3 * addr_size)
15454     {
15455       printf (_("    Malformed note - too short for supplied file count\n"));
15456       return 0;
15457     }
15458 
15459   printf (_("    Page size: "));
15460   print_vma (page_size, DEC);
15461   printf ("\n");
15462 
15463   printf (_("    %*s%*s%*s\n"),
15464 	  (int) (2 + 2 * addr_size), _("Start"),
15465 	  (int) (4 + 2 * addr_size), _("End"),
15466 	  (int) (4 + 2 * addr_size), _("Page Offset"));
15467   filenames = descdata + count * 3 * addr_size;
15468   while (count-- > 0)
15469     {
15470       bfd_vma start, end, file_ofs;
15471 
15472       if (filenames == descend)
15473 	{
15474 	  printf (_("    Malformed note - filenames end too early\n"));
15475 	  return 0;
15476 	}
15477 
15478       start = byte_get (descdata, addr_size);
15479       descdata += addr_size;
15480       end = byte_get (descdata, addr_size);
15481       descdata += addr_size;
15482       file_ofs = byte_get (descdata, addr_size);
15483       descdata += addr_size;
15484 
15485       printf ("    ");
15486       print_vma (start, FULL_HEX);
15487       printf ("  ");
15488       print_vma (end, FULL_HEX);
15489       printf ("  ");
15490       print_vma (file_ofs, FULL_HEX);
15491       printf ("\n        %s\n", filenames);
15492 
15493       filenames += 1 + strlen ((char *) filenames);
15494     }
15495 
15496   return 1;
15497 }
15498 
15499 static const char *
get_gnu_elf_note_type(unsigned e_type)15500 get_gnu_elf_note_type (unsigned e_type)
15501 {
15502   static char buff[64];
15503 
15504   switch (e_type)
15505     {
15506     case NT_GNU_ABI_TAG:
15507       return _("NT_GNU_ABI_TAG (ABI version tag)");
15508     case NT_GNU_HWCAP:
15509       return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
15510     case NT_GNU_BUILD_ID:
15511       return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
15512     case NT_GNU_GOLD_VERSION:
15513       return _("NT_GNU_GOLD_VERSION (gold version)");
15514     default:
15515       break;
15516     }
15517 
15518   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15519   return buff;
15520 }
15521 
15522 static int
print_gnu_note(Elf_Internal_Note * pnote)15523 print_gnu_note (Elf_Internal_Note *pnote)
15524 {
15525   switch (pnote->type)
15526     {
15527     case NT_GNU_BUILD_ID:
15528       {
15529 	unsigned long i;
15530 
15531 	printf (_("    Build ID: "));
15532 	for (i = 0; i < pnote->descsz; ++i)
15533 	  printf ("%02x", pnote->descdata[i] & 0xff);
15534 	printf ("\n");
15535       }
15536       break;
15537 
15538     case NT_GNU_ABI_TAG:
15539       {
15540 	unsigned long os, major, minor, subminor;
15541 	const char *osname;
15542 
15543 	/* PR 17531: file: 030-599401-0.004.  */
15544 	if (pnote->descsz < 16)
15545 	  {
15546 	    printf (_("    <corrupt GNU_ABI_TAG>\n"));
15547 	    break;
15548 	  }
15549 
15550 	os = byte_get ((unsigned char *) pnote->descdata, 4);
15551 	major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
15552 	minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
15553 	subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
15554 
15555 	switch (os)
15556 	  {
15557 	  case GNU_ABI_TAG_LINUX:
15558 	    osname = "Linux";
15559 	    break;
15560 	  case GNU_ABI_TAG_HURD:
15561 	    osname = "Hurd";
15562 	    break;
15563 	  case GNU_ABI_TAG_SOLARIS:
15564 	    osname = "Solaris";
15565 	    break;
15566 	  case GNU_ABI_TAG_FREEBSD:
15567 	    osname = "FreeBSD";
15568 	    break;
15569 	  case GNU_ABI_TAG_NETBSD:
15570 	    osname = "NetBSD";
15571 	    break;
15572 	  case GNU_ABI_TAG_SYLLABLE:
15573 	    osname = "Syllable";
15574 	    break;
15575 	  case GNU_ABI_TAG_NACL:
15576 	    osname = "NaCl";
15577 	    break;
15578 	  default:
15579 	    osname = "Unknown";
15580 	    break;
15581 	  }
15582 
15583 	printf (_("    OS: %s, ABI: %ld.%ld.%ld\n"), osname,
15584 		major, minor, subminor);
15585       }
15586       break;
15587 
15588     case NT_GNU_GOLD_VERSION:
15589       {
15590 	unsigned long i;
15591 
15592 	printf (_("    Version: "));
15593 	for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
15594 	  printf ("%c", pnote->descdata[i]);
15595 	printf ("\n");
15596       }
15597       break;
15598     }
15599 
15600   return 1;
15601 }
15602 
15603 static const char *
get_v850_elf_note_type(enum v850_notes n_type)15604 get_v850_elf_note_type (enum v850_notes n_type)
15605 {
15606   static char buff[64];
15607 
15608   switch (n_type)
15609     {
15610     case V850_NOTE_ALIGNMENT:  return _("Alignment of 8-byte objects");
15611     case V850_NOTE_DATA_SIZE:  return _("Sizeof double and long double");
15612     case V850_NOTE_FPU_INFO:   return _("Type of FPU support needed");
15613     case V850_NOTE_SIMD_INFO:  return _("Use of SIMD instructions");
15614     case V850_NOTE_CACHE_INFO: return _("Use of cache");
15615     case V850_NOTE_MMU_INFO:   return _("Use of MMU");
15616     default:
15617       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
15618       return buff;
15619     }
15620 }
15621 
15622 static int
print_v850_note(Elf_Internal_Note * pnote)15623 print_v850_note (Elf_Internal_Note * pnote)
15624 {
15625   unsigned int val;
15626 
15627   if (pnote->descsz != 4)
15628     return 0;
15629   val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
15630 
15631   if (val == 0)
15632     {
15633       printf (_("not set\n"));
15634       return 1;
15635     }
15636 
15637   switch (pnote->type)
15638     {
15639     case V850_NOTE_ALIGNMENT:
15640       switch (val)
15641 	{
15642 	case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return 1;
15643 	case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return 1;
15644 	}
15645       break;
15646 
15647     case V850_NOTE_DATA_SIZE:
15648       switch (val)
15649 	{
15650 	case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return 1;
15651 	case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return 1;
15652 	}
15653       break;
15654 
15655     case V850_NOTE_FPU_INFO:
15656       switch (val)
15657 	{
15658 	case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return 1;
15659 	case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return 1;
15660 	}
15661       break;
15662 
15663     case V850_NOTE_MMU_INFO:
15664     case V850_NOTE_CACHE_INFO:
15665     case V850_NOTE_SIMD_INFO:
15666       if (val == EF_RH850_SIMD)
15667 	{
15668 	  printf (_("yes\n"));
15669 	  return 1;
15670 	}
15671       break;
15672 
15673     default:
15674       /* An 'unknown note type' message will already have been displayed.  */
15675       break;
15676     }
15677 
15678   printf (_("unknown value: %x\n"), val);
15679   return 0;
15680 }
15681 
15682 static int
process_netbsd_elf_note(Elf_Internal_Note * pnote)15683 process_netbsd_elf_note (Elf_Internal_Note * pnote)
15684 {
15685   unsigned int version;
15686 
15687   switch (pnote->type)
15688     {
15689     case NT_NETBSD_IDENT:
15690       version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
15691       if ((version / 10000) % 100)
15692         printf ("  NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
15693 		version, version / 100000000, (version / 1000000) % 100,
15694 		(version / 10000) % 100 > 26 ? "Z" : "",
15695 		'A' + (version / 10000) % 26);
15696       else
15697 	printf ("  NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
15698 	        version, version / 100000000, (version / 1000000) % 100,
15699 		(version / 100) % 100);
15700       return 1;
15701 
15702     case NT_NETBSD_MARCH:
15703       printf ("  NetBSD\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
15704 	      pnote->descdata);
15705       return 1;
15706 
15707     default:
15708       break;
15709     }
15710 
15711   printf ("  NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n", pnote->descsz,
15712 	  pnote->type);
15713   return 1;
15714 }
15715 
15716 static const char *
get_freebsd_elfcore_note_type(unsigned e_type)15717 get_freebsd_elfcore_note_type (unsigned e_type)
15718 {
15719   switch (e_type)
15720     {
15721     case NT_FREEBSD_THRMISC:
15722       return _("NT_THRMISC (thrmisc structure)");
15723     case NT_FREEBSD_PROCSTAT_PROC:
15724       return _("NT_PROCSTAT_PROC (proc data)");
15725     case NT_FREEBSD_PROCSTAT_FILES:
15726       return _("NT_PROCSTAT_FILES (files data)");
15727     case NT_FREEBSD_PROCSTAT_VMMAP:
15728       return _("NT_PROCSTAT_VMMAP (vmmap data)");
15729     case NT_FREEBSD_PROCSTAT_GROUPS:
15730       return _("NT_PROCSTAT_GROUPS (groups data)");
15731     case NT_FREEBSD_PROCSTAT_UMASK:
15732       return _("NT_PROCSTAT_UMASK (umask data)");
15733     case NT_FREEBSD_PROCSTAT_RLIMIT:
15734       return _("NT_PROCSTAT_RLIMIT (rlimit data)");
15735     case NT_FREEBSD_PROCSTAT_OSREL:
15736       return _("NT_PROCSTAT_OSREL (osreldate data)");
15737     case NT_FREEBSD_PROCSTAT_PSSTRINGS:
15738       return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
15739     case NT_FREEBSD_PROCSTAT_AUXV:
15740       return _("NT_PROCSTAT_AUXV (auxv data)");
15741     }
15742   return get_note_type (e_type);
15743 }
15744 
15745 static const char *
get_netbsd_elfcore_note_type(unsigned e_type)15746 get_netbsd_elfcore_note_type (unsigned e_type)
15747 {
15748   static char buff[64];
15749 
15750   if (e_type == NT_NETBSDCORE_PROCINFO)
15751     {
15752       /* NetBSD core "procinfo" structure.  */
15753       return _("NetBSD procinfo structure");
15754     }
15755 
15756   /* As of Jan 2002 there are no other machine-independent notes
15757      defined for NetBSD core files.  If the note type is less
15758      than the start of the machine-dependent note types, we don't
15759      understand it.  */
15760 
15761   if (e_type < NT_NETBSDCORE_FIRSTMACH)
15762     {
15763       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15764       return buff;
15765     }
15766 
15767   switch (elf_header.e_machine)
15768     {
15769     /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
15770        and PT_GETFPREGS == mach+2.  */
15771 
15772     case EM_OLD_ALPHA:
15773     case EM_ALPHA:
15774     case EM_SPARC:
15775     case EM_SPARC32PLUS:
15776     case EM_SPARCV9:
15777       switch (e_type)
15778 	{
15779 	case NT_NETBSDCORE_FIRSTMACH + 0:
15780 	  return _("PT_GETREGS (reg structure)");
15781 	case NT_NETBSDCORE_FIRSTMACH + 2:
15782 	  return _("PT_GETFPREGS (fpreg structure)");
15783 	default:
15784 	  break;
15785 	}
15786       break;
15787 
15788     /* On all other arch's, PT_GETREGS == mach+1 and
15789        PT_GETFPREGS == mach+3.  */
15790     default:
15791       switch (e_type)
15792 	{
15793 	case NT_NETBSDCORE_FIRSTMACH + 1:
15794 	  return _("PT_GETREGS (reg structure)");
15795 	case NT_NETBSDCORE_FIRSTMACH + 3:
15796 	  return _("PT_GETFPREGS (fpreg structure)");
15797 	default:
15798 	  break;
15799 	}
15800     }
15801 
15802   snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
15803 	    e_type - NT_NETBSDCORE_FIRSTMACH);
15804   return buff;
15805 }
15806 
15807 static const char *
get_stapsdt_note_type(unsigned e_type)15808 get_stapsdt_note_type (unsigned e_type)
15809 {
15810   static char buff[64];
15811 
15812   switch (e_type)
15813     {
15814     case NT_STAPSDT:
15815       return _("NT_STAPSDT (SystemTap probe descriptors)");
15816 
15817     default:
15818       break;
15819     }
15820 
15821   snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15822   return buff;
15823 }
15824 
15825 static int
print_stapsdt_note(Elf_Internal_Note * pnote)15826 print_stapsdt_note (Elf_Internal_Note *pnote)
15827 {
15828   int addr_size = is_32bit_elf ? 4 : 8;
15829   char *data = pnote->descdata;
15830   char *data_end = pnote->descdata + pnote->descsz;
15831   bfd_vma pc, base_addr, semaphore;
15832   char *provider, *probe, *arg_fmt;
15833 
15834   pc = byte_get ((unsigned char *) data, addr_size);
15835   data += addr_size;
15836   base_addr = byte_get ((unsigned char *) data, addr_size);
15837   data += addr_size;
15838   semaphore = byte_get ((unsigned char *) data, addr_size);
15839   data += addr_size;
15840 
15841   provider = data;
15842   data += strlen (data) + 1;
15843   probe = data;
15844   data += strlen (data) + 1;
15845   arg_fmt = data;
15846   data += strlen (data) + 1;
15847 
15848   printf (_("    Provider: %s\n"), provider);
15849   printf (_("    Name: %s\n"), probe);
15850   printf (_("    Location: "));
15851   print_vma (pc, FULL_HEX);
15852   printf (_(", Base: "));
15853   print_vma (base_addr, FULL_HEX);
15854   printf (_(", Semaphore: "));
15855   print_vma (semaphore, FULL_HEX);
15856   printf ("\n");
15857   printf (_("    Arguments: %s\n"), arg_fmt);
15858 
15859   return data == data_end;
15860 }
15861 
15862 static const char *
get_ia64_vms_note_type(unsigned e_type)15863 get_ia64_vms_note_type (unsigned e_type)
15864 {
15865   static char buff[64];
15866 
15867   switch (e_type)
15868     {
15869     case NT_VMS_MHD:
15870       return _("NT_VMS_MHD (module header)");
15871     case NT_VMS_LNM:
15872       return _("NT_VMS_LNM (language name)");
15873     case NT_VMS_SRC:
15874       return _("NT_VMS_SRC (source files)");
15875     case NT_VMS_TITLE:
15876       return "NT_VMS_TITLE";
15877     case NT_VMS_EIDC:
15878       return _("NT_VMS_EIDC (consistency check)");
15879     case NT_VMS_FPMODE:
15880       return _("NT_VMS_FPMODE (FP mode)");
15881     case NT_VMS_LINKTIME:
15882       return "NT_VMS_LINKTIME";
15883     case NT_VMS_IMGNAM:
15884       return _("NT_VMS_IMGNAM (image name)");
15885     case NT_VMS_IMGID:
15886       return _("NT_VMS_IMGID (image id)");
15887     case NT_VMS_LINKID:
15888       return _("NT_VMS_LINKID (link id)");
15889     case NT_VMS_IMGBID:
15890       return _("NT_VMS_IMGBID (build id)");
15891     case NT_VMS_GSTNAM:
15892       return _("NT_VMS_GSTNAM (sym table name)");
15893     case NT_VMS_ORIG_DYN:
15894       return "NT_VMS_ORIG_DYN";
15895     case NT_VMS_PATCHTIME:
15896       return "NT_VMS_PATCHTIME";
15897     default:
15898       snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
15899       return buff;
15900     }
15901 }
15902 
15903 static int
print_ia64_vms_note(Elf_Internal_Note * pnote)15904 print_ia64_vms_note (Elf_Internal_Note * pnote)
15905 {
15906   switch (pnote->type)
15907     {
15908     case NT_VMS_MHD:
15909       if (pnote->descsz > 36)
15910         {
15911           size_t l = strlen (pnote->descdata + 34);
15912           printf (_("    Creation date  : %.17s\n"), pnote->descdata);
15913           printf (_("    Last patch date: %.17s\n"), pnote->descdata + 17);
15914           printf (_("    Module name    : %s\n"), pnote->descdata + 34);
15915           printf (_("    Module version : %s\n"), pnote->descdata + 34 + l + 1);
15916         }
15917       else
15918         printf (_("    Invalid size\n"));
15919       break;
15920     case NT_VMS_LNM:
15921       printf (_("   Language: %s\n"), pnote->descdata);
15922       break;
15923 #ifdef BFD64
15924     case NT_VMS_FPMODE:
15925       printf (_("   Floating Point mode: "));
15926       printf ("0x%016" BFD_VMA_FMT "x\n",
15927               (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
15928       break;
15929     case NT_VMS_LINKTIME:
15930       printf (_("   Link time: "));
15931       print_vms_time
15932         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
15933       printf ("\n");
15934       break;
15935     case NT_VMS_PATCHTIME:
15936       printf (_("   Patch time: "));
15937       print_vms_time
15938         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
15939       printf ("\n");
15940       break;
15941     case NT_VMS_ORIG_DYN:
15942       printf (_("   Major id: %u,  minor id: %u\n"),
15943               (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
15944               (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
15945       printf (_("   Last modified  : "));
15946       print_vms_time
15947         ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
15948       printf (_("\n   Link flags  : "));
15949       printf ("0x%016" BFD_VMA_FMT "x\n",
15950               (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
15951       printf (_("   Header flags: 0x%08x\n"),
15952               (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
15953       printf (_("   Image id    : %s\n"), pnote->descdata + 32);
15954       break;
15955 #endif
15956     case NT_VMS_IMGNAM:
15957       printf (_("    Image name: %s\n"), pnote->descdata);
15958       break;
15959     case NT_VMS_GSTNAM:
15960       printf (_("    Global symbol table name: %s\n"), pnote->descdata);
15961       break;
15962     case NT_VMS_IMGID:
15963       printf (_("    Image id: %s\n"), pnote->descdata);
15964       break;
15965     case NT_VMS_LINKID:
15966       printf (_("    Linker id: %s\n"), pnote->descdata);
15967       break;
15968     default:
15969       break;
15970     }
15971   return 1;
15972 }
15973 
15974 /* Note that by the ELF standard, the name field is already null byte
15975    terminated, and namesz includes the terminating null byte.
15976    I.E. the value of namesz for the name "FSF" is 4.
15977 
15978    If the value of namesz is zero, there is no name present.  */
15979 static int
process_note(Elf_Internal_Note * pnote)15980 process_note (Elf_Internal_Note * pnote)
15981 {
15982   const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
15983   const char * nt;
15984 
15985   if (pnote->namesz == 0)
15986     /* If there is no note name, then use the default set of
15987        note type strings.  */
15988     nt = get_note_type (pnote->type);
15989 
15990   else if (const_strneq (pnote->namedata, "GNU"))
15991     /* GNU-specific object file notes.  */
15992     nt = get_gnu_elf_note_type (pnote->type);
15993 
15994   else if (const_strneq (pnote->namedata, "FreeBSD"))
15995     /* FreeBSD-specific core file notes.  */
15996     nt = get_freebsd_elfcore_note_type (pnote->type);
15997 
15998   else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
15999     /* NetBSD-specific core file notes.  */
16000     nt = get_netbsd_elfcore_note_type (pnote->type);
16001 
16002   else if (const_strneq (pnote->namedata, "NetBSD"))
16003     /* NetBSD-specific core file notes.  */
16004     return process_netbsd_elf_note (pnote);
16005 
16006   else if (strneq (pnote->namedata, "SPU/", 4))
16007     {
16008       /* SPU-specific core file notes.  */
16009       nt = pnote->namedata + 4;
16010       name = "SPU";
16011     }
16012 
16013   else if (const_strneq (pnote->namedata, "IPF/VMS"))
16014     /* VMS/ia64-specific file notes.  */
16015     nt = get_ia64_vms_note_type (pnote->type);
16016 
16017   else if (const_strneq (pnote->namedata, "stapsdt"))
16018     nt = get_stapsdt_note_type (pnote->type);
16019 
16020   else
16021     /* Don't recognize this note name; just use the default set of
16022        note type strings.  */
16023     nt = get_note_type (pnote->type);
16024 
16025   printf ("  %-20s 0x%08lx\t%s\n", name, pnote->descsz, nt);
16026 
16027   if (const_strneq (pnote->namedata, "IPF/VMS"))
16028     return print_ia64_vms_note (pnote);
16029   else if (const_strneq (pnote->namedata, "GNU"))
16030     return print_gnu_note (pnote);
16031   else if (const_strneq (pnote->namedata, "stapsdt"))
16032     return print_stapsdt_note (pnote);
16033   else if (const_strneq (pnote->namedata, "CORE"))
16034     return print_core_note (pnote);
16035   else
16036     return 1;
16037 }
16038 
16039 
16040 static int
process_corefile_note_segment(FILE * file,bfd_vma offset,bfd_vma length)16041 process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
16042 {
16043   Elf_External_Note * pnotes;
16044   Elf_External_Note * external;
16045   char * end;
16046   int res = 1;
16047 
16048   if (length <= 0)
16049     return 0;
16050 
16051   pnotes = (Elf_External_Note *) get_data (NULL, file, offset, 1, length,
16052 					   _("notes"));
16053   if (pnotes == NULL)
16054     return 0;
16055 
16056   external = pnotes;
16057 
16058   printf (_("\nDisplaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
16059 	  (unsigned long) offset, (unsigned long) length);
16060   printf (_("  %-20s %10s\tDescription\n"), _("Owner"), _("Data size"));
16061 
16062   end = (char *) pnotes + length;
16063   while ((char *) external < end)
16064     {
16065       Elf_Internal_Note inote;
16066       size_t min_notesz;
16067       char *next;
16068       char * temp = NULL;
16069       size_t data_remaining = end - (char *) external;
16070 
16071       if (!is_ia64_vms ())
16072 	{
16073 	  /* PR binutils/15191
16074 	     Make sure that there is enough data to read.  */
16075 	  min_notesz = offsetof (Elf_External_Note, name);
16076 	  if (data_remaining < min_notesz)
16077 	    {
16078 	      warn (_("Corrupt note: only %d bytes remain, not enough for a full note\n"),
16079 		    (int) data_remaining);
16080 	      break;
16081 	    }
16082 	  inote.type     = BYTE_GET (external->type);
16083 	  inote.namesz   = BYTE_GET (external->namesz);
16084 	  inote.namedata = external->name;
16085 	  inote.descsz   = BYTE_GET (external->descsz);
16086 	  inote.descdata = inote.namedata + align_power (inote.namesz, 2);
16087 	  /* PR 17531: file: 3443835e.  */
16088 	  if (inote.descdata < (char *) pnotes || inote.descdata > end)
16089 	    {
16090 	      warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
16091 	      inote.descdata = inote.namedata;
16092 	      inote.namesz   = 0;
16093 	    }
16094 
16095 	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
16096 	  next = inote.descdata + align_power (inote.descsz, 2);
16097 	}
16098       else
16099 	{
16100 	  Elf64_External_VMS_Note *vms_external;
16101 
16102 	  /* PR binutils/15191
16103 	     Make sure that there is enough data to read.  */
16104 	  min_notesz = offsetof (Elf64_External_VMS_Note, name);
16105 	  if (data_remaining < min_notesz)
16106 	    {
16107 	      warn (_("Corrupt note: only %d bytes remain, not enough for a full note\n"),
16108 		    (int) data_remaining);
16109 	      break;
16110 	    }
16111 
16112 	  vms_external = (Elf64_External_VMS_Note *) external;
16113 	  inote.type     = BYTE_GET (vms_external->type);
16114 	  inote.namesz   = BYTE_GET (vms_external->namesz);
16115 	  inote.namedata = vms_external->name;
16116 	  inote.descsz   = BYTE_GET (vms_external->descsz);
16117 	  inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16118 	  inote.descpos  = offset + (inote.descdata - (char *) pnotes);
16119 	  next = inote.descdata + align_power (inote.descsz, 3);
16120 	}
16121 
16122       if (inote.descdata < (char *) external + min_notesz
16123 	  || next < (char *) external + min_notesz
16124 	  /* PR binutils/17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4.  */
16125 	  || inote.namedata + inote.namesz < inote.namedata
16126 	  || inote.descdata + inote.descsz < inote.descdata
16127 	  || data_remaining < (size_t)(next - (char *) external))
16128 	{
16129 	  warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
16130 		(unsigned long) ((char *) external - (char *) pnotes));
16131 	  warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx\n"),
16132 		inote.type, inote.namesz, inote.descsz);
16133 	  break;
16134 	}
16135 
16136       external = (Elf_External_Note *) next;
16137 
16138       /* Verify that name is null terminated.  It appears that at least
16139 	 one version of Linux (RedHat 6.0) generates corefiles that don't
16140 	 comply with the ELF spec by failing to include the null byte in
16141 	 namesz.  */
16142       if (inote.namedata[inote.namesz - 1] != '\0')
16143 	{
16144 	  temp = (char *) malloc (inote.namesz + 1);
16145 	  if (temp == NULL)
16146 	    {
16147 	      error (_("Out of memory allocating space for inote name\n"));
16148 	      res = 0;
16149 	      break;
16150 	    }
16151 
16152 	  strncpy (temp, inote.namedata, inote.namesz);
16153 	  temp[inote.namesz] = 0;
16154 
16155 	  /* warn (_("'%s' NOTE name not properly null terminated\n"), temp);  */
16156 	  inote.namedata = temp;
16157 	}
16158 
16159       res &= process_note (& inote);
16160 
16161       if (temp != NULL)
16162 	{
16163 	  free (temp);
16164 	  temp = NULL;
16165 	}
16166     }
16167 
16168   free (pnotes);
16169 
16170   return res;
16171 }
16172 
16173 static int
process_corefile_note_segments(FILE * file)16174 process_corefile_note_segments (FILE * file)
16175 {
16176   Elf_Internal_Phdr * segment;
16177   unsigned int i;
16178   int res = 1;
16179 
16180   if (! get_program_headers (file))
16181       return 0;
16182 
16183   for (i = 0, segment = program_headers;
16184        i < elf_header.e_phnum;
16185        i++, segment++)
16186     {
16187       if (segment->p_type == PT_NOTE)
16188 	res &= process_corefile_note_segment (file,
16189 					      (bfd_vma) segment->p_offset,
16190 					      (bfd_vma) segment->p_filesz);
16191     }
16192 
16193   return res;
16194 }
16195 
16196 static int
process_v850_notes(FILE * file,bfd_vma offset,bfd_vma length)16197 process_v850_notes (FILE * file, bfd_vma offset, bfd_vma length)
16198 {
16199   Elf_External_Note * pnotes;
16200   Elf_External_Note * external;
16201   char * end;
16202   int res = 1;
16203 
16204   if (length <= 0)
16205     return 0;
16206 
16207   pnotes = (Elf_External_Note *) get_data (NULL, file, offset, 1, length,
16208                                            _("v850 notes"));
16209   if (pnotes == NULL)
16210     return 0;
16211 
16212   external = pnotes;
16213   end = (char*) pnotes + length;
16214 
16215   printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
16216 	  (unsigned long) offset, (unsigned long) length);
16217 
16218   while ((char *) external + sizeof (Elf_External_Note) < end)
16219     {
16220       Elf_External_Note * next;
16221       Elf_Internal_Note inote;
16222 
16223       inote.type     = BYTE_GET (external->type);
16224       inote.namesz   = BYTE_GET (external->namesz);
16225       inote.namedata = external->name;
16226       inote.descsz   = BYTE_GET (external->descsz);
16227       inote.descdata = inote.namedata + align_power (inote.namesz, 2);
16228       inote.descpos  = offset + (inote.descdata - (char *) pnotes);
16229 
16230       if (inote.descdata < (char *) pnotes || inote.descdata >= end)
16231 	{
16232 	  warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
16233 	  inote.descdata = inote.namedata;
16234 	  inote.namesz   = 0;
16235 	}
16236 
16237       next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
16238 
16239       if (   ((char *) next > end)
16240 	  || ((char *) next <  (char *) pnotes))
16241 	{
16242 	  warn (_("corrupt descsz found in note at offset 0x%lx\n"),
16243 		(unsigned long) ((char *) external - (char *) pnotes));
16244 	  warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
16245 		inote.type, inote.namesz, inote.descsz);
16246 	  break;
16247 	}
16248 
16249       external = next;
16250 
16251       /* Prevent out-of-bounds indexing.  */
16252       if (   inote.namedata + inote.namesz > end
16253 	  || inote.namedata + inote.namesz < inote.namedata)
16254         {
16255           warn (_("corrupt namesz found in note at offset 0x%lx\n"),
16256                 (unsigned long) ((char *) external - (char *) pnotes));
16257           warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
16258                 inote.type, inote.namesz, inote.descsz);
16259           break;
16260         }
16261 
16262       printf ("  %s: ", get_v850_elf_note_type (inote.type));
16263 
16264       if (! print_v850_note (& inote))
16265 	{
16266 	  res = 0;
16267 	  printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
16268 		  inote.namesz, inote.descsz);
16269 	}
16270     }
16271 
16272   free (pnotes);
16273 
16274   return res;
16275 }
16276 
16277 static int
process_note_sections(FILE * file)16278 process_note_sections (FILE * file)
16279 {
16280   Elf_Internal_Shdr * section;
16281   unsigned long i;
16282   int n = 0;
16283   int res = 1;
16284 
16285   for (i = 0, section = section_headers;
16286        i < elf_header.e_shnum && section != NULL;
16287        i++, section++)
16288     {
16289       if (section->sh_type == SHT_NOTE)
16290 	{
16291 	  res &= process_corefile_note_segment (file,
16292 						(bfd_vma) section->sh_offset,
16293 						(bfd_vma) section->sh_size);
16294 	  n++;
16295 	}
16296 
16297       if ((   elf_header.e_machine == EM_V800
16298 	   || elf_header.e_machine == EM_V850
16299 	   || elf_header.e_machine == EM_CYGNUS_V850)
16300 	  && section->sh_type == SHT_RENESAS_INFO)
16301 	{
16302 	  res &= process_v850_notes (file,
16303 				     (bfd_vma) section->sh_offset,
16304 				     (bfd_vma) section->sh_size);
16305 	  n++;
16306 	}
16307     }
16308 
16309   if (n == 0)
16310     /* Try processing NOTE segments instead.  */
16311     return process_corefile_note_segments (file);
16312 
16313   return res;
16314 }
16315 
16316 static int
process_notes(FILE * file)16317 process_notes (FILE * file)
16318 {
16319   /* If we have not been asked to display the notes then do nothing.  */
16320   if (! do_notes)
16321     return 1;
16322 
16323   if (elf_header.e_type != ET_CORE)
16324     return process_note_sections (file);
16325 
16326   /* No program headers means no NOTE segment.  */
16327   if (elf_header.e_phnum > 0)
16328     return process_corefile_note_segments (file);
16329 
16330   printf (_("No note segments present in the core file.\n"));
16331   return 1;
16332 }
16333 
16334 static int
process_arch_specific(FILE * file)16335 process_arch_specific (FILE * file)
16336 {
16337   if (! do_arch)
16338     return 1;
16339 
16340   switch (elf_header.e_machine)
16341     {
16342     case EM_ARM:
16343       return process_arm_specific (file);
16344     case EM_MIPS:
16345     case EM_MIPS_RS3_LE:
16346       return process_mips_specific (file);
16347       break;
16348     case EM_NDS32:
16349       return process_nds32_specific (file);
16350       break;
16351     case EM_PPC:
16352       return process_power_specific (file);
16353       break;
16354     case EM_S390:
16355     case EM_S390_OLD:
16356       return process_s390_specific (file);
16357       break;
16358     case EM_SPARC:
16359     case EM_SPARC32PLUS:
16360     case EM_SPARCV9:
16361       return process_sparc_specific (file);
16362       break;
16363     case EM_TI_C6000:
16364       return process_tic6x_specific (file);
16365       break;
16366     case EM_MSP430:
16367       return process_msp430x_specific (file);
16368     default:
16369       break;
16370     }
16371   return 1;
16372 }
16373 
16374 static int
get_file_header(FILE * file)16375 get_file_header (FILE * file)
16376 {
16377   /* Read in the identity array.  */
16378   if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
16379     return 0;
16380 
16381   /* Determine how to read the rest of the header.  */
16382   switch (elf_header.e_ident[EI_DATA])
16383     {
16384     default: /* fall through */
16385     case ELFDATANONE: /* fall through */
16386     case ELFDATA2LSB:
16387       byte_get = byte_get_little_endian;
16388       byte_put = byte_put_little_endian;
16389       break;
16390     case ELFDATA2MSB:
16391       byte_get = byte_get_big_endian;
16392       byte_put = byte_put_big_endian;
16393       break;
16394     }
16395 
16396   /* For now we only support 32 bit and 64 bit ELF files.  */
16397   is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
16398 
16399   /* Read in the rest of the header.  */
16400   if (is_32bit_elf)
16401     {
16402       Elf32_External_Ehdr ehdr32;
16403 
16404       if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
16405 	return 0;
16406 
16407       elf_header.e_type      = BYTE_GET (ehdr32.e_type);
16408       elf_header.e_machine   = BYTE_GET (ehdr32.e_machine);
16409       elf_header.e_version   = BYTE_GET (ehdr32.e_version);
16410       elf_header.e_entry     = BYTE_GET (ehdr32.e_entry);
16411       elf_header.e_phoff     = BYTE_GET (ehdr32.e_phoff);
16412       elf_header.e_shoff     = BYTE_GET (ehdr32.e_shoff);
16413       elf_header.e_flags     = BYTE_GET (ehdr32.e_flags);
16414       elf_header.e_ehsize    = BYTE_GET (ehdr32.e_ehsize);
16415       elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
16416       elf_header.e_phnum     = BYTE_GET (ehdr32.e_phnum);
16417       elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
16418       elf_header.e_shnum     = BYTE_GET (ehdr32.e_shnum);
16419       elf_header.e_shstrndx  = BYTE_GET (ehdr32.e_shstrndx);
16420     }
16421   else
16422     {
16423       Elf64_External_Ehdr ehdr64;
16424 
16425       /* If we have been compiled with sizeof (bfd_vma) == 4, then
16426 	 we will not be able to cope with the 64bit data found in
16427 	 64 ELF files.  Detect this now and abort before we start
16428 	 overwriting things.  */
16429       if (sizeof (bfd_vma) < 8)
16430 	{
16431 	  error (_("This instance of readelf has been built without support for a\n\
16432 64 bit data type and so it cannot read 64 bit ELF files.\n"));
16433 	  return 0;
16434 	}
16435 
16436       if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
16437 	return 0;
16438 
16439       elf_header.e_type      = BYTE_GET (ehdr64.e_type);
16440       elf_header.e_machine   = BYTE_GET (ehdr64.e_machine);
16441       elf_header.e_version   = BYTE_GET (ehdr64.e_version);
16442       elf_header.e_entry     = BYTE_GET (ehdr64.e_entry);
16443       elf_header.e_phoff     = BYTE_GET (ehdr64.e_phoff);
16444       elf_header.e_shoff     = BYTE_GET (ehdr64.e_shoff);
16445       elf_header.e_flags     = BYTE_GET (ehdr64.e_flags);
16446       elf_header.e_ehsize    = BYTE_GET (ehdr64.e_ehsize);
16447       elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
16448       elf_header.e_phnum     = BYTE_GET (ehdr64.e_phnum);
16449       elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
16450       elf_header.e_shnum     = BYTE_GET (ehdr64.e_shnum);
16451       elf_header.e_shstrndx  = BYTE_GET (ehdr64.e_shstrndx);
16452     }
16453 
16454   if (elf_header.e_shoff)
16455     {
16456       /* There may be some extensions in the first section header.  Don't
16457 	 bomb if we can't read it.  */
16458       if (is_32bit_elf)
16459 	get_32bit_section_headers (file, TRUE);
16460       else
16461 	get_64bit_section_headers (file, TRUE);
16462     }
16463 
16464   return 1;
16465 }
16466 
16467 /* Process one ELF object file according to the command line options.
16468    This file may actually be stored in an archive.  The file is
16469    positioned at the start of the ELF object.  */
16470 
16471 static int
process_object(char * file_name,FILE * file)16472 process_object (char * file_name, FILE * file)
16473 {
16474   unsigned int i;
16475 
16476   if (! get_file_header (file))
16477     {
16478       error (_("%s: Failed to read file header\n"), file_name);
16479       return 1;
16480     }
16481 
16482   /* Initialise per file variables.  */
16483   for (i = ARRAY_SIZE (version_info); i--;)
16484     version_info[i] = 0;
16485 
16486   for (i = ARRAY_SIZE (dynamic_info); i--;)
16487     dynamic_info[i] = 0;
16488   dynamic_info_DT_GNU_HASH = 0;
16489 
16490   /* Process the file.  */
16491   if (show_name)
16492     printf (_("\nFile: %s\n"), file_name);
16493 
16494   /* Initialise the dump_sects array from the cmdline_dump_sects array.
16495      Note we do this even if cmdline_dump_sects is empty because we
16496      must make sure that the dump_sets array is zeroed out before each
16497      object file is processed.  */
16498   if (num_dump_sects > num_cmdline_dump_sects)
16499     memset (dump_sects, 0, num_dump_sects * sizeof (* dump_sects));
16500 
16501   if (num_cmdline_dump_sects > 0)
16502     {
16503       if (num_dump_sects == 0)
16504 	/* A sneaky way of allocating the dump_sects array.  */
16505 	request_dump_bynumber (num_cmdline_dump_sects, 0);
16506 
16507       assert (num_dump_sects >= num_cmdline_dump_sects);
16508       memcpy (dump_sects, cmdline_dump_sects,
16509 	      num_cmdline_dump_sects * sizeof (* dump_sects));
16510     }
16511 
16512   if (! process_file_header ())
16513     return 1;
16514 
16515   if (! process_section_headers (file))
16516     {
16517       /* Without loaded section headers we cannot process lots of
16518 	 things.  */
16519       do_unwind = do_version = do_dump = do_arch = 0;
16520 
16521       if (! do_using_dynamic)
16522 	do_syms = do_dyn_syms = do_reloc = 0;
16523     }
16524 
16525   if (! process_section_groups (file))
16526     {
16527       /* Without loaded section groups we cannot process unwind.  */
16528       do_unwind = 0;
16529     }
16530 
16531   if (process_program_headers (file))
16532     process_dynamic_section (file);
16533 
16534   process_relocs (file);
16535 
16536   process_unwind (file);
16537 
16538   process_symbol_table (file);
16539 
16540   process_syminfo (file);
16541 
16542   process_version_sections (file);
16543 
16544   process_section_contents (file);
16545 
16546   process_notes (file);
16547 
16548   process_gnu_liblist (file);
16549 
16550   process_arch_specific (file);
16551 
16552   if (program_headers)
16553     {
16554       free (program_headers);
16555       program_headers = NULL;
16556     }
16557 
16558   if (section_headers)
16559     {
16560       free (section_headers);
16561       section_headers = NULL;
16562     }
16563 
16564   if (string_table)
16565     {
16566       free (string_table);
16567       string_table = NULL;
16568       string_table_length = 0;
16569     }
16570 
16571   if (dynamic_strings)
16572     {
16573       free (dynamic_strings);
16574       dynamic_strings = NULL;
16575       dynamic_strings_length = 0;
16576     }
16577 
16578   if (dynamic_symbols)
16579     {
16580       free (dynamic_symbols);
16581       dynamic_symbols = NULL;
16582       num_dynamic_syms = 0;
16583     }
16584 
16585   if (dynamic_syminfo)
16586     {
16587       free (dynamic_syminfo);
16588       dynamic_syminfo = NULL;
16589     }
16590 
16591   if (dynamic_section)
16592     {
16593       free (dynamic_section);
16594       dynamic_section = NULL;
16595     }
16596 
16597   if (section_headers_groups)
16598     {
16599       free (section_headers_groups);
16600       section_headers_groups = NULL;
16601     }
16602 
16603   if (section_groups)
16604     {
16605       struct group_list * g;
16606       struct group_list * next;
16607 
16608       for (i = 0; i < group_count; i++)
16609 	{
16610 	  for (g = section_groups [i].root; g != NULL; g = next)
16611 	    {
16612 	      next = g->next;
16613 	      free (g);
16614 	    }
16615 	}
16616 
16617       free (section_groups);
16618       section_groups = NULL;
16619     }
16620 
16621   free_debug_memory ();
16622 
16623   return 0;
16624 }
16625 
16626 /* Process an ELF archive.
16627    On entry the file is positioned just after the ARMAG string.  */
16628 
16629 static int
process_archive(char * file_name,FILE * file,bfd_boolean is_thin_archive)16630 process_archive (char * file_name, FILE * file, bfd_boolean is_thin_archive)
16631 {
16632   struct archive_info arch;
16633   struct archive_info nested_arch;
16634   size_t got;
16635   int ret;
16636 
16637   show_name = 1;
16638 
16639   /* The ARCH structure is used to hold information about this archive.  */
16640   arch.file_name = NULL;
16641   arch.file = NULL;
16642   arch.index_array = NULL;
16643   arch.sym_table = NULL;
16644   arch.longnames = NULL;
16645 
16646   /* The NESTED_ARCH structure is used as a single-item cache of information
16647      about a nested archive (when members of a thin archive reside within
16648      another regular archive file).  */
16649   nested_arch.file_name = NULL;
16650   nested_arch.file = NULL;
16651   nested_arch.index_array = NULL;
16652   nested_arch.sym_table = NULL;
16653   nested_arch.longnames = NULL;
16654 
16655   if (setup_archive (&arch, file_name, file, is_thin_archive, do_archive_index) != 0)
16656     {
16657       ret = 1;
16658       goto out;
16659     }
16660 
16661   if (do_archive_index)
16662     {
16663       if (arch.sym_table == NULL)
16664 	error (_("%s: unable to dump the index as none was found\n"), file_name);
16665       else
16666 	{
16667 	  unsigned long i, l;
16668 	  unsigned long current_pos;
16669 
16670 	  printf (_("Index of archive %s: (%lu entries, 0x%lx bytes in the symbol table)\n"),
16671 		  file_name, (unsigned long) arch.index_num, arch.sym_size);
16672 	  current_pos = ftell (file);
16673 
16674 	  for (i = l = 0; i < arch.index_num; i++)
16675 	    {
16676 	      if ((i == 0) || ((i > 0) && (arch.index_array[i] != arch.index_array[i - 1])))
16677 	        {
16678 	          char * member_name;
16679 
16680 		  member_name = get_archive_member_name_at (&arch, arch.index_array[i], &nested_arch);
16681 
16682                   if (member_name != NULL)
16683                     {
16684 	              char * qualified_name = make_qualified_name (&arch, &nested_arch, member_name);
16685 
16686                       if (qualified_name != NULL)
16687                         {
16688 		          printf (_("Contents of binary %s at offset "), qualified_name);
16689 			  (void) print_vma (arch.index_array[i], PREFIX_HEX);
16690 			  putchar ('\n');
16691 		          free (qualified_name);
16692 		        }
16693 		    }
16694 		}
16695 
16696 	      if (l >= arch.sym_size)
16697 		{
16698 		  error (_("%s: end of the symbol table reached before the end of the index\n"),
16699 			 file_name);
16700 		  break;
16701 		}
16702 	      /* PR 17531: file: 0b6630b2.  */
16703 	      printf ("\t%.*s\n", (int) (arch.sym_size - l), arch.sym_table + l);
16704 	      l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
16705 	    }
16706 
16707 	  if (arch.uses_64bit_indicies)
16708 	    l = (l + 7) & ~ 7;
16709 	  else
16710 	    l += l & 1;
16711 
16712 	  if (l < arch.sym_size)
16713 	    error (_("%s: %ld bytes remain in the symbol table, but without corresponding entries in the index table\n"),
16714 		   file_name, arch.sym_size - l);
16715 
16716 	  if (fseek (file, current_pos, SEEK_SET) != 0)
16717 	    {
16718 	      error (_("%s: failed to seek back to start of object files in the archive\n"), file_name);
16719 	      ret = 1;
16720 	      goto out;
16721 	    }
16722 	}
16723 
16724       if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
16725 	  && !do_segments && !do_header && !do_dump && !do_version
16726 	  && !do_histogram && !do_debugging && !do_arch && !do_notes
16727 	  && !do_section_groups && !do_dyn_syms)
16728 	{
16729 	  ret = 0; /* Archive index only.  */
16730 	  goto out;
16731 	}
16732     }
16733 
16734   ret = 0;
16735 
16736   while (1)
16737     {
16738       char * name;
16739       size_t namelen;
16740       char * qualified_name;
16741 
16742       /* Read the next archive header.  */
16743       if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
16744         {
16745           error (_("%s: failed to seek to next archive header\n"), file_name);
16746           return 1;
16747         }
16748       got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
16749       if (got != sizeof arch.arhdr)
16750         {
16751           if (got == 0)
16752 	    break;
16753           error (_("%s: failed to read archive header\n"), file_name);
16754           ret = 1;
16755           break;
16756         }
16757       if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
16758         {
16759           error (_("%s: did not find a valid archive header\n"), arch.file_name);
16760           ret = 1;
16761           break;
16762         }
16763 
16764       arch.next_arhdr_offset += sizeof arch.arhdr;
16765 
16766       archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
16767       if (archive_file_size & 01)
16768         ++archive_file_size;
16769 
16770       name = get_archive_member_name (&arch, &nested_arch);
16771       if (name == NULL)
16772 	{
16773 	  error (_("%s: bad archive file name\n"), file_name);
16774 	  ret = 1;
16775 	  break;
16776 	}
16777       namelen = strlen (name);
16778 
16779       qualified_name = make_qualified_name (&arch, &nested_arch, name);
16780       if (qualified_name == NULL)
16781 	{
16782 	  error (_("%s: bad archive file name\n"), file_name);
16783 	  ret = 1;
16784 	  break;
16785 	}
16786 
16787       if (is_thin_archive && arch.nested_member_origin == 0)
16788         {
16789           /* This is a proxy for an external member of a thin archive.  */
16790           FILE * member_file;
16791           char * member_file_name = adjust_relative_path (file_name, name, namelen);
16792           if (member_file_name == NULL)
16793             {
16794               ret = 1;
16795               break;
16796             }
16797 
16798           member_file = fopen (member_file_name, "rb");
16799           if (member_file == NULL)
16800             {
16801               error (_("Input file '%s' is not readable.\n"), member_file_name);
16802               free (member_file_name);
16803               ret = 1;
16804               break;
16805             }
16806 
16807           archive_file_offset = arch.nested_member_origin;
16808 
16809           ret |= process_object (qualified_name, member_file);
16810 
16811           fclose (member_file);
16812           free (member_file_name);
16813         }
16814       else if (is_thin_archive)
16815         {
16816 	  /* PR 15140: Allow for corrupt thin archives.  */
16817 	  if (nested_arch.file == NULL)
16818 	    {
16819 	      error (_("%s: contains corrupt thin archive: %s\n"),
16820 		     file_name, name);
16821 	      ret = 1;
16822 	      break;
16823 	    }
16824 
16825           /* This is a proxy for a member of a nested archive.  */
16826           archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
16827 
16828           /* The nested archive file will have been opened and setup by
16829              get_archive_member_name.  */
16830           if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
16831             {
16832               error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
16833               ret = 1;
16834               break;
16835             }
16836 
16837           ret |= process_object (qualified_name, nested_arch.file);
16838         }
16839       else
16840         {
16841           archive_file_offset = arch.next_arhdr_offset;
16842           arch.next_arhdr_offset += archive_file_size;
16843 
16844           ret |= process_object (qualified_name, file);
16845         }
16846 
16847       if (dump_sects != NULL)
16848 	{
16849 	  free (dump_sects);
16850 	  dump_sects = NULL;
16851 	  num_dump_sects = 0;
16852 	}
16853 
16854       free (qualified_name);
16855     }
16856 
16857  out:
16858   if (nested_arch.file != NULL)
16859     fclose (nested_arch.file);
16860   release_archive (&nested_arch);
16861   release_archive (&arch);
16862 
16863   return ret;
16864 }
16865 
16866 static int
process_file(char * file_name)16867 process_file (char * file_name)
16868 {
16869   FILE * file;
16870   struct stat statbuf;
16871   char armag[SARMAG];
16872   int ret;
16873 
16874   if (stat (file_name, &statbuf) < 0)
16875     {
16876       if (errno == ENOENT)
16877 	error (_("'%s': No such file\n"), file_name);
16878       else
16879 	error (_("Could not locate '%s'.  System error message: %s\n"),
16880 	       file_name, strerror (errno));
16881       return 1;
16882     }
16883 
16884   if (! S_ISREG (statbuf.st_mode))
16885     {
16886       error (_("'%s' is not an ordinary file\n"), file_name);
16887       return 1;
16888     }
16889 
16890   file = fopen (file_name, "rb");
16891   if (file == NULL)
16892     {
16893       error (_("Input file '%s' is not readable.\n"), file_name);
16894       return 1;
16895     }
16896 
16897   if (fread (armag, SARMAG, 1, file) != 1)
16898     {
16899       error (_("%s: Failed to read file's magic number\n"), file_name);
16900       fclose (file);
16901       return 1;
16902     }
16903 
16904   current_file_size = (bfd_size_type) statbuf.st_size;
16905 
16906   if (memcmp (armag, ARMAG, SARMAG) == 0)
16907     ret = process_archive (file_name, file, FALSE);
16908   else if (memcmp (armag, ARMAGT, SARMAG) == 0)
16909     ret = process_archive (file_name, file, TRUE);
16910   else
16911     {
16912       if (do_archive_index)
16913 	error (_("File %s is not an archive so its index cannot be displayed.\n"),
16914 	       file_name);
16915 
16916       rewind (file);
16917       archive_file_size = archive_file_offset = 0;
16918       ret = process_object (file_name, file);
16919     }
16920 
16921   fclose (file);
16922 
16923   current_file_size = 0;
16924   return ret;
16925 }
16926 
16927 #ifdef SUPPORT_DISASSEMBLY
16928 /* Needed by the i386 disassembler.  For extra credit, someone could
16929    fix this so that we insert symbolic addresses here, esp for GOT/PLT
16930    symbols.  */
16931 
16932 void
print_address(unsigned int addr,FILE * outfile)16933 print_address (unsigned int addr, FILE * outfile)
16934 {
16935   fprintf (outfile,"0x%8.8x", addr);
16936 }
16937 
16938 /* Needed by the i386 disassembler.  */
16939 void
db_task_printsym(unsigned int addr)16940 db_task_printsym (unsigned int addr)
16941 {
16942   print_address (addr, stderr);
16943 }
16944 #endif
16945 
16946 int
main(int argc,char ** argv)16947 main (int argc, char ** argv)
16948 {
16949   int err;
16950 
16951 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
16952   setlocale (LC_MESSAGES, "");
16953 #endif
16954 #if defined (HAVE_SETLOCALE)
16955   setlocale (LC_CTYPE, "");
16956 #endif
16957   bindtextdomain (PACKAGE, LOCALEDIR);
16958   textdomain (PACKAGE);
16959 
16960   expandargv (&argc, &argv);
16961 
16962   parse_args (argc, argv);
16963 
16964   if (num_dump_sects > 0)
16965     {
16966       /* Make a copy of the dump_sects array.  */
16967       cmdline_dump_sects = (dump_type *)
16968           malloc (num_dump_sects * sizeof (* dump_sects));
16969       if (cmdline_dump_sects == NULL)
16970 	error (_("Out of memory allocating dump request table.\n"));
16971       else
16972 	{
16973 	  memcpy (cmdline_dump_sects, dump_sects,
16974 		  num_dump_sects * sizeof (* dump_sects));
16975 	  num_cmdline_dump_sects = num_dump_sects;
16976 	}
16977     }
16978 
16979   if (optind < (argc - 1))
16980     show_name = 1;
16981   else if (optind >= argc)
16982     {
16983       warn (_("Nothing to do.\n"));
16984       usage (stderr);
16985     }
16986 
16987   err = 0;
16988   while (optind < argc)
16989     err |= process_file (argv[optind++]);
16990 
16991   if (dump_sects != NULL)
16992     free (dump_sects);
16993   if (cmdline_dump_sects != NULL)
16994     free (cmdline_dump_sects);
16995 
16996   return err;
16997 }
16998